Class: VER::MajorMode

Inherits:
Struct
  • Object
show all
Includes:
Keymap::Results, ModeResolving, Platform
Defined in:
lib/ver/major_mode.rb

Overview

This mode is responsible for maintaining a keymap of its own and a list of minor modes that are immediately queried. A major mode can inherit the keymap of another major mode in order to specialize it. The inherited keymap is duplicated upon inheritance and the original will not be modified, the duplicate is merged in a manner that will not replace existing patterns.

The bound_keys property acts as a cache of the keys bound to the tag, so we don’t have to query the tag, as the bound proc is the same for all keys.

Constant Summary collapse

MODES =
{}

Constants inherited from Struct

Struct::CACHE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ModeResolving

#resolve_impossible, #resolve_incomplete

Methods included from Platform

aqua?, bsd?, mac?, operatingsystem, unix?, win32?, windowingsystem, windows?, x11?

Methods inherited from Struct

new

Constructor Details

#initialize(name) ⇒ MajorMode

Returns a new instance of MajorMode.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ver/major_mode.rb', line 42

def initialize(name)
  self.name = name = name.to_sym

  if MODES.key?(name)
    raise ArgumentError, "Duplicate #{self.class}: %p" % [name]
  else
    MODES[name] = self
  end

  self.minors = []
  self.keymap = Keymap.new
  self.bound_keys = Set.new
  self.tag = Tk::BindTag.new("#{name}-mode")

  # and now register all we didn't have yet.
  Event.each{|event| bind_key(event) }
end

Instance Attribute Details

#bound_keysObject

Returns the value of attribute bound_keys

Returns:

  • (Object)

    the current value of bound_keys



19
20
21
# File 'lib/ver/major_mode.rb', line 19

def bound_keys
  @bound_keys
end

#fallback_actionObject

Returns the value of attribute fallback_action

Returns:

  • (Object)

    the current value of fallback_action



19
20
21
# File 'lib/ver/major_mode.rb', line 19

def fallback_action
  @fallback_action
end

#keymapObject

Returns the value of attribute keymap

Returns:

  • (Object)

    the current value of keymap



19
20
21
# File 'lib/ver/major_mode.rb', line 19

def keymap
  @keymap
end

#minorsObject

Returns the value of attribute minors

Returns:

  • (Object)

    the current value of minors



19
20
21
# File 'lib/ver/major_mode.rb', line 19

def minors
  @minors
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



19
20
21
# File 'lib/ver/major_mode.rb', line 19

def name
  @name
end

#receiverObject

Returns the value of attribute receiver

Returns:

  • (Object)

    the current value of receiver



19
20
21
# File 'lib/ver/major_mode.rb', line 19

def receiver
  @receiver
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



19
20
21
# File 'lib/ver/major_mode.rb', line 19

def tag
  @tag
end

Class Method Details

.[](name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/ver/major_mode.rb', line 28

def self.[](name)
  name = name.to_sym

  if MODES.key?(name)
    MODES[name]
  else
    new(name)
  end
end

.clearObject



38
39
40
# File 'lib/ver/major_mode.rb', line 38

def self.clear
  MODES.each_value(&:destroy)
end

Instance Method Details

#actionsObject



141
142
143
# File 'lib/ver/major_mode.rb', line 141

def actions
  keymap.actions
end

#bind_key(key) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ver/major_mode.rb', line 121

def bind_key(key)
  case key
  when Event
    pattern = key.pattern
  when String
    pattern = Event[key].pasttern
  else
    raise ArgumentError, "Invalid key: %p" % [key]
  end

  return if bound_keys.include?(pattern)

  tag.bind(pattern) do |event|
    event.widget.major_mode.on_event(event)
    Tk.callback_break
  end

  bound_keys << pattern
end

#destroyObject



60
61
62
63
# File 'lib/ver/major_mode.rb', line 60

def destroy
  tag.bind.each{|key| tag.unbind(key) }
  MODES.delete(name)
end

#enter(invocation) ⇒ Object



79
80
81
82
83
84
# File 'lib/ver/major_mode.rb', line 79

def enter(invocation)
  action = Action.new(invocation, receiver, self)
  tag.bind "<<EnterMajorMode#{to_camel_case}>>" do |event|
    action.call(WidgetEvent.new(event.widget, event))
  end
end

#eql?(other) ⇒ Boolean

we assume that name is unique

Returns:

  • (Boolean)


158
159
160
# File 'lib/ver/major_mode.rb', line 158

def eql?(other)
  other.class == self.class && other.name == self.name
end

#forget(*minors) ⇒ Object



106
107
108
# File 'lib/ver/major_mode.rb', line 106

def forget(*minors)
  self.minors -= minors.map{|name| MinorMode[name] }
end

#handler(object) ⇒ Object



65
66
67
# File 'lib/ver/major_mode.rb', line 65

def handler(object)
  self.receiver = object
end

#hashObject



153
154
155
# File 'lib/ver/major_mode.rb', line 153

def hash
  name.hash
end

#inherits(name) ⇒ Object



93
94
95
96
# File 'lib/ver/major_mode.rb', line 93

def inherits(name)
  mode = self.class[name]
  keymap.merge!(mode.keymap)
end

#inspectObject



149
150
151
# File 'lib/ver/major_mode.rb', line 149

def inspect
  "#<VER::MajorMode name=%p>" % [name]
end

#leave(invocation) ⇒ Object



86
87
88
89
90
91
# File 'lib/ver/major_mode.rb', line 86

def leave(invocation)
  action = Action.new(invocation, receiver, self)
  tag.bind "<<LeaveMajorMode#{to_camel_case}>>" do |event|
    action.call(WidgetEvent.new(event.widget, event))
  end
end

#map(invocation, *patterns) ⇒ Object



69
70
71
72
# File 'lib/ver/major_mode.rb', line 69

def map(invocation, *patterns)
  action = Action.new(invocation, receiver, self)
  patterns.each{|pattern| keymap[pattern] = action }
end

#missing(invocation) ⇒ Object



74
75
76
77
# File 'lib/ver/major_mode.rb', line 74

def missing(invocation)
  action = Fallback.new(invocation, receiver, self)
  self.fallback_action = action
end

#replace_minor(old, new) ⇒ Object



115
116
117
118
119
# File 'lib/ver/major_mode.rb', line 115

def replace_minor(old, new)
  minors.each do |minor|
    minor.replace_parent(self, MinorMode[old], MinorMode[new])
  end
end

#resolve(pattern, minors = []) ⇒ Object

recursively try to find the pattern in the major mode and its minors.



111
112
113
# File 'lib/ver/major_mode.rb', line 111

def resolve(pattern, minors = [])
  super
end

#to_symObject



145
146
147
# File 'lib/ver/major_mode.rb', line 145

def to_sym
  name
end

#use(*minors) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ver/major_mode.rb', line 98

def use(*minors)
  minors.each do |name|
    minor = MinorMode[name]
    next if self.minors.any?{|m| m.name == minor.name }
    self.minors << minor
  end
end