'main': Introduce style fallback for the command word.

Fixes #316.

* danielsh/i316-v2:
  'main': Enable fallback to the 'arg0' style.
  'main': Set fallback style for the 'arg0' style.
This commit is contained in:
Daniel Shahaf 2016-09-28 13:25:05 +00:00
commit 094329eb14
3 changed files with 92 additions and 7 deletions

View File

@ -44,6 +44,7 @@ This highlighter defines the following styles:
* `assign` - parameter assignments * `assign` - parameter assignments
* `redirection` - redirection operators (`<`, `>`, etc) * `redirection` - redirection operators (`<`, `>`, etc)
* `comment` - comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`) * `comment` - comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`)
* `arg0` - a command word other than one of those enumrated above (other than a command, precommand, alias, function, or shell builtin command).
* `default` - everything else * `default` - everything else
To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`, To override one of those styles, change its entry in `ZSH_HIGHLIGHT_STYLES`,
@ -65,4 +66,26 @@ The syntax for values is the same as the syntax of "types of highlighting" of
the zsh builtin `$zle_highlight` array, which is documented in [the `zshzle(1)` the zsh builtin `$zle_highlight` array, which is documented in [the `zshzle(1)`
manual page][zshzle-Character-Highlighting]. manual page][zshzle-Character-Highlighting].
### Useless trivia
#### Forward compatibility.
zsh-syntax-highlighting attempts to be forward-compatible with zsh.
Specifically, we attempt to facilitate highlighting _command word_ types that
had not yet been invented when this version of zsh-syntax-highlighting was
released.
A _command word_ is something like a function name, external command name, et
cetera. (See
[Simple Commands & Pipelines in `zshmisc(1)`][zshmisc-Simple-Commands-And-Pipelines]
for a formal definition.)
If a new _kind_ of command word is ever added to zsh — something conceptually
different than "function" and "alias" and "external command" — then command words
of that (new) kind will be highlighted by the style `arg0_$kind`,
where `$kind` is the output of `type -w` on the new kind of command word. If that
style is not defined, then the style `arg0` will be used instead.
[zshmisc-Simple-Commands-And-Pipelines]: http://zsh.sourceforge.net/Doc/Release/Shell-Grammar.html#Simple-Commands-_0026-Pipelines
[zshzle-Character-Highlighting]: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting [zshzle-Character-Highlighting]: http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Character-Highlighting

View File

@ -32,17 +32,11 @@
: ${ZSH_HIGHLIGHT_STYLES[default]:=none} : ${ZSH_HIGHLIGHT_STYLES[default]:=none}
: ${ZSH_HIGHLIGHT_STYLES[unknown-token]:=fg=red,bold} : ${ZSH_HIGHLIGHT_STYLES[unknown-token]:=fg=red,bold}
: ${ZSH_HIGHLIGHT_STYLES[reserved-word]:=fg=yellow} : ${ZSH_HIGHLIGHT_STYLES[reserved-word]:=fg=yellow}
: ${ZSH_HIGHLIGHT_STYLES[alias]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[suffix-alias]:=fg=green,underline} : ${ZSH_HIGHLIGHT_STYLES[suffix-alias]:=fg=green,underline}
: ${ZSH_HIGHLIGHT_STYLES[builtin]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[function]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[command]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline} : ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none} : ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green}
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=} : ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=}
: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline}
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=} : ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=}
: ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue}
: ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue}
@ -58,6 +52,7 @@
: ${ZSH_HIGHLIGHT_STYLES[assign]:=none} : ${ZSH_HIGHLIGHT_STYLES[assign]:=none}
: ${ZSH_HIGHLIGHT_STYLES[redirection]:=none} : ${ZSH_HIGHLIGHT_STYLES[redirection]:=none}
: ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold} : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold}
: ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green}
# Whether the highlighter should be called or not. # Whether the highlighter should be called or not.
_zsh_highlight_highlighter_main_predicate() _zsh_highlight_highlighter_main_predicate()
@ -71,6 +66,33 @@ _zsh_highlight_main_add_region_highlight() {
integer start=$1 end=$2 integer start=$1 end=$2
shift 2 shift 2
if (( $+argv[2] )); then
# Caller specified inheritance explicitly.
else
# Automate inheritance.
typeset -A fallback_of; fallback_of=(
alias arg0
suffix-alias arg0
builtin arg0
function arg0
command arg0
precommand arg0
hashed-command arg0
path_prefix path
# The path separator fallback won't ever be used, due to the optimisation
# in _zsh_highlight_main_highlighter_highlight_path_separators().
path_pathseparator path
path_prefix_pathseparator path_prefix
)
local needle=$1 value
while [[ -n ${value::=$fallback_of[$needle]} ]]; do
unset "fallback_of[$needle]" # paranoia against infinite loops
argv+=($value)
needle=$value
done
fi
# The calculation was relative to $PREBUFFER$BUFFER, but region_highlight is # The calculation was relative to $PREBUFFER$BUFFER, but region_highlight is
# relative to $BUFFER. # relative to $BUFFER.
(( start -= $#PREBUFFER )) (( start -= $#PREBUFFER ))
@ -581,7 +603,7 @@ _zsh_highlight_highlighter_main_paint()
fi fi
fi fi
;; ;;
*) _zsh_highlight_main_add_region_highlight $start_pos $end_pos commandtypefromthefuture-$res *) _zsh_highlight_main_add_region_highlight $start_pos $end_pos arg0_$res arg0
already_added=1 already_added=1
;; ;;
esac esac

View File

@ -0,0 +1,40 @@
#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2016 zsh-syntax-highlighting contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
# may be used to endorse or promote products derived from this software without specific prior
# written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
# -------------------------------------------------------------------------------------------------
_zsh_highlight_add_highlight()
{
region_highlight+=("$1 $2 ${(j.,.)argv[3,-1]}")
}
BUFFER='type'
expected_region_highlight=(
'1 4 builtin,arg0' # type
)