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", '#'
47 k.add :undo, "Undo the previous action", 'u'
50 def initialize hidden_labels=[], load_thread_opts={}
52 @mutex = Mutex.new # covers the following variables:
55 @size_widget_width = nil
57 @tags = Tagger.new self
59 ## these guys, and @text and @lines, are not covered
61 @load_thread_opts = load_thread_opts
62 @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
63 @date_width = DATE_WIDTH
65 @interrupt_search = false
67 initialize_threads # defines @ts and @ts_mutex
68 update # defines @text and @lines
70 UpdateManager.register self
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 }
81 def lines; @text.length; end
82 def [] i; @text[i]; end
83 def contains_thread? t; @threads.include?(t) end
88 BufferManager.draw_screen
89 load_threads :num => buffer.content_height
92 ## open up a thread view window
93 def select t=nil, when_done=nil
94 t ||= cursor_thread or return
96 Redwood::reporting_thread("load messages for thread-view-mode") do
98 message = "Loading #{num.pluralize 'message body'}..."
99 BufferManager.say(message) do |sid|
100 t.each_with_index do |(m, *o), i|
102 BufferManager.say "#{message} (#{i}/#{num})", sid if t.size > 1
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
114 update_text_for_line curpos
115 UpdateManager.relay self, :read, t.first
116 when_done.call if when_done
120 def multi_select threads
121 threads.each { |t| select t }
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
131 def launch_prev_thread_before thread, &b
132 launch_another_thread thread, -1, &b
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
144 if t # there's a next thread
145 set_cursor_pos target_l # move out of mutex?
147 elsif b # no next thread. call the block anyways
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
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)
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
173 %w(read unread archived starred unstarred).each do |state|
174 define_method "handle_#{state}_update" do |*a|
175 handle_simple_update(*a)
180 def is_relevant? m; false; end
182 def handle_added_update sender, m
184 BufferManager.draw_screen
187 def handle_single_message_deleted_update sender, m
188 @ts_mutex.synchronize do
189 return unless @ts.contains? m
195 def handle_deleted_update sender, m
196 t = @ts_mutex.synchronize { @ts.thread_for m }
202 def handle_spammed_update sender, m
203 t = @ts_mutex.synchronize { @ts.thread_for m }
209 def handle_undeleted_update sender, m
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 }
229 return unless(t = cursor_thread)
230 message, *crap = t.find { |m, *o| m.has_label? :draft }
232 mode = ResumeMode.new message
233 BufferManager.spawn "Edit message", mode
235 BufferManager.flash "Not a draft message!"
239 def actually_toggle_starred t
240 thread = t # cargo cult programming
242 if t.has_label? :starred # if ANY message has a star
244 thread.first.add_label :starred
245 update_text_for_line pos
246 UpdateManager.relay self, :starred, thread.first
248 t.remove_label :starred # remove from all
249 UpdateManager.relay self, :unstarred, t.first
252 thread.remove_label :starred
253 update_text_for_line pos
254 UpdateManager.relay self, :unstarred, thread.first
256 t.first.add_label :starred # add only to first
257 UpdateManager.relay self, :starred, t.first
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
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'}",
278 def actually_toggle_archived t
281 if t.has_label? :inbox
282 t.remove_label :inbox
284 thread.apply_label :inbox
285 update_text_for_line pos
286 UpdateManager.relay self,:unarchived, thread.first
288 UpdateManager.relay self, :archived, t.first
292 thread.remove_label :inbox
293 update_text_for_line pos
294 UpdateManager.relay self, :unarchived, thread.first
296 UpdateManager.relay self, :unarchived, t.first
302 def actually_toggle_spammed t
303 if t.has_label? :spam
305 UpdateManager.relay self, :unspammed, t.first
308 UpdateManager.relay self, :spammed, t.first
312 def actually_toggle_deleted t
313 if t.has_label? :deleted
314 t.remove_label :deleted
315 UpdateManager.relay self, :undeleted, t.first
317 t.apply_label :deleted
318 UpdateManager.relay self, :deleted, t.first
323 t = cursor_thread or return
324 undo = [actually_toggle_archived(t), lambda {self.update_text_for_line curpos}]
325 UndoManager.register("deleting/undeleting thread #{t.first.id}",undo)
326 update_text_for_line curpos
329 def multi_toggle_archived threads
330 undo = threads.map { |t| actually_toggle_archived t}
331 UndoManager.register("deleting/undeleting #{threads.size} #{threads.size.pluralize 'thread'}",
332 undo << lambda {self.regen_text})
337 t = cursor_thread or return
338 t.toggle_label :unread
339 update_text_for_line curpos
343 def multi_toggle_new threads
344 threads.each { |t| t.toggle_label :unread }
348 def multi_toggle_tagged threads
349 @mutex.synchronize { @tags.drop_all_tags }
354 ## this command has no non-tagged form. as a convenience, allow this
355 ## command to be applied to tagged threads without hitting ';'.
356 @tags.apply_to_tagged :join_threads
359 def multi_join_threads threads
360 @ts.join_threads threads or return
361 @tags.drop_all_tags # otherwise we have tag pointers to invalid threads!
366 n = @mutex.synchronize do
367 ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } ||
368 (0 ... curpos).find { |i| @threads[i].has_label? :unread }
371 ## jump there if necessary
372 jump_to_line n unless n >= topline && n < botline
375 BufferManager.flash "No new messages"
380 t = cursor_thread or return
381 multi_toggle_spam [t]
382 HookManager.run("mark-as-spam", :thread => t)
385 ## both spam and deleted have the curious characteristic that you
386 ## always want to hide the thread after either applying or removing
387 ## that label. in all thread-index-views except for
388 ## label-search-results-mode, when you mark a message as spam or
389 ## deleted, you want it to disappear immediately; in LSRM, you only
390 ## see deleted or spam emails, and when you undelete or unspam them
391 ## you also want them to disappear immediately.
392 def multi_toggle_spam threads
394 actually_toggle_spammed t
401 t = cursor_thread or return
402 multi_toggle_deleted [t]
405 ## see comment for multi_toggle_spam
406 def multi_toggle_deleted threads
408 actually_toggle_deleted t
415 t = cursor_thread or return
419 def multi_kill threads
421 t.apply_label :killed
425 BufferManager.flash "#{threads.size.pluralize 'Thread'} killed."
429 dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
430 return if dirty_threads.empty?
432 BufferManager.say("Saving threads...") do |say_id|
433 dirty_threads.each_with_index do |t, i|
434 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
441 UpdateManager.unregister self
445 BufferManager.clear @mbid if @mbid
446 sleep 0.1 # TODO: necessary?
447 BufferManager.erase_flash
454 t = cursor_thread or return
455 @mutex.synchronize { @tags.toggle_tag_for t }
456 update_text_for_line curpos
460 def toggle_tagged_all
461 @mutex.synchronize { @threads.each { |t| @tags.toggle_tag_for t } }
466 query = BufferManager.ask :search, "tag threads matching: "
467 return if query.nil? || query.empty?
469 @mutex.synchronize { @threads.each { |t| @tags.tag t if thread_matches?(t, query) } }
473 def apply_to_tagged; @tags.apply_to_tagged; end
476 thread = cursor_thread or return
477 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
478 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
480 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
482 return unless user_labels
483 thread.labels = keepl + user_labels
484 user_labels.each { |l| LabelManager << l }
485 update_text_for_line curpos
486 UpdateManager.relay self, :labeled, thread.first
489 def multi_edit_labels threads
490 user_labels = BufferManager.ask_for_labels :add_labels, "Add labels: ", [], @hidden_labels
491 return unless user_labels
493 hl = user_labels.select { |l| @hidden_labels.member? l }
495 threads.each { |t| user_labels.each { |l| t.apply_label l } }
496 user_labels.each { |l| LabelManager << l }
498 BufferManager.flash "'#{hl}' is a reserved label!"
504 t = cursor_thread or return
506 return if m.nil? # probably won't happen
508 mode = ReplyMode.new m
509 BufferManager.spawn "Reply to #{m.subj}", mode
513 t = cursor_thread or return
515 return if m.nil? # probably won't happen
517 ForwardMode.spawn_nicely :message => m
520 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
521 return if @load_thread # todo: wrap in mutex
522 @load_thread = Redwood::reporting_thread("load threads for thread-index-mode") do
523 num = load_n_threads n, opts
524 opts[:when_done].call(num) if opts[:when_done]
529 ## TODO: figure out @ts_mutex in this method
530 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
531 @interrupt_search = false
532 @mbid = BufferManager.say "Searching for threads..."
535 ts_to_load = ts_to_load + @ts.size unless n == -1 # -1 means all threads
538 last_update = Time.now
539 @ts.load_n_threads(ts_to_load, opts) do |i|
540 if (Time.now - last_update) >= 0.25
541 BufferManager.say "Loaded #{i.pluralize 'thread'}...", @mbid
543 BufferManager.draw_screen
544 last_update = Time.now
546 break if @interrupt_search
548 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
551 BufferManager.clear @mbid
553 BufferManager.draw_screen
556 ignore_concurrent_calls :load_n_threads
562 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
567 @interrupt_search = true
571 load_threads :num => -1
574 def load_threads opts={}
576 n = ThreadIndexMode::LOAD_MORE_THREAD_NUM
581 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
582 opts[:when_done].call(num) if opts[:when_done]
585 BufferManager.flash "Found #{num.pluralize 'thread'}."
587 BufferManager.flash "No matches."
591 if opts[:background] || opts[:background].nil?
592 load_n_threads_background n, myopts
594 load_n_threads n, myopts
597 ignore_concurrent_calls :load_threads
599 def resize rows, cols
607 @ts_mutex.synchronize do
608 if (is_relevant?(m) || @ts.is_relevant?(m)) && !@ts.contains?(m)
609 @ts.load_thread_for_message m
612 @hidden_threads.delete @ts.thread_for(m)
618 def thread_containing m; @ts_mutex.synchronize { @ts.thread_for m } end
620 ## used to tag threads by query. this can be made a lot more sophisticated,
621 ## but for right now we'll do the obvious this.
622 def thread_matches? t, query
623 t.subj =~ query || t.snippet =~ query || t.participants.any? { |x| x.longname =~ query }
626 def size_widget_for_thread t
627 HookManager.run("index-mode-size-widget", :thread => t) || default_size_widget_for(t)
630 def cursor_thread; @mutex.synchronize { @threads[curpos] }; end
639 @mutex.synchronize do
640 i = @threads.index(t) or return
641 raise "already hidden" if @hidden_threads[t]
642 @hidden_threads[t] = true
644 @size_widgets.delete_at i
649 def update_text_for_line l
650 return unless l # not sure why this happens, but it does, occasionally
654 @mutex.synchronize do
655 @size_widgets[l] = size_widget_for_thread @threads[l]
657 ## if the widget size has increased, we need to redraw everyone
658 need_update = @size_widgets[l].size > @size_widget_width
664 @text[l] = text_for_thread_at l
665 buffer.mark_dirty if buffer
670 threads = @mutex.synchronize { @threads }
671 @text = threads.map_with_index { |t, i| text_for_thread_at i }
672 @lines = threads.map_with_index { |t, i| [t, i] }.to_h
673 buffer.mark_dirty if buffer
676 def authors; map { |m, *o| m.from if m }.compact.uniq; end
678 def author_names_and_newness_for_thread t
680 authors = t.map do |m, *o|
684 if AccountManager.is_account?(m.from)
686 elsif t.authors.size == 1
692 new[name] ||= m.has_label?(:unread)
696 authors.compact.uniq.map { |a| [a, new[a]] }
699 def text_for_thread_at line
700 t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
702 date = t.date.to_nice_s
704 starred = t.has_label?(:starred)
706 ## format the from column
708 ann = author_names_and_newness_for_thread t
710 ann.each_with_index do |(name, newness), i|
711 break if cur_width >= from_width
712 last = i == ann.length - 1
715 if cur_width + name.length > from_width
716 name[0 ... (from_width - cur_width - 1)] + "."
717 elsif cur_width + name.length == from_width
718 name[0 ... (from_width - cur_width)]
721 name[0 ... (from_width - cur_width)]
723 name[0 ... (from_width - cur_width - 1)] + ","
727 cur_width += abbrev.length
729 if last && from_width > cur_width
730 abbrev += " " * (from_width - cur_width)
733 from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
736 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
737 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
740 if t.has_label?(:draft)
742 elsif t.has_label?(:unread)
750 snippet = t.snippet + (t.snippet.empty? ? "" : "...")
752 size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
755 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
756 [:none, sprintf("%#{@date_width}s", date)],
757 (starred ? [:starred_color, "*"] : [:none, " "]),
761 [subj_color, size_widget_text],
762 [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
763 [:to_me_color, dp ? ">" : (p ? '+' : " ")],
764 [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
766 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
767 [[:snippet_color, snippet]
772 def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
776 def default_size_widget_for t
786 [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
789 def initialize_threads
790 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
791 @ts_mutex = Mutex.new