1 " notmuch.vim plugin --- run notmuch within vim
3 " Copyright © Carl Worth
5 " This file is part of Notmuch.
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.
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.
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/>.
20 " Authors: Bart Trojanowski <bart@jukie.net>
22 " --- configuration defaults {{{1
24 let s:notmuch_defaults = {
25 \ 'g:notmuch_cmd': 'notmuch' ,
27 \ 'g:notmuch_search_newest_first': 1 ,
28 \ 'g:notmuch_search_from_column_width': 20 ,
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 ,
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\\)[{}].*$',
47 \ 'g:notmuch_show_message_parse_regexp': '\(id:[^ ]*\) depth:\([0-9]*\) filename:\(.*\)$',
48 \ 'g:notmuch_show_tags_regexp': '(\([^)]*\))$' ,
50 \ 'g:notmuch_show_signature_regexp': '^\(-- \?\|_\+\)$' ,
51 \ 'g:notmuch_show_signature_lines_max': 12 ,
53 \ 'g:notmuch_show_citation_regexp': '^\s*>' ,
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',
62 " defaults for g:notmuch_show_headers
63 " override with: let g:notmuch_show_headers = [ ... ]
64 let s:notmuch_show_headers_defaults = [
72 " --- keyboard mapping definitions {{{1
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>',
91 " --- --- bindings for show screen {{{2
92 let g:notmuch_show_maps = {
93 \ '<C-P>': ':call <SID>NM_show_prev()<CR>',
94 \ '<C-N>': ':call <SID>NM_show_next()<CR>',
95 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
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>',
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>',
111 \ 'r': ':call <SID>NM_show_reply()<CR>',
112 \ 'm': ':call <SID>NM_new_mail()<CR>',
116 " --- implement search screen {{{1
118 function! s:NM_cmd_search(words)
120 if g:notmuch_search_newest_first
121 let cmd = cmd + ['--sort=newest-first']
123 let cmd = cmd + ['--sort=oldest-first']
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)')
133 call <SID>NM_newBuffer('search', join(disp, "\n"))
134 let b:nm_raw_lines = lines
135 let b:nm_search_words = a:words
137 call <SID>NM_cmd_search_mksyntax()
138 call <SID>NM_set_map(g:notmuch_search_maps)
142 function! s:NM_cmd_search_fmtline(line)
143 let m = matchlist(a:line, '^\(thread:\S\+\)\s\([^]]\+\]\) \([^;]\+\); \(.*\) (\([^(]*\))$')
145 return 'ERROR PARSING: ' . a:line
147 let max = g:notmuch_search_from_column_width
149 if strlen(from) >= max
150 let from = m[3][0:max-4] . '...'
152 return printf('%s %-20s | %s (%s)', m[2], from, m[4], m[5])
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)
162 " --- --- search screen action functions {{{2
164 function! s:NM_search_show_thread()
165 let id = <SID>NM_search_find_thread_id()
167 call <SID>NM_cmd_show([id])
171 function! s:NM_search_prompt()
172 " TODO: input() can support completion
173 let text = input('NotMuch Search: ')
175 let tags = split(text)
177 let tags = s:notmuch_initial_search_words_defaults
179 setlocal bufhidden=delete
180 call <SID>NM_cmd_search(tags)
183 function! s:NM_search_edit()
184 " TODO: input() can support completion
185 let text = input('NotMuch Search: ', join(b:nm_search_words, ' '))
187 call <SID>NM_cmd_search(split(text))
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
195 s/(\([^)]*\)\<inbox\>\([^)]*\))$/(\1\2)/
196 setlocal nomodifiable
200 function! s:NM_search_filter()
201 call <SID>NM_search_filter_helper('Filter: ', '')
204 function! s:NM_search_filter_by_tag()
205 call <SID>NM_search_filter_helper('Filter Tag(s): ', 'tag:')
208 function! s:NM_search_filter_helper(prompt, prefix)
209 " TODO: input() can support completion
210 let text = input(a:prompt)
215 let tags = split(text)
216 map(tags, 'and a:prefix . v:val')
217 let tags = b:nm_search_words + tags
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
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()
234 function! s:NM_search_reply_to_thread()
235 echo 'not implemented'
238 function! s:NM_search_add_tags(tags)
239 call <SID>NM_search_add_remove_tags('Add Tag(s): ', '+', a:tags)
242 function! s:NM_search_remove_tags(tags)
243 call <SID>NM_search_add_remove_tags('Remove Tag(s): ', '-', a:tags)
246 function! s:NM_search_refresh_view()
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)
254 " --- --- search screen helper functions {{{2
256 function! s:NM_search_find_thread_id()
257 if !exists('b:nm_raw_lines')
258 echoe 'no b:nm_raw_lines'
262 let info = b:nm_raw_lines[line-1]
263 let what = split(info, '\s\+')[0]
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)
275 call <SID>NM_add_remove_tags(a:prefix, split(text, ' '))
277 call <SID>NM_add_remove_tags(a:prefix, a:intags)
279 call <SID>NM_search_refresh_view()
282 function! s:NM_add_remove_tags(prefix, tags)
283 let id = <SID>NM_search_find_thread_id()
285 echoe 'Eeek! I couldn''t find the thead id!'
287 call map(a:tags, 'a:prefix . v:val')
288 " TODO: handle errors
289 call <SID>NM_run(['tag'] + a:tags + ['--', id])
292 " --- implement show screen {{{1
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")
299 let info = s:NM_cmd_show_parse(lines)
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
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()
312 setlocal foldcolumn=6
316 function! s:NM_show_prev()
317 echoe "not implemented"
320 function! s:NM_show_next()
321 let info = b:nm_raw_info
324 for msg in info['msgs']
326 if lnum >= msg['start']
330 exec printf('norm %dG', msg['start'])
335 call <SID>NM_search_show_thread()
338 function! s:NM_show_archive_thread()
339 echo 'not implemented'
342 function! s:NM_show_mark_read_then_archive_thread()
343 echo 'not implemented'
346 function! s:NM_show_next_message()
347 echo 'not implemented'
350 function! s:NM_show_mark_read_then_next_open_message()
351 echo 'not implemented'
354 function! s:NM_show_previous_message()
355 echo 'not implemented'
358 function! s:NM_show_reply()
359 echo 'not implemented'
362 function! s:NM_show_view_all_mime_parts()
363 echo 'not implemented'
366 function! s:NM_show_view_raw_message()
367 echo 'not implemented'
370 function! s:NM_show_add_tag()
371 echo 'not implemented'
374 function! s:NM_show_remove_tag()
375 echo 'not implemented'
378 function! s:NM_show_advance_marking_read_and_archiving()
379 echo 'not implemented'
382 function! s:NM_show_pipe_message()
383 echo 'not implemented'
386 " --- --- search screen helper functions {{{2
388 function! s:NM_show_fold_toggle(key, type, fold)
389 let info = b:nm_raw_info
394 for fld in info['folds']
396 exec printf('%dfold%s', fld[1], act)
399 exec printf('nnoremap <buffer> %s :call <SID>NM_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
403 " s:NM_cmd_show_parse returns the following dictionary:
404 " 'disp': lines to display
405 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
406 " 'folds': fold info arrays [ type, start, end ]
407 " 'foldtext': fold text indexed by start line
408 function! s:NM_cmd_show_parse(inlines)
409 let info = { 'disp': [],
428 for line in a:inlines
429 let inlnum = inlnum + 1
435 if match(line, g:notmuch_show_part_end_regexp) != -1
436 let part_end = len(info['disp'])
438 call add(info['disp'], line)
441 if in_part == 'text/plain'
442 if !part_end && mode_type == ''
443 if match(line, g:notmuch_show_signature_regexp) != -1
444 let mode_type = 'sig'
445 let mode_start = len(info['disp'])
446 elseif match(line, g:notmuch_show_citation_regexp) != -1
447 let mode_type = 'cit'
448 let mode_start = len(info['disp'])
450 elseif mode_type == 'cit'
451 if part_end || match(line, g:notmuch_show_citation_regexp) == -1
452 let outlnum = len(info['disp'])
453 let foldinfo = [ mode_type, mode_start, outlnum-1,
454 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
457 elseif mode_type == 'sig'
458 let outlnum = len(info['disp'])
459 if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
462 let foldinfo = [ mode_type, mode_start, outlnum,
463 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
470 " FIXME: this is a hack for handling two folds being added for one line
471 " we should handle addinga fold in a function
473 call add(info['folds'], foldinfo[0:2])
474 let info['foldtext'][foldinfo[1]] = foldinfo[3]
477 let foldinfo = [ 'text', part_start, part_end,
478 \ printf('[ %d-line %s. Press "p" to show. ]', part_end - part_start, in_part) ]
480 call add(info['disp'], '')
484 if !has_key(msg,'body_start')
485 let msg['body_start'] = len(info['disp']) + 1
487 if match(line, g:notmuch_show_body_end_regexp) != -1
488 let body_end = len(info['disp'])
489 let foldinfo = [ 'bdy', body_start, body_end,
490 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
494 elseif match(line, g:notmuch_show_part_begin_regexp) != -1
495 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
496 let in_part = 'unknown'
500 call add(info['disp'],
501 \ printf('--- %s ---', in_part))
502 let part_start = len(info['disp']) + 1
507 let msg['descr'] = line
508 call add(info['disp'], line)
510 let msg['hdr_start'] = len(info['disp']) + 1
513 if match(line, g:notmuch_show_header_end_regexp) != -1
514 let hdr_start = msg['hdr_start']+1
515 let hdr_end = len(info['disp'])
516 let foldinfo = [ 'hdr', hdr_start, hdr_end,
517 \ printf('[ %d-line headers. Press "h" to show. ]', hdr_end - hdr_start) ]
518 let msg['header'] = hdr
522 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
525 if match(g:notmuch_show_headers, m[1]) != -1
526 call add(info['disp'], line)
533 if match(line, g:notmuch_show_message_end_regexp) != -1
534 let msg['end'] = len(info['disp'])
535 call add(info['disp'], '')
537 let foldinfo = [ 'msg', msg['start'], msg['end'],
538 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
540 call add(info['msgs'], msg)
547 elseif match(line, g:notmuch_show_header_begin_regexp) != -1
551 elseif match(line, g:notmuch_show_body_begin_regexp) != -1
552 let body_start = len(info['disp']) + 1
558 if match(line, g:notmuch_show_message_begin_regexp) != -1
559 let msg['start'] = len(info['disp']) + 1
561 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
564 let msg['depth'] = m[2]
565 let msg['filename'] = m[3]
573 call add(info['folds'], foldinfo[0:2])
574 let info['foldtext'][foldinfo[1]] = foldinfo[3]
580 function! s:NM_cmd_show_mkfolds()
581 let info = b:nm_raw_info
583 for afold in info['folds']
584 exec printf('%d,%dfold', afold[1], afold[2])
585 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
586 \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
587 \ || (afold[0] == 'bdy' && g:notmuch_show_fold_bodies)
588 \ || (afold[0] == 'hdr' && g:notmuch_show_fold_headers)
589 exec printf('%dfoldclose', afold[1])
591 exec printf('%dfoldopen', afold[1])
596 function! s:NM_cmd_show_mksyntax()
597 let info = b:nm_raw_info
599 for msg in info['msgs']
601 let start = msg['start']
602 let hdr_start = msg['hdr_start']
603 let body_start = msg['body_start']
605 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
606 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
607 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
611 function! NM_cmd_show_foldtext()
612 let foldtext = b:nm_raw_info['foldtext']
613 return foldtext[v:foldstart]
617 " --- notmuch helper functions {{{1
619 function! s:NM_newBuffer(ft, content)
621 setlocal buftype=nofile readonly modifiable
624 setlocal nomodifiable
625 execute printf('set filetype=notmuch-%s', a:ft)
626 execute printf('set syntax=notmuch-%s', a:ft)
629 function! s:NM_run(args)
630 let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
631 let out = system(cmd)
634 echo substitute(out, '\n*$', '', '')
642 " --- external mail handling helpers {{{1
644 function! s:NM_new_mail()
645 echo 'not implemented'
648 " --- other helpers {{{1
650 function! s:NM_kill_this_buffer()
651 if exists('b:nm_prev_bufnr')
652 setlocal bufhidden=delete
653 exec printf(":buffer %d", b:nm_prev_bufnr)
655 echo "Nothing to kill."
659 " --- process and set the defaults {{{1
661 function! NM_set_defaults(force)
662 for [key, dflt] in items(s:notmuch_defaults)
664 if !a:force && exists(key) && type(dflt) == type(eval(key))
666 elseif type(dflt) == type(0)
667 let cmd = printf('let %s = %d', key, dflt)
668 elseif type(dflt) == type('')
669 let cmd = printf('let %s = ''%s''', key, dflt)
670 "elseif type(dflt) == type([])
671 " let cmd = printf('let %s = %s', key, string(dflt))
673 echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
674 \ a:force, key, string(dflt))
680 call NM_set_defaults(0)
682 " for some reason NM_set_defaults() didn't work for arrays...
683 if !exists('g:notmuch_show_headers')
684 let g:notmuch_show_headers = s:notmuch_show_headers_defaults
686 if !exists('g:notmuch_initial_search_words')
687 let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
691 " --- assign keymaps {{{1
693 function! s:NM_set_map(maps)
694 for [key, code] in items(a:maps)
695 exec printf('nnoremap <buffer> %s %s', key, code)
699 " --- command handler {{{1
701 function! NotMuch(args)
703 if exists('b:nm_search_words')
704 let words = b:nm_search_words
706 let words = g:notmuch_initial_search_words
708 call <SID>NM_cmd_search(words)
714 let words = split(a:args)
715 " TODO: handle commands passed as arguments
717 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
724 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
725 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
727 " --- hacks, only for development :) {{{1
729 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
731 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :