blob: cd735c3a3c89b271120bc15c3482120337458674 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Vim script
3" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaare18dbe82016-07-02 21:42:23 +02004" Last Change: 2016 Jun 27
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
6" Only load this indent file when no other was loaded.
7if exists("b:did_indent")
8 finish
9endif
10let b:did_indent = 1
11
12setlocal indentexpr=GetVimIndent()
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020013setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\,0=\"\\\
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
Bram Moolenaarc8734422012-06-01 22:38:45 +020015let b:undo_indent = "setl indentkeys< indentexpr<"
16
Bram Moolenaar071d4272004-06-13 20:20:40 +000017" Only define the function once.
18if exists("*GetVimIndent")
19 finish
20endif
Bram Moolenaar8e52a592012-05-18 21:49:28 +020021let s:keepcpo= &cpo
22set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000023
24function GetVimIndent()
Bram Moolenaar9b451252012-08-15 17:43:31 +020025 let ignorecase_save = &ignorecase
26 try
27 let &ignorecase = 0
28 return GetVimIndentIntern()
29 finally
30 let &ignorecase = ignorecase_save
31 endtry
32endfunc
33
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020034let s:lineContPat = '^\s*\(\\\|"\\ \)'
35
Bram Moolenaar9b451252012-08-15 17:43:31 +020036function GetVimIndentIntern()
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 " Find a non-blank line above the current line.
38 let lnum = prevnonblank(v:lnum - 1)
39
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020040 " If the current line doesn't start with '\' or '"\ ' and below a line that
41 " starts with '\' or '"\ ', use the indent of the line above it.
Bram Moolenaar91e15e12014-09-19 22:38:48 +020042 let cur_text = getline(v:lnum)
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020043 if cur_text !~ s:lineContPat
44 while lnum > 0 && getline(lnum) =~ s:lineContPat
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 let lnum = lnum - 1
46 endwhile
47 endif
48
49 " At the start of the file use zero indent.
50 if lnum == 0
51 return 0
52 endif
Bram Moolenaar91e15e12014-09-19 22:38:48 +020053 let prev_text = getline(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55 " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020056 " and :else. Add it three times for a line that starts with '\' or '"\ '
57 " after a line that doesn't (or g:vim_indent_cont if it exists).
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 let ind = indent(lnum)
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020059 if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
Bram Moolenaard4755bb2004-09-02 19:12:26 +000060 if exists("g:vim_indent_cont")
61 let ind = ind + g:vim_indent_cont
62 else
Bram Moolenaar705ada12016-01-24 17:56:50 +010063 let ind = ind + shiftwidth() * 3
Bram Moolenaard4755bb2004-09-02 19:12:26 +000064 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020065 elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>'
Bram Moolenaar705ada12016-01-24 17:56:50 +010066 let ind = ind + shiftwidth()
Bram Moolenaarcab49df2011-03-22 17:40:10 +010067 else
Bram Moolenaar91e15e12014-09-19 22:38:48 +020068 " A line starting with :au does not increment/decrement indent.
69 if prev_text !~ '^\s*au\%[tocmd]'
70 let i = match(prev_text, '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>')
71 if i >= 0
Bram Moolenaar705ada12016-01-24 17:56:50 +010072 let ind += shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +020073 if strpart(prev_text, i, 1) == '|' && has('syntax_items')
74 \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +010075 let ind -= shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +020076 endif
Bram Moolenaarcab49df2011-03-22 17:40:10 +010077 endif
78 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 endif
80
81 " If the previous line contains an "end" after a pipe, but not in an ":au"
Bram Moolenaar520470a2005-06-16 21:59:56 +000082 " command. And not when there is a backslash before the pipe.
Bram Moolenaardc27ac12005-07-06 22:35:45 +000083 " And when syntax HL is enabled avoid a match inside a string.
Bram Moolenaar91e15e12014-09-19 22:38:48 +020084 let i = match(prev_text, '[^\\]|\s*\(ene\@!\)')
85 if i > 0 && prev_text !~ '^\s*au\%[tocmd]'
Bram Moolenaardc27ac12005-07-06 22:35:45 +000086 if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +010087 let ind = ind - shiftwidth()
Bram Moolenaardc27ac12005-07-06 22:35:45 +000088 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000089 endif
90
91
92 " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
93 " :endfun, :else and :augroup END.
Bram Moolenaare18dbe82016-07-02 21:42:23 +020094 if cur_text =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s\+[eE][nN][dD]\)'
Bram Moolenaar705ada12016-01-24 17:56:50 +010095 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 endif
97
98 return ind
99endfunction
100
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200101let &cpo = s:keepcpo
102unlet s:keepcpo
103
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104" vim:sw=2