vimamsa 0.1.27 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +139 -89
  3. data/custom_example.rb +18 -6
  4. data/doc/TODO.md +327 -110
  5. data/doc/action_names_plan.md +442 -0
  6. data/doc/bundling_plan.md +2 -1
  7. data/doc/delta_syntax_plan.md +293 -0
  8. data/doc/demo_recording_plan.md +288 -0
  9. data/doc/design_goals.md +27 -0
  10. data/doc/intro_page_plan.md +266 -0
  11. data/doc/key_bindings_report_plan.md +45 -0
  12. data/doc/line_tree_plan.md +89 -0
  13. data/doc/missing_vim_bindings.md +34 -44
  14. data/doc/modules_settings_plan.md +1 -1
  15. data/doc/registries_plan.md +202 -0
  16. data/doc/startup_speed_plan.md +97 -0
  17. data/doc/treap_visualizer.html +585 -0
  18. data/doc/vim_compatibility_plan.md +186 -31
  19. data/doc/vimlike_vim_conflicts.md +236 -228
  20. data/doc/vm_snapshots_plan.md +4 -4
  21. data/doc/wysiwyg_plan.md +231 -0
  22. data/exe/run_tests.rb +8 -1
  23. data/exe/vimamsa +49 -6
  24. data/help/intro.txt +30 -0
  25. data/help/vim_intro.txt +31 -0
  26. data/img/demo.gif +0 -0
  27. data/img/demo.mp4 +0 -0
  28. data/lang/hyperplaintext.lang +1 -1
  29. data/lib/vimamsa/ack.rb +2 -0
  30. data/lib/vimamsa/actions.rb +381 -36
  31. data/lib/vimamsa/bookmarks.rb +8 -8
  32. data/lib/vimamsa/buffer.rb +83 -37
  33. data/lib/vimamsa/buffer_changetext.rb +58 -36
  34. data/lib/vimamsa/buffer_list.rb +2 -2
  35. data/lib/vimamsa/buffer_manager.rb +6 -6
  36. data/lib/vimamsa/cli_help.rb +75 -0
  37. data/lib/vimamsa/clipboard.rb +6 -3
  38. data/lib/vimamsa/conf.rb +9 -0
  39. data/lib/vimamsa/crash_handler.rb +2 -2
  40. data/lib/vimamsa/debug.rb +2 -2
  41. data/lib/vimamsa/demo_player.rb +458 -0
  42. data/lib/vimamsa/demo_recorder.rb +277 -0
  43. data/lib/vimamsa/diff_buffer.rb +1 -1
  44. data/lib/vimamsa/dot_repeat.rb +104 -0
  45. data/lib/vimamsa/easy_jump.rb +54 -3
  46. data/lib/vimamsa/editor.rb +89 -46
  47. data/lib/vimamsa/file_history.rb +1 -1
  48. data/lib/vimamsa/file_manager.rb +23 -23
  49. data/lib/vimamsa/form_input.rb +388 -0
  50. data/lib/vimamsa/gui.rb +83 -13
  51. data/lib/vimamsa/gui_command_line.rb +44 -5
  52. data/lib/vimamsa/gui_form_generator.rb +17 -6
  53. data/lib/vimamsa/gui_key_hint.rb +165 -0
  54. data/lib/vimamsa/gui_keylog_panel.rb +164 -2
  55. data/lib/vimamsa/gui_menu.rb +69 -50
  56. data/lib/vimamsa/gui_select_window.rb +77 -43
  57. data/lib/vimamsa/gui_settings.rb +9 -5
  58. data/lib/vimamsa/gui_sourceview.rb +15 -81
  59. data/lib/vimamsa/gui_theme.rb +11 -2
  60. data/lib/vimamsa/hook.rb +2 -0
  61. data/lib/vimamsa/intro_page.rb +368 -0
  62. data/lib/vimamsa/key_actions.rb +315 -225
  63. data/lib/vimamsa/key_binding_tree.rb +228 -61
  64. data/lib/vimamsa/key_bindings_notepad.rb +18 -16
  65. data/lib/vimamsa/key_bindings_report.rb +290 -67
  66. data/lib/vimamsa/key_bindings_vim.rb +47 -297
  67. data/lib/vimamsa/key_bindings_vimlike.rb +347 -339
  68. data/lib/vimamsa/key_names.rb +80 -0
  69. data/lib/vimamsa/line_tree.rb +367 -0
  70. data/lib/vimamsa/macro.rb +20 -3
  71. data/lib/vimamsa/rbvma.rb +15 -0
  72. data/lib/vimamsa/registers.rb +125 -0
  73. data/lib/vimamsa/run_ruby.rb +64 -0
  74. data/lib/vimamsa/search_replace.rb +4 -4
  75. data/lib/vimamsa/string_util.rb +17 -3
  76. data/lib/vimamsa/tests.rb +8 -8
  77. data/lib/vimamsa/text_objects.rb +11 -54
  78. data/lib/vimamsa/text_transforms.rb +4 -0
  79. data/lib/vimamsa/tmp_user_dir.rb +39 -0
  80. data/lib/vimamsa/user_dirs.rb +83 -0
  81. data/lib/vimamsa/version.rb +1 -1
  82. data/lib/vimamsa/vim_core.rb +355 -0
  83. data/lib/vimamsa/vim_operators.rb +365 -0
  84. data/modules/calculator/calculator.rb +5 -5
  85. data/modules/color_highlight/color_highlight.rb +3 -3
  86. data/modules/dictation/dictation.rb +32 -32
  87. data/modules/ollama/ollama.rb +5 -5
  88. data/modules/terminal/terminal.rb +5 -5
  89. data/modules/tts/tts.rb +10 -10
  90. data/run_e2e_vm.sh +3 -3
  91. data/run_tests_vm.rb +138 -0
  92. data/scripts/demo/demo_easyjump_steps.txt +60 -0
  93. data/scripts/demo/demo_files_steps.txt +52 -0
  94. data/scripts/demo/demo_steps.txt +9 -1
  95. data/scripts/demo/notes.txt +6 -0
  96. data/scripts/demo/record_demo.rb +60 -81
  97. data/scripts/demo/report.rb +7 -0
  98. data/scripts/demo/shapes.rb +1 -0
  99. data/scripts/demo/square/shapes.rb +29 -0
  100. data/scripts/demo/square/steps.txt +185 -0
  101. data/scripts/lib/perf_startup.rb +138 -0
  102. data/scripts/lib/script_helpers.rb +53 -0
  103. data/scripts/perf_compare.rb +34 -0
  104. data/scripts/profile_startup.rb +72 -0
  105. data/tests/test_action_ids.rb +232 -0
  106. data/tests/test_auto_format.rb +2 -2
  107. data/tests/test_autocomplete.rb +1 -1
  108. data/tests/test_basic_editing.rb +18 -18
  109. data/tests/test_bookmarks.rb +6 -6
  110. data/tests/test_bracket_match.rb +3 -0
  111. data/tests/test_cli_help.rb +64 -0
  112. data/tests/test_color_highlight.rb +2 -2
  113. data/tests/test_command_line.rb +1 -1
  114. data/tests/test_context_menu_file.rb +18 -0
  115. data/tests/test_copy_paste.rb +33 -33
  116. data/tests/test_custom_two_phase.rb +1 -1
  117. data/tests/test_demo_player.rb +301 -0
  118. data/tests/test_demo_recorder.rb +202 -0
  119. data/tests/test_dictation.rb +16 -16
  120. data/tests/test_dot_repeat.rb +88 -0
  121. data/tests/test_easy_jump.rb +93 -0
  122. data/tests/test_extract_file_links.rb +3 -3
  123. data/tests/test_file_panel.rb +2 -2
  124. data/tests/test_flash.rb +1 -1
  125. data/tests/test_fold.rb +1 -1
  126. data/tests/test_form_input.rb +203 -0
  127. data/tests/test_func_panel.rb +2 -2
  128. data/tests/test_gtk_sync.rb +7 -7
  129. data/tests/test_intro_page.rb +437 -0
  130. data/tests/test_key_categories.rb +163 -0
  131. data/tests/test_keylog_panel.rb +163 -3
  132. data/tests/test_line_ends.rb +51 -0
  133. data/tests/test_line_tree.rb +86 -0
  134. data/tests/test_lsp_clangd.rb +2 -2
  135. data/tests/test_macro.rb +2 -2
  136. data/tests/test_module_interface.rb +9 -9
  137. data/tests/test_notepad_bindings.rb +5 -5
  138. data/tests/test_perf_startup.rb +28 -0
  139. data/tests/test_random_edit.rb +27 -27
  140. data/tests/test_registers.rb +122 -0
  141. data/tests/test_run_ruby.rb +77 -0
  142. data/tests/test_startup_file.rb +1 -1
  143. data/tests/test_text_objects.rb +1 -1
  144. data/tests/test_text_transforms.rb +12 -0
  145. data/tests/test_tmp_user_dir.rb +79 -0
  146. data/tests/test_tts.rb +10 -10
  147. data/tests/test_undo.rb +34 -34
  148. data/tests/test_user_dirs.rb +122 -0
  149. data/tests/test_vim_bindings.rb +107 -6
  150. data/tests/test_vim_conflicts.rb +41 -4
  151. data/tests/test_vim_operators.rb +116 -0
  152. data/tests/test_window_layout.rb +83 -0
  153. data/vimamsa.gemspec +1 -1
  154. data/vm_guest_run.sh +22 -2
  155. metadata +58 -3
  156. data/run_tests_vm.sh +0 -130
@@ -0,0 +1,458 @@
1
+ module Vimamsa
2
+ # Plays a demo inside the running editor (Help ▸ Demos), on fresh copies of
3
+ # files in scripts/demo, with the key log panel showing each key and the
4
+ # action it ran. The demos (DEMOS) are step files in scripts/demo:
5
+ # basic square/steps.txt (recorded with Help ▸ Demos ▸ Record demo):
6
+ # editing shapes.rb into a Square class, with Insert and Visual
7
+ # mode, easy jump, search and replace and the action search.
8
+ # (demo_steps.txt is the README demo that record_demo.rb
9
+ # records into img/demo.gif.)
10
+ # easyjump demo_easyjump_steps.txt: moving and selecting with easy jump
11
+ # files demo_files_steps.txt: switching between files (tab, browse
12
+ # mode h / l, the file explorer , f)
13
+ #
14
+ # The keys go to the key bindings as if typed. At the end the files go back
15
+ # to how they were and the demo starts again, until it is stopped: the same
16
+ # menu item again, the key log's ✕, or switching to a buffer that isn't one
17
+ # of the demo's (or the file explorer).
18
+ # The buttons above the key log (and the help.demo.* actions) pause and
19
+ # continue it and change its speed. When it stops, the key log panel is
20
+ # hidden again if it wasn't shown before.
21
+ class DemoPlayer
22
+ DIR = File.expand_path("../../scripts/demo", __dir__)
23
+ STEPS = File.join(DIR, "demo_steps.txt")
24
+ # steps: the step file. files: copied and opened, the first one last (it
25
+ # is current and the newest in the buffer history). dir: a subfolder of
26
+ # WORK_DIR, emptied first, so the file explorer shows only these files.
27
+ # title, desc, action: for the Help ▸ Demos menu, the introduction page
28
+ # (help/intro.txt) and the demo list above the key log.
29
+ DEMOS = {
30
+ basic: { steps: File.join(DIR, "square", "steps.txt"), files: ["shapes.rb"], src: File.join(DIR, "square"),
31
+ title: "Basic editing", desc: "a Square class: editing, Visual mode, easy jump, replace, actions", action: "help.demo" },
32
+ easyjump: { steps: File.join(DIR, "demo_easyjump_steps.txt"), files: ["shapes.rb"],
33
+ title: "Easy jump", desc: "jump anywhere on the screen with a few keys", action: "help.demo.easyjump" },
34
+ files: { steps: File.join(DIR, "demo_files_steps.txt"), files: ["shapes.rb", "report.rb", "notes.txt"], dir: "files",
35
+ title: "Switching files", desc: "the last file, buffer history, the file explorer", action: "help.demo.files" },
36
+ }
37
+ WORK_DIR = File.join(Dir.tmpdir, "vimamsa-demo")
38
+
39
+ # Recorded demos (demo_recorder.rb): <user dir>/demos/<slug>/steps.txt and
40
+ # the files they start from
41
+ def self.user_demos_dir
42
+ File.join(vma_user_dir, "demos")
43
+ end
44
+
45
+ def self.user_demo_key(slug) = :"user_#{slug}"
46
+
47
+ # The recorded demos, like DEMOS. Their title, desc and files come from
48
+ # the steps file's header (title, desc, file). Each gets an action
49
+ # help.demo.user.<slug>.
50
+ def self.user_demos
51
+ Dir[File.join(user_demos_dir, "*", "steps.txt")].sort.to_h { |f|
52
+ dir = File.dirname(f)
53
+ slug = File.basename(dir)
54
+ steps = (parse(File.read(f)) rescue [])
55
+ head = lambda { |c| steps.find { |x, _| x == c }&.last }
56
+ files = steps.select { |x, _| x == "file" }.map(&:last)
57
+ key = user_demo_key(slug)
58
+ title = head.("title") || slug
59
+ action = "help.demo.user.#{slug}"
60
+ if vma.actions && !vma.actions.include?(action)
61
+ reg_act(action, proc { DemoPlayer.toggle(key) }, "Play the recorded demo #{title} (again: stop it)")
62
+ end
63
+ [key, { steps: f, files: files.empty? ? ["demo.txt"] : files, src: dir, dir: "user_#{slug}",
64
+ title: title, desc: head.("desc") || "recorded demo", action: action }]
65
+ }
66
+ end
67
+
68
+ # The built-in demos and the recorded ones
69
+ def self.demos
70
+ DEMOS.merge(user_demos)
71
+ end
72
+ SPEED = 0.7 # as the recording: every delay and wait is divided by this
73
+ RATES = [0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4] # user speeds, times SPEED
74
+
75
+ # Plays demo name; if it is the one playing, stops it instead (another
76
+ # one playing is stopped first, and the new one keeps its speed)
77
+ def self.toggle(name = :basic)
78
+ rate = nil
79
+ if @current&.running?
80
+ same = @current.name == name
81
+ rate = @current.rate
82
+ @current.stop("Demo stopped")
83
+ return if same
84
+ end
85
+ @current = rate ? new(name, rate: rate) : new(name)
86
+ @current.start
87
+ end
88
+
89
+ # The player started last
90
+ def self.current
91
+ @current
92
+ end
93
+
94
+ def self.current=(player)
95
+ @current = player
96
+ end
97
+
98
+ # The player that is playing (or paused), else nil with a message
99
+ def self.playing
100
+ return @current if @current&.running?
101
+ message("No demo is playing (Help ▸ Play demo)")
102
+ nil
103
+ end
104
+
105
+ def self.pause_toggle = playing&.toggle_pause
106
+ def self.faster = playing&.faster
107
+ def self.slower = playing&.slower
108
+ def self.stop = playing&.stop("Demo stopped")
109
+
110
+ attr_reader :buf, :rate, :rounds, :name
111
+
112
+ # speed: every delay and wait is divided by it (tests play it fast).
113
+ # loop: start again at the end. steps: [command, argument] pairs instead
114
+ # of demo_steps.txt (tests).
115
+ # demo: a demo entry like DEMOS' ({ steps:, files:, src: }) instead of
116
+ # name's (e.g. a steps file given to scripts/demo/record_demo.rb).
117
+ # controls: false hides the pause / speed controls (in a recording).
118
+ # on_finish: called with the stop message when the demo stops.
119
+ def initialize(name = :basic, speed: SPEED, loop: true, steps: nil, rate: 0.75, demo: nil, controls: true, on_finish: nil)
120
+ @name = name
121
+ @demo = demo || DemoPlayer.demos.fetch(name)
122
+ @controls = controls
123
+ @on_finish = on_finish
124
+ @steps_file = @demo[:steps]
125
+ @speed = speed
126
+ @loop = loop
127
+ @given_steps = steps
128
+ @rate = rate
129
+ @rounds = 0 # finished rounds
130
+ end
131
+
132
+ def running?
133
+ @running
134
+ end
135
+
136
+ def paused?
137
+ @paused
138
+ end
139
+
140
+ # Pause between two steps; a paused demo keeps its place
141
+ def pause
142
+ return if !@running || @paused
143
+ @paused = true
144
+ remove_timer
145
+ show_controls
146
+ message("Demo paused")
147
+ end
148
+
149
+ # Continue from where it was paused
150
+ def resume
151
+ return if !@running || !@paused
152
+ @paused = false
153
+ show_controls
154
+ message("Demo continues")
155
+ run_next
156
+ end
157
+
158
+ def toggle_pause
159
+ @paused ? resume : pause
160
+ end
161
+
162
+ # Speed, as a multiple of the normal one (one of RATES); applies from the
163
+ # next step
164
+ def set_rate(r)
165
+ @rate = r
166
+ show_controls if @running
167
+ message("Demo speed #{DemoPlayer.rate_label(r)}")
168
+ end
169
+
170
+ def faster
171
+ set_rate(RATES.find { |r| r > @rate } || RATES.last)
172
+ end
173
+
174
+ def slower
175
+ set_rate(RATES.reverse.find { |r| r < @rate } || RATES.first)
176
+ end
177
+
178
+ def self.rate_label(r)
179
+ "#{r.to_s.sub(/\.0\z/, "")}×"
180
+ end
181
+
182
+ # Seconds one round of demo name takes at the default speed (keys after
183
+ # an easy jump label count as one)
184
+ def self.duration(name, rate: new(name).rate)
185
+ secs = 0.0
186
+ delay = 0.4
187
+ parse(File.read(demos.fetch(name)[:steps])).each { |cmd, arg|
188
+ case cmd
189
+ when "wait" then secs += arg.to_f
190
+ when "delay" then delay = arg.to_f
191
+ when "keys" then secs += delay * arg.split.size
192
+ when "type" then secs += delay * arg.size
193
+ end
194
+ }
195
+ secs / (SPEED * rate)
196
+ end
197
+
198
+ # "40 s", "2 min"
199
+ def self.duration_text(name)
200
+ s = duration(name)
201
+ s < 90 ? "#{(s / 10).round * 10} s" : "#{(s / 60).round} min"
202
+ end
203
+
204
+ # The steps of demo_steps.txt as [command, argument] pairs (see its header)
205
+ def self.parse(text)
206
+ text.lines(chomp: true).filter_map { |line|
207
+ line = line.sub(/\s+#\s.*\z/, "") unless line.start_with?("type ")
208
+ next if line.strip.empty? || line.start_with?("#")
209
+ cmd, arg = line.split(" ", 2)
210
+ raise "demo_steps.txt: unknown command #{line.inspect}" if !%w[keys type wait delay target title desc file].include?(cmd)
211
+ [cmd, arg.to_s]
212
+ }
213
+ end
214
+
215
+ def start
216
+ if !@given_steps && !File.exist?(@steps_file)
217
+ message("Demo steps not found: #{@steps_file}")
218
+ return
219
+ end
220
+ @steps = @given_steps || DemoPlayer.parse(File.read(@steps_file))
221
+ # Started from a help page (the introduction): back to it at the end
222
+ @return_buf = vma.buf if vma.buf&.help_page
223
+ open_files
224
+ @keylog_was_shown = vma.gui.instance_variable_get(:@keylog_shown)
225
+ vma.gui.show_keylog_panel
226
+ DemoPlayer.current.stop if DemoPlayer.current&.running? && !DemoPlayer.current.equal?(self)
227
+ DemoPlayer.current = self
228
+ @running = true
229
+ @paused = false
230
+ show_controls
231
+ message("Playing the demo. Pause and speed: the buttons above the key log; ✕ stops it.")
232
+ begin_round
233
+ end
234
+
235
+ # Why it stopped (the message shown), e.g. "Demo finished"
236
+ attr_reader :stop_message
237
+
238
+ def stop(msg = nil)
239
+ return if !@running
240
+ @stop_message = msg
241
+ @running = false
242
+ @paused = false
243
+ remove_timer
244
+ in_demo = demo_buf?(vma.buf) || explorer_buf?(vma.buf)
245
+ vma.kbd.set_mode(:command) if demo_buf?(vma.buf)
246
+ vma.gui.keylog_panel.hide_demo_controls
247
+ # Back to the help page it was started from, unless the user went to
248
+ # another buffer
249
+ if in_demo && @return_buf && vma.buffers.list.any? { |b| b.equal?(@return_buf) }
250
+ vma.buffers.set_current_buffer_by_id(@return_buf.id)
251
+ end
252
+ # The key log panel goes away again if the demo opened it
253
+ vma.gui.hide_keylog_panel if !@keylog_was_shown
254
+ message(msg) if msg
255
+ @on_finish&.call(msg)
256
+ end
257
+
258
+ # The demo's buffers, the first file's first
259
+ attr_reader :bufs
260
+
261
+ private
262
+
263
+ def demo_buf?(b)
264
+ @bufs.any? { |x| x.equal?(b) }
265
+ end
266
+
267
+ # The file explorer (, f), which the files demo uses
268
+ def explorer_buf?(b)
269
+ b.respond_to?(:module) && b.module.is_a?(FileManager)
270
+ end
271
+
272
+ # Fresh copies of the demo's files (so the demo can edit them), opened
273
+ # with the first one last
274
+ def open_files
275
+ dir = WORK_DIR
276
+ if @demo[:dir]
277
+ dir = File.join(WORK_DIR, @demo[:dir])
278
+ FileUtils.rm_rf(dir) if dir.start_with?(WORK_DIR + "/")
279
+ end
280
+ FileUtils.mkdir_p(dir)
281
+ files = @demo[:files].map { |f|
282
+ path = File.join(dir, f)
283
+ FileUtils.cp(File.join(@demo[:src] || DIR, f), path)
284
+ old = vma.buffers.list.find { |b| b.fname == path }
285
+ vma.buffers.close_buffer(old.id) if old
286
+ path
287
+ }
288
+ @bufs = files.reverse.map { |f| open_new_file(f) }.reverse
289
+ @buf = @bufs[0]
290
+ @originals = @bufs.map { |b| File.read(b.fname) }
291
+ end
292
+
293
+
294
+ # Each round starts with an empty key log
295
+ def begin_round
296
+ vma.gui.keylog_panel.clear
297
+ vma.kbd.set_mode(:command)
298
+ @queue = [] # [delay in s, proc]
299
+ @delay = 0.4
300
+ @target = nil
301
+ @steps.each { |cmd, arg| plan(cmd, arg) }
302
+ run_next
303
+ end
304
+
305
+ # The file as it was at the start, and the cursor at the top, for the
306
+ # next round
307
+ def restart
308
+ @rounds += 1
309
+ vma.kbd.set_mode(:command)
310
+ # The files as at the start, and the buffer history in the same order
311
+ @bufs.zip(@originals).reverse_each { |b, text|
312
+ vma.buffers.set_current_buffer(b.id)
313
+ b.revert_by_diff(text.dup)
314
+ b.set_pos(0)
315
+ b.view&.handle_deltas
316
+ }
317
+ begin_round
318
+ end
319
+
320
+ def remove_timer
321
+ GLib::Source.remove(@timer) rescue nil if @timer
322
+ @timer = nil
323
+ end
324
+
325
+ def show_controls
326
+ return if !@controls
327
+ vma.gui.keylog_panel.show_demo_controls(paused: @paused, rate: DemoPlayer.rate_label(@rate), demo: @name)
328
+ end
329
+
330
+ def plan(cmd, arg)
331
+ case cmd
332
+ when "wait" then @queue << [arg.to_f, nil]
333
+ when "delay" then @delay = arg.to_f
334
+ when "target" then @queue << [0, proc { @target = arg }]
335
+ when "keys"
336
+ arg.split.each { |k|
337
+ if k == "@JUMP_LABEL@"
338
+ # The label easy jump gave the target line: known only once the
339
+ # labels are drawn, for this window size
340
+ @queue << [@delay, proc { type_jump_label }]
341
+ else
342
+ @queue << [@delay, proc { press(k) }]
343
+ end
344
+ }
345
+ when "type"
346
+ arg.each_char { |c| @queue << [@delay, proc { press(c == " " ? "space" : c) }] }
347
+ end
348
+ end
349
+
350
+ # Waits for the next step and runs it. The step leaves the queue only when
351
+ # it runs, so a pause in between plays it, with its whole delay, on resume.
352
+ def run_next
353
+ return if !@running || @paused
354
+ if !demo_buf?(vma.buf) && !explorer_buf?(vma.buf)
355
+ stop("Demo stopped (another buffer was opened: #{vma.buf&.fname || vma.buf.class})")
356
+ return
357
+ end
358
+ # A paste (p, P, ctrl-v) reads the clipboard asynchronously: the next
359
+ # key waits for it (at most 2 s: the flag stays set if the read fails)
360
+ if vma.buf.instance_variable_get(:@clipboard_paste_running)
361
+ @paste_wait ||= Process.clock_gettime(Process::CLOCK_MONOTONIC)
362
+ if Process.clock_gettime(Process::CLOCK_MONOTONIC) - @paste_wait < 2
363
+ @timer = GLib::Timeout.add(20) { @timer = nil; run_next; false }
364
+ return
365
+ end
366
+ end
367
+ @paste_wait = nil
368
+ if @queue.empty?
369
+ @loop ? restart : stop("Demo finished")
370
+ return
371
+ end
372
+ delay = @queue.first[0]
373
+ @timer = GLib::Timeout.add([(delay / (@speed * @rate) * 1000).round, 1].max) {
374
+ @timer = nil
375
+ if @running && !@paused
376
+ _delay, action = @queue.shift
377
+ begin
378
+ action&.call
379
+ rescue => e
380
+ stop("Demo stopped: #{e.message}")
381
+ end
382
+ run_next
383
+ end
384
+ false
385
+ }
386
+ end
387
+
388
+ # One key, as the GUI passes it to the key bindings: "ctrl" alone is a
389
+ # press and a release (vimlike's ctrl! leaves Insert mode)
390
+ def press(key)
391
+ vma.kbd.match_key_conf(key, nil, :key_press)
392
+ vma.kbd.match_key_conf("#{key}!", nil, :key_release) if %w[ctrl alt shift].include?(key)
393
+ v = vma.buf.view
394
+ v.handle_deltas
395
+ v.draw_cursor
396
+ end
397
+
398
+ # Types the label easy jump gave the target. A target not shown in this
399
+ # window (smaller than the one the demo was recorded in) has no label:
400
+ # easy jump is cancelled and the cursor goes there directly, where the
401
+ # jump would have taken it.
402
+ def type_jump_label
403
+ keys = jump_label
404
+ tp = target_pos
405
+ if keys == ["esc"] && tp
406
+ press("esc")
407
+ vma.buf.set_pos(tp)
408
+ v = vma.buf.view
409
+ v&.handle_deltas
410
+ v&.draw_cursor
411
+ else
412
+ keys.each { |c| press(c) }
413
+ end
414
+ end
415
+
416
+ # The easy jump label of the start of the line @target names: "line N"
417
+ # (1-based), or else the first line holding that text
418
+ def jump_label
419
+ ej = EasyJump.class_variable_get(:@@cur) rescue nil
420
+ tp = target_pos
421
+ return ["esc"] if ej.nil? || tp.nil?
422
+ marks = ej.instance_variable_get(:@easy_jump_wsmarks)
423
+ seq = ej.instance_variable_get(:@jump_sequence)
424
+ n = marks.index(tp)
425
+ if n.nil?
426
+ # Not labelled here (e.g. not visible in this window): the nearest
427
+ # labelled place on the same line
428
+ ls = line_start_of(tp)
429
+ le = vma.buf.index("\n", tp) || vma.buf.size
430
+ near = marks.each_with_index.select { |m, _| m >= ls && m <= le }.min_by { |m, _| (m - tp).abs }
431
+ n = near&.last
432
+ end
433
+ n.nil? || seq[n].nil? ? ["esc"] : seq[n].downcase.chars
434
+ end
435
+
436
+ # Where @target says easy jump goes: "line N col C" (1-based, the place
437
+ # the recorder saw the cursor land), "line N" (its start), or the start
438
+ # of the first line holding the text
439
+ def target_pos
440
+ return nil if @target.nil?
441
+ if (m = /\Aline (\d+)(?: col (\d+))?\z/.match(@target))
442
+ l = m[1].to_i - 1
443
+ return nil if l < 0 || l > vma.buf.line_ends.size
444
+ ls = l == 0 ? 0 : vma.buf.line_ends[l - 1] + 1
445
+ return ls if m[2].nil?
446
+ le = vma.buf.index("\n", ls) || vma.buf.size
447
+ return (ls + m[2].to_i - 1).clamp(ls, le)
448
+ end
449
+ i = vma.buf.index(@target)
450
+ return nil if i.nil?
451
+ line_start_of(i)
452
+ end
453
+
454
+ def line_start_of(pos)
455
+ pos == 0 ? 0 : (vma.buf.rindex("\n", pos - 1) || -1) + 1
456
+ end
457
+ end
458
+ end # module Vimamsa