vimamsa 0.1.27 → 0.1.29

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