| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " Vim indent file | 
					
						
							|  |  |  | " Language:	Vim script | 
					
						
							|  |  |  | " Maintainer:	Bram Moolenaar <Bram@vim.org> | 
					
						
							| 
									
										
										
										
											2019-11-02 14:09:23 +01:00
										 |  |  | " Last Change:	2019 Oct 31 | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							| 
									
										
										
										
											2020-01-26 15:56:19 +01:00
										 |  |  | setlocal indentkeys+==end,=},=else,=cat,=fina,=END,0\\,0=\"\\\  | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   " Find a non-blank line above the current line. | 
					
						
							|  |  |  |   let lnum = prevnonblank(v:lnum - 1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  |   let cur_text = getline(v:lnum) | 
					
						
							| 
									
										
										
										
											2018-09-11 22:37:29 +02:00
										 |  |  |   if cur_text !~ s:lineContPat | 
					
						
							|  |  |  |     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 | 
					
						
							| 
									
										
										
										
											2014-09-19 22:38:48 +02:00
										 |  |  |   let prev_text = getline(lnum) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc' | 
					
						
							|  |  |  | 	  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 | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							|  |  |  |     if prev_text !~ '^\s*au\%[tocmd]' | 
					
						
							| 
									
										
										
										
											2020-01-26 15:56:19 +01:00
										 |  |  |       let i = match(prev_text, '\(^\||\)\s*\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\%[lly]\|fu\%[nction]\|def\|el\%[seif]\)\>\)') | 
					
						
							| 
									
										
										
										
											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') | 
					
						
							|  |  |  | 	      \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$' | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry, | 
					
						
							| 
									
										
										
										
											2020-01-26 15:56:19 +01:00
										 |  |  |   " :endfun, :enddef, :else and :augroup END. | 
					
						
							|  |  |  |   if cur_text =~ '^\s*\(ene\@!\|}\|cat\|fina\|el\|aug\%[roup]\s\+[eE][nN][dD]\)' | 
					
						
							| 
									
										
										
										
											2016-01-24 17:56:50 +01:00
										 |  |  |     let ind = ind - shiftwidth() | 
					
						
							| 
									
										
										
										
											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 |