Skip to content

Commit dee05a7

Browse files
committed
'main': Let AUTO_CD directories be highlighted with their own style.
1 parent e0849a8 commit dee05a7

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

docs/highlighters/main.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This highlighter defines the following styles:
2626
* `precommand` - precommand modifiers (e.g., `noglob`, `builtin`)
2727
* `commandseparator` - command separation tokens (`;`, `&&`)
2828
* `hashed-command` - hashed commands
29+
* `autodirectory` - a directory name in command position when the `AUTO_CD` option is set
2930
* `path` - existing filenames
3031
* `path_pathseparator` - path separators in filenames (`/`); if unset, `path` is used (default)
3132
* `path_prefix` - prefixes of existing filenames

highlighters/main/main-highlighter.zsh

+20-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
: ${ZSH_HIGHLIGHT_STYLES[suffix-alias]:=fg=green,underline}
3636
: ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
3737
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
38+
: ${ZSH_HIGHLIGHT_STYLES[autodirectory]:=fg=green,underline}
3839
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
3940
: ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=}
4041
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=}
@@ -110,6 +111,7 @@ _zsh_highlight_main_calculate_fallback() {
110111
command arg0
111112
precommand arg0
112113
hashed-command arg0
114+
autodirectory arg0
113115
arg0_\* arg0
114116

115117
path_prefix path
@@ -1092,6 +1094,8 @@ _zsh_highlight_main_highlighter_check_path()
10921094
fi
10931095

10941096
if (( in_command_position )); then
1097+
# ### Currently, this value is never returned: either it's overwritten
1098+
# ### below, or the return code is non-zero
10951099
REPLY=arg0
10961100
else
10971101
REPLY=path
@@ -1113,8 +1117,16 @@ _zsh_highlight_main_highlighter_check_path()
11131117
done
11141118

11151119
if (( in_command_position )); then
1116-
if [[ -x $expanded_path ]] && { (( autocd )) || [[ ! -d $expanded_path ]] }; then
1117-
return 0
1120+
if [[ -x $expanded_path ]]; then
1121+
if (( autocd )); then
1122+
if [[ -d $expanded_path ]]; then
1123+
REPLY=autodirectory
1124+
fi
1125+
return 0
1126+
elif [[ ! -d $expanded_path ]]; then
1127+
# ### This seems unreachable for the current callers
1128+
return 0
1129+
fi
11181130
fi
11191131
else
11201132
if [[ -L $expanded_path || -e $expanded_path ]]; then
@@ -1127,7 +1139,12 @@ _zsh_highlight_main_highlighter_check_path()
11271139
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
11281140
local cdpath_dir
11291141
for cdpath_dir in $cdpath ; do
1130-
[[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]] && return 0
1142+
if [[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]]; then
1143+
if (( in_command_position && autocd )); then
1144+
REPLY=autodirectory
1145+
fi
1146+
return 0
1147+
fi
11311148
done
11321149
fi
11331150

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ setopt autocd
3232
BUFFER=$'/'
3333

3434
expected_region_highlight=(
35-
'1 1 arg0' # /
35+
'1 1 autodirectory' # /
3636
)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ setopt autocd
3232
BUFFER=$'/bin; /bin'
3333

3434
expected_region_highlight=(
35-
'1 4 arg0' # /bin (in middle)
35+
'1 4 autodirectory' # /bin (in middle)
3636
'5 5 commandseparator' # ;
37-
'7 10 arg0' # /bin (at end)
37+
'7 10 autodirectory' # /bin (at end)
3838
)

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@
2929
# -------------------------------------------------------------------------------------------------
3030

3131
setopt autocd
32-
3332
BUFFER=$'$PWD; ${PWD}'
3433

3534
expected_region_highlight=(
36-
'1 4 arg0' # $PWD
35+
'1 4 autodirectory' # $PWD
3736
'5 5 commandseparator' # ;
38-
'7 12 arg0' # ${PWD}
37+
'7 12 autodirectory' # ${PWD}
3938
)

0 commit comments

Comments
 (0)