From b4537a972eedb920fc61cabb359afd3ec7ea4b34 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 16 Aug 2016 17:24:58 +0000 Subject: [PATCH 1/2] 'main': Set fallback style for the 'arg0' style. The fallback style name uses '_' in anticipation of a future auto-fallback feature keyed on style names. 'arg0' was previously known as 'commandtypefromthefuture'. --- docs/highlighters/main.md | 23 +++++++++++++++++++++++ highlighters/main/main-highlighter.zsh | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/highlighters/main.md b/docs/highlighters/main.md index 4793fed..bf83820 100644 --- a/docs/highlighters/main.md +++ b/docs/highlighters/main.md @@ -44,6 +44,7 @@ This highlighter defines the following styles: * `assign` - parameter assignments * `redirection` - redirection operators (`<`, `>`, etc) * `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 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)` 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 diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a810eff..095a8fd 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -58,6 +58,7 @@ : ${ZSH_HIGHLIGHT_STYLES[assign]:=none} : ${ZSH_HIGHLIGHT_STYLES[redirection]:=none} : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold} +: ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green} # Whether the highlighter should be called or not. _zsh_highlight_main_highlighter_predicate() @@ -488,7 +489,7 @@ _zsh_highlight_main_highlighter() 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 ;; esac From bccc3dc26943949e5b2f9525e6827440507b03ac Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 20 Aug 2016 09:31:13 +0000 Subject: [PATCH 2/2] 'main': Enable fallback to the 'arg0' style. --- highlighters/main/main-highlighter.zsh | 33 +++++++++++++---- highlighters/main/test-data/inheritance.zsh | 40 +++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 highlighters/main/test-data/inheritance.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 095a8fd..9c7fb12 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -32,17 +32,11 @@ : ${ZSH_HIGHLIGHT_STYLES[default]:=none} : ${ZSH_HIGHLIGHT_STYLES[unknown-token]:=fg=red,bold} : ${ZSH_HIGHLIGHT_STYLES[reserved-word]:=fg=yellow} -: ${ZSH_HIGHLIGHT_STYLES[alias]:=fg=green} : ${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[commandseparator]:=none} -: ${ZSH_HIGHLIGHT_STYLES[hashed-command]:=fg=green} : ${ZSH_HIGHLIGHT_STYLES[path]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=} -: ${ZSH_HIGHLIGHT_STYLES[path_prefix]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=} : ${ZSH_HIGHLIGHT_STYLES[globbing]:=fg=blue} : ${ZSH_HIGHLIGHT_STYLES[history-expansion]:=fg=blue} @@ -72,6 +66,33 @@ _zsh_highlight_main_add_region_highlight() { integer start=$1 end=$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 # relative to $BUFFER. (( start -= $#PREBUFFER )) diff --git a/highlighters/main/test-data/inheritance.zsh b/highlighters/main/test-data/inheritance.zsh new file mode 100644 index 0000000..e851ca3 --- /dev/null +++ b/highlighters/main/test-data/inheritance.zsh @@ -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 +)