]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
folding for citations
[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
103         exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
104 endfunction
105
106 function! s:NM_cmd_show_mkfolds()
107         let modetype = ''
108         let modeline = -1
109         let lnum = 1
110         while lnum <= line('$')
111                 let line = getline(lnum)
112                 if modetype == ''
113                         if match(line, s:notmuch_show_signature_regexp) != -1
114                                 let modetype = 'sig'
115                                 let modeline = lnum
116                         elseif match(line, s:notmuch_show_citation_regexp) != -1
117                                 let modetype = 'cit'
118                                 let modeline = lnum
119                         endif
120                 elseif modetype == 'cit'
121                         if match(line, s:notmuch_show_citation_regexp) == -1
122                                 exec printf('%d,%dfold', modeline, lnum)
123                                 let modetype = ''
124                         endif
125                 elseif modetype == 'sig'
126                         if (lnum - modeline) > s:notmuch_show_signature_lines_max
127                                 let modetype = ''
128                         elseif match(line, s:notmuch_show_part_end_regexp) != -1
129                                 exec printf('%d,%dfold', modeline, lnum)
130                                 let modetype = ''
131                         endif
132                 endif
133
134                 let lnum = lnum + 1
135         endwhile
136 endfunction
137
138
139 " --- helper functions
140
141 function! s:NM_newBuffer(ft, content)
142         enew
143         setlocal buftype=nofile readonly modifiable
144         silent put=a:content
145         keepjumps 0d
146         setlocal nomodifiable
147         execute printf('set filetype=notmuch-%s', a:ft)
148         execute printf('set syntax=notmuch-%s', a:ft)
149 endfunction
150
151 function! s:NM_run(args)
152         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
153         let out = system(cmd)
154         if v:shell_error
155                 echohl Error
156                 echo substitute(out, '\n*$', '', '')
157                 echohl None
158                 return ''
159         else
160                 return out
161         endif
162 endfunction
163
164
165 " --- command handler
166
167 function! NotMuch(args)
168         if !strlen(a:args)
169                 call s:NM_cmd_search(['tag:inbox'])
170                 return
171         endif
172
173         echo "blarg!"
174
175         let words = split(a:args)
176         " TODO: handle commands passed as arguments
177 endfunction
178 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
179         return []
180 endfunction
181
182
183 " --- glue
184
185 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
186 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
187
188 " --- hacks, only for development :)
189
190 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>