blob: 8ebfa12cafc27baf498fc58e5ce9f879fd563c06 [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()
13setlocal indentkeys+==end,=else,=cat,=fina,=END,0\\
14
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
34function GetVimIndentIntern()
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 " Find a non-blank line above the current line.
36 let lnum = prevnonblank(v:lnum - 1)
37
38 " If the current line doesn't start with '\' and below a line that starts
39 " with '\', use the indent of the line above it.
Bram Moolenaar91e15e12014-09-19 22:38:48 +020040 let cur_text = getline(v:lnum)
41 if cur_text !~ '^\s*\\'
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 while lnum > 0 && getline(lnum) =~ '^\s*\\'
43 let lnum = lnum - 1
44 endwhile
45 endif
46
47 " At the start of the file use zero indent.
48 if lnum == 0
49 return 0
50 endif
Bram Moolenaar91e15e12014-09-19 22:38:48 +020051 let prev_text = getline(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53 " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
54 " and :else. Add it three times for a line that starts with '\' after
Bram Moolenaard4755bb2004-09-02 19:12:26 +000055 " a line that doesn't (or g:vim_indent_cont if it exists).
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 let ind = indent(lnum)
Bram Moolenaar91e15e12014-09-19 22:38:48 +020057 if cur_text =~ '^\s*\\' && v:lnum > 1 && prev_text !~ '^\s*\\'
Bram Moolenaard4755bb2004-09-02 19:12:26 +000058 if exists("g:vim_indent_cont")
59 let ind = ind + g:vim_indent_cont
60 else
Bram Moolenaar705ada12016-01-24 17:56:50 +010061 let ind = ind + shiftwidth() * 3
Bram Moolenaard4755bb2004-09-02 19:12:26 +000062 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020063 elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>'
Bram Moolenaar705ada12016-01-24 17:56:50 +010064 let ind = ind + shiftwidth()
Bram Moolenaarcab49df2011-03-22 17:40:10 +010065 else
Bram Moolenaar91e15e12014-09-19 22:38:48 +020066 " A line starting with :au does not increment/decrement indent.
67 if prev_text !~ '^\s*au\%[tocmd]'
68 let i = match(prev_text, '\(^\||\)\s*\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|el\%[seif]\)\>')
69 if i >= 0
Bram Moolenaar705ada12016-01-24 17:56:50 +010070 let ind += shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +020071 if strpart(prev_text, i, 1) == '|' && has('syntax_items')
72 \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +010073 let ind -= shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +020074 endif
Bram Moolenaarcab49df2011-03-22 17:40:10 +010075 endif
76 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 endif
78
79 " If the previous line contains an "end" after a pipe, but not in an ":au"
Bram Moolenaar520470a2005-06-16 21:59:56 +000080 " command. And not when there is a backslash before the pipe.
Bram Moolenaardc27ac12005-07-06 22:35:45 +000081 " And when syntax HL is enabled avoid a match inside a string.
Bram Moolenaar91e15e12014-09-19 22:38:48 +020082 let i = match(prev_text, '[^\\]|\s*\(ene\@!\)')
83 if i > 0 && prev_text !~ '^\s*au\%[tocmd]'
Bram Moolenaardc27ac12005-07-06 22:35:45 +000084 if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +010085 let ind = ind - shiftwidth()
Bram Moolenaardc27ac12005-07-06 22:35:45 +000086 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000087 endif
88
89
90 " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
91 " :endfun, :else and :augroup END.
Bram Moolenaare18dbe82016-07-02 21:42:23 +020092 if cur_text =~ '^\s*\(ene\@!\|cat\|fina\|el\|aug\%[roup]\s\+[eE][nN][dD]\)'
Bram Moolenaar705ada12016-01-24 17:56:50 +010093 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 endif
95
96 return ind
97endfunction
98
Bram Moolenaar8e52a592012-05-18 21:49:28 +020099let &cpo = s:keepcpo
100unlet s:keepcpo
101
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102" vim:sw=2