]> git.cworth.org Git - sup/blob - lib/sup/modes/thread-view-mode.rb
make enter collapse current message in thread-view-mode
[sup] / lib / sup / modes / thread-view-mode.rb
1 require 'open3'
2 module Redwood
3
4 class ThreadViewMode < LineCursorMode
5   ## this holds all info we need to lay out a message
6   class MessageLayout
7     attr_accessor :top, :bot, :prev, :next, :depth, :width, :state, :color, :star_color, :orig_new
8   end
9
10   class ChunkLayout
11     attr_accessor :state
12   end
13
14   DATE_FORMAT = "%B %e %Y %l:%M%P"
15   INDENT_SPACES = 2 # how many spaces to indent child messages
16
17   HookManager.register "detailed-headers", <<EOS
18 Add or remove headers from the detailed header display of a message.
19 Variables:
20   message: The message whose headers are to be formatted.
21   headers: A hash of header (name, value) pairs, initialized to the default
22            headers.
23 Return value:
24   None. The variable 'headers' should be modified in place.
25 EOS
26
27   register_keymap do |k|
28     k.add :toggle_detailed_header, "Toggle detailed header", 'h'
29     k.add :show_header, "Show full message header", 'H'
30     k.add :activate_chunk, "Expand/collapse or activate item", :enter
31     k.add :expand_all_messages, "Expand/collapse all messages", 'E'
32     k.add :edit_draft, "Edit draft", 'e'
33     k.add :send_draft, "Send draft", 'y'
34     k.add :edit_labels, "Edit or add labels for a thread", 'l'
35     k.add :expand_all_quotes, "Expand/collapse all quotes in a message", 'o'
36     k.add :jump_to_next_open, "Jump to next open message", 'n'
37     k.add :jump_to_prev_open, "Jump to previous open message", 'p'
38     k.add :align_current_message, "Align current message in buffer", 'z'
39     k.add :toggle_starred, "Star or unstar message", '*'
40     k.add :toggle_new, "Toggle unread/read status of message", 'N'
41 #    k.add :collapse_non_new_messages, "Collapse all but unread messages", 'N'
42     k.add :reply, "Reply to a message", 'r'
43     k.add :forward, "Forward a message or attachment", 'f'
44     k.add :alias, "Edit alias/nickname for a person", 'i'
45     k.add :edit_as_new, "Edit message as new", 'D'
46     k.add :save_to_disk, "Save message/attachment to disk", 's'
47     k.add :search, "Search for messages from particular people", 'S'
48     k.add :compose, "Compose message to person", 'm'
49     k.add :subscribe_to_list, "Subscribe to/unsubscribe from mailing list", "("
50     k.add :unsubscribe_from_list, "Subscribe to/unsubscribe from mailing list", ")"
51     k.add :pipe_message, "Pipe message or attachment to a shell command", '|'
52
53     k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as u(N)read:", '.' do |kk|
54       kk.add :archive_and_kill, "Archive this thread and kill buffer", 'a'
55       kk.add :delete_and_kill, "Delete this thread and kill buffer", 'd'
56       kk.add :spam_and_kill, "Mark this thread as spam and kill buffer", 's'
57       kk.add :unread_and_kill, "Mark this thread as unread and kill buffer", 'N'
58     end
59
60     k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as u(N)read/do (n)othing:", ',' do |kk|
61       kk.add :archive_and_next, "Archive this thread, kill buffer, and view next", 'a'
62       kk.add :delete_and_next, "Delete this thread, kill buffer, and view next", 'd'
63       kk.add :spam_and_next, "Mark this thread as spam, kill buffer, and view next", 's'
64       kk.add :unread_and_next, "Mark this thread as unread, kill buffer, and view next", 'N'
65       kk.add :do_nothing_and_next, "Kill buffer, and view next", 'n'
66     end
67
68     k.add_multi "(a)rchive/(d)elete/mark as (s)pam/mark as u(N)read/do (n)othing:", ']' do |kk|
69       kk.add :archive_and_prev, "Archive this thread, kill buffer, and view previous", 'a'
70       kk.add :delete_and_prev, "Delete this thread, kill buffer, and view previous", 'd'
71       kk.add :spam_and_prev, "Mark this thread as spam, kill buffer, and view previous", 's'
72       kk.add :unread_and_prev, "Mark this thread as unread, kill buffer, and view previous", 'N'
73       kk.add :do_nothing_and_prev, "Kill buffer, and view previous", 'n'
74     end
75   end
76
77   ## there are a couple important instance variables we hold to format
78   ## the thread and to provide line-based functionality. @layout is a
79   ## map from Messages to MessageLayouts, and @chunk_layout from
80   ## Chunks to ChunkLayouts.  @message_lines is a map from row #s to
81   ## Message objects.  @chunk_lines is a map from row #s to Chunk
82   ## objects. @person_lines is a map from row #s to Person objects.
83
84   def initialize thread, hidden_labels=[], index_mode=nil
85     super()
86     @thread = thread
87     @hidden_labels = hidden_labels
88
89     ## used for dispatch-and-next
90     @index_mode = index_mode
91     @dying = false
92
93     @layout = SavingHash.new { MessageLayout.new }
94     @chunk_layout = SavingHash.new { ChunkLayout.new }
95     earliest, latest = nil, nil
96     latest_date = nil
97     altcolor = false
98
99     @thread.each do |m, d, p|
100       next unless m
101       earliest ||= m
102       @layout[m].state = initial_state_for m
103       @layout[m].color = altcolor ? :alternate_patina_color : :message_patina_color
104       @layout[m].star_color = altcolor ? :alternate_starred_patina_color : :starred_patina_color
105       @layout[m].orig_new = m.has_label? :read
106       altcolor = !altcolor
107       if latest_date.nil? || m.date > latest_date
108         latest_date = m.date
109         latest = m
110       end
111     end
112
113     @layout[latest].state = :open if @layout[latest].state == :closed
114     @layout[earliest].state = :detailed if earliest.has_label?(:unread) || @thread.size == 1
115
116     @thread.remove_label :unread
117     regen_text
118   end
119
120   def draw_line ln, opts={}
121     if ln == curpos
122       super ln, :highlight => true
123     else
124       super
125     end
126   end
127   def lines; @text.length; end
128   def [] i; @text[i]; end
129
130   def show_header
131     m = @message_lines[curpos] or return
132     BufferManager.spawn_unless_exists("Full header for #{m.id}") do
133       TextMode.new m.raw_header
134     end
135   end
136
137   def toggle_detailed_header
138     m = @message_lines[curpos] or return
139     @layout[m].state = (@layout[m].state == :detailed ? :open : :detailed)
140     update
141   end
142
143   def reply
144     m = @message_lines[curpos] or return
145     mode = ReplyMode.new m
146     BufferManager.spawn "Reply to #{m.subj}", mode
147   end
148
149   def subscribe_to_list
150     m = @message_lines[curpos] or return
151     if m.list_subscribe && m.list_subscribe =~ /<mailto:(.*?)\?(subject=(.*?))>/
152       ComposeMode.spawn_nicely :from => AccountManager.account_for(m.recipient_email), :to => [Person.from_address($1)], :subj => $3
153     else
154       BufferManager.flash "Can't find List-Subscribe header for this message."
155     end
156   end
157
158   def unsubscribe_from_list
159     m = @message_lines[curpos] or return
160     if m.list_unsubscribe && m.list_unsubscribe =~ /<mailto:(.*?)\?(subject=(.*?))>/
161       ComposeMode.spawn_nicely :from => AccountManager.account_for(m.recipient_email), :to => [Person.from_address($1)], :subj => $3
162     else
163       BufferManager.flash "Can't find List-Unsubscribe header for this message."
164     end
165   end
166
167   def forward
168     if(chunk = @chunk_lines[curpos]) && chunk.is_a?(Chunk::Attachment)
169       ForwardMode.spawn_nicely :attachments => [chunk]
170     elsif(m = @message_lines[curpos])
171       ForwardMode.spawn_nicely :message => m
172     end
173   end
174
175   include CanAliasContacts
176   def alias
177     p = @person_lines[curpos] or return
178     alias_contact p
179     update
180   end
181
182   def search
183     p = @person_lines[curpos] or return
184     mode = PersonSearchResultsMode.new [p]
185     BufferManager.spawn "Search for #{p.name}", mode
186     mode.load_threads :num => mode.buffer.content_height
187   end    
188
189   def compose
190     p = @person_lines[curpos]
191     if p
192       ComposeMode.spawn_nicely :to_default => p
193     else
194       ComposeMode.spawn_nicely
195     end
196   end    
197
198   def edit_labels
199     reserved_labels = @thread.labels.select { |l| LabelManager::RESERVED_LABELS.include? l }
200     new_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", @thread.labels
201
202     return unless new_labels
203     @thread.labels = (reserved_labels + new_labels).uniq
204     new_labels.each { |l| LabelManager << l }
205     update
206     UpdateManager.relay self, :labeled, @thread.first
207   end
208
209   def toggle_starred
210     m = @message_lines[curpos] or return
211     toggle_label m, :starred
212   end
213
214   def toggle_new
215     m = @message_lines[curpos] or return
216     toggle_label m, :unread
217   end
218
219   def toggle_label m, label
220     if m.has_label? label
221       m.remove_label label
222     else
223       m.add_label label
224     end
225     ## TODO: don't recalculate EVERYTHING just to add a stupid little
226     ## star to the display
227     update
228     UpdateManager.relay self, :single_message_labeled, m
229   end
230
231   ## called when someone presses enter when the cursor is highlighting
232   ## a chunk. for expandable chunks (including messages) we toggle
233   ## open/closed state; for viewable chunks (like attachments) we
234   ## view.
235   def activate_chunk
236     chunk = @chunk_lines[curpos] or return
237     if chunk.is_a? Chunk::Text
238       ## if the cursor is over a text region, expand/collapse the
239       ## entire message
240       chunk = @message_lines[curpos]
241     end
242     layout = if chunk.is_a?(Message)
243       @layout[chunk]
244     elsif chunk.expandable?
245       @chunk_layout[chunk]
246     end
247     if layout
248       layout.state = (layout.state != :closed ? :closed : :open)
249       #cursor_down if layout.state == :closed # too annoying
250       update
251     elsif chunk.viewable?
252       view chunk
253     end
254     if chunk.is_a?(Message)
255       jump_to_message chunk
256       jump_to_next_open
257     end
258   end
259
260   def edit_as_new
261     m = @message_lines[curpos] or return
262     mode = ComposeMode.new(:body => m.quotable_body_lines, :to => m.to, :cc => m.cc, :subj => m.subj, :bcc => m.bcc, :refs => m.refs, :replytos => m.replytos)
263     BufferManager.spawn "edit as new", mode
264     mode.edit_message
265   end
266
267   def save_to_disk
268     chunk = @chunk_lines[curpos] or return
269     case chunk
270     when Chunk::Attachment
271       default_dir = File.join(($config[:default_attachment_save_dir] || "."), chunk.filename)
272       fn = BufferManager.ask_for_filename :filename, "Save attachment to file: ", default_dir
273       save_to_file(fn) { |f| f.print chunk.raw_content } if fn
274     else
275       m = @message_lines[curpos]
276       fn = BufferManager.ask_for_filename :filename, "Save message to file: "
277       return unless fn
278       save_to_file(fn) do |f|
279         m.each_raw_message_line { |l| f.print l }
280       end
281     end
282   end
283
284   def edit_draft
285     m = @message_lines[curpos] or return
286     if m.is_draft?
287       mode = ResumeMode.new m
288       BufferManager.spawn "Edit message", mode
289       BufferManager.kill_buffer self.buffer
290       mode.edit_message
291     else
292       BufferManager.flash "Not a draft message!"
293     end
294   end
295
296   def send_draft
297     m = @message_lines[curpos] or return
298     if m.is_draft?
299       mode = ResumeMode.new m
300       BufferManager.spawn "Send message", mode
301       BufferManager.kill_buffer self.buffer
302       mode.send_message
303     else
304       BufferManager.flash "Not a draft message!"
305     end
306   end
307
308   def jump_to_first_open loose_alignment=false
309     m = @message_lines[0] or return
310     if @layout[m].state != :closed
311       jump_to_message m, loose_alignment
312     else
313       jump_to_next_open loose_alignment
314     end
315   end
316
317   def jump_to_next_open loose_alignment=false
318     return continue_search_in_buffer if in_search? # hack: allow 'n' to apply to both operations
319     m = (curpos ... @message_lines.length).argfind { |i| @message_lines[i] }
320     return unless m
321     while nextm = @layout[m].next
322       break if @layout[nextm].state != :closed
323       m = nextm
324     end
325     jump_to_message nextm, loose_alignment if nextm
326   end
327
328   def align_current_message
329     m = @message_lines[curpos] or return
330     jump_to_message m
331   end
332
333   def jump_to_prev_open loose_alignment=false
334     m = (0 .. curpos).to_a.reverse.argfind { |i| @message_lines[i] } # bah, .to_a
335     return unless m
336     ## jump to the top of the current message if we're in the body;
337     ## otherwise, to the previous message
338     
339     top = @layout[m].top
340     if curpos == top
341       while(prevm = @layout[m].prev)
342         break if @layout[prevm].state != :closed
343         m = prevm
344       end
345       jump_to_message prevm, loose_alignment if prevm
346     else
347       jump_to_message m, loose_alignment
348     end
349   end
350
351   IDEAL_TOP_CONTEXT = 3 # try and give 3 rows of top context
352   IDEAL_LEFT_CONTEXT = 4 # try and give 4 columns of left context
353   def jump_to_message m, loose_alignment=false
354     l = @layout[m]
355     left = l.depth * INDENT_SPACES
356     right = left + l.width
357
358     ## jump to the top line
359     if loose_alignment
360       jump_to_line [l.top - IDEAL_TOP_CONTEXT, 0].max # give 3 lines of top context
361     else
362       jump_to_line l.top
363     end
364
365     ## jump to the left column
366     ideal_left = left +
367       if loose_alignment
368         -IDEAL_LEFT_CONTEXT + (l.width - buffer.content_width + IDEAL_LEFT_CONTEXT + 1).clamp(0, IDEAL_LEFT_CONTEXT)
369       else
370         0
371       end
372
373     jump_to_col [ideal_left, 0].max
374
375     ## either way, move the cursor to the first line
376     set_cursor_pos l.top
377   end
378
379   def expand_all_messages
380     @global_message_state ||= :closed
381     @global_message_state = (@global_message_state == :closed ? :open : :closed)
382     @layout.each { |m, l| l.state = @global_message_state }
383     update
384   end
385
386   def collapse_non_new_messages
387     @layout.each { |m, l| l.state = l.orig_new ? :open : :closed }
388     update
389   end
390
391   def expand_all_quotes
392     if(m = @message_lines[curpos])
393       quotes = m.chunks.select { |c| (c.is_a?(Chunk::Quote) || c.is_a?(Chunk::Signature)) && c.lines.length > 1 }
394       numopen = quotes.inject(0) { |s, c| s + (@chunk_layout[c].state == :open ? 1 : 0) }
395       newstate = numopen > quotes.length / 2 ? :closed : :open
396       quotes.each { |c| @chunk_layout[c].state = newstate }
397       update
398     end
399   end
400
401   def cleanup
402     @layout = @chunk_layout = @text = nil # for good luck
403   end
404
405   def archive_and_kill; archive_and_then :kill end
406   def spam_and_kill; spam_and_then :kill end
407   def delete_and_kill; delete_and_then :kill end
408   def unread_and_kill; unread_and_then :kill end
409
410   def archive_and_next; archive_and_then :next end
411   def spam_and_next; spam_and_then :next end
412   def delete_and_next; delete_and_then :next end
413   def unread_and_next; unread_and_then :next end
414   def do_nothing_and_next; do_nothing_and_then :next end
415
416   def archive_and_prev; archive_and_then :prev end
417   def spam_and_prev; spam_and_then :prev end
418   def delete_and_prev; delete_and_then :prev end
419   def unread_and_prev; unread_and_then :prev end
420   def do_nothing_and_prev; do_nothing_and_then :prev end
421
422   def archive_and_then op
423     dispatch op do
424       @thread.remove_label :inbox
425       UpdateManager.relay self, :archived, @thread.first
426     end
427   end
428
429   def spam_and_then op
430     dispatch op do
431       @thread.apply_label :spam
432       UpdateManager.relay self, :spammed, @thread.first
433     end
434   end
435
436   def delete_and_then op
437     dispatch op do
438       @thread.apply_label :deleted
439       UpdateManager.relay self, :deleted, @thread.first
440     end
441   end
442
443   def unread_and_then op
444     dispatch op do
445       @thread.apply_label :unread
446       UpdateManager.relay self, :unread, @thread.first
447     end
448   end
449
450   def do_nothing_and_then op
451     dispatch op
452   end
453
454   def dispatch op
455     return if @dying
456     @dying = true
457
458     l = lambda do
459       yield if block_given?
460       BufferManager.kill_buffer_safely buffer
461     end
462
463     case op
464     when :next
465       @index_mode.launch_next_thread_after @thread, &l
466     when :prev
467       @index_mode.launch_prev_thread_before @thread, &l
468     when :kill
469       l.call
470     else
471       raise ArgumentError, "unknown thread dispatch operation #{op.inspect}"
472     end
473   end
474   private :dispatch
475
476   def pipe_message
477     chunk = @chunk_lines[curpos]
478     chunk = nil unless chunk.is_a?(Chunk::Attachment)
479     message = @message_lines[curpos] unless chunk
480
481     return unless chunk || message
482
483     command = BufferManager.ask(:shell, "pipe command: ")
484     return if command.nil? || command.empty?
485
486     output = pipe_to_process(command) do |stream|
487       if chunk
488         stream.print chunk.raw_content
489       else
490         message.each_raw_message_line { |l| stream.print l }
491       end
492     end
493
494     if output
495       BufferManager.spawn "Output of '#{command}'", TextMode.new(output)
496     else
497       BufferManager.flash "'#{command}' done!"
498     end
499   end
500
501 private
502
503   def initial_state_for m
504     if m.has_label?(:starred) || m.has_label?(:unread)
505       :open
506     else
507       :closed
508     end
509   end
510
511   def update
512     regen_text
513     buffer.mark_dirty if buffer
514   end
515
516   ## here we generate the actual content lines. we accumulate
517   ## everything into @text, and we set @chunk_lines and
518   ## @message_lines, and we update @layout.
519   def regen_text
520     @text = []
521     @chunk_lines = []
522     @message_lines = []
523     @person_lines = []
524
525     prevm = nil
526     @thread.each do |m, depth, parent|
527       unless m.is_a? Message # handle nil and :fake_root
528         @text += chunk_to_lines m, nil, @text.length, depth, parent
529         next
530       end
531       l = @layout[m]
532
533       ## is this still necessary?
534       next unless @layout[m].state # skip discarded drafts
535
536       ## build the patina
537       text = chunk_to_lines m, l.state, @text.length, depth, parent, l.color, l.star_color
538       
539       l.top = @text.length
540       l.bot = @text.length + text.length # updated below
541       l.prev = prevm
542       l.next = nil
543       l.depth = depth
544       # l.state we preserve
545       l.width = 0 # updated below
546       @layout[l.prev].next = m if l.prev
547
548       (0 ... text.length).each do |i|
549         @chunk_lines[@text.length + i] = m
550         @message_lines[@text.length + i] = m
551         lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.display_length }.sum
552       end
553
554       @text += text
555       prevm = m 
556       if l.state != :closed
557         m.chunks.each do |c|
558           cl = @chunk_layout[c]
559
560           ## set the default state for chunks
561           cl.state ||=
562             if c.expandable? && c.respond_to?(:initial_state)
563               c.initial_state
564             else
565               :closed
566             end
567
568           text = chunk_to_lines c, cl.state, @text.length, depth
569           (0 ... text.length).each do |i|
570             @chunk_lines[@text.length + i] = c
571             @message_lines[@text.length + i] = m
572             lw = text[i].flatten.select { |x| x.is_a? String }.map { |x| x.display_length }.sum - (depth * INDENT_SPACES)
573             l.width = lw if lw > l.width
574           end
575           @text += text
576         end
577         @layout[m].bot = @text.length
578       end
579     end
580   end
581
582   def message_patina_lines m, state, start, parent, prefix, color, star_color
583     prefix_widget = [color, prefix]
584
585     open_widget = [color, (state == :closed ? "+ " : "- ")]
586     new_widget = [color, (m.has_label?(:unread) ? "N" : " ")]
587     starred_widget = if m.has_label?(:starred)
588         [star_color, "*"]
589       else
590         [color, " "]
591       end
592     attach_widget = [color, (m.has_label?(:attachment) ? "@" : " ")]
593
594     case state
595     when :open
596       @person_lines[start] = m.from
597       [[prefix_widget, open_widget, new_widget, attach_widget, starred_widget,
598         [color, 
599             "#{m.from ? m.from.mediumname : '?'} to #{m.recipients.map { |l| l.shortname }.join(', ')} #{m.date.to_nice_s} (#{m.date.to_nice_distance_s})"]]]
600
601     when :closed
602       @person_lines[start] = m.from
603       [[prefix_widget, open_widget, new_widget, attach_widget, starred_widget,
604         [color, 
605         "#{m.from ? m.from.mediumname : '?'}, #{m.date.to_nice_s} (#{m.date.to_nice_distance_s})  #{m.snippet}"]]]
606
607     when :detailed
608       @person_lines[start] = m.from
609       from_line = [[prefix_widget, open_widget, new_widget, attach_widget, starred_widget,
610           [color, "From: #{m.from ? format_person(m.from) : '?'}"]]]
611
612       addressee_lines = []
613       unless m.to.empty?
614         m.to.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
615         addressee_lines += format_person_list "   To: ", m.to
616       end
617       unless m.cc.empty?
618         m.cc.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
619         addressee_lines += format_person_list "   Cc: ", m.cc
620       end
621       unless m.bcc.empty?
622         m.bcc.each_with_index { |p, i| @person_lines[start + addressee_lines.length + from_line.length + i] = p }
623         addressee_lines += format_person_list "   Bcc: ", m.bcc
624       end
625
626       headers = OrderedHash.new
627       headers["Date"] = "#{m.date.strftime DATE_FORMAT} (#{m.date.to_nice_distance_s})"
628       headers["Subject"] = m.subj
629
630       show_labels = @thread.labels - LabelManager::HIDDEN_RESERVED_LABELS
631       unless show_labels.empty?
632         headers["Labels"] = show_labels.map { |x| x.to_s }.sort.join(', ')
633       end
634       if parent
635         headers["In reply to"] = "#{parent.from.mediumname}'s message of #{parent.date.strftime DATE_FORMAT}"
636       end
637
638       HookManager.run "detailed-headers", :message => m, :headers => headers
639       
640       from_line + (addressee_lines + headers.map { |k, v| "   #{k}: #{v}" }).map { |l| [[color, prefix + "  " + l]] }
641     end
642   end
643
644   def format_person_list prefix, people
645     ptext = people.map { |p| format_person p }
646     pad = " " * prefix.display_length
647     [prefix + ptext.first + (ptext.length > 1 ? "," : "")] + 
648       ptext[1 .. -1].map_with_index do |e, i|
649         pad + e + (i == ptext.length - 1 ? "" : ",")
650       end
651   end
652
653   def format_person p
654     p.longname + (ContactManager.is_aliased_contact?(p) ? " (#{ContactManager.alias_for p})" : "")
655   end
656
657   ## todo: check arguments on this overly complex function
658   def chunk_to_lines chunk, state, start, depth, parent=nil, color=nil, star_color=nil
659     prefix = " " * INDENT_SPACES * depth
660     case chunk
661     when :fake_root
662       [[[:missing_message_color, "#{prefix}<one or more unreceived messages>"]]]
663     when nil
664       [[[:missing_message_color, "#{prefix}<an unreceived message>"]]]
665     when Message
666       message_patina_lines(chunk, state, start, parent, prefix, color, star_color) +
667         (chunk.is_draft? ? [[[:draft_notification_color, prefix + " >>> This message is a draft. Hit 'e' to edit, 'y' to send. <<<"]]] : [])
668
669     else
670       raise "Bad chunk: #{chunk.inspect}" unless chunk.respond_to?(:inlineable?) ## debugging
671       if chunk.inlineable?
672         chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
673       elsif chunk.expandable?
674         case state
675         when :closed
676           [[[chunk.patina_color, "#{prefix}+ #{chunk.patina_text}"]]]
677         when :open
678           [[[chunk.patina_color, "#{prefix}- #{chunk.patina_text}"]]] + chunk.lines.map { |line| [[chunk.color, "#{prefix}#{line}"]] }
679         end
680       else
681         [[[chunk.patina_color, "#{prefix}x #{chunk.patina_text}"]]]
682       end
683     end
684   end
685
686   def view chunk
687     BufferManager.flash "viewing #{chunk.content_type} attachment..."
688     success = chunk.view!
689     BufferManager.erase_flash
690     BufferManager.completely_redraw_screen
691     unless success
692       BufferManager.spawn "Attachment: #{chunk.filename}", TextMode.new(chunk.to_s, chunk.filename)
693       BufferManager.flash "Couldn't execute view command, viewing as text."
694     end
695   end
696 end
697
698 end