vimamsa 0.1.26 → 0.1.27

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 (170) 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 +62 -16
  6. data/custom_example.rb +43 -13
  7. data/doc/TODO.md +461 -0
  8. data/doc/bundling_plan.md +569 -0
  9. data/doc/key_bindings_report_plan.md +168 -0
  10. data/doc/missing_vim_bindings.md +71 -0
  11. data/doc/modules_settings_plan.md +433 -0
  12. data/doc/refactor_dedup_plan.md +117 -0
  13. data/doc/unused_code_plan.md +120 -0
  14. data/doc/vim_compatibility_plan.md +296 -0
  15. data/doc/vimlike_vim_conflicts.md +254 -0
  16. data/doc/vm_snapshots_plan.md +146 -0
  17. data/exe/run_tests.rb +117 -3
  18. data/exe/vimamsa +24 -4
  19. data/fonts/OFL-Oxanium.txt +93 -0
  20. data/fonts/Oxanium[wght].ttf +0 -0
  21. data/icon.png +0 -0
  22. data/img/demo.gif +0 -0
  23. data/lang/hyperplaintext.lang +6 -0
  24. data/lib/vimamsa/ack.rb +11 -1
  25. data/lib/vimamsa/actions.rb +4 -1
  26. data/lib/vimamsa/appimage_env.rb +35 -0
  27. data/lib/vimamsa/audio.rb +2 -5
  28. data/lib/vimamsa/bookmarks.rb +302 -0
  29. data/lib/vimamsa/buffer.rb +593 -82
  30. data/lib/vimamsa/buffer_changetext.rb +102 -42
  31. data/lib/vimamsa/buffer_cursor.rb +7 -0
  32. data/lib/vimamsa/buffer_list.rb +4 -0
  33. data/lib/vimamsa/buffer_manager.rb +4 -1
  34. data/lib/vimamsa/bundled_fonts.rb +32 -0
  35. data/lib/vimamsa/clipboard.rb +3 -0
  36. data/lib/vimamsa/conf.rb +59 -3
  37. data/lib/vimamsa/constants.rb +3 -0
  38. data/lib/vimamsa/crash_handler.rb +149 -0
  39. data/lib/vimamsa/debug.rb +4 -1
  40. data/lib/vimamsa/desktop_install.rb +96 -0
  41. data/lib/vimamsa/diff_buffer.rb +39 -10
  42. data/lib/vimamsa/easy_jump.rb +3 -0
  43. data/lib/vimamsa/editor.rb +263 -57
  44. data/lib/vimamsa/encrypt.rb +78 -29
  45. data/lib/vimamsa/file_finder.rb +4 -19
  46. data/lib/vimamsa/file_history.rb +3 -0
  47. data/lib/vimamsa/file_manager.rb +30 -15
  48. data/lib/vimamsa/fold.rb +226 -0
  49. data/lib/vimamsa/gui.rb +520 -106
  50. data/lib/vimamsa/gui_command_line.rb +208 -0
  51. data/lib/vimamsa/gui_dialog.rb +94 -42
  52. data/lib/vimamsa/gui_file_panel.rb +356 -25
  53. data/lib/vimamsa/gui_form_generator.rb +59 -35
  54. data/lib/vimamsa/gui_func_panel.rb +67 -22
  55. data/lib/vimamsa/gui_image.rb +3 -0
  56. data/lib/vimamsa/gui_keylog_panel.rb +246 -0
  57. data/lib/vimamsa/gui_menu.rb +23 -11
  58. data/lib/vimamsa/gui_select_window.rb +19 -48
  59. data/lib/vimamsa/gui_settings.rb +78 -37
  60. data/lib/vimamsa/gui_sourceview.rb +273 -19
  61. data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
  62. data/lib/vimamsa/gui_text.rb +63 -2
  63. data/lib/vimamsa/gui_theme.rb +606 -0
  64. data/lib/vimamsa/hook.rb +3 -0
  65. data/lib/vimamsa/hyper_plain_text.rb +3 -0
  66. data/lib/vimamsa/key_actions.rb +112 -13
  67. data/lib/vimamsa/key_binding_tree.rb +242 -215
  68. data/lib/vimamsa/key_bindings_notepad.rb +43 -0
  69. data/lib/vimamsa/key_bindings_report.rb +261 -0
  70. data/lib/vimamsa/key_bindings_vim.rb +354 -0
  71. data/lib/vimamsa/key_bindings_vimlike.rb +65 -9
  72. data/lib/vimamsa/langservp.rb +271 -54
  73. data/lib/vimamsa/macro.rb +3 -0
  74. data/lib/vimamsa/main.rb +14 -8
  75. data/lib/vimamsa/rbvma.rb +26 -1
  76. data/lib/vimamsa/search.rb +3 -0
  77. data/lib/vimamsa/search_replace.rb +11 -10
  78. data/lib/vimamsa/string_util.rb +120 -40
  79. data/lib/vimamsa/terminal.rb +3 -0
  80. data/lib/vimamsa/test_framework.rb +28 -2
  81. data/lib/vimamsa/tests.rb +3 -0
  82. data/lib/vimamsa/text_objects.rb +353 -0
  83. data/lib/vimamsa/text_transforms.rb +14 -0
  84. data/lib/vimamsa/util.rb +46 -7
  85. data/lib/vimamsa/version.rb +1 -1
  86. data/lib/vimamsa.rb +0 -1
  87. data/modules/color_highlight/color_highlight.rb +118 -0
  88. data/modules/color_highlight/color_highlight_info.rb +3 -0
  89. data/modules/dictation/dictation.rb +1054 -0
  90. data/modules/dictation/dictation_info.rb +3 -0
  91. data/modules/dictation/whisper_worker.py +273 -0
  92. data/modules/ollama/ollama.rb +107 -0
  93. data/modules/ollama/ollama_info.rb +3 -0
  94. data/modules/tts/tts.rb +119 -0
  95. data/modules/tts/tts_info.rb +3 -0
  96. data/run_e2e_vm.sh +28 -0
  97. data/run_tests_vm.sh +130 -0
  98. data/scripts/appimage/AppRun +54 -0
  99. data/scripts/appimage/Dockerfile +18 -0
  100. data/scripts/appimage/build.sh +19 -0
  101. data/scripts/appimage/build_in_container.sh +192 -0
  102. data/scripts/appimage/smoke_test.sh +27 -0
  103. data/scripts/demo/demo_steps.txt +59 -0
  104. data/scripts/demo/record_demo.rb +177 -0
  105. data/scripts/demo/shapes.rb +28 -0
  106. data/styles/134272-molokai.xml +3 -0
  107. data/styles/molokai_cyber.xml +48 -0
  108. data/styles/molokai_edit.xml +1 -0
  109. data/tests/support/virtual_keyboard.rb +254 -0
  110. data/tests/support/virtual_mic.rb +67 -0
  111. data/tests/test_ack.rb +22 -0
  112. data/tests/test_auto_format.rb +122 -0
  113. data/tests/test_autocomplete.rb +211 -0
  114. data/tests/test_autosave.rb +145 -0
  115. data/tests/test_basic_editing.rb +33 -3
  116. data/tests/test_bookmarks.rb +206 -0
  117. data/tests/test_bracket_match.rb +85 -0
  118. data/tests/test_bundling.rb +92 -0
  119. data/tests/test_color_highlight.rb +92 -0
  120. data/tests/test_command_line.rb +79 -0
  121. data/tests/test_command_line_file.rb +27 -0
  122. data/tests/test_context_menu_file.rb +129 -0
  123. data/tests/test_copy_paste.rb +80 -0
  124. data/tests/test_crash_handler.rb +39 -0
  125. data/tests/test_custom_two_phase.rb +92 -0
  126. data/tests/test_dialog_callback_errors.rb +44 -0
  127. data/tests/test_dictation.rb +294 -0
  128. data/tests/test_diff_buffer_name.rb +51 -0
  129. data/tests/test_double_click.rb +41 -0
  130. data/tests/test_drag_selection.rb +125 -0
  131. data/tests/test_e2e_cmdline.rb +116 -0
  132. data/tests/test_e2e_dictation.rb +98 -0
  133. data/tests/test_e2e_replace_char.rb +58 -0
  134. data/tests/test_extract_file_links.rb +55 -0
  135. data/tests/test_file_format.rb +217 -0
  136. data/tests/test_file_manager.rb +72 -0
  137. data/tests/test_file_panel.rb +269 -0
  138. data/tests/test_flash.rb +78 -0
  139. data/tests/test_fold.rb +111 -0
  140. data/tests/test_font_refresh.rb +56 -0
  141. data/tests/test_func_panel.rb +68 -0
  142. data/tests/test_grep_apply.rb +46 -0
  143. data/tests/test_gtk_sync.rb +126 -0
  144. data/tests/test_hpt_highlight_visible.rb +75 -0
  145. data/tests/test_hpt_link_highlight.rb +82 -0
  146. data/tests/test_jump_to_file.rb +41 -0
  147. data/tests/test_key_bindings.rb +208 -0
  148. data/tests/test_keylog_panel.rb +101 -0
  149. data/tests/test_lsp_clangd.rb +281 -0
  150. data/tests/test_lsp_jump.rb +87 -0
  151. data/tests/test_lsp_position_encoding.rb +49 -0
  152. data/tests/test_lsp_symbols.rb +68 -0
  153. data/tests/test_macro.rb +137 -0
  154. data/tests/test_markdown_highlight.rb +57 -0
  155. data/tests/test_mode_badge.rb +41 -0
  156. data/tests/test_notepad_bindings.rb +67 -0
  157. data/tests/test_popup_queue.rb +51 -0
  158. data/tests/test_random_edit.rb +109 -0
  159. data/tests/test_revert.rb +127 -0
  160. data/tests/test_startup_file.rb +58 -0
  161. data/tests/test_status_area.rb +50 -0
  162. data/tests/test_text_objects.rb +163 -0
  163. data/tests/test_text_transforms.rb +329 -0
  164. data/tests/test_tts.rb +68 -0
  165. data/tests/test_vim_bindings.rb +252 -0
  166. data/tests/test_vim_conflicts.rb +63 -0
  167. data/tests/test_window_layout.rb +219 -0
  168. data/vimamsa.gemspec +3 -1
  169. data/vm_guest_run.sh +109 -0
  170. metadata +112 -10
@@ -0,0 +1,3 @@
1
+ def dictation_info
2
+ { name: "Voice Dictation", no_restart: true }
3
+ end
@@ -0,0 +1,273 @@
1
+ #!/usr/bin/env python3
2
+ """Persistent faster-whisper transcription worker for the Vimamsa dictation module.
3
+
4
+ Loads the model once and stays alive, transcribing audio files on demand so that
5
+ repeated dictation is fast (no per-call model reload). The Ruby side
6
+ (modules/dictation/dictation.rb) spawns this, reads the readiness line, then feeds
7
+ one WAV path per line and reads one JSON response per line.
8
+
9
+ Protocol (line-based, JSON so transcripts with newlines stay one line):
10
+ startup -> {"progress": {"phase": "download", "fraction": 0.42}} (repeatable)
11
+ -> {"progress": {"phase": "load"}}
12
+ -> {"ready": true} (model loaded)
13
+ or {"ready": false, "error": "..."} (load/deps failed; then exits)
14
+ stdin <- /path/to/audio.wav\\n (bare path), or
15
+ <- {"path": "...", "initial_prompt": "...", "beam_size": 1,
16
+ "normalize": false, "vad": false}\\n (per-request overrides)
17
+ stdout -> {"progress": {"phase": "transcribe", "fraction": 0.6}} (repeatable)
18
+ -> {"ok": true, "text": "..."} (per request)
19
+ or {"ok": false, "error": "..."}
20
+
21
+ "progress" lines may appear at any point before the terminating line of a
22
+ startup or a request; a client that does not care can skip any line carrying a
23
+ "progress" key and keep reading.
24
+
25
+ The transcription logic mirrors ~/Drive/util/bin/transcribe_audio.py (faster-whisper
26
+ + ffmpeg level normalization + Silero VAD); it is kept self-contained here so the
27
+ editor does not depend on that personal script.
28
+
29
+ Requires: faster-whisper (pip install faster-whisper) and ffmpeg on PATH.
30
+ """
31
+
32
+ import argparse
33
+ import json
34
+ import os
35
+ import subprocess
36
+ import sys
37
+ import tempfile
38
+ import threading
39
+ import time
40
+ from contextlib import contextmanager
41
+
42
+ # ffmpeg filter chain that evens out uneven speaker levels before ASR, so quiet
43
+ # speech isn't dropped by the VAD/decoder. Mirrors transcribe_audio.py.
44
+ NORMALIZE_FILTER = "afftdn=nr=12:nf=-25,dynaudnorm=f=150:g=7:p=0.9:m=25"
45
+
46
+
47
+ def _emit(obj):
48
+ """Write one JSON object as a single flushed line on stdout."""
49
+ sys.stdout.write(json.dumps(obj) + "\n")
50
+ sys.stdout.flush()
51
+
52
+
53
+ def _emit_progress(phase, fraction=None):
54
+ """Report progress of a long phase. `fraction` is 0..1, or None when the
55
+ phase has no measurable progress (model load)."""
56
+ p = {"phase": phase}
57
+ if fraction is not None:
58
+ p["fraction"] = round(min(max(fraction, 0.0), 1.0), 4)
59
+ _emit({"progress": p})
60
+
61
+
62
+ MODEL_PATTERNS = ["config.json", "preprocessor_config.json", "model.bin",
63
+ "tokenizer.json", "vocabulary.*"]
64
+
65
+
66
+ def _repo_total_bytes(repo):
67
+ """Total size of the files we are about to fetch, from the hub API. Returns
68
+ 0 if it can't be determined, which disables percentage reporting."""
69
+ import fnmatch
70
+ from huggingface_hub import HfApi
71
+
72
+ info = HfApi().model_info(repo, files_metadata=True)
73
+ return sum(s.size or 0 for s in info.siblings
74
+ if any(fnmatch.fnmatch(s.rfilename, p) for p in MODEL_PATTERNS))
75
+
76
+
77
+ def _repo_cached_bytes(repo):
78
+ """Bytes of this repo currently on disk, counting partial downloads. Only
79
+ the blobs dir holds real data — snapshots are symlinks into it, so walking
80
+ both would double count."""
81
+ from huggingface_hub.constants import HF_HUB_CACHE
82
+
83
+ blobs = os.path.join(HF_HUB_CACHE, "models--" + repo.replace("/", "--"), "blobs")
84
+ total = 0
85
+ for root, _dirs, files in os.walk(blobs):
86
+ for name in files:
87
+ path = os.path.join(root, name)
88
+ if os.path.islink(path):
89
+ continue
90
+ try:
91
+ total += os.path.getsize(path)
92
+ except OSError:
93
+ pass # file swapped out from under us mid-download
94
+ return total
95
+
96
+
97
+ @contextmanager
98
+ def _download_progress(repo):
99
+ """Report download progress by watching the cache grow toward the repo's
100
+ known total size.
101
+
102
+ Deliberately not hooked into the transfer machinery: hub's progress bars
103
+ report an "incomplete total" for Xet-backed repos, so their byte totals are
104
+ unusable, and the details differ between hub versions. Sizes on disk do not.
105
+ """
106
+ try:
107
+ total = _repo_total_bytes(repo)
108
+ except Exception:
109
+ total = 0
110
+ if not total:
111
+ yield
112
+ return
113
+
114
+ stop = threading.Event()
115
+
116
+ def poll():
117
+ while not stop.wait(0.5):
118
+ try:
119
+ _emit_progress("download", _repo_cached_bytes(repo) / total)
120
+ except Exception:
121
+ pass
122
+
123
+ _emit_progress("download", _repo_cached_bytes(repo) / total)
124
+ watcher = threading.Thread(target=poll, daemon=True)
125
+ watcher.start()
126
+ try:
127
+ yield
128
+ finally:
129
+ stop.set()
130
+
131
+
132
+ def _fetch_model(name):
133
+ """Download the model snapshot up front so a first run reports progress
134
+ instead of sitting silent for several GB. Returns a local path, or `name`
135
+ unchanged when it is not a hub id we can resolve — WhisperModel then does
136
+ its own (silent) resolution, which is the pre-existing behaviour.
137
+ """
138
+ if os.path.isdir(name):
139
+ return name
140
+ try:
141
+ from faster_whisper.utils import _MODELS
142
+ import huggingface_hub
143
+
144
+ repo = _MODELS.get(name, name)
145
+ if "/" not in repo:
146
+ return name
147
+ with _download_progress(repo):
148
+ return huggingface_hub.snapshot_download(
149
+ repo, allow_patterns=MODEL_PATTERNS)
150
+ except Exception:
151
+ # Offline, private repo, unexpected hub version: fall back rather than
152
+ # failing startup over a progress-reporting nicety.
153
+ return name
154
+
155
+
156
+ @contextmanager
157
+ def _normalized_audio(audio_path, normalize):
158
+ """Yield a path to feed the decoder: the original file, or a normalized temp wav."""
159
+ if not normalize:
160
+ yield audio_path
161
+ return
162
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=True) as tmp:
163
+ cmd = ["ffmpeg", "-nostdin", "-v", "error", "-y", "-i", audio_path,
164
+ "-ac", "1", "-ar", "16000", "-af", NORMALIZE_FILTER, tmp.name]
165
+ subprocess.run(cmd, check=True)
166
+ yield tmp.name
167
+
168
+
169
+ def main():
170
+ ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
171
+ ap.add_argument("--model", default="large-v3", help="faster-whisper model")
172
+ ap.add_argument("--language", default="en", help="spoken language code")
173
+ ap.add_argument("--device", default="auto", help="auto, cuda, or cpu")
174
+ ap.add_argument("--compute-type", default="float16",
175
+ help="compute type, e.g. float16 or int8")
176
+ ap.add_argument("--no-normalize", action="store_true",
177
+ help="skip ffmpeg level normalization (on by default)")
178
+ ap.add_argument("--initial-prompt", default="",
179
+ help="text prompt that biases decoding toward a vocabulary/style")
180
+ ap.add_argument("--beam-size", type=int, default=5, help="decoding beam size")
181
+ ap.add_argument("--idle-timeout", type=float, default=300.0,
182
+ help="exit after this many seconds of inactivity to release "
183
+ "VRAM (<= 0 disables)")
184
+ args = ap.parse_args()
185
+ normalize = not args.no_normalize
186
+ initial_prompt = args.initial_prompt or None
187
+
188
+ # Load the model once. Report failure (missing deps, bad device, OOM) and exit.
189
+ try:
190
+ from faster_whisper import WhisperModel
191
+ model_path = _fetch_model(args.model)
192
+ _emit_progress("load")
193
+ model = WhisperModel(model_path, device=args.device,
194
+ compute_type=args.compute_type)
195
+ except Exception as e: # noqa: BLE001 - report any startup failure to the client
196
+ _emit({"ready": False, "error": f"{type(e).__name__}: {e}"})
197
+ return 1
198
+
199
+ # Exit the whole process after a period of inactivity so the model weights and
200
+ # the CUDA context are fully released. `busy` keeps us alive mid-transcription
201
+ # even if a long final pass exceeds the timeout. The Ruby side respawns us on
202
+ # the next dictation.
203
+ state = {"last": time.monotonic(), "busy": False}
204
+
205
+ def _idle_watch(timeout):
206
+ while True:
207
+ time.sleep(min(max(timeout, 1.0), 15.0))
208
+ if not state["busy"] and (time.monotonic() - state["last"]) > timeout:
209
+ os._exit(0)
210
+
211
+ if args.idle_timeout > 0:
212
+ threading.Thread(target=_idle_watch, args=(args.idle_timeout,),
213
+ daemon=True).start()
214
+
215
+ _emit({"ready": True})
216
+
217
+ # One request per stdin line; one JSON response per line.
218
+ # A request is either a bare WAV path, or a JSON object with per-request
219
+ # overrides: {"path", "initial_prompt", "beam_size", "normalize", "vad"}.
220
+ # The CLI args above supply the defaults (used for the preliminary passes,
221
+ # which send fast overrides; the final pass sends high-quality overrides).
222
+ for line in sys.stdin:
223
+ line = line.strip()
224
+ if not line:
225
+ continue
226
+ state["busy"] = True
227
+ state["last"] = time.monotonic()
228
+ try:
229
+ if line.startswith("{"):
230
+ req = json.loads(line)
231
+ path = req["path"]
232
+ req_prompt = req.get("initial_prompt", args.initial_prompt) or None
233
+ req_beam = int(req.get("beam_size", args.beam_size))
234
+ req_norm = bool(req.get("normalize", normalize))
235
+ req_vad = bool(req.get("vad", True))
236
+ else:
237
+ path = line
238
+ req_prompt, req_beam, req_norm, req_vad = (
239
+ initial_prompt, args.beam_size, normalize, True)
240
+
241
+ with _normalized_audio(path, req_norm) as source:
242
+ segments_iter, info = model.transcribe(
243
+ source,
244
+ language=args.language,
245
+ vad_filter=req_vad,
246
+ beam_size=req_beam,
247
+ initial_prompt=req_prompt,
248
+ )
249
+ # transcribe() is lazy: decoding happens as segments are pulled.
250
+ # Each carries an end timestamp, so position within the clip is
251
+ # a usable progress measure for a long final pass.
252
+ duration = getattr(info, "duration", 0) or 0
253
+ parts = []
254
+ last = 0.0
255
+ for seg in segments_iter:
256
+ if seg.text.strip():
257
+ parts.append(seg.text.strip())
258
+ now = time.monotonic()
259
+ if duration and now - last > 0.5:
260
+ last = now
261
+ _emit_progress("transcribe", seg.end / duration)
262
+ _emit({"ok": True, "text": " ".join(parts)})
263
+ except Exception as e: # noqa: BLE001 - keep the worker alive across errors
264
+ _emit({"ok": False, "error": f"{type(e).__name__}: {e}"})
265
+ finally:
266
+ state["last"] = time.monotonic()
267
+ state["busy"] = False
268
+
269
+ return 0
270
+
271
+
272
+ if __name__ == "__main__":
273
+ sys.exit(main())
@@ -0,0 +1,107 @@
1
+ require "net/http"
2
+ require "json"
3
+
4
+ def ollama_improve_selection
5
+ return unless vma.buf.visual_mode?
6
+ r = vma.buf.get_visual_mode_range
7
+ txt = vma.buf[r]
8
+ return if txt.nil? || txt.strip.empty?
9
+
10
+ vma.buf.end_visual_mode
11
+ message("Ollama: thinking…")
12
+
13
+ Thread.new do
14
+ result = ollama_request(txt)
15
+ GLib::Idle.add do
16
+ if result
17
+ vma.buf.replace_range(r, result)
18
+ vma.buf.view.handle_deltas
19
+ message("Ollama: done")
20
+ else
21
+ message("Ollama: request failed — is ollama running?")
22
+ end
23
+ false
24
+ end
25
+ end
26
+ end
27
+
28
+ def ollama_improve_selection_multi(n = 3)
29
+ return unless vma.buf.visual_mode?
30
+
31
+ r = vma.buf.get_visual_mode_range
32
+ txt = vma.buf[r]
33
+ return if txt.nil? || txt.strip.empty?
34
+
35
+ # Find end of paragraph: first blank line after selection end, or end of buffer
36
+ buf_str = vma.buf.to_s
37
+ blank = buf_str.index(/\n[ \t]*\n/, r.last)
38
+ para_end = blank || buf_str.rindex("\n") || buf_str.size - 1
39
+
40
+ vma.buf.end_visual_mode
41
+ message("Ollama: generating #{n} variants…")
42
+
43
+ Thread.new do
44
+ temp = cnf.ollama.multi_temperature! || 1.2
45
+ threads = n.times.map { Thread.new { ollama_request(txt, temperature: temp) } }
46
+ results = threads.map(&:value)
47
+ GLib::Idle.add do
48
+ successful = results.compact
49
+ if successful.any?
50
+ block = successful.map.with_index(1) { |v, i| "#{i}. #{v}" }.join("\n")
51
+ vma.buf.insert_txt_at(block, para_end + 1)
52
+ vma.buf.view.handle_deltas
53
+ message("Ollama: inserted #{successful.size} variant(s)")
54
+ else
55
+ message("Ollama: request failed — is ollama running?")
56
+ end
57
+ false
58
+ end
59
+ end
60
+ end
61
+
62
+ def ollama_request(text, temperature: nil)
63
+ model = cnf.ollama.model! || "qwen3.5:0.8b"
64
+ host = cnf.ollama.host! || "localhost"
65
+ port = cnf.ollama.port! || 11434
66
+ prompt = "Improve the following English text. " \
67
+ "Return only the improved text, no explanation:\n\n#{text}"
68
+
69
+ uri = URI::HTTP.build(host: host, port: port, path: "/api/generate")
70
+ req_body = { model: model, prompt: prompt, stream: false, think: false }
71
+ req_body[:options] = { temperature: temperature } if temperature
72
+ body = JSON.generate(req_body)
73
+
74
+ http = Net::HTTP.new(uri.host, uri.port)
75
+ http.read_timeout = cnf.ollama.timeout! || 60
76
+ req = Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
77
+ req.body = body
78
+
79
+ resp = http.request(req)
80
+ # require "pry";binding.pry
81
+ return nil unless resp.is_a?(Net::HTTPSuccess)
82
+
83
+ JSON.parse(resp.body)["response"]&.strip
84
+ rescue => e
85
+ debug "Ollama error: #{e}"
86
+ nil
87
+ end
88
+
89
+ def ollama_init
90
+ reg_act(:ollama_improve_selection,
91
+ proc { ollama_improve_selection },
92
+ "Ollama: improve selected text")
93
+ reg_act(:ollama_improve_selection_multi,
94
+ proc { ollama_improve_selection_multi },
95
+ "Ollama: generate N improved variants below paragraph")
96
+
97
+ bindings = { "V , o i" => :ollama_improve_selection }
98
+ (1..9).each { |n| bindings["V , o #{n} i"] = "ollama_improve_selection_multi(#{n})" }
99
+ add_keys "ollama", bindings
100
+ end
101
+
102
+ def ollama_disable
103
+ unreg_act(:ollama_improve_selection)
104
+ unreg_act(:ollama_improve_selection_multi)
105
+ unbindkey "V , o i"
106
+ (1..9).each { |n| unbindkey "V , o #{n} i" }
107
+ end
@@ -0,0 +1,3 @@
1
+ def ollama_info
2
+ { name: "Ollama", no_restart: true }
3
+ end
@@ -0,0 +1,119 @@
1
+ require "open3"
2
+ require "json"
3
+
4
+ # Text-to-Speech module
5
+ #
6
+ # Select text (visual mode) and activate to hear it spoken aloud with Piper TTS.
7
+ # Mirrors ~/Drive/util/bin/speak.rb: piper --output-raw | aplay, with the sample
8
+ # rate auto-detected from the voice's .onnx.json. Playback runs in a background
9
+ # thread so the editor stays responsive, and can be stopped.
10
+ #
11
+ # V , s speak the current selection
12
+ # C , q stop speaking
13
+ #
14
+ # Configuration (set in ~/.vimamsa/settings.rb via cnf, all optional):
15
+ # cnf.tts.voice Piper voice name (default "en_GB-alan-medium")
16
+ # cnf.tts.data_dir voices directory (default "~/piper-voices")
17
+ # cnf.tts.rate sample rate (Hz) (default: from <voice>.onnx.json, else 22050)
18
+ #
19
+ # Requires piper and aplay on PATH and a Piper voice installed in data_dir.
20
+
21
+ class VmaTts
22
+ def initialize
23
+ @mutex = Mutex.new
24
+ @pids = []
25
+ end
26
+
27
+ # Speak `text` aloud. Cancels any current playback first. Non-blocking.
28
+ def speak(text)
29
+ return if text.nil? || text.strip.empty?
30
+ unless which("piper") && which("aplay")
31
+ message("TTS: need 'piper' and 'aplay' on PATH")
32
+ return
33
+ end
34
+ stop # one utterance at a time
35
+
36
+ voice = cnf.tts.voice! || "en_GB-alan-medium"
37
+ data_dir = File.expand_path(cnf.tts.data_dir! || "~/piper-voices")
38
+ rate = (cnf.tts.rate! || detect_rate(data_dir, voice) || 22050).to_s
39
+
40
+ piper = ["piper", "-m", voice, "--data-dir", data_dir, "--output-raw"]
41
+ player = ["aplay", "-q", "-r", rate, "-f", "S16_LE", "-t", "raw", "-"]
42
+
43
+ Thread.new do
44
+ begin
45
+ stdin, threads = Open3.pipeline_w(piper, player)
46
+ @mutex.synchronize { @pids = threads.map(&:pid) }
47
+ stdin.write(text)
48
+ stdin.close
49
+ threads.each(&:join)
50
+ rescue => e
51
+ GLib::Idle.add { message("TTS error: #{e.message}"); false }
52
+ ensure
53
+ @mutex.synchronize { @pids = [] }
54
+ end
55
+ end
56
+ end
57
+
58
+ # Stop any in-progress playback.
59
+ def stop
60
+ @mutex.synchronize do
61
+ @pids.each { |pid| Process.kill("TERM", pid) rescue nil }
62
+ @pids = []
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ # Read the voice's preferred sample rate from <data_dir>/<voice>.onnx.json.
69
+ def detect_rate(data_dir, voice)
70
+ cfg = File.join(data_dir, "#{voice}.onnx.json")
71
+ return nil unless File.exist?(cfg)
72
+ JSON.parse(File.read(cfg)).dig("audio", "sample_rate")
73
+ rescue
74
+ nil
75
+ end
76
+ end
77
+
78
+ def tts_engine
79
+ $vma_tts ||= VmaTts.new
80
+ end
81
+
82
+ def tts_speak_selection
83
+ unless vma.buf.visual_mode?
84
+ message("TTS: select text first")
85
+ return
86
+ end
87
+ r = vma.buf.get_visual_mode_range
88
+ text = vma.buf[r]
89
+ vma.buf.end_visual_mode
90
+ if text.nil? || text.strip.empty?
91
+ message("TTS: nothing selected")
92
+ return
93
+ end
94
+ tts_engine.speak(text)
95
+ message("TTS: speaking…")
96
+ end
97
+
98
+ def tts_stop
99
+ $vma_tts&.stop
100
+ message("TTS: stopped")
101
+ end
102
+
103
+ def tts_init
104
+ reg_act(:tts_speak_selection, proc { tts_speak_selection }, "Speak selected text")
105
+ reg_act(:tts_stop, proc { tts_stop }, "Stop speaking")
106
+ add_keys "tts", { "V , s" => :tts_speak_selection, "C , q" => :tts_stop }
107
+ vma.gui.menu.add_module_action(:tts_speak_selection, "Speak Selection")
108
+ vma.gui.menu.add_module_action(:tts_stop, "Stop Speaking")
109
+ end
110
+
111
+ def tts_disable
112
+ $vma_tts&.stop
113
+ unreg_act(:tts_speak_selection)
114
+ unreg_act(:tts_stop)
115
+ unbindkey "V , s"
116
+ unbindkey "C , q"
117
+ vma.gui.menu.remove_module_action(:tts_speak_selection)
118
+ vma.gui.menu.remove_module_action(:tts_stop)
119
+ end
@@ -0,0 +1,3 @@
1
+ def tts_info
2
+ { name: "Text to Speech", no_restart: true }
3
+ end
data/run_e2e_vm.sh ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env bash
2
+ # Run ONLY the uinput E2E tests in the test VM ($VM_NAME), semi-interactively.
3
+ #
4
+ # These tests type real key events through a virtual keyboard (uinput), which
5
+ # land in whatever window currently has focus. Wayland won't let a window that
6
+ # was launched non-interactively (via guestcontrol) steal focus, so this needs
7
+ # exactly ONE bit of interaction: when prompted, click the vimamsa window on the
8
+ # VM's desktop once. Every test in the run then finds it already focused.
9
+ #
10
+ # The heavy lifting (sync, provision, uinput/Wayland setup) is shared with
11
+ # run_tests_vm.sh; this wrapper just restricts the run to the E2E file and turns
12
+ # on the interactive focus wait.
13
+ #
14
+ # Usage:
15
+ # (VM_NAME, VM_USER and VM_PASS exported, as for run_tests_vm.sh)
16
+ # ./run_e2e_vm.sh
17
+ # ./run_e2e_vm.sh e2e_cmdline # explicit filter
18
+ set -euo pipefail
19
+
20
+ HERE="$(cd "$(dirname "$0")" && pwd)"
21
+ export VMA_E2E_INTERACTIVE=1
22
+
23
+ echo ">> E2E VM run: the vimamsa window will open on the VM desktop."
24
+ echo ">> When you see the '[E2E] Click the vimamsa window…' prompt, click that"
25
+ echo ">> window once to focus it; the tests then type into it via uinput."
26
+
27
+ # Default to the cmdline E2E file; forward any explicit filter args instead.
28
+ exec "$HERE/run_tests_vm.sh" "${@:-e2e_cmdline}"
data/run_tests_vm.sh ADDED
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env bash
2
+ # Run the vimamsa test suite inside the VirtualBox VM named by $VM_NAME, on its real
3
+ # logged-in desktop (so the GTK window actually appears in the VM).
4
+ #
5
+ # Reaches the guest via VBoxManage guestcontrol (Guest Additions) — no SSH or
6
+ # shared folder needed. Syncs the current working tree (incl. uncommitted changes),
7
+ # provisions the guest on first run, builds the native extension, then runs
8
+ # exe/run_tests.rb on DISPLAY :0. Exits with the suite's exit code (0 pass / 1 fail).
9
+ #
10
+ # Usage (VM_NAME, VM_USER and VM_PASS exported, e.g. in ~/.bashrc):
11
+ # ./run_tests_vm.sh [test filter args...]
12
+ # Examples:
13
+ # ./run_tests_vm.sh # full suite
14
+ # ./run_tests_vm.sh undo # tests/test_undo.rb only
15
+ # FORCE_PROVISION=1 ./run_tests_vm.sh # re-run apt provisioning
16
+ #
17
+ # Env:
18
+ # VM_NAME (required) VirtualBox VM name
19
+ # VM_USER (required) guest login username
20
+ # VM_PASS (required) guest login/sudo password for user $VM_USER
21
+ # VM_DISPLAY guest desktop X display (default: :0)
22
+ # FORCE_PROVISION set to 1 to force guest re-provisioning
23
+ set -euo pipefail
24
+
25
+ VM_NAME="${VM_NAME:-}"
26
+ VM_USER="${VM_USER:-}"
27
+ VM_DISPLAY="${VM_DISPLAY:-:0}"
28
+ GUEST_HOME="/home/${VM_USER}"
29
+
30
+ missing=()
31
+ [ -n "$VM_NAME" ] || missing+=(VM_NAME)
32
+ [ -n "$VM_USER" ] || missing+=(VM_USER)
33
+ [ -n "${VM_PASS:-}" ] || missing+=(VM_PASS)
34
+ if [ ${#missing[@]} -gt 0 ]; then
35
+ echo "!! Not set: ${missing[*]}. Export VM_NAME (VirtualBox VM), VM_USER (guest user)" >&2
36
+ echo " and VM_PASS (guest password), e.g. in ~/.bashrc." >&2
37
+ exit 2
38
+ fi
39
+
40
+ REPO="$(git -C "$(dirname "$0")" rev-parse --show-toplevel)"
41
+
42
+ # Temp files: a chmod-600 passwordfile (so the password isn't visible in `ps`)
43
+ # and the source tarball. Both are removed on exit.
44
+ PF="$(mktemp)"
45
+ TAR="$(mktemp --suffix=.tgz)"
46
+ chmod 600 "$PF"
47
+ printf '%s' "$VM_PASS" > "$PF"
48
+ cleanup() { rm -f "$PF" "$TAR"; }
49
+ trap cleanup EXIT
50
+
51
+ # In VBoxManage 7.x the credentials/options belong to the run/copyto subcommand
52
+ # (after it), and use `--opt=value` syntax.
53
+ GC=(VBoxManage guestcontrol "$VM_NAME")
54
+ CRED=(--username="$VM_USER" --passwordfile="$PF")
55
+
56
+ # 1. Ensure the VM is running and Guest Additions answer.
57
+ if ! VBoxManage list runningvms | grep -q "\"$VM_NAME\""; then
58
+ echo ">> Starting VM $VM_NAME"
59
+ VBoxManage startvm "$VM_NAME" --type gui
60
+ fi
61
+ echo ">> Waiting for Guest Additions in $VM_NAME"
62
+ for i in $(seq 1 60); do
63
+ if "${GC[@]}" run "${CRED[@]}" --exe=/bin/true -- true >/dev/null 2>&1; then break; fi
64
+ if [ "$i" = 60 ]; then
65
+ echo "!! Guest Additions not responding after 60s. Is the guest booted and logged in?" >&2
66
+ exit 4
67
+ fi
68
+ sleep 2
69
+ done
70
+
71
+ # 2. Tarball the working tree (include .git for `gem build`; drop heavy junk).
72
+ # tolerate exit 1 ("file changed as we read it" — e.g. .git churning during the
73
+ # read); only a fatal tar error (>=2) should abort.
74
+ echo ">> Packing working tree"
75
+ set +e
76
+ tar czf "$TAR" -C "$REPO" \
77
+ --warning=no-file-changed \
78
+ --exclude='./.git/hooks' \
79
+ --exclude='*_vma_autosave' \
80
+ --exclude='.*_vma_autosave' \
81
+ --exclude='*.gem' \
82
+ --exclude='*.o' \
83
+ --exclude='*.so' \
84
+ --exclude='*.tar.xz' \
85
+ --exclude='__pycache__' \
86
+ --exclude='*.log' \
87
+ --exclude='claude-log.*' \
88
+ --exclude='dot_git_old' \
89
+ --exclude='./build' \
90
+ --exclude='2del_*' \
91
+ --exclude='bug*.txt' \
92
+ --exclude='*.mp3' --exclude='*.wav' \
93
+ .
94
+ trc=$?
95
+ set -e
96
+ if [ "$trc" -gt 1 ]; then
97
+ echo "!! tar failed (exit $trc)" >&2
98
+ exit 6
99
+ fi
100
+
101
+ # 3. Copy the tarball and the guest runner into the VM (copyto overwrites by default).
102
+ echo ">> Copying source + runner into guest"
103
+ "${GC[@]}" copyto "${CRED[@]}" "$TAR" "${GUEST_HOME}/vimamsa_src.tgz"
104
+ "${GC[@]}" copyto "${CRED[@]}" "${REPO}/vm_guest_run.sh" "${GUEST_HOME}/vm_guest_run.sh"
105
+
106
+ # 4. Run the guest script, streaming output (tee'd so we can parse the result).
107
+ # Secrets/env go via --putenv, never onto the repo. NOTE: VBoxManage does not
108
+ # propagate the guest exit code, so pass/fail is taken from the VMA_SUITE_EXIT
109
+ # sentinel the guest prints, not from VBoxManage's own exit status.
110
+ echo ">> Running test suite in guest (DISPLAY $VM_DISPLAY)"
111
+ OUT="$(mktemp)"
112
+ trap 'rm -f "$PF" "$TAR" "$OUT"' EXIT
113
+ set +e
114
+ "${GC[@]}" run "${CRED[@]}" --exe=/bin/bash --wait-stdout --wait-stderr \
115
+ --putenv="SUDO_PASS=${VM_PASS}" \
116
+ --putenv="VMA_DISPLAY=${VM_DISPLAY}" \
117
+ --putenv="VMA_TEST_ARGS=$*" \
118
+ --putenv="FORCE_PROVISION=${FORCE_PROVISION:-}" \
119
+ --putenv="VMA_E2E_INTERACTIVE=${VMA_E2E_INTERACTIVE:-}" \
120
+ -- "${GUEST_HOME}/vm_guest_run.sh" 2>&1 | tee "$OUT"
121
+ set -e
122
+
123
+ sentinel="$(grep -oE 'VMA_SUITE_EXIT=[0-9]+' "$OUT" | tail -1 | cut -d= -f2)"
124
+ if [ -z "$sentinel" ]; then
125
+ echo "!! Did not receive a VMA_SUITE_EXIT sentinel from the guest — the run did not" >&2
126
+ echo " reach the test phase (provisioning/build failure above)." >&2
127
+ exit 5
128
+ fi
129
+ echo ">> Suite exit code (from guest): $sentinel"
130
+ exit "$sentinel"