]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
fbe4cd9ea61027d25b999379fc5cedbce9e4486b
[notmuch] / vim / plugin / notmuch.vim
1 " notmuch.vim plugin --- run notmuch within vim
2 "
3 " Copyright © Carl Worth
4 "
5 " This file is part of Notmuch.
6 "
7 " Notmuch is free software: you can redistribute it and/or modify it
8 " under the terms of the GNU General Public License as published by
9 " the Free Software Foundation, either version 3 of the License, or
10 " (at your option) any later version.
11 "
12 " Notmuch is distributed in the hope that it will be useful, but
13 " WITHOUT ANY WARRANTY; without even the implied warranty of
14 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 " General Public License for more details.
16 "
17 " You should have received a copy of the GNU General Public License
18 " along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 "
20 " Authors: Bart Trojanowski <bart@jukie.net>
21
22 " --- configuration defaults {{{1
23
24 let s:notmuch_defaults = {
25         \ 'g:notmuch_cmd':                           'notmuch'                    ,
26         \
27         \ 'g:notmuch_search_newest_first':           1                            ,
28         \ 'g:notmuch_search_from_column_width':      20                           ,
29         \
30         \ 'g:notmuch_show_fold_signatures':          1                            ,
31         \ 'g:notmuch_show_fold_citations':           1                            ,
32         \ 'g:notmuch_show_fold_bodies':              0                            ,
33         \ 'g:notmuch_show_fold_headers':             1                            ,
34         \
35         \ 'g:notmuch_show_message_begin_regexp':     '^\fmessage{'                ,
36         \ 'g:notmuch_show_message_end_regexp':       '^\fmessage}'                ,
37         \ 'g:notmuch_show_header_begin_regexp':      '^\fheader{'                 ,
38         \ 'g:notmuch_show_header_end_regexp':        '^\fheader}'                 ,
39         \ 'g:notmuch_show_body_begin_regexp':        '^\fbody{'                   ,
40         \ 'g:notmuch_show_body_end_regexp':          '^\fbody}'                   ,
41         \ 'g:notmuch_show_attachment_begin_regexp':  '^\fattachment{'             ,
42         \ 'g:notmuch_show_attachment_end_regexp':    '^\fattachment}'             ,
43         \ 'g:notmuch_show_part_begin_regexp':        '^\fpart{'                   ,
44         \ 'g:notmuch_show_part_end_regexp':          '^\fpart}'                   ,
45         \ 'g:notmuch_show_marker_regexp':            '^\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$',
46         \
47         \ 'g:notmuch_show_message_parse_regexp':     '\(id:[^ ]*\) depth:\([0-9]*\) filename:\(.*\)$',
48         \ 'g:notmuch_show_tags_regexp':              '(\([^)]*\))$'               ,
49         \
50         \ 'g:notmuch_show_signature_regexp':         '^\(-- \?\|_\+\)$'           ,
51         \ 'g:notmuch_show_signature_lines_max':      12                           ,
52         \
53         \ 'g:notmuch_show_citation_regexp':          '^\s*>'                      ,
54         \ }
55
56 " defaults for g:notmuch_initial_search_words
57 " override with: let g:notmuch_initial_search_words = [ ... ]
58 let s:notmuch_initial_search_words_defaults = [
59         \ 'tag:inbox and tag:unread',
60         \ ]
61
62 " defaults for g:notmuch_show_headers
63 " override with: let g:notmuch_show_headers = [ ... ]
64 let s:notmuch_show_headers_defaults = [
65         \ 'Subject',
66         \ 'To',
67         \ 'Cc',
68         \ 'Bcc',
69         \ 'Date',
70         \ ]
71
72 " defaults for g:notmuch_folders
73 " override with: let g:notmuch_folders = [ ... ]
74 let s:notmuch_folders_defaults = [
75         \ [ 'new',    'tag:inbox and tag:unread' ],
76         \ [ 'inbox',  'tag:inbox'                ],
77         \ [ 'unread', 'tag:unread'               ],
78         \ ]
79
80 " --- keyboard mapping definitions {{{1
81
82 " --- --- bindings for folders mode {{{2
83
84 let g:notmuch_folders_maps = {
85         \ 's':          ':call <SID>NM_search_prompt()<CR>',
86         \ 'q':          ':call <SID>NM_kill_this_buffer()<CR>',
87         \ '=':          ':call <SID>NM_folders_refresh_view()<CR>',
88         \ '<Enter>':    ':call <SID>NM_folders_show_search()<CR>',
89         \ }
90
91 " --- --- bindings for search screen {{{2
92 let g:notmuch_search_maps = {
93         \ '<Enter>':    ':call <SID>NM_search_show_thread()<CR>',
94         \ '<C-]>':      ':call <SID>NM_search_expand(''<cword>'')<CR>',
95         \ 'a':          ':call <SID>NM_search_archive_thread()<CR>',
96         \ 'f':          ':call <SID>NM_search_filter()<CR>',
97         \ 'm':          ':call <SID>NM_new_mail()<CR>',
98         \ 'o':          ':call <SID>NM_search_toggle_order()<CR>',
99         \ 'r':          ':call <SID>NM_search_reply_to_thread()<CR>',
100         \ 's':          ':call <SID>NM_search_prompt()<CR>',
101         \ ',s':         ':call <SID>NM_search_edit()<CR>',
102         \ 't':          ':call <SID>NM_search_filter_by_tag()<CR>',
103         \ 'q':          ':call <SID>NM_kill_this_buffer()<CR>',
104         \ '+':          ':call <SID>NM_search_add_tags([])<CR>',
105         \ '-':          ':call <SID>NM_search_remove_tags([])<CR>',
106         \ '=':          ':call <SID>NM_search_refresh_view()<CR>',
107         \ '?':          ':echo <SID>NM_search_thread_id()<CR>',
108         \ }
109
110 " --- --- bindings for show screen {{{2
111 let g:notmuch_show_maps = {
112         \ '<C-P>':      ':call <SID>NM_show_previous(1)<CR>',
113         \ '<C-N>':      ':call <SID>NM_show_next(1)<CR>',
114         \ '<C-]>':      ':call <SID>NM_search_expand(''<cword>'')<CR>',
115         \ 'q':          ':call <SID>NM_kill_this_buffer()<CR>',
116         \
117         \ 'b':          ':call <SID>NM_show_fold_toggle(''b'', ''bdy'', !g:notmuch_show_fold_bodies)<CR>',
118         \ 'c':          ':call <SID>NM_show_fold_toggle(''c'', ''cit'', !g:notmuch_show_fold_citations)<CR>',
119         \ 'h':          ':call <SID>NM_show_fold_toggle(''h'', ''hdr'', !g:notmuch_show_fold_headers)<CR>',
120         \ 's':          ':call <SID>NM_show_fold_toggle(''s'', ''sig'', !g:notmuch_show_fold_signatures)<CR>',
121         \
122         \ 'a':          ':call <SID>NM_show_archive_thread()<CR>',
123         \ 'A':          ':call <SID>NM_show_mark_read_then_archive_thread()<CR>',
124         \ 'N':          ':call <SID>NM_show_mark_read_then_next_open_message()<CR>',
125         \ 'v':          ':call <SID>NM_show_view_all_mime_parts()<CR>',
126         \ '+':          ':call <SID>NM_show_add_tag()<CR>',
127         \ '-':          ':call <SID>NM_show_remove_tag()<CR>',
128         \ '<Space>':    ':call <SID>NM_show_advance_marking_read_and_archiving()<CR>',
129         \ '\|':         ':call <SID>NM_show_pipe_message()<CR>',
130         \
131         \ '<S-Tab>':    ':call <SID>NM_show_previous_fold()<CR>',
132         \ '<Tab>':      ':call <SID>NM_show_next_fold()<CR>',
133         \ '<Enter>':    ':call <SID>NM_show_toggle_fold()<CR>',
134         \
135         \ 'r':          ':call <SID>NM_show_reply()<CR>',
136         \ 'm':          ':call <SID>NM_new_mail()<CR>',
137         \ '?':          ':echo <SID>NM_show_message_id() . ''  @ '' . join(<SID>NM_show_search_words())<CR>',
138         \ }
139
140
141 " --- implement folders screen {{{1
142
143 function! s:NM_cmd_folders(words)
144         if len(a:words)
145                 echoe 'Not exapecting any arguments for folders command.'
146         endif
147         let cmd = ['count']
148         let disp = []
149         let searches = []
150         for entry in g:notmuch_folders
151                 let [ name, search ] = entry
152                 let data = s:NM_run(cmd + [search])
153                 let cnt = matchlist(data, '\(\d\+\)')[1]
154                 call add(disp, printf('%9d %-20s (%s)', cnt, name, search))
155                 call add(searches, search)
156         endfor
157
158         call <SID>NM_newBuffer('folders', join(disp, "\n"))
159         let b:nm_searches = searches
160         let b:nm_timestamp = reltime()
161
162         call <SID>NM_cmd_folders_mksyntax()
163         call <SID>NM_set_map(g:notmuch_folders_maps)
164         setlocal cursorline
165         setlocal nowrap
166 endfunction
167
168 function! s:NM_cmd_folders_mksyntax()
169 endfunction
170
171 " --- --- folders screen action functions {{{2
172
173 function! s:NM_folders_refresh_view()
174         let lno = line('.')
175         setlocal bufhidden=delete
176         call s:NM_cmd_folders([])
177         exec printf('norm %dG', lno)
178 endfunction
179
180 function! s:NM_folders_show_search()
181         let line = line('.')
182         let search = b:nm_searches[line-1]
183
184         let prev_bufnr = bufnr('%')
185         setlocal bufhidden=hide
186         call <SID>NM_cmd_search([search])
187         setlocal bufhidden=delete
188         let b:nm_prev_bufnr = prev_bufnr
189 endfunction
190
191
192 " --- implement search screen {{{1
193
194 function! s:NM_cmd_search(words)
195         let cmd = ['search']
196         if g:notmuch_search_newest_first
197                 let cmd = cmd + ['--sort=newest-first']
198         else
199                 let cmd = cmd + ['--sort=oldest-first']
200         endif
201         let data = s:NM_run(cmd + a:words)
202         let lines = split(data, "\n")
203         let disp = copy(lines)
204         call map(disp, 's:NM_cmd_search_fmtline(v:val)')
205
206         call <SID>NM_newBuffer('search', join(disp, "\n"))
207         let b:nm_raw_lines = lines
208         let b:nm_search_words = a:words
209
210         call <SID>NM_cmd_search_mksyntax()
211         call <SID>NM_set_map(g:notmuch_search_maps)
212         setlocal cursorline
213         setlocal nowrap
214 endfunction
215 function! s:NM_cmd_search_fmtline(line)
216         let m = matchlist(a:line, '^\(thread:\S\+\)\s\([^]]\+\]\) \([^;]\+\); \(.*\) (\([^(]*\))$')
217         if !len(m)
218                 return 'ERROR PARSING: ' . a:line
219         endif
220         let max = g:notmuch_search_from_column_width
221         let from = m[3]
222         if strlen(from) >= max
223                 let from = substitute(m[3][0:max-4], '[^A-Za-z1-9_]*$', '', '') . '...'
224         endif
225         return printf('%-20s %-20s | %s (%s)', m[2], from, m[4], m[5])
226 endfunction
227 function! s:NM_cmd_search_mksyntax()
228         syntax clear nmSearchFrom
229         exec printf('syntax match nmSearchFrom /\(\] \)\@<=.\{%d\}/ oneline contained', g:notmuch_search_from_column_width)
230 endfunction
231
232 " --- --- search screen action functions {{{2
233
234 function! s:NM_search_show_thread()
235         let id = <SID>NM_search_thread_id()
236         if id != ''
237                 let words = [id]
238                 if exists('b:nm_search_words')
239                         let words = ['('] + b:nm_search_words + [')', 'and', id]
240                 endif
241                 if len(words)
242                         call <SID>NM_cmd_show(words)
243                 endif
244         endif
245 endfunction
246
247 function! s:NM_search_prompt()
248         " TODO: input() can support completion
249         let text = input('NotMuch Search: ')
250         if strlen(text)
251                 let tags = split(text)
252         else
253                 let tags = s:notmuch_initial_search_words_defaults
254         endif
255         let prev_bufnr = bufnr('%')
256         if b:nm_type == 'search'
257                 " TODO: we intend to replace the current buffer,
258                 "       ... maybe we could just clear it
259                 let prev_bufnr = b:nm_prev_bufnr
260                 setlocal bufhidden=delete
261         else
262                 setlocal bufhidden=hide
263         endif
264         call <SID>NM_cmd_search(tags)
265         setlocal bufhidden=delete
266         let b:nm_prev_bufnr = prev_bufnr
267 endfunction
268
269 function! s:NM_search_edit()
270         " TODO: input() can support completion
271         let text = input('NotMuch Search: ', join(b:nm_search_words, ' '))
272         if strlen(text)
273                 call <SID>NM_cmd_search(split(text))
274         endif
275 endfunction
276
277 function! s:NM_search_archive_thread()
278         call <SID>NM_add_remove_tags_on_screen('-', ['inbox'])
279         call <SID>NM_add_remove_tags('-', ['inbox'])
280         norm j
281 endfunction
282
283 function! s:NM_search_filter()
284         call <SID>NM_search_filter_helper('Filter: ', '', '')
285 endfunction
286
287 function! s:NM_search_filter_by_tag()
288         call <SID>NM_search_filter_helper('Filter Tag(s): ', 'tag:', 'and')
289 endfunction
290
291 function! s:NM_search_filter_helper(prompt, prefix, joiner)
292         " TODO: input() can support completion
293         let text = input(a:prompt)
294         if !strlen(text)
295                 return
296         endif
297
298         let tags = split(text)
299         if strlen(a:prefix)
300                 call map(tags, 'a:prefix . v:val')
301         endif
302         if strlen(a:joiner)
303                 let idx = len(tags) - 1
304                 while idx > 0
305                         call insert(tags, a:joiner, idx)
306                         let idx = idx - 1
307                 endwhile
308         endif
309         let tags = b:nm_search_words + ['and', '('] + tags + [')']
310
311         let prev_bufnr = bufnr('%')
312         setlocal bufhidden=hide
313         call <SID>NM_cmd_search(tags)
314         setlocal bufhidden=delete
315         let b:nm_prev_bufnr = prev_bufnr
316 endfunction
317
318 function! s:NM_search_toggle_order()
319         let g:notmuch_search_newest_first = !g:notmuch_search_newest_first
320         " FIXME: maybe this would be better done w/o reading re-reading the lines
321         "         reversing the b:nm_raw_lines and the buffer lines would be better
322         call <SID>NM_search_refresh_view()
323 endfunction
324
325 function! s:NM_search_reply_to_thread()
326         echo 'not implemented'
327 endfunction
328
329 function! s:NM_search_add_tags(tags)
330         call <SID>NM_search_add_remove_tags('Add Tag(s): ', '+', a:tags)
331 endfunction
332
333 function! s:NM_search_remove_tags(tags)
334         call <SID>NM_search_add_remove_tags('Remove Tag(s): ', '-', a:tags)
335 endfunction
336
337 function! s:NM_search_refresh_view()
338         let lno = line('.')
339         let prev_bufnr = b:nm_prev_bufnr
340         setlocal bufhidden=delete
341         call <SID>NM_cmd_search(b:nm_search_words)
342         let b:nm_prev_bufnr = prev_bufnr
343         " FIXME: should find the line of the thread we were on if possible
344         exec printf('norm %dG', lno)
345 endfunction
346
347 " --- --- search screen helper functions {{{2
348
349 function! s:NM_search_thread_id()
350         if !exists('b:nm_raw_lines')
351                 echoe 'no b:nm_raw_lines'
352                 return ''
353         else
354                 let line = line('.')
355                 let info = b:nm_raw_lines[line-1]
356                 let what = split(info, '\s\+')[0]
357                 return what
358         endif
359 endfunction
360
361 function! s:NM_search_add_remove_tags(prompt, prefix, intags)
362         if type(a:intags) != type([]) || len(a:intags) == 0
363                 " TODO: input() can support completion
364                 let text = input(a:prompt)
365                 if !strlen(text)
366                         return
367                 endif
368                 let tags = split(text, ' ')
369         else
370                 let tags = a:intags
371         endif
372         call <SID>NM_add_remove_tags(a:prefix, tags)
373         call <SID>NM_add_remove_tags_on_screen(a:prefix, tags)
374 endfunction
375
376 " --- implement show screen {{{1
377
378 function! s:NM_cmd_show(words)
379         let prev_bufnr = bufnr('%')
380         let data = s:NM_run(['show'] + a:words)
381         let lines = split(data, "\n")
382
383         let info = s:NM_cmd_show_parse(lines)
384
385         setlocal bufhidden=hide
386         call <SID>NM_newBuffer('show', join(info['disp'], "\n"))
387         setlocal bufhidden=delete
388         let b:nm_words = a:words
389         let b:nm_raw_info = info
390         let b:nm_prev_bufnr = prev_bufnr
391
392         call <SID>NM_cmd_show_mkfolds()
393         call <SID>NM_cmd_show_mksyntax()
394         call <SID>NM_set_map(g:notmuch_show_maps)
395         setlocal foldtext=NM_cmd_show_foldtext()
396         setlocal fillchars=
397         setlocal foldcolumn=6
398
399 endfunction
400
401 function! s:NM_show_previous(can_change_thread)
402         let info = b:nm_raw_info
403         let lnum = line('.')
404         for msg in reverse(copy(info['msgs']))
405                 if lnum <= msg['start']
406                         continue
407                 endif
408
409                 exec printf('norm %dG', msg['start'])
410                 " TODO: try to fit the message on screen
411                 norm zz
412                 return
413         endfor
414         if !a:can_change_thread
415                 return
416         endif
417         call <SID>NM_kill_this_buffer()
418         if line('.') != line('0')
419                 norm k
420                 call <SID>NM_search_show_thread()
421                 norm G
422                 call <SID>NM_show_previous(0)
423         else
424                 echo 'No more messages.'
425         endif
426 endfunction
427
428 function! s:NM_show_next(can_change_thread)
429         let info = b:nm_raw_info
430         let lnum = line('.')
431         for msg in info['msgs']
432                 if lnum >= msg['start']
433                         continue
434                 endif
435
436                 exec printf('norm %dG', msg['start'])
437                 " TODO: try to fit the message on screen
438                 norm zz
439                 return
440         endfor
441         if !a:can_change_thread
442                 return
443         endif
444         call <SID>NM_kill_this_buffer()
445         if line('.') != line('$')
446                 norm j
447                 call <SID>NM_search_show_thread()
448         else
449                 echo 'No more messages.'
450         endif
451 endfunction
452
453 function! s:NM_show_archive_thread()
454         echo 'not implemented'
455 endfunction
456
457 function! s:NM_show_mark_read_then_archive_thread()
458         echo 'not implemented'
459 endfunction
460
461 function! s:NM_show_next_message()
462         echo 'not implemented'
463 endfunction
464
465 function! s:NM_show_mark_read_then_next_open_message()
466         echo 'not implemented'
467 endfunction
468
469 function! s:NM_show_previous_message()
470         echo 'not implemented'
471 endfunction
472
473 function! s:NM_show_reply()
474         echo 'not implemented'
475 endfunction
476
477 function! s:NM_show_view_all_mime_parts()
478         echo 'not implemented'
479 endfunction
480
481 function! s:NM_show_view_raw_message()
482         echo 'not implemented'
483 endfunction
484
485 function! s:NM_show_add_tag()
486         echo 'not implemented'
487 endfunction
488
489 function! s:NM_show_remove_tag()
490         echo 'not implemented'
491 endfunction
492
493 function! s:NM_show_advance_marking_read_and_archiving()
494         echo 'not implemented'
495 endfunction
496
497 function! s:NM_show_pipe_message()
498         echo 'not implemented'
499 endfunction
500
501 function! s:NM_show_previous_fold()
502         echo 'not implemented'
503 endfunction
504
505 function! s:NM_show_next_fold()
506         echo 'not implemented'
507 endfunction
508
509 function! s:NM_show_toggle_fold()
510         echo 'not implemented'
511 endfunction
512
513
514 " --- --- show screen helper functions {{{2
515
516 function! s:NM_show_message_id()
517         if !exists('b:nm_raw_info')
518                 echoe 'no b:nm_raw_info'
519                 return ''
520         endif
521         let info = b:nm_raw_info
522         let lnum = line('.')
523         for msg in info['msgs']
524                 if lnum > msg['end']
525                         continue
526                 endif
527
528                 return msg['id']
529         endfor
530         return ''
531 endfunction
532
533 function! s:NM_show_search_words()
534         if !exists('b:nm_words')
535                 echoe 'no b:nm_words'
536                 return []
537         endif
538         return b:nm_words
539 endfunction
540
541 function! s:NM_show_fold_toggle(key, type, fold)
542         let info = b:nm_raw_info
543         let act = 'open'
544         if a:fold
545                 let act = 'close'
546         endif
547         for fld in info['folds']
548                 if fld[0] == a:type
549                         exec printf('%dfold%s', fld[1], act)
550                 endif
551         endfor
552         exec printf('nnoremap <buffer> %s :call <SID>NM_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
553 endfunction
554
555
556 " s:NM_cmd_show_parse returns the following dictionary:
557 "    'disp':     lines to display
558 "    'msgs':     message info dicts { start, end, id, depth, filename, descr, header }
559 "    'folds':    fold info arrays [ type, start, end ]
560 "    'foldtext': fold text indexed by start line
561 function! s:NM_cmd_show_parse(inlines)
562         let info = { 'disp': [],       
563                    \ 'msgs': [],       
564                    \ 'folds': [],      
565                    \ 'foldtext': {} }  
566         let msg = {}
567         let hdr = {}
568
569         let in_message = 0
570         let in_header = 0
571         let in_body = 0
572         let in_part = ''
573
574         let body_start = -1
575         let part_start = -1
576
577         let mode_type = ''
578         let mode_start = -1
579
580         let inlnum = 0
581         for line in a:inlines
582                 let inlnum = inlnum + 1
583                 let foldinfo = []
584
585                 if strlen(in_part)
586                         let part_end = 0
587
588                         if match(line, g:notmuch_show_part_end_regexp) != -1
589                                 let part_end = len(info['disp'])
590                         else
591                                 call add(info['disp'], line)
592                         endif
593
594                         if in_part == 'text/plain'
595                                 if !part_end && mode_type == ''
596                                         if match(line, g:notmuch_show_signature_regexp) != -1
597                                                 let mode_type = 'sig'
598                                                 let mode_start = len(info['disp'])
599                                         elseif match(line, g:notmuch_show_citation_regexp) != -1
600                                                 let mode_type = 'cit'
601                                                 let mode_start = len(info['disp'])
602                                         endif
603                                 elseif mode_type == 'cit'
604                                         if part_end || match(line, g:notmuch_show_citation_regexp) == -1
605                                                 let outlnum = len(info['disp'])
606                                                 let foldinfo = [ mode_type, mode_start, outlnum-1,
607                                                                \ printf('[ %d-line citation.  Press "c" to show. ]', outlnum - mode_start) ]
608                                                 let mode_type = ''
609                                         endif
610                                 elseif mode_type == 'sig'
611                                         let outlnum = len(info['disp'])
612                                         if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
613                                                 let mode_type = ''
614                                         elseif part_end
615                                                 let foldinfo = [ mode_type, mode_start, outlnum-1,
616                                                                \ printf('[ %d-line signature.  Press "s" to show. ]', outlnum - mode_start) ]
617                                                 let mode_type = ''
618                                         endif
619                                 endif
620                         endif
621
622                         if part_end
623                                 " FIXME: this is a hack for handling two folds being added for one line
624                                 "         we should handle addinga fold in a function
625                                 if len(foldinfo) && foldinfo[1] < foldinfo[2]
626                                         call add(info['folds'], foldinfo[0:2])
627                                         let info['foldtext'][foldinfo[1]] = foldinfo[3]
628                                 endif
629
630                                 let foldinfo = [ 'text', part_start, part_end,
631                                                \ printf('[ %d-line %s.  Press "p" to show. ]', part_end - part_start, in_part) ]
632                                 let in_part = ''
633                                 call add(info['disp'], '')
634                         endif
635
636                 elseif in_body
637                         if !has_key(msg,'body_start')
638                                 let msg['body_start'] = len(info['disp']) + 1
639                         endif
640                         if match(line, g:notmuch_show_body_end_regexp) != -1
641                                 let body_end = len(info['disp'])
642                                 let foldinfo = [ 'bdy', body_start, body_end,
643                                                \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
644
645                                 let in_body = 0
646
647                         elseif match(line, g:notmuch_show_part_begin_regexp) != -1
648                                 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
649                                 let in_part = 'unknown'
650                                 if len(m)
651                                         let in_part = m[2]
652                                 endif
653                                 call add(info['disp'],
654                                          \ printf('--- %s ---', in_part))
655                                 let part_start = len(info['disp']) + 1
656                         endif
657
658                 elseif in_header
659                         if in_header == 1
660                                 let msg['descr'] = line
661                                 call add(info['disp'], line)
662                                 let in_header = 2
663                                 let msg['hdr_start'] = len(info['disp']) + 1
664
665                         else
666                                 if match(line, g:notmuch_show_header_end_regexp) != -1
667                                         let hdr_start = msg['hdr_start']+1
668                                         let hdr_end = len(info['disp'])
669                                         let foldinfo = [ 'hdr', hdr_start, hdr_end,
670                                                \ printf('[ %d-line headers.  Press "h" to show. ]', hdr_end + 1 - hdr_start) ]
671                                         let msg['header'] = hdr
672                                         let in_header = 0
673                                         let hdr = {}
674                                 else
675                                         let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
676                                         if len(m)
677                                                 let hdr[m[1]] = m[2]
678                                                 if match(g:notmuch_show_headers, m[1]) != -1
679                                                         call add(info['disp'], line)
680                                                 endif
681                                         endif
682                                 endif
683                         endif
684
685                 elseif in_message
686                         if match(line, g:notmuch_show_message_end_regexp) != -1
687                                 let msg['end'] = len(info['disp'])
688                                 call add(info['disp'], '')
689
690                                 let foldinfo = [ 'msg', msg['start'], msg['end'],
691                                                \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
692
693                                 call add(info['msgs'], msg)
694                                 let msg = {}
695                                 let in_message = 0
696                                 let in_header = 0
697                                 let in_body = 0
698                                 let in_part = ''
699
700                         elseif match(line, g:notmuch_show_header_begin_regexp) != -1
701                                 let in_header = 1
702                                 continue
703
704                         elseif match(line, g:notmuch_show_body_begin_regexp) != -1
705                                 let body_start = len(info['disp']) + 1
706                                 let in_body = 1
707                                 continue
708                         endif
709
710                 else
711                         if match(line, g:notmuch_show_message_begin_regexp) != -1
712                                 let msg['start'] = len(info['disp']) + 1
713
714                                 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
715                                 if len(m)
716                                         let msg['id'] = m[1]
717                                         let msg['depth'] = m[2]
718                                         let msg['filename'] = m[3]
719                                 endif
720
721                                 let in_message = 1
722                         endif
723                 endif
724
725                 if len(foldinfo) && foldinfo[1] < foldinfo[2]
726                         call add(info['folds'], foldinfo[0:2])
727                         let info['foldtext'][foldinfo[1]] = foldinfo[3]
728                 endif
729         endfor
730         return info
731 endfunction
732
733 function! s:NM_cmd_show_mkfolds()
734         let info = b:nm_raw_info
735
736         for afold in info['folds']
737                 exec printf('%d,%dfold', afold[1], afold[2])
738                 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
739                  \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
740                  \ || (afold[0] == 'bdy' && g:notmuch_show_fold_bodies)
741                  \ || (afold[0] == 'hdr' && g:notmuch_show_fold_headers)
742                         exec printf('%dfoldclose', afold[1])
743                 else
744                         exec printf('%dfoldopen', afold[1])
745                 endif
746         endfor
747 endfunction
748
749 function! s:NM_cmd_show_mksyntax()
750         let info = b:nm_raw_info
751         let cnt = 0
752         for msg in info['msgs']
753                 let cnt = cnt + 1
754                 let start = msg['start']
755                 let hdr_start = msg['hdr_start']
756                 let body_start = msg['body_start']
757                 let end = msg['end']
758                 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
759                 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
760                 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
761         endfor
762 endfunction
763
764 function! NM_cmd_show_foldtext()
765         let foldtext = b:nm_raw_info['foldtext']
766         return foldtext[v:foldstart]
767 endfunction
768
769
770 " --- notmuch helper functions {{{1
771
772 function! s:NM_newBuffer(type, content)
773         enew
774         setlocal buftype=nofile readonly modifiable
775         silent put=a:content
776         keepjumps 0d
777         setlocal nomodifiable
778         execute printf('set filetype=notmuch-%s', a:type)
779         execute printf('set syntax=notmuch-%s', a:type)
780         let b:nm_type = a:type
781 endfunction
782
783 function! s:NM_shell_escape(word)
784         let word = substitute(a:word, '''', '\\''', 'g')
785         return '''' . word . ''''
786 endfunction
787
788 function! s:NM_run(args)
789         let words = a:args
790         call map(words, 's:NM_shell_escape(v:val)')
791         let cmd = g:notmuch_cmd . ' ' . join(words) . '< /dev/null'
792
793         let start = reltime()
794         let out = system(cmd)
795         let err = v:shell_error
796         let delta = reltime(start)
797
798         echo printf('[%s] {%s} %s', reltimestr(delta), string(err), string(cmd))
799
800         if err
801                 echohl Error
802                 echo substitute(out, '\n*$', '', '')
803                 echohl None
804                 return ''
805         else
806                 return out
807         endif
808 endfunction
809
810 " --- external mail handling helpers {{{1
811
812 function! s:NM_new_mail()
813         echo 'not implemented'
814 endfunction
815
816 " --- other helpers {{{1
817
818 function! s:NM_kill_this_buffer()
819         if exists('b:nm_prev_bufnr')
820                 setlocal bufhidden=delete
821                 exec printf(":buffer %d", b:nm_prev_bufnr)
822         else
823                 echo "This is the last buffer; use :q<CR> to quit."
824         endif
825 endfunction
826
827 function! s:NM_search_expand(arg)
828         let word = expand(a:arg)
829         let prev_bufnr = bufnr('%')
830         setlocal bufhidden=hide
831         call <SID>NM_cmd_search([word])
832         setlocal bufhidden=delete
833         let b:nm_prev_bufnr = prev_bufnr
834 endfunction
835
836 function! s:NM_add_remove_tags(prefix, tags)
837         let id = <SID>NM_search_thread_id()
838         if id == ''
839                 echoe 'Eeek! I couldn''t find the thead id!'
840         endif
841         call map(a:tags, 'a:prefix . v:val')
842         " TODO: handle errors
843         call <SID>NM_run(['tag'] + a:tags + ['--', id])
844 endfunction
845
846 function! s:NM_add_remove_tags_on_screen(prefix, tags)
847         let online = ''
848         setlocal modifiable
849         if a:prefix == '-'
850                 for tagname in a:tags
851                         exec printf('silent %ss/(\([^)]*\)\<%s\>\([^)]*\))$/(\1\2)/', online, tagname)
852                 endfor
853         else
854                 for tagname in a:tags
855                         exec printf('silent %ss/(\([^)]*\)\([^)]*\))$/(\1 %s)/', online, tagname)
856                 endfor
857         endif
858         setlocal nomodifiable
859 endfunction
860
861 " --- process and set the defaults {{{1
862
863 function! NM_set_defaults(force)
864         for [key, dflt] in items(s:notmuch_defaults)
865                 let cmd = ''
866                 if !a:force && exists(key) && type(dflt) == type(eval(key))
867                         continue
868                 elseif type(dflt) == type(0)
869                         let cmd = printf('let %s = %d', key, dflt)
870                 elseif type(dflt) == type('')
871                         let cmd = printf('let %s = ''%s''', key, dflt)
872                 " FIXME: not sure why this didn't work when dflt is an array
873                 "elseif type(dflt) == type([])
874                 "        let cmd = printf('let %s = %s', key, string(dflt))
875                 else
876                         echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
877                                                 \ a:force, key, string(dflt))
878                         continue
879                 endif
880                 exec cmd
881         endfor
882 endfunction
883 call NM_set_defaults(0)
884
885 " for some reason NM_set_defaults() didn't work for arrays...
886 if !exists('g:notmuch_show_headers')
887         let g:notmuch_show_headers = s:notmuch_show_headers_defaults
888 endif
889 if !exists('g:notmuch_initial_search_words')
890         let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
891 endif
892 if !exists('g:notmuch_folders')
893         let g:notmuch_folders = s:notmuch_folders_defaults
894 endif
895
896
897 " --- assign keymaps {{{1
898
899 function! s:NM_set_map(maps)
900         nmapclear
901         for [key, code] in items(a:maps)
902                 exec printf('nnoremap <buffer> %s %s', key, code)
903         endfor
904         " --- this is a hack for development :)
905         nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
906 endfunction
907
908 " --- command handler {{{1
909
910 function! NotMuch(args)
911         let args = a:args
912         if !strlen(args)
913                 let args = 'folders'
914         endif
915
916         let words = split(args)
917         if words[0] == 'folders'
918                 let words = words[1:]
919                 call <SID>NM_cmd_folders(words)
920         elseif words[0] == 'search'
921                 if len(words) > 1
922                         let words = words[1:]
923                 elseif exists('b:nm_search_words')
924                         let words = b:nm_search_words
925                 else
926                         let words = g:notmuch_initial_search_words
927                 endif
928                 call <SID>NM_cmd_search(words)
929
930         elseif words[0] == 'show'
931                 echoe 'show is not yet implemented.'
932         endif
933 endfunction
934 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
935         return []
936 endfunction
937
938
939 " --- glue {{{1
940
941 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
942 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
943
944 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :