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 " --- implement search screen
34 function! s:NM_cmd_search(words)
36 if g:notmuch_search_reverse
37 let cmd = cmd + ['--reverse']
39 let data = s:NM_run(cmd + a:words)
40 "let data = substitute(data, '27/27', '25/27', '')
41 "let data = substitute(data, '\[4/4\]', '[0/4]', '')
42 let lines = split(data, "\n")
43 let disp = copy(lines)
44 call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
46 call s:NM_newBuffer('search', join(disp, "\n"))
47 let b:nm_raw_data = lines
49 nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
50 nnoremap <buffer> s :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
55 function! s:NM_search_display()
57 if !exists('b:nm_raw_data')
58 echo 'no b:nm_raw_data'
60 let info = b:nm_raw_data[line]
61 let what = split(info, '\s\+')[0]
62 call s:NM_cmd_show([what])
67 " --- implement show screen
69 function! s:NM_cmd_show(words)
70 let bufnr = bufnr('%')
71 let data = s:NM_run(['show'] + a:words)
73 call s:NM_newBuffer('show', data)
74 setlocal bufhidden=delete
75 let b:nm_raw_data = data
77 exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
83 function! s:NM_newBuffer(ft, content)
85 setlocal buftype=nofile readonly modifiable
89 execute printf('set filetype=notmuch-%s', a:ft)
90 execute printf('set syntax=notmuch-%s', a:ft)
93 function! s:NM_run(args)
94 let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
98 echo substitute(out, '\n*$', '', '')
107 " --- command handler
109 function! NotMuch(args)
111 call s:NM_cmd_search(['tag:inbox'])
117 let words = split(a:args)
118 " TODO: handle commands passed as arguments
120 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
127 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
128 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
130 " --- hacks, only for development :)
132 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>