Archive of RubyForge sup-devel mailing list
 help / color / mirror / Atom feed
* [sup-devel] [PATCH 1/3] make mode keymap easily accessible to hooks
@ 2010-01-25  3:09 Rich Lane
  2010-01-25  3:09 ` [sup-devel] [PATCH 2/3] add keymap rebinding support Rich Lane
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Lane @ 2010-01-25  3:09 UTC (permalink / raw)
  To: sup-devel

---
 lib/sup/mode.rb |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/lib/sup/mode.rb b/lib/sup/mode.rb
index c8ad0cc..b9e0698 100644
--- a/lib/sup/mode.rb
+++ b/lib/sup/mode.rb
@@ -10,6 +10,10 @@ class Mode
     @@keymaps[self] = keymap
   end
 
+  def self.keymap
+    @@keymaps[self] || register_keymap
+  end
+
   def initialize
     @buffer = nil
   end
-- 
1.6.3.3

_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [sup-devel] [PATCH 2/3] add keymap rebinding support
  2010-01-25  3:09 [sup-devel] [PATCH 1/3] make mode keymap easily accessible to hooks Rich Lane
@ 2010-01-25  3:09 ` Rich Lane
  2010-01-25  3:09   ` [sup-devel] [PATCH 3/3] keybindings hook Rich Lane
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Lane @ 2010-01-25  3:09 UTC (permalink / raw)
  To: sup-devel

Keymap#delete removes an existing keybinding and Keymap#add! deletes existing
bindings to the given keys before adding new ones. Keymap#add_multi now allows
adding new bindings to an existing submap.
---
 lib/sup/keymap.rb |   27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/lib/sup/keymap.rb b/lib/sup/keymap.rb
index cb039e4..4cdeeed 100644
--- a/lib/sup/keymap.rb
+++ b/lib/sup/keymap.rb
@@ -60,10 +60,31 @@ class Keymap
     end
   end
 
+  def delete k
+    kc = Keymap.keysym_to_keycode(k)
+    return unless @map.member? kc
+    entry = @map.delete kc
+    keys = entry[2]
+    keys.delete k
+    @order.delete entry if keys.empty?
+  end
+
+  def add! action, help, *keys
+    keys.each { |k| delete k }
+    add action, help, *keys
+  end
+
   def add_multi prompt, key
-    submap = Keymap.new
-    add submap, prompt, key
-    yield submap
+    kc = Keymap.keysym_to_keycode(key)
+    if @map.member? kc
+      action = @map[kc].first
+      raise "existing action is not a keymap" unless action.is_a?(Keymap)
+      yield action
+    else
+      submap = Keymap.new
+      add submap, prompt, key
+      yield submap
+    end
   end
 
   def action_for kc
-- 
1.6.3.3

_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [sup-devel] [PATCH 3/3] keybindings hook
  2010-01-25  3:09 ` [sup-devel] [PATCH 2/3] add keymap rebinding support Rich Lane
@ 2010-01-25  3:09   ` Rich Lane
  2010-02-27  7:34     ` Rich Lane
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Lane @ 2010-01-25  3:09 UTC (permalink / raw)
  To: sup-devel

Add a hook specifically for custom keybindings. The advantage of this over the
startup hook is that we can assume it's safe to reload this one while Sup is
running. The "modes" variable provides a debatably-useful mapping between
user-visible mode names and the actual classes, for use with class_eval.
---
 bin/sup           |    9 +++++++++
 lib/sup/hook.rb   |    1 +
 lib/sup/keymap.rb |   17 +++++++++++++++++
 lib/sup/mode.rb   |    4 ++++
 4 files changed, 31 insertions(+), 0 deletions(-)

diff --git a/bin/sup b/bin/sup
index 7824aca..ad1271f 100755
--- a/bin/sup
+++ b/bin/sup
@@ -83,8 +83,13 @@ global_keymap = Keymap.new do |k|
   k.add :recall_draft, "Edit most recent draft message", 'R'
   k.add :show_inbox, "Show the Inbox buffer", 'I'
   k.add :show_console, "Show the Console buffer", '~'
+  k.add_multi "Rerun (k)eybindings hook:", 'O' do |kk|
+    kk.add :run_keybindings_hook, "Rerun keybindings hook", 'k'
+  end
 end
 
+Redwood::Keymap.run_hook global_keymap
+
 ## the following magic enables wide characters when used with a ruby
 ## ncurses.so that's been compiled against libncursesw. (note the w.) why
 ## this works, i have no idea. much like pretty much every aspect of
@@ -339,6 +344,10 @@ begin
     when :show_console
       b, new = bm.spawn_unless_exists("Console", :system => true) { ConsoleMode.new }
       b.mode.run
+    when :run_keybindings_hook
+      HookManager.clear_one 'keybindings'
+      Keymap.run_hook global_keymap
+      bm.flash "keybindings hook run"
     when :nothing, InputSequenceAborted
     when :redraw
       bm.completely_redraw_screen
diff --git a/lib/sup/hook.rb b/lib/sup/hook.rb
index 3bf9823..08738cd 100644
--- a/lib/sup/hook.rb
+++ b/lib/sup/hook.rb
@@ -113,6 +113,7 @@ EOS
   def enabled? name; !hook_for(name).nil? end
 
   def clear; @hooks.clear; end
+  def clear_one k; @hooks.delete k; end
 
 private
 
diff --git a/lib/sup/keymap.rb b/lib/sup/keymap.rb
index 4cdeeed..93060b8 100644
--- a/lib/sup/keymap.rb
+++ b/lib/sup/keymap.rb
@@ -1,6 +1,14 @@
 module Redwood
 
 class Keymap
+
+  HookManager.register "keybindings", <<EOS
+Add custom keybindings.
+Methods:
+  modes: Hash from mode names to mode classes.
+  global_keymap: The top-level keymap.
+EOS
+
   def initialize
     @map = {}
     @order = []
@@ -116,6 +124,15 @@ class Keymap
     llen = lines.max_of { |a, b| a.length }
     lines.map { |a, b| sprintf " %#{llen}s : %s", a, b }.join("\n")
   end
+
+  def self.run_hook global_keymap
+    modes = Hash[Mode.keymaps.map { |klass,keymap| [Mode.make_name(klass.name),klass] }]
+    locals = {
+      :modes => modes,
+      :global_keymap => global_keymap,
+    }
+    HookManager.run 'keybindings', locals
+  end
 end
 
 end
diff --git a/lib/sup/mode.rb b/lib/sup/mode.rb
index b9e0698..f5aee1c 100644
--- a/lib/sup/mode.rb
+++ b/lib/sup/mode.rb
@@ -14,6 +14,10 @@ class Mode
     @@keymaps[self] || register_keymap
   end
 
+  def self.keymaps
+    @@keymaps
+  end
+
   def initialize
     @buffer = nil
   end
-- 
1.6.3.3

_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [sup-devel] [PATCH 3/3] keybindings hook
  2010-01-25  3:09   ` [sup-devel] [PATCH 3/3] keybindings hook Rich Lane
@ 2010-02-27  7:34     ` Rich Lane
  0 siblings, 0 replies; 4+ messages in thread
From: Rich Lane @ 2010-02-27  7:34 UTC (permalink / raw)
  To: sup-devel

Branch keybindings, merged into next.
_______________________________________________
Sup-devel mailing list
Sup-devel@rubyforge.org
http://rubyforge.org/mailman/listinfo/sup-devel


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-02-27  7:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-01-25  3:09 [sup-devel] [PATCH 1/3] make mode keymap easily accessible to hooks Rich Lane
2010-01-25  3:09 ` [sup-devel] [PATCH 2/3] add keymap rebinding support Rich Lane
2010-01-25  3:09   ` [sup-devel] [PATCH 3/3] keybindings hook Rich Lane
2010-02-27  7:34     ` Rich Lane

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox