blob: ffe03be4a9a3f81eaf38a45df539e221a250e779 [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 Moolenaarf461c8e2005-06-25 23:04:51 +00005" Last Change: 2005 Jun 23
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006
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
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000013setlocal indentexpr=GetLuaIndent()
14
15" To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until'
16" on the current line ('else' is default and includes 'elseif').
17setlocal indentkeys+=0=end,0=until
18
19setlocal autoindent
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021" Only define the function once.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000022if exists("*GetLuaIndent")
23 finish
24endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026function! 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
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000035 " Add a 'shiftwidth' after lines that start a block:
36 " 'function', 'if', 'for', 'while', 'repeat', 'else', 'elseif', '{'
Bram Moolenaar071d4272004-06-13 20:20:40 +000037 let ind = indent(lnum)
38 let flag = 0
Bram Moolenaard4755bb2004-09-02 19:12:26 +000039 let prevline = getline(lnum)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000040 if prevline =~ '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>\)'
41 \ || prevline =~ '{\s*$' || prevline =~ '\<function\>\s*\%(\k\|[.:]\)\{-}\s*('
Bram Moolenaard4755bb2004-09-02 19:12:26 +000042 let ind = ind + &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000043 let flag = 1
44 endif
45
46 " Subtract a 'shiftwidth' after lines ending with
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000047 " 'end' when they begin with 'while', 'if', 'for', etc. too.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000048 if flag == 1 && prevline =~ '\<end\>\|\<until\>'
49 let ind = ind - &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000050 endif
51
52 " Subtract a 'shiftwidth' on end, else (and elseif), until and '}'
53 " This is the part that requires 'indentkeys'.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000054 if getline(v:lnum) =~ '^\s*\%(end\|else\|until\|}\)'
55 let ind = ind - &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 endif
57
58 return ind
59endfunction