zsh-users
 help / color / mirror / Atom feed
* Re: _pick_variant
  2020-11-13 17:41 _pick_variant Thomas Lauer
@ 2020-11-12 18:35 ` Bart Schaefer
  2020-11-12 18:40   ` _pick_variant Roman Perepelitsa
  2020-11-13  9:34   ` _pick_variant Thomas Lauer
  0 siblings, 2 replies; 13+ messages in thread
From: Bart Schaefer @ 2020-11-12 18:35 UTC (permalink / raw)
  To: Thomas Lauer; +Cc: Zsh Users

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

On Thu, Nov 12, 2020 at 9:41 AM Thomas Lauer <thomas.lauer@virgin.net>
wrote:

>
> This works as expected. However, if I rename it to md5() { ... }, zsh
> hangs when I type
> md5 a<tab> to expand a filename.
>

Zsh is invoking the completion for the system default "md5" command
(sometimes found as /sbin/md5).  That completion attempts to run "md5
--version" to find out whether it is completing for GNU md5 or some other
variant.  Your function doesn't handle that, and invokes xclip in the
background, which hangs.

[-- Attachment #2: Type: text/html, Size: 862 bytes --]

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

* Re: _pick_variant
  2020-11-12 18:35 ` _pick_variant Bart Schaefer
@ 2020-11-12 18:40   ` Roman Perepelitsa
  2020-11-12 19:07     ` _pick_variant Ray Andrews
  2020-11-13  9:34   ` _pick_variant Thomas Lauer
  1 sibling, 1 reply; 13+ messages in thread
From: Roman Perepelitsa @ 2020-11-12 18:40 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Thomas Lauer, Zsh Users

By the way, what's the recommended way to deal with this sort of
issue? A few times I've defined a personal script with a short name
and discovered the hard way that zsh supplies a completion function
for a utility of the same name (that I've never heard of). I usually
end up adding `compdef _default myutility` to `~/.zshrc` whenever this
happens. Is there a better and more proactive way?

Roman.


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

* Re: _pick_variant
  2020-11-12 18:40   ` _pick_variant Roman Perepelitsa
@ 2020-11-12 19:07     ` Ray Andrews
  2020-11-12 19:17       ` _pick_variant Roman Perepelitsa
  2020-11-12 19:20       ` Fwd: _pick_variant JAMES R CUTLER
  0 siblings, 2 replies; 13+ messages in thread
From: Ray Andrews @ 2020-11-12 19:07 UTC (permalink / raw)
  To: zsh-users

On 2020-11-12 10:40 a.m., Roman Perepelitsa wrote:
> and discovered the hard way that zsh supplies a completion function
> for a utility of the same name
That's why my last question was how to turn all that kind of thing off.  
Cool that it's there when you need it, but there are those bizarre 
situations.  I understand that the power of the thing makes it 
impossible to be simple at the same time, still one might wish for some 
panic button that ... well, again I can see that it is always going to 
be difficult and I did find out how to complete only to local files so 
I'm safe.



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

* Re: _pick_variant
  2020-11-12 19:07     ` _pick_variant Ray Andrews
@ 2020-11-12 19:17       ` Roman Perepelitsa
  2020-11-12 19:20       ` Fwd: _pick_variant JAMES R CUTLER
  1 sibling, 0 replies; 13+ messages in thread
From: Roman Perepelitsa @ 2020-11-12 19:17 UTC (permalink / raw)
  To: Ray Andrews; +Cc: Zsh Users

On Thu, Nov 12, 2020 at 8:08 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> On 2020-11-12 10:40 a.m., Roman Perepelitsa wrote:
> > and discovered the hard way that zsh supplies a completion function
> > for a utility of the same name
>
> That's why my last question was how to turn all that kind of thing off.

Sounds like an instance of XY problem. If the problem you are having
is caused by builtin completion functions clashing with your own
private scripts, it doesn't necessarily follow that the best solution
is to disable programmable completions.

FWIW, I know how to disable smart completions for all my scripts
(because they are all in ~/bin, so it's easy) and all my functions
(because they are in ~/functions) with a few lines of zsh but I'm
still wondering what other people do.

Roman.


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

* Fwd: _pick_variant
  2020-11-12 19:07     ` _pick_variant Ray Andrews
  2020-11-12 19:17       ` _pick_variant Roman Perepelitsa
@ 2020-11-12 19:20       ` JAMES R CUTLER
  1 sibling, 0 replies; 13+ messages in thread
From: JAMES R CUTLER @ 2020-11-12 19:20 UTC (permalink / raw)
  To: zsh-users

On 2020-11-12 10:40 a.m., Roman Perepelitsa wrote:
> and discovered the hard way that zsh supplies a completion function
> for a utility of the same name

1.  The simplest technique for dealing with a flat namespace in any programming language, including shell, is to prefix local names. For example, my command scripts uses “my_” for prefixing where naming collisions may occur. I suppose I could have used “JRC_”.

2. “which” is your friend.




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

* Re: _pick_variant
  2020-11-12 18:35 ` _pick_variant Bart Schaefer
  2020-11-12 18:40   ` _pick_variant Roman Perepelitsa
@ 2020-11-13  9:34   ` Thomas Lauer
  2020-11-13 10:42     ` _pick_variant Roman Perepelitsa
  1 sibling, 1 reply; 13+ messages in thread
From: Thomas Lauer @ 2020-11-13  9:34 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

On Thu, 12 Nov 2020 10:35:05 -0800 Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Thu, Nov 12, 2020 at 9:41 AM Thomas Lauer <thomas.lauer@virgin.net>
> wrote:
> 
> >
> > This works as expected. However, if I rename it to md5() { ... }, zsh
> > hangs when I type
> > md5 a<tab> to expand a filename.
> >
> 
> Zsh is invoking the completion for the system default "md5" command
> (sometimes found as /sbin/md5).  That completion attempts to run "md5
> --version" to find out whether it is completing for GNU md5 or some other
> variant.

There's no md5 command anywhere on my system. I have md5{sum,pass} and that's it.

> Your function doesn't handle that, and invokes xclip in the
> background, which hangs.

Well, the quick solution is to change the name, as I did. However, when you say my "function doesn't handle that" this seems to imply that there's a way it could handle that situation. How?

And THX for the response.

-- 
Tom


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

* Re: _pick_variant
  2020-11-13  9:34   ` _pick_variant Thomas Lauer
@ 2020-11-13 10:42     ` Roman Perepelitsa
  2020-11-13 10:57       ` _pick_variant Thomas Lauer
                         ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Roman Perepelitsa @ 2020-11-13 10:42 UTC (permalink / raw)
  To: Thomas Lauer; +Cc: Bart Schaefer, Zsh Users

On Fri, Nov 13, 2020 at 10:35 AM Thomas Lauer <thomas.lauer@virgin.net> wrote:
>
> There's no md5 command anywhere on my system. I have md5{sum,pass} and that's it.

The clash isn't with an existing command but rather with a completion
function that's included in zsh. Zsh will use the completion function
_cksum whenever you are completing arguments of the command md5. It
doesn't matter (to Zsh) where the md5 command came from.

My latest similar clash was with the open command. This is a macOS
command but I'm using Linux, so I cannot even have the open command
that _open expects.

> Well, the quick solution is to change the name, as I did. However, when you say my "function doesn't handle that" this seems to imply that there's a way it could handle that situation. How?

If you want to complete only files when invoking md5, add this to
~/.zshrc (make sure it's after compinit):

    compdef _default md5

Alternatively, replace $1 with "$@" in the definition of your md5 and
tell zsh to complete md5 as md5sum:

    compdef md5=md5sum

The first solution can be generalized:

    () {
      local k v
      for k v in "${(kv)_comps[@]}"; do
        if [[ ${functions_source[$k]:-$commands[$k]} == ~/* &&
              $functions_source[$v] != ~/* ]]; then
          _comps[$k]=_default
        fi
      done
    }

This will disable programmable completions for all commands and
functions defined under $HOME that don't have their own completions.
If you have compdef calls in your dotfiles, you'll need to move them
below this stanza.

Roman.


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

* Re: _pick_variant
  2020-11-13 10:42     ` _pick_variant Roman Perepelitsa
@ 2020-11-13 10:57       ` Thomas Lauer
  2020-11-13 11:18       ` _pick_variant Roman Perepelitsa
  2020-11-13 22:26       ` _pick_variant Bart Schaefer
  2 siblings, 0 replies; 13+ messages in thread
From: Thomas Lauer @ 2020-11-13 10:57 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Bart Schaefer, Zsh Users

On Fri, 13 Nov 2020 11:42:07 +0100 Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:

> <snipped long explanation>

THX for that detailed write-up, much appreciated. I will
chew over it... something new to learn every day.

Case closed AFAIC.

-- 
Tom


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

* Re: _pick_variant
  2020-11-13 10:42     ` _pick_variant Roman Perepelitsa
  2020-11-13 10:57       ` _pick_variant Thomas Lauer
@ 2020-11-13 11:18       ` Roman Perepelitsa
  2020-11-13 22:26       ` _pick_variant Bart Schaefer
  2 siblings, 0 replies; 13+ messages in thread
From: Roman Perepelitsa @ 2020-11-13 11:18 UTC (permalink / raw)
  To: Thomas Lauer; +Cc: Bart Schaefer, Zsh Users

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

On Fri, Nov 13, 2020 at 11:42 AM Roman Perepelitsa <
roman.perepelitsa@gmail.com> wrote:

> The first solution can be generalized:
>
>     () {
>       local k v
>       for k v in "${(kv)_comps[@]}"; do
>         if [[ ${functions_source[$k]:-$commands[$k]} == ~/* &&
>               $functions_source[$v] != ~/* ]]; then
>           _comps[$k]=_default
>         fi
>       done
>     }
>
> This will disable programmable completions for all commands and
> functions defined under $HOME that don't have their own completions.
> If you have compdef calls in your dotfiles, you'll need to move them
> below this stanza.
>

If anyone wants to use this, beware that it may increase zsh startup time.
On a raspberry pi -- the slowest machine I've tested this on -- it takes
45ms. Here's a faster version that does the same thing. As is often the
case with optimized zsh, it's unreadable.

  () {
    emulate -L zsh -o extended_glob
    local cmd MATCH MBEGIN MEND
    for cmd in
${${(M)${(k)_comps:/(#m)*/${${functions_source[$MATCH]:+$functions_source[$MATCH]/$MATCH}:-$commands[$MATCH]}}:#~/*}:t};
do
      if [[ $functions_source[$_comps[$cmd]] != ~/* ]]; then
        _comps[$cmd]=_default
      fi
    done
    fi
  }

Roman.

[-- Attachment #2: Type: text/html, Size: 1902 bytes --]

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

* _pick_variant
@ 2020-11-13 17:41 Thomas Lauer
  2020-11-12 18:35 ` _pick_variant Bart Schaefer
  0 siblings, 1 reply; 13+ messages in thread
From: Thomas Lauer @ 2020-11-13 17:41 UTC (permalink / raw)
  To: zsh-users

I tend to try and solve problems on my own but this one has really left me puzzled.

Consider this simple function in my zsh init:
mde5() {
	local sum=$(md5sum $1)
	echo $sum
	echo $sum md5 | cut -d " " -f 1,4 | xclip -i -selection clipboard
}

This works as expected. However, if I rename it to md5() { ... }, zsh hangs when I type
md5 a<tab> to expand a filename. Ctrl+C shows this message:
" illed by signal in _pick_variant after 1s" (yes the first character is a blank).

Hm... after going through a few permutations I found out that the hang disappears if I remove the final xclip command, ie:
md5() {
	local sum=$(md5sum $1)
	echo $sum
	echo $sum md5 | cut -d " " -f 1,4
}

No hangs and I have no idea what's going on. :-/


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

* Re: _pick_variant
  2020-11-13 10:42     ` _pick_variant Roman Perepelitsa
  2020-11-13 10:57       ` _pick_variant Thomas Lauer
  2020-11-13 11:18       ` _pick_variant Roman Perepelitsa
@ 2020-11-13 22:26       ` Bart Schaefer
  2020-11-14  9:56         ` _pick_variant Thomas Lauer
  2 siblings, 1 reply; 13+ messages in thread
From: Bart Schaefer @ 2020-11-13 22:26 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Thomas Lauer, Zsh Users

On Fri, Nov 13, 2020 at 2:42 AM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> The clash isn't with an existing command but rather with a completion
> function that's included in zsh.

It is conceivably possible to rewrite compinit such that it does not
install completions for commands not found in $path, but as you
mention in a later message in this thread, that will increase startup
time, and/or has to be incorporated into the .zcompdump cache.

> My latest similar clash was with the open command. This is a macOS
> command but I'm using Linux, so I cannot even have the open command
> that _open expects.

That's why _open is in the Completion/Darwin subtree; in an ideal
world, a linux install would omit files in that subtree from $fpath.
In practice, everybody seems to install everything everywhere.


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

* Re: _pick_variant
  2020-11-13 22:26       ` _pick_variant Bart Schaefer
@ 2020-11-14  9:56         ` Thomas Lauer
  2020-11-14 19:21           ` _pick_variant Bart Schaefer
  0 siblings, 1 reply; 13+ messages in thread
From: Thomas Lauer @ 2020-11-14  9:56 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Roman Perepelitsa, Thomas Lauer, Zsh Users

On Fri, 13 Nov 2020 14:26:34 -0800 Bart Schaefer
<schaefer@brasslantern.com> wrote:
> On Fri, Nov 13, 2020 at 2:42 AM Roman Perepelitsa
> <roman.perepelitsa@gmail.com> wrote:
> >
> > The clash isn't with an existing command but rather with a completion
> > function that's included in zsh.
> 
> It is conceivably possible to rewrite compinit such that it does not
> install completions for commands not found in $path

This is indeed what I was half expecting, given that zsh completion is
such a powerful and flexible beast.

-- 
Tom


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

* Re: _pick_variant
  2020-11-14  9:56         ` _pick_variant Thomas Lauer
@ 2020-11-14 19:21           ` Bart Schaefer
  0 siblings, 0 replies; 13+ messages in thread
From: Bart Schaefer @ 2020-11-14 19:21 UTC (permalink / raw)
  To: Thomas Lauer; +Cc: Thomas Lauer, Zsh Users

On Sat, Nov 14, 2020 at 1:56 AM Thomas Lauer <thomas.lauer@gmail.com> wrote:
>
> On Fri, 13 Nov 2020 14:26:34 -0800 Bart Schaefer
> <schaefer@brasslantern.com> wrote:
> >
> > It is conceivably possible to rewrite compinit such that it does not
> > install completions for commands not found in $path
>
> This is indeed what I was half expecting, given that zsh completion is
> such a powerful and flexible beast.

The trouble with this is that there are completion routines included
with the distribution that are designed for autoloadable functions
also included with the distribution.  If compinit filters out
completions for commands not found in $path (plus builtins), it will
miss out the completions for those "standard" functions too.

It's probably just as well to let compinit do its usual thing and then
remove the completions you don't want, as discussed at
https://zsh.org/users/26048


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

end of thread, other threads:[~2020-11-14 19:22 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-13 17:41 _pick_variant Thomas Lauer
2020-11-12 18:35 ` _pick_variant Bart Schaefer
2020-11-12 18:40   ` _pick_variant Roman Perepelitsa
2020-11-12 19:07     ` _pick_variant Ray Andrews
2020-11-12 19:17       ` _pick_variant Roman Perepelitsa
2020-11-12 19:20       ` Fwd: _pick_variant JAMES R CUTLER
2020-11-13  9:34   ` _pick_variant Thomas Lauer
2020-11-13 10:42     ` _pick_variant Roman Perepelitsa
2020-11-13 10:57       ` _pick_variant Thomas Lauer
2020-11-13 11:18       ` _pick_variant Roman Perepelitsa
2020-11-13 22:26       ` _pick_variant Bart Schaefer
2020-11-14  9:56         ` _pick_variant Thomas Lauer
2020-11-14 19:21           ` _pick_variant Bart Schaefer

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

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

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


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