blob: 2ea16dbf84b7ebb039fb31db7b8cda9e0110490d [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Lua script
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003" Maintainer: Marcus Aurelius Farias <marcus.cf 'at' bol.com.br>
4" First Author: Max Ischenko <mfi 'at' ukr.net>
5" Last Change: 2004 Aug 29
Bram Moolenaar071d4272004-06-13 20:20:40 +00006
7" Only define the function once.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008if exists("*GetLuaIndent")
9 finish
10endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011
12setlocal indentexpr=GetLuaIndent()
13
14" To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until'
15" on the current line (else is default).
16setlocal indentkeys+=0=end,0=until
17
18setlocal autoindent
19
20function! GetLuaIndent()
21 " Find a non-blank line above the current line.
22 let lnum = prevnonblank(v:lnum - 1)
23
24 " Hit the start of the file, use zero indent.
25 if lnum == 0
26 return 0
27 endif
28
29 " Add a 'shiftwidth' after lines beginning with:
30 " function, if, for, while, repeat, else, elseif, '{'
31 let ind = indent(lnum)
32 let flag = 0
Bram Moolenaard4755bb2004-09-02 19:12:26 +000033 let prevline = getline(lnum)
34 if prevline =~ '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\)' || prevline =~ '{\s*$' || prevline =~ '\<function\>\s*\%(\k\|[.:]\)\{-}\s*('
35 let ind = ind + &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000036 let flag = 1
37 endif
38
39 " Subtract a 'shiftwidth' after lines ending with
40 " 'end' when they begin with while, if, for, etc.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000041 if flag == 1 && prevline =~ '\<end\>\|\<until\>'
42 let ind = ind - &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 endif
44
45 " Subtract a 'shiftwidth' on end, else (and elseif), until and '}'
46 " This is the part that requires 'indentkeys'.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000047 if getline(v:lnum) =~ '^\s*\%(end\|else\|until\|}\)'
48 let ind = ind - &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 endif
50
51 return ind
52endfunction