From fb69f4ca81ee0d161a0885693239f8dfa396dda2 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 17 Mar 2020 03:59:30 +0000 Subject: [PATCH] 'main': When the redirection operator '>&' or '<&' is followed by a positive integer, do not consider that as a filename; it's always a file descriptor. Fixes #694. --- changelog.md | 3 +++ docs/highlighters/main.md | 3 ++- highlighters/main/main-highlighter.zsh | 17 +++++++++++++---- .../main/test-data/fd-target-not-filename.zsh | 2 +- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index 7978a82..952a33c 100644 --- a/changelog.md +++ b/changelog.md @@ -35,6 +35,9 @@ - Fix `cat < *` being highlighting as globbing when the `MULTIOS` option is unset. [#583] +- Fix `echo >&2` highlighting the `2` as a filename if a file by that name happened to exist + [#694] + # Changes in version 0.7.1 - Remove out-of-date information from the 0.7.0 changelog. diff --git a/docs/highlighters/main.md b/docs/highlighters/main.md index 1566fa4..8f4ef4b 100644 --- a/docs/highlighters/main.md +++ b/docs/highlighters/main.md @@ -59,7 +59,8 @@ This highlighter defines the following styles: * `redirection` - redirection operators (`<`, `>`, etc) * `comment` - comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`) * `comment` - elided parameters in command position (`$x ls` when `$x` is unset or empty) -* `named-fd` - named file descriptor (`echo foo {fd}>&2`) +* `named-fd` - named file descriptor (the `fd` in `echo foo {fd}>&2`) +* `numeric-fd` - numeric file descriptor (the `2` in `echo foo {fd}>&2`) * `arg0` - a command word other than one of those enumerated above (other than a command, precommand, alias, function, or shell builtin command). * `default` - everything else diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 8829ceb..e43efa7 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -59,6 +59,7 @@ : ${ZSH_HIGHLIGHT_STYLES[redirection]:=fg=yellow} : ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold} : ${ZSH_HIGHLIGHT_STYLES[named-fd]:=none} +: ${ZSH_HIGHLIGHT_STYLES[numeric-fd]:=none} : ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green} # Whether the highlighter should be called or not. @@ -112,6 +113,10 @@ _zsh_highlight_main_calculate_fallback() { hashed-command arg0 arg0_\* arg0 + # TODO: Maybe these? — + # named-fd file-descriptor + # numeric-fd file-descriptor + path_prefix path # The path separator fallback won't ever be used, due to the optimisation # in _zsh_highlight_main_highlighter_highlight_path_separators(). @@ -1271,10 +1276,14 @@ _zsh_highlight_main_highlighter_highlight_argument() esac done - if (( path_eligible )) && _zsh_highlight_main_highlighter_check_path $arg[$1,-1]; then - base_style=$REPLY - _zsh_highlight_main_highlighter_highlight_path_separators $base_style - highlights+=($reply) + if (( path_eligible )); then + if (( in_redirection )) && [[ $last_arg == *['<>']['&'] && $arg[$1,-1] == <0-> ]]; then + base_style=numeric-fd + elif _zsh_highlight_main_highlighter_check_path $arg[$1,-1]; then + base_style=$REPLY + _zsh_highlight_main_highlighter_highlight_path_separators $base_style + highlights+=($reply) + fi fi highlights=($(( start_pos + $1 - 1 )) $end_pos $base_style $highlights) diff --git a/highlighters/main/test-data/fd-target-not-filename.zsh b/highlighters/main/test-data/fd-target-not-filename.zsh index 15de3db..5c3cd08 100644 --- a/highlighters/main/test-data/fd-target-not-filename.zsh +++ b/highlighters/main/test-data/fd-target-not-filename.zsh @@ -36,5 +36,5 @@ expected_region_highlight=( '1 4 builtin' # echo '6 8 default' # foo '9 10 redirection' # >& - '11 11 file-descriptor "issue #694"' # 2 (not path) + '11 11 numeric-fd' # 2 (not path) )