[PATCH] Add zsh completion for extensions

Alexandre Pujol alexandre at pujol.io
Sun Dec 9 20:42:57 CET 2018


To add your own 'foo' extension, create a _pass-foo() function inside an
autoloaded #compdef file. You can add a description to such a function
adding a line matching:

  #description DESCRIPTION

This extension format is based on the completion format used by git.
---
 src/completion/pass.zsh-completion | 43 ++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/src/completion/pass.zsh-completion b/src/completion/pass.zsh-completion
index 27ce15a..a541316 100644
--- a/src/completion/pass.zsh-completion
+++ b/src/completion/pass.zsh-completion
@@ -17,6 +17,15 @@
 #   PASSWORD_STORE_DIR=$HOME/work/pass pass $@
 # }
 
+# If you have your own pass extension, you can make that command known to the
+# completion creating a function _pass-foo() to handle specific completion
+# for that command. Place such a function inside an autoloaded #compdef file
+# and you should be all set. You can add a description to such a function by
+# adding a line matching
+#
+#     #description DESCRIPTION
+#
+# as the second line in the file.
 
 _pass () {
 	local cmd
@@ -84,6 +93,9 @@ _pass () {
 				)
 				_describe -t commands 'pass git' subcommands
 				;;
+			${~ext})
+				_pass-$cmd "$@"
+				;;
 			show|*)
 				_pass_cmd_show
 				;;
@@ -106,6 +118,7 @@ _pass () {
 			"version:Output version information"
 			"help:Output help message"
 		)
+		subcommands+=($_pass_extensions)
 		_describe -t commands 'pass' subcommands
 		_arguments : \
 			"--version[Output version information]" \
@@ -141,4 +154,34 @@ _pass_complete_keys () {
 	_values 'gpg keys' $(gpg2 --list-secret-keys --with-colons | cut -d : -f 10 | sort -u | sed '/^$/d')
 }
 
+# Load any _pass-* definitions so that they may be completed as extensions.
+local -a _pass_extensions_names
+declare -gUa _pass_extensions
+_pass_extensions_names=()
+_pass_extensions=()
+
+local file input
+for file in ${^fpath}/_pass-*(-.N); do
+	local name=${${file:t}#_pass-}
+	if (( $+_pass_extensions[$name] )); then
+		continue
+	fi
+
+	local desc=
+	integer i=1
+	while read input; do
+		if (( i == 2 )); then
+			if [[ $input == '#description '* ]]; then
+				desc=:${input#\#description }
+			fi
+			break
+		fi
+		(( i++ ))
+	done < $file
+
+	_pass_extensions+=$name$desc
+	_pass_extensions_names+=$name
+done
+ext="(${(j.|.)_pass_extensions_names})"
+
 _pass
-- 
2.19.2



More information about the Password-Store mailing list