xkbcli-zsh-completion.zsh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #compdef xkbcli -P xkbcli-*
  2. local context state state_descr line
  3. typeset -A opt_args
  4. local expl
  5. local -a modifiers=(
  6. "Shift"
  7. "Lock"
  8. "Control"
  9. "Mod1"
  10. "Mod2"
  11. "Mod3"
  12. "Mod4"
  13. "Mod5"
  14. "Alt"
  15. "Hyper"
  16. "LevelThree"
  17. "LevelFive"
  18. "Meta"
  19. "NumLock"
  20. "ScrollLock"
  21. "Super"
  22. )
  23. _xkbcli_modifiers_mapping() {
  24. local expl
  25. compset -P '*,'
  26. # TODO: don't complete duplicate mods in map?
  27. if compset -P '*:'; then
  28. _wanted modifier expl "modifier" compadd "$@" -S, -q -a - modifiers
  29. else
  30. compset -P '*+'
  31. _wanted modifier expl "modifier" compadd "$@" -S+ -r "+:\-" -a - modifiers
  32. fi
  33. }
  34. _xkbcli_modifiers_mask() {
  35. _values -s+ "modifiers" $modifiers
  36. }
  37. _xkbcli_layout_mapping() {
  38. compset -P '*,'
  39. _numbers
  40. }
  41. _xkbcli_controls() {
  42. local -a pm=(
  43. '+:Enable a control'
  44. '-:Disable a control'
  45. )
  46. local -a controls=(
  47. "sticky-keys:sticky-keys accessibility feature"
  48. "sticky-keys-latch-to-lock:latch-to-lock option for sticky-keys"
  49. "sticky-keys-no-simultaneous:deactivate sticky-keys if 2 keys or more are pressed simultaneously"
  50. "latch-simultaneous:relax the strict tapping sequence requirement for operating key latches"
  51. "overlay1:Overlay 1"
  52. "overlay2:Overlay 2"
  53. "overlay3:Overlay 3"
  54. "overlay4:Overlay 4"
  55. "overlay5:Overlay 5"
  56. "overlay6:Overlay 6"
  57. "overlay7:Overlay 7"
  58. "overlay8:Overlay 8"
  59. )
  60. local -a completed_controls=( ${${(s-,-)${words[CURRENT]#*=}}} )
  61. local -a remaining_controls=( ${controls:#(${(~j.|.)completed_controls#[+-]}):*} )
  62. (( $#remaining_controls )) || return 0;
  63. compset -P '*,'
  64. if compset -P '[+-]'; then
  65. _describe "xkb controls" remaining_controls -S "," -q
  66. else
  67. _alternative \
  68. 'action:action:{_describe "action" pm -S ""}' \
  69. 'xkb-control:xkb controls:{_describe "xkb controls" remaining_controls -S "," -q}'
  70. fi
  71. }
  72. _xkbcli_commands() {
  73. local -a commands=(
  74. 'list:list available rules, models, layouts, variants and options'
  75. 'info:print information about libxkbcommon configuration'
  76. 'interactive:interactive debugger for XKB keymaps'
  77. 'interactive-wayland:interactive debugger for XKB keymaps for Wayland'
  78. 'interactive-x11:interactive debugger for XKB keymaps for X11'
  79. 'interactive-evdev:interactive debugger for XKB keymaps for evdev'
  80. 'dump-keymap:dump an XKB keymap from a Wayland or X11 compositor'
  81. 'dump-keymap-wayland:dump an XKB keymap from a Wayland compositor'
  82. 'dump-keymap-x11:dump an XKB keymap from an X server'
  83. 'compile-keymap:compile an XKB keymap'
  84. 'compile-compose:compile a Compose file'
  85. 'how-to-type:print key sequences to type a Unicode codepoint'
  86. )
  87. _describe -t xkbcli-command 'xkbcli command' commands
  88. }
  89. local -a rmlvo_opts_common=(
  90. '(--keymap)--rules=[the XKB ruleset]:rules:(base evdev)'
  91. '(--keymap)--model=[the XKB model]:model:_xkb_model'
  92. '(--keymap)--layout=[the XKB layout]:layout:_xkb_layout'
  93. '(--keymap)--variant=[the XKB variant]:variant:_xkb_variant'
  94. '(--keymap)--options=[the XKB options]:options:_xkb_options'
  95. '(--keymap)--enable-environment-names[set the default RMLVO values from the environment]'
  96. )
  97. _xkbcli-list() {
  98. _arguments -S : \
  99. '(-v --verbose)'{-v,--verbose}'[increase verbosity]' \
  100. '(-h --help)'{-h,--help}'[print a help message and exit]' \
  101. '(-V --version)'{-V,--version}'[print version information and exit]' \
  102. '--ruleset=[load a ruleset]' \
  103. '--skip-default-paths[do not load the default XKB paths]' \
  104. '--load-exotic[load the exotic (extra) rulesets]' \
  105. '*:xkb base directory:_files -/'
  106. }
  107. local -a interactive_common=(
  108. '(-h --help)'{-h,--help}'[print a help message and exit]'
  109. '(-V --version)'{-V,--version}'[print version information and exit]'
  110. '--verbose[enable verbose debugging output]'
  111. '(-* --multiline -1 --uniline)'{-\*,--multiline}'[enable multiline event output]'
  112. '(-* --multiline -1 --uniline)'{-1,--uniline}'[enable uniline event output]'
  113. '--consumed-mode=[select the consumed modifiers mode]:mode:(xkb gtk)'
  114. '(--report-state-changes)--no-state-report[do not report changes to the state]'
  115. '--format=[use the given keymap format]:xkb format:(v1 v2)'
  116. '--strict[parse using the strict mode]'
  117. '--enable-compose[enable Compose]'
  118. '--legacy-state-api=[use the legacy state API instead of server machine API]:state:(true false)'
  119. '(--local-state --legacy-state-api)--modifiers-mapping=[use the given modifiers mapping]:modifiers mapping:_xkbcli_modifiers_mapping' \
  120. '(--local-state --legacy-state-api)--shortcuts-mask=[modifier mask to enable selecting a specific layout]:modifiers mask:_xkbcli_modifiers_mask' \
  121. '(--local-state --legacy-state-api)--shortcuts-mapping=[layout indices mapping to enable selective a specific layout]:layout mapping:_xkbcli_layout_mapping' \
  122. '(--local-state --legacy-state-api)--controls=[enable or disable controls]:xkb conbtrols:_xkbcli_controls' \
  123. )
  124. _xkbcli_keymap() {
  125. _alternative \
  126. 'stdin:stdin:((-:"read from stdin"))' \
  127. 'files:keymap file:_files'
  128. }
  129. _xkbcli-interactive() {
  130. _arguments -S : \
  131. '--local-state[enable local state handling and ignore modifiers/layouts]' \
  132. '(--local-state)--keymap=[use the given keymap instead of the compositor keymap]:keymap:_xkbcli_keymap' \
  133. "$interactive_common[@]"
  134. }
  135. _xkbcli-interactive-evdev() {
  136. _arguments -S : \
  137. '--short[shorter event output]' \
  138. '(--no-state-report)--report-state-changes[report changes to the state]' \
  139. '--without-x11-offset[do not add X11 keycode offset]' \
  140. '(--rules --model --layout --variant --options)--keymap=[use the given keymap]:keymap:_xkbcli_keymap' \
  141. "$interactive_common[@]" \
  142. "$rmlvo_opts_common[@]"
  143. }
  144. local -a dump_common=(
  145. '--verbose[enable verbose debugging output]'
  146. '(-h --help)'{-h,--help}'[print a help message and exit]'
  147. '(-V --version)'{-V,--version}'[print version information and exit]'
  148. '(--format)--input-format=[use the given input keymap format]:xkb format:(v1 v2)'
  149. '(--format)--output-format=[use the given output keymap format]:xkb format:(v1 v2)'
  150. '(--input-format --output-format)--format=[use the given keymap format for input and output]:xkb format:(v1 v2)'
  151. '--strict[parse using the strict mode]'
  152. '--no-pretty[do not pretty print when serializing a keymap]'
  153. '--drop-unused[disable unused bits serialization]'
  154. )
  155. _xkbcli-dump-keymap() {
  156. _arguments -S : \
  157. "$dump_common[@]"
  158. }
  159. _xkbcli-dump-keymap-wayland() {
  160. _arguments -S : \
  161. '--raw[dump raw keymap without parsing it]' \
  162. "$dump_common[@]"
  163. }
  164. _xkbcli-compile-keymap() {
  165. _arguments -S : \
  166. '(-h --help)'{-h,--help}'[print a help message and exit]' \
  167. '(-V --version)'{-V,--version}'[print version information and exit]' \
  168. '--verbose[enable verbose debugging output]' \
  169. '--test[test compilation but do not print the keymap]' \
  170. + input \
  171. '*--include[add the given path to the include path list]' \
  172. '--include-defaults[add the default set of include directories]' \
  173. '(--format)--input-format=[the keymap format to use for parsing]:xkb format:(v1 v2)' \
  174. '(--input-format --output-format)--format=[the keymap format to use for parsing and serializing]:xkb format:(v1 v2)' \
  175. '--strict[parse using the strict mode]' \
  176. '(--rules --model --layout --variant --options --enable-environment-names)--keymap=[use the given keymap file]:keymap:_xkbcli_keymap' \
  177. "$rmlvo_opts_common[@]" \
  178. + '(output)' \
  179. '(--format)--output-format=[the keymap format to use for serializing]:xkb format:(v1 v2)' \
  180. '--no-pretty[do not pretty print when serializing a keymap]' \
  181. '--drop-unused[disable unused bits serialization]' \
  182. '--explicit-defaults[force serializing defaults values]' \
  183. '--explicit-vmods[force serializing virtual modifiers encodings]' \
  184. '--explicit-keys[force serializing keys values]' \
  185. '--explicit-values[force serializing all values]' \
  186. '--layouts-mask[Hexadecimal mask of the layouts indices to select for serializing]:' \
  187. '(--kccgst-yaml --rmlvo --modmaps)--kccgst[print a keymap in KcCGST format]' \
  188. '(--kccgst --rmlvo --modmaps)--kccgst-yaml[print a KcCGST keymap in YAML format]' \
  189. '(--kccgst-yaml --modmaps)--rmlvo[print the full RMLVO in YAML format]' \
  190. '(--kccgst-yaml --rmlvo )--modmaps[print the real and virtual key modmaps in YAML format]'
  191. }
  192. _xkbcli-compile-compose() {
  193. _arguments -S : \
  194. '(-h --help)'{-h,--help}'[print a help message and exit]' \
  195. '(-V --version)'{-V,--version}'[print version information and exit]' \
  196. '--verbose[enable verbose debugging output]' \
  197. '--test[test compilation but do not print the Compose file]' \
  198. '--locale=[use the specified locale]:locale:_locales'
  199. }
  200. _xkbcli-keysyms() {
  201. local include="/usr/include/xkbcommon/xkbcommon-keysyms.h"
  202. local -Ua keysyms=(${(@f)"$(_call_program -l keysym grep -Pwo 'XKB_KEY_\\K\\w+' $include)"})
  203. _wanted keysym expl 'keysym' compadd -a "$@" - keysyms
  204. }
  205. _xkbcli-how-to-type() {
  206. local context state state_descr line
  207. typeset -A opt_args
  208. local ret=1
  209. _arguments -S : \
  210. '(-h --help)'{-h,--help}'[print a help message and exit]' \
  211. '(-V --version)'{-V,--version}'[print version information and exit]' \
  212. '--verbose[enable verbose debugging output]' \
  213. '--keysym[treat the argument only as a keysym]' \
  214. '--disable-compose[disable Compose support]' \
  215. + xkb \
  216. '--format=[the keymap format to use]:xkb format:(v1 v2)' \
  217. '(--rules --model --layout --variant --options)--keymap=[the keymap file to load]:xkb keymap:_xkbcli_keymap' \
  218. "$rmlvo_opts_common[@]" \
  219. ': :->key' && ret=0
  220. case $state in
  221. key)
  222. if (( $+opt_args[--keysym] )); then
  223. _xkbcli-keysyms && ret=0
  224. else
  225. _alternative \
  226. 'character:character:()' \
  227. 'codepoint:codepoint:()' \
  228. 'keysym:keysym:_xkbcli-keysyms' && ret=0
  229. fi
  230. ;;
  231. esac
  232. return ret
  233. }
  234. local ret=1
  235. case $service in
  236. xkbcli-interactive-(wayland|x11))
  237. _xkbcli-interactive
  238. return
  239. ;;
  240. xkbcli-dump-keymap-x11)
  241. _xkbcli-dump-keymap
  242. return
  243. ;;
  244. xkbcli-*)
  245. _call_function ret "_$service"
  246. return ret
  247. ;;
  248. xkbcli)
  249. _arguments -S : \
  250. '(-)'{-h,--help}'[show a help message and exit]' \
  251. '(-)'{-V,--version}'[show version info and exit]' \
  252. '(-): :->command' \
  253. '(-)*:: :->option-or-argument' && ret=0
  254. ;;
  255. esac
  256. case $state in
  257. command)
  258. _xkbcli_commands && ret=0
  259. ;;
  260. option-or-argument)
  261. local curcontext=${curcontext%:*:*}:xkbcli-$words[1]:
  262. case $words[1] in
  263. list)
  264. _xkbcli-list && ret=0 ;;
  265. interactive(|-wayland|-x11))
  266. _xkbcli-interactive && ret=0 ;;
  267. interactive-evdev)
  268. _xkbcli-interactive-evdev && ret=0 ;;
  269. dump-keymap(|-x11))
  270. _xkbcli-dump-keymap && ret=0 ;;
  271. dump-keymap-wayland)
  272. _xkbcli-dump-keymap-wayland && ret=0 ;;
  273. compile-keymap)
  274. _xkbcli-compile-keymap && ret=0 ;;
  275. compile-compose)
  276. _xkbcli-compile-compose && ret=0 ;;
  277. how-to-type)
  278. _xkbcli-how-to-type && ret=0 ;;
  279. *)
  280. _message "unknown $service command ${(qq)words[1]}"
  281. ;;
  282. esac
  283. ;;
  284. esac
  285. return ret