module Redwood class Keymap HookManager.register "keybindings", <" when :up then "" when :left then "" when :right then "" when :page_down then "" when :page_up then "" when :backspace then "" when :home then "" when :end then "" when :enter, :return then "" when :tab then "tab" when " " then "" else Curses::keyname(keysym_to_keycode(k)) end end def add action, help, *keys entry = [action, help, keys] @order << entry keys.each do |k| kc = Keymap.keysym_to_keycode k raise ArgumentError, "key '#{k}' already defined (as #{@map[kc].first})" if @map.include? kc @map[kc] = entry 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 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 action, help, keys = @map[kc] [action, help] end def has_key? k; @map[k] end def keysyms; @map.values.map { |action, help, keys| keys }.flatten; end def help_lines except_for={}, prefix="" lines = [] # :( @order.each do |action, help, keys| valid_keys = keys.select { |k| !except_for[k] } next if valid_keys.empty? case action when Symbol lines << [valid_keys.map { |k| prefix + Keymap.keysym_to_string(k) }.join(", "), help] when Keymap lines += action.help_lines({}, prefix + Keymap.keysym_to_string(keys.first)) end end.compact lines end def help_text except_for={} lines = help_lines except_for 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