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
40
41
42
# 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
  @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.



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



233
234
235
236
237
238
# File 'lib/textbringer/controller.rb', line 233

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



44
45
46
47
# File 'lib/textbringer/controller.rb', line 44

def close
  @next_tick_input.close
  @next_tick_output.close
end

#command_loop(tag) ⇒ Object



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
99
100
# File 'lib/textbringer/controller.rb', line 49

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



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
199
200
# File 'lib/textbringer/controller.rb', line 174

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



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

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



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

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)


244
245
246
# File 'lib/textbringer/controller.rb', line 244

def executing_keyboard_macro?
  !@executing_keyboard_macro.nil?
end

#key_binding(key_sequence) ⇒ Object



248
249
250
251
252
# File 'lib/textbringer/controller.rb', line 248

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



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

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

#read_eventObject



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
147
148
# File 'lib/textbringer/controller.rb', line 117

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, [], [], 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



150
151
152
# File 'lib/textbringer/controller.rb', line 150

def read_event_nonblock
  read_event_with_keyboard_macro(:read_event_nonblock)
end

#received_keyboard_quit?Boolean

Returns:

  • (Boolean)


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

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)


240
241
242
# File 'lib/textbringer/controller.rb', line 240

def recording_keyboard_macro?
  !@recording_keyboard_macro.nil?
end

#recursive_editObject



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

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



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

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



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

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