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,247 @@
1
+
2
+ module Vimamsa
3
+ # Interactive command line at the bottom of the window, like vim's ":".
4
+ # Lives in the minibuffer area (shares the Gtk::Stack with the message label).
5
+ #
6
+ # Input is Ruby code by default, evaluated at top level, so the whole editor
7
+ # API is available: buf, vma, cnf, bufs, registered helper methods, etc.
8
+ # Config changes are plain Ruby assignments:
9
+ # :cnf.font.size = 14
10
+ # Local variables persist between invocations (TOPLEVEL_BINDING).
11
+ #
12
+ # Convenience forms checked before Ruby eval:
13
+ # :!cmd run cmd in a shell, output to a new buffer
14
+ # :42 jump to line 42
15
+ # :e PATH open file
16
+ # :w :q :wq save / quit / save-and-quit
17
+ # :reg show the registers (also :registers, :di, :display)
18
+ #
19
+ # Actions run with a.<id>!, e.g. :a.buf.save! — tab completes the id after
20
+ # "a." one level at a time (namespaces, then the segments below them).
21
+ class CommandLine
22
+ attr_reader :history, :entry
23
+
24
+ HISTORY_FILE = "cmd_history.txt" # in the data dir
25
+ HISTORY_MAX = 100
26
+
27
+ def initialize(css_provider)
28
+ @active = false
29
+ @history = load_history
30
+ @hist_idx = nil
31
+
32
+ prompt = Gtk::Label.new(":")
33
+ prompt.add_css_class("minibuf")
34
+ prompt.style_context.add_provider(css_provider)
35
+
36
+ @entry = Gtk::Entry.new
37
+ @entry.hexpand = true
38
+ @entry.has_frame = false
39
+ @entry.add_css_class("minibuf")
40
+ @entry.style_context.add_provider(css_provider)
41
+
42
+ @box = Gtk::Box.new(:horizontal, 0)
43
+ @box.append(prompt)
44
+ @box.append(@entry)
45
+
46
+ @entry.signal_connect("activate") do
47
+ line = @entry.text
48
+ close
49
+ execute(line)
50
+ end
51
+
52
+ keyctr = Gtk::EventControllerKey.new
53
+ keyctr.signal_connect "key-pressed" do |_ctr, keyval, _keycode, _state|
54
+ case Gdk::Keyval.to_name(keyval)
55
+ when "Escape"
56
+ close
57
+ true
58
+ when "Up"
59
+ history_move(1)
60
+ true
61
+ when "Down"
62
+ history_move(-1)
63
+ true
64
+ when "Tab", "ISO_Left_Tab"
65
+ complete_action_id
66
+ true # keep focus in the entry
67
+ else
68
+ false
69
+ end
70
+ end
71
+ @entry.add_controller(keyctr)
72
+
73
+ # Clicking elsewhere abandons the command line (like vim losing ":" focus)
74
+ focusctr = Gtk::EventControllerFocus.new
75
+ focusctr.signal_connect("leave") { close }
76
+ @entry.add_controller(focusctr)
77
+ end
78
+
79
+ # The outermost widget, added to the minibuffer stack as "cmdline".
80
+ def widget
81
+ @box
82
+ end
83
+
84
+ def active?
85
+ @active
86
+ end
87
+
88
+ def start
89
+ return if @active
90
+ @active = true
91
+ @hist_idx = nil
92
+ @entry.text = ""
93
+ vma.gui.cmd_line_show
94
+ @entry.grab_focus
95
+ end
96
+
97
+ def close
98
+ return if !@active
99
+ @active = false
100
+ vma.gui.cmd_line_hide
101
+ end
102
+
103
+ # Parse and run one command line. Public so tests and other code can drive
104
+ # the command line without the GUI entry.
105
+ def execute(line)
106
+ line = line.strip
107
+ return if line.empty?
108
+ add_to_history(line)
109
+
110
+ case line
111
+ when /\A!(.+)\z/m
112
+ shell_to_buffer(Regexp.last_match(1).strip)
113
+ when /\A\d+\z/
114
+ buf.jump_to_line(line.to_i)
115
+ when "w"
116
+ buf.save
117
+ when "q"
118
+ exec_action("app.quit")
119
+ when "wq"
120
+ buf.save
121
+ exec_action("app.quit")
122
+ when "reg", "registers", "di", "display"
123
+ exec_action("edit.register.list")
124
+ when "intro"
125
+ exec_action("help.intro")
126
+ when /\Ae\s+(\S.*)\z/
127
+ open_new_file(File.expand_path(Regexp.last_match(1).strip))
128
+ else
129
+ eval_ruby(line)
130
+ end
131
+ end
132
+
133
+ # Complete the action id after "a." before the cursor: the only candidate,
134
+ # or the candidates' common prefix, with the candidates shown as a message.
135
+ # Returns the candidates.
136
+ def complete_action_id
137
+ pos = @entry.position
138
+ text = @entry.text
139
+ before = text[0, pos]
140
+ m = before.match(/(?<![\w.])a\.([a-z0-9_.]*)\z/)
141
+ return [] if m.nil?
142
+ prefix = m[1]
143
+ cands = vma.actions.complete(prefix[0, (prefix.rindex(".") || -1) + 1])
144
+ cands = cands.select { |c| c.start_with?(prefix) }
145
+ return cands if cands.empty?
146
+ common = cands.reduce { |x, y|
147
+ i = 0
148
+ i += 1 while i < x.size && i < y.size && x[i] == y[i]
149
+ x[0, i]
150
+ }
151
+ # one action that is also a parent ("buf.save", "buf.save."): stop at it
152
+ common = cands[0] if cands.size == 2 && cands[1] == cands[0] + "."
153
+ if common.size > prefix.size
154
+ @entry.text = before[0, before.size - prefix.size] + common + text[pos..]
155
+ @entry.set_position(pos + common.size - prefix.size)
156
+ end
157
+ message(cands.join(" ")) if cands.size > 1
158
+ cands
159
+ end
160
+
161
+ private
162
+
163
+ # Run Ruby at top level; locals persist across invocations because
164
+ # TOPLEVEL_BINDING is reused. Result (or error) is echoed to the minibuffer.
165
+ def eval_ruby(code)
166
+ result = eval(code, TOPLEVEL_BINDING)
167
+ s = (result.inspect rescue result.class.to_s)
168
+ s = s[0, 200] + "…" if s.size > 200
169
+ message("=> #{s}")
170
+ rescue SystemExit, SignalException
171
+ raise
172
+ rescue Exception => e
173
+ message("ERROR: #{e.class}: #{e.message}")
174
+ end
175
+
176
+ # Run a shell command in a background thread; open stdout (+stderr) in a
177
+ # new buffer. GTK work is marshalled back onto the main loop.
178
+ def shell_to_buffer(cmd)
179
+ return if cmd.empty?
180
+ Thread.new do
181
+ begin
182
+ require "open3"
183
+ stdout, stderr, status = Open3.capture3(cmd)
184
+ GLib::Idle.add do
185
+ out = stdout
186
+ out += "\n[stderr]\n#{stderr}" if !stderr.empty?
187
+ out = "(no output)\n" if out.empty?
188
+ create_new_buffer(out, "shellcmd")
189
+ message("!#{cmd}: exit status #{status.exitstatus}") if !status.success?
190
+ false
191
+ end
192
+ rescue Exception => e
193
+ GLib::Idle.add do
194
+ message("ERROR: !#{cmd}: #{e.message}")
195
+ false
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ # ── History ────────────────────────────────────────────────────────────────
202
+
203
+ # dir: 1 = older, -1 = newer. Leaving the newest entry restores the text
204
+ # that was being typed before browsing started.
205
+ def history_move(dir)
206
+ return if @history.empty?
207
+ if @hist_idx.nil?
208
+ return if dir < 0
209
+ @pending_text = @entry.text
210
+ @hist_idx = 0
211
+ else
212
+ @hist_idx += dir
213
+ end
214
+ if @hist_idx < 0
215
+ @hist_idx = nil
216
+ @entry.text = @pending_text.to_s
217
+ else
218
+ @hist_idx = @history.size - 1 if @hist_idx >= @history.size
219
+ @entry.text = @history[@history.size - 1 - @hist_idx]
220
+ end
221
+ @entry.set_position(-1)
222
+ end
223
+
224
+ def add_to_history(line)
225
+ @history.delete(line)
226
+ @history << line
227
+ @history = @history.last(HISTORY_MAX)
228
+ save_history
229
+ end
230
+
231
+ def load_history
232
+ f = get_data_path(HISTORY_FILE)
233
+ return [] if !File.exist?(f)
234
+ File.readlines(f, chomp: true).reject(&:empty?).last(HISTORY_MAX)
235
+ rescue StandardError
236
+ []
237
+ end
238
+
239
+ def save_history
240
+ return if ARGV.include?("--test") # don't pollute history from test runs
241
+ f = get_data_path(HISTORY_FILE)
242
+ return if !Dir.exist?(File.dirname(f))
243
+ File.write(f, @history.join("\n") + "\n")
244
+ rescue StandardError
245
+ end
246
+ end
247
+ end # module Vimamsa
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  module Gui
2
4
  def self.confirm(title, callback, param: nil)
3
5
  params = {}
@@ -9,8 +11,8 @@ module Gui
9
11
  end
10
12
  end
11
13
 
12
- module Gtk
13
- class Frame
14
+ module ::Gtk
15
+ class Widget
14
16
  def margin=(a)
15
17
  self.margin_bottom = a
16
18
  self.margin_top = a
@@ -18,31 +20,97 @@ module Gtk
18
20
  self.margin_start = a
19
21
  end
20
22
  end
23
+ end
21
24
 
22
- class Box
23
- def margin=(a)
24
- self.margin_bottom = a
25
- self.margin_top = a
26
- self.margin_end = a
27
- self.margin_start = a
25
+ # Create a popup Gtk::Window with the shared dialog setup: "vma-dialog" CSS
26
+ # class, transient for the main window, modal by default.
27
+ def make_modal_window(title = "", modal: true)
28
+ w = Gtk::Window.new
29
+ w.add_css_class("vma-dialog")
30
+ w.set_transient_for($vmag.window) if $vmag&.window
31
+ w.modal = modal
32
+ w.title = title
33
+ w
34
+ end
35
+
36
+ # Run a dialog callback, reporting (not raising) its errors. An exception
37
+ # escaping a GTK signal handler makes ruby-gnome call exit, quitting the
38
+ # editor. Returns the block's value, or nil if it raised. Only StandardError
39
+ # is caught, so exit/SystemExit still propagates.
40
+ def run_dialog_callback(label = "dialog callback")
41
+ yield
42
+ rescue StandardError => e
43
+ message("ERROR in #{label}: #{e.message}")
44
+ debug "#{label}: #{e.class}: #{e.message}\n#{(e.backtrace || []).first(10).join("\n")}"
45
+ nil
46
+ end
47
+
48
+ # Attach a CAPTURE-phase key controller to widget: Return calls submit,
49
+ # Escape calls escape (each only when given). Any other key is offered to the
50
+ # optional block with its keyval; a truthy result marks the key as handled.
51
+ def on_submit_escape(widget, submit: nil, escape: nil, &extra)
52
+ press = Gtk::EventControllerKey.new
53
+ press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
54
+ widget.add_controller(press)
55
+ press.signal_connect "key-pressed" do |_gesture, keyval, _keycode, _state|
56
+ if submit && keyval == Gdk::Keyval::KEY_Return
57
+ run_dialog_callback("dialog submit") { submit.call }
58
+ true
59
+ elsif escape && keyval == Gdk::Keyval::KEY_Escape
60
+ run_dialog_callback("dialog escape") { escape.call }
61
+ true
62
+ else
63
+ (extra && run_dialog_callback("dialog key handler") { extra.call(keyval) }) ? true : false
28
64
  end
29
65
  end
66
+ press
30
67
  end
31
68
 
32
- def set_margin_all(widget, m)
33
- widget.margin_bottom = m
34
- widget.margin_top = m
35
- widget.margin_end = m
36
- widget.margin_start = m
69
+ # Build the common list-panel widget pair: a headerless TreeView over store
70
+ # with a single ellipsized, expanding text column, inside a
71
+ # ScrolledWindow(hpolicy, :automatic). The optional block is invoked with the
72
+ # iter of an activated row. Returns [tree, scrolled_window].
73
+ # markup: true renders text_col as Pango markup instead of plain text.
74
+ def build_list_tree(store, ellipsize: Pango::EllipsizeMode::END, text_col: 0,
75
+ single_click: true, hpolicy: :never, markup: false)
76
+ tree = Gtk::TreeView.new(store)
77
+ tree.headers_visible = false
78
+ tree.activate_on_single_click = single_click
79
+ renderer = Gtk::CellRendererText.new
80
+ renderer.ellipsize = ellipsize
81
+ col = Gtk::TreeViewColumn.new("", renderer, (markup ? :markup : :text) => text_col)
82
+ col.expand = true
83
+ tree.append_column(col)
84
+ if block_given?
85
+ tree.signal_connect("row-activated") do |_tv, path, _col|
86
+ iter = store.get_iter(path)
87
+ yield(iter) if iter
88
+ end
89
+ end
90
+ sw = Gtk::ScrolledWindow.new
91
+ sw.set_policy(hpolicy, :automatic)
92
+ sw.set_child(tree)
93
+ [tree, sw]
94
+ end
95
+
96
+ # Shared #run for toggle-style popups: show the window if hidden, destroy it
97
+ # if already visible.
98
+ module PopupRun
99
+ def run
100
+ if !@window.visible?
101
+ @window.show
102
+ else
103
+ @window.destroy
104
+ end
105
+ @window
106
+ end
37
107
  end
38
108
 
39
109
  class OneInputAction
110
+ include PopupRun
111
+
40
112
  def initialize(main_window, title, field_label, button_title, callback, opt = {})
41
- @window = Gtk::Window.new()
42
- @window.set_transient_for($vmag.window) if $vmag&.window
43
- @window.modal = true
44
- # @window.screen = main_window.screen
45
- @window.title = ""
113
+ @window = make_modal_window()
46
114
  # @window.width_request = 800
47
115
  # @window.hexpand = false
48
116
 
@@ -80,25 +148,17 @@ class OneInputAction
80
148
  @window.destroy
81
149
  end
82
150
 
151
+ @cancel_button = cancel_button
83
152
  cancel_button.signal_connect "clicked" do
84
153
  @window.destroy
85
154
  end
86
155
 
87
- press = Gtk::EventControllerKey.new
88
- press.set_propagation_phase(Gtk::PropagationPhase::CAPTURE)
89
- @window.add_controller(press)
90
- press.signal_connect "key-pressed" do |gesture, keyval, keycode, y|
91
- if keyval == Gdk::Keyval::KEY_Return
92
- callback.call(@entry1.text)
93
- @window.destroy
94
- true
95
- elsif keyval == Gdk::Keyval::KEY_Escape
96
- @window.destroy
97
- true
98
- else
99
- false
100
- end
101
- end
156
+ on_submit_escape(@window,
157
+ submit: proc {
158
+ callback.call(@entry1.text) if !@cancel_button.has_focus?
159
+ @window.destroy
160
+ },
161
+ escape: proc { @window.destroy })
102
162
 
103
163
  hbox.append(label)
104
164
  hbox.append(@entry1)
@@ -106,13 +166,5 @@ class OneInputAction
106
166
  hbox.append(cancel_button)
107
167
  return
108
168
  end
109
-
110
- def run
111
- if !@window.visible?
112
- @window.show
113
- else
114
- @window.destroy
115
- end
116
- @window
117
- end
118
169
  end
170
+ end # module Vimamsa