blob: bab89b916e794f7e3ef65840a17647ddcbf4b7f6 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim syntax file
Bram Moolenaarf2571c62015-06-09 19:44:55 +02002" Language: Zsh shell script
3" Maintainer: Christian Brabandt <cb@256bit.org>
4" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
Bram Moolenaar23515b42020-11-29 14:36:24 +01005" Latest Revision: 2020-11-21
Bram Moolenaarf2571c62015-06-09 19:44:55 +02006" License: Vim (see :h license)
Bram Moolenaar94237492017-04-23 18:40:21 +02007" Repository: https://github.com/chrisbra/vim-zsh
Bram Moolenaar071d4272004-06-13 20:20:40 +00008
Bram Moolenaarc81e5e72007-05-05 18:24:42 +00009if exists("b:current_syntax")
Bram Moolenaar071d4272004-06-13 20:20:40 +000010 finish
11endif
12
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000013let s:cpo_save = &cpo
14set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
Bram Moolenaarb17893a2020-03-14 08:19:51 +010016function! s:ContainedGroup()
Bram Moolenaar23515b42020-11-29 14:36:24 +010017 " needs 7.4.2008 for execute() function
Bram Moolenaarb17893a2020-03-14 08:19:51 +010018 let result='TOP'
19 " vim-pandoc syntax defines the @langname cluster for embedded syntax languages
20 " However, if no syntax is defined yet, `syn list @zsh` will return
21 " "No syntax items defined", so make sure the result is actually a valid syn cluster
22 for cluster in ['markdownHighlightzsh', 'zsh']
23 try
24 " markdown syntax defines embedded clusters as @markdownhighlight<lang>,
25 " pandoc just uses @<lang>, so check both for both clusters
26 let a=split(execute('syn list @'. cluster), "\n")
27 if len(a) == 2 && a[0] =~# '^---' && a[1] =~? cluster
28 return '@'. cluster
29 endif
30 catch /E392/
31 " ignore
32 endtry
33 endfor
34 return result
35endfunction
36
37let s:contained=s:ContainedGroup()
38
39syn iskeyword @,48-57,_,192-255,#,-
Bram Moolenaarf3913272016-02-25 00:00:01 +010040if get(g:, 'zsh_fold_enable', 0)
41 setlocal foldmethod=syntax
42endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
Bram Moolenaarfa3b7232021-12-24 13:18:38 +000044syn match zshQuoted '\\.'
Bram Moolenaarb17893a2020-03-14 08:19:51 +010045syn match zshPOSIXQuoted '\\[xX][0-9a-fA-F]\{1,2}'
46syn match zshPOSIXQuoted '\\[0-7]\{1,3}'
47syn match zshPOSIXQuoted '\\u[0-9a-fA-F]\{1,4}'
48syn match zshPOSIXQuoted '\\U[1-9a-fA-F]\{1,8}'
Bram Moolenaarfa3b7232021-12-24 13:18:38 +000049
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000050syn region zshString matchgroup=zshStringDelimiter start=+"+ end=+"+
Bram Moolenaarf3913272016-02-25 00:00:01 +010051 \ contains=zshQuoted,@zshDerefs,@zshSubst fold
52syn region zshString matchgroup=zshStringDelimiter start=+'+ end=+'+ fold
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000053syn region zshPOSIXString matchgroup=zshStringDelimiter start=+\$'+
Bram Moolenaarb17893a2020-03-14 08:19:51 +010054 \ skip=+\\[\\']+ end=+'+ contains=zshPOSIXQuoted,zshQuoted
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000055syn match zshJobSpec '%\(\d\+\|?\=\w\+\|[%+-]\)'
Bram Moolenaar071d4272004-06-13 20:20:40 +000056
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000057syn keyword zshPrecommand noglob nocorrect exec command builtin - time
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
Bram Moolenaarf2571c62015-06-09 19:44:55 +020059syn keyword zshDelimiter do done end
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000061syn keyword zshConditional if then elif else fi case in esac select
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaare37d50a2008-08-06 17:06:04 +000063syn keyword zshRepeat while until repeat
64
65syn keyword zshRepeat for foreach nextgroup=zshVariable skipwhite
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000067syn keyword zshException always
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000069syn keyword zshKeyword function nextgroup=zshKSHFunction skipwhite
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarf3913272016-02-25 00:00:01 +010071syn match zshKSHFunction contained '\w\S\+'
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000072syn match zshFunction '^\s*\k\+\ze\s*()'
73
74syn match zshOperator '||\|&&\|;\|&!\='
75
76syn match zshRedir '\d\=\(<\|<>\|<<<\|<&\s*[0-9p-]\=\)'
77syn match zshRedir '\d\=\(>\|>>\|>&\s*[0-9p-]\=\|&>\|>>&\|&>>\)[|!]\='
78syn match zshRedir '|&\='
79
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000080syn region zshHereDoc matchgroup=zshRedir
Bram Moolenaare37d50a2008-08-06 17:06:04 +000081 \ start='<\@<!<<\s*\z([^<]\S*\)'
82 \ end='^\z1\>'
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020083 \ contains=@zshSubst,@zshDerefs,zshQuoted,zshPOSIXString
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000084syn region zshHereDoc matchgroup=zshRedir
Bram Moolenaare37d50a2008-08-06 17:06:04 +000085 \ start='<\@<!<<\s*\\\z(\S\+\)'
86 \ end='^\z1\>'
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020087 \ contains=@zshSubst,@zshDerefs,zshQuoted,zshPOSIXString
Bram Moolenaare37d50a2008-08-06 17:06:04 +000088syn region zshHereDoc matchgroup=zshRedir
89 \ start='<\@<!<<-\s*\\\=\z(\S\+\)'
90 \ end='^\s*\z1\>'
Bram Moolenaar7db25fe2018-05-13 00:02:36 +020091 \ contains=@zshSubst,@zshDerefs,zshQuoted,zshPOSIXString
Bram Moolenaare37d50a2008-08-06 17:06:04 +000092syn region zshHereDoc matchgroup=zshRedir
Bram Moolenaarf2571c62015-06-09 19:44:55 +020093 \ start=+<\@<!<<\s*\(["']\)\z(\S\+\)\1+
Bram Moolenaare37d50a2008-08-06 17:06:04 +000094 \ end='^\z1\>'
95syn region zshHereDoc matchgroup=zshRedir
96 \ start=+<\@<!<<-\s*\(["']\)\z(\S\+\)\1+
Bram Moolenaarc81e5e72007-05-05 18:24:42 +000097 \ end='^\s*\z1\>'
98
Bram Moolenaare37d50a2008-08-06 17:06:04 +000099syn match zshVariable '\<\h\w*' contained
100
101syn match zshVariableDef '\<\h\w*\ze+\=='
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000102" XXX: how safe is this?
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000103syn region zshVariableDef oneline
Bram Moolenaar94237492017-04-23 18:40:21 +0200104 \ start='\$\@<!\<\h\w*\[' end='\]\ze+\?=\?'
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000105 \ contains=@zshSubst
106
Bram Moolenaar94237492017-04-23 18:40:21 +0200107syn cluster zshDerefs contains=zshShortDeref,zshLongDeref,zshDeref,zshDollarVar
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000108
Bram Moolenaar94237492017-04-23 18:40:21 +0200109syn match zshShortDeref '\$[!#$*@?_-]\w\@!'
110syn match zshShortDeref '\$[=^~]*[#+]*\d\+\>'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
Bram Moolenaar94237492017-04-23 18:40:21 +0200112syn match zshLongDeref '\$\%(ARGC\|argv\|status\|pipestatus\|CPUTYPE\|EGID\|EUID\|ERRNO\|GID\|HOST\|LINENO\|LOGNAME\)'
113syn match zshLongDeref '\$\%(MACHTYPE\|OLDPWD OPTARG\|OPTIND\|OSTYPE\|PPID\|PWD\|RANDOM\|SECONDS\|SHLVL\|signals\)'
114syn match zshLongDeref '\$\%(TRY_BLOCK_ERROR\|TTY\|TTYIDLE\|UID\|USERNAME\|VENDOR\|ZSH_NAME\|ZSH_VERSION\|REPLY\|reply\|TERM\)'
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000115
Bram Moolenaar94237492017-04-23 18:40:21 +0200116syn match zshDollarVar '\$\h\w*'
117syn match zshDeref '\$[=^~]*[#+]*\h\w*\>'
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000118
119syn match zshCommands '\%(^\|\s\)[.:]\ze\s'
120syn keyword zshCommands alias autoload bg bindkey break bye cap cd
121 \ chdir clone comparguments compcall compctl
122 \ compdescribe compfiles compgroups compquote
123 \ comptags comptry compvalues continue dirs
124 \ disable disown echo echotc echoti emulate
125 \ enable eval exec exit export false fc fg
126 \ functions getcap getln getopts hash history
127 \ jobs kill let limit log logout popd print
Bram Moolenaar96f45c02019-10-26 19:53:45 +0200128 \ printf pushd pushln pwd r read
Bram Moolenaar94237492017-04-23 18:40:21 +0200129 \ rehash return sched set setcap shift
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000130 \ source stat suspend test times trap true
131 \ ttyctl type ulimit umask unalias unfunction
Bram Moolenaar94237492017-04-23 18:40:21 +0200132 \ unhash unlimit unset vared wait
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000133 \ whence where which zcompile zformat zftp zle
Bram Moolenaar7db25fe2018-05-13 00:02:36 +0200134 \ zmodload zparseopts zprof zpty zrecompile
135 \ zregexparse zsocket zstyle ztcp
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000136
Bram Moolenaarfa3b7232021-12-24 13:18:38 +0000137" Options, generated by from the zsh source with the make-options.zsh script.
Bram Moolenaarf2571c62015-06-09 19:44:55 +0200138syn case ignore
Bram Moolenaarfa3b7232021-12-24 13:18:38 +0000139syn match zshOptStart
140 \ /\v^\s*%(%(un)?setopt|set\s+[-+]o)/
141 \ nextgroup=zshOption skipwhite
142syn match zshOption nextgroup=zshOption,zshComment skipwhite contained /\v
143 \ <%(no_?)?%(
144 \ auto_?cd|auto_?pushd|cdable_?vars|cd_?silent|chase_?dots|chase_?links|posix_?cd|pushd_?ignore_?dups|pushd_?minus|pushd_?silent|pushd_?to_?home|always_?last_?prompt|always_?to_?end|auto_?list|auto_?menu|auto_?name_?dirs|auto_?param_?keys|auto_?param_?slash|auto_?remove_?slash|bash_?auto_?list|complete_?aliases|complete_?in_?word|glob_?complete|hash_?list_?all|list_?ambiguous|list_?beep|list_?packed|list_?rows_?first|list_?types|menu_?complete|rec_?exact|bad_?pattern|bare_?glob_?qual|brace_?ccl|case_?glob|case_?match|case_?paths|csh_?null_?glob|equals|extended_?glob|force_?float|glob|glob_?assign|glob_?dots|glob_?star_?short|glob_?subst|hist_?subst_?pattern|ignore_?braces|ignore_?close_?braces|ksh_?glob|magic_?equal_?subst|mark_?dirs|multibyte|nomatch|null_?glob|numeric_?glob_?sort|rc_?expand_?param|rematch_?pcre|sh_?glob|unset|warn_?create_?global|warn_?nested_?var|warnnestedvar|append_?history|bang_?hist|extended_?history|hist_?allow_?clobber|hist_?beep|hist_?expire_?dups_?first|hist_?fcntl_?lock|hist_?find_?no_?dups|hist_?ignore_?all_?dups|hist_?ignore_?dups|hist_?ignore_?space|hist_?lex_?words|hist_?no_?functions|hist_?no_?store|hist_?reduce_?blanks|hist_?save_?by_?copy|hist_?save_?no_?dups|hist_?verify|inc_?append_?history|inc_?append_?history_?time|share_?history|all_?export|global_?export|global_?rcs|rcs|aliases|clobber|clobber_?empty|correct|correct_?all|dvorak|flow_?control|ignore_?eof|interactive_?comments|hash_?cmds|hash_?dirs|hash_?executables_?only|mail_?warning|path_?dirs|path_?script|print_?eight_?bit|print_?exit_?value|rc_?quotes|rm_?star_?silent|rm_?star_?wait|short_?loops|short_?repeat|sun_?keyboard_?hack|auto_?continue|auto_?resume|bg_?nice|check_?jobs|check_?running_?jobs|hup|long_?list_?jobs|monitor|notify|posix_?jobs|prompt_?bang|prompt_?cr|prompt_?sp|prompt_?percent|prompt_?subst|transient_?rprompt|alias_?func_?def|c_?bases|c_?precedences|debug_?before_?cmd|err_?exit|err_?return|eval_?lineno|exec|function_?argzero|local_?loops|local_?options|local_?patterns|local_?traps|multi_?func_?def|multios|octal_?zeroes|pipe_?fail|source_?trace|typeset_?silent|typeset_?to_?unset|verbose|xtrace|append_?create|bash_?rematch|bsd_?echo|continue_?on_?error|csh_?junkie_?history|csh_?junkie_?loops|csh_?junkie_?quotes|csh_?nullcmd|ksh_?arrays|ksh_?autoload|ksh_?option_?print|ksh_?typeset|ksh_?zero_?subscript|posix_?aliases|posix_?argzero|posix_?builtins|posix_?identifiers|posix_?strings|posix_?traps|sh_?file_?expansion|sh_?nullcmd|sh_?option_?letters|sh_?word_?split|traps_?async|interactive|login|privileged|restricted|shin_?stdin|single_?command|beep|combining_?chars|emacs|overstrike|single_?line_?zle|vi|zle|brace_?expand|dot_?glob|hash_?all|hist_?append|hist_?expand|log|mail_?warn|one_?cmd|physical|prompt_?vars|stdin|track_?all|no_?match
145 \)>/
Bram Moolenaar23515b42020-11-29 14:36:24 +0100146syn case match
147
Bram Moolenaar2f058492017-11-30 20:27:52 +0100148syn keyword zshTypes float integer local typeset declare private readonly
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000149
150" XXX: this may be too much
151" syn match zshSwitches '\s\zs--\=[a-zA-Z0-9-]\+'
152
153syn match zshNumber '[+-]\=\<\d\+\>'
154syn match zshNumber '[+-]\=\<0x\x\+\>'
155syn match zshNumber '[+-]\=\<0\o\+\>'
156syn match zshNumber '[+-]\=\d\+#[-+]\=\w\+\>'
157syn match zshNumber '[+-]\=\d\+\.\d\+\>'
158
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000159" TODO: $[...] is the same as $((...)), so add that as well.
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000160syn cluster zshSubst contains=zshSubst,zshOldSubst,zshMathSubst
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100161exe 'syn region zshSubst matchgroup=zshSubstDelim transparent start=/\$(/ skip=/\\)/ end=/)/ contains='.s:contained. ' fold'
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100162syn region zshParentheses transparent start='(' skip='\\)' end=')' fold
Bram Moolenaar94237492017-04-23 18:40:21 +0200163syn region zshGlob start='(#' end=')'
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000164syn region zshMathSubst matchgroup=zshSubstDelim transparent
Bram Moolenaar23515b42020-11-29 14:36:24 +0100165 \ start='\%(\$\?\)[<=>]\@<!((' skip='\\)' end='))'
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000166 \ contains=zshParentheses,@zshSubst,zshNumber,
Bram Moolenaarfa3b7232021-12-24 13:18:38 +0000167 \ @zshDerefs,zshString fold
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100168" The ms=s+1 prevents matching zshBrackets several times on opening brackets
169" (see https://github.com/chrisbra/vim-zsh/issues/21#issuecomment-576330348)
170syn region zshBrackets contained transparent start='{'ms=s+1 skip='\\}'
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100171 \ end='}' fold
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100172exe 'syn region zshBrackets transparent start=/{/ms=s+1 skip=/\\}/ end=/}/ contains='.s:contained. ' fold'
173
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000174syn region zshSubst matchgroup=zshSubstDelim start='\${' skip='\\}'
Bram Moolenaar681baaf2016-02-04 20:57:07 +0100175 \ end='}' contains=@zshSubst,zshBrackets,zshQuoted,zshString fold
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100176exe 'syn region zshOldSubst matchgroup=zshSubstDelim start=/`/ skip=/\\[\\`]/ end=/`/ contains='.s:contained. ',zshOldSubst fold'
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000177
Bram Moolenaarf2571c62015-06-09 19:44:55 +0200178syn sync minlines=50 maxlines=90
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000179syn sync match zshHereDocSync grouphere NONE '<<-\=\s*\%(\\\=\S\+\|\(["']\)\S\+\1\)'
180syn sync match zshHereDocEndSync groupthere NONE '^\s*EO\a\+\>'
181
Bram Moolenaar23515b42020-11-29 14:36:24 +0100182syn keyword zshTodo contained TODO FIXME XXX NOTE
183
184syn region zshComment oneline start='\%(^\|\s\+\)#' end='$'
185 \ contains=zshTodo,@Spell fold
186
187syn region zshComment start='^\s*#' end='^\%(\s*#\)\@!'
188 \ contains=zshTodo,@Spell fold
189
190syn match zshPreProc '^\%1l#\%(!\|compdef\|autoload\).*$'
191
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000192hi def link zshTodo Todo
193hi def link zshComment Comment
194hi def link zshPreProc PreProc
195hi def link zshQuoted SpecialChar
Bram Moolenaarb17893a2020-03-14 08:19:51 +0100196hi def link zshPOSIXQuoted SpecialChar
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000197hi def link zshString String
198hi def link zshStringDelimiter zshString
199hi def link zshPOSIXString zshString
200hi def link zshJobSpec Special
201hi def link zshPrecommand Special
202hi def link zshDelimiter Keyword
203hi def link zshConditional Conditional
204hi def link zshException Exception
205hi def link zshRepeat Repeat
206hi def link zshKeyword Keyword
207hi def link zshFunction None
208hi def link zshKSHFunction zshFunction
209hi def link zshHereDoc String
Bram Moolenaarf2571c62015-06-09 19:44:55 +0200210hi def link zshOperator None
211hi def link zshRedir Operator
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000212hi def link zshVariable None
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000213hi def link zshVariableDef zshVariable
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000214hi def link zshDereferencing PreProc
Bram Moolenaar94237492017-04-23 18:40:21 +0200215hi def link zshShortDeref zshDereferencing
216hi def link zshLongDeref zshDereferencing
217hi def link zshDeref zshDereferencing
218hi def link zshDollarVar zshDereferencing
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000219hi def link zshCommands Keyword
Bram Moolenaar94237492017-04-23 18:40:21 +0200220hi def link zshOptStart Keyword
221hi def link zshOption Constant
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000222hi def link zshTypes Type
223hi def link zshSwitches Special
224hi def link zshNumber Number
225hi def link zshSubst PreProc
226hi def link zshMathSubst zshSubst
227hi def link zshOldSubst zshSubst
228hi def link zshSubstDelim zshSubst
Bram Moolenaar94237492017-04-23 18:40:21 +0200229hi def link zshGlob zshSubst
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231let b:current_syntax = "zsh"
232
Bram Moolenaarc81e5e72007-05-05 18:24:42 +0000233let &cpo = s:cpo_save
234unlet s:cpo_save