nsis.vim 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. " Vim indent file
  2. " Language: NSIS script
  3. " Maintainer: Ken Takata
  4. " URL: https://github.com/k-takata/vim-nsis
  5. " Last Change: 2018-01-21
  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. if exists("*GetNsisIndent")
  18. finish
  19. endif
  20. function! GetNsisIndent(lnum)
  21. " If this line is explicitly joined: If the previous line was also joined,
  22. " line it up with that one, otherwise add two 'shiftwidth'
  23. if getline(a:lnum - 1) =~ '\\$'
  24. if a:lnum > 1 && getline(a:lnum - 2) =~ '\\$'
  25. return indent(a:lnum - 1)
  26. endif
  27. return indent(a:lnum - 1) + shiftwidth() * 2
  28. endif
  29. " Grab the current line, stripping comments.
  30. let l:thisl = substitute(getline(a:lnum), '[;#].*$', '', '')
  31. " Check if this line is a conditional preprocessor line.
  32. let l:preproc = l:thisl =~? '^\s*!\%(if\|else\|endif\)'
  33. " Grab the previous line, stripping comments.
  34. " Skip preprocessor lines and continued lines.
  35. let l:prevlnum = a:lnum
  36. while 1
  37. let l:prevlnum = prevnonblank(l:prevlnum - 1)
  38. if l:prevlnum == 0
  39. " top of file
  40. return 0
  41. endif
  42. let l:prevl = substitute(getline(l:prevlnum), '[;#].*$', '', '')
  43. let l:prevpreproc = l:prevl =~? '^\s*!\%(if\|else\|endif\)'
  44. if l:preproc == l:prevpreproc && getline(l:prevlnum - 1) !~? '\\$'
  45. break
  46. endif
  47. endwhile
  48. let l:previ = indent(l:prevlnum)
  49. let l:ind = l:previ
  50. if l:preproc
  51. " conditional preprocessor
  52. if l:prevl =~? '^\s*!\%(if\%(\%(macro\)\?n\?def\)\?\|else\)\>'
  53. let l:ind += shiftwidth()
  54. endif
  55. if l:thisl =~? '^\s*!\%(else\|endif\)\?\>'
  56. let l:ind -= shiftwidth()
  57. endif
  58. return l:ind
  59. endif
  60. 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\>\)'
  61. " previous line opened a block
  62. let l:ind += shiftwidth()
  63. endif
  64. 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\>\)'
  65. " this line closed a block
  66. let l:ind -= shiftwidth()
  67. elseif l:thisl =~? '^\s*\${\%(Case\|Case[2-5]\|CaseElse\|Default\)\>}\?'
  68. if l:prevl !~? '^\s*\${\%(Select\|Switch\)}'
  69. let l:ind -= shiftwidth()
  70. endif
  71. elseif l:thisl =~? '^\s*\${\%(EndSelect\|EndSwitch\)\>}\?'
  72. " this line closed a block
  73. if l:prevl =~? '^\s*\${\%(Select\|Switch\)}'
  74. let l:ind -= shiftwidth()
  75. else
  76. let l:ind -= shiftwidth() * 2
  77. endif
  78. endif
  79. return l:ind
  80. endfunction
  81. " vim: ts=8 sw=2 sts=2