]> git.cworth.org Git - sup/commitdiff
Added UndoManager class
authorMike Stipicevic <stipim@rpi.edu>
Mon, 16 Feb 2009 05:39:50 +0000 (00:39 -0500)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 16 Mar 2009 11:59:59 +0000 (07:59 -0400)
The UndoManager keeps a list of lambdas that undo actions. It's designed to be used by keypress hooks. It is initialized in the main sup thread along with UpdateManager, etc.

lib/sup.rb
lib/sup/modes/thread-index-mode.rb
lib/sup/undo.rb [new file with mode: 0644]

index 93369a5f62e01933491cd4aaa352013d673fd7ef..eda673b94b76bb24f660c0300aaf335a5c3726f4 100644 (file)
@@ -125,6 +125,7 @@ module Redwood
     Redwood::PollManager.new
     Redwood::SuicideManager.new Redwood::SUICIDE_FN
     Redwood::CryptoManager.new
+    Redwood::UndoManager.new
   end
 
   def finish
@@ -281,6 +282,7 @@ require "sup/tagger"
 require "sup/draft"
 require "sup/poll"
 require "sup/crypto"
+require "sup/undo"
 require "sup/horizontal-selector"
 require "sup/modes/line-cursor-mode"
 require "sup/modes/help-mode"
index 4de4613655e1b9c5c8d1f1caff5ad3a6b0f473b4..ee30284f01f130161ca9777edacb7b68b5169408 100644 (file)
@@ -44,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={}
@@ -83,6 +84,7 @@ EOS
 
   def reload
     drop_all_threads
+    UndoManager.clear
     BufferManager.draw_screen
     load_threads :num => buffer.content_height
   end
@@ -208,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
diff --git a/lib/sup/undo.rb b/lib/sup/undo.rb
new file mode 100644 (file)
index 0000000..250433d
--- /dev/null
@@ -0,0 +1,42 @@
+module Redwood
+
+## Implements a single undo list for the Sup instance
+##
+## The basic idea is to keep a list of lambdas to undo
+## things. When an action is called (such as 'archive'),
+## a lambda is registered with UndoManager that will
+## undo the archival action
+
+class UndoManager
+  include Singleton
+
+  def initialize
+    @@actionlist = []
+    self.class.i_am_the_instance self
+  end
+
+  def register desc, actions
+    actions = [actions] unless actions.is_a?Array
+    raise StandardError, "when would I need to undo 'nothing?'" unless actions.length > 0
+    Redwood::log "registering #{actions.length} actions: #{desc}"
+    @@actionlist.push({:desc => desc, :actions => actions})
+  end
+
+  def undo
+    unless @@actionlist.length == 0 then
+      actionset = @@actionlist.pop
+      Redwood::log "undoing #{actionset[:desc]}..."
+      actionset[:actions].each{|action|
+        action.call
+      }
+      BufferManager.flash "undid #{actionset[:desc]}"
+    else
+      BufferManager.flash "nothing more to undo"
+    end
+  end
+
+  def clear
+    @@actionlist = []
+  end
+end
+end