" Vim syntax file
" Language:	shell (sh) Korn shell (ksh) bash (sh)
" Maintainer:	Lennart Schultz <les@dmi.min.dk>
" Last change:	1997 May 15
"
" Using the following VIM variables:
" is_kornshell               if defined enhance with kornshell syntax
" is_bash                    if defined enhance with bash syntax
" highlight_balanced_quotes  if defined highlight balanced single quotes
"                               inside a pair of double quotes
" highlight_function_name    if defined do also highlight the function name
"                               in function declarations

" Remove any old syntax stuff hanging around
syntax clear

" A bunch of useful sh keywords
syntax keyword shStatement    break cd chdir continue echo eval exec 
syntax keyword shStatement    exit export kill newgrp pwd read readonly
syntax keyword shStatement    return set shift test trap
syntax keyword shStatement    ulimit umask unset wait
syntax keyword shConditional  if else esac case then elif fi in
syntax keyword shRepeat       while for do done
syntax keyword shShellVariables	HOME PATH CDPATH SHELL LC_TYPE LC_MESSAGE
syntax keyword shShellVariables	MAIL MAILCHECK PS1 PS2 IFS SHACCT 
syntax match   shSpecialShellVar "\$[-#@*$?!0-9]" 

syntax match   shSource       "^[ 	]*[.][ 	]\+\|[ 	]\+[.][ 	]\+" 
syntax region  shColon        start="^[ 	]*:" end="$\|#" contains ALLBUT shFunction

if exists("is_kornshell") || exists("is_bash")
syntax keyword shStatement    bg fg jobs stop suspend 
syntax keyword shStatement    function
syntax keyword shStatement    alias fc getopts hash history let print time 
syntax keyword shStatement    times type typeset whence unalias 
syntax keyword shRepeat       select until
syntax keyword shShellVariables	COLUMNS EDITOR ENV FCEDIT LINES 
syntax keyword shShellVariables	OLDPWD PPID PS3 PS4
syntax keyword shShellVariables	PWD RANDOM REPLY SECONDS 
syntax keyword shShellVariables	TMOUT VISUAL
syntax keyword shShellVariables	OPTARG OPTIND 
syntax keyword shShellVariables	HISTSIZE HISTFILE
syntax keyword shShellVariables	LANG LC_ALL LC_COLLATE
syntax keyword shSetVariables allexport errexit emacs
syntax keyword shSetVariables ignoreeof keyword monitor
syntax keyword shSetVariables noclobber noexec noglob notify
syntax keyword shSetVariables nounset privileged verbose vi xtrace
if exists("is_bash")
syntax keyword shStatement    source bind builtin declare dirs
syntax keyword shStatement    disown enable help history local
syntax keyword shStatement    logout popd pushd shopt
syntax keyword shShellVariables	EUID BASH BASH_VERSION BASH_INFO
syntax keyword shShellVariables	SHLVL HISTCMD DIRSTACK PIPESTATUS
syntax keyword shShellVariables	HOSTNAME HOSTTYPE OSTYPE MACHTYPE
syntax keyword shShellVariables	SHELLOPTS TIMEFORMAT BASH_ENV OPTERR 
syntax keyword shShellVariables	PROMPT_COMMAND IGNOREEOF FIGNORE
syntax keyword shShellVariables	GLOBIGNORE INPUTRC HISTCONTROL UID
syntax keyword shShellVariables	histchars HOSTFILE auto_resume HISTFILESIZE
syntax keyword shSetVariables   braceexpand histexpand
syntax keyword shSetVariables   onecmd physical posix
else
syntax keyword shStatement    login newgrp
syntax keyword shShellVariables	ERRNO FPATH LINENO MAILPATH NLSPATH
syntax keyword shSetVariables bgnice gmacs markdirs nolog trackall viraw
endif
endif
"This will save some syntax mismatch
syntax match   shBackQuote    "\\`.*'"

" Following is worth to notice:
" command substitution, file redirection and functions (so these features turns red)

if exists("highlight_function_name")
syntax match   shFunction     "[a-zA-Z0-9_]*[ 	]*[(][)]"
else
syntax match   shFunction     "[(][)]"
endif
syntax match   shCurlyList    "[{][ ]\=\|\;\=\}"
syntax region  shCommandSub   start="`" skip="\\`" end="`" contains ALLBUT shFunction 
syntax match   shRedirStdOut2 ">>-\="
syntax match   shRedirStdOut1 "[0-9]\=>\(\(&[-0-9]\)\=\|[^>]E-1\)"
syntax match   shRedirStdIn2  "<<-\="
syntax match   shRedirStdIn1  "[0-9]\=<\(\(&[-0-9]\)\=\|[^<]E-1\)"

syntax keyword shTodo contained TODO

syntax region  shDerefOpr     start="\${" end="}" contains shShellVariables shIdentifier
syntax match   shDerefIdentifier "\$\<[a-zA-Z_][a-zA-Z0-9_]*\>"
syntax match   shOperator     "[][&;|)(]"
syntax region  shTest         start="\[" skip="\\$" end="\]" contains shString shTestOpr shDerefIdentifier shDerefOpr

" String and Character contstants
" Highlight special characters (those which have a backslash) differently
syntax match   shSpecial      contained "\\[0-9][0-9][0-9]\|\\[abcfnrtv\\]"

syntax match   shNumber       "-\=\<[0-9]\+\>"
syntax match   shComment      "#.*$" contains shTodo 

syntax region  shSinglequote start=+'+  skip=+\\'+  end=+'+  

if exists("highlight_balanced_quotes")
syntax region  shString       start=+"+  skip=+\\"+  end=+"+  contains ALLBUT shFunction  
else
syntax region  shString       start=+"+  skip=+\\"+  end=+"+  contains ALLBUT shFunction shSinglequote
endif

"Identifier should be last (it will hide other syntax classes if not)
syntax match   shIdentifier   "\<[a-zA-Z._][a-zA-Z0-9._]*\>"

if !exists("did_sh_syntax_inits")
  let did_sh_syntax_inits = 1
  " The default methods for highlighting.  Can be overridden later
  highlight link shSinglequote		shString
  highlight link shConditional		Conditional
  highlight link shRepeat		Repeat
  highlight link shSource		shStatement
  highlight link shColon		shStatement
  highlight link shtest 		shStatement
  highlight link shFunctionName		Function
  highlight link shCurlyList		shFunction
  highlight link shCommandSub		shFunction
  highlight link shRedirStdOut1		shFunction
  highlight link shRedirStdOut2		shFunction
  highlight link shRedirStdIn1		shFunction
  highlight link shRedirStdIn2		shFunction
  highlight link shSetVariables		shShellVariables
  highlight link shSpecialShellVar	shShellVariables
  highlight link shTestOpr		shOperator
  highlight link shDerefOpr		shSpecial
  highlight link shDerefIdentifier	shSpecial
  highlight link shStatement		Statement
  highlight link shNumber		Number
  highlight link shString		String
  highlight link shComment		Comment
  highlight link shSpecial		Special
  highlight link shTodo			Todo
 " highlight link shIdentifier		Identifier
  highlight shOperator		term=underline ctermfg=6 guifg=Purple gui=bold
  highlight shShellVariables	term=underline ctermfg=2 guifg=SeaGreen gui=bold
  highlight shFunction		term=bold ctermbg=1 guifg=Red gui=bold
endif
