Skip to content

Instantly share code, notes, and snippets.

@mooz
Created November 1, 2014 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooz/75c6ef9a0917df21552a to your computer and use it in GitHub Desktop.
Save mooz/75c6ef9a0917df21552a to your computer and use it in GitHub Desktop.
rbindkeys helper
# Load rbindkeys-helper
require "path-to-rbindkeys-helper"
# Emacs-like settings
def define_emacs_keys()
bind "C-g", "ESC"
# cursor move
bind "C-f", "<right>"
bind "C-b", "<left>"
bind "C-p", "<up>"
bind "C-n", "<down>"
bind "C-a", "<home>"
bind "C-e", "<end>"
# page scroll
bind "C-v", "<pagedown>"
bind "M-v", "<pageup>"
# edit
bind "C-d", "<delete>"
bind "C-h", "<backspace>"
bind "C-m", "<enter>"
bind "C-i", "<tab>"
bind "C-@", "<esc>"
# cut, copy and paste
bind "C-w", "C-x"
bind "M-w", "C-c"
bind "C-y", "C-v"
# Kill line
bind "C-k", "S-<end> C-x"
end
# ------------------------------------------------------------ #
# Gedit
window(@default_bind_resolver, :class => /gedit/) do
define_emacs_keys
end
window(@default_bind_resolver, :title => /unity-dash/) do
define_emacs_keys
end
window(BindResolver.new, :class => /gnome-terminal/) do
# Make Emacs recognize special key bindings
bind "C->" , "C-x @ c >"
bind "C-<" , "C-x @ c <"
bind "C-RET" , "C-x @ c RET"
bind "C-M-g" , "M-g @"
bind "C-:" , "C-x @ c :"
bind "C-=" , "C-x @ c ="
end
SPECIAL_KEY_MAP = {
"SPC" => [KEY_SPACE],
"RET" => [KEY_ENTER],
"." => [KEY_DOT],
">" => [KEY_RIGHTSHIFT, KEY_DOT],
"<" => [KEY_RIGHTSHIFT, KEY_COMMA],
"/" => [KEY_SLASH],
"\\" => [KEY_RO],
"_" => [KEY_RIGHTSHIFT, KEY_RO],
"?" => [KEY_RIGHTSHIFT, KEY_SLASH],
"@" => [KEY_LEFTBRACE],
"`" => [KEY_RIGHTSHIFT, KEY_RIGHTBRACE],
":" => [KEY_APOSTROPHE],
"*" => [KEY_RIGHTSHIFT, KEY_APOSTROPHE],
";" => [KEY_SEMICOLON],
"+" => [KEY_RIGHTSHIFT, KEY_SEMICOLON],
"]" => [KEY_BACKSLASH],
"}" => [KEY_RIGHTSHIFT, KEY_BACKSLASH],
"[" => [KEY_RIGHTBRACE],
"{" => [KEY_RIGHTSHIFT, KEY_RIGHTBRACE]
}
def parse_key_str(key_str)
ctrl_key, alt_key, shift_key = nil
while true
if matched = key_str.match(/^([CMS])-/)
key_str = key_str[2..-1]
case matched[1]
when "C"
ctrl_key = true
when "M"
alt_key = true
when "S"
shift_key = true
end
else
break
end
end
{
:ctrl_key => ctrl_key,
:alt_key => alt_key,
:shift_key => shift_key,
:key => key_str
}
end
def key_event(name)
eval("KEY_" + name)
end
def key_info_to_events(key_info)
events = []
events += [KEY_LEFTCTRL] if key_info[:ctrl_key]
events += [KEY_LEFTALT] if key_info[:alt_key]
events += [KEY_LEFTSHIFT] if key_info[:shift_key]
key_str = key_info[:key]
p key_str
if key_str.match(/^[a-zA-Z0-9]+$/)
events.push(key_event(key_str.upcase))
elsif key_str.match(/^<([a-zA-Z0-9]+)>$/)
events.push(key_event($1.upcase))
elsif SPECIAL_KEY_MAP.include?(key_str)
events += SPECIAL_KEY_MAP[key_str]
end
end
def key_str_to_events(key_str)
key_info_to_events(parse_key_str(key_str))
end
def dispatch_event_by_key_str(op, key_str)
events = key_str_to_events(key_str)
events.each do |event|
op.press_key(event)
end
events.reverse.each do |event|
op.release_key(event)
end
end
def bind(input_key_str, dispatch_key_str)
bind_key key_str_to_events(input_key_str) do |ev, op|
release_modifiers(op)
dispatch_key_str.split(" ").each do |key_str|
dispatch_event_by_key_str(op, key_str)
end
end
end
def release_modifiers(operator)
operator.release_key KEY_LEFTCTRL
operator.release_key KEY_LEFTALT
operator.release_key KEY_LEFTSHIFT
operator.release_key KEY_RIGHTCTRL
operator.release_key KEY_RIGHTALT
operator.release_key KEY_RIGHTSHIFT
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment