]> git.cworth.org Git - sup/blobdiff - lib/sup/modes/inbox-mode.rb
Allow thread index view to sort oldest first
[sup] / lib / sup / modes / inbox-mode.rb
index 21eb9ac43bff5cbd01fb0e10807b61381aabf1bd..f347ffb9cc19f0f1f812f50a2d8ccb263de5f494 100644 (file)
@@ -1,4 +1,4 @@
-require 'thread'
+require 'sup'
 
 module Redwood
 
@@ -7,16 +7,30 @@ class InboxMode < ThreadIndexMode
     ## overwrite toggle_archived with archive
     k.add :archive, "Archive thread (remove from inbox)", 'a'
     k.add :read_and_archive, "Archive thread (remove from inbox) and mark read", 'A'
+    k.add :refine_search, "Refine search", '|'
+  end
+
+  def self.newest_first
+    if !$config[:inbox_newest_first].nil?
+      $config[:inbox_newest_first]
+    else
+      true
+    end
   end
 
   def initialize
     super [:inbox, :sent, :draft], { :label => :inbox, :skip_killed => true }
     raise "can't have more than one!" if defined? @@instance
     @@instance = self
+    @newest_first = InboxMode.newest_first
   end
 
-  def is_relevant? m
-    m.has_label?(:inbox) && ([:spam, :deleted, :killed] & m.labels).empty?
+  def is_relevant? m; (m.labels & [:spam, :deleted, :killed, :inbox]) == Set.new([:inbox]) end
+
+  def refine_search
+    text = BufferManager.ask :search, "refine query: ", "label:inbox AND "
+    return unless text && text !~ /^\s*$/
+    SearchResultsMode.spawn_from_query text, @newest_first
   end
 
   ## label-list-mode wants to be able to raise us if the user selects
@@ -28,11 +42,10 @@ class InboxMode < ThreadIndexMode
     return unless cursor_thread
     thread = cursor_thread # to make sure lambda only knows about 'old' cursor_thread
 
-    undo = lambda {
+    UndoManager.register "archiving thread" do
       thread.apply_label :inbox
       add_or_unhide thread.first
-    }
-    UndoManager.register("archiving thread #{thread.first.id}", undo)
+    end
 
     cursor_thread.remove_label :inbox
     hide_thread cursor_thread
@@ -40,13 +53,13 @@ class InboxMode < ThreadIndexMode
   end
 
   def multi_archive threads
-    undo = threads.map {|t|
-             lambda{
-               t.apply_label :inbox
-               add_or_unhide t.first
-             }}
-    UndoManager.register("archiving #{threads.size} #{threads.size.pluralize 'thread'}",
-                         undo << lambda {regen_text} )
+    UndoManager.register "archiving #{threads.size.pluralize 'thread'}" do
+      threads.map do |t|
+        t.apply_label :inbox
+        add_or_unhide t.first
+      end
+      regen_text
+    end
 
     threads.each do |t|
       t.remove_label :inbox
@@ -59,12 +72,11 @@ class InboxMode < ThreadIndexMode
     return unless cursor_thread
     thread = cursor_thread # to make sure lambda only knows about 'old' cursor_thread
 
-    undo = lambda {
+    UndoManager.register "reading and archiving thread" do
       thread.apply_label :inbox
       thread.apply_label :unread
       add_or_unhide thread.first
-    }
-    UndoManager.register("reading and archiving thread ", undo)
+    end
 
     cursor_thread.remove_label :unread
     cursor_thread.remove_label :inbox
@@ -73,15 +85,7 @@ class InboxMode < ThreadIndexMode
   end
 
   def multi_read_and_archive threads
-    undo = threads.map {|t|
-      lambda {
-        t.apply_label :inbox
-        t.apply_label :unread
-        add_or_unhide t.first
-      }
-    }
-    UndoManager.register("reading and archiving #{threads.size} #{threads.size.pluralize 'thread'}",
-                         undo << lambda {regen_text})
+    old_labels = threads.map { |t| t.labels.dup }
 
     threads.each do |t|
       t.remove_label :unread
@@ -89,6 +93,15 @@ class InboxMode < ThreadIndexMode
       hide_thread t
     end
     regen_text
+
+    UndoManager.register "reading and archiving #{threads.size.pluralize 'thread'}" do
+      threads.zip(old_labels).each do |t, l|
+        t.labels = l
+        add_or_unhide t.first
+      end
+      regen_text
+    end
+
   end
 
   def handle_unarchived_update sender, m