]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
naively fold all signatures
[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 " --- implement search screen
55
56 function! s:NM_cmd_search(words)
57         let cmd = ['search']
58         if g:notmuch_search_reverse
59                 let cmd = cmd + ['--reverse']
60         endif
61         let data = s:NM_run(cmd + a:words)
62         "let data = substitute(data, '27/27', '25/27', '')
63         "let data = substitute(data, '\[4/4\]', '[0/4]', '')
64         let lines = split(data, "\n")
65         let disp = copy(lines)
66         call map(disp, 'substitute(v:val, "^thread:\\S* ", "", "")' )
67
68         call s:NM_newBuffer('search', join(disp, "\n"))
69         let b:nm_raw_data = lines
70
71         nnoremap <buffer> <Enter> :call <SID>NM_search_display()<CR>
72         nnoremap <buffer> s       :call <SID>NM_cmd_search(split(input('NotMuch Search:')))<CR>
73         setlocal cursorline
74         setlocal nowrap
75 endfunction
76
77 function! s:NM_search_display()
78         if !exists('b:nm_raw_data')
79                 echo 'no b:nm_raw_data'
80         else
81                 let line = line('.')
82                 let info = b:nm_raw_data[line-1]
83                 let what = split(info, '\s\+')[0]
84                 call s:NM_cmd_show([what])
85         endif
86 endfunction
87
88
89 " --- implement show screen
90
91 function! s:NM_cmd_show(words)
92         let bufnr = bufnr('%')
93         let data = s:NM_run(['show'] + a:words)
94
95         call s:NM_newBuffer('show', data)
96         setlocal bufhidden=delete
97         let b:nm_raw_data = data
98
99         call s:NM_cmd_show_mkfolds()
100
101         exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
102 endfunction
103
104 function! s:NM_cmd_show_mkfolds()
105         let modetype = ''
106         let modeline = -1
107         let lnum = 1
108         while lnum <= line('$')
109                 let line = getline(lnum)
110                 if modetype == ''
111                         if match(line, s:notmuch_show_signature_regexp) != -1
112                                 let modetype = 'sig'
113                                 let modeline = lnum
114                                 echo "start=" . modeline
115                         endif
116                 elseif modetype == 'sig'
117                         if (lnum - modeline) > s:notmuch_show_signature_lines_max
118                                 let modetype = ''
119                         elseif match(line, s:notmuch_show_part_end_regexp) != -1
120                                 exec printf('%d,%dfold', modeline, lnum)
121                                 let modetype = ''
122                         endif
123                 endif
124
125                 let lnum = lnum + 1
126         endwhile
127 endfunction
128
129
130 " --- helper functions
131
132 function! s:NM_newBuffer(ft, content)
133         enew
134         setlocal buftype=nofile readonly modifiable
135         silent put=a:content
136         keepjumps 0d
137         setlocal nomodifiable
138         execute printf('set filetype=notmuch-%s', a:ft)
139         execute printf('set syntax=notmuch-%s', a:ft)
140 endfunction
141
142 function! s:NM_run(args)
143         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
144         let out = system(cmd)
145         if v:shell_error
146                 echohl Error
147                 echo substitute(out, '\n*$', '', '')
148                 echohl None
149                 return ''
150         else
151                 return out
152         endif
153 endfunction
154
155
156 " --- command handler
157
158 function! NotMuch(args)
159         if !strlen(a:args)
160                 call s:NM_cmd_search(['tag:inbox'])
161                 return
162         endif
163
164         echo "blarg!"
165
166         let words = split(a:args)
167         " TODO: handle commands passed as arguments
168 endfunction
169 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
170         return []
171 endfunction
172
173
174 " --- glue
175
176 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
177 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
178
179 " --- hacks, only for development :)
180
181 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>