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,4 +1,7 @@
1
1
  require "openssl"
2
+ require "securerandom"
3
+
4
+ module Vimamsa
2
5
 
3
6
  def decrypt_dialog(filename:, wrong_pass: false)
4
7
  callback = proc { |x| Encrypt.open(filename, x) }
@@ -8,64 +11,109 @@ def decrypt_dialog(filename:, wrong_pass: false)
8
11
  end
9
12
 
10
13
  class Encrypt
14
+ HEADER_V1 = "VMACRYPT001"
15
+ HEADER_V2 = "VMACRYPT002"
16
+
17
+ PBKDF2_ITERATIONS = 600_000
18
+ PBKDF2_KEY_LEN = 32 # 256-bit key for AES-256
19
+ SALT_LEN = 16
20
+ NONCE_LEN = 12
21
+ TAG_LEN = 16
22
+
11
23
  def self.is_encrypted?(fn)
12
24
  debug "self.is_encrypted?(fn)", 2
13
25
  begin
14
26
  file = File.open(fn, "r")
15
- first_11_characters = file.read(11)
16
- return true if first_11_characters == "VMACRYPT001"
27
+ header = file.read(11)
28
+ return true if header == HEADER_V1 || header == HEADER_V2
17
29
  rescue Errno::ENOENT
18
- puts "File not found: #{file_path}"
30
+ puts "File not found: #{fn}"
19
31
  rescue => e
20
32
  puts "An error occurred: #{e.message}"
21
33
  ensure
22
34
  file&.close
23
35
  end
24
- return false
36
+ false
25
37
  end
26
38
 
27
39
  def self.open(fn, password)
28
40
  debug "open_encrypted(filename,password)", 2
29
- encrypted = read_file("", fn)[11..-1]
41
+ content = read_file("", fn)
42
+ header = content[0..10]
43
+ payload = content[11..-1]
30
44
  begin
31
45
  crypt = Encrypt.new(password)
32
- str = crypt.decrypt(encrypted)
33
- # debug "PASS OK!", 2
46
+ str = (header == HEADER_V1) ? crypt.decrypt_v1(payload) : crypt.decrypt_v2(payload)
34
47
  bu = create_new_buffer(str)
35
- bu.init_encrypted(crypt: crypt, filename: fn, encrypted: encrypted)
48
+ bu.init_encrypted(crypt: crypt, filename: fn, encrypted: payload)
36
49
  rescue OpenSSL::Cipher::CipherError => e
37
- # Wrong password
38
50
  decrypt_dialog(filename: fn, wrong_pass: true)
39
51
  end
40
52
  end
41
53
 
42
54
  def initialize(pass_phrase)
43
- salt = "uvgixEtU"
44
- @enc = OpenSSL::Cipher.new "AES-128-CBC"
45
- @enc.encrypt
46
- @enc.pkcs5_keyivgen pass_phrase, salt
47
- @dec = OpenSSL::Cipher.new "AES-128-CBC"
48
- @dec.decrypt
49
- @dec.pkcs5_keyivgen pass_phrase, salt
55
+ @pass_phrase = pass_phrase
56
+ # Lazy-init legacy cipher only when needed for V1 decryption
57
+ @dec_v1 = nil
50
58
  end
51
59
 
60
+ # Always produces V2 (AES-256-GCM) output. Returns uppercase hex payload.
52
61
  def encrypt(text)
53
- cipher = @enc
54
- encrypted = cipher.update text
55
- encrypted << cipher.final
56
- encrypted = encrypted.unpack("H*")[0].upcase
57
- @enc.reset
58
- return encrypted
62
+ salt = SecureRandom.random_bytes(SALT_LEN)
63
+ nonce = SecureRandom.random_bytes(NONCE_LEN)
64
+ key = derive_key(salt)
65
+
66
+ cipher = OpenSSL::Cipher.new("AES-256-GCM")
67
+ cipher.encrypt
68
+ cipher.key = key
69
+ cipher.iv = nonce
70
+
71
+ ciphertext = cipher.update(text.b) + cipher.final
72
+ tag = cipher.auth_tag(TAG_LEN)
73
+
74
+ (salt + nonce + tag + ciphertext).unpack1("H*").upcase
75
+ end
76
+
77
+ # Decrypt a V1 (AES-128-CBC, pkcs5_keyivgen) hex payload.
78
+ def decrypt_v1(hex_payload)
79
+ @dec_v1 ||= begin
80
+ c = OpenSSL::Cipher.new("AES-128-CBC")
81
+ c.decrypt
82
+ c.pkcs5_keyivgen(@pass_phrase, "uvgixEtU")
83
+ c
84
+ end
85
+ raw = [hex_payload.strip].pack("H*")
86
+ plain = @dec_v1.update(raw) + @dec_v1.final
87
+ @dec_v1.reset
88
+ plain.force_encoding("utf-8")
59
89
  end
60
90
 
61
- def decrypt(encrypted)
62
- cipher = @dec
63
- encrypted = [encrypted].pack("H*").unpack("C*").pack("c*")
64
- plain = cipher.update encrypted
65
- plain << cipher.final
91
+ # Decrypt a V2 (AES-256-GCM, PBKDF2) hex payload.
92
+ # Raises OpenSSL::Cipher::CipherError on wrong password or tampered data.
93
+ def decrypt_v2(hex_payload)
94
+ raw = [hex_payload.strip].pack("H*")
95
+
96
+ offset = 0
97
+ salt = raw[offset, SALT_LEN]; offset += SALT_LEN
98
+ nonce = raw[offset, NONCE_LEN]; offset += NONCE_LEN
99
+ tag = raw[offset, TAG_LEN]; offset += TAG_LEN
100
+ ciphertext = raw[offset..-1]
101
+
102
+ key = derive_key(salt)
103
+ cipher = OpenSSL::Cipher.new("AES-256-GCM")
104
+ cipher.decrypt
105
+ cipher.key = key
106
+ cipher.iv = nonce
107
+ cipher.auth_tag = tag
108
+
109
+ plain = cipher.update(ciphertext) + cipher.final
66
110
  plain.force_encoding("utf-8")
67
- @dec.reset
68
- return plain
111
+ end
112
+
113
+ private
114
+
115
+ def derive_key(salt)
116
+ OpenSSL::PKCS5.pbkdf2_hmac(@pass_phrase, salt, PBKDF2_ITERATIONS, PBKDF2_KEY_LEN, "SHA256")
69
117
  end
70
118
  end
71
119
 
@@ -77,3 +125,4 @@ end
77
125
  def encrypt_cur_buffer_callback(password, b = nil)
78
126
  vma.buf.set_encrypted(password)
79
127
  end
128
+ end # module Vimamsa
@@ -1,6 +1,8 @@
1
1
  require "parallel"
2
2
  require "stridx"
3
3
 
4
+ module Vimamsa
5
+
4
6
  # Limit file search to these extensions:
5
7
  cnf.find_extensions = [".txt", ".h", ".c", ".cpp", ".hpp", ".rb", ".java", ".js", ".py"]
6
8
  cnf.search_dirs = []
@@ -67,7 +69,7 @@ class FileFinder
67
69
  @@idx.add(x, i)
68
70
  end
69
71
  @@idx_updating = false
70
- message("Finish updating file index")
72
+ GLib::Idle.add { message("Finish updating file index"); false }
71
73
  }
72
74
  end
73
75
 
@@ -149,8 +151,6 @@ class FileFinder
149
151
 
150
152
  def filter_files(search_str)
151
153
  puts "search list: #{@@dir_list.size}"
152
- dir_hash = {}
153
-
154
154
  res = @@idx.find(search_str)
155
155
  resultarr = []
156
156
  for (idx, score) in res
@@ -159,21 +159,6 @@ class FileFinder
159
159
  resultarr << [fn, score]
160
160
  end
161
161
  return resultarr
162
-
163
- # Ripl.start :binding => binding
164
-
165
- scores = Parallel.map(@@dir_list, in_threads: 8) do |file|
166
- [file, srn_dst(search_str, file)]
167
- end
168
- for s in scores
169
- dir_hash[s[0]] = s[1] if s[1] > 0
170
- end
171
- # debug scores
172
- dir_hash = dir_hash.sort_by { |k, v| -v }
173
- dir_hash = dir_hash[0..20]
174
- dir_hash.map do |file, d|
175
- debug "D:#{d} #{file}"
176
- end
177
- return dir_hash
178
162
  end
179
163
  end
164
+ end # module Vimamsa
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  # History of previously opened files
2
4
 
3
5
  class FileHistory
@@ -8,7 +10,7 @@ class FileHistory
8
10
  vma.hook.register(:change_buffer, self.method("update"))
9
11
  vma.hook.register(:shutdown, self.method("save"))
10
12
 
11
- reg_act(:fhist_remove_nonexisting, proc { remove_nonexisting }, "Cleanup history, remove non-existing files")
13
+ reg_act("file.history.prune", proc { remove_nonexisting }, "Cleanup history, remove non-existing files")
12
14
 
13
15
  @history = vma.marshal_load("file_history", {})
14
16
  $search_list = []
@@ -96,3 +98,4 @@ def gui_file_history_select_callback(search_str, idx)
96
98
  gui_select_window_close(0)
97
99
  open_new_file(selected_file)
98
100
  end
101
+ end # module Vimamsa
@@ -1,7 +1,9 @@
1
1
  require "fileutils"
2
2
 
3
+ module Vimamsa
4
+
3
5
  class FileManager
4
- @@cur
6
+ @@cur = nil # Current object of class
5
7
 
6
8
  def initialize()
7
9
  @buf = nil
@@ -10,50 +12,60 @@ class FileManager
10
12
  end
11
13
 
12
14
  def self.chdir_parent()
13
- @@cur.chdir_parent
15
+ with_cur { |fm| fm.chdir_parent }
14
16
  end
15
17
 
16
18
  def self.cur()
17
19
  return @@cur
18
20
  end
19
21
 
22
+ # Run the block with the active FileManager. No-op (with a message) if the
23
+ # file selector has never been opened, i.e. @@cur is still nil.
24
+ def self.with_cur()
25
+ if @@cur.nil?
26
+ message("File selector is not open")
27
+ return nil
28
+ end
29
+ return yield @@cur
30
+ end
31
+
20
32
  def self.init()
21
- reg_act(:start_file_selector, proc { FileManager.new.run; vma.kbd.set_mode(:file_exp) }, "File selector")
33
+ reg_act("file.explorer", proc { FileManager.new.run; vma.kbd.set_mode(:file_exp) }, "File selector")
22
34
 
23
- reg_act(:fexp_chdir_parent, proc { FileManager.chdir_parent }, "File selector")
24
- reg_act(:fexp_select, proc { buf.module.select_line }, "")
35
+ reg_act("fexp.chdir_parent", proc { FileManager.chdir_parent }, "File selector")
36
+ reg_act("fexp.select", proc { buf.module.select_line }, "")
25
37
 
26
- reg_act(:fexp_sort_mtime, proc { FileManager.cur.sort_mtime }, "Sort based on time")
27
- reg_act(:fexp_sort_fname, proc { FileManager.cur.sort_fname }, "Sort based on file name")
38
+ reg_act("fexp.sort_mtime", proc { FileManager.with_cur { |fm| fm.sort_mtime } }, "Sort based on time")
39
+ reg_act("fexp.sort_fname", proc { FileManager.with_cur { |fm| fm.sort_fname } }, "Sort based on file name")
28
40
 
29
- bindkey "C , j f", :start_file_selector
30
- bindkey "C , f", :start_file_selector
41
+ bindkey "C , j f", "file.explorer"
42
+ bindkey "C , f", "file.explorer"
31
43
 
32
44
  # bindkey "C o", :delete_state
33
45
 
34
46
  vma.kbd.add_minor_mode("fexp", :file_exp, :command)
35
47
 
36
- bindkey "fexp o m", :fexp_sort_mtime
37
- bindkey "fexp o f", :fexp_sort_fname
48
+ bindkey "fexp o m", "fexp.sort_mtime"
49
+ bindkey "fexp o f", "fexp.sort_fname"
38
50
 
39
51
  # These are not yet safe to use
40
52
  if cnf.fexp.experimental?
41
- reg_act(:fexp_cut_file, proc { FileManager.cur.cut_file }, "Cut file (to paste elsewhere)")
42
- reg_act(:fexp_copy_file, proc { FileManager.cur.copy_file }, "Copy file (to paste elsewhere)")
43
- reg_act(:fexp_delete_file, proc { FileManager.cur.delete_file }, "Delete current file")
44
- reg_act(:fexp_paste_files, proc { FileManager.cur.paste_files }, "Move previously cut files here")
45
-
46
- bindkey "fexp d d", :fexp_cut_file
47
- bindkey "fexp y y", :fexp_copy_file
48
- bindkey "fexp d D", :fexp_delete_file
49
- bindkey "fexp p p", :fexp_paste_files
53
+ reg_act("fexp.cut_file", proc { FileManager.with_cur { |fm| fm.cut_file } }, "Cut file (to paste elsewhere)")
54
+ reg_act("fexp.copy_file", proc { FileManager.with_cur { |fm| fm.copy_file } }, "Copy file (to paste elsewhere)")
55
+ reg_act("fexp.delete_file", proc { FileManager.with_cur { |fm| fm.delete_file } }, "Delete current file")
56
+ reg_act("fexp.paste_files", proc { FileManager.with_cur { |fm| fm.paste_files } }, "Move previously cut files here")
57
+
58
+ bindkey "fexp d d", "fexp.cut_file"
59
+ bindkey "fexp y y", "fexp.copy_file"
60
+ bindkey "fexp d D", "fexp.delete_file"
61
+ bindkey "fexp p p", "fexp.paste_files"
50
62
  end
51
63
 
52
- # bindkey "fexp l", [:fexp_right, proc { debug "==fexp_right==" }, ""]
53
- bindkey "fexp h", :fexp_chdir_parent
54
- bindkey "fexp esc", [:fexp_quit, proc { FileManager.cur.quit }, ""]
55
- bindkey "fexp enter", :fexp_select
56
- bindkey "fexp l", :fexp_select
64
+ # bindkey "fexp l", ["fexp.right", proc { debug "==fexp_right==" }, ""]
65
+ bindkey "fexp h", "fexp.chdir_parent"
66
+ bindkey "fexp esc", ["fexp.quit", proc { FileManager.with_cur { |fm| fm.quit } }, ""]
67
+ bindkey "fexp enter", "fexp.select"
68
+ bindkey "fexp l", "fexp.select"
57
69
 
58
70
  @sort_by = :name
59
71
  end
@@ -179,7 +191,7 @@ class FileManager
179
191
  @header << "=" * 40
180
192
  @ld = dirpath # Path to current directory
181
193
  @dlist = Dir.children(@ld).sort
182
- @cdirs = [] # Dirs in current directory
194
+ @cdirs = [] # Dirs in current directory [name, stat]
183
195
  @cfiles = [] # Files in current directory
184
196
  for x in @dlist
185
197
  fpath = fullp(x)
@@ -192,13 +204,14 @@ class FileManager
192
204
  end
193
205
  next if x[0] == "."
194
206
  if File.directory?(fpath)
195
- # if f.directory?(fpath)
196
- @cdirs << x
207
+ @cdirs << [x, fstat]
197
208
  else
198
209
  @cfiles << [x, fstat]
199
210
  end
200
211
  end
201
212
 
213
+ @cdirs.sort_by! { |x| x[1].mtime }.reverse! if @sort_by == :mtime
214
+ @cdirs.sort_by! { |x| x[0] } if @sort_by == :name
202
215
  @cfiles.sort_by! { |x| x[1].mtime }.reverse! if @sort_by == :mtime
203
216
  @cfiles.sort_by! { |x| x[1].size }.reverse! if @sort_by == :size
204
217
  @cfiles.sort_by! { |x| x[0] } if @sort_by == :name
@@ -207,8 +220,9 @@ class FileManager
207
220
  s << @header.join("\n")
208
221
  s << "\n"
209
222
  s << "..\n"
210
- s << @cdirs.join("\n")
211
- s << "\n"
223
+ @cdirs.each do |d|
224
+ s << "#{d[0]}\n"
225
+ end
212
226
  s << "\n"
213
227
  jumppos = nil
214
228
  for f in @cfiles
@@ -237,7 +251,7 @@ class FileManager
237
251
  r = vma.buf.line_range(2, @cdirs.size+1)
238
252
 
239
253
  # Hilight works only if done after buffer is drawn
240
- run_as_idle proc { Gui.hilight_range(vma.buf, r, color: "#4488ffff") }
254
+ run_as_idle proc { Gui.hilight_range(vma.buf, r, color: theme_color(:search_jump)) }
241
255
  end
242
256
  end
243
257
 
@@ -284,3 +298,4 @@ class FileManager
284
298
  @buf.close
285
299
  end
286
300
  end
301
+ end # module Vimamsa
@@ -0,0 +1,226 @@
1
+
2
+ module Vimamsa
3
+
4
+ # Vim-style marker folds ({{{ ... }}}), "extract & stash" model.
5
+ #
6
+ # When a fold is closed, the whole {{{...}}} block is *removed* from the buffer
7
+ # string and stashed in @folds; the block is replaced by a single placeholder
8
+ # line. Opening re-inserts the stashed text verbatim. Because the folded text is
9
+ # no longer in the buffer string, every path that serialises the buffer to disk
10
+ # (Buffer#write_contents_to_file, Buffer#autosave) routes through
11
+ # #content_for_disk, which re-inflates folds so the file on disk always holds the
12
+ # fully expanded text.
13
+ #
14
+ # Known v1 limitations (inherent to the extract & stash approach): folded text is
15
+ # absent from in-buffer search / replace / LSP while closed, and per-fold state
16
+ # does not persist across restart (the {{{ }}} markers do; with
17
+ # cnf.fold.start_closed — the default — a reopened file starts fully collapsed,
18
+ # otherwise fully expanded). A region that already contains a *closed* fold
19
+ # cannot be folded until the inner fold is opened.
20
+
21
+ # One closed fold. `body` is the exact original block text (the {{{ line through
22
+ # the }}} line, trailing newline included) so open/save round-trips byte-for-byte.
23
+ FoldRecord = Struct.new(:anchor_id, :label, :body, :nlines)
24
+
25
+ class Buffer < String
26
+ FOLD_START_RE = /^\s*\{\{\{/
27
+ FOLD_END_RE = /^\s*\}\}\}/
28
+
29
+ def _fold_state
30
+ @folds ||= []
31
+ @fold_anchors ||= {}
32
+ @fold_next_id ||= 0
33
+ end
34
+
35
+ # Drop all fold state. Called whenever the buffer content is replaced
36
+ # wholesale (set_content / revert): the stashed bodies and anchors refer to
37
+ # the old text and would splice garbage into the new one.
38
+ def fold_reset
39
+ @folds = []
40
+ @fold_anchors = {}
41
+ @fold_next_id = 0
42
+ end
43
+
44
+ def _fold_placeholder(label, nlines)
45
+ head = label.empty? ? "{{{" : "{{{ #{label}"
46
+ "#{head} ⟨#{nlines} lines⟩\n"
47
+ end
48
+
49
+ # Return `str` with every top-level {{{...}}} block replaced by a closed-fold
50
+ # placeholder, the bodies stashed as if each fold had been closed by hand.
51
+ # Runs on the raw string *before* it is installed as buffer content
52
+ # (set_content), so anchors are positions in the returned string and no
53
+ # deltas / undo history / modified state are produced. Unbalanced {{{ lines
54
+ # are left as plain text.
55
+ def fold_collapse_all(str)
56
+ fold_reset
57
+ lines = str.lines
58
+ out = +""
59
+ i = 0
60
+ while i < lines.size
61
+ line = lines[i]
62
+ if line =~ FOLD_START_RE
63
+ depth = 1
64
+ j = i + 1
65
+ while j < lines.size
66
+ depth += 1 if lines[j] =~ FOLD_START_RE
67
+ depth -= 1 if lines[j] =~ FOLD_END_RE
68
+ break if depth == 0
69
+ j += 1
70
+ end
71
+ if depth == 0
72
+ body = lines[i..j].join
73
+ label = line.sub(FOLD_START_RE, "").strip
74
+ nlines = body.count("\n")
75
+ anchor_id = (@fold_next_id += 1)
76
+ @fold_anchors[anchor_id] = out.size
77
+ @folds << FoldRecord.new(anchor_id, label, body, nlines)
78
+ out << _fold_placeholder(label, nlines)
79
+ i = j + 1
80
+ next
81
+ end
82
+ end
83
+ out << line
84
+ i += 1
85
+ end
86
+ out
87
+ end
88
+
89
+ def fold_line_text(line_i)
90
+ self[line_range(line_i, 1)]
91
+ end
92
+
93
+ # Toggle the fold on line `line_i`. Returns true if the line was a fold-start or
94
+ # a closed-fold placeholder (i.e. this consumed the key); false otherwise, so
95
+ # the caller can run its normal line action.
96
+ def fold_toggle_at(line_i)
97
+ _fold_state
98
+ return false if line_i < 0 || line_i >= @line_ends.size
99
+
100
+ lr = line_range(line_i, 1)
101
+ anchor = @fold_anchors.find { |_id, p| p >= lr.begin && p <= lr.end }
102
+ if anchor
103
+ _fold_open(anchor[0])
104
+ return true
105
+ end
106
+
107
+ line = fold_line_text(line_i)
108
+ if line =~ FOLD_START_RE
109
+ _fold_close(line_i)
110
+ return true
111
+ end
112
+
113
+ false
114
+ end
115
+
116
+ # Close the fold whose start marker is on line `start_lpos`.
117
+ def _fold_close(start_lpos)
118
+ _fold_state
119
+ start_range = line_range(start_lpos, 1)
120
+ start_line = self[start_range]
121
+ label = start_line.sub(FOLD_START_RE, "").strip
122
+
123
+ # Find the depth-matched closing }}} line (nested folds counted so an outer
124
+ # fold swallows inner markers as literal text).
125
+ depth = 1
126
+ end_lpos = nil
127
+ i = start_lpos + 1
128
+ while i < @line_ends.size
129
+ lt = fold_line_text(i)
130
+ depth += 1 if lt =~ FOLD_START_RE
131
+ depth -= 1 if lt =~ FOLD_END_RE
132
+ if depth == 0
133
+ end_lpos = i
134
+ break
135
+ end
136
+ i += 1
137
+ end
138
+
139
+ if end_lpos.nil?
140
+ message("Unbalanced fold marker: no matching }}}")
141
+ return
142
+ end
143
+
144
+ end_range = line_range(end_lpos, 1)
145
+ block_begin = start_range.begin
146
+ block_end = end_range.end # inclusive; covers the }}} line + its \n
147
+
148
+ # v1: refuse to fold a region that already contains a closed fold — its
149
+ # placeholder anchor and stashed body would be orphaned by the extraction.
150
+ if @fold_anchors.values.any? { |p| p > block_begin && p <= block_end }
151
+ message("Cannot fold: region contains a closed fold — open it first")
152
+ return
153
+ end
154
+
155
+ body = self[block_begin..block_end]
156
+ nlines = body.count("\n")
157
+ placeholder = _fold_placeholder(label, nlines)
158
+
159
+ # Insert the placeholder first, then delete the original block (now shifted
160
+ # past it). Deleting first would momentarily empty the buffer and trip
161
+ # add_delta's auto-trailing-newline guard, doubling the final newline.
162
+ new_undo_group
163
+ block_len = block_end - block_begin + 1
164
+ add_delta([block_begin, INSERT, placeholder.size, placeholder], true)
165
+ add_delta([block_begin + placeholder.size, DELETE, block_len], true)
166
+ new_undo_group
167
+
168
+ anchor_id = (@fold_next_id += 1)
169
+ @fold_anchors[anchor_id] = block_begin
170
+ @folds << FoldRecord.new(anchor_id, label, body, nlines)
171
+
172
+ set_pos(block_begin)
173
+ end
174
+
175
+ # Open the closed fold identified by `anchor_id`: replace its placeholder line
176
+ # with the stashed body.
177
+ def _fold_open(anchor_id)
178
+ _fold_state
179
+ rec = @folds.find { |f| f.anchor_id == anchor_id }
180
+ pos = @fold_anchors[anchor_id]
181
+ return if rec.nil? || pos.nil?
182
+
183
+ # The placeholder is a single line starting at `pos`.
184
+ line_end = self.index("\n", pos) || (self.size - 1)
185
+ ph_len = line_end - pos + 1 # placeholder line incl. its \n
186
+
187
+ # Insert the body first, then delete the placeholder (now shifted past it);
188
+ # see _fold_close for why insert-before-delete avoids a doubled newline.
189
+ new_undo_group
190
+ add_delta([pos, INSERT, rec.body.size, rec.body], true)
191
+ add_delta([pos + rec.body.size, DELETE, ph_len], true)
192
+ new_undo_group
193
+
194
+ @folds.delete(rec)
195
+ @fold_anchors.delete(anchor_id)
196
+ set_pos(pos)
197
+ end
198
+
199
+ # The buffer text with every closed fold re-inflated to its original block.
200
+ # Used for all disk writes so the extract & stash model never loses folded
201
+ # text. Does not mutate the live (folded) buffer.
202
+ def content_for_disk
203
+ _fold_state
204
+ return self.to_s if @folds.empty?
205
+
206
+ result = self.to_s.dup
207
+ # Process placeholders back-to-front so earlier offsets stay valid.
208
+ @folds.sort_by { |f| -(@fold_anchors[f.anchor_id] || -1) }.each do |rec|
209
+ pos = @fold_anchors[rec.anchor_id]
210
+ next if pos.nil? || pos > result.size
211
+
212
+ lb = pos > 0 ? result.rindex("\n", pos - 1) : nil
213
+ lb = lb ? lb + 1 : 0
214
+ le = result.index("\n", pos) || (result.size - 1)
215
+
216
+ # Safety: only splice if the anchored line still looks like a placeholder.
217
+ # If the user deleted the placeholder line, the body was intentionally
218
+ # dropped — leave the (already-collapsed) text as is.
219
+ next unless result[lb..le] =~ FOLD_START_RE
220
+
221
+ result[lb..le] = rec.body
222
+ end
223
+ result
224
+ end
225
+ end
226
+ end # module Vimamsa