]> git.notmuchmail.org Git - notmuch/blob - completion/notmuch-completion.bash
completion: complete notmuch emacs-mua
[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 || return
315
316     case "${cur}" in
317         -*)
318             local options="--no-hooks --quiet ${_notmuch_shared_options}"
319             compopt -o nospace
320             COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
321             ;;
322     esac
323 }
324
325 _notmuch_reply()
326 {
327     local cur prev words cword split
328     _init_completion -s || return
329
330     $split &&
331     case "${prev}" in
332         --format)
333             COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
334             return
335             ;;
336         --reply-to)
337             COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
338             return
339             ;;
340     esac
341
342     ! $split &&
343     case "${cur}" in
344         -*)
345             local options="--format= --format-version= --reply-to= --decrypt ${_notmuch_shared_options}"
346             compopt -o nospace
347             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
348             ;;
349         *)
350             _notmuch_search_terms
351             ;;
352     esac
353 }
354
355 _notmuch_restore()
356 {
357     local cur prev words cword split
358     _init_completion -s || return
359
360     $split &&
361     case "${prev}" in
362         --format)
363             COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
364             return
365             ;;
366         --input)
367             _filedir
368             return
369             ;;
370     esac
371
372     ! $split &&
373     case "${cur}" in
374         -*)
375             local options="--format= --accumulate --input= ${_notmuch_shared_options}"
376             compopt -o nospace
377             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
378             ;;
379     esac
380 }
381
382 _notmuch_search()
383 {
384     local cur prev words cword split
385     _init_completion -s || return
386
387     $split &&
388     case "${prev}" in
389         --format)
390             COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
391             return
392             ;;
393         --output)
394             COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
395             return
396             ;;
397         --sort)
398             COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
399             return
400             ;;
401         --exclude)
402             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
403             return
404             ;;
405     esac
406
407     ! $split &&
408     case "${cur}" in
409         -*)
410             local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate= ${_notmuch_shared_options}"
411             compopt -o nospace
412             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
413             ;;
414         *)
415             _notmuch_search_terms
416             ;;
417     esac
418 }
419
420 _notmuch_address()
421 {
422     local cur prev words cword split
423     _init_completion -s || return
424
425     $split &&
426     case "${prev}" in
427         --format)
428             COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
429             return
430             ;;
431         --output)
432             COMPREPLY=( $( compgen -W "sender recipients count" -- "${cur}" ) )
433             return
434             ;;
435         --sort)
436             COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
437             return
438             ;;
439         --exclude)
440             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
441             return
442             ;;
443         --deduplicate)
444             COMPREPLY=( $( compgen -W "no mailbox address" -- "${cur}" ) )
445             return
446             ;;
447     esac
448
449     ! $split &&
450     case "${cur}" in
451         -*)
452             local options="--format= --output= --sort= --exclude= --deduplicate= ${_notmuch_shared_options}"
453             compopt -o nospace
454             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
455             ;;
456         *)
457             _notmuch_search_terms
458             ;;
459     esac
460 }
461
462 _notmuch_show()
463 {
464     local cur prev words cword split
465     _init_completion -s || return
466
467     $split &&
468     case "${prev}" in
469         --entire-thread)
470             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
471             return
472             ;;
473         --format)
474             COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
475             return
476             ;;
477         --exclude|--body)
478             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
479             return
480             ;;
481     esac
482
483     ! $split &&
484     case "${cur}" in
485         -*)
486             local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html ${_notmuch_shared_options}"
487             compopt -o nospace
488             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
489             ;;
490         *)
491             _notmuch_search_terms
492             ;;
493     esac
494 }
495
496 _notmuch_tag()
497 {
498     local cur prev words cword split
499     # handle tags with colons and equal signs
500     _init_completion -s -n := || return
501
502     $split &&
503     case "${prev}" in
504         --input)
505             _filedir
506             return
507             ;;
508     esac
509
510     ! $split &&
511     case "${cur}" in
512         --*)
513             local options="--batch --input= --remove-all ${_notmuch_shared_options}"
514             compopt -o nospace
515             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
516             return
517             ;;
518         +*)
519             COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
520             ;;
521         -*)
522             COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
523             ;;
524         *)
525             _notmuch_search_terms
526             return
527             ;;
528     esac
529     # handle tags with colons
530     __ltrim_colon_completions "${cur}"
531 }
532
533 _notmuch()
534 {
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
537
538     # require bash-completion with _init_completion
539     type -t _init_completion >/dev/null 2>&1 || return
540
541     _init_completion || return
542
543     COMPREPLY=()
544
545     # subcommand
546     _get_first_arg
547
548     # complete --help option like the subcommand
549     if [ -z "${arg}" -a "${prev}" = "--help" ]; then
550         arg="help"
551     fi
552
553     if [ -z "${arg}" ]; then
554         # top level completion
555         case "${cur}" in
556             -*)
557                 # XXX: handle ${_notmuch_shared_options} and --config=
558                 local options="--help --version"
559                 COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
560                 ;;
561             *)
562                 COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) )
563                 ;;
564         esac
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}) )
569     else
570         # complete using _notmuch_subcommand if one exist
571         local completion_func="_notmuch_${arg//-/_}"
572         declare -f $completion_func >/dev/null && $completion_func
573     fi
574 } &&
575 complete -F _notmuch notmuch