lib/sup/modes/thread-view-mode.rb in sup-0.0.8 vs lib/sup/modes/thread-view-mode.rb in sup-0.1
- old
+ new
@@ -1,22 +1,26 @@
module Redwood
class ThreadViewMode < LineCursorMode
## this holds all info we need to lay out a message
- class Layout
+ class MessageLayout
attr_accessor :top, :bot, :prev, :next, :depth, :width, :state, :color, :star_color, :orig_new
end
+ class ChunkLayout
+ attr_accessor :state
+ end
+
DATE_FORMAT = "%B %e %Y %l:%M%P"
INDENT_SPACES = 2 # how many spaces to indent child messages
register_keymap do |k|
k.add :toggle_detailed_header, "Toggle detailed header", 'd'
k.add :show_header, "Show full message header", 'H'
k.add :toggle_expanded, "Expand/collapse item", :enter
k.add :expand_all_messages, "Expand/collapse all messages", 'E'
- k.add :edit_message, "Edit message (drafts only)", 'e'
+ k.add :edit_draft, "Edit draft", 'e'
k.add :expand_all_quotes, "Expand/collapse all quotes in a message", 'o'
k.add :jump_to_next_open, "Jump to next open message", 'n'
k.add :jump_to_prev_open, "Jump to previous open message", 'p'
k.add :toggle_starred, "Star or unstar message", '*'
k.add :collapse_non_new_messages, "Collapse all but new messages", 'N'
@@ -28,31 +32,31 @@
k.add :search, "Search for messages from particular people", 'S'
k.add :compose, "Compose message to person", 'm'
k.add :archive_and_kill, "Archive thread and kill buffer", 'A'
end
- ## there are a couple important instance variables we hold to lay
- ## out the thread and to provide line-based functionality. @layout
- ## is a map from Message and Chunk objects to Layout objects. (for
- ## chunks, we only use the state field right now.) @message_lines is
- ## a map from row #s to Message objects. @chunk_lines is a map from
- ## row #s to Chunk objects. @person_lines is a map from row #s to
- ## Person objects.
+ ## there are a couple important instance variables we hold to format
+ ## the thread and to provide line-based functionality. @layout is a
+ ## map from Messages to MessageLayouts, and @chunk_layout from
+ ## Chunks to ChunkLayouts. @message_lines is a map from row #s to
+ ## Message objects. @chunk_lines is a map from row #s to Chunk
+ ## objects. @person_lines is a map from row #s to Person objects.
def initialize thread, hidden_labels=[]
super()
@thread = thread
@hidden_labels = hidden_labels
- @layout = {}
+ @layout = SavingHash.new { MessageLayout.new }
+ @chunk_layout = SavingHash.new { ChunkLayout.new }
earliest, latest = nil, nil
latest_date = nil
altcolor = false
+
@thread.each do |m, d, p|
next unless m
earliest ||= m
- @layout[m] = Layout.new
@layout[m].state = initial_state_for m
@layout[m].color = altcolor ? :alternate_patina_color : :message_patina_color
@layout[m].star_color = altcolor ? :alternate_starred_patina_color : :starred_patina_color
@layout[m].orig_new = m.has_label? :unread
altcolor = !altcolor
@@ -144,17 +148,23 @@
end
def toggle_expanded
chunk = @chunk_lines[curpos] or return
case chunk
- when Message, Message::Quote, Message::Signature
- return if chunk.lines.length == 1 unless chunk.is_a? Message # too small to expand/close
+ when Message
l = @layout[chunk]
l.state = (l.state != :closed ? :closed : :open)
cursor_down if l.state == :closed
+ when Message::Quote, Message::Signature
+ return if chunk.lines.length == 1
+ toggle_chunk_expansion chunk
when Message::Attachment
- view_attachment chunk
+ if chunk.inlineable?
+ toggle_chunk_expansion chunk
+ else
+ view_attachment chunk
+ end
end
update
end
def edit_as_new
@@ -167,23 +177,24 @@
def save_to_disk
chunk = @chunk_lines[curpos] or return
case chunk
when Message::Attachment
fn = BufferManager.ask :filename, "Save attachment to file: ", chunk.filename
- save_to_file(fn) { |f| f.print chunk } if fn
+ save_to_file(fn) { |f| f.print chunk.raw_content } if fn
else
m = @message_lines[curpos]
fn = BufferManager.ask :filename, "Save message to file: "
save_to_file(fn) { |f| f.print m.raw_full_message } if fn
end
end
- def edit_message
+ def edit_draft
m = @message_lines[curpos] or return
if m.is_draft?
mode = ResumeMode.new m
BufferManager.spawn "Edit message", mode
+ BufferManager.kill_buffer self.buffer
mode.edit
else
BufferManager.flash "Not a draft message!"
end
end
@@ -239,41 +250,47 @@
end
def expand_all_messages
@global_message_state ||= :closed
@global_message_state = (@global_message_state == :closed ? :open : :closed)
- @layout.each { |m, l| l.state = @global_message_state if m.is_a? Message }
+ @layout.each { |m, l| l.state = @global_message_state }
update
end
def collapse_non_new_messages
- @layout.each { |m, l| l.state = l.orig_new ? :open : :closed if m.is_a? Message }
+ @layout.each { |m, l| l.state = l.orig_new ? :open : :closed }
update
end
def expand_all_quotes
if(m = @message_lines[curpos])
quotes = m.chunks.select { |c| (c.is_a?(Message::Quote) || c.is_a?(Message::Signature)) && c.lines.length > 1 }
- numopen = quotes.inject(0) { |s, c| s + (@layout[c].state == :open ? 1 : 0) }
+ numopen = quotes.inject(0) { |s, c| s + (@chunk_layout[c].state == :open ? 1 : 0) }
newstate = numopen > quotes.length / 2 ? :closed : :open
- quotes.each { |c| @layout[c].state = newstate }
+ quotes.each { |c| @chunk_layout[c].state = newstate }
update
end
end
def cleanup
- @layout = @text = nil # for good luck
+ @layout = @chunk_layout = @text = nil # for good luck
end
def archive_and_kill
@thread.remove_label :inbox
UpdateManager.relay self, :archived, @thread
BufferManager.kill_buffer_safely buffer
end
private
+ def toggle_chunk_expansion chunk
+ l = @chunk_layout[chunk]
+ l.state = (l.state != :closed ? :closed : :open)
+ cursor_down if l.state == :closed
+ end
+
def initial_state_for m
if m.has_label?(:starred) || m.has_label?(:unread)
:open
else
:closed
@@ -298,14 +315,17 @@
@thread.each do |m, depth, parent|
unless m.is_a? Message # handle nil and :fake_root
@text += chunk_to_lines m, nil, @text.length, depth, parent
next
end
- l = @layout[m] or next # TODO: figure out why this is nil sometimes
+ l = @layout[m]
+ ## is this still necessary?
+ next unless @layout[m].state # skip discarded drafts
+
## build the patina
- text = chunk_to_lines m, l.state, @text.length, depth, parent, @layout[m].color, @layout[m].star_color
+ text = chunk_to_lines m, l.state, @text.length, depth, parent, l.color, l.star_color
l.top = @text.length
l.bot = @text.length + text.length # updated below
l.prev = prevm
l.next = nil
@@ -320,14 +340,22 @@
lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum
end
@text += text
prevm = m
- if @layout[m].state != :closed
+ if l.state != :closed
m.chunks.each do |c|
- cl = (@layout[c] ||= Layout.new)
- cl.state ||= :closed
+ cl = @chunk_layout[c]
+
+ ## set the default state for chunks
+ cl.state ||=
+ if c.is_a?(Message::Attachment) && c.inlineable?
+ :open
+ else
+ :closed
+ end
+
text = chunk_to_lines c, cl.state, @text.length, depth
(0 ... text.length).each do |i|
@chunk_lines[@text.length + i] = c
@message_lines[@text.length + i] = m
lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.length }.sum - (depth * INDENT_SPACES)
@@ -420,11 +448,17 @@
[[[:missing_message_color, "#{prefix}<an unreceived message>"]]]
when Message
message_patina_lines(chunk, state, start, parent, prefix, color, star_color) +
(chunk.is_draft? ? [[[:draft_notification_color, prefix + " >>> This message is a draft. To edit, hit 'e'. <<<"]]] : [])
when Message::Attachment
- [[[:mime_color, "#{prefix}+ MIME attachment #{chunk.content_type}#{chunk.desc ? ' (' + chunk.desc + ')': ''}"]]]
+ return [[[:attachment_color, "#{prefix}x Attachment: #{chunk.filename} (#{chunk.content_type})"]]] unless chunk.inlineable?
+ case state
+ when :closed
+ [[[:attachment_color, "#{prefix}+ Attachment: #{chunk.filename} (#{chunk.lines.length} lines)"]]]
+ when :open
+ [[[:attachment_color, "#{prefix}- Attachment: #{chunk.filename} (#{chunk.lines.length} lines)"]]] + chunk.lines.map { |line| [[:none, "#{prefix}#{line}"]] }
+ end
when Message::Text
t = chunk.lines
if t.last =~ /^\s*$/ && t.length > 1
t.pop while t[-2] =~ /^\s*$/ # pop until only one file empty line
end
@@ -453,11 +487,13 @@
def view_attachment a
BufferManager.flash "viewing #{a.content_type} attachment..."
success = a.view!
BufferManager.erase_flash
BufferManager.completely_redraw_screen
- BufferManager.flash "Couldn't execute view command." unless success
+ unless success
+ BufferManager.spawn "Attachment: #{a.filename}", TextMode.new(a.to_s)
+ BufferManager.flash "Couldn't execute view command, viewing as text."
+ end
end
-
end
end