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,368 @@
1
+ module Vimamsa
2
+ # Help pages written as hyperplaintext templates in help/ (doc/intro_page_plan.md):
3
+ # intro the introduction page: shown on the first start and from
4
+ # Help ▸ Introduction; buttons that start the demos
5
+ # vim_intro a one-screen summary of how vimlike differs from Vim
6
+ #
7
+ # Placeholders in the templates, filled in when a page is opened:
8
+ # {key:ACTION} / {key:MODE:ACTION} the keys bound to the action (up to two,
9
+ # Command mode first, or only MODE), as ⦁keys⦁; the menu path if unbound
10
+ # {demos} one line per demo (DemoPlayer::DEMOS) with its button link
11
+ # {vim_key_table} the table of VIM_INTRO_KEYS
12
+ #
13
+ # Links ⟦run:ACTION|LABEL⟧ run an action (enter on the link, the context menu,
14
+ # or the button shown at the end of its line); only actions allowed by
15
+ # hpt_run_allowed? run, as any .txt file can contain such a link.
16
+
17
+ HELP_DIR = File.expand_path("../../help", __dir__)
18
+ HELP_PAGES = %w[intro vim_intro]
19
+
20
+ # Keys a Vim user hits first that do something else in vimlike: [mode, keys,
21
+ # what they do in Vim, the vimlike action]. The page shows the vimlike
22
+ # action's description; tests check the action against the live bindings.
23
+ VIM_INTRO_KEYS = [
24
+ ["C", "s", "change a character", "jump.easy"],
25
+ ["C", "C", "change to end of line", "grep.files"],
26
+ ["C", "O", "open a line above", "move.line.end"],
27
+ ["C", "X", "delete a character back", "edit.line.below"],
28
+ ["C", "space <char>", "right, then a command", "edit.insert.char"],
29
+ ["C", "tab", "jump forward", "buf.switch.last"],
30
+ ["C", "z", "prefix (zz, zt)", "mode.browse"],
31
+ ["C", "ctrl-o", "jump back", "file.open"],
32
+ ["I", "ctrl-n", "completion", "move.line.next"],
33
+ ["I", "ctrl-p", "completion", "move.line.prev"],
34
+ ["I", "ctrl-c", "leave Insert mode", "sel.copy"],
35
+ ]
36
+
37
+ # Actions that ⟦run:…⟧ links may run
38
+ HPT_RUN_ALLOWED = [/\Ahelp\./, /\Aapp\.demo\z/]
39
+
40
+ def hpt_run_allowed?(action)
41
+ HPT_RUN_ALLOWED.any? { |re| re.match?(action.to_s) }
42
+ end
43
+
44
+ # Runs the action of a ⟦run:ACTION|LABEL⟧ link (spec = "ACTION|LABEL")
45
+ def hpt_run_link(spec)
46
+ action = spec.split("|", 2)[0].strip
47
+ if !hpt_run_allowed?(action)
48
+ message("Link not run: #{action} is not allowed in ⟦run:…⟧ links")
49
+ return false
50
+ end
51
+ if !vma.actions.include?(action)
52
+ message("Link not run: no action #{action}")
53
+ return false
54
+ end
55
+ call_action(action)
56
+ true
57
+ end
58
+
59
+ # The ⟦…⟧ link around pos on its line (with the brackets), or nil
60
+ def hpt_link_at(str, pos)
61
+ ls = pos == 0 ? 0 : (str.rindex("\n", pos - 1) || -1) + 1
62
+ le = str.index("\n", pos) || str.size
63
+ line = str[ls...le]
64
+ line.to_enum(:scan, /⟦[^⟧\n]*⟧/).each {
65
+ m = Regexp.last_match
66
+ return m[0] if pos - ls >= m.begin(0) && pos - ls < m.end(0)
67
+ }
68
+ nil
69
+ end
70
+
71
+ # "⦁i⦁ or ⦁ctrl!⦁" for {key:SPEC}; SPEC is ACTION or MODE:ACTION
72
+ def help_key_text(spec, kbd = vma.kbd)
73
+ mode, action = spec.include?(":") ? spec.split(":", 2) : [nil, spec]
74
+ modes = mode ? [mode] : %w[C V I]
75
+ recs = kbd.bindings.select { |b| b[:action].to_s == action && b[:template].nil? && modes.include?(b[:mode]) }
76
+ # in mode order, bindings without conditions first, then as bound
77
+ recs = recs.each_with_index.sort_by { |b, i| [modes.index(b[:mode]), b[:keys].any? { |k| k.include?("(") } ? 1 : 0, i] }.map(&:first)
78
+ keys = recs.map { |b| b[:keys].join(" ") }.uniq.first(2)
79
+ return keys.map { |k| "⦁#{k}⦁" }.join(" or ") if keys.any?
80
+ path = vma.gui&.menu&.path_for(action) rescue nil
81
+ path ? path : "(no key for #{action})"
82
+ end
83
+
84
+ def help_demo_lines
85
+ DemoPlayer::DEMOS.map { |name, d|
86
+ "⟦run:#{d[:action]}|▶ #{d[:title]}⟧ #{d[:desc]} (#{DemoPlayer.duration_text(name)})"
87
+ }.join("\n")
88
+ end
89
+
90
+ def help_vim_key_table
91
+ rows = VIM_INTRO_KEYS.map { |mode, keys, vim, action|
92
+ k = mode == "C" ? keys : "#{KEY_MODE_NAMES[mode]} #{keys}"
93
+ [k, vim, vma.kbd.action_desc(action.to_sym).to_s]
94
+ }
95
+ w0 = ["Key", *rows.map { |r| r[0] }].map(&:size).max
96
+ w1 = ["Vim", *rows.map { |r| r[1] }].map(&:size).max
97
+ (["#{"Key".ljust(w0)} #{"Vim".ljust(w1)} Vimamsa (vimlike)"] +
98
+ rows.map { |k, v, d| "#{k.ljust(w0)} #{v.ljust(w1)} #{d}" }).map { |l| " " + l }.join("\n")
99
+ end
100
+
101
+ # The text of help page name, with the placeholders filled in
102
+ def help_page_text(name)
103
+ File.read(File.join(HELP_DIR, "#{name}.txt"))
104
+ .gsub("{demos}") { help_demo_lines }
105
+ .gsub("{vim_key_table}") { help_vim_key_table }
106
+ .gsub(/\{key:([^}]+)\}/) { help_key_text($1) }
107
+ .gsub(/⟦run:[^⟧\n]*⟧/) { "#{HPT_BUTTON_SLOT}#{$&}" } # the char a button takes
108
+ end
109
+
110
+ # Before each ⟦run:…⟧ link of a help page: the link's button takes its place
111
+ # (before, not after: GTK doesn't draw a child widget that follows invisible
112
+ # text)
113
+ HPT_BUTTON_SLOT = " "
114
+
115
+ # Opens help page name in a read-only buffer, or switches to it if open
116
+ def show_help_page(name)
117
+ b = vma.buffers.list.find { |x| x.help_page == name }
118
+ if b
119
+ vma.buffers.set_current_buffer_by_id(b.id)
120
+ return b
121
+ end
122
+ b = create_new_buffer(help_page_text(name), name)
123
+ b.help_page = name
124
+ b.read_only = true
125
+ gui_set_file_lang(b.id, "hyperplaintext")
126
+ b.set_pos(0)
127
+ help_add_run_buttons(b)
128
+ hpt_hide_marks(b)
129
+ b
130
+ end
131
+
132
+ # Style marks that a read-only buffer hides (⦁ around ⦁bold⦁ keys); the
133
+ # text between them keeps its style
134
+ HPT_HIDDEN_MARKS = /⦁/
135
+
136
+ # Hides the HPT_HIDDEN_MARKS of read-only buffer b with the invisible GTK
137
+ # tag, so the text and its offsets stay as they are. Once: a read-only
138
+ # buffer doesn't change.
139
+ def hpt_hide_marks(b)
140
+ return if b.nil? || !b.read_only || b.view.nil?
141
+ vbuf = b.view.buffer
142
+ tag = hpt_hidden_tag(vbuf)
143
+ b.to_enum(:scan, HPT_HIDDEN_MARKS).each {
144
+ i = Regexp.last_match.begin(0)
145
+ vbuf.apply_tag(tag, vbuf.get_iter_at(offset: i), vbuf.get_iter_at(offset: i + 1))
146
+ }
147
+ hpt_watch_run_buttons
148
+ end
149
+
150
+ # Moves the cursor of read-only buffer b off a hidden mark, on in the
151
+ # direction it moved (a step onto a hidden mark would look like no move)
152
+ def hpt_skip_hidden_marks(b)
153
+ return if b.nil? || !b.read_only
154
+ $hpt_mark_last_pos ||= {}
155
+ last = $hpt_mark_last_pos[b.id] || 0
156
+ pos = b.pos
157
+ if HPT_HIDDEN_MARKS.match?(b[pos].to_s)
158
+ dir = pos < last ? -1 : 1
159
+ pos += dir while pos > 0 && pos < b.size - 1 && HPT_HIDDEN_MARKS.match?(b[pos].to_s)
160
+ # at the start or end of the buffer: the other way
161
+ pos -= dir while pos > 0 && pos < b.size - 1 && HPT_HIDDEN_MARKS.match?(b[pos].to_s)
162
+ b.set_pos(pos) if pos != b.pos
163
+ end
164
+ $hpt_mark_last_pos[b.id] = b.pos
165
+ end
166
+
167
+ # A button in place of each ⟦run:ACTION|LABEL⟧ link: the link text is hidden
168
+ # (an invisible GTK tag, so the text stays and keeps its offsets) and the
169
+ # button takes the place of the HPT_BUTTON_SLOT char before it, so the GTK
170
+ # buffer keeps the same length as the Buffer. In a buffer that can be edited
171
+ # the link text shows again, instead of the button, while the cursor is on its
172
+ # line (hpt_update_run_buttons); in a read-only one (help pages) never.
173
+ def help_add_run_buttons(b)
174
+ view = b.view
175
+ return if view.nil?
176
+ vbuf = view.buffer
177
+ help_button_refresh_css if $help_button_css_provider.nil?
178
+ b.help_buttons = []
179
+ b.to_enum(:scan, /#{HPT_BUTTON_SLOT}⟦run:([^|⟧\n]+)\|([^⟧\n]+)⟧/).map { Regexp.last_match }.each { |m|
180
+ action, label = m[1], m[2]
181
+ at = m.begin(0) # the slot
182
+ vbuf.delete(vbuf.get_iter_at(offset: at), vbuf.get_iter_at(offset: at + 1))
183
+ anchor = vbuf.create_child_anchor(vbuf.get_iter_at(offset: at))
184
+ btn = Gtk::Button.new(label: label)
185
+ btn.focus_on_click = false
186
+ # its own mouse pointer: it would show the text view's text cursor
187
+ btn.set_cursor_from_name("pointer")
188
+ btn.add_css_class("help-run-button")
189
+ btn.valign = :baseline_center rescue nil # the label on the text's baseline (GTK 4.12+)
190
+ btn.signal_connect("clicked") { hpt_run_link(action) }
191
+ view.add_child_at_anchor(btn, anchor)
192
+ vbuf.apply_tag(hpt_hidden_tag(vbuf), vbuf.get_iter_at(offset: at + 1), vbuf.get_iter_at(offset: m.end(0)))
193
+ b.help_buttons << [action, btn, anchor]
194
+ }
195
+ hpt_watch_run_buttons
196
+ hpt_update_run_buttons(b)
197
+ end
198
+
199
+ # Looks of the ⟦run:…⟧ buttons (cnf.help.button_style, Settings ▸
200
+ # Appearance). Each is CSS for button.help-run-button, in its own provider
201
+ # above the theme's (PRIORITY_USER + 1), so the theme's general button rule
202
+ # (border, glow) doesn't override it. c: a cyberpunk palette color.
203
+ HELP_BUTTON_STYLES = {
204
+ # small, square-cornered, the theme's colors, no glow
205
+ "flat" => lambda { |c| <<~CSS },
206
+ button.help-run-button { padding: 0px 6px; margin: 0px; min-height: 0px; font-size: 80%;
207
+ border-radius: 1px; box-shadow: none; }
208
+ CSS
209
+ # like a link: accent text, underlined; tinted on hover
210
+ "link" => lambda { |c| <<~CSS },
211
+ button.help-run-button { padding: 0px 2px; margin: 0px; min-height: 0px;
212
+ background: none; border: none; box-shadow: none; border-radius: 2px;
213
+ color: #{c[:base]}; font-weight: bold; }
214
+ button.help-run-button label { text-decoration: underline; }
215
+ button.help-run-button:hover { background: #{cyber_rgba(c[:base], 0.18)}; color: #{c[:base_bright]}; }
216
+ CSS
217
+ # rounded pill: thin accent outline, faint accent fill
218
+ "pill" => lambda { |c| <<~CSS },
219
+ button.help-run-button { padding: 0px 10px; margin: 0px 2px; min-height: 0px; font-size: 90%;
220
+ border-radius: 999px; border: 1px solid #{cyber_rgba(c[:base], 0.8)}; box-shadow: none;
221
+ background: #{cyber_rgba(c[:base], 0.12)}; color: #{c[:base_bright]}; }
222
+ button.help-run-button:hover { background: #{cyber_rgba(c[:base], 0.35)}; color: #{c[:hover_bright]}; }
223
+ CSS
224
+ # filled with the accent color, dark bold text
225
+ "solid" => lambda { |c| <<~CSS },
226
+ button.help-run-button { padding: 0px 8px; margin: 0px 2px; min-height: 0px; font-size: 90%;
227
+ border-radius: 3px; border: none; box-shadow: none; font-weight: bold;
228
+ background: #{c[:base]}; color: #{c[:bg_mid]}; }
229
+ button.help-run-button:hover { background: #{c[:base_bright]}; color: #{c[:bg_end]}; }
230
+ CSS
231
+ # a key cap: monospace, a thicker bottom edge
232
+ "key" => lambda { |c| <<~CSS },
233
+ button.help-run-button { padding: 0px 6px; margin: 0px 2px; min-height: 0px; font-family: monospace; font-size: 90%;
234
+ border-radius: 4px; border: 1px solid #{cyber_rgba(c[:base], 0.6)}; border-bottom-width: 3px; box-shadow: none;
235
+ background: #{c[:button_bg]}; color: #{c[:window_fg]}; }
236
+ button.help-run-button:hover { border-color: #{c[:base_bright]}; color: #{c[:hover_bright]}; }
237
+ CSS
238
+ }
239
+
240
+ def help_button_css(style = cnf.help.button_style!)
241
+ c = Hash.new { |_h, k| cyber_color(k) }
242
+ (HELP_BUTTON_STYLES[style.to_s] || HELP_BUTTON_STYLES["solid"]).call(c)
243
+ end
244
+
245
+ # Installs or reloads the button CSS (at the first help page and when the
246
+ # colors or settings change: gui_refresh_colors)
247
+ def help_button_refresh_css
248
+ return if Gdk::Display.default.nil?
249
+ if $help_button_css_provider.nil?
250
+ $help_button_css_provider = Gtk::CssProvider.new
251
+ Gtk::StyleContext.add_provider_for_display(Gdk::Display.default, $help_button_css_provider,
252
+ Gtk::StyleProvider::PRIORITY_USER + 1)
253
+ end
254
+ $help_button_css_provider.load(data: help_button_css)
255
+ end
256
+
257
+ # The hidden-text tag of b's GTK buffer
258
+ def hpt_hidden_tag(vbuf)
259
+ vbuf.tag_table.lookup("hpt_hidden") || vbuf.create_tag("hpt_hidden", { "invisible" => true })
260
+ end
261
+
262
+ # [anchor offset, link end, button] of each button of b, in order: the link
263
+ # is from anchor + 1 to link end
264
+ def hpt_button_ranges(b)
265
+ vbuf = b.view.buffer
266
+ b.help_buttons.map { |action, btn, anchor|
267
+ at = vbuf.get_iter_at(anchor: anchor).offset
268
+ m = b[(at + 1)..].match(/\A⟦run:[^⟧\n]*⟧/)
269
+ [at, m ? at + 1 + m.end(0) : at + 1, btn]
270
+ }
271
+ end
272
+
273
+ # Hides the link text and shows the button, or (editable buffer, cursor on
274
+ # the line) the other way round. The GTK tag changes only when a button's
275
+ # state does (the button's visibility is its state), so a read-only page
276
+ # makes no GTK calls per key. In a read-only buffer the cursor treats a
277
+ # button as one char: hpt_skip_button. action: the key action that ran.
278
+ def hpt_update_run_buttons(b, action = nil)
279
+ return if b.nil? || b.help_buttons.nil? || b.help_buttons.empty? || b.view.nil?
280
+ vbuf = b.view.buffer
281
+ tag = hpt_hidden_tag(vbuf)
282
+ ranges = hpt_button_ranges(b)
283
+ ranges.each { |at, to, btn|
284
+ show_code = !b.read_only && b.get_line_start(b.pos) == b.get_line_start(at)
285
+ next if show_code == !btn.visible? # no change
286
+ s, e = vbuf.get_iter_at(offset: at + 1), vbuf.get_iter_at(offset: to)
287
+ if show_code
288
+ vbuf.remove_tag(tag, s, e)
289
+ btn.visible = false
290
+ else
291
+ vbuf.apply_tag(tag, s, e)
292
+ btn.visible = true
293
+ end
294
+ }
295
+ hpt_skip_button(b, ranges, action) if b.read_only
296
+ end
297
+
298
+ # A button counts as one char for the cursor: a move that ends in its hidden
299
+ # link text goes on to the button (from before it, or moving back or to
300
+ # another line) or past the link (from the button itself, e.g. w or l on
301
+ # it; after a word motion such as w, on to the next word)
302
+ def hpt_skip_button(b, ranges = hpt_button_ranges(b), action = nil)
303
+ $hpt_button_last_pos ||= {}
304
+ last = $hpt_button_last_pos[b.id] || b.pos
305
+ r = ranges.find { |at, to, _| b.pos > at && b.pos < to }
306
+ if r
307
+ at, to = r
308
+ other_line = b.get_line_start(last) != b.get_line_start(b.pos)
309
+ forward_from_button = !other_line && b.pos > last && last >= at
310
+ if forward_from_button
311
+ pos = [to, b.size - 1].min
312
+ pos = (b.index(/\S/, pos) || pos) if action.to_s.include?("word")
313
+ b.set_pos(pos)
314
+ else
315
+ b.set_pos(at)
316
+ end
317
+ end
318
+ $hpt_button_last_pos[b.id] = b.pos
319
+ end
320
+
321
+ # Updates the buttons of the current buffer after each key action and buffer
322
+ # switch (registered once)
323
+ def hpt_watch_run_buttons
324
+ return if $hpt_run_buttons_watched
325
+ $hpt_run_buttons_watched = true
326
+ vma.kbd.on(:action_handled) { |_trail, action| hpt_update_run_buttons(vma.buf, action); hpt_skip_hidden_marks(vma.buf) }
327
+ vma.hook.register(:change_buffer, method(:hpt_update_run_buttons))
328
+ end
329
+
330
+ # Show the introduction page at start? argv: the command line arguments
331
+ # (exe/vimamsa has removed --intro and --user-dir)
332
+ def intro_on_startup?(argv: ARGV, forced: $vma_show_intro, test_mode: vma_test_mode?, session: nil)
333
+ return true if forced
334
+ return false if test_mode || !cnf.intro.show_on_startup?
335
+ # a startup file that is opened (custom_example.rb sets one that usually
336
+ # doesn't exist)
337
+ return false if cnf.startup_file? && File.exist?(File.expand_path(cnf.startup_file!))
338
+ return false if argv.any? { |a| File.exist?(File.expand_path(a)) }
339
+ return false if (session || session_files_to_restore).any?
340
+ true
341
+ end
342
+
343
+ # Files of the last session that would be offered for restoring
344
+ def session_files_to_restore
345
+ path = get_data_path("session.txt")
346
+ return [] if !File.exist?(path)
347
+ File.read(path).lines.map(&:strip).reject(&:empty?).select { |f| File.exist?(f) }
348
+ end
349
+
350
+ # Called at the end of Editor#start. After the page has been shown at start
351
+ # once, it isn't shown at start again (cnf.intro.show_on_startup = false,
352
+ # saved in settings.rb), unless --intro is given.
353
+ def show_intro_on_startup
354
+ return if !intro_on_startup?
355
+ show_help_page("intro")
356
+ if !$vma_show_intro && cnf.intro.show_on_startup?
357
+ cnf.intro.show_on_startup = false
358
+ save_settings_to_file(quiet: true)
359
+ end
360
+ end
361
+
362
+ def toggle_intro_on_startup
363
+ cnf.intro.show_on_startup = !cnf.intro.show_on_startup?
364
+ save_settings_to_file(quiet: true)
365
+ message(cnf.intro.show_on_startup? ? "The introduction page will be shown at the next start" :
366
+ "The introduction page won't be shown at start (Help ▸ Introduction)")
367
+ end
368
+ end # module Vimamsa