zsh-users
 help / color / mirror / code / Atom feed
* dynamic file completion
@ 2006-03-17 17:03 Andy Spiegl
  2006-03-17 17:34 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Spiegl @ 2006-03-17 17:03 UTC (permalink / raw)
  To: zsh-users

Hi!

I just wrote my first completion function, yippieh. :-)

It basically works but I'd like to add the feature that the filenames to be
completed are from a specific directory (without the path) and that this
directory can be overriden with the cmdline parameter --dir (or --criddir).
So somehow the function has to dynamically change the list of possible
files, right?  I have no idea how to do that nicely.  Please help!

Here's the function:  (please ignore the German comments)
------------------------------------------------------------------
#compdef gigaset

_arguments \
   '(--help --usage)'{--help,--usage}'[Hilfestellung anzeigen]' \
   '--version[Programmversion anzeigen]' \
   '--manpage[komplette Manpage aufrufen]' \
   '--debug[Debugmodus aktivieren, mehrfach benutzbar]' \
   '(--dir --criddir)'{--dir=,--criddir=}'[Verzeichnis mit den Aufnahmen und crid-Dateien]:criddir:_directories' \
   '--exportdir=[Verzeichnis für den TS-Export der Aufnahmen]:exportdir:_directories' \
   '--cutdir=[Verzeichnis mit den fertig geschnittenen (noch nicht gemuxten) Filmen]:cutdir:_directories' \
   '--mpgdir=[Verzeichnis mit den fertig geschnittenen und gemuxten MPG-Filmen]:mpgdir:_directories' \
   '(--yes --ja)'{--yes,--ja}'[Keine Sicherheitsabfragen, immer ja antworten]' \
   '--nowarnings[Warnungen unterdrücken]' \
   '(--dontdel --nodel)'{--dontdel,--nodel}'[Temporäre Dateien nicht löschen]' \
   '(--force --overwrite)'{--force,--overwrite}'[vorhandene Dateien überschreiben]' \
   '(--nocolor --nc)'{--nocolor,--nc}'[monochrome Ausgabe]' \
   '(--alang --audiolang)'{--alang=,--audiolang=}'[Audio-Sprache einstellen, z.B. -alang deu,fra]:alang:(deu fra en spa it)' \
   '--search=[Beschränkung auf Aufnahmen mit dem Suchstring]:string:' \
   '--commands[Liste aller Kommandos]' \
   ':cmds: compadd ${${(f)"$(gigaset --commands)"}%%[[:space:]]##--*}' \
   '*:cridfiles:_cridfiles'
------------------------------------------------------------------

For the last line to work I had to write another file "_cridfiles":
(How can I include this definition in the first file?)

------------------------------------------------------------------
#compdef gigaset

local expl ext

_description files expl 'crid file for gigaset'
_files "$@" "$expl[@]" -g "*.(#i)crid"
------------------------------------------------------------------


Thanks a lot and have a nice weekend!,
 Andy.

-- 
 Linux - It is now safe to turn on your computer.


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

* Re: dynamic file completion
  2006-03-17 17:03 dynamic file completion Andy Spiegl
@ 2006-03-17 17:34 ` Peter Stephenson
  2006-03-21 16:35   ` Andy Spiegl
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2006-03-17 17:34 UTC (permalink / raw)
  To: zsh-users

Andy Spiegl wrote:
> It basically works but I'd like to add the feature that the filenames to be
> completed are from a specific directory (without the path) and that this
> directory can be overriden with the cmdline parameter --dir (or --criddir).

You need the -W argument to _files; I'd suggest using "_path_files -W
<dir>", since going directly to _path_files removes some smart behaviour
in _files you don't want in this case.

> So somehow the function has to dynamically change the list of possible
> files, right?  I have no idea how to do that nicely.  Please help!

So you need to use your specific directory, but override it if there's a
--dir.  You'll need to check if there's a --dir on the command line
already and grab it's argument.  You need something like:

  local dir=${words[(r)--dir=*]##--dir=}
  if [[ -z $dir ]]; then
    integer dirind=${words[(i)--dir]}
    if (( dirind )); then
      dir=$words[dirind+1]
    fi
  fi
  if [[ -z $dir ]]; then
    dir="default directory"
  fi

>    ':cmds: compadd ${${(f)"$(gigaset --commands)"}%%[[:space:]]##--*}' \
>    '*:cridfiles:_cridfiles'
>
> For the last line to work I had to write another file "_cridfiles":
> (How can I include this definition in the first file?)

The most general way to avoid a separate function is the "->state"
action for _arguments; look in the zshcompsys manual page or various of
the more complicated functions (you need to make some variables local to
use it).  It sets the variable compstate which you can test to see if
it's in the particular state.

In your case, it might be possible to specify the description some
other way, so you could put the _path_files as the action, but I'm not
sure how.

-- 
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


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: dynamic file completion
  2006-03-17 17:34 ` Peter Stephenson
@ 2006-03-21 16:35   ` Andy Spiegl
  0 siblings, 0 replies; 3+ messages in thread
From: Andy Spiegl @ 2006-03-21 16:35 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote:

> You need the -W argument to _files; I'd suggest using "_path_files -W
> <dir>", since going directly to _path_files removes some smart behaviour
> in _files you don't want in this case.
Great, that works!  I don't even need the extra file _cridfiles anymore!

> So you need to use your specific directory, but override it if there's a
> --dir.  You'll need to check if there's a --dir on the command line
> already and grab it's argument.  You need something like:
Wonderful, I always loved zsh!  :-)

But _path_files seems to have a problem with paths with ~ in it.
This works:
 gigaset --dir /home/spiegl/movies/gigaset/PVR ls <TAB>
but this doesn't:
 gigaset --dir ~/movies/gigaset/PVR ls <TAB>
Can that be done too?

Ah, neither does this work:
 gigaset --dir $HOME/movies/gigaset/PVR ls <TAB>

Thanks,
 Andy.


Okay, here's what I wrote so far:
-------------------------------------------------
#compdef gigaset

local dir=${words[(r)--dir=*]##--dir=}
if [[ -z $dir ]]; then
  integer dirind=${words[(i)--dir]}
  if (( dirind )); then
    dir=$words[dirind+1]
  fi
fi
if [[ -z $dir ]]; then
  dir="/data/movies/gigaset/PVR/"
fi

_arguments \
   '(--help --usage)'{--help,--usage}'[Hilfestellung anzeigen]' \
   '--version[Programmversion anzeigen]' \
   '--manpage[komplette Manpage aufrufen]' \
   '--debug[Debugmodus aktivieren, mehrfach benutzbar]' \
   '(--dir --criddir)'{--dir=,--criddir=}'[Verzeichnis mit den Aufnahmen und crid-Dateien]:criddir:_path_files -/' \
   '--exportdir=[Verzeichnis für den TS-Export der Aufnahmen]:exportdir:_path_files -/' \
   '--cutdir=[Verzeichnis mit den fertig geschnittenen (noch nicht gemuxten) Filmen]:cutdir:_path_files -/' \
   '--mpgdir=[Verzeichnis mit den fertig geschnittenen und gemuxten MPG-Filmen]:mpgdir:_path_files -/' \
   '(--yes --ja)'{--yes,--ja}'[Keine Sicherheitsabfragen, immer ja antworten]' \
   '--nowarnings[Warnungen unterdrücken]' \
   '(--dontdel --nodel)'{--dontdel,--nodel}'[Temporäre Dateien nicht löschen]' \
   '(--force --overwrite)'{--force,--overwrite}'[vorhandene Dateien überschreiben]' \
   '(--nocolor --nc)'{--nocolor,--nc}'[monochrome Ausgabe]' \
   '(--alang --audiolang)'{--alang=,--audiolang=}'[Audio-Sprache einstellen, z.B. -alang deu,fra]:alang:(deu fra en spa it)' \
   '--search=[Beschränkung auf Aufnahmen mit dem Suchstring]:string:' \
   '--commands[Liste aller Kommandos]' \
   ':cmds: compadd ${${(f)"$(gigaset --commands)"}%%[[:space:]]##--*}' \
   '*:cridfiles:_path_files -W $dir -g "*.crid"'

-------------------------------------------------

-- 
 Your mouse has moved. Windows must be restarted for the change
 to take effect. Reboot now?


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

end of thread, other threads:[~2006-03-21 16:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-17 17:03 dynamic file completion Andy Spiegl
2006-03-17 17:34 ` Peter Stephenson
2006-03-21 16:35   ` Andy Spiegl

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).