Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] Let 'extra-verbose' completion show alias values



See attachment.
From 5743dc5ef34e94944ff0de7929f261f5d1d8b0d8 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@xxxxxxxxx>
Date: Sat, 29 May 2021 00:56:02 +0300
Subject: [PATCH] Let 'extra-verbose' completion show alias values

---
 Completion/Base/Completer/_expand_alias |  3 +-
 Completion/Zsh/Command/_disable         |  6 ++--
 Completion/Zsh/Command/_enable          |  6 ++--
 Completion/Zsh/Type/_aliases            | 40 +++++++++++++++----------
 Completion/Zsh/Type/_command_names      |  2 +-
 Test/Y01completion.ztst                 | 23 ++++++++++++++
 6 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/Completion/Base/Completer/_expand_alias b/Completion/Base/Completer/_expand_alias
index 8240e4162..275da157c 100644
--- a/Completion/Base/Completer/_expand_alias
+++ b/Completion/Base/Completer/_expand_alias
@@ -59,7 +59,8 @@ if [[ -n $tmp ]]; then
     fi
   fi
   zstyle -T ":completion:${curcontext}:" add-space || suf=( -S '' )
-  $pre _wanted aliases expl alias compadd -UQ "$suf[@]" -- ${tmp%%[[:blank:]]##}
+  $pre _wanted expansions expl expansion compadd -UQ "$suf[@]" -- ${tmp%%[[:blank:]]##} &&
+      ! zstyle -t ":completion:${curcontext}:" complete
 elif (( $#pre )) && zstyle -t ":completion:${curcontext}:" complete; then
   $pre _aliases -s "$sel" -S ''
 else
diff --git a/Completion/Zsh/Command/_disable b/Completion/Zsh/Command/_disable
index 52b82a6e9..2dceeb6c8 100644
--- a/Completion/Zsh/Command/_disable
+++ b/Completion/Zsh/Command/_disable
@@ -2,15 +2,13 @@
 
 local -a ali_arr sali_arr func_arr
 
-ali_arr=(${(k)aliases} ${(k)galiases})
-sali_arr=(${(k)saliases})
 func_arr=(${(k)functions})
 
 _arguments -C -s -A "-*" -S \
-  "(-f -r -s -p)-a[act on regular or global aliases]:*:regular or global aliases:compadd -k ali_arr" \
+  "(-f -r -s -p)-a[act on regular or global aliases]:*:regular or global aliases:_aliases -s rg" \
   "(-a -r -s -p)-f[act on functions]:*:functions:compadd -k func_arr" \
   "(-a -f -s -p)-r[act on reserved words]:*:reserved-words:compadd -k reswords" \
-  "(-a -f -r -p)-s[act on suffix aliases]:*:suffix aliases:compadd -k sali_arr" \
+  "(-a -f -r -p)-s[act on suffix aliases]:*:suffix aliases:_aliases -s s" \
   "(-a -f -r -s)-p[act on pattern characters]:*:pattern characters:compadd -k patchars" \
   '-m[treat arguments as patterns]' \
   "*:builtin command:(${(k)builtins})"
diff --git a/Completion/Zsh/Command/_enable b/Completion/Zsh/Command/_enable
index 9410651b7..53a9e36db 100644
--- a/Completion/Zsh/Command/_enable
+++ b/Completion/Zsh/Command/_enable
@@ -2,15 +2,13 @@
 
 local -a ali_arr sali_arr func_arr
 
-ali_arr=(${(k)dis_aliases} ${(k)dis_galiases})
-sali_arr=(${(k)dis_saliases})
 func_arr=(${(k)dis_functions})
 
 _arguments -C -s -A "-*" -S \
-  "(-f -r -s -p)-a[act on regular or global aliases]:*:aliases:compadd -k ali_arr" \
+  "(-f -r -s -p)-a[act on regular or global aliases]:*:regular or global aliases:_aliases -s RG" \
   "(-a -r -s -p)-f[act on functions]:*:functions:compadd -k func_arr" \
   "(-a -f -s -p)-r[act on reserved words]:*:reserved-words:compadd -k dis_reswords" \
-  "(-a -f -r -p)-s[act on suffix aliases]:*:suffix aliases:compadd -k sali_arr" \
+  "(-a -f -r -p)-s[act on suffix aliases]:*:suffix aliases:_aliases -s S" \
   "(-a -f -r -s)-p[act on pattern characters]:*:pattern characters:compadd -k dis_patchars" \
   '-m[treat arguments as patterns]' \
   "*:builtin command:(${(k)dis_builtins})"
diff --git a/Completion/Zsh/Type/_aliases b/Completion/Zsh/Type/_aliases
index 6506ece13..c4d5abac1 100644
--- a/Completion/Zsh/Type/_aliases
+++ b/Completion/Zsh/Type/_aliases
@@ -1,19 +1,29 @@
 #compdef unalias
 
-local expl sel args opts
+_aliases_add() {
+  if zstyle -t ":completion:${curcontext}:$2" extra-verbose; then
+    local -a items=()
+    printf -v items "%s\:%s" "${(@q)${(@kvq)${(PAA)1}}}"
+    args+=( "${2}:${3}:(( ${items:#'\:'} ))" )
+  else
+    args+=( "${2}:${3}:compadd -k $1" )
+  fi
+}
 
-zparseopts -E -D s:=sel
+_aliases() {
+  local -a args=() sel=( -s rgs )
+  zparseopts -D -E -K s:=sel
+  shift sel
+  case $sel in
+    ( *r* ) _aliases_add aliases      aliases                 'regular alias'          ;|
+    ( *g* ) _aliases_add galiases     global-aliases          'global alias'           ;|
+    ( *s* ) _aliases_add saliases     suffix-aliases          'suffix alias'           ;|
+    ( *R* ) _aliases_add dis_aliases  disabled-aliases        'disabled regular alias' ;|
+    ( *G* ) _aliases_add dis_galiases disabled-global-aliases 'disabled global alias'  ;|
+    ( *S* ) _aliases_add dis_saliases disabled-suffix-aliases 'disabled suffix alias'  ;|
+  esac
+  local -a opts=( "$@" )
+  _alternative -O opts "$args[@]"
+}
 
-[[ -z $sel ]] && sel=rgs
-
-opts=( "$@" )
-
-args=()
-[[ $sel = *r* ]] && args=( $args 'aliases:regular alias:compadd -k aliases' )
-[[ $sel = *g* ]] && args=( $args 'global-aliases:global alias:compadd -k galiases' )
-[[ $sel = *s* ]] && args=( $args 'suffix-aliases:suffix alias:compadd -k saliases' )
-[[ $sel = *R* ]] && args=( $args 'disabled-aliases:disabled regular alias:compadd -k dis_aliases' )
-[[ $sel = *G* ]] && args=( $args 'disabled-global-aliases:disabled global alias:compadd -k dis_galiases' )
-[[ $sel = *S* ]] && args=( $args 'disabled-suffix-aliases:disabled suffix alias:compadd -k dis_saliases' )
-
-_alternative -O opts $args
+_aliases "$@"
diff --git a/Completion/Zsh/Type/_command_names b/Completion/Zsh/Type/_command_names
index b1c35f013..874c564a4 100644
--- a/Completion/Zsh/Type/_command_names
+++ b/Completion/Zsh/Type/_command_names
@@ -29,7 +29,7 @@ else
   defs=( "$defs[@]"
     'builtins:builtin command:compadd -Qk builtins'
     "functions:shell function:compadd -k 'functions$ffilt'"
-    'aliases:alias:compadd -Qk aliases'
+    'aliases:alias:_aliases -s r'
     'suffix-aliases:suffix alias:_suffix_alias_files'
     'reserved-words:reserved word:compadd -Qk reswords'
     'jobs:: _jobs -t'
diff --git a/Test/Y01completion.ztst b/Test/Y01completion.ztst
index 882a0adc4..ae5b15204 100644
--- a/Test/Y01completion.ztst
+++ b/Test/Y01completion.ztst
@@ -286,6 +286,29 @@ F:regression test workers/31611
 >NO:{bat}
 >NO:{bay}
 
+  comptesteval "alias tst1='\tst1 tst2'; alias -g tst2=; alias -s tst3=tst3"
+  comptesteval 'zstyle ":completion:*:*aliases" extra-verbose yes'
+  comptest $'unalias tst\t'
+0:extra-verbose shows alias values
+>line: {unalias tst}{}
+>DESCRIPTION:{regular alias}
+>NO:{tst1  -- \tst1 tst2}
+>DESCRIPTION:{global alias}
+>NO:{tst2  -- ''}
+>DESCRIPTION:{suffix alias}
+>NO:{tst3  -- tst3}
+
+  comptesteval 'zstyle -d ":completion:*:*aliases" extra-verbose'
+  comptest $'unalias tst\t'
+0:alias values not shown without extra-verbose
+>line: {unalias tst}{}
+>DESCRIPTION:{regular alias}
+>NO:{tst1}
+>DESCRIPTION:{global alias}
+>NO:{tst2}
+>DESCRIPTION:{suffix alias}
+>NO:{tst3}
+
   comptesteval '_tst() { local disp=( {a..z} ); compadd -ld disp $disp[@]; comppostfuncs=( _pst ) }'
   comptesteval '_pst() { local disp=( "<INSERT>$compstate[insert]</INSERT>" ); compadd -Qld disp $disp }'
   comptesteval "zstyle ':completion:*' menu select=long-list"
-- 
2.30.1 (Apple Git-130)



Messages sorted by: Reverse Date, Date, Thread, Author