Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Vim syntax file for zsh

* Felix Rosencrantz <f.rosencrantz@xxxxxxxxx> [Dec 03, 2004 15:11]:
> Viewing completion function files under the cvs tree (e.g.
> Completion/Unix/Type/_files)  It seems like both of these syntax files
> had some problems with variable syntax.  Though Nikolai's seems to
> behave better.

It should, it doesn't try to highlight very much.

> Though Nikolai's had problems with variables not listed in braces
> (e.g. $#foo would only highlight the '$#'). It would only highlight
> the first character.

This is intentional.  Well, at least somewhat.  The $# in the part of
$#foo shouldn't be, I fixed that (but $# on its own should).

My reasoning is that you want "special" variables, such as $#, $n, and
the like to be highlighted, as well as advanced stuff such as ${...}
allows, but not necesarilly $foo, as the highlighting can become rather
cluttered, as in the standard Vim one.

The question is, does this make sense to most other people?

I'm actually thinking that none of them should be highlighted, not even
those that are "special".  Any input?

Three choices:

1.  Highlight all variable references alike.
2.  Highlight only "special" variables, such as $$, $#, and $n.
3.  Highlight no variable references at all.

If you look at the C syntax definition, no variable references are
highlighted, and this isn't only due to the fact that it is infinitely
much harder to determine what a variable in C (no marking $) is but also
to keep things simple and clean.  Variables are everywhere, so
highlighting all of them makes the display cluttered.

> I would like to see the syntax files be able to handle more of the
> parameter expansion  syntax and flags (like found in man zshexpn) .
> It would be nice if it could color things like:
> "${(@j:|:M)${(@)tmp#-}#?}" or show an error if it saw code like
> "${(b)tmp} ('b' is not a valid flag).

I have come, after much experimentation and many syntax definitions for
many different kinds of formats and languages, that trying to do syntax
validation in a syntax definition is not the right place.  One may
debate that certain errors can and should be caught in the syntax
definition, but I hardly see ${(b)tmp} to be an instance of this case.
The problem is that once the 'b' becomes a valid flag, the syntax
definition is more or less useless, as it will report false information
to its user.

> Also things like the first line #compdef or #autoload lines are not
> marked or highlighted in any way, other than as a comment.

Hm, that's an interesting suggestion.  The #! /bin/shell stuff should
perhaps also be highlighted.  It's simple enough to add.

Could you perhaps point me to where I find the exact information that
may appear in a beginning comment line?  I always seem to lose that
page in the info pages.

::: name: Nikolai Weibull    :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA    :: loc atm: Gothenburg, Sweden    :::
::: page: www.pcppopper.org  :: fun atm: gf,lps,ruby,lisp,war3 :::
" Vim syntax file
" Language:	    Zsh shell script
" Maintainer:	    Nikolai Weibull <source@xxxxxxxxxxxxx>
" URL:		    http://www.pcppopper.org/vim/syntax/pcp/zsh/
" Latest Revision:  2004-12-04
" arch-tag:	    2e2c7097-99cb-4b87-a771-3a819b69995e

if version < 600
  syntax clear
elseif exists("b:current_syntax")

" Set iskeyword since we need `-' (and potentially others) in keywords.
" For version 5.x: Set it globally
" For version 6.x: Set it locally
if version >= 600
  command -nargs=1 SetIsk setlocal iskeyword=<args>
  command -nargs=1 SetIsk set iskeyword=<args>
SetIsk @,48-57,_,-
delcommand SetIsk

" Todo
syn keyword zshTodo         contained TODO FIXME XXX NOTE

" Comments
syn region  zshComment      matchgroup=zshComment start='\(^\|\s\)#' end='$' contains=zshTodo

" Strings
syn match   zshQuoted       '\\.'
syn region  zshString       matchgroup=zshString start=+"+ skip=+\\"+ end=+"+ contains=zshQuoted
syn region  zshString       matchgroup=zshString start=+'+ end=+'+
syn match   zshJobSpec      '%\(\d\+\|?\=\w\+\|[%+-]\)'

" Precommand Modifiers
syn keyword zshPrecommand   noglob nocorrect exec command builtin
syn match   zshPrecommand   '\<-\s'

" Delimiters
syn keyword zshDelimiter    do done

" Conditionals
syn keyword zshConditional  if then elif else fi case in esac select

" Exceptions
syn keyword zshException    always

" Loops
syn keyword zshRepeat       for while until repeat foreach

" Keywords
syn keyword zshKeyword      function time

" Functions
syn match   zshFunction     '\k\+\s*\ze()'

" Redirections
syn match   zshRedir        '\d\=\(<\|<>\|<<<\|<&\s*[0-9p-]\=\)'
syn match   zshRedir        '\d\=\(>\|>>\|>&\s*[0-9p-]\=\|&>\|>>&\|&>>\)[|!]\='
syn match   zshRedir        '|&\='

" Here Documents
if version < 600
    " Do nothing for now TODO: do something
    syn region	zshHereDoc  matchgroup=zshRedir start='<<\s*\z(\S*\)' end='^\z1$'
    syn region	zshHereDoc  matchgroup=zshRedir start='<<-\s*\z(\S*\)' end='^\s*\z1$'
    syn region	zshHereDoc  matchgroup=zshRedir start='<<\s*\z(["'\\]\S*\)'  end='^\z1$'
    syn region	zshHereDoc  matchgroup=zshRedir start='<<-\s*\z(["'\\]\S*\)' end='^\s*\z1$'

" Variables
syn match   zshShellVar     '\$[!#$*@?_-]\w\@!'
syn match   zshShellVar     '\$\d\+\>'
"syn match   zshShellVar     '$\(ARGC\|argv\|status\|pipestatus\|CPUTYPE\|EGID\|EUID\|ERRNO\|GID\|HOST\|

" Commands
syn match   zshCommands     '\s[.:]\s'
syn keyword zshCommands     alias autoload bg bindkey break bye cap cd chdir
syn keyword zshCommands     clone comparguments compcall compctl compdescribe
syn keyword zshCommands     compfiles compgroups compquote comptags comptry
syn keyword zshCommands     compvalues continue declare dirs disable disown
syn keyword zshCommands     echo echotc echoti emulate enable eval exec exit
syn keyword zshCommands     export false fc fg functions getcap getln
syn keyword zshCommands     getopts hash history jobs kill let limit
syn keyword zshCommands     log logout popd print printf pushd pushln
syn keyword zshCommands     pwd r read readonly rehash return sched set
syn keyword zshCommands     setcap setopt shift source stat suspend test times
syn keyword zshCommands     trap true ttyctl type ulimit umask unalias
syn keyword zshCommands     unfunction unhash unlimit unset unsetopt vared
syn keyword zshCommands     wait whence where which zcompile zformat zftp zle
syn keyword zshCommands     zmodload zparseopts zprof zpty zregexparse zsocket
syn keyword zshCommands     zstyle ztcp

" Types
syn keyword zshTypes        float integer local typeset

" Switches
" XXX: this may be too much
syn match   zshSwitches     '\s\zs--\=[a-zA-Z0-9-]\+'

" Numbers
syn match   zshNumber       '\<-\=\(\d\+#\|0x\=\)\=\d\+\>'
syn match   zshNumber       '\<-\=\d\+.\d\+\>'

" Substitution
syn region  zshSubst        matchgroup=zshShellVar transparent start='\$(' end=')'
syn region  zshSubst        matchgroup=zshShellVar transparent start='\$((' end='))'
syn region  zshSubst	    matchgroup=zshShellVar start='\${' skip='\\}' end='}' contains=zshSubst

" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_zsh_syn_inits")
  if version < 508
    let did_zsh_syn_inits = 1
    command -nargs=+ HiLink hi link <args>
    command -nargs=+ HiLink hi def link <args>

  HiLink zshTodo          Todo
  HiLink zshComment       Comment
  HiLink zshQuoted        SpecialChar
  HiLink zshString        String
  HiLink zshJobSpec       Special
  HiLink zshPrecommand    Special
  HiLink zshDelimiter     Keyword
  HiLink zshConditional   Conditional
  HiLink zshException	  Exception
  HiLink zshRepeat        Repeat
  HiLink zshKeyword       Keyword
  HiLink zshFunction      Function
  HiLink zshHereDoc	  String
  HiLink zshRedir         Operator
  HiLink zshShellVar      Identifier
  HiLink zshCommands      Keyword
  HiLink zshTypes         Type
  HiLink zshSwitches      Special
  HiLink zshNumber        Number
  HiLink zshSubst	  Identifier

  delcommand HiLink

let b:current_syntax = "zsh"

" vim: set sts=2 sw=2:

Messages sorted by: Reverse Date, Date, Thread, Author