* help for a function and its completion
@ 2009-03-19 21:58 ugaciaka
2009-03-19 23:48 ` Peter Stephenson
0 siblings, 1 reply; 4+ messages in thread
From: ugaciaka @ 2009-03-19 21:58 UTC (permalink / raw)
To: zsh-users
Hi,
I writed this functions
### functions
#
##################
#extract files eg: ex tarball.tar#
##################
ex () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar vxjf $1 ;;
*.tar.gz) tar vxzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar vxf $1 ;;
*.tbz2) tar vxjf $1 ;;
*.tgz) tar vxzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "$1 non può essere estratto con ex()" ;;
esac
else
echo "$1 non è un archivio valido"
fi
}
##################
#compress directory eg: cm tarball.tar tarball#
##################
cm () {
if [ -d $2 ] ; then
case $1 in
*.tar.bz2) tar vcjf $1 $2 ;;
*.tar.gz) tar vczf $1 $2 ;;
*.bz2) bunzip2 $1 $2 ;;
# *.rar) unrar -e $1 $2 ;;
*.gz) gzip $1 $2 ;;
*.tar) tar vcf $1 $2 ;;
*.tbz2) tar vcjf $1 $2 ;;
*.tgz) tar vczf $1 $2 ;;
*.zip) zip -r $1 $2 ;;
*.Z) compress $1 $2 ;;
*.7z) 7z a -t7z $1 $2 ;;
*) echo "$2 non può essere compresso con cm()" ;;
esac
else
echo "$2 non è un archivio valido"
fi
}
I tried to understand how to write zstyle completion to make your life
easier but are not very good.
I only wish that when I enter, eg,
cm directory directory.tar
postfix tar appear on a list where there are tar, tar.gz, etc.
Maybe I ask too much but a little help, I would make great strides
Thanks
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: help for a function and its completion
2009-03-19 21:58 help for a function and its completion ugaciaka
@ 2009-03-19 23:48 ` Peter Stephenson
2009-03-20 8:34 ` ugaciaka
0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2009-03-19 23:48 UTC (permalink / raw)
To: ugaciaka; +Cc: zsh-users
On Thu, 19 Mar 2009 22:58:39 +0100
ugaciaka <ugaciaka@gmail.com> wrote:
> ##################
> #extract files eg: ex tarball.tar#
> ##################
> ex () {
>...
> }
>
> ##################
> #compress directory eg: cm tarball.tar tarball#
> ##################
> cm () {
> ...
> }
(I don't think your "compress" and "gzip" items will work quite the way
you've written them. Also, "ex" is the name of an editor, although you
probably wouldn't want to run it directly, so that may not matter.
However, that's not what you're asking about.)
> I tried to understand how to write zstyle completion to make your life
> easier but are not very good.
>
> I only wish that when I enter, eg,
>
> cm directory directory.tar
>
> postfix tar appear on a list where there are tar, tar.gz, etc.
You can use zstyle to tell the system that you only want certain files
completed after a command. However, since you want something a little
bit more than that, to complete an archive as the first argument and a
directory as the second, it's best to use a new function. You can call
the following "_cm" and put it in your function path (make sure the
#compdef is in the first line of the file):
#compdef cm ex
local expl
if [[ CURRENT -eq 3 && $service = cm ]]; then
# Complete directory
_wanted directories expl directory _path_files -/
else
# Complete archive
_wanted archives expl archive \
_files -g '*.(tar.gz|bz2|rar|gz|tar|tbz2|tgz|zip|Z|7z)'
fi
Here's a summary of what's going on.
- The "#compdef cm ex" means that when you run "compinit" and it
searches all your functions beginning with "_", it will find that this
function does completion for the commands "cm" and "ex".
- The local variable "expl" is used by the completion system for
temporarily saving explanations to display. You'll see it's the
second argument after "_wanted" later on.
- If the current argument is 3 (where the command counts a s 1), and the
command is "cm" ($service stores the name of the command), then complete
directories.
- Else complete a pattern giving one of the forms of compressed file or
archive you want.
- The "_wanted <tag> expl <description>" is there to help the
completion system know how to deal with the completion you're giving
it. It adds a description to the completion, as well as a tag that
can be used if you want to make a zstyle for this completion.
- The important bit of the completions are these: for directories,
_path_files -/
This says complete files, with an option saying that the files must
be directories.
- For the archives or compressed files,
_files -g '*.(tar.gz|bz2|rar|gz|tar|tbz2|tgz|zip|Z|7z)'
says complete files, and gives as an option the pattern that the files
must match.
- The difference betewen _path_files and _files is that _files will
also complete directories. So if your archive is in another
directory, you can still use completion to find it. If you don't want
that, you can change it to
_path_files -g '*.(tar.gz|bz2|rar|gz|tar|tbz2|tgz|zip|Z|7z)'
and it will still complete the archives, but not directories.
--
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: help for a function and its completion
2009-03-19 23:48 ` Peter Stephenson
@ 2009-03-20 8:34 ` ugaciaka
2009-03-20 9:51 ` Peter Stephenson
0 siblings, 1 reply; 4+ messages in thread
From: ugaciaka @ 2009-03-20 8:34 UTC (permalink / raw)
To: zsh-users
ok thank's,
But I have a problem, do not use a file containing the functions. I
copied everything. zshrc renaming cm in _cm but when source .zshrc
expl=''
_tags:comptags:36: can only be called from completion function
_tags:comptry:55: can only be called from completion function
_tags:comptags:60: can only be called from completion function
_tags:comptags:67: can only be called from completion function
how can I solve? thanks
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: help for a function and its completion
2009-03-20 8:34 ` ugaciaka
@ 2009-03-20 9:51 ` Peter Stephenson
0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2009-03-20 9:51 UTC (permalink / raw)
To: ugaciaka, zsh-users
ugaciaka wrote:
> ok thank's,
> But I have a problem, do not use a file containing the functions. I
> copied everything. zshrc renaming cm in _cm but when source .zshrc
>
> expl=''
> _tags:comptags:36: can only be called from completion function
> _tags:comptry:55: can only be called from completion function
> _tags:comptags:60: can only be called from completion function
> _tags:comptags:67: can only be called from completion function
>
> how can I solve? thanks
The function I sent needs to be inside a special file in your function
path, given by the $fpath variable. If you don't have such a directory,
create one, for example "mkdir ~/zsh-function". Then in your .zshrc
(at some point before "compinit" is run) put
fpath=(~/zsh-functions $fpath)
Then put the contents of the function in a file _cm in that directory.
You should be able to do:
% cat ~/zsh-functions/_cm
#compdef cm ex
... rest of file ...
Then the next time you start the shell completion for "cm" and "ex" will
work.
--
Peter Stephenson <pws@csr.com> Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2009-03-20 9:52 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-19 21:58 help for a function and its completion ugaciaka
2009-03-19 23:48 ` Peter Stephenson
2009-03-20 8:34 ` ugaciaka
2009-03-20 9:51 ` Peter Stephenson
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).