3 ## subclasses should implement:
6 class ThreadIndexMode < LineCursorMode
7 DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
9 LOAD_MORE_THREAD_NUM = 20
11 HookManager.register "index-mode-size-widget", <<EOS
12 Generates the per-thread size widget for each thread.
14 thread: The message thread to be formatted.
17 HookManager.register "mark-as-spam", <<EOS
18 This hook is run when a thread is marked as spam
20 thread: The message thread being marked as spam.
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)", '!'
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", '#'
49 def initialize hidden_labels=[], load_thread_opts={}
51 @mutex = Mutex.new # covers the following variables:
54 @size_widget_width = nil
56 @tags = Tagger.new self
58 ## these guys, and @text and @lines, are not covered
60 @load_thread_opts = load_thread_opts
61 @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
62 @date_width = DATE_WIDTH
64 @interrupt_search = false
66 initialize_threads # defines @ts and @ts_mutex
67 update # defines @text and @lines
69 UpdateManager.register self
71 @last_load_more_size = nil
72 to_load_more do |size|
73 next if @last_load_more_size == 0
74 load_threads :num => 1, :background => false
75 load_threads :num => (size - 1),
76 :when_done => lambda { |num| @last_load_more_size = num }
80 def lines; @text.length; end
81 def [] i; @text[i]; end
82 def contains_thread? t; @threads.include?(t) end
86 BufferManager.draw_screen
87 load_threads :num => buffer.content_height
90 ## open up a thread view window
91 def select t=nil, when_done=nil
92 t ||= cursor_thread or return
94 Redwood::reporting_thread("load messages for thread-view-mode") do
96 message = "Loading #{num.pluralize 'message body'}..."
97 BufferManager.say(message) do |sid|
98 t.each_with_index do |(m, *o), i|
100 BufferManager.say "#{message} (#{i}/#{num})", sid if t.size > 1
104 mode = ThreadViewMode.new t, @hidden_labels, self
105 BufferManager.spawn t.subj, mode
106 BufferManager.draw_screen
107 mode.jump_to_first_open true
108 BufferManager.draw_screen # lame TODO: make this unnecessary
109 ## the first draw_screen is needed before topline and botline
110 ## are set, and the second to show the cursor having moved
112 update_text_for_line curpos
113 UpdateManager.relay self, :read, t.first
114 when_done.call if when_done
118 def multi_select threads
119 threads.each { |t| select t }
122 ## these two methods are called by thread-view-modes when the user
123 ## wants to view the previous/next thread without going back to
124 ## index-mode. we update the cursor as a convenience.
125 def launch_next_thread_after thread, &b
126 launch_another_thread thread, 1, &b
129 def launch_prev_thread_before thread, &b
130 launch_another_thread thread, -1, &b
133 def launch_another_thread thread, direction, &b
134 l = @lines[thread] or return
135 target_l = l + direction
136 t = @mutex.synchronize do
137 if target_l >= 0 && target_l < @threads.length
142 if t # there's a next thread
143 set_cursor_pos target_l # move out of mutex?
145 elsif b # no next thread. call the block anyways
150 def handle_single_message_labeled_update sender, m
151 ## no need to do anything different here; we don't differentiate
152 ## messages from their containing threads
153 handle_labeled_update sender, m
156 def handle_labeled_update sender, m
157 if(t = thread_containing(m))
158 l = @lines[t] or return
159 update_text_for_line l
160 elsif is_relevant?(m)
165 def handle_simple_update sender, m
166 t = thread_containing(m) or return
167 l = @lines[t] or return
168 update_text_for_line l
171 %w(read unread archived starred unstarred).each do |state|
172 define_method "handle_#{state}_update" do |*a|
173 handle_simple_update(*a)
178 def is_relevant? m; false; end
180 def handle_added_update sender, m
182 BufferManager.draw_screen
185 def handle_single_message_deleted_update sender, m
186 @ts_mutex.synchronize do
187 return unless @ts.contains? m
193 def handle_deleted_update sender, m
194 t = @ts_mutex.synchronize { @ts.thread_for m }
200 def handle_spammed_update sender, m
201 t = @ts_mutex.synchronize { @ts.thread_for m }
207 def handle_undeleted_update sender, m
212 @mutex.synchronize do
213 ## let's see you do THIS in python
214 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| [t.date, t.first.id] }.reverse
215 @size_widgets = @threads.map { |t| size_widget_for_thread t }
216 @size_widget_width = @size_widgets.max_of { |w| w.length }
223 return unless(t = cursor_thread)
224 message, *crap = t.find { |m, *o| m.has_label? :draft }
226 mode = ResumeMode.new message
227 BufferManager.spawn "Edit message", mode
229 BufferManager.flash "Not a draft message!"
233 def actually_toggle_starred t
234 if t.has_label? :starred # if ANY message has a star
235 t.remove_label :starred # remove from all
236 UpdateManager.relay self, :unstarred, t.first
238 t.first.add_label :starred # add only to first
239 UpdateManager.relay self, :starred, t.first
244 t = cursor_thread or return
245 actually_toggle_starred t
246 update_text_for_line curpos
250 def multi_toggle_starred threads
251 threads.each { |t| actually_toggle_starred t }
255 def actually_toggle_archived t
256 if t.has_label? :inbox
257 t.remove_label :inbox
258 UpdateManager.relay self, :archived, t.first
261 UpdateManager.relay self, :unarchived, t.first
265 def actually_toggle_spammed t
266 if t.has_label? :spam
268 UpdateManager.relay self, :unspammed, t.first
271 UpdateManager.relay self, :spammed, t.first
275 def actually_toggle_deleted t
276 if t.has_label? :deleted
277 t.remove_label :deleted
278 UpdateManager.relay self, :undeleted, t.first
280 t.apply_label :deleted
281 UpdateManager.relay self, :deleted, t.first
286 t = cursor_thread or return
287 actually_toggle_archived t
288 update_text_for_line curpos
291 def multi_toggle_archived threads
292 threads.each { |t| actually_toggle_archived t }
297 t = cursor_thread or return
298 t.toggle_label :unread
299 update_text_for_line curpos
303 def multi_toggle_new threads
304 threads.each { |t| t.toggle_label :unread }
308 def multi_toggle_tagged threads
309 @mutex.synchronize { @tags.drop_all_tags }
314 ## this command has no non-tagged form. as a convenience, allow this
315 ## command to be applied to tagged threads without hitting ';'.
316 @tags.apply_to_tagged :join_threads
319 def multi_join_threads threads
320 @ts.join_threads threads or return
321 @tags.drop_all_tags # otherwise we have tag pointers to invalid threads!
326 n = @mutex.synchronize do
327 ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } ||
328 (0 ... curpos).find { |i| @threads[i].has_label? :unread }
331 ## jump there if necessary
332 jump_to_line n unless n >= topline && n < botline
335 BufferManager.flash "No new messages"
340 t = cursor_thread or return
341 multi_toggle_spam [t]
342 HookManager.run("mark-as-spam", :thread => t)
345 ## both spam and deleted have the curious characteristic that you
346 ## always want to hide the thread after either applying or removing
347 ## that label. in all thread-index-views except for
348 ## label-search-results-mode, when you mark a message as spam or
349 ## deleted, you want it to disappear immediately; in LSRM, you only
350 ## see deleted or spam emails, and when you undelete or unspam them
351 ## you also want them to disappear immediately.
352 def multi_toggle_spam threads
354 actually_toggle_spammed t
361 t = cursor_thread or return
362 multi_toggle_deleted [t]
365 ## see comment for multi_toggle_spam
366 def multi_toggle_deleted threads
368 actually_toggle_deleted t
375 t = cursor_thread or return
379 def multi_kill threads
381 t.apply_label :killed
385 BufferManager.flash "#{threads.size.pluralize 'Thread'} killed."
389 BufferManager.say("Saving contacts...") { ContactManager.instance.save }
390 dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
391 return if dirty_threads.empty?
393 BufferManager.say("Saving threads...") do |say_id|
394 dirty_threads.each_with_index do |t, i|
395 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
402 UpdateManager.unregister self
406 BufferManager.clear @mbid if @mbid
407 sleep 0.1 # TODO: necessary?
408 BufferManager.erase_flash
415 t = cursor_thread or return
416 @mutex.synchronize { @tags.toggle_tag_for t }
417 update_text_for_line curpos
421 def toggle_tagged_all
422 @mutex.synchronize { @threads.each { |t| @tags.toggle_tag_for t } }
427 query = BufferManager.ask :search, "tag threads matching: "
428 return if query.nil? || query.empty?
430 @mutex.synchronize { @threads.each { |t| @tags.tag t if thread_matches?(t, query) } }
434 def apply_to_tagged; @tags.apply_to_tagged; end
437 thread = cursor_thread or return
438 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
439 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
441 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
443 return unless user_labels
444 thread.labels = keepl + user_labels
445 user_labels.each { |l| LabelManager << l }
446 update_text_for_line curpos
447 UpdateManager.relay self, :labeled, thread.first
450 def multi_edit_labels threads
451 user_labels = BufferManager.ask_for_labels :add_labels, "Add labels: ", [], @hidden_labels
452 return unless user_labels
454 hl = user_labels.select { |l| @hidden_labels.member? l }
456 threads.each { |t| user_labels.each { |l| t.apply_label l } }
457 user_labels.each { |l| LabelManager << l }
459 BufferManager.flash "'#{hl}' is a reserved label!"
465 t = cursor_thread or return
467 return if m.nil? # probably won't happen
469 mode = ReplyMode.new m
470 BufferManager.spawn "Reply to #{m.subj}", mode
474 t = cursor_thread or return
476 return if m.nil? # probably won't happen
478 ForwardMode.spawn_nicely :message => m
481 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
482 return if @load_thread # todo: wrap in mutex
483 @load_thread = Redwood::reporting_thread("load threads for thread-index-mode") do
484 num = load_n_threads n, opts
485 opts[:when_done].call(num) if opts[:when_done]
490 ## TODO: figure out @ts_mutex in this method
491 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
492 @interrupt_search = false
493 @mbid = BufferManager.say "Searching for threads..."
496 ts_to_load = ts_to_load + @ts.size unless n == -1 # -1 means all threads
499 last_update = Time.now
500 @ts.load_n_threads(ts_to_load, opts) do |i|
501 if (Time.now - last_update) >= 0.25
502 BufferManager.say "Loaded #{i.pluralize 'thread'}...", @mbid
504 BufferManager.draw_screen
505 last_update = Time.now
507 break if @interrupt_search
509 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
512 BufferManager.clear @mbid
514 BufferManager.draw_screen
517 ignore_concurrent_calls :load_n_threads
523 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
528 @interrupt_search = true
532 load_threads :num => -1
535 def load_threads opts={}
537 n = ThreadIndexMode::LOAD_MORE_THREAD_NUM
542 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
543 opts[:when_done].call(num) if opts[:when_done]
546 BufferManager.flash "Found #{num.pluralize 'thread'}."
548 BufferManager.flash "No matches."
552 if opts[:background] || opts[:background].nil?
553 load_n_threads_background n, myopts
555 load_n_threads n, myopts
558 ignore_concurrent_calls :load_threads
560 def resize rows, cols
568 @ts_mutex.synchronize do
569 if (is_relevant?(m) || @ts.is_relevant?(m)) && !@ts.contains?(m)
570 @ts.load_thread_for_message m
573 @hidden_threads.delete @ts.thread_for(m)
579 def thread_containing m; @ts_mutex.synchronize { @ts.thread_for m } end
581 ## used to tag threads by query. this can be made a lot more sophisticated,
582 ## but for right now we'll do the obvious this.
583 def thread_matches? t, query
584 t.subj =~ query || t.snippet =~ query || t.participants.any? { |x| x.longname =~ query }
587 def size_widget_for_thread t
588 HookManager.run("index-mode-size-widget", :thread => t) || default_size_widget_for(t)
591 def cursor_thread; @mutex.synchronize { @threads[curpos] }; end
600 @mutex.synchronize do
601 i = @threads.index(t) or return
602 raise "already hidden" if @hidden_threads[t]
603 @hidden_threads[t] = true
605 @size_widgets.delete_at i
610 def update_text_for_line l
611 return unless l # not sure why this happens, but it does, occasionally
615 @mutex.synchronize do
616 @size_widgets[l] = size_widget_for_thread @threads[l]
618 ## if the widget size has increased, we need to redraw everyone
619 need_update = @size_widgets[l].size > @size_widget_width
625 @text[l] = text_for_thread_at l
626 buffer.mark_dirty if buffer
631 threads = @mutex.synchronize { @threads }
632 @text = threads.map_with_index { |t, i| text_for_thread_at i }
633 @lines = threads.map_with_index { |t, i| [t, i] }.to_h
634 buffer.mark_dirty if buffer
637 def authors; map { |m, *o| m.from if m }.compact.uniq; end
639 def author_names_and_newness_for_thread t
641 authors = t.map do |m, *o|
645 if AccountManager.is_account?(m.from)
647 elsif t.authors.size == 1
653 new[name] ||= m.has_label?(:unread)
657 authors.compact.uniq.map { |a| [a, new[a]] }
660 def text_for_thread_at line
661 t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
663 date = t.date.to_nice_s
665 starred = t.has_label?(:starred)
667 ## format the from column
669 ann = author_names_and_newness_for_thread t
671 ann.each_with_index do |(name, newness), i|
672 break if cur_width >= from_width
673 last = i == ann.length - 1
676 if cur_width + name.length > from_width
677 name[0 ... (from_width - cur_width - 1)] + "."
678 elsif cur_width + name.length == from_width
679 name[0 ... (from_width - cur_width)]
682 name[0 ... (from_width - cur_width)]
684 name[0 ... (from_width - cur_width - 1)] + ","
688 cur_width += abbrev.length
690 if last && from_width > cur_width
691 abbrev += " " * (from_width - cur_width)
694 from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
697 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
698 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
701 if t.has_label?(:draft)
703 elsif t.has_label?(:unread)
711 snippet = t.snippet + (t.snippet.empty? ? "" : "...")
713 size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
716 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
717 [:none, sprintf("%#{@date_width}s", date)],
718 (starred ? [:starred_color, "*"] : [:none, " "]),
722 [subj_color, size_widget_text],
723 [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
724 [:to_me_color, dp ? ">" : (p ? '+' : " ")],
725 [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
727 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
728 [[:snippet_color, snippet]
733 def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
737 def default_size_widget_for t
747 [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
750 def initialize_threads
751 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
752 @ts_mutex = Mutex.new