Class: Textbringer::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/textbringer/controller.rb

Constant Summary collapse

@@current =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/textbringer/controller.rb', line 24

def initialize
  @top_self = eval("self", TOPLEVEL_BINDING)
  @key_sequence = []
  @last_key = nil
  @recursive_edit_level = 0
  @this_command_keys = nil
  @this_command = nil
  @last_command = nil
  @overriding_map = nil
  @prefix_arg = nil
  @current_prefix_arg = nil
  @echo_immediately = false
  @recording_keyboard_macro = nil
  @last_keyboard_macro = nil
  @executing_keyboard_macro = nil
end

Instance Attribute Details

#current_prefix_argObject

Returns the value of attribute current_prefix_arg.



10
11
12
# File 'lib/textbringer/controller.rb', line 10

def current_prefix_arg
  @current_prefix_arg
end

#key_sequenceObject (readonly)

Returns the value of attribute key_sequence.



11
12
13
# File 'lib/textbringer/controller.rb', line 11

def key_sequence
  @key_sequence
end

#last_commandObject

Returns the value of attribute last_command.



9
10
11
# File 'lib/textbringer/controller.rb', line 9

def last_command
  @last_command
end

#last_keyObject (readonly)

Returns the value of attribute last_key.



11
12
13
# File 'lib/textbringer/controller.rb', line 11

def last_key
  @last_key
end

#last_keyboard_macroObject (readonly)

Returns the value of attribute last_keyboard_macro.



12
13
14
# File 'lib/textbringer/controller.rb', line 12

def last_keyboard_macro
  @last_keyboard_macro
end

#overriding_mapObject

Returns the value of attribute overriding_map.



9
10
11
# File 'lib/textbringer/controller.rb', line 9

def overriding_map
  @overriding_map
end

#prefix_argObject

Returns the value of attribute prefix_arg.



10
11
12
# File 'lib/textbringer/controller.rb', line 10

def prefix_arg
  @prefix_arg
end

#recursive_edit_levelObject (readonly)

Returns the value of attribute recursive_edit_level.



11
12
13
# File 'lib/textbringer/controller.rb', line 11

def recursive_edit_level
  @recursive_edit_level
end

#this_commandObject

Returns the value of attribute this_command.



9
10
11
# File 'lib/textbringer/controller.rb', line 9

def this_command
  @this_command
end

#this_command_keysObject (readonly)

Returns the value of attribute this_command_keys.



8
9
10
# File 'lib/textbringer/controller.rb', line 8

def this_command_keys
  @this_command_keys
end

Class Method Details

.currentObject



16
17
18
# File 'lib/textbringer/controller.rb', line 16

def self.current
  @@current
end

.current=(controller) ⇒ Object



20
21
22
# File 'lib/textbringer/controller.rb', line 20

def self.current=(controller)
  @@current = controller
end

Instance Method Details

#call_last_keyboard_macro(n) ⇒ Object



188
189
190
191
192
193
# File 'lib/textbringer/controller.rb', line 188

def call_last_keyboard_macro(n)
  if @last_keyboard_macro.nil?
    raise EditorError, "Keyboard macro not defined"
  end
  execute_keyboard_macro(@last_keyboard_macro, n)
end

#command_loop(tag) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/textbringer/controller.rb', line 41

def command_loop(tag)
  catch(tag) do
    loop do
      begin
        echo_input
        c = read_char
        break if c.nil?
        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)
          @this_command_keys = @key_sequence
          @key_sequence = []
          @this_command = cmd
          @current_prefix_arg = @prefix_arg
          @prefix_arg = nil
          begin
            run_hooks(:pre_command_hook, remove_on_error: true)
            if cmd.is_a?(Symbol)
              @top_self.send(cmd)
            else
              cmd.call
            end
          ensure
            run_hooks(:post_command_hook, remove_on_error: true)
            @last_command = @this_command
            @this_command = nil
          end
        else
          if cmd.nil?
            keys = Keymap.key_sequence_string(@key_sequence)
            @key_sequence.clear
            @prefix_arg = nil
            message("#{keys} is undefined")
          end
        end
        Window.redisplay
      rescue Exception => e
        show_exception(e)
        @prefix_arg = nil
        @recording_keyboard_macro = nil
        Window.redisplay
        if Window.echo_area.active?
          wait_input(2000)
          Window.echo_area.clear_message
          Window.redisplay
        end
      end
    end
  end
end

#echo_inputObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/textbringer/controller.rb', line 129

def echo_input
  return if executing_keyboard_macro?
  if @prefix_arg || !@key_sequence.empty?
    if !@echo_immediately
      return if wait_input(1000)
    end
    @echo_immediately = true
    s = String.new
    if @prefix_arg
      s << "C-u"
      if @prefix_arg != [4]
        s << "(#{@prefix_arg.inspect})"
      end
    end
    if !@key_sequence.empty?
      s << " " if !s.empty?
      s << Keymap.key_sequence_string(@key_sequence)
    end
    s << "-"
    Window.echo_area.show(s)
    Window.echo_area.redisplay
    Window.current.window.noutrefresh
    Window.update
  else
    @echo_immediately = false
  end
end

#end_keyboard_macroObject



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/textbringer/controller.rb', line 165

def end_keyboard_macro
  if @recording_keyboard_macro.nil?
    raise EditorError, "Not recording keyboard macro"
  end
  if @recording_keyboard_macro.empty?
    raise EditorError, "Empty keyboard macro"
  end
  @recording_keyboard_macro.pop(@this_command_keys.size)
  @last_keyboard_macro = @recording_keyboard_macro
  @recording_keyboard_macro = nil
end

#execute_keyboard_macro(macro, n = 1) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/textbringer/controller.rb', line 177

def execute_keyboard_macro(macro, n = 1)
  n.times do
    @executing_keyboard_macro = macro.dup
    begin
      recursive_edit
    ensure
      @executing_keyboard_macro = nil
    end
  end
end

#executing_keyboard_macro?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/textbringer/controller.rb', line 199

def executing_keyboard_macro?
  !@executing_keyboard_macro.nil?
end

#key_binding(key_sequence) ⇒ Object



203
204
205
206
207
# File 'lib/textbringer/controller.rb', line 203

def key_binding(key_sequence)
  @overriding_map&.lookup(key_sequence) ||
  Buffer.current&.keymap&.lookup(key_sequence) ||
    GLOBAL_MAP.lookup(key_sequence)
end

#read_charObject



101
102
103
# File 'lib/textbringer/controller.rb', line 101

def read_char
  read_char_with_keyboard_macro(:read_char)
end

#read_char_nonblockObject



105
106
107
# File 'lib/textbringer/controller.rb', line 105

def read_char_nonblock
  read_char_with_keyboard_macro(:read_char_nonblock)
end

#received_keyboard_quit?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
# File 'lib/textbringer/controller.rb', line 109

def received_keyboard_quit?
  while key = read_char_nonblock
    if GLOBAL_MAP.lookup([key]) == :keyboard_quit
      return true
    end
  end
  false
end

#recording_keyboard_macro?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/textbringer/controller.rb', line 195

def recording_keyboard_macro?
  !@recording_keyboard_macro.nil?
end

#recursive_editObject



118
119
120
121
122
123
124
125
126
127
# File 'lib/textbringer/controller.rb', line 118

def recursive_edit
  @recursive_edit_level += 1
  begin
    if command_loop(RECURSIVE_EDIT_TAG)
      raise Quit
    end
  ensure
    @recursive_edit_level -= 1
  end
end

#start_keyboard_macroObject



157
158
159
160
161
162
163
# File 'lib/textbringer/controller.rb', line 157

def start_keyboard_macro
  if @recording_keyboard_macro
    @recording_keyboard_macro = nil
    raise EditorError, "Already recording keyboard macro"
  end
  @recording_keyboard_macro = []
end

#wait_input(msecs) ⇒ Object



94
95
96
97
98
99
# File 'lib/textbringer/controller.rb', line 94

def wait_input(msecs)
  if executing_keyboard_macro?
    return @executing_keyboard_macro.first
  end
  Window.current.wait_input(msecs)
end