]> git.cworth.org Git - sup/blob - lib/sup/modes/thread-index-mode.rb
Added undo for label edit
[sup] / lib / sup / modes / thread-index-mode.rb
1 module Redwood
2
3 ## subclasses should implement:
4 ## - is_relevant?
5
6 class ThreadIndexMode < LineCursorMode
7   DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
8   MIN_FROM_WIDTH = 15
9   LOAD_MORE_THREAD_NUM = 20
10
11   HookManager.register "index-mode-size-widget", <<EOS
12 Generates the per-thread size widget for each thread.
13 Variables:
14   thread: The message thread to be formatted.
15 EOS
16
17   HookManager.register "mark-as-spam", <<EOS
18 This hook is run when a thread is marked as spam
19 Variables:
20   thread: The message thread being marked as spam.
21 EOS
22
23   register_keymap do |k|
24     k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
25     k.add_multi "Load all threads (! to confirm) :", '!' do |kk|
26       kk.add :load_all_threads, "Load all threads (may list a _lot_ of threads)", '!'
27     end
28     k.add :cancel_search, "Cancel current search", :ctrl_g
29     k.add :reload, "Refresh view", '@'
30     k.add :toggle_archived, "Toggle archived status", 'a'
31     k.add :toggle_starred, "Star or unstar all messages in thread", '*'
32     k.add :toggle_new, "Toggle new/read status of all messages in thread", 'N'
33     k.add :edit_labels, "Edit or add labels for a thread", 'l'
34     k.add :edit_message, "Edit message (drafts only)", 'e'
35     k.add :toggle_spam, "Mark/unmark thread as spam", 'S'
36     k.add :toggle_deleted, "Delete/undelete thread", 'd'
37     k.add :kill, "Kill thread (never to be seen in inbox again)", '&'
38     k.add :save, "Save changes now", '$'
39     k.add :jump_to_next_new, "Jump to next new thread", :tab
40     k.add :reply, "Reply to latest message in a thread", 'r'
41     k.add :forward, "Forward latest message in a thread", 'f'
42     k.add :toggle_tagged, "Tag/untag selected thread", 't'
43     k.add :toggle_tagged_all, "Tag/untag all threads", 'T'
44     k.add :tag_matching, "Tag matching threads", 'g'
45     k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
46     k.add :join_threads, "Force tagged threads to be joined into the same thread", '#'
47     k.add :undo, "Undo the previous action", 'u'
48   end
49
50   def initialize hidden_labels=[], load_thread_opts={}
51     super()
52     @mutex = Mutex.new # covers the following variables:
53     @threads = {}
54     @hidden_threads = {}
55     @size_widget_width = nil
56     @size_widgets = {}
57     @tags = Tagger.new self
58
59     ## these guys, and @text and @lines, are not covered
60     @load_thread = nil
61     @load_thread_opts = load_thread_opts
62     @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
63     @date_width = DATE_WIDTH
64
65     @interrupt_search = false
66     
67     initialize_threads # defines @ts and @ts_mutex
68     update # defines @text and @lines
69
70     UpdateManager.register self
71
72     @last_load_more_size = nil
73     to_load_more do |size|
74       next if @last_load_more_size == 0
75       load_threads :num => 1, :background => false
76       load_threads :num => (size - 1),
77                    :when_done => lambda { |num| @last_load_more_size = num }
78     end
79   end
80
81   def lines; @text.length; end
82   def [] i; @text[i]; end
83   def contains_thread? t; @threads.include?(t) end
84
85   def reload
86     drop_all_threads
87     UndoManager.clear
88     BufferManager.draw_screen
89     load_threads :num => buffer.content_height
90   end
91
92   ## open up a thread view window
93   def select t=nil, when_done=nil
94     t ||= cursor_thread or return
95
96     Redwood::reporting_thread("load messages for thread-view-mode") do
97       num = t.size
98       message = "Loading #{num.pluralize 'message body'}..."
99       BufferManager.say(message) do |sid|
100         t.each_with_index do |(m, *o), i|
101           next unless m
102           BufferManager.say "#{message} (#{i}/#{num})", sid if t.size > 1
103           m.load_from_source! 
104         end
105       end
106       mode = ThreadViewMode.new t, @hidden_labels, self
107       BufferManager.spawn t.subj, mode
108       BufferManager.draw_screen
109       mode.jump_to_first_open true
110       BufferManager.draw_screen # lame TODO: make this unnecessary
111       ## the first draw_screen is needed before topline and botline
112       ## are set, and the second to show the cursor having moved
113
114       update_text_for_line curpos
115       UpdateManager.relay self, :read, t.first
116       when_done.call if when_done
117     end
118   end
119
120   def multi_select threads
121     threads.each { |t| select t }
122   end
123
124   ## these two methods are called by thread-view-modes when the user
125   ## wants to view the previous/next thread without going back to
126   ## index-mode. we update the cursor as a convenience.
127   def launch_next_thread_after thread, &b
128     launch_another_thread thread, 1, &b
129   end
130
131   def launch_prev_thread_before thread, &b
132     launch_another_thread thread, -1, &b
133   end
134
135   def launch_another_thread thread, direction, &b
136     l = @lines[thread] or return
137     target_l = l + direction
138     t = @mutex.synchronize do
139       if target_l >= 0 && target_l < @threads.length
140         @threads[target_l]
141       end
142     end
143
144     if t # there's a next thread
145       set_cursor_pos target_l # move out of mutex?
146       select t, b
147     elsif b # no next thread. call the block anyways
148       b.call
149     end
150   end
151   
152   def handle_single_message_labeled_update sender, m
153     ## no need to do anything different here; we don't differentiate 
154     ## messages from their containing threads
155     handle_labeled_update sender, m
156   end
157
158   def handle_labeled_update sender, m
159     if(t = thread_containing(m)) 
160       l = @lines[t] or return
161       update_text_for_line l
162     elsif is_relevant?(m)
163       add_or_unhide m
164     end
165   end
166
167   def handle_simple_update sender, m
168     t = thread_containing(m) or return
169     l = @lines[t] or return
170     update_text_for_line l
171   end
172
173   %w(read unread archived starred unstarred).each do |state|
174     define_method "handle_#{state}_update" do |*a|
175       handle_simple_update(*a)
176     end
177   end
178
179   ## overwrite me!
180   def is_relevant? m; false; end
181
182   def handle_added_update sender, m
183     add_or_unhide m
184     BufferManager.draw_screen
185   end
186
187   def handle_single_message_deleted_update sender, m
188     @ts_mutex.synchronize do
189       return unless @ts.contains? m
190       @ts.remove_id m.id
191     end
192     update
193   end
194
195   def handle_deleted_update sender, m
196     t = @ts_mutex.synchronize { @ts.thread_for m }
197     return unless t
198     hide_thread t
199     update
200   end
201
202   def handle_spammed_update sender, m
203     t = @ts_mutex.synchronize { @ts.thread_for m }
204     return unless t
205     hide_thread t
206     update
207   end
208
209   def handle_undeleted_update sender, m
210     add_or_unhide m
211   end
212
213   def undo
214     UndoManager.undo
215   end
216
217   def update
218     @mutex.synchronize do
219       ## let's see you do THIS in python
220       @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| [t.date, t.first.id] }.reverse
221       @size_widgets = @threads.map { |t| size_widget_for_thread t }
222       @size_widget_width = @size_widgets.max_of { |w| w.length }
223     end
224
225     regen_text
226   end
227
228   def edit_message
229     return unless(t = cursor_thread)
230     message, *crap = t.find { |m, *o| m.has_label? :draft }
231     if message
232       mode = ResumeMode.new message
233       BufferManager.spawn "Edit message", mode
234     else
235       BufferManager.flash "Not a draft message!"
236     end
237   end
238
239   def actually_toggle_starred t
240     thread = t # cargo cult programming
241     pos = curpos
242     if t.has_label? :starred # if ANY message has a star
243       undo = lambda {
244         thread.first.add_label :starred
245         update_text_for_line pos
246         UpdateManager.relay self, :starred, thread.first
247       }
248       t.remove_label :starred # remove from all
249       UpdateManager.relay self, :unstarred, t.first
250     else
251       undo = lambda {
252         thread.remove_label :starred
253         update_text_for_line pos
254         UpdateManager.relay self, :unstarred, thread.first
255       }
256       t.first.add_label :starred # add only to first
257       UpdateManager.relay self, :starred, t.first
258     end
259
260     return undo
261   end  
262
263   def toggle_starred 
264     t = cursor_thread or return
265     undo = actually_toggle_starred t
266     UndoManager.register("starring/unstarring thread #{t.first.id}",undo)
267     update_text_for_line curpos
268     cursor_down
269   end
270
271   def multi_toggle_starred threads
272     undo = threads.map { |t| actually_toggle_starred t }
273     UndoManager.register("starring/unstarring #{threads.size} #{threads.size.pluralize 'thread'}",
274                          undo)
275     regen_text
276   end
277
278   def actually_toggle_archived t
279     thread = t
280     pos = curpos
281     if t.has_label? :inbox
282       t.remove_label :inbox
283       undo = lambda {
284         thread.apply_label :inbox
285         update_text_for_line pos
286         UpdateManager.relay self,:unarchived, thread.first
287       }
288       UpdateManager.relay self, :archived, t.first
289     else
290       t.apply_label :inbox
291       undo = lambda {
292         thread.remove_label :inbox
293         update_text_for_line pos
294         UpdateManager.relay self, :unarchived, thread.first
295       }
296       UpdateManager.relay self, :unarchived, t.first
297     end
298
299     return undo
300   end
301
302   def actually_toggle_spammed t
303     thread = t
304     if t.has_label? :spam
305       undo = lambda {
306         thread.apply_label :spam
307         self.hide_thread thread
308         UpdateManager.relay self,:spammed, thread.first
309       }
310       t.remove_label :spam
311       add_or_unhide t.first
312       UpdateManager.relay self, :unspammed, t.first
313     else
314       undo = lambda {
315         thread.remove_label :spam
316         add_or_unhide thread.first
317         UpdateManager.relay self,:unspammed, thread.first
318       }
319       t.apply_label :spam
320       hide_thread t
321       UpdateManager.relay self, :spammed, t.first
322     end
323
324     return undo
325   end
326
327   def actually_toggle_deleted t
328     if t.has_label? :deleted
329       undo = lambda {
330         t.apply_label :deleted
331         hide_thread t
332         UpdateManager.relay self, :deleted, t.first
333       }
334       t.remove_label :deleted
335       add_or_unhide t.first
336       UpdateManager.relay self, :undeleted, t.first
337     else
338       undo = lambda {
339         t.remove_label :deleted
340         add_or_unhide t.first
341         UpdateManager.relay self, :undeleted, t.first
342       }
343       t.apply_label :deleted
344   hide_thread t
345       UpdateManager.relay self, :deleted, t.first
346     end
347
348     return undo
349   end
350
351   def toggle_archived 
352     t = cursor_thread or return
353     undo = [actually_toggle_archived(t), lambda {self.update_text_for_line curpos}]
354     UndoManager.register("deleting/undeleting thread #{t.first.id}",undo)
355     update_text_for_line curpos
356   end
357
358   def multi_toggle_archived threads
359     undo = threads.map { |t| actually_toggle_archived t}
360     UndoManager.register("deleting/undeleting #{threads.size} #{threads.size.pluralize 'thread'}",
361                          undo << lambda {self.regen_text})
362     regen_text
363   end
364
365   def toggle_new
366     t = cursor_thread or return
367     t.toggle_label :unread
368     update_text_for_line curpos
369     cursor_down
370   end
371
372   def multi_toggle_new threads
373     threads.each { |t| t.toggle_label :unread }
374     regen_text
375   end
376
377   def multi_toggle_tagged threads
378     @mutex.synchronize { @tags.drop_all_tags }
379     regen_text
380   end
381
382   def join_threads
383     ## this command has no non-tagged form. as a convenience, allow this
384     ## command to be applied to tagged threads without hitting ';'.
385     @tags.apply_to_tagged :join_threads
386   end
387
388   def multi_join_threads threads
389     @ts.join_threads threads or return
390     @tags.drop_all_tags # otherwise we have tag pointers to invalid threads!
391     update
392   end
393
394   def jump_to_next_new
395     n = @mutex.synchronize do
396       ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } ||
397         (0 ... curpos).find { |i| @threads[i].has_label? :unread }
398     end
399     if n
400       ## jump there if necessary
401       jump_to_line n unless n >= topline && n < botline
402       set_cursor_pos n
403     else
404       BufferManager.flash "No new messages"
405     end
406   end
407
408   def toggle_spam
409     t = cursor_thread or return
410     multi_toggle_spam [t]
411     HookManager.run("mark-as-spam", :thread => t)
412   end
413
414   ## both spam and deleted have the curious characteristic that you
415   ## always want to hide the thread after either applying or removing
416   ## that label. in all thread-index-views except for
417   ## label-search-results-mode, when you mark a message as spam or
418   ## deleted, you want it to disappear immediately; in LSRM, you only
419   ## see deleted or spam emails, and when you undelete or unspam them
420   ## you also want them to disappear immediately.
421   def multi_toggle_spam threads
422     undo = threads.map{ |t| actually_toggle_spammed t}
423     UndoManager.register("marking/unmarking #{threads.size} #{threads.size.pluralize 'thread'} as spam",
424                          undo <<  lambda {self.regen_text})
425     regen_text
426   end
427
428   def toggle_deleted
429     t = cursor_thread or return
430     multi_toggle_deleted [t]
431   end
432
433   ## see comment for multi_toggle_spam
434   def multi_toggle_deleted threads
435     undo = threads.map{ |t| actually_toggle_deleted t}
436     UndoManager.register("deleting/undeleting #{threads.size} #{threads.size.pluralize 'thread'}",
437                          undo << lambda {regen_text})
438     regen_text
439   end
440
441   def kill
442     t = cursor_thread or return
443     multi_kill [t]
444   end
445
446   ## m-m-m-m-MULTI-KILL
447   def multi_kill threads
448     undo = threads.map do |t|
449       t.apply_label :killed
450       hide_thread t
451       thread = t
452       lambda { thread.remove_label :killed
453         add_or_unhide thread.first
454       }
455     end
456     UndoManager.register("killing #{threads.size} #{threads.size.pluralize 'thread'}",
457                          undo << lambda {regen_text})
458     regen_text
459     BufferManager.flash "#{threads.size.pluralize 'Thread'} killed."
460   end
461
462   def save
463     dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
464     return if dirty_threads.empty?
465
466     BufferManager.say("Saving threads...") do |say_id|
467       dirty_threads.each_with_index do |t, i|
468         BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
469         t.save Index
470       end
471     end
472   end
473
474   def cleanup
475     UpdateManager.unregister self
476
477     if @load_thread
478       @load_thread.kill 
479       BufferManager.clear @mbid if @mbid
480       sleep 0.1 # TODO: necessary?
481       BufferManager.erase_flash
482     end
483     save
484     super
485   end
486
487   def toggle_tagged
488     t = cursor_thread or return
489     @mutex.synchronize { @tags.toggle_tag_for t }
490     update_text_for_line curpos
491     cursor_down
492   end
493   
494   def toggle_tagged_all
495     @mutex.synchronize { @threads.each { |t| @tags.toggle_tag_for t } }
496     regen_text
497   end
498
499   def tag_matching
500     query = BufferManager.ask :search, "tag threads matching: "
501     return if query.nil? || query.empty?
502     query = /#{query}/i
503     @mutex.synchronize { @threads.each { |t| @tags.tag t if thread_matches?(t, query) } }
504     regen_text
505   end
506
507   def apply_to_tagged; @tags.apply_to_tagged; end
508
509   def edit_labels
510     thread = cursor_thread or return
511     speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
512
513     old_labels = thread.labels
514     pos = curpos
515
516     keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
517
518     user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
519
520     return unless user_labels
521     thread.labels = keepl + user_labels
522     user_labels.each { |l| LabelManager << l }
523     update_text_for_line curpos
524
525     undo = lambda{
526       thread.labels = old_labels
527       update_text_for_line pos
528       UpdateManager.relay self, :labeled, thread.first
529     }
530
531     UndoManager.register("labeling thread #{thread.first.id}", undo)
532
533     UpdateManager.relay self, :labeled, thread.first
534   end
535
536   def multi_edit_labels threads
537     user_labels = BufferManager.ask_for_labels :add_labels, "Add labels: ", [], @hidden_labels
538     return unless user_labels
539     
540     hl = user_labels.select { |l| @hidden_labels.member? l }
541     if hl.empty?
542       undo = threads.map { |t| old_labels = t.labels
543         user_labels.each { |l| t.apply_label l }
544         ## UpdateManager or some other regresh mechanism?
545         UpdateManager.relay self, :labeled, t.first
546         lambda {
547           t.labels = old_labels
548           UpdateManager.relay self, :labeled, t.first
549         }
550       }
551     user_labels.each { |l| LabelManager << l }
552     UndoManager.register("labeling #{threads.size} #{threads.size.pluralize 'thread'}",
553                          undo << lambda { regen_text})
554     else
555       BufferManager.flash "'#{hl}' is a reserved label!"
556     end
557     regen_text
558   end
559
560   def reply
561     t = cursor_thread or return
562     m = t.latest_message
563     return if m.nil? # probably won't happen
564     m.load_from_source!
565     mode = ReplyMode.new m
566     BufferManager.spawn "Reply to #{m.subj}", mode
567   end
568
569   def forward
570     t = cursor_thread or return
571     m = t.latest_message
572     return if m.nil? # probably won't happen
573     m.load_from_source!
574     ForwardMode.spawn_nicely :message => m
575   end
576
577   def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
578     return if @load_thread # todo: wrap in mutex
579     @load_thread = Redwood::reporting_thread("load threads for thread-index-mode") do
580       num = load_n_threads n, opts
581       opts[:when_done].call(num) if opts[:when_done]
582       @load_thread = nil
583     end
584   end
585
586   ## TODO: figure out @ts_mutex in this method
587   def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
588     @interrupt_search = false
589     @mbid = BufferManager.say "Searching for threads..."
590
591     ts_to_load = n
592     ts_to_load = ts_to_load + @ts.size unless n == -1 # -1 means all threads
593
594     orig_size = @ts.size
595     last_update = Time.now
596     @ts.load_n_threads(ts_to_load, opts) do |i|
597       if (Time.now - last_update) >= 0.25
598         BufferManager.say "Loaded #{i.pluralize 'thread'}...", @mbid
599         update
600         BufferManager.draw_screen
601         last_update = Time.now
602       end
603       break if @interrupt_search
604     end
605     @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
606
607     update
608     BufferManager.clear @mbid
609     @mbid = nil
610     BufferManager.draw_screen
611     @ts.size - orig_size
612   end
613   ignore_concurrent_calls :load_n_threads
614
615   def status
616     if (l = lines) == 0
617       "line 0 of 0"
618     else
619       "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
620     end
621   end
622
623   def cancel_search
624     @interrupt_search = true
625   end
626
627   def load_all_threads
628     load_threads :num => -1
629   end
630
631   def load_threads opts={}
632     if opts[:num].nil?
633       n = ThreadIndexMode::LOAD_MORE_THREAD_NUM
634     else
635       n = opts[:num]
636     end
637
638     myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
639       opts[:when_done].call(num) if opts[:when_done]
640
641       if num > 0
642         BufferManager.flash "Found #{num.pluralize 'thread'}."
643       else
644         BufferManager.flash "No matches."
645       end
646     end)})
647
648     if opts[:background] || opts[:background].nil?
649       load_n_threads_background n, myopts
650     else
651       load_n_threads n, myopts
652     end
653   end
654   ignore_concurrent_calls :load_threads
655
656   def resize rows, cols
657     regen_text
658     super
659   end
660
661 protected
662
663   def add_or_unhide m
664     @ts_mutex.synchronize do
665       if (is_relevant?(m) || @ts.is_relevant?(m)) && !@ts.contains?(m)
666         @ts.load_thread_for_message m
667       end
668
669       @hidden_threads.delete @ts.thread_for(m)
670     end
671
672     update
673   end
674
675   def thread_containing m; @ts_mutex.synchronize { @ts.thread_for m } end
676
677   ## used to tag threads by query. this can be made a lot more sophisticated,
678   ## but for right now we'll do the obvious this.
679   def thread_matches? t, query
680     t.subj =~ query || t.snippet =~ query || t.participants.any? { |x| x.longname =~ query }
681   end
682
683   def size_widget_for_thread t
684     HookManager.run("index-mode-size-widget", :thread => t) || default_size_widget_for(t)
685   end
686
687   def cursor_thread; @mutex.synchronize { @threads[curpos] }; end
688
689   def drop_all_threads
690     @tags.drop_all_tags
691     initialize_threads
692     update
693   end
694
695   def hide_thread t
696     @mutex.synchronize do
697       i = @threads.index(t) or return
698       raise "already hidden" if @hidden_threads[t]
699       @hidden_threads[t] = true
700       @threads.delete_at i
701       @size_widgets.delete_at i
702       @tags.drop_tag_for t
703     end
704   end
705
706   def update_text_for_line l
707     return unless l # not sure why this happens, but it does, occasionally
708     
709     need_update = false
710
711     @mutex.synchronize do
712       @size_widgets[l] = size_widget_for_thread @threads[l]
713
714       ## if the widget size has increased, we need to redraw everyone
715       need_update = @size_widgets[l].size > @size_widget_width
716     end
717
718     if need_update
719       update
720     else
721       @text[l] = text_for_thread_at l
722       buffer.mark_dirty if buffer
723     end
724   end
725
726   def regen_text
727     threads = @mutex.synchronize { @threads }
728     @text = threads.map_with_index { |t, i| text_for_thread_at i }
729     @lines = threads.map_with_index { |t, i| [t, i] }.to_h
730     buffer.mark_dirty if buffer
731   end
732   
733   def authors; map { |m, *o| m.from if m }.compact.uniq; end
734
735   def author_names_and_newness_for_thread t
736     new = {}
737     authors = t.map do |m, *o|
738       next unless m
739
740       name = 
741         if AccountManager.is_account?(m.from)
742           "me"
743         elsif t.authors.size == 1
744           m.from.mediumname
745         else
746           m.from.shortname
747         end
748
749       new[name] ||= m.has_label?(:unread)
750       name
751     end
752
753     authors.compact.uniq.map { |a| [a, new[a]] }
754   end
755
756   def text_for_thread_at line
757     t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
758
759     date = t.date.to_nice_s
760
761     starred = t.has_label?(:starred)
762
763     ## format the from column
764     cur_width = 0
765     ann = author_names_and_newness_for_thread t
766     from = []
767     ann.each_with_index do |(name, newness), i|
768       break if cur_width >= from_width
769       last = i == ann.length - 1
770
771       abbrev =
772         if cur_width + name.length > from_width
773           name[0 ... (from_width - cur_width - 1)] + "."
774         elsif cur_width + name.length == from_width
775           name[0 ... (from_width - cur_width)]
776         else
777           if last
778             name[0 ... (from_width - cur_width)]
779           else
780             name[0 ... (from_width - cur_width - 1)] + "," 
781           end
782         end
783
784       cur_width += abbrev.length
785
786       if last && from_width > cur_width
787         abbrev += " " * (from_width - cur_width)
788       end
789
790       from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
791     end
792
793     dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
794     p = dp || t.participants.any? { |p| AccountManager.is_account? p }
795
796     subj_color =
797       if t.has_label?(:draft)
798         :index_draft_color
799       elsif t.has_label?(:unread)
800         :index_new_color
801       elsif starred
802         :index_starred_color
803       else 
804         :index_old_color
805       end
806
807     snippet = t.snippet + (t.snippet.empty? ? "" : "...")
808
809     size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
810
811     [ 
812       [:tagged_color, @tags.tagged?(t) ? ">" : " "],
813       [:none, sprintf("%#{@date_width}s", date)],
814       (starred ? [:starred_color, "*"] : [:none, " "]),
815     ] +
816       from +
817       [
818       [subj_color, size_widget_text],
819       [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
820       [:to_me_color, dp ? ">" : (p ? '+' : " ")],
821       [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
822     ] +
823       (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
824       [[:snippet_color, snippet]
825     ]
826
827   end
828
829   def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
830
831 private
832
833   def default_size_widget_for t
834     case t.size
835     when 1
836       ""
837     else
838       "(#{t.size})"
839     end
840   end
841
842   def from_width
843     [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
844   end
845
846   def initialize_threads
847     @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
848     @ts_mutex = Mutex.new
849     @hidden_threads = {}
850   end
851 end
852
853 end