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

Re: PATCH: _a2ps completion



Peter Stephenson wrote:

> ...
> 
> Preferably it should allow you to specify the tag you want altered,
> i.e. you may want to alter just globbed-files, leaving all-files doing its
> thing, or for special effects you might want all-files not to live up to
> its name (e.g., I want to get rm to complete discardable files like *.o by
> default (globbed-files), otherwise anything else (all-files), but I've
> decide I never want it to complete vmlinuz). In which case, I suppose
> `file-pattern' is the better name, since it can apply to all *files tags.
> You could argue it should be `selected-files' to go with `ignored-files',
> but I don't think they're really similar enough for that.

This adds the file-patterns style. It can be used to override
glob-patterns used by completion functions and -- if the function uses 
_files -- to make it use the globbed-files and directories tags even
if the calling function doesn't say so.

I can only hope that the manual is clear enough.

> I suppose I'm going to have to include Clint's patch in 9333.

Yep.

Bye
 Sven

diff -ru ../z.old/Completion/Core/_files Completion/Core/_files
--- ../z.old/Completion/Core/_files	Wed Jan 19 10:07:55 2000
+++ Completion/Core/_files	Wed Jan 19 16:53:13 2000
@@ -1,16 +1,19 @@
 #autoload
 
-local opts opt type=file glob group
+local opts opt type=file glob group gopts dopts aopts tmp _file_pat_checked=yes
 
 opts=()
 group=()
+gopts=()
+dopts=(-/)
+aopts=(-f)
 while getopts "P:S:qr:R:W:F:J:V:X:f/g:M:12n" opt; do
   case "$opt" in
-  /)      type="${type}dir"                   ;;
-  g)      type="${type}glob"; glob="$OPTARG"  ;;
-  [qn12]) opts=("$opts[@]" "-$opt"          ) ;;
-  [JV])   group=(          "-$opt" "$OPTARG") ;;
-  [^f])   opts=("$opts[@]" "-$opt" "$OPTARG") ;;
+  /)      type="${type}dir"                        ;;
+  g)      type="${type}glob"; gopts=(-g "$OPTARG") ;;
+  [qn12]) opts=("$opts[@]" "-$opt"          )      ;;
+  [JV])   group=(          "-$opt" "$OPTARG")      ;;
+  [^f])   opts=("$opts[@]" "-$opt" "$OPTARG")      ;;
   esac
 done
 
@@ -19,11 +22,37 @@
   group=()
 fi
 
+if zstyle -s ":completion${curcontext}:all-files" file-patterns tmp &&
+   [[ -n "$tmp" ]]; then
+  aopts=(-g "$tmp")
+fi
+if zstyle -s ":completion${curcontext}:directories" file-patterns tmp &&
+   [[ -n "$tmp" ]]; then
+  dopts=(-g "$tmp")
+  if [[ "$type" = (*dir*glob*|*glob*dir*) ]]; then
+    type=glob
+  elif [[ "$type" != *(dir|glob)* ]]; then
+    type="${type}dir"
+  fi
+fi
+if zstyle -s ":completion${curcontext}:globbed-files" file-patterns tmp &&
+   [[ -n "$tmp" ]]; then
+  gopts=(-g "$tmp")
+  if [[ "$type" != (*dir*glob*|*glob*dir*) ]]; then
+    if [[ "$type" = *(dir|glob)* ]]; then
+      type=glob
+    else
+      type=globall
+    fi
+  fi
+fi
+
 case "$type" in
-*dir*glob*|*glob*dir) _tags globbed-files all-files             ;;
-*glob*)               _tags globbed-files directories all-files ;;
-*dir*)                _tags directories all-files               ;;
-*)                    _tags all-files                           ;;
+*dir*glob*|*glob*dir*) _tags globbed-files all-files             ;;
+*all*glob*|*glob*all*) _tags globbed-files all-files             ;;
+*glob*)                _tags globbed-files directories all-files ;;
+*dir*)                 _tags directories all-files               ;;
+*)                     _tags all-files                           ;;
 esac
 
 while _tags; do
@@ -32,7 +61,7 @@
       group[2]=all-files
       _setup all-files
     fi
-    _path_files "$opts[@]" -f
+    _path_files "$opts[@]" "$aopts[@]"
     return
   elif _requested directories; then
     if _requested globbed-files; then
@@ -40,13 +69,13 @@
         group[2]=globbed-files
 	_setup globbed-files
       fi
-      _path_files "$opts[@]" -/g "$glob" && return 0
+      _path_files "$opts[@]" "$dopts[@]" "$gopts[@]" && return 0
     else
       if (( $#group )); then
         group[2]=directories
 	_setup directories
       fi
-      _path_files "$opts[@]" -/ && return 0
+      _path_files "$opts[@]" "$dopts[@]" && return 0
     fi
   elif _requested globbed-files; then
     if (( $#group )); then
@@ -54,9 +83,9 @@
       _setup globbed-files
     fi
     if [[ "$type" = (*dir*glob*|*glob*dir*) ]]; then
-      _path_files "$opts[@]" -/g "$glob" && return 0
+      _path_files "$opts[@]" "$dopts[@]" "$gopts[@]" && return 0
     else
-      _path_files "$opts[@]" -g "$glob" && return 0
+      _path_files "$opts[@]" "$gopts[@]" && return 0
     fi
   fi
 done
diff -ru ../z.old/Completion/Core/_path_files Completion/Core/_path_files
--- ../z.old/Completion/Core/_path_files	Wed Jan 19 10:07:56 2000
+++ Completion/Core/_path_files	Wed Jan 19 10:35:04 2000
@@ -81,6 +81,20 @@
   esac
 done
 
+if [[ -z "$_file_pat_checked" ]] &&
+   zstyle -s ":completion${curcontext}:files" file-patterns tmp1 &&
+   [[ -n "$tmp1" ]]; then
+  if [[ "$tmp1" = '*(-/)' ]]; then
+    gopt=''
+    sopt=-/
+  else
+    gopt='-g'
+    sopt=-
+  fi
+  pats=( $=tmp1 )
+  haspats=yes
+fi
+
 if (( ! ( $#group + $#expl ) )); then
   if [[ -z "$gopt" && "$sopt" = -/ ]]; then
     _description directories expl directory
diff -ru ../z.old/Doc/Zsh/compsys.yo Doc/Zsh/compsys.yo
--- ../z.old/Doc/Zsh/compsys.yo	Wed Jan 19 10:07:36 2000
+++ Doc/Zsh/compsys.yo	Wed Jan 19 17:13:02 2000
@@ -720,6 +720,39 @@
 this will also cycle through the names of the files in pathname
 components after the first ambiguous one.
 )
+item(tt(file-patterns))(
+The completion system uses two functions to complete filenames,
+tt(_files) and tt(_path_files), with one of them calling the other,
+but this second one is sometimes also called directly. Depending on
+how it is called, the first one uses the tags tt(globbed-files),
+tt(directories) and tt(all-files). The second one, when called
+directly, uses the tag tt(files).
+
+Using this style one can specify which filenames should be completed
+in certain contexts. It is tested by tt(_files) and, if called
+directly, tt(_path_files) with the tags mentioned above. If it is set
+for these tags, the value is taken as a list of glob-patterns that
+should be used to select filenames when completing for the tag. Note
+that with tt(_files), calling completion function may specify that
+all files are to be completed. Normally this would make tt(_files) use 
+only the tt(all-files) tag, but if this style is set for any of the
+other two tags (tt(globbed-files) and tt(directories)), these tags
+will be used, too, in the normal order given above (unless the user
+specified another order to be used with the tt(tag-order) style).
+
+For example, to make the completion system first try only filenames
+matching the pattern tt(*.o) for the tt(rm) command, one would use:
+
+example(zstyle ':completion:*::rm*:globbed-files' file-patterns '*.o')
+
+With this, using only filenames ending in tt(.o) will be the first
+choice and other filenames will only be used if what is on the line
+matches none of the tt(.o) files (or if there are none).
+
+Note also that during the execution of completion functions, the
+tt(EXTENDED_GLOB) option is in effect, so the characters `tt(#)',
+`tt(~)' and `tt(^)' have special meanings in the patterns.
+)
 item(tt(format))(
 If this is set for the tt(descriptions) tag, its value is used as a
 string to display above matches in completion lists. The sequence
@@ -841,6 +874,10 @@
 patterns will be ignored as long as there are other matches not
 matched by any of the patterns.  It is a more configurable version
 of the shell parameter tt($fignore).
+
+Note that during the execution of completion functions, the
+tt(EXTENDED_GLOB) option is in effect, so the characters `tt(#)',
+`tt(~)' and `tt(^)' have special meanings in the patterns.
 )
 item(tt(insert-unambiguous))(
 This is used by the tt(_match) and tt(_approximate) completer

--
Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx



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