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.



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

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
  @next_tick_queue = []
  @next_tick_queue_mutex = Mutex.new
  @next_tick_input, @next_tick_output = IO.pipe
end

Instance Attribute Details

#current_prefix_argObject

Returns the value of attribute current_prefix_arg.



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

def current_prefix_arg
  @current_prefix_arg
end

#key_sequenceObject (readonly)

Returns the value of attribute key_sequence.



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

def key_sequence
  @key_sequence
end

#last_commandObject

Returns the value of attribute last_command.



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

def last_command
  @last_command
end

#last_keyObject (readonly)

Returns the value of attribute last_key.



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

def last_key
  @last_key
end

#last_keyboard_macroObject (readonly)

Returns the value of attribute last_keyboard_macro.



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

def last_keyboard_macro
  @last_keyboard_macro
end

#overriding_mapObject

Returns the value of attribute overriding_map.



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

def overriding_map
  @overriding_map
end

#prefix_argObject

Returns the value of attribute prefix_arg.



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

def prefix_arg
  @prefix_arg
end

#recursive_edit_levelObject (readonly)

Returns the value of attribute recursive_edit_level.



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

def recursive_edit_level
  @recursive_edit_level
end

#this_commandObject

Returns the value of attribute this_command.



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

def this_command
  @this_command
end

#this_command_keysObject (readonly)

Returns the value of attribute this_command_keys.



6
7
8
# File 'lib/textbringer/controller.rb', line 6

def this_command_keys
  @this_command_keys
end

Class Method Details

.currentObject



14
15
16
# File 'lib/textbringer/controller.rb', line 14

def self.current
  @@current
end

.current=(controller) ⇒ Object



18
19
20
# File 'lib/textbringer/controller.rb', line 18

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

Instance Method Details

#call_last_keyboard_macro(n) ⇒ Object



231
232
233
234
235
236
# File 'lib/textbringer/controller.rb', line 231

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

#closeObject



42
43
44
45
# File 'lib/textbringer/controller.rb', line 42

def close
  @next_tick_input.close
  @next_tick_output.close
end

#command_loop(tag) ⇒ Object



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
93
94
95
96
97
98
# File 'lib/textbringer/controller.rb', line 47

def command_loop(tag)
  catch(tag) do
    loop do
      begin
        echo_input
        c = read_event
        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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/textbringer/controller.rb', line 172

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 = +""
    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



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/textbringer/controller.rb', line 208

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



220
221
222
223
224
225
226
227
228
229
# File 'lib/textbringer/controller.rb', line 220

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)


242
243
244
# File 'lib/textbringer/controller.rb', line 242

def executing_keyboard_macro?
  !@executing_keyboard_macro.nil?
end

#key_binding(key_sequence) ⇒ Object



246
247
248
249
250
# File 'lib/textbringer/controller.rb', line 246

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

#next_tick(&block) ⇒ Object



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

def next_tick(&block)
  @next_tick_queue_mutex.synchronize do
    @next_tick_queue.push(block)
  end
  @next_tick_output.write("\n")
end

#read_eventObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/textbringer/controller.rb', line 115

def read_event
  if executing_keyboard_macro?
    return @executing_keyboard_macro.shift
  end
  event = read_event_nonblock
  if event
    return event
  end
  loop do
    if Window.echo_area.active?
      wait_files = [STDIN]
    else
      wait_files = [STDIN, @next_tick_input]
    end
    files, = IO.select(wait_files, nil, nil, 1)
    # KEY_RESIZE may be returned even if STDIN is not included in files.
    event = read_event_nonblock
    if event
      return event
    end
    if !Window.echo_area.active? && files&.include?(@next_tick_input)
      c = @next_tick_input.read_nonblock(1, exception: false)
      if !c.nil? && c != :wait_readable
        block = @next_tick_queue_mutex.synchronize {
          @next_tick_queue.shift
        }
        block.call
        Window.redisplay
      end
    end
  end
end

#read_event_nonblockObject



148
149
150
# File 'lib/textbringer/controller.rb', line 148

def read_event_nonblock
  read_event_with_keyboard_macro(:read_event_nonblock)
end

#received_keyboard_quit?Boolean

Returns:

  • (Boolean)


152
153
154
155
156
157
158
159
# File 'lib/textbringer/controller.rb', line 152

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

#recording_keyboard_macro?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/textbringer/controller.rb', line 238

def recording_keyboard_macro?
  !@recording_keyboard_macro.nil?
end

#recursive_editObject



161
162
163
164
165
166
167
168
169
170
# File 'lib/textbringer/controller.rb', line 161

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



200
201
202
203
204
205
206
# File 'lib/textbringer/controller.rb', line 200

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



100
101
102
103
104
105
106
# File 'lib/textbringer/controller.rb', line 100

def wait_input(msecs)
  # TODO: Check @next_tick_queue
  if executing_keyboard_macro?
    return @executing_keyboard_macro.first
  end
  Window.current.wait_input(msecs)
end