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