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

Re: context for globbing qualifiers based on command



On 29 February 2012 19:22, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> (It wasn't necessary to send this inquiry twice.  It sometimes takes a
> little while for a response.)
>
> On Feb 27, 11:20am, Martin Richter wrote:
>>
>> Is it possible to tweak the completion such that shell functions are
>> grouped at the top when pressing TAB when using (:e...) on a given command?
>>   print -l *(e:TAB --> gives a lot of possible functions etc
>> but
>>   special_command *(e:TAB
>> gives only shell function matching some regexp or gives only the single
>> function 'foo' or members of an array of functions?
>
> Unfortunately, no.  The zstyle context after *(e: is identical to the
> context in any other command position.
>
> However, the $words array hasn't been altered, so you could write a
> function to be installed in your "completer" style that examines the
> values of $words[1] and $words[CURRENT], something like this:
>
> _special_command_glob_completer() {
>  if [[ CURRENT -gt 1 &&
>        "$words[1]" = 'special_command' &&
>        "$words[CURRENT]" = *\(*e:* ]]
>  then
>    compset -p ${#words[CURRENT]}
>    compadd -J special-globbers sum_equals product_equals
>    return 0
>  else
>    return 1
>  fi
> }
>
> zstyle ':completion:*' completer _expand \
>        _special_command_glob_completer \
>        _complete _match _ignored _approximate _prefix
>
> (the rest of that completer style is just for example, but you probably
> want at least _complete in there after your special one).
>
> It might also be possible to do this more generically with a "matcher"
> zstyle in the context :completion::complete:-command-::* but I'll leave
> that for someone else to work out.  (It'd have to use "zstyle -e" and
> some kind of test similar to the "if" above.)

You can probably do this a bit less fragilely with $zsh_eval_context:

% print -l $(echo .(e:'reply=($zsh_eval_context)':))
cmdsubst
globqual
toplevel


-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author