blob: ace7fd1a7fae16a8ea7023110bec48e727c9d9b6 [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>
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005" Last Change: 2005 Jun 09
6
7" Only load this indent file when no other was loaded.
8if exists("b:did_indent")
9 finish
10endif
11let b:did_indent = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000012
13" Only define the function once.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000014if exists("*GetLuaIndent")
15 finish
16endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017
18setlocal indentexpr=GetLuaIndent()
19
20" To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until'
21" on the current line (else is default).
22setlocal indentkeys+=0=end,0=until
23
24setlocal autoindent
25
26function! GetLuaIndent()
27 " Find a non-blank line above the current line.
28 let lnum = prevnonblank(v:lnum - 1)
29
30 " Hit the start of the file, use zero indent.
31 if lnum == 0
32 return 0
33 endif
34
35 " Add a 'shiftwidth' after lines beginning with:
36 " function, if, for, while, repeat, else, elseif, '{'
37 let ind = indent(lnum)
38 let flag = 0
Bram Moolenaard4755bb2004-09-02 19:12:26 +000039 let prevline = getline(lnum)
40 if prevline =~ '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\)' || prevline =~ '{\s*$' || prevline =~ '\<function\>\s*\%(\k\|[.:]\)\{-}\s*('
41 let ind = ind + &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000042 let flag = 1
43 endif
44
45 " Subtract a 'shiftwidth' after lines ending with
46 " 'end' when they begin with while, if, for, etc.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000047 if flag == 1 && prevline =~ '\<end\>\|\<until\>'
48 let ind = ind - &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 endif
50
51 " Subtract a 'shiftwidth' on end, else (and elseif), until and '}'
52 " This is the part that requires 'indentkeys'.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000053 if getline(v:lnum) =~ '^\s*\%(end\|else\|until\|}\)'
54 let ind = ind - &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000055 endif
56
57 return ind
58endfunction