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

Re: Quoting with compadd



Simon Haines <simon.haines@xxxxxxxxxxxxxxxxxx>:
> This awful attempt is the best I could come up with so far (assuming
> 'compdef j_complete j')
> j_complete() {
> 	local -a dirs
> 	dirs=$(grep -o -e ${words[CURRENT]}'[^|]*' < ~/.j)
> 	compadd $dirs
> }

No need for taking the current word into account yourself.

[...]
> The completion needs to scan the file ~/.j for directories containing
> the completion word. The file has the format <dir>|<count>|<time>
> (notice non-greedy matching in egrep because of the second '|').
> Ideally completion should only be offered where the completion word
> matches the beginning of a path or part of a path string that follows
> '/'.

I don't quite know if I fully understand what you're after, but
consider this:

~/.j:
[snip]
/foo/bar|34|1234
/foo/baz|54|1434
/fop/bag|64|3234
[snap]

j_complete() {
  local -a dirs
  dirs=( ${${$(<~/.j)}%%\|*} )
  compadd $dirs
}

% compdef j_complete j
% j <tab>
results in 'j /fo'

% j /foo<tab>
results in 'j /foo/ba'

% j /home<tab>
results in 'j /home'

By common convention, completion functions are named starting in an
underscore. So you'd name your completion _j_complete().

If you do so, you could also put it into a file names _j_complete in
your $fpath, like this:

[snip]
#compdef j
local -a dirs
dirs=( ${${$(<~/.j)}%%\|*} )
compadd $dirs
[snap]

If that's in a directory in your $fpath before calling compinit,
compinit will take care of calling compdef for you. You could also
distribute the completion via your github repository.

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925



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