From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:8:6d80::]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 0DksGosCh2AlLAAAgWs5BA (envelope-from ) for ; Mon, 26 Apr 2021 20:12:27 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id 6Gu+FYsCh2CaTgAAbx9fmQ (envelope-from ) for ; Mon, 26 Apr 2021 18:12:27 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id BD9981ECB3 for ; Mon, 26 Apr 2021 20:12:26 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 19F2C27167; Mon, 26 Apr 2021 14:12:23 -0400 (EDT) Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by mail.notmuchmail.org (Postfix) with ESMTPS id CC7971FBD2 for ; Mon, 26 Apr 2021 14:12:19 -0400 (EDT) Received: by mail-wr1-x430.google.com with SMTP id r7so44854846wrm.1 for ; Mon, 26 Apr 2021 11:12:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=pd6sxuSsE0DKBC2x86ti/UV8uctpI8zN/mhgB/xXWz4=; b=TD+Q0N6unnktGCh7Q+kwKzMV9ukL1ic+pdkdQU7SSbMyniryL0XxqOMlS4kQdR0DAA 9DjKRQvyXSQLQAogx3btxwwGF4fP9h6EsXPlegQw4oQG3WjSSEfz9vOhwk0lp7lENt4P 2m/P/6/n816SIvS/lTqoL/OHbkEnENp5LA8mUWHNSdOCtgFIma9QoIJVYbRzaEebZedk ZCUJAhyb6Ya5zLv9RiG07UG2IhTH9LiDgNQgk7F73eCZVPZ54LVDy+5dOQLfjmyR2iF0 qUusQ0OUHOYmDi61v61Dcqx4CGgaN5cfVwG39/HoUTPyUvu7Ulz55LbbWfPBPcl++Btn 3gSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=pd6sxuSsE0DKBC2x86ti/UV8uctpI8zN/mhgB/xXWz4=; b=b/SKCYkZe6rilefDRjsDb5zwuJWXpLSJW9/p4GT7iippBrN77iktdcmPC61bPIySwt RH2k7RtfIQ1eGARXv2W7zimUnK/aJZOan4bwY0/tUjro09R5vpW1Ydu5nVj1ubKcliHw 3a1WiMtky7rieY4vUYuCrxxQDojWHYisS+8nCHWV9+KOS4O3QUbRYtN++E84xVAWTdei ekmsAkeoBq27rBk7NVR3F2jG5fm6J3ShKT8CFNo7kXuX9J3LLOafKGpLu0iewlowdyzT ONZwIMDYAU2YA2zoTwVQgoz2nX6B292EJLWLzCkA31Kgaq2a+QrXcgpKJF28m1jmbIQA SFCA== X-Gm-Message-State: AOAM531zoUlCmgQQYdmv64FppFC+4PA5haVUzVwU5Kj0y6/yvHFtec8i 6gF+4xwIUKobu8hcyYOoIFvSlG/To8wZMg== X-Google-Smtp-Source: ABdhPJzwwYyZ/1+wT27IdgK+J41Ji+/MJ2I91ODpvh4DSURdXlcBoNC1wMHRtcmQyMVHOsYFKVsTfg== X-Received: by 2002:adf:fc46:: with SMTP id e6mr16977486wrs.169.1619460738239; Mon, 26 Apr 2021 11:12:18 -0700 (PDT) Received: from localhost.localdomain ([90.255.50.46]) by smtp.gmail.com with ESMTPSA id v20sm1817174wmj.15.2021.04.26.11.12.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Apr 2021 11:12:17 -0700 (PDT) From: Tobias Backer Dirks To: notmuch@notmuchmail.org Subject: [PATCH] cli: handle bash completion of all shared options Date: Mon, 26 Apr 2021 19:12:20 +0100 Message-Id: <20210426181220.1097563-1-omgitsaheadcrab@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Message-ID-Hash: ZBFZRZ7BQHSHKNBKN66L3V3DK7NLKHD6 X-Message-ID-Hash: ZBFZRZ7BQHSHKNBKN66L3V3DK7NLKHD6 X-MailFrom: omgitsaheadcrab@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Tobias Backer Dirks X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1619460747; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=jr80Pu2uiED9fD4tHxzHGAlmQh5GXi+02hUPMj9PNSA=; b=aMMNczEQNmfHy90fzq3C3Q5w3l/1dwHYAFrcnzJPf3Vol+LynhFdikFoVh2w1mwVdYWQmT wIBJqSeMwbHJ4gbUS1e3b1nf9QJ/AfNKQmIcdAHhActLH/Xcyzb++5mIjBEfIH/9YJIDxP N1fg8JEJLg+ugovSnENQZuprXBRYsGjOtMgOJiy6GG74W39TYP8idNzrpxTblrMTwWLxe2 +s1jLld0WnuPgr0gCWtMZwM5hMy+V/uqaIjuuZYDf1bfvoKkxbJHmcL06/ffSQkmY7RVd7 h/kgYWhK5GJT7m51QdQIqmTywVq28YkjGoFxbbDpIkpxdn13jIV2Ts3CwMniQA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1619460747; a=rsa-sha256; cv=none; b=LvliGKvvAwX9+IgYTZ7oLsB8Xncyp4Vb9sWkMCKJHCKVnQWCqrAPgy3+bkPxCyUH9x9mVF anNno0AzdVaCBZwvhNNCTavAlPoaLxpsY/m5Ss8ElxaDIZTNKavYrdicSoGgUMr6BF9Jlr 4WmLZwzQKyrRyiDyzYlBjycVcb4P0OMki6Qn6bnafm8QUUBoQGi9aH8b8UP1pwypllPxAn T6jGBqD1wNl0uJA0uA3qE3LxDD8PhJVD9FA4dOjoOF8mIHAVIpEEivWZXwi7DFSJ8sunbi 4/wd3jmfWRF71vHTtFF0WgUclrcezzpD3tHyoBJMqjJJvK9iP8kgnZZWDKXIgw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20161025 header.b=TD+Q0N6u; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: 0.12 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20161025 header.b=TD+Q0N6u; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: BD9981ECB3 X-Spam-Score: 0.12 X-Migadu-Scanner: scn0.migadu.com X-TUID: 9UbjdYWlxmy3 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 --- 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