Skip to content

Commit b197462

Browse files
committed
'main': In command position, do not highlight directories (unless AUTO_CD is set) and non-executable files. Fixes zsh-users#202.
Test expectations are updated. For example, BUFFER='/bin' is now highlighted as path_prefix because it's a prefix of '/bin/sh' which would be valid. However, BUFFER='/bin;' is now properly highlighted as an error (unless AUTO_CD is set).
1 parent 184f5d9 commit b197462

5 files changed

+28
-12
lines changed

highlighters/main/main-highlighter.zsh

+23-8
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ _zsh_highlight_main_highlighter_highlight_list()
857857
fi
858858
_zsh_highlight_main__stack_pop 'R' reserved-word
859859
else
860-
if _zsh_highlight_main_highlighter_check_path $arg; then
860+
if _zsh_highlight_main_highlighter_check_path $arg 1; then
861861
style=$REPLY
862862
else
863863
style=unknown-token
@@ -960,10 +960,13 @@ _zsh_highlight_main_highlighter_highlight_path_separators()
960960
# Check if $1 is a path.
961961
# If yes, return 0 and in $REPLY the style to use.
962962
# Else, return non-zero (and the contents of $REPLY is undefined).
963+
#
964+
# $2 should be non-zero iff we're in command position.
963965
_zsh_highlight_main_highlighter_check_path()
964966
{
965967
_zsh_highlight_main_highlighter_expand_path "$1"
966968
local expanded_path="$REPLY" tmp_path
969+
integer in_command_position=$2
967970

968971
REPLY=path
969972

@@ -982,16 +985,25 @@ _zsh_highlight_main_highlighter_check_path()
982985
tmp_path=$tmp_path:h
983986
done
984987

985-
[[ -L $expanded_path ]] && return 0
986-
[[ -e $expanded_path ]] && return 0
988+
if (( in_command_position )); then
989+
if [[ -x $expanded_path ]] && [[ $zsyh_user_options[autocd] == on || ! -d $expanded_path ]]; then
990+
return 0
991+
fi
992+
else
993+
if [[ -L $expanded_path || -e $expanded_path ]]; then
994+
return 0
995+
fi
996+
fi
987997

988998
# Search the path in CDPATH
989999
if [[ $expanded_path != /* ]]; then
990-
local cdpath_dir
9911000
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
992-
for cdpath_dir in $cdpath ; do
993-
[[ -e "$cdpath_dir/$expanded_path" ]] && return 0
994-
done
1001+
if (( ! in_command_position )) || [[ $zsyh_user_options[autocd] == on ]]; then
1002+
local cdpath_dir
1003+
for cdpath_dir in $cdpath ; do
1004+
[[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]] && return 0
1005+
done
1006+
fi
9951007
fi
9961008

9971009
# If dirname($1) doesn't exist, neither does $1.
@@ -1012,6 +1024,8 @@ _zsh_highlight_main_highlighter_check_path()
10121024
# Highlight an argument and possibly special chars in quotes starting at $1 in $arg
10131025
# This command will at least highlight $1 to end_pos with the default style
10141026
# If $2 is set to 0, the argument cannot be highlighted as an option.
1027+
#
1028+
# This function currently assumes it's never called for the command word.
10151029
_zsh_highlight_main_highlighter_highlight_argument()
10161030
{
10171031
local base_style=default i=$1 option_eligible=${2:-1} path_eligible=1 ret start style
@@ -1132,7 +1146,8 @@ _zsh_highlight_main_highlighter_highlight_argument()
11321146
esac
11331147
done
11341148

1135-
if (( path_eligible )) && _zsh_highlight_main_highlighter_check_path $arg[$1,-1]; then
1149+
# This function is currently never called for the command word, so $2 is hard-coded as 0.
1150+
if (( path_eligible )) && _zsh_highlight_main_highlighter_check_path $arg[$1,-1] 0; then
11361151
base_style=$REPLY
11371152
_zsh_highlight_main_highlighter_highlight_path_separators $base_style
11381153
highlights+=($reply)

highlighters/main/test-data/abspath-in-command-position1.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
BUFFER=$'/'
3232

3333
expected_region_highlight=(
34-
'1 1 path' # /
34+
'1 1 path_prefix' # /
3535
)

highlighters/main/test-data/abspath-in-command-position3.zsh

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# vim: ft=zsh sw=2 ts=2 et
2929
# -------------------------------------------------------------------------------------------------
3030

31+
setopt autocd
3132
BUFFER=$'/bin'
3233

3334
expected_region_highlight=(

highlighters/main/test-data/path-dollared-word3.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
BUFFER='$PWD; ${PWD}'
3535

3636
expected_region_highlight=(
37-
"1 4 path" # $PWD
37+
"1 4 unknown-token" # $PWD (without AUTO_CD)
3838
"5 5 commandseparator" # ;
39-
"7 12 path" # ${PWD}
39+
"7 12 path_prefix" # ${PWD}
4040
)

highlighters/main/test-data/plain-file-in-command-position.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ chmod -x foo
3333
BUFFER=$'./foo bar'
3434

3535
expected_region_highlight=(
36-
'1 5 unknown-token "issue #202"' # ./foo
36+
'1 5 unknown-token' # ./foo
3737
# The only reason 'bar' is here because at this time BUFFER='./foo' is (not
3838
# quite correctly) highlighted as 'path_prefix'. The test will remain
3939
# forever valid, even if that 'path_prefix' issue is fixed, though.

0 commit comments

Comments
 (0)