vimamsa 0.1.26 → 0.1.27

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 (170) 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 +62 -16
  6. data/custom_example.rb +43 -13
  7. data/doc/TODO.md +461 -0
  8. data/doc/bundling_plan.md +569 -0
  9. data/doc/key_bindings_report_plan.md +168 -0
  10. data/doc/missing_vim_bindings.md +71 -0
  11. data/doc/modules_settings_plan.md +433 -0
  12. data/doc/refactor_dedup_plan.md +117 -0
  13. data/doc/unused_code_plan.md +120 -0
  14. data/doc/vim_compatibility_plan.md +296 -0
  15. data/doc/vimlike_vim_conflicts.md +254 -0
  16. data/doc/vm_snapshots_plan.md +146 -0
  17. data/exe/run_tests.rb +117 -3
  18. data/exe/vimamsa +24 -4
  19. data/fonts/OFL-Oxanium.txt +93 -0
  20. data/fonts/Oxanium[wght].ttf +0 -0
  21. data/icon.png +0 -0
  22. data/img/demo.gif +0 -0
  23. data/lang/hyperplaintext.lang +6 -0
  24. data/lib/vimamsa/ack.rb +11 -1
  25. data/lib/vimamsa/actions.rb +4 -1
  26. data/lib/vimamsa/appimage_env.rb +35 -0
  27. data/lib/vimamsa/audio.rb +2 -5
  28. data/lib/vimamsa/bookmarks.rb +302 -0
  29. data/lib/vimamsa/buffer.rb +593 -82
  30. data/lib/vimamsa/buffer_changetext.rb +102 -42
  31. data/lib/vimamsa/buffer_cursor.rb +7 -0
  32. data/lib/vimamsa/buffer_list.rb +4 -0
  33. data/lib/vimamsa/buffer_manager.rb +4 -1
  34. data/lib/vimamsa/bundled_fonts.rb +32 -0
  35. data/lib/vimamsa/clipboard.rb +3 -0
  36. data/lib/vimamsa/conf.rb +59 -3
  37. data/lib/vimamsa/constants.rb +3 -0
  38. data/lib/vimamsa/crash_handler.rb +149 -0
  39. data/lib/vimamsa/debug.rb +4 -1
  40. data/lib/vimamsa/desktop_install.rb +96 -0
  41. data/lib/vimamsa/diff_buffer.rb +39 -10
  42. data/lib/vimamsa/easy_jump.rb +3 -0
  43. data/lib/vimamsa/editor.rb +263 -57
  44. data/lib/vimamsa/encrypt.rb +78 -29
  45. data/lib/vimamsa/file_finder.rb +4 -19
  46. data/lib/vimamsa/file_history.rb +3 -0
  47. data/lib/vimamsa/file_manager.rb +30 -15
  48. data/lib/vimamsa/fold.rb +226 -0
  49. data/lib/vimamsa/gui.rb +520 -106
  50. data/lib/vimamsa/gui_command_line.rb +208 -0
  51. data/lib/vimamsa/gui_dialog.rb +94 -42
  52. data/lib/vimamsa/gui_file_panel.rb +356 -25
  53. data/lib/vimamsa/gui_form_generator.rb +59 -35
  54. data/lib/vimamsa/gui_func_panel.rb +67 -22
  55. data/lib/vimamsa/gui_image.rb +3 -0
  56. data/lib/vimamsa/gui_keylog_panel.rb +246 -0
  57. data/lib/vimamsa/gui_menu.rb +23 -11
  58. data/lib/vimamsa/gui_select_window.rb +19 -48
  59. data/lib/vimamsa/gui_settings.rb +78 -37
  60. data/lib/vimamsa/gui_sourceview.rb +273 -19
  61. data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
  62. data/lib/vimamsa/gui_text.rb +63 -2
  63. data/lib/vimamsa/gui_theme.rb +606 -0
  64. data/lib/vimamsa/hook.rb +3 -0
  65. data/lib/vimamsa/hyper_plain_text.rb +3 -0
  66. data/lib/vimamsa/key_actions.rb +112 -13
  67. data/lib/vimamsa/key_binding_tree.rb +242 -215
  68. data/lib/vimamsa/key_bindings_notepad.rb +43 -0
  69. data/lib/vimamsa/key_bindings_report.rb +261 -0
  70. data/lib/vimamsa/key_bindings_vim.rb +354 -0
  71. data/lib/vimamsa/key_bindings_vimlike.rb +65 -9
  72. data/lib/vimamsa/langservp.rb +271 -54
  73. data/lib/vimamsa/macro.rb +3 -0
  74. data/lib/vimamsa/main.rb +14 -8
  75. data/lib/vimamsa/rbvma.rb +26 -1
  76. data/lib/vimamsa/search.rb +3 -0
  77. data/lib/vimamsa/search_replace.rb +11 -10
  78. data/lib/vimamsa/string_util.rb +120 -40
  79. data/lib/vimamsa/terminal.rb +3 -0
  80. data/lib/vimamsa/test_framework.rb +28 -2
  81. data/lib/vimamsa/tests.rb +3 -0
  82. data/lib/vimamsa/text_objects.rb +353 -0
  83. data/lib/vimamsa/text_transforms.rb +14 -0
  84. data/lib/vimamsa/util.rb +46 -7
  85. data/lib/vimamsa/version.rb +1 -1
  86. data/lib/vimamsa.rb +0 -1
  87. data/modules/color_highlight/color_highlight.rb +118 -0
  88. data/modules/color_highlight/color_highlight_info.rb +3 -0
  89. data/modules/dictation/dictation.rb +1054 -0
  90. data/modules/dictation/dictation_info.rb +3 -0
  91. data/modules/dictation/whisper_worker.py +273 -0
  92. data/modules/ollama/ollama.rb +107 -0
  93. data/modules/ollama/ollama_info.rb +3 -0
  94. data/modules/tts/tts.rb +119 -0
  95. data/modules/tts/tts_info.rb +3 -0
  96. data/run_e2e_vm.sh +28 -0
  97. data/run_tests_vm.sh +130 -0
  98. data/scripts/appimage/AppRun +54 -0
  99. data/scripts/appimage/Dockerfile +18 -0
  100. data/scripts/appimage/build.sh +19 -0
  101. data/scripts/appimage/build_in_container.sh +192 -0
  102. data/scripts/appimage/smoke_test.sh +27 -0
  103. data/scripts/demo/demo_steps.txt +59 -0
  104. data/scripts/demo/record_demo.rb +177 -0
  105. data/scripts/demo/shapes.rb +28 -0
  106. data/styles/134272-molokai.xml +3 -0
  107. data/styles/molokai_cyber.xml +48 -0
  108. data/styles/molokai_edit.xml +1 -0
  109. data/tests/support/virtual_keyboard.rb +254 -0
  110. data/tests/support/virtual_mic.rb +67 -0
  111. data/tests/test_ack.rb +22 -0
  112. data/tests/test_auto_format.rb +122 -0
  113. data/tests/test_autocomplete.rb +211 -0
  114. data/tests/test_autosave.rb +145 -0
  115. data/tests/test_basic_editing.rb +33 -3
  116. data/tests/test_bookmarks.rb +206 -0
  117. data/tests/test_bracket_match.rb +85 -0
  118. data/tests/test_bundling.rb +92 -0
  119. data/tests/test_color_highlight.rb +92 -0
  120. data/tests/test_command_line.rb +79 -0
  121. data/tests/test_command_line_file.rb +27 -0
  122. data/tests/test_context_menu_file.rb +129 -0
  123. data/tests/test_copy_paste.rb +80 -0
  124. data/tests/test_crash_handler.rb +39 -0
  125. data/tests/test_custom_two_phase.rb +92 -0
  126. data/tests/test_dialog_callback_errors.rb +44 -0
  127. data/tests/test_dictation.rb +294 -0
  128. data/tests/test_diff_buffer_name.rb +51 -0
  129. data/tests/test_double_click.rb +41 -0
  130. data/tests/test_drag_selection.rb +125 -0
  131. data/tests/test_e2e_cmdline.rb +116 -0
  132. data/tests/test_e2e_dictation.rb +98 -0
  133. data/tests/test_e2e_replace_char.rb +58 -0
  134. data/tests/test_extract_file_links.rb +55 -0
  135. data/tests/test_file_format.rb +217 -0
  136. data/tests/test_file_manager.rb +72 -0
  137. data/tests/test_file_panel.rb +269 -0
  138. data/tests/test_flash.rb +78 -0
  139. data/tests/test_fold.rb +111 -0
  140. data/tests/test_font_refresh.rb +56 -0
  141. data/tests/test_func_panel.rb +68 -0
  142. data/tests/test_grep_apply.rb +46 -0
  143. data/tests/test_gtk_sync.rb +126 -0
  144. data/tests/test_hpt_highlight_visible.rb +75 -0
  145. data/tests/test_hpt_link_highlight.rb +82 -0
  146. data/tests/test_jump_to_file.rb +41 -0
  147. data/tests/test_key_bindings.rb +208 -0
  148. data/tests/test_keylog_panel.rb +101 -0
  149. data/tests/test_lsp_clangd.rb +281 -0
  150. data/tests/test_lsp_jump.rb +87 -0
  151. data/tests/test_lsp_position_encoding.rb +49 -0
  152. data/tests/test_lsp_symbols.rb +68 -0
  153. data/tests/test_macro.rb +137 -0
  154. data/tests/test_markdown_highlight.rb +57 -0
  155. data/tests/test_mode_badge.rb +41 -0
  156. data/tests/test_notepad_bindings.rb +67 -0
  157. data/tests/test_popup_queue.rb +51 -0
  158. data/tests/test_random_edit.rb +109 -0
  159. data/tests/test_revert.rb +127 -0
  160. data/tests/test_startup_file.rb +58 -0
  161. data/tests/test_status_area.rb +50 -0
  162. data/tests/test_text_objects.rb +163 -0
  163. data/tests/test_text_transforms.rb +329 -0
  164. data/tests/test_tts.rb +68 -0
  165. data/tests/test_vim_bindings.rb +252 -0
  166. data/tests/test_vim_conflicts.rb +63 -0
  167. data/tests/test_window_layout.rb +219 -0
  168. data/vimamsa.gemspec +3 -1
  169. data/vm_guest_run.sh +109 -0
  170. metadata +112 -10
@@ -1,3 +1,5 @@
1
+
2
+ module Vimamsa
1
3
  # History of previously opened files
2
4
 
3
5
  class FileHistory
@@ -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,21 +12,31 @@ 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
33
  reg_act(:start_file_selector, proc { FileManager.new.run; vma.kbd.set_mode(:file_exp) }, "File selector")
22
34
 
23
35
  reg_act(:fexp_chdir_parent, proc { FileManager.chdir_parent }, "File selector")
24
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
41
  bindkey "C , j f", :start_file_selector
30
42
  bindkey "C , f", :start_file_selector
@@ -38,10 +50,10 @@ class FileManager
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")
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")
45
57
 
46
58
  bindkey "fexp d d", :fexp_cut_file
47
59
  bindkey "fexp y y", :fexp_copy_file
@@ -51,7 +63,7 @@ class FileManager
51
63
 
52
64
  # bindkey "fexp l", [:fexp_right, proc { debug "==fexp_right==" }, ""]
53
65
  bindkey "fexp h", :fexp_chdir_parent
54
- bindkey "fexp esc", [:fexp_quit, proc { FileManager.cur.quit }, ""]
66
+ bindkey "fexp esc", [:fexp_quit, proc { FileManager.with_cur { |fm| fm.quit } }, ""]
55
67
  bindkey "fexp enter", :fexp_select
56
68
  bindkey "fexp l", :fexp_select
57
69
 
@@ -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