]> git.notmuchmail.org Git - notmuch/blob - completion/notmuch-completion.bash
cli/new: add --try-decrypt=(true|false)
[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_emacs_mua()
244 {
245     local cur prev words cword split
246     _init_completion -s || return
247
248     $split &&
249     case "${prev}" in
250         --to|--cc|--bcc)
251             COMPREPLY=( $(compgen -W "`_notmuch_email to:${cur}`" -- ${cur}) )
252             return
253             ;;
254         --body)
255             _filedir
256             return
257             ;;
258     esac
259
260     ! $split &&
261     case "${cur}" in
262         -*)
263             local options="--subject= --to= --cc= --bcc= --body= --no-window-system --client --auto-daemon --create-frame --print --help --hello"
264
265             compopt -o nospace
266             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
267             ;;
268         *)
269             COMPREPLY=( $(compgen -W "`_notmuch_email to:${cur}`" -- ${cur}) )
270             return
271             ;;
272     esac
273 }
274
275 _notmuch_insert()
276 {
277     local cur prev words cword split
278     # handle tags with colons and equal signs
279     _init_completion -s -n := || return
280
281     $split &&
282     case "${prev}" in
283         --folder)
284             local path=`notmuch config get database.path`
285             compopt -o nospace
286             COMPREPLY=( $(compgen -d "$path/${cur}" | \
287                 sed "s|^$path/||" | grep -v "\(^\|/\)\(cur\|new\|tmp\)$" ) )
288             return
289             ;;
290     esac
291
292     ! $split &&
293     case "${cur}" in
294         --*)
295             local options="--create-folder --folder= --keep --no-hooks ${_notmuch_shared_options}"
296             compopt -o nospace
297             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
298             return
299             ;;
300         +*)
301             COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
302             ;;
303         -*)
304             COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
305             ;;
306     esac
307     # handle tags with colons
308     __ltrim_colon_completions "${cur}"
309 }
310
311 _notmuch_new()
312 {
313     local cur prev words cword split
314     _init_completion -s || return
315
316     $split &&
317     case "${prev}" in
318         --try-decrypt)
319             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
320             return
321             ;;
322     esac
323
324     ! $split &&
325     case "${cur}" in
326         -*)
327             local options="--no-hooks --try-decrypt= --quiet ${_notmuch_shared_options}"
328             compopt -o nospace
329             COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
330             ;;
331     esac
332 }
333
334 _notmuch_reply()
335 {
336     local cur prev words cword split
337     _init_completion -s || return
338
339     $split &&
340     case "${prev}" in
341         --format)
342             COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
343             return
344             ;;
345         --reply-to)
346             COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
347             return
348             ;;
349     esac
350
351     ! $split &&
352     case "${cur}" in
353         -*)
354             local options="--format= --format-version= --reply-to= --decrypt ${_notmuch_shared_options}"
355             compopt -o nospace
356             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
357             ;;
358         *)
359             _notmuch_search_terms
360             ;;
361     esac
362 }
363
364 _notmuch_restore()
365 {
366     local cur prev words cword split
367     _init_completion -s || return
368
369     $split &&
370     case "${prev}" in
371         --format)
372             COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
373             return
374             ;;
375         --input)
376             _filedir
377             return
378             ;;
379     esac
380
381     ! $split &&
382     case "${cur}" in
383         -*)
384             local options="--format= --accumulate --input= ${_notmuch_shared_options}"
385             compopt -o nospace
386             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
387             ;;
388     esac
389 }
390
391 _notmuch_search()
392 {
393     local cur prev words cword split
394     _init_completion -s || return
395
396     $split &&
397     case "${prev}" in
398         --format)
399             COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
400             return
401             ;;
402         --output)
403             COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
404             return
405             ;;
406         --sort)
407             COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
408             return
409             ;;
410         --exclude)
411             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
412             return
413             ;;
414     esac
415
416     ! $split &&
417     case "${cur}" in
418         -*)
419             local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
420             compopt -o nospace
421             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
422             ;;
423         *)
424             _notmuch_search_terms
425             ;;
426     esac
427 }
428
429 _notmuch_reindex()
430 {
431     local cur prev words cword split
432     _init_completion -s || return
433
434     ! $split &&
435     case "${cur}" in
436         -*)
437             local options="${_notmuch_shared_options}"
438             compopt -o nospace
439             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
440             ;;
441         *)
442             _notmuch_search_terms
443             ;;
444     esac
445 }
446
447 _notmuch_address()
448 {
449     local cur prev words cword split
450     _init_completion -s || return
451
452     $split &&
453     case "${prev}" in
454         --format)
455             COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
456             return
457             ;;
458         --output)
459             COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
460             return
461             ;;
462         --sort)
463             COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
464             return
465             ;;
466         --exclude)
467             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
468             return
469             ;;
470         --deduplicate)
471             COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
472             return
473             ;;
474     esac
475
476     ! $split &&
477     case "${cur}" in
478         -*)
479             local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
480             compopt -o nospace
481             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
482             ;;
483         *)
484             _notmuch_search_terms
485             ;;
486     esac
487 }
488
489 _notmuch_show()
490 {
491     local cur prev words cword split
492     _init_completion -s || return
493
494     $split &&
495     case "${prev}" in
496         --entire-thread)
497             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
498             return
499             ;;
500         --format)
501             COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
502             return
503             ;;
504         --exclude|--body)
505             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
506             return
507             ;;
508     esac
509
510     ! $split &&
511     case "${cur}" in
512         -*)
513             local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html ${_notmuch_shared_options}"
514             compopt -o nospace
515             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
516             ;;
517         *)
518             _notmuch_search_terms
519             ;;
520     esac
521 }
522
523 _notmuch_tag()
524 {
525     local cur prev words cword split
526     # handle tags with colons and equal signs
527     _init_completion -s -n := || return
528
529     $split &&
530     case "${prev}" in
531         --input)
532             _filedir
533             return
534             ;;
535     esac
536
537     ! $split &&
538     case "${cur}" in
539         --*)
540             local options="--batch --input= --remove-all ${_notmuch_shared_options}"
541             compopt -o nospace
542             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
543             return
544             ;;
545         +*)
546             COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
547             ;;
548         -*)
549             COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
550             ;;
551         *)
552             _notmuch_search_terms
553             return
554             ;;
555     esac
556     # handle tags with colons
557     __ltrim_colon_completions "${cur}"
558 }
559
560 _notmuch()
561 {
562     local _notmuch_commands="compact config count dump help insert new reply restore reindex search address setup show tag emacs-mua"
563     local arg cur prev words cword split
564
565     # require bash-completion with _init_completion
566     type -t _init_completion >/dev/null 2>&1 || return
567
568     _init_completion || return
569
570     COMPREPLY=()
571
572     # subcommand
573     _get_first_arg
574
575     # complete --help option like the subcommand
576     if [ -z "${arg}" -a "${prev}" = "--help" ]; then
577         arg="help"
578     fi
579
580     if [ -z "${arg}" ]; then
581         # top level completion
582         case "${cur}" in
583             -*)
584                 # XXX: handle ${_notmuch_shared_options} and --config=
585                 local options="--help --version"
586                 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
587                 ;;
588             *)
589                 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
590                 ;;
591         esac
592     elif [ "${arg}" = "help" ]; then
593         # handle help command specially due to _notmuch_commands usage
594         local help_topics="$_notmuch_commands hooks search-terms"
595         COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
596     else
597         # complete using _notmuch_subcommand if one exist
598         local completion_func="_notmuch_${arg//-/_}"
599         declare -f $completion_func >/dev/null && $completion_func
600     fi
601 } &&
602 complete -F _notmuch notmuch