From 52615f646626e93a1560d3bcebcdf122f246e59a Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 11 Dec 2016 16:22:36 +1300 Subject: Still untangling the shell highlighting mess --- vim/after/ftdetect/sh.vim | 7 ++++ vim/after/ftplugin/sh.vim | 18 +++------ vim/after/syntax/sh.vim | 98 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 vim/after/syntax/sh.vim (limited to 'vim/after') diff --git a/vim/after/ftdetect/sh.vim b/vim/after/ftdetect/sh.vim index 73bbfffe..1d87d43e 100644 --- a/vim/after/ftdetect/sh.vim +++ b/vim/after/ftdetect/sh.vim @@ -15,3 +15,10 @@ autocmd BufNewFile,BufRead \ **/.dotfiles/sh/**,.shinit,.shrc,.xinitrc,/etc/default/* \ let b:is_posix = 1 | \ setlocal filetype=sh + +" If we determined something is b:is_kornshell, tack on b:is_ksh as well so we +" can still tease out what is actually a kornshell script after sh.vim is done +" changing our options for us; it conflates POSIX with Korn shell. +if exists('b:is_kornshell') + let b:is_ksh = 1 +endif diff --git a/vim/after/ftplugin/sh.vim b/vim/after/ftplugin/sh.vim index 61ff9ae8..2b73611e 100644 --- a/vim/after/ftplugin/sh.vim +++ b/vim/after/ftplugin/sh.vim @@ -1,15 +1,9 @@ -" Don't highlight errors for me; something not quite right here. The syntax -" highlighter seems to flag '/baz' in '"${foo:-"$bar"/baz}"' as an error, and -" I'm pretty sure it's not. -let g:sh_noerror = 1 - -" If the file is not already tagged as a shell type, default to POSIX shell, -" as I never write Bourne. I would set g:is_posix here rather than b:is_posix, -" but sh.vim makes some weird assumptions about me actually meaning ksh for -" some reason when I do that. -if !exists('b:is_kornshell') && !exists('b:is_bash') && !exists('b:is_posix') - let b:is_posix = 1 -endif +" Default to POSIX shell, as I never write Bourne, and if I write Bash or Ksh +" it'll be denoted with either a shebang or an appropriate extension. At the +" time of writing, changing this also prompts sh.vim to set g:is_kornshell, +" which is absurd, and requires a bit more massaging in after/syntax/sh.vim to +" turn off some unwanted stuff. +let g:is_posix = 1 " Use han(1df) as a man(1) wrapper for Bash files if available if exists('b:is_bash') && executable('han') diff --git a/vim/after/syntax/sh.vim b/vim/after/syntax/sh.vim new file mode 100644 index 00000000..e8b41ba4 --- /dev/null +++ b/vim/after/syntax/sh.vim @@ -0,0 +1,98 @@ +" If g:is_posix is set, g:is_kornshell is probably set too, a strange decision +" by sh.vim. No matter; we can tease out whether this is actually a Korn shell +" script using our own b:is_ksh flag set at the end of +" ~/.vim/after/ftdetect/sh.vim, and if it isn't, we'll throw away the +" highlighting groups for ksh. +if exists('g:is_posix') && exists('g:is_kornshell') && !exists('b:is_ksh') + syntax clear kshSpecialVariables + syntax clear kshStatement +endif + +" Some corrections for highlighting if we have any of POSIX, Bash, or Ksh +if exists('g:is_posix') || exists('b:is_bash') || exists('b:is_ksh') + + " The syntax highlighter seems to flag '/baz' in '"${foo:-"$bar"/baz}"' as an + " error, and I'm pretty sure it's not, 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 + +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 -- cgit v1.2.3