]> git.notmuchmail.org Git - notmuch/blob - completion/notmuch-completion.bash
completion: bash completion for notmuch new --quiet option
[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 # http://bash-completion.alioth.debian.org/
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 http://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_user_emails()
31 {
32     notmuch config get user.primary_email
33     notmuch config get user.other_email
34 }
35
36 _notmuch_search_terms()
37 {
38     local cur prev words cword split
39     # handle search prefixes and tags with colons and equal signs
40     _init_completion -n := || return
41
42     case "${cur}" in
43         tag:*)
44             COMPREPLY=( $(compgen -P "tag:" -W "`notmuch search --output=tags \*`" -- ${cur##tag:}) )
45             ;;
46         to:*)
47             COMPREPLY=( $(compgen -P "to:" -W "`_notmuch_user_emails`" -- ${cur##to:}) )
48             ;;
49         from:*)
50             COMPREPLY=( $(compgen -P "from:" -W "`_notmuch_user_emails`" -- ${cur##from:}) )
51             ;;
52         *)
53             local search_terms="from: to: subject: attachment: tag: id: thread: folder: date:"
54             compopt -o nospace
55             COMPREPLY=( $(compgen -W "${search_terms}" -- ${cur}) )
56             ;;
57     esac
58     # handle search prefixes and tags with colons
59     __ltrim_colon_completions "${cur}"
60 }
61
62 _notmuch_compact()
63 {
64     local cur prev words cword split
65     _init_completion -s || return
66
67     $split &&
68     case "${prev}" in
69         --backup)
70             _filedir
71             return
72             ;;
73     esac
74
75     ! $split &&
76     case "${cur}" in
77         -*)
78             local options="--backup= --quiet"
79             compopt -o nospace
80             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
81             ;;
82     esac
83 }
84
85 _notmuch_config()
86 {
87     local cur prev words cword split
88     _init_completion || return
89
90     case "${prev}" in
91         config)
92             COMPREPLY=( $(compgen -W "get set list" -- ${cur}) )
93             ;;
94         get|set)
95             COMPREPLY=( $(compgen -W "`notmuch config list | sed 's/=.*\$//'`" -- ${cur}) )
96             ;;
97         # these will also complete on config get, but we don't care
98         database.path)
99             _filedir
100             ;;
101         maildir.synchronize_flags)
102             COMPREPLY=( $(compgen -W "true false" -- ${cur}) )
103             ;;
104     esac
105 }
106
107 _notmuch_count()
108 {
109     local cur prev words cword split
110     _init_completion -s || return
111
112     $split &&
113     case "${prev}" in
114         --output)
115             COMPREPLY=( $( compgen -W "messages threads files" -- "${cur}" ) )
116             return
117             ;;
118         --exclude)
119             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
120             return
121             ;;
122         --input)
123             _filedir
124             return
125             ;;
126     esac
127
128     ! $split &&
129     case "${cur}" in
130         -*)
131             local options="--output= --exclude= --batch --input="
132             compopt -o nospace
133             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
134             ;;
135         *)
136             _notmuch_search_terms
137             ;;
138     esac
139 }
140
141 _notmuch_dump()
142 {
143     local cur prev words cword split
144     _init_completion -s || return
145
146     $split &&
147     case "${prev}" in
148         --format)
149             COMPREPLY=( $( compgen -W "sup batch-tag" -- "${cur}" ) )
150             return
151             ;;
152         --output)
153             _filedir
154             return
155             ;;
156     esac
157
158     ! $split &&
159     case "${cur}" in
160         -*)
161             local options="--format= --output="
162             compopt -o nospace
163             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
164             ;;
165         *)
166             _notmuch_search_terms
167             ;;
168     esac
169 }
170
171 _notmuch_insert()
172 {
173     local cur prev words cword split
174     # handle tags with colons and equal signs
175     _init_completion -s -n := || return
176
177     $split &&
178     case "${prev}" in
179         --folder)
180             _filedir
181             return
182             ;;
183     esac
184
185     ! $split &&
186     case "${cur}" in
187         --*)
188             local options="--create-folder --folder="
189             compopt -o nospace
190             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
191             return
192             ;;
193         +*)
194             COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
195             ;;
196         -*)
197             COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
198             ;;
199     esac
200     # handle tags with colons
201     __ltrim_colon_completions "${cur}"
202 }
203
204 _notmuch_new()
205 {
206     local cur prev words cword split
207     _init_completion || return
208
209     case "${cur}" in
210         -*)
211             local options="--no-hooks --quiet"
212             COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
213             ;;
214     esac
215 }
216
217 _notmuch_reply()
218 {
219     local cur prev words cword split
220     _init_completion -s || return
221
222     $split &&
223     case "${prev}" in
224         --format)
225             COMPREPLY=( $( compgen -W "default json sexp headers-only" -- "${cur}" ) )
226             return
227             ;;
228         --reply-to)
229             COMPREPLY=( $( compgen -W "all sender" -- "${cur}" ) )
230             return
231             ;;
232     esac
233
234     ! $split &&
235     case "${cur}" in
236         -*)
237             local options="--format= --format-version= --reply-to= --decrypt"
238             compopt -o nospace
239             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
240             ;;
241         *)
242             _notmuch_search_terms
243             ;;
244     esac
245 }
246
247 _notmuch_restore()
248 {
249     local cur prev words cword split
250     _init_completion -s || return
251
252     $split &&
253     case "${prev}" in
254         --format)
255             COMPREPLY=( $( compgen -W "sup batch-tag auto" -- "${cur}" ) )
256             return
257             ;;
258         --input)
259             _filedir
260             return
261             ;;
262     esac
263
264     ! $split &&
265     case "${cur}" in
266         -*)
267             local options="--format= --accumulate --input="
268             compopt -o nospace
269             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
270             ;;
271     esac
272 }
273
274 _notmuch_search()
275 {
276     local cur prev words cword split
277     _init_completion -s || return
278
279     $split &&
280     case "${prev}" in
281         --format)
282             COMPREPLY=( $( compgen -W "json sexp text text0" -- "${cur}" ) )
283             return
284             ;;
285         --output)
286             COMPREPLY=( $( compgen -W "summary threads messages files tags" -- "${cur}" ) )
287             return
288             ;;
289         --sort)
290             COMPREPLY=( $( compgen -W "newest-first oldest-first" -- "${cur}" ) )
291             return
292             ;;
293         --exclude)
294             COMPREPLY=( $( compgen -W "true false flag all" -- "${cur}" ) )
295             return
296             ;;
297     esac
298
299     ! $split &&
300     case "${cur}" in
301         -*)
302             local options="--format= --output= --sort= --offset= --limit= --exclude= --duplicate="
303             compopt -o nospace
304             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
305             ;;
306         *)
307             _notmuch_search_terms
308             ;;
309     esac
310 }
311
312 _notmuch_show()
313 {
314     local cur prev words cword split
315     _init_completion -s || return
316
317     $split &&
318     case "${prev}" in
319         --entire-thread)
320             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
321             return
322             ;;
323         --format)
324             COMPREPLY=( $( compgen -W "text json sexp mbox raw" -- "${cur}" ) )
325             return
326             ;;
327         --exclude|--body)
328             COMPREPLY=( $( compgen -W "true false" -- "${cur}" ) )
329             return
330             ;;
331     esac
332
333     ! $split &&
334     case "${cur}" in
335         -*)
336             local options="--entire-thread= --format= --exclude= --body= --format-version= --part= --verify --decrypt --include-html"
337             compopt -o nospace
338             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
339             ;;
340         *)
341             _notmuch_search_terms
342             ;;
343     esac
344 }
345
346 _notmuch_tag()
347 {
348     local cur prev words cword split
349     # handle tags with colons and equal signs
350     _init_completion -s -n := || return
351
352     $split &&
353     case "${prev}" in
354         --input)
355             _filedir
356             return
357             ;;
358     esac
359
360     ! $split &&
361     case "${cur}" in
362         --*)
363             local options="--batch --input= --remove-all"
364             compopt -o nospace
365             COMPREPLY=( $(compgen -W "$options" -- ${cur}) )
366             return
367             ;;
368         +*)
369             COMPREPLY=( $(compgen -P "+" -W "`notmuch search --output=tags \*`" -- ${cur##+}) )
370             ;;
371         -*)
372             COMPREPLY=( $(compgen -P "-" -W "`notmuch search --output=tags \*`" -- ${cur##-}) )
373             ;;
374         *)
375             _notmuch_search_terms
376             return
377             ;;
378     esac
379     # handle tags with colons
380     __ltrim_colon_completions "${cur}"
381 }
382
383 _notmuch()
384 {
385     local _notmuch_commands="compact config count dump help insert new reply restore search setup show tag"
386     local arg cur prev words cword split
387     _init_completion || return
388
389     COMPREPLY=()
390
391     # subcommand
392     _get_first_arg
393
394     # complete --help option like the subcommand
395     if [ -z "${arg}" -a "${prev}" = "--help" ]; then
396         arg="help"
397     fi
398
399     if [ -z "${arg}" ]; then
400         # top level completion
401         local top_options="--help --version"
402         case "${cur}" in
403             -*) COMPREPLY=( $(compgen -W "${top_options}" -- ${cur}) ) ;;
404             *) COMPREPLY=( $(compgen -W "${_notmuch_commands}" -- ${cur}) ) ;;
405         esac
406     elif [ "${arg}" = "help" ]; then
407         # handle help command specially due to _notmuch_commands usage
408         local help_topics="$_notmuch_commands hooks search-terms"
409         COMPREPLY=( $(compgen -W "${help_topics}" -- ${cur}) )
410     else
411         # complete using _notmuch_subcommand if one exist
412         local completion_func="_notmuch_${arg//-/_}"
413         declare -f $completion_func >/dev/null && $completion_func
414     fi
415 } &&
416 complete -F _notmuch notmuch