Allow shell command execution

pull/1/head
Takashi Kokubun 8 years ago
parent cb4f9b5cc4
commit 55e172e895

@ -0,0 +1,3 @@
remap 'C-o', to: execute('nocturn')
remap 'C-u', to: execute('google-chrome-stable')
remap 'C-h', to: execute('urxvt')

@ -6,6 +6,7 @@ MRuby::Gem::Specification.new('xkremap') do |spec|
spec.add_dependency 'mruby-eval', core: 'mruby-eval'
spec.add_dependency 'mruby-io', mgem: 'mruby-io'
spec.add_dependency 'mruby-io', mgem: 'mruby-io'
spec.add_dependency 'mruby-process', mgem: 'mruby-process'
spec.add_dependency 'mruby-onig-regexp', mgem: 'mruby-onig-regexp'
end

@ -1,6 +1,8 @@
module Xkremap
class Config
# FIXME: :to_keys should be :to_actions, and Key and Execute should be adapted.
Remap = Struct.new(:from_key, :to_keys)
Execute = Struct.new(:command, :action)
class Key < Struct.new(:keysym, :modifier, :action)
def initialize(*)

@ -7,7 +7,10 @@ module Xkremap
end
def remap(from_str, options = {})
to_strs = Array(options.fetch(:to))
# Array() doesn't work for Config::Execute somehow.
to_strs = options.fetch(:to)
to_strs = [to_strs] unless to_strs.is_a?(Array)
@config.remaps_by_window[@window] << Config::Remap.new(
compile_exp(from_str),
to_strs.map { |str| compile_exp(str) }
@ -19,6 +22,10 @@ module Xkremap
ConfigDSL.new(@config, win).instance_exec(&block)
end
def execute(str)
Config::Execute.new(str, :execute)
end
def press(str)
key = compile_exp(str)
key.action = :press
@ -34,9 +41,10 @@ module Xkremap
private
def compile_exp(exp)
if exp.is_a?(Config::Key)
case exp
when Config::Key, Config::Execute
exp
elsif exp.is_a?(String)
when String
KeyExpression.compile(exp)
else
raise "unexpected expression: #{exp.inspect}"

@ -28,6 +28,10 @@ module Xkremap
Proc.new { XlibWrapper.press_key(@display, to.keysym, to.modifier) }
when :release
Proc.new { XlibWrapper.release_key(@display, to.keysym, to.modifier) }
when :execute
Proc.new { system("nohup #{to.command} >/dev/null 2>&1 &") }
else
raise "unexpected action: #{to.action.inspect}"
end
end

Loading…
Cancel
Save