From 83ac855cebd1b781ba75ea306a251672d49697fc Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sun, 12 Jan 2020 20:17:59 +0000 Subject: [PATCH] 'main': Let AUTO_CD directories be highlighted with their own style. --- docs/highlighters/main.md | 1 + highlighters/main/main-highlighter.zsh | 23 ++++++++++++++++--- .../abspath-in-command-position1b.zsh | 2 +- .../abspath-in-command-position3b.zsh | 4 ++-- .../main/test-data/path-dollared-word3b.zsh | 5 ++-- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/docs/highlighters/main.md b/docs/highlighters/main.md index 37e7d13..5eec335 100644 --- a/docs/highlighters/main.md +++ b/docs/highlighters/main.md @@ -27,6 +27,7 @@ This highlighter defines the following styles: * `precommand` - precommand modifiers (e.g., `noglob`, `builtin`) * `commandseparator` - command separation tokens (`;`, `&&`) * `hashed-command` - hashed commands +* `autodirectory` - a directory name in command position when the `AUTO_CD` option is set * `path` - existing filenames * `path_pathseparator` - path separators in filenames (`/`); if unset, `path` is used (default) * `path_prefix` - prefixes of existing filenames diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 05ff2ff..0222160 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -36,6 +36,7 @@ : ${ZSH_HIGHLIGHT_STYLES[global-alias]:=fg=cyan} : ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline} : ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none} +: ${ZSH_HIGHLIGHT_STYLES[autodirectory]:=fg=green,underline} : ${ZSH_HIGHLIGHT_STYLES[path]:=underline} : ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=} : ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=} @@ -113,6 +114,7 @@ _zsh_highlight_main_calculate_fallback() { command arg0 precommand arg0 hashed-command arg0 + autodirectory arg0 arg0_\* arg0 # TODO: Maybe these? — @@ -1130,6 +1132,8 @@ _zsh_highlight_main_highlighter_check_path() fi if (( in_command_position )); then + # ### Currently, this value is never returned: either it's overwritten + # ### below, or the return code is non-zero REPLY=arg0 else REPLY=path @@ -1156,8 +1160,16 @@ _zsh_highlight_main_highlighter_check_path() done if (( in_command_position )); then - if [[ -x $expanded_path ]] && { (( autocd )) || [[ ! -d $expanded_path ]] }; then - return 0 + if [[ -x $expanded_path ]]; then + if (( autocd )); then + if [[ -d $expanded_path ]]; then + REPLY=autodirectory + fi + return 0 + elif [[ ! -d $expanded_path ]]; then + # ### This seems unreachable for the current callers + return 0 + fi fi else if [[ -L $expanded_path || -e $expanded_path ]]; then @@ -1170,7 +1182,12 @@ _zsh_highlight_main_highlighter_check_path() # TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here. local cdpath_dir for cdpath_dir in $cdpath ; do - [[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]] && return 0 + if [[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]]; then + if (( in_command_position && autocd )); then + REPLY=autodirectory + fi + return 0 + fi done fi diff --git a/highlighters/main/test-data/abspath-in-command-position1b.zsh b/highlighters/main/test-data/abspath-in-command-position1b.zsh index 82292ed..88fe60c 100644 --- a/highlighters/main/test-data/abspath-in-command-position1b.zsh +++ b/highlighters/main/test-data/abspath-in-command-position1b.zsh @@ -32,5 +32,5 @@ setopt autocd BUFFER=$'/' expected_region_highlight=( - '1 1 arg0' # / + '1 1 autodirectory' # / ) diff --git a/highlighters/main/test-data/abspath-in-command-position3b.zsh b/highlighters/main/test-data/abspath-in-command-position3b.zsh index e43b7fb..0e65c98 100644 --- a/highlighters/main/test-data/abspath-in-command-position3b.zsh +++ b/highlighters/main/test-data/abspath-in-command-position3b.zsh @@ -32,7 +32,7 @@ setopt autocd BUFFER=$'/bin; /bin' expected_region_highlight=( - '1 4 arg0' # /bin (in middle) + '1 4 autodirectory' # /bin (in middle) '5 5 commandseparator' # ; - '7 10 arg0' # /bin (at end) + '7 10 autodirectory' # /bin (at end) ) diff --git a/highlighters/main/test-data/path-dollared-word3b.zsh b/highlighters/main/test-data/path-dollared-word3b.zsh index 7c904f3..72a2f7c 100644 --- a/highlighters/main/test-data/path-dollared-word3b.zsh +++ b/highlighters/main/test-data/path-dollared-word3b.zsh @@ -29,11 +29,10 @@ # ------------------------------------------------------------------------------------------------- setopt autocd - BUFFER=$'$PWD; ${PWD}' expected_region_highlight=( - '1 4 arg0' # $PWD + '1 4 autodirectory' # $PWD '5 5 commandseparator' # ; - '7 12 arg0' # ${PWD} + '7 12 autodirectory' # ${PWD} )