[pve-devel] [PATCH v2 common] Fix 1891: Add zsh command completion generator

Christian Ebner c.ebner at proxmox.com
Wed Feb 20 11:59:14 CET 2019


This adds the function needed to generate the zsh autocompletion scripts.
Using the bash completion code path, this generates the list of possible
completions and adds them to the zsh completion by compadd.
For the autocompletion scripts to be loaded automatically, the following two
lines have to be placed in the .zshrc:

autoload -U compinit
compinit

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
Version 2:
    * Thanks to the input and offline discussion with Thomas this now relies on
      the new completion system with compadd (although we only use it to wrap to
      the perl bashcompletion).
    * For it to work

      autoload -U compinit
      compinit

      have to be placed into .zshrc

 src/PVE/CLIHandler.pm | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/PVE/CLIHandler.pm b/src/PVE/CLIHandler.pm
index 2107108..2f607cd 100644
--- a/src/PVE/CLIHandler.pm
+++ b/src/PVE/CLIHandler.pm
@@ -490,6 +490,30 @@ complete -o default -C '$exename bashcomplete' $exename
 __EOD__
 }
 
+sub generate_zsh_completions {
+    my ($class) = @_;
+
+    # generate zsh completion config
+
+    $exename = &$get_exe_name($class);
+
+    print <<__EOD__;
+#compdef _$exename $exename
+
+function _$exename() {
+    local cwords line point cmd curr prev
+    cwords=\${#words[@]}
+    line=\$words
+    point=\${#line}
+    cmd=\${words[1]}
+    curr=\${words[cwords]}
+    prev=\${words[cwords-1]}
+    compadd \$(COMP_CWORD="\$cwords" COMP_LINE="\$line" COMP_POINT="\$point" \\
+	$exename bashcomplete "\$cmd" "\$curr" "\$prev")
+}
+__EOD__
+}
+
 sub generate_asciidoc_synopsys {
     my ($class) = @_;
     $class->generate_asciidoc_synopsis();
-- 
2.11.0




More information about the pve-devel mailing list