vimamsa 0.1.27 → 0.1.29

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