1 # bash completion for notmuch -*- shell-script -*-
3 # Copyright © 2013 Jani Nikula
5 # Based on the bash-completion package:
6 # https://github.com/scop/bash-completion
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see https://www.gnu.org/licenses/ .
21 # Author: Jani Nikula <jani@nikula.org>
26 # Add space after an --option without parameter (e.g. reply --decrypt)
30 _notmuch_shared_options="--help --uuid= --version"
32 # $1: current input of the form prefix:partialinput, where prefix is
36 local output prefix cur
41 # Cut the input to be completed at punctuation because
42 # (apparently) Xapian does not support the trailing wildcard '*'
43 # operator for input with punctuation. We let compgen handle the
44 # extra filtering required.
45 cur="${cur%%[^a-zA-Z0-9]*}"
48 # Note: It would be more accurate and less surprising to have
49 # output=recipients here for to: addresses, but as gathering
50 # the recipient addresses requires disk access for each
51 # matching message, this becomes prohibitively slow.
52 to|from) output=sender;;
56 # Only emit plain, lower case, unique addresses.
57 notmuch address --output=$output $prefix:"${cur}*" | \
58 sed 's/[^<]*<\([^>]*\)>/\1/' | tr "[:upper:]" "[:lower:]" | sort -u
63 # use mime types from mime-support package if available, and fall
64 # back to a handful of common ones otherwise
65 if [ -r "/etc/mime.types" ]; then
66 sed -n '/^[[:alpha:]]/{s/[[:space:]].*//;p;}' /etc/mime.types
89 _notmuch_search_terms()
91 local cur prev words cword split
92 # handle search prefixes and tags with colons and equal signs
93 _init_completion -n := || return
97 COMPREPLY=( $(compgen -P "tag:" -W "`notmuch search --output=tags \*`" -- ${cur##tag:}) )
100 COMPREPLY=( $(compgen -P "to:" -W "`_notmuch_email ${cur}`" -- ${cur##to:}) )
103 COMPREPLY=( $(compgen -P "from:" -W "`_notmuch_email ${cur}`" -- ${cur##from:}) )
106 local path=`notmuch config get database.path`
108 COMPREPLY=( $(compgen -d "$path/${cur##path:}" | sed "s|^$path/||" ) )
111 local path=`notmuch config get database.path`
113 COMPREPLY=( $(compgen -d "$path/${cur##folder:}" | \
114 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
118 COMPREPLY=( $(compgen -P "mimetype:" -W "`_notmuch_mimetype ${cur}`" -- ${cur##mimetype:}) )
122 COMPREPLY=( $(compgen -P "query:" -W "`notmuch config list | sed -n '/^query\./s/^query\.\([^=]*\)=.*/\1/p'`" -- ${cur##query:}) )
125 local search_terms="from: to: subject: attachment: mimetype: tag: id: thread: folder: path: date: lastmod: query: property:"
127 COMPREPLY=( $(compgen -W "${search_terms}" -- ${cur}) )
130 # handle search prefixes and tags with colons
131 __ltrim_colon_completions "${cur}"
136 local cur prev words cword split
137 _init_completion -s || return
150 local options="--backup= --quiet ${_notmuch_shared_options}"
152 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
159 local cur prev words cword split
160 _init_completion || return
164 COMPREPLY=( $(compgen -W "get set list" -- ${cur}) )
167 COMPREPLY=( $(compgen -W "`notmuch config list | sed 's/=.*\$//'`" -- ${cur}) )
169 # these will also complete on config get, but we don't care
173 maildir.synchronize_flags)
174 COMPREPLY=( $(compgen -W "true false" -- ${cur}) )
181 local cur prev words cword split
182 _init_completion -s || return
187 COMPREPLY=( $( compgen -W "messages threads files" -- "${cur}" ) )
191 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
203 local options="--output= --exclude= --batch --input= --lastmod ${_notmuch_shared_options}"
205 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
208 _notmuch_search_terms
215 local cur prev words cword split
216 _init_completion -s || return
221 COMPREPLY=( $( compgen -W "sup batch-tag" -- "${cur}" ) )
233 local options="--gzip --format= --output= ${_notmuch_shared_options}"
235 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
238 _notmuch_search_terms
245 local cur prev words cword split
246 _init_completion -s || return
251 COMPREPLY=( $(compgen -W "`_notmuch_email to:${cur}`" -- ${cur}) )
263 local options="--subject= --to= --cc= --bcc= --body= --no-window-system --client --auto-daemon --create-frame --print --help --hello"
266 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
269 COMPREPLY=( $(compgen -W "`_notmuch_email to:${cur}`" -- ${cur}) )
277 local cur prev words cword split
278 # handle tags with colons and equal signs
279 _init_completion -s -n := || return
284 local path=`notmuch config get database.path`
286 COMPREPLY=( $(compgen -d "$path/${cur}" | \
287 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
291 COMPREPLY=( $( compgen -W "true false auto nostash" -- "${cur}" ) )
299 local options="--create-folder --folder= --keep --no-hooks --decrypt= ${_notmuch_shared_options}"
301 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
305 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
308 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
311 # handle tags with colons
312 __ltrim_colon_completions "${cur}"
317 local cur prev words cword split
318 _init_completion -s || return
323 COMPREPLY=( $( compgen -W "true false auto nostash" -- "${cur}" ) )
331 local options="--no-hooks --decrypt= --quiet ${_notmuch_shared_options}"
333 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
340 local cur prev words cword split
341 _init_completion -s || return
346 COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
350 COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
354 COMPREPLY=( $( compgen -W "true auto false" -- "${cur}" ) )
362 local options="--format= --format-version= --reply-to= --decrypt= ${_notmuch_shared_options}"
364 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
367 _notmuch_search_terms
374 local cur prev words cword split
375 _init_completion -s || return
380 COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
392 local options="--format= --accumulate --input= ${_notmuch_shared_options}"
394 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
401 local cur prev words cword split
402 _init_completion -s || return
407 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
411 COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
415 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
419 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
427 local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
429 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
432 _notmuch_search_terms
439 local cur prev words cword split
440 _init_completion -s || return
445 COMPREPLY=( $( compgen -W "true false auto nostash" -- "${cur}" ) )
453 local options="--decrypt= ${_notmuch_shared_options}"
455 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
458 _notmuch_search_terms
465 local cur prev words cword split
466 _init_completion -s || return
471 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
475 COMPREPLY=( $( compgen -W "sender recipients count address" -- "${cur}" ) )
479 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
483 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
487 COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
495 local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
497 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
500 _notmuch_search_terms
507 local cur prev words cword split
508 _init_completion -s || return
513 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
517 COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
521 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
525 COMPREPLY=( $( compgen -W "true auto false stash" -- "${cur}" ) )
533 local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt= --include-html ${_notmuch_shared_options}"
535 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
538 _notmuch_search_terms
545 local cur prev words cword split
546 # handle tags with colons and equal signs
547 _init_completion -s -n := || return
560 local options="--batch --input= --remove-all ${_notmuch_shared_options}"
562 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
566 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
569 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
572 _notmuch_search_terms
576 # handle tags with colons
577 __ltrim_colon_completions "${cur}"
582 local _notmuch_commands="compact config count dump help insert new reply restore reindex search address setup show tag emacs-mua"
583 local arg cur prev words cword split
585 # require bash-completion with _init_completion
586 type -t _init_completion >/dev/null 2>&1 || return
588 _init_completion || return
595 # complete --help option like the subcommand
596 if [ -z "${arg}" -a "${prev}" = "--help" ]; then
600 if [ -z "${arg}" ]; then
601 # top level completion
604 # XXX: handle ${_notmuch_shared_options} and --config=
605 local options="--help --version"
606 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
609 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
612 elif [ "${arg}" = "help" ]; then
613 # handle help command specially due to _notmuch_commands usage
614 local help_topics="$_notmuch_commands hooks search-terms properties"
615 COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
617 # complete using _notmuch_subcommand if one exist
618 local completion_func="_notmuch_${arg//-/_}"
619 declare -f $completion_func >/dev/null && $completion_func
622 complete -F _notmuch notmuch