zsh-workers
 help / color / mirror / code / Atom feed
From: Marlon Richert <marlon.richert@gmail.com>
To: zsh-workers@zsh.org
Subject: [PATCH] Let _approximate work w/ possible compadd function
Date: Sat, 27 May 2023 16:12:53 +0300	[thread overview]
Message-ID: <20230527131253.33840-1-marlon.richert@gmail.com> (raw)

Before this patch, if compadd had been overridden by a function of the
same name, _approximate would not do corrections.
---
 Completion/Base/Completer/_approximate | 55 ++++++++++++++------------
 1 file changed, 30 insertions(+), 25 deletions(-)

diff --git a/Completion/Base/Completer/_approximate b/Completion/Base/Completer/_approximate
index dcd8b2776..cdde7a623 100644
--- a/Completion/Base/Completer/_approximate
+++ b/Completion/Base/Completer/_approximate
@@ -12,7 +12,6 @@

 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
@@ -44,34 +43,35 @@ fi

 _tags corrections original

-# Otherwise temporarily define a function to use instead of
-# the builtin that adds matches. This is used to be able
-# to stick the `(#a...)' in the right place (after an
-# ignored prefix).
+# Otherwise temporarily define a function to use instead of 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.
+# Current shell structure for use with "always", to make sure we unfunction our
+# compadd and restore any compadd function defined previously.
 {
-if (( ! $+functions[compadd] )); then
-  dounfunction=1
-  compadd() {
-    local ppre="$argv[(I)-p]"
-
-    [[ ${argv[(I)-[a-zA-Z]#U[a-zA-Z]#]} -eq 0 &&
-       "${#:-$PREFIX$SUFFIX}" -le _comp_correct ]] && return
-
-    if [[ "$PREFIX" = \~* && ( ppre -eq 0 || "$argv[ppre+1]" != \~* ) ]]; then
-      PREFIX="~(#a${_comp_correct})${PREFIX[2,-1]}"
-    else
-      PREFIX="(#a${_comp_correct})$PREFIX"
-    fi
+[[ -v functions[compadd] ]] && functions -c compadd _approximate_compadd
+compadd() {
+  local ppre="$argv[(I)-p]"
+
+  [[ ${argv[(I)-[a-zA-Z]#U[a-zA-Z]#]} -eq 0 &&
+      "${#:-$PREFIX$SUFFIX}" -le _comp_correct ]] && return
+
+  if [[ "$PREFIX" = \~* && ( ppre -eq 0 || "$argv[ppre+1]" != \~* ) ]]; then
+    PREFIX="~(#a${_comp_correct})${PREFIX[2,-1]}"
+  else
+    PREFIX="(#a${_comp_correct})$PREFIX"
+  fi

-    (( $_correct_group && ${${argv[1,(r)-(|-)]}[(I)-*[JV]]} )) &&
-        _correct_expl[_correct_group]=${argv[1,(r)-(-|)][(R)-*[JV]]}
+  (( $_correct_group && ${${argv[1,(r)-(|-)]}[(I)-*[JV]]} )) &&
+      _correct_expl[_correct_group]=${argv[1,(r)-(-|)][(R)-*[JV]]}

+  if [[ -v functions[_approximate_compadd] ]]; then
+    $functions[_approximate_compadd] "$_correct_expl[@]" "$@"
+  else
     builtin compadd "$_correct_expl[@]" "$@"
-  }
-fi
+  fi
+}

 _comp_correct=1

@@ -115,7 +115,12 @@ while [[ _comp_correct -le comax ]]; do
 done

 } always {
-    [[ -n $dounfunction ]] && (( $+functions[compadd] )) && unfunction compadd
+  if [[ -v functions[_approximate_compadd] ]]; then
+    functions -c _approximate_compadd compadd
+    unfunction _approximate_compadd
+  else
+    unfunction compadd
+  fi
 }

 (( ret == 0 )) && return 0
--
2.39.2 (Apple Git-143)



             reply	other threads:[~2023-05-27 13:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-27 13:12 Marlon Richert [this message]
2023-05-27 16:11 ` Bart Schaefer
2023-06-15 14:29   ` Marlon Richert
2023-06-21  3:28     ` Bart Schaefer
2023-07-04  7:06       ` Marlon Richert
2023-06-21  3:49     ` Bart Schaefer
2023-05-27 17:47 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230527131253.33840-1-marlon.richert@gmail.com \
    --to=marlon.richert@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).