Skip to content

Commit 91e59af

Browse files
committed
tools: add zsh completion for RMLVO components
To spare parsing yaml output in zsh code, these completers rely on yq to parse the xkbcli output. If yq is not installed, the completion should fail silently (similar to no available completions), or display a message that yq is required for completions if the zsh user has messages enabled. The model completer includes a fallback parser using sed, we could possibly add more fallbacks for the other RMLVO completions.
1 parent ac11a85 commit 91e59af

File tree

6 files changed

+115
-5
lines changed

6 files changed

+115
-5
lines changed

meson.build

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,18 @@ if build_tools
570570
install_data('tools/xkbcli-zsh-completion.zsh',
571571
rename: '_xkbcli',
572572
install_dir: zsh_completion_path)
573+
install_data('tools/xkb-model-zsh-completion.zsh',
574+
rename: '_xkb_model',
575+
install_dir: zsh_completion_path)
576+
install_data('tools/xkb-layout-zsh-completion.zsh',
577+
rename: '_xkb_layout',
578+
install_dir: zsh_completion_path)
579+
install_data('tools/xkb-variant-zsh-completion.zsh',
580+
rename: '_xkb_variant',
581+
install_dir: zsh_completion_path)
582+
install_data('tools/xkb-options-zsh-completion.zsh',
583+
rename: '_xkb_options',
584+
install_dir: zsh_completion_path)
573585
endif
574586

575587
# Tool: compile-keymap
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#compdef -value-,XKB_DEFAULT_LAYOUT,-default-
2+
3+
local layouts_yaml
4+
layouts_yaml="$(_call_program -l xkb-yaml xkbcli list)"
5+
6+
case $status in
7+
127 ) _message "xkb layout completion requires xkbcli" && return 127 ;;
8+
<1->) _message "error listing xkb layouts" && return 1 ;;
9+
esac
10+
11+
local yq_prog='.layouts[] | select(.variant == "") | "\(.layout):\(.description)"'
12+
local -a layouts
13+
layouts=( ${(@f)"$(printf '%s\n' $layouts_yaml | _call_program -l xkb-parse-yaml yq ${(q)yq_prog})"} )
14+
15+
case $status in
16+
127 ) _message "xkb layout completion requires yq" && return 127 ;;
17+
<1->) _message "error completing xkb layouts" && return 1 ;;
18+
esac
19+
20+
_describe -t xkb-layout 'xkb layout' layouts "$@"

tools/xkb-model-zsh-completion.zsh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#compdef -value-,XKB_DEFAULT_MODEL,-default-
2+
3+
local models_yaml
4+
models_yaml="$(_call_program -l xkb-yaml xkbcli list)"
5+
6+
case $status in
7+
127 ) _message "xkb model completion requires xkbcli" && return 127 ;;
8+
<1->) _message "error listing xkb models" && return 1 ;;
9+
esac
10+
11+
local yq_prog='.models[] | "\(.name):\(.description)"'
12+
local -a models
13+
models=( ${(@f)"$(printf '%s\n' $models_yaml | _call_program -l xkb-parse-yaml yq ${(q)yq_prog})"} )
14+
case $status in
15+
127)
16+
local sed_models='/^models:/,/^$/p' sed_names='s/- name: //p' sed_descriptions='s/ description: //p'
17+
local models_sed="$(printf '%s\n' $models_yaml | _call_program -l sed sed -n ${(q)sed_models})"
18+
local -a model_names=( ${(@f)"$(printf '%s\n' $models_sed | _call_program -l sed-names sed -n ${(q)sed_names})"})
19+
local -a model_descriptions=( ${(@f)"$(printf '%s\n' $models_sed | _call_program -l sed-models sed -n ${(q)sed_descriptions})"})
20+
printf -v models '%s:%s' ${model_names:^model_descriptions}
21+
;;
22+
23+
<1->) _message "error completing xkb models" && return 1 ;;
24+
esac
25+
26+
_describe -t xkb-model 'xkb model' models "$@"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#compdef -value-,XKB_DEFAULT_OPTIONS,-default-
2+
3+
local options_yaml
4+
options_yaml="$(_call_program -l xkb-yaml xkbcli list)"
5+
6+
case $status in
7+
127 ) _message "xkb option completion requires xkbcli" && return 127 ;;
8+
<1->) _message "error listing xkb options" && return 1 ;;
9+
esac
10+
11+
local yq_prog='.option_groups[] | "\(.name)[\(.description)]: :->option-\(.name)"'
12+
local -a option_groups
13+
option_groups=( ${(@f)"$(printf '%s\n' $options_yaml | _call_program -l xkb-parse-yaml yq ${(q)yq_prog})"} )
14+
15+
case $status in
16+
127 ) _message "xkb option completion requires yq" && return 127 ;;
17+
<1->) _message "error completing xkb options" && return 1 ;;
18+
esac
19+
20+
local context state state_descr line
21+
typeset -A val_args
22+
23+
_values -S: 'xkb option' ${option_groups:s/Compose key/compose}
24+
if [[ $state == option-* ]]; then
25+
local -a xkb_options
26+
yq_prog='.option_groups[] | select(.name == "'${state#option-}'").options[] | "\(.name):\(.description)"'
27+
xkb_options=( ${(@f)"$(printf '%s\n' $options_yaml | _call_program -l xkb-parse-yaml yq ${(q)yq_prog})"} )
28+
xkb_options=( ${xkb_options#*:} )
29+
_describe -t xkb-option 'xkb option' xkb_options
30+
fi
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#compdef -value-,XKB_DEFAULT_VARIANT,-default-
2+
3+
local variants_yaml
4+
variants_yaml="$(_call_program -l xkb-yaml xkbcli list)"
5+
6+
case $status in
7+
127 ) _message "xkb model completion requires xkbcli" && return 127 ;;
8+
<1->) _message "error listing xkb variants" && return 1 ;;
9+
esac
10+
11+
local yq_prog1='.layouts[] | select(.variant != "") | "\(.layout)(\(.variant)):\(.description)"'
12+
local yq_prog2='.layouts[] | select(.variant != "") | "\(.variant):\(.description)"'
13+
local -a variants1 variants2
14+
variants1=( ${(@f)"$(printf '%s\n' $variants_yaml | _call_program -l xkb-parse-yaml yq ${(q)yq_prog1})"} )
15+
variants2=( ${(@f)"$(printf '%s\n' $variants_yaml | _call_program -l xkb-parse-yaml yq ${(q)yq_prog2})"} )
16+
17+
case $status in
18+
127 ) _message "xkb model completion requires yq" && return 127 ;;
19+
<1->) _message "error completing xkb variants" && return 1 ;;
20+
esac
21+
22+
_describe -t xkb-model 'xkb model' variants1 variants2 "$@"

tools/xkbcli-zsh-completion.zsh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ _xkbcli_commands() {
2323
}
2424

2525
local -a rmlvo_opts_common=(
26-
'--rules=[the XKB ruleset]:rules:()'
27-
'--model=[the XKB model]:model:()'
28-
'--layout=[the XKB layout]:layout:()'
29-
'--variant=[the XKB variant]:variant:()'
30-
'--options=[the XKB options]:options:()'
26+
'--rules=[the XKB ruleset]:rules:(base evdev)'
27+
'--model=[the XKB model]:model:_xkb_model'
28+
'--layout=[the XKB layout]:layout:_xkb_layout'
29+
'--variant=[the XKB variant]:variant:_xkb_variant'
30+
'--options=[the XKB options]:options:_xkb_options'
3131
'--enable-environment-names[set the default RMLVO values from the environment]'
3232
)
3333

0 commit comments

Comments
 (0)