2 " Language: MSDOS batch file (with NT command extensions)
3 " Maintainer: Mike Williams <mrw@eandem.co.uk>
5 " Last Change: 6th September 2009
6 " Web Page: http://www.eandem.co.uk/mrw/vim
9 " dosbatch_cmdextversion - 1 = Windows NT, 2 = Windows 2000 [default]
12 " For version 5.x: Clear all syntax items
13 " For version 6.x: Quit when a syntax file was already loaded
16 elseif exists("b:current_syntax")
20 " Set default highlighting to Win2k
21 if !exists("dosbatch_cmdextversion")
22 let dosbatch_cmdextversion = 2
25 " DOS bat files are case insensitive but case preserving!
28 syn keyword dosbatchTodo contained TODO
31 syn keyword dosbatchStatement goto call exit
32 syn keyword dosbatchConditional if else
33 syn keyword dosbatchRepeat for
35 " Some operators - first lot are case sensitive!
37 syn keyword dosbatchOperator EQU NEQ LSS LEQ GTR GEQ
39 syn match dosbatchOperator "\s[-+\*/%!~]\s"
40 syn match dosbatchOperator "="
41 syn match dosbatchOperator "[-+\*/%]="
42 syn match dosbatchOperator "\s\(&\||\|^\|<<\|>>\)=\=\s"
43 syn match dosbatchIfOperator "if\s\+\(\(not\)\=\s\+\)\=\(exist\|defined\|errorlevel\|cmdextversion\)\="lc=2
45 " String - using "'s is a convenience rather than a requirement outside of FOR
46 syn match dosbatchString "\"[^"]*\"" contains=dosbatchVariable,dosBatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell
47 syn match dosbatchString "\<echo\([^)>|]\|\^\@<=[)>|]\)*"lc=4 contains=dosbatchVariable,dosbatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell
48 syn match dosbatchEchoOperator "\<echo\s\+\(on\|off\)\s*$"lc=4
50 " For embedded commands
51 syn match dosbatchCmd "(\s*'[^']*'"lc=1 contains=dosbatchString,dosbatchVariable,dosBatchArgument,@dosbatchNumber,dosbatchImplicit,dosbatchStatement,dosbatchConditional,dosbatchRepeat,dosbatchOperator
53 " Numbers - surround with ws to not include in dir and filenames
54 syn match dosbatchInteger "[[:space:]=(/:,!~-]\d\+"lc=1
55 syn match dosbatchHex "[[:space:]=(/:,!~-]0x\x\+"lc=1
56 syn match dosbatchBinary "[[:space:]=(/:,!~-]0b[01]\+"lc=1
57 syn match dosbatchOctal "[[:space:]=(/:,!~-]0\o\+"lc=1
58 syn cluster dosbatchNumber contains=dosbatchInteger,dosbatchHex,dosbatchBinary,dosbatchOctal
60 " Command line switches
61 syn match dosbatchSwitch "/\(\a\+\|?\)"
63 " Various special escaped char formats
64 syn match dosbatchSpecialChar "\^[&|()<>^]"
65 syn match dosbatchSpecialChar "\$[a-hl-npqstv_$+]"
66 syn match dosbatchSpecialChar "%%"
68 " Environment variables
69 syn match dosbatchIdentifier contained "\s\h\w*\>"
70 syn match dosbatchVariable "%\h\w*%"
71 syn match dosbatchVariable "%\h\w*:\*\=[^=]*=[^%]*%"
72 syn match dosbatchVariable "%\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=%" contains=dosbatchInteger
73 syn match dosbatchVariable "!\h\w*!"
74 syn match dosbatchVariable "!\h\w*:\*\=[^=]*=[^!]*!"
75 syn match dosbatchVariable "!\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=!" contains=dosbatchInteger
76 syn match dosbatchSet "\s\h\w*[+-]\==\{-1}" contains=dosbatchIdentifier,dosbatchOperator
78 " Args to bat files and for loops, etc
79 syn match dosbatchArgument "%\(\d\|\*\)"
80 syn match dosbatchArgument "%[a-z]\>"
81 if dosbatch_cmdextversion == 1
82 syn match dosbatchArgument "%\~[fdpnxs]\+\(\($PATH:\)\=[a-z]\|\d\)\>"
84 syn match dosbatchArgument "%\~[fdpnxsatz]\+\(\($PATH:\)\=[a-z]\|\d\)\>"
88 syn match dosbatchLabel "^\s*:\s*\h\w*\>"
89 syn match dosbatchLabel "\<\(goto\|call\)\s\+:\h\w*\>"lc=4
90 syn match dosbatchLabel "\<goto\s\+\h\w*\>"lc=4
91 syn match dosbatchLabel ":\h\w*\>"
93 " Comments - usual rem but also two colons as first non-space is an idiom
94 syn match dosbatchComment "^rem\($\|\s.*$\)"lc=3 contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell
95 syn match dosbatchComment "^@rem\($\|\s.*$\)"lc=4 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell
96 syn match dosbatchComment "\srem\($\|\s.*$\)"lc=4 contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell
97 syn match dosbatchComment "\s@rem\($\|\s.*$\)"lc=5 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell
98 syn match dosbatchComment "\s*:\s*:.*$" contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell
100 " Comments in ()'s - still to handle spaces before rem
101 syn match dosbatchComment "(rem\([^)]\|\^\@<=)\)*"lc=4 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell
103 syn keyword dosbatchImplicit append assoc at attrib break cacls cd chcp chdir
104 syn keyword dosbatchImplicit chkdsk chkntfs cls cmd color comp compact convert copy
105 syn keyword dosbatchImplicit date del dir diskcomp diskcopy doskey echo endlocal
106 syn keyword dosbatchImplicit erase fc find findstr format ftype
107 syn keyword dosbatchImplicit graftabl help keyb label md mkdir mode more move
108 syn keyword dosbatchImplicit path pause popd print prompt pushd rd recover rem
109 syn keyword dosbatchImplicit ren rename replace restore rmdir set setlocal shift
110 syn keyword dosbatchImplicit sort start subst time title tree type ver verify
111 syn keyword dosbatchImplicit vol xcopy
113 " Define the default highlighting.
114 " For version 5.7 and earlier: only when not done already
115 " For version 5.8 and later: only when an item doesn't have highlighting yet
116 if version >= 508 || !exists("did_dosbatch_syntax_inits")
118 let did_dosbatch_syntax_inits = 1
119 command -nargs=+ HiLink hi link <args>
121 command -nargs=+ HiLink hi def link <args>
124 HiLink dosbatchTodo Todo
126 HiLink dosbatchStatement Statement
127 HiLink dosbatchCommands dosbatchStatement
128 HiLink dosbatchLabel Label
129 HiLink dosbatchConditional Conditional
130 HiLink dosbatchRepeat Repeat
132 HiLink dosbatchOperator Operator
133 HiLink dosbatchEchoOperator dosbatchOperator
134 HiLink dosbatchIfOperator dosbatchOperator
136 HiLink dosbatchArgument Identifier
137 HiLink dosbatchIdentifier Identifier
138 HiLink dosbatchVariable dosbatchIdentifier
140 HiLink dosbatchSpecialChar SpecialChar
141 HiLink dosbatchString String
142 HiLink dosbatchNumber Number
143 HiLink dosbatchInteger dosbatchNumber
144 HiLink dosbatchHex dosbatchNumber
145 HiLink dosbatchBinary dosbatchNumber
146 HiLink dosbatchOctal dosbatchNumber
148 HiLink dosbatchComment Comment
149 HiLink dosbatchImplicit Function
151 HiLink dosbatchSwitch Special
153 HiLink dosbatchCmd PreProc
158 let b:current_syntax = "dosbatch"