zsh-workers
 help / color / mirror / code / Atom feed
From: Oliver Kiddle <opk@zsh.org>
To: Marlon Richert <marlon.richert@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: [FEATURE][PATCH] Complete local executables with ./ prefix, if prefix-needed is false
Date: Thu, 09 Dec 2021 00:16:54 +0100	[thread overview]
Message-ID: <64846-1639005414.316056@22FL.WcIa.VGfN> (raw)
In-Reply-To: <CAHLkEDsOfypLT9BSb4fRHTGXpz3btuFPZAbJos8XahsP4-uajg@mail.gmail.com>

Marlon Richert wrote:
> This allows local executables to be completed more easily, without the
> need for . in $path.

This is the type of thing which often can already be achieved with
styles and, where not, a few tweaks may enable it. The nearest example
from my own config involves using the fake style and a matcher to ignore
the prefix. But that's for a more specific context[1].

In this case for the style context, the tag 'commands' is used for a lot
of commands that aren't external Unix commands. The 'executables' tag is
better but guarded by a condition that will evaluate to false:

[[ -n "$path[(r).]" || $PREFIX = */* ]] &&
    defs+=( 'executables:executable file:_files -g \*\(-\*\)' 

The fake style is looked up in _description so a dummy call to it is one
option. The patch below adds that but I'd be interested in any thoughts
on that. With caveats[2], that allows:

  zstyle -e ':completion:*:executables' fake 'reply=( ./*(-*) )'
  zstyle ':completion:*:executables' matcher 'b:=./'

There is a command-path style looked up by _command_names to override
$path. But that doesn't entirely help for a couple of reasons. External
commands are completed based on keys of the commands association but
that will not contain commands based on relative directories listed in
$path. Even if it did, they can't be identified to add back a ./ prefix
so that they actually work. In the patch below, I do change it to use
path=( $cmdpath:A ) when the command-path style is set. That resolves
only the first of these issues and allows for relative paths in the
command-path style to work as intended, even if that isn't especially
useful.

Oliver

[1] If anyone's interested, that example is for ssh public keys. Just
the fake and matcher style are needed but it looks like this:
zstyle -e ':completion:*:((ssh|scp|sftp):*:option-i-1|ssh-add:*:argument-rest)' fake '[[ -prefix - ]] || reply=( ~/.ssh/id^*.pub(-.) )'
zstyle -e ':completion:*:((ssh|scp|sftp):*:option-i-1|ssh-add:*:argument-rest)' matcher '[[ -prefix - ]] || reply=( "l:|=*" )'
zstyle ':completion:*:((ssh|scp|sftp):*:option-i-1|ssh-add:*:argument-rest)' menu yes=0 search
zstyle ':completion:*:((ssh|scp|sftp):*:option-i-1|ssh-add:*:argument-rest)' list-colors "=(#b)(*/)(*)==38;5;208=0"

[2] With the fake style in general, the return status of the calling
completion function doesn't account for fake matches which may mean
unwanted later completers are invoked (e.g. _approximate).

diff --git a/Completion/Zsh/Type/_command_names b/Completion/Zsh/Type/_command_names
index b1c35f013..12cbd69c1 100644
--- a/Completion/Zsh/Type/_command_names
+++ b/Completion/Zsh/Type/_command_names
@@ -4,7 +4,7 @@
 # complete only external commands and executable files. This and a
 # `-' as the first argument is then removed from the arguments.
 
-local args defs ffilt
+local args defs expl ffilt
 
 zstyle -t ":completion:${curcontext}:commands" rehash && rehash
 
@@ -16,8 +16,12 @@ defs=(
   'commands:external command:_path_commands'
 )
 
-[[ -n "$path[(r).]" || $PREFIX = */* ]] &&
-    defs+=( 'executables:executable file:_files -g \*\(-\*\)' )
+if [[ -n "$path[(r).]" || $PREFIX = */* ]]; then
+  defs+=( 'executables:executable file:_files -g \*\(-\*\)' )
+else
+  # this is ignored but exists to facilitate the use of the fake style
+  _description executables expl 'executable file'
+fi
 
 if [[ "$1" = -e ]]; then
   shift
@@ -58,7 +62,7 @@ fi
 if (( $#cmdpath )); then
   local -a +h path
   local -A +h commands
-  path=( $cmdpath )
+  path=( $cmdpath:A )
 fi
 
 _alternative -O args "$defs[@]"


  parent reply	other threads:[~2021-12-08 23:17 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-07 19:15 Marlon Richert
2021-12-08  0:22 ` Aaron Schrab
2021-12-08  0:50   ` Bart Schaefer
2021-12-08  1:16     ` Aaron Schrab
2021-12-08 16:25       ` Daniel Shahaf
2021-12-08 17:07         ` Bart Schaefer
2021-12-08 19:13           ` Daniel Shahaf
2021-12-08 23:16 ` Oliver Kiddle [this message]
2021-12-08 23:42   ` Bart Schaefer
2021-12-09 21:19     ` Oliver Kiddle

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=64846-1639005414.316056@22FL.WcIa.VGfN \
    --to=opk@zsh.org \
    --cc=marlon.richert@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).