I'm trying to create an autoloaded completion file for zsh. To do so, I've added a _myprog
file in a folder that is in my fpath.
This file goes roughly like this:
#compdef myprog
_myprog () {
local cmd
if (( CURRENT > 2)); then
cmd=${words[2]}
curcontext="${curcontext%:*:*}:myprog-$cmd"
(( CURRENT-- ))
shift words
case "${cmd}" in
list|ls)
_arguments : "--limit[Max depth]" "--folders[Print flat list of folders]"
_describe -t commands "myprog list" subcommands
;;
insert)
_arguments : "--force[Overwrite file]" "--append[Append to existing data]"
_describe -t commands "myprog insert" subcommands
_myprog_complete_folders
;;
esac
else
local -a subcommands
subcommands=(
"insert:Insert a new file"
"list:List existing files"
)
_describe -t command 'myprog' subcommands
_arguments : "--yes[Assume yes]"
_myprog_complete_files
fi
}
_myprog_complete_files () {
_values 'files' $(myprog ls)
}
_myprog_complete_folders () {
_values 'folders' $(myprog ls --folders)
}
_myprog
So, my program has two scenarios where I want auto-completion:
When used without subcommand, I'd like to specify a path in a remote location. This is done by the function
_myprog_complete_files
thanks to the_value
function. The file list is provided by runningmyprog ls
, which prints each remote file on a new line.When inserting a new remote file, I'd like to be able to autocomplete the folders name, which is done in the function
_myprog_complete_folders
using_value
too, but this time the folder list is generated usingmyprog ls --folder
instead, which only prints folders.
So far so good... Excepted that zsh is inserting a space after the folder name when I'm using <tab><tab>
to have the value list displayed and I'm trying to type the file name after the folder name.
For example:
$ myprog insert web<tab><tab><tab>
-- folders --
web/ web/foo/ web/bar/
is supposed to select web/foo
and it effectively does so and autocomplete to myprog insert web/foo/
but notice the trailing space! So if I'm trying to type directly the name of the file I want to insert once I've selected the folder I wanted using <tab>
, say I want to call it baz
, I'm ending up with myprog insert web/foo/ baz
which is not my goal at all.
I've tried browsing through the zsh doc, without success.
I've for example tried to set zstyle ':completion::complete:myprog-insert:' add-space false
to no avail, both in my .zshrc and in my _myprog
file. I couldn't even see a difference when having it set or not.
I'm guessing I must be missing something, since it seems to be a desirable feature to be able to type directly after autocompletion in order to specify a parameter or so.