Skip to content

Commit fb69f4c

Browse files
committed
'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.
1 parent 1024ae8 commit fb69f4c

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
- Fix `cat < *` being highlighting as globbing when the `MULTIOS` option is unset.
3636
[#583]
3737

38+
- Fix `echo >&2` highlighting the `2` as a filename if a file by that name happened to exist
39+
[#694]
40+
3841
# Changes in version 0.7.1
3942

4043
- Remove out-of-date information from the 0.7.0 changelog.

docs/highlighters/main.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ This highlighter defines the following styles:
5959
* `redirection` - redirection operators (`<`, `>`, etc)
6060
* `comment` - comments, when `setopt INTERACTIVE_COMMENTS` is in effect (`echo # foo`)
6161
* `comment` - elided parameters in command position (`$x ls` when `$x` is unset or empty)
62-
* `named-fd` - named file descriptor (`echo foo {fd}>&2`)
62+
* `named-fd` - named file descriptor (the `fd` in `echo foo {fd}>&2`)
63+
* `numeric-fd` - numeric file descriptor (the `2` in `echo foo {fd}>&2`)
6364
* `arg0` - a command word other than one of those enumerated above (other than a command, precommand, alias, function, or shell builtin command).
6465
* `default` - everything else
6566

highlighters/main/main-highlighter.zsh

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
: ${ZSH_HIGHLIGHT_STYLES[redirection]:=fg=yellow}
6060
: ${ZSH_HIGHLIGHT_STYLES[comment]:=fg=black,bold}
6161
: ${ZSH_HIGHLIGHT_STYLES[named-fd]:=none}
62+
: ${ZSH_HIGHLIGHT_STYLES[numeric-fd]:=none}
6263
: ${ZSH_HIGHLIGHT_STYLES[arg0]:=fg=green}
6364

6465
# Whether the highlighter should be called or not.
@@ -112,6 +113,10 @@ _zsh_highlight_main_calculate_fallback() {
112113
hashed-command arg0
113114
arg0_\* arg0
114115

116+
# TODO: Maybe these? —
117+
# named-fd file-descriptor
118+
# numeric-fd file-descriptor
119+
115120
path_prefix path
116121
# The path separator fallback won't ever be used, due to the optimisation
117122
# in _zsh_highlight_main_highlighter_highlight_path_separators().
@@ -1271,10 +1276,14 @@ _zsh_highlight_main_highlighter_highlight_argument()
12711276
esac
12721277
done
12731278

1274-
if (( path_eligible )) && _zsh_highlight_main_highlighter_check_path $arg[$1,-1]; then
1275-
base_style=$REPLY
1276-
_zsh_highlight_main_highlighter_highlight_path_separators $base_style
1277-
highlights+=($reply)
1279+
if (( path_eligible )); then
1280+
if (( in_redirection )) && [[ $last_arg == *['<>']['&'] && $arg[$1,-1] == <0-> ]]; then
1281+
base_style=numeric-fd
1282+
elif _zsh_highlight_main_highlighter_check_path $arg[$1,-1]; then
1283+
base_style=$REPLY
1284+
_zsh_highlight_main_highlighter_highlight_path_separators $base_style
1285+
highlights+=($reply)
1286+
fi
12781287
fi
12791288

12801289
highlights=($(( start_pos + $1 - 1 )) $end_pos $base_style $highlights)

highlighters/main/test-data/fd-target-not-filename.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ expected_region_highlight=(
3636
'1 4 builtin' # echo
3737
'6 8 default' # foo
3838
'9 10 redirection' # >&
39-
'11 11 file-descriptor "issue #694"' # 2 (not path)
39+
'11 11 numeric-fd' # 2 (not path)
4040
)

0 commit comments

Comments
 (0)