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