Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | " Vim syntax file |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 2 | " Language: MS-DOS/Windows batch file (with NT command extensions) |
| 3 | " Maintainer: Mike Williams <mrmrdubya@gmail.com> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4 | " Filenames: *.bat |
Christian Brabandt | f7f33e3 | 2024-02-06 10:56:26 +0100 | [diff] [blame] | 5 | " Last Change: 3rd February 2024 |
Ken Takata | 11c92be | 2024-08-14 21:56:42 +0200 | [diff] [blame^] | 6 | " 2024 Aug 14 by Vim Project: improve syntax (#15453) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7 | " |
| 8 | " Options Flags: |
| 9 | " dosbatch_cmdextversion - 1 = Windows NT, 2 = Windows 2000 [default] |
Christian Brabandt | f7f33e3 | 2024-02-06 10:56:26 +0100 | [diff] [blame] | 10 | " dosbatch_colons_comment - any value to allow :: comments in code blocks |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11 | " |
| 12 | |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 13 | " quit when a syntax file was already loaded |
| 14 | if exists("b:current_syntax") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | finish |
| 16 | endif |
| 17 | |
| 18 | " Set default highlighting to Win2k |
| 19 | if !exists("dosbatch_cmdextversion") |
| 20 | let dosbatch_cmdextversion = 2 |
| 21 | endif |
| 22 | |
| 23 | " DOS bat files are case insensitive but case preserving! |
| 24 | syn case ignore |
| 25 | |
| 26 | syn keyword dosbatchTodo contained TODO |
| 27 | |
| 28 | " Dosbat keywords |
| 29 | syn keyword dosbatchStatement goto call exit |
| 30 | syn keyword dosbatchConditional if else |
| 31 | syn keyword dosbatchRepeat for |
| 32 | |
| 33 | " Some operators - first lot are case sensitive! |
| 34 | syn case match |
| 35 | syn keyword dosbatchOperator EQU NEQ LSS LEQ GTR GEQ |
| 36 | syn case ignore |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 37 | syn match dosbatchOperator "\s[-+\*/%!~]\s" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | syn match dosbatchOperator "=" |
| 39 | syn match dosbatchOperator "[-+\*/%]=" |
| 40 | syn match dosbatchOperator "\s\(&\||\|^\|<<\|>>\)=\=\s" |
| 41 | syn match dosbatchIfOperator "if\s\+\(\(not\)\=\s\+\)\=\(exist\|defined\|errorlevel\|cmdextversion\)\="lc=2 |
| 42 | |
| 43 | " String - using "'s is a convenience rather than a requirement outside of FOR |
Bram Moolenaar | 864207d | 2008-06-24 22:14:38 +0000 | [diff] [blame] | 44 | syn match dosbatchString "\"[^"]*\"" contains=dosbatchVariable,dosBatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell |
| 45 | syn match dosbatchString "\<echo\([^)>|]\|\^\@<=[)>|]\)*"lc=4 contains=dosbatchVariable,dosbatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | syn match dosbatchEchoOperator "\<echo\s\+\(on\|off\)\s*$"lc=4 |
| 47 | |
| 48 | " For embedded commands |
Ken Takata | 11c92be | 2024-08-14 21:56:42 +0200 | [diff] [blame^] | 49 | syn match dosbatchCmd "(\s*'[^']*'"lc=1 contains=dosbatchString,dosbatchVariable,dosBatchArgument,@dosbatchNumber,dosbatchImplicit,dosbatchStatement,dosbatchConditional,dosbatchRepeat,dosbatchOperator,dosbatchIfOperator |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | |
| 51 | " Numbers - surround with ws to not include in dir and filenames |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 52 | syn match dosbatchInteger "[[:space:]=(/:,!~-]\d\+"lc=1 |
| 53 | syn match dosbatchHex "[[:space:]=(/:,!~-]0x\x\+"lc=1 |
| 54 | syn match dosbatchBinary "[[:space:]=(/:,!~-]0b[01]\+"lc=1 |
| 55 | syn match dosbatchOctal "[[:space:]=(/:,!~-]0\o\+"lc=1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | syn cluster dosbatchNumber contains=dosbatchInteger,dosbatchHex,dosbatchBinary,dosbatchOctal |
| 57 | |
| 58 | " Command line switches |
| 59 | syn match dosbatchSwitch "/\(\a\+\|?\)" |
| 60 | |
| 61 | " Various special escaped char formats |
| 62 | syn match dosbatchSpecialChar "\^[&|()<>^]" |
| 63 | syn match dosbatchSpecialChar "\$[a-hl-npqstv_$+]" |
| 64 | syn match dosbatchSpecialChar "%%" |
| 65 | |
| 66 | " Environment variables |
| 67 | syn match dosbatchIdentifier contained "\s\h\w*\>" |
| 68 | syn match dosbatchVariable "%\h\w*%" |
| 69 | syn match dosbatchVariable "%\h\w*:\*\=[^=]*=[^%]*%" |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 70 | syn match dosbatchVariable "%\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=%" contains=dosbatchInteger |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | syn match dosbatchVariable "!\h\w*!" |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 72 | syn match dosbatchVariable "!\h\w*:\*\=[^=]*=[^!]*!" |
| 73 | syn match dosbatchVariable "!\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=!" contains=dosbatchInteger |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | syn match dosbatchSet "\s\h\w*[+-]\==\{-1}" contains=dosbatchIdentifier,dosbatchOperator |
| 75 | |
| 76 | " Args to bat files and for loops, etc |
| 77 | syn match dosbatchArgument "%\(\d\|\*\)" |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 78 | syn match dosbatchArgument "%[a-z]\>" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | if dosbatch_cmdextversion == 1 |
| 80 | syn match dosbatchArgument "%\~[fdpnxs]\+\(\($PATH:\)\=[a-z]\|\d\)\>" |
| 81 | else |
| 82 | syn match dosbatchArgument "%\~[fdpnxsatz]\+\(\($PATH:\)\=[a-z]\|\d\)\>" |
| 83 | endif |
| 84 | |
| 85 | " Line labels |
| 86 | syn match dosbatchLabel "^\s*:\s*\h\w*\>" |
| 87 | syn match dosbatchLabel "\<\(goto\|call\)\s\+:\h\w*\>"lc=4 |
| 88 | syn match dosbatchLabel "\<goto\s\+\h\w*\>"lc=4 |
| 89 | syn match dosbatchLabel ":\h\w*\>" |
| 90 | |
| 91 | " Comments - usual rem but also two colons as first non-space is an idiom |
Christian Brabandt | f7f33e3 | 2024-02-06 10:56:26 +0100 | [diff] [blame] | 92 | syn match dosbatchRemComment "^rem\($\|\s.*$\)"lc=3 contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell |
| 93 | syn match dosbatchRemComment "^@rem\($\|\s.*$\)"lc=4 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell |
| 94 | syn match dosbatchRemComment "\srem\($\|\s.*$\)"lc=4 contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell |
| 95 | syn match dosbatchRemComment "\s@rem\($\|\s.*$\)"lc=5 contains=dosbatchTodo,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell |
| 96 | syn match dosbatchColonComment "\s*:\s*:.*$" contains=dosbatchTodo,dosbatchSpecialChar,@dosbatchNumber,dosbatchVariable,dosbatchArgument,@Spell |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | |
Christian Brabandt | f7f33e3 | 2024-02-06 10:56:26 +0100 | [diff] [blame] | 98 | " Commands code blocks |
| 99 | syn cluster dosbatchCodeBlockComment contains=dosbatchRemComment |
| 100 | if exists("dosbatch_colons_comment") |
| 101 | syn cluster dosbatchCodeBlockComment add=dosbatchColonComment |
| 102 | else |
| 103 | syn match dosbatchColonCommentErr contained "\s*:\s*:.*$" |
| 104 | endif |
| 105 | syn match dosbatchColonCommentErr contained "\s*:\s*:[^)]*\(\(\n\s*\)\?)\)\@=" |
Ken Takata | 11c92be | 2024-08-14 21:56:42 +0200 | [diff] [blame^] | 106 | syn region dosbatchCodeBlock transparent start=+(+ end=+)+ contains=dosbatchString,dosbatchVariable,dosBatchArgument,@dosbatchNumber,dosbatchImplicit,dosbatchStatement,dosbatchConditional,dosbatchRepeat,dosbatchOperator,dosbatchIfOperator,@dosbatchCodeBlockComment,dosbatchColonCommentErr,dosbatchCodeBlock |
Christian Brabandt | f7f33e3 | 2024-02-06 10:56:26 +0100 | [diff] [blame] | 107 | syn match dosbatchCodeBlockErr ")" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 108 | |
Ken Takata | 11c92be | 2024-08-14 21:56:42 +0200 | [diff] [blame^] | 109 | syn sync match dosbatchSyncCodeBlock grouphere NONE "^)\s*$" |
| 110 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | syn keyword dosbatchImplicit append assoc at attrib break cacls cd chcp chdir |
| 112 | syn keyword dosbatchImplicit chkdsk chkntfs cls cmd color comp compact convert copy |
| 113 | syn keyword dosbatchImplicit date del dir diskcomp diskcopy doskey echo endlocal |
| 114 | syn keyword dosbatchImplicit erase fc find findstr format ftype |
| 115 | syn keyword dosbatchImplicit graftabl help keyb label md mkdir mode more move |
| 116 | syn keyword dosbatchImplicit path pause popd print prompt pushd rd recover rem |
| 117 | syn keyword dosbatchImplicit ren rename replace restore rmdir set setlocal shift |
| 118 | syn keyword dosbatchImplicit sort start subst time title tree type ver verify |
| 119 | syn keyword dosbatchImplicit vol xcopy |
| 120 | |
| 121 | " Define the default highlighting. |
Bram Moolenaar | 89bcfda | 2016-08-30 23:26:57 +0200 | [diff] [blame] | 122 | " Only when an item doesn't have highlighting yet |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 124 | hi def link dosbatchTodo Todo |
| 125 | hi def link dosbatchError Error |
Christian Brabandt | f7f33e3 | 2024-02-06 10:56:26 +0100 | [diff] [blame] | 126 | hi def link dosbatchCodeBlockErr dosbatchError |
| 127 | hi def link dosbatchColonCommentErr dosbatchError |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 129 | hi def link dosbatchStatement Statement |
| 130 | hi def link dosbatchCommands dosbatchStatement |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 131 | hi def link dosbatchLabel Label |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 132 | hi def link dosbatchConditional Conditional |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 133 | hi def link dosbatchRepeat Repeat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 135 | hi def link dosbatchOperator Operator |
| 136 | hi def link dosbatchEchoOperator dosbatchOperator |
| 137 | hi def link dosbatchIfOperator dosbatchOperator |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 139 | hi def link dosbatchArgument Identifier |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 140 | hi def link dosbatchIdentifier Identifier |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 141 | hi def link dosbatchVariable dosbatchIdentifier |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 142 | |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 143 | hi def link dosbatchSpecialChar SpecialChar |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 144 | hi def link dosbatchString String |
| 145 | hi def link dosbatchNumber Number |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 146 | hi def link dosbatchInteger dosbatchNumber |
| 147 | hi def link dosbatchHex dosbatchNumber |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 148 | hi def link dosbatchBinary dosbatchNumber |
| 149 | hi def link dosbatchOctal dosbatchNumber |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 151 | hi def link dosbatchComment Comment |
Christian Brabandt | f7f33e3 | 2024-02-06 10:56:26 +0100 | [diff] [blame] | 152 | hi def link dosbatchRemComment dosbatchComment |
| 153 | hi def link dosbatchColonComment dosbatchComment |
| 154 | |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 155 | hi def link dosbatchImplicit Function |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | |
Bram Moolenaar | 938ae28 | 2023-02-20 20:44:55 +0000 | [diff] [blame] | 157 | hi def link dosbatchSwitch Special |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | |
Bram Moolenaar | f37506f | 2016-08-31 22:22:10 +0200 | [diff] [blame] | 159 | hi def link dosbatchCmd PreProc |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | |
| 162 | let b:current_syntax = "dosbatch" |
| 163 | |
| 164 | " vim: ts=8 |