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