From 602c2d81251d8c57dd1d0ea228dc5a3e8df68e00 Mon Sep 17 00:00:00 2001 From: Tanaka Akira Date: Sat, 11 Sep 1999 08:54:45 +0000 Subject: zsh-workers/7778 --- Completion/Base/_regex_arguments | 67 +++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/Completion/Base/_regex_arguments b/Completion/Base/_regex_arguments index 7f6ecd424..12f4d6d2d 100644 --- a/Completion/Base/_regex_arguments +++ b/Completion/Base/_regex_arguments @@ -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 @@ -396,31 +403,47 @@ _regex_arguments () { 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 "$@" -- cgit 1.4.1