vimamsa 0.1.27 → 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 (156) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +139 -89
  3. data/custom_example.rb +18 -6
  4. data/doc/TODO.md +327 -110
  5. data/doc/action_names_plan.md +442 -0
  6. data/doc/bundling_plan.md +2 -1
  7. data/doc/delta_syntax_plan.md +293 -0
  8. data/doc/demo_recording_plan.md +288 -0
  9. data/doc/design_goals.md +27 -0
  10. data/doc/intro_page_plan.md +266 -0
  11. data/doc/key_bindings_report_plan.md +45 -0
  12. data/doc/line_tree_plan.md +89 -0
  13. data/doc/missing_vim_bindings.md +34 -44
  14. data/doc/modules_settings_plan.md +1 -1
  15. data/doc/registries_plan.md +202 -0
  16. data/doc/startup_speed_plan.md +97 -0
  17. data/doc/treap_visualizer.html +585 -0
  18. data/doc/vim_compatibility_plan.md +186 -31
  19. data/doc/vimlike_vim_conflicts.md +236 -228
  20. data/doc/vm_snapshots_plan.md +4 -4
  21. data/doc/wysiwyg_plan.md +231 -0
  22. data/exe/run_tests.rb +8 -1
  23. data/exe/vimamsa +49 -6
  24. data/help/intro.txt +30 -0
  25. data/help/vim_intro.txt +31 -0
  26. data/img/demo.gif +0 -0
  27. data/img/demo.mp4 +0 -0
  28. data/lang/hyperplaintext.lang +1 -1
  29. data/lib/vimamsa/ack.rb +2 -0
  30. data/lib/vimamsa/actions.rb +381 -36
  31. data/lib/vimamsa/bookmarks.rb +8 -8
  32. data/lib/vimamsa/buffer.rb +83 -37
  33. data/lib/vimamsa/buffer_changetext.rb +58 -36
  34. data/lib/vimamsa/buffer_list.rb +2 -2
  35. data/lib/vimamsa/buffer_manager.rb +6 -6
  36. data/lib/vimamsa/cli_help.rb +75 -0
  37. data/lib/vimamsa/clipboard.rb +6 -3
  38. data/lib/vimamsa/conf.rb +9 -0
  39. data/lib/vimamsa/crash_handler.rb +2 -2
  40. data/lib/vimamsa/debug.rb +2 -2
  41. data/lib/vimamsa/demo_player.rb +458 -0
  42. data/lib/vimamsa/demo_recorder.rb +277 -0
  43. data/lib/vimamsa/diff_buffer.rb +1 -1
  44. data/lib/vimamsa/dot_repeat.rb +104 -0
  45. data/lib/vimamsa/easy_jump.rb +54 -3
  46. data/lib/vimamsa/editor.rb +89 -46
  47. data/lib/vimamsa/file_history.rb +1 -1
  48. data/lib/vimamsa/file_manager.rb +23 -23
  49. data/lib/vimamsa/form_input.rb +388 -0
  50. data/lib/vimamsa/gui.rb +83 -13
  51. data/lib/vimamsa/gui_command_line.rb +44 -5
  52. data/lib/vimamsa/gui_form_generator.rb +17 -6
  53. data/lib/vimamsa/gui_key_hint.rb +165 -0
  54. data/lib/vimamsa/gui_keylog_panel.rb +164 -2
  55. data/lib/vimamsa/gui_menu.rb +69 -50
  56. data/lib/vimamsa/gui_select_window.rb +77 -43
  57. data/lib/vimamsa/gui_settings.rb +9 -5
  58. data/lib/vimamsa/gui_sourceview.rb +15 -81
  59. data/lib/vimamsa/gui_theme.rb +11 -2
  60. data/lib/vimamsa/hook.rb +2 -0
  61. data/lib/vimamsa/intro_page.rb +368 -0
  62. data/lib/vimamsa/key_actions.rb +315 -225
  63. data/lib/vimamsa/key_binding_tree.rb +228 -61
  64. data/lib/vimamsa/key_bindings_notepad.rb +18 -16
  65. data/lib/vimamsa/key_bindings_report.rb +290 -67
  66. data/lib/vimamsa/key_bindings_vim.rb +47 -297
  67. data/lib/vimamsa/key_bindings_vimlike.rb +347 -339
  68. data/lib/vimamsa/key_names.rb +80 -0
  69. data/lib/vimamsa/line_tree.rb +367 -0
  70. data/lib/vimamsa/macro.rb +20 -3
  71. data/lib/vimamsa/rbvma.rb +15 -0
  72. data/lib/vimamsa/registers.rb +125 -0
  73. data/lib/vimamsa/run_ruby.rb +64 -0
  74. data/lib/vimamsa/search_replace.rb +4 -4
  75. data/lib/vimamsa/string_util.rb +17 -3
  76. data/lib/vimamsa/tests.rb +8 -8
  77. data/lib/vimamsa/text_objects.rb +11 -54
  78. data/lib/vimamsa/text_transforms.rb +4 -0
  79. data/lib/vimamsa/tmp_user_dir.rb +39 -0
  80. data/lib/vimamsa/user_dirs.rb +83 -0
  81. data/lib/vimamsa/version.rb +1 -1
  82. data/lib/vimamsa/vim_core.rb +355 -0
  83. data/lib/vimamsa/vim_operators.rb +365 -0
  84. data/modules/calculator/calculator.rb +5 -5
  85. data/modules/color_highlight/color_highlight.rb +3 -3
  86. data/modules/dictation/dictation.rb +32 -32
  87. data/modules/ollama/ollama.rb +5 -5
  88. data/modules/terminal/terminal.rb +5 -5
  89. data/modules/tts/tts.rb +10 -10
  90. data/run_e2e_vm.sh +3 -3
  91. data/run_tests_vm.rb +138 -0
  92. data/scripts/demo/demo_easyjump_steps.txt +60 -0
  93. data/scripts/demo/demo_files_steps.txt +52 -0
  94. data/scripts/demo/demo_steps.txt +9 -1
  95. data/scripts/demo/notes.txt +6 -0
  96. data/scripts/demo/record_demo.rb +60 -81
  97. data/scripts/demo/report.rb +7 -0
  98. data/scripts/demo/shapes.rb +1 -0
  99. data/scripts/demo/square/shapes.rb +29 -0
  100. data/scripts/demo/square/steps.txt +185 -0
  101. data/scripts/lib/perf_startup.rb +138 -0
  102. data/scripts/lib/script_helpers.rb +53 -0
  103. data/scripts/perf_compare.rb +34 -0
  104. data/scripts/profile_startup.rb +72 -0
  105. data/tests/test_action_ids.rb +232 -0
  106. data/tests/test_auto_format.rb +2 -2
  107. data/tests/test_autocomplete.rb +1 -1
  108. data/tests/test_basic_editing.rb +18 -18
  109. data/tests/test_bookmarks.rb +6 -6
  110. data/tests/test_bracket_match.rb +3 -0
  111. data/tests/test_cli_help.rb +64 -0
  112. data/tests/test_color_highlight.rb +2 -2
  113. data/tests/test_command_line.rb +1 -1
  114. data/tests/test_context_menu_file.rb +18 -0
  115. data/tests/test_copy_paste.rb +33 -33
  116. data/tests/test_custom_two_phase.rb +1 -1
  117. data/tests/test_demo_player.rb +301 -0
  118. data/tests/test_demo_recorder.rb +202 -0
  119. data/tests/test_dictation.rb +16 -16
  120. data/tests/test_dot_repeat.rb +88 -0
  121. data/tests/test_easy_jump.rb +93 -0
  122. data/tests/test_extract_file_links.rb +3 -3
  123. data/tests/test_file_panel.rb +2 -2
  124. data/tests/test_flash.rb +1 -1
  125. data/tests/test_fold.rb +1 -1
  126. data/tests/test_form_input.rb +203 -0
  127. data/tests/test_func_panel.rb +2 -2
  128. data/tests/test_gtk_sync.rb +7 -7
  129. data/tests/test_intro_page.rb +437 -0
  130. data/tests/test_key_categories.rb +163 -0
  131. data/tests/test_keylog_panel.rb +163 -3
  132. data/tests/test_line_ends.rb +51 -0
  133. data/tests/test_line_tree.rb +86 -0
  134. data/tests/test_lsp_clangd.rb +2 -2
  135. data/tests/test_macro.rb +2 -2
  136. data/tests/test_module_interface.rb +9 -9
  137. data/tests/test_notepad_bindings.rb +5 -5
  138. data/tests/test_perf_startup.rb +28 -0
  139. data/tests/test_random_edit.rb +27 -27
  140. data/tests/test_registers.rb +122 -0
  141. data/tests/test_run_ruby.rb +77 -0
  142. data/tests/test_startup_file.rb +1 -1
  143. data/tests/test_text_objects.rb +1 -1
  144. data/tests/test_text_transforms.rb +12 -0
  145. data/tests/test_tmp_user_dir.rb +79 -0
  146. data/tests/test_tts.rb +10 -10
  147. data/tests/test_undo.rb +34 -34
  148. data/tests/test_user_dirs.rb +122 -0
  149. data/tests/test_vim_bindings.rb +107 -6
  150. data/tests/test_vim_conflicts.rb +41 -4
  151. data/tests/test_vim_operators.rb +116 -0
  152. data/tests/test_window_layout.rb +83 -0
  153. data/vimamsa.gemspec +1 -1
  154. data/vm_guest_run.sh +22 -2
  155. metadata +58 -3
  156. data/run_tests_vm.sh +0 -130
@@ -15,69 +15,263 @@ def show_free_key_bindings()
15
15
  gui_set_file_lang(b.id, "hyperplaintext")
16
16
  end
17
17
 
18
- def show_key_bindings()
19
- kbd_s = "❙Key bindings❙\n"
20
- kbd_s << "\n⦁[Mode] <keys> : <action>⦁\n"
21
- done = []
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
+ ]
22
44
 
23
- kbd_s << "[B]=Browse, [C]=Command, [I]=Insert, [V]=Visual\n"
24
- kbd_s << "<key>!: Press <key> once, release before pressing any other keys\n"
25
- kbd_s << "===============================================\n"
26
- kbd_s << "◼ Basic\n"
27
- kbd_s << "◼◼ Command mode\n"
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
+ }
28
58
 
29
- x = vma.kbd.get_by_keywords(modes: ["C"], keywords: ["intro"])
30
- done.concat(x.lines); kbd_s << x
59
+ # Bindings in these major modes, when nothing else decides
60
+ KEY_MODE_CATEGORY = { "B" => "buffer", "X" => "edit", "R" => "internal", "F" => "edit" }
31
61
 
32
- kbd_s << "\n"
33
- kbd_s << "◼◼ Insert mode\n"
34
- x = vma.kbd.get_by_keywords(modes: ["I"], keywords: ["intro"])
35
- done.concat(x.lines); kbd_s << x
36
- kbd_s << "\n"
37
- kbd_s << "◼◼ Visual mode\n"
38
- x = vma.kbd.get_by_keywords(modes: ["V"], keywords: ["intro"])
39
- done.concat(x.lines); kbd_s << x
40
- kbd_s << "\n"
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
+ }
41
68
 
42
- kbd_s << "◼ Hyper Plaintext\n"
43
- x = vma.kbd.get_by_keywords(modes: ["C"], keywords: ["hyperplaintext"])
44
- x2 = vma.kbd.get_by_keywords(modes: ["V"], keywords: ["hyperplaintext"])
45
- done.concat(x.lines); kbd_s << x << "\n" << x2
46
- kbd_s << "\n"
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
47
99
 
48
- kbd_s << "◼ Core\n"
49
- x = vma.kbd.get_by_keywords(modes: [], keywords: ["core"])
50
- x << vma.kbd.get_by_keywords(modes: ["X"], keywords: ["intro"])
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
51
108
 
52
- done.concat(x.lines); kbd_s << x
53
- kbd_s << "\n"
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
54
119
 
55
- kbd_s << "◼ Debug / Experimental\n"
56
- x = vma.kbd.get_by_keywords(modes: [], keywords: ["experimental"])
57
- done.concat(x.lines); kbd_s << x
58
- kbd_s << "\n"
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
+ ]
59
131
 
60
- kbd_s << "◼ Others\n"
61
- # x = vma.kbd.get_by_keywords(modes: [], keywords:["experimental"])
62
- # x = vma.kbd.to_s
63
- x = vma.kbd.get_by_keywords(modes: [], keywords: [])
64
- done << x.lines - done
65
- kbd_s << (x.lines - done).join
66
- kbd_s << "\n"
132
+ KEY_MODE_NAMES = {
133
+ "C" => "Command", "V" => "Visual", "I" => "Insert", "X" => "Replace",
134
+ "B" => "Browse", "R" => "Read char", "M" => "Minibuffer", "F" => "Form",
135
+ }
67
136
 
68
- kbd_s << "===============================================\n"
69
- b = create_new_buffer(kbd_s, "key-bindings")
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")
70
263
  gui_set_file_lang(b.id, "hyperplaintext")
71
264
  end
72
265
 
73
266
  # ── Vim keymap vs vimlike keymap ─────────────────────────────────────────────
74
267
 
75
- # Build a separate KeyBindingTree from a keymap scheme file ("vim", "vimlike")
268
+ # Build a separate KeyBindingTree from a keymap scheme file ("vim", "vimlike"),
269
+ # or several loaded in order (["vimlike", "notepad"]: notepad builds on vimlike)
76
270
  # without changing the running editor. A scheme file binds into vma.kbd,
77
271
  # registers actions in vma.actions, defines helper methods and sets the
78
272
  # cnf.mode cursor colors; all of these are swapped out or restored here.
79
273
  def load_scheme_kbd(scheme)
80
- file = File.join(__dir__, "key_bindings_#{scheme}.rb")
274
+ files = Array(scheme).map { |x| File.join(__dir__, "key_bindings_#{x}.rb") }
81
275
  ed = vma
82
276
  saved_kbd, saved_actions = ed.kbd, ed.actions
83
277
  names = Vimamsa.instance_methods(false) + Vimamsa.private_instance_methods(false)
@@ -90,7 +284,7 @@ def load_scheme_kbd(scheme)
90
284
  begin
91
285
  ed.kbd = kbd
92
286
  ed.instance_variable_set(:@actions, acts)
93
- load file
287
+ files.each { |f| load f }
94
288
  ensure
95
289
  ed.kbd = saved_kbd
96
290
  ed.instance_variable_set(:@actions, saved_actions)
@@ -102,26 +296,11 @@ def load_scheme_kbd(scheme)
102
296
  kbd
103
297
  end
104
298
 
105
- # Every binding of a tree as [mode, keys, action]; keys is an Array of key
106
- # names, with "(eval_rule)" appended where the binding has one.
107
- def kbd_leaf_bindings(kbd)
108
- out = []
109
- stack = kbd.root.children.map { |m| [m, m.key_name, []] }
110
- while (x = stack.pop)
111
- st, mode, path = x
112
- st.children.each { |c|
113
- p = path + [c.eval_rule.empty? ? c.key_name : "#{c.key_name}(#{c.eval_rule})"]
114
- out << [mode, p, c.action] if !c.action.nil?
115
- stack << [c, mode, p] if c.children.any?
116
- }
117
- end
118
- out.sort_by { |m, k, _a| [m, k.join(" ")] }
119
- end
120
-
121
299
  # Compare each binding of the vim keymap with the vimlike keymap (tree
122
300
  # `other`, default: a fresh vimlike tree with no module or custom.rb
123
301
  # bindings). Returns rows {mode:, keys:, action:, status:, why:, other:}:
124
- # :shared same keys, same action
302
+ # :shared same keys, same action, bound by vimlike itself
303
+ # :added vimlike got it from the shared table (bind_vim_bindings)
125
304
  # :missing vimlike has nothing there, so the binding could be added
126
305
  # :conflict vimlike uses the keys differently; why is one of
127
306
  # :same_keys same keys bound to another action
@@ -131,12 +310,43 @@ end
131
310
  def vim_binding_conflicts(other = nil)
132
311
  other ||= load_scheme_kbd("vimlike")
133
312
  vim = load_scheme_kbd("vim")
134
- kbd_leaf_bindings(vim).map { |mode, keys, action|
135
- row = { mode: mode, keys: keys, action: kbd_action_code(action, vim.actions) }
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
136
324
  row.merge(vim_binding_status(other, mode, keys, action, vim.actions))
137
325
  }
138
326
  end
139
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
+
140
350
  # What an action runs, as text, so bindings can be compared and listed:
141
351
  # the code of a String action or the body of a one-line proc. A registered
142
352
  # action whose code isn't one line is ":name"; an unnamed multi-line proc is
@@ -230,7 +440,9 @@ def vim_binding_conflicts_report(rows = vim_binding_conflicts)
230
440
  out << "Bindings that modules and `custom.rb` add at startup are not included.\n\n"
231
441
  out << "| | Count |\n|---|---|\n"
232
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"
233
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"
234
446
  out << "| Shared (same keys, same action) | #{by.fetch(:shared, []).size} |\n\n"
235
447
 
236
448
  out << "## Conflicts\n\n| Mode | Vim keys | Vim action | vimlike | Reason |\n|---|---|---|---|---|\n"
@@ -240,12 +452,23 @@ def vim_binding_conflicts_report(rows = vim_binding_conflicts)
240
452
  vl = code.(oa) if r[:why] == :prefix
241
453
  out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} | #{vl} | #{VIM_CONFLICT_WHY[r[:why]]} |\n"
242
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
+ }
243
461
  out << "\n## Missing from vimlike\n\n| Mode | Vim keys | Vim action |\n|---|---|---|\n"
244
- by.fetch(:missing, []).each { |r|
462
+ vim_conflict_rows_grouped(by.fetch(:missing, [])).each { |r|
245
463
  out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} |\n"
246
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
+ }
247
470
  out << "\n## Shared\n\n| Mode | Keys | Action |\n|---|---|---|\n"
248
- by.fetch(:shared, []).each { |r|
471
+ vim_conflict_rows_grouped(by.fetch(:shared, [])).each { |r|
249
472
  out << "| #{r[:mode]} | #{code.(r[:keys].join(" "))} | #{act.(r[:action])} |\n"
250
473
  }
251
474
  out