1 # bash completion for notmuch -*- shell-script -*-
3 # Copyright © 2013 Jani Nikula
5 # Based on the bash-completion package:
6 # http://bash-completion.alioth.debian.org/
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 http://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
61 _notmuch_search_terms()
63 local cur prev words cword split
64 # handle search prefixes and tags with colons and equal signs
65 _init_completion -n := || return
69 COMPREPLY=( $(compgen -P "tag:" -W "`notmuch search --output=tags \*`" -- ${cur##tag:}) )
72 COMPREPLY=( $(compgen -P "to:" -W "`_notmuch_email ${cur}`" -- ${cur##to:}) )
75 COMPREPLY=( $(compgen -P "from:" -W "`_notmuch_email ${cur}`" -- ${cur##from:}) )
78 local path=`notmuch config get database.path`
80 COMPREPLY=( $(compgen -d "$path/${cur##path:}" | sed "s|^$path/||" ) )
83 local path=`notmuch config get database.path`
85 COMPREPLY=( $(compgen -d "$path/${cur##folder:}" | \
86 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
89 local search_terms="from: to: subject: attachment: mimetype: tag: id: thread: folder: path: date: lastmod:"
91 COMPREPLY=( $(compgen -W "${search_terms}" -- ${cur}) )
94 # handle search prefixes and tags with colons
95 __ltrim_colon_completions "${cur}"
100 local cur prev words cword split
101 _init_completion -s || return
114 local options="--backup= --quiet ${_notmuch_shared_options}"
116 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
123 local cur prev words cword split
124 _init_completion || return
128 COMPREPLY=( $(compgen -W "get set list" -- ${cur}) )
131 COMPREPLY=( $(compgen -W "`notmuch config list | sed 's/=.*\$//'`" -- ${cur}) )
133 # these will also complete on config get, but we don't care
137 maildir.synchronize_flags)
138 COMPREPLY=( $(compgen -W "true false" -- ${cur}) )
145 local cur prev words cword split
146 _init_completion -s || return
151 COMPREPLY=( $( compgen -W "messages threads files" -- "${cur}" ) )
155 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
167 local options="--output= --exclude= --batch --input= --lastmod ${_notmuch_shared_options}"
169 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
172 _notmuch_search_terms
179 local cur prev words cword split
180 _init_completion -s || return
185 COMPREPLY=( $( compgen -W "sup batch-tag" -- "${cur}" ) )
197 local options="--gzip --format= --output= ${_notmuch_shared_options}"
199 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
202 _notmuch_search_terms
209 local cur prev words cword split
210 # handle tags with colons and equal signs
211 _init_completion -s -n := || return
216 local path=`notmuch config get database.path`
218 COMPREPLY=( $(compgen -d "$path/${cur}" | \
219 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
227 local options="--create-folder --folder= --keep --no-hooks ${_notmuch_shared_options}"
229 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
233 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
236 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
239 # handle tags with colons
240 __ltrim_colon_completions "${cur}"
245 local cur prev words cword split
246 _init_completion || return
250 local options="--no-hooks --quiet ${_notmuch_shared_options}"
252 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
259 local cur prev words cword split
260 _init_completion -s || return
265 COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
269 COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
277 local options="--format= --format-version= --reply-to= --decrypt ${_notmuch_shared_options}"
279 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
282 _notmuch_search_terms
289 local cur prev words cword split
290 _init_completion -s || return
295 COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
307 local options="--format= --accumulate --input= ${_notmuch_shared_options}"
309 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
316 local cur prev words cword split
317 _init_completion -s || return
322 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
326 COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
330 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
334 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
342 local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
344 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
347 _notmuch_search_terms
354 local cur prev words cword split
355 _init_completion -s || return
360 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
364 COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
368 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
372 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
376 COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
384 local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
386 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
389 _notmuch_search_terms
396 local cur prev words cword split
397 _init_completion -s || return
402 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
406 COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
410 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
418 local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html ${_notmuch_shared_options}"
420 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
423 _notmuch_search_terms
430 local cur prev words cword split
431 # handle tags with colons and equal signs
432 _init_completion -s -n := || return
445 local options="--batch --input= --remove-all ${_notmuch_shared_options}"
447 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
451 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
454 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
457 _notmuch_search_terms
461 # handle tags with colons
462 __ltrim_colon_completions "${cur}"
467 local _notmuch_commands="compact config count dump help insert new reply restore search address setup show tag"
468 local arg cur prev words cword split
470 # require bash-completion with _init_completion
471 type -t _init_completion >/dev/null 2>&1 || return
473 _init_completion || return
480 # complete --help option like the subcommand
481 if [ -z "${arg}" -a "${prev}" = "--help" ]; then
485 if [ -z "${arg}" ]; then
486 # top level completion
489 # XXX: handle ${_notmuch_shared_options} and --config=
490 local options="--help --version"
491 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
494 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
497 elif [ "${arg}" = "help" ]; then
498 # handle help command specially due to _notmuch_commands usage
499 local help_topics="$_notmuch_commands hooks search-terms"
500 COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
502 # complete using _notmuch_subcommand if one exist
503 local completion_func="_notmuch_${arg//-/_}"
504 declare -f $completion_func >/dev/null && $completion_func
507 complete -F _notmuch notmuch