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