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

PATCH: _regex_arguments use cache.



I made _regex_arguments to cache generated state machines.

Index: Completion/Base/_regex_arguments
===================================================================
RCS file: /projects/zsh/zsh/Completion/Base/_regex_arguments,v
retrieving revision 1.1.1.2
diff -u -F^( -r1.1.1.2 _regex_arguments
--- _regex_arguments	1999/09/11 01:42:45	1.1.1.2
+++ _regex_arguments	1999/09/11 08:48:27
@@ -6,6 +6,13 @@
 
 ## usage: _regex_arguments funcname regex
 
+## configuration key used:
+
+# regex_arguments_path
+#  The path to a directory for caching. (default: ~/.zsh/regex_arguments)
+
+##
+
 # _regex_arguments compiles `regex' and emit the result of the state
 # machine into the function `funcname'. `funcname' parses a command line
 # according to `regex' and evaluate appropriate actions in `regex'. Before
@@ -395,32 +402,48 @@
   typeset -A tbl cutoff pattern lookahead parse_action complete_action
   local regex index first last nullable
   local i state next
-
-  funcname="$1"
-  shift
 
-  regex=("$@")
-  index=1
-  tbl=()
-  pattern=()
-  lookahead=()
-  parse_action=()
-  complete_action=()
-  _ra_parse_alt
-
-  if (( $? == 2 || index != $#regex + 1 )); then
-    if (( index != $#regex + 1 )); then
-      print "regex parse error at $index: $regex[index]" >&2
-    else
-      print "regex parse error at $index (end)" >&2
+  local cache_dir="${compconfig[regex_arguments_path]:-$HOME/.zsh/regex_arguments}"
+  local cache_file="$cache_dir/$1"
+  local cache_test
+
+  if ! [[ -f "$cache_file" ]] || ! source "$cache_file" "$@"; then
+    cache_test='[[ $# -eq '$#' && "$*" = '"${*:q}"' ]]'
+
+    funcname="$1"
+    shift
+
+    regex=("$@")
+    index=1
+    tbl=()
+    pattern=()
+    lookahead=()
+    parse_action=()
+    complete_action=()
+    _ra_parse_alt
+
+    if (( $? == 2 || index != $#regex + 1 )); then
+      if (( index != $#regex + 1 )); then
+	print "regex parse error at $index: $regex[index]" >&2
+      else
+	print "regex parse error at $index (end)" >&2
+      fi
+      return 1
     fi
-    return 1
-  fi
+
+    funcdef="$(_ra_gen_func)"
 
-  funcdef="$(_ra_gen_func)"
+    unfunction "$funcname" 2>/dev/null
+    eval "${(F)funcdef}"
 
-  unfunction "$funcname" 2>/dev/null
-  eval "${(F)funcdef}"
+    [[ -d "$cache_dir" && -w "$cache_dir" ]] && {
+      print -lr - \
+	"if $cache_test; then" \
+	"$funcdef" \
+	'true; else false; fi' > "${cache_file}.$HOST.$$"
+      mv "${cache_file}.$HOST.$$" "${cache_file}"
+    }
+  fi
 }
 
 _regex_arguments "$@"
-- 
Tanaka Akira



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