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\)$" ) )
295 local options="--create-folder --folder= --keep --no-hooks ${_notmuch_shared_options}"
297 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
301 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
304 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
307 # handle tags with colons
308 __ltrim_colon_completions "${cur}"
313 local cur prev words cword split
314 _init_completion || return
318 local options="--no-hooks --quiet ${_notmuch_shared_options}"
320 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
327 local cur prev words cword split
328 _init_completion -s || return
333 COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
337 COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
345 local options="--format= --format-version= --reply-to= --decrypt ${_notmuch_shared_options}"
347 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
350 _notmuch_search_terms
357 local cur prev words cword split
358 _init_completion -s || return
363 COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
375 local options="--format= --accumulate --input= ${_notmuch_shared_options}"
377 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
384 local cur prev words cword split
385 _init_completion -s || return
390 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
394 COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
398 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
402 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
410 local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
412 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
415 _notmuch_search_terms
422 local cur prev words cword split
423 _init_completion -s || return
428 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
432 COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
436 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
440 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
444 COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
452 local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
454 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
457 _notmuch_search_terms
464 local cur prev words cword split
465 _init_completion -s || return
470 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
474 COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
478 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
486 local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html ${_notmuch_shared_options}"
488 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
491 _notmuch_search_terms
498 local cur prev words cword split
499 # handle tags with colons and equal signs
500 _init_completion -s -n := || return
513 local options="--batch --input= --remove-all ${_notmuch_shared_options}"
515 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
519 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
522 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
525 _notmuch_search_terms
529 # handle tags with colons
530 __ltrim_colon_completions "${cur}"
535 local _notmuch_commands="compact config count dump help insert new reply restore search address setup show tag emacs-mua"
536 local arg cur prev words cword split
538 # require bash-completion with _init_completion
539 type -t _init_completion >/dev/null 2>&1 || return
541 _init_completion || return
548 # complete --help option like the subcommand
549 if [ -z "${arg}" -a "${prev}" = "--help" ]; then
553 if [ -z "${arg}" ]; then
554 # top level completion
557 # XXX: handle ${_notmuch_shared_options} and --config=
558 local options="--help --version"
559 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
562 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
565 elif [ "${arg}" = "help" ]; then
566 # handle help command specially due to _notmuch_commands usage
567 local help_topics="$_notmuch_commands hooks search-terms"
568 COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
570 # complete using _notmuch_subcommand if one exist
571 local completion_func="_notmuch_${arg//-/_}"
572 declare -f $completion_func >/dev/null && $completion_func
575 complete -F _notmuch notmuch