unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH] cli: handle bash completion of all shared options
@ 2021-04-26 18:12 Tobias Backer Dirks
  2021-05-02 10:35 ` David Bremner
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Tobias Backer Dirks @ 2021-04-26 18:12 UTC (permalink / raw)
  To: notmuch; +Cc: Tobias Backer Dirks

A slight rework of base bash completion to handle all shared options.
This simplifies the handling of base commands and adds creature comforts
such as directory completion for --config, and removing trailing spaces
from options ending in an `=`. Also fixes a bug reported in [1], wherein
completion after an `=` produced erroneous output.

[1]: id:20200602103703.GA13623@taurus.defre.kleine-koenig.org

Signed-off-by: Tobias Backer Dirks <omgitsaheadcrab@gmail.com>
---
 completion/notmuch-completion.bash | 74 ++++++++++++++++--------------
 1 file changed, 40 insertions(+), 34 deletions(-)

diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash
index 15425697..6d349081 100644
--- a/completion/notmuch-completion.bash
+++ b/completion/notmuch-completion.bash
@@ -27,7 +27,7 @@
 # on completion.
 #
 
-_notmuch_shared_options="--help --uuid= --version"
+_notmuch_shared_options="--config= --help --uuid= --version"
 
 # $1: current input of the form prefix:partialinput, where prefix is
 # to or from.
@@ -580,43 +580,49 @@ _notmuch_tag()
 _notmuch()
 {
     local _notmuch_commands="compact config count dump help insert new reply restore reindex search address setup show tag emacs-mua"
-    local arg cur prev words cword split
+    local cur prev split
 
-    # require bash-completion with _init_completion
-    type -t _init_completion >/dev/null 2>&1 || return
-
-    _init_completion || return
+    _init_completion -s || return
 
-    COMPREPLY=()
+    # handle _notmuch_shared_options
+    case $prev in
+	--version | --uuid)
+	    return
+            ;;
+	--help)
+	    local help_topics="$_notmuch_commands hooks search-terms properties"
+	    COMPREPLY=( $(compgen -W "${help_topics}" -- "$cur") )
+	    return
+	    ;;
+	--config)
+	    _filedir
+            COMPREPLY=("${COMPREPLY[@]/#/}")
+	    return
+	    ;;
+    esac
 
-    # subcommand
-    _get_first_arg
+    $split && return
 
-    # complete --help option like the subcommand
-    if [ -z "${arg}" -a "${prev}" = "--help" ]; then
-	arg="help"
-    fi
+    case $COMP_CWORD in
+	1) # handle top level completions
+	    COMPREPLY=($(compgen -W "$_notmuch_commands" -- "$cur"))
 
-    if [ -z "${arg}" ]; then
-	# top level completion
-	case "${cur}" in
-	    -*)
-		# XXX: handle ${_notmuch_shared_options} and --config=
-		local options="--help --version"
-		COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
-		;;
-	    *)
-		COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
-		;;
-	esac
-    elif [ "${arg}" = "help" ]; then
-	# handle help command specially due to _notmuch_commands usage
-	local help_topics="$_notmuch_commands hooks search-terms properties"
-	COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
-    else
-	# complete using _notmuch_subcommand if one exist
-	local completion_func="_notmuch_${arg//-/_}"
-	declare -f $completion_func >/dev/null && $completion_func
-    fi
+	    # complete _notmuch_shared_options
+	    if [[ "$cur" == -* ]]; then
+		COMPREPLY=($(compgen -W "$_notmuch_shared_options" -- "$cur"))
+		# if completion ends in `=` do not add a space
+		[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
+		return
+	    fi
+	    ;;
+	2) # handle second level completions
+	    local completion_func prev
+	    prev=${COMP_WORDS[COMP_CWORD-1]}
+	    completion_func="_notmuch_${prev}"
+	    # try use a completion function if available
+	    declare -f "$completion_func" >/dev/null && "$completion_func"
+	    return
+	    ;;
+    esac
 } &&
 complete -F _notmuch notmuch
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-12-25 16:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-26 18:12 [PATCH] cli: handle bash completion of all shared options Tobias Backer Dirks
2021-05-02 10:35 ` David Bremner
2021-12-04 15:50 ` David Bremner
2021-12-25 16:58 ` David Bremner

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).