zsh-users
 help / color / mirror / code / Atom feed
From: Eric Smith <Eric.Smith@biovolt.nl>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: implementing a control for completing filenames with a defined list of tokens
Date: Sun, 16 Mar 2014 15:13:17 +0100	[thread overview]
Message-ID: <mailbox-21676-1394978698-927113@pepper.0.0.200> (raw)
In-Reply-To: <131202075840.ZM3182@torch.brasslantern.com>

Thank you Bart.

I want to use this completion facility at the start of file (a
dir) names and also *in the middle* of the name. 
So the tags may appear anywhere in the filename.
So I could type foobar__^K
for example and the function will complete with my tokens which all begin with a double underscore.

$ cat tokenfile 
__job__1403-whatever__
__article__
__finance__
__contract__
__published__
__project-foo__ 
__job__1402-visit__

How may I adapt your suggestions for this?

Thanks a lot.

Eric

Bart Schaefer wrote on Mon-02-Dec 13  4:58PM
> On Dec 2,  3:26pm, Eric Smith wrote:
> } Subject: implementing a control for completing filenames with a defined li
> }
> } I would like to define a completion widget to complete filenames with
> } a defined list of tokens. The widget would look up in a list the
> } tokens and suggest these (ideally in order) to the user.
> 
> This one is about as straightforward as it's possible to get.
> 
> First you need a function that reads tokenfile and passes the lines
> therein as arguments to the "compadd" builtin.
> 
>     _tokens() { compadd ${(f)"$(<tokenfile)"} }
> 
> Next you need a completion widget.  There's a ready-made function for
> creating completion widgets, called _generic.
> 
>     zle -C token-completion complete-word _generic
>     bindkey ^K token-completion
> 
> (^K is normally kill-line so you might want to pick another binding.)
> 
> Finally tell the completion system that when the token-completer is
> invoked, it should use the _tokens function to supply the matches:
> 
>     zstyle ':completion:token-completion:*' completer _tokens
> 
> And you're done.  If you eventually want it to complete other things, you
> can append more functions to the zstyle.
> 
> Another way to do this is to create a file in your $fpath having a name
> starting with underscore, and begin that file with a "#compdef -k ..."
> line.  That could be as simple as this:
> 
> ---- 8< ---- snip ---- 8< ----
> #compdef -k complete-word ^K
> 
> _tokens() { compadd ${(f)"$(<tokenfile)"} }
> _generic _tokens
> ---- 8< ---- snip ---- 8< ----
> 
> However, that will redefine the _tokens function on each completion.  A
> slightly better formulation would be to have the file redefine the same
> function as its file name.  If the file is named "_token_completion":
> 
> ---- 8< ---- snip ---- 8< ----
> #compdef -k complete-word ^K
> 
> _tokens() { compadd ${(f)"$(<tokenfile)"} }
> _token_completion() { _generic _tokens }
> _token_completion
> ---- 8< ---- snip ---- 8< ----
> 
> It's probably not necessary to go this far when _tokens is so simple,
> but if you eventually write something more involved it's a good pattern.
> 
> Note that with the file you don't need the zstyle: passing _tokens as an
> argument to _generic has the equivalent effect.  You could omit _tokens
> there and instead have the style, just replace "token-completion" in the
> style pattern with the name of the file.

-- 
Eric Smith


  reply	other threads:[~2014-03-16 14:23 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-02 14:26 Eric Smith
2013-12-02 15:58 ` Bart Schaefer
2014-03-16 14:13   ` Eric Smith [this message]
2014-03-16 19:27     ` Bart Schaefer
2014-03-16 20:13       ` Bart Schaefer
2014-03-18  3:10         ` set -F kills read -t Ray Andrews
2014-03-18  6:50           ` Bart Schaefer
2014-03-18 16:22             ` Ray Andrews
2014-03-18 16:47               ` Peter Stephenson
2014-03-18 17:45               ` Bart Schaefer
2014-03-18 22:08                 ` Ray Andrews
2014-03-18 23:12                   ` Jan Larres
2014-03-19  4:06                     ` Ray Andrews
2014-03-19  5:30                       ` Jan Larres
2014-03-19 15:23                         ` Ray Andrews
2014-03-19 20:00                           ` Bart Schaefer
2014-03-20  1:47                             ` Ray Andrews
2014-03-19  1:17                   ` Bart Schaefer
2014-03-19  5:00                     ` Ray Andrews
2014-03-19  6:37                       ` Bart Schaefer
2014-03-19 17:08                         ` Ray Andrews
2014-03-19 17:22                           ` Roman Neuhauser
2014-03-19 22:21                           ` Bart Schaefer
2014-03-20  1:46                             ` Ray Andrews
2014-03-20  4:21                               ` Bart Schaefer
2014-03-20 15:49                                 ` Ray Andrews
2014-03-20 16:08                                   ` Bart Schaefer
2014-03-20 21:27                                     ` Ray Andrews
2014-03-19 10:00                       ` Roman Neuhauser

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=mailbox-21676-1394978698-927113@pepper.0.0.200 \
    --to=eric.smith@biovolt.nl \
    --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).