Blob Blame History Raw
From 50bb352ccad52c5f56a457305f5422b1359cbf42 Mon Sep 17 00:00:00 2001
From: Eric Cook <llua@gmx.com>
Date: Fri, 29 May 2015 10:40:11 -0400
Subject: [PATCH] zsh-completion: a more style/tag aware _systemctl

using _wanted instead of calling compadd directly. this allows the user to customize
possible matches.

An example being, grouping units by type:
autoload -Uz compinit; compinit
zstyle ':completion:*' menu select
zstyle ':completion:*' group-name ''
zstyle ':completion:*' format 'Completing %d'
zstyle -e ':completion:*:*:systemctl-(((re|)en|dis)able|(*re|)start|reload*):*' \
tag-order 'local type; for type in service template target socket;
    reply+=( systemd-units:-${type}:${type} ); reply=( "$reply systemd-units:-misc:misc" )'
zstyle ':completion:*:systemd-units-template' ignored-patterns '^*@'
zstyle ':completion:*:systemd-units-target' ignored-patterns '^*.target'
zstyle ':completion:*:systemd-units-socket' ignored-patterns '^*.socket'
zstyle ':completion:*:systemd-units-service' ignored-patterns '^*.service'
zstyle ':completion:*:systemd-units-misc' ignored-patterns '*(@|.(service|socket|target))'

also, <poke> http://lists.freedesktop.org/archives/systemd-devel/2015-May/032012.html

(cherry picked from commit d34b7c117bd016cb9ef2c36d474c9a917924abda)
---
 shell-completion/zsh/_systemctl.in | 60 +++++++++++++++++++++++---------------
 1 file changed, 37 insertions(+), 23 deletions(-)

diff --git a/shell-completion/zsh/_systemctl.in b/shell-completion/zsh/_systemctl.in
index db9bdb60c5..17736de01c 100644
--- a/shell-completion/zsh/_systemctl.in
+++ b/shell-completion/zsh/_systemctl.in
@@ -65,7 +65,7 @@
   if (( CURRENT == 1 )); then
     _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
   else
-    local curcontext="$curcontext"
+    local curcontext="$curcontext" expl
 
     cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
     # Deal with any aliases
@@ -172,7 +172,8 @@ for fun in is-active is-failed is-enabled status show cat mask preset help list-
   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
   {
     _systemctl_really_all_units
-    compadd "$@" -a - _sys_really_all_units
+    _wanted systemd-units expl unit \
+      compadd "$@" -a - _sys_really_all_units
   }
 done
 
@@ -180,34 +181,39 @@ done
 (( $+functions[_systemctl_disable] )) || _systemctl_disable()
 {
     local _sys_unit_state; _systemctl_unit_state
-    compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
+    _wanted systemd-units expl 'enabled unit' \
+      compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
 }
 
 (( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
 {
     local _sys_unit_state; _systemctl_unit_state
-    compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
+    _wanted systemd-units expl 'enabled/disabled unit' \
+      compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
 }
 
 # Completion functions for DISABLED_UNITS
 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
 {
   local _sys_unit_state; _systemctl_unit_state
-  compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
+  _wanted systemd-units expl 'disabled unit' \
+    compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
 }
 
 # Completion functions for FAILED_UNITS
 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
 {
   local _sys_failed_units; _systemctl_failed_units
-  compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
+  _wanted systemd-units expl 'failed unit' \
+    compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
 }
 
 # Completion functions for STARTABLE_UNITS
 (( $+functions[_systemctl_start] )) || _systemctl_start()
 {
    local _sys_startable_units; _systemctl_startable_units
-   compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names)
+   _wanted systemd-units expl 'startable unit' \
+     compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names)
 }
 
 # Completion functions for STOPPABLE_UNITS
@@ -215,8 +221,9 @@ for fun in stop kill try-restart condrestart ; do
   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
   {
     local _sys_active_units; _systemctl_active_units
-    compadd "$@" - $( _filter_units_by_property CanStop yes \
-      ${_sys_active_units[*]} )
+    _wanted systemd-units expl 'stoppable unit' \
+      compadd "$@" - $( _filter_units_by_property CanStop yes \
+        ${_sys_active_units[*]} )
   }
 done
 
@@ -224,8 +231,9 @@ done
 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
 {
   _systemctl_all_units
-  compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
-    ${_sys_all_units[*]} )
+  _wanted systemd-units expl 'isolatable unit' \
+    compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
+      ${_sys_all_units[*]} )
 }
 
 # Completion functions for RELOADABLE_UNITS
@@ -233,8 +241,9 @@ for fun in reload reload-or-try-restart force-reload ; do
   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
   {
     local _sys_active_units; _systemctl_active_units
-    compadd "$@" - $( _filter_units_by_property CanReload yes \
-      ${_sys_active_units[*]} )
+    _wanted systemd-units expl 'reloadable unit' \
+      compadd "$@" - $( _filter_units_by_property CanReload yes \
+        ${_sys_active_units[*]} )
   }
 done
 
@@ -243,7 +252,8 @@ for fun in restart reload-or-restart ; do
   (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
   {
     local _sys_restartable_units; _systemctl_restartable_units
-    compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names)
+    _wanted systemd-units expl 'restartable unit' \
+      compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names)
   }
 done
 
@@ -251,28 +261,32 @@ done
 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
 {
   local _sys_unit_state; _systemctl_unit_state
-  compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
+  _wanted systemd-units expl 'masked unit' \
+    compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
 }
 
 # Completion functions for JOBS
 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
 {
-  compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
-    _message "no jobs found"
+  _wanted systemd-jobs expl job \
+    compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
+      _message "no jobs found"
 }
 
 # Completion functions for SNAPSHOTS
 (( $+functions[_systemctl_delete] )) || _systemctl_delete()
 {
-  compadd "$@" - ${${(f)"$(__systemctl list-units --type snapshot --all)"}%% *} ||
-    _message "no snapshots found"
+  _wanted systemd-snapshots expl snapshot \
+    compadd "$@" - ${${(f)"$(__systemctl list-units --type snapshot --all)"}%% *} ||
+      _message "no snapshots found"
 }
 
 # Completion functions for TARGETS
 (( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
 {
-  compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
-    _message "no targets found"
+  _wanted systemd-targets expl target \
+    compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
+      _message "no targets found"
 }
 
 # Completion functions for ENVS
@@ -284,8 +298,8 @@ for fun in set-environment unset-environment ; do
     if [[ "${fun}" = "set-environment" ]]; then
       suf='-S='
     fi
-
-    compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
+    _wanted systemd-environment expl 'environment variable' \
+      compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
   }
 done