Skip to content

Commit 90b3615

Browse files
committed
'main': Let AUTO_CD directories be highlighted with their own style.
1 parent de6a132 commit 90b3615

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]:=}
@@ -111,6 +112,7 @@ _zsh_highlight_main_calculate_fallback() {
111112
command arg0
112113
precommand arg0
113114
hashed-command arg0
115+
autodirectory arg0
114116
arg0_\* arg0
115117

116118
# TODO: Maybe these? —
@@ -1121,6 +1123,8 @@ _zsh_highlight_main_highlighter_check_path()
11211123
fi
11221124

11231125
if (( in_command_position )); then
1126+
# ### Currently, this value is never returned: either it's overwritten
1127+
# ### below, or the return code is non-zero
11241128
REPLY=arg0
11251129
else
11261130
REPLY=path
@@ -1142,8 +1146,16 @@ _zsh_highlight_main_highlighter_check_path()
11421146
done
11431147

11441148
if (( in_command_position )); then
1145-
if [[ -x $expanded_path ]] && { (( autocd )) || [[ ! -d $expanded_path ]] }; then
1146-
return 0
1149+
if [[ -x $expanded_path ]]; then
1150+
if (( autocd )); then
1151+
if [[ -d $expanded_path ]]; then
1152+
REPLY=autodirectory
1153+
fi
1154+
return 0
1155+
elif [[ ! -d $expanded_path ]]; then
1156+
# ### This seems unreachable for the current callers
1157+
return 0
1158+
fi
11471159
fi
11481160
else
11491161
if [[ -L $expanded_path || -e $expanded_path ]]; then
@@ -1156,7 +1168,12 @@ _zsh_highlight_main_highlighter_check_path()
11561168
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
11571169
local cdpath_dir
11581170
for cdpath_dir in $cdpath ; do
1159-
[[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]] && return 0
1171+
if [[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]]; then
1172+
if (( in_command_position && autocd )); then
1173+
REPLY=autodirectory
1174+
fi
1175+
return 0
1176+
fi
11601177
done
11611178
fi
11621179

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)