blob: d24b1392420cb85029f0788c3d762df26df7c426 [file] [log] [blame]
Bram Moolenaar20aac6c2018-09-02 21:07:30 +02001" 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
Bram Moolenaar079ba762021-10-23 12:08:41 +01005" Last Change: 2021-10-18
Bram Moolenaar20aac6c2018-09-02 21:07:30 +02006" Filenames: *.bat
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=GetDosBatchIndent(v:lnum)
17setlocal indentkeys=!^F,o,O
18setlocal indentkeys+=0=)
19
Bram Moolenaar079ba762021-10-23 12:08:41 +010020let b:undo_indent = "setl ai< inde< indk< si<"
21
Bram Moolenaar20aac6c2018-09-02 21:07:30 +020022if exists("*GetDosBatchIndent")
23 finish
24endif
25
26let s:cpo_save = &cpo
27set cpo&vim
28
29function! GetDosBatchIndent(lnum)
30 let l:prevlnum = prevnonblank(a:lnum-1)
31 if l:prevlnum == 0
32 " top of file
33 return 0
34 endif
35
36 " grab the previous and current line, stripping comments.
37 let l:prevl = substitute(getline(l:prevlnum), '\c^\s*\%(@\s*\)\?rem\>.*$', '', '')
38 let l:thisl = getline(a:lnum)
39 let l:previ = indent(l:prevlnum)
40
41 let l:ind = l:previ
42
43 if l:prevl =~? '^\s*@\=if\>.*(\s*$' ||
44 \ l:prevl =~? '\<do\>\s*(\s*$' ||
45 \ l:prevl =~? '\<else\>\s*\%(if\>.*\)\?(\s*$' ||
46 \ l:prevl =~? '^.*\(&&\|||\)\s*(\s*$'
47 " previous line opened a block
48 let l:ind += shiftwidth()
49 endif
50 if l:thisl =~ '^\s*)'
51 " this line closed a block
52 let l:ind -= shiftwidth()
53 endif
54
55 return l:ind
56endfunction
57
58let &cpo = s:cpo_save
59unlet s:cpo_save
60
61" vim: ts=8 sw=2 sts=2