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:}) )
121 local search_terms="from: to: subject: attachment: mimetype: tag: id: thread: folder: path: date: lastmod:"
123 COMPREPLY=( $(compgen -W "${search_terms}" -- ${cur}) )
126 # handle search prefixes and tags with colons
127 __ltrim_colon_completions "${cur}"
132 local cur prev words cword split
133 _init_completion -s || return
146 local options="--backup= --quiet ${_notmuch_shared_options}"
148 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
155 local cur prev words cword split
156 _init_completion || return
160 COMPREPLY=( $(compgen -W "get set list" -- ${cur}) )
163 COMPREPLY=( $(compgen -W "`notmuch config list | sed 's/=.*\$//'`" -- ${cur}) )
165 # these will also complete on config get, but we don't care
169 maildir.synchronize_flags)
170 COMPREPLY=( $(compgen -W "true false" -- ${cur}) )
177 local cur prev words cword split
178 _init_completion -s || return
183 COMPREPLY=( $( compgen -W "messages threads files" -- "${cur}" ) )
187 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
199 local options="--output= --exclude= --batch --input= --lastmod ${_notmuch_shared_options}"
201 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
204 _notmuch_search_terms
211 local cur prev words cword split
212 _init_completion -s || return
217 COMPREPLY=( $( compgen -W "sup batch-tag" -- "${cur}" ) )
229 local options="--gzip --format= --output= ${_notmuch_shared_options}"
231 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
234 _notmuch_search_terms
241 local cur prev words cword split
242 # handle tags with colons and equal signs
243 _init_completion -s -n := || return
248 local path=`notmuch config get database.path`
250 COMPREPLY=( $(compgen -d "$path/${cur}" | \
251 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
259 local options="--create-folder --folder= --keep --no-hooks ${_notmuch_shared_options}"
261 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
265 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
268 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
271 # handle tags with colons
272 __ltrim_colon_completions "${cur}"
277 local cur prev words cword split
278 _init_completion || return
282 local options="--no-hooks --quiet ${_notmuch_shared_options}"
284 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
291 local cur prev words cword split
292 _init_completion -s || return
297 COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
301 COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
309 local options="--format= --format-version= --reply-to= --decrypt ${_notmuch_shared_options}"
311 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
314 _notmuch_search_terms
321 local cur prev words cword split
322 _init_completion -s || return
327 COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
339 local options="--format= --accumulate --input= ${_notmuch_shared_options}"
341 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
348 local cur prev words cword split
349 _init_completion -s || return
354 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
358 COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
362 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
366 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
374 local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
376 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
379 _notmuch_search_terms
386 local cur prev words cword split
387 _init_completion -s || return
392 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
396 COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
400 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
404 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
408 COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
416 local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
418 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
421 _notmuch_search_terms
428 local cur prev words cword split
429 _init_completion -s || return
434 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
438 COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
442 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
450 local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html ${_notmuch_shared_options}"
452 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
455 _notmuch_search_terms
462 local cur prev words cword split
463 # handle tags with colons and equal signs
464 _init_completion -s -n := || return
477 local options="--batch --input= --remove-all ${_notmuch_shared_options}"
479 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
483 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
486 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
489 _notmuch_search_terms
493 # handle tags with colons
494 __ltrim_colon_completions "${cur}"
499 local _notmuch_commands="compact config count dump help insert new reply restore search address setup show tag"
500 local arg cur prev words cword split
502 # require bash-completion with _init_completion
503 type -t _init_completion >/dev/null 2>&1 || return
505 _init_completion || return
512 # complete --help option like the subcommand
513 if [ -z "${arg}" -a "${prev}" = "--help" ]; then
517 if [ -z "${arg}" ]; then
518 # top level completion
521 # XXX: handle ${_notmuch_shared_options} and --config=
522 local options="--help --version"
523 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
526 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
529 elif [ "${arg}" = "help" ]; then
530 # handle help command specially due to _notmuch_commands usage
531 local help_topics="$_notmuch_commands hooks search-terms"
532 COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
534 # complete using _notmuch_subcommand if one exist
535 local completion_func="_notmuch_${arg//-/_}"
536 declare -f $completion_func >/dev/null && $completion_func
539 complete -F _notmuch notmuch