]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/thread-index-mode.rb
maintain labels as Sets rather than arrays
[sup] / lib / sup / modes / thread-index-mode.rb
index 7a0b8150c455cc4df19cdb581e3ae9144cacd1e0..905ad982034155d7e582b6a08239a63bd440ecb2 100644 (file)
@@ -1,3 +1,5 @@
+require 'set'
+
 module Redwood
 
 ## subclasses should implement:
@@ -14,6 +16,12 @@ Variables:
   thread: The message thread to be formatted.
 EOS
 
+  HookManager.register "mark-as-spam", <<EOS
+This hook is run when a thread is marked as spam
+Variables:
+  thread: The message thread being marked as spam.
+EOS
+
   register_keymap do |k|
     k.add :load_threads, "Load #{LOAD_MORE_THREAD_NUM} more threads", 'M'
     k.add_multi "Load all threads (! to confirm) :", '!' do |kk|
@@ -36,8 +44,9 @@ EOS
     k.add :toggle_tagged, "Tag/untag selected thread", 't'
     k.add :toggle_tagged_all, "Tag/untag all threads", 'T'
     k.add :tag_matching, "Tag matching threads", 'g'
-    k.add :apply_to_tagged, "Apply next command to all tagged threads", ';'
+    k.add :apply_to_tagged, "Apply next command to all tagged threads", '+', '='
     k.add :join_threads, "Force tagged threads to be joined into the same thread", '#'
+    k.add :undo, "Undo the previous action", 'u'
   end
 
   def initialize hidden_labels=[], load_thread_opts={}
@@ -62,6 +71,8 @@ EOS
 
     UpdateManager.register self
 
+    @save_thread_mutex = Mutex.new
+
     @last_load_more_size = nil
     to_load_more do |size|
       next if @last_load_more_size == 0
@@ -71,12 +82,14 @@ EOS
     end
   end
 
+  def unsaved?; dirty? end
   def lines; @text.length; end
   def [] i; @text[i]; end
   def contains_thread? t; @threads.include?(t) end
 
   def reload
     drop_all_threads
+    UndoManager.clear
     BufferManager.draw_screen
     load_threads :num => buffer.content_height
   end
@@ -202,12 +215,16 @@ EOS
     add_or_unhide m
   end
 
+  def undo
+    UndoManager.undo
+  end
+
   def update
     @mutex.synchronize do
       ## 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
@@ -224,66 +241,122 @@ EOS
     end
   end
 
+  ## returns an undo lambda
   def actually_toggle_starred t
+    pos = curpos
     if t.has_label? :starred # if ANY message has a star
       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
       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
   end  
 
   def toggle_starred 
     t = cursor_thread or return
-    actually_toggle_starred t
+    undo = actually_toggle_starred t
+    UndoManager.register "toggling thread starred status", undo
     update_text_for_line curpos
     cursor_down
   end
 
   def multi_toggle_starred threads
-    threads.each { |t| actually_toggle_starred t }
+    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
       UpdateManager.relay self, :archived, t.first
+      lambda do
+        thread.apply_label :inbox
+        update_text_for_line pos
+        UpdateManager.relay self,:unarchived, thread.first
+      end
     else
       t.apply_label :inbox
       UpdateManager.relay self, :unarchived, t.first
+      lambda do
+        thread.remove_label :inbox
+        update_text_for_line pos
+        UpdateManager.relay self, :unarchived, thread.first
+      end
     end
   end
 
+  ## returns an undo lambda
   def actually_toggle_spammed t
+    thread = t
     if t.has_label? :spam
       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
       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
   end
 
+  ## returns an undo lambda
   def actually_toggle_deleted t
     if t.has_label? :deleted
       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
       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
+      end
     end
   end
 
   def toggle_archived 
     t = cursor_thread or return
-    actually_toggle_archived t
+    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
-    threads.each { |t| actually_toggle_archived t }
+    undos = threads.map { |t| actually_toggle_archived t }
+    UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}", undos, lambda { regen_text }
     regen_text
   end
 
@@ -333,6 +406,7 @@ EOS
   def toggle_spam
     t = cursor_thread or return
     multi_toggle_spam [t]
+    HookManager.run("mark-as-spam", :thread => t)
   end
 
   ## both spam and deleted have the curious characteristic that you
@@ -343,10 +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
-    threads.each do |t|
-      actually_toggle_spammed t
-      hide_thread t 
-    end
+    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
 
@@ -357,10 +430,9 @@ EOS
 
   ## see comment for multi_toggle_spam
   def multi_toggle_deleted threads
-    threads.each do |t|
-      actually_toggle_deleted t
-      hide_thread t 
-    end
+    undos = threads.map { |t| actually_toggle_deleted t }
+    UndoManager.register "deleting/undeleting #{threads.size.pluralize 'thread'}",
+                         undos, lambda { regen_text }
     regen_text
   end
 
@@ -369,23 +441,44 @@ EOS
     multi_kill [t]
   end
 
+  ## m-m-m-m-MULTI-KILL
   def multi_kill threads
+    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
     end
+
     regen_text
-    BufferManager.flash "#{threads.size.pluralize 'Thread'} killed."
+    BufferManager.flash "#{threads.size.pluralize 'thread'} killed."
+  end
+
+  def save background=true
+    if background
+      Redwood::reporting_thread("saving thread") { actually_save }
+    else
+      actually_save
+    end
   end
 
-  def save
-    dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
-    return if dirty_threads.empty?
+  def actually_save
+    @save_thread_mutex.synchronize do
+      BufferManager.say("Saving contacts...") { ContactManager.instance.save }
+      dirty_threads = @mutex.synchronize { (@threads + @hidden_threads.keys).select { |t| t.dirty? } }
+      next if dirty_threads.empty?
 
-    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
+      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
+        end
       end
     end
   end
@@ -399,7 +492,7 @@ EOS
       sleep 0.1 # TODO: necessary?
       BufferManager.erase_flash
     end
-    save
+    save false
     super
   end
 
@@ -416,9 +509,14 @@ EOS
   end
 
   def tag_matching
-    query = BufferManager.ask :search, "tag threads matching: "
+    query = BufferManager.ask :search, "tag threads matching (regex): "
     return if query.nil? || query.empty?
-    query = /#{query}/i
+    query = begin
+      /#{query}/i
+    rescue RegexpError => e
+      BufferManager.flash "error interpreting '#{query}': #{e.message}"
+      return
+    end
     @mutex.synchronize { @threads.each { |t| @tags.tag t if thread_matches?(t, query) } }
     regen_text
   end
@@ -428,30 +526,62 @@ EOS
   def edit_labels
     thread = cursor_thread or return
     speciall = (@hidden_labels + LabelManager::RESERVED_LABELS).uniq
+
+    old_labels = thread.labels
+    pos = curpos
+
     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
+
+    UndoManager.register "labeling thread" do
+      thread.labels = old_labels
+      update_text_for_line pos
+      UpdateManager.relay self, :labeled, thread.first
+    end
+
     UpdateManager.relay self, :labeled, thread.first
   end
 
   def multi_edit_labels threads
-    answer = BufferManager.ask :add_labels, "add labels: "
-    return unless answer
-    user_labels = answer.split(/\s+/).map { |l| l.intern }
-    
-    hl = user_labels.select { |l| @hidden_labels.member? l }
-    if hl.empty?
-      threads.each { |t| user_labels.each { |l| t.apply_label l } }
-      user_labels.each { |l| LabelManager << l }
-    else
+    user_labels = BufferManager.ask_for_labels :labels, "Add/remove labels (use -label to remove): ", [], @hidden_labels
+    return unless user_labels
+
+    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 }
+    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
+      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
@@ -560,7 +690,7 @@ protected
   def add_or_unhide m
     @ts_mutex.synchronize do
       if (is_relevant?(m) || @ts.is_relevant?(m)) && !@ts.contains?(m)
-        @ts.load_thread_for_message m
+        @ts.load_thread_for_message m, @load_thread_opts
       end
 
       @hidden_threads.delete @ts.thread_for(m)
@@ -629,10 +759,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)
@@ -644,31 +776,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
@@ -678,7 +811,7 @@ protected
           end
         end
 
-      cur_width += abbrev.length
+      cur_width += abbrev.display_length
 
       if last && from_width > cur_width
         abbrev += " " * (from_width - cur_width)
@@ -713,13 +846,14 @@ protected
       from +
       [
       [subj_color, size_widget_text],
-      [:to_me_color, dp ? " >" : (p ? ' +' : "  ")],
-      [subj_color, t.subj + (t.subj.empty? ? "" : " ")],
+      [:to_me_color, t.labels.member?(:attachment) ? "@" : " "],
+      [:to_me_color, dp ? ">" : (p ? '+' : " ")],
     ] +
-      (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
 
   def dirty?; @mutex.synchronize { (@hidden_threads.keys + @threads).any? { |t| t.dirty? } } end