dosbatch.vim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. " Vim indent file
  2. " Language: MSDOS batch file (with NT command extensions)
  3. " Maintainer: Ken Takata
  4. " URL: https://github.com/k-takata/vim-dosbatch-indent
  5. " Last Change: 2021-10-18
  6. " Filenames: *.bat
  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=GetDosBatchIndent(v:lnum)
  15. setlocal indentkeys=!^F,o,O
  16. setlocal indentkeys+=0=)
  17. let b:undo_indent = "setl ai< inde< indk< si<"
  18. if exists("*GetDosBatchIndent")
  19. finish
  20. endif
  21. let s:cpo_save = &cpo
  22. set cpo&vim
  23. function! GetDosBatchIndent(lnum)
  24. let l:prevlnum = prevnonblank(a:lnum-1)
  25. if l:prevlnum == 0
  26. " top of file
  27. return 0
  28. endif
  29. " grab the previous and current line, stripping comments.
  30. let l:prevl = substitute(getline(l:prevlnum), '\c^\s*\%(@\s*\)\?rem\>.*$', '', '')
  31. let l:thisl = getline(a:lnum)
  32. let l:previ = indent(l:prevlnum)
  33. let l:ind = l:previ
  34. if l:prevl =~? '^\s*@\=if\>.*(\s*$' ||
  35. \ l:prevl =~? '\<do\>\s*(\s*$' ||
  36. \ l:prevl =~? '\<else\>\s*\%(if\>.*\)\?(\s*$' ||
  37. \ l:prevl =~? '^.*\(&&\|||\)\s*(\s*$'
  38. " previous line opened a block
  39. let l:ind += shiftwidth()
  40. endif
  41. if l:thisl =~ '^\s*)'
  42. " this line closed a block
  43. let l:ind -= shiftwidth()
  44. endif
  45. return l:ind
  46. endfunction
  47. let &cpo = s:cpo_save
  48. unlet s:cpo_save
  49. " vim: ts=8 sw=2 sts=2