1
0
Fork 0
mirror of synced 2024-11-29 20:25:34 -05:00
ultimate-vim/sources_non_forked/vim-markdown/indent/markdown.vim

76 lines
2.2 KiB
VimL
Raw Normal View History

2022-08-08 09:45:56 -04:00
if exists('b:did_indent') | finish | endif
2018-02-04 06:35:08 -05:00
let b:did_indent = 1
setlocal indentexpr=GetMarkdownIndent()
setlocal nolisp
setlocal autoindent
" Automatically continue blockquote on line break
2018-07-30 17:18:16 -04:00
setlocal formatoptions+=r
setlocal comments=b:>
2022-08-08 09:45:56 -04:00
if get(g:, 'vim_markdown_auto_insert_bullets', 1)
2018-07-30 17:18:16 -04:00
" Do not automatically insert bullets when auto-wrapping with text-width
setlocal formatoptions-=c
" Accept various markers as bullets
setlocal comments+=b:*,b:+,b:-
endif
2018-02-04 06:35:08 -05:00
" Only define the function once
2022-08-08 09:45:56 -04:00
if exists('*GetMarkdownIndent') | finish | endif
2018-02-04 06:35:08 -05:00
function! s:IsMkdCode(lnum)
let name = synIDattr(synID(a:lnum, 1, 0), 'name')
2022-08-08 09:45:56 -04:00
return (name =~# '^mkd\%(Code$\|Snippet\)' || name !=# '' && name !~? '^\%(mkd\|html\)')
2018-02-04 06:35:08 -05:00
endfunction
function! s:IsLiStart(line)
2022-08-08 09:45:56 -04:00
return a:line !~# '^ *\([*-]\)\%( *\1\)\{2}\%( \|\1\)*$' &&
\ a:line =~# '^\s*[*+-] \+'
2018-02-04 06:35:08 -05:00
endfunction
function! s:IsHeaderLine(line)
2022-08-08 09:45:56 -04:00
return a:line =~# '^\s*#'
2018-02-04 06:35:08 -05:00
endfunction
function! s:IsBlankLine(line)
2022-08-08 09:45:56 -04:00
return a:line =~# '^$'
2018-02-04 06:35:08 -05:00
endfunction
function! s:PrevNonBlank(lnum)
let i = a:lnum
while i > 1 && s:IsBlankLine(getline(i))
let i -= 1
endwhile
return i
endfunction
function GetMarkdownIndent()
if v:lnum > 2 && s:IsBlankLine(getline(v:lnum - 1)) && s:IsBlankLine(getline(v:lnum - 2))
return 0
endif
2022-08-08 09:45:56 -04:00
let list_ind = get(g:, 'vim_markdown_new_list_item_indent', 4)
2018-02-04 06:35:08 -05:00
" Find a non-blank line above the current line.
let lnum = s:PrevNonBlank(v:lnum - 1)
" At the start of the file use zero indent.
if lnum == 0 | return 0 | endif
let ind = indent(lnum)
let line = getline(lnum) " Last line
let cline = getline(v:lnum) " Current line
if s:IsLiStart(cline)
" Current line is the first line of a list item, do not change indent
return indent(v:lnum)
elseif s:IsHeaderLine(cline) && !s:IsMkdCode(v:lnum)
" Current line is the header, do not indent
return 0
elseif s:IsLiStart(line)
if s:IsMkdCode(lnum)
return ind
else
" Last line is the first line of a list item, increase indent
return ind + list_ind
end
else
return ind
endif
endfunction