zsh-users
 help / color / mirror / code / Atom feed
* How do I complete from words in a file? (again)
@ 2004-01-11 15:37 Eric Smith
  2004-01-11 17:58 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Smith @ 2004-01-11 15:37 UTC (permalink / raw)
  To: Zsh Users

For example I have keys in the form
"_what_ever_" in certain files and want to type
on the command line:
_wh<tab>

and this should give me all the words commencing 
with `_wh' in the specified files.

This completion could occur anywhere on the command line.

FWIW I am using compsys.

Thanks for any help!

-- 
Eric Smith
zsh devotee


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How do I complete from words in a file? (again)
  2004-01-11 15:37 How do I complete from words in a file? (again) Eric Smith
@ 2004-01-11 17:58 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2004-01-11 17:58 UTC (permalink / raw)
  To: Zsh Users

On Jan 11,  4:37pm, Eric Smith wrote:
}
} For example I have keys in the form
} "_what_ever_" in certain files and want to type
} on the command line:
} _wh<tab>

I'm going to assume that "keys in certain files" means one key per line.
If it's more complicated than that, you'll have to describe file format
in more detail.

A week or so ago, you wrote:

} It would be as an argument to particular commands.

So the first general step is to put a file in a directory named in your
$fpath, that begins with a line similar to:

#compdef particular commands ...

where of course "particular commands ..." are actually the names of the
commands for which you want this to complete.  There are other ways to
use #compdef, documented under "Initialization" in the compsys manual.

The rest of the file is the body of the function that looks up possible
matches.  Note that it doesn't have to look up _actual_ matches, only
_possible_ matches -- that is, it can generate extra strings that do
not match and the completion internals will discard those.

In the case you've described, the function body can be very simple:

local keyfile
for keyfile in certain files
do
    compadd ${(f)"$(<$keyfile)"}
done

Again "certain files" there stands in for the actual file names, and the
${(f)"..."} expression is the standard idiom for reading a file and then
splitting it into one quoted shell word per line.

If you need to use a different key file for each command name, you can
either use multiple function files each with a different #compdef line,
or use a case statement instead of the loop above:

local keyfile command=$words[1]
case $command in
 (particular) keyfile=certain;;
 (commands)   keyfile=files;;
esac
compadd ${(f)"$(<$keyfile)"}

If that's not enough to get you where you want to go, you're going to
have to provide a more specific example of what you'd like to have.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-01-11 17:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-11 15:37 How do I complete from words in a file? (again) Eric Smith
2004-01-11 17:58 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).