]> git.notmuchmail.org Git - notmuch/blob - completion/notmuch-completion.bash
7cad047f8499b083640c8a968551c09e9246c094
[notmuch] / completion / notmuch-completion.bash
1 # bash completion for notmuch                              -*- shell-script -*-
2 #
3 # Copyright © 2013 Jani Nikula
4 #
5 # Based on the bash-completion package:
6 # https://github.com/scop/bash-completion
7 #
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.
12 #
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.
17 #
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/ .
20 #
21 # Author: Jani Nikula <jani@nikula.org>
22 #
23 #
24 # BUGS:
25 #
26 # Add space after an --option without parameter (e.g. reply --decrypt)
27 # on completion.
28 #
29
30 _notmuch_shared_options="--help --uuid= --version"
31
32 # $1: current input of the form prefix:partialinput, where prefix is
33 # to or from.
34 _notmuch_email()
35 {
36     local output prefix cur
37
38     prefix="${1%%:*}"
39     cur="${1#*:}"
40
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]*}"
46
47     case "$prefix" in
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;;
53         *) return;;
54     esac
55
56     # Only emit plain, lower case, unique addresses.
57     notmuch address --output=$output $prefix:"${cur}*" | \
58         sed 's/[^<]*<\([^>]*\)>/\1/' | tr "[:upper:]" "[:lower:]" | sort -u
59 }
60
61 _notmuch_mimetype()
62 {
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
67     else
68         cat <<EOF
69 application/gzip
70 application/msword
71 application/pdf
72 application/zip
73 audio/mpeg
74 audio/ogg
75 image/gif
76 image/jpeg
77 image/png
78 message/rfc822
79 text/calendar
80 text/html
81 text/plain
82 text/vcard
83 text/x-diff
84 text/x-vcalendar
85 EOF
86     fi
87 }
88
89 _notmuch_search_terms()
90 {
91     local cur prev words cword split
92     # handle search prefixes and tags with colons and equal signs
93     _init_completion -n := || return
94
95     case "${cur}" in
96         tag:*)
97             COMPREPLY=( $(compgen -P "tag:" -W "`notmuch search --output=tags \*`" -- ${cur##tag:}) )
98             ;;
99         to:*)
100             COMPREPLY=( $(compgen -P "to:" -W "`_notmuch_email ${cur}`" -- ${cur##to:}) )
101             ;;
102         from:*)
103             COMPREPLY=( $(compgen -P "from:" -W "`_notmuch_email ${cur}`" -- ${cur##from:}) )
104             ;;
105         path:*)
106             local path=`notmuch config get database.path`
107             compopt -o nospace
108             COMPREPLY=( $(compgen -d "$path/${cur##path:}" | sed "s|^$path/||" ) )
109             ;;
110         folder:*)
111             local path=`notmuch config get database.path`
112             compopt -o nospace
113             COMPREPLY=( $(compgen -d "$path/${cur##folder:}" | \
114                 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
115             ;;
116         mimetype:*)
117             compopt -o nospace
118             COMPREPLY=( $(compgen -P "mimetype:" -W "`_notmuch_mimetype ${cur}`" -- ${cur##mimetype:}) )
119             ;;
120         query:*)
121             compopt -o nospace
122             COMPREPLY=( $(compgen -P "query:" -W "`notmuch config list | sed -n '/^query\./s/^query\.\([^=]*\)=.*/\1/p'`" -- ${cur##query:}) )
123             ;;
124         *)
125             local search_terms="from: to: subject: attachment: mimetype: tag: id: thread: folder: path: date: lastmod: query: property:"
126             compopt -o nospace
127             COMPREPLY=( $(compgen -W "${search_terms}" -- ${cur}) )
128             ;;
129     esac
130     # handle search prefixes and tags with colons
131     __ltrim_colon_completions "${cur}"
132 }
133
134 _notmuch_compact()
135 {
136     local cur prev words cword split
137     _init_completion -s || return
138
139     $split &&
140     case "${prev}" in
141         --backup)
142             _filedir -d
143             return
144             ;;
145     esac
146
147     ! $split &&
148     case "${cur}" in
149         -*)
150             local options="--backup= --quiet ${_notmuch_shared_options}"
151             compopt -o nospace
152             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
153             ;;
154     esac
155 }
156
157 _notmuch_config()
158 {
159     local cur prev words cword split
160     _init_completion || return
161
162     case "${prev}" in
163         config)
164             COMPREPLY=( $(compgen -W "get set list" -- ${cur}) )
165             ;;
166         get|set)
167             COMPREPLY=( $(compgen -W "`notmuch config list | sed 's/=.*\$//'`" -- ${cur}) )
168             ;;
169         # these will also complete on config get, but we don't care
170         database.path)
171             _filedir -d
172             ;;
173         maildir.synchronize_flags)
174             COMPREPLY=( $(compgen -W "true false" -- ${cur}) )
175             ;;
176     esac
177 }
178
179 _notmuch_count()
180 {
181     local cur prev words cword split
182     _init_completion -s || return
183
184     $split &&
185     case "${prev}" in
186         --output)
187             COMPREPLY=( $( compgen -W "messages threads files" -- "${cur}" ) )
188             return
189             ;;
190         --exclude)
191             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
192             return
193             ;;
194         --input)
195             _filedir
196             return
197             ;;
198     esac
199
200     ! $split &&
201     case "${cur}" in
202         -*)
203             local options="--output= --exclude= --batch --input= --lastmod ${_notmuch_shared_options}"
204             compopt -o nospace
205             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
206             ;;
207         *)
208             _notmuch_search_terms
209             ;;
210     esac
211 }
212
213 _notmuch_dump()
214 {
215     local cur prev words cword split
216     _init_completion -s || return
217
218     $split &&
219     case "${prev}" in
220         --format)
221             COMPREPLY=( $( compgen -W "sup batch-tag" -- "${cur}" ) )
222             return
223             ;;
224         --output)
225             _filedir
226             return
227             ;;
228     esac
229
230     ! $split &&
231     case "${cur}" in
232         -*)
233             local options="--gzip --format= --output= ${_notmuch_shared_options}"
234             compopt -o nospace
235             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
236             ;;
237         *)
238             _notmuch_search_terms
239             ;;
240     esac
241 }
242
243 _notmuch_insert()
244 {
245     local cur prev words cword split
246     # handle tags with colons and equal signs
247     _init_completion -s -n := || return
248
249     $split &&
250     case "${prev}" in
251         --folder)
252             local path=`notmuch config get database.path`
253             compopt -o nospace
254             COMPREPLY=( $(compgen -d "$path/${cur}" | \
255                 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
256             return
257             ;;
258     esac
259
260     ! $split &&
261     case "${cur}" in
262         --*)
263             local options="--create-folder --folder= --keep --no-hooks ${_notmuch_shared_options}"
264             compopt -o nospace
265             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
266             return
267             ;;
268         +*)
269             COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
270             ;;
271         -*)
272             COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
273             ;;
274     esac
275     # handle tags with colons
276     __ltrim_colon_completions "${cur}"
277 }
278
279 _notmuch_new()
280 {
281     local cur prev words cword split
282     _init_completion || return
283
284     case "${cur}" in
285         -*)
286             local options="--no-hooks --quiet ${_notmuch_shared_options}"
287             compopt -o nospace
288             COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
289             ;;
290     esac
291 }
292
293 _notmuch_reply()
294 {
295     local cur prev words cword split
296     _init_completion -s || return
297
298     $split &&
299     case "${prev}" in
300         --format)
301             COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
302             return
303             ;;
304         --reply-to)
305             COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
306             return
307             ;;
308     esac
309
310     ! $split &&
311     case "${cur}" in
312         -*)
313             local options="--format= --format-version= --reply-to= --decrypt ${_notmuch_shared_options}"
314             compopt -o nospace
315             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
316             ;;
317         *)
318             _notmuch_search_terms
319             ;;
320     esac
321 }
322
323 _notmuch_restore()
324 {
325     local cur prev words cword split
326     _init_completion -s || return
327
328     $split &&
329     case "${prev}" in
330         --format)
331             COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
332             return
333             ;;
334         --input)
335             _filedir
336             return
337             ;;
338     esac
339
340     ! $split &&
341     case "${cur}" in
342         -*)
343             local options="--format= --accumulate --input= ${_notmuch_shared_options}"
344             compopt -o nospace
345             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
346             ;;
347     esac
348 }
349
350 _notmuch_search()
351 {
352     local cur prev words cword split
353     _init_completion -s || return
354
355     $split &&
356     case "${prev}" in
357         --format)
358             COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
359             return
360             ;;
361         --output)
362             COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
363             return
364             ;;
365         --sort)
366             COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
367             return
368             ;;
369         --exclude)
370             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
371             return
372             ;;
373     esac
374
375     ! $split &&
376     case "${cur}" in
377         -*)
378             local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
379             compopt -o nospace
380             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
381             ;;
382         *)
383             _notmuch_search_terms
384             ;;
385     esac
386 }
387
388 _notmuch_address()
389 {
390     local cur prev words cword split
391     _init_completion -s || return
392
393     $split &&
394     case "${prev}" in
395         --format)
396             COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
397             return
398             ;;
399         --output)
400             COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
401             return
402             ;;
403         --sort)
404             COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
405             return
406             ;;
407         --exclude)
408             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
409             return
410             ;;
411         --deduplicate)
412             COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
413             return
414             ;;
415     esac
416
417     ! $split &&
418     case "${cur}" in
419         -*)
420             local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
421             compopt -o nospace
422             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
423             ;;
424         *)
425             _notmuch_search_terms
426             ;;
427     esac
428 }
429
430 _notmuch_show()
431 {
432     local cur prev words cword split
433     _init_completion -s || return
434
435     $split &&
436     case "${prev}" in
437         --entire-thread)
438             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
439             return
440             ;;
441         --format)
442             COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
443             return
444             ;;
445         --exclude|--body)
446             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
447             return
448             ;;
449     esac
450
451     ! $split &&
452     case "${cur}" in
453         -*)
454             local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html ${_notmuch_shared_options}"
455             compopt -o nospace
456             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
457             ;;
458         *)
459             _notmuch_search_terms
460             ;;
461     esac
462 }
463
464 _notmuch_tag()
465 {
466     local cur prev words cword split
467     # handle tags with colons and equal signs
468     _init_completion -s -n := || return
469
470     $split &&
471     case "${prev}" in
472         --input)
473             _filedir
474             return
475             ;;
476     esac
477
478     ! $split &&
479     case "${cur}" in
480         --*)
481             local options="--batch --input= --remove-all ${_notmuch_shared_options}"
482             compopt -o nospace
483             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
484             return
485             ;;
486         +*)
487             COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
488             ;;
489         -*)
490             COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
491             ;;
492         *)
493             _notmuch_search_terms
494             return
495             ;;
496     esac
497     # handle tags with colons
498     __ltrim_colon_completions "${cur}"
499 }
500
501 _notmuch()
502 {
503     local _notmuch_commands="compact config count dump help insert new reply restore search address setup show tag"
504     local arg cur prev words cword split
505
506     # require bash-completion with _init_completion
507     type -t _init_completion >/dev/null 2>&1 || return
508
509     _init_completion || return
510
511     COMPREPLY=()
512
513     # subcommand
514     _get_first_arg
515
516     # complete --help option like the subcommand
517     if [ -z "${arg}" -a "${prev}" = "--help" ]; then
518         arg="help"
519     fi
520
521     if [ -z "${arg}" ]; then
522         # top level completion
523         case "${cur}" in
524             -*)
525                 # XXX: handle ${_notmuch_shared_options} and --config=
526                 local options="--help --version"
527                 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
528                 ;;
529             *)
530                 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
531                 ;;
532         esac
533     elif [ "${arg}" = "help" ]; then
534         # handle help command specially due to _notmuch_commands usage
535         local help_topics="$_notmuch_commands hooks search-terms"
536         COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
537     else
538         # complete using _notmuch_subcommand if one exist
539         local completion_func="_notmuch_${arg//-/_}"
540         declare -f $completion_func >/dev/null && $completion_func
541     fi
542 } &&
543 complete -F _notmuch notmuch