blob: ff0f64be291e6822790d11c9a16f283d5d53851b [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 Moolenaar3ec574f2017-06-13 18:12:01 +02005" Last Change: 2017 Jun 13
Bram Moolenaar9712ff12022-09-18 13:04:22 +01006" 2022 Sep 07: b:undo_indent added by Doug Kearns
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007
8" Only load this indent file when no other was loaded.
9if exists("b:did_indent")
10 finish
11endif
12let b:did_indent = 1
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000014setlocal indentexpr=GetLuaIndent()
15
16" To make Vim call GetLuaIndent() when it finds '\s*end' or '\s*until'
17" on the current line ('else' is default and includes 'elseif').
18setlocal indentkeys+=0=end,0=until
19
20setlocal autoindent
21
Bram Moolenaar9712ff12022-09-18 13:04:22 +010022let b:undo_indent = "setlocal autoindent< indentexpr< indentkeys<"
23
Bram Moolenaar071d4272004-06-13 20:20:40 +000024" Only define the function once.
Bram Moolenaard4755bb2004-09-02 19:12:26 +000025if exists("*GetLuaIndent")
26 finish
27endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000028
Bram Moolenaar071d4272004-06-13 20:20:40 +000029function! GetLuaIndent()
30 " Find a non-blank line above the current line.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000031 let prevlnum = prevnonblank(v:lnum - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
33 " Hit the start of the file, use zero indent.
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000034 if prevlnum == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000035 return 0
36 endif
37
Bram Moolenaarf461c8e2005-06-25 23:04:51 +000038 " Add a 'shiftwidth' after lines that start a block:
39 " 'function', 'if', 'for', 'while', 'repeat', 'else', 'elseif', '{'
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000040 let ind = indent(prevlnum)
41 let prevline = getline(prevlnum)
42 let midx = match(prevline, '^\s*\%(if\>\|for\>\|while\>\|repeat\>\|else\>\|elseif\>\|do\>\|then\>\)')
43 if midx == -1
champignoom66336112023-08-21 02:49:16 +080044 let midx = match(prevline, '{\s*\%(--\%([^[].*\)\?\)\?$')
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000045 if midx == -1
46 let midx = match(prevline, '\<function\>\s*\%(\k\|[.:]\)\{-}\s*(')
47 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000048 endif
49
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000050 if midx != -1
51 " Add 'shiftwidth' if what we found previously is not in a comment and
52 " an "end" or "until" is not present on the same line.
53 if synIDattr(synID(prevlnum, midx + 1, 1), "name") != "luaComment" && prevline !~ '\<end\>\|\<until\>'
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020054 let ind = ind + shiftwidth()
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000055 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 endif
57
Bram Moolenaaracb4f222016-01-10 15:59:26 +010058 " Subtract a 'shiftwidth' on end, else, elseif, until and '}'
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 " This is the part that requires 'indentkeys'.
Bram Moolenaaracb4f222016-01-10 15:59:26 +010060 let midx = match(getline(v:lnum), '^\s*\%(end\>\|else\>\|elseif\>\|until\>\|}\)')
Bram Moolenaar3577c6f2008-06-24 21:16:56 +000061 if midx != -1 && synIDattr(synID(v:lnum, midx + 1, 1), "name") != "luaComment"
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020062 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +000063 endif
64
65 return ind
66endfunction