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*>' ,
56 \ 'g:notmuch_compose_temp_file_dir': '~/.notmuch/compose/' ,
59 " defaults for g:notmuch_initial_search_words
60 " override with: let g:notmuch_initial_search_words = [ ... ]
61 let s:notmuch_initial_search_words_defaults = [
62 \ 'tag:inbox and tag:unread',
65 " defaults for g:notmuch_show_headers
66 " override with: let g:notmuch_show_headers = [ ... ]
67 let s:notmuch_show_headers_defaults = [
75 " defaults for g:notmuch_folders
76 " override with: let g:notmuch_folders = [ ... ]
77 let s:notmuch_folders_defaults = [
78 \ [ 'new', 'tag:inbox and tag:unread' ],
79 \ [ 'inbox', 'tag:inbox' ],
80 \ [ 'unread', 'tag:unread' ],
83 " defaults for g:notmuch_signature
84 " override with: let g:notmuch_signature = [ ... ]
85 let s:notmuch_signature_defaults = [
88 \ 'email sent from notmuch.vim plugin'
91 " defaults for g:notmuch_compose_headers
92 " override with: let g:notmuch_compose_headers = [ ... ]
93 let s:notmuch_compose_headers_defaults = [
101 " --- keyboard mapping definitions {{{1
103 " --- --- bindings for folders mode {{{2
105 let g:notmuch_folders_maps = {
106 \ 's': ':call <SID>NM_search_prompt()<CR>',
107 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
108 \ '=': ':call <SID>NM_folders_refresh_view()<CR>',
109 \ '<Enter>': ':call <SID>NM_folders_show_search()<CR>',
112 " --- --- bindings for search screen {{{2
113 let g:notmuch_search_maps = {
114 \ '<Enter>': ':call <SID>NM_search_show_thread()<CR>',
115 \ '<C-]>': ':call <SID>NM_search_expand(''<cword>'')<CR>',
116 \ 'a': ':call <SID>NM_search_archive_thread()<CR>',
117 \ 'f': ':call <SID>NM_search_filter()<CR>',
118 \ 'm': ':call <SID>NM_new_mail()<CR>',
119 \ 'o': ':call <SID>NM_search_toggle_order()<CR>',
120 \ 'r': ':call <SID>NM_search_reply_to_thread()<CR>',
121 \ 's': ':call <SID>NM_search_prompt()<CR>',
122 \ ',s': ':call <SID>NM_search_edit()<CR>',
123 \ 't': ':call <SID>NM_search_filter_by_tag()<CR>',
124 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
125 \ '+': ':call <SID>NM_search_add_tags([])<CR>',
126 \ '-': ':call <SID>NM_search_remove_tags([])<CR>',
127 \ '=': ':call <SID>NM_search_refresh_view()<CR>',
128 \ '?': ':echo <SID>NM_search_thread_id()<CR>',
131 " --- --- bindings for show screen {{{2
132 let g:notmuch_show_maps = {
133 \ '<C-P>': ':call <SID>NM_show_previous(1, 0)<CR>',
134 \ '<C-N>': ':call <SID>NM_show_next(1, 0)<CR>',
135 \ '<C-]>': ':call <SID>NM_search_expand(''<cword>'')<CR>',
136 \ 'q': ':call <SID>NM_kill_this_buffer()<CR>',
138 \ 'b': ':call <SID>NM_show_fold_toggle(''b'', ''bdy'', !g:notmuch_show_fold_bodies)<CR>',
139 \ 'c': ':call <SID>NM_show_fold_toggle(''c'', ''cit'', !g:notmuch_show_fold_citations)<CR>',
140 \ 'h': ':call <SID>NM_show_fold_toggle(''h'', ''hdr'', !g:notmuch_show_fold_headers)<CR>',
141 \ 's': ':call <SID>NM_show_fold_toggle(''s'', ''sig'', !g:notmuch_show_fold_signatures)<CR>',
143 \ 'a': ':call <SID>NM_show_archive_thread()<CR>',
144 \ 'A': ':call <SID>NM_show_mark_read_then_archive_thread()<CR>',
145 \ 'N': ':call <SID>NM_show_mark_read_then_next_open_message()<CR>',
146 \ 'v': ':call <SID>NM_show_view_all_mime_parts()<CR>',
147 \ '+': ':call <SID>NM_show_add_tag()<CR>',
148 \ '-': ':call <SID>NM_show_remove_tag()<CR>',
149 \ '<Space>': ':call <SID>NM_show_advance_marking_read_and_archiving()<CR>',
150 \ '\|': ':call <SID>NM_show_pipe_message()<CR>',
152 \ '<S-Tab>': ':call <SID>NM_show_previous_fold()<CR>',
153 \ '<Tab>': ':call <SID>NM_show_next_fold()<CR>',
154 \ '<Enter>': ':call <SID>NM_show_toggle_fold()<CR>',
156 \ 'r': ':call <SID>NM_show_reply()<CR>',
157 \ 'm': ':call <SID>NM_new_mail()<CR>',
158 \ '?': ':echo <SID>NM_show_message_id() . '' @ '' . join(<SID>NM_show_search_words())<CR>',
161 " --- --- bindings for compose screen {{{2
162 let g:notmuch_compose_nmaps = {
163 \ ',s': ':call <SID>NM_compose_send()<CR>',
164 \ ',q': ':call <SID>NM_kill_this_buffer()<CR>',
165 \ '<Tab>': ':call <SID>NM_compose_next_entry_area()<CR>',
167 let g:notmuch_compose_imaps = {
168 \ '<Tab>': '<C-r>=<SID>NM_compose_next_entry_area()<CR>',
171 " --- implement folders screen {{{1
173 function! s:NM_cmd_folders(words)
175 echoe 'Not exapecting any arguments for folders command.'
180 for entry in g:notmuch_folders
181 let [ name, search ] = entry
182 let data = s:NM_run(cmd + [search])
183 let cnt = matchlist(data, '\(\d\+\)')[1]
184 call add(disp, printf('%9d %-20s (%s)', cnt, name, search))
185 call add(searches, search)
188 call <SID>NM_newBuffer('folders', join(disp, "\n"))
189 let b:nm_searches = searches
190 let b:nm_timestamp = reltime()
192 call <SID>NM_cmd_folders_mksyntax()
193 call <SID>NM_set_map('n', g:notmuch_folders_maps)
198 function! s:NM_cmd_folders_mksyntax()
201 " --- --- folders screen action functions {{{2
203 function! s:NM_folders_refresh_view()
205 setlocal bufhidden=delete
206 call s:NM_cmd_folders([])
207 exec printf('norm %dG', lno)
210 function! s:NM_folders_show_search()
212 let search = b:nm_searches[line-1]
214 let prev_bufnr = bufnr('%')
215 setlocal bufhidden=hide
216 call <SID>NM_cmd_search([search])
217 setlocal bufhidden=delete
218 let b:nm_prev_bufnr = prev_bufnr
222 " --- implement search screen {{{1
224 function! s:NM_cmd_search(words)
226 if g:notmuch_search_newest_first
227 let cmd = cmd + ['--sort=newest-first']
229 let cmd = cmd + ['--sort=oldest-first']
231 let data = s:NM_run(cmd + a:words)
232 let lines = split(data, "\n")
233 let disp = copy(lines)
234 call map(disp, 's:NM_cmd_search_fmtline(v:val)')
236 call <SID>NM_newBuffer('search', join(disp, "\n"))
237 let b:nm_raw_lines = lines
238 let b:nm_search_words = a:words
240 call <SID>NM_cmd_search_mksyntax()
241 call <SID>NM_set_map('n', g:notmuch_search_maps)
245 function! s:NM_cmd_search_fmtline(line)
246 let m = matchlist(a:line, '^\(thread:\S\+\)\s\([^]]\+\]\) \([^;]\+\); \(.*\) (\([^(]*\))$')
248 return 'ERROR PARSING: ' . a:line
250 let max = g:notmuch_search_from_column_width
252 if strlen(from) >= max
253 let from = substitute(m[3][0:max-4], '[^A-Za-z1-9_]*$', '', '') . '...'
255 return printf('%-20s %-20s | %s (%s)', m[2], from, m[4], m[5])
257 function! s:NM_cmd_search_mksyntax()
258 syntax clear nmSearchFrom
259 exec printf('syntax match nmSearchFrom /\(\] \)\@<=.\{%d\}/ oneline contained', g:notmuch_search_from_column_width)
262 " --- --- search screen action functions {{{2
264 function! s:NM_search_show_thread()
265 let id = <SID>NM_search_thread_id()
268 if exists('b:nm_search_words')
269 let words = ['('] + b:nm_search_words + [')', 'and', id]
272 call <SID>NM_cmd_show(words)
277 function! s:NM_search_prompt()
278 " TODO: input() can support completion
279 let text = input('NotMuch Search: ')
281 let tags = split(text)
283 let tags = s:notmuch_initial_search_words_defaults
285 let prev_bufnr = bufnr('%')
286 if b:nm_type == 'search' && exists('b:nm_prev_bufnr')
287 " TODO: we intend to replace the current buffer,
288 " ... maybe we could just clear it
289 let prev_bufnr = b:nm_prev_bufnr
290 setlocal bufhidden=delete
292 setlocal bufhidden=hide
294 call <SID>NM_cmd_search(tags)
295 setlocal bufhidden=delete
296 let b:nm_prev_bufnr = prev_bufnr
299 function! s:NM_search_edit()
300 " TODO: input() can support completion
301 let text = input('NotMuch Search: ', join(b:nm_search_words, ' '))
303 call <SID>NM_cmd_search(split(text))
307 function! s:NM_search_archive_thread()
308 call <SID>NM_add_remove_tags_on_screen('', '-', ['inbox'])
309 call <SID>NM_add_remove_tags([], '-', ['inbox'])
313 function! s:NM_search_filter()
314 call <SID>NM_search_filter_helper('Filter: ', '', '')
317 function! s:NM_search_filter_by_tag()
318 call <SID>NM_search_filter_helper('Filter Tag(s): ', 'tag:', 'and')
321 function! s:NM_search_filter_helper(prompt, prefix, joiner)
322 " TODO: input() can support completion
323 let text = input(a:prompt)
328 let tags = split(text)
330 call map(tags, 'a:prefix . v:val')
333 let idx = len(tags) - 1
335 call insert(tags, a:joiner, idx)
339 let tags = b:nm_search_words + ['and', '('] + tags + [')']
341 let prev_bufnr = bufnr('%')
342 setlocal bufhidden=hide
343 call <SID>NM_cmd_search(tags)
344 setlocal bufhidden=delete
345 let b:nm_prev_bufnr = prev_bufnr
348 function! s:NM_search_toggle_order()
349 let g:notmuch_search_newest_first = !g:notmuch_search_newest_first
350 " FIXME: maybe this would be better done w/o reading re-reading the lines
351 " reversing the b:nm_raw_lines and the buffer lines would be better
352 call <SID>NM_search_refresh_view()
355 function! s:NM_search_reply_to_thread()
356 echo 'not implemented'
359 function! s:NM_search_add_tags(tags)
360 call <SID>NM_search_add_remove_tags('Add Tag(s): ', '+', a:tags)
363 function! s:NM_search_remove_tags(tags)
364 call <SID>NM_search_add_remove_tags('Remove Tag(s): ', '-', a:tags)
367 function! s:NM_search_refresh_view()
369 let prev_bufnr = b:nm_prev_bufnr
370 setlocal bufhidden=delete
371 call <SID>NM_cmd_search(b:nm_search_words)
372 let b:nm_prev_bufnr = prev_bufnr
373 " FIXME: should find the line of the thread we were on if possible
374 exec printf('norm %dG', lno)
377 " --- --- search screen helper functions {{{2
379 function! s:NM_search_thread_id()
380 if !exists('b:nm_raw_lines')
381 echoe 'no b:nm_raw_lines'
384 let mnum = line('.') - 1
385 if len(b:nm_raw_lines) <= mnum
388 echo 'len=' . string(len(b:nm_raw_lines)) . ' mnum=' . string(mnum)
389 let info = b:nm_raw_lines[mnum]
390 let what = split(info, '\s\+')[0]
394 function! s:NM_search_add_remove_tags(prompt, prefix, intags)
395 if type(a:intags) != type([]) || len(a:intags) == 0
396 " TODO: input() can support completion
397 let text = input(a:prompt)
401 let tags = split(text, ' ')
405 call <SID>NM_add_remove_tags([], a:prefix, tags)
406 call <SID>NM_add_remove_tags_on_screen('', a:prefix, tags)
409 " --- implement show screen {{{1
411 function! s:NM_cmd_show(words)
412 let prev_bufnr = bufnr('%')
413 let data = s:NM_run(['show'] + a:words)
414 let lines = split(data, "\n")
416 let info = s:NM_cmd_show_parse(lines)
418 setlocal bufhidden=hide
419 call <SID>NM_newBuffer('show', join(info['disp'], "\n"))
420 setlocal bufhidden=delete
421 let b:nm_words = a:words
422 let b:nm_raw_info = info
423 let b:nm_prev_bufnr = prev_bufnr
425 call <SID>NM_cmd_show_mkfolds()
426 call <SID>NM_cmd_show_mksyntax()
427 call <SID>NM_set_map('n', g:notmuch_show_maps)
428 setlocal foldtext=NM_cmd_show_foldtext()
430 setlocal foldcolumn=6
434 function! s:NM_show_previous(can_change_thread, find_matching)
435 let info = b:nm_raw_info
437 for msg in reverse(copy(info['msgs']))
438 if a:find_matching && msg['match'] == '0'
441 if lnum <= msg['start']
445 exec printf('norm %dGzt', msg['start'])
446 " TODO: try to fit the message on screen
449 if !a:can_change_thread
452 call <SID>NM_kill_this_buffer()
455 call <SID>NM_search_show_thread()
457 call <SID>NM_show_previous(0, a:find_matching)
459 echo 'No more messages.'
463 function! s:NM_show_next(can_change_thread, find_matching)
464 let info = b:nm_raw_info
466 for msg in info['msgs']
467 if a:find_matching && msg['match'] == '0'
470 if lnum >= msg['start']
474 exec printf('norm %dGzt', msg['start'])
475 " TODO: try to fit the message on screen
478 if a:can_change_thread
479 call <SID>NM_show_next_thread()
483 function! s:NM_show_next_thread()
484 call <SID>NM_kill_this_buffer()
485 if line('.') != line('$')
487 call <SID>NM_search_show_thread()
489 echo 'No more messages.'
493 function! s:NM_show_archive_thread()
494 echo 'not implemented'
497 function! s:NM_show_mark_read_then_archive_thread()
498 echo 'not implemented'
501 function! s:NM_show_mark_read_then_next_open_message()
502 echo 'not implemented'
505 function! s:NM_show_previous_message()
506 echo 'not implemented'
509 function! s:NM_show_reply()
510 echo 'not implemented'
513 function! s:NM_show_view_all_mime_parts()
514 echo 'not implemented'
517 function! s:NM_show_view_raw_message()
518 echo 'not implemented'
521 function! s:NM_show_add_tag()
522 echo 'not implemented'
525 function! s:NM_show_remove_tag()
526 echo 'not implemented'
529 " if entire message is not visible scroll down 1/2 page or less to get to the bottom of message
530 " otherwise go to next message
531 " any message that is viewed entirely has inbox and unread tags removed
532 function! s:NM_show_advance_marking_read_and_archiving()
533 let advance_tags = ['unread', 'inbox']
535 let vis_top = line('w0')
536 let vis_bot = line('w$')
538 let msg_top = <SID>NM_show_get_message_for_line(vis_top)
539 if !has_key(msg_top,'id')
540 echo "No top visible message."
543 " if the top message is the last message, just expunge the entire thread and move on
544 if msg_top['end'] == line('$')
546 for msg in b:nm_raw_info['msgs']
547 if has_key(msg,'match') && msg['match'] != '0'
551 call add(ids, msg['id'])
555 let filter = ['('] + advance_tags + [')', 'AND', '('] + ids + [')']
556 call <SID>NM_add_remove_tags(filter, '-', advance_tags)
557 call <SID>NM_show_next(1, 1)
561 let msg_bot = <SID>NM_show_get_message_for_line(vis_bot)
562 if !has_key(msg_bot,'id')
563 echo "No bottom visible message."
566 " if entire message fits on the screen, read/archive it, move to the next one
567 if msg_top['id'] != msg_bot['id'] || msg_top['end'] <= vis_bot
568 call <SID>NM_add_remove_tags_on_screen(msg_top['start'], '-', advance_tags)
569 exec printf('norm %dG', vis_top)
570 call <SID>NM_show_next(0, 1)
571 if has_key(msg_top,'match') && msg_top['match'] != '0'
573 " do this last to hide the latency
574 let filter = ['('] + advance_tags + [')', 'AND', msg_top['id']]
575 call <SID>NM_add_remove_tags(filter, '-', advance_tags)
580 " entire message does not fit on the screen, scroll down to bottom, max 1/2 screen
581 let jmp = winheight(winnr()) / 2
582 let max = msg_bot['end'] - vis_bot
586 exec printf('norm %dGzt', vis_top + jmp)
590 function! s:NM_show_pipe_message()
591 echo 'not implemented'
594 function! s:NM_show_previous_fold()
595 echo 'not implemented'
598 function! s:NM_show_next_fold()
599 echo 'not implemented'
602 function! s:NM_show_toggle_fold()
603 echo 'not implemented'
607 " --- --- show screen helper functions {{{2
609 function! s:NM_show_get_message_for_line(line)
610 for msg in b:nm_raw_info['msgs']
611 if a:line > msg['end']
619 function! s:NM_show_message_id()
620 if !exists('b:nm_raw_info')
621 echoe 'no b:nm_raw_info'
624 let msg = <SID>NM_show_get_message_for_line(line('.'))
631 function! s:NM_show_search_words()
632 if !exists('b:nm_words')
633 echoe 'no b:nm_words'
639 function! s:NM_show_fold_toggle(key, type, fold)
640 let info = b:nm_raw_info
645 for fld in info['folds']
647 exec printf('%dfold%s', fld[1], act)
650 exec printf('nnoremap <buffer> %s :call <SID>NM_show_fold_toggle(''%s'', ''%s'', %d)<CR>', a:key, a:key, a:type, !a:fold)
654 " s:NM_cmd_show_parse returns the following dictionary:
655 " 'disp': lines to display
656 " 'msgs': message info dicts { start, end, id, depth, filename, descr, header }
657 " 'folds': fold info arrays [ type, start, end ]
658 " 'foldtext': fold text indexed by start line
659 function! s:NM_cmd_show_parse(inlines)
660 let info = { 'disp': [],
679 for line in a:inlines
680 let inlnum = inlnum + 1
686 if match(line, g:notmuch_show_part_end_regexp) != -1
687 let part_end = len(info['disp'])
689 call add(info['disp'], line)
692 if in_part == 'text/plain'
693 if !part_end && mode_type == ''
694 if match(line, g:notmuch_show_signature_regexp) != -1
695 let mode_type = 'sig'
696 let mode_start = len(info['disp'])
697 elseif match(line, g:notmuch_show_citation_regexp) != -1
698 let mode_type = 'cit'
699 let mode_start = len(info['disp'])
701 elseif mode_type == 'cit'
702 if part_end || match(line, g:notmuch_show_citation_regexp) == -1
703 let outlnum = len(info['disp'])
704 let foldinfo = [ mode_type, mode_start, outlnum-1, len(info['msgs']),
705 \ printf('[ %d-line citation. Press "c" to show. ]', outlnum - mode_start) ]
708 elseif mode_type == 'sig'
709 let outlnum = len(info['disp'])
710 if (outlnum - mode_start) > g:notmuch_show_signature_lines_max
713 let foldinfo = [ mode_type, mode_start, outlnum-1, len(info['msgs']),
714 \ printf('[ %d-line signature. Press "s" to show. ]', outlnum - mode_start) ]
721 " FIXME: this is a hack for handling two folds being added for one line
722 " we should handle addinga fold in a function
723 if len(foldinfo) && foldinfo[1] < foldinfo[2]
724 call add(info['folds'], foldinfo[0:3])
725 let info['foldtext'][foldinfo[1]] = foldinfo[4]
728 let foldinfo = [ 'text', part_start, part_end, len(info['msgs']),
729 \ printf('[ %d-line %s. Press "p" to show. ]', part_end - part_start, in_part) ]
731 call add(info['disp'], '')
735 if !has_key(msg,'body_start')
736 let msg['body_start'] = len(info['disp']) + 1
738 if match(line, g:notmuch_show_body_end_regexp) != -1
739 let body_end = len(info['disp'])
740 let foldinfo = [ 'bdy', body_start, body_end, len(info['msgs']),
741 \ printf('[ BODY %d - %d lines ]', len(info['msgs']), body_end - body_start) ]
745 elseif match(line, g:notmuch_show_part_begin_regexp) != -1
746 let m = matchlist(line, 'ID: \(\d\+\), Content-type: \(\S\+\)')
747 let in_part = 'unknown'
751 call add(info['disp'],
752 \ printf('--- %s ---', in_part))
753 let part_start = len(info['disp']) + 1
758 let msg['descr'] = line
759 call add(info['disp'], line)
761 let msg['hdr_start'] = len(info['disp']) + 1
764 if match(line, g:notmuch_show_header_end_regexp) != -1
765 let hdr_start = msg['hdr_start']+1
766 let hdr_end = len(info['disp'])
767 let foldinfo = [ 'hdr', hdr_start, hdr_end, len(info['msgs']),
768 \ printf('[ %d-line headers. Press "h" to show. ]', hdr_end + 1 - hdr_start) ]
769 let msg['header'] = hdr
773 let m = matchlist(line, '^\(\w\+\):\s*\(.*\)$')
776 if match(g:notmuch_show_headers, m[1]) != -1
777 call add(info['disp'], line)
784 if match(line, g:notmuch_show_message_end_regexp) != -1
785 let msg['end'] = len(info['disp'])
786 call add(info['disp'], '')
788 let foldinfo = [ 'msg', msg['start'], msg['end'], len(info['msgs']),
789 \ printf('[ MSG %d - %s ]', len(info['msgs']), msg['descr']) ]
791 call add(info['msgs'], msg)
798 elseif match(line, g:notmuch_show_header_begin_regexp) != -1
802 elseif match(line, g:notmuch_show_body_begin_regexp) != -1
803 let body_start = len(info['disp']) + 1
809 if match(line, g:notmuch_show_message_begin_regexp) != -1
810 let msg['start'] = len(info['disp']) + 1
812 let m = matchlist(line, g:notmuch_show_message_parse_regexp)
815 let msg['depth'] = m[2]
816 let msg['match'] = m[3]
817 let msg['filename'] = m[4]
824 if len(foldinfo) && foldinfo[1] < foldinfo[2]
825 call add(info['folds'], foldinfo[0:3])
826 let info['foldtext'][foldinfo[1]] = foldinfo[4]
832 function! s:NM_cmd_show_mkfolds()
833 let info = b:nm_raw_info
835 for afold in info['folds']
836 exec printf('%d,%dfold', afold[1], afold[2])
838 if (afold[0] == 'sig' && g:notmuch_show_fold_signatures)
839 \ || (afold[0] == 'cit' && g:notmuch_show_fold_citations)
840 \ || (afold[0] == 'bdy' && g:notmuch_show_fold_bodies)
841 \ || (afold[0] == 'hdr' && g:notmuch_show_fold_headers)
843 elseif afold[0] == 'msg'
845 let msg = info['msgs'][idx]
846 if has_key(msg,'match') && msg['match'] == '0'
850 exec printf('%dfold%s', afold[1], state)
854 function! s:NM_cmd_show_mksyntax()
855 let info = b:nm_raw_info
857 for msg in info['msgs']
859 let start = msg['start']
860 let hdr_start = msg['hdr_start']
861 let body_start = msg['body_start']
863 exec printf('syntax region nmShowMsg%dDesc start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgDesc', cnt, start, start+1)
864 exec printf('syntax region nmShowMsg%dHead start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgHead', cnt, hdr_start, body_start)
865 exec printf('syntax region nmShowMsg%dBody start=''\%%%dl'' end=''\%%%dl'' contains=@nmShowMsgBody', cnt, body_start, end)
869 function! NM_cmd_show_foldtext()
870 let foldtext = b:nm_raw_info['foldtext']
871 return foldtext[v:foldstart]
875 " --- implement compose screen {{{1
877 function! s:NM_cmd_compose(words, body_lines)
879 let start_on_line = 0
883 let m = matchlist(word, '^\([^:]\+\):\s*\(.*\)\s*$')
885 throw 'Eeek! bad parameter ''' . string(word) . ''''
887 let key = substitute(m[1], '\<\w', '\U&', 'g')
888 if !has_key(hdrs, key)
892 call add(hdrs[key], m[2])
896 if !has_key(hdrs, 'From') || !len(hdrs['From'])
897 let me = <SID>NM_compose_get_user_email()
898 let hdrs['From'] = [ me ]
901 for key in g:notmuch_compose_headers
902 let text = has_key(hdrs, key) ? join(hdrs[key], ', ') : ''
903 call add(lines, key . ': ' . text)
904 if !start_on_line && !strlen(text)
905 let start_on_line = len(lines)
909 for [key,val] in items(hdrs)
910 if match(g:notmuch_compose_headers, key) == -1
911 let line = key . ': ' . join(val, ', ')
912 call add(lines, line)
916 call extend(lines, [ '', '' ])
918 let start_on_line = len(lines) + 1
922 call extend(lines, a:body_lines)
926 call extend(lines, g:notmuch_signature)
928 let prev_bufnr = bufnr('%')
929 setlocal bufhidden=hide
930 call <SID>NM_newFileBuffer(g:notmuch_compose_temp_file_dir, '%s.mail',
932 setlocal bufhidden=hide
934 call <SID>NM_cmd_compose_mksyntax()
935 call <SID>NM_set_map('n', g:notmuch_compose_nmaps)
936 call <SID>NM_set_map('i', g:notmuch_compose_imaps)
938 exec printf('norm %dG', start_on_line)
940 echo 'Type your message, use <TAB> to jump to next header and then body.'
942 function! s:NM_cmd_compose_mksyntax()
943 silent! setlocal syntax=mail
946 function! s:NM_compose_send()
947 echo 'not implemented'
950 function! s:NM_compose_next_entry_area()
952 let hdr_end = <SID>NM_compose_find_line_match(1,'^$',1)
953 echo 'header end = ' . string(hdr_end)
956 let line = getline(lnum)
957 if match(line, '^\([^:]\+\):\s*$') == -1
958 call cursor(lnum, strlen(line) + 1)
961 while match(getline(lnum+1), '^\s') != -1
964 call cursor(lnum, strlen(getline(lnum)) + 1)
967 elseif lnum == hdr_end
968 call cursor(lnum+1, strlen(getline(lnum+1)) + 1)
971 echo 'mode=' . mode()
973 if !getbufvar(bufnr('.'), '&et')
977 let shiftwidth = a:shiftwidth
978 let shiftwidth = shiftwidth - ((virtcol('.')-1) % shiftwidth)
979 " we assume no one has shiftwidth set to more than 40 :)
980 return ' '[0:shiftwidth]
984 " --- --- compose screen helper functions {{{2
986 function! s:NM_compose_get_user_email()
987 let name = substitute(system('id -u -n'), '\v(^\s*|\s*$|\n)', '', 'g')
988 let fqdn = substitute(system('hostname -f'), '\v(^\s*|\s*$|\n)', '', 'g')
990 " TODO: do this properly
991 return name . '@' . fqdn
994 function! s:NM_compose_find_line_match(start, pattern, failure)
998 if match(getline(lnum), a:pattern) != -1
1007 " --- notmuch helper functions {{{1
1009 function! s:NM_newBuffer(type, content)
1011 setlocal buftype=nofile readonly modifiable scrolloff=0 sidescrolloff=0
1012 silent put=a:content
1014 setlocal nomodifiable
1015 execute printf('set filetype=notmuch-%s', a:type)
1016 execute printf('set syntax=notmuch-%s', a:type)
1017 let b:nm_type = a:type
1020 function! s:NM_newFileBuffer(fdir, fname, type, lines)
1021 let fdir = expand(a:fdir)
1022 if !isdirectory(fdir)
1023 call mkdir(fdir, 'p')
1025 let file_name = <SID>NM_mktemp(fdir, a:fname)
1026 if writefile(a:lines, file_name)
1027 throw 'Eeek! couldn''t write to temporary file ' . file_name
1029 exec printf('edit %s', file_name)
1030 setlocal buftype= noreadonly modifiable scrolloff=0 sidescrolloff=0
1031 execute printf('set filetype=notmuch-%s', a:type)
1032 execute printf('set syntax=notmuch-%s', a:type)
1033 let b:nm_type = a:type
1036 function! s:NM_mktemp(dir, name)
1037 let time_stamp = strftime('%Y%m%d-%H%M%S')
1038 let file_name = substitute(a:dir,'/*$','/','') . printf(a:name, time_stamp)
1039 " TODO: check if it exists, try again
1043 function! s:NM_shell_escape(word)
1044 " TODO: use shellescape()
1045 let word = substitute(a:word, '''', '\\''', 'g')
1046 return '''' . word . ''''
1049 " this function was taken from git.vim, then fixed up
1050 " http://github.com/motemen/git-vim
1051 function! s:NM_shell_split(cmd)
1052 let l:split_cmd = []
1056 let t = match(cmd, '\S', iStart)
1062 let iSpace = match(cmd, '\v(\s|$)', iStart)
1067 let iQuote1 = match(cmd, '\(^["'']\|[^\\]\@<=["'']\)', iStart)
1068 if iQuote1 > iSpace || iQuote1 < iStart
1069 let iEnd = iSpace - 1
1070 let l:split_cmd += [ cmd[iStart : iEnd] ]
1072 let q = cmd[iQuote1]
1073 let iQuote2 = match(cmd, '[^\\]\@<=[' . q . ']', iQuote1 + 1)
1074 if iQuote2 < iQuote1
1075 throw 'No matching ' . q . ' quote'
1078 let l:split_cmd += [ cmd[iStart+1 : iEnd-1 ] ]
1082 let iStart = iEnd + 1
1089 function! s:NM_run(args)
1091 call map(words, 's:NM_shell_escape(v:val)')
1092 let cmd = g:notmuch_cmd . ' ' . join(words) . '< /dev/null'
1094 let start = reltime()
1095 let out = system(cmd)
1096 let err = v:shell_error
1097 let delta = reltime(start)
1099 if exists('g:notmuch_debug') && g:notmuch_debug
1100 echo printf('[%s] {%s} %s', reltimestr(delta), string(err), string(cmd))
1105 echo substitute(out, '\n*$', '', '')
1113 " --- external mail handling helpers {{{1
1115 function! s:NM_new_mail()
1116 echo 'not implemented'
1119 " --- other helpers {{{1
1121 function! s:NM_kill_this_buffer()
1122 if exists('b:nm_prev_bufnr')
1123 setlocal bufhidden=delete
1124 exec printf(":buffer %d", b:nm_prev_bufnr)
1126 echo "This is the last buffer; use :q<CR> to quit."
1130 function! s:NM_search_expand(arg)
1131 let word = expand(a:arg)
1132 let prev_bufnr = bufnr('%')
1133 setlocal bufhidden=hide
1134 call <SID>NM_cmd_search([word])
1135 setlocal bufhidden=delete
1136 let b:nm_prev_bufnr = prev_bufnr
1139 function! s:NM_add_remove_tags(filter, prefix, tags)
1140 let filter = len(a:filter) ? a:filter : [<SID>NM_search_thread_id()]
1142 echoe 'Eeek! I couldn''t find the thead id!'
1144 call map(a:tags, 'a:prefix . v:val')
1146 call extend(args, a:tags)
1147 call add(args, '--')
1148 call extend(args, filter)
1149 " TODO: handle errors
1150 call <SID>NM_run(args)
1153 function! s:NM_add_remove_tags_on_screen(online, prefix, tags)
1156 for tagname in a:tags
1157 exec printf('silent! %ss/(\([^)]*\)\<%s\>\([^)]*\))$/(\1\2)/', string(a:online), tagname)
1160 for tagname in a:tags
1161 exec printf('silent! %ss/(\([^)]*\)\([^)]*\))$/(\1 %s)/', string(a:online), tagname)
1164 setlocal nomodifiable
1167 " --- process and set the defaults {{{1
1169 function! NM_set_defaults(force)
1170 for [key, dflt] in items(s:notmuch_defaults)
1172 if !a:force && exists(key) && type(dflt) == type(eval(key))
1174 elseif type(dflt) == type(0)
1175 let cmd = printf('let %s = %d', key, dflt)
1176 elseif type(dflt) == type('')
1177 let cmd = printf('let %s = ''%s''', key, dflt)
1178 " FIXME: not sure why this didn't work when dflt is an array
1179 "elseif type(dflt) == type([])
1180 " let cmd = printf('let %s = %s', key, string(dflt))
1182 echoe printf('E: Unknown type in NM_set_defaults(%d) using [%s,%s]',
1183 \ a:force, key, string(dflt))
1189 call NM_set_defaults(0)
1191 " for some reason NM_set_defaults() didn't work for arrays...
1192 if !exists('g:notmuch_show_headers')
1193 let g:notmuch_show_headers = s:notmuch_show_headers_defaults
1195 if !exists('g:notmuch_initial_search_words')
1196 let g:notmuch_initial_search_words = s:notmuch_initial_search_words_defaults
1198 if !exists('g:notmuch_folders')
1199 let g:notmuch_folders = s:notmuch_folders_defaults
1202 if !exists('g:notmuch_signature')
1203 let g:notmuch_signature = s:notmuch_signature_defaults
1205 if !exists('g:notmuch_compose_headers')
1206 let g:notmuch_compose_headers = s:notmuch_compose_headers_defaults
1209 " --- assign keymaps {{{1
1211 function! s:NM_set_map(type, maps)
1213 for [key, code] in items(a:maps)
1214 exec printf('%snoremap <buffer> %s %s', a:type, key, code)
1216 " --- this is a hack for development :)
1217 nnoremap ,nmr :source ~/.vim/plugin/notmuch.vim<CR>:call NotMuch('')<CR>
1220 " --- command handler {{{1
1222 function! NotMuch(args)
1225 let args = 'folders'
1228 let words = <SID>NM_shell_split(args)
1229 if words[0] == 'folders' || words[0] == 'f'
1230 let words = words[1:]
1231 call <SID>NM_cmd_folders(words)
1233 elseif words[0] == 'search' || words[0] == 's'
1235 let words = words[1:]
1236 elseif exists('b:nm_search_words')
1237 let words = b:nm_search_words
1239 let words = g:notmuch_initial_search_words
1241 call <SID>NM_cmd_search(words)
1243 elseif words[0] == 'show'
1244 echoe 'show is not yet implemented.'
1246 elseif words[0] == 'new' || words[0] == 'compose'
1247 let words = words[1:]
1248 call <SID>NM_cmd_compose(words, [])
1251 function! CompleteNotMuch(arg_lead, cmd_line, cursor_pos)
1258 command! -nargs=* -complete=customlist,CompleteNotMuch NotMuch call NotMuch(<q-args>)
1259 cabbrev notmuch <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'NotMuch' : 'notmuch')<CR>
1261 " vim: set ft=vim ts=8 sw=8 et foldmethod=marker :