zsh-users
 help / color / mirror / code / Atom feed
* Three questions about a completer
@ 2016-09-12 11:38 Jesper Nygårds
  2016-09-12 20:53 ` Oliver Kiddle
  0 siblings, 1 reply; 10+ messages in thread
From: Jesper Nygårds @ 2016-09-12 11:38 UTC (permalink / raw)
  To: Zsh Users

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

I have a completer function that I have bound with menu-complete to alt-e
(or alt-E for reverse-menu-complete). The relevant code is below. I have
simplified what it does to generate the alternatives to not obscure what I
cannot get to work.

setopt BASH_AUTO_LIST

_list-result() {
    setopt LOCAL_OPTIONS EXTENDED_GLOB
    local -a hlist

    if [[ -n $NUMERIC ]]; then hlist=($HOME/*); else hlist=(/etc/*); fi

    [[ -n $words[CURRENT] ]] && hlist=(${(M)hlist:#(#i)*$words[CURRENT]*})

    compadd -V list_result -U -- $hlist
}

zle -C list-comp menu-complete _list-result
bindkey '\ee' list-comp
zstyle ':completion:list-comp:*:*' menu no

zle -C rev-list-comp reverse-menu-complete _list-result
bindkey '\eE' rev-list-comp
zstyle ':completion:rev-list-comp:*:*' menu no

This works reasonably well, but I have three questions:

1) I want to have the alternatives offered by consecutive presses of alt-e,
and I don't want the alternatives to be listed below the command line. To
achieve this, I have had to set the option BASH_AUTO_LIST. If this option
is not set, a list of alternatives is displayed as soon as I hit alt-e (and
at the same time the first alternative is put on the command line, which is
good). But I don't want this option to be set globally. I have not been
able to figure out how to make this menu NOT appear for this particular
completion, but without setting the global option. Is there a way to
achieve this?

2) The line starting with "[[ -n $words[CURRENT] ]] ..." is meant to make
sure that only files that match whats given on the command line (case
insensitively and anywhere within the file name) is offered as
alternatives. I does work, but it also means that reversing the order -
either by starting with alt-e and then hitting alt-E, or the opposite -
does not work. I assume that when I reverse the order, the widget is called
again, and since there is already a file on the command line (from pressing
alt-e once), there is only one file matching. Is there a better way to get
this behaviour? Again, I want to put a string on the command line, hit
alt-e, and only those files having a part matching what i wrote should be
offered. If I then reverse the order, I want the same list, i.e. those
files matching what I originally wrote.

3) I like the idea of using $NUMERIC to trigger alternative behaviour, in
this example getting the file names from another directory. That means I
can hit alt-1 and then alt-e and it gives me the matches from the
alternative directory. However, only alt-1 works. If I hit alt-2, the
NUMERIC variable is of course set to 2, and this makes the menu-completion
skip 2 places in the list every time I repeat alt-e. In other,
non-completion widgets, I have used this: "[[ -n $NUMERIC ]] && NUMERIC=1",
but it seems I cannot do this in a completion widget. I get: "read-only
variable: NUMERIC". Is there a better alternative to just have one
keybinding for a widget, but sometimes triggering an alternative behaviour
with a prefix? (I realise if I restrict myself to only hitting alt-1 it
works, but it's a bit limited).

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

end of thread, other threads:[~2016-09-15  6:28 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-12 11:38 Three questions about a completer Jesper Nygårds
2016-09-12 20:53 ` Oliver Kiddle
2016-09-13 16:16   ` Jesper Nygårds
2016-09-13 19:18     ` Jesper Nygårds
2016-09-14  2:59       ` Bart Schaefer
2016-09-14  7:22         ` Jesper Nygårds
2016-09-14 16:39           ` Bart Schaefer
2016-09-14 20:32             ` Jesper Nygårds
2016-09-15  4:49               ` Bart Schaefer
2016-09-15  6:01               ` Jesper Nygårds

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).