Skip to content

Commit

Permalink
Merge pull request felipec#9 from ff2000/manual_folding
Browse files Browse the repository at this point in the history
Manual folding
  • Loading branch information
imain committed Jan 6, 2015
2 parents 68c1c07 + 047665e commit 6e642bf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
44 changes: 31 additions & 13 deletions plugin/notmuch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_thread_id

def get_message
n = $curbuf.line_number
return $curbuf.messages.find { |m| n >= m.start && n <= m.end }
return $curbuf.messages.find { |m| n >= m.start && n < m.end }
end

def get_cur_view
Expand Down Expand Up @@ -551,13 +551,25 @@ def rb_show_save_patches(dir)
end
end

def fold_range(from, to)
VIM::command("normal #{from}G")
VIM::command("normal zf#{to}G")
end

def fold_message(msg, fold_headers)
fold_range(msg.full_header_start, msg.full_header_end-1) if fold_headers
fold_range(msg.start, msg.end-1)
end

def rb_show(thread_id, msg_id)
show_full_headers = VIM::evaluate('g:notmuch_show_folded_full_headers')
show_threads_folded = VIM::evaluate('g:notmuch_show_folded_threads')
show_full_headers = VIM::evaluate('g:notmuch_show_folded_full_headers') == 1
# show_threads_folded = VIM::evaluate('g:notmuch_show_folded_threads') == 1
showheaders = VIM::evaluate('g:notmuch_show_headers')

$curbuf.cur_thread = thread_id
messages = $curbuf.messages
messages.clear
focus_msg = nil
$curbuf.render do |b|
q = $curbuf.query(get_cur_view)
q.sort = Notmuch::SORT_OLDEST_FIRST
Expand All @@ -571,13 +583,12 @@ def rb_show(thread_id, msg_id)
date = Time.at(msg.date).strftime(date_fmt)
nm_m.start = b.count
b << "From: %s %s (%s)" % [msg['from'], date, msg.tags]
showheaders = VIM::evaluate('g:notmuch_show_headers')
showheaders.each do |h|
b << "%s: %s" % [h, m.header[h]]
end
nm_m.full_header_start = b.count
if show_full_headers
# Now show the rest in a folded area.
nm_m.full_header_start = b.count
m.header.fields.each do |k|
# Only show the ones we haven't already printed out.
if not showheaders.include?(k.name)
Expand All @@ -603,8 +614,9 @@ def rb_show(thread_id, msg_id)
end
b << ""
nm_m.end = b.count
focus_msg = nm_m if !focus_msg and nm_m.tags.include?('unread')
if !msg_id.empty? and nm_m.message_id == msg_id
VIM::command("normal #{nm_m.start}zt")
focus_msg = nm_m
end
end
b.delete(b.count)
Expand All @@ -613,15 +625,21 @@ def rb_show(thread_id, msg_id)
messages.each_with_index do |msg, i|
VIM::command("syntax region nmShowMsg#{i}Desc start='\\%%%il' end='\\%%%il' contains=@nmShowMsgDesc" % [msg.start, msg.start + 1])
VIM::command("syntax region nmShowMsg#{i}Head start='\\%%%il' end='\\%%%il' contains=@nmShowMsgHead" % [msg.start + 1, msg.full_header_start])
VIM::command("syntax region nmShowMsg#{i}Body start='\\%%%il' end='\\%%%dl' contains=@nmShowMsgBody" % [msg.body_start, msg.end])
if show_full_headers
VIM::command("syntax region nmFold#{i}Headers start='\\%%%il' end='\\%%%il' fold transparent contains=@nmShowMsgHead" % [msg.full_header_start, msg.full_header_end])
end
# Only fold the whole message if there are multiple emails in this thread.
if messages.count > 1 and show_threads_folded
VIM::command("syntax region nmShowMsgFold#{i} start='\\%%%il' end='\\%%%il' fold transparent contains=ALL" % [msg.start, msg.end])
VIM::command("syntax region nmShowMsg#{i}Body start='\\%%%il' end='\\%%%dl' contains=@nmShowMsgBody" % [msg.body_start, msg.end - 1])

fold_message(msg, show_full_headers)

if msg.tags.include?('unread')
VIM::command("normal #{msg.start}G")
VIM::command("foldopen")
end
end
focus_msg = messages[-1] if !focus_msg
VIM::command("normal #{focus_msg.start}G")
VIM::command("foldopen")
scrolloff = VIM::evaluate("&scrolloff")
VIM::command("normal #{scrolloff}j")
VIM::command("normal zt")
end

def rb_search_show_thread(mode)
Expand Down
2 changes: 1 addition & 1 deletion plugin/notmuch.vim
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ function! s:show(thread_id, msg_id)
ruby rb_show(VIM::evaluate('a:thread_id'), VIM::evaluate('a:msg_id'))

setlocal nomodifiable
setlocal foldmethod=syntax
setlocal foldmethod=manual
call s:set_map(g:notmuch_show_maps)
endfunction

Expand Down

0 comments on commit 6e642bf

Please sign in to comment.