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