'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.
This commit is contained in:
Daniel Shahaf 2020-03-17 03:59:30 +00:00
parent 1024ae8177
commit fb69f4ca81
4 changed files with 19 additions and 6 deletions

View file

@ -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.

View file

@ -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

View file

@ -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)

View file

@ -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)
)