]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/thread-index-mode.rb
Merge branch 'master' into next
[sup] / lib / sup / modes / thread-index-mode.rb
index b44dc1889f623e30b32ca095cf3c0249cf6b0494..82f258b689681183614b65d6475829ba10d0563e 100644 (file)
@@ -1,3 +1,5 @@
+require 'set'
+
 module Redwood
 
 ## subclasses should implement:
@@ -38,6 +40,7 @@ EOS
     k.add :save, "Save changes now", '$'
     k.add :jump_to_next_new, "Jump to next new thread", :tab
     k.add :reply, "Reply to latest message in a thread", 'r'
+    k.add :reply_all, "Reply to all participants of the latest message in a thread", 'G'
     k.add :forward, "Forward latest message in a thread", 'f'
     k.add :toggle_tagged, "Tag/untag selected thread", 't'
     k.add :toggle_tagged_all, "Tag/untag all threads", 'T'
@@ -74,8 +77,7 @@ EOS
     @last_load_more_size = nil
     to_load_more do |size|
       next if @last_load_more_size == 0
-      load_threads :num => 1, :background => false
-      load_threads :num => (size - 1),
+      load_threads :num => size,
                    :when_done => lambda { |num| @last_load_more_size = num }
     end
   end
@@ -109,7 +111,7 @@ EOS
       mode = ThreadViewMode.new t, @hidden_labels, self
       BufferManager.spawn t.subj, mode
       BufferManager.draw_screen
-      mode.jump_to_first_open true
+      mode.jump_to_first_open
       BufferManager.draw_screen # lame TODO: make this unnecessary
       ## the first draw_screen is needed before topline and botline
       ## are set, and the second to show the cursor having moved
@@ -222,7 +224,7 @@ EOS
       ## let's see you do THIS in python
       @threads = @ts.threads.select { |t| !@hidden_threads[t] }.sort_by { |t| [t.date, t.first.id] }.reverse
       @size_widgets = @threads.map { |t| size_widget_for_thread t }
-      @size_widget_width = @size_widgets.max_of { |w| w.length }
+      @size_widget_width = @size_widgets.max_of { |w| w.display_length }
     end
 
     regen_text
@@ -239,129 +241,122 @@ EOS
     end
   end
 
+  ## returns an undo lambda
   def actually_toggle_starred t
-    thread = t # cargo cult programming
     pos = curpos
     if t.has_label? :starred # if ANY message has a star
-      undo = lambda {
-        thread.first.add_label :starred
-        update_text_for_line pos
-        UpdateManager.relay self, :starred, thread.first
-      }
       t.remove_label :starred # remove from all
       UpdateManager.relay self, :unstarred, t.first
+      lambda do
+        t.first.add_label :starred
+        UpdateManager.relay self, :starred, t.first
+        regen_text
+      end
     else
-      undo = lambda {
-        thread.remove_label :starred
-        update_text_for_line pos
-        UpdateManager.relay self, :unstarred, thread.first
-      }
       t.first.add_label :starred # add only to first
       UpdateManager.relay self, :starred, t.first
+      lambda do
+        t.remove_label :starred
+        UpdateManager.relay self, :unstarred, t.first
+        regen_text
+      end
     end
-
-    return undo
   end  
 
   def toggle_starred 
     t = cursor_thread or return
     undo = actually_toggle_starred t
-    UndoManager.register("starring/unstarring thread #{t.first.id}",undo)
+    UndoManager.register "toggling thread starred status", undo
     update_text_for_line curpos
     cursor_down
   end
 
   def multi_toggle_starred threads
-    undo = threads.map { |t| actually_toggle_starred t }
-    UndoManager.register("starring/unstarring #{threads.size} #{threads.size.pluralize 'thread'}",
-                         undo)
+    UndoManager.register "toggling #{threads.size.pluralize 'thread'} starred status",
+      threads.map { |t| actually_toggle_starred t }
     regen_text
   end
 
+  ## returns an undo lambda
   def actually_toggle_archived t
     thread = t
     pos = curpos
     if t.has_label? :inbox
       t.remove_label :inbox
-      undo = lambda {
+      UpdateManager.relay self, :archived, t.first
+      lambda do
         thread.apply_label :inbox
         update_text_for_line pos
         UpdateManager.relay self,:unarchived, thread.first
-      }
-      UpdateManager.relay self, :archived, t.first
+      end
     else
       t.apply_label :inbox
-      undo = lambda {
+      UpdateManager.relay self, :unarchived, t.first
+      lambda do
         thread.remove_label :inbox
         update_text_for_line pos
         UpdateManager.relay self, :unarchived, thread.first
-      }
-      UpdateManager.relay self, :unarchived, t.first
+      end
     end
-
-    return undo
   end
 
+  ## returns an undo lambda
   def actually_toggle_spammed t
     thread = t
     if t.has_label? :spam
-      undo = lambda {
-        thread.apply_label :spam
-        self.hide_thread thread
-        UpdateManager.relay self,:spammed, thread.first
-      }
       t.remove_label :spam
       add_or_unhide t.first
       UpdateManager.relay self, :unspammed, t.first
+      lambda do
+        thread.apply_label :spam
+        self.hide_thread thread
+        UpdateManager.relay self,:spammed, thread.first
+      end
     else
-      undo = lambda {
-        thread.remove_label :spam
-        add_or_unhide thread.first
-        UpdateManager.relay self,:unspammed, thread.first
-      }
       t.apply_label :spam
       hide_thread t
       UpdateManager.relay self, :spammed, t.first
+      lambda do
+        thread.remove_label :spam
+        add_or_unhide thread.first
+        UpdateManager.relay self,:unspammed, thread.first
+      end
     end
-
-    return undo
   end
 
+  ## returns an undo lambda
   def actually_toggle_deleted t
     if t.has_label? :deleted
-      undo = lambda {
-        t.apply_label :deleted
-        hide_thread t
-        UpdateManager.relay self, :deleted, t.first
-      }
       t.remove_label :deleted
       add_or_unhide t.first
       UpdateManager.relay self, :undeleted, t.first
+      lambda do
+        t.apply_label :deleted
+        hide_thread t
+        UpdateManager.relay self, :deleted, t.first
+      end
     else
-      undo = lambda {
+      t.apply_label :deleted
+      hide_thread t
+      UpdateManager.relay self, :deleted, t.first
+      lambda do
         t.remove_label :deleted
         add_or_unhide t.first
         UpdateManager.relay self, :undeleted, t.first
-      }
-      t.apply_label :deleted
-  hide_thread t
-      UpdateManager.relay self, :deleted, t.first
+      end
     end
-
-    return undo
   end
 
   def toggle_archived 
     t = cursor_thread or return
-    undo = [actually_toggle_archived(t), lambda {self.update_text_for_line curpos}]
-    UndoManager.register("deleting/undeleting thread #{t.first.id}",undo)
+    undo = actually_toggle_archived t
+    UndoManager.register "deleting/undeleting thread #{t.first.id}", undo, lambda { update_text_for_line curpos }
     update_text_for_line curpos
   end
 
   def multi_toggle_archived threads
-    undo = threads.map { |t| actually_toggle_archived t}
-    UndoManager.register("deleting/undeleting #{threads.size} #{threads.size.pluralize 'thread'}",
-                         undo << lambda {self.regen_text})
+    undos = threads.map { |t| actually_toggle_archived t }
+    UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}", undos, lambda { regen_text }
     regen_text
   end
 
@@ -422,9 +417,9 @@ EOS
   ## see deleted or spam emails, and when you undelete or unspam them
   ## you also want them to disappear immediately.
   def multi_toggle_spam threads
-    undo = threads.map{ |t| actually_toggle_spammed t}
-    UndoManager.register("marking/unmarking #{threads.size} #{threads.size.pluralize 'thread'} as spam",
-                         undo <<  lambda {self.regen_text})
+    undos = threads.map { |t| actually_toggle_spammed t }
+    UndoManager.register "marking/unmarking  #{threads.size.pluralize 'thread'} as spam",
+                         undos, lambda { regen_text }
     regen_text
   end
 
@@ -435,9 +430,9 @@ EOS
 
   ## see comment for multi_toggle_spam
   def multi_toggle_deleted threads
-    undo = threads.map{ |t| actually_toggle_deleted t}
-    UndoManager.register("deleting/undeleting #{threads.size} #{threads.size.pluralize 'thread'}",
-                         undo << lambda {regen_text})
+    undos = threads.map { |t| actually_toggle_deleted t }
+    UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}",
+                         undos, lambda { regen_text }
     regen_text
   end
 
@@ -448,18 +443,21 @@ EOS
 
   ## m-m-m-m-MULTI-KILL
   def multi_kill threads
-    undo = threads.map do |t|
+    UndoManager.register "killing #{threads.size.pluralize 'thread'}" do
+      threads.each do |t|
+        t.remove_label :killed
+        add_or_unhide t.first
+      end
+      regen_text
+    end
+
+    threads.each do |t|
       t.apply_label :killed
       hide_thread t
-      thread = t
-      lambda { thread.remove_label :killed
-        add_or_unhide thread.first
-      }
     end
-    UndoManager.register("killing #{threads.size} #{threads.size.pluralize 'thread'}",
-                         undo << lambda {regen_text})
+
     regen_text
-    BufferManager.flash "#{threads.size.pluralize 'Thread'} killed."
+    BufferManager.flash "#{threads.size.pluralize 'thread'} killed."
   end
 
   def save background=true
@@ -479,7 +477,7 @@ EOS
       BufferManager.say("Saving threads...") do |say_id|
         dirty_threads.each_with_index do |t, i|
           BufferManager.say "Saving modified thread #{i + 1} of #{dirty_threads.length}...", say_id
-          t.save Index
+          t.save_state Index
         end
       end
     end
@@ -535,19 +533,17 @@ EOS
     keepl, modifyl = thread.labels.partition { |t| speciall.member? t }
 
     user_labels = BufferManager.ask_for_labels :label, "Labels for thread: ", modifyl, @hidden_labels
-
     return unless user_labels
-    thread.labels = keepl + user_labels
+
+    thread.labels = Set.new(keepl) + user_labels
     user_labels.each { |l| LabelManager << l }
     update_text_for_line curpos
 
-    undo = lambda{
+    UndoManager.register "labeling thread" do
       thread.labels = old_labels
       update_text_for_line pos
       UpdateManager.relay self, :labeled, thread.first
-    }
-
-    UndoManager.register("labeling thread #{thread.first.id}", undo)
+    end
 
     UpdateManager.relay self, :labeled, thread.first
   end
@@ -558,41 +554,47 @@ EOS
 
     user_labels.map! { |l| (l.to_s =~ /^-/)? [l.to_s.gsub(/^-?/, '').to_sym, true] : [l, false] }
     hl = user_labels.select { |(l,_)| @hidden_labels.member? l }
-    if hl.empty?
-      undo = threads.map do |t|
-        old_labels = t.labels
-        user_labels.each do |(l, to_remove)|
-          if to_remove
-            t.remove_label l
-          else
-            t.apply_label l
-          end
-        end
-        ## UpdateManager or some other regresh mechanism?
-        UpdateManager.relay self, :labeled, t.first
-        lambda do
-          t.labels = old_labels
-          UpdateManager.relay self, :labeled, t.first
+    unless hl.empty?
+      BufferManager.flash "'#{hl}' is a reserved label!"
+      return
+    end
+
+    old_labels = threads.map { |t| t.labels.dup }
+
+    threads.each do |t|
+      user_labels.each do |(l, to_remove)|
+        if to_remove
+          t.remove_label l
+        else
+          t.apply_label l
+          LabelManager << l
         end
       end
-      user_labels.each { |(l,_)| LabelManager << l }
-      UndoManager.register("labeling #{threads.size} #{threads.size.pluralize 'thread'}",
-                          undo << lambda { regen_text})
-    else
-      BufferManager.flash "'#{hl}' is a reserved label!"
+      UpdateManager.relay self, :labeled, t.first
     end
+
     regen_text
+
+    UndoManager.register "labeling #{threads.size.pluralize 'thread'}" do
+      threads.zip(old_labels).map do |t, old_labels|
+        t.labels = old_labels
+        UpdateManager.relay self, :labeled, t.first
+      end
+      regen_text
+    end
   end
 
-  def reply
+  def reply type_arg=nil
     t = cursor_thread or return
     m = t.latest_message
     return if m.nil? # probably won't happen
     m.load_from_source!
-    mode = ReplyMode.new m
+    mode = ReplyMode.new m, type_arg
     BufferManager.spawn "Reply to #{m.subj}", mode
   end
 
+  def reply_all; reply :all; end
+
   def forward
     t = cursor_thread or return
     m = t.latest_message
@@ -627,6 +629,7 @@ EOS
         BufferManager.draw_screen
         last_update = Time.now
       end
+      ::Thread.pass
       break if @interrupt_search
     end
     @ts.threads.each { |th| th.labels.each { |l| LabelManager << l } }
@@ -759,10 +762,12 @@ protected
   
   def authors; map { |m, *o| m.from if m }.compact.uniq; end
 
-  def author_names_and_newness_for_thread t
+  def author_names_and_newness_for_thread t, limit=nil
     new = {}
-    authors = t.map do |m, *o|
+    authors = Set.new
+    t.each do |m, *o|
       next unless m
+      break if limit and authors.size >= limit
 
       name = 
         if AccountManager.is_account?(m.from)
@@ -774,31 +779,32 @@ protected
         end
 
       new[name] ||= m.has_label?(:unread)
-      name
+      authors << name
     end
 
-    authors.compact.uniq.map { |a| [a, new[a]] }
+    authors.to_a.map { |a| [a, new[a]] }
   end
 
+  AUTHOR_LIMIT = 5
   def text_for_thread_at line
     t, size_widget = @mutex.synchronize { [@threads[line], @size_widgets[line]] }
 
     date = t.date.to_nice_s
 
-    starred = t.has_label?(:starred)
+    starred = t.has_label? :starred
 
     ## format the from column
     cur_width = 0
-    ann = author_names_and_newness_for_thread t
+    ann = author_names_and_newness_for_thread t, AUTHOR_LIMIT
     from = []
     ann.each_with_index do |(name, newness), i|
       break if cur_width >= from_width
       last = i == ann.length - 1
 
       abbrev =
-        if cur_width + name.length > from_width
+        if cur_width + name.display_length > from_width
           name[0 ... (from_width - cur_width - 1)] + "."
-        elsif cur_width + name.length == from_width
+        elsif cur_width + name.display_length == from_width
           name[0 ... (from_width - cur_width)]
         else
           if last
@@ -808,7 +814,7 @@ protected
           end
         end
 
-      cur_width += abbrev.length
+      cur_width += abbrev.display_length
 
       if last && from_width > cur_width
         abbrev += " " * (from_width - cur_width)
@@ -845,10 +851,11 @@ protected
       [subj_color, size_widget_text],
       [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
       [:to_me_color, dp ? ">" : (p ? '+' : " ")],
-      [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
     ] +
-      (t.labels - @hidden_labels).map { |label| [:label_color, "+#{label} "] } +
-      [[:snippet_color, snippet]
+      (t.labels - @hidden_labels).map { |label| [:label_color, "#{label} "] } +
+      [
+      [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
+      [:snippet_color, snippet],
     ]
   end