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 " for some reason NM_set_defaults() didn't work for arrays...
52 if !exists('g:notmuch_show_headers')
53 let g:notmuch_show_headers = [ 'Subject', 'From' ]
56 " --- process and set the defaults {{{1
58 function! NM_set_defaults(force)
59 for [key, dflt] in items(s:notmuch_defaults)
61 if !a:force && exists(key) && type(dflt) == type(eval(key))
63 elseif type(dflt) == type(0)
64 let cmd = printf('let %s = %d', key, dflt)
65 elseif type(dflt) == type('')
66 let cmd = printf('let %s = ''%s''', key, dflt)
67 "elseif type(dflt) == type([])
68 " let cmd = printf('let %s = %s', key, string(dflt))
70 echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
71 \ a:force, key, string(dflt))
78 call NM_set_defaults(0)
80 " --- implement search screen {{{1
82 function! s:NM_cmd_search(words)
84 if g:notmuch_search_reverse
85 let cmd = cmd + ['--reverse']
87 let data = s:NM_run(cmd + a:words)
88 "let data = substitute(data, '27/27', '25/27', '')
89 "let data = substitute(data, '\[4/4\]', '[0/4]', '')
90 let lines = split(data, "\n")
91 let disp = copy(lines)
92 call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
94 call s:NM_newBuffer('search', join(disp, "\n"))
95 let b:nm_raw_lines = lines
97 nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
98 nnoremap <buffer> s :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
103 function! s:NM_search_display()
104 if !exists('b:nm_raw_lines')
105 echo 'no b:nm_raw_lines'
108 let info = b:nm_raw_lines[line-1]
109 let what = split(info, '\s\+')[0]
110 call s:NM_cmd_show([what])
115 " --- implement show screen {{{1
117 function! s:NM_cmd_show(words)
118 let prev_bufnr = bufnr('%')
119 let data = s:NM_run(['show'] + a:words)
120 let lines = split(data, "\n")
122 let info = s:NM_cmd_show_parse(lines)
124 call s:NM_newBuffer('show', join(info['disp'], "\n"))
125 setlocal bufhidden=delete
126 let b:nm_raw_info = info
127 let b:nm_prev_bufnr = prev_bufnr
129 call s:NM_cmd_show_mkfolds()
130 call s:NM_cmd_show_mksyntax()
131 setlocal foldtext=NM_cmd_show_foldtext()
133 setlocal foldcolumn=6
135 exec printf("nnoremap <buffer> q :b %d<CR>", b:nm_prev_bufnr)
136 nnoremap <buffer> <C-N> :call <SID>NM_cmd_show_next()<CR>
137 nnoremap <buffer> c :call <SID>NM_cmd_show_fold_toggle('c', 'cit', !g:notmuch_show_fold_citations)<CR>
138 nnoremap <buffer> s :call <SID>NM_cmd_show_fold_toggle('s', 'sig', !g:notmuch_show_fold_signatures)<CR>
141 function! s:NM_cmd_show_next()
142 let info = b:nm_raw_info
145 for msg in info['msgs']
147 if lnum >= msg['start']
151 exec printf('norm %dG', msg['start'])
156 call <SID>NM_search_display()
159 function! s:NM_cmd_show_fold_toggle(key, type, fold)
160 let info = b:nm_raw_info
165 for fld in info['folds']
167 exec printf('%dfold%s', fld[1], act)
170 exec printf('nnoremap <buffer> %s :call <SID>NM_cmd_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
174 " s:NM_cmd_show_parse returns the following dictionary:
175 " 'disp': lines to display
176 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
177 " 'folds': fold info arrays [ type, start, end ]
178 " 'foldtext': fold text indexed by start line
179 function! s:NM_cmd_show_parse(inlines)
180 let info = { 'disp': [],
199 for line in a:inlines
200 let inlnum = inlnum + 1
206 if match(line, g:notmuch_show_part_end_regexp) != -1
207 let part_end = len(info['disp'])
209 call add(info['disp'], line)
212 if in_part == 'text/plain'
213 if !part_end && mode_type == ''
214 if match(line, g:notmuch_show_signature_regexp) != -1
215 let mode_type = 'sig'
216 let mode_start = len(info['disp'])
217 elseif match(line, g:notmuch_show_citation_regexp) != -1
218 let mode_type = 'cit'
219 let mode_start = len(info['disp'])
221 elseif mode_type == 'cit'
222 if part_end || match(line, g:notmuch_show_citation_regexp) == -1
223 let outlnum = len(info['disp'])
224 let foldinfo = [ mode_type, mode_start, outlnum,
225 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
228 elseif mode_type == 'sig'
229 let outlnum = len(info['disp'])
230 if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
231 echoe 'line ' . outlnum . ' stopped matching'
234 let foldinfo = [ mode_type, mode_start, outlnum,
235 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
242 " FIXME: this is a hack for handling two folds being added for one line
243 " we should handle addinga fold in a function
245 call add(info['folds'], foldinfo[0:2])
246 let info['foldtext'][foldinfo[1]] = foldinfo[3]
249 let foldinfo = [ 'text', part_start, part_end,
250 \ printf('[ %d-line %s. Press "p" to show. ]', part_end - part_start, in_part) ]
252 call add(info['disp'], '')
256 if !has_key(msg,'body_start')
257 let msg['body_start'] = len(info['disp']) + 1
259 if match(line, g:notmuch_show_body_end_regexp) != -1
260 let body_end = len(info['disp'])
261 let foldinfo = [ 'body', body_start, body_end,
262 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
266 elseif match(line, g:notmuch_show_part_begin_regexp) != -1
267 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
268 let in_part = 'unknown'
272 call add(info['disp'],
273 \ printf('--- %s ---', in_part))
274 let part_start = len(info['disp']) + 1
279 let msg['descr'] = line
280 call add(info['disp'], line)
282 let msg['hdr_start'] = len(info['disp']) + 1
285 if match(line, g:notmuch_show_header_end_regexp) != -1
286 let msg['header'] = hdr
290 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
293 if match(g:notmuch_show_headers, m[1]) != -1
294 call add(info['disp'], line)
301 if match(line, g:notmuch_show_message_end_regexp) != -1
302 let msg['end'] = len(info['disp'])
303 call add(info['disp'], '')
305 let foldinfo = [ 'match', msg['start'], msg['end'],
306 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
308 call add(info['msgs'], msg)
315 elseif match(line, g:notmuch_show_header_begin_regexp) != -1
319 elseif match(line, g:notmuch_show_body_begin_regexp) != -1
320 let body_start = len(info['disp']) + 1
326 if match(line, g:notmuch_show_message_begin_regexp) != -1
327 let msg['start'] = len(info['disp']) + 1
329 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
332 let msg['depth'] = m[2]
333 let msg['filename'] = m[3]
341 call add(info['folds'], foldinfo[0:2])
342 let info['foldtext'][foldinfo[1]] = foldinfo[3]
348 function! s:NM_cmd_show_mkfolds()
349 let info = b:nm_raw_info
351 for afold in info['folds']
352 exec printf('%d,%dfold', afold[1], afold[2])
353 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
354 \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
355 exec printf('%dfoldclose', afold[1])
357 exec printf('%dfoldopen', afold[1])
362 function! s:NM_cmd_show_mksyntax()
363 let info = b:nm_raw_info
365 for msg in info['msgs']
367 let start = msg['start']
368 let hdr_start = msg['hdr_start']
369 let body_start = msg['body_start']
371 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
372 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
373 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
377 function! NM_cmd_show_foldtext()
378 let foldtext = b:nm_raw_info['foldtext']
379 return foldtext[v:foldstart]
383 " --- helper functions {{{1
385 function! s:NM_newBuffer(ft, content)
387 setlocal buftype=nofile readonly modifiable
390 setlocal nomodifiable
391 execute printf('set filetype=notmuch-%s', a:ft)
392 execute printf('set syntax=notmuch-%s', a:ft)
395 function! s:NM_run(args)
396 let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
397 let out = system(cmd)
400 echo substitute(out, '\n*$', '', '')
409 " --- command handler {{{1
411 function! NotMuch(args)
413 call s:NM_cmd_search(['tag:inbox'])
419 let words = split(a:args)
420 " TODO: handle commands passed as arguments
422 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
429 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
430 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
432 " --- hacks, only for development :) {{{1
434 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
436 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :