vimamsa 0.1.26 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/appimage.yml +93 -0
  3. data/.gitignore +4 -1
  4. data/Dockerfile +7 -4
  5. data/README.md +195 -99
  6. data/custom_example.rb +59 -17
  7. data/doc/TODO.md +678 -0
  8. data/doc/action_names_plan.md +442 -0
  9. data/doc/bundling_plan.md +570 -0
  10. data/doc/delta_syntax_plan.md +293 -0
  11. data/doc/demo_recording_plan.md +288 -0
  12. data/doc/design_goals.md +27 -0
  13. data/doc/intro_page_plan.md +266 -0
  14. data/doc/key_bindings_report_plan.md +213 -0
  15. data/doc/line_tree_plan.md +89 -0
  16. data/doc/missing_vim_bindings.md +61 -0
  17. data/doc/modules_settings_plan.md +433 -0
  18. data/doc/refactor_dedup_plan.md +117 -0
  19. data/doc/registries_plan.md +202 -0
  20. data/doc/startup_speed_plan.md +97 -0
  21. data/doc/treap_visualizer.html +585 -0
  22. data/doc/unused_code_plan.md +120 -0
  23. data/doc/vim_compatibility_plan.md +451 -0
  24. data/doc/vimlike_vim_conflicts.md +262 -0
  25. data/doc/vm_snapshots_plan.md +146 -0
  26. data/doc/wysiwyg_plan.md +231 -0
  27. data/exe/run_tests.rb +124 -3
  28. data/exe/vimamsa +68 -5
  29. data/fonts/OFL-Oxanium.txt +93 -0
  30. data/fonts/Oxanium[wght].ttf +0 -0
  31. data/help/intro.txt +30 -0
  32. data/help/vim_intro.txt +31 -0
  33. data/icon.png +0 -0
  34. data/img/demo.gif +0 -0
  35. data/img/demo.mp4 +0 -0
  36. data/lang/hyperplaintext.lang +7 -1
  37. data/lib/vimamsa/ack.rb +13 -1
  38. data/lib/vimamsa/actions.rb +385 -37
  39. data/lib/vimamsa/appimage_env.rb +35 -0
  40. data/lib/vimamsa/audio.rb +2 -5
  41. data/lib/vimamsa/bookmarks.rb +302 -0
  42. data/lib/vimamsa/buffer.rb +675 -118
  43. data/lib/vimamsa/buffer_changetext.rb +150 -68
  44. data/lib/vimamsa/buffer_cursor.rb +7 -0
  45. data/lib/vimamsa/buffer_list.rb +6 -2
  46. data/lib/vimamsa/buffer_manager.rb +10 -7
  47. data/lib/vimamsa/bundled_fonts.rb +32 -0
  48. data/lib/vimamsa/cli_help.rb +75 -0
  49. data/lib/vimamsa/clipboard.rb +9 -3
  50. data/lib/vimamsa/conf.rb +68 -3
  51. data/lib/vimamsa/constants.rb +3 -0
  52. data/lib/vimamsa/crash_handler.rb +149 -0
  53. data/lib/vimamsa/debug.rb +6 -3
  54. data/lib/vimamsa/demo_player.rb +458 -0
  55. data/lib/vimamsa/demo_recorder.rb +277 -0
  56. data/lib/vimamsa/desktop_install.rb +96 -0
  57. data/lib/vimamsa/diff_buffer.rb +40 -11
  58. data/lib/vimamsa/dot_repeat.rb +104 -0
  59. data/lib/vimamsa/easy_jump.rb +57 -3
  60. data/lib/vimamsa/editor.rb +327 -78
  61. data/lib/vimamsa/encrypt.rb +78 -29
  62. data/lib/vimamsa/file_finder.rb +4 -19
  63. data/lib/vimamsa/file_history.rb +4 -1
  64. data/lib/vimamsa/file_manager.rb +46 -31
  65. data/lib/vimamsa/fold.rb +226 -0
  66. data/lib/vimamsa/form_input.rb +388 -0
  67. data/lib/vimamsa/gui.rb +603 -119
  68. data/lib/vimamsa/gui_command_line.rb +247 -0
  69. data/lib/vimamsa/gui_dialog.rb +94 -42
  70. data/lib/vimamsa/gui_file_panel.rb +356 -25
  71. data/lib/vimamsa/gui_form_generator.rb +75 -40
  72. data/lib/vimamsa/gui_func_panel.rb +67 -22
  73. data/lib/vimamsa/gui_image.rb +3 -0
  74. data/lib/vimamsa/gui_key_hint.rb +165 -0
  75. data/lib/vimamsa/gui_keylog_panel.rb +408 -0
  76. data/lib/vimamsa/gui_menu.rb +84 -53
  77. data/lib/vimamsa/gui_select_window.rb +80 -75
  78. data/lib/vimamsa/gui_settings.rb +86 -41
  79. data/lib/vimamsa/gui_sourceview.rb +280 -92
  80. data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
  81. data/lib/vimamsa/gui_text.rb +63 -2
  82. data/lib/vimamsa/gui_theme.rb +615 -0
  83. data/lib/vimamsa/hook.rb +5 -0
  84. data/lib/vimamsa/hyper_plain_text.rb +3 -0
  85. data/lib/vimamsa/intro_page.rb +368 -0
  86. data/lib/vimamsa/key_actions.rb +382 -193
  87. data/lib/vimamsa/key_binding_tree.rb +467 -273
  88. data/lib/vimamsa/key_bindings_notepad.rb +45 -0
  89. data/lib/vimamsa/key_bindings_report.rb +484 -0
  90. data/lib/vimamsa/key_bindings_vim.rb +104 -0
  91. data/lib/vimamsa/key_bindings_vimlike.rb +353 -289
  92. data/lib/vimamsa/key_names.rb +80 -0
  93. data/lib/vimamsa/langservp.rb +271 -54
  94. data/lib/vimamsa/line_tree.rb +367 -0
  95. data/lib/vimamsa/macro.rb +23 -3
  96. data/lib/vimamsa/main.rb +14 -8
  97. data/lib/vimamsa/rbvma.rb +41 -1
  98. data/lib/vimamsa/registers.rb +125 -0
  99. data/lib/vimamsa/run_ruby.rb +64 -0
  100. data/lib/vimamsa/search.rb +3 -0
  101. data/lib/vimamsa/search_replace.rb +15 -14
  102. data/lib/vimamsa/string_util.rb +134 -40
  103. data/lib/vimamsa/terminal.rb +3 -0
  104. data/lib/vimamsa/test_framework.rb +28 -2
  105. data/lib/vimamsa/tests.rb +11 -8
  106. data/lib/vimamsa/text_objects.rb +310 -0
  107. data/lib/vimamsa/text_transforms.rb +18 -0
  108. data/lib/vimamsa/tmp_user_dir.rb +39 -0
  109. data/lib/vimamsa/user_dirs.rb +83 -0
  110. data/lib/vimamsa/util.rb +46 -7
  111. data/lib/vimamsa/version.rb +1 -1
  112. data/lib/vimamsa/vim_core.rb +355 -0
  113. data/lib/vimamsa/vim_operators.rb +365 -0
  114. data/lib/vimamsa.rb +0 -1
  115. data/modules/calculator/calculator.rb +5 -5
  116. data/modules/color_highlight/color_highlight.rb +118 -0
  117. data/modules/color_highlight/color_highlight_info.rb +3 -0
  118. data/modules/dictation/dictation.rb +1054 -0
  119. data/modules/dictation/dictation_info.rb +3 -0
  120. data/modules/dictation/whisper_worker.py +273 -0
  121. data/modules/ollama/ollama.rb +107 -0
  122. data/modules/ollama/ollama_info.rb +3 -0
  123. data/modules/terminal/terminal.rb +5 -5
  124. data/modules/tts/tts.rb +119 -0
  125. data/modules/tts/tts_info.rb +3 -0
  126. data/run_e2e_vm.sh +28 -0
  127. data/run_tests_vm.rb +138 -0
  128. data/scripts/appimage/AppRun +54 -0
  129. data/scripts/appimage/Dockerfile +18 -0
  130. data/scripts/appimage/build.sh +19 -0
  131. data/scripts/appimage/build_in_container.sh +192 -0
  132. data/scripts/appimage/smoke_test.sh +27 -0
  133. data/scripts/demo/demo_easyjump_steps.txt +60 -0
  134. data/scripts/demo/demo_files_steps.txt +52 -0
  135. data/scripts/demo/demo_steps.txt +67 -0
  136. data/scripts/demo/notes.txt +6 -0
  137. data/scripts/demo/record_demo.rb +156 -0
  138. data/scripts/demo/report.rb +7 -0
  139. data/scripts/demo/shapes.rb +29 -0
  140. data/scripts/demo/square/shapes.rb +29 -0
  141. data/scripts/demo/square/steps.txt +185 -0
  142. data/scripts/lib/perf_startup.rb +138 -0
  143. data/scripts/lib/script_helpers.rb +53 -0
  144. data/scripts/perf_compare.rb +34 -0
  145. data/scripts/profile_startup.rb +72 -0
  146. data/styles/134272-molokai.xml +3 -0
  147. data/styles/molokai_cyber.xml +48 -0
  148. data/styles/molokai_edit.xml +1 -0
  149. data/tests/support/virtual_keyboard.rb +254 -0
  150. data/tests/support/virtual_mic.rb +67 -0
  151. data/tests/test_ack.rb +22 -0
  152. data/tests/test_action_ids.rb +232 -0
  153. data/tests/test_auto_format.rb +122 -0
  154. data/tests/test_autocomplete.rb +211 -0
  155. data/tests/test_autosave.rb +145 -0
  156. data/tests/test_basic_editing.rb +46 -16
  157. data/tests/test_bookmarks.rb +206 -0
  158. data/tests/test_bracket_match.rb +88 -0
  159. data/tests/test_bundling.rb +92 -0
  160. data/tests/test_cli_help.rb +64 -0
  161. data/tests/test_color_highlight.rb +92 -0
  162. data/tests/test_command_line.rb +79 -0
  163. data/tests/test_command_line_file.rb +27 -0
  164. data/tests/test_context_menu_file.rb +147 -0
  165. data/tests/test_copy_paste.rb +106 -26
  166. data/tests/test_crash_handler.rb +39 -0
  167. data/tests/test_custom_two_phase.rb +92 -0
  168. data/tests/test_demo_player.rb +301 -0
  169. data/tests/test_demo_recorder.rb +202 -0
  170. data/tests/test_dialog_callback_errors.rb +44 -0
  171. data/tests/test_dictation.rb +294 -0
  172. data/tests/test_diff_buffer_name.rb +51 -0
  173. data/tests/test_dot_repeat.rb +88 -0
  174. data/tests/test_double_click.rb +41 -0
  175. data/tests/test_drag_selection.rb +125 -0
  176. data/tests/test_e2e_cmdline.rb +116 -0
  177. data/tests/test_e2e_dictation.rb +98 -0
  178. data/tests/test_e2e_replace_char.rb +58 -0
  179. data/tests/test_easy_jump.rb +93 -0
  180. data/tests/test_extract_file_links.rb +55 -0
  181. data/tests/test_file_format.rb +217 -0
  182. data/tests/test_file_manager.rb +72 -0
  183. data/tests/test_file_panel.rb +269 -0
  184. data/tests/test_flash.rb +78 -0
  185. data/tests/test_fold.rb +111 -0
  186. data/tests/test_font_refresh.rb +56 -0
  187. data/tests/test_form_input.rb +203 -0
  188. data/tests/test_func_panel.rb +68 -0
  189. data/tests/test_grep_apply.rb +46 -0
  190. data/tests/test_gtk_sync.rb +126 -0
  191. data/tests/test_hpt_highlight_visible.rb +75 -0
  192. data/tests/test_hpt_link_highlight.rb +82 -0
  193. data/tests/test_intro_page.rb +437 -0
  194. data/tests/test_jump_to_file.rb +41 -0
  195. data/tests/test_key_bindings.rb +208 -0
  196. data/tests/test_key_categories.rb +163 -0
  197. data/tests/test_keylog_panel.rb +261 -0
  198. data/tests/test_line_ends.rb +51 -0
  199. data/tests/test_line_tree.rb +86 -0
  200. data/tests/test_lsp_clangd.rb +281 -0
  201. data/tests/test_lsp_jump.rb +87 -0
  202. data/tests/test_lsp_position_encoding.rb +49 -0
  203. data/tests/test_lsp_symbols.rb +68 -0
  204. data/tests/test_macro.rb +137 -0
  205. data/tests/test_markdown_highlight.rb +57 -0
  206. data/tests/test_mode_badge.rb +41 -0
  207. data/tests/test_module_interface.rb +9 -9
  208. data/tests/test_notepad_bindings.rb +67 -0
  209. data/tests/test_perf_startup.rb +28 -0
  210. data/tests/test_popup_queue.rb +51 -0
  211. data/tests/test_random_edit.rb +109 -0
  212. data/tests/test_registers.rb +122 -0
  213. data/tests/test_revert.rb +127 -0
  214. data/tests/test_run_ruby.rb +77 -0
  215. data/tests/test_startup_file.rb +58 -0
  216. data/tests/test_status_area.rb +50 -0
  217. data/tests/test_text_objects.rb +163 -0
  218. data/tests/test_text_transforms.rb +341 -0
  219. data/tests/test_tmp_user_dir.rb +79 -0
  220. data/tests/test_tts.rb +68 -0
  221. data/tests/test_undo.rb +34 -34
  222. data/tests/test_user_dirs.rb +122 -0
  223. data/tests/test_vim_bindings.rb +353 -0
  224. data/tests/test_vim_conflicts.rb +100 -0
  225. data/tests/test_vim_operators.rb +116 -0
  226. data/tests/test_window_layout.rb +302 -0
  227. data/vimamsa.gemspec +3 -1
  228. data/vm_guest_run.sh +129 -0
  229. metadata +167 -10
data/lib/vimamsa/gui.rb CHANGED
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  $idle_scroll_to_mark = false
2
4
 
3
5
  $removed_controllers = []
@@ -19,25 +21,6 @@ def end_profiler
19
21
  printer.print(STDOUT)
20
22
  end
21
23
 
22
- # Wait for window resize to take effect
23
- # GTk3 had a resize notify event which got removed in gtk4
24
- # https://discourse.gnome.org/t/gtk4-any-way-to-connect-to-a-window-resize-signal/14869/3
25
- def wait_for_resize(window, tries = 200)
26
- i = 0
27
- widthold = @window.width
28
- heightold = @window.height
29
- while true
30
- iterate_gui_main_loop
31
- break if widthold != window.width
32
- break if heightold != window.height
33
- if i >= tries
34
- debug "i >= tries", 2
35
- break
36
- end
37
- i += 1
38
- end
39
- end
40
-
41
24
  def gui_remove_controllers(widget)
42
25
  clist = widget.observe_controllers
43
26
  to_remove = []
@@ -61,6 +44,7 @@ def gui_open_file_dialog(dirpath)
61
44
  :action => :open,
62
45
  :buttons => [["Open", :accept],
63
46
  ["Cancel", :cancel]])
47
+ dialog.add_css_class("vma-dialog")
64
48
  dialog.set_current_folder(Gio::File.new_for_path(dirpath))
65
49
 
66
50
  dialog.signal_connect("response") do |dialog, response_id|
@@ -79,6 +63,7 @@ def gui_file_saveas(dirpath)
79
63
  :action => :save,
80
64
  :buttons => [["Save", :accept],
81
65
  ["Cancel", :cancel]])
66
+ dialog.add_css_class("vma-dialog")
82
67
  dialog.set_current_folder(Gio::File.new_for_path(dirpath))
83
68
  dialog.signal_connect("response") do |dialog, response_id|
84
69
  if response_id == Gtk::ResponseType::ACCEPT
@@ -135,6 +120,7 @@ def gui_create_buffer(id, bufo)
135
120
  view.set_highlight_current_line(true)
136
121
  view.set_show_line_numbers(true)
137
122
  view.set_buffer(buf1)
123
+ view.register_buffer_signals()
138
124
 
139
125
  provider = Gtk::CssProvider.new
140
126
 
@@ -184,17 +170,26 @@ def gui_set_current_buffer(id)
184
170
  return
185
171
  end
186
172
 
187
- def gui_set_window_title(wtitle, subtitle = "")
173
+ def gui_set_window_title(wtitle, subtitle = "", modified: false)
188
174
  wtitle = wtitle[0..150]
189
175
  $vmag.window.title = "Vimamsa - #{wtitle}"
190
- # $vmag.subtitle.markup = "<span weight='ultrabold'>#{subtitle}</span>"
191
- $vmag.subtitle.markup = "<span weight='light' size='small'>#{subtitle}</span>"
176
+ # Subtitle shows the file path: dim directory, bright basename,
177
+ # "+ " when there are unsaved changes (same marker as the window title;
178
+ # no fixed color, so it follows the theme).
179
+ subtitle = subtitle.to_s
180
+ dir = File.dirname(subtitle)
181
+ dir = (dir == ".") ? "" : (dir == "/" ? "/" : "#{dir}/")
182
+ base = File.basename(subtitle)
183
+ mark = modified ? "<span weight='bold'>+ </span>" : ""
184
+ $vmag.subtitle.markup = "<span size='small'>#{mark}" \
185
+ "<span alpha='55%'>#{CGI.escapeHTML(dir)}</span>" \
186
+ "<span weight='bold' alpha='92%'>#{CGI.escapeHTML(base)}</span></span>"
192
187
  # $vmag.window.titlebar.subtitle = subtitle #TODO:gtk4
193
188
  end
194
189
 
195
190
  class VMAgui
196
- attr_accessor :buffers, :sw1, :sw2, :view, :buf1, :window, :delex, :statnfo, :overlay, :sws, :two_c
197
- attr_reader :two_column, :windows, :subtitle, :app, :active_window, :action_trail_label, :file_panel, :func_panel
191
+ attr_accessor :buffers, :sw1, :sw2, :view, :buf1, :window, :delex, :statnfo, :overlay, :sws, :two_c, :scheme_is_light, :cyber_css_provider, :chrome_css_provider, :minibuf_css_provider, :orig_icon_theme
192
+ attr_reader :two_column, :windows, :subtitle, :app, :active_window, :action_trail_label, :file_panel, :func_panel, :keylog_panel, :keytrail, :cmd_line
198
193
 
199
194
  def initialize()
200
195
  @two_column = false
@@ -217,6 +212,12 @@ class VMAgui
217
212
  @entry_has_focus = active
218
213
  end
219
214
 
215
+ # True while a text entry (command line, calculator field, ...) owns the
216
+ # keyboard focus and the editor view must not steal it back.
217
+ def entry_focused?
218
+ @entry_has_focus
219
+ end
220
+
220
221
  def run
221
222
  init_window
222
223
  # init_rtext
@@ -275,13 +276,43 @@ class VMAgui
275
276
  end
276
277
  end
277
278
 
278
- def overlay_draw_text(text, textpos)
279
+ # Clears the overlay after ms milliseconds (e.g. to leave the chosen easy
280
+ # jump label up for a moment). A new overlay drawn meanwhile is kept.
281
+ def clear_overlay_later(ms)
282
+ da, ov = @da, @active_window[:overlay]
283
+ return if da.nil?
284
+ @da = nil
285
+ GLib::Timeout.add(ms) { ov.remove_overlay(da) rescue nil; false }
286
+ end
287
+
288
+ # Removes one widget drawn with overlay_draw_text
289
+ def overlay_remove(widget)
290
+ @da&.remove(widget)
291
+ end
292
+
293
+ # A soft glow around overlay labels (easy jump), with cnf.easy_jump.glow.
294
+ # Off by default: without a GPU (GTK's software renderer) the shadows made
295
+ # easy jump about 60% slower (a full screen, 433 labels: 0.65 s vs 0.41 s).
296
+ OVERLAY_LABEL_CSS = <<~CSS
297
+ label.overlay-label {
298
+ text-shadow: 0 0 3px rgba(255, 40, 40, 0.9);
299
+ }
300
+ CSS
301
+
302
+ # Draws text (or Pango markup) at buffer position textpos; returns the label
303
+ def overlay_draw_text(text, textpos, markup: nil)
279
304
  # debug "overlay_draw_text #{[x,y]}"
280
305
  (x, y) = @view.pos_to_coord(textpos)
281
306
  # debug "overlay_draw_text #{[x,y]}"
282
- label = Gtk::Label.new("<span background='#000000ff' foreground='#ff0000' weight='ultrabold'>#{text}</span>")
307
+ label = Gtk::Label.new(markup || "<span background='#000000ff' foreground='#ff0000' weight='ultrabold'>#{text}</span>")
283
308
  label.use_markup = true
309
+ label.add_css_class("overlay-label")
310
+ if cnf.easy_jump.glow?
311
+ @overlay_label_css ||= Gtk::CssProvider.new.tap { |c| c.load(data: OVERLAY_LABEL_CSS) }
312
+ label.style_context.add_provider(@overlay_label_css)
313
+ end
284
314
  @da.put(label, x, y)
315
+ label
285
316
  end
286
317
 
287
318
  def end_overlay_draw()
@@ -395,9 +426,9 @@ class VMAgui
395
426
  @minibuf_expanded = false
396
427
  @minibuf_history_height = 120
397
428
 
398
- css = "label.minibuf, textview.minibuf { color: #cdd6f4; font-family: Monospace; font-size: 10pt; padding: 3px 8px; background-color: #1e1e2e; }"
399
429
  provider = Gtk::CssProvider.new
400
- provider.load(data: css)
430
+ provider.load(data: minibuf_css) # colors from gui_theme.rb (cnf-backed)
431
+ self.minibuf_css_provider = provider
401
432
 
402
433
  # Collapsed view: single line, latest message
403
434
  @minibuf_label = Gtk::Label.new("")
@@ -428,6 +459,10 @@ class VMAgui
428
459
  @minibuf_stack.add_named(@minibuf_label, "label")
429
460
  @minibuf_stack.add_named(scroll, "history")
430
461
 
462
+ # Interactive command line (vim-style ":"), third stack child
463
+ @cmd_line = CommandLine.new(provider)
464
+ @minibuf_stack.add_named(@cmd_line.widget, "cmdline")
465
+
431
466
  @minibuf_content = Gtk::Box.new(:vertical, 0)
432
467
  @minibuf_content.append(Gtk::Separator.new(:horizontal))
433
468
  @minibuf_content.append(@minibuf_stack)
@@ -450,6 +485,24 @@ class VMAgui
450
485
  @minibuf_hide_source = nil
451
486
  end
452
487
 
488
+ # ── command line (vim-style ":", see gui_command_line.rb) ─────────────────
489
+
490
+ def cmd_line_show
491
+ GLib::Source.remove(@minibuf_hide_source) if @minibuf_hide_source
492
+ @minibuf_hide_source = nil
493
+ @minibuf_content.visible = true
494
+ @minibuf_stack.visible_child_name = "cmdline"
495
+ @minibuf_vpane.position = @minibuf_vpane.height - 34 unless @minibuf_expanded
496
+ notify_entry_focus(true)
497
+ end
498
+
499
+ def cmd_line_hide
500
+ notify_entry_focus(false)
501
+ @minibuf_stack.visible_child_name = @minibuf_expanded ? "history" : "label"
502
+ @minibuf_content.visible = false unless @minibuf_expanded
503
+ vma.buf.view.grab_focus if vma.buf&.view
504
+ end
505
+
453
506
  def make_header_button(action_id, icon, cb)
454
507
  act = Gio::SimpleAction.new(action_id)
455
508
  @app.add_action(act)
@@ -491,14 +544,13 @@ class VMAgui
491
544
  # @view.check_controllers
492
545
  end
493
546
 
494
- ctrl_fn = File.expand_path(get_dot_path("ripl_ctrl"))
547
+ ctrl_fn = File.expand_path(get_data_path("ripl_ctrl"))
495
548
  # Allows to debug in case keyboard handling is lost
496
549
  if File.exist?(ctrl_fn)
497
550
  File.delete(ctrl_fn)
498
551
  start_ripl
499
552
  end
500
553
 
501
- sleep 0.02
502
554
  return true
503
555
  end
504
556
 
@@ -541,13 +593,7 @@ class VMAgui
541
593
  # eventctr: Gtk::EventControllerKey
542
594
  # modtype: Gdk::ModifierType
543
595
  debug "modifier change"
544
- vma.kbd.modifiers[:ctrl] = modtype.control_mask?
545
- vma.kbd.modifiers[:alt] = modtype.alt_mask?
546
- vma.kbd.modifiers[:hyper] = modtype.hyper_mask?
547
- vma.kbd.modifiers[:lock] = modtype.lock_mask?
548
- vma.kbd.modifiers[:meta] = modtype.meta_mask?
549
- vma.kbd.modifiers[:shift] = modtype.shift_mask?
550
- vma.kbd.modifiers[:super] = modtype.super_mask?
596
+ gui_set_modifiers(modtype) # key_names.rb
551
597
 
552
598
  #TODO:?
553
599
  # button1_mask?
@@ -584,33 +630,15 @@ class VMAgui
584
630
  end
585
631
  end
586
632
 
587
- def idle_set_size()
588
- # Need to wait for a while to window to be maximized to get correct @window.width
589
- @window.maximize
590
- wait_for_resize(@window)
591
- # Set new size as half of the screeen
592
- width = @window.width / 2
593
- height = @window.height - 5
594
- width = 600 if width < 600
595
- height = 600 if height < 600
596
-
597
- #Minimum size:
598
- @window.set_size_request(600, 600)
599
- @window.set_default_size(width, height)
600
- debug "size #{[width, height]}", 2
601
- @window.unmaximize
602
-
603
- #set_default_size doesn't always have effect if run immediately
604
- wait_for_resize(@window)
605
- @window.set_default_size(width, height)
606
-
607
- return false
608
- end
609
-
610
633
  def init_window
611
634
  @last_debug_idle = Time.now
612
- app = Gtk::Application.new("net.samiddhi.vimamsa.r#{rand(1000)}", :flags_none)
635
+ # Stable application id so desktop environments can match the running window
636
+ # to its launcher entry / icon (see desktop_install.rb). :non_unique avoids
637
+ # GTK's D-Bus single-instance enforcement (single-instance forwarding is
638
+ # handled separately via the listen file in exe/vimamsa).
639
+ app = Gtk::Application.new(VMA_APP_ID, :non_unique)
613
640
  @app = app
641
+ Gtk::Window.set_default_icon_name("vimamsa")
614
642
 
615
643
  Gtk::Settings.default.gtk_application_prefer_dark_theme = true
616
644
  Gtk::Settings.default.gtk_theme_name = "Adwaita"
@@ -632,9 +660,10 @@ class VMAgui
632
660
  @vbox = Gtk::Grid.new()
633
661
  @window.set_child(@vbox)
634
662
 
635
- Thread.new {
636
- GLib::Idle.add(proc { debug_idle_func })
637
- }
663
+ # Periodic timeouts, not idles that always return true: an idle that is
664
+ # always ready keeps GLib from ever dispatching lower-priority sources,
665
+ # e.g. GtkSourceView's matching-bracket highlight never ran.
666
+ GLib::Timeout.add(200) { debug_idle_func }
638
667
 
639
668
  reset_controllers
640
669
 
@@ -662,23 +691,31 @@ class VMAgui
662
691
 
663
692
  @last_adj_time = Time.now
664
693
 
665
- # To show keyboard key binding state
694
+ # To show keyboard key binding state (mode badge, e.g. COMMAND)
666
695
  @statnfo = Gtk::Label.new
696
+ @statnfo.add_css_class("mode-badge")
697
+
698
+ # Pending key chord / repeat count next to the mode badge
699
+ @keytrail = Gtk::Label.new("")
700
+ @keytrail.add_css_class("keytrail")
667
701
 
668
702
  # To show e.g. current folder
669
703
  @subtitle = Gtk::Label.new("")
670
704
 
671
- @statbox = Gtk::Box.new(:horizontal, 2)
672
- @statnfo.set_size_request(150, 10)
705
+ # Persistent status slots for long-running work that outlives a minibuf
706
+ # line — e.g. dictation reporting that its speech model is still loading.
707
+ # Modules claim a slot by key via #set_status_indicator; core attaches no
708
+ # meaning to the keys.
709
+ @status_indicators = {}
710
+ @status_indicator_css = {}
711
+ @status_indicator_box = Gtk::Box.new(:horizontal, 6)
712
+
713
+ @statbox = Gtk::Box.new(:horizontal, 6)
673
714
  @statbox.append(@subtitle)
674
715
  @subtitle.hexpand = true
716
+ @statbox.append(@status_indicator_box)
717
+ @statbox.append(@keytrail)
675
718
  @statbox.append(@statnfo)
676
- provider = Gtk::CssProvider.new
677
- @statnfo.add_css_class("statnfo")
678
- provider.load(data: "label.statnfo { background-color:#353535; font-size: 10pt; margin-top:2px; margin-bottom:2px; align:right;}")
679
-
680
- provider = Gtk::CssProvider.new
681
- @statnfo.style_context.add_provider(provider)
682
719
 
683
720
  # numbers: left, top, width, height
684
721
  @vbox.attach(@windows[1][:overlay], 0, 2, 2, 1)
@@ -701,6 +738,7 @@ class VMAgui
701
738
  @action_trail_label = Gtk::Label.new("")
702
739
  @action_trail_label.add_css_class("action-trail")
703
740
  menubar_row = Gtk::Box.new(:horizontal, 0)
741
+ menubar_row.add_css_class("menubar-row")
704
742
  menubar_row.append(menubar_bar)
705
743
  menubar_row.append(@action_trail_label)
706
744
  @vbox.attach(menubar_row, 0, 0, 2, 1)
@@ -710,7 +748,11 @@ class VMAgui
710
748
  init_header_bar
711
749
  file_panel_init
712
750
  func_panel_init
751
+ keylog_panel_init
713
752
 
753
+ # Size the window before it is first shown (see apply_startup_window_size)
754
+ apply_startup_window_size
755
+ @window.signal_connect("map") { layout_recheck_monitor }
714
756
  @window.show
715
757
 
716
758
  surface = @window.native.surface
@@ -719,61 +761,126 @@ class VMAgui
719
761
  # puts "Window resized or moved, other redraw."
720
762
  end
721
763
 
722
- run_as_idle proc { idle_set_size }
723
764
 
724
- prov = Gtk::CssProvider.new
765
+ # Baseline chrome CSS + all its colors live in gui_theme.rb (cnf-backed).
725
766
  # See gtk-4.9.4/gtk/theme/Default/_common.scss on how to theme
726
767
  # gtksourceview/gtksourcestyleschemepreview.c
727
768
  # gtksourceview/gtksourcestylescheme.c
728
- prov.load(data: " headerbar { padding: 0 0px; min-height: 16px; border-width: 0 0 0px; border-style: solid; }
729
-
730
- textview border.left gutter { color: #8aa; font-size:8pt; }
731
-
732
- textview border.left gutter { padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px; color: #8aa; font-size:9pt; }
733
-
734
- headerbar .title {
735
- font-weight: bold;
736
- font-size: 11pt;
737
- color: #cdffee;
738
- }
739
-
740
- headerbar > windowhandle > box .start {
741
- border-spacing: 6px;
742
- }
743
-
744
- headerbar windowcontrols button {
745
- min-height: 15px;
746
- min-width: 15px;
747
- }
748
-
749
- popover background > contents { padding: 8px; border-radius: 20px; }
750
-
751
- label.action-trail { font-family: monospace; font-size: 10pt; margin-right: 8px; color: #aaaaaa; }
752
- ")
769
+ prov = Gtk::CssProvider.new
770
+ prov.load(data: base_chrome_css)
771
+ self.chrome_css_provider = prov
753
772
  @window.style_context.add_provider(prov)
754
773
 
755
774
  sc = Gtk::StyleContext.add_provider_for_display(Gdk::Display.default, prov)
756
775
 
757
776
  vma.start
777
+
778
+ # After vma.start: user settings.rb (which may enable the theme) is
779
+ # loaded during Editor#start. Refresh reads any cnf color overrides and
780
+ # installs the cyberpunk provider if enabled.
781
+ gui_refresh_colors
758
782
  end
759
783
 
760
- GLib::Idle.add(proc { self.monitor })
784
+ GLib::Timeout.add(200) { self.monitor }
761
785
 
762
786
  app.run
763
787
  end
764
788
 
789
+ # CSS class of mode badge label per mode key_name; others get "mode-other"
790
+ MODE_BADGE_CSS = { "C" => "mode-command", "I" => "mode-insert", "V" => "mode-visual",
791
+ "B" => "mode-browse", "X" => "mode-replace" }.freeze
792
+
793
+ # The mode badge's text (e.g. "COMMAND") and CSS class (MODE_BADGE_CSS)
794
+ def mode_badge_info
795
+ [@last_badge_text, @badge_css_class]
796
+ end
797
+
798
+ # Render the mode badge and pending key chord in the status area.
799
+ # Subscribed to KeyBindingTree's :state_trail_changed event.
800
+ def update_mode_badge(badge, badge_key, trail)
801
+ cls = MODE_BADGE_CSS.fetch(badge_key, "mode-other")
802
+ if cls != @badge_css_class
803
+ @statnfo.remove_css_class(@badge_css_class) if @badge_css_class
804
+ @statnfo.add_css_class(cls)
805
+ @badge_css_class = cls
806
+ end
807
+ @statnfo.text = badge if badge != @last_badge_text
808
+ @keytrail.text = trail if trail != @last_trail_text
809
+ @last_badge_text = badge
810
+ @last_trail_text = trail
811
+ end
812
+
813
+ # Show a persistent status message in its own slot in the status area, for
814
+ # work that takes long enough that the user needs to see it is still going —
815
+ # a minibuf line scrolls away and is easy to miss. `key` namespaces the slot
816
+ # (one per module/feature), `text` nil or empty removes it, `css_class` styles
817
+ # the label. Call from the main thread (GLib::Idle.add from a worker thread).
818
+ def set_status_indicator(key, text, css_class: nil)
819
+ return if @status_indicator_box.nil? # GUI not built yet
820
+ label = @status_indicators[key]
821
+
822
+ if text.nil? || text.to_s.empty?
823
+ return if label.nil?
824
+ @status_indicator_box.remove(label)
825
+ @status_indicators.delete(key)
826
+ @status_indicator_css.delete(key)
827
+ return
828
+ end
829
+
830
+ if label.nil?
831
+ label = Gtk::Label.new("")
832
+ label.add_css_class("status-indicator")
833
+ @status_indicators[key] = label
834
+ @status_indicator_box.append(label)
835
+ end
836
+
837
+ if css_class != @status_indicator_css[key]
838
+ prev = @status_indicator_css[key]
839
+ label.remove_css_class(prev) if prev
840
+ label.add_css_class(css_class) if css_class
841
+ @status_indicator_css[key] = css_class
842
+ end
843
+ label.text = text.to_s
844
+ end
845
+
846
+ # Text currently shown in a status slot, nil when the slot is empty.
847
+ def status_indicator(key)
848
+ @status_indicators[key]&.text
849
+ end
850
+
851
+ # Show the last executed key chord and action next to the menubar.
852
+ # Subscribed to KeyBindingTree's :action_handled event.
853
+ def show_action_trail(trail_str, action)
854
+ return if !cnf.kbd.show_prev_action? or trail_str.class != String
855
+ len_limit = 35
856
+ if action.class == String && (m = action.match(/\Abuf\.insert_txt\((.+)\)\z/))
857
+ action_desc = "insert #{m[1]}"
858
+ else
859
+ action_desc = vma.actions[action]&.method_name || action.to_s
860
+ action_desc = action_desc[0..len_limit] if action_desc.size > len_limit
861
+ end
862
+ trail_esc = CGI.escapeHTML(trail_str)
863
+ action_esc = CGI.escapeHTML(action_desc)
864
+ # Completed key chord bright, executed action name dim
865
+ @action_trail_label.markup = "<span alpha='70%'>#{trail_esc} #{action_esc}</span>"
866
+ end
867
+
765
868
  def monitor
869
+ # kill / ctrl-c (the trap in exe/vimamsa)
870
+ if $vma_quit_requested
871
+ $vma_quit_requested = false
872
+ vma.shutdown
873
+ return false
874
+ end
875
+ return true if @windows[1].nil?
766
876
  swa = @windows[1][:sw]
767
- @monitor_time ||= Time.now
768
877
  @sw_width ||= swa.width
769
- return true if Time.now - @monitor_time < 0.2
770
878
  # Detect element resize
771
879
  if swa.width != @sw_width
772
880
  debug "Width change sw_width #{@sw_width}"
773
881
  @sw_width = swa.width
774
882
  DelayExecutioner.exec(id: :scale_images, wait: 0.7, callable: proc { debug ":scale_images"; vma.gui.scale_all_images })
775
883
  end
776
- @monitor_time = Time.now
777
884
  return true
778
885
  end
779
886
 
@@ -791,6 +898,7 @@ class VMAgui
791
898
  else
792
899
  set_two_column
793
900
  end
901
+ save_window_layout
794
902
  end
795
903
 
796
904
  def set_one_column
@@ -798,6 +906,7 @@ class VMAgui
798
906
  #This always closes the leftmost column/window
799
907
  #TODO: close rightmost column if left active
800
908
  set_active_window(1)
909
+ @column_split = @pane.position # kept for the next set_two_column
801
910
 
802
911
  @windows[2][:sw].set_child(nil)
803
912
  @windows.delete(2)
@@ -819,8 +928,12 @@ class VMAgui
819
928
  def new_window(win_id)
820
929
  n_sw = Gtk::ScrolledWindow.new
821
930
  n_sw.set_policy(:automatic, :automatic)
931
+ n_sw.add_css_class("editor-frame")
932
+ # The scrolled window is the overlay's main child, which GTK always draws
933
+ # below the overlay children (key hint, easy jump labels): a buffer switch
934
+ # replaces it there and doesn't cover them (set_current_buffer)
822
935
  n_overlay = Gtk::Overlay.new
823
- n_overlay.add_overlay(n_sw)
936
+ n_overlay.set_child(n_sw)
824
937
  # @pane = Gtk::Paned.new(:horizontal)
825
938
 
826
939
  win = { :sw => n_sw, :overlay => n_overlay, :id => win_id }
@@ -864,6 +977,7 @@ class VMAgui
864
977
 
865
978
  # Remove overlay from its current parent and add the Gtk::Paned instead
866
979
  @pane = Gtk::Paned.new(:horizontal)
980
+ @pane.set_position(@column_split) if @column_split.to_i > 0
867
981
  if @func_panel_shown
868
982
  @func_panel_pane.set_end_child(nil)
869
983
  @pane.set_start_child(w2[:overlay])
@@ -985,13 +1099,14 @@ class VMAgui
985
1099
  #TODO: improve
986
1100
  swa = @active_window[:sw]
987
1101
  ol = @active_window[:overlay]
988
- ol.remove_overlay(swa)
989
1102
  swa.set_child(nil)
990
1103
  # Creating a new ScrolledWindow every time to avoid a layout bug
991
1104
  # https://gitlab.gnome.org/GNOME/gtk/-/issues/6189
992
1105
  swb = new_scrolled_window
993
1106
  swb.set_child(view2)
994
- ol.add_overlay(swb)
1107
+ # The overlay's main child: the overlay children (key hint, easy jump
1108
+ # labels) stay above it
1109
+ ol.set_child(swb)
995
1110
  @active_window[:view] = view
996
1111
  @active_window[:sw] = swb
997
1112
  end
@@ -1037,6 +1152,21 @@ class VMAgui
1037
1152
  view.draw_cursor
1038
1153
  end
1039
1154
 
1155
+ # Wrap a side panel widget and the current editor content in a horizontal
1156
+ # Paned with the panel on the left at the given divider position.
1157
+ def make_side_pane(panel_widget, inner, pos)
1158
+ pane = Gtk::Paned.new(:horizontal)
1159
+ pane.hexpand = true
1160
+ pane.vexpand = true
1161
+ pane.set_start_child(panel_widget)
1162
+ pane.set_end_child(inner)
1163
+ # The panel keeps its width when the window is resized (or maximized
1164
+ # during startup); the editor takes the difference
1165
+ pane.resize_start_child = false
1166
+ pane.set_position(pos)
1167
+ pane
1168
+ end
1169
+
1040
1170
  def file_panel_init
1041
1171
  @file_panel = FileTreePanel.new
1042
1172
  @file_panel_shown = false
@@ -1047,6 +1177,37 @@ class VMAgui
1047
1177
  @file_panel.refresh
1048
1178
  end
1049
1179
 
1180
+ # Marks of the current buffer changed (set, or shifted by an edit): update
1181
+ # the panel's marks section, coalescing bursts (typing) into one update.
1182
+ def file_panel_marks_changed
1183
+ return unless @file_panel_shown
1184
+ return if @file_panel_marks_pending
1185
+ @file_panel_marks_pending = true
1186
+ GLib::Timeout.add(300) do
1187
+ @file_panel_marks_pending = false
1188
+ @file_panel.refresh_marks if @file_panel_shown
1189
+ false
1190
+ end
1191
+ end
1192
+
1193
+ def file_panel_shown?
1194
+ @file_panel_shown
1195
+ end
1196
+
1197
+ # alt-h / alt-l: previous/next file in the panel's display order.
1198
+ # A no-op while the panel is hidden.
1199
+ def file_panel_select_adjacent(delta)
1200
+ return unless @file_panel_shown
1201
+ @file_panel.select_adjacent(delta)
1202
+ end
1203
+
1204
+ # g s: label the panel's file rows and switch to the file whose label is
1205
+ # typed (see FileTreePanel#easy_jump_start). A no-op while the panel is hidden.
1206
+ def file_panel_easy_jump
1207
+ return unless @file_panel_shown
1208
+ @file_panel.easy_jump_start
1209
+ end
1210
+
1050
1211
  def show_file_panel
1051
1212
  return if @file_panel_shown
1052
1213
  # If func panel is shown it sits in minibuf_vpane; wrap it too
@@ -1058,12 +1219,7 @@ class VMAgui
1058
1219
  @windows[1][:overlay]
1059
1220
  end
1060
1221
  @minibuf_vpane.set_start_child(nil) # unparent inner before re-parenting
1061
- @file_panel_pane = Gtk::Paned.new(:horizontal)
1062
- @file_panel_pane.hexpand = true
1063
- @file_panel_pane.vexpand = true
1064
- @file_panel_pane.set_start_child(@file_panel.widget)
1065
- @file_panel_pane.set_end_child(inner)
1066
- @file_panel_pane.set_position(180)
1222
+ @file_panel_pane = make_side_pane(@file_panel.widget, inner, @file_panel_width || PANEL_DEFAULT_WIDTH[:file_panel])
1067
1223
  set_editor_area(@file_panel_pane)
1068
1224
  @file_panel_shown = true
1069
1225
  @file_panel.refresh
@@ -1071,6 +1227,7 @@ class VMAgui
1071
1227
 
1072
1228
  def hide_file_panel
1073
1229
  return unless @file_panel_shown
1230
+ @file_panel_width = @file_panel_pane.position
1074
1231
  inner = @file_panel_pane.end_child
1075
1232
  @file_panel_pane.set_start_child(nil)
1076
1233
  @file_panel_pane.set_end_child(nil)
@@ -1078,8 +1235,11 @@ class VMAgui
1078
1235
  @file_panel_shown = false
1079
1236
  end
1080
1237
 
1238
+ # The shown/hidden state is remembered across restarts (see
1239
+ # save_window_layout). Tests toggle the panel too; they must not clobber it.
1081
1240
  def toggle_file_panel
1082
1241
  @file_panel_shown ? hide_file_panel : show_file_panel
1242
+ save_window_layout
1083
1243
  end
1084
1244
 
1085
1245
  def func_panel_init
@@ -1104,12 +1264,7 @@ class VMAgui
1104
1264
  inner = @windows[1][:overlay]
1105
1265
  @minibuf_vpane.set_start_child(nil)
1106
1266
  end
1107
- @func_panel_pane = Gtk::Paned.new(:horizontal)
1108
- @func_panel_pane.hexpand = true
1109
- @func_panel_pane.vexpand = true
1110
- @func_panel_pane.set_start_child(@func_panel.widget)
1111
- @func_panel_pane.set_end_child(inner)
1112
- @func_panel_pane.set_position(160)
1267
+ @func_panel_pane = make_side_pane(@func_panel.widget, inner, @func_panel_width || PANEL_DEFAULT_WIDTH[:func_panel])
1113
1268
  if @file_panel_shown
1114
1269
  @file_panel_pane.set_end_child(@func_panel_pane)
1115
1270
  else
@@ -1121,6 +1276,7 @@ class VMAgui
1121
1276
 
1122
1277
  def hide_func_panel
1123
1278
  return unless @func_panel_shown
1279
+ @func_panel_width = @func_panel_pane.position
1124
1280
  inner = @func_panel_pane.end_child
1125
1281
  @func_panel_pane.set_start_child(nil)
1126
1282
  @func_panel_pane.set_end_child(nil)
@@ -1134,5 +1290,333 @@ class VMAgui
1134
1290
 
1135
1291
  def toggle_func_panel
1136
1292
  @func_panel_shown ? hide_func_panel : show_func_panel
1293
+ save_window_layout
1294
+ end
1295
+
1296
+ def keylog_panel_init
1297
+ @keylog_panel = KeyLogPanel.new
1298
+ @keylog_shown = false
1299
+ end
1300
+
1301
+ # The key log wraps @minibuf_vpane at the grid level (right side), so it is
1302
+ # independent of the file/func panel and two-column nesting, which only
1303
+ # touch @minibuf_vpane's start child.
1304
+ def show_keylog_panel
1305
+ return if @keylog_shown
1306
+ @vbox.remove(@minibuf_vpane)
1307
+ @keylog_pane = Gtk::Paned.new(:horizontal)
1308
+ @keylog_pane.hexpand = true
1309
+ @keylog_pane.vexpand = true
1310
+ @keylog_pane.set_start_child(@minibuf_vpane)
1311
+ @keylog_pane.set_end_child(@keylog_panel.widget)
1312
+ @keylog_pane.resize_start_child = true # editor absorbs window resize
1313
+ @keylog_pane.resize_end_child = false # log keeps its width
1314
+ @keylog_pane.shrink_end_child = false
1315
+ @vbox.attach(@keylog_pane, 0, 2, 2, 1)
1316
+ @keylog_shown = true
1317
+ @keylog_panel.set_active(true)
1318
+ # Always set a position: left unset, GtkPaned sizes the panel from the
1319
+ # natural (unwrapped) width of its labels, so long entries widen it
1320
+ set_keylog_width(@keylog_width || PANEL_DEFAULT_WIDTH[:keylog])
1321
+ end
1322
+
1323
+ def hide_keylog_panel
1324
+ return unless @keylog_shown
1325
+ @keylog_width = keylog_width
1326
+ @keylog_panel.set_active(false)
1327
+ @keylog_pane.set_start_child(nil)
1328
+ @keylog_pane.set_end_child(nil)
1329
+ @vbox.remove(@keylog_pane)
1330
+ @vbox.attach(@minibuf_vpane, 0, 2, 2, 1)
1331
+ @keylog_shown = false
1332
+ end
1333
+
1334
+ def toggle_keylog_panel
1335
+ @keylog_shown ? hide_keylog_panel : show_keylog_panel
1336
+ save_window_layout
1337
+ end
1338
+
1339
+ # ── Window geometry and view layout, restored on startup ─────────────────
1340
+ # doc/TODO.md: "Restore window geometry and view layout on startup".
1341
+ # Saved by Editor#save_state (shutdown, restart) and when a column or panel
1342
+ # is toggled, to get_data_path("window_layout"): one layout per screen
1343
+ # resolution ("1920x1080@1" = logical monitor size @ scale factor), the
1344
+ # LAYOUT_KEEP most recently used. With no layout for the current resolution
1345
+ # the most recent one is fitted to the screen (fit_view_layout). GTK4 can't
1346
+ # place windows on Wayland, so only the size and maximized state are kept.
1347
+ # Test runs neither read nor write it; cnf.restore_layout = false turns it off.
1348
+
1349
+ LAYOUT_FILE = "window_layout"
1350
+ LAYOUT_KEEP = 5
1351
+ EDITOR_MIN_WIDTH = 300
1352
+ WINDOW_MIN_SIZE = 600
1353
+ DEFAULT_WINDOW_SIZE = [1000, 800]
1354
+ PANEL_DEFAULT_WIDTH = { :file_panel => 180, :func_panel => 160, :keylog => 320 }
1355
+ SIDE_PANELS = [:file_panel, :func_panel, :keylog]
1356
+
1357
+ def layout_enabled?
1358
+ !vma_test_mode? && cnf.restore_layout? != false
1359
+ end
1360
+
1361
+ # Monitor the window is on. Before the window is mapped that isn't known:
1362
+ # then the first monitor (layout_recheck_monitor corrects a wrong guess).
1363
+ def layout_monitor
1364
+ d = Gdk::Display.default
1365
+ surface = (@window.native&.surface rescue nil)
1366
+ m = (d.get_monitor_at_surface(surface) rescue nil) if surface
1367
+ m || (d.monitors.get_item(0) rescue nil)
1368
+ end
1369
+
1370
+ def monitor_key(m)
1371
+ return "unknown" if m.nil?
1372
+ g = m.geometry
1373
+ "#{g.width}x#{g.height}@#{m.scale_factor}"
1374
+ end
1375
+
1376
+ def monitor_size(m)
1377
+ return [100_000, 100_000] if m.nil?
1378
+ g = m.geometry
1379
+ [g.width, g.height]
1380
+ end
1381
+
1382
+ def file_panel_width
1383
+ @file_panel_shown ? @file_panel_pane.position : @file_panel_width
1384
+ end
1385
+
1386
+ def func_panel_width
1387
+ @func_panel_shown ? @func_panel_pane.position : @func_panel_width
1388
+ end
1389
+
1390
+ def keylog_width
1391
+ return @keylog_width unless @keylog_shown && @keylog_pane.width > 0
1392
+ @keylog_pane.width - @keylog_pane.position
1393
+ end
1394
+
1395
+ # The key log is on the right: its divider position depends on the total
1396
+ # width, which is known only once the pane has been allocated.
1397
+ def set_keylog_width(w)
1398
+ tries = 0
1399
+ GLib::Timeout.add(50) {
1400
+ if !@keylog_shown
1401
+ false
1402
+ elsif @keylog_pane.width > 0
1403
+ @keylog_pane.position = [@keylog_pane.width - w, EDITOR_MIN_WIDTH].max
1404
+ false
1405
+ else
1406
+ (tries += 1) < 100
1407
+ end
1408
+ }
1409
+ end
1410
+
1411
+ # Current geometry and layout as a Hash (see apply_view_layout). Widths are
1412
+ # in pixels, plus as a fraction of the window width (:frac) for a screen of
1413
+ # another size.
1414
+ def window_layout
1415
+ max = @window.maximized?
1416
+ # While maximized, the default size is the size to unmaximize to
1417
+ width, height = max ? [@window.default_width, @window.default_height] : [@window.width, @window.height]
1418
+ win_w = @window.width
1419
+ part = ->(shown, w) {
1420
+ { :shown => !!shown, :width => w, :frac => (w && win_w > 0 ? w.to_f / win_w : nil) }
1421
+ }
1422
+ w2 = @two_column && @windows[2] && @windows[2][:sw].child
1423
+ {
1424
+ :width => width, :height => height, :maximized => max, :win_width => win_w,
1425
+ :file_panel => part.(@file_panel_shown, file_panel_width),
1426
+ :func_panel => part.(@func_panel_shown, func_panel_width),
1427
+ :keylog => part.(@keylog_shown, keylog_width),
1428
+ :two_column => part.(@two_column, @two_column ? @pane.position : @column_split),
1429
+ :column2_file => (w2.respond_to?(:bufo) ? w2.bufo.fname : nil),
1430
+ :minibuf_history_height => @minibuf_history_height,
1431
+ }
1432
+ end
1433
+
1434
+ def load_layouts
1435
+ f = get_data_path(LAYOUT_FILE)
1436
+ h = File.exist?(f) ? Marshal.load(File.binread(f)) : {}
1437
+ h.class == Hash ? h : {}
1438
+ rescue => ex
1439
+ debug "load_layouts failed: #{ex}"
1440
+ {}
1441
+ end
1442
+
1443
+ # all with layout l stored under key, keeping the LAYOUT_KEEP most recent
1444
+ def merge_layouts(all, key, l, now = Time.now.to_f)
1445
+ all = all.merge(key => l.merge(:used => now))
1446
+ all.sort_by { |_k, v| -v[:used].to_f }.first(LAYOUT_KEEP).to_h
1447
+ end
1448
+
1449
+ def save_window_layout
1450
+ return if !layout_enabled? || @window.nil?
1451
+ all = merge_layouts(load_layouts, monitor_key(layout_monitor), window_layout)
1452
+ File.binwrite(get_data_path(LAYOUT_FILE), Marshal.dump(all))
1453
+ rescue => ex
1454
+ debug "save_window_layout failed: #{ex}"
1455
+ end
1456
+
1457
+ # Layout to start with on a key/mw x mh monitor: the one saved for this
1458
+ # resolution, else the most recently used one fitted to this screen.
1459
+ # Never nil: with nothing saved, the default window and no panels.
1460
+ def pick_layout(all, key, mw, mh)
1461
+ return fit_view_layout(all[key], mw, mh, true) if all[key]
1462
+ recent = all.values.max_by { |v| v[:used].to_f }
1463
+ if recent.nil? && vma.marshal_load("file_panel_shown", false)
1464
+ recent = { :file_panel => { :shown => true } } # saved by older versions
1465
+ end
1466
+ fit_view_layout(recent || {}, mw, mh, recent.nil?)
1467
+ end
1468
+
1469
+ # Fit saved layout l to a mw x mh monitor. exact = saved for this
1470
+ # resolution: only clamp to the monitor. Otherwise (layout from another
1471
+ # screen) limit the window to 90% of the monitor, don't maximize if the
1472
+ # saved window was bigger than the screen, and scale the widths by their
1473
+ # fraction of the window width. Side panels that would leave the editor
1474
+ # narrower than EDITOR_MIN_WIDTH are hidden.
1475
+ def fit_view_layout(saved, mw, mh, exact)
1476
+ l = Marshal.load(Marshal.dump(saved || {}))
1477
+ w, h = l[:width], l[:height]
1478
+ if w.to_i <= 0 || h.to_i <= 0
1479
+ # First start: the default size, leaving some room on a small screen
1480
+ # (a window nearly as big as the screen gets maximized by e.g. GNOME)
1481
+ w, h = DEFAULT_WINDOW_SIZE
1482
+ exact = false
1483
+ end
1484
+ if exact
1485
+ w, h = [w, mw].min, [h, mh].min
1486
+ else
1487
+ l[:maximized] = false if w > mw || h > mh
1488
+ w, h = [w, (mw * 0.9).to_i].min, [h, (mh * 0.9).to_i].min
1489
+ end
1490
+ l[:width] = [w, [WINDOW_MIN_SIZE, mw].min].max
1491
+ l[:height] = [h, [WINDOW_MIN_SIZE, mh].min].max
1492
+ l[:maximized] = !!l[:maximized]
1493
+
1494
+ # Width the window will have. Maximized, that's the monitor minus panels
1495
+ # and docks (e.g. GNOME's), which isn't known here: approximately mw.
1496
+ eff = l[:maximized] ? mw : l[:width]
1497
+ # Saved on this resolution: the pixel widths are right as they are
1498
+ same = exact || eff == l[:win_width]
1499
+ (SIDE_PANELS + [:two_column]).each { |k|
1500
+ p = (l[k] ||= {})
1501
+ p[:shown] = !!p[:shown]
1502
+ p[:width] = (p[:frac] * eff).round if !same && p[:frac] && p[:width]
1503
+ }
1504
+ side = ->() {
1505
+ SIDE_PANELS.sum { |k| l[k][:shown] ? (l[k][:width] || PANEL_DEFAULT_WIDTH[k]) : 0 }
1506
+ }
1507
+ [:keylog, :func_panel, :file_panel].each { |k|
1508
+ break if eff - side.() >= EDITOR_MIN_WIDTH
1509
+ l[k][:shown] = false
1510
+ }
1511
+ # Split of the two columns: inside the editor area
1512
+ sp = l[:two_column][:width]
1513
+ l[:two_column][:width] = nil if sp && sp >= eff - side.() - 100
1514
+ l
1515
+ end
1516
+
1517
+ # Before the window is first shown: pick the layout for this screen and
1518
+ # size the window (GTK ignores a new default size once it is mapped).
1519
+ # The columns and panels come later, in restore_view_layout.
1520
+ def apply_startup_window_size
1521
+ m = layout_monitor
1522
+ mw, mh = monitor_size(m)
1523
+ @startup_monitor_key = monitor_key(m)
1524
+ l = layout_enabled? ? pick_layout(load_layouts, @startup_monitor_key, mw, mh) : fit_view_layout({}, mw, mh, true)
1525
+ @startup_layout = l
1526
+ @window.set_size_request([WINDOW_MIN_SIZE, mw].min, [WINDOW_MIN_SIZE, mh].min)
1527
+ @window.set_default_size(l[:width], l[:height])
1528
+ @window.maximize if l[:maximized]
1529
+ end
1530
+
1531
+ # On the first map the real monitor is known. If it isn't the one guessed
1532
+ # in apply_startup_window_size, use the layout for the real one.
1533
+ def layout_recheck_monitor
1534
+ return if @layout_rechecked || !layout_enabled?
1535
+ @layout_rechecked = true
1536
+ GLib::Timeout.add(300) {
1537
+ m = layout_monitor
1538
+ key = monitor_key(m)
1539
+ if key != @startup_monitor_key
1540
+ mw, mh = monitor_size(m)
1541
+ l = pick_layout(load_layouts, key, mw, mh)
1542
+ @startup_layout = l
1543
+ l[:maximized] ? @window.maximize : @window.set_default_size(l[:width], l[:height])
1544
+ apply_view_layout(l) if @view_layout_restored
1545
+ end
1546
+ undo_auto_maximize
1547
+ false
1548
+ }
1549
+ end
1550
+
1551
+ # GNOME (mutter, org.gnome.mutter auto-maximize) maximizes a new window that
1552
+ # covers most of the screen, so a saved window only a little smaller than
1553
+ # the screen would come back maximized, and be saved so at quit. For a
1554
+ # while after the first map: if the layout says not maximized but the
1555
+ # window is, unmaximize it to the saved size.
1556
+ def undo_auto_maximize
1557
+ l = @startup_layout
1558
+ return if l.nil? || l[:maximized]
1559
+ tries = 0
1560
+ GLib::Timeout.add(100) {
1561
+ if @window.maximized?
1562
+ @window.unmaximize
1563
+ # Unmaximizing is asynchronous: set the size once it's done, or the
1564
+ # window manager's own unmaximized size wins
1565
+ waits = 0
1566
+ GLib::Timeout.add(50) {
1567
+ if !@window.maximized?
1568
+ @window.set_default_size(l[:width], l[:height])
1569
+ false
1570
+ else
1571
+ (waits += 1) < 60 # 3 s
1572
+ end
1573
+ }
1574
+ false
1575
+ else
1576
+ (tries += 1) < 20 # 2 s
1577
+ end
1578
+ }
1579
+ end
1580
+
1581
+ # Called at startup once the first buffer exists: columns and panels.
1582
+ def restore_view_layout
1583
+ return if !layout_enabled? || @startup_layout.nil?
1584
+ apply_view_layout(@startup_layout)
1585
+ @view_layout_restored = true
1586
+ end
1587
+
1588
+ # Show the columns and panels of layout l (from window_layout or
1589
+ # fit_view_layout) with their widths. Columns go first: show_file_panel /
1590
+ # show_func_panel wrap whatever is in the editor area.
1591
+ def apply_view_layout(l)
1592
+ got = ->(k) { l[k] || {} }
1593
+ @column_split = got.(:two_column)[:width] || @column_split
1594
+ @file_panel_width = got.(:file_panel)[:width] || @file_panel_width
1595
+ @func_panel_width = got.(:func_panel)[:width] || @func_panel_width
1596
+ @keylog_width = got.(:keylog)[:width] || @keylog_width
1597
+ @minibuf_history_height = l[:minibuf_history_height] if l[:minibuf_history_height]
1598
+
1599
+ got.(:two_column)[:shown] ? set_two_column : set_one_column
1600
+ got.(:func_panel)[:shown] ? show_func_panel : hide_func_panel
1601
+ got.(:file_panel)[:shown] ? show_file_panel : hide_file_panel
1602
+ got.(:keylog)[:shown] ? show_keylog_panel : hide_keylog_panel
1603
+ # Already shown panels keep their old widths otherwise
1604
+ @pane.set_position(@column_split) if @two_column && @column_split.to_i > 0
1605
+ @file_panel_pane.set_position(@file_panel_width) if @file_panel_shown && @file_panel_width.to_i > 0
1606
+ @func_panel_pane.set_position(@func_panel_width) if @func_panel_shown && @func_panel_width.to_i > 0
1607
+ set_keylog_width(@keylog_width) if @keylog_shown && @keylog_width.to_i > 0
1608
+ @column2_file = l[:column2_file]
1609
+ restore_column2_buffer
1610
+ end
1611
+
1612
+ # Put the file that was in the second column back there, once it is open
1613
+ # (the session's files load after the layout; see restore_session_files).
1614
+ def restore_column2_buffer
1615
+ return if !@two_column || @column2_file.nil?
1616
+ id = vma.buffers.get_buffer_by_filename(@column2_file)
1617
+ return if id.nil? || id == vma.buf.id
1618
+ set_buffer_to_window(id, 2)
1619
+ @column2_file = nil
1137
1620
  end
1138
1621
  end
1622
+ end # module Vimamsa