]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
70f38e2ac5a1a42768eafd5b7efab08401c8e295
[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 " --- used to match output of notmuch
33
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\\)[{}].*$'
45
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             = '(\([^)]*\))$'
50
51 let s:notmuch_show_signature_regexp        = '^\(-- \?\|_\+\)$'
52 let s:notmuch_show_signature_lines_max     = 12
53
54 let s:notmuch_show_citation_regexp         = '^\s*>'
55
56 " --- implement search screen
57
58 function! s:NM_cmd_search(words)
59         let cmd = ['search']
60         if g:notmuch_search_reverse
61                 let cmd = cmd + ['--reverse']
62         endif
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* ", "", "")' )
69
70         call s:NM_newBuffer('search', join(disp, "\n"))
71         let b:nm_raw_data = lines
72
73         nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
74         nnoremap <buffer> s       :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
75         setlocal cursorline
76         setlocal nowrap
77 endfunction
78
79 function! s:NM_search_display()
80         if !exists('b:nm_raw_data')
81                 echo 'no b:nm_raw_data'
82         else
83                 let line = line('.')
84                 let info = b:nm_raw_data[line-1]
85                 let what = split(info, '\s\+')[0]
86                 call s:NM_cmd_show([what])
87         endif
88 endfunction
89
90
91 " --- implement show screen
92
93 function! s:NM_cmd_show(words)
94         let bufnr = bufnr('%')
95         let data = s:NM_run(['show'] + a:words)
96
97         call s:NM_newBuffer('show', data)
98         setlocal bufhidden=delete
99         let b:nm_raw_data = data
100
101         call s:NM_cmd_show_mkfolds()
102         setlocal foldtext=NM_cmd_show_foldtext()
103         setlocal fillchars=
104         setlocal foldcolumn=5
105
106         exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
107 endfunction
108
109 function! s:NM_cmd_show_mkfolds()
110         let msg_start = -1
111         let hdr_start = -1
112         let bdy_start = -1
113         let prt_start = -1
114         let modetype = ''
115         let modeline = -1
116         let lnum = 1
117         let b:nm_fold_data = {}
118         while lnum <= line('$')
119                 let line = getline(lnum)
120                 if match(line, s:notmuch_show_message_begin_regexp) != -1
121                         let msg_start = lnum
122                 elseif match(line, s:notmuch_show_message_end_regexp) != -1
123                         exec printf('%d,%dfold', msg_start, lnum)
124                         exec printf('%dfoldopen', msg_start)
125                         let b:nm_fold_data[msg_start] = ['msg', getline(msg_start)]
126
127                 elseif match(line, s:notmuch_show_header_begin_regexp) != -1
128                         let hdr_start = lnum
129                 elseif match(line, s:notmuch_show_header_end_regexp) != -1
130                         exec printf('%d,%dfold', hdr_start, lnum)
131                         exec printf('%dfoldclose', hdr_start)
132                         let b:nm_fold_data[hdr_start] = ['hdr', '* ' . getline(hdr_start+1) . ' [ Press "h" for full header. ]']
133
134                 elseif match(line, s:notmuch_show_body_begin_regexp) != -1
135                         let bdy_start = lnum
136                 elseif match(line, s:notmuch_show_body_end_regexp) != -1
137                         exec printf('%d,%dfold', bdy_start, lnum)
138                         exec printf('%dfoldopen', bdy_start)
139                         let b:nm_fold_data[bdy_start] = ['bdy', getline(bdy_start)]
140
141                 elseif match(line, s:notmuch_show_part_begin_regexp) != -1
142                         let prt_start = lnum
143                 elseif match(line, s:notmuch_show_part_end_regexp) != -1
144                         exec printf('%d,%dfold', prt_start, lnum)
145                         exec printf('%dfoldopen', prt_start)
146                         let b:nm_fold_data[msg_start] = ['msg', getline(prt_start)]
147
148                 elseif modetype == ''
149                         if match(line, s:notmuch_show_signature_regexp) != -1
150                                 let modetype = 'sig'
151                                 let modeline = lnum
152                         elseif match(line, s:notmuch_show_citation_regexp) != -1
153                                 let modetype = 'cit'
154                                 let modeline = lnum
155                         endif
156                 elseif modetype == 'cit'
157                         if match(line, s:notmuch_show_citation_regexp) == -1
158                                 exec printf('%d,%dfold', modeline, lnum)
159                                 let b:nm_fold_data[modeline] = [modetype, printf('[ %d-line citation.  Press "c" to show. ]', lnum - modeline)]
160                                 let modetype = ''
161                         endif
162                 elseif modetype == 'sig'
163                         if (lnum - modeline) > s:notmuch_show_signature_lines_max
164                                 let modetype = ''
165                         elseif match(line, s:notmuch_show_part_end_regexp) != -1
166                                 let modeline2 = lnum - 1
167                                 exec printf('%d,%dfold', modeline, modeline2)
168                                 let b:nm_fold_data[modeline] = [modetype, printf('[ %d-line signature.  Press "s" to show. ]', modeline2 - modeline)]
169                                 let modetype = ''
170                         endif
171                 endif
172
173                 let lnum = lnum + 1
174         endwhile
175 endfunction
176
177 function! NM_cmd_show_foldtext()
178         return b:nm_fold_data[v:foldstart][1]
179 endfunction
180
181
182 " --- helper functions
183
184 function! s:NM_newBuffer(ft, content)
185         enew
186         setlocal buftype=nofile readonly modifiable
187         silent put=a:content
188         keepjumps 0d
189         setlocal nomodifiable
190         execute printf('set filetype=notmuch-%s', a:ft)
191         execute printf('set syntax=notmuch-%s', a:ft)
192 endfunction
193
194 function! s:NM_run(args)
195         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
196         let out = system(cmd)
197         if v:shell_error
198                 echohl Error
199                 echo substitute(out, '\n*$', '', '')
200                 echohl None
201                 return ''
202         else
203                 return out
204         endif
205 endfunction
206
207
208 " --- command handler
209
210 function! NotMuch(args)
211         if !strlen(a:args)
212                 call s:NM_cmd_search(['tag:inbox'])
213                 return
214         endif
215
216         echo "blarg!"
217
218         let words = split(a:args)
219         " TODO: handle commands passed as arguments
220 endfunction
221 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
222         return []
223 endfunction
224
225
226 " --- glue
227
228 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
229 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
230
231 " --- hacks, only for development :)
232
233 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>