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 # $1: current input of the form prefix:partialinput, where prefix is
34 local output prefix cur
39 # Cut the input to be completed at punctuation because
40 # (apparently) Xapian does not support the trailing wildcard '*'
41 # operator for input with punctuation. We let compgen handle the
42 # extra filtering required.
43 cur="${cur%%[^a-zA-Z0-9]*}"
46 # Note: It would be more accurate and less surprising to have
47 # output=recipients here for to: addresses, but as gathering
48 # the recipient addresses requires disk access for each
49 # matching message, this becomes prohibitively slow.
50 to|from) output=sender;;
54 # Only emit plain, lower case, unique addresses.
55 notmuch address --output=$output $prefix:"${cur}*" | \
56 sed 's/[^<]*<\([^>]*\)>/\1/' | tr "[:upper:]" "[:lower:]" | sort -u
59 _notmuch_search_terms()
61 local cur prev words cword split
62 # handle search prefixes and tags with colons and equal signs
63 _init_completion -n := || return
67 COMPREPLY=( $(compgen -P "tag:" -W "`notmuch search --output=tags \*`" -- ${cur##tag:}) )
70 COMPREPLY=( $(compgen -P "to:" -W "`_notmuch_email ${cur}`" -- ${cur##to:}) )
73 COMPREPLY=( $(compgen -P "from:" -W "`_notmuch_email ${cur}`" -- ${cur##from:}) )
76 local path=`notmuch config get database.path`
78 COMPREPLY=( $(compgen -d "$path/${cur##path:}" | sed "s|^$path/||" ) )
81 local path=`notmuch config get database.path`
83 COMPREPLY=( $(compgen -d "$path/${cur##folder:}" | \
84 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
87 local search_terms="from: to: subject: attachment: mimetype: tag: id: thread: folder: path: date:"
89 COMPREPLY=( $(compgen -W "${search_terms}" -- ${cur}) )
92 # handle search prefixes and tags with colons
93 __ltrim_colon_completions "${cur}"
98 local cur prev words cword split
99 _init_completion -s || return
112 local options="--backup= --quiet"
114 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
121 local cur prev words cword split
122 _init_completion || return
126 COMPREPLY=( $(compgen -W "get set list" -- ${cur}) )
129 COMPREPLY=( $(compgen -W "`notmuch config list | sed 's/=.*\$//'`" -- ${cur}) )
131 # these will also complete on config get, but we don't care
135 maildir.synchronize_flags)
136 COMPREPLY=( $(compgen -W "true false" -- ${cur}) )
143 local cur prev words cword split
144 _init_completion -s || return
149 COMPREPLY=( $( compgen -W "messages threads files" -- "${cur}" ) )
153 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
165 local options="--output= --exclude= --batch --input="
167 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
170 _notmuch_search_terms
177 local cur prev words cword split
178 _init_completion -s || return
183 COMPREPLY=( $( compgen -W "sup batch-tag" -- "${cur}" ) )
195 local options="--gzip --format= --output="
197 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
200 _notmuch_search_terms
207 local cur prev words cword split
208 # handle tags with colons and equal signs
209 _init_completion -s -n := || return
214 local path=`notmuch config get database.path`
216 COMPREPLY=( $(compgen -d "$path/${cur}" | \
217 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
225 local options="--create-folder --folder= --keep --no-hooks"
227 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
231 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
234 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
237 # handle tags with colons
238 __ltrim_colon_completions "${cur}"
243 local cur prev words cword split
244 _init_completion || return
248 local options="--no-hooks --quiet"
249 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
256 local cur prev words cword split
257 _init_completion -s || return
262 COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
266 COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
274 local options="--format= --format-version= --reply-to= --decrypt"
276 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
279 _notmuch_search_terms
286 local cur prev words cword split
287 _init_completion -s || return
292 COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
304 local options="--format= --accumulate --input="
306 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
313 local cur prev words cword split
314 _init_completion -s || return
319 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
323 COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
327 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
331 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
339 local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate="
341 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
344 _notmuch_search_terms
351 local cur prev words cword split
352 _init_completion -s || return
357 COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
361 COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
365 COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
369 COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
377 local options="--format= --output= --sort= --exclude="
379 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
382 _notmuch_search_terms
389 local cur prev words cword split
390 _init_completion -s || return
395 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
399 COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
403 COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
411 local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html"
413 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
416 _notmuch_search_terms
423 local cur prev words cword split
424 # handle tags with colons and equal signs
425 _init_completion -s -n := || return
438 local options="--batch --input= --remove-all"
440 COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
444 COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
447 COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
450 _notmuch_search_terms
454 # handle tags with colons
455 __ltrim_colon_completions "${cur}"
460 local _notmuch_commands="compact config count dump help insert new reply restore search address setup show tag"
461 local arg cur prev words cword split
463 # require bash-completion with _init_completion
464 type -t _init_completion >/dev/null 2>&1 || return
466 _init_completion || return
473 # complete --help option like the subcommand
474 if [ -z "${arg}" -a "${prev}" = "--help" ]; then
478 if [ -z "${arg}" ]; then
479 # top level completion
480 local top_options="--help --version"
482 -*) COMPREPLY=( $(compgen -W "${top_options}" -- ${cur}) ) ;;
483 *) COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) ) ;;
485 elif [ "${arg}" = "help" ]; then
486 # handle help command specially due to _notmuch_commands usage
487 local help_topics="$_notmuch_commands hooks search-terms"
488 COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
490 # complete using _notmuch_subcommand if one exist
491 local completion_func="_notmuch_${arg//-/_}"
492 declare -f $completion_func >/dev/null && $completion_func
495 complete -F _notmuch notmuch