xref: /vim-8.2.3635/runtime/indent/nsis.vim (revision 079ba76a)
1" Vim indent file
2" Language:		NSIS script
3" Maintainer:		Ken Takata
4" URL:			https://github.com/k-takata/vim-nsis
5" Last Change:		2021-10-18
6" Filenames:		*.nsi
7" License:		VIM License
8
9if exists("b:did_indent")
10  finish
11endif
12let b:did_indent = 1
13
14setlocal nosmartindent
15setlocal noautoindent
16setlocal indentexpr=GetNsisIndent(v:lnum)
17setlocal indentkeys=!^F,o,O
18setlocal indentkeys+==~${Else,=~${EndIf,=~${EndUnless,=~${AndIf,=~${AndUnless,=~${OrIf,=~${OrUnless,=~${Case,=~${Default,=~${EndSelect,=~${EndSwith,=~${Loop,=~${Next,=~${MementoSectionEnd,=~FunctionEnd,=~SectionEnd,=~SectionGroupEnd,=~PageExEnd,0=~!macroend,0=~!if,0=~!else,0=~!endif
19
20let b:undo_indent = "setl ai< inde< indk< si<"
21
22if exists("*GetNsisIndent")
23  finish
24endif
25
26function! GetNsisIndent(lnum)
27  " If this line is explicitly joined: If the previous line was also joined,
28  " line it up with that one, otherwise add two 'shiftwidth'
29  if getline(a:lnum - 1) =~ '\\$'
30    if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
31      return indent(a:lnum - 1)
32    endif
33    return indent(a:lnum - 1) + shiftwidth() * 2
34  endif
35
36  " Grab the current line, stripping comments.
37  let l:thisl = substitute(getline(a:lnum), '[;#].*$', '', '')
38  " Check if this line is a conditional preprocessor line.
39  let l:preproc = l:thisl =~? '^\s*!\%(if\|else\|endif\)'
40
41  " Grab the previous line, stripping comments.
42  " Skip preprocessor lines and continued lines.
43  let l:prevlnum = a:lnum
44  while 1
45    let l:prevlnum = prevnonblank(l:prevlnum - 1)
46    if l:prevlnum == 0
47      " top of file
48      return 0
49    endif
50    let l:prevl = substitute(getline(l:prevlnum), '[;#].*$', '', '')
51    let l:prevpreproc = l:prevl =~? '^\s*!\%(if\|else\|endif\)'
52    if l:preproc == l:prevpreproc && getline(l:prevlnum - 1) !~? '\\$'
53      break
54    endif
55  endwhile
56  let l:previ = indent(l:prevlnum)
57  let l:ind = l:previ
58
59  if l:preproc
60    " conditional preprocessor
61    if l:prevl =~? '^\s*!\%(if\%(\%(macro\)\?n\?def\)\?\|else\)\>'
62      let l:ind += shiftwidth()
63    endif
64    if l:thisl =~? '^\s*!\%(else\|endif\)\?\>'
65      let l:ind -= shiftwidth()
66    endif
67    return l:ind
68  endif
69
70  if l:prevl =~? '^\s*\%(\${\%(If\|IfNot\|Unless\|ElseIf\|ElseIfNot\|ElseUnless\|Else\|AndIf\|AndIfNot\|AndUnless\|OrIf\|OrIfNot\|OrUnless\|Select\|Case\|Case[2-5]\|CaseElse\|Default\|Switch\|Do\|DoWhile\|DoUntil\|For\|ForEach\|MementoSection\)}\|Function\>\|Section\>\|SectionGroup\|PageEx\>\|!macro\>\)'
71    " previous line opened a block
72    let l:ind += shiftwidth()
73  endif
74  if l:thisl =~? '^\s*\%(\${\%(ElseIf\|ElseIfNot\|ElseUnless\|Else\|EndIf\|EndUnless\|AndIf\|AndIfNot\|AndUnless\|OrIf\|OrIfNot\|OrUnless\|Loop\|LoopWhile\|LoopUntil\|Next\|MementoSectionEnd\)\>}\?\|FunctionEnd\>\|SectionEnd\>\|SectionGroupEnd\|PageExEnd\>\|!macroend\>\)'
75    " this line closed a block
76    let l:ind -= shiftwidth()
77  elseif l:thisl =~? '^\s*\${\%(Case\|Case[2-5]\|CaseElse\|Default\)\>}\?'
78    if l:prevl !~? '^\s*\${\%(Select\|Switch\)}'
79      let l:ind -= shiftwidth()
80    endif
81  elseif l:thisl =~? '^\s*\${\%(EndSelect\|EndSwitch\)\>}\?'
82    " this line closed a block
83    if l:prevl =~? '^\s*\${\%(Select\|Switch\)}'
84      let l:ind -= shiftwidth()
85    else
86      let l:ind -= shiftwidth() * 2
87    endif
88  endif
89
90  return l:ind
91endfunction
92
93" vim: ts=8 sw=2 sts=2
94