forked from gisphm/vim-gitignore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitignore.vim
56 lines (47 loc) · 1.41 KB
/
gitignore.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
" Vim indent file
" Language: .gitignore
" Maintainer: gisphm <[email protected]>
" URL: https://github.com/gisphm/vim-gitignore
if exists("b:did_indent") | finish | endif
let b:did_indent = 1
let s:save_cpo = &cpo
set cpo&vim
setlocal indentexpr=GetGitignoreIndent()
setlocal nolisp
setlocal noautoindent
" Only define the function once
if exists("*GetGitignoreIndent") | finish | endif
function! s:is_li_start(line)
return a:line !~ '^ *\([*-]\)\%( *\1\)\{2}\%( \|\1\)*$' &&
\ a:line =~ '^\s*[*+-] \+'
endfunction
function! s:is_blank_line(line)
return a:line =~ '^$'
endfunction
function! s:prevnonblank(lnum)
let i = a:lnum
while i > 1 && s:is_blank_line(getline(i))
let i -= 1
endwhile
return i
endfunction
function GetGitignoreIndent()
let list_ind = 4
" Find a non-blank line above the current line.
let lnum = 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:is_li_start(cline)
" Current line is the first line of a list item, do not change indent
return indent(v:lnum)
elseif s:is_li_start(line)
" Last line is the first line of a list item, increase indent
return ind + list_ind
else
return ind
endif
endfunction
let &cpo = s:save_cpo