neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

dosbatch.vim (7150B)


      1 " Vim syntax file
      2 " Language:	MS-DOS/Windows batch file (with NT command extensions)
      3 " Maintainer:	Mike Williams <mrmrdubya@gmail.com>
      4 " Filenames:    *.bat
      5 " Last Change:	3rd February 2024
      6 " 2024 Aug 14 by Vim Project: improve syntax (#15453)
      7 "
      8 " Options Flags:
      9 " dosbatch_cmdextversion	- 1 = Windows NT, 2 = Windows 2000 [default]
     10 " dosbatch_colons_comment       - any value to allow :: comments in code blocks
     11 "
     12 
     13 " quit when a syntax file was already loaded
     14 if exists("b:current_syntax")
     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
     37 syn match dosbatchOperator      "\s[-+\*/%!~]\s"
     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
     44 syn match dosbatchString	"\"[^"]*\"" contains=dosbatchVariable,dosBatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell
     45 syn match dosbatchString	"\<echo\([^)>|]\|\^\@<=[)>|]\)*"lc=4 contains=dosbatchVariable,dosbatchArgument,dosbatchSpecialChar,@dosbatchNumber,@Spell
     46 syn match dosbatchEchoOperator  "\<echo\s\+\(on\|off\)\s*$"lc=4
     47 
     48 " For embedded commands
     49 syn match dosbatchCmd		"(\s*'[^']*'"lc=1 contains=dosbatchString,dosbatchVariable,dosBatchArgument,@dosbatchNumber,dosbatchImplicit,dosbatchStatement,dosbatchConditional,dosbatchRepeat,dosbatchOperator,dosbatchIfOperator
     50 
     51 " Numbers - surround with ws to not include in dir and filenames
     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
     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*:\*\=[^=]*=[^%]*%"
     70 syn match dosbatchVariable	"%\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=%" contains=dosbatchInteger
     71 syn match dosbatchVariable	"!\h\w*!"
     72 syn match dosbatchVariable	"!\h\w*:\*\=[^=]*=[^!]*!"
     73 syn match dosbatchVariable	"!\h\w*:\~[-]\=\d\+\(,[-]\=\d\+\)\=!" contains=dosbatchInteger
     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\|\*\)"
     78 syn match dosbatchArgument	"%%[a-z]\>"
     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
     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
     97 
     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*\)\?)\)\@="
    106 syn region dosbatchCodeBlock	transparent start=+(+ end=+)+ contains=dosbatchString,dosbatchVariable,dosBatchArgument,@dosbatchNumber,dosbatchImplicit,dosbatchStatement,dosbatchConditional,dosbatchRepeat,dosbatchOperator,dosbatchIfOperator,@dosbatchCodeBlockComment,dosbatchColonCommentErr,dosbatchCodeBlock
    107 syn match dosbatchCodeBlockErr	")"
    108 
    109 syn sync match dosbatchSyncCodeBlock grouphere NONE "^)\s*$"
    110 
    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.
    122 " Only when an item doesn't have highlighting yet
    123 
    124 hi def link dosbatchTodo	Todo
    125 hi def link dosbatchError	Error
    126 hi def link dosbatchCodeBlockErr dosbatchError
    127 hi def link dosbatchColonCommentErr dosbatchError
    128 
    129 hi def link dosbatchStatement	Statement
    130 hi def link dosbatchCommands	dosbatchStatement
    131 hi def link dosbatchLabel	Label
    132 hi def link dosbatchConditional	Conditional
    133 hi def link dosbatchRepeat	Repeat
    134 
    135 hi def link dosbatchOperator	Operator
    136 hi def link dosbatchEchoOperator dosbatchOperator
    137 hi def link dosbatchIfOperator	dosbatchOperator
    138 
    139 hi def link dosbatchArgument	Identifier
    140 hi def link dosbatchIdentifier	Identifier
    141 hi def link dosbatchVariable	dosbatchIdentifier
    142 
    143 hi def link dosbatchSpecialChar	SpecialChar
    144 hi def link dosbatchString	String
    145 hi def link dosbatchNumber	Number
    146 hi def link dosbatchInteger	dosbatchNumber
    147 hi def link dosbatchHex		dosbatchNumber
    148 hi def link dosbatchBinary	dosbatchNumber
    149 hi def link dosbatchOctal	dosbatchNumber
    150 
    151 hi def link dosbatchComment	Comment
    152 hi def link dosbatchRemComment	dosbatchComment
    153 hi def link dosbatchColonComment dosbatchComment
    154 
    155 hi def link dosbatchImplicit	Function
    156 
    157 hi def link dosbatchSwitch	Special
    158 
    159 hi def link dosbatchCmd		PreProc
    160 
    161 
    162 let b:current_syntax = "dosbatch"
    163 
    164 " vim: ts=8