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
@@ -0,0 +1,80 @@
1
+ module Vimamsa
2
+ # The editor's names for GTK key events ("j", "ctrl-r", "shift-tab", "esc",
3
+ # "ctrl" for the key alone), used by the editor window (VSourceView#
4
+ # handle_key_event) and by forms (form_input.rb), so both feed the key
5
+ # bindings the same names.
6
+
7
+ GUI_KEYVAL_NAMES = {
8
+ Gdk::Keyval::KEY_Control_L => "ctrl",
9
+ Gdk::Keyval::KEY_Control_R => "ctrl",
10
+ Gdk::Keyval::KEY_Escape => "esc",
11
+ Gdk::Keyval::KEY_Return => "enter",
12
+ Gdk::Keyval::KEY_ISO_Enter => "enter",
13
+ Gdk::Keyval::KEY_KP_Enter => "enter",
14
+ Gdk::Keyval::KEY_Alt_L => "alt",
15
+ Gdk::Keyval::KEY_Alt_R => "alt",
16
+ Gdk::Keyval::KEY_Caps_Lock => "caps",
17
+ Gdk::Keyval::KEY_BackSpace => "backspace",
18
+ Gdk::Keyval::KEY_KP_Page_Down => "pagedown",
19
+ Gdk::Keyval::KEY_KP_Page_Up => "pageup",
20
+ Gdk::Keyval::KEY_Page_Down => "pagedown",
21
+ Gdk::Keyval::KEY_Page_Up => "pageup",
22
+ Gdk::Keyval::KEY_Left => "left",
23
+ Gdk::Keyval::KEY_Right => "right",
24
+ Gdk::Keyval::KEY_Down => "down",
25
+ Gdk::Keyval::KEY_Up => "up",
26
+ Gdk::Keyval::KEY_space => "space",
27
+ Gdk::Keyval::KEY_Shift_L => "shift",
28
+ Gdk::Keyval::KEY_Shift_R => "shift",
29
+ Gdk::Keyval::KEY_Tab => "tab",
30
+ Gdk::Keyval::KEY_ISO_Left_Tab => "tab",
31
+ Gdk::Keyval::KEY_Home => "home",
32
+ Gdk::Keyval::KEY_KP_Home => "home",
33
+ Gdk::Keyval::KEY_End => "end",
34
+ Gdk::Keyval::KEY_KP_End => "end",
35
+ Gdk::Keyval::KEY_Delete => "delete",
36
+ Gdk::Keyval::KEY_KP_Delete => "delete",
37
+ }
38
+
39
+ # The key name for a key event: keyval (Gdk keyval), keystr (its character,
40
+ # Gdk::Keyval.to_unicode), modifiers (vma.kbd.modifiers: :ctrl, :alt, :shift,
41
+ # :meta, :super). "" for a key with no name.
42
+ def gui_key_name(keyval, keystr, modifiers = vma.kbd.modifiers)
43
+ keyname = GUI_KEYVAL_NAMES[keyval] || keystr
44
+ parts = []
45
+ parts << "ctrl" if modifiers[:ctrl]
46
+ parts << "alt" if modifiers[:alt]
47
+ parts << "shift" if modifiers[:shift]
48
+ parts << "meta" if modifiers[:meta]
49
+ parts << "super" if modifiers[:super]
50
+ parts << keyname
51
+
52
+ # After remapping capslock to control in gnome-tweak tool, pressing and
53
+ # immediately releasing the capslock (control) key gives first "caps" on
54
+ # keydown and ctrl-"caps" on keyup (keyval 65509, KEY_Caps_Lock). Mapping
55
+ # capslock to ctrl in hardware gives keyval 65507 (KEY_Control_L) instead.
56
+ parts.delete_at(1) if parts[0] == "ctrl" && parts[1] == "caps"
57
+
58
+ # No "ctrl-ctrl" or "alt-alt"
59
+ parts.delete_at(0) if parts[0] == parts[1]
60
+
61
+ # "shift-P" is just "P", but shift-tab stays
62
+ parts.delete_at(0) if parts[0] == "shift" && parts.size == 2 && parts[1].size == 1
63
+
64
+ key_str = parts.join("-")
65
+ key_str == "\u0000" ? "" : key_str
66
+ end
67
+
68
+ # vma.kbd.modifiers from a key controller's "modifiers" signal (modtype:
69
+ # Gdk::ModifierType), for the editor window and forms
70
+ def gui_set_modifiers(modtype)
71
+ m = vma.kbd.modifiers
72
+ m[:ctrl] = modtype.control_mask?
73
+ m[:alt] = modtype.alt_mask?
74
+ m[:hyper] = modtype.hyper_mask?
75
+ m[:lock] = modtype.lock_mask?
76
+ m[:meta] = modtype.meta_mask?
77
+ m[:shift] = modtype.shift_mask?
78
+ m[:super] = modtype.super_mask?
79
+ end
80
+ end # module Vimamsa
@@ -0,0 +1,367 @@
1
+ module Vimamsa
2
+
3
+ # The line ends of a buffer, as an implicit treap of lines (see
4
+ # doc/line_tree_plan.md).
5
+ #
6
+ # One node per line, in document order: `len` is the line's length in
7
+ # characters with its "\n", `prio` a random priority (a parent's is higher than
8
+ # its children's), and `cnt` / `sum` the number of lines and characters in the
9
+ # subtree. Line numbers are not stored: a node's line number is the count of
10
+ # lines before it. The last node holds the text after the last "\n" (usually
11
+ # empty), so every position belongs to a node.
12
+ #
13
+ # Answers the Array methods Buffer code uses on @line_ends (size, [i],
14
+ # bsearch_index, bsearch) with the newline positions, so callers don't change.
15
+ # Lookups and edits are O(log n) expected; the old Array needed O(n) per edit.
16
+ class LineTree
17
+ class Node
18
+ attr_accessor :len, :prio, :left, :right, :cnt, :sum
19
+
20
+ def initialize(len)
21
+ @len = len
22
+ @prio = rand
23
+ @left = nil
24
+ @right = nil
25
+ @cnt = 1
26
+ @sum = len
27
+ end
28
+ end
29
+
30
+ def self.from_string(str)
31
+ lens = []
32
+ str.each_line { |l| lens << l.size }
33
+ lens << 0 if lens.empty? || str.end_with?("\n") # the (empty) text after the last "\n"
34
+ new(lens)
35
+ end
36
+
37
+ # lens: the length of each line, "\n" included; the last entry is the text
38
+ # after the last "\n".
39
+ def initialize(lens = [0])
40
+ @root = build(lens)
41
+ end
42
+
43
+ # ---------------------------------------------------------------- Array interface
44
+
45
+ # Number of "\n" characters (= the size of the old @line_ends Array).
46
+ def size
47
+ @root.cnt - 1
48
+ end
49
+
50
+ def length = size
51
+ def empty? = size == 0
52
+
53
+ # Position of the i-th "\n" (negative i counts from the end, as in Array).
54
+ def [](i)
55
+ i += size if i < 0
56
+ return nil if i < 0 || i >= size
57
+ line_start(i) + line_len(i) - 1
58
+ end
59
+
60
+ def first = self[0]
61
+ def last = self[-1]
62
+
63
+ # Array#bsearch_index in find-minimum mode over the "\n" positions: the index
64
+ # of the first position for which the block is true, or nil.
65
+ def bsearch_index
66
+ n = @root
67
+ base = 0 # characters before n's subtree
68
+ idx = 0 # lines before n's subtree
69
+ found = nil
70
+ last_line = size
71
+ while n
72
+ lc = cnt(n.left)
73
+ line_i = idx + lc
74
+ if line_i == last_line
75
+ # The final node has no "\n": it's after every newline, so look left.
76
+ n = n.left
77
+ next
78
+ end
79
+ nl_pos = base + sum(n.left) + n.len - 1
80
+ if yield(nl_pos)
81
+ found = line_i
82
+ n = n.left
83
+ else
84
+ base += sum(n.left) + n.len
85
+ idx = line_i + 1
86
+ n = n.right
87
+ end
88
+ end
89
+ found
90
+ end
91
+
92
+ def bsearch(&blk)
93
+ i = bsearch_index(&blk)
94
+ i.nil? ? nil : self[i]
95
+ end
96
+
97
+ def to_a
98
+ out = []
99
+ pos = -1
100
+ each_len { |len| pos += len; out << pos }
101
+ out.pop # the final node has no "\n"
102
+ out
103
+ end
104
+
105
+ def ==(other)
106
+ other.respond_to?(:to_a) && to_a == other.to_a
107
+ end
108
+
109
+ def dup
110
+ LineTree.new(lens)
111
+ end
112
+
113
+ def clone(freeze: nil) = dup
114
+
115
+ def inspect
116
+ "#<LineTree #{size} newlines, #{total} chars>"
117
+ end
118
+
119
+ # ---------------------------------------------------------------- line queries
120
+
121
+ def total
122
+ @root.sum
123
+ end
124
+
125
+ def line_count
126
+ @root.cnt
127
+ end
128
+
129
+ # Character position where line i starts (0 <= i < line_count).
130
+ def line_start(i)
131
+ pos = 0
132
+ n = @root
133
+ while n
134
+ lc = cnt(n.left)
135
+ if i < lc
136
+ n = n.left
137
+ elsif i == lc
138
+ return pos + sum(n.left)
139
+ else
140
+ pos += sum(n.left) + n.len
141
+ i -= lc + 1
142
+ n = n.right
143
+ end
144
+ end
145
+ raise IndexError, "line #{i} out of range"
146
+ end
147
+
148
+ # Length of line i, "\n" included.
149
+ def line_len(i)
150
+ node_at(i).len
151
+ end
152
+
153
+ # [line, column] of character position p. A position at or past the end
154
+ # belongs to the last line.
155
+ def line_at(p)
156
+ if p >= total
157
+ last = line_count - 1
158
+ return [last, p - line_start(last)]
159
+ end
160
+ p = 0 if p < 0
161
+ line = 0
162
+ n = @root
163
+ while n
164
+ ls = sum(n.left)
165
+ if p < ls
166
+ n = n.left
167
+ elsif p < ls + n.len
168
+ return [line + cnt(n.left), p - ls]
169
+ else
170
+ p -= ls + n.len
171
+ line += cnt(n.left) + 1
172
+ n = n.right
173
+ end
174
+ end
175
+ raise IndexError, "position out of range"
176
+ end
177
+
178
+ def lens
179
+ out = []
180
+ each_len { |len| out << len }
181
+ out
182
+ end
183
+
184
+ # ---------------------------------------------------------------- edits
185
+
186
+ # `text` was inserted at character position pos.
187
+ def insert(pos, text)
188
+ return if text.nil? || text.empty?
189
+ j, c = line_at(pos)
190
+ if !text.include?("\n")
191
+ add_len(j, text.size)
192
+ return
193
+ end
194
+ pieces = text.split("\n", -1) # k newlines -> k + 1 pieces
195
+ old = line_len(j)
196
+ set_len(j, c + pieces[0].size + 1)
197
+ new_lens = pieces[1...-1].map { |s| s.size + 1 }
198
+ new_lens << pieces[-1].size + (old - c) # the rest of line j
199
+ insert_lines(j + 1, new_lens)
200
+ end
201
+
202
+ # `text` (the removed text) was deleted at character position pos.
203
+ def delete(pos, text)
204
+ return if text.nil? || text.empty?
205
+ n = text.size
206
+ m = text.count("\n")
207
+ j, c = line_at(pos)
208
+ if m == 0
209
+ add_len(j, -n)
210
+ return
211
+ end
212
+ je = j + m
213
+ ce = pos + n - line_start(je) # where the deletion ends, as a column of line je
214
+ set_len(j, c + (line_len(je) - ce))
215
+ delete_lines(j + 1, m)
216
+ end
217
+
218
+ # Raises if heap order or a subtree count is wrong (tests, debugging).
219
+ def check!
220
+ walk = lambda do |n|
221
+ return [0, 0] if n.nil?
222
+ [n.left, n.right].each do |ch|
223
+ raise "heap order broken" if ch && ch.prio > n.prio
224
+ end
225
+ lc, ls = walk.call(n.left)
226
+ rc, rs = walk.call(n.right)
227
+ raise "cnt wrong" if n.cnt != 1 + lc + rc
228
+ raise "sum wrong" if n.sum != n.len + ls + rs
229
+ [n.cnt, n.sum]
230
+ end
231
+ walk.call(@root)
232
+ true
233
+ end
234
+
235
+ private
236
+
237
+ def cnt(n) = n ? n.cnt : 0
238
+ def sum(n) = n ? n.sum : 0
239
+
240
+ def update(n)
241
+ n.cnt = 1 + cnt(n.left) + cnt(n.right)
242
+ n.sum = n.len + sum(n.left) + sum(n.right)
243
+ end
244
+
245
+ # In-order lengths, without recursion.
246
+ def each_len
247
+ stack = []
248
+ n = @root
249
+ while n || !stack.empty?
250
+ while n
251
+ stack << n
252
+ n = n.left
253
+ end
254
+ n = stack.pop
255
+ yield n.len
256
+ n = n.right
257
+ end
258
+ end
259
+
260
+ def node_at(i)
261
+ n = @root
262
+ while n
263
+ lc = cnt(n.left)
264
+ if i < lc
265
+ n = n.left
266
+ elsif i == lc
267
+ return n
268
+ else
269
+ i -= lc + 1
270
+ n = n.right
271
+ end
272
+ end
273
+ raise IndexError, "line out of range"
274
+ end
275
+
276
+ # Path from the root to line i, root first.
277
+ def path_to(i)
278
+ path = []
279
+ n = @root
280
+ while n
281
+ path << n
282
+ lc = cnt(n.left)
283
+ if i < lc
284
+ n = n.left
285
+ elsif i == lc
286
+ return path
287
+ else
288
+ i -= lc + 1
289
+ n = n.right
290
+ end
291
+ end
292
+ raise IndexError, "line out of range"
293
+ end
294
+
295
+ def add_len(i, d)
296
+ path = path_to(i)
297
+ path.last.len += d
298
+ path.each { |n| n.sum += d } # only sums change; counts stay
299
+ end
300
+
301
+ def set_len(i, len)
302
+ add_len(i, len - line_len(i))
303
+ end
304
+
305
+ def insert_lines(i, lens)
306
+ l, r = split(@root, i)
307
+ @root = merge(merge(l, build(lens)), r)
308
+ end
309
+
310
+ def delete_lines(i, k)
311
+ l, rest = split(@root, i)
312
+ _mid, r = split(rest, k)
313
+ @root = merge(l, r)
314
+ end
315
+
316
+ # [lines 0...i, lines i..]
317
+ def split(n, i)
318
+ return [nil, nil] if n.nil?
319
+ if i <= cnt(n.left)
320
+ l, r = split(n.left, i)
321
+ n.left = r
322
+ update(n)
323
+ [l, n]
324
+ else
325
+ l, r = split(n.right, i - cnt(n.left) - 1)
326
+ n.right = l
327
+ update(n)
328
+ [n, r]
329
+ end
330
+ end
331
+
332
+ # Every line of a comes before every line of b.
333
+ def merge(a, b)
334
+ return b if a.nil?
335
+ return a if b.nil?
336
+
337
+ if a.prio > b.prio
338
+ a.right = merge(a.right, b)
339
+ update(a)
340
+ a
341
+ else
342
+ b.left = merge(a, b.left)
343
+ update(b)
344
+ b
345
+ end
346
+ end
347
+
348
+ # O(k) build from lengths in document order (a Cartesian tree on the
349
+ # priorities, using a stack of the right spine).
350
+ def build(lens)
351
+ stack = []
352
+ lens.each do |len|
353
+ n = Node.new(len)
354
+ last = nil
355
+ while !stack.empty? && stack[-1].prio < n.prio
356
+ last = stack.pop
357
+ update(last)
358
+ end
359
+ n.left = last
360
+ stack[-1].right = n if !stack.empty?
361
+ stack << n
362
+ end
363
+ stack.reverse_each { |s| update(s) }
364
+ stack[0]
365
+ end
366
+ end
367
+ end # module Vimamsa
data/lib/vimamsa/macro.rb CHANGED
@@ -39,6 +39,10 @@ class Macro
39
39
 
40
40
  @recorded_macros = vma.marshal_load("macros", {})
41
41
  @named_macros = load_named_macros
42
+ # Old action ids (ACTION_ALIASES) => new ids; the recorded ones are saved
43
+ # at shutdown, the named ones now
44
+ convert_action_aliases(@recorded_macros)
45
+ save_named_macros if convert_action_aliases(@named_macros)
42
46
  vma.hook.register(:shutdown, self.method("save"))
43
47
  end
44
48
 
@@ -70,6 +74,21 @@ class Macro
70
74
  {}
71
75
  end
72
76
 
77
+ # Replace old action ids in macros ({name => [action, ...]}) with their new
78
+ # dotted ids. Returns true if anything changed.
79
+ def convert_action_aliases(macros)
80
+ changed = false
81
+ macros.each_value { |acts|
82
+ next if acts.class != Array
83
+ acts.map! { |x|
84
+ n = (x.class == Symbol || x.class == String) ? ACTION_ALIASES[x.to_sym] : nil
85
+ changed = true if n
86
+ n || x
87
+ }
88
+ }
89
+ changed
90
+ end
91
+
73
92
  def save()
74
93
  vma.marshal_save("macros", @recorded_macros)
75
94
  # named_macros are kept current via save_named_macros; save a Marshal copy as backup
@@ -216,9 +235,7 @@ class Macro
216
235
  m = @recorded_macros[name]
217
236
  return if !(m.kind_of?(Array) and m.any?)
218
237
  contents = m.join(";")
219
- dot_dir = File.expand_path("~/.config/.vimamsa")
220
- Dir.mkdir(dot_dir) unless File.exist?(dot_dir)
221
- save_fn = "#{dot_dir}/macro_#{name}.rb"
238
+ save_fn = get_dot_path("macro_#{name}.rb") # with the named macros
222
239
 
223
240
  Thread.new {
224
241
  File.open(save_fn, "w+") do |io|
data/lib/vimamsa/rbvma.rb CHANGED
@@ -27,8 +27,18 @@ require "uri"
27
27
  require "vimamsa/conf"
28
28
  require "vimamsa/util"
29
29
  require "vimamsa/string_util"
30
+ require "vimamsa/line_tree"
30
31
  # Clipboard must load before main: main.rb runs Editor.new at load time,
31
32
  # and Editor#initialize creates a Clipboard.
33
+ require "vimamsa/registers"
34
+ require "vimamsa/dot_repeat"
35
+ require "vimamsa/demo_player"
36
+ require "vimamsa/demo_recorder"
37
+ require "vimamsa/run_ruby"
38
+ require "vimamsa/intro_page"
39
+ require "vimamsa/cli_help"
40
+ require_relative "tmp_user_dir" # also loaded by exe/vimamsa, before the rest
41
+ require_relative "user_dirs" # also loaded by exe/vimamsa, before the rest
32
42
  require "vimamsa/clipboard"
33
43
  # exit!
34
44
  require "vimamsa/main"
@@ -44,6 +54,7 @@ require "vimamsa/appimage_env"
44
54
  require "vimamsa/bundled_fonts"
45
55
  Vimamsa::BundledFonts.setup! # install bundled Oxanium before the GUI renders
46
56
  require "vimamsa/gui"
57
+ require "vimamsa/form_input"
47
58
  require "vimamsa/gui_form_generator"
48
59
  require "vimamsa/gui_text"
49
60
  require "vimamsa/gui_menu"
@@ -53,8 +64,10 @@ require "vimamsa/gui_settings"
53
64
  require "vimamsa/gui_theme"
54
65
  require "vimamsa/gui_file_panel"
55
66
  require "vimamsa/gui_func_panel"
67
+ require "vimamsa/gui_key_hint"
56
68
  require "vimamsa/gui_keylog_panel"
57
69
  require "vimamsa/gui_select_window"
70
+ require "vimamsa/key_names"
58
71
  require "vimamsa/gui_sourceview"
59
72
  require "vimamsa/gui_sourceview_autocomplete"
60
73
  require "vimamsa/gui_image"
@@ -65,6 +78,8 @@ require "vimamsa/buffer"
65
78
  require "vimamsa/buffer_cursor"
66
79
  require "vimamsa/buffer_changetext"
67
80
  require "vimamsa/text_objects"
81
+ require "vimamsa/vim_operators"
82
+ require "vimamsa/vim_core"
68
83
  require "vimamsa/fold"
69
84
  # require "vimamsa/buffer_list"
70
85
  require "vimamsa/buffer_list"
@@ -0,0 +1,125 @@
1
+ module Vimamsa
2
+ # Vim registers (doc/vim_compatibility_plan.md, step 4). Each register holds
3
+ # text and whether it was copied as whole lines, so a put after yy or dd adds
4
+ # lines below or above, wherever the text was copied.
5
+ #
6
+ # "" unnamed: the last copy or delete (also the system clipboard)
7
+ # "0 the last copy (yank)
8
+ # "1 .. "9 the last deletes of whole lines or of text spanning lines,
9
+ # newest in "1 (each delete shifts them down)
10
+ # "- the last smaller delete
11
+ # "a .. "z named; "A .. "Z append to them
12
+ # "+ "* the system clipboard
13
+ # "_ black hole: deleting into it keeps all registers as they are
14
+ #
15
+ # Select one for the next command with "x (Command and Visual mode): "ayw,
16
+ # "ap, "_dd. Insert mode ctrl-r x (vim keymap) inserts a register.
17
+ class Registers
18
+ Entry = Struct.new(:text, :linewise)
19
+
20
+ NAME_RE = /\A[a-zA-Z0-9"\-+*_]\z/
21
+
22
+ # The register selected with "x for the next command, or nil
23
+ attr_reader :pending
24
+
25
+ def initialize
26
+ @regs = {}
27
+ @pending = nil
28
+ end
29
+
30
+ def valid?(name)
31
+ name.to_s.match?(NAME_RE)
32
+ end
33
+
34
+ # "x: use register name for the next copy, delete or put
35
+ def select(name)
36
+ name = name.to_s
37
+ if !valid?(name)
38
+ message("No register #{name.inspect}")
39
+ return false
40
+ end
41
+ @pending = name
42
+ true
43
+ end
44
+
45
+ # The pending register, which a command takes: it applies only once
46
+ def take_pending
47
+ r = @pending
48
+ @pending = nil
49
+ r
50
+ end
51
+
52
+ def clear_pending
53
+ @pending = nil
54
+ end
55
+
56
+ # The entry of a register, or nil. "+ and "* are the system clipboard as
57
+ # far as the editor knows it: the unnamed register (reading the real one
58
+ # is asynchronous, see Buffer#paste).
59
+ def [](name)
60
+ name = name.to_s
61
+ name = '"' if name == "+" || name == "*"
62
+ @regs[name.match?(/\A[A-Z]\z/) ? name.downcase : name]
63
+ end
64
+
65
+ # Store copied (kind :yank) or deleted (kind :delete) text, in the register
66
+ # reg, else the pending one, else following vim's rules for the unnamed
67
+ # register. Returns false for the black hole register (nothing stored).
68
+ def store(text, linewise: false, kind: :yank, reg: nil)
69
+ reg ||= take_pending
70
+ return false if reg == "_"
71
+ e = Entry.new(text, linewise)
72
+ case reg
73
+ when nil, '"', "+", "*"
74
+ if kind == :yank
75
+ @regs["0"] = e
76
+ elsif linewise || text.include?("\n")
77
+ 8.downto(1) { |i| @regs[(i + 1).to_s] = @regs[i.to_s] if @regs[i.to_s] }
78
+ @regs["1"] = e
79
+ else
80
+ @regs["-"] = e
81
+ end
82
+ when /\A[A-Z]\z/
83
+ old = @regs[reg.downcase]
84
+ e = append(old, e) if old
85
+ @regs[reg.downcase] = e
86
+ else
87
+ @regs[reg] = e
88
+ end
89
+ @regs['"'] = e
90
+ true
91
+ end
92
+
93
+ # Names and entries, in the order vim's :registers lists them
94
+ def to_h
95
+ order = ['"', "0", *("1".."9"), "-", *("a".."z")]
96
+ order.filter_map { |n| [n, @regs[n]] if @regs[n] }.to_h
97
+ end
98
+
99
+ private
100
+
101
+ # "A: linewise if either part is; the old text then ends with a newline
102
+ def append(old, e)
103
+ if old.linewise || e.linewise
104
+ t = old.text.end_with?("\n") ? old.text : old.text + "\n"
105
+ t += e.text
106
+ t += "\n" if !t.end_with?("\n")
107
+ Entry.new(t, true)
108
+ else
109
+ Entry.new(old.text + e.text, false)
110
+ end
111
+ end
112
+ end
113
+
114
+ # A buffer listing the registers (vim :registers)
115
+ def show_registers
116
+ lines = vma.registers.to_h.map { |n, e|
117
+ t = e.text.gsub("\n", "^J")
118
+ t = t[0, 70] + "…" if t.size > 70
119
+ "\"#{n} #{e.linewise ? "l" : "c"} #{t}"
120
+ }
121
+ s = "Registers (l = whole lines, c = characters)\n\n"
122
+ s += lines.empty? ? "(all empty)\n" : lines.join("\n") + "\n"
123
+ create_new_buffer(s, "registers")
124
+ end
125
+ end # module Vimamsa