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
@@ -1,6 +1,13 @@
1
+
2
+ module Vimamsa
1
3
  # class VSourceView < Gtk::TextView
2
4
  class VSourceView < GtkSource::View
3
- attr_accessor :bufo, :autocp_active, :cpl_list
5
+ attr_accessor :bufo, :autocp_active
6
+
7
+ # Link / file under the last right-click. Class-wide: the app.ctx_* actions
8
+ # are registered once, by the first view, but serve the menus of all views.
9
+ @@context_link = nil
10
+ @@context_file = nil
4
11
 
5
12
  # def initialize(title = nil,bufo=nil)
6
13
  def initialize(title, bufo)
@@ -16,6 +23,9 @@ class VSourceView < GtkSource::View
16
23
  self.highlight_current_line = true
17
24
 
18
25
  @tt = nil
26
+ @applying_delta = false
27
+ @last_key_press_text = nil
28
+ autocp_init
19
29
 
20
30
  # Mainly after page-up or page-down
21
31
 
@@ -52,11 +62,14 @@ class VSourceView < GtkSource::View
52
62
  end
53
63
 
54
64
  def set_content(str)
65
+ @applying_delta = true
55
66
  if @bufo&.lang == "hyperplaintext"
56
67
  self.buffer.set_text(mask_for_display(str))
57
68
  else
58
69
  self.buffer.set_text(str)
59
70
  end
71
+ @applying_delta = false
72
+ $hook&.call(:view_content_set, self)
60
73
  end
61
74
 
62
75
  # Sync the GTK buffer to mask_for_display(@bufo.to_s).
@@ -77,10 +90,12 @@ class VSourceView < GtkSource::View
77
90
  j = expected.length - 1
78
91
  j -= 1 while j >= i && expected[j] == gtk_text[j]
79
92
 
93
+ @applying_delta = true
80
94
  s_iter = buffer.get_iter_at(:offset => i)
81
95
  e_iter = buffer.get_iter_at(:offset => j + 1)
82
96
  buffer.delete(s_iter, e_iter)
83
97
  buffer.insert(buffer.get_iter_at(:offset => i), expected[i..j])
98
+ @applying_delta = false
84
99
  end
85
100
 
86
101
  # Returns [word, word_start, word_end] if +pos+ (Ruby-buffer offset) is
@@ -99,10 +114,12 @@ class VSourceView < GtkSource::View
99
114
 
100
115
  # Replace the masked (***) span with the real word from the Ruby buffer.
101
116
  def unmask_gtk_region(word, word_start, word_end)
117
+ @applying_delta = true
102
118
  s_iter = buffer.get_iter_at(:offset => word_start)
103
119
  e_iter = buffer.get_iter_at(:offset => word_end)
104
120
  buffer.delete(s_iter, e_iter)
105
121
  buffer.insert(buffer.get_iter_at(:offset => word_start), word)
122
+ @applying_delta = false
106
123
  end
107
124
 
108
125
  # When in insert mode with cursor on a «word, expose the real text in the GTK
@@ -253,6 +270,13 @@ class VSourceView < GtkSource::View
253
270
  self.add_controller(@cnt_drag)
254
271
  @cnt_drag.signal_connect "drag-begin" do |gesture, x, y|
255
272
  debug "drag-begin", 2
273
+ @drag_start_x = x
274
+ @drag_start_y = y
275
+ # Pressed inside the selection: drag-end moves it (see "pressed")
276
+ if @move_sel
277
+ set_cursor_from_name("grabbing")
278
+ next
279
+ end
256
280
  i = coord_to_iter(x, y, true)
257
281
  pp i
258
282
  @range_start = i
@@ -262,6 +286,13 @@ class VSourceView < GtkSource::View
262
286
  end
263
287
 
264
288
  @cnt_drag.signal_connect "drag-update" do |gesture, offset_x, offset_y|
289
+ if @move_sel
290
+ t = drop_pos(@drag_start_x + offset_x, @drag_start_y + offset_y)
291
+ # Dropping inside (or right after) the selection would not move it
292
+ no_op = t.nil? || (t >= @move_sel.begin && t <= @move_sel.end + 1)
293
+ show_drop_marker(no_op ? nil : t)
294
+ next
295
+ end
265
296
  next unless @range_start
266
297
  cur_x = @drag_start_x + offset_x
267
298
  cur_y = @drag_start_y + offset_y
@@ -276,6 +307,25 @@ class VSourceView < GtkSource::View
276
307
 
277
308
  @cnt_drag.signal_connect "drag-end" do |gesture, offsetx, offsety|
278
309
  debug "drag-end", 2
310
+ if @move_sel
311
+ r = @move_sel
312
+ @move_sel = nil
313
+ set_cursor_from_name("text")
314
+ show_drop_marker(nil)
315
+ if offsetx.abs < 5 and offsety.abs < 5
316
+ # Just a click inside the selection: clear it like any other click
317
+ @bufo.end_visual_mode
318
+ i = coord_to_iter(@drag_start_x, @drag_start_y, true)
319
+ @bufo.set_pos(i) if !i.nil?
320
+ else
321
+ target = drop_pos(@drag_start_x + offsetx, @drag_start_y + offsety)
322
+ # Dropping ends the selection, whether or not the text moved
323
+ @bufo.end_visual_mode || @bufo.end_selection
324
+ @bufo.move_text(r, target)
325
+ end
326
+ handle_deltas
327
+ next
328
+ end
279
329
  if offsetx.abs < 5 and offsety.abs < 5
280
330
  debug "Not enough drag", 2
281
331
  buf.end_selection
@@ -312,15 +362,25 @@ class VSourceView < GtkSource::View
312
362
  debug "SourceView, GestureClick released x=#{x} y=#{y}"
313
363
  vma.gui.instance_variable_set(:@kbd_passthrough, false)
314
364
 
315
- if buf.visual_mode?
316
- buf.end_visual_mode
317
- end
318
-
319
365
  xloc = (x - gutter_width).to_i
320
366
  yloc = (y + visible_rect.y).to_i
321
367
  debug "xloc=#{xloc} yloc=#{yloc}"
322
368
 
323
369
  i = coord_to_iter(xloc, yloc)
370
+
371
+ # Pressing inside the selection grabs it for a drag-and-drop move
372
+ # (cnf.mouse.drag_selection). The selection stays until drag-end.
373
+ @move_sel = nil
374
+ if cnf.mouse.drag_selection? and n_press == 1 and !i.nil? and
375
+ vma.gui.current_view == self and @bufo.visual_mode? and
376
+ @bufo.get_visual_mode_range.include?(i)
377
+ @move_sel = @bufo.get_visual_mode_range
378
+ next true
379
+ end
380
+
381
+ if buf.visual_mode?
382
+ buf.end_visual_mode
383
+ end
324
384
  # @range_start = i
325
385
 
326
386
  # This needs to happen after xloc calculation, otherwise xloc gets a wrong value (around 200 bigger)
@@ -331,8 +391,14 @@ class VSourceView < GtkSource::View
331
391
  @bufo.set_pos(i) if !i.nil?
332
392
  if n_press == 2
333
393
  #TODO: refactor to have one function for all line actions
334
- if @bufo.module&.respond_to?(:select_line)
335
- @bufo.module.select_line
394
+ # Same as Enter (Buffer#handle_line_action): the buffer's own line
395
+ # handler first. Ack's select_line takes the line number, so it is
396
+ # only reachable that way.
397
+ m = @bufo.module
398
+ if [Proc, Method].include?(@bufo.line_action_handler.class)
399
+ @bufo.line_action_handler.call(@bufo.lpos)
400
+ elsif m&.respond_to?(:select_line) && m.method(:select_line).arity == 0
401
+ m.select_line
336
402
  else
337
403
  @bufo.cur_nonwhitespace_word_action()
338
404
  end
@@ -342,6 +408,7 @@ class VSourceView < GtkSource::View
342
408
 
343
409
  click.signal_connect "released" do |gesture, n_press, x, y, z|
344
410
  debug "SourceView, GestureClick released x=#{x} y=#{y} button=#{gesture.button}"
411
+ next true if @move_sel # drag-end handles it
345
412
 
346
413
  xloc = (x - gutter_width).to_i
347
414
  yloc = (y + visible_rect.y).to_i
@@ -362,6 +429,93 @@ class VSourceView < GtkSource::View
362
429
  # @range_start = nil
363
430
  true
364
431
  end
432
+
433
+ end
434
+
435
+ # Must be called after set_buffer(buf) so the signal connects to the live buffer,
436
+ # not the temporary internal one created by super().
437
+ def register_buffer_signals
438
+ # Catch Wayland IM text commits (e.g. AltGr+q → ä).
439
+ # On Wayland the compositor feeds composed characters directly into the
440
+ # GtkTextBuffer via the text-input protocol, bypassing key events entirely.
441
+ # Our key handler never sees a key_press for these characters, so they are
442
+ # routed into the key binding tree here instead — what a character does is
443
+ # then up to the bindings ("I <char>" inserts, "C r <char>" replaces), not
444
+ # to this handler.
445
+ buffer.signal_connect("insert-text") do |_buf, _iter, text, _len|
446
+ unless @applying_delta || @bufo.nil?
447
+ # Cancel the insertion before it happens: the Ruby buffer is the source
448
+ # of truth, and letting the text land here first would put it on screen
449
+ # in the wrong place (or in the wrong mode) until a delete took it back
450
+ # out. Stopping the emission keeps the default handler from running, so
451
+ # nothing is ever inserted and nothing has to be undone.
452
+ _buf.signal_emit_stop("insert-text")
453
+ # A printable key arrives twice: as a key event AND as this IM commit.
454
+ # The key event already went to the key binding tree, so only a commit
455
+ # with no matching key press (AltGr, dead keys, compose) still needs to
456
+ # be fed in. Consume the record either way — the next keystroke, even a
457
+ # repeat of the same character, gets its own.
458
+ im_keys = (@last_key_press_text == text) ? nil : text
459
+ @last_key_press_text = nil
460
+ # The binding that handles the character ("I <char>", "C r <char>", ...)
461
+ # puts it in through the normal delta path.
462
+ dispatch_im_text_as_keys(im_keys) if im_keys
463
+ end
464
+ false
465
+ end
466
+ end
467
+
468
+ # Hand IM-committed text to the key binding tree as if it had arrived as key
469
+ # presses. Deferred to an idle because we are inside the "insert-text"
470
+ # emission, whose emitter still holds iterators into the buffer that the
471
+ # action is free to mutate. Whatever the action queued is flushed afterwards,
472
+ # the same way handle_key_event flushes after a real key press.
473
+ def dispatch_im_text_as_keys(text)
474
+ GLib::Idle.add(proc {
475
+ text.each_char { |c| vma.kbd.match_key_conf(c, nil, :key_press) }
476
+ handle_deltas
477
+ false
478
+ })
479
+ end
480
+
481
+ # Insertion point (char boundary nearest to widget coords x, y) for dropping
482
+ # dragged text: past the middle of a char means after it.
483
+ def drop_pos(x, y)
484
+ bx = (x - gutter_width).to_i
485
+ by = (y + visible_rect.y).to_i
486
+ iter, trailing = get_iter_at_position(bx, by)
487
+ if iter.nil? # Not over text (e.g. right of a line's end): end of that line
488
+ ls = coord_to_iter(x, y, true)
489
+ return nil if ls.nil?
490
+ return bx > 0 ? (@bufo.index("\n", ls) || ls) : ls
491
+ end
492
+ # Not past a line's "\n": that would drop onto the start of the next line
493
+ return iter.offset if @bufo[iter.offset] == "\n"
494
+ return iter.offset + trailing.to_i
495
+ end
496
+
497
+ # Thin bar showing where dragged text would land; pos nil hides it.
498
+ attr_reader :drop_marker_pos
499
+ def show_drop_marker(pos)
500
+ @drop_marker_pos = pos
501
+ if pos.nil?
502
+ @drop_marker.visible = false if @drop_marker
503
+ return
504
+ end
505
+ rect = get_iter_location(buffer.get_iter_at(:offset => pos))
506
+ if @drop_marker.nil?
507
+ @drop_marker = Gtk::Box.new(:horizontal, 0)
508
+ @drop_marker.can_target = false # never steal the pointer from the view
509
+ prov = Gtk::CssProvider.new
510
+ bg = $confh[:mode][:command][:cursor][:background] rescue nil
511
+ prov.load(data: "box { background-color: #{bg || "#05c5a0"}; }")
512
+ @drop_marker.style_context.add_provider(prov, Gtk::StyleProvider::PRIORITY_APPLICATION)
513
+ add_overlay(@drop_marker, rect.x, rect.y)
514
+ else
515
+ move_overlay(@drop_marker, rect.x, rect.y)
516
+ end
517
+ @drop_marker.set_size_request(2, rect.height)
518
+ @drop_marker.visible = true
365
519
  end
366
520
 
367
521
  def coord_to_iter(xloc, yloc, transform_coord = false)
@@ -403,6 +557,30 @@ class VSourceView < GtkSource::View
403
557
  }
404
558
  end
405
559
 
560
+ # Buffer offset of the line at the top of the visible area
561
+ def top_visible_pos
562
+ r = get_line_at_y(visible_rect.y)
563
+ return nil if r.nil? || r[0].nil?
564
+ r[0].offset
565
+ end
566
+
567
+ # Number of (logical) lines that fit in the visible area
568
+ def visible_line_count
569
+ vr = visible_rect
570
+ a = get_line_at_y(vr.y)
571
+ b = get_line_at_y(vr.y + vr.height - 1)
572
+ return nil if a.nil? || b.nil? || a[0].nil? || b[0].nil?
573
+ b[0].line - a[0].line
574
+ end
575
+
576
+ # Scroll so that the line containing pos is at the top of the view
577
+ def scroll_to_top_pos(pos)
578
+ itr = buffer.get_iter_at(:offset => pos)
579
+ @top_mark ||= buffer.create_mark("top", itr, true)
580
+ buffer.move_mark(@top_mark, itr)
581
+ scroll_to_mark(@top_mark, 0.0, true, 0.0, 0.0)
582
+ end
583
+
406
584
  def set_cursor_to_top
407
585
  debug "set_cursor_to_top", 2
408
586
  bc = window_to_buffer_coords(Gtk::TextWindowType::WIDGET, gutter_width + 2, 60)
@@ -432,75 +610,16 @@ class VSourceView < GtkSource::View
432
610
  # Gdk::Keyval.to_name()
433
611
  # end
434
612
 
435
- keyval_trans = {}
436
- keyval_trans[Gdk::Keyval::KEY_Control_L] = "ctrl"
437
- keyval_trans[Gdk::Keyval::KEY_Control_R] = "ctrl"
438
-
439
- keyval_trans[Gdk::Keyval::KEY_Escape] = "esc"
440
-
441
- keyval_trans[Gdk::Keyval::KEY_Return] = "enter"
442
- keyval_trans[Gdk::Keyval::KEY_ISO_Enter] = "enter"
443
- keyval_trans[Gdk::Keyval::KEY_KP_Enter] = "enter"
444
- keyval_trans[Gdk::Keyval::KEY_Alt_L] = "alt"
445
- keyval_trans[Gdk::Keyval::KEY_Alt_R] = "alt"
446
- keyval_trans[Gdk::Keyval::KEY_Caps_Lock] = "caps"
447
-
448
- keyval_trans[Gdk::Keyval::KEY_BackSpace] = "backspace"
449
- keyval_trans[Gdk::Keyval::KEY_KP_Page_Down] = "pagedown"
450
- keyval_trans[Gdk::Keyval::KEY_KP_Page_Up] = "pageup"
451
- keyval_trans[Gdk::Keyval::KEY_Page_Down] = "pagedown"
452
- keyval_trans[Gdk::Keyval::KEY_Page_Up] = "pageup"
453
- keyval_trans[Gdk::Keyval::KEY_Left] = "left"
454
- keyval_trans[Gdk::Keyval::KEY_Right] = "right"
455
- keyval_trans[Gdk::Keyval::KEY_Down] = "down"
456
- keyval_trans[Gdk::Keyval::KEY_Up] = "up"
457
- keyval_trans[Gdk::Keyval::KEY_space] = "space"
458
-
459
- keyval_trans[Gdk::Keyval::KEY_Shift_L] = "shift"
460
- keyval_trans[Gdk::Keyval::KEY_Shift_R] = "shift"
461
- keyval_trans[Gdk::Keyval::KEY_Tab] = "tab"
462
- keyval_trans[Gdk::Keyval::KEY_ISO_Left_Tab] = "tab"
463
-
464
- key_trans = {}
465
- key_trans["\e"] = "esc"
466
- tk = keyval_trans[keyval]
467
- keyname = tk if !tk.nil?
468
-
469
- key_str_parts = []
470
- key_str_parts << "ctrl" if vma.kbd.modifiers[:ctrl]
471
- key_str_parts << "alt" if vma.kbd.modifiers[:alt]
472
- key_str_parts << "shift" if vma.kbd.modifiers[:shift]
473
- key_str_parts << "meta" if vma.kbd.modifiers[:meta]
474
- key_str_parts << "super" if vma.kbd.modifiers[:super]
475
- key_str_parts << keyname
476
-
477
- # After remapping capslock to control in gnome-tweak tool,
478
- # if pressing and immediately releasing the capslock (control) key,
479
- # we get first "caps" on keydown and ctrl-"caps" on keyup (keyval 65509, Gdk::Keyval::KEY_Caps_Lock)
480
- # If mapping capslock to ctrl in hardware, we get keyval 65507 (Gdk::Keyval::KEY_Control_L) instead
481
- if key_str_parts[0] == "ctrl" and key_str_parts[1] == "caps"
482
- # Replace ctrl-caps with ctrl
483
- key_str_parts.delete_at(1)
484
- end
485
-
486
- if key_str_parts[0] == key_str_parts[1]
487
- # We don't want "ctrl-ctrl" or "alt-alt"
488
- # TODO:There should be a better way to do this
489
- key_str_parts.delete_at(0)
490
- end
491
-
492
- if key_str_parts[0] == "shift" and key_str_parts.size == 2
493
- if key_str_parts[1].size == 1 # and key_str_parts[1].match(/^[[:upper:]]$/)
494
- #"shift-P" to just "P" etc.
495
- # but keep shift-tab as is
496
- key_str_parts.delete_at(0)
497
- end
498
- end
499
-
500
- key_str = key_str_parts.join("-")
501
- if key_str == "\u0000"
502
- key_str = ""
503
- end
613
+ # The editor's name for the key (key_names.rb, shared with forms)
614
+ key_str = gui_key_name(keyval, keyname)
615
+ keyname = GUI_KEYVAL_NAMES[keyval] || keyname
616
+
617
+ # if keyname == "\u0000"
618
+ # puts "FOOOO 0000"
619
+ # return false
620
+ # keyname= ""
621
+ # end
622
+
504
623
 
505
624
  keynfo = { :key_str => key_str, :key_name => keyname, :keyval => keyval }
506
625
  debug keynfo.inspect
@@ -512,6 +631,9 @@ class VSourceView < GtkSource::View
512
631
  vma.kbd.match_key_conf(key_str + "!", nil, :key_release)
513
632
  @last_event = [keynfo, :key_release]
514
633
  elsif sig == :key_press
634
+ # Remember a printable key so the IM commit that echoes it (see
635
+ # register_buffer_signals) is not fed to the bindings a second time.
636
+ @last_key_press_text = key_str.size == 1 ? key_str.dup : nil
515
637
  vma.kbd.match_key_conf(key_str, nil, :key_press)
516
638
  @last_event = [keynfo, key_str, :key_press]
517
639
  end
@@ -549,7 +671,9 @@ class VSourceView < GtkSource::View
549
671
  return pos_to_coord(buffer.cursor_position)
550
672
  end
551
673
 
552
- def handle_deltas()
674
+ # scroll: false places the cursor without scrolling to it (the caller
675
+ # positions the view itself, e.g. Buffer#revert_by_diff).
676
+ def handle_deltas(scroll: true)
553
677
  any_change = false
554
678
  while d = @bufo.deltas.shift
555
679
  any_change = true
@@ -562,14 +686,17 @@ class VSourceView < GtkSource::View
562
686
  enditer = buffer.get_iter_at(:offset => pos + num)
563
687
  buffer.delete(startiter, enditer)
564
688
  elsif op == INSERT
689
+ @applying_delta = true
565
690
  startiter = buffer.get_iter_at(:offset => pos)
566
691
  buffer.insert(startiter, txt)
692
+ @applying_delta = false
567
693
  end
568
694
  end
569
695
  if any_change
570
696
  remask_gtk_buffer
571
697
  #TODO: only when necessary
572
- self.set_cursor_pos(pos)
698
+ self.set_cursor_pos(@bufo.pos, scroll: scroll)
699
+ $hook&.call(:view_text_changed, self)
573
700
  end
574
701
 
575
702
  # sanity_check #TODO
@@ -590,7 +717,7 @@ class VSourceView < GtkSource::View
590
717
  end
591
718
  end
592
719
 
593
- def set_cursor_pos(pos)
720
+ def set_cursor_pos(pos, scroll: true)
594
721
  itr = buffer.get_iter_at(:offset => pos)
595
722
  itr2 = buffer.get_iter_at(:offset => pos + 1)
596
723
  buffer.place_cursor(itr)
@@ -605,6 +732,7 @@ class VSourceView < GtkSource::View
605
732
  else
606
733
  buffer.move_mark(@curpos_mark, itr)
607
734
  end
735
+ return true if !scroll
608
736
  scroll_to_mark(@curpos_mark, within_margin, use_align, xalign, yalign)
609
737
  $idle_scroll_to_mark = true
610
738
  ensure_cursor_visible
@@ -682,13 +810,14 @@ class VSourceView < GtkSource::View
682
810
  def set_cursor_color(ctype)
683
811
  if @ctype != ctype
684
812
  bg = $confh[:mode][ctype][:cursor][:background]
813
+ bg = "#000000" if ctype == :insert && vma.gui.scheme_is_light
685
814
  if bg.class == String
686
815
  if !@cursor_prov.nil?
687
816
  self.style_context.remove_provider(@cursor_prov)
688
817
  end
689
818
  prov = Gtk::CssProvider.new
690
819
  # prov.load(data: ".view text selection { background-color: #{bg}; color: #ffffff; }")
691
- prov.load(data: ".view text selection { background-color: #{bg}; color: #ffffff; } .view { caret-color: #{bg}; }")
820
+ prov.load(data: ".view text selection { background-color: #{bg}; color: #{theme_color(:selection_fg)}; } .view { caret-color: #{bg}; }")
692
821
  self.style_context.add_provider(prov)
693
822
  @cursor_prov = prov
694
823
  end
@@ -745,7 +874,8 @@ class VSourceView < GtkSource::View
745
874
 
746
875
  # Sometimes we lose focus and the cursor vanishes because of that
747
876
  # TODO: determine why&when
748
- if !self.has_focus?
877
+ # Not when an entry widget (e.g. the ":" command line) has focus on purpose.
878
+ if !self.has_focus? and !vma.gui.entry_focused?
749
879
  self.grab_focus
750
880
  self.cursor_visible = false
751
881
  self.cursor_visible = true
@@ -755,13 +885,13 @@ class VSourceView < GtkSource::View
755
885
  end #end draw_cursor
756
886
 
757
887
  CONTEXT_MENU_ITEMS = [
758
- ["Paste", :paste_after_cursor],
759
- ["Previous buffer", :history_switch_backwards],
888
+ ["Paste", "edit.paste.after"],
889
+ ["Previous buffer", "buf.history.prev"],
760
890
  ]
761
891
 
762
892
  CONTEXT_MENU_ITEMS_SELECTION = [
763
- ["Copy", :copy_selection],
764
- ["Cut", :cut_selection],
893
+ ["Copy", "sel.copy"],
894
+ ["Cut", "sel.cut"],
765
895
  ]
766
896
 
767
897
  def init_context_menu
@@ -778,20 +908,49 @@ class VSourceView < GtkSource::View
778
908
  end
779
909
  end
780
910
 
911
+ unless vma.gui.app.lookup_action("ctx_lsp_jump_to_definition")
912
+ act = Gio::SimpleAction.new("ctx_lsp_jump_to_definition")
913
+ vma.gui.app.add_action(act)
914
+ act.signal_connect("activate") do
915
+ call_action("lsp.definition")
916
+ after_action
917
+ end
918
+ end
919
+
781
920
  unless vma.gui.app.lookup_action("ctx_open_link")
782
921
  act = Gio::SimpleAction.new("ctx_open_link")
783
922
  vma.gui.app.add_action(act)
784
923
  act.signal_connect("activate") do
785
- next unless @context_link
786
- if is_url(@context_link)
787
- open_url(@context_link)
924
+ next unless @@context_link
925
+ if is_url(@@context_link)
926
+ open_url(@@context_link)
927
+ elsif m = @@context_link.match(/⟦help:(.*)⟧/)
928
+ vma.buf.handle_word([m[1], :help])
929
+ elsif m = @@context_link.match(/⟦run:(.*)⟧/)
930
+ vma.buf.handle_word([m[1], :run])
788
931
  else
789
- fn = hpt_check_cur_word(@context_link)
790
- open_existing_file(fn) if fn
932
+ fn = hpt_check_cur_word(@@context_link)
933
+ open_new_file(fn) if fn
791
934
  end
792
935
  end
793
936
  end
794
937
 
938
+ unless vma.gui.app.lookup_action("ctx_open_file")
939
+ act = Gio::SimpleAction.new("ctx_open_file")
940
+ vma.gui.app.add_action(act)
941
+ act.signal_connect("activate") do
942
+ open_new_file(@@context_file) if @@context_file
943
+ end
944
+ end
945
+
946
+ unless vma.gui.app.lookup_action("ctx_open_file_external")
947
+ act = Gio::SimpleAction.new("ctx_open_file_external")
948
+ vma.gui.app.add_action(act)
949
+ act.signal_connect("activate") do
950
+ open_with_default_program(@@context_file) if @@context_file
951
+ end
952
+ end
953
+
795
954
  @context_menu = Gtk::PopoverMenu.new
796
955
  @context_menu.set_parent(self)
797
956
  @context_menu.has_arrow = false
@@ -801,6 +960,9 @@ class VSourceView < GtkSource::View
801
960
  bx, by = window_to_buffer_coords(:widget, x.to_i, y.to_i)
802
961
  iter = get_iter_at_location(bx, by)
803
962
  return nil unless iter
963
+ # ⟦run:…⟧ / ⟦help:…⟧ links may have spaces in them
964
+ link = hpt_link_at(@bufo, iter.offset)
965
+ return link if link
804
966
  word, _range = @bufo.get_word_in_pos(iter.offset, boundary: :space)
805
967
  return nil unless word
806
968
  return word if is_url(word)
@@ -808,11 +970,36 @@ class VSourceView < GtkSource::View
808
970
  nil
809
971
  end
810
972
 
973
+ def file_at(x, y)
974
+ bx, by = window_to_buffer_coords(:widget, x.to_i, y.to_i)
975
+ iter = get_iter_at_location(bx, by)
976
+ return nil unless iter
977
+ @bufo.file_path_at(iter.offset)
978
+ end
979
+
811
980
  def show_context_menu(x, y)
812
981
  init_context_menu if @context_menu.nil?
982
+
983
+ # Move the text cursor to the right-click position so that cursor-dependent
984
+ # actions (e.g. Jump to Definition) operate on the clicked symbol.
985
+ # Skip if there is an active selection — the user may be right-clicking
986
+ # inside it to copy/paste, and moving the cursor would clear it.
987
+ unless @bufo.selection_active?
988
+ bx, by = window_to_buffer_coords(:widget, x.to_i, y.to_i)
989
+ iter = get_iter_at_location(bx, by)
990
+ @bufo.set_pos(iter.offset) if iter
991
+ end
992
+
813
993
  menu = Gio::Menu.new
814
- @context_link = link_at(x, y)
815
- menu.append("Open link", "app.ctx_open_link") if @context_link
994
+ @@context_link = link_at(x, y)
995
+ menu.append("Open link", "app.ctx_open_link") if @@context_link
996
+ # Clicked text names an existing file (e.g. "report.pdf")
997
+ @@context_file = @@context_link ? nil : file_at(x, y)
998
+ if @@context_file
999
+ menu.append("Open file", "app.ctx_open_file") if file_is_text_file(@@context_file)
1000
+ menu.append("Open file (external)", "app.ctx_open_file_external")
1001
+ end
1002
+ menu.append("Jump to Definition", "app.ctx_lsp_jump_to_definition") if @bufo.lsp
816
1003
  CONTEXT_MENU_ITEMS.each { |label, action_id| menu.append(label, "app.ctx_#{action_id}") }
817
1004
  if @bufo.selection_active?
818
1005
  CONTEXT_MENU_ITEMS_SELECTION.each { |label, action_id| menu.append(label, "app.ctx_#{action_id}") }
@@ -822,3 +1009,4 @@ class VSourceView < GtkSource::View
822
1009
  @context_menu.popup
823
1010
  end
824
1011
  end
1012
+ end # module Vimamsa