3 ## subclasses should implement:
6 class ThreadIndexMode < LineCursorMode
7 include CanSpawnForwardMode
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 register_keymap do |k|
20 k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
21 k.add :reload, "Refresh view", '@'
22 k.add :toggle_archived, "Toggle archived status", 'a'
23 k.add :toggle_starred, "Star or unstar all messages in thread", '*'
24 k.add :toggle_new, "Toggle new/read status of all messages in thread", 'N'
25 k.add :edit_labels, "Edit or add labels for a thread", 'l'
26 k.add :edit_message, "Edit message (drafts only)", 'e'
27 k.add :toggle_spam, "Mark/unmark thread as spam", 'S'
28 k.add :toggle_deleted, "Delete/undelete thread", 'd'
29 k.add :kill, "Kill thread (never to be seen in inbox again)", '&'
30 k.add :save, "Save changes now", '$'
31 k.add :jump_to_next_new, "Jump to next new thread", :tab
32 k.add :reply, "Reply to latest message in a thread", 'r'
33 k.add :forward, "Forward latest message in a thread", 'f'
34 k.add :toggle_tagged, "Tag/untag selected thread", 't'
35 k.add :toggle_tagged_all, "Tag/untag all threads", 'T'
36 k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
39 def initialize hidden_labels=[], load_thread_opts={}
41 @mutex = Mutex.new # covers the following variables
44 @size_widget_width = nil
46 @tags = Tagger.new self
49 @load_thread_opts = load_thread_opts
50 @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
51 @date_width = DATE_WIDTH
53 initialize_threads # defines @ts and @ts_mutex
56 UpdateManager.register self
58 @last_load_more_size = nil
59 to_load_more do |size|
60 next if @last_load_more_size == 0
61 load_threads :num => 1, :background => false
62 load_threads :num => (size - 1),
63 :when_done => lambda { |num| @last_load_more_size = num }
67 def lines; @text.length; end
68 def [] i; @text[i]; end
69 #def contains_thread? t; !@lines[t].nil?; end
70 def contains_thread? t; @threads.contains?(t) end
74 BufferManager.draw_screen
75 load_threads :num => buffer.content_height
78 ## open up a thread view window
80 t ||= cursor_thread or return
82 ## TODO: don't regen text completely
83 Redwood::reporting_thread do
84 BufferManager.say("Loading message bodies...") do |sid|
85 t.each { |m, *o| m.load_from_source! if m }
87 mode = ThreadViewMode.new t, @hidden_labels
88 BufferManager.spawn t.subj, mode
89 BufferManager.draw_screen
90 mode.jump_to_first_open
91 BufferManager.draw_screen # lame TODO: make this unnecessary
92 ## the first draw_screen is needed before topline and botline
93 ## are set, and the second to show the cursor having moved
95 update_text_for_line curpos
96 UpdateManager.relay self, :read, t
100 def multi_select threads
101 threads.each { |t| select t }
104 def handle_label_update sender, m
105 t = @ts_mutex.synchronize { @ts.thread_for(m) } or return
106 handle_label_thread_update sender, t
109 def handle_label_thread_update sender, t
110 l = @lines[t] or return
111 update_text_for_line l
112 BufferManager.draw_screen
115 def handle_read_update sender, t
116 l = @lines[t] or return
117 update_text_for_line l
118 BufferManager.draw_screen
121 def handle_archived_update *a; handle_read_update(*a); end
123 def handle_deleted_update sender, t
124 handle_read_update sender, t
130 def is_relevant? m; false; end
132 def handle_add_update sender, m
133 @ts_mutex.synchronize do
134 return unless is_relevant?(m) || @ts.is_relevant?(m)
135 @ts.load_thread_for_message m
138 BufferManager.draw_screen
141 def handle_delete_update sender, mid
142 @ts_mutex.synchronize do
143 return unless @ts.contains_id? mid
147 BufferManager.draw_screen
151 @mutex.synchronize do
152 ## let's see you do THIS in python
153 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
154 @size_widgets = @threads.map { |t| size_widget_for_thread t }
155 @size_widget_width = @size_widgets.max_of { |w| w.length }
162 return unless(t = cursor_thread)
163 message, *crap = t.find { |m, *o| m.has_label? :draft }
165 mode = ResumeMode.new message
166 BufferManager.spawn "Edit message", mode
168 BufferManager.flash "Not a draft message!"
172 def actually_toggle_starred t
173 if t.has_label? :starred # if ANY message has a star
174 t.remove_label :starred # remove from all
175 UpdateManager.relay self, :unstarred, t
177 t.first.add_label :starred # add only to first
178 UpdateManager.relay self, :starred, t
183 t = cursor_thread or return
184 actually_toggle_starred t
185 update_text_for_line curpos
189 def multi_toggle_starred threads
190 threads.each { |t| actually_toggle_starred t }
194 def actually_toggle_archived t
195 if t.has_label? :inbox
196 t.remove_label :inbox
197 UpdateManager.relay self, :archived, t
200 UpdateManager.relay self, :unarchived, t
204 def actually_toggle_spammed t
205 if t.has_label? :spam
207 UpdateManager.relay self, :unspammed, t
210 UpdateManager.relay self, :spammed, t
214 def actually_toggle_deleted t
215 if t.has_label? :deleted
216 t.remove_label :deleted
217 UpdateManager.relay self, :undeleted, t
219 t.apply_label :deleted
220 UpdateManager.relay self, :deleted, t
225 t = cursor_thread or return
226 actually_toggle_archived t
227 update_text_for_line curpos
230 def multi_toggle_archived threads
231 threads.each { |t| actually_toggle_archived t }
236 t = cursor_thread or return
237 t.toggle_label :unread
238 update_text_for_line curpos
242 def multi_toggle_new threads
243 threads.each { |t| t.toggle_label :unread }
247 def multi_toggle_tagged threads
248 @mutex.synchronize { @tags.drop_all_tags }
253 n = @mutex.synchronize do
254 ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } ||
255 (0 ... curpos).find { |i| @threads[i].has_label? :unread }
258 ## jump there if necessary
259 jump_to_line n unless n >= topline && n < botline
262 BufferManager.flash "No new messages"
267 t = cursor_thread or return
268 multi_toggle_spam [t]
271 ## both spam and deleted have the curious characteristic that you
272 ## always want to hide the thread after either applying or removing
273 ## that label. in all thread-index-views except for
274 ## label-search-results-mode, when you mark a message as spam or
275 ## deleted, you want it to disappear immediately; in LSRM, you only
276 ## see deleted or spam emails, and when you undelete or unspam them
277 ## you also want them to disappear immediately.
278 def multi_toggle_spam threads
280 actually_toggle_spammed t
287 t = cursor_thread or return
288 multi_toggle_deleted [t]
291 ## see comment for multi_toggle_spam
292 def multi_toggle_deleted threads
294 actually_toggle_deleted t
301 t = cursor_thread or return
305 def multi_kill threads
307 t.apply_label :killed
311 BufferManager.flash "Thread#{threads.size == 1 ? '' : 's'} killed."
315 dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
316 return if dirty_threads.empty?
318 BufferManager.say("Saving threads...") do |say_id|
319 dirty_threads.each_with_index do |t, i|
320 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
327 UpdateManager.unregister self
331 BufferManager.clear @mbid if @mbid
332 sleep 0.1 # TODO: necessary?
333 BufferManager.erase_flash
340 t = cursor_thread or return
341 @mutex.synchronize { @tags.toggle_tag_for t }
342 update_text_for_line curpos
346 def toggle_tagged_all
347 @mutex.synchronize { @threads.each { |t| @tags.toggle_tag_for t } }
351 def apply_to_tagged; @tags.apply_to_tagged; end
354 thread = cursor_thread or return
355 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
356 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
358 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
360 return unless user_labels
361 thread.labels = keepl + user_labels
362 user_labels.each { |l| LabelManager << l }
363 update_text_for_line curpos
366 def multi_edit_labels threads
367 answer = BufferManager.ask :add_labels, "add labels: "
369 user_labels = answer.split(/\s+/).map { |l| l.intern }
371 hl = user_labels.select { |l| @hidden_labels.member? l }
373 threads.each { |t| user_labels.each { |l| t.apply_label l } }
374 user_labels.each { |l| LabelManager << l }
376 BufferManager.flash "'#{hl}' is a reserved label!"
382 t = cursor_thread or return
384 return if m.nil? # probably won't happen
386 mode = ReplyMode.new m
387 BufferManager.spawn "Reply to #{m.subj}", mode
391 t = cursor_thread or return
393 return if m.nil? # probably won't happen
398 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
399 return if @load_thread # todo: wrap in mutex
400 @load_thread = Redwood::reporting_thread do
401 num = load_n_threads n, opts
402 opts[:when_done].call(num) if opts[:when_done]
407 ## TODO: figure out @ts_mutex in this method
408 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
409 @mbid = BufferManager.say "Searching for threads..."
411 last_update = Time.now
412 @ts.load_n_threads(@ts.size + n, opts) do |i|
413 if (Time.now - last_update) >= 0.25
414 BufferManager.say "Loaded #{i} threads...", @mbid
416 BufferManager.draw_screen
417 last_update = Time.now
420 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
423 BufferManager.clear @mbid
425 BufferManager.draw_screen
428 ignore_concurrent_calls :load_n_threads
434 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
438 def load_threads opts={}
439 n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
441 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
442 opts[:when_done].call(num) if opts[:when_done]
444 BufferManager.flash "Found #{num} threads."
446 BufferManager.flash "No matches."
450 if opts[:background] || opts[:background].nil?
451 load_n_threads_background n, myopts
453 load_n_threads n, myopts
456 ignore_concurrent_calls :load_threads
458 def resize rows, cols
465 def size_widget_for_thread t
466 HookManager.run("index-mode-size-widget", :thread => t) || default_size_widget_for(t)
469 def cursor_thread; @mutex.synchronize { @threads[curpos] }; end
478 @mutex.synchronize do
479 raise "already hidden" if @hidden_threads[t]
480 @hidden_threads[t] = true
483 @size_widgets.delete_at i
488 def update_text_for_line l
489 return unless l # not sure why this happens, but it does, occasionally
493 @mutex.synchronize do
494 @size_widgets[l] = size_widget_for_thread @threads[l]
496 ## if the widget size has increased, we need to redraw everyone
497 need_update = @size_widgets[l].size > @size_widget_width
503 @text[l] = text_for_thread_at l
504 buffer.mark_dirty if buffer
509 threads = @mutex.synchronize { @threads }
510 @text = threads.map_with_index { |t, i| text_for_thread_at i }
511 @lines = threads.map_with_index { |t, i| [t, i] }.to_h
512 buffer.mark_dirty if buffer
515 def authors; map { |m, *o| m.from if m }.compact.uniq; end
517 def author_names_and_newness_for_thread t
519 authors = t.map do |m, *o|
523 if AccountManager.is_account?(m.from)
525 elsif t.authors.size == 1
531 new[name] ||= m.has_label?(:unread)
535 authors.compact.uniq.map { |a| [a, new[a]] }
538 def text_for_thread_at line
539 t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
541 date = t.date.to_nice_s
543 new = t.has_label?(:unread)
544 starred = t.has_label?(:starred)
546 ## format the from column
548 ann = author_names_and_newness_for_thread t
550 ann.each_with_index do |(name, newness), i|
551 break if cur_width >= from_width
552 last = i == ann.length - 1
555 if cur_width + name.length > from_width
556 name[0 ... (from_width - cur_width - 1)] + "."
557 elsif cur_width + name.length == from_width
558 name[0 ... (from_width - cur_width)]
561 name[0 ... (from_width - cur_width)]
563 name[0 ... (from_width - cur_width - 1)] + ","
567 cur_width += abbrev.length
569 if last && from_width > cur_width
570 abbrev += " " * (from_width - cur_width)
573 from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
576 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
577 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
588 snippet = t.snippet + (t.snippet.empty? ? "" : "...")
590 size_widget_text = sprintf "%#{ @size_widget_width}s", size_widget
593 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
594 [:none, sprintf("%#{@date_width}s", date)],
595 (starred ? [:starred_color, "*"] : [:none, " "]),
599 [subj_color, size_widget_text],
600 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
601 [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
603 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
604 [[:snippet_color, snippet]
609 def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end
613 def default_size_widget_for t
623 [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
626 def initialize_threads
627 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
628 @ts_mutex = Mutex.new