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