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

# Three questions about a completer

• X-seq: zsh-users 21871
• From: Jesper Nygårds <jesper.nygards@xxxxxxxxx>
• To: Zsh Users <zsh-users@xxxxxxx>
• Subject: Three questions about a completer
• Date: Mon, 12 Sep 2016 13:38:50 +0200
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=6ZykZWsTjVL+7Gp9cZnpNBTnBbjuxAnJbJuw1il+aB8=; b=KRXlgQqx+5iBHxgp8PcK00bv6KvLs2+FiXXQwfg28hww5HkhygxBXqSWB1HSJG+J4I TWsxDOAeGzqcxpbYtU/fh8bBZplqIH7gok01JqgyCSO2z72U1kufLRnF52Mh0DaoIbvu sdz/c+nEFHIZt9mMZcObUnfDGz19Tz63f5//x1I8jwY55GJQSNdbAaTxkrZumN4jQKGC WV6kEmYzIldbUQuorpiiuC6axcbSTtDtIoGDfUhap4F3fWg/Z5T3kAFYo5XWgezFW2ET WSzbKmtN7m9ou3aIRIUclZeVf5ZFWZdYkuO509w8i3IE3nPhVw7PM943k/sOqmyc22qk udrg==
• List-help: <mailto:zsh-users-help@zsh.org>
• List-id: Zsh Users List <zsh-users.zsh.org>
• List-post: <mailto:zsh-users@zsh.org>
• Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm

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
}

bindkey '\ee' list-comp

bindkey '\eE' rev-list-comp

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