Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Three questions about a completer



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


Messages sorted by: Reverse Date, Date, Thread, Author