5 ## subclasses should implement:
8 class ThreadIndexMode < LineCursorMode
9 DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
11 LOAD_MORE_THREAD_NUM = 20
13 HookManager.register "index-mode-size-widget", <<EOS
14 Generates the per-thread size widget for each thread.
16 thread: The message thread to be formatted.
19 HookManager.register "mark-as-spam", <<EOS
20 This hook is run when a thread is marked as spam
22 thread: The message thread being marked as spam.
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)", '!'
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'
52 def initialize hidden_labels=[], load_thread_opts={}
54 @mutex = Mutex.new # covers the following variables:
57 @size_widget_width = nil
59 @tags = Tagger.new self
61 ## these guys, and @text and @lines, are not covered
63 @load_thread_opts = load_thread_opts
64 @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
65 @date_width = DATE_WIDTH
67 @interrupt_search = false
69 initialize_threads # defines @ts and @ts_mutex
70 update # defines @text and @lines
72 UpdateManager.register self
74 @save_thread_mutex = Mutex.new
76 @last_load_more_size = nil
77 to_load_more do |size|
78 next if @last_load_more_size == 0
79 load_threads :num => size,
80 :when_done => lambda { |num| @last_load_more_size = num }
84 def unsaved?; dirty? end
85 def lines; @text.length; end
86 def [] i; @text[i]; end
87 def contains_thread? t; @threads.include?(t) end
92 BufferManager.draw_screen
93 load_threads :num => buffer.content_height
96 ## open up a thread view window
97 def select t=nil, when_done=nil
98 t ||= cursor_thread or return
100 Redwood::reporting_thread("load messages for thread-view-mode") do
102 message = "Loading #{num.pluralize 'message body'}..."
103 BufferManager.say(message) do |sid|
104 t.each_with_index do |(m, *o), i|
106 BufferManager.say "#{message} (#{i}/#{num})", sid if t.size > 1
110 mode = ThreadViewMode.new t, @hidden_labels, self
111 BufferManager.spawn t.subj, mode
112 BufferManager.draw_screen
113 mode.jump_to_first_open true
114 BufferManager.draw_screen # lame TODO: make this unnecessary
115 ## the first draw_screen is needed before topline and botline
116 ## are set, and the second to show the cursor having moved
118 update_text_for_line curpos
119 UpdateManager.relay self, :read, t.first
120 when_done.call if when_done
124 def multi_select threads
125 threads.each { |t| select t }
128 ## these two methods are called by thread-view-modes when the user
129 ## wants to view the previous/next thread without going back to
130 ## index-mode. we update the cursor as a convenience.
131 def launch_next_thread_after thread, &b
132 launch_another_thread thread, 1, &b
135 def launch_prev_thread_before thread, &b
136 launch_another_thread thread, -1, &b
139 def launch_another_thread thread, direction, &b
140 l = @lines[thread] or return
141 target_l = l + direction
142 t = @mutex.synchronize do
143 if target_l >= 0 && target_l < @threads.length
148 if t # there's a next thread
149 set_cursor_pos target_l # move out of mutex?
151 elsif b # no next thread. call the block anyways
156 def handle_single_message_labeled_update sender, m
157 ## no need to do anything different here; we don't differentiate
158 ## messages from their containing threads
159 handle_labeled_update sender, m
162 def handle_labeled_update sender, m
163 if(t = thread_containing(m))
164 l = @lines[t] or return
165 update_text_for_line l
166 elsif is_relevant?(m)
171 def handle_simple_update sender, m
172 t = thread_containing(m) or return
173 l = @lines[t] or return
174 update_text_for_line l
177 %w(read unread archived starred unstarred).each do |state|
178 define_method "handle_#{state}_update" do |*a|
179 handle_simple_update(*a)
184 def is_relevant? m; false; end
186 def handle_added_update sender, m
188 BufferManager.draw_screen
191 def handle_single_message_deleted_update sender, m
192 @ts_mutex.synchronize do
193 return unless @ts.contains? m
199 def handle_deleted_update sender, m
200 t = @ts_mutex.synchronize { @ts.thread_for m }
206 def handle_spammed_update sender, m
207 t = @ts_mutex.synchronize { @ts.thread_for m }
213 def handle_undeleted_update sender, m
222 @mutex.synchronize do
223 ## let's see you do THIS in python
224 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| [t.date, t.first.id] }.reverse
225 @size_widgets = @threads.map { |t| size_widget_for_thread t }
226 @size_widget_width = @size_widgets.max_of { |w| w.display_length }
233 return unless(t = cursor_thread)
234 message, *crap = t.find { |m, *o| m.has_label? :draft }
236 mode = ResumeMode.new message
237 BufferManager.spawn "Edit message", mode
239 BufferManager.flash "Not a draft message!"
243 ## returns an undo lambda
244 def actually_toggle_starred t
246 if t.has_label? :starred # if ANY message has a star
247 t.remove_label :starred # remove from all
248 UpdateManager.relay self, :unstarred, t.first
250 t.first.add_label :starred
251 UpdateManager.relay self, :starred, t.first
255 t.first.add_label :starred # add only to first
256 UpdateManager.relay self, :starred, t.first
258 t.remove_label :starred
259 UpdateManager.relay self, :unstarred, t.first
266 t = cursor_thread or return
267 undo = actually_toggle_starred t
268 UndoManager.register "toggling thread starred status", undo
269 update_text_for_line curpos
273 def multi_toggle_starred threads
274 UndoManager.register "toggling #{threads.size.pluralize 'thread'} starred status",
275 threads.map { |t| actually_toggle_starred t }
279 ## returns an undo lambda
280 def actually_toggle_archived t
283 if t.has_label? :inbox
284 t.remove_label :inbox
285 UpdateManager.relay self, :archived, t.first
287 thread.apply_label :inbox
288 update_text_for_line pos
289 UpdateManager.relay self,:unarchived, thread.first
293 UpdateManager.relay self, :unarchived, t.first
295 thread.remove_label :inbox
296 update_text_for_line pos
297 UpdateManager.relay self, :unarchived, thread.first
302 ## returns an undo lambda
303 def actually_toggle_spammed t
305 if t.has_label? :spam
307 add_or_unhide t.first
308 UpdateManager.relay self, :unspammed, t.first
310 thread.apply_label :spam
311 self.hide_thread thread
312 UpdateManager.relay self,:spammed, thread.first
317 UpdateManager.relay self, :spammed, t.first
319 thread.remove_label :spam
320 add_or_unhide thread.first
321 UpdateManager.relay self,:unspammed, thread.first
326 ## returns an undo lambda
327 def actually_toggle_deleted t
328 if t.has_label? :deleted
329 t.remove_label :deleted
330 add_or_unhide t.first
331 UpdateManager.relay self, :undeleted, t.first
333 t.apply_label :deleted
335 UpdateManager.relay self, :deleted, t.first
338 t.apply_label :deleted
340 UpdateManager.relay self, :deleted, t.first
342 t.remove_label :deleted
343 add_or_unhide t.first
344 UpdateManager.relay self, :undeleted, t.first
350 t = cursor_thread or return
351 undo = actually_toggle_archived t
352 UndoManager.register "deleting/undeleting thread #{t.first.id}", undo, lambda { update_text_for_line curpos }
353 update_text_for_line curpos
356 def multi_toggle_archived threads
357 undos = threads.map { |t| actually_toggle_archived t }
358 UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}", undos, lambda { regen_text }
363 t = cursor_thread or return
364 t.toggle_label :unread
365 update_text_for_line curpos
369 def multi_toggle_new threads
370 threads.each { |t| t.toggle_label :unread }
374 def multi_toggle_tagged threads
375 @mutex.synchronize { @tags.drop_all_tags }
380 ## this command has no non-tagged form. as a convenience, allow this
381 ## command to be applied to tagged threads without hitting ';'.
382 @tags.apply_to_tagged :join_threads
385 def multi_join_threads threads
386 @ts.join_threads threads or return
387 @tags.drop_all_tags # otherwise we have tag pointers to invalid threads!
392 n = @mutex.synchronize do
393 ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } ||
394 (0 ... curpos).find { |i| @threads[i].has_label? :unread }
397 ## jump there if necessary
398 jump_to_line n unless n >= topline && n < botline
401 BufferManager.flash "No new messages"
406 t = cursor_thread or return
407 multi_toggle_spam [t]
408 HookManager.run("mark-as-spam", :thread => t)
411 ## both spam and deleted have the curious characteristic that you
412 ## always want to hide the thread after either applying or removing
413 ## that label. in all thread-index-views except for
414 ## label-search-results-mode, when you mark a message as spam or
415 ## deleted, you want it to disappear immediately; in LSRM, you only
416 ## see deleted or spam emails, and when you undelete or unspam them
417 ## you also want them to disappear immediately.
418 def multi_toggle_spam threads
419 undos = threads.map { |t| actually_toggle_spammed t }
420 UndoManager.register "marking/unmarking #{threads.size.pluralize 'thread'} as spam",
421 undos, lambda { regen_text }
426 t = cursor_thread or return
427 multi_toggle_deleted [t]
430 ## see comment for multi_toggle_spam
431 def multi_toggle_deleted threads
432 undos = threads.map { |t| actually_toggle_deleted t }
433 UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}",
434 undos, lambda { regen_text }
439 t = cursor_thread or return
443 ## m-m-m-m-MULTI-KILL
444 def multi_kill threads
445 UndoManager.register "killing #{threads.size.pluralize 'thread'}" do
447 t.remove_label :killed
448 add_or_unhide t.first
454 t.apply_label :killed
459 BufferManager.flash "#{threads.size.pluralize 'thread'} killed."
462 def save background=true
464 Redwood::reporting_thread("saving thread") { actually_save }
471 @save_thread_mutex.synchronize do
472 BufferManager.say("Saving contacts...") { ContactManager.instance.save }
473 dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
474 next if dirty_threads.empty?
476 BufferManager.say("Saving threads...") do |say_id|
477 dirty_threads.each_with_index do |t, i|
478 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
486 UpdateManager.unregister self
490 BufferManager.clear @mbid if @mbid
491 sleep 0.1 # TODO: necessary?
492 BufferManager.erase_flash
499 t = cursor_thread or return
500 @mutex.synchronize { @tags.toggle_tag_for t }
501 update_text_for_line curpos
505 def toggle_tagged_all
506 @mutex.synchronize { @threads.each { |t| @tags.toggle_tag_for t } }
511 query = BufferManager.ask :search, "tag threads matching (regex): "
512 return if query.nil? || query.empty?
515 rescue RegexpError => e
516 BufferManager.flash "error interpreting '#{query}': #{e.message}"
519 @mutex.synchronize { @threads.each { |t| @tags.tag t if thread_matches?(t, query) } }
523 def apply_to_tagged; @tags.apply_to_tagged; end
526 thread = cursor_thread or return
527 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
529 old_labels = thread.labels
532 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
534 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
535 return unless user_labels
537 thread.labels = Set.new(keepl) + user_labels
538 user_labels.each { |l| LabelManager << l }
539 update_text_for_line curpos
541 UndoManager.register "labeling thread" do
542 thread.labels = old_labels
543 update_text_for_line pos
544 UpdateManager.relay self, :labeled, thread.first
547 UpdateManager.relay self, :labeled, thread.first
550 def multi_edit_labels threads
551 user_labels = BufferManager.ask_for_labels :labels, "Add/remove labels (use -label to remove): ", [], @hidden_labels
552 return unless user_labels
554 user_labels.map! { |l| (l.to_s =~ /^-/)? [l.to_s.gsub(/^-?/, '').to_sym, true] : [l, false] }
555 hl = user_labels.select { |(l,_)| @hidden_labels.member? l }
557 BufferManager.flash "'#{hl}' is a reserved label!"
561 old_labels = threads.map { |t| t.labels.dup }
564 user_labels.each do |(l, to_remove)|
572 UpdateManager.relay self, :labeled, t.first
577 UndoManager.register "labeling #{threads.size.pluralize 'thread'}" do
578 threads.zip(old_labels).map do |t, old_labels|
579 t.labels = old_labels
580 UpdateManager.relay self, :labeled, t.first
587 t = cursor_thread or return
589 return if m.nil? # probably won't happen
591 mode = ReplyMode.new m
592 BufferManager.spawn "Reply to #{m.subj}", mode
596 t = cursor_thread or return
598 return if m.nil? # probably won't happen
600 ForwardMode.spawn_nicely :message => m
603 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
604 return if @load_thread # todo: wrap in mutex
605 @load_thread = Redwood::reporting_thread("load threads for thread-index-mode") do
606 num = load_n_threads n, opts
607 opts[:when_done].call(num) if opts[:when_done]
612 ## TODO: figure out @ts_mutex in this method
613 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
614 @interrupt_search = false
615 @mbid = BufferManager.say "Searching for threads..."
618 ts_to_load = ts_to_load + @ts.size unless n == -1 # -1 means all threads
621 last_update = Time.now
622 @ts.load_n_threads(ts_to_load, opts) do |i|
623 if (Time.now - last_update) >= 0.25
624 BufferManager.say "Loaded #{i.pluralize 'thread'}...", @mbid
626 BufferManager.draw_screen
627 last_update = Time.now
630 break if @interrupt_search
632 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
635 BufferManager.clear @mbid
637 BufferManager.draw_screen
640 ignore_concurrent_calls :load_n_threads
646 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
651 @interrupt_search = true
655 load_threads :num => -1
658 def load_threads opts={}
660 n = ThreadIndexMode::LOAD_MORE_THREAD_NUM
665 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
666 opts[:when_done].call(num) if opts[:when_done]
669 BufferManager.flash "Found #{num.pluralize 'thread'}."
671 BufferManager.flash "No matches."
675 if opts[:background] || opts[:background].nil?
676 load_n_threads_background n, myopts
678 load_n_threads n, myopts
681 ignore_concurrent_calls :load_threads
683 def resize rows, cols
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
696 @hidden_threads.delete @ts.thread_for(m)
702 def thread_containing m; @ts_mutex.synchronize { @ts.thread_for m } end
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 }
710 def size_widget_for_thread t
711 HookManager.run("index-mode-size-widget", :thread => t) || default_size_widget_for(t)
714 def cursor_thread; @mutex.synchronize { @threads[curpos] }; end
723 @mutex.synchronize do
724 i = @threads.index(t) or return
725 raise "already hidden" if @hidden_threads[t]
726 @hidden_threads[t] = true
728 @size_widgets.delete_at i
733 def update_text_for_line l
734 return unless l # not sure why this happens, but it does, occasionally
738 @mutex.synchronize do
739 @size_widgets[l] = size_widget_for_thread @threads[l]
741 ## if the widget size has increased, we need to redraw everyone
742 need_update = @size_widgets[l].size > @size_widget_width
748 @text[l] = text_for_thread_at l
749 buffer.mark_dirty if buffer
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
760 def authors; map { |m, *o| m.from if m }.compact.uniq; end
762 def author_names_and_newness_for_thread t, limit=nil
767 break if limit and authors.size >= limit
770 if AccountManager.is_account?(m.from)
772 elsif t.authors.size == 1
778 new[name] ||= m.has_label?(:unread)
782 authors.to_a.map { |a| [a, new[a]] }
786 def text_for_thread_at line
787 t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
789 date = t.date.to_nice_s
791 starred = t.has_label? :starred
793 ## format the from column
795 ann = author_names_and_newness_for_thread t, AUTHOR_LIMIT
797 ann.each_with_index do |(name, newness), i|
798 break if cur_width >= from_width
799 last = i == ann.length - 1
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)]
808 name[0 ... (from_width - cur_width)]
810 name[0 ... (from_width - cur_width - 1)] + ","
814 cur_width += abbrev.display_length
816 if last && from_width > cur_width
817 abbrev += " " * (from_width - cur_width)
820 from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
823 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
824 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
827 if t.has_label?(:draft)
829 elsif t.has_label?(:unread)
837 snippet = t.snippet + (t.snippet.empty? ? "" : "...")
839 size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
842 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
843 [:none, sprintf("%#{@date_width}s", date)],
844 (starred ? [:starred_color, "*"] : [:none, " "]),
848 [subj_color, size_widget_text],
849 [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
850 [:to_me_color, dp ? ">" : (p ? '+' : " ")],
852 (t.labels - @hidden_labels).map { |label| [:label_color, "#{label} "] } +
854 [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
855 [:snippet_color, snippet],
859 def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
863 def default_size_widget_for t
873 [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
876 def initialize_threads
877 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
878 @ts_mutex = Mutex.new