| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | " Vim indent file | 
					
						
							|  |  |  | " Language:	Lua script | 
					
						
							| 
									
										
										
										
											2004-09-02 19:12:26 +00:00
										 |  |  | " Maintainer:	Marcus Aurelius Farias <marcus.cf 'at' bol.com.br> | 
					
						
							|  |  |  | " First Author:	Max Ischenko <mfi 'at' ukr.net> | 
					
						
							| 
									
										
										
										
											2008-06-24 21:16:56 +00:00
										 |  |  | " Last Change:	2007 Jul 23 | 
					
						
							| 
									
										
										
										
											2005-06-13 22:28:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | " Only load this indent file when no other was loaded. | 
					
						
							|  |  |  | if exists("b:did_indent") | 
					
						
							|  |  |  |   finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | let b:did_indent = 1 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | setlocal indentexpr=GetLuaIndent() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | " To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until' | 
					
						
							| 
									
										
										
										
											2005-06-25 23:04:51 +00:00
										 |  |  | " on the current line ('else' is default and includes 'elseif'). | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | setlocal indentkeys+=0=end,0=until | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setlocal autoindent | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-25 23:04:51 +00:00
										 |  |  | " Only define the function once. | 
					
						
							|  |  |  | if exists("*GetLuaIndent") | 
					
						
							|  |  |  |   finish | 
					
						
							|  |  |  | endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | function! GetLuaIndent() | 
					
						
							|  |  |  |   " Find a non-blank line above the current line. | 
					
						
							| 
									
										
										
										
											2008-06-24 21:16:56 +00:00
										 |  |  |   let prevlnum = prevnonblank(v:lnum - 1) | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   " Hit the start of the file, use zero indent. | 
					
						
							| 
									
										
										
										
											2008-06-24 21:16:56 +00:00
										 |  |  |   if prevlnum == 0 | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |     return 0 | 
					
						
							|  |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-25 23:04:51 +00:00
										 |  |  |   " Add a 'shiftwidth' after lines that start a block: | 
					
						
							|  |  |  |   " 'function', 'if', 'for', 'while', 'repeat', 'else', 'elseif', '{' | 
					
						
							| 
									
										
										
										
											2008-06-24 21:16:56 +00:00
										 |  |  |   let ind = indent(prevlnum) | 
					
						
							|  |  |  |   let prevline = getline(prevlnum) | 
					
						
							|  |  |  |   let midx = match(prevline, '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>\)') | 
					
						
							|  |  |  |   if midx == -1 | 
					
						
							|  |  |  |     let midx = match(prevline, '{\s*$') | 
					
						
							|  |  |  |     if midx == -1 | 
					
						
							|  |  |  |       let midx = match(prevline, '\<function\>\s*\%(\k\|[.:]\)\{-}\s*(') | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-24 21:16:56 +00:00
										 |  |  |   if midx != -1 | 
					
						
							|  |  |  |     " Add 'shiftwidth' if what we found previously is not in a comment and | 
					
						
							|  |  |  |     " an "end" or "until" is not present on the same line. | 
					
						
							|  |  |  |     if synIDattr(synID(prevlnum, midx + 1, 1), "name") != "luaComment" && prevline !~ '\<end\>\|\<until\>' | 
					
						
							|  |  |  |       let ind = ind + &shiftwidth | 
					
						
							|  |  |  |     endif | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   " Subtract a 'shiftwidth' on end, else (and elseif), until and '}' | 
					
						
							|  |  |  |   " This is the part that requires 'indentkeys'. | 
					
						
							| 
									
										
										
										
											2008-06-24 21:16:56 +00:00
										 |  |  |   let midx = match(getline(v:lnum), '^\s*\%(end\|else\|until\|}\)') | 
					
						
							|  |  |  |   if midx != -1 && synIDattr(synID(v:lnum, midx + 1, 1), "name") != "luaComment" | 
					
						
							| 
									
										
										
										
											2004-09-02 19:12:26 +00:00
										 |  |  |     let ind = ind - &shiftwidth | 
					
						
							| 
									
										
										
										
											2004-06-13 20:20:40 +00:00
										 |  |  |   endif | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ind | 
					
						
							|  |  |  | endfunction |