blob: 3809943388c79438efddc4dc2a6ebad89380aef6 [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 Moolenaar2547aa92020-07-26 17:00:44 +02004" Last Change: 2020 Jul 19
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 Moolenaar8a7d6542020-01-26 15:56:19 +010013setlocal indentkeys+==end,=},=else,=cat,=fina,=END,0\\,0=\"\\\
Bram Moolenaar2547aa92020-07-26 17:00:44 +020014setlocal indentkeys-=0#
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
Bram Moolenaarc8734422012-06-01 22:38:45 +020016let b:undo_indent = "setl indentkeys< indentexpr<"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018" Only define the function once.
19if exists("*GetVimIndent")
20 finish
21endif
Bram Moolenaar8e52a592012-05-18 21:49:28 +020022let s:keepcpo= &cpo
23set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25function GetVimIndent()
Bram Moolenaar9b451252012-08-15 17:43:31 +020026 let ignorecase_save = &ignorecase
27 try
28 let &ignorecase = 0
29 return GetVimIndentIntern()
30 finally
31 let &ignorecase = ignorecase_save
32 endtry
33endfunc
34
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020035let s:lineContPat = '^\s*\(\\\|"\\ \)'
36
Bram Moolenaar9b451252012-08-15 17:43:31 +020037function GetVimIndentIntern()
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 " Find a non-blank line above the current line.
39 let lnum = prevnonblank(v:lnum - 1)
40
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020041 " If the current line doesn't start with '\' or '"\ ' and below a line that
42 " starts with '\' or '"\ ', use the indent of the line above it.
Bram Moolenaar91e15e12014-09-19 22:38:48 +020043 let cur_text = getline(v:lnum)
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020044 if cur_text !~ s:lineContPat
45 while lnum > 0 && getline(lnum) =~ s:lineContPat
Bram Moolenaar071d4272004-06-13 20:20:40 +000046 let lnum = lnum - 1
47 endwhile
48 endif
49
50 " At the start of the file use zero indent.
51 if lnum == 0
52 return 0
53 endif
Bram Moolenaar91e15e12014-09-19 22:38:48 +020054 let prev_text = getline(lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
56 " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020057 " and :else. Add it three times for a line that starts with '\' or '"\ '
58 " after a line that doesn't (or g:vim_indent_cont if it exists).
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 let ind = indent(lnum)
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010060
61 " In heredoc indenting works completely differently.
62 if has('syntax_items')
63 let syn_here = synIDattr(synID(v:lnum, 1, 1), "name")
64 if syn_here =~ 'vimLetHereDocStop'
65 " End of heredoc: use indent of matching start line
66 let lnum = v:lnum - 1
67 while lnum > 0
68 if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc'
69 return indent(lnum)
70 endif
71 let lnum -= 1
72 endwhile
73 return 0
74 endif
75 if syn_here =~ 'vimLetHereDoc'
76 if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc'
77 " First line in heredoc: increase indent
78 return ind + shiftwidth()
79 endif
80 " Heredoc continues: no change in indent
81 return ind
82 endif
83 endif
84
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020085 if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
Bram Moolenaard4755bb2004-09-02 19:12:26 +000086 if exists("g:vim_indent_cont")
87 let ind = ind + g:vim_indent_cont
88 else
Bram Moolenaar705ada12016-01-24 17:56:50 +010089 let ind = ind + shiftwidth() * 3
Bram Moolenaard4755bb2004-09-02 19:12:26 +000090 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020091 elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>'
Bram Moolenaar705ada12016-01-24 17:56:50 +010092 let ind = ind + shiftwidth()
Bram Moolenaarcab49df2011-03-22 17:40:10 +010093 else
Bram Moolenaar91e15e12014-09-19 22:38:48 +020094 " A line starting with :au does not increment/decrement indent.
95 if prev_text !~ '^\s*au\%[tocmd]'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010096 let i = match(prev_text, '\(^\||\)\s*\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|def\|el\%[seif]\)\>\)')
Bram Moolenaar91e15e12014-09-19 22:38:48 +020097 if i >= 0
Bram Moolenaar705ada12016-01-24 17:56:50 +010098 let ind += shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +020099 if strpart(prev_text, i, 1) == '|' && has('syntax_items')
100 \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +0100101 let ind -= shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200102 endif
Bram Moolenaarcab49df2011-03-22 17:40:10 +0100103 endif
104 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 endif
106
107 " If the previous line contains an "end" after a pipe, but not in an ":au"
Bram Moolenaar520470a2005-06-16 21:59:56 +0000108 " command. And not when there is a backslash before the pipe.
Bram Moolenaardc27ac12005-07-06 22:35:45 +0000109 " And when syntax HL is enabled avoid a match inside a string.
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200110 let i = match(prev_text, '[^\\]|\s*\(ene\@!\)')
111 if i > 0 && prev_text !~ '^\s*au\%[tocmd]'
Bram Moolenaardc27ac12005-07-06 22:35:45 +0000112 if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +0100113 let ind = ind - shiftwidth()
Bram Moolenaardc27ac12005-07-06 22:35:45 +0000114 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 endif
116
117
118 " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100119 " :endfun, :enddef, :else and :augroup END.
120 if cur_text =~ '^\s*\(ene\@!\|}\|cat\|fina\|el\|aug\%[roup]\s\+[eE][nN][dD]\)'
Bram Moolenaar705ada12016-01-24 17:56:50 +0100121 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 endif
123
124 return ind
125endfunction
126
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200127let &cpo = s:keepcpo
128unlet s:keepcpo
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130" vim:sw=2