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" -- "${cur}" ) )
299 local options="--create-folder --folder= --keep --no-hooks --try-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" -- "${cur}" ) )
331 local options="--no-hooks --try-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}" ) )
358 local options="--format= --format-version= --reply-to= --decrypt ${_notmuch_shared_options}"
360 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
363 _notmuch_search_terms
370 local cur prev words cword split
371 _init_completion -s || return
376 COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
388 local options="--format= --accumulate --input= ${_notmuch_shared_options}"
390 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
397 local cur prev words cword split
398 _init_completion -s || return
403 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
407 COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
411 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
415 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
423 local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
425 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
428 _notmuch_search_terms
435 local cur prev words cword split
436 _init_completion -s || return
441 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
449 local options="--try-decrypt= ${_notmuch_shared_options}"
451 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
454 _notmuch_search_terms
461 local cur prev words cword split
462 _init_completion -s || return
467 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
471 COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
475 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
479 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
483 COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
491 local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
493 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
496 _notmuch_search_terms
503 local cur prev words cword split
504 _init_completion -s || return
509 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
513 COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
517 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
525 local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html ${_notmuch_shared_options}"
527 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
530 _notmuch_search_terms
537 local cur prev words cword split
538 # handle tags with colons and equal signs
539 _init_completion -s -n := || return
552 local options="--batch --input= --remove-all ${_notmuch_shared_options}"
554 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
558 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
561 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
564 _notmuch_search_terms
568 # handle tags with colons
569 __ltrim_colon_completions "${cur}"
574 local _notmuch_commands="compact config count dump help insert new reply restore reindex search address setup show tag emacs-mua"
575 local arg cur prev words cword split
577 # require bash-completion with _init_completion
578 type -t _init_completion >/dev/null 2>&1 || return
580 _init_completion || return
587 # complete --help option like the subcommand
588 if [ -z "${arg}" -a "${prev}" = "--help" ]; then
592 if [ -z "${arg}" ]; then
593 # top level completion
596 # XXX: handle ${_notmuch_shared_options} and --config=
597 local options="--help --version"
598 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
601 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
604 elif [ "${arg}" = "help" ]; then
605 # handle help command specially due to _notmuch_commands usage
606 local help_topics="$_notmuch_commands hooks search-terms"
607 COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
609 # complete using _notmuch_subcommand if one exist
610 local completion_func="_notmuch_${arg//-/_}"
611 declare -f $completion_func >/dev/null && $completion_func
614 complete -F _notmuch notmuch