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