lib/textbringer/controller.rb in textbringer-0.1.4 vs lib/textbringer/controller.rb in textbringer-0.1.5
- old
+ new
@@ -5,11 +5,11 @@
RECURSIVE_EDIT_TAG = Object.new
class Controller
attr_accessor :this_command, :last_command, :overriding_map
attr_accessor :prefix_arg, :current_prefix_arg
- attr_reader :last_key
+ attr_reader :last_key, :recursive_edit_level
@@current = nil
def self.current
@@current
@@ -32,11 +32,11 @@
def command_loop(tag)
catch(tag) do
loop do
begin
- c = Window.current.read_char
+ c = read_char
Window.echo_area.clear_message
@last_key = c
@key_sequence << @last_key
cmd = key_binding(@key_sequence)
if cmd.is_a?(Symbol) || cmd.respond_to?(:call)
@@ -58,15 +58,15 @@
end
else
if cmd.nil?
keys = @key_sequence.map { |ch| key_name(ch) }.join(" ")
@key_sequence.clear
- Window.echo_area.show("#{keys} is undefined")
+ message("#{keys} is undefined")
end
end
rescue Exception => e
- handle_exception(e)
+ show_exception(e)
end
Window.redisplay
end
end
end
@@ -77,12 +77,16 @@
def read_char
Window.current.read_char
end
+ def read_char_nonblock
+ Window.current.read_char_nonblock
+ end
+
def received_keyboard_quit?
- while key = Window.current.read_char_nonblock
+ while key = read_char_nonblock
if GLOBAL_MAP.lookup([key]) == :keyboard_quit
return true
end
end
false
@@ -97,29 +101,23 @@
ensure
@recursive_edit_level -= 1
end
end
- private
-
def key_name(key)
case key
- when Integer
- if key < 0x80
- s = Curses.keyname(key)
- case s
- when /\AKEY_(.*)/
- "<#{$1.downcase}>"
- else
- s
- end
- else
- key.chr(Encoding::UTF_8)
- end
+ when Symbol
+ "<#{key}>"
+ when "\e"
+ "ESC"
+ when /\A[\0-\b\v-\x1f\x7f]\z/
+ "C-" + (key.ord ^ 0x40).chr.downcase
else
key.to_s
end
end
+
+ private
def key_binding(key_sequence)
@overriding_map&.lookup(key_sequence) ||
Buffer.current&.keymap&.lookup(key_sequence) ||
GLOBAL_MAP.lookup(key_sequence)