]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/thread-index-mode.rb
Added undo for spam
[sup] / lib / sup / modes / thread-index-mode.rb
index 6a6d4571aabd37680a2b053304d40a568fe1eed4..1e02b5b0c8681159ced1f1c54dcd109b256b1138 100644 (file)
@@ -14,6 +14,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|
@@ -38,6 +44,7 @@ EOS
     k.add :tag_matching, "Tag matching threads", 'g'
     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={}
@@ -77,6 +84,7 @@ EOS
 
   def reload
     drop_all_threads
+    UndoManager.clear
     BufferManager.draw_screen
     load_threads :num => buffer.content_height
   end
@@ -113,19 +121,32 @@ EOS
     threads.each { |t| select t }
   end
 
-  ## this is called by thread-view-modes when the user wants to view
-  ## the next thread without going to index-mode. we update the cursor
-  ## as a convenience.
+  ## these two methods are called by thread-view-modes when the user
+  ## wants to view the previous/next thread without going back to
+  ## index-mode. we update the cursor as a convenience.
   def launch_next_thread_after thread, &b
+    launch_another_thread thread, 1, &b
+  end
+
+  def launch_prev_thread_before thread, &b
+    launch_another_thread thread, -1, &b
+  end
+
+  def launch_another_thread thread, direction, &b
     l = @lines[thread] or return
+    target_l = l + direction
     t = @mutex.synchronize do
-      if l < @threads.length - 1
-        set_cursor_pos l + 1 # move out of mutex?
-        @threads[l + 1]
+      if target_l >= 0 && target_l < @threads.length
+        @threads[target_l]
       end
-    end or return
+    end
 
-    select t, b
+    if t # there's a next thread
+      set_cursor_pos target_l # move out of mutex?
+      select t, b
+    elsif b # no next thread. call the block anyways
+      b.call
+    end
   end
   
   def handle_single_message_labeled_update sender, m
@@ -172,10 +193,16 @@ EOS
   end
 
   def handle_deleted_update sender, m
-    @ts_mutex.synchronize do
-      return unless @ts.contains? m
-      @ts.remove_thread_containing_id m.id
-    end
+    t = @ts_mutex.synchronize { @ts.thread_for m }
+    return unless t
+    hide_thread t
+    update
+  end
+
+  def handle_spammed_update sender, m
+    t = @ts_mutex.synchronize { @ts.thread_for m }
+    return unless t
+    hide_thread t
     update
   end
 
@@ -183,6 +210,10 @@ EOS
     add_or_unhide m
   end
 
+  def undo
+    UndoManager.undo
+  end
+
   def update
     @mutex.synchronize do
       ## let's see you do THIS in python
@@ -206,45 +237,91 @@ EOS
   end
 
   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
     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
     end
+
+    return undo
   end  
 
   def toggle_starred 
     t = cursor_thread or return
-    actually_toggle_starred t
+    undo = actually_toggle_starred t
+    UndoManager.register("starring/unstarring thread #{t.first.id}",undo)
     update_text_for_line curpos
     cursor_down
   end
 
   def multi_toggle_starred threads
-    threads.each { |t| actually_toggle_starred t }
+    undo = threads.map { |t| actually_toggle_starred t }
+    UndoManager.register("starring/unstarring #{threads.size} #{threads.size.pluralize 'thread'}",
+                         undo)
     regen_text
   end
 
   def actually_toggle_archived t
+    thread = t
+    pos = curpos
     if t.has_label? :inbox
       t.remove_label :inbox
+      undo = lambda {
+        thread.apply_label :inbox
+        update_text_for_line pos
+        UpdateManager.relay self,:unarchived, thread.first
+      }
       UpdateManager.relay self, :archived, t.first
     else
       t.apply_label :inbox
+      undo = lambda {
+        thread.remove_label :inbox
+        update_text_for_line pos
+        UpdateManager.relay self, :unarchived, thread.first
+      }
       UpdateManager.relay self, :unarchived, t.first
     end
+
+    return undo
   end
 
   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
     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
     end
+
+    return undo
   end
 
   def actually_toggle_deleted t
@@ -259,12 +336,15 @@ EOS
 
   def toggle_archived 
     t = cursor_thread or return
-    actually_toggle_archived t
+    undo = [actually_toggle_archived(t), lambda {self.update_text_for_line curpos}]
+    UndoManager.register("deleting/undeleting thread #{t.first.id}",undo)
     update_text_for_line curpos
   end
 
   def multi_toggle_archived threads
-    threads.each { |t| actually_toggle_archived t }
+    undo = threads.map { |t| actually_toggle_archived t}
+    UndoManager.register("deleting/undeleting #{threads.size} #{threads.size.pluralize 'thread'}",
+                         undo << lambda {self.regen_text})
     regen_text
   end
 
@@ -314,6 +394,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
@@ -324,10 +405,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
+    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})
     regen_text
   end
 
@@ -421,9 +501,8 @@ EOS
   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 }
+    user_labels = BufferManager.ask_for_labels :add_labels, "Add labels: ", [], @hidden_labels
+    return unless user_labels
     
     hl = user_labels.select { |l| @hidden_labels.member? l }
     if hl.empty?
@@ -636,7 +715,6 @@ protected
 
     date = t.date.to_nice_s
 
-    new = t.has_label?(:unread)
     starred = t.has_label?(:starred)
 
     ## format the from column
@@ -673,7 +751,9 @@ protected
     p = dp || t.participants.any? { |p| AccountManager.is_account? p }
 
     subj_color =
-      if new
+      if t.has_label?(:draft)
+        :index_draft_color
+      elsif t.has_label?(:unread)
         :index_new_color
       elsif starred
         :index_starred_color
@@ -693,7 +773,8 @@ protected
       from +
       [
       [subj_color, size_widget_text],
-      [:to_me_color, dp ? " >" : (p ? ' +' : "  ")],
+      [: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} "] } +