From 62e09984bf054e96cedb7eefaf0f59b35b270c07 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Fri, 12 Jan 2024 17:46:04 +0300 Subject: added support for Zsh autocompletions * actually autocompletions are much more powerful than on Bash --- Makefile | 12 ++++++++++-- README.md | 9 ++++++++- openrussian-completion.zsh | 12 ++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 openrussian-completion.zsh diff --git a/Makefile b/Makefile index 8d51ea3..f0bfb1b 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ PREFIX ?= /usr/local LUA ?= lua5.2 LUAC ?= luac5.2 -COMPLETIONSDIR ?= $(shell pkg-config --variable=completionsdir bash-completion) +BASH_COMPLETIONSDIR ?= $(shell pkg-config --variable=completionsdir bash-completion) +ZSH_COMPLETIONSDIR ?= $(PREFIX)/share/zsh/site-functions all : openrussian openrussian-sqlite3.db @@ -44,7 +45,14 @@ install : openrussian openrussian-sqlite3.db \ install openrussian $(DESTDIR)$(PREFIX)/bin mkdir -p $(DESTDIR)$(PREFIX)/share/openrussian cp openrussian-sqlite3.db $(DESTDIR)$(PREFIX)/share/openrussian - cp openrussian-completion.bash $(DESTDIR)$(COMPLETIONSDIR)/openrussian +ifneq ($(BASH_COMPLETIONSDIR),) + mkdir -p $(DESTDIR)$(BASH_COMPLETIONSDIR) + cp openrussian-completion.bash $(DESTDIR)$(BASH_COMPLETIONSDIR)/openrussian +endif +ifneq ($(ZSH_COMPLETIONSDIR),) + mkdir -p $(DESTDIR)$(ZSH_COMPLETIONSDIR) + cp openrussian-completion.zsh $(DESTDIR)$(ZSH_COMPLETIONSDIR)/_openrussian +endif mkdir -p $(DESTDIR)$(PREFIX)/man/man1 cp openrussian.1 $(DESTDIR)$(PREFIX)/man/man1 mandb || true diff --git a/README.md b/README.md index 89fd84d..af5e6f8 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ based on the database of [openrussian.org](https://en.openrussian.org/). pages ~~very easy~~ possible. * Puts very low requirements on the runtime environment: a black and white terminal display is sufficient. -* Bash auto completions supported. +* Auto completions supported for Bash and Zsh. Most of this is handled by the script's `-C` argument, so it is trivial to add support for other shells. Contributions are welcome! @@ -125,6 +125,13 @@ something like the following to your `~/.bash_completion`: alias ru='openrussian -Lde -Len' ру='openrussian -Lde -Len' complete -F _complete_alias ru ру +### Zsh Aliases + +Autocompletion will automatically work with aliases in the Zsh, so it is +sufficient to add the following to your `~/.zshrc`: + + alias ru='openrussian -Lde -Len' ру='openrussian -Lde -Len' + ## Examples A simple lookup: diff --git a/openrussian-completion.zsh b/openrussian-completion.zsh new file mode 100644 index 0000000..e3669c0 --- /dev/null +++ b/openrussian-completion.zsh @@ -0,0 +1,12 @@ +#compdef openrussian + +# FIXME: If supported by `-C`, we could even show translations in the completions' +# help strings. +_get_terms() { + compadd "${(@f)"$(openrussian -C "$words[-1]" 2>/dev/null)"}" +} + +_arguments '-Len[Generate English translations]' '-Lde[Generate German translations]' \ + '-V[Verbatim matching (no case folding and inflections)]' \ + '-p[Print Troff code to stdout]' \ + '*:term:_get_terms' -- cgit v1.2.3