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