| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " Vim indent file | 
					
						
							|  |  |  | " Language:	Vim script | 
					
						
							|  |  |  | " Maintainer:	Bram Moolenaar <Bram@vim.org> | 
					
						
							| 
									
										
										
										
											2022-06-24 11:48:03 +01:00
										 |  |  | " Last Change:	2022 Jun 24 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | " Only load this indent file when no other was loaded. | 
					
						
							|  |  |  | if exists("b:did_indent") | 
					
						
							|  |  |  |   finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | let b:did_indent = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setlocal indentexpr=GetVimIndent() | 
					
						
							| 
									
										
										
										
											2022-02-26 12:25:45 +00:00
										 |  |  | setlocal indentkeys+==endif,=enddef,=endfu,=endfor,=endwh,=endtry,=},=else,=cat,=finall,=END,0\\,0=\"\\\  | 
					
						
							| 
									
										
										
										
											2020-07-26 17:00:44 +02:00
										 |  |  | setlocal indentkeys-=0# | 
					
						
							| 
									
										
										
										
											2021-12-05 21:54:04 +00:00
										 |  |  | setlocal indentkeys-=: | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-01 22:38:45 +02:00
										 |  |  | let b:undo_indent = "setl indentkeys< indentexpr<" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " Only define the function once. | 
					
						
							|  |  |  | if exists("*GetVimIndent") | 
					
						
							|  |  |  |   finish | 
					
						
							|  |  |  | endif | 
					
						
							| 
									
										
										
										
											2012-05-18 21:49:28 +02:00
										 |  |  | let s:keepcpo= &cpo | 
					
						
							|  |  |  | set cpo&vim | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | function GetVimIndent() | 
					
						
							| 
									
										
										
										
											2012-08-15 17:43:31 +02:00
										 |  |  |   let ignorecase_save = &ignorecase | 
					
						
							|  |  |  |   try | 
					
						
							|  |  |  |     let &ignorecase = 0 | 
					
						
							|  |  |  |     return GetVimIndentIntern() | 
					
						
							|  |  |  |   finally | 
					
						
							|  |  |  |     let &ignorecase = ignorecase_save | 
					
						
							|  |  |  |   endtry | 
					
						
							|  |  |  | endfunc | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:37:29 +02:00
										 |  |  | let s:lineContPat = '^\s*\(\\\|"\\ \)' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-15 17:43:31 +02:00
										 |  |  | function GetVimIndentIntern() | 
					
						
							| 
									
										
										
										
											2022-06-24 11:48:03 +01:00
										 |  |  |   " If the current line has line continuation and the previous one too, use | 
					
						
							|  |  |  |   " the same indent.  This does not skip empty lines. | 
					
						
							|  |  |  |   let cur_text = getline(v:lnum) | 
					
						
							|  |  |  |   let cur_has_linecont = cur_text =~ s:lineContPat | 
					
						
							|  |  |  |   if cur_has_linecont && v:lnum > 1 && getline(v:lnum - 1) =~ s:lineContPat | 
					
						
							|  |  |  |     return indent(v:lnum - 1) | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   " Find a non-blank line above the current line. | 
					
						
							|  |  |  |   let lnum = prevnonblank(v:lnum - 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-25 21:14:57 +01:00
										 |  |  |   " The previous line, ignoring line continuation | 
					
						
							|  |  |  |   let prev_text_end = lnum > 0 ? getline(lnum) : '' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:37:29 +02:00
										 |  |  |   " If the current line doesn't start with '\' or '"\ ' and below a line that | 
					
						
							|  |  |  |   " starts with '\' or '"\ ', use the indent of the line above it. | 
					
						
							| 
									
										
										
										
											2022-06-24 11:48:03 +01:00
										 |  |  |   if !cur_has_linecont | 
					
						
							| 
									
										
										
										
											2018-09-11 22:37:29 +02:00
										 |  |  |     while lnum > 0 && getline(lnum) =~ s:lineContPat | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |       let lnum = lnum - 1 | 
					
						
							|  |  |  |     endwhile | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " At the start of the file use zero indent. | 
					
						
							|  |  |  |   if lnum == 0 | 
					
						
							|  |  |  |     return 0 | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2021-01-25 21:14:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   " the start of the previous line, skipping over line continuation | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  |   let prev_text = getline(lnum) | 
					
						
							| 
									
										
										
										
											2021-01-11 19:40:15 +01:00
										 |  |  |   let found_cont = 0 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function | 
					
						
							| 
									
										
										
										
											2018-09-11 22:37:29 +02:00
										 |  |  |   " and :else.  Add it three times for a line that starts with '\' or '"\ ' | 
					
						
							|  |  |  |   " after a line that doesn't (or g:vim_indent_cont if it exists). | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   let ind = indent(lnum) | 
					
						
							| 
									
										
										
										
											2019-11-02 14:09:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   " In heredoc indenting works completely differently. | 
					
						
							|  |  |  |   if has('syntax_items')  | 
					
						
							|  |  |  |     let syn_here = synIDattr(synID(v:lnum, 1, 1), "name") | 
					
						
							|  |  |  |     if syn_here =~ 'vimLetHereDocStop' | 
					
						
							|  |  |  |       " End of heredoc: use indent of matching start line | 
					
						
							|  |  |  |       let lnum = v:lnum - 1 | 
					
						
							|  |  |  |       while lnum > 0 | 
					
						
							| 
									
										
										
										
											2021-04-21 18:09:37 +02:00
										 |  |  | 	let attr = synIDattr(synID(lnum, 1, 1), "name") | 
					
						
							|  |  |  | 	if attr != '' && attr !~ 'vimLetHereDoc' | 
					
						
							| 
									
										
										
										
											2019-11-02 14:09:23 +01:00
										 |  |  | 	  return indent(lnum) | 
					
						
							|  |  |  | 	endif | 
					
						
							|  |  |  | 	let lnum -= 1 | 
					
						
							|  |  |  |       endwhile | 
					
						
							|  |  |  |       return 0 | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |     if syn_here =~ 'vimLetHereDoc' | 
					
						
							|  |  |  |       if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc' | 
					
						
							|  |  |  | 	" First line in heredoc: increase indent | 
					
						
							|  |  |  | 	return ind + shiftwidth() | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |       " Heredoc continues: no change in indent | 
					
						
							|  |  |  |       return ind | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-11 22:37:29 +02:00
										 |  |  |   if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat | 
					
						
							| 
									
										
										
										
											2021-01-11 19:40:15 +01:00
										 |  |  |     let found_cont = 1 | 
					
						
							| 
									
										
										
										
											2004-09-02 19:12:26 +00:00
										 |  |  |     if exists("g:vim_indent_cont") | 
					
						
							|  |  |  |       let ind = ind + g:vim_indent_cont | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2016-01-24 17:56:50 +01:00
										 |  |  |       let ind = ind + shiftwidth() * 3 | 
					
						
							| 
									
										
										
										
											2004-09-02 19:12:26 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2016-07-02 21:42:23 +02:00
										 |  |  |   elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>' | 
					
						
							| 
									
										
										
										
											2016-01-24 17:56:50 +01:00
										 |  |  |     let ind = ind + shiftwidth() | 
					
						
							| 
									
										
										
										
											2011-03-22 17:40:10 +01:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  |     " A line starting with :au does not increment/decrement indent. | 
					
						
							| 
									
										
										
										
											2021-02-13 18:14:48 +01:00
										 |  |  |     " A { may start a block or a dict.  Assume that when a } follows it's a | 
					
						
							|  |  |  |     " terminated dict. | 
					
						
							| 
									
										
										
										
											2022-02-26 12:25:45 +00:00
										 |  |  |     " ":function" starts a block but "function(" doesn't. | 
					
						
							| 
									
										
										
										
											2021-02-13 18:14:48 +01:00
										 |  |  |     if prev_text !~ '^\s*au\%[tocmd]' && prev_text !~ '^\s*{.*}' | 
					
						
							| 
									
										
										
										
											2022-03-08 21:35:07 +00:00
										 |  |  |       let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|def\|el\%[seif]\)\>\|fu\%[nction][! ]\)') | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  |       if i >= 0 | 
					
						
							| 
									
										
										
										
											2016-01-24 17:56:50 +01:00
										 |  |  | 	let ind += shiftwidth() | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  | 	if strpart(prev_text, i, 1) == '|' && has('syntax_items') | 
					
						
							| 
									
										
										
										
											2021-11-07 20:27:04 +00:00
										 |  |  | 	      \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\|PatSep\)$' | 
					
						
							| 
									
										
										
										
											2016-01-24 17:56:50 +01:00
										 |  |  | 	  let ind -= shiftwidth() | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  | 	endif | 
					
						
							| 
									
										
										
										
											2011-03-22 17:40:10 +01:00
										 |  |  |       endif | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " If the previous line contains an "end" after a pipe, but not in an ":au" | 
					
						
							| 
									
										
										
										
											2005-06-16 21:59:56 +00:00
										 |  |  |   " command.  And not when there is a backslash before the pipe. | 
					
						
							| 
									
										
										
										
											2005-07-06 22:35:45 +00:00
										 |  |  |   " And when syntax HL is enabled avoid a match inside a string. | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  |   let i = match(prev_text, '[^\\]|\s*\(ene\@!\)') | 
					
						
							|  |  |  |   if i > 0 && prev_text !~ '^\s*au\%[tocmd]' | 
					
						
							| 
									
										
										
										
											2005-07-06 22:35:45 +00:00
										 |  |  |     if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$' | 
					
						
							| 
									
										
										
										
											2016-01-24 17:56:50 +01:00
										 |  |  |       let ind = ind - shiftwidth() | 
					
						
							| 
									
										
										
										
											2005-07-06 22:35:45 +00:00
										 |  |  |     endif | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-11 19:40:15 +01:00
										 |  |  |   " For a line starting with "}" find the matching "{".  If it is at the start | 
					
						
							|  |  |  |   " of the line align with it, probably end of a block. | 
					
						
							|  |  |  |   " Use the mapped "%" from matchit to find the match, otherwise we may match | 
					
						
							|  |  |  |   " a { inside a comment or string. | 
					
						
							|  |  |  |   if cur_text =~ '^\s*}' | 
					
						
							|  |  |  |     if maparg('%') != '' | 
					
						
							|  |  |  |       exe v:lnum | 
					
						
							|  |  |  |       silent! normal % | 
					
						
							|  |  |  |       if line('.') < v:lnum && getline('.') =~ '^\s*{' | 
					
						
							|  |  |  | 	let ind = indent('.') | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       " todo: use searchpair() to find a match | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " Below a line starting with "}" find the matching "{".  If it is at the | 
					
						
							|  |  |  |   " end of the line we must be below the end of a dictionary. | 
					
						
							|  |  |  |   if prev_text =~ '^\s*}' | 
					
						
							|  |  |  |     if maparg('%') != '' | 
					
						
							|  |  |  |       exe lnum | 
					
						
							|  |  |  |       silent! normal % | 
					
						
							|  |  |  |       if line('.') == lnum || getline('.') !~ '^\s*{' | 
					
						
							|  |  |  | 	let ind = ind - shiftwidth() | 
					
						
							|  |  |  |       endif | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       " todo: use searchpair() to find a match | 
					
						
							|  |  |  |     endif | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " Below a line starting with "]" we must be below the end of a list. | 
					
						
							| 
									
										
										
										
											2021-02-13 18:14:48 +01:00
										 |  |  |   " Include a "}" and "},} in case a dictionary ends too. | 
					
						
							|  |  |  |   if prev_text_end =~ '^\s*\(},\=\s*\)\=]' | 
					
						
							| 
									
										
										
										
											2021-01-11 19:40:15 +01:00
										 |  |  |     let ind = ind - shiftwidth() | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-13 18:14:48 +01:00
										 |  |  |   let ends_in_comment = has('syntax_items') | 
					
						
							| 
									
										
										
										
											2021-02-27 16:38:07 +01:00
										 |  |  | 	\ && synIDattr(synID(lnum, len(getline(lnum)), 1), "name") =~ '\(Comment\|String\)$' | 
					
						
							| 
									
										
										
										
											2021-02-13 18:14:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 16:38:07 +01:00
										 |  |  |   " A line ending in "{" or "[" is most likely the start of a dict/list literal, | 
					
						
							| 
									
										
										
										
											2021-02-13 18:14:48 +01:00
										 |  |  |   " indent the next line more.  Not for a continuation line or {{{. | 
					
						
							|  |  |  |   if !ends_in_comment && prev_text_end =~ '\s[{[]\s*$' && !found_cont | 
					
						
							| 
									
										
										
										
											2021-01-11 19:40:15 +01:00
										 |  |  |     let ind = ind + shiftwidth() | 
					
						
							|  |  |  |   endif | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-26 12:25:45 +00:00
										 |  |  |   " Subtract a 'shiftwidth' on a :endif, :endwhile, :endfor, :catch, :finally, | 
					
						
							|  |  |  |   " :endtry, :endfun, :enddef, :else and :augroup END. | 
					
						
							|  |  |  |   " Although ":en" would be enough only match short command names as in | 
					
						
							|  |  |  |   " 'indentkeys'. | 
					
						
							|  |  |  |   if cur_text =~ '^\s*\(endif\|endwh\|endfor\|endtry\|endfu\|enddef\|cat\|finall\|else\|aug\%[roup]\s\+[eE][nN][dD]\)' | 
					
						
							| 
									
										
										
										
											2016-01-24 17:56:50 +01:00
										 |  |  |     let ind = ind - shiftwidth() | 
					
						
							| 
									
										
										
										
											2022-02-26 12:25:45 +00:00
										 |  |  |     if ind < 0 | 
					
						
							|  |  |  |       let ind = 0 | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ind | 
					
						
							|  |  |  | endfunction | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-18 21:49:28 +02:00
										 |  |  | let &cpo = s:keepcpo | 
					
						
							|  |  |  | unlet s:keepcpo | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " vim:sw=2 |