blob: d1d2c0d600fee30ded74bba6e532424fe039b70d [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 Moolenaaracb4f222016-01-10 15:59:26 +01005" Last Change: 2016 Jan 10
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.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000028 let prevlnum = prevnonblank(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
30 " Hit the start of the file, use zero indent.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000031 if prevlnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 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 Moolenaar3577c6f2008-06-24 21:16:56 +000037 let ind = indent(prevlnum)
38 let prevline = getline(prevlnum)
39 let midx = match(prevline, '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>\)')
40 if midx == -1
41 let midx = match(prevline, '{\s*$')
42 if midx == -1
43 let midx = match(prevline, '\<function\>\s*\%(\k\|[.:]\)\{-}\s*(')
44 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 endif
46
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000047 if midx != -1
48 " Add 'shiftwidth' if what we found previously is not in a comment and
49 " an "end" or "until" is not present on the same line.
50 if synIDattr(synID(prevlnum, midx + 1, 1), "name") != "luaComment" && prevline !~ '\<end\>\|\<until\>'
51 let ind = ind + &shiftwidth
52 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000053 endif
54
Bram Moolenaaracb4f222016-01-10 15:59:26 +010055 " Subtract a 'shiftwidth' on end, else, elseif, until and '}'
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 " This is the part that requires 'indentkeys'.
Bram Moolenaaracb4f222016-01-10 15:59:26 +010057 let midx = match(getline(v:lnum), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\)')
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000058 if midx != -1 && synIDattr(synID(v:lnum, midx + 1, 1), "name") != "luaComment"
Bram Moolenaard4755bb2004-09-02 19:12:26 +000059 let ind = ind - &shiftwidth
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 endif
61
62 return ind
63endfunction