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' ,
26 \ 'g:notmuch_search_reverse': 1 ,
27 \ 'g:notmuch_show_fold_signatures': 1 ,
28 \ 'g:notmuch_show_fold_citations': 1 ,
30 \ 'g:notmuch_show_message_begin_regexp': '^
\fmessage{' ,
31 \ 'g:notmuch_show_message_end_regexp': '^
\fmessage}' ,
32 \ 'g:notmuch_show_header_begin_regexp': '^
\fheader{' ,
33 \ 'g:notmuch_show_header_end_regexp': '^
\fheader}' ,
34 \ 'g:notmuch_show_body_begin_regexp': '^
\fbody{' ,
35 \ 'g:notmuch_show_body_end_regexp': '^
\fbody}' ,
36 \ 'g:notmuch_show_attachment_begin_regexp': '^
\fattachment{' ,
37 \ 'g:notmuch_show_attachment_end_regexp': '^
\fattachment}' ,
38 \ 'g:notmuch_show_part_begin_regexp': '^
\fpart{' ,
39 \ 'g:notmuch_show_part_end_regexp': '^
\fpart}' ,
40 \ 'g:notmuch_show_marker_regexp': '^
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$',
42 \ 'g:notmuch_show_message_parse_regexp': '\(id:[^ ]*\) depth:\([0-9]*\) filename:\(.*\)$',
43 \ 'g:notmuch_show_tags_regexp': '(\([^)]*\))$' ,
45 \ 'g:notmuch_show_signature_regexp': '^\(-- \?\|_\+\)$' ,
46 \ 'g:notmuch_show_signature_lines_max': 12 ,
48 \ 'g:notmuch_show_citation_regexp': '^\s*>' ,
51 " defaults for g:notmuch_initial_search_words
52 " override with: let g:notmuch_initial_search_words = [ ... ]
53 let s:notmuch_initial_search_words_defaults = [
57 " defaults for g:notmuch_show_headers
58 " override with: let g:notmuch_show_headers = [ ... ]
59 let s:notmuch_show_headers_defaults = [
64 " --- keyboard mapping definitions {{{1
66 " --- --- bindings for search screen {{{2
67 let g:notmuch_search_maps = {
68 \ '<Enter>': ':call <SID>NM_search_show_thread()<CR>',
69 \ 'a': ':call <SID>NM_search_archive_thread()<CR>',
70 \ 'f': ':call <SID>NM_search_filter()<CR>',
71 \ 'm': ':call <SID>NM_new_mail()<CR>',
72 \ 'o': ':call <SID>NM_search_toggle_order()<CR>',
73 \ 'r': ':call <SID>NM_search_reply_to_thread()<CR>',
74 \ 's': ':call <SID>NM_search_prompt()<CR>',
75 \ 'S': ':call <SID>NM_search_edit()<CR>',
76 \ 't': ':call <SID>NM_search_filter_by_tag()<CR>',
77 \ '+': ':call <SID>NM_search_add_tags([])<CR>',
78 \ '-': ':call <SID>NM_search_remove_tags([])<CR>',
79 \ '=': ':call <SID>NM_search_refresh_view()<CR>',
82 " --- --- bindings for show screen {{{2
83 let g:notmuch_show_maps = {
84 \ 'q': ':call <SID>NM_cmd_show_quit()<CR>',
85 \ '<C-N>': ':call <SID>NM_cmd_show_next()<CR>',
86 \ 'c': ':call <SID>NM_cmd_show_fold_toggle(''c'', ''cit'', !g:notmuch_show_fold_citations)<CR>',
87 \ 's': ':call <SID>NM_cmd_show_fold_toggle(''s'', ''sig'', !g:notmuch_show_fold_signatures)<CR>',
90 " --- implement search screen {{{1
92 function! s:NM_cmd_search(words)
94 if g:notmuch_search_reverse
95 let cmd = cmd + ['--reverse']
97 let g:notmuch_current_search_words = a:words
98 let data = s:NM_run(cmd + a:words)
99 "let data = substitute(data, '27/27', '25/27', '')
100 "let data = substitute(data, '\[4/4\]', '[0/4]', '')
101 let lines = split(data, "\n")
102 let disp = copy(lines)
103 call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
105 call <SID>NM_newBuffer('search', join(disp, "\n"))
106 let b:nm_raw_lines = lines
108 call <SID>NM_set_map(g:notmuch_search_maps)
113 " --- --- search screen action functions {{{2
115 function! s:NM_search_show_thread()
116 let id = <SID>NM_search_find_thread_id()
118 call <SID>NM_cmd_show([id])
122 function! s:NM_search_prompt()
123 " TODO: input() can support completion
124 let text = input('NotMuch Search: ')
126 call <SID>NM_cmd_search(split(text))
130 function! s:NM_search_edit()
131 " TODO: input() can support completion
132 let text = input('NotMuch Search: ', join(g:notmuch_current_search_words, ' '))
134 call <SID>NM_cmd_search(split(text))
138 function! s:NM_search_archive_thread()
139 call <SID>NM_add_remove_tags('-', ['inbox'])
141 s/(\([^)]*\)\<inbox\>\([^)]*\))$/(\1\2)/
142 setlocal nomodifiable
146 function! s:NM_search_filter()
147 echoe 'Not implemented'
150 function! s:NM_new_mail()
151 echoe 'Not implemented'
154 function! s:NM_search_toggle_order()
155 echoe 'Not implemented'
158 function! s:NM_search_reply_to_thread()
159 echoe 'Not implemented'
162 function! s:NM_search_filter_by_tag()
163 echoe 'Not implemented'
166 function! s:NM_search_add_tags(tags)
167 call <SID>NM_search_add_remove_tags('Add Tag(s): ', '+', a:tags)
170 function! s:NM_search_remove_tags(tags)
171 call <SID>NM_search_add_remove_tags('Remove Tag(s): ', '-', a:tags)
174 function! s:NM_search_refresh_view()
176 call <SID>NM_cmd_search(g:notmuch_current_search_words)
177 " FIXME: should find the line of the thread we were on if possible
178 exec printf('norm %dG', lno)
181 " --- --- search screen helper functions {{{2
183 function! s:NM_search_find_thread_id()
184 if !exists('b:nm_raw_lines')
185 echoe 'no b:nm_raw_lines'
189 let info = b:nm_raw_lines[line-1]
190 let what = split(info, '\s\+')[0]
195 function! s:NM_search_add_remove_tags(prompt, prefix, intags)
196 if type(a:intags) != type([]) || len(a:intags) == 0
197 " TODO: input() can support completion
198 let text = input(a:prompt)
202 call <SID>NM_add_remove_tags(prefix, split(text, ' '))
204 call <SID>NM_add_remove_tags(prefix, a:intags)
206 call <SID>NM_search_refresh_view()
209 function! s:NM_add_remove_tags(prefix, tags)
210 let id = <SID>NM_search_find_thread_id()
212 echoe 'Eeek! I couldn''t find the thead id!'
214 call map(a:tags, 'a:prefix . v:val')
215 " TODO: handle errors
216 call <SID>NM_run(['tag'] + a:tags + ['--', id])
219 " --- implement show screen {{{1
221 function! s:NM_cmd_show(words)
222 let prev_bufnr = bufnr('%')
223 let data = s:NM_run(['show'] + a:words)
224 let lines = split(data, "\n")
226 let info = s:NM_cmd_show_parse(lines)
228 call <SID>NM_newBuffer('show', join(info['disp'], "\n"))
229 setlocal bufhidden=delete
230 let b:nm_raw_info = info
231 let b:nm_prev_bufnr = prev_bufnr
233 call <SID>NM_cmd_show_mkfolds()
234 call <SID>NM_cmd_show_mksyntax()
235 call <SID>NM_set_map(g:notmuch_show_maps)
236 setlocal foldtext=NM_cmd_show_foldtext()
238 setlocal foldcolumn=6
242 function! s:NM_cmd_show_quit()
243 exec printf(":buffer %d", b:nm_prev_bufnr)
246 function! s:NM_cmd_show_next()
247 let info = b:nm_raw_info
250 for msg in info['msgs']
252 if lnum >= msg['start']
256 exec printf('norm %dG', msg['start'])
261 call <SID>NM_search_show_thread()
264 function! s:NM_cmd_show_fold_toggle(key, type, fold)
265 let info = b:nm_raw_info
270 for fld in info['folds']
272 exec printf('%dfold%s', fld[1], act)
275 exec printf('nnoremap <buffer> %s :call <SID>NM_cmd_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
279 " s:NM_cmd_show_parse returns the following dictionary:
280 " 'disp': lines to display
281 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
282 " 'folds': fold info arrays [ type, start, end ]
283 " 'foldtext': fold text indexed by start line
284 function! s:NM_cmd_show_parse(inlines)
285 let info = { 'disp': [],
304 for line in a:inlines
305 let inlnum = inlnum + 1
311 if match(line, g:notmuch_show_part_end_regexp) != -1
312 let part_end = len(info['disp'])
314 call add(info['disp'], line)
317 if in_part == 'text/plain'
318 if !part_end && mode_type == ''
319 if match(line, g:notmuch_show_signature_regexp) != -1
320 let mode_type = 'sig'
321 let mode_start = len(info['disp'])
322 elseif match(line, g:notmuch_show_citation_regexp) != -1
323 let mode_type = 'cit'
324 let mode_start = len(info['disp'])
326 elseif mode_type == 'cit'
327 if part_end || match(line, g:notmuch_show_citation_regexp) == -1
328 let outlnum = len(info['disp'])
329 let foldinfo = [ mode_type, mode_start, outlnum,
330 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
333 elseif mode_type == 'sig'
334 let outlnum = len(info['disp'])
335 if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
336 echoe 'line ' . outlnum . ' stopped matching'
339 let foldinfo = [ mode_type, mode_start, outlnum,
340 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
347 " FIXME: this is a hack for handling two folds being added for one line
348 " we should handle addinga fold in a function
350 call add(info['folds'], foldinfo[0:2])
351 let info['foldtext'][foldinfo[1]] = foldinfo[3]
354 let foldinfo = [ 'text', part_start, part_end,
355 \ printf('[ %d-line %s. Press "p" to show. ]', part_end - part_start, in_part) ]
357 call add(info['disp'], '')
361 if !has_key(msg,'body_start')
362 let msg['body_start'] = len(info['disp']) + 1
364 if match(line, g:notmuch_show_body_end_regexp) != -1
365 let body_end = len(info['disp'])
366 let foldinfo = [ 'body', body_start, body_end,
367 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
371 elseif match(line, g:notmuch_show_part_begin_regexp) != -1
372 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
373 let in_part = 'unknown'
377 call add(info['disp'],
378 \ printf('--- %s ---', in_part))
379 let part_start = len(info['disp']) + 1
384 let msg['descr'] = line
385 call add(info['disp'], line)
387 let msg['hdr_start'] = len(info['disp']) + 1
390 if match(line, g:notmuch_show_header_end_regexp) != -1
391 let msg['header'] = hdr
395 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
398 if match(g:notmuch_show_headers, m[1]) != -1
399 call add(info['disp'], line)
406 if match(line, g:notmuch_show_message_end_regexp) != -1
407 let msg['end'] = len(info['disp'])
408 call add(info['disp'], '')
410 let foldinfo = [ 'match', msg['start'], msg['end'],
411 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
413 call add(info['msgs'], msg)
420 elseif match(line, g:notmuch_show_header_begin_regexp) != -1
424 elseif match(line, g:notmuch_show_body_begin_regexp) != -1
425 let body_start = len(info['disp']) + 1
431 if match(line, g:notmuch_show_message_begin_regexp) != -1
432 let msg['start'] = len(info['disp']) + 1
434 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
437 let msg['depth'] = m[2]
438 let msg['filename'] = m[3]
446 call add(info['folds'], foldinfo[0:2])
447 let info['foldtext'][foldinfo[1]] = foldinfo[3]
453 function! s:NM_cmd_show_mkfolds()
454 let info = b:nm_raw_info
456 for afold in info['folds']
457 exec printf('%d,%dfold', afold[1], afold[2])
458 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
459 \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
460 exec printf('%dfoldclose', afold[1])
462 exec printf('%dfoldopen', afold[1])
467 function! s:NM_cmd_show_mksyntax()
468 let info = b:nm_raw_info
470 for msg in info['msgs']
472 let start = msg['start']
473 let hdr_start = msg['hdr_start']
474 let body_start = msg['body_start']
476 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
477 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
478 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
482 function! NM_cmd_show_foldtext()
483 let foldtext = b:nm_raw_info['foldtext']
484 return foldtext[v:foldstart]
488 " --- notmuch helper functions {{{1
490 function! s:NM_newBuffer(ft, content)
492 setlocal buftype=nofile readonly modifiable
495 setlocal nomodifiable
496 execute printf('set filetype=notmuch-%s', a:ft)
497 execute printf('set syntax=notmuch-%s', a:ft)
500 function! s:NM_run(args)
501 let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
502 let out = system(cmd)
505 echo substitute(out, '\n*$', '', '')
513 " --- process and set the defaults {{{1
515 function! NM_set_defaults(force)
516 for [key, dflt] in items(s:notmuch_defaults)
518 if !a:force && exists(key) && type(dflt) == type(eval(key))
520 elseif type(dflt) == type(0)
521 let cmd = printf('let %s = %d', key, dflt)
522 elseif type(dflt) == type('')
523 let cmd = printf('let %s = ''%s''', key, dflt)
524 "elseif type(dflt) == type([])
525 " let cmd = printf('let %s = %s', key, string(dflt))
527 echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
528 \ a:force, key, string(dflt))
534 call NM_set_defaults(0)
536 " for some reason NM_set_defaults() didn't work for arrays...
537 if !exists('g:notmuch_show_headers')
538 let g:notmuch_show_headers = s:notmuch_show_headers_defaults
540 if !exists('g:notmuch_initial_search_words')
541 let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
544 " this is the default querry
545 let g:notmuch_current_search_words = g:notmuch_initial_search_words
548 " --- assign keymaps {{{1
550 function! s:NM_set_map(maps)
551 for [key, code] in items(a:maps)
552 exec printf('nnoremap <buffer> %s %s', key, code)
556 " --- command handler {{{1
558 function! NotMuch(args)
560 call <SID>NM_cmd_search(g:notmuch_current_search_words)
566 let words = split(a:args)
567 " TODO: handle commands passed as arguments
569 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
576 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
577 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
579 " --- hacks, only for development :) {{{1
581 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
583 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :