aboutsummaryrefslogtreecommitdiff
path: root/vim/after/syntax/sh.vim
blob: 797bdab9a27081163ce72dea25bed5a225af4f23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
" Remove g:is_posix if we resorted to it in order to get correct POSIX sh
" highlighting with older Vim runtime files
if exists('is_posix')
  unlet! is_posix is_kornshell
endif

" If we know we have another shell type, clear away the others completely, now
" that core syntax/sh.vim is done prodding /bin/sh to determine the system
" shell type (which I don't care about).
if exists('b:is_bash')
  unlet! b:is_sh b:is_posix b:is_kornshell
elseif exists('b:is_kornshell')
  unlet! b:is_sh b:is_posix
elseif exists('b:is_posix')
  unlet! b:is_sh
endif

" The syntax highlighter seems to flag '/baz' in '"${foo:-"$bar"/baz}"' as an
" error, which it isn't, at least in POSIX sh, Bash, and Ksh.
syntax clear shDerefWordError

" The syntax highlighter doesn't match parens for subshells for 'if' tests
" correctly if they're on separate lines.  This happens enough that it's
" probably not worth keeping the error.
syntax clear shParenError

" The syntax highlighter flags this code with an error on the final square
" bracket: `case $foo in [![:ascii:]]) ;; esac`, but that's all legal.  I'm not
" yet sure how to fix it, so will just turn the error group for now.
syntax clear shTestError

" Highlighting corrections specific to POSIX mode
if exists('b:is_posix')

  " Highlight some commands that are both defined by POSIX and builtin
  " commands in dash, as a rough but useable proxy for 'shell builtins'.  This
  " list was mostly wrested from `man 1 dash`.  Also include control structure
  " keywords like `break`, `continue`, and `return`.
  syntax clear shStatement
  syntax cluster shCommandSubList add=shStatement
  syntax cluster shCaseList add=shStatement
  syntax keyword shStatement
        \ alias
        \ bg
        \ break
        \ cd
        \ command
        \ continue
        \ echo
        \ eval
        \ exec
        \ exit
        \ export
        \ fc
        \ fg
        \ getopts
        \ hash
        \ kill
        \ printf
        \ pwd
        \ read
        \ readonly
        \ return
        \ set
        \ shift
        \ test
        \ times
        \ trap
        \ true
        \ type
        \ ulimit
        \ umask
        \ unalias
        \ unset
        \ wait

  " Core syntax/sh.vim puts IFS and other variables that affect shell function
  " in another color, but a subset of them actually apply to POSIX shell too
  " (and plain Bourne).  These are selected by searching the POSIX manpages.  I
  " added NLSPATH too, which wasn't in the original.
  syntax clear shShellVariables
  syntax cluster shCommandSubList add=shShellVariables
  syntax keyword shShellVariables
        \ CDPATH
        \ ENV
        \ FCEDIT
        \ HISTFILE
        \ HISTSIZE
        \ HISTTIMEFORMAT
        \ HOME
        \ IFS
        \ LANG
        \ LC_ALL
        \ LC_COLLATE
        \ LC_CTYPE
        \ LC_MESSAGES
        \ LC_NUMERIC
        \ LINENO
        \ MAIL
        \ MAILCHECK
        \ MAILPATH
        \ NLSPATH
        \ OLDPWD
        \ OPTARG
        \ OPTERR
        \ OPTIND
        \ PATH
        \ PS1
        \ PS2
        \ PS3
        \ PS4
        \ PWD

  " Core syntax/sh.vim thinks 'until' is a POSIX control structure keyword,
  " but it isn't.  Reset shRepeat and rebuild it with just 'while'.  I only
  " sort-of understand what this does, but it works.
  syntax clear shRepeat
  syntax region shRepeat
        \ matchgroup=shLoop
        \ start='\<while\_s' end='\<do\>'me=e-2
        \ contains=@shLoopList

  " Run some clustering that core syntax/sh.vim thinks doesn't apply to POSIX;
  " this fixes while loops so they can be within other blocks.
  syntax cluster shCaseList add=shRepeat
  syntax cluster shFunctionList add=shRepeat

  " ${foo%bar}, ${foo%%bar}, ${foo#bar}, and ${foo##bar} are all valid forms
  " of parameter expansion in POSIX, but sh.vim makes them conditional on
  " Bash or Korn shell.  We reinstate them (slightly adapted) here.
  syntax match shDerefOp contained
        \ '##\|#\|%%\|%'
        \ nextgroup=@shDerefPatternList
  syntax match shDerefPattern contained
        \ '[^{}]\+'
        \ contains=shDeref,shDerefSimple,shDerefPattern,shDerefString,shCommandSub,shDerefEscape
        \ nextgroup=shDerefPattern
  syntax region shDerefPattern contained
        \ start='{' end='}'
        \ contains=shDeref,shDerefSimple,shDerefString,shCommandSub
        \ nextgroup=shDerefPattern

endif

" Some corrections for highlighting specific to the Bash mode
if exists('b:is_bash')

  " I don't like bashAdminStatement; these are not keywords, they're just
  " strings for init scripts.
  syntax clear bashAdminStatement

  " Reduce bashStatement down to just builtins; highlighting 'grep' is not
  " very useful.  This list was taken from `compgen -A helptopic` on Bash
  " 4.4.5.
  syntax clear bashStatement
  syntax keyword bashStatement
        \ .
        \ :
        \ alias
        \ bg
        \ bind
        \ break
        \ builtin
        \ caller
        \ cd
        \ command
        \ compgen
        \ complete
        \ compopt
        \ continue
        \ coproc
        \ dirs
        \ disown
        \ echo
        \ enable
        \ eval
        \ exec
        \ exit
        \ false
        \ fc
        \ fg
        \ function
        \ getopts
        \ hash
        \ help
        \ history
        \ jobs
        \ kill
        \ let
        \ logout
        \ mapfile
        \ popd
        \ printf
        \ pushd
        \ pwd
        \ read
        \ readarray
        \ readonly
        \ return
        \ select
        \ set
        \ shift
        \ shopt
        \ source
        \ suspend
        \ test
        \ time
        \ times
        \ trap
        \ true
        \ type
        \ ulimit
        \ umask
        \ unalias
        \ until
        \ variables
        \ wait
endif