1 " notmuch.vim plugin --- run notmuch within vim
3 " Copyright © Carl Worth
5 " This file is part of Notmuch.
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.
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.
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/>.
20 " Authors: Bart Trojanowski <bart@jukie.net>
22 " --- configuration defaults {{{1
24 let s:notmuch_defaults = {
25 \ 'g:notmuch_cmd': 'notmuch' ,
26 \ 'g:notmuch_debug': 0 ,
28 \ 'g:notmuch_search_newest_first': 1 ,
29 \ 'g:notmuch_search_from_column_width': 20 ,
31 \ 'g:notmuch_show_fold_signatures': 1 ,
32 \ 'g:notmuch_show_fold_citations': 1 ,
33 \ 'g:notmuch_show_fold_bodies': 0 ,
34 \ 'g:notmuch_show_fold_headers': 1 ,
36 \ 'g:notmuch_show_message_begin_regexp': '^
\fmessage{' ,
37 \ 'g:notmuch_show_message_end_regexp': '^
\fmessage}' ,
38 \ 'g:notmuch_show_header_begin_regexp': '^
\fheader{' ,
39 \ 'g:notmuch_show_header_end_regexp': '^
\fheader}' ,
40 \ 'g:notmuch_show_body_begin_regexp': '^
\fbody{' ,
41 \ 'g:notmuch_show_body_end_regexp': '^
\fbody}' ,
42 \ 'g:notmuch_show_attachment_begin_regexp': '^
\fattachment{' ,
43 \ 'g:notmuch_show_attachment_end_regexp': '^
\fattachment}' ,
44 \ 'g:notmuch_show_part_begin_regexp': '^
\fpart{' ,
45 \ 'g:notmuch_show_part_end_regexp': '^
\fpart}' ,
46 \ 'g:notmuch_show_marker_regexp': '^
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$',
48 \ 'g:notmuch_show_message_parse_regexp': '\(id:[^ ]*\) depth:\([0-9]*\) match:\([0-9]*\) filename:\(.*\)$',
49 \ 'g:notmuch_show_tags_regexp': '(\([^)]*\))$' ,
51 \ 'g:notmuch_show_signature_regexp': '^\(-- \?\|_\+\)$' ,
52 \ 'g:notmuch_show_signature_lines_max': 12 ,
54 \ 'g:notmuch_show_citation_regexp': '^\s*>' ,
57 " defaults for g:notmuch_initial_search_words
58 " override with: let g:notmuch_initial_search_words = [ ... ]
59 let s:notmuch_initial_search_words_defaults = [
60 \ 'tag:inbox and tag:unread',
63 " defaults for g:notmuch_show_headers
64 " override with: let g:notmuch_show_headers = [ ... ]
65 let s:notmuch_show_headers_defaults = [
73 " defaults for g:notmuch_folders
74 " override with: let g:notmuch_folders = [ ... ]
75 let s:notmuch_folders_defaults = [
76 \ [ 'new', 'tag:inbox and tag:unread' ],
77 \ [ 'inbox', 'tag:inbox' ],
78 \ [ 'unread', 'tag:unread' ],
81 " --- keyboard mapping definitions {{{1
83 " --- --- bindings for folders mode {{{2
85 let g:notmuch_folders_maps = {
86 \ 's': ':call <SID>NM_search_prompt()<CR>',
87 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
88 \ '=': ':call <SID>NM_folders_refresh_view()<CR>',
89 \ '<Enter>': ':call <SID>NM_folders_show_search()<CR>',
92 " --- --- bindings for search screen {{{2
93 let g:notmuch_search_maps = {
94 \ '<Enter>': ':call <SID>NM_search_show_thread()<CR>',
95 \ '<C-]>': ':call <SID>NM_search_expand(''<cword>'')<CR>',
96 \ 'a': ':call <SID>NM_search_archive_thread()<CR>',
97 \ 'f': ':call <SID>NM_search_filter()<CR>',
98 \ 'm': ':call <SID>NM_new_mail()<CR>',
99 \ 'o': ':call <SID>NM_search_toggle_order()<CR>',
100 \ 'r': ':call <SID>NM_search_reply_to_thread()<CR>',
101 \ 's': ':call <SID>NM_search_prompt()<CR>',
102 \ ',s': ':call <SID>NM_search_edit()<CR>',
103 \ 't': ':call <SID>NM_search_filter_by_tag()<CR>',
104 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
105 \ '+': ':call <SID>NM_search_add_tags([])<CR>',
106 \ '-': ':call <SID>NM_search_remove_tags([])<CR>',
107 \ '=': ':call <SID>NM_search_refresh_view()<CR>',
108 \ '?': ':echo <SID>NM_search_thread_id()<CR>',
111 " --- --- bindings for show screen {{{2
112 let g:notmuch_show_maps = {
113 \ '<C-P>': ':call <SID>NM_show_previous(1, 0)<CR>',
114 \ '<C-N>': ':call <SID>NM_show_next(1, 0)<CR>',
115 \ '<C-]>': ':call <SID>NM_search_expand(''<cword>'')<CR>',
116 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
118 \ 'b': ':call <SID>NM_show_fold_toggle(''b'', ''bdy'', !g:notmuch_show_fold_bodies)<CR>',
119 \ 'c': ':call <SID>NM_show_fold_toggle(''c'', ''cit'', !g:notmuch_show_fold_citations)<CR>',
120 \ 'h': ':call <SID>NM_show_fold_toggle(''h'', ''hdr'', !g:notmuch_show_fold_headers)<CR>',
121 \ 's': ':call <SID>NM_show_fold_toggle(''s'', ''sig'', !g:notmuch_show_fold_signatures)<CR>',
123 \ 'a': ':call <SID>NM_show_archive_thread()<CR>',
124 \ 'A': ':call <SID>NM_show_mark_read_then_archive_thread()<CR>',
125 \ 'N': ':call <SID>NM_show_mark_read_then_next_open_message()<CR>',
126 \ 'v': ':call <SID>NM_show_view_all_mime_parts()<CR>',
127 \ '+': ':call <SID>NM_show_add_tag()<CR>',
128 \ '-': ':call <SID>NM_show_remove_tag()<CR>',
129 \ '<Space>': ':call <SID>NM_show_advance_marking_read_and_archiving()<CR>',
130 \ '\|': ':call <SID>NM_show_pipe_message()<CR>',
132 \ '<S-Tab>': ':call <SID>NM_show_previous_fold()<CR>',
133 \ '<Tab>': ':call <SID>NM_show_next_fold()<CR>',
134 \ '<Enter>': ':call <SID>NM_show_toggle_fold()<CR>',
136 \ 'r': ':call <SID>NM_show_reply()<CR>',
137 \ 'm': ':call <SID>NM_new_mail()<CR>',
138 \ '?': ':echo <SID>NM_show_message_id() . '' @ '' . join(<SID>NM_show_search_words())<CR>',
142 " --- implement folders screen {{{1
144 function! s:NM_cmd_folders(words)
146 echoe 'Not exapecting any arguments for folders command.'
151 for entry in g:notmuch_folders
152 let [ name, search ] = entry
153 let data = s:NM_run(cmd + [search])
154 let cnt = matchlist(data, '\(\d\+\)')[1]
155 call add(disp, printf('%9d %-20s (%s)', cnt, name, search))
156 call add(searches, search)
159 call <SID>NM_newBuffer('folders', join(disp, "\n"))
160 let b:nm_searches = searches
161 let b:nm_timestamp = reltime()
163 call <SID>NM_cmd_folders_mksyntax()
164 call <SID>NM_set_map(g:notmuch_folders_maps)
169 function! s:NM_cmd_folders_mksyntax()
172 " --- --- folders screen action functions {{{2
174 function! s:NM_folders_refresh_view()
176 setlocal bufhidden=delete
177 call s:NM_cmd_folders([])
178 exec printf('norm %dG', lno)
181 function! s:NM_folders_show_search()
183 let search = b:nm_searches[line-1]
185 let prev_bufnr = bufnr('%')
186 setlocal bufhidden=hide
187 call <SID>NM_cmd_search([search])
188 setlocal bufhidden=delete
189 let b:nm_prev_bufnr = prev_bufnr
193 " --- implement search screen {{{1
195 function! s:NM_cmd_search(words)
197 if g:notmuch_search_newest_first
198 let cmd = cmd + ['--sort=newest-first']
200 let cmd = cmd + ['--sort=oldest-first']
202 let data = s:NM_run(cmd + a:words)
203 let lines = split(data, "\n")
204 let disp = copy(lines)
205 call map(disp, 's:NM_cmd_search_fmtline(v:val)')
207 call <SID>NM_newBuffer('search', join(disp, "\n"))
208 let b:nm_raw_lines = lines
209 let b:nm_search_words = a:words
211 call <SID>NM_cmd_search_mksyntax()
212 call <SID>NM_set_map(g:notmuch_search_maps)
216 function! s:NM_cmd_search_fmtline(line)
217 let m = matchlist(a:line, '^\(thread:\S\+\)\s\([^]]\+\]\) \([^;]\+\); \(.*\) (\([^(]*\))$')
219 return 'ERROR PARSING: ' . a:line
221 let max = g:notmuch_search_from_column_width
223 if strlen(from) >= max
224 let from = substitute(m[3][0:max-4], '[^A-Za-z1-9_]*$', '', '') . '...'
226 return printf('%-20s %-20s | %s (%s)', m[2], from, m[4], m[5])
228 function! s:NM_cmd_search_mksyntax()
229 syntax clear nmSearchFrom
230 exec printf('syntax match nmSearchFrom /\(\] \)\@<=.\{%d\}/ oneline contained', g:notmuch_search_from_column_width)
233 " --- --- search screen action functions {{{2
235 function! s:NM_search_show_thread()
236 let id = <SID>NM_search_thread_id()
239 if exists('b:nm_search_words')
240 let words = ['('] + b:nm_search_words + [')', 'and', id]
243 call <SID>NM_cmd_show(words)
248 function! s:NM_search_prompt()
249 " TODO: input() can support completion
250 let text = input('NotMuch Search: ')
252 let tags = split(text)
254 let tags = s:notmuch_initial_search_words_defaults
256 let prev_bufnr = bufnr('%')
257 if b:nm_type == 'search'
258 " TODO: we intend to replace the current buffer,
259 " ... maybe we could just clear it
260 let prev_bufnr = b:nm_prev_bufnr
261 setlocal bufhidden=delete
263 setlocal bufhidden=hide
265 call <SID>NM_cmd_search(tags)
266 setlocal bufhidden=delete
267 let b:nm_prev_bufnr = prev_bufnr
270 function! s:NM_search_edit()
271 " TODO: input() can support completion
272 let text = input('NotMuch Search: ', join(b:nm_search_words, ' '))
274 call <SID>NM_cmd_search(split(text))
278 function! s:NM_search_archive_thread()
279 call <SID>NM_add_remove_tags_on_screen('', '-', ['inbox'])
280 call <SID>NM_add_remove_tags([], '-', ['inbox'])
284 function! s:NM_search_filter()
285 call <SID>NM_search_filter_helper('Filter: ', '', '')
288 function! s:NM_search_filter_by_tag()
289 call <SID>NM_search_filter_helper('Filter Tag(s): ', 'tag:', 'and')
292 function! s:NM_search_filter_helper(prompt, prefix, joiner)
293 " TODO: input() can support completion
294 let text = input(a:prompt)
299 let tags = split(text)
301 call map(tags, 'a:prefix . v:val')
304 let idx = len(tags) - 1
306 call insert(tags, a:joiner, idx)
310 let tags = b:nm_search_words + ['and', '('] + tags + [')']
312 let prev_bufnr = bufnr('%')
313 setlocal bufhidden=hide
314 call <SID>NM_cmd_search(tags)
315 setlocal bufhidden=delete
316 let b:nm_prev_bufnr = prev_bufnr
319 function! s:NM_search_toggle_order()
320 let g:notmuch_search_newest_first = !g:notmuch_search_newest_first
321 " FIXME: maybe this would be better done w/o reading re-reading the lines
322 " reversing the b:nm_raw_lines and the buffer lines would be better
323 call <SID>NM_search_refresh_view()
326 function! s:NM_search_reply_to_thread()
327 echo 'not implemented'
330 function! s:NM_search_add_tags(tags)
331 call <SID>NM_search_add_remove_tags('Add Tag(s): ', '+', a:tags)
334 function! s:NM_search_remove_tags(tags)
335 call <SID>NM_search_add_remove_tags('Remove Tag(s): ', '-', a:tags)
338 function! s:NM_search_refresh_view()
340 let prev_bufnr = b:nm_prev_bufnr
341 setlocal bufhidden=delete
342 call <SID>NM_cmd_search(b:nm_search_words)
343 let b:nm_prev_bufnr = prev_bufnr
344 " FIXME: should find the line of the thread we were on if possible
345 exec printf('norm %dG', lno)
348 " --- --- search screen helper functions {{{2
350 function! s:NM_search_thread_id()
351 if !exists('b:nm_raw_lines')
352 echoe 'no b:nm_raw_lines'
356 let info = b:nm_raw_lines[line-1]
357 let what = split(info, '\s\+')[0]
362 function! s:NM_search_add_remove_tags(prompt, prefix, intags)
363 if type(a:intags) != type([]) || len(a:intags) == 0
364 " TODO: input() can support completion
365 let text = input(a:prompt)
369 let tags = split(text, ' ')
373 call <SID>NM_add_remove_tags([], a:prefix, tags)
374 call <SID>NM_add_remove_tags_on_screen('', a:prefix, tags)
377 " --- implement show screen {{{1
379 function! s:NM_cmd_show(words)
380 let prev_bufnr = bufnr('%')
381 let data = s:NM_run(['show'] + a:words)
382 let lines = split(data, "\n")
384 let info = s:NM_cmd_show_parse(lines)
386 setlocal bufhidden=hide
387 call <SID>NM_newBuffer('show', join(info['disp'], "\n"))
388 setlocal bufhidden=delete
389 let b:nm_words = a:words
390 let b:nm_raw_info = info
391 let b:nm_prev_bufnr = prev_bufnr
393 call <SID>NM_cmd_show_mkfolds()
394 call <SID>NM_cmd_show_mksyntax()
395 call <SID>NM_set_map(g:notmuch_show_maps)
396 setlocal foldtext=NM_cmd_show_foldtext()
398 setlocal foldcolumn=6
402 function! s:NM_show_previous(can_change_thread, find_matching)
403 let info = b:nm_raw_info
405 for msg in reverse(copy(info['msgs']))
406 if a:find_matching && msg['match'] == '0'
409 if lnum <= msg['start']
413 exec printf('norm %dG', msg['start'])
414 " TODO: try to fit the message on screen
418 if !a:can_change_thread
421 call <SID>NM_kill_this_buffer()
422 if line('.') != line('0')
424 call <SID>NM_search_show_thread()
426 call <SID>NM_show_previous(0)
428 echo 'No more messages.'
432 function! s:NM_show_next(can_change_thread, find_matching)
433 let info = b:nm_raw_info
435 for msg in info['msgs']
436 if a:find_matching && msg['match'] == '0'
439 if lnum >= msg['start']
443 exec printf('norm %dG', msg['start'])
444 " TODO: try to fit the message on screen
448 if !a:can_change_thread
451 call <SID>NM_kill_this_buffer()
452 if line('.') != line('$')
454 call <SID>NM_search_show_thread()
456 echo 'No more messages.'
460 function! s:NM_show_archive_thread()
461 echo 'not implemented'
464 function! s:NM_show_mark_read_then_archive_thread()
465 echo 'not implemented'
468 function! s:NM_show_next_message()
469 echo 'not implemented'
472 function! s:NM_show_mark_read_then_next_open_message()
473 echo 'not implemented'
476 function! s:NM_show_previous_message()
477 echo 'not implemented'
480 function! s:NM_show_reply()
481 echo 'not implemented'
484 function! s:NM_show_view_all_mime_parts()
485 echo 'not implemented'
488 function! s:NM_show_view_raw_message()
489 echo 'not implemented'
492 function! s:NM_show_add_tag()
493 echo 'not implemented'
496 function! s:NM_show_remove_tag()
497 echo 'not implemented'
500 function! s:NM_show_advance_marking_read_and_archiving()
501 echo 'not implemented'
504 function! s:NM_show_pipe_message()
505 echo 'not implemented'
508 function! s:NM_show_previous_fold()
509 echo 'not implemented'
512 function! s:NM_show_next_fold()
513 echo 'not implemented'
516 function! s:NM_show_toggle_fold()
517 echo 'not implemented'
521 " --- --- show screen helper functions {{{2
523 function! s:NM_show_message_id()
524 if !exists('b:nm_raw_info')
525 echoe 'no b:nm_raw_info'
528 let info = b:nm_raw_info
530 for msg in info['msgs']
542 function! s:NM_show_search_words()
543 if !exists('b:nm_words')
544 echoe 'no b:nm_words'
550 function! s:NM_show_fold_toggle(key, type, fold)
551 let info = b:nm_raw_info
556 for fld in info['folds']
558 exec printf('%dfold%s', fld[1], act)
561 exec printf('nnoremap <buffer> %s :call <SID>NM_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
565 " s:NM_cmd_show_parse returns the following dictionary:
566 " 'disp': lines to display
567 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
568 " 'folds': fold info arrays [ type, start, end ]
569 " 'foldtext': fold text indexed by start line
570 function! s:NM_cmd_show_parse(inlines)
571 let info = { 'disp': [],
590 for line in a:inlines
591 let inlnum = inlnum + 1
597 if match(line, g:notmuch_show_part_end_regexp) != -1
598 let part_end = len(info['disp'])
600 call add(info['disp'], line)
603 if in_part == 'text/plain'
604 if !part_end && mode_type == ''
605 if match(line, g:notmuch_show_signature_regexp) != -1
606 let mode_type = 'sig'
607 let mode_start = len(info['disp'])
608 elseif match(line, g:notmuch_show_citation_regexp) != -1
609 let mode_type = 'cit'
610 let mode_start = len(info['disp'])
612 elseif mode_type == 'cit'
613 if part_end || match(line, g:notmuch_show_citation_regexp) == -1
614 let outlnum = len(info['disp'])
615 let foldinfo = [ mode_type, mode_start, outlnum-1, len(info['msgs']),
616 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
619 elseif mode_type == 'sig'
620 let outlnum = len(info['disp'])
621 if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
624 let foldinfo = [ mode_type, mode_start, outlnum-1, len(info['msgs']),
625 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
632 " FIXME: this is a hack for handling two folds being added for one line
633 " we should handle addinga fold in a function
634 if len(foldinfo) && foldinfo[1] < foldinfo[2]
635 call add(info['folds'], foldinfo[0:3])
636 let info['foldtext'][foldinfo[1]] = foldinfo[4]
639 let foldinfo = [ 'text', part_start, part_end, len(info['msgs']),
640 \ printf('[ %d-line %s. Press "p" to show. ]', part_end - part_start, in_part) ]
642 call add(info['disp'], '')
646 if !has_key(msg,'body_start')
647 let msg['body_start'] = len(info['disp']) + 1
649 if match(line, g:notmuch_show_body_end_regexp) != -1
650 let body_end = len(info['disp'])
651 let foldinfo = [ 'bdy', body_start, body_end, len(info['msgs']),
652 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
656 elseif match(line, g:notmuch_show_part_begin_regexp) != -1
657 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
658 let in_part = 'unknown'
662 call add(info['disp'],
663 \ printf('--- %s ---', in_part))
664 let part_start = len(info['disp']) + 1
669 let msg['descr'] = line
670 call add(info['disp'], line)
672 let msg['hdr_start'] = len(info['disp']) + 1
675 if match(line, g:notmuch_show_header_end_regexp) != -1
676 let hdr_start = msg['hdr_start']+1
677 let hdr_end = len(info['disp'])
678 let foldinfo = [ 'hdr', hdr_start, hdr_end, len(info['msgs']),
679 \ printf('[ %d-line headers. Press "h" to show. ]', hdr_end + 1 - hdr_start) ]
680 let msg['header'] = hdr
684 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
687 if match(g:notmuch_show_headers, m[1]) != -1
688 call add(info['disp'], line)
695 if match(line, g:notmuch_show_message_end_regexp) != -1
696 let msg['end'] = len(info['disp'])
697 call add(info['disp'], '')
699 let foldinfo = [ 'msg', msg['start'], msg['end'], len(info['msgs']),
700 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
702 call add(info['msgs'], msg)
709 elseif match(line, g:notmuch_show_header_begin_regexp) != -1
713 elseif match(line, g:notmuch_show_body_begin_regexp) != -1
714 let body_start = len(info['disp']) + 1
720 if match(line, g:notmuch_show_message_begin_regexp) != -1
721 let msg['start'] = len(info['disp']) + 1
723 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
726 let msg['depth'] = m[2]
727 let msg['match'] = m[3]
728 let msg['filename'] = m[4]
735 if len(foldinfo) && foldinfo[1] < foldinfo[2]
736 call add(info['folds'], foldinfo[0:3])
737 let info['foldtext'][foldinfo[1]] = foldinfo[4]
743 function! s:NM_cmd_show_mkfolds()
744 let info = b:nm_raw_info
746 for afold in info['folds']
747 exec printf('%d,%dfold', afold[1], afold[2])
749 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
750 \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
751 \ || (afold[0] == 'bdy' && g:notmuch_show_fold_bodies)
752 \ || (afold[0] == 'hdr' && g:notmuch_show_fold_headers)
754 elseif afold[0] == 'msg'
756 let msg = info['msgs'][idx]
757 if has_key(msg,'match') && msg['match'] == '0'
761 exec printf('%dfold%s', afold[1], state)
765 function! s:NM_cmd_show_mksyntax()
766 let info = b:nm_raw_info
768 for msg in info['msgs']
770 let start = msg['start']
771 let hdr_start = msg['hdr_start']
772 let body_start = msg['body_start']
774 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
775 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
776 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
780 function! NM_cmd_show_foldtext()
781 let foldtext = b:nm_raw_info['foldtext']
782 return foldtext[v:foldstart]
786 " --- notmuch helper functions {{{1
788 function! s:NM_newBuffer(type, content)
790 setlocal buftype=nofile readonly modifiable
793 setlocal nomodifiable
794 execute printf('set filetype=notmuch-%s', a:type)
795 execute printf('set syntax=notmuch-%s', a:type)
796 let b:nm_type = a:type
799 function! s:NM_shell_escape(word)
800 let word = substitute(a:word, '''', '\\''', 'g')
801 return '''' . word . ''''
804 function! s:NM_run(args)
806 call map(words, 's:NM_shell_escape(v:val)')
807 let cmd = g:notmuch_cmd . ' ' . join(words) . '< /dev/null'
809 let start = reltime()
810 let out = system(cmd)
811 let err = v:shell_error
812 let delta = reltime(start)
814 if exists('g:notmuch_debug') && g:notmuch_debug
815 echo printf('[%s] {%s} %s', reltimestr(delta), string(err), string(cmd))
820 echo substitute(out, '\n*$', '', '')
828 " --- external mail handling helpers {{{1
830 function! s:NM_new_mail()
831 echo 'not implemented'
834 " --- other helpers {{{1
836 function! s:NM_kill_this_buffer()
837 if exists('b:nm_prev_bufnr')
838 setlocal bufhidden=delete
839 exec printf(":buffer %d", b:nm_prev_bufnr)
841 echo "This is the last buffer; use :q<CR> to quit."
845 function! s:NM_search_expand(arg)
846 let word = expand(a:arg)
847 let prev_bufnr = bufnr('%')
848 setlocal bufhidden=hide
849 call <SID>NM_cmd_search([word])
850 setlocal bufhidden=delete
851 let b:nm_prev_bufnr = prev_bufnr
854 function! s:NM_add_remove_tags(filter, prefix, tags)
855 let filter = len(a:filter) ? a:filter : [<SID>NM_search_thread_id()]
857 echoe 'Eeek! I couldn''t find the thead id!'
859 echo 'filter = ' . string(filter) . ' ... ' . string(type(filter))
860 call map(a:tags, 'a:prefix . v:val')
861 " TODO: handle errors
863 call extend(args, a:tags)
865 call extend(args, filter)
866 echo 'NUM_run( ' . string(args) . ' )'
867 call <SID>NM_run(args)
870 function! s:NM_add_remove_tags_on_screen(online, prefix, tags)
873 for tagname in a:tags
874 exec printf('silent! %ss/(\([^)]*\)\<%s\>\([^)]*\))$/(\1\2)/', string(a:online), tagname)
877 for tagname in a:tags
878 exec printf('silent! %ss/(\([^)]*\)\([^)]*\))$/(\1 %s)/', string(a:online), tagname)
881 setlocal nomodifiable
884 " --- process and set the defaults {{{1
886 function! NM_set_defaults(force)
887 for [key, dflt] in items(s:notmuch_defaults)
889 if !a:force && exists(key) && type(dflt) == type(eval(key))
891 elseif type(dflt) == type(0)
892 let cmd = printf('let %s = %d', key, dflt)
893 elseif type(dflt) == type('')
894 let cmd = printf('let %s = ''%s''', key, dflt)
895 " FIXME: not sure why this didn't work when dflt is an array
896 "elseif type(dflt) == type([])
897 " let cmd = printf('let %s = %s', key, string(dflt))
899 echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
900 \ a:force, key, string(dflt))
906 call NM_set_defaults(0)
908 " for some reason NM_set_defaults() didn't work for arrays...
909 if !exists('g:notmuch_show_headers')
910 let g:notmuch_show_headers = s:notmuch_show_headers_defaults
912 if !exists('g:notmuch_initial_search_words')
913 let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
915 if !exists('g:notmuch_folders')
916 let g:notmuch_folders = s:notmuch_folders_defaults
920 " --- assign keymaps {{{1
922 function! s:NM_set_map(maps)
924 for [key, code] in items(a:maps)
925 exec printf('nnoremap <buffer> %s %s', key, code)
927 " --- this is a hack for development :)
928 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
931 " --- command handler {{{1
933 function! NotMuch(args)
939 let words = split(args)
940 if words[0] == 'folders'
941 let words = words[1:]
942 call <SID>NM_cmd_folders(words)
943 elseif words[0] == 'search'
945 let words = words[1:]
946 elseif exists('b:nm_search_words')
947 let words = b:nm_search_words
949 let words = g:notmuch_initial_search_words
951 call <SID>NM_cmd_search(words)
953 elseif words[0] == 'show'
954 echoe 'show is not yet implemented.'
957 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
964 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
965 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
967 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :