Skip to content
This repository

zsh: rework ZSH completion #30

Open
wants to merge 1 commit into from

2 participants

Vincent Bernat Mislav Marohnić
Vincent Bernat

The completion for ZSH is upgraded to the new ZSH completion
system. To work, the file should be placed somewhere in ZSH fpath to
be autoloaded when needed and named _sub.

There are other embedded misc changes:

  • $CURRENT indicate which word is being autocompleted.
  • In ZSH, automatic word splitting is disabled in completion. There is no need to quote words.

The completion also handles plugins that are able to complete
args. However, most plugins don't and will complete with a subcommand
instead. I don't think there is an universal solution for this.

I am no ZSH wizard. Take this commit with care (works for me).

Vincent Bernat zsh: rework ZSH completion
The completion for ZSH is upgraded to the new ZSH completion
system. To work, the file should be placed somewhere in ZSH fpath to
be autoloaded when needed and named `_sub`.

There are other embedded misc changes:
 - `$CURRENT` indicate which word is being autocompleted.
 - In ZSH, automatic word splitting is disabled in completion. There
   is no need to quote words.
4976f83
Mislav Marohnić

Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 19, 2012
Vincent Bernat zsh: rework ZSH completion
The completion for ZSH is upgraded to the new ZSH completion
system. To work, the file should be placed somewhere in ZSH fpath to
be autoloaded when needed and named `_sub`.

There are other embedded misc changes:
 - `$CURRENT` indicate which word is being autocompleted.
 - In ZSH, automatic word splitting is disabled in completion. There
   is no need to quote words.
4976f83
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 20 additions and 17 deletions. Show diff stats Hide diff stats

  1. 37  completions/sub.zsh
37  completions/sub.zsh
... ...
@@ -1,19 +1,22 @@
1  
-if [[ ! -o interactive ]]; then
2  
-    return
3  
-fi
  1
+#compdef sub
4 2
 
5  
-compctl -K _sub sub
  3
+# This completion file should be linked to a directory in ZSH fpath
  4
+# and named `_sub`.
6 5
 
7  
-_sub() {
8  
-  local word words completions
9  
-  read -cA words
10  
-  word="${words[2]}"
11  
-
12  
-  if [ "${#words}" -eq 2 ]; then
13  
-    completions="$(sub commands)"
14  
-  else
15  
-    completions="$(sub completions "${word}")"
16  
-  fi
17  
-
18  
-  reply=("${(ps:\n:)completions}")
19  
-}
  6
+case $CURRENT in
  7
+    2)
  8
+        local cmds
  9
+        local -a commands
  10
+        cmds=$(sub commands)
  11
+        commands=(${(ps:\n:)cmds})
  12
+        _wanted command expl "sub command" compadd -a commands
  13
+        ;;
  14
+    *)
  15
+        local cmd subcmds
  16
+        local -a commands
  17
+        cmd=${words[2]}
  18
+        subcmds=$(sub completions ${words[2,$(($CURRENT - 1))]})
  19
+        commands=(${(ps:\n:)subcmds})
  20
+        _wanted subcommand expl "sub $cmd subcommand" compadd -a commands
  21
+        ;;
  22
+esac
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.