]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
90678b93caf6430052d29452c36ffba0b08f83f8
[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 if !exists('g:notmuch_search_reverse')
29         let g:notmuch_search_reverse = 1
30 endif
31
32 " --- implement search screen
33
34 function! s:NM_cmd_search(words)
35         let cmd = ['search']
36         if g:notmuch_search_reverse
37                 let cmd = cmd + ['--reverse']
38         endif
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* ", "", "")' )
45
46         call s:NM_newBuffer('search', join(disp, "\n"))
47         let b:nm_raw_data = lines
48
49         nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
50         nnoremap <buffer> s       :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
51         setlocal cursorline
52         setlocal nowrap
53 endfunction
54
55 function! s:NM_search_display()
56         if !exists('b:nm_raw_data')
57                 echo 'no b:nm_raw_data'
58         else
59                 let line = line('.')
60                 let info = b:nm_raw_data[line-1]
61                 let what = split(info, '\s\+')[0]
62                 call s:NM_cmd_show([what])
63         endif
64 endfunction
65
66
67 " --- implement show screen
68
69 function! s:NM_cmd_show(words)
70         let bufnr = bufnr('%')
71         let data = s:NM_run(['show'] + a:words)
72
73         call s:NM_newBuffer('show', data)
74         setlocal bufhidden=delete
75         let b:nm_raw_data = data
76
77         exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
78 endfunction
79
80
81 " --- helper functions
82
83 function! s:NM_newBuffer(ft, content)
84         enew
85         setlocal buftype=nofile readonly modifiable
86         silent put=a:content
87         keepjumps 0d
88         setlocal nomodifiable
89         execute printf('set filetype=notmuch-%s', a:ft)
90         execute printf('set syntax=notmuch-%s', a:ft)
91 endfunction
92
93 function! s:NM_run(args)
94         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
95         let out = system(cmd)
96         if v:shell_error
97                 echohl Error
98                 echo substitute(out, '\n*$', '', '')
99                 echohl None
100                 return ''
101         else
102                 return out
103         endif
104 endfunction
105
106
107 " --- command handler
108
109 function! NotMuch(args)
110         if !strlen(a:args)
111                 call s:NM_cmd_search(['tag:inbox'])
112                 return
113         endif
114
115         echo "blarg!"
116
117         let words = split(a:args)
118         " TODO: handle commands passed as arguments
119 endfunction
120 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
121         return []
122 endfunction
123
124
125 " --- glue
126
127 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
128 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
129
130 " --- hacks, only for development :)
131
132 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>