unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Tobias Backer Dirks <omgitsaheadcrab@gmail.com>
To: notmuch@notmuchmail.org
Cc: Tobias Backer Dirks <omgitsaheadcrab@gmail.com>
Subject: [PATCH] cli: handle bash completion of all shared options
Date: Mon, 26 Apr 2021 19:12:20 +0100	[thread overview]
Message-ID: <20210426181220.1097563-1-omgitsaheadcrab@gmail.com> (raw)

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

             reply	other threads:[~2021-04-26 18:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-26 18:12 Tobias Backer Dirks [this message]
2021-05-02 10:35 ` [PATCH] cli: handle bash completion of all shared options David Bremner
2021-12-04 15:50 ` David Bremner
2021-12-25 16:58 ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210426181220.1097563-1-omgitsaheadcrab@gmail.com \
    --to=omgitsaheadcrab@gmail.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).