From 77fe96772e18a93a64cacb295c2ca1a73155df53 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 08:16:22 +0000 Subject: [PATCH 01/24] 'main': Expand aliases first. (Issue #264.) This commit causes an alias to an invalid command to be highlighted as an error (unknown-token). --- highlighters/main/main-highlighter.zsh | 43 +++++++++++++++---- .../main/test-data/alias-comment1.zsh | 37 ++++++++++++++++ .../main/test-data/alias-comment2.zsh | 37 ++++++++++++++++ highlighters/main/test-data/off-by-one.zsh | 2 +- 4 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 highlighters/main/test-data/alias-comment1.zsh create mode 100644 highlighters/main/test-data/alias-comment2.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a0cd600..052317c 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -214,7 +214,7 @@ _zsh_highlight_highlighter_main_paint() fi ## Variable declarations and initializations - local start_pos=0 end_pos highlight_glob=true arg style + local start_pos=0 end_pos highlight_glob=true arg arg_raw style local in_array_assignment=false # true between 'a=(' and the matching ')' typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS @@ -322,6 +322,9 @@ _zsh_highlight_highlighter_main_paint() args=(${(z)buf}) fi for arg in $args; do + # Save an unmunged copy of the current word. + arg_raw="$arg" + # Initialize $next_word. if (( in_redirection )); then (( --in_redirection )) @@ -432,6 +435,30 @@ _zsh_highlight_highlighter_main_paint() fi fi + # Expand aliases. + # TODO: this should be done iteratively, e.g., 'alias x=y y=z z=w\n x' + # And then the entire 'alias' branch of the 'case' statement should + # be done here. + () { + # TODO: path expansion should happen _after_ alias expansion + _zsh_highlight_main_highlighter_expand_path $arg + local expanded_arg="$REPLY" + _zsh_highlight_main__type ${expanded_arg} + } + local res="$REPLY" + if [[ $res == "alias" ]]; then + _zsh_highlight_main__resolve_alias $arg + () { + # Use a temporary array to ensure the subscript is interpreted as + # an array subscript, not as a scalar subscript + local -a reply + # TODO: the ${interactive_comments+set} path needs to skip comments; see test-data/alias-comment1.zsh + reply=( ${interactive_comments-${(z)REPLY}} + ${interactive_comments+${(zZ+c+)REPLY}} ) + arg=$reply[1] + } + fi + # Special-case the first word after 'sudo'. if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]] && [[ $arg != -* ]]; then @@ -472,10 +499,6 @@ _zsh_highlight_highlighter_main_paint() next_word+=':sudo_opt:' next_word+=':start:' else - _zsh_highlight_main_highlighter_expand_path $arg - local expanded_arg="$REPLY" - _zsh_highlight_main__type ${expanded_arg} - local res="$REPLY" () { # Special-case: command word is '$foo', like that, without braces or anything. # @@ -548,8 +571,9 @@ _zsh_highlight_highlighter_main_paint() ;; 'suffix alias') style=suffix-alias;; alias) () { + # Make sure to use $arg_raw here, rather than $arg. integer insane_alias - case $arg in + case $arg_raw in # Issue #263: aliases with '=' on their LHS. # # There are three cases: @@ -563,12 +587,13 @@ _zsh_highlight_highlighter_main_paint() esac if (( insane_alias )); then style=unknown-token + # Calling 'type' again; since __type memoizes the answer, this call is just a hash lookup. + elif _zsh_highlight_main__type "$arg"; [[ $REPLY == 'none' ]]; then + style=unknown-token else # The common case. style=alias - _zsh_highlight_main__resolve_alias $arg - local alias_target="$REPLY" - [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$alias_target"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg) + [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg_raw"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg_raw) fi } ;; diff --git a/highlighters/main/test-data/alias-comment1.zsh b/highlighters/main/test-data/alias-comment1.zsh new file mode 100644 index 0000000..a2b56c6 --- /dev/null +++ b/highlighters/main/test-data/alias-comment1.zsh @@ -0,0 +1,37 @@ +# ------------------------------------------------------------------------------------------------- +# 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 +# ------------------------------------------------------------------------------------------------- + +# see alias-comment2.zsh +setopt interactivecomments +alias x=$'# foo\npwd' +BUFFER='x' + +expected_region_highlight=( + "1 1 alias 'interactivecomments applies to aliases'" # x becomes pwd +) diff --git a/highlighters/main/test-data/alias-comment2.zsh b/highlighters/main/test-data/alias-comment2.zsh new file mode 100644 index 0000000..e9ecfca --- /dev/null +++ b/highlighters/main/test-data/alias-comment2.zsh @@ -0,0 +1,37 @@ +# ------------------------------------------------------------------------------------------------- +# 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 +# ------------------------------------------------------------------------------------------------- + +# see alias-comment1.zsh +setopt NO_interactivecomments +alias x=$'# foo\npwd' +BUFFER='x' + +expected_region_highlight=( + "1 1 unknown-token" # x becomes # +) diff --git a/highlighters/main/test-data/off-by-one.zsh b/highlighters/main/test-data/off-by-one.zsh index 550c09f..3870e20 100644 --- a/highlighters/main/test-data/off-by-one.zsh +++ b/highlighters/main/test-data/off-by-one.zsh @@ -27,7 +27,7 @@ # vim: ft=zsh sw=2 ts=2 et # ------------------------------------------------------------------------------------------------- -alias a=A +alias a=: f() {} BUFFER='a;f;' From d21d3b31da796a7e14fc3abeb512185e21e78488 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 22:41:23 +0000 Subject: [PATCH 02/24] 'main': New XFail test for parameter expansion at command word. --- highlighters/main/test-data/commmand-parameter.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/highlighters/main/test-data/commmand-parameter.zsh b/highlighters/main/test-data/commmand-parameter.zsh index 355f890..0860e62 100644 --- a/highlighters/main/test-data/commmand-parameter.zsh +++ b/highlighters/main/test-data/commmand-parameter.zsh @@ -28,9 +28,11 @@ # ------------------------------------------------------------------------------------------------- local x=/usr/bin/env -BUFFER='$x "argument"' +local y=sudo +BUFFER='$x "argument"; $y' expected_region_highlight=( "1 2 command" # $x "4 13 double-quoted-argument" # "argument" + "16 17 precommand 'parameter expansion precedes precommand recognition'" # $y (sudo) ) From 40dcbfbcf6de66fd84f6fe7eb57f0ee0cccceeb9 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 22:41:23 +0000 Subject: [PATCH 03/24] 'main': Fix the last commit's issue concerning parameter expansion at command word. --- highlighters/main/main-highlighter.zsh | 37 ++++++++++--------- .../main/test-data/commmand-parameter.zsh | 2 +- .../main/test-data/path-dollared-word3.zsh | 4 +- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 052317c..51827d2 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -459,6 +459,26 @@ _zsh_highlight_highlighter_main_paint() } fi + # Expand parameters. + # + # ### For now, expand just '$foo', like that, without braces or anything. + () { + # That's not entirely correct --- if the parameter's value happens to be a reserved + # word, the parameter expansion will be highlighted as a reserved word --- but that + # incorrectness is outweighed by the usability improvement of permitting the use of + # parameters that refer to commands, functions, and builtins. + local -a match mbegin mend + local MATCH; integer MBEGIN MEND + if [[ $res == none ]] && (( ${+parameters} )) && + [[ ${arg[1]} == \$ ]] && [[ ${arg:1} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && + (( ${+parameters[${MATCH}]} )) + then + arg=${(P)MATCH} + _zsh_highlight_main__type "$arg" + res=$REPLY + fi + } + # Special-case the first word after 'sudo'. if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]] && [[ $arg != -* ]]; then @@ -499,23 +519,6 @@ _zsh_highlight_highlighter_main_paint() next_word+=':sudo_opt:' next_word+=':start:' else - () { - # Special-case: command word is '$foo', like that, without braces or anything. - # - # That's not entirely correct --- if the parameter's value happens to be a reserved - # word, the parameter expansion will be highlighted as a reserved word --- but that - # incorrectness is outweighed by the usability improvement of permitting the use of - # parameters that refer to commands, functions, and builtins. - local -a match mbegin mend - local MATCH; integer MBEGIN MEND - if [[ $res == none ]] && (( ${+parameters} )) && - [[ ${arg[1]} == \$ ]] && [[ ${arg:1} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && - (( ${+parameters[${MATCH}]} )) - then - _zsh_highlight_main__type ${(P)MATCH} - res=$REPLY - fi - } case $res in reserved) # reserved word style=reserved-word diff --git a/highlighters/main/test-data/commmand-parameter.zsh b/highlighters/main/test-data/commmand-parameter.zsh index 0860e62..8218afc 100644 --- a/highlighters/main/test-data/commmand-parameter.zsh +++ b/highlighters/main/test-data/commmand-parameter.zsh @@ -34,5 +34,5 @@ BUFFER='$x "argument"; $y' expected_region_highlight=( "1 2 command" # $x "4 13 double-quoted-argument" # "argument" - "16 17 precommand 'parameter expansion precedes precommand recognition'" # $y (sudo) + "16 17 precommand" # $y (sudo) ) diff --git a/highlighters/main/test-data/path-dollared-word3.zsh b/highlighters/main/test-data/path-dollared-word3.zsh index fc3324e..ab6b9d3 100644 --- a/highlighters/main/test-data/path-dollared-word3.zsh +++ b/highlighters/main/test-data/path-dollared-word3.zsh @@ -29,9 +29,11 @@ # «/usr» at this point would be highlighted as path_prefix; so should # a parameter that expands to an equivalent string be highlighted. +# +# More complicated parameter substitutions aren't eval'd; issue #328. BUFFER='$PWD; ${PWD}' expected_region_highlight=( - "1 4 unknown-token" # $PWD - not eval'd; issue #328 + "1 4 path" # $PWD "7 12 unknown-token" # ${PWD} ) From 9c8b95171c0f74ccc0418af5f213e4572c1d8624 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 25 Dec 2017 05:20:40 +0000 Subject: [PATCH 04/24] =?UTF-8?q?'main':=20Following=20up=20to=20the=20las?= =?UTF-8?q?t=20commit,=20also=20highlight=20braced=20parameter=20expansion?= =?UTF-8?q?s:=20=C2=AB${foo}=C2=BB.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- highlighters/main/main-highlighter.zsh | 8 +++++++- highlighters/main/test-data/path-dollared-word3.zsh | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 51827d2..a1e709b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -469,8 +469,14 @@ _zsh_highlight_highlighter_main_paint() # parameters that refer to commands, functions, and builtins. local -a match mbegin mend local MATCH; integer MBEGIN MEND + local parameter_name + if [[ $arg[1] == '$' ]] && [[ ${arg[2]} == '{' ]] && [[ ${arg[-1]} == '}' ]]; then + parameter_name=${${arg:2}%?} + elif [[ $arg[1] == '$' ]]; then + parameter_name=${arg:1} + fi if [[ $res == none ]] && (( ${+parameters} )) && - [[ ${arg[1]} == \$ ]] && [[ ${arg:1} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && + [[ ${parameter_name} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && (( ${+parameters[${MATCH}]} )) then arg=${(P)MATCH} diff --git a/highlighters/main/test-data/path-dollared-word3.zsh b/highlighters/main/test-data/path-dollared-word3.zsh index ab6b9d3..a26b41c 100644 --- a/highlighters/main/test-data/path-dollared-word3.zsh +++ b/highlighters/main/test-data/path-dollared-word3.zsh @@ -35,5 +35,5 @@ BUFFER='$PWD; ${PWD}' expected_region_highlight=( "1 4 path" # $PWD - "7 12 unknown-token" # ${PWD} + "7 12 path" # ${PWD} ) From be63ff7e12f7b3357d4c4dee392321a3c926ec5b Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 07:37:59 +0000 Subject: [PATCH 05/24] 'main': Make sudo handling more generic. Part of issue #343. --- highlighters/main/main-highlighter.zsh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a1e709b..d62a0a3 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -233,6 +233,16 @@ _zsh_highlight_highlighter_main_paint() # ":" for 'then' local braces_stack + # $flags_with_argument is a set of letters, corresponding to the option letters + # that would be followed by a colon in a getopts specification. + local flags_with_argument + # $precommand_options maps precommand name to value of $flags_with_argument + # for that precommand. + local -A precommand_options + precommand_options=( + 'sudo' Cgprtu + ) + if [[ $zsyh_user_options[ignorebraces] == on || ${zsyh_user_options[ignoreclosebraces]:-off} == on ]]; then local right_brace_is_recognised_everywhere=false else @@ -497,7 +507,8 @@ _zsh_highlight_highlighter_main_paint() if [[ $this_word == *':sudo_opt:'* ]]; then case "$arg" in # Flag that requires an argument - '-'[Cgprtu]) this_word=${this_word//:start:/}; + '-'[$flags_with_argument]) + this_word=${this_word//:start:/}; next_word=':sudo_arg:';; # This prevents misbehavior with sudo -u -otherargument '-'*) this_word=${this_word//:start:/}; @@ -519,8 +530,9 @@ _zsh_highlight_highlighter_main_paint() elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then style=precommand - elif [[ "$arg" = "sudo" ]] && { _zsh_highlight_main__type sudo; [[ -n $REPLY && $REPLY != "none" ]] }; then + elif (( ${+precommand_options[$arg]} )) && { _zsh_highlight_main__type $arg; [[ -n $REPLY && $REPLY != "none" ]] }; then style=precommand + flags_with_argument=${precommand_options[$arg]} next_word=${next_word//:regular:/} next_word+=':sudo_opt:' next_word+=':start:' From b8c955d1645e37cfcd9f5225fdc8e0fa2b54a2bb Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 07:42:54 +0000 Subject: [PATCH 06/24] 'main': Permit $flags_with_argument to be empty. --- highlighters/main/main-highlighter.zsh | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index d62a0a3..6203398 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -505,17 +505,20 @@ _zsh_highlight_highlighter_main_paint() # Parse the sudo command line if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]]; then - case "$arg" in + if [[ -n $flags_with_argument ]] && + [[ $arg == '-'[$flags_with_argument] ]]; then # Flag that requires an argument - '-'[$flags_with_argument]) - this_word=${this_word//:start:/}; - next_word=':sudo_arg:';; + this_word=${this_word//:start:/} + next_word=':sudo_arg:' + elif [[ $arg == '-'* ]]; then + # Flag that requires no argument, or unknown flag. # This prevents misbehavior with sudo -u -otherargument - '-'*) this_word=${this_word//:start:/}; - next_word+=':start:'; - next_word+=':sudo_opt:';; - *) ;; - esac + this_word=${this_word//:start:/} + next_word+=':start:' + next_word+=':sudo_opt:' + else + # + fi elif [[ $this_word == *':sudo_arg:'* ]]; then next_word+=':sudo_opt:' next_word+=':start:' From 6ccba63f21b976a24b78db5a319ac762c2e52224 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 08:26:48 +0000 Subject: [PATCH 07/24] 'main': Update state machine docs. No functional change. --- highlighters/main/main-highlighter.zsh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 6203398..133864e 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -289,8 +289,9 @@ _zsh_highlight_highlighter_main_paint() # # The states are: # - :start: Command word - # - :sudo_opt: A leading-dash option to sudo (such as "-u" or "-i") - # - :sudo_arg: The argument to a sudo leading-dash option that takes one, + # - :sudo_opt: A leading-dash option to a precommand, whether it takes an + # argument or not. (Example: sudo's "-u" or "-i".) + # - :sudo_arg: The argument to a precommand's leading-dash option, # when given as a separate word; i.e., "foo" in "-u foo" (two # words) but not in "-ufoo" (one word). # - :regular: "Not a command word", and command delimiters are permitted. From ea09d483bec68733205c9f85d1e7b01e778325ad Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 07:50:38 +0000 Subject: [PATCH 08/24] 'main': Highlight the 'command' precommand. Fixes #343. --- highlighters/main/main-highlighter.zsh | 9 +++++---- highlighters/main/test-data/precommand2.zsh | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 133864e..2a12d15 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -240,6 +240,7 @@ _zsh_highlight_highlighter_main_paint() # for that precommand. local -A precommand_options precommand_options=( + 'command' '' 'sudo' Cgprtu ) @@ -261,7 +262,7 @@ _zsh_highlight_highlighter_main_paint() # ';;' ';&' ';|' ) ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS=( - 'builtin' 'command' 'exec' 'nocorrect' 'noglob' + 'builtin' 'exec' 'nocorrect' 'noglob' 'pkexec' # immune to #121 because it's usually not passed --option flags ) @@ -532,14 +533,14 @@ _zsh_highlight_highlighter_main_paint() style=reserved-word # de facto a reserved word, although not de jure next_word=':start:' elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word - if [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then - style=precommand - elif (( ${+precommand_options[$arg]} )) && { _zsh_highlight_main__type $arg; [[ -n $REPLY && $REPLY != "none" ]] }; then + if (( ${+precommand_options[$arg]} )) && { _zsh_highlight_main__type $arg; [[ -n $REPLY && $REPLY != "none" ]] }; then style=precommand flags_with_argument=${precommand_options[$arg]} next_word=${next_word//:regular:/} next_word+=':sudo_opt:' next_word+=':start:' + elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then + style=precommand else case $res in reserved) # reserved word diff --git a/highlighters/main/test-data/precommand2.zsh b/highlighters/main/test-data/precommand2.zsh index 3d8f332..75dceab 100644 --- a/highlighters/main/test-data/precommand2.zsh +++ b/highlighters/main/test-data/precommand2.zsh @@ -31,6 +31,6 @@ BUFFER='command -v ls' expected_region_highlight=( "1 7 precommand" # command - "9 10 single-hyphen-option 'issue #343'" # -v - "12 13 command 'issue #343'" # ls + "9 10 single-hyphen-option" # -v + "12 13 command" # ls ) From d1556027c1e3943ad565a38d009dfca214bf8e30 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 28 Jul 2016 08:22:40 +0000 Subject: [PATCH 09/24] 'main': Highlight 'nice'. Fixes #168. --- highlighters/main/main-highlighter.zsh | 1 + highlighters/main/test-data/precommand3.zsh | 41 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 highlighters/main/test-data/precommand3.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 2a12d15..3bbd219 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -241,6 +241,7 @@ _zsh_highlight_highlighter_main_paint() local -A precommand_options precommand_options=( 'command' '' + 'nice' n 'sudo' Cgprtu ) diff --git a/highlighters/main/test-data/precommand3.zsh b/highlighters/main/test-data/precommand3.zsh new file mode 100644 index 0000000..f8b0c78 --- /dev/null +++ b/highlighters/main/test-data/precommand3.zsh @@ -0,0 +1,41 @@ +# ------------------------------------------------------------------------------------------------- +# 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 +# ------------------------------------------------------------------------------------------------- + +BUFFER='nice -n10 ls; nice -n 10 ls' + +expected_region_highlight=( + "1 4 precommand" # nice + "6 9 single-hyphen-option" # -n10 + "11 12 command" # ls + "13 13 commandseparator" # ; + "15 18 precommand" # nice + "20 21 single-hyphen-option" # -n + "23 24 default" # 10 + "26 27 command" # ls +) From d838a29df342e9c02f2d006e63a5b1eb5210cfd7 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Thu, 15 Sep 2016 15:42:35 +0000 Subject: [PATCH 10/24] 'main': Highlight 'doas'. Fixes #365. --- highlighters/main/main-highlighter.zsh | 1 + 1 file changed, 1 insertion(+) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 3bbd219..5016f38 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -243,6 +243,7 @@ _zsh_highlight_highlighter_main_paint() 'command' '' 'nice' n 'sudo' Cgprtu + 'doas' aCu ) if [[ $zsyh_user_options[ignorebraces] == on || ${zsyh_user_options[ignoreclosebraces]:-off} == on ]]; then From 1fee620e6235d3d1789dd3b62e65f5618464dbea Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:02:39 +0000 Subject: [PATCH 11/24] 'main': Remove superfluous variable. No functional change. --- highlighters/main/main-highlighter.zsh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 5016f38..6298fd5 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -456,8 +456,7 @@ _zsh_highlight_highlighter_main_paint() () { # TODO: path expansion should happen _after_ alias expansion _zsh_highlight_main_highlighter_expand_path $arg - local expanded_arg="$REPLY" - _zsh_highlight_main__type ${expanded_arg} + _zsh_highlight_main__type "$REPLY" } local res="$REPLY" if [[ $res == "alias" ]]; then From cda30ca3b6da1a4d4131da3d0147000a13ecd63a Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:05:07 +0000 Subject: [PATCH 12/24] 'main': Correct a comment. --- highlighters/main/main-highlighter.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 6298fd5..4c500b1 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -474,7 +474,9 @@ _zsh_highlight_highlighter_main_paint() # Expand parameters. # - # ### For now, expand just '$foo', like that, without braces or anything. + # ### For now, expand just '$foo' or '${foo}', possibly with braces, but with + # ### no other features of the parameter expansion syntax. (No ${(x)foo}, + # ### no ${foo[x]}, no ${foo:-x}.) () { # That's not entirely correct --- if the parameter's value happens to be a reserved # word, the parameter expansion will be highlighted as a reserved word --- but that From 2c15b0e996ad24ab0517a978ce43d1eab92086c7 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:32:30 +0000 Subject: [PATCH 13/24] 'main': Learn $flags_sans_arguments and use that to parse '-xy foo' correctly where -x takes no argument and -y does. --- highlighters/main/main-highlighter.zsh | 25 ++++++++----- highlighters/main/test-data/precommand4.zsh | 39 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 highlighters/main/test-data/precommand4.zsh diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 4c500b1..ae6686c 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -234,16 +234,22 @@ _zsh_highlight_highlighter_main_paint() local braces_stack # $flags_with_argument is a set of letters, corresponding to the option letters - # that would be followed by a colon in a getopts specification. + # that would be followed by a colon in a getopts specification. local flags_with_argument - # $precommand_options maps precommand name to value of $flags_with_argument - # for that precommand. + # $flags_sans_argument is a set of letters, corresponding to the option letters + # that wouldn't be followed by a colon in a getopts specification. + local flags_sans_argument + # $precommand_options maps precommand name to values of $flags_with_argument and + # $flags_sans_argument for that precommand, joined by a colon. + # + # Currently, setting $flags_sans_argument is only important for commands that + # have a non-empty $flags_with_argument; see test-data/precommand4.zsh. local -A precommand_options precommand_options=( - 'command' '' - 'nice' n - 'sudo' Cgprtu - 'doas' aCu + 'command' :pvV # as of zsh 5.4.2 + 'nice' n # as of current POSIX spec + 'sudo' Cgprtu:AEHKPSVbhiklnsv # as of sudo 1.8.21p2 + 'doas' aCu:Lns # as of OpenBSD's doas(1) dated September 4, 2016 ) if [[ $zsyh_user_options[ignorebraces] == on || ${zsyh_user_options[ignoreclosebraces]:-off} == on ]]; then @@ -511,7 +517,7 @@ _zsh_highlight_highlighter_main_paint() if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]]; then if [[ -n $flags_with_argument ]] && - [[ $arg == '-'[$flags_with_argument] ]]; then + ( setopt extendedglob; [[ $arg == '-'[$flags_sans_argument]#[$flags_with_argument] ]] ); then # Flag that requires an argument this_word=${this_word//:start:/} next_word=':sudo_arg:' @@ -538,7 +544,8 @@ _zsh_highlight_highlighter_main_paint() elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word if (( ${+precommand_options[$arg]} )) && { _zsh_highlight_main__type $arg; [[ -n $REPLY && $REPLY != "none" ]] }; then style=precommand - flags_with_argument=${precommand_options[$arg]} + flags_with_argument=${precommand_options[$arg]%:*} + flags_sans_argument=${precommand_options[$arg]#*:} next_word=${next_word//:regular:/} next_word+=':sudo_opt:' next_word+=':start:' diff --git a/highlighters/main/test-data/precommand4.zsh b/highlighters/main/test-data/precommand4.zsh new file mode 100644 index 0000000..b9e6119 --- /dev/null +++ b/highlighters/main/test-data/precommand4.zsh @@ -0,0 +1,39 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2018 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 +# ------------------------------------------------------------------------------------------------- + +doas(){} +BUFFER=$'doas -nu phy1729 ls' + +expected_region_highlight=( + '1 4 precommand' # doas + '6 8 single-hyphen-option' # -nu + '10 16 default' # phy1729 + '18 19 command' # ls +) From f66887c0235b59183f49d3611ba194980080bd06 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:38:06 +0000 Subject: [PATCH 14/24] 'main': Write the "remainder" of the sentence in a comment. No functional change. --- highlighters/main/main-highlighter.zsh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index ae6686c..a56061b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -528,7 +528,9 @@ _zsh_highlight_highlighter_main_paint() next_word+=':start:' next_word+=':sudo_opt:' else - # + # Not an option flag; nothing to do. (If the command line is + # syntactically valid, ${this_state//:sudo_opt:/} should be + # non-empty now.) fi elif [[ $this_word == *':sudo_arg:'* ]]; then next_word+=':sudo_opt:' From cb31c97e14f71353a5d14fce2e7c97b02c4de677 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:45:52 +0000 Subject: [PATCH 15/24] 'main': Let _zsh_highlight_main__type return false on failure. --- highlighters/main/main-highlighter.zsh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index a56061b..1d3bbb6 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -160,6 +160,8 @@ _zsh_highlight_main__type() { if (( $+_zsh_highlight_main__command_type_cache )); then _zsh_highlight_main__command_type_cache[(e)$1]=$REPLY fi + [[ -n $REPLY ]] + return $? } # Check whether the first argument is a redirection operator token. @@ -544,7 +546,7 @@ _zsh_highlight_highlighter_main_paint() style=reserved-word # de facto a reserved word, although not de jure next_word=':start:' elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word - if (( ${+precommand_options[$arg]} )) && { _zsh_highlight_main__type $arg; [[ -n $REPLY && $REPLY != "none" ]] }; then + if (( ${+precommand_options[$arg]} )) && { _zsh_highlight_main__type $arg && [[ $REPLY != "none" ]] }; then style=precommand flags_with_argument=${precommand_options[$arg]%:*} flags_sans_argument=${precommand_options[$arg]#*:} @@ -626,7 +628,7 @@ _zsh_highlight_highlighter_main_paint() if (( insane_alias )); then style=unknown-token # Calling 'type' again; since __type memoizes the answer, this call is just a hash lookup. - elif _zsh_highlight_main__type "$arg"; [[ $REPLY == 'none' ]]; then + elif _zsh_highlight_main__type "$arg" && [[ $REPLY == 'none' ]]; then style=unknown-token else # The common case. From 62019eabb74b904d0621dfccce0e61e1df7cef7e Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:46:11 +0000 Subject: [PATCH 16/24] 'main': Break out a new helper function for readability. --- highlighters/main/main-highlighter.zsh | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 1d3bbb6..3871182 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -164,6 +164,17 @@ _zsh_highlight_main__type() { return $? } +# Checks whether $1 is something that can be run. +# +# Return 0 if runnable, 1 if not runnable, 2 if trouble. +_zsh_highlight_main__is_runnable() { + if _zsh_highlight_main__type "$1"; then + [[ -n $REPLY ]] + else + return 2 + fi +} + # Check whether the first argument is a redirection operator token. # Report result via the exit code. _zsh_highlight_main__is_redirection() { @@ -546,7 +557,7 @@ _zsh_highlight_highlighter_main_paint() style=reserved-word # de facto a reserved word, although not de jure next_word=':start:' elif [[ $this_word == *':start:'* ]] && (( in_redirection == 0 )); then # $arg is the command word - if (( ${+precommand_options[$arg]} )) && { _zsh_highlight_main__type $arg && [[ $REPLY != "none" ]] }; then + if (( ${+precommand_options[$arg]} )) && _zsh_highlight_main__is_runnable $arg; then style=precommand flags_with_argument=${precommand_options[$arg]%:*} flags_sans_argument=${precommand_options[$arg]#*:} From caa727f9ac1feef5730a06ad8a1427e171bc6c18 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:49:03 +0000 Subject: [PATCH 17/24] 'main': Use a more robust way of testing for $parameters' availability. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 3871182..1e08f6b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -509,7 +509,7 @@ _zsh_highlight_highlighter_main_paint() elif [[ $arg[1] == '$' ]]; then parameter_name=${arg:1} fi - if [[ $res == none ]] && (( ${+parameters} )) && + if [[ $res == none ]] && zmodload -e zsh/parameter && [[ ${parameter_name} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && (( ${+parameters[${MATCH}]} )) then From 1bf54d7f268be78f71364f55485f081e5f91f323 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 13:54:02 +0000 Subject: [PATCH 18/24] 'main': Highlight array parameters in command position. --- highlighters/main/main-highlighter.zsh | 12 +++++++++++- highlighters/main/test-data/commmand-parameter.zsh | 4 +++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 1e08f6b..ff06521 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -513,7 +513,17 @@ _zsh_highlight_highlighter_main_paint() [[ ${parameter_name} =~ ^([A-Za-z_][A-Za-z0-9_]*|[0-9]+)$ ]] && (( ${+parameters[${MATCH}]} )) then - arg=${(P)MATCH} + # Set $arg. + case ${(tP)MATCH} in + (*array*|*assoc*) + local -a words=( ${(P)MATCH} ) + arg=${words[1]} + ;; + (*) + # scalar, presumably + arg=${(P)MATCH} + ;; + esac _zsh_highlight_main__type "$arg" res=$REPLY fi diff --git a/highlighters/main/test-data/commmand-parameter.zsh b/highlighters/main/test-data/commmand-parameter.zsh index 8218afc..fa1a360 100644 --- a/highlighters/main/test-data/commmand-parameter.zsh +++ b/highlighters/main/test-data/commmand-parameter.zsh @@ -29,10 +29,12 @@ local x=/usr/bin/env local y=sudo -BUFFER='$x "argument"; $y' +local -a z; z=(zsh -f) +BUFFER='$x "argument"; $y; $z' expected_region_highlight=( "1 2 command" # $x "4 13 double-quoted-argument" # "argument" "16 17 precommand" # $y (sudo) + "20 21 command" # $z - 'zsh' being the command ) From e9ab802593ebbfce5cf749b1854d2cc201b3aa68 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 14:03:39 +0000 Subject: [PATCH 19/24] 'main': Unify $ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS and $precommand_options. --- highlighters/main/main-highlighter.zsh | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index ff06521..66939a8 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -230,7 +230,6 @@ _zsh_highlight_highlighter_main_paint() local start_pos=0 end_pos highlight_glob=true arg arg_raw style local in_array_assignment=false # true between 'a=(' and the matching ')' typeset -a ZSH_HIGHLIGHT_TOKENS_COMMANDSEPARATOR - typeset -a ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS typeset -a ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW local -a options_to_set # used in callees local buf="$PREBUFFER$BUFFER" @@ -263,6 +262,11 @@ _zsh_highlight_highlighter_main_paint() 'nice' n # as of current POSIX spec 'sudo' Cgprtu:AEHKPSVbhiklnsv # as of sudo 1.8.21p2 'doas' aCu:Lns # as of OpenBSD's doas(1) dated September 4, 2016 + 'builtin' '' # as of zsh 5.4.2 + 'exec' a:cl # as of zsh 5.4.2 + 'nocorrect' '' # as of zsh 5.4.2 + 'noglob' '' # as of zsh 5.4.2 + 'pkexec' '' # doesn't take short options; immune to #121 because it's usually not passed --option flags ) if [[ $zsyh_user_options[ignorebraces] == on || ${zsyh_user_options[ignoreclosebraces]:-off} == on ]]; then @@ -282,10 +286,6 @@ _zsh_highlight_highlighter_main_paint() # ### 'case' syntax, but followed by a pattern, not by a command # ';;' ';&' ';|' ) - ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS=( - 'builtin' 'exec' 'nocorrect' 'noglob' - 'pkexec' # immune to #121 because it's usually not passed --option flags - ) # Tokens that, at (naively-determined) "command position", are followed by # a de jure command position. All of these are reserved words. @@ -574,8 +574,6 @@ _zsh_highlight_highlighter_main_paint() next_word=${next_word//:regular:/} next_word+=':sudo_opt:' next_word+=':start:' - elif [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} ]]; then - style=precommand else case $res in reserved) # reserved word @@ -654,7 +652,9 @@ _zsh_highlight_highlighter_main_paint() else # The common case. style=alias - [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && -z ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg_raw"} ]] && ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg_raw) + if (( ${+precommand_options[(re)"$arg"]} )) && (( ! ${+precommand_options[(re)"$arg_raw"]} )); then + precommand_options[$arg_raw]=$precommand_options[$arg] + fi fi } ;; @@ -800,8 +800,7 @@ _zsh_highlight_highlighter_main_paint() highlight_glob=true fi elif - [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]] || - [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$arg"} && $this_word == *':start:'* ]]; then + [[ -n ${(M)ZSH_HIGHLIGHT_TOKENS_CONTROL_FLOW:#"$arg"} && $this_word == *':start:'* ]]; then next_word=':start:' elif [[ $arg == "repeat" && $this_word == *':start:'* ]]; then # skip the repeat-count word From 34f50d7b4fb27af78773817034579dfa9da705cc Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 16:01:27 +0000 Subject: [PATCH 20/24] fixup! 'main': Break out a new helper function for readability. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 66939a8..8b27558 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -169,7 +169,7 @@ _zsh_highlight_main__type() { # Return 0 if runnable, 1 if not runnable, 2 if trouble. _zsh_highlight_main__is_runnable() { if _zsh_highlight_main__type "$1"; then - [[ -n $REPLY ]] + [[ $REPLY != none ]] else return 2 fi From a3bc345e542af7359df066d7e95c704e90d2d31d Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 16:02:41 +0000 Subject: [PATCH 21/24] No functional change. Follow-up to 1fee620e6235d3d1789dd3b62e65f5618464dbea. --- highlighters/main/main-highlighter.zsh | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 8b27558..e118e16 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -472,11 +472,9 @@ _zsh_highlight_highlighter_main_paint() # TODO: this should be done iteratively, e.g., 'alias x=y y=z z=w\n x' # And then the entire 'alias' branch of the 'case' statement should # be done here. - () { - # TODO: path expansion should happen _after_ alias expansion - _zsh_highlight_main_highlighter_expand_path $arg - _zsh_highlight_main__type "$REPLY" - } + # TODO: path expansion should happen _after_ alias expansion + _zsh_highlight_main_highlighter_expand_path $arg + _zsh_highlight_main__type "$REPLY" local res="$REPLY" if [[ $res == "alias" ]]; then _zsh_highlight_main__resolve_alias $arg From 4480eb6cf86da4f86144438ba449fd8402b77fdf Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 16:05:04 +0000 Subject: [PATCH 22/24] fixup! 'main': Learn $flags_sans_arguments and use that to parse '-xy foo' correctly where -x takes no argument and -y does. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index e118e16..9bf942f 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -538,7 +538,7 @@ _zsh_highlight_highlighter_main_paint() if (( ! in_redirection )); then if [[ $this_word == *':sudo_opt:'* ]]; then if [[ -n $flags_with_argument ]] && - ( setopt extendedglob; [[ $arg == '-'[$flags_sans_argument]#[$flags_with_argument] ]] ); then + [[ $arg == '-'[$flags_sans_argument]#[$flags_with_argument] ]]; then # Flag that requires an argument this_word=${this_word//:start:/} next_word=':sudo_arg:' From 8fa7a15640c695a384f156b41c52b5caa9bbdaaf Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 16:06:52 +0000 Subject: [PATCH 23/24] fixup! 'main': Write the "remainder" of the sentence in a comment. No functional change. --- highlighters/main/main-highlighter.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 9bf942f..4ea3842 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -550,7 +550,7 @@ _zsh_highlight_highlighter_main_paint() next_word+=':sudo_opt:' else # Not an option flag; nothing to do. (If the command line is - # syntactically valid, ${this_state//:sudo_opt:/} should be + # syntactically valid, ${this_word//:sudo_opt:/} should be # non-empty now.) fi elif [[ $this_word == *':sudo_arg:'* ]]; then From 2580a8bd8ba5fd6fb64204588fcea5ebe6600250 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Fri, 12 Jan 2018 16:08:22 +0000 Subject: [PATCH 24/24] 'main': Update $this_word state with our inferences. Found by code inspection. --- highlighters/main/main-highlighter.zsh | 1 + 1 file changed, 1 insertion(+) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 4ea3842..cb4e914 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -552,6 +552,7 @@ _zsh_highlight_highlighter_main_paint() # Not an option flag; nothing to do. (If the command line is # syntactically valid, ${this_word//:sudo_opt:/} should be # non-empty now.) + this_word=${this_word//:sudo_opt:/} fi elif [[ $this_word == *':sudo_arg:'* ]]; then next_word+=':sudo_opt:'