From 8e158d247cba285a5df1441ee11d39af8a75eb19 Mon Sep 17 00:00:00 2001 From: Marlon Richert Date: Sat, 29 Apr 2023 20:59:03 +0300 Subject: [PATCH 2/3] Make dynamic dir completion easier to implement --- .../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..e173dcb7f 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=1 + local func= + local tag=dynamically-named-directories + [[ -z $ISUFFIX ]] && + local suf=-S] -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 + _tags "$tag" + while _tags; do + while _next_label "$tag" expl "$descr"; do + expl+=( $suf ) + 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)