Class: SublimeDSL::SublimeText::Keyboard::DSLReader

Inherits:
Object
  • Object
show all
Defined in:
lib/sublime_dsl/sublime_text/keyboard.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ DSLReader

Returns a new instance of DSLReader.



543
544
545
546
547
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 543

def initialize(file)
  @_keyboard = nil
  @in_definition = false
  instance_eval ::File.read(file, encoding: 'utf-8'), file
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Raises:



549
550
551
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 549

def method_missing(sym, *args, &block)
  raise Error, "'#{sym}' is not a keyboard DSL statement"
end

Instance Attribute Details

#_keyboardObject (readonly)

Returns the value of attribute _keyboard.



541
542
543
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 541

def _keyboard
  @_keyboard
end

Instance Method Details

#add_keys(spec, options = {}) ⇒ Object



581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 581

def add_keys(spec, options={})
  ensure_context __method__
  keys = parse_key_list(spec)

  st_spec = options.delete(:st_keys)
  if st_spec
    st_keys = parse_key_list(st_spec)
    keys.length == st_keys.length or
      raise Error, "st_keys: got #{st_keys.length} keys, expected #{keys.length}"
  end
  options.empty? or warn "extraneous arguments ignored: #{options.inspect}"

  keys.each_with_index do |name, i|
    _keyboard.add_key name
    if st_spec
      _keyboard.map_key name, st_keys[i]
    # done automatically when registering key events of new keystrokes:
    # elsif Keyboard.sublime.key(name)
    #   _keyboard.map_key name, name
    end
  end

end

#add_modifiers(spec) ⇒ Object



567
568
569
570
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 567

def add_modifiers(spec)
  ensure_context __method__
  spec.split(/\s/).each { |name| _keyboard.add_modifier name }
end

#keyboard(name) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



553
554
555
556
557
558
559
560
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 553

def keyboard(name)
  @_keyboard and raise Error, 'only one keyboard definition per file'
  @in_definition and raise Error, "'keyboard' blocks cannot be nested"
  @_keyboard = Keyboard.new(name)
  @in_definition = true
  yield self
  @in_definition = false
end

#map_char(options = {}) ⇒ Object



614
615
616
617
618
619
620
621
622
623
624
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 614

def map_char(options={})
  spec = options.keys.first
  spec or raise Error, 'missing argument'
  char = options.delete(spec)
  dead = options.delete(:dead)
  options.empty? or warn "extraneous arguments ignored: #{options.inspect}"
  char.length == 1 or raise Error, "map_dead: expected a character, got #{char.inspect}"
  ks = _keyboard.ensure_keystroke(spec)
  ks.chr_event = char
  ks.chr_dead = true if dead
end

#map_key(options = {}) ⇒ Object



605
606
607
608
609
610
611
612
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 605

def map_key(options={})
  ensure_context __method__
  name = options.keys.first
  name or raise Error, 'missing argument'
  st_name = options.delete(name)
  options.empty? or warn "extraneous arguments ignored: #{options.inspect}"
  _keyboard.map_key name, st_name
end

#map_modifier(options = {}) ⇒ Object



572
573
574
575
576
577
578
579
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 572

def map_modifier(options={})
  ensure_context __method__
  name = options.keys.first
  name or raise Error, 'missing argument'
  st_name = options.delete(name)
  options.empty? or warn "extraneous arguments ignored: #{options.inspect}"
  _keyboard.map_modifier name, st_name
end

#os(value) ⇒ Object



562
563
564
565
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 562

def os(value)
  ensure_context __method__
  _keyboard.os = value
end

#os_action(options = {}) ⇒ Object



626
627
628
629
630
631
632
633
634
635
# File 'lib/sublime_dsl/sublime_text/keyboard.rb', line 626

def os_action(options={})
  spec = options.keys.first
  spec or raise Error, 'missing argument'
  action = options.delete(spec)
  key_event = options.delete(:key_event)
  options.empty? or warn "extraneous arguments ignored: #{options.inspect}"
  ks = _keyboard.ensure_keystroke(spec)
  ks.os_action = action
  ks.key_event = nil unless key_event
end