vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

markdown.vim (3086B)


      1 " Vim filetype plugin
      2 " Language:     Markdown
      3 " Maintainer:   Tim Pope <https://github.com/tpope/vim-markdown>
      4 " Last Change:  2023 Dec 28
      5 "               2024 May 24 by Riley Bruins <ribru17@gmail.com> ('commentstring')
      6 "               2026 Jun 27 by Vim Project (normalize recommended style guard)
      7 
      8 if exists("b:did_ftplugin")
      9   finish
     10 endif
     11 
     12 runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
     13 
     14 let s:keepcpo= &cpo
     15 set cpo&vim
     16 
     17 setlocal comments=fb:*,fb:-,fb:+,n:> commentstring=<!--\ %s\ -->
     18 setlocal formatoptions+=tcqln formatoptions-=r formatoptions-=o
     19 setlocal formatlistpat=^\\s*\\d\\+\\.\\s\\+\\\|^\\s*[-*+]\\s\\+\\\|^\\[^\\ze[^\\]]\\+\\]:\\&^.\\{4\\}
     20 
     21 if exists('b:undo_ftplugin')
     22   let b:undo_ftplugin .= "|setl cms< com< fo< flp< et< ts< sts< sw<"
     23 else
     24   let b:undo_ftplugin = "setl cms< com< fo< flp< et< ts< sts< sw<"
     25 endif
     26 
     27 if get(g:, 'markdown_recommended_style',
     28       \ get(g:, 'filetype_recommended_style', 1))
     29   setlocal expandtab tabstop=4 softtabstop=4 shiftwidth=4
     30 endif
     31 
     32 if !exists("g:no_plugin_maps") && !exists("g:no_markdown_maps")
     33   nnoremap <silent><buffer> [[ :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
     34   nnoremap <silent><buffer> ]] :<C-U>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
     35   xnoremap <silent><buffer> [[ :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "bsW")<CR>
     36   xnoremap <silent><buffer> ]] :<C-U>exe "normal! gv"<Bar>call search('\%(^#\{1,5\}\s\+\S\\|^\S.*\n^[=-]\+$\)', "sW")<CR>
     37   let b:undo_ftplugin .= '|sil! nunmap <buffer> [[|sil! nunmap <buffer> ]]|sil! xunmap <buffer> [[|sil! xunmap <buffer> ]]'
     38 endif
     39 
     40 function! s:NotCodeBlock(lnum) abort
     41   return synIDattr(synID(a:lnum, 1, 1), 'name') !=# 'markdownCodeBlock'
     42 endfunction
     43 
     44 function! MarkdownFold() abort
     45   let line = getline(v:lnum)
     46 
     47   if line =~# '^#\+ ' && s:NotCodeBlock(v:lnum)
     48     return ">" . match(line, ' ')
     49   endif
     50 
     51   let nextline = getline(v:lnum + 1)
     52   if (line =~ '^.\+$') && (nextline =~ '^=\+$') && s:NotCodeBlock(v:lnum + 1)
     53     return ">1"
     54   endif
     55 
     56   if (line =~ '^.\+$') && (nextline =~ '^-\+$') && s:NotCodeBlock(v:lnum + 1)
     57     return ">2"
     58   endif
     59 
     60   return "="
     61 endfunction
     62 
     63 function! s:HashIndent(lnum) abort
     64   let hash_header = matchstr(getline(a:lnum), '^#\{1,6}')
     65   if len(hash_header)
     66     return hash_header
     67   else
     68     let nextline = getline(a:lnum + 1)
     69     if nextline =~# '^=\+\s*$'
     70       return '#'
     71     elseif nextline =~# '^-\+\s*$'
     72       return '##'
     73     endif
     74   endif
     75 endfunction
     76 
     77 function! MarkdownFoldText() abort
     78   let hash_indent = s:HashIndent(v:foldstart)
     79   let title = substitute(getline(v:foldstart), '^#\+\s*', '', '')
     80   let foldsize = (v:foldend - v:foldstart + 1)
     81   let linecount = '['.foldsize.' lines]'
     82   return hash_indent.' '.title.' '.linecount
     83 endfunction
     84 
     85 if has("folding") && get(g:, "markdown_folding", 0)
     86   setlocal foldexpr=MarkdownFold()
     87   setlocal foldmethod=expr
     88   setlocal foldtext=MarkdownFoldText()
     89   let b:undo_ftplugin .= "|setl foldexpr< foldmethod< foldtext<"
     90 endif
     91 
     92 let &cpo = s:keepcpo
     93 unlet s:keepcpo
     94 
     95 " vim:set sw=2: