3 ## subclasses should implement:
6 class ThreadIndexMode < LineCursorMode
7 DATE_WIDTH = Time::TO_NICE_S_MAX_LEN
9 LOAD_MORE_THREAD_NUM = 20
11 register_keymap do |k|
12 k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
13 k.add :reload, "Refresh view", '@'
14 k.add :toggle_archived, "Toggle archived status", 'a'
15 k.add :toggle_starred, "Star or unstar all messages in thread", '*'
16 k.add :toggle_new, "Toggle new/read status of all messages in thread", 'N'
17 k.add :edit_labels, "Edit or add labels for a thread", 'l'
18 k.add :edit_message, "Edit message (drafts only)", 'e'
19 k.add :toggle_spam, "Mark/unmark thread as spam", 'S'
20 k.add :toggle_deleted, "Delete/undelete thread", 'd'
21 k.add :kill, "Kill thread (never to be seen in inbox again)", '&'
22 k.add :save, "Save changes now", '$'
23 k.add :jump_to_next_new, "Jump to next new thread", :tab
24 k.add :reply, "Reply to latest message in a thread", 'r'
25 k.add :forward, "Forward latest message in a thread", 'f'
26 k.add :toggle_tagged, "Tag/untag current line", 't'
27 k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
30 def initialize hidden_labels=[], load_thread_opts={}
34 @load_thread_opts = load_thread_opts
35 @hidden_labels = hidden_labels + LabelManager::HIDDEN_RESERVED_LABELS
36 @date_width = DATE_WIDTH
39 @tags = Tagger.new self
44 UpdateManager.register self
46 @last_load_more_size = nil
47 to_load_more do |size|
48 next if @last_load_more_size == 0
49 load_threads :num => 1, :background => false
50 load_threads :num => (size - 1),
51 :when_done => lambda { |num| @last_load_more_size = num }
55 def lines; @text.length; end
56 def [] i; @text[i]; end
57 def contains_thread? t; !@lines[t].nil?; end
61 BufferManager.draw_screen
62 load_threads :num => buffer.content_height
65 ## open up a thread view window
67 t ||= @threads[curpos] or return
69 ## TODO: don't regen text completely
70 Redwood::reporting_thread do
71 BufferManager.say("Loading message bodies...") do |sid|
72 t.each { |m, *o| m.load_from_source! if m }
74 mode = ThreadViewMode.new t, @hidden_labels
75 BufferManager.spawn t.subj, mode
76 BufferManager.draw_screen
77 mode.jump_to_first_open
78 BufferManager.draw_screen # lame TODO: make this unnecessary
79 ## the first draw_screen is needed before topline and botline
80 ## are set, and the second to show the cursor having moved
82 update_text_for_line curpos
83 UpdateManager.relay self, :read, t
87 def multi_select threads
88 threads.each { |t| select t }
91 def handle_label_update sender, m
92 t = @ts.thread_for(m) or return
93 l = @lines[t] or return
94 update_text_for_line l
95 BufferManager.draw_screen
98 def handle_read_update sender, t
99 l = @lines[t] or return
100 update_text_for_line @lines[t]
101 BufferManager.draw_screen
104 def handle_archived_update *a; handle_read_update(*a); end
106 def handle_deleted_update sender, t
107 handle_read_update sender, t
113 def is_relevant? m; false; end
115 def handle_add_update sender, m
116 if is_relevant?(m) || @ts.is_relevant?(m)
117 @ts.load_thread_for_message m
119 BufferManager.draw_screen
123 def handle_delete_update sender, mid
124 if @ts.contains_id? mid
127 BufferManager.draw_screen
132 ## let's see you do THIS in python
133 @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| t.date }.reverse
134 @size_width = (@threads.max_of { |t| t.size } || 0).num_digits
139 return unless(t = @threads[curpos])
140 message, *crap = t.find { |m, *o| m.has_label? :draft }
142 mode = ResumeMode.new message
143 BufferManager.spawn "Edit message", mode
145 BufferManager.flash "Not a draft message!"
149 def actually_toggle_starred t
150 if t.has_label? :starred # if ANY message has a star
151 t.remove_label :starred # remove from all
152 UpdateManager.relay self, :unstarred, t
154 t.first.add_label :starred # add only to first
155 UpdateManager.relay self, :starred, t
160 t = @threads[curpos] or return
161 actually_toggle_starred t
162 update_text_for_line curpos
166 def multi_toggle_starred threads
167 threads.each { |t| actually_toggle_starred t }
171 def actually_toggle_archived t
172 if t.has_label? :inbox
173 t.remove_label :inbox
174 UpdateManager.relay self, :archived, t
177 UpdateManager.relay self, :unarchived, t
181 def actually_toggle_spammed t
182 if t.has_label? :spam
184 UpdateManager.relay self, :unspammed, t
187 UpdateManager.relay self, :spammed, t
191 def actually_toggle_deleted t
192 if t.has_label? :deleted
193 t.remove_label :deleted
194 UpdateManager.relay self, :undeleted, t
196 t.apply_label :deleted
197 UpdateManager.relay self, :deleted, t
202 t = @threads[curpos] or return
203 actually_toggle_archived t
204 update_text_for_line curpos
207 def multi_toggle_archived threads
208 threads.each { |t| actually_toggle_archived t }
213 t = @threads[curpos] or return
214 t.toggle_label :unread
215 update_text_for_line curpos
219 def multi_toggle_new threads
220 threads.each { |t| t.toggle_label :unread }
224 def multi_toggle_tagged threads
230 n = ((curpos + 1) ... lines).find { |i| @threads[i].has_label? :unread } || (0 ... curpos).find { |i| @threads[i].has_label? :unread }
232 ## jump there if necessary
233 jump_to_line n unless n >= topline && n < botline
236 BufferManager.flash "No new messages"
241 t = @threads[curpos] or return
242 multi_toggle_spam [t]
245 ## both spam and deleted have the curious characteristic that you
246 ## always want to hide the thread after either applying or removing
247 ## that label. in all thread-index-views except for
248 ## label-search-results-mode, when you mark a message as spam or
249 ## deleted, you want it to disappear immediately; in LSRM, you only
250 ## see deleted or spam emails, and when you undelete or unspam them
251 ## you also want them to disappear immediately.
252 def multi_toggle_spam threads
254 actually_toggle_spammed t
261 t = @threads[curpos] or return
262 multi_toggle_deleted [t]
265 ## see comment for multi_toggle_spam
266 def multi_toggle_deleted threads
268 actually_toggle_deleted t
275 t = @threads[curpos] or return
279 def multi_kill threads
281 t.apply_label :killed
285 BufferManager.flash "Thread#{threads.size == 1 ? '' : 's'} killed."
289 dirty_threads = (@threads + @hidden_threads.keys).select { |t| t.dirty? }
290 return if dirty_threads.empty?
292 BufferManager.say("Saving threads...") do |say_id|
293 dirty_threads.each_with_index do |t, i|
294 BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
301 UpdateManager.unregister self
305 BufferManager.clear @mbid if @mbid
306 sleep 0.1 # TODO: necessary?
307 BufferManager.erase_flash
314 t = @threads[curpos] or return
315 @tags.toggle_tag_for t
316 update_text_for_line curpos
320 def apply_to_tagged; @tags.apply_to_tagged; end
323 thread = @threads[curpos] or return
324 speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
325 keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
327 user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
329 return unless user_labels
330 thread.labels = keepl + user_labels
331 user_labels.each { |l| LabelManager << l }
332 update_text_for_line curpos
335 def multi_edit_labels threads
336 answer = BufferManager.ask :add_labels, "add labels: "
338 user_labels = answer.split(/\s+/).map { |l| l.intern }
340 hl = user_labels.select { |l| @hidden_labels.member? l }
342 threads.each { |t| user_labels.each { |l| t.apply_label l } }
343 user_labels.each { |l| LabelManager << l }
345 BufferManager.flash "'#{hl}' is a reserved label!"
351 t = @threads[curpos] or return
353 return if m.nil? # probably won't happen
355 mode = ReplyMode.new m
356 BufferManager.spawn "Reply to #{m.subj}", mode
360 t = @threads[curpos] or return
362 return if m.nil? # probably won't happen
364 mode = ForwardMode.new m
365 BufferManager.spawn "Forward of #{m.subj}", mode
369 def load_n_threads_background n=LOAD_MORE_THREAD_NUM, opts={}
370 return if @load_thread # todo: wrap in mutex
371 @load_thread = Redwood::reporting_thread do
372 num = load_n_threads n, opts
373 opts[:when_done].call(num) if opts[:when_done]
378 def load_n_threads n=LOAD_MORE_THREAD_NUM, opts={}
379 @mbid = BufferManager.say "Searching for threads..."
381 last_update = Time.now - 9999 # oh yeah
382 @ts.load_n_threads(@ts.size + n, opts) do |i|
383 BufferManager.say "Loaded #{i} threads...", @mbid
384 if (Time.now - last_update) >= 0.25
386 BufferManager.draw_screen
387 last_update = Time.now
390 @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
393 BufferManager.clear @mbid
395 BufferManager.draw_screen
398 synchronized :load_n_threads
404 "line #{curpos + 1} of #{l} #{dirty? ? '*modified*' : ''}"
408 def load_threads opts={}
409 n = opts[:num] || ThreadIndexMode::LOAD_MORE_THREAD_NUM
411 myopts = @load_thread_opts.merge({ :when_done => (lambda do |num|
412 opts[:when_done].call(num) if opts[:when_done]
414 BufferManager.flash "Found #{num} threads."
416 BufferManager.flash "No matches."
420 if opts[:background] || opts[:background].nil?
421 load_n_threads_background n, myopts
423 load_n_threads n, myopts
427 def resize rows, cols
434 def cursor_thread; @threads[curpos]; end
443 raise "already hidden" if @hidden_threads[t]
444 @hidden_threads[t] = true
450 if @hidden_threads[t]
451 @hidden_threads.delete t
458 def update_text_for_line l
459 return unless l # not sure why this happens, but it does, occasionally
460 @text[l] = text_for_thread @threads[l]
461 buffer.mark_dirty if buffer
465 @text = @threads.map_with_index { |t, i| text_for_thread t }
466 @lines = @threads.map_with_index { |t, i| [t, i] }.to_h
467 buffer.mark_dirty if buffer
470 def authors; map { |m, *o| m.from if m }.compact.uniq; end
472 def author_names_and_newness_for_thread t
474 authors = t.map do |m, *o|
478 if AccountManager.is_account?(m.from)
480 elsif t.authors.size == 1
486 new[name] ||= m.has_label?(:unread)
490 authors.compact.uniq.map { |a| [a, new[a]] }
493 def text_for_thread t
494 date = t.date.to_nice_s
496 new = t.has_label?(:unread)
497 starred = t.has_label?(:starred)
499 ## format the from column
501 ann = author_names_and_newness_for_thread t
503 ann.each_with_index do |(name, newness), i|
504 break if cur_width >= from_width
505 last = i == ann.length - 1
508 if cur_width + name.length > from_width
509 name[0 ... (from_width - cur_width - 1)] + "."
510 elsif cur_width + name.length == from_width
511 name[0 ... (from_width - cur_width)]
514 name[0 ... (from_width - cur_width)]
516 name[0 ... (from_width - cur_width - 1)] + ","
520 cur_width += abbrev.length
522 if last && from_width > cur_width
523 abbrev += " " * (from_width - cur_width)
526 from << [(newness ? :index_new_color : (starred ? :index_starred_color : :index_old_color)), abbrev]
529 dp = t.direct_participants.any? { |p| AccountManager.is_account? p }
530 p = dp || t.participants.any? { |p| AccountManager.is_account? p }
542 [:tagged_color, @tags.tagged?(t) ? ">" : " "],
543 [:none, sprintf("%#{@date_width}s", date)],
544 (starred ? [:starred_color, "*"] : [:none, " "]),
548 [:none, t.size == 1 ? " " * (@size_width + 2) : sprintf("(%#{@size_width}d)", t.size)],
549 [:to_me_color, dp ? " >" : (p ? ' +' : " ")],
550 [base_color, t.subj + (t.subj.empty? ? "" : " ")],
552 (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
553 [[:snippet_color, t.snippet]
558 def dirty?; (@hidden_threads.keys + @threads).any? { |t| t.dirty? }; end
563 [(buffer.content_width.to_f * 0.2).to_i, MIN_FROM_WIDTH].max
566 def initialize_threads
567 @ts = ThreadSet.new Index.instance, $config[:thread_by_subject]
568 @ts_mutex = Mutex.new