nsis.vim 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if exists("b:did_indent")
  9. finish
  10. endif
  11. let b:did_indent = 1
  12. setlocal nosmartindent
  13. setlocal noautoindent
  14. setlocal indentexpr=GetNsisIndent(v:lnum)
  15. setlocal indentkeys=!^F,o,O
  16. setlocal 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
  17. let b:undo_indent = "setl ai< inde< indk< si<"
  18. if exists("*GetNsisIndent")
  19. finish
  20. endif
  21. function! GetNsisIndent(lnum)
  22. " If this line is explicitly joined: If the previous line was also joined,
  23. " line it up with that one, otherwise add two 'shiftwidth'
  24. if getline(a:lnum - 1) =~ '\\$'
  25. if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
  26. return indent(a:lnum - 1)
  27. endif
  28. return indent(a:lnum - 1) + shiftwidth() * 2
  29. endif
  30. " Grab the current line, stripping comments.
  31. let l:thisl = substitute(getline(a:lnum), '[;#].*$', '', '')
  32. " Check if this line is a conditional preprocessor line.
  33. let l:preproc = l:thisl =~? '^\s*!\%(if\|else\|endif\)'
  34. " Grab the previous line, stripping comments.
  35. " Skip preprocessor lines and continued lines.
  36. let l:prevlnum = a:lnum
  37. while 1
  38. let l:prevlnum = prevnonblank(l:prevlnum - 1)
  39. if l:prevlnum == 0
  40. " top of file
  41. return 0
  42. endif
  43. let l:prevl = substitute(getline(l:prevlnum), '[;#].*$', '', '')
  44. let l:prevpreproc = l:prevl =~? '^\s*!\%(if\|else\|endif\)'
  45. if l:preproc == l:prevpreproc && getline(l:prevlnum - 1) !~? '\\$'
  46. break
  47. endif
  48. endwhile
  49. let l:previ = indent(l:prevlnum)
  50. let l:ind = l:previ
  51. if l:preproc
  52. " conditional preprocessor
  53. if l:prevl =~? '^\s*!\%(if\%(\%(macro\)\?n\?def\)\?\|else\)\>'
  54. let l:ind += shiftwidth()
  55. endif
  56. if l:thisl =~? '^\s*!\%(else\|endif\)\?\>'
  57. let l:ind -= shiftwidth()
  58. endif
  59. return l:ind
  60. endif
  61. 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\>\)'
  62. " previous line opened a block
  63. let l:ind += shiftwidth()
  64. endif
  65. 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\>\)'
  66. " this line closed a block
  67. let l:ind -= shiftwidth()
  68. elseif l:thisl =~? '^\s*\${\%(Case\|Case[2-5]\|CaseElse\|Default\)\>}\?'
  69. if l:prevl !~? '^\s*\${\%(Select\|Switch\)}'
  70. let l:ind -= shiftwidth()
  71. endif
  72. elseif l:thisl =~? '^\s*\${\%(EndSelect\|EndSwitch\)\>}\?'
  73. " this line closed a block
  74. if l:prevl =~? '^\s*\${\%(Select\|Switch\)}'
  75. let l:ind -= shiftwidth()
  76. else
  77. let l:ind -= shiftwidth() * 2
  78. endif
  79. endif
  80. return l:ind
  81. endfunction
  82. " vim: ts=8 sw=2 sts=2