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

Re: How to strip off common prefix for array elements?



On Feb 19,  2:10pm, Andrej Borsenkow wrote:
} Subject: How to strip off common prefix for array elements?
}
} Consider the case:
} 
} cdpath=(~ ~/src ~/build/mips)
} cd zshTAB -> gives me zsh-3.1.2 which is O.K., but I don't have any chance
} to cd into build, because ~/src stays in the way.

So what you're trying to do is use completion to disambiguate when two or
more cdpath directories have same-named subdirectories?

Try this variant:

# Start of cdmatch.
# Save in your functions directory and autoload, then do
# compctl -x 'S[/][~][./][../]' -g '*(-/)' - \
#         'n[-1,/], s[]' -K cdmatch -U -- cd pushd
#
# Completes directories for cd, pushd, ... anything which knows about cdpath.
# You do not have to include `.' in your cdpath.
#
# It works properly only if $ZSH_VERSION > 3.0-pre4.  Remove `emulate -R zsh'
# for all other values of $ZSH_VERSION > 2.6-beta2. For earlier versions
# it still works if RC_EXPAND_PARAM is not set or when cdpath is empty.
emulate -R zsh
setopt localoptions
local narg pref cdp

read -nc narg
read -Ac pref

cdp=(. $cdpath)
reply=( ${^cdp}/${pref[$narg]}*$2(-/DN^M) )

return
# End of cdmatch.

It could use some work, but the basic idea is that you don't remove any
prefixes, and use -U to replace the entire word with the full completion.
As is, the above is really only usable with menucomplete set, but it's a
start in the right direction.


-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



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