]> git.notmuchmail.org Git - notmuch/blob - vim/plugin/notmuch.vim
2b92ad688dfbc5a3ba37c22d8107a9e35984e774
[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         exec printf("nnoremap <buffer> q :b %d<CR>", bufnr)
100 endfunction
101
102
103 " --- helper functions
104
105 function! s:NM_newBuffer(ft, content)
106         enew
107         setlocal buftype=nofile readonly modifiable
108         silent put=a:content
109         keepjumps 0d
110         setlocal nomodifiable
111         execute printf('set filetype=notmuch-%s', a:ft)
112         execute printf('set syntax=notmuch-%s', a:ft)
113 endfunction
114
115 function! s:NM_run(args)
116         let cmd = g:notmuch_cmd . ' ' . join(a:args) . '< /dev/null'
117         let out = system(cmd)
118         if v:shell_error
119                 echohl Error
120                 echo substitute(out, '\n*$', '', '')
121                 echohl None
122                 return ''
123         else
124                 return out
125         endif
126 endfunction
127
128
129 " --- command handler
130
131 function! NotMuch(args)
132         if !strlen(a:args)
133                 call s:NM_cmd_search(['tag:inbox'])
134                 return
135         endif
136
137         echo "blarg!"
138
139         let words = split(a:args)
140         " TODO: handle commands passed as arguments
141 endfunction
142 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
143         return []
144 endfunction
145
146
147 " --- glue
148
149 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
150 cabbrev  notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
151
152 " --- hacks, only for development :)
153
154 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>