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_show_headers
52 " override with: let g:notmuch_show_headers = [ ... ]
53 let s:notmuch_show_headers_defaults = [
58 " --- keyboard mapping definitions {{{1
60 let g:notmuch_search_maps = {
61 \ '<Enter>': ':call <SID>NM_search_display()<CR>',
62 \ 's': ':call <SID>NM_cmd_search(split(input(''NotMuch Search:'')))<CR>',
65 " --- implement search screen {{{1
67 function! s:NM_cmd_search(words)
69 if g:notmuch_search_reverse
70 let cmd = cmd + ['--reverse']
72 let data = s:NM_run(cmd + a:words)
73 "let data = substitute(data, '27/27', '25/27', '')
74 "let data = substitute(data, '\[4/4\]', '[0/4]', '')
75 let lines = split(data, "\n")
76 let disp = copy(lines)
77 call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
79 call s:NM_newBuffer('search', join(disp, "\n"))
80 let b:nm_raw_lines = lines
82 call <SID>NM_set_map(g:notmuch_search_maps)
87 function! s:NM_search_display()
88 if !exists('b:nm_raw_lines')
89 echo 'no b:nm_raw_lines'
92 let info = b:nm_raw_lines[line-1]
93 let what = split(info, '\s\+')[0]
94 call s:NM_cmd_show([what])
99 " --- implement show screen {{{1
101 function! s:NM_cmd_show(words)
102 let prev_bufnr = bufnr('%')
103 let data = s:NM_run(['show'] + a:words)
104 let lines = split(data, "\n")
106 let info = s:NM_cmd_show_parse(lines)
108 call s:NM_newBuffer('show', join(info['disp'], "\n"))
109 setlocal bufhidden=delete
110 let b:nm_raw_info = info
111 let b:nm_prev_bufnr = prev_bufnr
113 call s:NM_cmd_show_mkfolds()
114 call s:NM_cmd_show_mksyntax()
115 setlocal foldtext=NM_cmd_show_foldtext()
117 setlocal foldcolumn=6
119 exec printf("nnoremap <buffer> q :b %d<CR>", b:nm_prev_bufnr)
120 nnoremap <buffer> <C-N> :call <SID>NM_cmd_show_next()<CR>
121 nnoremap <buffer> c :call <SID>NM_cmd_show_fold_toggle('c', 'cit', !g:notmuch_show_fold_citations)<CR>
122 nnoremap <buffer> s :call <SID>NM_cmd_show_fold_toggle('s', 'sig', !g:notmuch_show_fold_signatures)<CR>
125 function! s:NM_cmd_show_next()
126 let info = b:nm_raw_info
129 for msg in info['msgs']
131 if lnum >= msg['start']
135 exec printf('norm %dG', msg['start'])
140 call <SID>NM_search_display()
143 function! s:NM_cmd_show_fold_toggle(key, type, fold)
144 let info = b:nm_raw_info
149 for fld in info['folds']
151 exec printf('%dfold%s', fld[1], act)
154 exec printf('nnoremap <buffer> %s :call <SID>NM_cmd_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
158 " s:NM_cmd_show_parse returns the following dictionary:
159 " 'disp': lines to display
160 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
161 " 'folds': fold info arrays [ type, start, end ]
162 " 'foldtext': fold text indexed by start line
163 function! s:NM_cmd_show_parse(inlines)
164 let info = { 'disp': [],
183 for line in a:inlines
184 let inlnum = inlnum + 1
190 if match(line, g:notmuch_show_part_end_regexp) != -1
191 let part_end = len(info['disp'])
193 call add(info['disp'], line)
196 if in_part == 'text/plain'
197 if !part_end && mode_type == ''
198 if match(line, g:notmuch_show_signature_regexp) != -1
199 let mode_type = 'sig'
200 let mode_start = len(info['disp'])
201 elseif match(line, g:notmuch_show_citation_regexp) != -1
202 let mode_type = 'cit'
203 let mode_start = len(info['disp'])
205 elseif mode_type == 'cit'
206 if part_end || match(line, g:notmuch_show_citation_regexp) == -1
207 let outlnum = len(info['disp'])
208 let foldinfo = [ mode_type, mode_start, outlnum,
209 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
212 elseif mode_type == 'sig'
213 let outlnum = len(info['disp'])
214 if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
215 echoe 'line ' . outlnum . ' stopped matching'
218 let foldinfo = [ mode_type, mode_start, outlnum,
219 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
226 " FIXME: this is a hack for handling two folds being added for one line
227 " we should handle addinga fold in a function
229 call add(info['folds'], foldinfo[0:2])
230 let info['foldtext'][foldinfo[1]] = foldinfo[3]
233 let foldinfo = [ 'text', part_start, part_end,
234 \ printf('[ %d-line %s. Press "p" to show. ]', part_end - part_start, in_part) ]
236 call add(info['disp'], '')
240 if !has_key(msg,'body_start')
241 let msg['body_start'] = len(info['disp']) + 1
243 if match(line, g:notmuch_show_body_end_regexp) != -1
244 let body_end = len(info['disp'])
245 let foldinfo = [ 'body', body_start, body_end,
246 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
250 elseif match(line, g:notmuch_show_part_begin_regexp) != -1
251 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
252 let in_part = 'unknown'
256 call add(info['disp'],
257 \ printf('--- %s ---', in_part))
258 let part_start = len(info['disp']) + 1
263 let msg['descr'] = line
264 call add(info['disp'], line)
266 let msg['hdr_start'] = len(info['disp']) + 1
269 if match(line, g:notmuch_show_header_end_regexp) != -1
270 let msg['header'] = hdr
274 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
277 if match(g:notmuch_show_headers, m[1]) != -1
278 call add(info['disp'], line)
285 if match(line, g:notmuch_show_message_end_regexp) != -1
286 let msg['end'] = len(info['disp'])
287 call add(info['disp'], '')
289 let foldinfo = [ 'match', msg['start'], msg['end'],
290 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
292 call add(info['msgs'], msg)
299 elseif match(line, g:notmuch_show_header_begin_regexp) != -1
303 elseif match(line, g:notmuch_show_body_begin_regexp) != -1
304 let body_start = len(info['disp']) + 1
310 if match(line, g:notmuch_show_message_begin_regexp) != -1
311 let msg['start'] = len(info['disp']) + 1
313 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
316 let msg['depth'] = m[2]
317 let msg['filename'] = m[3]
325 call add(info['folds'], foldinfo[0:2])
326 let info['foldtext'][foldinfo[1]] = foldinfo[3]
332 function! s:NM_cmd_show_mkfolds()
333 let info = b:nm_raw_info
335 for afold in info['folds']
336 exec printf('%d,%dfold', afold[1], afold[2])
337 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
338 \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
339 exec printf('%dfoldclose', afold[1])
341 exec printf('%dfoldopen', afold[1])
346 function! s:NM_cmd_show_mksyntax()
347 let info = b:nm_raw_info
349 for msg in info['msgs']
351 let start = msg['start']
352 let hdr_start = msg['hdr_start']
353 let body_start = msg['body_start']
355 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
356 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
357 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
361 function! NM_cmd_show_foldtext()
362 let foldtext = b:nm_raw_info['foldtext']
363 return foldtext[v:foldstart]
367 " --- notmuch helper functions {{{1
369 function! s:NM_newBuffer(ft, content)
371 setlocal buftype=nofile readonly modifiable
374 setlocal nomodifiable
375 execute printf('set filetype=notmuch-%s', a:ft)
376 execute printf('set syntax=notmuch-%s', a:ft)
379 function! s:NM_run(args)
380 let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
381 let out = system(cmd)
384 echo substitute(out, '\n*$', '', '')
392 " --- process and set the defaults {{{1
394 function! NM_set_defaults(force)
395 for [key, dflt] in items(s:notmuch_defaults)
397 if !a:force && exists(key) && type(dflt) == type(eval(key))
399 elseif type(dflt) == type(0)
400 let cmd = printf('let %s = %d', key, dflt)
401 elseif type(dflt) == type('')
402 let cmd = printf('let %s = ''%s''', key, dflt)
403 "elseif type(dflt) == type([])
404 " let cmd = printf('let %s = %s', key, string(dflt))
406 echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
407 \ a:force, key, string(dflt))
414 call NM_set_defaults(0)
416 " for some reason NM_set_defaults() didn't work for arrays...
417 if !exists('g:notmuch_show_headers')
418 let g:notmuch_show_headers = s:notmuch_show_headers_defaults
421 " --- assign keymaps {{{1
423 function! s:NM_set_map(maps)
424 for [key, code] in items(a:maps)
425 exec printf('nnoremap <buffer> %s %s', key, code)
429 " --- command handler {{{1
431 function! NotMuch(args)
433 call s:NM_cmd_search(['tag:inbox'])
439 let words = split(a:args)
440 " TODO: handle commands passed as arguments
442 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
449 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
450 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
452 " --- hacks, only for development :) {{{1
454 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
456 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :