]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
primitive notmuch mail interface for vim
[notmuch] / vim / plugin / notmuch.vim
1 " notmuch.vim plugin --- run notmuch within vim
2 "
3 " Copyright © Carl Worth
4 "
5 " This file is part of Notmuch.
6 "
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.
11 "
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.
16 "
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/>.
19 "
20 " Authors: Bart Trojanowski <bart@jukie.net>
21
22 " --- defaults
23
24 if !exists('g:notmuch_cmd')
25         let g:notmuch_cmd = 'notmuch'
26 endif
27
28
29 " --- implement search screen
30
31 function! s:NM_cmd_search(words)
32         let data = split(s:NM_run(['search'] + a:words), "\n")
33         let disp = copy(data)
34         call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
35
36         call s:NM_newBuffer('search', join(disp, "\n"))
37         let b:nm_raw_data = data
38
39         nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
40 endfunction
41
42 function! s:NM_search_display()
43         let line = line('.')
44         if !exists('b:nm_raw_data')
45                 echo 'no b:nm_raw_data'
46         else
47                 let info = b:nm_raw_data[line]
48                 let what = split(info, '\W\+')[0]
49                 call s:NM_cmd_show([what])
50         endif
51 endfunction
52
53
54 " --- implement show screen
55
56 function! s:NM_cmd_show(words)
57         let data = s:NM_run(['show'] + a:words)
58
59         call s:NM_newBuffer('show', data)
60         let b:nm_raw_data = data
61 endfunction
62
63
64 " --- helper function
65
66 function! s:NM_newBuffer(ft, content)
67         enew
68         setlocal buftype=nofile readonly modifiable
69         setlocal bufhidden=delete
70         silent put=a:content
71         keepjumps 0d
72         setlocal nomodifiable
73         setlocal cursorline
74         execute printf('setlocal filetype=notmuch-%s', a:ft)
75 endfunction
76
77 function! s:NM_run(args)
78         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
79         let out = system(cmd)
80         if v:shell_error
81                 echohl Error
82                 echo substitute(out, '\n*$', '', '')
83                 echohl None
84                 return ''
85         else
86                 return out
87         endif
88 endfunction
89
90
91 " --- command handler
92
93 function! NotMuch(args)
94         if !strlen(a:args)
95                 call s:NM_cmd_search(['tag:inbox'])
96                 return
97         endif
98
99         echo "blarg!"
100
101         let words = split(a:args)
102         " TODO: handle commands passed as arguments
103 endfunction
104 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
105         return []
106 endfunction
107
108
109 " --- glue
110
111 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
112 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
113
114 " --- hacks, only for development :)
115
116 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>