From: Takeshi Banse <thb@laafc.net>
To: zsh-users@zsh.org
Cc: zsh-workers@zsh.org, Takeshi Banse <thb@laafc.net>
Subject: [RFC PATCH] _path_files: teach zsh_directory_name
Date: Fri, 23 Feb 2018 09:29:50 +0900 [thread overview]
Message-ID: <20180223002950.2892-1-thb@laafc.net> (raw)
In-Reply-To: <p6nhj1$5nv$1@blaine.gmane.org>
Signed-off-by: Takeshi Banse <thb@laafc.net>
---
Hi, I want this feature, too. It is necessary to tweak _path_files function
as far as I can tell. So, I wrote a patch.
To zsh-workers:
This `elif` clause could be merged with the following `elif` clause because
it starts with \~, too. But it may break something, so I did prepend the
separate `elif` clause for \~\[*.
Completion/Unix/Type/_path_files | 70 ++++++++++++++++++++++++++++++++++++++++
Test/Y01completion.ztst | 30 +++++++++++++++++
2 files changed, 100 insertions(+)
diff --git a/Completion/Unix/Type/_path_files b/Completion/Unix/Type/_path_files
index 9fa6ae9fc..8fbc21ffa 100644
--- a/Completion/Unix/Type/_path_files
+++ b/Completion/Unix/Type/_path_files
@@ -274,6 +274,76 @@ if [[ "$pre" = [^][*?#^\|\<\>\\]#(\`[^\`]#\`|\$)*/* && "$compstate[quote]" != \'
orig="${orig[1,(in:i:)/][1,-2]}"
donepath=
prepaths=( '' )
+elif [[ "$pre" = \~\[* && "$compstate[quote]" = (|\`) ]]; then
+ (($+functions[zsh_directory_name] || $+zsh_directory_name_functions)) &&
+ () {
+ # It begins with `~[', so the characters from `$pre[3]' to before the
+ # first `]' represent the dynamic directory name. Otherwise, it may fall
+ # through to the following elif/else clauses.
+
+ local name=
+ () {
+ local -i i=3 n=$#pre sq=0 dq=0
+ local c=
+ while ((i<=n)); do
+ c=$pre[i++]
+ case $c in
+ (\')
+ if ((sq)); then
+ ((sq=0))
+ elif ((dq==0)); then
+ ((sq=1))
+ fi
+ ;;
+ (\")
+ if [[ "$name[-1]" != '\' ]]; then
+ if ((dq)); then
+ ((dq=0))
+ elif ((sq==0)); then
+ ((dq=1))
+ fi
+ fi
+ ;;
+ (\;) ;&
+ (])
+ repeat 1; do
+ ((sq || dq)) && break
+ [[ "$name[-1]" == '\' ]] && break
+ [[ "$c" == ';' ]] && return 1
+ return 0
+ done
+ ;;
+ esac
+ name+="$c"
+ done
+ return 1
+ } &&
+ () {
+ local -a reply
+ local -i ret=1
+ if (($+functions[zsh_directory_name])); then
+ zsh_directory_name n "$name"
+ ret=$?
+ fi
+ if ((ret != 0)); then
+ local func=
+ for func in "${zsh_directory_name_functions[@]-}"; do
+ "$func" n "$name"
+ ret=$?
+ ((ret == 0)) && break
+ done
+ fi
+ if ((ret == 0)); then
+ realpath="$reply[1]/"
+ linepath="$pre[1,$#name+2+1]/"
+ fi
+ return ret
+ }
+ } then
+ pre="${pre#${linepath}}"
+ orig="${orig#${linepath}}"
+ donepath=
+ prepaths=( '' )
elif [[ "$pre[1]" = \~ && "$compstate[quote]" = (|\`) ]]; then
# It begins with `~', so remember anything before the first slash to be able
diff --git a/Test/Y01completion.ztst b/Test/Y01completion.ztst
index b1c0e40e5..cbfbb75d7 100644
--- a/Test/Y01completion.ztst
+++ b/Test/Y01completion.ztst
@@ -36,6 +36,36 @@
>line: {: dir1/}{}
>line: {: dir2/}{}
+ comptesteval 'zsh_directory_name () {
+ case "$1" in
+ (d)
+ return 1
+ ;;
+ (n)
+ reply=("$2")
+ ;;
+ (c)
+ local expl
+ _wanted dynamic-dirs expl dynamic compadd -S\] -Q -- ./
+ ;;
+ esac
+ }'
+ comptest $': ~[\t/\t\t\t\t\t\t\t'
+0:tilde with zsh_directory_name
+>line: {: ~[./]}{}
+>line: {: ~[./]/}{}
+>DESCRIPTION:{file}
+>DI:{dir1}
+>DI:{dir2}
+>FI:{file1}
+>FI:{file2}
+>line: {: ~[./]/dir1/}{}
+>line: {: ~[./]/dir2/}{}
+>line: {: ~[./]/file1}{}
+>line: {: ~[./]/file2}{}
+>line: {: ~[./]/dir1/}{}
+>line: {: ~[./]/dir2/}{}
+
comptesteval '_users () { compadd user1 user2 }'
comptest $': ~\t\t\t\t\t'
0:tilde
--
2.16.1
next prev parent reply other threads:[~2018-02-23 0:36 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-22 22:54 Dynamic named directories and completion Scott Frazer
2018-02-23 0:29 ` Takeshi Banse [this message]
2018-02-23 1:19 ` _path_files: fix copy paste error Takeshi Banse
2018-02-23 1:30 ` _path_files: fix copy paste error (2/2) Takeshi Banse
2018-02-23 8:01 ` Dynamic named directories and completion Vincent Bernat
2018-02-23 14:41 ` Scott Frazer
2018-02-23 15:02 ` Takeshi Banse
2018-02-23 14:42 ` Scott Frazer
2018-02-24 19:34 ` Peter Stephenson
2018-02-24 19:49 ` Mikael Magnusson
2018-02-24 20:12 ` Peter Stephenson
2018-02-26 14:31 ` Scott Frazer
2018-02-26 13:53 ` Scott Frazer
2018-02-26 14:47 ` Peter Stephenson
2018-02-26 16:44 ` Scott Frazer
2018-02-26 20:08 ` Peter Stephenson
2018-02-26 21:22 ` Peter Stephenson
2018-02-26 17:49 ` Mikael Magnusson
2018-02-26 17:55 ` Peter Stephenson
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=20180223002950.2892-1-thb@laafc.net \
--to=thb@laafc.net \
--cc=zsh-users@zsh.org \
--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).