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,45 @@
1
+ # Notepad scheme: ordinary non-modal editing (gedit/notepad style).
2
+ # Inherits all modes, infrastructure and bindings from the vimlike scheme,
3
+ # then overrides so the editor stays permanently in insert mode.
4
+ # Selected via cnf.keybindings.scheme = "notepad" (applied on restart).
5
+ require "vimamsa/key_bindings_vimlike"
6
+
7
+ module Vimamsa
8
+ vma.kbd.set_default_mode(:insert)
9
+ vma.kbd.__set_mode(:insert)
10
+
11
+ # esc never leaves insert mode: clear selection, dismiss autocomplete popup
12
+ reg_act("mode.notepad.escape", proc {
13
+ buf.end_selection
14
+ v = vma.buf.view
15
+ v.hide_completions if v.respond_to?(:hide_completions)
16
+ }, "Clear selection and dismiss popups")
17
+
18
+ # Replaces the "I esc" leaf of vimlike's "I esc || I ctrl!" => "mode.prev"
19
+ bindkey "I esc", "mode.notepad.escape"
20
+ # In vimlike, releasing bare ctrl in insert mode returns to command mode.
21
+ # Harmless here (mode stack is [:insert], to_previous_mode won't pop) but
22
+ # remove it so ctrl-release doesn't dispatch a pointless action.
23
+ unbindkey "I ctrl!"
24
+
25
+ add_keys "notepad", {
26
+ # Rebinds over conflicting vimlike insert-mode bindings
27
+ # (bindkey on an existing chord overwrites the leaf)
28
+ "I ctrl-f" => "search.buf", # was move FORWARD_CHAR
29
+ "I ctrl-h" => "search.replace", # was autocp trigger (ctrl-space remains)
30
+ "I ctrl-n" => "buf.new", # was move_next_line
31
+ "I ctrl-a" => "sel.all", # was jump_beginning_of_line
32
+ "I ctrl-q" => "app.quit",
33
+ "I ctrl-shift-z || I ctrl-shift-Z" => "undo.redo", # shift+z may arrive uppercase
34
+ "I delete" => "edit.delete.char_or_sel",
35
+ "I ctrl-home" => "move.buf.start",
36
+ "I ctrl-end" => "move.buf.end",
37
+ }
38
+
39
+ add_keys "notepad cat:motion", {
40
+ "I home" => "move.line.start.unselect",
41
+ "I end" => "move.line.end.unselect",
42
+ "I shift-home" => "sel.line.start",
43
+ "I shift-end" => "sel.line.end",
44
+ }
45
+ end # module Vimamsa
@@ -0,0 +1,484 @@
1
+
2
+ module Vimamsa
3
+ # Editor-side reports of the key binding tree contents, shown as buffers.
4
+ # Kept out of key_binding_tree.rb so the tree stays free of editor/GUI calls.
5
+
6
+ def show_free_key_bindings()
7
+ kbd_s = "❙Free key binding slots❙\n"
8
+ kbd_s << "\n⦁[Mode] <prefix> : <free keys>⦁\n"
9
+ kbd_s << "[B]=Browse, [C]=Command, [I]=Insert, [V]=Visual\n"
10
+ kbd_s << "Free = not yet bound under that prefix\n"
11
+ kbd_s << "===============================================\n"
12
+ kbd_s << vma.kbd.get_free_bindings
13
+ kbd_s << "\n"
14
+ b = create_new_buffer(kbd_s, "free-key-bindings")
15
+ gui_set_file_lang(b.id, "hyperplaintext")
16
+ end
17
+
18
+ # Categories of the key bindings page, in page order: [id, heading]
19
+ # (doc/key_bindings_report_plan.md). Modules and minor modes get their own
20
+ # "module:<name>" / "mode:<id>" categories after these.
21
+ KEY_CATEGORIES = [
22
+ ["motion", "Moving the cursor"],
23
+ ["search", "Searching and jumping"],
24
+ ["insert", "Entering insert mode and typing"],
25
+ ["edit", "Delete, change, copy, paste"],
26
+ ["textobj", "Text objects"],
27
+ ["case", "Case and text transforms"],
28
+ ["visual", "Visual mode and selections"],
29
+ ["undo", "Undo, redo, repeat"],
30
+ ["marks", "Marks and bookmarks"],
31
+ ["macro", "Macros"],
32
+ ["file", "Files"],
33
+ ["buffer", "Buffers and windows"],
34
+ ["view", "Panels and view"],
35
+ ["grep", "Search across files"],
36
+ ["code", "Code (LSP, comments, run line)"],
37
+ ["hpt", "Hyper plaintext"],
38
+ ["app", "Application"],
39
+ ["help", "Help"],
40
+ ["custom", "Your bindings (custom.rb)"],
41
+ ["debug", "Debug / experimental"],
42
+ ["internal", "Internal (argument input)"],
43
+ ]
44
+
45
+ # Action namespace => category. A longer prefix wins ("mode.visual").
46
+ ACTION_NS_CATEGORY = {
47
+ "app" => "app", "mode" => "insert", "mode.visual" => "visual", "mode.cmdline" => "app",
48
+ "mode.browse" => "buffer", "mode.passthrough" => "debug", "mode.minibuffer" => "debug",
49
+ "help" => "help", "debug" => "debug", "move" => "motion", "jump" => "search",
50
+ "search" => "search", "grep" => "grep", "ack" => "grep", "edit" => "edit",
51
+ "edit.comment" => "code", "edit.indent" => "code", "undo" => "undo", "case" => "case",
52
+ "sel" => "visual", "buf" => "buffer", "file" => "file", "panel" => "view", "view" => "view",
53
+ "style" => "hpt", "macro" => "macro", "mark" => "marks", "lsp" => "code", "term" => "code",
54
+ "custom" => "custom", "op" => "edit", "op.textobj" => "textobj", "sel.textobj" => "textobj", "kbd" => "internal", "kbd.count" => "undo",
55
+ "mode.replace" => "edit", "mode.audio" => "app", "edit.insert" => "insert",
56
+ "edit.tab" => "insert", "edit.untab" => "insert", "edit.complete" => "insert",
57
+ }
58
+
59
+ # Bindings in these major modes, when nothing else decides
60
+ KEY_MODE_CATEGORY = { "B" => "buffer", "X" => "edit", "R" => "internal", "F" => "edit" }
61
+
62
+ # Words of older section keywords (add_keys "vim motion", ...)
63
+ KEY_WORD_CATEGORY = {
64
+ "motion" => "motion", "edit" => "edit", "visual" => "visual", "insert" => "insert",
65
+ "replace" => "edit", "macros" => "macro", "text" => "textobj",
66
+ "hyperplaintext" => "hpt", "experimental" => "debug",
67
+ }
68
+
69
+ # The category id of a binding record (KeyBindingTree#bindings), or nil.
70
+ # kbd: the tree it came from (for its actions and modes).
71
+ def key_binding_category(b, kbd = vma.kbd)
72
+ mode = b[:mode]
73
+ return "mode:#{mode}" if mode.match?(/\A\p{Ll}/) # minor mode
74
+ a = b[:action]
75
+ id = a.to_s
76
+ if (a.class == String || a.class == Symbol) && id.match?(ACTION_ID_RE)
77
+ named = [vma.actions, kbd.actions].compact.any? { |acts|
78
+ x = acts[id]
79
+ x && !x.opt&.[](:code)
80
+ }
81
+ if named
82
+ segs = id.split(".")
83
+ segs.size.downto(1) { |n|
84
+ c = ACTION_NS_CATEGORY[segs[0, n].join(".")]
85
+ return c if c
86
+ }
87
+ return "module:#{segs[0]}" if File.directory?(File.join(__dir__, "../../modules", segs[0]))
88
+ return "mode:#{segs[0]}"
89
+ end
90
+ end
91
+ kws = b[:keywords]
92
+ kws.each { |k| return k.sub("cat:", "") if k.start_with?("cat:") }
93
+ kws.each { |k| return k if k.start_with?("module:") }
94
+ return "custom" if kws.include?("custom")
95
+ return KEY_MODE_CATEGORY[mode] if KEY_MODE_CATEGORY[mode]
96
+ kws.each { |k| return KEY_WORD_CATEGORY[k] if KEY_WORD_CATEGORY[k] }
97
+ nil
98
+ end
99
+
100
+ def key_category_heading(cat)
101
+ return "Uncategorized" if cat.nil?
102
+ h = KEY_CATEGORIES.find { |id, _| id == cat }
103
+ return h[1] if h
104
+ return "Module: #{cat.sub("module:", "")}" if cat.start_with?("module:")
105
+ return "Minor mode: #{cat.sub("mode:", "")}" if cat.start_with?("mode:")
106
+ "Uncategorized"
107
+ end
108
+
109
+ # Bindings grouped by category, in page order: [[category, [records]], ...].
110
+ # Bindings with no category come last, under nil.
111
+ def key_bindings_by_category(kbd = vma.kbd)
112
+ by = kbd.bindings.group_by { |b| key_binding_category(b, kbd) }
113
+ order = KEY_CATEGORIES.map(&:first)
114
+ rank = ->(c) {
115
+ c.nil? ? [3, ""] : order.include?(c) ? [0, order.index(c)] : c.start_with?("module:") ? [1, c] : [2, c]
116
+ }
117
+ by.sort_by { |c, _| rank.(c) }
118
+ end
119
+
120
+ # Actions listed under Basics, in this order, with whatever keys the active
121
+ # keymap binds them to (they also appear in their own category)
122
+ BASIC_ACTIONS = %w[
123
+ mode.insert mode.prev mode.visual
124
+ move.word.next move.word.prev move.line.start move.line.end move.buf.start move.buf.end
125
+ edit.delete.char edit.delete.line edit.line.copy edit.paste.after edit.paste.before
126
+ sel.copy sel.cut undo.last undo.redo
127
+ search.buf search.next search.actions
128
+ file.open file.finder buf.save buf.manager buf.close
129
+ mode.cmdline help.keys app.quit
130
+ ]
131
+
132
+ KEY_MODE_NAMES = {
133
+ "C" => "Command", "V" => "Visual", "I" => "Insert", "X" => "Replace",
134
+ "B" => "Browse", "R" => "Read char", "M" => "Minibuffer", "F" => "Form",
135
+ }
136
+
137
+ # Binding conditions ("C G(rule)") as text; others are shown as they are
138
+ KEY_CONDITIONS = {
139
+ "vma.buf.view.autocp_active" => "when the completion popup is open",
140
+ "vma.kbd.next_command_count!=nil" => "with a count",
141
+ "vma.kbd.next_command_count==nil" => "without a count",
142
+ "vma.macro.is_recording==true" => "while recording a macro",
143
+ }
144
+
145
+ def key_condition_text(rule)
146
+ KEY_CONDITIONS[rule] || "if #{rule}"
147
+ end
148
+
149
+ # Lines of one page section from binding records, kept in the given order:
150
+ # the same keys and description in several modes become one row, rows are
151
+ # grouped by their modes ("◼◼ Command, Visual" subheadings unless
152
+ # subheads: false; ordered by their first mode), rows of a group with the
153
+ # same description are merged ("up / k"), and keys and descriptions are
154
+ # aligned in columns.
155
+ def key_bindings_section(records, subheads: true)
156
+ rank = KEY_MODE_NAMES.keys
157
+ rows = {}
158
+ records.each { |b|
159
+ conds = []
160
+ # A template expansion (C d w) is shown as its template (<op> <motion>)
161
+ keys = b[:template] ? b[:template].split(" ", 2)[1].split : b[:keys]
162
+ keys = keys.map { |k|
163
+ m = /\A(.+)\((.*)\)\z/m.match(k)
164
+ next k if m.nil?
165
+ conds << key_condition_text(m[2])
166
+ m[1]
167
+ }.join(" ")
168
+ r = (rows[[keys, b[:desc], conds]] ||= { keys: keys, desc: b[:desc], conds: conds, modes: [] })
169
+ r[:modes] << b[:mode] if !r[:modes].include?(b[:mode])
170
+ }
171
+ groups = {}
172
+ rows.each_value { |r|
173
+ ms = r[:modes].sort_by { |m| rank.index(m) || rank.size }
174
+ g = (groups[ms] ||= {})
175
+ line = (g[[r[:desc], r[:conds]]] ||= { keys: [], desc: r[:desc], conds: r[:conds] })
176
+ line[:keys] << r[:keys]
177
+ }
178
+ # by the first mode (Command groups, then Visual, Insert, ..., minor
179
+ # modes), then by which group was defined first
180
+ groups = groups.each_with_index.sort_by { |(ms, _), i| [rank.index(ms[0]) || rank.size, i] }.map(&:first).to_h
181
+ lines = groups.values.flat_map(&:values)
182
+ w = lines.map { |l| l[:keys].join(" / ").size }.select { |n| n <= 28 }.max.to_i
183
+ out = []
184
+ groups.each { |ms, g|
185
+ out << "◼◼ " + ms.map { |m| KEY_MODE_NAMES[m] || m }.join(", ") if subheads
186
+ g.each_value { |l|
187
+ desc = l[:desc].to_s
188
+ desc += " (#{l[:conds].join(", ")})" if l[:conds].any?
189
+ out << " " + l[:keys].join(" / ").ljust(w) + " " + desc
190
+ }
191
+ }
192
+ out
193
+ end
194
+
195
+ # The key bindings page as text
196
+ def key_bindings_page(kbd = vma.kbd, scheme: cnf.keybindings.scheme! || "vimlike")
197
+ all = kbd.bindings
198
+ s = +"❙Key bindings — scheme: #{scheme}❙\n\n"
199
+ s << KEY_MODE_NAMES.map { |m, n| "[#{m}]=#{n}" }.first(5).join(" ") << "\n"
200
+ s << "<key>! = press and release <key> alone, with no other key in between\n"
201
+ s << "a / b = either of the keys\n"
202
+ s << "===============================================\n"
203
+ basic = BASIC_ACTIONS.flat_map { |id|
204
+ all.select { |b| b[:action].to_s == id && %w[C V I].include?(b[:mode]) }
205
+ }
206
+ if basic.any?
207
+ s << "◼ Basics\n" << key_bindings_section(basic).join("\n") << "\n\n"
208
+ end
209
+ # The key sets of the templates go after the text objects (or before the
210
+ # uncategorized bindings, if there are no text objects)
211
+ sets = key_bindings_sets_section(kbd)
212
+ key_bindings_by_category(kbd).each { |cat, bs|
213
+ if cat.nil? && sets.any?
214
+ s << sets.join("\n") << "\n\n"
215
+ sets = []
216
+ end
217
+ s << "◼ #{key_category_heading(cat)}\n"
218
+ s << key_bindings_section(bs, subheads: !cat.to_s.start_with?("mode:")).join("\n") << "\n\n"
219
+ if cat == "textobj" && sets.any?
220
+ s << sets.join("\n") << "\n\n"
221
+ sets = []
222
+ end
223
+ }
224
+ s << sets.join("\n") << "\n\n" if sets.any?
225
+ s << "===============================================\n"
226
+ end
227
+
228
+ # The key sets used by templates (<op>, <motion>, ...): each key and what it
229
+ # means, wrapped to lines of about 80 characters
230
+ def key_bindings_sets_section(kbd)
231
+ return [] if kbd.key_sets.empty?
232
+ out = ["◼ Operators, motions and text objects",
233
+ " A line like <op> <motion> works with every key below: d w, c 3 e, g U f x"]
234
+ w = kbd.key_sets.keys.map { |n| n.size + 2 }.max
235
+ order = [:op, :motion, :textobj]
236
+ kbd.key_sets.sort_by { |name, _| order.index(name) || order.size }.each { |name, entries|
237
+ items = entries.map { |k, v|
238
+ d = key_set_value_desc(name, v.is_a?(Hash) ? v[:id] : v)
239
+ d = d.gsub("<char>", v[:char] == " " ? "space" : v[:char]) if v.is_a?(Hash) && v[:char]
240
+ "#{k} #{d}"
241
+ }
242
+ line = " " + "<#{name}>".ljust(w) + " "
243
+ indent = " " * line.size
244
+ cur = line
245
+ items.each_with_index { |it, i|
246
+ piece = (i == 0 ? "" : " · ") + it
247
+ if cur.size + piece.size > 84 && cur != line
248
+ out << cur.rstrip
249
+ cur = indent + it
250
+ else
251
+ cur += piece
252
+ end
253
+ }
254
+ out << cur
255
+ }
256
+ out << " <same> = the last key of the operator again: d d, c c, > >, g U U (<count> lines)"
257
+ out << " text objects (<char>): w W s p \" ' ` ( ) b [ ] { } B < > t"
258
+ out
259
+ end
260
+
261
+ def show_key_bindings()
262
+ b = create_new_buffer(key_bindings_page, "key-bindings")
263
+ gui_set_file_lang(b.id, "hyperplaintext")
264
+ end
265
+
266
+ # ── Vim keymap vs vimlike keymap ─────────────────────────────────────────────
267
+
268
+ # Build a separate KeyBindingTree from a keymap scheme file ("vim", "vimlike"),
269
+ # or several loaded in order (["vimlike", "notepad"]: notepad builds on vimlike)
270
+ # without changing the running editor. A scheme file binds into vma.kbd,
271
+ # registers actions in vma.actions, defines helper methods and sets the
272
+ # cnf.mode cursor colors; all of these are swapped out or restored here.
273
+ def load_scheme_kbd(scheme)
274
+ files = Array(scheme).map { |x| File.join(__dir__, "key_bindings_#{x}.rb") }
275
+ ed = vma
276
+ saved_kbd, saved_actions = ed.kbd, ed.actions
277
+ names = Vimamsa.instance_methods(false) + Vimamsa.private_instance_methods(false)
278
+ saved_methods = names.to_h { |n| [n, Vimamsa.instance_method(n)] }
279
+ color_modes = %w[command default visual replace browse insert inactive]
280
+ saved_colors = color_modes.to_h { |m| [m, cnf.mode.send(m).cursor.background!] }
281
+ acts = ActionList.new
282
+ kbd = KeyBindingTree.new(actions: acts)
283
+ kbd.error_handler = proc { |_msg| }
284
+ begin
285
+ ed.kbd = kbd
286
+ ed.instance_variable_set(:@actions, acts)
287
+ files.each { |f| load f }
288
+ ensure
289
+ ed.kbd = saved_kbd
290
+ ed.instance_variable_set(:@actions, saved_actions)
291
+ saved_methods.each { |n, um|
292
+ Vimamsa.send(:define_method, n, um) if Vimamsa.instance_method(n) != um
293
+ }
294
+ saved_colors.each { |m, v| cnf.mode.send(m).cursor.background = v }
295
+ end
296
+ kbd
297
+ end
298
+
299
+ # Compare each binding of the vim keymap with the vimlike keymap (tree
300
+ # `other`, default: a fresh vimlike tree with no module or custom.rb
301
+ # bindings). Returns rows {mode:, keys:, action:, status:, why:, other:}:
302
+ # :shared same keys, same action, bound by vimlike itself
303
+ # :added vimlike got it from the shared table (bind_vim_bindings)
304
+ # :missing vimlike has nothing there, so the binding could be added
305
+ # :conflict vimlike uses the keys differently; why is one of
306
+ # :same_keys same keys bound to another action
307
+ # :shorter a shorter vimlike binding runs first (other = its keys)
308
+ # :prefix vimlike has longer bindings starting with these keys
309
+ # :wildcard a vimlike "<char>" binding takes the key
310
+ def vim_binding_conflicts(other = nil)
311
+ other ||= load_scheme_kbd("vimlike")
312
+ vim = load_scheme_kbd("vim")
313
+ added = other.vim_added || []
314
+ added = added.to_h { |x| [x, true] }
315
+ vim.bindings.sort_by { |b| [b[:mode], b[:keys].join(" ")] }.map { |b|
316
+ mode, keys, action = b[:mode], b[:keys], b[:action]
317
+ code = kbd_action_code(action, vim.actions)
318
+ code += "(#{b[:args].values.map { |x| x.is_a?(String) ? x.inspect : x }.join(", ")})" if b[:args]
319
+ row = { mode: mode, keys: keys, action: code, template: b[:template] }
320
+ next row.merge(status: :added) if added[[mode, keys]]
321
+ if (why = VIM_KEYMAP_ONLY["#{mode} #{keys.join(" ")}"])
322
+ next row.merge(status: :vim_only, why: why)
323
+ end
324
+ row.merge(vim_binding_status(other, mode, keys, action, vim.actions))
325
+ }
326
+ end
327
+
328
+ # Rows of one status for the report: template expansions ("C <op> <motion>")
329
+ # become one row per template, with the number of expansions
330
+ def vim_conflict_rows_grouped(rows)
331
+ out = []
332
+ seen = {}
333
+ rows.each { |r|
334
+ if r[:template].nil?
335
+ out << r
336
+ next
337
+ end
338
+ if (g = seen[r[:template]])
339
+ g[:n] += 1
340
+ next
341
+ end
342
+ m, keys = r[:template].split(" ", 2)
343
+ g = seen[r[:template]] = { mode: m, keys: keys.split, action: r[:action].sub(/\(.*\)\z/, ""), n: 1 }
344
+ out << g
345
+ }
346
+ out.each { |g| g[:keys] = g[:keys] + ["(#{g[:n]} combinations)"] if g[:n] && g[:n] > 1 }
347
+ out
348
+ end
349
+
350
+ # What an action runs, as text, so bindings can be compared and listed:
351
+ # the code of a String action or the body of a one-line proc. A registered
352
+ # action whose code isn't one line is ":name"; an unnamed multi-line proc is
353
+ # its first source line. "vma.buf" counts as "buf".
354
+ def kbd_action_code(action, acts = nil)
355
+ a = action
356
+ if a.class == Symbol
357
+ reg = acts&.[](a) || vma.actions[a]
358
+ return ":#{a}" if reg.nil? || reg.method.class != Proc
359
+ return ":#{a}" if kbd_proc_code(reg.method).nil?
360
+ a = reg.method
361
+ end
362
+ code = a.class == Proc ? (kbd_proc_code(a) || kbd_proc_first_line(a)) : a.to_s
363
+ code.gsub(/\bvma\.buf\b/, "buf").gsub(/\s+/, " ").strip
364
+ end
365
+
366
+ def kbd_proc_source(pr)
367
+ file, line = pr.source_location
368
+ @kbd_src_lines ||= {}
369
+ lines = (@kbd_src_lines[file] ||= (File.readlines(file) rescue []))
370
+ lines[line - 1].to_s
371
+ end
372
+
373
+ # Body of a proc written on one line, or nil
374
+ def kbd_proc_code(pr)
375
+ m = kbd_proc_source(pr).match(/proc\s*\{\s*(.*?)\s*\}/)
376
+ m && m[1]
377
+ end
378
+
379
+ def kbd_proc_first_line(pr)
380
+ "proc: " + kbd_proc_source(pr).strip.sub(/\A"[^"]*"\s*=>\s*/, "")
381
+ end
382
+
383
+ def vim_binding_status(kbd, mode, keys, action, vim_acts = nil)
384
+ node = kbd.root.children.find { |m| m.key_name == mode }
385
+ return { status: :missing } if node.nil?
386
+ path = []
387
+ keys.each_with_index { |k, i|
388
+ m = /\A(.+)\((.*)\)\z/.match(k)
389
+ name, rule = m ? [m[1], m[2]] : [k, ""]
390
+ child = node.children.find { |c| c.key_name == name && c.eval_rule == rule }
391
+ if child.nil?
392
+ wc = node.children.find { |c| c.key_name == "<char>" }
393
+ if wc && name.size == 1
394
+ return { status: :conflict, why: :wildcard, other: [path + ["<char>"], kbd_action_code(wc.action, kbd.actions)] }
395
+ end
396
+ return { status: :missing }
397
+ end
398
+ path << k
399
+ last = i == keys.size - 1
400
+ if !last && !child.action.nil? && child.children.empty?
401
+ return { status: :conflict, why: :shorter, other: [path, kbd_action_code(child.action, kbd.actions)] }
402
+ end
403
+ node = child
404
+ }
405
+ if node.children.any?
406
+ n = 0
407
+ stack = [node]
408
+ while (s = stack.pop)
409
+ s.children.each { |c| n += 1 if !c.action.nil?; stack << c }
410
+ end
411
+ return { status: :conflict, why: :prefix, other: [keys, "#{n} longer binding#{n == 1 ? "" : "s"}"] }
412
+ end
413
+ oc = kbd_action_code(node.action, kbd.actions)
414
+ return { status: :shared } if oc == kbd_action_code(action, vim_acts)
415
+ { status: :conflict, why: :same_keys, other: [keys, oc] }
416
+ end
417
+
418
+ VIM_CONFLICT_WHY = {
419
+ same_keys: "same keys, other action",
420
+ shorter: "a shorter vimlike binding runs first",
421
+ prefix: "vimlike uses these keys as a prefix",
422
+ wildcard: "a vimlike <char> binding takes the key",
423
+ }
424
+
425
+ # Markdown report of vim_binding_conflicts, written to
426
+ # doc/vimlike_vim_conflicts.md (tests/test_vim_conflicts.rb keeps it current).
427
+ def vim_binding_conflicts_report(rows = vim_binding_conflicts)
428
+ code = ->(s) {
429
+ s = s.to_s
430
+ s = s[0, 57] + "..." if s.size > 60
431
+ s = s.gsub("&", "&amp;").gsub("<", "&lt;").gsub(">", "&gt;").gsub("|", "&#124;")
432
+ "<code>#{s}</code>"
433
+ }
434
+ act = code
435
+ by = rows.group_by { |r| r[:status] }
436
+ out = +"# Vim keymap vs vimlike keymap\n\n"
437
+ out << "Generated by `vim_binding_conflicts_report` (lib/vimamsa/key_bindings_report.rb)\n"
438
+ out << "from `key_bindings_vim.rb` and `key_bindings_vimlike.rb`. Don't edit by hand:\n"
439
+ out << "run `VMA_UPDATE_DOC=1 ruby exe/run_tests.rb --headless vim_conflicts`.\n\n"
440
+ out << "Bindings that modules and `custom.rb` add at startup are not included.\n\n"
441
+ out << "| | Count |\n|---|---|\n"
442
+ out << "| Conflicts (the vim binding can't be used in vimlike) | #{by.fetch(:conflict, []).size} |\n"
443
+ out << "| Added to vimlike from the shared table (`VIM_BINDINGS`, vim_core.rb) | #{by.fetch(:added, []).size} |\n"
444
+ out << "| Missing from vimlike (no conflict, could be added) | #{by.fetch(:missing, []).size} |\n"
445
+ out << "| vim keymap only (left out of vimlike on purpose) | #{by.fetch(:vim_only, []).size} |\n"
446
+ out << "| Shared (same keys, same action) | #{by.fetch(:shared, []).size} |\n\n"
447
+
448
+ out << "## Conflicts\n\n| Mode | Vim keys | Vim action | vimlike | Reason |\n|---|---|---|---|---|\n"
449
+ by.fetch(:conflict, []).each { |r|
450
+ ok, oa = r[:other]
451
+ vl = ok == r[:keys] ? act.(oa) : "#{code.(ok.join(" "))} #{act.(oa)}"
452
+ vl = code.(oa) if r[:why] == :prefix
453
+ out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} | #{vl} | #{VIM_CONFLICT_WHY[r[:why]]} |\n"
454
+ }
455
+ out << "\n## Added to vimlike\n\nVim bindings that vimlike loads from the shared table because nothing of\n"
456
+ out << "its own is in the way. A template (`<op> <motion>`, vim_operators.rb) is one row with\n"
457
+ out << "the number of its combinations that vimlike got.\n\n| Mode | Vim keys | Vim action |\n|---|---|---|\n"
458
+ vim_conflict_rows_grouped(by.fetch(:added, [])).each { |r|
459
+ out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} |\n"
460
+ }
461
+ out << "\n## Missing from vimlike\n\n| Mode | Vim keys | Vim action |\n|---|---|---|\n"
462
+ vim_conflict_rows_grouped(by.fetch(:missing, [])).each { |r|
463
+ out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} |\n"
464
+ }
465
+ out << "\n## vim keymap only\n\nLeft out of vimlike on purpose (`VIM_KEYMAP_ONLY`, vim_core.rb).\n\n"
466
+ out << "| Mode | Vim keys | Vim action | Why |\n|---|---|---|---|\n"
467
+ by.fetch(:vim_only, []).each { |r|
468
+ out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} | #{r[:why]} |\n"
469
+ }
470
+ out << "\n## Shared\n\n| Mode | Keys | Action |\n|---|---|---|\n"
471
+ vim_conflict_rows_grouped(by.fetch(:shared, [])).each { |r|
472
+ out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} |\n"
473
+ }
474
+ out
475
+ end
476
+
477
+ # Show the report for the running editor: compared with the live key
478
+ # bindings when the vimlike keymap is in use, so custom.rb is included.
479
+ def show_vim_binding_conflicts
480
+ live = (cnf.keybindings.scheme! || "vimlike") == "vimlike"
481
+ b = create_new_buffer(vim_binding_conflicts_report(vim_binding_conflicts(live ? vma.kbd : nil)), "vim-binding-conflicts")
482
+ gui_set_file_lang(b.id, "markdown")
483
+ end
484
+ end # module Vimamsa
@@ -0,0 +1,104 @@
1
+ # Vim keybinding scheme: standard vim behavior, as close as the current
2
+ # action inventory allows. Does not load vimlike. The Vim bindings themselves
3
+ # are the shared VIM_BINDINGS table (vim_core.rb), which vimlike also loads
4
+ # where it doesn't conflict; this file adds the modes and the vim keymap's own
5
+ # deviations. Selected via cnf.keybindings.scheme = "vim" (applied on restart).
6
+ #
7
+ # Operators, motions and text objects combine freely (d w, c 3 e, g U t x,
8
+ # > j, d i (, ...): the "vim operators" templates of VIM_BINDINGS and the key
9
+ # sets in vim_operators.rb.
10
+ # Not implemented (no core primitives) — intentionally left unbound:
11
+ # %, ?, #, ge, { }, ( ), = and gq, V (linewise) / ctrl-v (blockwise)
12
+ # visual, : ex commands, insert-mode ctrl-w; "." of a Visual mode change.
13
+ # Known deviations kept on purpose:
14
+ # C tab = switch to last buffer; C enter = line action;
15
+ # ctrl-d/ctrl-u scroll a full page, not half; ctrl-o approximates the
16
+ # jumplist with jump_to_last_edit.
17
+ # Known deviation, not fixed yet: esc in Insert mode doesn't move the cursor
18
+ # one character left.
19
+
20
+ module Vimamsa
21
+ vma.kbd.add_mode("C", :command)
22
+ vma.kbd.add_mode("I", :insert, :insert)
23
+ vma.kbd.add_mode("V", :visual, :visual)
24
+ vma.kbd.add_mode("M", :minibuffer)
25
+ vma.kbd.add_mode("R", :readchar)
26
+ vma.kbd.add_minor_mode("audio", :audio, :command)
27
+ vma.kbd.add_minor_mode("macro", :macro, :command)
28
+ vma.kbd.add_mode("B", :browse, :browse, scope: :editor)
29
+ vma.kbd.add_mode("X", :replace, :replace, name: "Replace")
30
+ vma.kbd.set_default_mode(:command)
31
+ vma.kbd.__set_mode(:command)
32
+
33
+ cnf.mode.command.cursor.background = "#05c5a0"
34
+ cnf.mode.default.cursor.background = "#03fcca"
35
+ cnf.mode.visual.cursor.background = "#bc6040"
36
+ cnf.mode.replace.cursor.background = "#fc0331"
37
+ cnf.mode.browse.cursor.background = "#f803fc"
38
+ cnf.mode.insert.cursor.background = "#ffffff"
39
+ cnf.mode.inactive.cursor.background = "#777777"
40
+
41
+ def _insert_move(op)
42
+ if op == :pagedown
43
+ vma.gui.page_down
44
+ elsif op == :pageup
45
+ vma.gui.page_up
46
+ else
47
+ buf.move(op)
48
+ end
49
+ end
50
+
51
+ def insert_select_move(op)
52
+ buf.continue_selection
53
+ _insert_move(op)
54
+ end
55
+
56
+ def insert_move(op)
57
+ buf.end_selection
58
+ _insert_move(op)
59
+ end
60
+
61
+ # Vim behavior: the shared table and its actions, see vim_core.rb
62
+ bind_vim_bindings
63
+
64
+ # ── vim keymap's own bindings (not Vim behavior) ─────────────────────────────
65
+
66
+ add_keys "vim misc cat:buffer", {
67
+ # Kept deviations (documented in the header)
68
+ "C tab" => "buf.switch.last",
69
+ # prev/next file in file panel order (no-op while the panel is hidden)
70
+ "C alt-h" => "panel.files.prev",
71
+ "C alt-l" => "panel.files.next",
72
+ "C enter || C return" => ["jump.line", proc { buf.handle_line_action() }, "Line action"],
73
+
74
+ # Browse mode keys (mode reachable only via the "mode.browse" action)
75
+ "B h" => "buf.history.prev",
76
+ "B l" => "buf.history.next",
77
+ "B p" => "panel.files.prev",
78
+ "B n" => "panel.files.next",
79
+ "B z" => "mode.browse.center",
80
+ "B enter || B return || B esc || B j || B ctrl!" => "mode.browse.exit",
81
+ "B s" => "move.page.prev",
82
+ "B d" => "move.page.next",
83
+ "B r" => "move.page.next.quarter",
84
+ "B e" => "move.page.prev.quarter",
85
+ "B i" => "move.buf.start",
86
+ "B o" => "move.buf.end",
87
+ "B c" => "buf.close",
88
+ "B ;" => "jump.edit.prev",
89
+ "B q" => "jump.edit.prev",
90
+ "B w" => "jump.edit.next",
91
+ "B m" => "macro.run.last",
92
+ }
93
+
94
+ # Audio minor mode (used by the audio module; unreachable without it)
95
+ bindkey "audio f || audio right", ["audio.forward", proc { Audio.seek_forward }, "Seek forward in audio stream"]
96
+ bindkey "audio left", ["audio.backward", proc { Audio.seek_forward(-5.0) }, "Seek backward in audio stream"]
97
+ bindkey "audio space", "audio.stop"
98
+ bindkey "audio q || audio esc", "mode.prev"
99
+
100
+ # vim i_ctrl-r: insert a register. Here, not in VIM_BINDINGS, so vimlike
101
+ # doesn't get a new ctrl chord from the shared table.
102
+ bindkey "I ctrl-r <char>", "edit.insert.register", keywords: "vim insert"
103
+
104
+ end # module Vimamsa