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
@@ -1,9 +1,13 @@
1
1
  require "pty"
2
2
 
3
+ module Vimamsa
4
+
3
5
 
4
6
  class Editor
5
7
  attr_reader :file_content_search_paths, :file_name_search_paths, :gui, :hook, :macro, :actions
6
- attr_accessor :converters, :fh, :paint_stack, :kbd, :langsrv, :register, :cur_register, :clipboard
8
+ # @!attribute [rw] kbd
9
+ # @return [KeyBindingTree]
10
+ attr_accessor :converters, :fh, :bookmarks, :paint_stack, :kbd, :langsrv, :registers, :clipboard, :dot
7
11
  #attr_writer :call_func, :update_highlight
8
12
 
9
13
  def initialize()
@@ -20,19 +24,14 @@ class Editor
20
24
  @paint_stack = []
21
25
  @_plugins = {}
22
26
  @errors
23
- @register = Hash.new("")
24
- @cur_register = "a"
27
+ @registers = Registers.new
28
+ @dot = DotRepeat.new
25
29
  @clipboard = Clipboard.new
26
30
  end
27
31
 
32
+ # "x: use register char for the next copy, delete or put (registers.rb)
28
33
  def set_register(char)
29
- @cur_register = char
30
- message("Set register #{char}")
31
- end
32
-
33
- def paste_register(char)
34
- $c = @cur_register #TODO:??
35
- message("Paste: #{$c}")
34
+ @registers.select(char)
36
35
  end
37
36
 
38
37
  def open_file_listener(added)
@@ -43,18 +42,116 @@ class Editor
43
42
  File.delete(fp)
44
43
  for f in x.lines
45
44
  f.gsub!("\n", "")
46
- if File.exist?(f)
47
- if file_is_text_file(f)
48
- jump_to_file(f)
45
+ fpath = f.dup
46
+ if !File.exist?(fpath)
47
+ GLib::Idle.add do
48
+ Gui.confirm("File does not exist. Create new file?\r #{fpath}",
49
+ proc {
50
+ FileUtils.mkdir_p(File.dirname(fpath))
51
+ IO.write(fpath, "")
52
+ jump_to_file(fpath)
53
+ })
54
+ false
49
55
  end
56
+ else
57
+ # open_new_file reports files it cannot open (e.g. not text)
58
+ GLib::Idle.add { jump_to_file(fpath); false }
50
59
  end
51
60
  end
52
61
  end
53
62
  end
54
63
  end
55
64
 
65
+ # Connect the host-agnostic KeyBindingTree to this editor: action dispatch,
66
+ # logging, and the side effects of mode/state changes (undo grouping,
67
+ # autocomplete dismissal, mode stack sync, cursor redraw, GUI badge/keylog).
68
+ def setup_kbd_handlers
69
+ @kbd.action_handler = method(:exec_action)
70
+ @kbd.logger = method(:debug)
71
+ @kbd.fatal_handler = method(:fatal_error)
72
+ # Non-fatal binding problems (e.g. custom.rb binding to an unknown mode).
73
+ # message() can fire before the minibuffer exists (custom.rb is loaded
74
+ # during start), so it must not be allowed to break startup.
75
+ @kbd.error_handler = proc { |msg|
76
+ warn msg
77
+ begin
78
+ message(msg)
79
+ rescue Exception
80
+ end
81
+ }
82
+ @kbd.on_action_error = proc { |action, e|
83
+ if e.is_a?(SyntaxError)
84
+ message("SYNTAX ERROR with eval cmd #{action}: " + e.to_s)
85
+ else
86
+ puts "BACKTRACE"
87
+ puts e.backtrace
88
+ puts e.inspect
89
+ puts "BACKTRACE END"
90
+ if e.is_a?(SystemExit)
91
+ exit
92
+ else
93
+ crash("Error with action: #{action}: ", e)
94
+ end
95
+ end
96
+ }
97
+
98
+ # A mode change starts a new undo step, except inside a group run as one
99
+ # step (a macro, a "." repeat)
100
+ @kbd.on(:mode_set) { |label|
101
+ b = vma.buf
102
+ b.new_undo_group if b && !b.instance_variable_get(:@macro_group_active)
103
+ }
104
+ @kbd.on(:mode_changed) { |label|
105
+ if label != :insert
106
+ # Dismiss the autocomplete popup when leaving insert mode
107
+ v = (vma.buf&.view rescue nil)
108
+ v.hide_completions if v.respond_to?(:hide_completions)
109
+ end
110
+ if @kbd.get_scope != :editor and !vma.buf.nil?
111
+ vma.buf.mode_stack = @kbd.default_mode_stack.clone
112
+ end
113
+ vma.gui.view.draw_cursor() if !vma.gui.view.nil?
114
+ }
115
+ @kbd.on(:state_trail_changed) { |badge, badge_key, trail|
116
+ vma.gui.update_mode_badge(badge, badge_key, trail)
117
+ }
118
+ @kbd.on(:key_no_match) { |c, trail| vma.gui.keylog_panel&.log_nomatch(c, trail) }
119
+ @kbd.on(:key_pending) { |trail| vma.gui.keylog_panel&.log_pending(trail) }
120
+ @kbd.on(:key_action) { |trail, action|
121
+ vma.gui.keylog_panel&.log_action(trail, action)
122
+ @dot.before(action) # "." (dot_repeat.rb)
123
+ }
124
+ @kbd.on(:action_executed) { |action, ret|
125
+ if vma.macro.is_recording and ret != false
126
+ debug "RECORD ACTION:#{action}", 2
127
+ vma.macro.record_action(action)
128
+ end
129
+ }
130
+ @kbd.on(:action_handled) { |trail, action|
131
+ vma.gui.show_action_trail(trail, action)
132
+ # A "x register applies to the next command only (counts may come
133
+ # between: "a3yy)
134
+ a = action.to_s
135
+ @registers.clear_pending if !a.include?("edit.register!") && !a.include?("kbd.count.digit")
136
+ @dot.after(action)
137
+ }
138
+ end
139
+
140
+ # Moves the files of older versions to their new places, once
141
+ # (UserDirs.migrate). Only with the default dirs: with --user-dir (tests,
142
+ # --tmp-user-dir) config and data share one dir and nothing moves.
143
+ def migrate_user_files
144
+ return if !UserDirs.default_config_dir?(vma_user_dir)
145
+ UserDirs.migrate(config_dir: vma_user_dir, data_dir: vma_data_dir).each { |from, to|
146
+ puts "vimamsa: moved #{from} to #{to}"
147
+ }
148
+ rescue => e
149
+ debug "migrate_user_files: #{e}"
150
+ end
151
+
56
152
  def start
57
153
  @gui = $vmag #TODO
154
+ migrate_user_files
58
155
 
59
156
  $hook = Hook.new
60
157
  @hook = $hook
@@ -69,9 +166,35 @@ class Editor
69
166
  register_plugin(:Search, $search)
70
167
 
71
168
  # build_key_bindings_tree
72
- @kbd = KeyBindingTree.new()
73
- $kbd = @kbd #TODO: remove global
74
- require "vimamsa/key_bindings_vimlike"
169
+ @kbd = KeyBindingTree.new(actions: @actions)
170
+ setup_kbd_handlers
171
+
172
+ # Load user settings early so the keybinding scheme choice is available
173
+ # before the binding tree is populated. custom.rb still loads later.
174
+ settings_path = get_dot_path("settings.rb")
175
+ if File.exist?(settings_path)
176
+ begin
177
+ eval(IO.read(settings_path))
178
+ rescue Exception => e
179
+ message("Error in settings.rb: #{e}")
180
+ end
181
+ end
182
+
183
+ scheme = cnf.keybindings.scheme! || "vimlike"
184
+ # Tests assume the default scheme regardless of user settings;
185
+ # test files load scheme overrides themselves when needed.
186
+ scheme = "vimlike" if ARGV.include?("--test")
187
+ case scheme
188
+ when "notepad"
189
+ require "vimamsa/key_bindings_notepad"
190
+ when "vim"
191
+ require "vimamsa/key_bindings_vim"
192
+ else
193
+ require "vimamsa/key_bindings_vimlike"
194
+ end
195
+
196
+ # Form input mode and its keys, in every scheme (form_input.rb)
197
+ form_input_setup
75
198
 
76
199
  $buffers = BufferList.new
77
200
  $minibuffer = Buffer.new(">", "")
@@ -84,13 +207,13 @@ class Editor
84
207
  sleep(0.03)
85
208
 
86
209
  BufferManager.init
210
+ Bookmarks.init
87
211
 
88
212
  @gui.init_menu
89
213
 
90
214
  mkdir_if_not_exists(get_dot_path(""))
91
- mkdir_if_not_exists(get_dot_path("backup"))
92
- mkdir_if_not_exists(get_dot_path("testfoobar")) #TODO
93
- listen_dir = get_dot_path("listen")
215
+ mkdir_if_not_exists(get_data_path("backup"))
216
+ listen_dir = get_data_path("listen")
94
217
  mkdir_if_not_exists(listen_dir)
95
218
  listener = Listen.to(listen_dir) do |modified, added, removed|
96
219
  debug([modified: modified, added: added, removed: removed])
@@ -104,13 +227,16 @@ class Editor
104
227
  IO.write(custom_fn, example_custom)
105
228
  end
106
229
 
107
- settings_path = get_dot_path("settings.rb")
108
- if File.exist?(settings_path)
109
- eval(IO.read(settings_path))
110
- end
230
+ # Install desktop launcher + icon into ~/.local/share on first run.
231
+ install_desktop_integration
111
232
 
233
+ # Phase 1 of custom.rb: the body of the file. Runs before the inits below so
234
+ # that cnf.* set there is visible to them. Anything that needs modes/actions
235
+ # they register goes in phase 2 (hook_custom_after_init, called at the end of
236
+ # start; see call_custom_after_init).
112
237
  custom_script = read_file("", custom_fn)
113
- eval(custom_script) if custom_script
238
+ # bindings made by custom.rb get the keyword "custom" (key bindings page)
239
+ vma.kbd.with_keywords(["custom"]) { eval(custom_script) } if custom_script
114
240
 
115
241
  Grep.init
116
242
  FileManager.init
@@ -138,7 +264,8 @@ class Editor
138
264
  load main_file
139
265
  # Call <name>_init if the module defines it.
140
266
  init_fn = "#{mod_name}_init"
141
- send(init_fn) if respond_to?(init_fn, true)
267
+ # bindings made by the module get the keyword module:<name>
268
+ vma.kbd.with_keywords(["module:#{mod_name}"]) { send(init_fn) } if respond_to?(init_fn, true)
142
269
  end
143
270
  end
144
271
  end
@@ -148,11 +275,12 @@ class Editor
148
275
  fname_ = File.expand_path(cnf.startup_file!)
149
276
  if File.exist?(fname_)
150
277
  fname = fname_
278
+ @startup_fname = fname_
151
279
  end
152
280
  else
153
281
  # fname = ppath("demo.txt")
154
282
  end
155
- fname = ARGV[0] if ARGV.size >= 1 and File.file?(File.expand_path(ARGV[0]))
283
+ fname = ARGV[0] if ARGV.size >= 1 and File.file?(File.expand_path(ARGV[0])) and !ARGV.include?("--test")
156
284
  # vma.add_content_search_path(Dir.pwd)
157
285
  for fn in ARGV
158
286
  fn = File.expand_path(fn)
@@ -162,7 +290,7 @@ class Editor
162
290
  end
163
291
  end
164
292
 
165
- argv_has_files = ARGV.any? { |a| File.file?(File.expand_path(a)) }
293
+ argv_has_files = !ARGV.include?("--test") && ARGV.any? { |a| File.file?(File.expand_path(a)) }
166
294
 
167
295
  if fname
168
296
  open_new_file(fname)
@@ -170,25 +298,59 @@ class Editor
170
298
  create_new_buffer(file_contents = "\n")
171
299
  end
172
300
 
301
+ gui.restore_view_layout
302
+
173
303
  #Load plugins
174
304
  require "vimamsa/file_history.rb"
175
305
  @fh = FileHistory.new
176
306
  @_plugins[:FileHistory] = @fh
307
+ @bookmarks = Bookmarks.new
177
308
 
178
309
  register_plugin(:FileHistory, @fh)
179
310
  register_plugin(:FileFinder, FileFinder.new)
180
311
  # To access via vma.FileFinder
181
312
  # self.define_singleton_method(:FileFinder) { @_plugins[:FileFinder] }
182
313
 
183
- check_session_restore unless argv_has_files
314
+ # Not in test mode: the dialog grabs focus from the editor window, which
315
+ # breaks (and is never the subject of) an automated run.
316
+ check_session_restore unless argv_has_files || vma_test_mode?
184
317
 
185
318
  @hook.call(:after_init)
186
319
 
320
+ # Phase 2 of custom.rb: everything (modules, plugins, buffers) now exists.
321
+ call_custom_after_init
322
+
323
+ # Paint the initial mode badge (e.g. COMMAND). show_state_trail is otherwise
324
+ # only called from key handling, so without this the badge stays blank until
325
+ # the first keypress. set_state_to_root populates @state_trail the same way
326
+ # the key handler does before rendering.
327
+ @kbd.set_state_to_root
328
+ @kbd.show_state_trail
329
+
330
+ # The introduction page on the first start, or with --intro (intro_page.rb)
331
+ show_intro_on_startup
332
+
333
+ # AppImage: from here on, started programs get the user's environment
334
+ restore_host_env
335
+
187
336
  if ARGV.include?("--test")
188
337
  test_files = ARGV.select { |a| a.end_with?(".rb") && File.file?(a) }
189
338
  run_as_idle proc {
190
339
  test_files.each { |f| load f }
191
- success = run_vma_tests
340
+ classes = if $vma_test_class_filter
341
+ $vma_test_class_filter.filter_map { |name|
342
+ begin
343
+ Object.const_get(name)
344
+ rescue NameError
345
+ puts "Unknown test class: #{name}"
346
+ nil
347
+ end
348
+ }
349
+ else
350
+ []
351
+ end
352
+ success = run_vma_tests(*classes)
353
+ $vma_tests_ok = success # exe/run_tests.rb turns this into the exit code
192
354
  # Defer shutdown so GTK can drain all pending idle callbacks from test
193
355
  # teardown before widget destruction begins (avoids heap corruption).
194
356
  GLib::Timeout.add(300) { shutdown(); false }
@@ -227,8 +389,16 @@ class Editor
227
389
  end
228
390
  end
229
391
 
392
+ # marshal_save files the program generates, kept in the data dir; the
393
+ # others (bookmarks, named_macros) are the user's, in the config dir
394
+ DATA_VARS = %w[file_index file_history file_panel_shown macros]
395
+
396
+ def var_file_path(varname)
397
+ DATA_VARS.include?(varname.to_s) ? get_data_path(varname) : get_dot_path(varname)
398
+ end
399
+
230
400
  def save_var_to_file(varname, vardata)
231
- fn = get_dot_path(varname)
401
+ fn = var_file_path(varname)
232
402
  #TODO: check that save path is safe
233
403
  f = File.open(fn, "w")
234
404
  File.binwrite(f, vardata)
@@ -236,7 +406,7 @@ class Editor
236
406
  end
237
407
 
238
408
  def load_var_from_file(varname)
239
- fn = get_dot_path(varname)
409
+ fn = var_file_path(varname)
240
410
  if File.exist?(fn)
241
411
  vardata = IO.binread(fn)
242
412
  if vardata
@@ -258,7 +428,9 @@ class Editor
258
428
  @gui.quit
259
429
  end
260
430
 
431
+ # Window size and view layout (see VMAgui#save_window_layout)
261
432
  def save_state
433
+ @gui.save_window_layout
262
434
  end
263
435
 
264
436
  def save_session
@@ -266,13 +438,13 @@ class Editor
266
438
  .map { |b| b.fname }
267
439
  .compact
268
440
  .select { |f| File.exist?(f) }
269
- IO.write(get_dot_path("session.txt"), fnames.join("\n") + "\n")
441
+ IO.write(get_data_path("session.txt"), fnames.join("\n") + "\n")
270
442
  rescue => ex
271
443
  debug "save_session failed: #{ex}"
272
444
  end
273
445
 
274
446
  def check_session_restore
275
- session_path = get_dot_path("session.txt")
447
+ session_path = get_data_path("session.txt")
276
448
  return unless File.exist?(session_path)
277
449
 
278
450
  fnames = File.read(session_path).lines.map(&:strip).reject(&:empty?)
@@ -280,6 +452,13 @@ class Editor
280
452
  fnames.reject! { |f| vma.buffers.get_buffer_by_filename(f) }
281
453
  return if fnames.empty?
282
454
 
455
+ # After a restart (restart_editor) reopen the previous files silently.
456
+ if ENV["VIMAMSA_RESTORE_SESSION"]
457
+ restore_session_files(fnames)
458
+ message("Session restored: #{fnames.size} file(s)")
459
+ return
460
+ end
461
+
283
462
  n = fnames.size
284
463
  label = n == 1 ? "1 file" : "#{n} files"
285
464
  params = {
@@ -290,9 +469,7 @@ class Editor
290
469
  },
291
470
  :callback => proc { |x|
292
471
  if x["yes_btn"] == "submit"
293
- initial = vma.buffers.list.find { |b| b.fname.nil? }
294
- fnames.each { |f| load_buffer(f) }
295
- initial&.close
472
+ restore_session_files(fnames)
296
473
  message("Session restored: #{label}")
297
474
  end
298
475
  },
@@ -300,6 +477,18 @@ class Editor
300
477
  PopupFormGenerator.new(params).run
301
478
  end
302
479
 
480
+ def restore_session_files(fnames)
481
+ initial = vma.buffers.list.find { |b| b.fname.nil? }
482
+ fnames.each { |f| b = load_buffer(f); b&.needs_autosave_check = true }
483
+ initial&.close
484
+ # Loading the session switches to its last file; cnf.startup_file stays
485
+ # the buffer shown on startup.
486
+ if @startup_fname and (id = vma.buffers.get_buffer_by_filename(@startup_fname))
487
+ vma.buffers.set_current_buffer(id)
488
+ end
489
+ gui.restore_column2_buffer
490
+ end
491
+
303
492
  def add_content_search_path(pathstr)
304
493
  p = File.expand_path(pathstr)
305
494
  if !@file_content_search_paths.include?(p)
@@ -310,7 +499,9 @@ class Editor
310
499
  # Register converter
311
500
  def reg_conv(converter, converter_id)
312
501
  @converters[converter_id] = converter
313
- reg_act(converter_id, proc { vma.buf.convert_selected_text(converter_id) }, "Converter #{converter_id}", { :scope => [:selection] })
502
+ id = "sel.convert.#{converter_id}"
503
+ ACTION_ALIASES[converter_id.to_sym] = id # the old action id was the converter id
504
+ reg_act(id, proc { vma.buf.convert_selected_text(converter_id) }, "Converter #{converter_id}", { :scope => [:selection] })
314
505
  end
315
506
 
316
507
  def apply_conv(converter_id, txt)
@@ -368,30 +559,6 @@ def open_file_dialog()
368
559
  gui_open_file_dialog(File.dirname(path))
369
560
  end
370
561
 
371
- class Clipboard
372
- def initialize
373
- @clipboard = []
374
- end
375
-
376
- def set(s)
377
- if !(s.class <= String) or s.size == 0
378
- debug s.inspect
379
- debug [s, s.class, s.size]
380
- log_error("s.class != String or s.size == 0")
381
- return
382
- end
383
- @clipboard << s
384
- set_system_clipboard(s)
385
- vma.register[vma.cur_register] = s
386
- debug "SET CLIPBOARD: [#{s}]"
387
- debug "REGISTER: #{vma.cur_register}:#{vma.register[vma.cur_register]}"
388
- end
389
-
390
- def get()
391
- return @clipboard[-1]
392
- end
393
- end
394
-
395
562
  def set_cursor_pos(new_pos)
396
563
  buf.set_pos(new_pos)
397
564
  #render_buffer(vma.buf)
@@ -530,7 +697,12 @@ def load_buffer(fname)
530
697
  return nil
531
698
  end
532
699
  debug("LOAD BUFFER: #{fname}")
533
- buffer = Buffer.new(read_file("", fname), fname)
700
+ content, fmt = read_file_with_format(fname)
701
+ buffer = Buffer.new(content, fname)
702
+ buffer.file_format = fmt
703
+ if buffer.file_format_desc
704
+ message("#{File.basename(fname)} is #{buffer.file_format_desc}; #{buffer.file_format_save_note}")
705
+ end
534
706
  # gui_set_current_buffer(buffer.id)
535
707
  buffer.set_active
536
708
  debug("DONE LOAD: #{fname}")
@@ -562,9 +734,14 @@ def jump_to_file(filename, tnum = nil, charn = nil)
562
734
  end
563
735
  end
564
736
 
565
- #TODO: needed?
566
- def open_existing_file(filename)
567
- open_new_file(filename)
737
+ def do_open_file(filename)
738
+ message "New file opened: #{filename}"
739
+ bu = load_buffer(filename)
740
+ vma.buffers.set_current_buffer_by_id(bu.id)
741
+ # The repo/working dir is full of *_vma_autosave files; prompting about them
742
+ # mid-test steals the keyboard. tests/test_autosave.rb calls
743
+ # check_autosave_load directly, so the prompt itself stays covered.
744
+ bu.check_autosave_load unless vma_test_mode?
568
745
  end
569
746
 
570
747
  def open_new_file(filename, file_contents = "")
@@ -576,25 +753,28 @@ def open_new_file(filename, file_contents = "")
576
753
  message "Switching to: #{filename}"
577
754
  bu = vma.buffers.set_current_buffer(b)
578
755
  else
579
- if !is_path_writable(filename)
580
- message("Path #{filename} cannot be written to")
581
- return false
582
- elsif !File.exist?(filename)
756
+ if !File.exist?(filename)
583
757
  message("File #{filename} does not exist")
584
758
  return false
585
759
  elsif !file_is_text_file(filename)
586
760
  message("File #{filename} does not contain text")
587
761
  return false
588
762
  end
763
+ message("Note: #{filename} is read-only") unless is_path_writable(filename)
589
764
  if Encrypt.is_encrypted?(filename)
590
765
  decrypt_dialog(filename: filename)
591
766
  return nil
592
767
  end
593
- message "New file opened: #{filename}"
594
- fname = filename
595
- bu = load_buffer(fname)
596
- vma.buffers.set_current_buffer_by_id(bu.id)
597
- bu.check_autosave_load
768
+ if File.size(filename) > 1_000_000
769
+ size_mb = File.size(filename) / 1_000_000.0
770
+ Gui.confirm("Warning: #{filename}\nis large (#{format("%.1f", size_mb)} MB).\nOpen anyway?",
771
+ proc { do_open_file(filename) })
772
+ return nil
773
+ end
774
+ do_open_file(filename)
775
+ # get_buffer_by_filename returns a buffer id, not the Buffer; resolve it so
776
+ # callers (e.g. jump_to_file) get a Buffer to act on.
777
+ bu = vma.buffers.get_buffer_by_id(vma.buffers.get_buffer_by_filename(filename))
598
778
  end
599
779
  return bu
600
780
  end
@@ -605,13 +785,36 @@ def scan_word_start_marks(search_str)
605
785
  return wsmarks
606
786
  end
607
787
 
788
+ # Base directory for user-edited config (settings.rb, custom.rb, styles,
789
+ # bookmarks, named macros): $XDG_CONFIG_HOME/vimamsa, by default
790
+ # ~/.config/vimamsa (user_dirs.rb). Can be overridden with the --user-dir
791
+ # switch or by setting $vma_user_dir before startup. Tests point this at an
792
+ # isolated directory so they never read or write the real user config.
793
+ def vma_user_dir
794
+ $vma_user_dir ||= UserDirs.default_config_dir
795
+ end
796
+
608
797
  def get_dot_path(sfx)
609
- dot_dir = File.expand_path("~/.config/vimamsa")
798
+ dot_dir = vma_user_dir
610
799
  FileUtils.mkdir_p(dot_dir) unless File.exist?(dot_dir)
611
800
  dpath = "#{dot_dir}/#{sfx}"
612
801
  return dpath
613
802
  end
614
803
 
804
+ # Base directory for files the program generates (session, histories,
805
+ # backups, crash reports, listen dir, window layout, ...):
806
+ # $XDG_DATA_HOME/vimamsa, by default ~/.local/share/vimamsa. When the user
807
+ # dir was moved (--user-dir, tests), data goes there too, so those runs stay
808
+ # isolated (user_dirs.rb).
809
+ def vma_data_dir
810
+ $vma_data_dir ||= UserDirs.data_dir_for(vma_user_dir)
811
+ end
812
+
813
+ def get_data_path(sfx)
814
+ FileUtils.mkdir_p(vma_data_dir) unless File.exist?(vma_data_dir)
815
+ "#{vma_data_dir}/#{sfx}"
816
+ end
817
+
615
818
  def get_file_line_pointer(s)
616
819
  #"/code/vimamsa/lib/vimamsa/buffer_select.rb:31:def"
617
820
  # m = s.match(/(~[a-z]*)?\/.*\//)
@@ -671,14 +874,59 @@ def find_project_dir_of_cur_buffer()
671
874
  return pdir
672
875
  end
673
876
 
877
+ # Quit and relaunch the editor: reloads settings.rb/custom.rb and reopens the
878
+ # files that were open (via the saved session), all automatically. Replaces the
879
+ # current process with exec, so the same window is torn down as the new one comes up.
880
+ def restart_editor
881
+ vma.save_session
882
+ vma.gui.save_window_layout
883
+ vma.hook.call(:shutdown)
884
+ ruby = RbConfig.ruby
885
+ script = $PROGRAM_NAME
886
+ message("Restarting...")
887
+ # Signal the new instance to restore the session without prompting.
888
+ env = { "VIMAMSA_RESTORE_SESSION" => "1" }
889
+ # The same user dir: a --tmp-user-dir one (kept, deleted by the new
890
+ # process at quit) or a --user-dir one
891
+ args = []
892
+ if $vma_tmp_user_dir
893
+ env[TmpUserDir::ENV_KEY] = $vma_tmp_user_dir
894
+ elsif !UserDirs.default_config_dir?(vma_user_dir)
895
+ args = ["--user-dir", vma_user_dir]
896
+ end
897
+ exec(env, ruby, script, *args)
898
+ rescue => ex
899
+ message("Restart failed: #{ex}")
900
+ end
901
+
674
902
  def reload_customrb
675
903
  custom_fn = get_dot_path("custom.rb")
676
904
  custom_script = read_file("", custom_fn)
677
- eval(custom_script) if custom_script
905
+ vma.kbd.with_keywords(["custom"]) { eval(custom_script) } if custom_script
906
+ call_custom_after_init # everything is loaded already: run phase 2 right away
907
+ gui_refresh_colors # apply any cnf.theme.colors/cyber overrides live
678
908
  message("Reloaded #{custom_fn}")
679
909
  end
680
910
 
681
- def save_settings_to_file
911
+ # Phase 2 of custom.rb. The body of custom.rb is evaluated early during
912
+ # Editor#start, before the modes and actions of Grep/FileManager/Autocomplete
913
+ # and of the modules are registered — so that cnf.* from custom.rb is visible to
914
+ # them (e.g. cnf.fexp.experimental). Anything that depends on those, above all
915
+ # key bindings for their modes ("fexp ...", "grep ...", module minor modes),
916
+ # belongs in an optional hook_custom_after_init function, which is called from
917
+ # here once everything is loaded.
918
+ def call_custom_after_init
919
+ return if !respond_to?(:hook_custom_after_init, true)
920
+ begin
921
+ vma.kbd.with_keywords(["custom"]) { send(:hook_custom_after_init) }
922
+ rescue Exception => e
923
+ warn e.inspect
924
+ warn e.backtrace
925
+ message("Error in hook_custom_after_init: #{e}")
926
+ end
927
+ end
928
+
929
+ def save_settings_to_file(quiet: false)
682
930
  settings_path = get_dot_path("settings.rb")
683
931
  lines = all_settings_defs.flat_map { |section| section[:settings] }.map do |s|
684
932
  key_str = "cnf." + s[:key].join(".")
@@ -686,7 +934,7 @@ def save_settings_to_file
686
934
  "#{key_str} = #{val.inspect}"
687
935
  end
688
936
  IO.write(settings_path, lines.join("\n") + "\n")
689
- message("Settings saved to #{settings_path}")
937
+ message("Settings saved to #{settings_path}") if !quiet
690
938
  end
691
939
 
692
940
  DEMO_FILES = ["demo.txt", "sheep.jpg", "README.md"]
@@ -716,3 +964,4 @@ def install_demo_files_callback(x)
716
964
  message("Demo files installed to #{dest_dir}")
717
965
  end
718
966
 
967
+ end # module Vimamsa