Skip to content

Commit 83ac855

Browse files
committed
'main': Let AUTO_CD directories be highlighted with their own style.
1 parent 3f930fb commit 83ac855

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

docs/highlighters/main.md

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

highlighters/main/main-highlighter.zsh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
: ${ZSH_HIGHLIGHT_STYLES[global-alias]:=fg=cyan}
3737
: ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
3838
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
39+
: ${ZSH_HIGHLIGHT_STYLES[autodirectory]:=fg=green,underline}
3940
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
4041
: ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=}
4142
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=}
@@ -113,6 +114,7 @@ _zsh_highlight_main_calculate_fallback() {
113114
command arg0
114115
precommand arg0
115116
hashed-command arg0
117+
autodirectory arg0
116118
arg0_\* arg0
117119

118120
# TODO: Maybe these? —
@@ -1130,6 +1132,8 @@ _zsh_highlight_main_highlighter_check_path()
11301132
fi
11311133

11321134
if (( in_command_position )); then
1135+
# ### Currently, this value is never returned: either it's overwritten
1136+
# ### below, or the return code is non-zero
11331137
REPLY=arg0
11341138
else
11351139
REPLY=path
@@ -1156,8 +1160,16 @@ _zsh_highlight_main_highlighter_check_path()
11561160
done
11571161

11581162
if (( in_command_position )); then
1159-
if [[ -x $expanded_path ]] && { (( autocd )) || [[ ! -d $expanded_path ]] }; then
1160-
return 0
1163+
if [[ -x $expanded_path ]]; then
1164+
if (( autocd )); then
1165+
if [[ -d $expanded_path ]]; then
1166+
REPLY=autodirectory
1167+
fi
1168+
return 0
1169+
elif [[ ! -d $expanded_path ]]; then
1170+
# ### This seems unreachable for the current callers
1171+
return 0
1172+
fi
11611173
fi
11621174
else
11631175
if [[ -L $expanded_path || -e $expanded_path ]]; then
@@ -1170,7 +1182,12 @@ _zsh_highlight_main_highlighter_check_path()
11701182
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
11711183
local cdpath_dir
11721184
for cdpath_dir in $cdpath ; do
1173-
[[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]] && return 0
1185+
if [[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]]; then
1186+
if (( in_command_position && autocd )); then
1187+
REPLY=autodirectory
1188+
fi
1189+
return 0
1190+
fi
11741191
done
11751192
fi
11761193

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 3 deletions
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)