Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: implementing a control for completing filenames with a defined list of tokens
- X-seq: zsh-users 18614
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: implementing a control for completing filenames with a defined list of tokens
- Date: Sun, 16 Mar 2014 12:27:27 -0700
- In-reply-to: <mailbox-21676-1394978698-927113@pepper.0.0.200>
- 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
- References: <20131202142614.GA27697@trustfood.org> <131202075840.ZM3182@torch.brasslantern.com> <mailbox-21676-1394978698-927113@pepper.0.0.200>
On Mar 16, 3:13pm, Eric Smith wrote:
}
} 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.
Hmm, that's a somewhat different question, and the answer depends on
whether you want to complete existing directory names (limit the result
to those that match your tokens) or whether you are trying to construct
new names from the input line plus the tokens.
Limiting to existing names is quite a bit easier; instead of defining a
completer for the token-completion generic widget, we just define a
file-patterns style:
zle -C token-completion complete-word _generic
bindkey ^K token-completion
zstyle -e ':completion:token-completion:*' file-patterns \
'reply=( "*(${(j:|:)${(qf)"$(<tokenfile)"}})*(/)" )'
I've used zstyle -e there so that tokenfile is re-read every time the
patterns are needed, so you can update the tokens without having to
reload the completion. Replace tokenfile with a full path unless you
want the tokens to be relative to the current directory.
What happens there is that the pattern is used to create a list of all
possible directories (because of the trailing "(/)" qualifier) that
match your tokens, and then the completion system will automatically
narrow that list to the ones that also match the current input line.
To build up a word for a file that doesn't exist, you need a completer
as in the original scheme, but you'll have to paste the prefix/suffix
from the command line around the tokens yourself. Something like:
_tokens() {
if [[ $PREFIX$SUFFIX = *__* ]]; then
compadd ${PREFIX%_#}${^${(f)"$(<tokenfile)"}}${SUFFIX#_#}
else
return 1
fi
}
where ${PREFIX%_#} means the prefix with any trailing underscores
removed, and similarly ${SUFFIX#_#} removes leading underscores.
And then you need "setopt complete_in_word" to have this in the middle
of the word.
If you want to combine both schemes, e.g., complete words that match a
file name but offer words that don't if there are no such files, you
need something like
zle -C token-completion complete-word _generic
bindkey ^K token-completion
zstyle -e ':completion:token-completion:*' file-patterns \
'reply=( "*(${(j:|:)${(qf)"$(<tokenfile)"}})*(/)" )'
zstyle ':completion:token-completion:*' completer _files _tokens
(with _tokens as above).
Messages sorted by:
Reverse Date,
Date,
Thread,
Author