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

PATCH: use `always' with _approximate



I've been meaning to do this for a while.

You've probably noticed the message:
_main_complete:unfunction:161: no such hash table element: compadd
you sometimes get from aborting approximate completion at the wrong
point.

The key feature here is the "dounfunction" variable and the extra test
for $+functions[compadd].  However, the "always" block ensures that the
code is always executed, which you couldn't easily ensure with a trap.

You should be able to convince yourself, I hope, that it's
impossible for the unfunction to be run or omitted incorrectly.

Index: Completion/Base/Completer/_approximate
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/Completer/_approximate,v
retrieving revision 1.7
diff -u -r1.7 _approximate
--- Completion/Base/Completer/_approximate	7 Jan 2002 14:38:14 -0000	1.7
+++ Completion/Base/Completer/_approximate	21 Mar 2005 22:04:24 -0000
@@ -12,6 +12,8 @@
 
 local _comp_correct _correct_expl _correct_group comax cfgacc match
 local oldcontext="${curcontext}" opm="$compstate[pattern_match]"
+local dounfunction
+integer ret=1
 
 if [[ "$1" = -a* ]]; then
   cfgacc="${1[3,-1]}"
@@ -46,8 +48,12 @@
 # the builtin that adds matches. This is used to be able
 # to stick the `(#a...)' in the right place (after an
 # ignored prefix).
-
+#
+# Current shell structure for use with "always", to make sure
+# we unfunction the compadd.
+{
 if (( ! $+functions[compadd] )); then
+  dounfunction=1
   compadd() {
     local ppre="$argv[(I)-p]"
 
@@ -65,7 +71,6 @@
 
     builtin compadd "$_correct_expl[@]" "$@"
   }
-  trap 'unfunction compadd' EXIT INT
 fi
 
 _comp_correct=1
@@ -101,13 +106,20 @@
     fi
     compstate[pattern_match]="$opm"
 
-    return 0
+    ret=0
+    break
   fi
 
   [[ "${#:-$PREFIX$SUFFIX}" -le _comp_correct+1 ]] && break
   (( _comp_correct++ ))
 done
 
+} always {
+    [[ -n $dounfunction ]] && (( $+functions[compadd] )) && unfunction compadd
+}
+
+(( ret == 0 )) && return 0
+
 compstate[pattern_match]="$opm"
 
 return 1

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxx>
Work: pws@xxxxxxx
Web: http://www.pwstephenson.fsnet.co.uk



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