vimamsa 0.1.26 → 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 (229) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/appimage.yml +93 -0
  3. data/.gitignore +4 -1
  4. data/Dockerfile +7 -4
  5. data/README.md +195 -99
  6. data/custom_example.rb +59 -17
  7. data/doc/TODO.md +678 -0
  8. data/doc/action_names_plan.md +442 -0
  9. data/doc/bundling_plan.md +570 -0
  10. data/doc/delta_syntax_plan.md +293 -0
  11. data/doc/demo_recording_plan.md +288 -0
  12. data/doc/design_goals.md +27 -0
  13. data/doc/intro_page_plan.md +266 -0
  14. data/doc/key_bindings_report_plan.md +213 -0
  15. data/doc/line_tree_plan.md +89 -0
  16. data/doc/missing_vim_bindings.md +61 -0
  17. data/doc/modules_settings_plan.md +433 -0
  18. data/doc/refactor_dedup_plan.md +117 -0
  19. data/doc/registries_plan.md +202 -0
  20. data/doc/startup_speed_plan.md +97 -0
  21. data/doc/treap_visualizer.html +585 -0
  22. data/doc/unused_code_plan.md +120 -0
  23. data/doc/vim_compatibility_plan.md +451 -0
  24. data/doc/vimlike_vim_conflicts.md +262 -0
  25. data/doc/vm_snapshots_plan.md +146 -0
  26. data/doc/wysiwyg_plan.md +231 -0
  27. data/exe/run_tests.rb +124 -3
  28. data/exe/vimamsa +68 -5
  29. data/fonts/OFL-Oxanium.txt +93 -0
  30. data/fonts/Oxanium[wght].ttf +0 -0
  31. data/help/intro.txt +30 -0
  32. data/help/vim_intro.txt +31 -0
  33. data/icon.png +0 -0
  34. data/img/demo.gif +0 -0
  35. data/img/demo.mp4 +0 -0
  36. data/lang/hyperplaintext.lang +7 -1
  37. data/lib/vimamsa/ack.rb +13 -1
  38. data/lib/vimamsa/actions.rb +385 -37
  39. data/lib/vimamsa/appimage_env.rb +35 -0
  40. data/lib/vimamsa/audio.rb +2 -5
  41. data/lib/vimamsa/bookmarks.rb +302 -0
  42. data/lib/vimamsa/buffer.rb +675 -118
  43. data/lib/vimamsa/buffer_changetext.rb +150 -68
  44. data/lib/vimamsa/buffer_cursor.rb +7 -0
  45. data/lib/vimamsa/buffer_list.rb +6 -2
  46. data/lib/vimamsa/buffer_manager.rb +10 -7
  47. data/lib/vimamsa/bundled_fonts.rb +32 -0
  48. data/lib/vimamsa/cli_help.rb +75 -0
  49. data/lib/vimamsa/clipboard.rb +9 -3
  50. data/lib/vimamsa/conf.rb +68 -3
  51. data/lib/vimamsa/constants.rb +3 -0
  52. data/lib/vimamsa/crash_handler.rb +149 -0
  53. data/lib/vimamsa/debug.rb +6 -3
  54. data/lib/vimamsa/demo_player.rb +458 -0
  55. data/lib/vimamsa/demo_recorder.rb +277 -0
  56. data/lib/vimamsa/desktop_install.rb +96 -0
  57. data/lib/vimamsa/diff_buffer.rb +40 -11
  58. data/lib/vimamsa/dot_repeat.rb +104 -0
  59. data/lib/vimamsa/easy_jump.rb +57 -3
  60. data/lib/vimamsa/editor.rb +327 -78
  61. data/lib/vimamsa/encrypt.rb +78 -29
  62. data/lib/vimamsa/file_finder.rb +4 -19
  63. data/lib/vimamsa/file_history.rb +4 -1
  64. data/lib/vimamsa/file_manager.rb +46 -31
  65. data/lib/vimamsa/fold.rb +226 -0
  66. data/lib/vimamsa/form_input.rb +388 -0
  67. data/lib/vimamsa/gui.rb +603 -119
  68. data/lib/vimamsa/gui_command_line.rb +247 -0
  69. data/lib/vimamsa/gui_dialog.rb +94 -42
  70. data/lib/vimamsa/gui_file_panel.rb +356 -25
  71. data/lib/vimamsa/gui_form_generator.rb +75 -40
  72. data/lib/vimamsa/gui_func_panel.rb +67 -22
  73. data/lib/vimamsa/gui_image.rb +3 -0
  74. data/lib/vimamsa/gui_key_hint.rb +165 -0
  75. data/lib/vimamsa/gui_keylog_panel.rb +408 -0
  76. data/lib/vimamsa/gui_menu.rb +84 -53
  77. data/lib/vimamsa/gui_select_window.rb +80 -75
  78. data/lib/vimamsa/gui_settings.rb +86 -41
  79. data/lib/vimamsa/gui_sourceview.rb +280 -92
  80. data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
  81. data/lib/vimamsa/gui_text.rb +63 -2
  82. data/lib/vimamsa/gui_theme.rb +615 -0
  83. data/lib/vimamsa/hook.rb +5 -0
  84. data/lib/vimamsa/hyper_plain_text.rb +3 -0
  85. data/lib/vimamsa/intro_page.rb +368 -0
  86. data/lib/vimamsa/key_actions.rb +382 -193
  87. data/lib/vimamsa/key_binding_tree.rb +467 -273
  88. data/lib/vimamsa/key_bindings_notepad.rb +45 -0
  89. data/lib/vimamsa/key_bindings_report.rb +484 -0
  90. data/lib/vimamsa/key_bindings_vim.rb +104 -0
  91. data/lib/vimamsa/key_bindings_vimlike.rb +353 -289
  92. data/lib/vimamsa/key_names.rb +80 -0
  93. data/lib/vimamsa/langservp.rb +271 -54
  94. data/lib/vimamsa/line_tree.rb +367 -0
  95. data/lib/vimamsa/macro.rb +23 -3
  96. data/lib/vimamsa/main.rb +14 -8
  97. data/lib/vimamsa/rbvma.rb +41 -1
  98. data/lib/vimamsa/registers.rb +125 -0
  99. data/lib/vimamsa/run_ruby.rb +64 -0
  100. data/lib/vimamsa/search.rb +3 -0
  101. data/lib/vimamsa/search_replace.rb +15 -14
  102. data/lib/vimamsa/string_util.rb +134 -40
  103. data/lib/vimamsa/terminal.rb +3 -0
  104. data/lib/vimamsa/test_framework.rb +28 -2
  105. data/lib/vimamsa/tests.rb +11 -8
  106. data/lib/vimamsa/text_objects.rb +310 -0
  107. data/lib/vimamsa/text_transforms.rb +18 -0
  108. data/lib/vimamsa/tmp_user_dir.rb +39 -0
  109. data/lib/vimamsa/user_dirs.rb +83 -0
  110. data/lib/vimamsa/util.rb +46 -7
  111. data/lib/vimamsa/version.rb +1 -1
  112. data/lib/vimamsa/vim_core.rb +355 -0
  113. data/lib/vimamsa/vim_operators.rb +365 -0
  114. data/lib/vimamsa.rb +0 -1
  115. data/modules/calculator/calculator.rb +5 -5
  116. data/modules/color_highlight/color_highlight.rb +118 -0
  117. data/modules/color_highlight/color_highlight_info.rb +3 -0
  118. data/modules/dictation/dictation.rb +1054 -0
  119. data/modules/dictation/dictation_info.rb +3 -0
  120. data/modules/dictation/whisper_worker.py +273 -0
  121. data/modules/ollama/ollama.rb +107 -0
  122. data/modules/ollama/ollama_info.rb +3 -0
  123. data/modules/terminal/terminal.rb +5 -5
  124. data/modules/tts/tts.rb +119 -0
  125. data/modules/tts/tts_info.rb +3 -0
  126. data/run_e2e_vm.sh +28 -0
  127. data/run_tests_vm.rb +138 -0
  128. data/scripts/appimage/AppRun +54 -0
  129. data/scripts/appimage/Dockerfile +18 -0
  130. data/scripts/appimage/build.sh +19 -0
  131. data/scripts/appimage/build_in_container.sh +192 -0
  132. data/scripts/appimage/smoke_test.sh +27 -0
  133. data/scripts/demo/demo_easyjump_steps.txt +60 -0
  134. data/scripts/demo/demo_files_steps.txt +52 -0
  135. data/scripts/demo/demo_steps.txt +67 -0
  136. data/scripts/demo/notes.txt +6 -0
  137. data/scripts/demo/record_demo.rb +156 -0
  138. data/scripts/demo/report.rb +7 -0
  139. data/scripts/demo/shapes.rb +29 -0
  140. data/scripts/demo/square/shapes.rb +29 -0
  141. data/scripts/demo/square/steps.txt +185 -0
  142. data/scripts/lib/perf_startup.rb +138 -0
  143. data/scripts/lib/script_helpers.rb +53 -0
  144. data/scripts/perf_compare.rb +34 -0
  145. data/scripts/profile_startup.rb +72 -0
  146. data/styles/134272-molokai.xml +3 -0
  147. data/styles/molokai_cyber.xml +48 -0
  148. data/styles/molokai_edit.xml +1 -0
  149. data/tests/support/virtual_keyboard.rb +254 -0
  150. data/tests/support/virtual_mic.rb +67 -0
  151. data/tests/test_ack.rb +22 -0
  152. data/tests/test_action_ids.rb +232 -0
  153. data/tests/test_auto_format.rb +122 -0
  154. data/tests/test_autocomplete.rb +211 -0
  155. data/tests/test_autosave.rb +145 -0
  156. data/tests/test_basic_editing.rb +46 -16
  157. data/tests/test_bookmarks.rb +206 -0
  158. data/tests/test_bracket_match.rb +88 -0
  159. data/tests/test_bundling.rb +92 -0
  160. data/tests/test_cli_help.rb +64 -0
  161. data/tests/test_color_highlight.rb +92 -0
  162. data/tests/test_command_line.rb +79 -0
  163. data/tests/test_command_line_file.rb +27 -0
  164. data/tests/test_context_menu_file.rb +147 -0
  165. data/tests/test_copy_paste.rb +106 -26
  166. data/tests/test_crash_handler.rb +39 -0
  167. data/tests/test_custom_two_phase.rb +92 -0
  168. data/tests/test_demo_player.rb +301 -0
  169. data/tests/test_demo_recorder.rb +202 -0
  170. data/tests/test_dialog_callback_errors.rb +44 -0
  171. data/tests/test_dictation.rb +294 -0
  172. data/tests/test_diff_buffer_name.rb +51 -0
  173. data/tests/test_dot_repeat.rb +88 -0
  174. data/tests/test_double_click.rb +41 -0
  175. data/tests/test_drag_selection.rb +125 -0
  176. data/tests/test_e2e_cmdline.rb +116 -0
  177. data/tests/test_e2e_dictation.rb +98 -0
  178. data/tests/test_e2e_replace_char.rb +58 -0
  179. data/tests/test_easy_jump.rb +93 -0
  180. data/tests/test_extract_file_links.rb +55 -0
  181. data/tests/test_file_format.rb +217 -0
  182. data/tests/test_file_manager.rb +72 -0
  183. data/tests/test_file_panel.rb +269 -0
  184. data/tests/test_flash.rb +78 -0
  185. data/tests/test_fold.rb +111 -0
  186. data/tests/test_font_refresh.rb +56 -0
  187. data/tests/test_form_input.rb +203 -0
  188. data/tests/test_func_panel.rb +68 -0
  189. data/tests/test_grep_apply.rb +46 -0
  190. data/tests/test_gtk_sync.rb +126 -0
  191. data/tests/test_hpt_highlight_visible.rb +75 -0
  192. data/tests/test_hpt_link_highlight.rb +82 -0
  193. data/tests/test_intro_page.rb +437 -0
  194. data/tests/test_jump_to_file.rb +41 -0
  195. data/tests/test_key_bindings.rb +208 -0
  196. data/tests/test_key_categories.rb +163 -0
  197. data/tests/test_keylog_panel.rb +261 -0
  198. data/tests/test_line_ends.rb +51 -0
  199. data/tests/test_line_tree.rb +86 -0
  200. data/tests/test_lsp_clangd.rb +281 -0
  201. data/tests/test_lsp_jump.rb +87 -0
  202. data/tests/test_lsp_position_encoding.rb +49 -0
  203. data/tests/test_lsp_symbols.rb +68 -0
  204. data/tests/test_macro.rb +137 -0
  205. data/tests/test_markdown_highlight.rb +57 -0
  206. data/tests/test_mode_badge.rb +41 -0
  207. data/tests/test_module_interface.rb +9 -9
  208. data/tests/test_notepad_bindings.rb +67 -0
  209. data/tests/test_perf_startup.rb +28 -0
  210. data/tests/test_popup_queue.rb +51 -0
  211. data/tests/test_random_edit.rb +109 -0
  212. data/tests/test_registers.rb +122 -0
  213. data/tests/test_revert.rb +127 -0
  214. data/tests/test_run_ruby.rb +77 -0
  215. data/tests/test_startup_file.rb +58 -0
  216. data/tests/test_status_area.rb +50 -0
  217. data/tests/test_text_objects.rb +163 -0
  218. data/tests/test_text_transforms.rb +341 -0
  219. data/tests/test_tmp_user_dir.rb +79 -0
  220. data/tests/test_tts.rb +68 -0
  221. data/tests/test_undo.rb +34 -34
  222. data/tests/test_user_dirs.rb +122 -0
  223. data/tests/test_vim_bindings.rb +353 -0
  224. data/tests/test_vim_conflicts.rb +100 -0
  225. data/tests/test_vim_operators.rb +116 -0
  226. data/tests/test_window_layout.rb +302 -0
  227. data/vimamsa.gemspec +3 -1
  228. data/vm_guest_run.sh +129 -0
  229. metadata +167 -10
@@ -0,0 +1,72 @@
1
+ # Where the editor's startup time goes. Loaded with -r before exe/vimamsa:
2
+ #
3
+ # ruby -r ./scripts/profile_startup.rb exe/vimamsa [file]
4
+ #
5
+ # Prints to stderr, at exit: the slow requires (a tree, > 10 ms) and, after
6
+ # the code is loaded, the slow calls of vimamsa's own methods (a tree, > 10
7
+ # ms, inclusive times), with their start offset from when this file was
8
+ # loaded. Tracing adds overhead, so method times are somewhat too high.
9
+ # Env: VMA_PROFILE_MIN_MS (10), VMA_PROFILE_DEPTH (6).
10
+ module StartupProfile
11
+ T0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
12
+ MIN = (ENV["VMA_PROFILE_MIN_MS"] || 10).to_f / 1000
13
+ DEPTH = (ENV["VMA_PROFILE_DEPTH"] || 6).to_i
14
+ @lines = [] # [start, depth, text]
15
+ @rdepth = 0
16
+ @stack = []
17
+
18
+ def self.now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
19
+
20
+ def self.note(start, depth, dt, text)
21
+ @lines << [start, depth, dt, text] if dt >= MIN
22
+ end
23
+
24
+ def self.require_wrap(name)
25
+ t = now
26
+ @rdepth += 1
27
+ d = @rdepth
28
+ yield
29
+ ensure
30
+ @rdepth -= 1
31
+ note(t, d, now - t, "require #{name}")
32
+ end
33
+
34
+ OWN = %r{/(lib/vimamsa|modules|exe)/}
35
+ @tp = TracePoint.new(:call, :return) { |tp|
36
+ next unless OWN.match?(tp.path)
37
+ case tp.event
38
+ when :call
39
+ @stack << now
40
+ when :return
41
+ t = @stack.pop
42
+ next if t.nil?
43
+ d = @stack.size
44
+ next if d >= DEPTH
45
+ what = "#{tp.defined_class}##{tp.method_id}"
46
+ note(t, d + 1, now - t, "#{what} (#{tp.path.sub(%r{.*/(lib|modules|exe)/}, '\1/')}:#{tp.lineno})")
47
+ end
48
+ }
49
+
50
+ def self.report
51
+ @tp.disable
52
+ STDERR.puts "=== startup profile (offset s, inclusive ms) ==="
53
+ @lines.sort_by { |s, d, _, _| [s, d] }.each { |s, d, dt, text|
54
+ STDERR.puts format("%6.3f %7.1f %s%s", s - T0, dt * 1000, " " * (d - 1), text)
55
+ }
56
+ end
57
+
58
+ # Method calls are traced from when the code is loaded (tracing during the
59
+ # requires would slow them down a lot)
60
+ def self.start_tracing = @tp.enable
61
+ end
62
+
63
+ module Kernel
64
+ alias_method :__profile_require, :require
65
+ def require(name)
66
+ r = StartupProfile.require_wrap(name) { __profile_require(name) }
67
+ StartupProfile.start_tracing if name == "vimamsa"
68
+ r
69
+ end
70
+ end
71
+
72
+ at_exit { StartupProfile.report }
@@ -29,5 +29,8 @@
29
29
  <style foreground="#465457" name="def:comment"/>
30
30
  <style background="#3e3d32" name="current-line"/>
31
31
  <style background="#3b3a32" foreground="#bcbca7" name="line-numbers"/>
32
+ <style name="def:hyperlink" foreground="lightblue" bold="true" />
33
+ <style name="def:bold" bold="true" />
34
+ <style name="def:emphasis" italic="true" />
32
35
  </style-scheme>
33
36
 
@@ -0,0 +1,48 @@
1
+ <?xml version="1.0" ?>
2
+ <style-scheme id="molokai_cyber" name="Molokai_cyber" version="1.0">
3
+ <color name="lightblue" value="#00FFF8"/>
4
+
5
+ <author/>
6
+ <_description>
7
+ Molokai variant with navy backgrounds to match the Cyberpunk glow GUI theme.
8
+ </_description>
9
+
10
+ <color name="heading" value="#ffffff" />
11
+ <style name="selection" foreground="#ffffff" background="#10bd8e"/>
12
+
13
+
14
+ <style foreground="#AE81FF" name="def:boolean"/>
15
+ <style foreground="#AE81FF" name="def:number"/>
16
+ <style foreground="#E6DB74" name="def:string"/>
17
+ <style bold="true" foreground="#AE81FF" name="def:constant"/>
18
+ <style background="#F8F8F0" foreground="#ffffff" name="cursor"/>
19
+ <style background="#13354A" name="diff:added-line"/>
20
+ <style background="#4C4745" foreground="#89807D" name="diff:changed-line"/>
21
+ <style background="#1E0010" foreground="#960050" name="diff:removed-line"/>
22
+ <style background="#1E0010" foreground="#960050" name="def:error"/>
23
+ <style foreground="#A6E22E" name="def:function"/>
24
+ <style foreground="#FD971F" name="def:identifier"/>
25
+ <style bold="true" foreground="#F92672" name="def:keyword"/>
26
+ <style background="#FD971F" bold="true" foreground="#000000" name="bracket-match"/>
27
+ <style foreground="#A6E22E" name="def:preprocessor"/>
28
+ <style background="#455354" foreground="#FFFFFF" name="search-match"/>
29
+ <style background="#bg" foreground="#66D9EF" italic="true" name="def:specials"/>
30
+ <style bold="true" foreground="#F92672" name="def:statement"/>
31
+ <style foreground="#66D9EF" name="def:type"/>
32
+ <style background="#0b0c0c" foreground="#F8F8F2" name="text"/>
33
+ <style foreground="#b0b0b0" name="def:comment"/>
34
+ <style background="#141d33" name="current-line"/>
35
+ <style background="#0b0c0c" foreground="#8fb0b8" name="line-numbers"/>
36
+
37
+ <style name="def:title" scale="2.0" bold="true" />
38
+ <style name="def:hyperlink" foreground="lightblue" bold="true" />
39
+ <style name="def:heading0" scale="2.0" bold="true" foreground="heading" />
40
+ <style name="def:heading1" scale="1.75" bold="true" foreground="heading" underline="true" />
41
+ <style name="def:heading2" scale="1.5" foreground="heading" underline="single" />
42
+ <style name="def:heading3" scale="1.25" bold="true" foreground="heading" />
43
+ <style name="def:heading4" scale="1.175" bold="true" foreground="heading" />
44
+ <style name="def:heading5" scale="1.1" bold="true" foreground="heading" />
45
+ <style name="def:heading6" scale="1.0" bold="true" foreground="heading" />
46
+ <style name="def:bold" bold="true" />
47
+ <style name="def:emphasis" italic="true" />
48
+ </style-scheme>
@@ -48,5 +48,6 @@
48
48
  <style name="def:heading5" scale="1.1" bold="true" foreground="heading" />
49
49
  <style name="def:heading6" scale="1.0" bold="true" foreground="heading" />
50
50
  <style name="def:bold" bold="true" />
51
+ <style name="def:emphasis" italic="true" />
51
52
  </style-scheme>
52
53
 
@@ -0,0 +1,254 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A minimal Linux uinput virtual keyboard. Creates a real kernel input device and
4
+ # emits genuine evdev key events, which flow kernel -> libinput -> compositor ->
5
+ # the focused GTK app — i.e. as realistic as a physical keyboard, including
6
+ # separate, precisely-timed key down/up events.
7
+ #
8
+ # Talks to /dev/uinput directly with Ruby's built-in IO#ioctl (no FFI needed).
9
+ # Requires write access to /dev/uinput (e.g. granted to the user via an ACL).
10
+ #
11
+ # References: linux/uinput.h, linux/input.h, linux/input-event-codes.h.
12
+ class VirtualKeyboard
13
+ # event types
14
+ EV_SYN = 0x00
15
+ EV_KEY = 0x01
16
+ SYN_REPORT = 0x00
17
+
18
+ KEY_LEFTSHIFT = 42
19
+ KEY_RIGHTALT = 100 # AltGr on international layouts
20
+
21
+ # _IOC encoding (asm-generic/ioctl.h)
22
+ IOC_NONE = 0
23
+ IOC_WRITE = 1
24
+ NRBITS = 8
25
+ TYPEBITS = 8
26
+ SIZEBITS = 14
27
+ NRSHIFT = 0
28
+ TYPESHIFT = NRSHIFT + NRBITS
29
+ SIZESHIFT = TYPESHIFT + TYPEBITS
30
+ DIRSHIFT = SIZESHIFT + SIZEBITS
31
+ UINPUT_IOCTL_BASE = "U".ord
32
+
33
+ def self._ioc(dir, type, nr, size)
34
+ (dir << DIRSHIFT) | (type << TYPESHIFT) | (nr << NRSHIFT) | (size << SIZESHIFT)
35
+ end
36
+
37
+ UI_SET_EVBIT = _ioc(IOC_WRITE, UINPUT_IOCTL_BASE, 100, 4) # int arg
38
+ UI_SET_KEYBIT = _ioc(IOC_WRITE, UINPUT_IOCTL_BASE, 101, 4) # int arg
39
+ # struct uinput_setup: input_id(8) + char name[80] + __u32 ff_effects_max = 92
40
+ UINPUT_SETUP_SIZE = 8 + 80 + 4
41
+ UI_DEV_SETUP = _ioc(IOC_WRITE, UINPUT_IOCTL_BASE, 3, UINPUT_SETUP_SIZE)
42
+ UI_DEV_CREATE = _ioc(IOC_NONE, UINPUT_IOCTL_BASE, 1, 0) # 0x5501
43
+ UI_DEV_DESTROY = _ioc(IOC_NONE, UINPUT_IOCTL_BASE, 2, 0) # 0x5502
44
+
45
+ BUS_USB = 0x03
46
+
47
+ # Advertise every standard key code so any character can be typed.
48
+ KEYS = (1..248).to_a.freeze
49
+
50
+ def self.available?
51
+ File.writable?("/dev/uinput")
52
+ end
53
+
54
+ def initialize
55
+ @fd = nil
56
+ end
57
+
58
+ def open
59
+ @fd = File.open("/dev/uinput", File::WRONLY | File::NONBLOCK)
60
+ @fd.sync = true # unbuffered: nothing to flush at close (avoids EINVAL)
61
+
62
+ @fd.ioctl(UI_SET_EVBIT, EV_KEY)
63
+ @fd.ioctl(UI_SET_EVBIT, EV_SYN)
64
+ KEYS.each { |code| @fd.ioctl(UI_SET_KEYBIT, code) }
65
+
66
+ # struct uinput_setup { struct input_id id; char name[80]; __u32 ff_effects_max; }
67
+ # struct input_id { __u16 bustype, vendor, product, version; }
68
+ name = "vimamsa-virtual-kbd"
69
+ setup = [BUS_USB, 0x1234, 0x5678, 1].pack("S<4") # input_id
70
+ setup << name.ljust(80, "\x00")[0, 80] # name[80]
71
+ setup << [0].pack("L<") # ff_effects_max
72
+ @fd.ioctl(UI_DEV_SETUP, setup)
73
+ @fd.ioctl(UI_DEV_CREATE, 0)
74
+
75
+ sleep 0.4 # let udev/libinput/the compositor enumerate the new device
76
+ self
77
+ end
78
+
79
+ # struct input_event { struct timeval time; __u16 type, code; __s32 value; }
80
+ # timeval = { __kernel_long_t tv_sec, tv_usec } -> 2 x 64-bit on x86_64.
81
+ def emit(type, code, value)
82
+ @fd.write([0, 0, type, code, value].pack("q<q<S<S<l<"))
83
+ end
84
+
85
+ def syn
86
+ emit(EV_SYN, SYN_REPORT, 0)
87
+ end
88
+
89
+ def key(code, value)
90
+ emit(EV_KEY, code, value)
91
+ syn
92
+ end
93
+
94
+ def key_down(code) = key(code, 1)
95
+ def key_up(code) = key(code, 0)
96
+
97
+ # Press and immediately release a key (a "tap").
98
+ def tap(code, pause: 0.02)
99
+ key_down(code)
100
+ sleep pause
101
+ key_up(code)
102
+ sleep pause
103
+ end
104
+
105
+ # Hold `mods` (one code or an array, e.g. AltGr+Shift), tap `code` inside them,
106
+ # release in reverse order (e.g. Shift-; or AltGr-q).
107
+ def chord(mods, code, pause: 0.02)
108
+ mods = Array(mods)
109
+ mods.each { |m| key_down(m); sleep pause }
110
+ key_down(code)
111
+ sleep pause
112
+ key_up(code)
113
+ sleep pause
114
+ mods.reverse_each { |m| key_up(m); sleep pause }
115
+ end
116
+
117
+ def close
118
+ return unless @fd
119
+ begin
120
+ @fd.ioctl(UI_DEV_DESTROY, 0)
121
+ rescue SystemCallError
122
+ # device may already be gone
123
+ end
124
+ begin
125
+ @fd.close
126
+ rescue SystemCallError
127
+ # closing /dev/uinput can report EINVAL on the final flush; harmless.
128
+ end
129
+ @fd = nil
130
+ end
131
+ end
132
+
133
+ # Types characters through a VirtualKeyboard using the *active* keyboard layout,
134
+ # so shifted symbols like ":" and "!" land on the right physical key regardless
135
+ # of keymap. The layout is read from `xmodmap -pke` (keysym -> X keycode + shift
136
+ # level; evdev code = X keycode - 8). Character -> keysym name comes from
137
+ # Gdk::Keyval.to_name (for ASCII, keyval == codepoint).
138
+ #
139
+ # (Gdk::Display#map_keyval would be the native way, but Gdk::KeymapKey field
140
+ # readers segfault in the current ruby-gnome binding.)
141
+ class KeyTyper
142
+ def self.available?
143
+ system("which xmodmap > /dev/null 2>&1")
144
+ end
145
+
146
+ def initialize(vkb)
147
+ @vkb = vkb
148
+ @map = parse_xmodmap
149
+ end
150
+
151
+ # Type an ASCII string, e.g. type(":!ls")
152
+ def type(str)
153
+ str.each_char { |c| tap_keysym(Gdk::Keyval.to_name(c.ord)) }
154
+ end
155
+
156
+ # Tap a key by keysym name, e.g. tap_keysym("Return"), tap_keysym("Escape"),
157
+ # tap_keysym("adiaeresis") (AltGr-q on an us(altgr-intl) layout).
158
+ def tap_keysym(name)
159
+ code, mods = @map[name]
160
+ raise "no keycode found for keysym #{name.inspect}" if code.nil?
161
+ if mods.empty?
162
+ @vkb.tap(code)
163
+ else
164
+ @vkb.chord(mods, code)
165
+ end
166
+ end
167
+
168
+ # True if this keyboard layout can produce `str` at all (all its characters
169
+ # have a keycode). Lets a test skip cleanly on a layout that lacks e.g. "ä".
170
+ def can_type?(str)
171
+ str.each_char.all? { |c| @map.key?(Gdk::Keyval.to_name(c.ord)) }
172
+ end
173
+
174
+ private
175
+
176
+ SHIFT = VirtualKeyboard::KEY_LEFTSHIFT
177
+ ALTGR = VirtualKeyboard::KEY_RIGHTALT
178
+
179
+ # Modifiers for each keysym column of a `xmodmap -pke` row. XKB maps a 4-level
180
+ # layout into the core keymap as [g1l1, g1l2, g2l1, g2l2, g1l3, g1l4], so the
181
+ # AltGr levels sit at 4/5 when a second group is present (the usual case: the
182
+ # single configured layout is duplicated into group 2) and at 2/3 when not.
183
+ COLUMN_MODS_6 = { 0 => [], 1 => [SHIFT], 4 => [ALTGR], 5 => [ALTGR, SHIFT] }.freeze
184
+ COLUMN_MODS_4 = { 0 => [], 1 => [SHIFT], 2 => [ALTGR], 3 => [ALTGR, SHIFT] }.freeze
185
+
186
+ # Parse lines like "keycode 47 = semicolon colon semicolon colon ..." into
187
+ # { "semicolon" => [39, []], "colon" => [39, [SHIFT]] }. When a keysym appears
188
+ # in several positions, the one needing the fewest modifiers wins.
189
+ def parse_xmodmap
190
+ map = {}
191
+ `xmodmap -pke`.each_line do |line|
192
+ m = line.match(/\Akeycode\s+(\d+) = (.*)$/) or next
193
+ code = m[1].to_i - 8
194
+ next if code <= 0
195
+ syms = m[2].split
196
+ cols = syms.length > 4 ? COLUMN_MODS_6 : COLUMN_MODS_4
197
+ cols.each do |i, mods|
198
+ sym = syms[i]
199
+ next if sym.nil? || sym == "NoSymbol"
200
+ map[sym] = [code, mods] if !map.key?(sym) || map[sym][1].length > mods.length
201
+ end
202
+ end
203
+ map
204
+ end
205
+ end
206
+
207
+ # Mixin for VmaTest subclasses that drive the editor with a real virtual
208
+ # keyboard: `with_vkb { |kb| … }` guards the preconditions, opens the uinput
209
+ # device, and closes it again.
210
+ module VkbTest
211
+ # Guard preconditions, open/close the uinput device around the block.
212
+ def with_vkb
213
+ # uinput events go to the real seat's focused window. Only type when this
214
+ # app is itself on the real Wayland session — never on a headless backend
215
+ # (Broadway/Xvfb), where the keystrokes would land in some other program.
216
+ # gtype.name: ruby-gnome has no named Ruby class for some backends.
217
+ backend = Gdk::Display.default&.gtype&.name.to_s
218
+ if !backend.include?("Wayland")
219
+ skip "not on a real Wayland seat (#{backend})"
220
+ end
221
+ skip "no writable /dev/uinput" if !VirtualKeyboard.available?
222
+ skip "xmodmap not available" if !KeyTyper.available?
223
+ vkb = nil
224
+ vma.gui.window.present
225
+ # Wayland won't let a non-interactively launched window steal focus. In the
226
+ # semi-interactive VM mode (VMA_E2E_INTERACTIVE), wait longer and prompt for a
227
+ # single click; a real user click legitimately focuses the window, after which
228
+ # every following test in the run finds it already active (no more clicks).
229
+ focus_wait = ENV["VMA_E2E_INTERACTIVE"] ? 90 : 3
230
+ if ENV["VMA_E2E_INTERACTIVE"] && !vma.gui.window.active?
231
+ puts "\n>>> [E2E] Click the vimamsa window in the VM to give it focus " \
232
+ "(waiting up to #{focus_wait}s)…"
233
+ $stdout.flush
234
+ end
235
+ if !wait_until(focus_wait) { vma.gui.window.active? }
236
+ skip "editor window is not focused; typing would go to another window"
237
+ end
238
+ vkb = VirtualKeyboard.new.open
239
+ yield KeyTyper.new(vkb)
240
+ ensure
241
+ vkb&.close
242
+ end
243
+
244
+ # Poll cond while letting the GTK main loop process the incoming key events.
245
+ def wait_until(timeout)
246
+ t0 = Time.now
247
+ loop do
248
+ drain_idle
249
+ return true if yield
250
+ return false if Time.now - t0 > timeout
251
+ sleep 0.05
252
+ end
253
+ end
254
+ end
@@ -0,0 +1,67 @@
1
+ require "tmpdir"
2
+
3
+ # A virtual microphone for the dictation E2E test.
4
+ #
5
+ # Instead of a physical mic, it synthesizes a known phrase to a WAV (via
6
+ # espeak / espeak-ng) and feeds that file into the dictation recorder through
7
+ # the *same* `cnf.dictation.source` knob that normally selects the microphone —
8
+ # as a GStreamer `filesrc ... ! wavparse` chain. So the real recorder pipeline
9
+ # (Gst.parse_launch -> audioconvert -> wavenc -> filesink) and the Whisper
10
+ # worker run end-to-end, with a deterministic, hermetic audio input and no
11
+ # dependency on the OS audio server.
12
+ #
13
+ # Real-microphone variant: for a test that also exercises the physical capture
14
+ # path, swap `open`/`gst_source` for a PulseAudio/PipeWire null sink —
15
+ # pactl load-module module-null-sink sink_name=vma_test_sink
16
+ # paplay -d vma_test_sink <wav> # play while recording
17
+ # cnf.dictation.source = "pulsesrc device=vma_test_sink.monitor"
18
+ # and unload the module in `close`. That needs `pactl`/`paplay` and a running
19
+ # sound server, so it is intentionally not the default here.
20
+ class VirtualMic
21
+ attr_reader :wav_path, :phrase
22
+
23
+ # Synth tools that can write a WAV from text, in preference order.
24
+ SYNTH_TOOLS = %w[espeak-ng espeak].freeze
25
+
26
+ def self.synth_tool
27
+ SYNTH_TOOLS.find { |c| which(c) }
28
+ end
29
+
30
+ # nil if this test can run, otherwise a human-readable skip reason. Gated first
31
+ # on the opt-in env var so the test is skipped by default.
32
+ def self.unmet_requirement
33
+ return "set VMA_E2E_DICTATION=1 to run (opt-in, slow, loads a Whisper model)" unless ENV["VMA_E2E_DICTATION"]
34
+ return "no speech synth tool on PATH (#{SYNTH_TOOLS.join('/')})" unless synth_tool
35
+ py = cnf.dictation.python! || "python3"
36
+ unless system(py, "-c", "import faster_whisper", out: File::NULL, err: File::NULL)
37
+ return "faster-whisper not importable by #{py}"
38
+ end
39
+ nil
40
+ end
41
+
42
+ def initialize(phrase)
43
+ @phrase = phrase
44
+ @wav_path = File.join(Dir.tmpdir, "vma_vmic_#{Process.pid}_#{Time.now.to_i}.wav")
45
+ end
46
+
47
+ # Synthesize the phrase to @wav_path. Returns self; raises if synthesis fails.
48
+ def open
49
+ tool = self.class.synth_tool
50
+ ok = system(tool, "-w", @wav_path, @phrase, out: File::NULL, err: File::NULL)
51
+ unless ok && File.exist?(@wav_path) && File.size(@wav_path) > 44 # > WAV header
52
+ raise "speech synthesis failed via #{tool}"
53
+ end
54
+ self
55
+ end
56
+
57
+ # The GStreamer source description to assign to cnf.dictation.source.
58
+ def gst_source
59
+ "filesrc location=#{@wav_path} ! wavparse"
60
+ end
61
+
62
+ def close
63
+ File.delete(@wav_path) if @wav_path && File.exist?(@wav_path)
64
+ rescue => e
65
+ warn "VirtualMic cleanup: #{e}"
66
+ end
67
+ end
data/tests/test_ack.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "tmpdir"
2
+
3
+ class TestAck < VmaTest
4
+ # Regression: ack output from a latin-1 file used to raise
5
+ # "invalid byte sequence in UTF-8" in Ack#ack_buffer and kill the editor.
6
+ def test_non_utf8_match_does_not_raise
7
+ if `which ack ack-grep 2>/dev/null`.strip.empty?
8
+ skip "ack not installed"
9
+ end
10
+ Dir.mktmpdir do |dir|
11
+ File.binwrite("#{dir}/latin1.c", "needlexyz caf\xE9\n".b)
12
+ vma.define_singleton_method(:get_content_search_paths) { [dir] }
13
+ begin
14
+ Ack.new.ack_buffer("needlexyz")
15
+ drain_idle
16
+ ensure
17
+ vma.singleton_class.send(:remove_method, :get_content_search_paths)
18
+ end
19
+ assert_eq true, vma.buf.to_s.include?("latin1.c:1:needlexyz caf")
20
+ end
21
+ end
22
+ end