zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] Let _approximate work w/ possible compadd function
@ 2023-05-27 13:12 Marlon Richert
  2023-05-27 16:11 ` Bart Schaefer
  2023-05-27 17:47 ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: Marlon Richert @ 2023-05-27 13:12 UTC (permalink / raw)
  To: zsh-workers

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)



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Let _approximate work w/ possible compadd function
  2023-05-27 13:12 [PATCH] Let _approximate work w/ possible compadd function Marlon Richert
@ 2023-05-27 16:11 ` Bart Schaefer
  2023-06-15 14:29   ` Marlon Richert
  2023-05-27 17:47 ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2023-05-27 16:11 UTC (permalink / raw)
  To: Marlon Richert; +Cc: zsh-workers

On Sat, May 27, 2023 at 6:13 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> Before this patch, if compadd had been overridden by a function of the
> same name, _approximate would not do corrections.

This is what _shadow was added for.  I don't remember why I didn't
apply it to _approximate when I did so for _complete_help, but
possibly it's because those usages actually conflict, i.e., when
running the help function you don't want approximate to bypass the
override?  Will have to think about this a bit when I have more time.

The primary difference from your patch is that _shadow will handle
recursive calls to the function in which it is used.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Let _approximate work w/ possible compadd function
  2023-05-27 13:12 [PATCH] Let _approximate work w/ possible compadd function Marlon Richert
  2023-05-27 16:11 ` Bart Schaefer
@ 2023-05-27 17:47 ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2023-05-27 17:47 UTC (permalink / raw)
  To: Marlon Richert; +Cc: zsh-workers

On Sat, May 27, 2023 at 6:13 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> +  if [[ -v functions[_approximate_compadd] ]]; then
> +    $functions[_approximate_compadd] "$_correct_expl[@]" "$@"

What is this intended to do?  $functions[_approximate_compadd] is the
body of the function, how can that be inlined here?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Let _approximate work w/ possible compadd function
  2023-05-27 16:11 ` Bart Schaefer
@ 2023-06-15 14:29   ` Marlon Richert
  2023-06-21  3:28     ` Bart Schaefer
  2023-06-21  3:49     ` Bart Schaefer
  0 siblings, 2 replies; 7+ messages in thread
From: Marlon Richert @ 2023-06-15 14:29 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

Thanks, I wasn't aware of _shadow. Below is a new patch.

I would find _shadow useful in my own Zsh code, too, not just completions. How
about moving it from Completion/ to Functions/?

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

diff --git a/Completion/Base/Completer/_approximate b/Completion/Base/Completer/_approximate
index dcd8b2776..c0e605439 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,31 @@ 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
+_shadow -s _approximate compadd
+compadd() {
+  local ppre="$argv[(I)-p]"

-    (( $_correct_group && ${${argv[1,(r)-(|-)]}[(I)-*[JV]]} )) &&
-        _correct_expl[_correct_group]=${argv[1,(r)-(-|)][(R)-*[JV]]}
+  [[ ${argv[(I)-[a-zA-Z]#U[a-zA-Z]#]} -eq 0 &&
+      "${#:-$PREFIX$SUFFIX}" -le _comp_correct ]] && return

-    builtin compadd "$_correct_expl[@]" "$@"
-  }
-fi
+  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]]}
+
+  compadd@_approximate "$_correct_expl[@]" "$@"
+}

 _comp_correct=1

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

 } always {
-    [[ -n $dounfunction ]] && (( $+functions[compadd] )) && unfunction compadd
+  _unshadow -s _approximate compadd
 }

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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Let _approximate work w/ possible compadd function
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2023-06-21  3:28 UTC (permalink / raw)
  To: Marlon Richert; +Cc: zsh-workers

On Thu, Jun 15, 2023 at 7:29 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> I would find _shadow useful in my own Zsh code, too, not just completions. How
> about moving it from Completion/ to Functions/?

Unfortunately Functions/ aren't autoloaded by compinit.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Let _approximate work w/ possible compadd function
  2023-06-15 14:29   ` Marlon Richert
  2023-06-21  3:28     ` Bart Schaefer
@ 2023-06-21  3:49     ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2023-06-21  3:49 UTC (permalink / raw)
  To: Marlon Richert; +Cc: zsh-workers

On Thu, Jun 15, 2023 at 7:29 AM Marlon Richert <marlon.richert@gmail.com> wrote:
>
> +_shadow -s _approximate compadd
> +compadd() {
> +  local ppre="$argv[(I)-p]"
> [...]
> +
> +  compadd@_approximate "$_correct_expl[@]" "$@"
> +}

I'm concerned that this doesn't work in the event that _approximate is
called recursively.  Maybe that never happens?  If it did, "_shadow
-s" will not re-create compadd@_approximate, but _unshadow will remove
it, in each case leaving "compadd" pointing at the wrong earlier
shadow.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] Let _approximate work w/ possible compadd function
  2023-06-21  3:28     ` Bart Schaefer
@ 2023-07-04  7:06       ` Marlon Richert
  0 siblings, 0 replies; 7+ messages in thread
From: Marlon Richert @ 2023-07-04  7:06 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On Wed, Jun 21, 2023 at 6:28 AM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Thu, Jun 15, 2023 at 7:29 AM Marlon Richert <marlon.richert@gmail.com> wrote:
> >
> > I would find _shadow useful in my own Zsh code, too, not just completions. How
> > about moving it from Completion/ to Functions/?
>
> Unfortunately Functions/ aren't autoloaded by compinit.

Perhaps then at least add documentation for _shadow to compsys.yo?


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-07-04  7:07 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-27 13:12 [PATCH] Let _approximate work w/ possible compadd function Marlon Richert
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

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).