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_id_regexp = '\(id:[^ ]*\)'
47 let s:notmuch_show_depth_regexp = ' depth:\([0-9]*\) '
48 let s:notmuch_show_filename_regexp = 'filename:\(.*\)$'
49 let s:notmuch_show_tags_regexp = '(\([^)]*\))$'
51 let s:notmuch_show_signature_regexp = '^\(-- \?\|_\+\)$'
52 let s:notmuch_show_signature_lines_max = 12
54 let s:notmuch_show_citation_regexp = '^\s*>'
56 " --- implement search screen
58 function! s:NM_cmd_search(words)
60 if g:notmuch_search_reverse
61 let cmd = cmd + ['--reverse']
63 let data = s:NM_run(cmd + a:words)
64 "let data = substitute(data, '27/27', '25/27', '')
65 "let data = substitute(data, '\[4/4\]', '[0/4]', '')
66 let lines = split(data, "\n")
67 let disp = copy(lines)
68 call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
70 call s:NM_newBuffer('search', join(disp, "\n"))
71 let b:nm_raw_data = lines
73 nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
74 nnoremap <buffer> s :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
79 function! s:NM_search_display()
80 if !exists('b:nm_raw_data')
81 echo 'no b:nm_raw_data'
84 let info = b:nm_raw_data[line-1]
85 let what = split(info, '\s\+')[0]
86 call s:NM_cmd_show([what])
91 " --- implement show screen
93 function! s:NM_cmd_show(words)
94 let bufnr = bufnr('%')
95 let data = s:NM_run(['show'] + a:words)
97 call s:NM_newBuffer('show', data)
98 setlocal bufhidden=delete
99 let b:nm_raw_data = data
101 call s:NM_cmd_show_mkfolds()
103 exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
106 function! s:NM_cmd_show_mkfolds()
110 while lnum <= line('$')
111 let line = getline(lnum)
113 if match(line, s:notmuch_show_signature_regexp) != -1
116 elseif match(line, s:notmuch_show_citation_regexp) != -1
120 elseif modetype == 'cit'
121 if match(line, s:notmuch_show_citation_regexp) == -1
122 exec printf('%d,%dfold', modeline, lnum)
125 elseif modetype == 'sig'
126 if (lnum - modeline) > s:notmuch_show_signature_lines_max
128 elseif match(line, s:notmuch_show_part_end_regexp) != -1
129 exec printf('%d,%dfold', modeline, lnum)
139 " --- helper functions
141 function! s:NM_newBuffer(ft, content)
143 setlocal buftype=nofile readonly modifiable
146 setlocal nomodifiable
147 execute printf('set filetype=notmuch-%s', a:ft)
148 execute printf('set syntax=notmuch-%s', a:ft)
151 function! s:NM_run(args)
152 let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
153 let out = system(cmd)
156 echo substitute(out, '\n*$', '', '')
165 " --- command handler
167 function! NotMuch(args)
169 call s:NM_cmd_search(['tag:inbox'])
175 let words = split(a:args)
176 " TODO: handle commands passed as arguments
178 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
185 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
186 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
188 " --- hacks, only for development :)
190 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>