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>
24 if !exists('g:notmuch_cmd')
25 let g:notmuch_cmd = 'notmuch'
28 if !exists('g:notmuch_search_reverse')
29 let g:notmuch_search_reverse = 1
32 " --- used to match output of notmuch
34 let s:notmuch_show_message_begin_regexp = '^
\fmessage{'
35 let s:notmuch_show_message_end_regexp = '^
\fmessage}'
36 let s:notmuch_show_header_begin_regexp = '^
\fheader{'
37 let s:notmuch_show_header_end_regexp = '^
\fheader}'
38 let s:notmuch_show_body_begin_regexp = '^
\fbody{'
39 let s:notmuch_show_body_end_regexp = '^
\fbody}'
40 let s:notmuch_show_attachment_begin_regexp = '^
\fattachment{'
41 let s:notmuch_show_attachment_end_regexp = '^
\fattachment}'
42 let s:notmuch_show_part_begin_regexp = '^
\fpart{'
43 let s:notmuch_show_part_end_regexp = '^
\fpart}'
44 let s:notmuch_show_marker_regexp = '^
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$'
46 let s:notmuch_show_message_parse_regexp = '\(id:[^ ]*\) depth:\([0-9]*\) filename:\(.*\)$'
47 let s:notmuch_show_tags_regexp = '(\([^)]*\))$'
49 let s:notmuch_show_signature_regexp = '^\(-- \?\|_\+\)$'
50 let s:notmuch_show_signature_lines_max = 12
52 let s:notmuch_show_citation_regexp = '^\s*>'
54 let s:notmuch_show_headers = [ 'Subject', 'From' ]
56 let s:notmuch_show_fold_signatures = 1
57 let s:notmuch_show_fold_citations = 1
59 " --- implement search screen
61 function! s:NM_cmd_search(words)
63 if g:notmuch_search_reverse
64 let cmd = cmd + ['--reverse']
66 let data = s:NM_run(cmd + a:words)
67 "let data = substitute(data, '27/27', '25/27', '')
68 "let data = substitute(data, '\[4/4\]', '[0/4]', '')
69 let lines = split(data, "\n")
70 let disp = copy(lines)
71 call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
73 call s:NM_newBuffer('search', join(disp, "\n"))
74 let b:nm_raw_lines = lines
76 nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
77 nnoremap <buffer> s :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
82 function! s:NM_search_display()
83 if !exists('b:nm_raw_lines')
84 echo 'no b:nm_raw_lines'
87 let info = b:nm_raw_lines[line-1]
88 let what = split(info, '\s\+')[0]
89 call s:NM_cmd_show([what])
94 " --- implement show screen
96 function! s:NM_cmd_show(words)
97 let bufnr = bufnr('%')
98 let data = s:NM_run(['show'] + a:words)
99 let lines = split(data, "\n")
101 let info = s:NM_cmd_show_parse(lines)
103 call s:NM_newBuffer('show', join(info['disp'], "\n"))
104 setlocal bufhidden=delete
105 let b:nm_raw_info = info
107 call s:NM_cmd_show_mkfolds()
108 setlocal foldtext=NM_cmd_show_foldtext()
110 setlocal foldcolumn=5
112 exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
115 " s:NM_cmd_show_parse returns the following dictionary:
116 " 'disp': lines to display
117 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
118 " 'folds': fold info arrays [ type, start, end ]
119 " 'foldtext': fold text indexed by start line
120 function! s:NM_cmd_show_parse(inlines)
121 let info = { 'disp': [],
139 for line in a:inlines
140 let inlnum = inlnum + 1
144 if match(line, s:notmuch_show_part_end_regexp) != -1
145 call add(info['disp'], '')
148 call add(info['disp'], line)
151 if in_part && mode_type == ''
152 if match(line, s:notmuch_show_signature_regexp) != -1
153 let mode_type = 'sig'
154 let mode_start = len(info['disp'])
155 "echoe 'TYPE: ' . mode_type . ' @' . mode_start
156 elseif match(line, s:notmuch_show_citation_regexp) != -1
157 let mode_type = 'cit'
158 let mode_start = len(info['disp'])
159 "echoe 'TYPE: ' . mode_type . ' @' . mode_start
161 elseif mode_type == 'cit'
162 if !in_part || match(line, s:notmuch_show_citation_regexp) == -1
163 let outlnum = len(info['disp'])
164 let foldinfo = [ mode_type, mode_start, outlnum,
165 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
168 elseif mode_type == 'sig'
169 let outlnum = len(info['disp'])
170 if (outlnum - mode_start) > s:notmuch_show_signature_lines_max
173 let outlnum = outlnum - 1
174 let foldinfo = [ mode_type, mode_start, outlnum,
175 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
181 if match(line, s:notmuch_show_body_end_regexp) != -1
182 let body_end = len(info['disp'])
183 let foldinfo = [ 'body', body_start, body_end,
184 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
188 elseif match(line, s:notmuch_show_part_begin_regexp) != -1
190 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
192 call add(info['disp'],
193 \ printf('--- part %d --- %s ---', m[1], m[2]))
199 let msg['descr'] = line
200 call add(info['disp'], line)
204 if match(line, s:notmuch_show_header_end_regexp) != -1
205 let msg['header'] = hdr
209 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
212 if match(s:notmuch_show_headers, m[1]) != -1
213 call add(info['disp'], line)
220 if match(line, s:notmuch_show_message_end_regexp) != -1
221 let msg['end'] = len(info['disp'])
222 call add(info['disp'], '')
224 let foldinfo = [ 'match', msg['start'], msg['end'],
225 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
227 call add(info['msgs'], msg)
234 elseif match(line, s:notmuch_show_header_begin_regexp) != -1
238 elseif match(line, s:notmuch_show_body_begin_regexp) != -1
239 let body_start = len(info['disp']) + 1
245 if match(line, s:notmuch_show_message_begin_regexp) != -1
246 let msg['start'] = len(info['disp']) + 1
248 let m = matchlist(line, s:notmuch_show_message_parse_regexp)
251 let msg['depth'] = m[2]
252 let msg['filename'] = m[3]
260 call add(info['folds'], foldinfo[0:2])
261 let info['foldtext'][foldinfo[1]] = foldinfo[3]
267 function! s:NM_cmd_show_mkfolds()
268 let info = b:nm_raw_info
270 for afold in info['folds']
271 exec printf('%d,%dfold', afold[1], afold[2])
272 if (afold[0] == 'sig' && s:notmuch_show_fold_signatures)
273 \ || (afold[0] == 'cit' && s:notmuch_show_fold_citations)
274 exec printf('%dfoldclose', afold[1])
276 exec printf('%dfoldopen', afold[1])
281 function! NM_cmd_show_foldtext()
282 let foldtext = b:nm_raw_info['foldtext']
283 return foldtext[v:foldstart]
287 " --- helper functions
289 function! s:NM_newBuffer(ft, content)
291 setlocal buftype=nofile readonly modifiable
294 setlocal nomodifiable
295 execute printf('set filetype=notmuch-%s', a:ft)
296 execute printf('set syntax=notmuch-%s', a:ft)
299 function! s:NM_run(args)
300 let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
301 let out = system(cmd)
304 echo substitute(out, '\n*$', '', '')
313 " --- command handler
315 function! NotMuch(args)
317 call s:NM_cmd_search(['tag:inbox'])
323 let words = split(a:args)
324 " TODO: handle commands passed as arguments
326 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
333 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
334 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
336 " --- hacks, only for development :)
338 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>