zsh-workers
 help / color / mirror / code / Atom feed
* Patch 1/3: Fix prefix insertion logic
@ 2023-04-29 18:07 Marlon Richert
  2023-04-29 18:08 ` Patch 2/3: Make dynamic dir completion easier to implement Marlon Richert
  2023-04-29 18:58 ` Patch 1/3: Fix prefix insertion logic Felipe Contreras
  0 siblings, 2 replies; 28+ messages in thread
From: Marlon Richert @ 2023-04-29 18:07 UTC (permalink / raw)
  To: Zsh hackers list


[-- Attachment #1.1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #1.2: Type: text/html, Size: 96 bytes --]

[-- Attachment #2: 0001-51641-Fix-_prefix-insertion-logic.txt --]
[-- Type: text/plain, Size: 2609 bytes --]

From 6a4f094ebd11e10a03d8c10016a0f6de392022ba Mon Sep 17 00:00:00 2001
From: Marlon Richert <marlon.richert@hibox.tv>
Date: Sat, 29 Apr 2023 19:25:37 +0300
Subject: [PATCH 1/3] 51641: Fix _prefix insertion logic

---
 Completion/Base/Completer/_prefix |  9 +--------
 Test/Y01completion.ztst           | 18 ++++++++++++++++++
 Test/comptest                     |  2 +-
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/Completion/Base/Completer/_prefix b/Completion/Base/Completer/_prefix
index 74be5f47d..01739166e 100644
--- a/Completion/Base/Completer/_prefix
+++ b/Completion/Base/Completer/_prefix
@@ -49,14 +49,7 @@ for tmp in "$comp[@]"; do
     fi
 
     if [[ "$tmp" != _prefix ]] && "$tmp"; then
-      [[ compstate[nmatches] -gt 1 ]] && return 0
-      compadd -U -i "$IPREFIX" -I "$ISUFFIX" - "${compstate[unambiguous]%$suf}x"
-      compstate[list]=
-      if [[ -n $compstate[unambiguous] ]]; then
-        compstate[insert]=unambiguous
-      else
-        compstate[insert]=0
-      fi
+      compstate[to_end]=''
       return 0
     fi
     (( _matcher_num++ ))
diff --git a/Test/Y01completion.ztst b/Test/Y01completion.ztst
index f976f9f91..51e246307 100644
--- a/Test/Y01completion.ztst
+++ b/Test/Y01completion.ztst
@@ -63,6 +63,24 @@
 >NO:{file1}
 >NO:{file2}
 
+  comptesteval 'tst-insert() { compstate[insert]=1; compstate[list]= }'
+  comptesteval 'comppostfuncs=( tst-insert )'
+  comptest $': dir1\ebf\t'
+0:_prefix with compstate[insert]=1 does not move to end
+>line: {: file1}{dir1}
+
+  comptesteval 'unfunction tst-insert'
+  comptesteval 'comppostfuncs=()'
+  comptest $': dir1\ebf\t\t\t\t'
+0:_prefix inserts unambiguous and does not move to end
+>line: {: file}{dir1}
+>line: {: file}{dir1}
+>DESCRIPTION:{file}
+>FI:{file1}
+>FI:{file2}
+>line: {: file1}{dir1}
+>line: {: file2}{dir1}
+
   comptesteval $'zstyle -d \'*\' glob'
   comptesteval '_users () { compadd user1 user2 }'
   comptest $': ~\t\t\t\t\t'
diff --git a/Test/comptest b/Test/comptest
index 79c69979a..a57f4bcc4 100644
--- a/Test/comptest
+++ b/Test/comptest
@@ -40,7 +40,7 @@ KEYTIMEOUT=1
 setopt zle
 autoload -U compinit
 compinit -u
-zstyle ":completion:*" completer _expand _complete _ignored
+zstyle ":completion:*" completer _expand _complete _prefix _ignored
 zstyle ":completion:*:default" list-colors "no=<NO>" "fi=<FI>" "di=<DI>" "ln=<LN>" "pi=<PI>" "so=<SO>" "bd=<BD>" "cd=<CD>" "ex=<EX>" "mi=<MI>" "tc=<TC>" "sp=<SP>" "lc=<LC>" "ec=<EC>\n" "rc=<RC>"
 zstyle ":completion:*" group-name ""
 zstyle ":completion:*:messages" format "<MESSAGE>%d</MESSAGE>
-- 
2.39.2 (Apple Git-143)


^ permalink raw reply	[flat|nested] 28+ messages in thread
* Re: [PATCH 2/3] Make dynamic dir completion easier to implement
@ 2023-05-18 20:44 Marlon Richert
  0 siblings, 0 replies; 28+ messages in thread
From: Marlon Richert @ 2023-05-18 20:44 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-workers

Thanks for the feedback! Below is a new patch.

---

This patch implements most of the logic needed to write a custom
completion function for dynamic directory names into
_dynamic_directory_name.  This includes:

- Default tag and group names
- Correct logic for inserting `]` as a suffix
- A completion tag loop

This way, these don't need to be implemented and maintained for each
such function individually.
---
 .../Zsh/Context/_dynamic_directory_name       | 30 +++++++++----
 Doc/Zsh/expn.yo                               | 44 +++++++------------
 Test/Y01completion.ztst                       | 11 +++++
 3 files changed, 50 insertions(+), 35 deletions(-)

diff --git a/Completion/Zsh/Context/_dynamic_directory_name b/Completion/Zsh/Context/_dynamic_directory_name
index f449c3b12..5e0d73a8d 100644
--- a/Completion/Zsh/Context/_dynamic_directory_name
+++ b/Completion/Zsh/Context/_dynamic_directory_name
@@ -1,15 +1,29 @@
 #autoload
+local -a dirfuncs=(
+    ${(k)functions[zsh_directory_name]}
+    $zsh_directory_name_functions
+)
+local descr='dynamically named directory'

-local func
-integer ret=1
+if (( $#dirfuncs )); then
+  local -a expl
+  local -i ret
+  local func suf tag=dynamically-named-directories

-if [[ -n $functions[zsh_directory_name] || \
-  ${+zsh_directory_name_functions} -ne 0 ]] ; then
-  [[ -n $functions[zsh_directory_name] ]] && zsh_directory_name c && ret=0
-  for func in $zsh_directory_name_functions; do
-    $func c && ret=0
+  [[ $ISUFFIX != \]* ]] &&
+      suf=-S]
+
+  _tags "$tag"
+  while _tags; do
+    while _next_label "$tag" expl "$descr" $suf; do
+      for func in $dirfuncs; do
+        $func c && ret=0
+      done
+    done
+    (( ret )) || break
   done
   return ret
+
 else
-  _message 'dynamic directory name: implemented as zsh_directory_name c'
+  _message "${descr}: implement as zsh_directory_name c"
 fi
diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 19f5909ea..6f86d0c54 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -2066,34 +2066,24 @@ tt(/home/pws/perforce).  In this simple case a static name for the
 directory would be just as effective.

 example(zsh_directory_name+LPAR()RPAR() {
-  emulate -L zsh
-  setopt extendedglob
+  emulate -L zsh -o extendedglob
   local -a match mbegin mend
-  if [[ $1 = d ]]; then
-    # turn the directory into a name
-    if [[ $2 = (#b)(/home/pws/perforce/)([^/]##)* ]]; then
-      typeset -ga reply
-      reply=(p:$match[2] $(( ${#match[1]} + ${#match[2]} )) )
-    else
-      return 1
-    fi
-  elif [[ $1 = n ]]; then
-    # turn the name into a directory
-    [[ $2 != (#b)p:(?*) ]] && return 1
-    typeset -ga reply
-    reply=(/home/pws/perforce/$match[1])
-  elif [[ $1 = c ]]; then
-    # complete names
-    local expl
-    local -a dirs
-    dirs=(/home/pws/perforce/*(/:t))
-    dirs=(p:${^dirs})
-    _wanted dynamic-dirs expl 'dynamic directory' compadd -S\] -a dirs
-    return
-  else
-    return 1
-  fi
-  return 0
+  local base=/home/pws/perforce
+  case $1 in
+  ( d )  # Turn the directory into a name.
+    [[ $2 == (#b)($base/)([^/]##)* ]] &&
+        reply=( p:$match[2] $(( $#match[1] + $#match[2] )) )
+  ;;
+  ( n )  # Turn the name into a directory.
+    [[ $2 == (#b)p:(?*) ]] &&
+        reply=( $base/$match[1] )
+  ;;
+  ( c )  # Complete names.
+    local -a dirs=( $base/*(/:t) )
+    # Completion system populates $expl with flags for compadd.
+    compadd "$expl[@]" p:$^dirs
+  ;;
+  esac
 })

 texinode(Static named directories)(`=' expansion)(Dynamic named directories)(Filename Expansion)
diff --git a/Test/Y01completion.ztst b/Test/Y01completion.ztst
index 51e246307..2524c43bd 100644
--- a/Test/Y01completion.ztst
+++ b/Test/Y01completion.ztst
@@ -93,6 +93,17 @@
 >line: {: ~user2}{}
 >line: {: ~user1}{}

+  comptesteval 'zsh_directory_name() { compadd "$expl[@]" -- name1 name2 }'
+  comptest $': ~[\t\t\t\t'
+0:dynamic directory names after ~[
+>line: {: ~[name}{}
+>line: {: ~[name}{}
+>DESCRIPTION:{dynamically named directory}
+>NO:{name1}
+>NO:{name2}
+>line: {: ~[name1]}{}
+>line: {: ~[name2]}{}
+
   comptest $'echo ;:\C-b\C-b\t'
 0:directories and files before separator
 >line: {echo }{;:}
--
2.39.2 (Apple Git-143)



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

end of thread, other threads:[~2023-06-15 14:12 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-29 18:07 Patch 1/3: Fix prefix insertion logic Marlon Richert
2023-04-29 18:08 ` Patch 2/3: Make dynamic dir completion easier to implement Marlon Richert
2023-04-29 18:09   ` Patch 3/3: Fix subscript completion bugs inside ~[...] Marlon Richert
2023-04-29 18:58 ` Patch 1/3: Fix prefix insertion logic Felipe Contreras
2023-04-29 19:02   ` Marlon Richert
2023-04-29 19:17     ` Bart Schaefer
2023-05-01 21:08     ` Aaron Schrab
2023-05-01 22:03       ` Bart Schaefer
2023-05-05 11:41         ` [PATCH 1/3] Fix _prefix " Marlon Richert
2023-06-07  6:03           ` Marlon Richert
2023-06-08 12:41             ` Jun. T
2023-06-15 14:11             ` Marlon Richert
2023-05-05 11:41         ` [PATCH 2/3] Make dynamic dir completion easier to implement Marlon Richert
2023-05-06 17:28           ` Bart Schaefer
2023-05-13 17:30             ` Bart Schaefer
2023-05-15  9:04           ` Oliver Kiddle
2023-05-05 11:41         ` [PATCH 3/3] Fix subscript completion bugs inside ~[...] Marlon Richert
2023-05-01 22:37     ` Patch 1/3: Fix prefix insertion logic Felipe Contreras
2023-05-01 22:52       ` Bart Schaefer
2023-05-02  4:49         ` Felipe Contreras
2023-05-02 19:16           ` Bart Schaefer
2023-05-02 20:14             ` Felipe Contreras
2023-05-05 11:51           ` Marlon Richert
2023-05-05 12:38             ` Felipe Contreras
2023-05-05 15:04             ` Mikael Magnusson
2023-05-06 17:23               ` Bart Schaefer
2023-05-01 23:36       ` Vin Shelton
2023-05-18 20:44 [PATCH 2/3] Make dynamic dir completion easier to implement Marlon Richert

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