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

Sort of PATCH: local completions



I'm sure this can be done much better.  This modifies the normal way of
choosing a context-sensitive completion to allow you to have a special form
in a particular directory.  For example, you may want to offer a particular
set of options to `make' in a certain directory which it's too complicated
to make the default _make dig out.  Then you would create a file in that
directory called `.zcompletions' and put in it something like

  typeset -A _localcomps
  _localcomps=(make _make_for_this_directory)

where _make_for_this_directory is already registered as an autoloaded
function.  Completion for everything except make is (supposed to be) normal.

There are various problems with this as it stands.

It is tied to specific names and types --- .zcompletions, _localcomps.  I
wanted to allow the user to have arbitrary code in the function, though.
For example, you can declare other local variables, though I don't know how
useful that is in practice.  This seemed a bit better than imposing a
format and hiding the definition of _localcomps.

There is no way of making styles local.  This would take a certain amount
of internal hacking.  For example, we could add a flag to zstyle which tied
the definitions to the function level in the same way as local variables.
(Then the code for sourcing .zcompletions would most naturally live in
_main_complete.)

Using local styles could in principle allow a replacement for _localcomps,
which could itself become a style that the completion system will decant
into an associative array; this doesn't change the effect of the code, but
makes the interface much neater, e.g.
  zstyle -l ':completion:*' local-completions make _make_for_this_directory
(shame `-L' is already taken).  (local-completions would of course be a
style like any other, just not very useful the rest of the time.)

I'm certainly not going to commit this for now.

Index: Completion/Base/Core/_normal
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/Core/_normal,v
retrieving revision 1.1
diff -u -r1.1 _normal
--- Completion/Base/Core/_normal	2001/04/02 11:04:04	1.1
+++ Completion/Base/Core/_normal	2001/11/02 17:40:57
@@ -3,6 +3,10 @@
 local comp command cmd1 cmd2 pat val name i ret=1 _compskip="$_compskip"
 local curcontext="$curcontext" service
 
+if [[ -e .zcompletions ]]; then
+  . .zcompletions
+fi
+
 # If we get the option `-s', we don't reset `_compskip'. This ensures
 # that a value set in the function for the `-first-' context is kept,
 # but that we still use pattern functions when we were called form
@@ -18,7 +22,11 @@
 if [[ CURRENT -eq 1 ]]; then
   curcontext="${curcontext%:*:*}:-command-:"
 
-  comp="$_comps[-command-]"
+  if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[-command-]}" ]]; then
+    comp="${_localcomps[-command-]}"
+  else
+    comp="$_comps[-command-]"
+  fi
   [[ -z "$comp" ]] || "$comp" && ret=0
 
   return ret
@@ -76,11 +84,22 @@
 
 ret=1
 name="$cmd1"
-comp="$_comps[$cmd1]"
+if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[$cmd1]}" ]]; then
+  comp="${_localcomps[$cmd1]}"
+else
+  comp="$_comps[$cmd1]"
+fi
 service="${_services[$cmd1]:-$cmd1}"
 
-[[ -z "$comp" ]] &&
-    name="$cmd2" comp="$_comps[$cmd2]" service="${_services[$cmd2]:-$cmd2}"
+if [[ -z "$comp" ]]; then
+  name="$cmd2" 
+  if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[$cmd2]}" ]]; then
+    comp="${_localcomps[$cmd2]}"
+  else
+    comp="$_comps[$cmd2]"
+  fi
+  service="${_services[$cmd2]:-$cmd2}"
+fi
 
 # And generate the matches, probably using default completion.
 
@@ -90,7 +109,11 @@
   [[ "$_compskip" = (all|*patterns*) ]] && return ret
 elif [[ "$_compskip" != *default* ]]; then
   name=-default-
-  comp="$_comps[-default-]"
+  if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[-default-]}" ]]; then
+    comp="${_localcomps[-default-]}"
+  else
+    comp="$_comps[-default-]"
+  fi
 fi
 
 if [[ "$_compskip" != (all|*patterns*) ]]; then

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 392070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************



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