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' ,
27 \ 'g:notmuch_search_newest_first': 1 ,
28 \ 'g:notmuch_search_from_column_width': 20 ,
30 \ 'g:notmuch_show_fold_signatures': 1 ,
31 \ 'g:notmuch_show_fold_citations': 1 ,
32 \ 'g:notmuch_show_fold_bodies': 0 ,
33 \ 'g:notmuch_show_fold_headers': 1 ,
35 \ 'g:notmuch_show_message_begin_regexp': '^
\fmessage{' ,
36 \ 'g:notmuch_show_message_end_regexp': '^
\fmessage}' ,
37 \ 'g:notmuch_show_header_begin_regexp': '^
\fheader{' ,
38 \ 'g:notmuch_show_header_end_regexp': '^
\fheader}' ,
39 \ 'g:notmuch_show_body_begin_regexp': '^
\fbody{' ,
40 \ 'g:notmuch_show_body_end_regexp': '^
\fbody}' ,
41 \ 'g:notmuch_show_attachment_begin_regexp': '^
\fattachment{' ,
42 \ 'g:notmuch_show_attachment_end_regexp': '^
\fattachment}' ,
43 \ 'g:notmuch_show_part_begin_regexp': '^
\fpart{' ,
44 \ 'g:notmuch_show_part_end_regexp': '^
\fpart}' ,
45 \ 'g:notmuch_show_marker_regexp': '^
\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$',
47 \ 'g:notmuch_show_message_parse_regexp': '\(id:[^ ]*\) depth:\([0-9]*\) filename:\(.*\)$',
48 \ 'g:notmuch_show_tags_regexp': '(\([^)]*\))$' ,
50 \ 'g:notmuch_show_signature_regexp': '^\(-- \?\|_\+\)$' ,
51 \ 'g:notmuch_show_signature_lines_max': 12 ,
53 \ 'g:notmuch_show_citation_regexp': '^\s*>' ,
56 " defaults for g:notmuch_initial_search_words
57 " override with: let g:notmuch_initial_search_words = [ ... ]
58 let s:notmuch_initial_search_words_defaults = [
59 \ 'tag:inbox and tag:unread',
62 " defaults for g:notmuch_show_headers
63 " override with: let g:notmuch_show_headers = [ ... ]
64 let s:notmuch_show_headers_defaults = [
72 " defaults for g:notmuch_folders
73 " override with: let g:notmuch_folders = [ ... ]
74 let s:notmuch_folders_defaults = [
75 \ [ 'new', 'tag:inbox and tag:unread' ],
76 \ [ 'inbox', 'tag:inbox' ],
77 \ [ 'unread', 'tag:unread' ],
80 " --- keyboard mapping definitions {{{1
82 " --- --- bindings for folders mode {{{2
84 let g:notmuch_folders_maps = {
85 \ 's': ':call <SID>NM_search_prompt()<CR>',
86 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
87 \ '=': ':call <SID>NM_folders_refresh_view()<CR>',
88 \ '<Enter>': ':call <SID>NM_folders_show_search()<CR>',
91 " --- --- bindings for search screen {{{2
92 let g:notmuch_search_maps = {
93 \ '<Enter>': ':call <SID>NM_search_show_thread()<CR>',
94 \ '<C-]>': ':call <SID>NM_search_expand(''<cword>'')<CR>',
95 \ 'a': ':call <SID>NM_search_archive_thread()<CR>',
96 \ 'f': ':call <SID>NM_search_filter()<CR>',
97 \ 'm': ':call <SID>NM_new_mail()<CR>',
98 \ 'o': ':call <SID>NM_search_toggle_order()<CR>',
99 \ 'r': ':call <SID>NM_search_reply_to_thread()<CR>',
100 \ 's': ':call <SID>NM_search_prompt()<CR>',
101 \ ',s': ':call <SID>NM_search_edit()<CR>',
102 \ 't': ':call <SID>NM_search_filter_by_tag()<CR>',
103 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
104 \ '+': ':call <SID>NM_search_add_tags([])<CR>',
105 \ '-': ':call <SID>NM_search_remove_tags([])<CR>',
106 \ '=': ':call <SID>NM_search_refresh_view()<CR>',
107 \ '?': ':echo <SID>NM_search_thread_id()<CR>',
110 " --- --- bindings for show screen {{{2
111 let g:notmuch_show_maps = {
112 \ '<C-P>': ':call <SID>NM_show_previous(1)<CR>',
113 \ '<C-N>': ':call <SID>NM_show_next(1)<CR>',
114 \ '<C-]>': ':call <SID>NM_search_expand(''<cword>'')<CR>',
115 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
117 \ 'b': ':call <SID>NM_show_fold_toggle(''b'', ''bdy'', !g:notmuch_show_fold_bodies)<CR>',
118 \ 'c': ':call <SID>NM_show_fold_toggle(''c'', ''cit'', !g:notmuch_show_fold_citations)<CR>',
119 \ 'h': ':call <SID>NM_show_fold_toggle(''h'', ''hdr'', !g:notmuch_show_fold_headers)<CR>',
120 \ 's': ':call <SID>NM_show_fold_toggle(''s'', ''sig'', !g:notmuch_show_fold_signatures)<CR>',
122 \ 'a': ':call <SID>NM_show_archive_thread()<CR>',
123 \ 'A': ':call <SID>NM_show_mark_read_then_archive_thread()<CR>',
124 \ 'N': ':call <SID>NM_show_mark_read_then_next_open_message()<CR>',
125 \ 'v': ':call <SID>NM_show_view_all_mime_parts()<CR>',
126 \ '+': ':call <SID>NM_show_add_tag()<CR>',
127 \ '-': ':call <SID>NM_show_remove_tag()<CR>',
128 \ '<Space>': ':call <SID>NM_show_advance_marking_read_and_archiving()<CR>',
129 \ '\|': ':call <SID>NM_show_pipe_message()<CR>',
131 \ '<S-Tab>': ':call <SID>NM_show_previous_fold()<CR>',
132 \ '<Tab>': ':call <SID>NM_show_next_fold()<CR>',
133 \ '<Enter>': ':call <SID>NM_show_toggle_fold()<CR>',
135 \ 'r': ':call <SID>NM_show_reply()<CR>',
136 \ 'm': ':call <SID>NM_new_mail()<CR>',
137 \ '?': ':echo <SID>NM_show_message_id() . '' @ '' . join(<SID>NM_show_search_words())<CR>',
141 " --- implement folders screen {{{1
143 function! s:NM_cmd_folders(words)
145 echoe 'Not exapecting any arguments for folders command.'
150 for entry in g:notmuch_folders
151 let [ name, search ] = entry
152 let data = s:NM_run(cmd + [search])
153 let cnt = matchlist(data, '\(\d\+\)')[1]
154 call add(disp, printf('%9d %-20s (%s)', cnt, name, search))
155 call add(searches, search)
158 call <SID>NM_newBuffer('folders', join(disp, "\n"))
159 let b:nm_searches = searches
160 let b:nm_timestamp = reltime()
162 call <SID>NM_cmd_folders_mksyntax()
163 call <SID>NM_set_map(g:notmuch_folders_maps)
168 function! s:NM_cmd_folders_mksyntax()
171 " --- --- folders screen action functions {{{2
173 function! s:NM_folders_refresh_view()
175 setlocal bufhidden=delete
176 call s:NM_cmd_folders([])
177 exec printf('norm %dG', lno)
180 function! s:NM_folders_show_search()
182 let search = b:nm_searches[line-1]
184 let prev_bufnr = bufnr('%')
185 setlocal bufhidden=hide
186 call <SID>NM_cmd_search([search])
187 setlocal bufhidden=delete
188 let b:nm_prev_bufnr = prev_bufnr
192 " --- implement search screen {{{1
194 function! s:NM_cmd_search(words)
196 if g:notmuch_search_newest_first
197 let cmd = cmd + ['--sort=newest-first']
199 let cmd = cmd + ['--sort=oldest-first']
201 let data = s:NM_run(cmd + a:words)
202 let lines = split(data, "\n")
203 let disp = copy(lines)
204 call map(disp, 's:NM_cmd_search_fmtline(v:val)')
206 call <SID>NM_newBuffer('search', join(disp, "\n"))
207 let b:nm_raw_lines = lines
208 let b:nm_search_words = a:words
210 call <SID>NM_cmd_search_mksyntax()
211 call <SID>NM_set_map(g:notmuch_search_maps)
215 function! s:NM_cmd_search_fmtline(line)
216 let m = matchlist(a:line, '^\(thread:\S\+\)\s\([^]]\+\]\) \([^;]\+\); \(.*\) (\([^(]*\))$')
218 return 'ERROR PARSING: ' . a:line
220 let max = g:notmuch_search_from_column_width
222 if strlen(from) >= max
223 let from = substitute(m[3][0:max-4], '[^A-Za-z1-9_]*$', '', '') . '...'
225 return printf('%-20s %-20s | %s (%s)', m[2], from, m[4], m[5])
227 function! s:NM_cmd_search_mksyntax()
228 syntax clear nmSearchFrom
229 exec printf('syntax match nmSearchFrom /\(\] \)\@<=.\{%d\}/ oneline contained', g:notmuch_search_from_column_width)
232 " --- --- search screen action functions {{{2
234 function! s:NM_search_show_thread()
235 let id = <SID>NM_search_thread_id()
238 if exists('b:nm_search_words')
239 let words = ['('] + b:nm_search_words + [')', 'and', id]
242 call <SID>NM_cmd_show(words)
247 function! s:NM_search_prompt()
248 " TODO: input() can support completion
249 let text = input('NotMuch Search: ')
251 let tags = split(text)
253 let tags = s:notmuch_initial_search_words_defaults
255 let prev_bufnr = bufnr('%')
256 if b:nm_type == 'search'
257 " TODO: we intend to replace the current buffer,
258 " ... maybe we could just clear it
259 let prev_bufnr = b:nm_prev_bufnr
260 setlocal bufhidden=delete
262 setlocal bufhidden=hide
264 call <SID>NM_cmd_search(tags)
265 setlocal bufhidden=delete
266 let b:nm_prev_bufnr = prev_bufnr
269 function! s:NM_search_edit()
270 " TODO: input() can support completion
271 let text = input('NotMuch Search: ', join(b:nm_search_words, ' '))
273 call <SID>NM_cmd_search(split(text))
277 function! s:NM_search_archive_thread()
278 call <SID>NM_add_remove_tags_on_screen('-', ['inbox'])
279 call <SID>NM_add_remove_tags('-', ['inbox'])
283 function! s:NM_search_filter()
284 call <SID>NM_search_filter_helper('Filter: ', '', '')
287 function! s:NM_search_filter_by_tag()
288 call <SID>NM_search_filter_helper('Filter Tag(s): ', 'tag:', 'and')
291 function! s:NM_search_filter_helper(prompt, prefix, joiner)
292 " TODO: input() can support completion
293 let text = input(a:prompt)
298 let tags = split(text)
300 call map(tags, 'a:prefix . v:val')
303 let idx = len(tags) - 1
305 call insert(tags, a:joiner, idx)
309 let tags = b:nm_search_words + ['and', '('] + tags + [')']
311 let prev_bufnr = bufnr('%')
312 setlocal bufhidden=hide
313 call <SID>NM_cmd_search(tags)
314 setlocal bufhidden=delete
315 let b:nm_prev_bufnr = prev_bufnr
318 function! s:NM_search_toggle_order()
319 let g:notmuch_search_newest_first = !g:notmuch_search_newest_first
320 " FIXME: maybe this would be better done w/o reading re-reading the lines
321 " reversing the b:nm_raw_lines and the buffer lines would be better
322 call <SID>NM_search_refresh_view()
325 function! s:NM_search_reply_to_thread()
326 echo 'not implemented'
329 function! s:NM_search_add_tags(tags)
330 call <SID>NM_search_add_remove_tags('Add Tag(s): ', '+', a:tags)
333 function! s:NM_search_remove_tags(tags)
334 call <SID>NM_search_add_remove_tags('Remove Tag(s): ', '-', a:tags)
337 function! s:NM_search_refresh_view()
339 let prev_bufnr = b:nm_prev_bufnr
340 setlocal bufhidden=delete
341 call <SID>NM_cmd_search(b:nm_search_words)
342 let b:nm_prev_bufnr = prev_bufnr
343 " FIXME: should find the line of the thread we were on if possible
344 exec printf('norm %dG', lno)
347 " --- --- search screen helper functions {{{2
349 function! s:NM_search_thread_id()
350 if !exists('b:nm_raw_lines')
351 echoe 'no b:nm_raw_lines'
355 let info = b:nm_raw_lines[line-1]
356 let what = split(info, '\s\+')[0]
361 function! s:NM_search_add_remove_tags(prompt, prefix, intags)
362 if type(a:intags) != type([]) || len(a:intags) == 0
363 " TODO: input() can support completion
364 let text = input(a:prompt)
368 let tags = split(text, ' ')
372 call <SID>NM_add_remove_tags(a:prefix, tags)
373 call <SID>NM_add_remove_tags_on_screen(a:prefix, tags)
376 " --- implement show screen {{{1
378 function! s:NM_cmd_show(words)
379 let prev_bufnr = bufnr('%')
380 let data = s:NM_run(['show'] + a:words)
381 let lines = split(data, "\n")
383 let info = s:NM_cmd_show_parse(lines)
385 setlocal bufhidden=hide
386 call <SID>NM_newBuffer('show', join(info['disp'], "\n"))
387 setlocal bufhidden=delete
388 let b:nm_words = a:words
389 let b:nm_raw_info = info
390 let b:nm_prev_bufnr = prev_bufnr
392 call <SID>NM_cmd_show_mkfolds()
393 call <SID>NM_cmd_show_mksyntax()
394 call <SID>NM_set_map(g:notmuch_show_maps)
395 setlocal foldtext=NM_cmd_show_foldtext()
397 setlocal foldcolumn=6
401 function! s:NM_show_previous(can_change_thread)
402 let info = b:nm_raw_info
404 for msg in reverse(copy(info['msgs']))
405 if lnum <= msg['start']
409 exec printf('norm %dG', msg['start'])
410 " TODO: try to fit the message on screen
414 if !a:can_change_thread
417 call <SID>NM_kill_this_buffer()
418 if line('.') != line('0')
420 call <SID>NM_search_show_thread()
422 call <SID>NM_show_previous(0)
424 echo 'No more messages.'
428 function! s:NM_show_next(can_change_thread)
429 let info = b:nm_raw_info
431 for msg in info['msgs']
432 if lnum >= msg['start']
436 exec printf('norm %dG', msg['start'])
437 " TODO: try to fit the message on screen
441 if !a:can_change_thread
444 call <SID>NM_kill_this_buffer()
445 if line('.') != line('$')
447 call <SID>NM_search_show_thread()
449 echo 'No more messages.'
453 function! s:NM_show_archive_thread()
454 echo 'not implemented'
457 function! s:NM_show_mark_read_then_archive_thread()
458 echo 'not implemented'
461 function! s:NM_show_next_message()
462 echo 'not implemented'
465 function! s:NM_show_mark_read_then_next_open_message()
466 echo 'not implemented'
469 function! s:NM_show_previous_message()
470 echo 'not implemented'
473 function! s:NM_show_reply()
474 echo 'not implemented'
477 function! s:NM_show_view_all_mime_parts()
478 echo 'not implemented'
481 function! s:NM_show_view_raw_message()
482 echo 'not implemented'
485 function! s:NM_show_add_tag()
486 echo 'not implemented'
489 function! s:NM_show_remove_tag()
490 echo 'not implemented'
493 function! s:NM_show_advance_marking_read_and_archiving()
494 echo 'not implemented'
497 function! s:NM_show_pipe_message()
498 echo 'not implemented'
501 function! s:NM_show_previous_fold()
502 echo 'not implemented'
505 function! s:NM_show_next_fold()
506 echo 'not implemented'
509 function! s:NM_show_toggle_fold()
510 echo 'not implemented'
514 " --- --- show screen helper functions {{{2
516 function! s:NM_show_message_id()
517 if !exists('b:nm_raw_info')
518 echoe 'no b:nm_raw_info'
521 let info = b:nm_raw_info
523 for msg in info['msgs']
533 function! s:NM_show_search_words()
534 if !exists('b:nm_words')
535 echoe 'no b:nm_words'
541 function! s:NM_show_fold_toggle(key, type, fold)
542 let info = b:nm_raw_info
547 for fld in info['folds']
549 exec printf('%dfold%s', fld[1], act)
552 exec printf('nnoremap <buffer> %s :call <SID>NM_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
556 " s:NM_cmd_show_parse returns the following dictionary:
557 " 'disp': lines to display
558 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
559 " 'folds': fold info arrays [ type, start, end ]
560 " 'foldtext': fold text indexed by start line
561 function! s:NM_cmd_show_parse(inlines)
562 let info = { 'disp': [],
581 for line in a:inlines
582 let inlnum = inlnum + 1
588 if match(line, g:notmuch_show_part_end_regexp) != -1
589 let part_end = len(info['disp'])
591 call add(info['disp'], line)
594 if in_part == 'text/plain'
595 if !part_end && mode_type == ''
596 if match(line, g:notmuch_show_signature_regexp) != -1
597 let mode_type = 'sig'
598 let mode_start = len(info['disp'])
599 elseif match(line, g:notmuch_show_citation_regexp) != -1
600 let mode_type = 'cit'
601 let mode_start = len(info['disp'])
603 elseif mode_type == 'cit'
604 if part_end || match(line, g:notmuch_show_citation_regexp) == -1
605 let outlnum = len(info['disp'])
606 let foldinfo = [ mode_type, mode_start, outlnum-1,
607 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
610 elseif mode_type == 'sig'
611 let outlnum = len(info['disp'])
612 if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
615 let foldinfo = [ mode_type, mode_start, outlnum-1,
616 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
623 " FIXME: this is a hack for handling two folds being added for one line
624 " we should handle addinga fold in a function
625 if len(foldinfo) && foldinfo[1] < foldinfo[2]
626 call add(info['folds'], foldinfo[0:2])
627 let info['foldtext'][foldinfo[1]] = foldinfo[3]
630 let foldinfo = [ 'text', part_start, part_end,
631 \ printf('[ %d-line %s. Press "p" to show. ]', part_end - part_start, in_part) ]
633 call add(info['disp'], '')
637 if !has_key(msg,'body_start')
638 let msg['body_start'] = len(info['disp']) + 1
640 if match(line, g:notmuch_show_body_end_regexp) != -1
641 let body_end = len(info['disp'])
642 let foldinfo = [ 'bdy', body_start, body_end,
643 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
647 elseif match(line, g:notmuch_show_part_begin_regexp) != -1
648 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
649 let in_part = 'unknown'
653 call add(info['disp'],
654 \ printf('--- %s ---', in_part))
655 let part_start = len(info['disp']) + 1
660 let msg['descr'] = line
661 call add(info['disp'], line)
663 let msg['hdr_start'] = len(info['disp']) + 1
666 if match(line, g:notmuch_show_header_end_regexp) != -1
667 let hdr_start = msg['hdr_start']+1
668 let hdr_end = len(info['disp'])
669 let foldinfo = [ 'hdr', hdr_start, hdr_end,
670 \ printf('[ %d-line headers. Press "h" to show. ]', hdr_end + 1 - hdr_start) ]
671 let msg['header'] = hdr
675 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
678 if match(g:notmuch_show_headers, m[1]) != -1
679 call add(info['disp'], line)
686 if match(line, g:notmuch_show_message_end_regexp) != -1
687 let msg['end'] = len(info['disp'])
688 call add(info['disp'], '')
690 let foldinfo = [ 'msg', msg['start'], msg['end'],
691 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
693 call add(info['msgs'], msg)
700 elseif match(line, g:notmuch_show_header_begin_regexp) != -1
704 elseif match(line, g:notmuch_show_body_begin_regexp) != -1
705 let body_start = len(info['disp']) + 1
711 if match(line, g:notmuch_show_message_begin_regexp) != -1
712 let msg['start'] = len(info['disp']) + 1
714 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
717 let msg['depth'] = m[2]
718 let msg['filename'] = m[3]
725 if len(foldinfo) && foldinfo[1] < foldinfo[2]
726 call add(info['folds'], foldinfo[0:2])
727 let info['foldtext'][foldinfo[1]] = foldinfo[3]
733 function! s:NM_cmd_show_mkfolds()
734 let info = b:nm_raw_info
736 for afold in info['folds']
737 exec printf('%d,%dfold', afold[1], afold[2])
738 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
739 \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
740 \ || (afold[0] == 'bdy' && g:notmuch_show_fold_bodies)
741 \ || (afold[0] == 'hdr' && g:notmuch_show_fold_headers)
742 exec printf('%dfoldclose', afold[1])
744 exec printf('%dfoldopen', afold[1])
749 function! s:NM_cmd_show_mksyntax()
750 let info = b:nm_raw_info
752 for msg in info['msgs']
754 let start = msg['start']
755 let hdr_start = msg['hdr_start']
756 let body_start = msg['body_start']
758 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
759 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
760 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
764 function! NM_cmd_show_foldtext()
765 let foldtext = b:nm_raw_info['foldtext']
766 return foldtext[v:foldstart]
770 " --- notmuch helper functions {{{1
772 function! s:NM_newBuffer(type, content)
774 setlocal buftype=nofile readonly modifiable
777 setlocal nomodifiable
778 execute printf('set filetype=notmuch-%s', a:type)
779 execute printf('set syntax=notmuch-%s', a:type)
780 let b:nm_type = a:type
783 function! s:NM_shell_escape(word)
784 let word = substitute(a:word, '''', '\\''', 'g')
785 return '''' . word . ''''
788 function! s:NM_run(args)
790 call map(words, 's:NM_shell_escape(v:val)')
791 let cmd = g:notmuch_cmd . ' ' . join(words) . '< /dev/null'
793 let start = reltime()
794 let out = system(cmd)
795 let err = v:shell_error
796 let delta = reltime(start)
798 echo printf('[%s] {%s} %s', reltimestr(delta), string(err), string(cmd))
802 echo substitute(out, '\n*$', '', '')
810 " --- external mail handling helpers {{{1
812 function! s:NM_new_mail()
813 echo 'not implemented'
816 " --- other helpers {{{1
818 function! s:NM_kill_this_buffer()
819 if exists('b:nm_prev_bufnr')
820 setlocal bufhidden=delete
821 exec printf(":buffer %d", b:nm_prev_bufnr)
823 echo "This is the last buffer; use :q<CR> to quit."
827 function! s:NM_search_expand(arg)
828 let word = expand(a:arg)
829 let prev_bufnr = bufnr('%')
830 setlocal bufhidden=hide
831 call <SID>NM_cmd_search([word])
832 setlocal bufhidden=delete
833 let b:nm_prev_bufnr = prev_bufnr
836 function! s:NM_add_remove_tags(prefix, tags)
837 let id = <SID>NM_search_thread_id()
839 echoe 'Eeek! I couldn''t find the thead id!'
841 call map(a:tags, 'a:prefix . v:val')
842 " TODO: handle errors
843 call <SID>NM_run(['tag'] + a:tags + ['--', id])
846 function! s:NM_add_remove_tags_on_screen(prefix, tags)
850 for tagname in a:tags
851 exec printf('silent %ss/(\([^)]*\)\<%s\>\([^)]*\))$/(\1\2)/', online, tagname)
854 for tagname in a:tags
855 exec printf('silent %ss/(\([^)]*\)\([^)]*\))$/(\1 %s)/', online, tagname)
858 setlocal nomodifiable
861 " --- process and set the defaults {{{1
863 function! NM_set_defaults(force)
864 for [key, dflt] in items(s:notmuch_defaults)
866 if !a:force && exists(key) && type(dflt) == type(eval(key))
868 elseif type(dflt) == type(0)
869 let cmd = printf('let %s = %d', key, dflt)
870 elseif type(dflt) == type('')
871 let cmd = printf('let %s = ''%s''', key, dflt)
872 " FIXME: not sure why this didn't work when dflt is an array
873 "elseif type(dflt) == type([])
874 " let cmd = printf('let %s = %s', key, string(dflt))
876 echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
877 \ a:force, key, string(dflt))
883 call NM_set_defaults(0)
885 " for some reason NM_set_defaults() didn't work for arrays...
886 if !exists('g:notmuch_show_headers')
887 let g:notmuch_show_headers = s:notmuch_show_headers_defaults
889 if !exists('g:notmuch_initial_search_words')
890 let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
892 if !exists('g:notmuch_folders')
893 let g:notmuch_folders = s:notmuch_folders_defaults
897 " --- assign keymaps {{{1
899 function! s:NM_set_map(maps)
901 for [key, code] in items(a:maps)
902 exec printf('nnoremap <buffer> %s %s', key, code)
904 " --- this is a hack for development :)
905 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
908 " --- command handler {{{1
910 function! NotMuch(args)
916 let words = split(args)
917 if words[0] == 'folders'
918 let words = words[1:]
919 call <SID>NM_cmd_folders(words)
920 elseif words[0] == 'search'
922 let words = words[1:]
923 elseif exists('b:nm_search_words')
924 let words = b:nm_search_words
926 let words = g:notmuch_initial_search_words
928 call <SID>NM_cmd_search(words)
930 elseif words[0] == 'show'
931 echoe 'show is not yet implemented.'
934 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
941 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
942 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
944 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :