zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Don't complete escaped words as aliases
@ 2021-11-12 21:34 Marlon Richert
  2021-11-13  7:02 ` Mikael Magnusson
  0 siblings, 1 reply; 5+ messages in thread
From: Marlon Richert @ 2021-11-12 21:34 UTC (permalink / raw)
  To: Zsh hackers list

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

It doesn't make sense to complete \escaped 'words' as aliases, since
they won't be expanded as aliases either.

[-- Attachment #2: 0001-Don-t-complete-escaped-words-as-aliases.txt --]
[-- Type: text/plain, Size: 1803 bytes --]

From 9c8f1b6e5e3a8f60b151bccd1eea8438a40d6b23 Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlonrichert@users.noreply.github.com>
Date: Fri, 12 Nov 2021 23:24:58 +0200
Subject: [PATCH] Don't complete escaped words as aliases

---
 Completion/Zsh/Type/_command_names | 10 +++++++---
 Test/Y01completion.ztst            |  6 ++++++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/Completion/Zsh/Type/_command_names b/Completion/Zsh/Type/_command_names
index b1c35f013..3ed39141d 100644
--- a/Completion/Zsh/Type/_command_names
+++ b/Completion/Zsh/Type/_command_names
@@ -26,11 +26,15 @@ elif (( ${#precommands:|builtin_precommands} )); then
 else
   [[ "$1" = - ]] && shift
 
-  defs=( "$defs[@]"
+  [[ $words[CURRENT] == ${(Q)words[CURRENT]} ]] &&
+      defs+=(
+          'aliases:alias:compadd -Qk aliases'
+          'suffix-aliases:suffix alias:_suffix_alias_files'
+      )
+
+  defs+=(
     'builtins:builtin command:compadd -Qk builtins'
     "functions:shell function:compadd -k 'functions$ffilt'"
-    'aliases:alias:compadd -Qk aliases'
-    'suffix-aliases:suffix alias:_suffix_alias_files'
     'reserved-words:reserved word:compadd -Qk reswords'
     'jobs:: _jobs -t'
     'parameters:: _parameters -g "^*(readonly|association)*" -qS= -r "\n\t\- =[+"'
diff --git a/Test/Y01completion.ztst b/Test/Y01completion.ztst
index 882a0adc4..97886b30c 100644
--- a/Test/Y01completion.ztst
+++ b/Test/Y01completion.ztst
@@ -97,6 +97,12 @@
 >FI:{file2}
 F:regression test workers/42164
 
+  comptesteval 'alias while='
+  comptest '\while'$'\C-D'
+0:escaped words should not be completed as aliases
+>DESCRIPTION:{reserved word}
+>NO:{while}
+
 # Depends on path assignment in comptestinit
   comptesteval "path=( $ZTST_srcdir:A )"
   comptest $'zt\t'
-- 
2.30.1 (Apple Git-130)


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

* Re: [PATCH] Don't complete escaped words as aliases
  2021-11-12 21:34 [PATCH] Don't complete escaped words as aliases Marlon Richert
@ 2021-11-13  7:02 ` Mikael Magnusson
  2021-11-13 17:11   ` Marlon Richert
  0 siblings, 1 reply; 5+ messages in thread
From: Mikael Magnusson @ 2021-11-13  7:02 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Zsh hackers list

On 11/12/21, Marlon Richert <marlon.richert@gmail.com> wrote:
> It doesn't make sense to complete \escaped 'words' as aliases, since
> they won't be expanded as aliases either.

Would this break aliases of this form?

% alias 'foo\bar'=echo\ hi
% foo\bar
hi

-- 
Mikael Magnusson


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

* Re: [PATCH] Don't complete escaped words as aliases
  2021-11-13  7:02 ` Mikael Magnusson
@ 2021-11-13 17:11   ` Marlon Richert
  2021-11-13 20:59     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Marlon Richert @ 2021-11-13 17:11 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh hackers list

On Sat, Nov 13, 2021 at 9:02 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> On 11/12/21, Marlon Richert <marlon.richert@gmail.com> wrote:
> > It doesn't make sense to complete \escaped 'words' as aliases, since
> > they won't be expanded as aliases either.
>
> Would this break aliases of this form?
>
> % alias 'foo\bar'=echo\ hi
> % foo\bar
> hi

I think "break" is a bit of an overstatement. These aliases will still
be expanded once the command line has been accepted. They just won't
be completed.

If we require that aliases of that form are still completed, an
alternative could be to do `local +h PREFIX=$words[CURRENT] SUFFIX=`
before calling alias completion. The drawback to that approach is that
it effectively negates COMPLETE_IN_WORD. Getting unquoted versions of
$PREFIX and $SUFFIX, however, doesn't appear to be possible within a
completion function.

Would this be preferable? I can make that change. No other
alternatives come to mind right now.


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

* Re: [PATCH] Don't complete escaped words as aliases
  2021-11-13 17:11   ` Marlon Richert
@ 2021-11-13 20:59     ` Bart Schaefer
  2021-11-14 13:02       ` Oliver Kiddle
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2021-11-13 20:59 UTC (permalink / raw)
  To: Marlon Richert; +Cc: Mikael Magnusson, Zsh hackers list

On Sat, Nov 13, 2021 at 9:18 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> Would this be preferable? I can make that change. No other
> alternatives come to mind right now.

Hm, it seems as though what's really needed here is for the completion
internals to compare the full quoted word to the set of matches,
rather than remove the quoting first.  Skipping the completion because
the word is quoted is at best a workaround.

I suppose that would need a new option to compadd.  It might help in
some other edge cases as well -- lots of completion bugs arise from
the internals attempting to properly remove and restore quoting.

Aside from that, this circles right back to the discussion begun after
workers/49559, namely, whether it's preferable to complete too much
and let the user decide if he wants it, vs. to aggressively limit the
completions to what the zsh devs think is "only what makes sense in
this context."  Usually we've fallen on the side of "too much" in the
past.


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

* Re: [PATCH] Don't complete escaped words as aliases
  2021-11-13 20:59     ` Bart Schaefer
@ 2021-11-14 13:02       ` Oliver Kiddle
  0 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2021-11-14 13:02 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Marlon Richert, Mikael Magnusson, Zsh hackers list

Bart Schaefer wrote:
> On Sat, Nov 13, 2021 at 9:18 AM Marlon Richert <marlon.richert@gmail.com> wrote:
> >
> > Would this be preferable? I can make that change. No other
> > alternatives come to mind right now.

That approach would also have the effect of adding back in to $PREFIX
characters that were shifted off to $IPREFIX.

Another option might be to filter out just quoted aliases and complete
only them:

  aq=( "${(@Qk)aliases}" )
  print -lr --  "${(@k)aliases:|aq}"

> Hm, it seems as though what's really needed here is for the completion
> internals to compare the full quoted word to the set of matches,
> rather than remove the quoting first.  Skipping the completion because
> the word is quoted is at best a workaround.
>
> I suppose that would need a new option to compadd.  It might help in

Intuitively I would think this should be an additional effect of the
existing -Q option.

> some other edge cases as well -- lots of completion bugs arise from
> the internals attempting to properly remove and restore quoting.

The trouble is that completion fully strips quotes and parses the
command-line into words very early on. There's no getting the original
form back, at least not in a way where you know which character
corresponds to which. It would needed a layer of abstraction such as a
rope data structure when unquoting.

Oliver


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

end of thread, other threads:[~2021-11-14 13:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-12 21:34 [PATCH] Don't complete escaped words as aliases Marlon Richert
2021-11-13  7:02 ` Mikael Magnusson
2021-11-13 17:11   ` Marlon Richert
2021-11-13 20:59     ` Bart Schaefer
2021-11-14 13:02       ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).