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 @save_thread_mutex = Mutex.new
74 @last_load_more_size = nil
75 to_load_more do |size|
76 next if @last_load_more_size == 0
77 load_threads :num => 1, :background => false
78 load_threads :num => (size - 1),
79 :when_done => lambda { |num| @last_load_more_size = num }
83 def unsaved?; dirty? end
84 def lines; @text.length; end
85 def [] i; @text[i]; end
86 def contains_thread? t; @threads.include?(t) end
91 BufferManager.draw_screen
92 load_threads :num => buffer.content_height
95 ## open up a thread view window
96 def select t=nil, when_done=nil
97 t ||= cursor_thread or return
99 Redwood::reporting_thread("load messages for thread-view-mode") do
101 message = "Loading #{num.pluralize 'message body'}..."
102 BufferManager.say(message) do |sid|
103 t.each_with_index do |(m, *o), i|
105 BufferManager.say "#{message} (#{i}/#{num})", sid if t.size > 1
109 mode = ThreadViewMode.new t, @hidden_labels, self
110 BufferManager.spawn t.subj, mode
111 BufferManager.draw_screen
112 mode.jump_to_first_open true
113 BufferManager.draw_screen # lame TODO: make this unnecessary
114 ## the first draw_screen is needed before topline and botline
115 ## are set, and the second to show the cursor having moved
117 update_text_for_line curpos
118 UpdateManager.relay self, :read, t.first
119 when_done.call if when_done
123 def multi_select threads
124 threads.each { |t| select t }
127 ## these two methods are called by thread-view-modes when the user
128 ## wants to view the previous/next thread without going back to
129 ## index-mode. we update the cursor as a convenience.
130 def launch_next_thread_after thread, &b
131 launch_another_thread thread, 1, &b
134 def launch_prev_thread_before thread, &b
135 launch_another_thread thread, -1, &b
138 def launch_another_thread thread, direction, &b
139 l = @lines[thread] or return
140 target_l = l + direction
141 t = @mutex.synchronize do
142 if target_l >= 0 && target_l < @threads.length
147 if t # there's a next thread
148 set_cursor_pos target_l # move out of mutex?
150 elsif b # no next thread. call the block anyways
155 def handle_single_message_labeled_update sender, m
156 ## no need to do anything different here; we don't differentiate
157 ## messages from their containing threads
158 handle_labeled_update sender, m
161 def handle_labeled_update sender, m
162 if(t = thread_containing(m))
163 l = @lines[t] or return
164 update_text_for_line l
165 elsif is_relevant?(m)
170 def handle_simple_update sender, m
171 t = thread_containing(m) or return
172 l = @lines[t] or return
173 update_text_for_line l
176 %w(read unread archived starred unstarred).each do |state|
177 define_method "handle_#{state}_update" do |*a|
178 handle_simple_update(*a)
183 def is_relevant? m; false; end
185 def handle_added_update sender, m
187 BufferManager.draw_screen
190 def handle_single_message_deleted_update sender, m
191 @ts_mutex.synchronize do
192 return unless @ts.contains? m
198 def handle_deleted_update sender, m
199 t = @ts_mutex.synchronize { @ts.thread_for m }
205 def handle_spammed_update sender, m
206 t = @ts_mutex.synchronize { @ts.thread_for m }
212 def handle_undeleted_update sender, m
221 @mutex.synchronize do
222 ## let's see you do THIS in python
223 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| [t.date, t.first.id] }.reverse
224 @size_widgets = @threads.map { |t| size_widget_for_thread t }
225 @size_widget_width = @size_widgets.max_of { |w| w.display_length }
232 return unless(t = cursor_thread)
233 message, *crap = t.find { |m, *o| m.has_label? :draft }
235 mode = ResumeMode.new message
236 BufferManager.spawn "Edit message", mode
238 BufferManager.flash "Not a draft message!"
242 ## returns an undo lambda
243 def actually_toggle_starred t
245 if t.has_label? :starred # if ANY message has a star
246 t.remove_label :starred # remove from all
247 UpdateManager.relay self, :unstarred, t.first
249 t.first.add_label :starred
250 UpdateManager.relay self, :starred, t.first
254 t.first.add_label :starred # add only to first
255 UpdateManager.relay self, :starred, t.first
257 t.remove_label :starred
258 UpdateManager.relay self, :unstarred, t.first
265 t = cursor_thread or return
266 undo = actually_toggle_starred t
267 UndoManager.register "toggling thread starred status", undo
268 update_text_for_line curpos
272 def multi_toggle_starred threads
273 UndoManager.register "toggling #{threads.size.pluralize 'thread'} starred status",
274 threads.map { |t| actually_toggle_starred t }
278 ## returns an undo lambda
279 def actually_toggle_archived t
282 if t.has_label? :inbox
283 t.remove_label :inbox
284 UpdateManager.relay self, :archived, t.first
286 thread.apply_label :inbox
287 update_text_for_line pos
288 UpdateManager.relay self,:unarchived, thread.first
292 UpdateManager.relay self, :unarchived, t.first
294 thread.remove_label :inbox
295 update_text_for_line pos
296 UpdateManager.relay self, :unarchived, thread.first
301 ## returns an undo lambda
302 def actually_toggle_spammed t
304 if t.has_label? :spam
306 add_or_unhide t.first
307 UpdateManager.relay self, :unspammed, t.first
309 thread.apply_label :spam
310 self.hide_thread thread
311 UpdateManager.relay self,:spammed, thread.first
316 UpdateManager.relay self, :spammed, t.first
318 thread.remove_label :spam
319 add_or_unhide thread.first
320 UpdateManager.relay self,:unspammed, thread.first
325 ## returns an undo lambda
326 def actually_toggle_deleted t
327 if t.has_label? :deleted
328 t.remove_label :deleted
329 add_or_unhide t.first
330 UpdateManager.relay self, :undeleted, t.first
332 t.apply_label :deleted
334 UpdateManager.relay self, :deleted, t.first
337 t.apply_label :deleted
339 UpdateManager.relay self, :deleted, t.first
341 t.remove_label :deleted
342 add_or_unhide t.first
343 UpdateManager.relay self, :undeleted, t.first
349 t = cursor_thread or return
350 undo = actually_toggle_archived t
351 UndoManager.register "deleting/undeleting thread #{t.first.id}", undo, lambda { update_text_for_line curpos }
352 update_text_for_line curpos
355 def multi_toggle_archived threads
356 undos = threads.map { |t| actually_toggle_archived t }
357 UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}", undos, lambda { regen_text }
362 t = cursor_thread or return
363 t.toggle_label :unread
364 update_text_for_line curpos
368 def multi_toggle_new threads
369 threads.each { |t| t.toggle_label :unread }
373 def multi_toggle_tagged threads
374 @mutex.synchronize { @tags.drop_all_tags }
379 ## this command has no non-tagged form. as a convenience, allow this
380 ## command to be applied to tagged threads without hitting ';'.
381 @tags.apply_to_tagged :join_threads
384 def multi_join_threads threads
385 @ts.join_threads threads or return
386 @tags.drop_all_tags # otherwise we have tag pointers to invalid threads!
391 n = @mutex.synchronize do
392 ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } ||
393 (0 ... curpos).find { |i| @threads[i].has_label? :unread }
396 ## jump there if necessary
397 jump_to_line n unless n >= topline && n < botline
400 BufferManager.flash "No new messages"
405 t = cursor_thread or return
406 multi_toggle_spam [t]
407 HookManager.run("mark-as-spam", :thread => t)
410 ## both spam and deleted have the curious characteristic that you
411 ## always want to hide the thread after either applying or removing
412 ## that label. in all thread-index-views except for
413 ## label-search-results-mode, when you mark a message as spam or
414 ## deleted, you want it to disappear immediately; in LSRM, you only
415 ## see deleted or spam emails, and when you undelete or unspam them
416 ## you also want them to disappear immediately.
417 def multi_toggle_spam threads
418 undos = threads.map { |t| actually_toggle_spammed t }
419 UndoManager.register "marking/unmarking #{threads.size.pluralize 'thread'} as spam",
420 undos, lambda { regen_text }
425 t = cursor_thread or return
426 multi_toggle_deleted [t]
429 ## see comment for multi_toggle_spam
430 def multi_toggle_deleted threads
431 undos = threads.map { |t| actually_toggle_deleted t }
432 UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}",
433 undos, lambda { regen_text }
438 t = cursor_thread or return
442 ## m-m-m-m-MULTI-KILL
443 def multi_kill threads
444 UndoManager.register "killing #{threads.size.pluralize 'thread'}" do
446 t.remove_label :killed
447 add_or_unhide t.first
453 t.apply_label :killed
458 BufferManager.flash "#{threads.size.pluralize 'thread'} killed."
461 def save background=true
463 Redwood::reporting_thread("saving thread") { actually_save }
470 @save_thread_mutex.synchronize do
471 BufferManager.say("Saving contacts...") { ContactManager.instance.save }
472 dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
473 next if dirty_threads.empty?
475 BufferManager.say("Saving threads...") do |say_id|
476 dirty_threads.each_with_index do |t, i|
477 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
485 UpdateManager.unregister self
489 BufferManager.clear @mbid if @mbid
490 sleep 0.1 # TODO: necessary?
491 BufferManager.erase_flash
498 t = cursor_thread or return
499 @mutex.synchronize { @tags.toggle_tag_for t }
500 update_text_for_line curpos
504 def toggle_tagged_all
505 @mutex.synchronize { @threads.each { |t| @tags.toggle_tag_for t } }
510 query = BufferManager.ask :search, "tag threads matching (regex): "
511 return if query.nil? || query.empty?
514 rescue RegexpError => e
515 BufferManager.flash "error interpreting '#{query}': #{e.message}"
518 @mutex.synchronize { @threads.each { |t| @tags.tag t if thread_matches?(t, query) } }
522 def apply_to_tagged; @tags.apply_to_tagged; end
525 thread = cursor_thread or return
526 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
528 old_labels = thread.labels
531 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
533 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
535 return unless user_labels
536 thread.labels = keepl + user_labels
537 user_labels.each { |l| LabelManager << l }
538 update_text_for_line curpos
540 UndoManager.register "labeling thread" do
541 thread.labels = old_labels
542 update_text_for_line pos
543 UpdateManager.relay self, :labeled, thread.first
546 UpdateManager.relay self, :labeled, thread.first
549 def multi_edit_labels threads
550 user_labels = BufferManager.ask_for_labels :labels, "Add/remove labels (use -label to remove): ", [], @hidden_labels
551 return unless user_labels
553 user_labels.map! { |l| (l.to_s =~ /^-/)? [l.to_s.gsub(/^-?/, '').to_sym, true] : [l, false] }
554 hl = user_labels.select { |(l,_)| @hidden_labels.member? l }
556 BufferManager.flash "'#{hl}' is a reserved label!"
560 old_labels = threads.map { |t| t.labels.dup }
563 user_labels.each do |(l, to_remove)|
571 UpdateManager.relay self, :labeled, t.first
576 UndoManager.register "labeling #{threads.size.pluralize 'thread'}" do
577 threads.zip(old_labels).map do |t, old_labels|
578 t.labels = old_labels
579 UpdateManager.relay self, :labeled, t.first
586 t = cursor_thread or return
588 return if m.nil? # probably won't happen
590 mode = ReplyMode.new m
591 BufferManager.spawn "Reply to #{m.subj}", mode
595 t = cursor_thread or return
597 return if m.nil? # probably won't happen
599 ForwardMode.spawn_nicely :message => m
602 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
603 return if @load_thread # todo: wrap in mutex
604 @load_thread = Redwood::reporting_thread("load threads for thread-index-mode") do
605 num = load_n_threads n, opts
606 opts[:when_done].call(num) if opts[:when_done]
611 ## TODO: figure out @ts_mutex in this method
612 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
613 @interrupt_search = false
614 @mbid = BufferManager.say "Searching for threads..."
617 ts_to_load = ts_to_load + @ts.size unless n == -1 # -1 means all threads
620 last_update = Time.now
621 @ts.load_n_threads(ts_to_load, opts) do |i|
622 if (Time.now - last_update) >= 0.25
623 BufferManager.say "Loaded #{i.pluralize 'thread'}...", @mbid
625 BufferManager.draw_screen
626 last_update = Time.now
628 break if @interrupt_search
630 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
633 BufferManager.clear @mbid
635 BufferManager.draw_screen
638 ignore_concurrent_calls :load_n_threads
644 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
649 @interrupt_search = true
653 load_threads :num => -1
656 def load_threads opts={}
658 n = ThreadIndexMode::LOAD_MORE_THREAD_NUM
663 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
664 opts[:when_done].call(num) if opts[:when_done]
667 BufferManager.flash "Found #{num.pluralize 'thread'}."
669 BufferManager.flash "No matches."
673 if opts[:background] || opts[:background].nil?
674 load_n_threads_background n, myopts
676 load_n_threads n, myopts
679 ignore_concurrent_calls :load_threads
681 def resize rows, cols
689 @ts_mutex.synchronize do
690 if (is_relevant?(m) || @ts.is_relevant?(m)) && !@ts.contains?(m)
691 @ts.load_thread_for_message m, @load_thread_opts
694 @hidden_threads.delete @ts.thread_for(m)
700 def thread_containing m; @ts_mutex.synchronize { @ts.thread_for m } end
702 ## used to tag threads by query. this can be made a lot more sophisticated,
703 ## but for right now we'll do the obvious this.
704 def thread_matches? t, query
705 t.subj =~ query || t.snippet =~ query || t.participants.any? { |x| x.longname =~ query }
708 def size_widget_for_thread t
709 HookManager.run("index-mode-size-widget", :thread => t) || default_size_widget_for(t)
712 def cursor_thread; @mutex.synchronize { @threads[curpos] }; end
721 @mutex.synchronize do
722 i = @threads.index(t) or return
723 raise "already hidden" if @hidden_threads[t]
724 @hidden_threads[t] = true
726 @size_widgets.delete_at i
731 def update_text_for_line l
732 return unless l # not sure why this happens, but it does, occasionally
736 @mutex.synchronize do
737 @size_widgets[l] = size_widget_for_thread @threads[l]
739 ## if the widget size has increased, we need to redraw everyone
740 need_update = @size_widgets[l].size > @size_widget_width
746 @text[l] = text_for_thread_at l
747 buffer.mark_dirty if buffer
752 threads = @mutex.synchronize { @threads }
753 @text = threads.map_with_index { |t, i| text_for_thread_at i }
754 @lines = threads.map_with_index { |t, i| [t, i] }.to_h
755 buffer.mark_dirty if buffer
758 def authors; map { |m, *o| m.from if m }.compact.uniq; end
760 def author_names_and_newness_for_thread t
762 authors = t.map do |m, *o|
766 if AccountManager.is_account?(m.from)
768 elsif t.authors.size == 1
774 new[name] ||= m.has_label?(:unread)
778 authors.compact.uniq.map { |a| [a, new[a]] }
781 def text_for_thread_at line
782 t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
784 date = t.date.to_nice_s
786 starred = t.has_label? :starred
788 ## format the from column
790 ann = author_names_and_newness_for_thread t
792 ann.each_with_index do |(name, newness), i|
793 break if cur_width >= from_width
794 last = i == ann.length - 1
797 if cur_width + name.display_length > from_width
798 name[0 ... (from_width - cur_width - 1)] + "."
799 elsif cur_width + name.display_length == from_width
800 name[0 ... (from_width - cur_width)]
803 name[0 ... (from_width - cur_width)]
805 name[0 ... (from_width - cur_width - 1)] + ","
809 cur_width += abbrev.display_length
811 if last && from_width > cur_width
812 abbrev += " " * (from_width - cur_width)
815 from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
818 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
819 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
822 if t.has_label?(:draft)
824 elsif t.has_label?(:unread)
832 snippet = t.snippet + (t.snippet.empty? ? "" : "...")
834 size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
837 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
838 [:none, sprintf("%#{@date_width}s", date)],
839 (starred ? [:starred_color, "*"] : [:none, " "]),
843 [subj_color, size_widget_text],
844 [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
845 [:to_me_color, dp ? ">" : (p ? '+' : " ")],
847 (t.labels - @hidden_labels).map { |label| [:label_color, "#{label} "] } +
849 [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
850 [:snippet_color, snippet],
854 def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
858 def default_size_widget_for t
868 [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
871 def initialize_threads
872 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
873 @ts_mutex = Mutex.new