]> git.notmuchmail.org Git - notmuch/blob - completion/zsh/_notmuch
Import notmuch_0.28.2.orig.tar.gz
[notmuch] / completion / zsh / _notmuch
1 #compdef notmuch -p notmuch-*
2
3 # ZSH completion for `notmuch`
4 # Copyright © 2018 Vincent Breitmoser <look@my.amazin.horse>
5
6 _notmuch_command() {
7   local -a notmuch_commands
8   notmuch_commands=(
9     'help:display documentation for a subcommand'
10     'setup:interactively configure notmuch'
11
12     'address:output addresses from matching messages'
13     'compact:compact the notmuch database'
14     'config:access notmuch configuration file'
15     'count:count messages matching the given search terms'
16     'dump:creates a plain-text dump of the tags of each message'
17     'insert:add a message to the maildir and notmuch database'
18     'new:incorporate new mail into the notmuch database'
19     'reindex:re-index a set of messages'
20     'reply:constructs a reply template for a set of messages'
21     'restore:restores the tags from the given file (see notmuch dump)'
22     'search:search for messages matching the given search terms'
23     'show:show messages matching the given search terms'
24     'tag:add/remove tags for all messages matching the search terms'
25   )
26
27   if ((CURRENT == 1)); then
28     _describe -t commands 'notmuch command' notmuch_commands
29   else
30       local curcontext="$curcontext"
31       cmd=$words[1]
32       if (( $+functions[_notmuch_$cmd] )); then
33         _notmuch_$cmd
34       else
35         _message -e "unknown command $cmd"
36       fi
37   fi
38 }
39
40 _notmuch_term_tag _notmuch_term_is () {
41   local ret=1 expl
42   local -a notmuch_tags
43
44   notmuch_tags=( ${(f)"$(notmuch search --output=tags '*')"} )
45
46   _description notmuch-tag expl 'tag'
47   compadd "$expl[@]" -a notmuch_tags && ret=0
48   return $ret
49 }
50
51 _notmuch_term_to _notmuch_term_from() {
52   _email_addresses -c
53 }
54
55 _notmuch_term_mimetype() {
56   local ret=1 expl
57   local -a commontypes
58   commontypes=(
59     'text/plain'
60     'text/html'
61     'application/pdf'
62   )
63   _description typical-mimetypes expl 'common types'
64   compadd "$expl[@]" -a commontypes && ret=0
65
66   _mime_types && ret=0
67   return $ret
68 }
69
70 _notmuch_term_path() {
71   local ret=1 expl
72   local maildir="$(notmuch config get database.path)"
73   [[ -d $maildir ]] || { _message -e "database.path not found" ; return $ret }
74
75   _description notmuch-folder expl 'maildir folder'
76   _files "$expl[@]" -W $maildir -/ && ret=0
77   return $ret
78 }
79
80 _notmuch_term_folder() {
81   local ret=1 expl
82   local maildir="$(notmuch config get database.path)"
83   [[ -d $maildir ]] || { _message -e "database.path not found" ; return $ret }
84
85   _description notmuch-folder expl 'maildir folder'
86   local ignoredfolders=( '*/(cur|new|tmp)' )
87   _files "$expl[@]" -W $maildir -F ignoredfolders -/ && ret=0
88   return $ret
89 }
90
91 _notmuch_term_query() {
92   local ret=1
93   local line query_name
94   local -a query_names query_content
95   for line in ${(f)"$(notmuch config list | grep '^query.')"}; do
96     query_name=${${line%%=*}#query.}
97     query_names+=( $query_name )
98     query_content+=( "$query_name = ${line#*=}" )
99   done
100
101   _description notmuch-named-query expl 'named query'
102   compadd "$expl[@]" -d query_content -a query_names && ret=0
103   return $ret
104 }
105
106 _notmuch_search_term() {
107   local ret=1 expl match
108   setopt localoptions extendedglob
109
110   typeset -a notmuch_search_terms
111   notmuch_search_terms=(
112     'from' 'to' 'subject' 'attachment' 'mimetype' 'tag' 'id' 'thread' 'path' 'folder' 'date' 'lastmod' 'query' 'property'
113   )
114
115   if compset -P '(#b)([^:]#):'; then
116     if (( $+functions[_notmuch_term_$match[1]] )); then
117       _notmuch_term_$match[1] && ret=0
118       return $ret
119     elif (( $+notmuch_search_terms[(r)$match[1]] )); then
120       _message "search term '$match[1]'" && ret=0
121       return $ret
122     else
123       _message -e "unknown search term '$match[1]'"
124       return $ret
125     fi
126   fi
127
128   _description notmuch-term expl 'search term'
129   compadd "$expl[@]" -S ':' -a notmuch_search_terms && ret=0
130
131   if [[ $CURRENT -gt 1 && $words[CURRENT-1] != '--' ]]; then
132     _description notmuch-op expl 'boolean operator'
133     compadd "$expl[@]" -- and or not xor && ret=0
134   fi
135
136   return $ret
137 }
138
139 _notmuch_tagging_or_search() {
140   setopt localoptions extendedglob
141   local ret=1 expl
142   local -a notmuch_tags
143
144   # first arg that is a search term, or $#words+1
145   integer searchtermarg=$(( $words[(I)--] != 0 ? $words[(i)--] : $words[(i)^(-|+)*] ))
146
147   if (( CURRENT > 1 )); then
148     () {
149       local -a words=( $argv )
150       local CURRENT=$(( CURRENT - searchtermarg + 1 ))
151       _notmuch_search_term && ret=0
152     } $words[searchtermarg,$]
153   fi
154
155   # only complete +/- tags if we're before the first search term
156   if (( searchtermarg >= CURRENT )); then
157     if compset -P '+'; then
158       notmuch_tags=( ${(f)"$(notmuch search --output=tags '*')"} )
159       _description notmuch-tag expl 'add tag'
160       compadd "$expl[@]" -a notmuch_tags
161       return 0
162     elif compset -P '-'; then
163       notmuch_tags=( ${(f)"$(notmuch search --output=tags '*')"} )
164       _description notmuch-tag expl 'remove tag'
165       compadd "$expl[@]" -a notmuch_tags
166       return 0
167     else
168       _description notmuch-tag expl 'add or remove tags'
169       compadd "$expl[@]" -S '' -- '+' '-' && ret=0
170     fi
171   fi
172
173   return $ret
174 }
175
176 _notmuch_address() {
177   _arguments -S \
178     '--format=[set output format]:output format:(json sexp text text0)' \
179     '--format-version=[set output format version]:format version: ' \
180     '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
181     '--output=[select output format]:output format:(sender recipients count address)' \
182     '--deduplicate=[deduplicate results]:deduplication mode:(no mailbox address)' \
183     '--exclude=[respect excluded tags setting]:exclude tags:(true false)' \
184     '*::search term:_notmuch_search_term'
185 }
186
187 _notmuch_compact() {
188   _arguments \
189     '--backup=[save a backup before compacting]:backup directory:_files -/' \
190     '--quiet[do not print progress or results]'
191 }
192
193 _notmuch_count() {
194   _arguments \
195      - normal \
196         '--lastmod[append lastmod and uuid to output]' \
197         '--exclude=[respect excluded tags setting]:exclude tags:(true false)' \
198         '--output=[select what to count]:output format:(messages threads files)' \
199         '*::search term:_notmuch_search_term' \
200     - batch \
201       '--batch[operate in batch mode]' \
202       '(--batch)--input=[read batch operations from file]:batch file:_files'
203 }
204
205 _notmuch_dump() {
206   _arguments -S \
207     '--gzip[compress output with gzip]' \
208     '--format=[specify output format]:output format:(batch-tag sup)' \
209     '*--include=[configure metadata to output (default all)]:metadata type:(config properties tags)' \
210     '--output=[write output to file]:output file:_files' \
211     '*::search term:_notmuch_search_term'
212 }
213
214 _notmuch_new() {
215   _arguments \
216     '--no-hooks[prevent hooks from being run]' \
217     '--quiet[do not print progress or results]' \
218     '--full-scan[don''t rely on directory modification times for scan]' \
219     '--decrypt=[decrypt messages]:decryption setting:((false\:"never decrypt" auto\:"decrypt if session key is known (default)" true\:"decrypt using secret keys" stash\:"decrypt, and store session keys"))'
220 }
221
222 _notmuch_reindex() {
223   _arguments \
224     '--decrypt=[decrypt messages]:decryption setting:((false\:"never decrypt" auto\:"decrypt if session key is known (default)" true\:"decrypt using secret keys" stash\:"decrypt, and store session keys"))' \
225     '*::search term:_notmuch_search_term'
226 }
227
228 _notmuch_search() {
229   _arguments -S \
230     '--max-threads=[display only the first x threads from the search results]:number of threads to show: ' \
231     '--first=[omit the first x threads from the search results]:number of threads to omit: ' \
232     '--sort=[sort results]:sorting:((newest-first\:"reverse chronological order" oldest-first\:"chronological order"))' \
233     '--output=[select what to output]:output:(summary threads messages files tags)' \
234     '*::search term:_notmuch_search_term'
235 }
236
237 _notmuch_show() {
238   _arguments -S \
239     '--entire-thread=[output entire threads]:show thread:(true false)' \
240     '--format=[set output format]:output format:(text json sexp mbox raw)' \
241     '--format-version=[set output format version]:format version: ' \
242     '--part=[output a single decoded mime part]:part number: ' \
243     '--verify[verify signed MIME parts]' \
244     '--decrypt=[decrypt messages]:decryption setting:((false\:"never decrypt" auto\:"decrypt if session key is known (default)" true\:"decrypt using secret keys" stash\:"decrypt, and store session keys"))' \
245     '--exclude=[respect excluded tags setting]:exclude tags:(true false)' \
246     '--body=[output body]:output body content:(true false)' \
247     '--include-html[include text/html parts in the output]' \
248     '*::search term:_notmuch_search_term'
249 }
250
251 _notmuch_reply() {
252   _arguments \
253     '--format=[set output format]:output format:(default json sexp headers-only)' \
254     '--format-version=[set output format version]:output format version: ' \
255     '--reply-to=[specify recipient types]:recipient types:(all sender)' \
256     '--decrypt=[decrypt messages]:decryption setting:((false\:"never decrypt" auto\:"decrypt if session key is known (default)" true\:"decrypt using secret keys"))' \
257     '*::search term:_notmuch_search_term'
258 }
259
260 _notmuch_restore() {
261   _arguments \
262     '--acumulate[add data to db instead of replacing]' \
263     '--format=[specify input format]:input format:(auto batch-tag sup)' \
264     '*--include=[configure metadata to import (default all)]:metadata type:(config properties tags)' \
265     '--input=[read from file]:notmuch dump file:_files'
266 }
267
268 _notmuch_tag() {
269   _arguments \
270     - normal \
271       '--remove-all[remove all tags from matching messages]:*:search term:_notmuch_search_term' \
272       '*::tag or search:_notmuch_tagging_or_search' \
273     - batch \
274       '--batch[operate in batch mode]' \
275       '(--batch)--input=[read batch operations from file]:batch file:_files'
276 }
277
278 _notmuch() {
279   if [[ $service == notmuch-* ]]; then
280     local compfunc=_${service//-/_}
281     (( $+functions[$compfunc] )) || return 1
282     $compfunc "$@"
283   else
284     _arguments \
285       '(* -)--help[show help]' \
286       '(* -)--version[show version]' \
287       '--config=-[specify config file]:config file:_files' \
288       '--uuid=-[check against database uuid or exit]:uuid: ' \
289       '*::notmuch commands:_notmuch_command'
290   fi
291 }
292
293 _notmuch "$@"