1 if exists("g:loaded_notmuch")
5 if !has("ruby") || version < 700
9 let g:loaded_notmuch = "yep"
11 let g:notmuch_folders_maps = {
12 \ '<Enter>': 'folders_show_search()',
13 \ 's': 'folders_search_prompt()',
14 \ '=': 'folders_refresh()',
18 let g:notmuch_search_maps = {
19 \ 'q': 'kill_this_buffer()',
20 \ '<Enter>': 'search_show_thread(1)',
21 \ '<Space>': 'search_show_thread(2)',
22 \ 'A': 'search_tag("-inbox -unread")',
23 \ 'I': 'search_tag("-unread")',
24 \ 't': 'search_tag("")',
25 \ 's': 'search_search_prompt()',
26 \ '=': 'search_refresh()',
27 \ '?': 'search_info()',
31 let g:notmuch_show_maps = {
32 \ 'q': 'kill_this_buffer()',
33 \ 'A': 'show_tag("-inbox -unread")',
34 \ 'I': 'show_tag("-unread")',
35 \ 't': 'show_tag("")',
36 \ 'o': 'show_open_msg()',
37 \ 'e': 'show_extract_msg()',
38 \ 's': 'show_save_msg()',
39 \ 'p': 'show_save_patches()',
40 \ 'r': 'show_reply()',
42 \ '<Tab>': 'show_next_msg()',
46 let g:notmuch_compose_maps = {
47 \ ',s': 'compose_send()',
48 \ ',q': 'compose_quit()',
51 let s:notmuch_folders_default = [
52 \ [ 'new', 'tag:inbox and tag:unread' ],
53 \ [ 'inbox', 'tag:inbox' ],
54 \ [ 'unread', 'tag:unread' ],
57 let s:notmuch_date_format_default = '%d.%m.%y'
58 let s:notmuch_datetime_format_default = '%d.%m.%y %H:%M:%S'
59 let s:notmuch_reader_default = 'mutt -f %s'
60 let s:notmuch_sendmail_default = 'sendmail'
61 let s:notmuch_folders_count_threads_default = 0
62 let s:notmuch_compose_start_insert_default = 1
64 function! s:new_file_buffer(type, fname)
65 exec printf('edit %s', a:fname)
66 execute printf('set filetype=notmuch-%s', a:type)
67 execute printf('set syntax=notmuch-%s', a:type)
68 ruby $curbuf.init(VIM::evaluate('a:type'))
71 function! s:on_compose_delete()
75 if input('[s]end/[q]uit? ') =~ '^s'
82 function! s:compose_quit()
83 let b:compose_done = 1
84 call s:kill_this_buffer()
87 function! s:compose_send()
88 let b:compose_done = 1
89 let fname = expand('%')
90 let lines = getline(5, '$')
93 # Generate proper mail to send
94 text = VIM::evaluate('lines').join("\n")
95 fname = VIM::evaluate('fname')
96 transport = Mail.new(text)
97 transport.message_id = generate_message_id
98 transport.charset = 'utf-8'
99 File.write(fname, transport.to_s)
102 let cmdtxt = g:notmuch_sendmail . ' -t -f ' . s:reply_from . ' < ' . fname
103 let out = system(cmdtxt)
104 let err = v:shell_error
107 echo 'Eeek! unable to send mail'
113 echo 'Mail sent successfully.'
114 call s:kill_this_buffer()
117 function! s:show_next_msg()
119 r, c = $curwin.cursor
120 n = $curbuf.line_number
121 i = $messages.index { |m| n >= m.start && n <= m.end }
125 VIM::command("normal #{m.start}zt")
126 $curwin.cursor = r, c
131 function! s:show_reply()
132 ruby open_reply get_message.mail
133 let b:compose_done = 0
134 call s:set_map(g:notmuch_compose_maps)
135 autocmd BufDelete <buffer> call s:on_compose_delete()
136 if g:notmuch_compose_start_insert
141 function! s:compose()
143 let b:compose_done = 0
144 call s:set_map(g:notmuch_compose_maps)
145 autocmd BufDelete <buffer> call s:on_compose_delete()
146 if g:notmuch_compose_start_insert
151 function! s:show_info()
152 ruby vim_puts get_message.inspect
155 function! s:show_extract_msg()
158 m.mail.attachments.each do |a|
159 File.open(a.filename, 'w') do |f|
160 f.write a.body.decoded
161 print "Extracted '#{a.filename}'"
167 function! s:show_open_msg()
170 mbox = File.expand_path('~/.notmuch/vim_mbox')
171 cmd = VIM::evaluate('g:notmuch_reader') % mbox
172 system "notmuch show --format=mbox id:#{m.message_id} > #{mbox} && #{cmd}"
176 function! s:show_save_msg()
177 let file = input('File name: ')
179 file = VIM::evaluate('file')
181 system "notmuch show --format=mbox id:#{m.message_id} > #{file}"
185 function! s:show_save_patches()
187 q = $curbuf.query($cur_thread)
188 t = q.search_threads.first
190 t.toplevel_messages.first.replies.each do |m|
191 next if not m['subject'] =~ /^\[PATCH.*\]/
192 file = "%04d.patch" % [n += 1]
193 system "notmuch show --format=mbox id:#{m.message_id} > #{file}"
195 vim_puts "Saved #{n} patches"
199 function! s:show_tag(intags)
201 let tags = input('tags: ')
205 ruby do_tag(get_cur_view, VIM::evaluate('l:tags'))
206 call s:show_next_thread()
209 function! s:search_search_prompt()
210 let text = input('Search: ')
216 $cur_search = VIM::evaluate('text')
218 search_render($cur_search)
220 setlocal nomodifiable
223 function! s:search_info()
224 ruby vim_puts get_thread_id
227 function! s:search_refresh()
230 ruby search_render($cur_search)
231 setlocal nomodifiable
234 function! s:search_tag(intags)
236 let tags = input('tags: ')
240 ruby do_tag(get_thread_id, VIM::evaluate('l:tags'))
244 function! s:folders_search_prompt()
245 let text = input('Search: ')
249 function! s:folders_refresh()
252 ruby folders_render()
253 setlocal nomodifiable
258 function! s:show_cursor_moved()
261 VIM::command('setlocal modifiable')
263 VIM::command('setlocal nomodifiable')
268 function! s:show_next_thread()
269 call s:kill_this_buffer()
270 if line('.') != line('$')
272 call s:search_show_thread(0)
274 echo 'No more messages.'
278 function! s:kill_this_buffer()
281 VIM::command("bdelete!")
285 function! s:set_map(maps)
287 for [key, code] in items(a:maps)
288 let cmd = printf(":call <SID>%s<CR>", code)
289 exec printf('nnoremap <buffer> %s %s', key, cmd)
293 function! s:new_buffer(type)
295 setlocal buftype=nofile bufhidden=hide
297 execute printf('set filetype=notmuch-%s', a:type)
298 execute printf('set syntax=notmuch-%s', a:type)
299 ruby $curbuf.init(VIM::evaluate('a:type'))
302 function! s:set_menu_buffer()
303 setlocal nomodifiable
310 function! s:show(thread_id)
311 call s:new_buffer('show')
314 thread_id = VIM::evaluate('a:thread_id')
315 $cur_thread = thread_id
317 $curbuf.render do |b|
318 q = $curbuf.query(get_cur_view)
319 q.sort = Notmuch::SORT_OLDEST_FIRST
320 $exclude_tags.each { |t|
323 msgs = q.search_messages
325 m = Mail.read(msg.filename)
326 part = m.find_first_text
327 nm_m = Message.new(msg, m)
329 date_fmt = VIM::evaluate('g:notmuch_datetime_format')
330 date = Time.at(msg.date).strftime(date_fmt)
332 b << "%s %s (%s)" % [msg['from'], date, msg.tags]
333 b << "Subject: %s" % [msg['subject']]
334 b << "To: %s" % msg['to']
335 b << "Cc: %s" % msg['cc']
336 b << "Date: %s" % msg['date']
337 nm_m.body_start = b.count
338 b << "--- %s ---" % part.mime_type
339 part.convert.each_line do |l|
347 $messages.each_with_index do |msg, i|
348 VIM::command("syntax region nmShowMsg#{i}Desc start='\\%%%il' end='\\%%%il' contains=@nmShowMsgDesc" % [msg.start, msg.start + 1])
349 VIM::command("syntax region nmShowMsg#{i}Head start='\\%%%il' end='\\%%%il' contains=@nmShowMsgHead" % [msg.start + 1, msg.body_start])
350 VIM::command("syntax region nmShowMsg#{i}Body start='\\%%%il' end='\\%%%dl' contains=@nmShowMsgBody" % [msg.body_start, msg.end])
353 setlocal nomodifiable
354 call s:set_map(g:notmuch_show_maps)
357 function! s:search_show_thread(mode)
359 mode = VIM::evaluate('a:mode')
363 when 1; $cur_filter = nil
364 when 2; $cur_filter = $cur_search
366 VIM::command("call s:show('#{id}')")
370 function! s:search(search)
371 call s:new_buffer('search')
373 $cur_search = VIM::evaluate('a:search')
374 search_render($cur_search)
376 call s:set_menu_buffer()
377 call s:set_map(g:notmuch_search_maps)
378 autocmd CursorMoved <buffer> call s:show_cursor_moved()
381 function! s:folders_show_search()
383 n = $curbuf.line_number
385 VIM::command("call s:search('#{s}')")
389 function! s:folders()
390 call s:new_buffer('folders')
391 ruby folders_render()
392 call s:set_menu_buffer()
393 call s:set_map(g:notmuch_folders_maps)
398 function! s:set_defaults()
399 if !exists('g:notmuch_date_format')
400 if exists('g:notmuch_rb_date_format')
401 let g:notmuch_date_format = g:notmuch_rb_date_format
403 let g:notmuch_date_format = s:notmuch_date_format_default
407 if !exists('g:notmuch_datetime_format')
408 if exists('g:notmuch_rb_datetime_format')
409 let g:notmuch_datetime_format = g:notmuch_rb_datetime_format
411 let g:notmuch_datetime_format = s:notmuch_datetime_format_default
415 if !exists('g:notmuch_reader')
416 if exists('g:notmuch_rb_reader')
417 let g:notmuch_reader = g:notmuch_rb_reader
419 let g:notmuch_reader = s:notmuch_reader_default
423 if !exists('g:notmuch_sendmail')
424 if exists('g:notmuch_rb_sendmail')
425 let g:notmuch_sendmail = g:notmuch_rb_sendmail
427 let g:notmuch_sendmail = s:notmuch_sendmail_default
431 if !exists('g:notmuch_folders_count_threads')
432 if exists('g:notmuch_rb_count_threads')
433 let g:notmuch_count_threads = g:notmuch_rb_count_threads
435 let g:notmuch_folders_count_threads = s:notmuch_folders_count_threads_default
439 if !exists('g:notmuch_compose_start_insert')
440 let g:notmuch_compose_start_insert = s:notmuch_compose_start_insert_default
443 if !exists('g:notmuch_custom_search_maps') && exists('g:notmuch_rb_custom_search_maps')
444 let g:notmuch_custom_search_maps = g:notmuch_rb_custom_search_maps
447 if !exists('g:notmuch_custom_show_maps') && exists('g:notmuch_rb_custom_show_maps')
448 let g:notmuch_custom_show_maps = g:notmuch_rb_custom_show_maps
451 if exists('g:notmuch_custom_search_maps')
452 call extend(g:notmuch_search_maps, g:notmuch_custom_search_maps)
455 if exists('g:notmuch_custom_show_maps')
456 call extend(g:notmuch_show_maps, g:notmuch_custom_show_maps)
459 if !exists('g:notmuch_folders')
460 if exists('g:notmuch_rb_folders')
461 let g:notmuch_folders = g:notmuch_rb_folders
463 let g:notmuch_folders = s:notmuch_folders_default
468 function! s:NotMuch(...)
469 call s:set_defaults()
482 $email = $email_name = $email_address = nil
487 $mail_installed = defined?(Mail)
489 def get_config_item(item)
491 IO.popen(['notmuch', 'config', 'get', item]) { |out|
498 $db_name = get_config_item('database.path')
499 $email_name = get_config_item('user.name')
500 $email_address = get_config_item('user.primary_email')
501 $email_name = get_config_item('user.name')
502 $email = "%s <%s>" % [$email_name, $email_address]
503 ignore_tags = get_config_item('search.exclude_tags')
504 $exclude_tags = ignore_tags.split("\n")
508 VIM::command("echo '#{s.to_s}'")
512 VIM::command("echo '#{s.inspect}'")
516 # TODO email format, aliases
518 a.gsub!(/[\.@].*/, '')
520 a.gsub!(/ \(.*\)/, '')
525 n = $curbuf.line_number - 1
526 return "thread:%s" % $threads[n]
530 n = $curbuf.line_number
531 return $messages.find { |m| n >= m.start && n <= m.end }
536 return "#{$cur_thread} and (#{$cur_filter})"
542 def generate_message_id
544 random_tag = sprintf('%x%x_%x%x%x',
546 $$, Thread.current.object_id.abs, rand(255))
547 return "<#{random_tag}@#{Socket.gethostname}.notmuch>"
550 def open_compose_helper(lines, cur)
552 'Notmuch-Help: Type in your message here; to help you use these bindings:',
553 'Notmuch-Help: ,s - send the message (Notmuch-Help lines will be removed)',
554 'Notmuch-Help: ,q - abort the message',
557 dir = File.expand_path('~/.notmuch/compose')
558 FileUtils.mkdir_p(dir)
559 Tempfile.open(['nm-', '.mail'], dir) do |f|
564 sig_file = File.expand_path('~/.signature')
565 if File.exists?(sig_file)
567 f.write(File.read(sig_file))
572 cur += help_lines.size + 1
574 VIM::command("let s:reply_from='%s'" % $email_address)
575 VIM::command("call s:new_file_buffer('compose', '#{f.path}')")
576 VIM::command("call cursor(#{cur}, 0)")
581 reply = orig.reply do |m|
584 m.to = [orig[:from].to_s, orig[:to].to_s]
595 addr = Mail::Address.new(orig[:from].value)
597 name = addr.local + "@" if name.nil? && !addr.local.nil?
601 name = "somebody" if name.nil?
603 body_lines << "%s wrote:" % name
604 part = orig.find_first_text
605 part.convert.each_line do |l|
606 body_lines << "> %s" % l.chomp
612 reply.body = body_lines.join("\n")
614 lines += reply.present.lines.map { |e| e.chomp }
617 cur = lines.count - 1
619 open_compose_helper(lines, cur)
625 lines << "From: #{$email}"
636 open_compose_helper(lines, cur)
640 $curbuf.render do |b|
641 folders = VIM::evaluate('g:notmuch_folders')
642 count_threads = VIM::evaluate('g:notmuch_folders_count_threads') == 1
644 folders.each do |name, search|
645 q = $curbuf.query(search)
646 $exclude_tags.each { |t|
650 count = count_threads ? q.count_threads : q.count_messages
651 b << "%9d %-20s (%s)" % [count, name, search]
656 def search_render(search)
657 date_fmt = VIM::evaluate('g:notmuch_date_format')
658 q = $curbuf.query(search)
659 q.sort = Notmuch::SORT_NEWEST_FIRST
660 $exclude_tags.each { |t|
666 $render = $curbuf.render_staged(t) do |b, items|
668 authors = e.authors.to_utf8.split(/[,|]/).map { |a| author_filter(a) }.join(",")
669 date = Time.at(e.newest_date).strftime(date_fmt)
670 subject = e.messages.first['subject']
672 subject = Mail::Field.parse("Subject: " + subject).to_s
674 subject = subject.force_encoding('utf-8')
676 b << "%-12s %3s %-20.20s | %s (%s)" % [date, e.matched_messages, authors, subject, e.tags]
677 $threads << e.thread_id
682 def do_tag(filter, tags)
683 $curbuf.do_write do |db|
685 q.search_messages.each do |e|
687 tags.split.each do |t|
698 e.tags_to_maildir_flags
707 @db = Notmuch::Database.new($db_name)
718 @queries.delete_if { |q| ! q.destroy! }
724 @db = Notmuch::Database.new($db_name)
728 db = Notmuch::Database.new($db_name, :mode => Notmuch::MODE_READ_WRITE)
738 attr_accessor :start, :body_start, :end
739 attr_reader :message_id, :filename, :mail
741 def initialize(msg, mail)
742 @message_id = msg.message_id
743 @filename = msg.filename
747 mail.import_headers(msg) if not $mail_installed
751 "id:%s" % @message_id
755 "id:%s, file:%s" % [@message_id, @filename]
760 def initialize(buffer, enumerable, block)
762 @enumerable = enumerable
766 @b.render { do_next }
770 @last_render - @b.line_number <= $curwin.height
774 items = @enumerable.take($curwin.height * 2)
775 return if items.empty?
776 @block.call @b, items
777 @last_render = @b.count
788 def render_staged(enumerable, &block)
789 StagedRender.new(self, enumerable, block)
795 (1..old_count).each do
807 class Notmuch::Message
813 # workaround for bug in vim's ruby
821 def self.parse(string)
822 return nil if string.empty?
823 return Header.new(string.split(/,\s+/))
831 def initialize(string = nil)
838 if string =~ /(.*?(\r\n|\n))\2/m
839 head, body = $1, $' || '', $2
841 head, body = string, ''
847 @headers[name.to_sym]
851 @headers[name.to_sym] = value
854 def format_header(value)
855 value.to_s.tr('_', '-').gsub(/(\w+)/) { $1.capitalize }
860 @headers.each do |key, value|
861 buffer << "%s: %s\r\n" %
862 [format_header(key), value]
890 r = Mail::Message.new
894 r[:in_reply_to] = self[:message_id]
895 r[:references] = self[:references]
899 HEADERS = [ :from, :to, :cc, :references, :in_reply_to, :reply_to, :message_id ]
901 def import_headers(m)
903 dashed = format_header(e)
904 @headers[e] = Header.parse(m[dashed])
911 if not $mail_installed
912 puts "WARNING: Install the 'mail' gem, without it support is limited"
914 def self.read(filename)
915 Message.new(File.open(filename, 'rb') { |f| f.read })
919 include SimpleMessage
926 return self if not multipart?
927 return text_part || html_part
931 if mime_type != "text/html"
934 IO.popen(VIM::evaluate('exists("g:notmuch_html_converter") ? ' +
935 'g:notmuch_html_converter : "elinks --dump"'), "w+") do |pipe|
936 pipe.write(decode_body)
946 header.fields.each do |f|
947 buffer << "%s: %s\r\n" % [f.name, f.to_s]
958 RUBY_VERSION >= "1.9" ? force_encoding('utf-8') : self
965 call s:search(join(a:000))
971 command -nargs=* NotMuch call s:NotMuch(<f-args>)
973 " vim: set noexpandtab: