zsh-workers
 help / color / mirror / Atom feed
* [PATCH] Make _expand_alias more usable as a completer
@ 2021-05-28 21:59 Marlon Richert
  2021-06-02 23:23 ` Oliver Kiddle
  0 siblings, 1 reply; 9+ messages in thread
From: Marlon Richert @ 2021-05-28 21:59 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 494 bytes --]

When _expand_alias is invoked as a completer, if 'complete' is set to
'true', let _expand_alias always return 1. This is analogous to how
this style behaves when _expand_alias is invoked as a widget or when
the _expand completer's 'accept-exact' style is set to 'continue',
which allows you to put it at the start of the 'completer' list.

Additionally, tag alias expansions as 'expansions', rather than as
'aliases', to prevent aliases & alias expansion from being lumped into
the same group.

[-- Attachment #2: 0001-Make-_expand_alias-more-usable-as-a-completer.txt --]
[-- Type: text/plain, Size: 2590 bytes --]

From 7722347b8e408413f7b49af5e16b1b4a518c1aa9 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@gmail.com>
Date: Sat, 29 May 2021 00:16:22 +0300
Subject: [PATCH] Make _expand_alias more usable as a completer

When _expand_alias is invoked as a completer, if 'complete' is
set to 'true', let _expand_alias always return 1. This is analogous to
how this style behaves when _expand_alias is invoked as a widget or when
the _expand completer's 'accept-exact' style is set to 'continue', which
allows you to put it at the start of the 'completer' list.

Additionally, tag alias expansions as 'expansions', rather than as
'aliases', to prevent aliases & alias expansion from being lumped into
the same group.
---
 Completion/Base/Completer/_expand_alias |  3 ++-
 Doc/Zsh/compsys.yo                      | 10 ++++++----
 2 files changed, 8 insertions(+), 5 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/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index 89b918d60..1b01f09a8 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -1295,10 +1295,12 @@ the two strings `tt(start)' and `tt(stop)'.
 )
 kindex(complete, completion style)
 item(tt(complete))(
-This is used by the tt(_expand_alias) function when invoked as a
-bindable command.  If set to `true' and the word on the command
-line is not the name of an alias, matching alias names will be
-completed.
+This is used by the tt(_expand_alias) function. When tt(_expand_alias) is 
+invoked as a bindable command, if tt(complete) is set to `true' and the word on 
+the command line is not the name of an alias, matching alias names will be 
+completed. When tt(_expand_alias) is invoked as a completer, if tt(complete) is 
+set to `true', tt(_expand_alias) will add the expansion as a match and the 
+completion system will also be allowed to continue.
 )
 kindex(complete-options, completion style)
 item(tt(complete-options))(
-- 
2.30.1 (Apple Git-130)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make _expand_alias more usable as a completer
  2021-05-28 21:59 [PATCH] Make _expand_alias more usable as a completer Marlon Richert
@ 2021-06-02 23:23 ` Oliver Kiddle
  2021-06-03 21:42   ` Marlon Richert
  0 siblings, 1 reply; 9+ messages in thread
From: Oliver Kiddle @ 2021-06-02 23:23 UTC (permalink / raw)
  To: Zsh hackers list

On 29 May, Marlon Richert wrote:
> When _expand_alias is invoked as a completer, if 'complete' is set to
> 'true', let _expand_alias always return 1. This is analogous to how
> this style behaves when _expand_alias is invoked as a widget or when
> the _expand completer's 'accept-exact' style is set to 'continue',
> which allows you to put it at the start of the 'completer' list.

In practical tests, I'm not finding that this works too well, it ends up
removing the characters typed. From _expand_aliases, you end up with a
call to compadd -QU and later you get a compadd -Q. For testing play
around with the following:

  _foo() {
    compadd -QU 'ls -al'
    compadd -Q lsusb lsvfs
  }
  compdef _foo foo
  foo ls<tab>

I can see the motivation for the feature but it needs compstate[insert]=menu
to be usable, at least from my setup. Setting that tends to be applicable
wherever you're using -U to change what was typed so far.

It feels like a bug that this example can't recognise the unambiguous
"ls" prefix but aliases often expand to something quite different to the
initial alias name.

I also wonder if this would be better done from within _command_names
directly but I'm not sure.

Oliver


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make _expand_alias more usable as a completer
  2021-06-02 23:23 ` Oliver Kiddle
@ 2021-06-03 21:42   ` Marlon Richert
  2021-06-20 19:49     ` Lawrence Velázquez
  0 siblings, 1 reply; 9+ messages in thread
From: Marlon Richert @ 2021-06-03 21:42 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Zsh hackers list

On Thu, Jun 3, 2021 at 2:24 AM Oliver Kiddle <opk@zsh.org> wrote:
> In practical tests, I'm not finding that this works too well, it ends up
> removing the characters typed.

I cannot reproduce that. Here's what I tried:

% cd $(mktemp -d)
% print "PS1='%# ' PS2='' RPS2='< %^'\nsetopt transientrprompt" > .zshrc
% exec env -i HOME=$PWD TERM=$TERM zsh -d
% autoload -Uz compinit
% compinit
% _foo() {
compadd -QU 'ls -al'
compadd -Q lsusb lsvfs
}
% compdef _foo foo
% foo <tab>
% foo ls<tab>
ls -al  lsusb   lsvfs

It inserts the unambiguous prefix and cycles through the three
choices, without removing any characters I've typed. Am I missing
something?

> I also wonder if this would be better done from within _command_names
> directly but I'm not sure.

Actually, I feel like it could instead be added to the _expand
completer. Would that be a better idea than what I presented in this
patch? I could, for example, move the actual business logic for
expanding aliases from _expand_alias to _expand, and then have
_expand_alias call _expand to do the work.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make _expand_alias more usable as a completer
  2021-06-03 21:42   ` Marlon Richert
@ 2021-06-20 19:49     ` Lawrence Velázquez
  2021-06-20 21:08       ` Mikael Magnusson
  0 siblings, 1 reply; 9+ messages in thread
From: Lawrence Velázquez @ 2021-06-20 19:49 UTC (permalink / raw)
  To: zsh-workers; +Cc: Marlon Richert

On Thu, Jun 3, 2021, at 5:42 PM, Marlon Richert wrote:
> On Thu, Jun 3, 2021 at 2:24 AM Oliver Kiddle <opk@zsh.org> wrote:
> > In practical tests, I'm not finding that this works too well, it ends up
> > removing the characters typed.
> 
> I cannot reproduce that. [...] Am I missing something?
> 
> > I also wonder if this would be better done from within _command_names
> > directly but I'm not sure.
> 
> Actually, I feel like it could instead be added to the _expand
> completer. Would that be a better idea than what I presented in this
> patch?

Anyone have answers to Marlon's questions?

-- 
vq


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make _expand_alias more usable as a completer
  2021-06-20 19:49     ` Lawrence Velázquez
@ 2021-06-20 21:08       ` Mikael Magnusson
  2021-06-20 22:24         ` Marlon Richert
  0 siblings, 1 reply; 9+ messages in thread
From: Mikael Magnusson @ 2021-06-20 21:08 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: zsh-workers, Marlon Richert

On 6/20/21, Lawrence Velázquez <larryv@zsh.org> wrote:
> On Thu, Jun 3, 2021, at 5:42 PM, Marlon Richert wrote:
>> On Thu, Jun 3, 2021 at 2:24 AM Oliver Kiddle <opk@zsh.org> wrote:
>> > In practical tests, I'm not finding that this works too well, it ends
>> > up
>> > removing the characters typed.
>>
>> I cannot reproduce that. [...] Am I missing something?
>>
>> > I also wonder if this would be better done from within _command_names
>> > directly but I'm not sure.
>>
>> Actually, I feel like it could instead be added to the _expand
>> completer. Would that be a better idea than what I presented in this
>> patch?
>
> Anyone have answers to Marlon's questions?

If it helps, I disagree with conflating _expand_alias (expanding
aliases) and _expand (filename expansion).

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make _expand_alias more usable as a completer
  2021-06-20 21:08       ` Mikael Magnusson
@ 2021-06-20 22:24         ` Marlon Richert
  2021-07-18 23:34           ` Lawrence Velázquez
  0 siblings, 1 reply; 9+ messages in thread
From: Marlon Richert @ 2021-06-20 22:24 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Lawrence Velázquez, Zsh hackers list

On Mon, Jun 21, 2021 at 12:08 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> On 6/20/21, Lawrence Velázquez <larryv@zsh.org> wrote:
> > On Thu, Jun 3, 2021, at 5:42 PM, Marlon Richert wrote:
> >> On Thu, Jun 3, 2021 at 2:24 AM Oliver Kiddle <opk@zsh.org> wrote:
> >>
> >> > I also wonder if this would be better done from within _command_names
> >> > directly but I'm not sure.
> >>
> >> Actually, I feel like it could instead be added to the _expand
> >> completer. Would that be a better idea than what I presented in this
> >> patch?
> >
> > Anyone have answers to Marlon's questions?
>
> If it helps, I disagree with conflating _expand_alias (expanding
> aliases) and _expand (filename expansion).

_expand already handles a lot more than just "filename expansion"
(which it actually does not handle at all). A quick test going through
all forms of expansion listed at
https://zsh.sourceforge.io/Doc/Release/Expansion.html shows that
_expand currently handles all of the following:
* parameter expansion
* command substitution
* arithmetic expansion
* brace expansion
* filename generation
(but not history expansion, alias expansion, process substitution or
filename expansion).

Since alias expansion is in fact listed at
https://zsh.sourceforge.io/Doc/Release/Expansion.html, I don't think
it would be too weird to add it to the _expand completer.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make _expand_alias more usable as a completer
  2021-06-20 22:24         ` Marlon Richert
@ 2021-07-18 23:34           ` Lawrence Velázquez
  2021-07-28  3:23             ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Lawrence Velázquez @ 2021-07-18 23:34 UTC (permalink / raw)
  To: zsh-workers; +Cc: Marlon Richert

On Sun, Jun 20, 2021, at 6:24 PM, Marlon Richert wrote:
> On Mon, Jun 21, 2021 at 12:08 AM Mikael Magnusson <mikachu@gmail.com> wrote:
> >
> > On 6/20/21, Lawrence Velázquez <larryv@zsh.org> wrote:
> > > On Thu, Jun 3, 2021, at 5:42 PM, Marlon Richert wrote:
> > >> On Thu, Jun 3, 2021 at 2:24 AM Oliver Kiddle <opk@zsh.org> wrote:
> > >>
> > >> > I also wonder if this would be better done from within _command_names
> > >> > directly but I'm not sure.
> > >>
> > >> Actually, I feel like it could instead be added to the _expand
> > >> completer. Would that be a better idea than what I presented in this
> > >> patch?
> > >
> > > Anyone have answers to Marlon's questions?
> >
> > If it helps, I disagree with conflating _expand_alias (expanding
> > aliases) and _expand (filename expansion).
> 
> _expand already handles a lot more than just "filename expansion"
> (which it actually does not handle at all). A quick test going through
> all forms of expansion listed at
> https://zsh.sourceforge.io/Doc/Release/Expansion.html shows that
> _expand currently handles all of the following:
> * parameter expansion
> * command substitution
> * arithmetic expansion
> * brace expansion
> * filename generation
> (but not history expansion, alias expansion, process substitution or
> filename expansion).
> 
> Since alias expansion is in fact listed at
> https://zsh.sourceforge.io/Doc/Release/Expansion.html, I don't think
> it would be too weird to add it to the _expand completer.

Any other feedback on 48946?

-- 
vq


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] Make _expand_alias more usable as a completer
  2021-07-18 23:34           ` Lawrence Velázquez
@ 2021-07-28  3:23             ` Bart Schaefer
  2021-08-01 18:50               ` [PATCH] Make _expand handle aliases (was Re: [PATCH] Make _expand_alias more usable as a completer) Marlon Richert
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2021-07-28  3:23 UTC (permalink / raw)
  To: Zsh hackers list; +Cc: Marlon Richert

On Sun, Jul 18, 2021 at 4:35 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> On Sun, Jun 20, 2021, at 6:24 PM, Marlon Richert wrote:
> > On Mon, Jun 21, 2021 at 12:08 AM Mikael Magnusson <mikachu@gmail.com> wrote:
> > >
> > > On 6/20/21, Lawrence Velázquez <larryv@zsh.org> wrote:
> > > > On Thu, Jun 3, 2021, at 5:42 PM, Marlon Richert wrote:
> > > >> On Thu, Jun 3, 2021 at 2:24 AM Oliver Kiddle <opk@zsh.org> wrote:
> > > >>
> > > >> > I also wonder if this would be better done from within _command_names
> > > >> > directly but I'm not sure.

Handling it in _command_names would not be sufficient for global
aliases.  That said, I haven't actually investigated the use of
_expand_alias for global aliases.

> > > >> Actually, I feel like it could instead be added to the _expand
> > > >> completer. Would that be a better idea than what I presented in this
> > > >> patch?
> > > >
> > > > Anyone have answers to Marlon's questions?
> > >
> > > If it helps, I disagree with conflating _expand_alias (expanding
> > > aliases) and _expand (filename expansion).
> >
> > _expand already handles a lot more than just "filename expansion"
> > (which it actually does not handle at all).

Naming has always been a bit strange here.  Yes, strictly per the
docs, "filename expansion" refers to replacing a leading tilde with a
path prefix, and "filename generation" is a fancy term for globbing
but is considered a form of expansion.  I've forgotten why we couldn't
just stick with "tilde expansion" and "globbing" and leave "filename"
out of it entirely, but there you are.  In any event I think it's
likely that Mikael was referring to globbing.

> > Since alias expansion is in fact listed at
> > https://zsh.sourceforge.io/Doc/Release/Expansion.html, I don't think
> > it would be too weird to add it to the _expand completer.

I think it's actually a bit weird to allow _expand_alias to work as a
completer at all, because the completion system generally functions
very badly when the matches do not consist of single words, and
aliases don't usually have one-word replacements.  As soon as you
introduce something that contains spaces, completion gets very
confused (unless you force it directly into menu completion or
selection); if (otherwise) you attempt to press TAB twice to enter
menu completion, it'll instead start a new completion based on the
word after the rightmost space that is still to the left of the
cursor.  This is especially true when using compadd -U.

I suspect this may be where Oliver's tests and Marlon's attempt to
reproduce, went astray from one another.  In any case, I'm fairly sure
this is the reason that _expand_alias (before the patch) does not
allow other completers to run, and similarly it's why the user is
admonished to place _expand before _complete in the zstyle.

> Any other feedback on 48946?

Looking independently at the two questions, my feeling is I'd avoid
the patch as-is, but it wouldn't be unreasonable for _expand to handle
aliases (under control of a zstyle).


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH] Make _expand handle aliases (was Re: [PATCH] Make _expand_alias more usable as a completer)
  2021-07-28  3:23             ` Bart Schaefer
@ 2021-08-01 18:50               ` Marlon Richert
  0 siblings, 0 replies; 9+ messages in thread
From: Marlon Richert @ 2021-08-01 18:50 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh hackers list, Oliver Kiddle

[-- Attachment #1: Type: text/plain, Size: 1696 bytes --]

On Wed, Jul 28, 2021 at 6:23 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> I think it's actually a bit weird to allow _expand_alias to work as a
> completer at all, because the completion system generally functions
> very badly when the matches do not consist of single words, and
> aliases don't usually have one-word replacements.  As soon as you
> introduce something that contains spaces, completion gets very
> confused (unless you force it directly into menu completion or
> selection); if (otherwise) you attempt to press TAB twice to enter
> menu completion, it'll instead start a new completion based on the
> word after the rightmost space that is still to the left of the
> cursor.  This is especially true when using compadd -U.
>
> I suspect this may be where Oliver's tests and Marlon's attempt to
> reproduce, went astray from one another.  In any case, I'm fairly sure
> this is the reason that _expand_alias (before the patch) does not
> allow other completers to run, and similarly it's why the user is
> admonished to place _expand before _complete in the zstyle.
>
> Looking independently at the two questions, my feeling is I'd avoid
> the patch as-is, but it wouldn't be unreasonable for _expand to handle
> aliases (under control of a zstyle).

Alright, here's a new patch, which moves the alias expansion code from
_expand_alias to _expand (and refactors it) and deprecates
_expand_alias's use as a completer. I also updated the documentation
and added tests.

Note that I reused the zstyle names from _expand_alias in _alias. I
think their names are less than optimal in _expand, but renaming them
would break people's existing zstyle config for _expand_alias.

[-- Attachment #2: 0001-Make-_expand-handle-aliases.txt --]
[-- Type: text/plain, Size: 12629 bytes --]

From 7467abb482824650913636340844919be4bcf212 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlonrichert@users.noreply.github.com>
Date: Sun, 1 Aug 2021 21:40:01 +0300
Subject: [PATCH] Make _expand handle aliases

This deprecates _expand_alias's use as a completer.
---
 Completion/Base/Completer/_expand       | 74 ++++++++++++++++++++++++-
 Completion/Base/Completer/_expand_alias | 70 +++--------------------
 Doc/Zsh/compsys.yo                      | 62 +++++++++------------
 Test/Y01completion.ztst                 | 16 +++++-
 4 files changed, 120 insertions(+), 102 deletions(-)

diff --git a/Completion/Base/Completer/_expand b/Completion/Base/Completer/_expand
index 86b4ac6e4..7a24f8aa1 100644
--- a/Completion/Base/Completer/_expand
+++ b/Completion/Base/Completer/_expand
@@ -11,11 +11,12 @@ setopt localoptions nonomatch
 
 [[ _matcher_num -gt 1 ]] && return 1
 
-local exp word sort expr expl subd pref suf=" " force opt asp tmp opre pre epre
-local continue=0
+local -a exp subd tmpa match mbegin mend
+local -i continue=0
+local word sort expl pref suf=' ' force opt asp tmp opre pre epre
 
 (( $# )) &&
-    while getopts gsco opt; do
+    while getopts agsco opt; do
       force="$force$opt"
     done
 
@@ -25,6 +26,73 @@ else
   word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
 fi
 
+# Alias expansion:
+() {
+
+  if { (( CURRENT == 1 )) && zstyle -T ":completion:${curcontext}:" regular } ||
+      zstyle -t ":completion:${curcontext}:" regular always; then 
+    exp=( $aliases[$word] )
+    if [[ -n "$exp" ]]; then
+      # If the first shell word in the expansion is the same as the alias, then 
+      # escape it, so it doesn't get re-expanded on execution.
+      [[ "${${(z)exp}[1]}" = "$word" ]] &&
+          exp=( "${(S)exp/#(#b)(*)$word/$match[1]\\$word}" )
+      return 0
+    fi
+  fi
+
+  if zstyle -T ":completion:${curcontext}:" global; then
+    exp=( $galiases[$word] )
+    if [[ -n "$exp" ]]; then
+      # Escape all occurences of the global alias in its expansion, so these 
+      # don't get re-expanded on execution.
+      tmp="$exp"
+      tmpa=( ${(z)tmp} )
+      exp=()
+      while (( ${#tmpa[@]} )); do
+        exp[1]+="${(M)tmp#*$tmpa[1]}"
+        tmp="${tmp#*$tmpa[1]}"
+        [[ "$tmpa[1]" == "$word" ]] &&
+            exp[1]="${exp/%$word/\\$word}"
+        shift tmpa
+      done
+      exp[1]+="$tmp"
+      tmp=
+      return 0
+    fi
+  fi
+
+  if zstyle -t ":completion:${curcontext}:" disabled; then
+    exp=( $dis_aliases[$word] )
+    [[ -n "$exp" ]] && 
+        return
+  fi
+
+  if zstyle -T ":completion:${curcontext}:" global && 
+      zstyle -t ":completion:${curcontext}:" disabled; then
+    exp=( $dis_galiases[$word] )
+    [[ -n "$exp" ]] &&
+        return
+  fi
+
+  return 1
+
+} && {
+  zstyle -T ":completion:${curcontext}:" add-space true yes on 1 &&
+      tmpa=( -S ' ' )
+  _description expansions expl expansions "o:$word"
+
+  if compadd "$expl[@]" -UQ $tmpa[@] -- $exp; then
+    ! zstyle -t ":completion:${curcontext}:" accept-exact continue
+    return
+  fi
+
+}
+
+# If we've been called with `-a`, then don't expand anything else than aliases.
+[[ "$force" = *a* ]] &&
+    return 1
+
 [[ "$word" = *\$(|\{[^\}]#) ||
    ( "$word" = *\$[a-zA-Z0-9_]## && $+parameters[${word##*\$}] -eq 0 ) ]] &&
     return 1
diff --git a/Completion/Base/Completer/_expand_alias b/Completion/Base/Completer/_expand_alias
index 8240e4162..c588dfadb 100644
--- a/Completion/Base/Completer/_expand_alias
+++ b/Completion/Base/Completer/_expand_alias
@@ -1,67 +1,15 @@
-#compdef -K _expand_alias complete-word \C-xa
+#compdef -K _expand_alias complete-word \C-Xa
 
-local word expl tmp pre sel what
-local -a tmpa suf
-
-eval "$_comp_setup"
-
-if [[ -n $funcstack[2] ]]; then
-  if [[ "$funcstack[2]" = _prefix ]]; then
-    word="$IPREFIX$PREFIX$SUFFIX"
-  else
-    word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
-  fi
-  pre=()
+if (( ${funcstack[(I)_main_complete]} )); then
+  compstate[to_end]=match
+  _main_complete _expand_alias
 else
-  local curcontext="$curcontext"
 
-  if [[ -z "$curcontext" ]]; then
-    curcontext="expand-alias-word:::"
-  else
-    curcontext="expand-alias-word:${curcontext#*:}"
+  if _expand -a; then
+    compstate[insert]=1
+    return
   fi
 
-  word="$IPREFIX$PREFIX$SUFFIX$ISUFFIX"
-  pre=(_main_complete - aliases)
-fi
-
-zstyle -s ":completion:${curcontext}:" regular tmp || tmp=yes
-case $tmp in
-always) sel=r;;
-yes|1|true|on) [[ CURRENT -eq 1 ]] && sel=r;;
-esac
-zstyle -T ":completion:${curcontext}:" global && sel="g$sel"
-zstyle -t ":completion:${curcontext}:" disabled && sel="${sel}${(U)sel}"
-
-tmp=
-[[ $sel = *r* ]] && tmp=$aliases[$word]
-[[ -z $tmp && $sel = *g* ]] && tmp=$galiases[$word]
-[[ -z $tmp && $sel = *R* ]] && tmp=$dis_aliases[$word]
-[[ -z $tmp && $sel = *G* ]] && tmp=$dis_galiases[$word]
-
-if [[ -n $tmp ]]; then
-  # We used to remove the quoting from the value in the parameter.
-  # That was probably just an oversight: an alias is always replaced
-  # literally.
-  tmp=${tmp%%[[:blank:]]##}
-  if [[ $tmp[1] = [[:alnum:]_] ]]; then
-    tmpa=(${(z)tmp})
-    if [[ $tmpa[1] = $word && $tmp = $aliases[$word] ]]; then
-      # This is an active regular alias and the first word in the result
-      # is the same as what was on the line already.  Quote it so
-      # that it doesn't get reexanded on execution.
-      #
-      # Strictly we also need to check if the original word matches
-      # a later word in the expansion and the previous words are
-      # all aliases where the expansion ends in " ", but I'm
-      # too lazy.
-      tmp="\\$tmp"
-    fi
-  fi
-  zstyle -T ":completion:${curcontext}:" add-space || suf=( -S '' )
-  $pre _wanted aliases expl alias compadd -UQ "$suf[@]" -- ${tmp%%[[:blank:]]##}
-elif (( $#pre )) && zstyle -t ":completion:${curcontext}:" complete; then
-  $pre _aliases -s "$sel" -S ''
-else
-  return 1
+  zstyle -t ":completion:${curcontext}:" complete &&
+      _main_complete - expand-alias _aliases
 fi
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index 89b918d60..88a368608 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -1179,10 +1179,10 @@ style (see below) to `false'.
 )
 kindex(add-space, completion style)
 item(tt(add-space))(
-This style is used by the tt(_expand) completer.  If it is `true' (the
-default), a space will be inserted after all words resulting from the 
-expansion, or a slash in the case of directory names.  If the value
-is `tt(file)', the completer will only add a space
+This style is used by the tt(_expand) completer and the tt(_expand_alias) 
+bindable command.  If it is `true' (the default), a space will be inserted 
+after all words resulting from the expansion, or a slash in the case of 
+directory names.  If the value is `tt(file)', the completer will only add a space
 to names of existing files.  Either a boolean `true' or the value
 `tt(file)' may be combined with `tt(subst)', in which case the completer
 will not add a space to words generated from the expansion of a
@@ -1295,10 +1295,9 @@ the two strings `tt(start)' and `tt(stop)'.
 )
 kindex(complete, completion style)
 item(tt(complete))(
-This is used by the tt(_expand_alias) function when invoked as a
-bindable command.  If set to `true' and the word on the command
-line is not the name of an alias, matching alias names will be
-completed.
+When the tt(_expand_alias) bindable command is invoked, if tt(complete) is set 
+to `true' and the word on the command line is not the name of an alias, 
+matching alias names will be completed.
 )
 kindex(complete-options, completion style)
 item(tt(complete-options))(
@@ -1362,8 +1361,8 @@ may be empty to force a delimiter to be typed.
 )
 kindex(disabled, completion style)
 item(tt(disabled))(
-If this is set to `true', the tt(_expand_alias) completer and bindable 
-command will try to expand disabled aliases, too.  The default is
+If this is set to `true', the tt(_expand) completer and the tt(_expand_alias) 
+bindable command will try to expand disabled aliases, too.  The default is
 `false'.
 )
 kindex(domains, completion style)
@@ -1656,8 +1655,8 @@ style) or else the original string from the line.
 )
 kindex(global, completion style)
 item(tt(global))(
-If this is set to `true' (the default), the tt(_expand_alias)
-completer and bindable command will try to expand global aliases.
+If this is set to `true' (the default), the tt(_expand) completer and the 
+tt(_expand_alias) bindable command will try to expand global aliases.
 )
 kindex(group-name, completion style)
 item(tt(group-name))(
@@ -2480,11 +2479,11 @@ tt(zle_tr<TAB>) can be completed to tt(Zle/zle_tricky.c).
 )
 kindex(regular, completion style)
 item(tt(regular))(
-This style is used by the tt(_expand_alias) completer and bindable 
-command.  If set to `true' (the default), regular aliases will be
-expanded but only in command position.  If it is set to `false',
-regular aliases will never be expanded.   If it is set to `tt(always)',
-regular aliases will be expanded even if not in command position.
+This style is used by the tt(_expand) completer and the tt(_expand_alias) 
+bindable command.  If set to `true' (the default), regular aliases will be
+expanded but only in command position.  If it is set to `false', regular 
+aliases will never be expanded.   If it is set to `tt(always)', regular aliases 
+will be expanded even if not in command position.
 )
 kindex(rehash, completion style)
 item(tt(rehash))(
@@ -3173,17 +3172,8 @@ tt(glob) and tt(subst-globs-only) styles.
 
 It is also possible to call tt(_expand) as a function, in which case the
 different modes may be selected with options: tt(-s) for
-tt(substitute), tt(-g) for tt(glob) and tt(-o) for tt(subst-globs-only).
-)
-findex(_expand_alias)
-item(tt(_expand_alias))(
-If the word the cursor is on is an alias, it is expanded and no other
-completers are called.  The types of aliases which are to be expanded can
-be controlled with the styles tt(regular), tt(global) and tt(disabled).
-
-This function is also a bindable command, see
-ifzman(the section `Bindable Commands' below)\
-ifnzman(noderef(Bindable Commands)).
+tt(substitute), tt(-g) for tt(glob) and tt(-o) for tt(subst-globs-only). 
+Alternatively, if called with tt(-a), tt(_expand) will do alias expansion only.
 )
 findex(_extensions)
 item(tt(_extensions))(
@@ -3406,17 +3396,15 @@ then calls the tt(_correct) completer.
 )
 findex(_expand_alias (^Xa))
 item(tt(_expand_alias) (tt(^Xa)))(
-This function can be used as a completer and as a bindable command.
-It expands the word the cursor is on if it is an alias.  The types of
-alias expanded can be controlled with the styles tt(regular), tt(global)
+This function expands the word the cursor is on if it is an alias.  The types 
+of alias expanded can be controlled with the styles tt(regular), tt(global)
 and tt(disabled).
 
-When used as a bindable command there is one additional feature that
-can be selected by setting the tt(complete) style to `true'.  In this
-case, if the word is not the name of an alias, tt(_expand_alias) tries
-to complete the word to a full alias name without expanding it.  It
-leaves the cursor directly after the completed word so that invoking
-tt(_expand_alias) once more will expand the now-complete alias name.
+Additionally, if the tt(complete) style is set to `true' and the word is not 
+the name of an alias, then tt(_expand_alias) tries to complete the word to a 
+full alias name without expanding it.  It leaves the cursor directly after the 
+completed word so that invoking tt(_expand_alias) once more will expand the 
+now-complete alias name.
 )
 findex(_expand_word (^Xe))
 item(tt(_expand_word) (tt(^Xe)))(
diff --git a/Test/Y01completion.ztst b/Test/Y01completion.ztst
index 882a0adc4..ed7f6738d 100644
--- a/Test/Y01completion.ztst
+++ b/Test/Y01completion.ztst
@@ -44,8 +44,22 @@
 >line: {: dir1/}{}
 >line: {: dir2/}{}
 
+  comptesteval "alias \*=' * * \$\$ tst'"
+  comptesteval "alias -g '\$\$'=' * \$\$ \$\$ tst'"
+  comptest $' *\C-D'
+0:_expand substitutes regular aliases and escapes them, if necessary
+>DESCRIPTION:{expansions}
+>NO:{ \* * $$ tst}
+
+  comptest $': $$\C-D'
+0:_expand can substitutes global aliases and escapes them, if necessary
+>DESCRIPTION:{expansions}
+>NO:{ * \$$ \$$ tst}
+
+  comptesteval "zstyle '*' regular no"
+  comptesteval "zstyle '*' global no"
   comptest $': *\t\t\t\t\t\t'
-0:_expand shows file types
+0:_expand shows file types (and does not substitute aliases, when this is disabled)
 >line: {: dir1/}{}
 >DESCRIPTION:{expansions}
 >DI:{dir1}
-- 
2.30.1 (Apple Git-130)


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-08-01 18:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-28 21:59 [PATCH] Make _expand_alias more usable as a completer Marlon Richert
2021-06-02 23:23 ` Oliver Kiddle
2021-06-03 21:42   ` Marlon Richert
2021-06-20 19:49     ` Lawrence Velázquez
2021-06-20 21:08       ` Mikael Magnusson
2021-06-20 22:24         ` Marlon Richert
2021-07-18 23:34           ` Lawrence Velázquez
2021-07-28  3:23             ` Bart Schaefer
2021-08-01 18:50               ` [PATCH] Make _expand handle aliases (was Re: [PATCH] Make _expand_alias more usable as a completer) Marlon Richert

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ https://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git