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

PATCH: some more small optimisations



Nothing really dramatic...

- _patcomps and _postpatcomps are now assocs (we can do this now that
  we have the (K) subscript flag)
- in _main_complete: avoid some expensive post-processing if the
  completion code will be called again due to multiple values for
  $compmatchers
- two small fixes in _normal: the command name put into $curcontext
  sometimes was the wrong one (the pathname where I wanted the
  non-pathname)


NOTE: the first change means that you have to rm the dumpfile after
this patch, otherwise you will get an error about an invalid value for 
an assoc.


Bye
 Sven

diff -ru ../z.old/Completion/Core/_main_complete Completion/Core/_main_complete
--- ../z.old/Completion/Core/_main_complete	Tue Dec 14 10:54:00 1999
+++ Completion/Core/_main_complete	Tue Dec 14 11:06:14 1999
@@ -69,79 +69,66 @@
   fi
 done
 
-# See which tags were or were not used.
-
-_used_tags=( "${(@)_tried_tags:#${(j:|:)~${(@)_failed_tags//\[/\\[}//\]/\\]}}" )
-_unused_tags=( "${(@)_offered_tags:#${(j:|:)~${(@)_used_tags//\[/\\[}//\]/\\]}}" )
-
-# Now call the post-functions.
-
-for post in "$comppostfuncs[@]"; do
-  "$post"
-done
-comppostfuncs=()
-
-_lastdescr=( "\`${(@)^_lastdescr:#}'" )
-
-[[ _last_nmatches -ge 0 && _last_nmatches -ne compstate[nmatches] ]] &&
-    _menu_style=( "$_last_menu_style[@]" "$_menu_style[@]" )
-
-if [[ "$compstate[insert]" = "$_saved_insert" ]]; then
-  if [[ -n "$_menu_style[(r)(yes|true|1|on)]" ||
-        ( -n "$_menu_style[(r)auto*]" &&
-          "$compstate[insert]" = automenu ) ]]; then
-    compstate[insert]=menu
-  elif [[ -n "$_menu_style[(r)auto*]" &&
-          "$compstate[insert]" != automenu ]]; then
-    compstate[insert]=automenu-unambiguous
-  elif [[ -n "$_menu_style[(r)(no|false|0|off)]" ]]; then
-    compstate[insert]=unambiguous
-  elif [[ -n "$_def_menu_style[(r)(yes|true|1|on)]" ||
-        ( -n "$_def_menu_style[(r)auto*]" &&
-          "$compstate[insert]" = automenu ) ]]; then
-    compstate[insert]=menu
-  elif [[ -n "$_def_menu_style[(r)auto*]" &&
-          "$compstate[insert]" != automenu ]]; then
-    compstate[insert]=automenu-unambiguous
-  elif [[ -n "$_def_menu_style[(r)(no|false|0|off)]" ]]; then
-    compstate[insert]=unambiguous
+if (( $compstate[nmatches] )); then
+  [[ _last_nmatches -ge 0 && _last_nmatches -ne compstate[nmatches] ]] &&
+      _menu_style=( "$_last_menu_style[@]" "$_menu_style[@]" )
+
+  if [[ "$compstate[insert]" = "$_saved_insert" ]]; then
+    if [[ -n "$_menu_style[(r)(yes|true|1|on)]" ||
+          ( -n "$_menu_style[(r)auto*]" &&
+            "$compstate[insert]" = automenu ) ]]; then
+      compstate[insert]=menu
+    elif [[ -n "$_menu_style[(r)auto*]" &&
+            "$compstate[insert]" != automenu ]]; then
+      compstate[insert]=automenu-unambiguous
+    elif [[ -n "$_menu_style[(r)(no|false|0|off)]" ]]; then
+      compstate[insert]=unambiguous
+    elif [[ -n "$_def_menu_style[(r)(yes|true|1|on)]" ||
+          ( -n "$_def_menu_style[(r)auto*]" &&
+            "$compstate[insert]" = automenu ) ]]; then
+      compstate[insert]=menu
+    elif [[ -n "$_def_menu_style[(r)auto*]" &&
+            "$compstate[insert]" != automenu ]]; then
+      compstate[insert]=automenu-unambiguous
+    elif [[ -n "$_def_menu_style[(r)(no|false|0|off)]" ]]; then
+      compstate[insert]=unambiguous
+    fi
   fi
-fi
-
-_menu_style=( "$_menu_style[@]" "$_def_menu_style[@]" )
 
-if [[ "$compstate[insert]" = *menu ]]; then
-  if [[ -n "$_menu_style[(r)no-select*]" ]]; then
-    unset SELECTMIN
-  else
-    sel=( "${(@M)_menu_style:#select*}" )
-
-    if (( $# )); then
-      local min=9999999 i num
-
-      for i in "$sel[@]"; do
-        if [[ "$i" = *\=* ]]; then
-	  num="${i#*\=}"
-	  [[ num -lt 0 ]] && num=0
-	else
-	  num=0
-	fi
-	[[ num -lt min ]] && min="$num"
+  _menu_style=( "$_menu_style[@]" "$_def_menu_style[@]" )
 
-	(( min )) || break
-      done
+  if [[ "$compstate[insert]" = *menu ]]; then
+    if [[ -n "$_menu_style[(r)no-select*]" ]]; then
+      unset SELECTMIN
+    else
+      sel=( "${(@M)_menu_style:#select*}" )
+
+      if (( $# )); then
+        local min=9999999 i num
+
+        for i in "$sel[@]"; do
+          if [[ "$i" = *\=* ]]; then
+  	    num="${i#*\=}"
+  	    [[ num -lt 0 ]] && num=0
+  	  else
+  	    num=0
+  	  fi
+  	  [[ num -lt min ]] && min="$num"
+  
+	  (( min )) || break
+        done
 
-      SELECTMIN="$min"
+        SELECTMIN="$min"
+      fi
     fi
   fi
-fi
-
-if [[ compstate[nmatches] -eq 0 &&
-      compstate[matcher] -eq compstate[total_matchers] &&
-      $#_lastdescr -ne 0 ]] &&
-   zstyle -s ":completion${curcontext}:warnings" format format; then
+elif [[ compstate[matcher] -eq compstate[total_matchers] &&
+        $#_lastdescr -ne 0 ]] &&
+     zstyle -s ":completion${curcontext}:warnings" format format; then
   local str
 
+  _lastdescr=( "\`${(@)^_lastdescr:#}'" )
+
   compstate[list]='list force'
   compstate[insert]=''
 
@@ -155,18 +142,34 @@
   compadd -UX "$format" -n ''
 fi
 
-_lastcomp=( "${(@kv)compstate}" )
-_lastcomp[completer]="$comp"
-_lastcomp[prefix]="$PREFIX"
-_lastcomp[suffix]="$SUFFIX"
-_lastcomp[iprefix]="$IPREFIX"
-_lastcomp[isuffix]="$ISUFFIX"
-_lastcomp[qiprefix]="$QIPREFIX"
-_lastcomp[qisuffix]="$QISUFFIX"
-_lastcomp[offered_tags]="${(j.:.)_offered_tags}"
-_lastcomp[tried_tags]="${(j.:.)_tried_tags}"
-_lastcomp[failed_tags]="${(j.:.)_failed_tags}"
-_lastcomp[unused_tags]="${(j.:.)_unused_tags}"
-_lastcomp[used_tags]="${(j.:.)_used_tags}"
+if [[ compstate[matcher] -eq compstate[total_matchers] ||
+      compstate[nmatches] -ne 0 ]]; then
+
+  # See which tags were or were not used.
+
+  _used_tags=( "${(@)_tried_tags:#${(j:|:)~${(@)_failed_tags//\[/\\[}//\]/\\]}}" )
+  _unused_tags=( "${(@)_offered_tags:#${(j:|:)~${(@)_used_tags//\[/\\[}//\]/\\]}}" )
+
+  # Now call the post-functions.
+
+  for post in "$comppostfuncs[@]"; do
+    "$post"
+  done
+  comppostfuncs=()
+
+  _lastcomp=( "${(@kv)compstate}" )
+  _lastcomp[completer]="$comp"
+  _lastcomp[prefix]="$PREFIX"
+  _lastcomp[suffix]="$SUFFIX"
+  _lastcomp[iprefix]="$IPREFIX"
+  _lastcomp[isuffix]="$ISUFFIX"
+  _lastcomp[qiprefix]="$QIPREFIX"
+  _lastcomp[qisuffix]="$QISUFFIX"
+  _lastcomp[offered_tags]="${(j.:.)_offered_tags}"
+  _lastcomp[tried_tags]="${(j.:.)_tried_tags}"
+  _lastcomp[failed_tags]="${(j.:.)_failed_tags}"
+  _lastcomp[unused_tags]="${(j.:.)_unused_tags}"
+  _lastcomp[used_tags]="${(j.:.)_used_tags}"
+fi
 
 return ret
diff -ru ../z.old/Completion/Core/_normal Completion/Core/_normal
--- ../z.old/Completion/Core/_normal	Tue Dec 14 10:54:01 1999
+++ Completion/Core/_normal	Tue Dec 14 11:06:14 1999
@@ -26,11 +26,11 @@
   if [[ "$command[1]" == '=' ]]; then
     eval cmd1\=$command
     cmd2="$command[2,-1]"
-    curcontext="${curcontext}::${cmd1}:"
+    curcontext="${curcontext}::${cmd2}:"
   elif [[ "$command" == */* ]]; then
     cmd1="$command"
     cmd2="${command:t}"
-    curcontext="${curcontext}::${cmd1}:"
+    curcontext="${curcontext}::${cmd2}:"
   else
     cmd1="$command"
     cmd2="$commands[$command]"
@@ -41,17 +41,22 @@
 # See if there are any matching pattern completions.
 
 if [[ "$_compskip" != (all|*patterns*) ]]; then
-  for i in "$_patcomps[@]"; do
-    pat="${i% *}"
-    val="${i#* }"
-    if [[ "$cmd1" == $~pat || "$cmd2" == $~pat ]]; then
-      "$val" && ret=0
-      if [[ "$_compskip" = *patterns* ]]; then
-        break
-      elif [[ "$_compskip" = all ]]; then
-        _compskip=''
-        return ret
-      fi
+  for i in "${(@)_patcomps[(K)$cmd1]}"; do
+    "$i" && ret=0
+    if [[ "$_compskip" = *patterns* ]]; then
+      break
+    elif [[ "$_compskip" = all ]]; then
+      _compskip=''
+      return ret
+    fi
+  done
+  for i in "${(@)_patcomps[(K)$cmd2]}"; do
+    "$i" && ret=0
+    if [[ "$_compskip" = *patterns* ]]; then
+      break
+    elif [[ "$_compskip" = all ]]; then
+      _compskip=''
+      return ret
     fi
   done
 fi
@@ -80,18 +85,24 @@
 fi
 
 if [[ "$_compskip" != (all|*patterns*) ]]; then
-  for i in "$_postpatcomps[@]"; do
-    pat="${i% *}"
-    val="${i#* }"
-    if [[ "$cmd1" == $~pat || "$cmd2" == $~pat ]]; then
-      _compskip=default
-      "$val" && ret=0
-      if [[ "$_compskip" = *patterns* ]]; then
-	break
-      elif [[ "$_compskip" = all ]]; then
-        _compskip=''
-        return ret
-      fi
+  for i in "${(@)_postpatcomps[(K)$cmd1]}"; do
+    _compskip=default
+    "$i" && ret=0
+    if [[ "$_compskip" = *patterns* ]]; then
+      break
+    elif [[ "$_compskip" = all ]]; then
+      _compskip=''
+      return ret
+    fi
+  done
+  for i in "${(@)_postpatcomps[(K)$cmd2]}"; do
+    _compskip=default
+    "$i" && ret=0
+    if [[ "$_compskip" = *patterns* ]]; then
+      break
+    elif [[ "$_compskip" = all ]]; then
+      _compskip=''
+      return ret
     fi
   done
 fi
diff -ru ../z.old/Completion/Core/compdump Completion/Core/compdump
--- ../z.old/Completion/Core/compdump	Tue Dec 14 10:54:02 1999
+++ Completion/Core/compdump	Tue Dec 14 11:06:14 1999
@@ -34,14 +34,14 @@
 print ")" >> $_d_file
 
 print "\n_patcomps=(" >> $_d_file
-for _d_f in "$_patcomps[@]"; do
-  print -r - "'${_d_f//\'/'\\''}'"
+for _d_f in "${(ok@)_patcomps}"; do
+  print -r - "'${_d_f//\'/'\\''}'" "'${_patcomps[$_d_f]//\'/'\\''}'"
 done >> $_d_file
 print ")" >> $_d_file
 
 print "\n_postpatcomps=(" >> $_d_file
-for _d_f in "$_postpatcomps[@]"; do
-  print -r - "'${_d_f//\'/'\\''}'"
+for _d_f in "${(ok@)_postpatcomps}"; do
+  print -r - "'${_d_f//\'/'\\''}'" "'${_postpatcomps[$_d_f]//\'/'\\''}'"
 done >> $_d_file
 print ")" >> $_d_file
 
diff -ru ../z.old/Completion/Core/compinit Completion/Core/compinit
--- ../z.old/Completion/Core/compinit	Tue Dec 14 10:54:02 1999
+++ Completion/Core/compinit	Tue Dec 14 11:06:14 1999
@@ -79,12 +79,10 @@
 done
 
 # The associative array containing the definitions for the commands.
-# Definitions for patterns will be stored in the normal arrays `_patcomps'
+# Definitions for patterns will be stored in the associations `_patcomps'
 # and `_postpatcomps'.
 
-typeset -gA _comps
-_patcomps=()
-_postpatcomps=()
+typeset -gA _comps _patcomps _postpatcomps
 
 # The associative array use to report information about the last
 # cmpletion to the outside.
@@ -212,17 +210,14 @@
         echo "$0: only one pattern allowed"
 	return 1
       fi
-      # Patterns are stored in strings like `c* foo', with a space
-      # between the pattern and the function name.
-
-      _patcomps=("$_patcomps[@]" "$1 $func")
+      _patcomps[$1]="$func"
       ;;
     postpattern)
       if [[ $# -gt 1 ]]; then
         echo "$0: only one pattern allowed"
 	return 1
       fi
-      _postpatcomps=("$_postpatcomps[@]" "$1 $func")
+      _postpatcomps[$1]="$func"
       ;;
     widgetkey)
       while [[ -n $1 ]]; do
@@ -283,15 +278,10 @@
     # Handle the `-d' option, deleting.
     case "$type" in
     pattern)
-      # Note the space.
-      for i; do
-        _patcomps=("${(@)_patcomps:#$i *}")
-      done
+      unset "_patcomps[$^@]"
       ;;
     postpattern)
-      for i; do
-        _postpatcomps=("${(@)_postpatcomps:#$i *}")
-      done
+      unset "_postpatcomps[$^@]"
       ;;
     key)
       # Oops, cannot do that yet.
@@ -300,10 +290,7 @@
       return 1
       ;;
     *)
-      # Deleting definitons for command is even simpler.
-      for i; do
-        unset "_comps[$i]"
-      done
+      unset "_comps[$^@]"
     esac
   fi
 }

--
Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx



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