zsh-users
 help / color / mirror / code / Atom feed
From: "Jesper Nygårds" <jesper.nygards@gmail.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Three questions about a completer
Date: Mon, 12 Sep 2016 13:38:50 +0200	[thread overview]
Message-ID: <CABZhJg-VR4ss0KpxLvUnrUS7JnTVGGdSWePH7NzCzQvdimMubg@mail.gmail.com> (raw)

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

             reply	other threads:[~2016-09-12 11:39 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-12 11:38 Jesper Nygårds [this message]
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

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=CABZhJg-VR4ss0KpxLvUnrUS7JnTVGGdSWePH7NzCzQvdimMubg@mail.gmail.com \
    --to=jesper.nygards@gmail.com \
    --cc=zsh-users@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).