Class: Redwood::Keymap

Inherits:
Object show all
Defined in:
lib/sup/keymap.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Keymap

Returns a new instance of Keymap.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
# File 'lib/sup/keymap.rb', line 14

def initialize
  @map = {}
  @order = []
  yield self if block_given?
end

Class Method Details

.keysym_to_keycode(k) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sup/keymap.rb', line 20

def self.keysym_to_keycode k
  case k
  when :down then Ncurses::KEY_DOWN
  when :up then Ncurses::KEY_UP
  when :left then Ncurses::KEY_LEFT
  when :right then Ncurses::KEY_RIGHT
  when :page_down then Ncurses::KEY_NPAGE
  when :page_up then Ncurses::KEY_PPAGE
  when :backspace then Ncurses::KEY_BACKSPACE
  when :home then Ncurses::KEY_HOME
  when :end then Ncurses::KEY_END
  when :ctrl_l then "\f".ord
  when :ctrl_g then "\a".ord
  when :tab then "\t".ord
  when :enter, :return then 10 #Ncurses::KEY_ENTER
  else
    if k.is_a?(String) && k.length == 1
      k.ord
    else
      raise ArgumentError, "unknown key name '#{k}'"
    end
  end
end

.keysym_to_string(k) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sup/keymap.rb', line 44

def self.keysym_to_string k
  case k
  when :down then "<down arrow>"
  when :up then "<up arrow>"
  when :left then "<left arrow>"
  when :right then "<right arrow>"
  when :page_down then "<page down>"
  when :page_up then "<page up>"
  when :backspace then "<backspace>"
  when :home then "<home>"
  when :end then "<end>"
  when :enter, :return then "<enter>"
  when :tab then "tab"
  when " " then "<space>"
  else
    Ncurses::keyname(keysym_to_keycode(k))
  end
end

.run_hook(global_keymap) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/sup/keymap.rb', line 130

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

Instance Method Details

#action_for(kc) ⇒ Object



100
101
102
103
# File 'lib/sup/keymap.rb', line 100

def action_for kc
  action, help, keys = @map[kc.code]
  [action, help]
end

#add(action, help, *keys) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/sup/keymap.rb', line 63

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

#add!(action, help, *keys) ⇒ Object



82
83
84
85
# File 'lib/sup/keymap.rb', line 82

def add! action, help, *keys
  keys.each { |k| delete k }
  add action, help, *keys
end

#add_multi(prompt, key) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sup/keymap.rb', line 87

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

#delete(k) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/sup/keymap.rb', line 73

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

#has_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


105
# File 'lib/sup/keymap.rb', line 105

def has_key? k; @map[k.code] end

#help_lines(except_for = {}, prefix = "") ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sup/keymap.rb', line 109

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

#help_text(except_for = {}) ⇒ Object



124
125
126
127
128
# File 'lib/sup/keymap.rb', line 124

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

#keysymsObject



107
# File 'lib/sup/keymap.rb', line 107

def keysyms; @map.values.map { |action, help, keys| keys }.flatten; end