lib/textbringer/keymap.rb in textbringer-0.1.8 vs lib/textbringer/keymap.rb in textbringer-0.1.9

- old
+ new

@@ -2,10 +2,12 @@ require "curses" module Textbringer class Keymap + include Enumerable + def initialize @map = {} end def define_key(key, command) @@ -33,14 +35,47 @@ k, *ks = key_sequence @map[k]&.lookup(ks) end end + def each(prefixes = [], &block) + @map.each do |key, val| + if val.is_a?(Keymap) + val.each([*prefixes, key], &block) + else + yield([*prefixes, key], val) + end + end + end + def handle_undefined_key @map.default_proc = Proc.new { |h, k| yield(k) } end + def self.key_name(key) + case key + when Symbol + "<#{key}>" + when " " + "SPC" + when "\t" + "TAB" + when "\e" + "ESC" + when "\C-m" + "RET" + when /\A[\0-\x1f\x7f]\z/ + "C-" + (key.ord ^ 0x40).chr.downcase + else + key.to_s + end + end + + def self.key_sequence_string(key_sequence) + key_sequence.map { |key| key_name(key) }.join(" ") + end + private def kbd(key) case key when Symbol @@ -83,49 +118,72 @@ (?\x20..?\x7e).each do |c| GLOBAL_MAP.define_key(c, :self_insert) end GLOBAL_MAP.define_key(?\t, :self_insert) GLOBAL_MAP.define_key(?\C-q, :quoted_insert) - GLOBAL_MAP.define_key("\C- ", :set_mark_command) + GLOBAL_MAP.define_key("\C-@", :set_mark_command) + GLOBAL_MAP.define_key("\C-x\C-@", :pop_global_mark) + GLOBAL_MAP.define_key("\e*", :next_global_mark) + GLOBAL_MAP.define_key("\e?", :previous_global_mark) GLOBAL_MAP.define_key("\C-x\C-x", :exchange_point_and_mark) GLOBAL_MAP.define_key("\ew", :copy_region) GLOBAL_MAP.define_key(?\C-w, :kill_region) GLOBAL_MAP.define_key(?\C-k, :kill_line) GLOBAL_MAP.define_key("\ed", :kill_word) GLOBAL_MAP.define_key(?\C-y, :yank) GLOBAL_MAP.define_key("\ey", :yank_pop) GLOBAL_MAP.define_key(?\C-_, :undo) - GLOBAL_MAP.define_key("\C-x\C-_", :redo) + GLOBAL_MAP.define_key("\C-x\C-_", :redo_command) GLOBAL_MAP.define_key("\C-t", :transpose_chars) GLOBAL_MAP.define_key("\C-j", :newline) GLOBAL_MAP.define_key("\C-m", :newline) + GLOBAL_MAP.define_key("\em", :back_to_indentation) + GLOBAL_MAP.define_key("\e^", :delete_indentation) GLOBAL_MAP.define_key("\C-l", :recenter) GLOBAL_MAP.define_key("\C-v", :scroll_up) GLOBAL_MAP.define_key(:npage, :scroll_up) GLOBAL_MAP.define_key("\ev", :scroll_down) GLOBAL_MAP.define_key(:ppage, :scroll_down) GLOBAL_MAP.define_key("\C-x0", :delete_window) GLOBAL_MAP.define_key("\C-x1", :delete_other_windows) GLOBAL_MAP.define_key("\C-x2", :split_window) GLOBAL_MAP.define_key("\C-xo", :other_window) GLOBAL_MAP.define_key("\C-x^", :enlarge_window) + GLOBAL_MAP.define_key("\C-x-", :shrink_window_if_larger_than_buffer) GLOBAL_MAP.define_key("\C-x\C-c", :exit_textbringer) GLOBAL_MAP.define_key("\C-z", :suspend_textbringer) GLOBAL_MAP.define_key("\C-x\C-f", :find_file) GLOBAL_MAP.define_key("\C-xb", :switch_to_buffer) GLOBAL_MAP.define_key("\C-x\C-s", :save_buffer) GLOBAL_MAP.define_key("\C-x\C-w", :write_file) GLOBAL_MAP.define_key("\C-xk", :kill_buffer) GLOBAL_MAP.define_key("\C-x\C-mf", :set_buffer_file_encoding) GLOBAL_MAP.define_key("\C-x\C-mn", :set_buffer_file_format) + GLOBAL_MAP.define_key("\e.", :find_tag) GLOBAL_MAP.define_key("\ex", :execute_command) GLOBAL_MAP.define_key("\e:", :eval_expression) GLOBAL_MAP.define_key(?\C-u, :universal_argument) GLOBAL_MAP.define_key(?\C-g, :keyboard_quit) GLOBAL_MAP.define_key(?\C-s, :isearch_forward) GLOBAL_MAP.define_key(?\C-r, :isearch_backward) GLOBAL_MAP.define_key("\e%", :query_replace_regexp) GLOBAL_MAP.define_key("\e!", :shell_execute) + GLOBAL_MAP.define_key("\C-xr ", :point_to_register) + GLOBAL_MAP.define_key("\C-xrj", :jump_to_register) + GLOBAL_MAP.define_key("\C-xrx", :copy_to_register) + GLOBAL_MAP.define_key("\C-xrs", :copy_to_register) + GLOBAL_MAP.define_key("\C-xrg", :insert_register) + GLOBAL_MAP.define_key("\C-xri", :insert_register) + GLOBAL_MAP.define_key("\C-xrn", :number_to_register) + GLOBAL_MAP.define_key("\C-xr+", :increment_register) + GLOBAL_MAP.define_key("\C-x(", :start_keyboard_macro) + GLOBAL_MAP.define_key(:f3, :start_keyboard_macro) + GLOBAL_MAP.define_key("\C-x)", :end_keyboard_macro) + GLOBAL_MAP.define_key("\C-xe", :end_and_call_keyboard_macro) + GLOBAL_MAP.define_key(:f4, :end_or_call_keyboard_macro) + GLOBAL_MAP.define_key([:f1, "b"], :describe_bindings) + GLOBAL_MAP.define_key([:f1, "f"], :describe_command) + GLOBAL_MAP.define_key([:f1, "k"], :describe_key) GLOBAL_MAP.handle_undefined_key do |key| if key.is_a?(String) && /[\0-\x7f]/ !~ key :self_insert else nil