neovm-core 0.0.2

Core runtime structures for NeoVM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
//! Buffer text storage.
//!
//! GNU Emacs separates per-buffer metadata from the underlying text object.
//! `BufferText` is the first local seam toward that design. Today it is a thin
//! wrapper around `GapBuffer`; later it can absorb shared text state, char/byte
//! caches, and interval ownership without forcing another tree-wide field-type
//! rewrite.

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fmt;
use std::rc::Rc;

use crate::emacs_core::value::Value;
use crate::gc_trace::GcTrace;

use super::buffer::{BufferId, InsertionType};
use super::gap_buffer::GapBuffer;
use super::text_props::{PropertyInterval, TextPropertyTable};

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct BufferTextLayout {
    pub gpt: usize,
    pub z: usize,
    pub gpt_byte: usize,
    pub z_byte: usize,
    pub gap_size: usize,
}

/// Last successful char↔byte conversion. Reused on a subsequent query if the
/// buffer text has not changed since the entry was stored. Mirrors GNU
/// `marker.c:202-203` but uses a (total_chars, total_bytes) epoch rather than
/// `chars_modiff` so it works correctly even when called directly on
/// `BufferText` without going through the `insdel.rs` tick-bumping path.
#[derive(Clone, Copy, Default)]
struct PositionCache {
    /// Total char count when this entry was stored. 0 = invalid.
    epoch_chars: usize,
    /// Total byte length when this entry was stored. 0 = invalid (disambiguates
    /// a legitimately empty buffer from an uninitialised cache).
    epoch_bytes: usize,
    charpos: usize,
    bytepos: usize,
}

struct BufferTextStorage {
    layout: BufferTextLayout,
    gap: GapBuffer,
    modified_tick: i64,
    chars_modified_tick: i64,
    save_modified_tick: i64,
    text_props: TextPropertyTable,
    /// Head of the intrusive per-buffer marker chain (GNU `buffer->own_text.markers`).
    /// Authoritative since T6; the parallel `Vec<MarkerEntry>` was deleted in T7.
    markers_head: *mut crate::tagged::header::MarkerObj,
    /// Interior-mutable last-query cache for char↔byte conversion.
    pos_cache: Cell<PositionCache>,
    /// Internal (non-Lisp-visible) anchor positions populated on long scans.
    /// Invalidated wholesale when `(total_chars, total_bytes)` advances.
    anchor_cache: RefCell<Vec<(usize, usize)>>,
    /// `(epoch_chars, epoch_bytes)` at which the anchor_cache is valid.
    /// Mismatch triggers a wholesale clear on next read.
    anchor_cache_key: Cell<(usize, usize)>,
}

impl Clone for BufferTextStorage {
    fn clone(&self) -> Self {
        Self {
            layout: self.layout.clone(),
            gap: self.gap.clone(),
            modified_tick: self.modified_tick,
            chars_modified_tick: self.chars_modified_tick,
            save_modified_tick: self.save_modified_tick,
            text_props: self.text_props.clone(),
            // Chain head intentionally not cloned: chain pointers are unique
            // per TaggedHeap; a cloned buffer starts with an empty chain and
            // rebuilds it via register_marker.
            markers_head: std::ptr::null_mut(),
            pos_cache: self.pos_cache.clone(),
            anchor_cache: self.anchor_cache.clone(),
            anchor_cache_key: self.anchor_cache_key.clone(),
        }
    }
}

pub struct BufferText {
    storage: Rc<RefCell<BufferTextStorage>>,
}

impl Clone for BufferText {
    fn clone(&self) -> Self {
        let storage = self.storage.borrow().clone();
        Self {
            storage: Rc::new(RefCell::new(storage)),
        }
    }
}

impl Default for BufferText {
    fn default() -> Self {
        Self::new()
    }
}

impl BufferText {
    fn from_gap(gap: GapBuffer) -> Self {
        Self {
            storage: Rc::new(RefCell::new(BufferTextStorage {
                layout: Self::layout_from_gap(&gap),
                gap,
                modified_tick: 1,
                chars_modified_tick: 1,
                save_modified_tick: 1,
                text_props: TextPropertyTable::new(),
                markers_head: std::ptr::null_mut(),
                pos_cache: Cell::new(PositionCache::default()),
                anchor_cache: RefCell::new(Vec::new()),
                anchor_cache_key: Cell::new((0, 0)),
            })),
        }
    }

    fn layout_from_gap(gap: &GapBuffer) -> BufferTextLayout {
        BufferTextLayout {
            gpt: gap.gpt(),
            z: gap.z(),
            gpt_byte: gap.gpt_byte(),
            z_byte: gap.z_byte(),
            gap_size: gap.gap_size(),
        }
    }

    pub fn new() -> Self {
        Self::from_gap(GapBuffer::new())
    }

    pub fn from_str(text: &str) -> Self {
        Self::from_gap(GapBuffer::from_str(text))
    }

    pub fn from_lisp_string(text: &crate::heap_types::LispString) -> Self {
        Self::from_gap(GapBuffer::from_emacs_bytes(
            text.as_bytes(),
            text.is_multibyte(),
        ))
    }

    pub fn len(&self) -> usize {
        self.storage.borrow().gap.len()
    }

    pub fn is_multibyte(&self) -> bool {
        self.storage.borrow().gap.is_multibyte()
    }

    pub fn set_multibyte(&self, multibyte: bool) {
        let mut storage = self.storage.borrow_mut();
        storage.gap.set_multibyte(multibyte);
        storage.layout = Self::layout_from_gap(&storage.gap);
    }

    pub fn is_empty(&self) -> bool {
        self.storage.borrow().gap.is_empty()
    }

    pub fn char_count(&self) -> usize {
        self.storage.borrow().gap.char_count()
    }

    pub fn emacs_byte_len(&self) -> usize {
        self.storage.borrow().gap.emacs_byte_len()
    }

    pub fn layout(&self) -> BufferTextLayout {
        self.storage.borrow().layout
    }

    pub fn modified_tick(&self) -> i64 {
        self.storage.borrow().modified_tick
    }

    pub fn chars_modified_tick(&self) -> i64 {
        self.storage.borrow().chars_modified_tick
    }

    pub fn save_modified_tick(&self) -> i64 {
        self.storage.borrow().save_modified_tick
    }

    pub fn byte_at(&self, pos: usize) -> u8 {
        self.storage.borrow().gap.byte_at(pos)
    }

    pub fn emacs_byte_at(&self, pos: usize) -> Option<u8> {
        self.storage.borrow().gap.emacs_byte_at(pos)
    }

    pub fn char_at(&self, pos: usize) -> Option<char> {
        self.storage
            .borrow()
            .gap
            .char_code_at(pos)
            .and_then(char::from_u32)
    }

    pub fn char_code_at(&self, pos: usize) -> Option<u32> {
        self.storage.borrow().gap.char_code_at(pos)
    }

    pub fn text_range(&self, start: usize, end: usize) -> String {
        self.storage.borrow().gap.text_range(start, end)
    }

    pub fn copy_bytes_to(&self, start: usize, end: usize, out: &mut Vec<u8>) {
        self.storage.borrow().gap.copy_bytes_to(start, end, out);
    }

    pub fn copy_emacs_bytes_to(&self, start: usize, end: usize, out: &mut Vec<u8>) {
        self.storage
            .borrow()
            .gap
            .copy_emacs_bytes_to(start, end, out);
    }

    pub fn to_string(&self) -> String {
        self.storage.borrow().gap.to_string()
    }

    pub fn insert_str(&mut self, pos: usize, text: &str) {
        if text.is_empty() {
            return;
        }
        let mut storage = self.storage.borrow_mut();
        storage.gap.insert_str(pos, text);
        storage.layout = Self::layout_from_gap(&storage.gap);
    }

    pub fn insert_emacs_bytes(&mut self, pos: usize, bytes: &[u8]) {
        if bytes.is_empty() {
            return;
        }
        let mut storage = self.storage.borrow_mut();
        storage.gap.insert_emacs_bytes(pos, bytes);
        storage.layout = Self::layout_from_gap(&storage.gap);
    }

    pub fn insert_emacs_bytes_both(&mut self, pos: usize, bytes: &[u8], nchars: usize) {
        if bytes.is_empty() {
            return;
        }
        let mut storage = self.storage.borrow_mut();
        storage.gap.insert_emacs_bytes_both(pos, bytes, nchars);
        storage.layout = Self::layout_from_gap(&storage.gap);
    }

    pub fn delete_range(&mut self, start: usize, end: usize) {
        if start >= end {
            return;
        }
        let mut storage = self.storage.borrow_mut();
        storage.gap.delete_range(start, end);
        storage.layout = Self::layout_from_gap(&storage.gap);
    }

    pub fn delete_range_both(&mut self, start: usize, end: usize, nchars: usize) {
        if start >= end {
            return;
        }
        let mut storage = self.storage.borrow_mut();
        storage.gap.delete_range_both(start, end, nchars);
        storage.layout = Self::layout_from_gap(&storage.gap);
    }

    pub fn replace_same_len_emacs_bytes(&mut self, start: usize, end: usize, replacement: &[u8]) {
        if start >= end {
            return;
        }
        let mut storage = self.storage.borrow_mut();
        storage
            .gap
            .replace_same_len_emacs_bytes(start, end, replacement);
        storage.layout = Self::layout_from_gap(&storage.gap);
    }

    pub fn byte_to_char(&self, byte_pos: usize) -> usize {
        self.buf_bytepos_to_charpos(byte_pos)
    }

    pub fn char_to_byte(&self, char_pos: usize) -> usize {
        self.buf_charpos_to_bytepos(char_pos)
    }

    pub fn emacs_byte_to_char(&self, byte_pos: usize) -> usize {
        // Storage bytes == Emacs bytes in current NeoMacs, so this is an
        // alias. If that ever diverges, do the extra translation first here.
        self.buf_bytepos_to_charpos(byte_pos)
    }

    pub fn char_to_emacs_byte(&self, char_pos: usize) -> usize {
        self.buf_charpos_to_bytepos(char_pos)
    }

    pub fn storage_byte_to_emacs_byte(&self, byte_pos: usize) -> usize {
        self.storage
            .borrow()
            .gap
            .storage_byte_to_emacs_byte(byte_pos)
    }

    pub fn emacs_byte_to_storage_byte(&self, byte_pos: usize) -> usize {
        self.storage
            .borrow()
            .gap
            .emacs_byte_to_storage_byte(byte_pos)
    }

    pub fn shared_clone(&self) -> Self {
        Self {
            storage: Rc::clone(&self.storage),
        }
    }

    pub(crate) fn shares_storage_with(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.storage, &other.storage)
    }

    pub(crate) fn dump_text(&self) -> Vec<u8> {
        self.storage.borrow().gap.dump_text()
    }

    pub(crate) fn from_dump(text: Vec<u8>, multibyte: bool) -> Self {
        Self::from_gap(GapBuffer::from_dump(text, multibyte))
    }

    pub fn set_modification_state(
        &self,
        modified_tick: i64,
        chars_modified_tick: i64,
        save_modified_tick: i64,
    ) {
        let mut storage = self.storage.borrow_mut();
        storage.modified_tick = modified_tick;
        storage.chars_modified_tick = chars_modified_tick;
        storage.save_modified_tick = save_modified_tick;
    }

    pub fn set_modified_tick(&self, tick: i64) {
        self.storage.borrow_mut().modified_tick = tick;
    }

    pub fn set_save_modified_tick(&self, tick: i64) {
        self.storage.borrow_mut().save_modified_tick = tick;
    }

    pub fn increment_modified_tick(&self, delta: i64) {
        self.storage.borrow_mut().modified_tick += delta;
    }

    pub fn record_char_modification(&self, delta: i64) {
        let mut storage = self.storage.borrow_mut();
        storage.modified_tick += delta;
        storage.chars_modified_tick = storage.modified_tick;
    }

    pub fn range_contains_char_code(&self, start: usize, end: usize, code: u32) -> bool {
        if start >= end {
            return false;
        }
        // Walk buffer bytes directly, avoiding the storage-form conversion
        // previously done through text_range(). For multibyte buffers each
        // Emacs char is decoded via emacs_char::string_char; for unibyte
        // buffers each byte is one "character" in the range 0..=0xFF.
        let mut bytes = Vec::with_capacity(end - start);
        self.storage
            .borrow()
            .gap
            .copy_bytes_to(start, end, &mut bytes);
        if self.is_multibyte() {
            let mut pos = 0;
            while pos < bytes.len() {
                let (c, len) = crate::emacs_core::emacs_char::string_char(&bytes[pos..]);
                if c == code {
                    return true;
                }
                pos += len.max(1);
            }
            false
        } else {
            if code > 0xFF {
                return false;
            }
            bytes.iter().any(|&b| b as u32 == code)
        }
    }

    pub fn text_props_is_empty(&self) -> bool {
        self.storage.borrow().text_props.is_empty()
    }

    pub fn text_props_snapshot(&self) -> TextPropertyTable {
        self.storage.borrow().text_props.clone()
    }

    pub fn text_props_replace(&self, table: TextPropertyTable) {
        self.storage.borrow_mut().text_props = table;
    }

    pub fn replace_storage(&self, text: &str, multibyte: bool, text_props: TextPropertyTable) {
        let bytes =
            crate::emacs_core::string_escape::storage_string_to_buffer_bytes(text, multibyte);
        let string = if multibyte {
            crate::heap_types::LispString::from_emacs_bytes(bytes)
        } else {
            crate::heap_types::LispString::from_unibyte(bytes)
        };
        self.replace_lisp_string(&string, text_props);
    }
    pub fn replace_lisp_string(
        &self,
        text: &crate::heap_types::LispString,
        text_props: TextPropertyTable,
    ) {
        let mut storage = self.storage.borrow_mut();
        storage.gap = GapBuffer::from_emacs_bytes(text.as_bytes(), text.is_multibyte());
        storage.layout = Self::layout_from_gap(&storage.gap);
        storage.text_props = text_props;
        // Wholesale content replacement: invalidate position caches. If the new
        // content happens to have the same (total_chars, total_bytes) as the old,
        // a stale pos_cache entry could otherwise return a wrong bytepos.
        storage.pos_cache.set(PositionCache::default());
        storage.anchor_cache.borrow_mut().clear();
        storage.anchor_cache_key.set((0, 0));
    }

    /// Walk the intrusive marker chain and remap each marker's (bytepos,
    /// charpos) through the caller-supplied closure. Used by
    /// `set-buffer-multibyte` to translate marker positions across a
    /// wholesale gap-buffer replacement (the boundary arithmetic lives in
    /// the caller; this helper only handles chain traversal).
    ///
    /// The closure receives the marker's current `bytepos` and returns
    /// the new `(bytepos, charpos)` pair.
    pub fn remap_markers_through<F>(&self, mut remap: F)
    where
        F: FnMut(usize) -> (usize, usize),
    {
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: `curr` walks live chain-owned MarkerObj pointers from
        // `markers_head` until null. Each non-null node was spliced in via
        // `chain_splice_at_head`, so its `data.next_marker` is a valid
        // chain link or null.
        unsafe {
            while !curr.is_null() {
                let data = &mut (*curr).data;
                let (new_byte, new_char) = remap(data.bytepos);
                data.bytepos = new_byte;
                data.charpos = new_char;
                curr = data.next_marker;
            }
        }
    }

    pub fn text_props_put_property(
        &self,
        start: usize,
        end: usize,
        name: Value,
        value: Value,
    ) -> bool {
        self.storage
            .borrow_mut()
            .text_props
            .put_property(start, end, name, value)
    }

    pub fn text_props_get_property(&self, pos: usize, name: Value) -> Option<Value> {
        self.storage
            .borrow()
            .text_props
            .get_property(pos, name)
            .copied()
    }

    pub fn text_props_get_properties(&self, pos: usize) -> HashMap<Value, Value> {
        self.storage.borrow().text_props.get_properties(pos)
    }

    pub fn text_props_get_properties_ordered(&self, pos: usize) -> Vec<(Value, Value)> {
        self.storage.borrow().text_props.get_properties_ordered(pos)
    }

    pub fn text_props_remove_property(&self, start: usize, end: usize, name: Value) -> bool {
        self.storage
            .borrow_mut()
            .text_props
            .remove_property(start, end, name)
    }

    pub fn text_props_remove_all(&self, start: usize, end: usize) {
        self.storage
            .borrow_mut()
            .text_props
            .remove_all_properties(start, end);
    }

    pub fn text_props_next_change(&self, pos: usize) -> Option<usize> {
        self.storage.borrow().text_props.next_property_change(pos)
    }

    pub fn text_props_previous_change(&self, pos: usize) -> Option<usize> {
        self.storage
            .borrow()
            .text_props
            .previous_property_change(pos)
    }

    pub fn text_props_append_shifted(&self, other: &TextPropertyTable, byte_offset: usize) {
        self.storage
            .borrow_mut()
            .text_props
            .append_shifted(other, byte_offset);
    }

    pub fn text_props_slice(&self, start: usize, end: usize) -> TextPropertyTable {
        self.storage.borrow().text_props.slice(start, end)
    }

    pub fn text_props_intervals_snapshot(&self) -> Vec<PropertyInterval> {
        self.storage.borrow().text_props.intervals_snapshot()
    }

    pub fn adjust_text_props_for_insert(&self, pos: usize, len: usize) {
        self.storage
            .borrow_mut()
            .text_props
            .adjust_for_insert(pos, len);
    }

    pub fn adjust_text_props_for_delete(&self, start: usize, end: usize) {
        self.storage
            .borrow_mut()
            .text_props
            .adjust_for_delete(start, end);
    }

    pub fn trace_text_prop_roots(&self, roots: &mut Vec<Value>) {
        self.storage.borrow().text_props.trace_roots(roots);
    }

    /// Register a marker in this buffer. Updates `MarkerData` fields
    /// authoritatively (buffer/bytepos/charpos/marker_id/insertion_type)
    /// and splices the marker into this buffer's intrusive chain at head.
    ///
    /// **Precondition:** `marker_ptr.data.next_marker` is null, i.e. the
    /// marker is not currently on any chain. Callers re-binding a marker
    /// must `chain_unlink` from the old buffer first; the
    /// `debug_assert!` in `chain_splice_at_head` catches violations.
    pub fn register_marker(
        &self,
        marker_ptr: *mut crate::tagged::header::MarkerObj,
        buffer_id: BufferId,
        marker_id: u64,
        byte_pos: usize,
        char_pos: usize,
        insertion_type: InsertionType,
    ) {
        // Update MarkerData so its fields are authoritative before the
        // chain ever exposes this marker.
        //
        // SAFETY: `marker_ptr` is a live MarkerObj allocated via
        // `TaggedHeap::alloc_marker`; writes through a raw pointer are
        // sound for the heap's lifetime. The chain precondition is
        // enforced by `chain_splice_at_head`'s debug_assert below.
        unsafe {
            (*marker_ptr).data.buffer = Some(buffer_id);
            (*marker_ptr).data.marker_id = Some(marker_id);
            (*marker_ptr).data.bytepos = byte_pos;
            (*marker_ptr).data.charpos = char_pos;
            (*marker_ptr).data.insertion_type = insertion_type == InsertionType::After;
        }
        self.chain_splice_at_head(marker_ptr);
    }

    /// Walk the intrusive marker chain head→tail and invoke `f` on each
    /// live `MarkerData` by reference. Read-only counterpart to
    /// `chain_walk_mut`; used by pdump (v26) to serialize the chain
    /// without materializing an intermediate Vec.
    ///
    /// SAFETY: walks live chain-owned MarkerObj pointers from
    /// `storage.markers_head` until null; each `(*curr).data` reference
    /// stays valid for the duration of the call because the GC sweep
    /// runs `unchain_dead_markers` between mark and free.
    pub fn chain_walk_data<F: FnMut(&crate::heap_types::MarkerData)>(&self, mut f: F) {
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        unsafe {
            while !curr.is_null() {
                let data = &(*curr).data;
                f(data);
                curr = data.next_marker;
            }
        }
    }

    /// Walk this buffer's intrusive marker chain and return the raw
    /// MarkerObj pointer for the first node whose `marker_id` matches,
    /// or null when none found. Used by pdump load (v26) to resolve
    /// `BufferStateMarkers` (pt/begv/zv) ids back to chain pointers
    /// after the chain has been reconstructed.
    ///
    /// Pointer lifetime: the returned `*mut MarkerObj` is only valid while
    /// `self`'s chain still holds it. Any subsequent splice/unlink on this
    /// buffer's chain, or a GC cycle that runs `unchain_dead_markers`, may
    /// detach the node from the chain — callers must use the pointer
    /// before doing anything that could mutate the chain, and must not
    /// re-enter the chain (or invoke arbitrary Lisp) between lookup and
    /// use.
    pub fn chain_find_by_id(&self, marker_id: u64) -> *mut crate::tagged::header::MarkerObj {
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: chain walks live chain-owned MarkerObj pointers from
        // `storage.markers_head` until null.
        unsafe {
            while !curr.is_null() {
                if (*curr).data.marker_id == Some(marker_id) {
                    return curr;
                }
                curr = (*curr).data.next_marker;
            }
        }
        std::ptr::null_mut()
    }

    /// Walk the intrusive chain and return the MarkerData-derived fields
    /// `(bytepos, charpos, insertion_type)` for the marker with the given
    /// id, or `None` if no live chain node carries that id.
    ///
    /// Production code should prefer reading `MarkerData` directly off a
    /// Lisp `Value`. This helper exists for internal buffer-manager
    /// callers (e.g. `clone_marker_in_buffer`) that track markers by id
    /// without holding the Lisp value.
    pub fn marker_chain_lookup(&self, marker_id: u64) -> Option<(usize, usize, InsertionType)> {
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: chain walks live chain-owned MarkerObj pointers until null.
        unsafe {
            while !curr.is_null() {
                let data = &(*curr).data;
                if data.marker_id == Some(marker_id) {
                    let ins = if data.insertion_type {
                        InsertionType::After
                    } else {
                        InsertionType::Before
                    };
                    return Some((data.bytepos, data.charpos, ins));
                }
                curr = data.next_marker;
            }
        }
        None
    }

    pub fn remove_marker(&self, marker_id: u64) {
        // Post-T8: `unchain_dead_markers` splices unmarked MarkerObjs
        // out of this buffer's chain between the mark and sweep GC
        // phases, so a chain walk between GC cycles never dereferences
        // a freed allocation. Walk the chain directly and splice the
        // matching node.
        let marker_ptr: Option<*mut crate::tagged::header::MarkerObj> = {
            let storage = self.storage.borrow();
            let mut curr = storage.markers_head;
            let mut found = None;
            // SAFETY: chain walks live chain-owned MarkerObj pointers
            // from `storage.markers_head` until null.
            unsafe {
                while !curr.is_null() {
                    if (*curr).data.marker_id == Some(marker_id) {
                        found = Some(curr);
                        break;
                    }
                    curr = (*curr).data.next_marker;
                }
            }
            found
        };
        if let Some(ptr) = marker_ptr {
            self.chain_unlink(ptr);
            // SAFETY: `ptr` was read from this buffer's chain; chain-
            // owned allocations stay live until the next GC sweep.
            // `chain_unlink` left it detached; field writes are sound.
            unsafe {
                (*ptr).data.buffer = None;
                (*ptr).data.bytepos = 0;
                (*ptr).data.charpos = 0;
            }
        }
    }

    pub fn update_marker_insertion_type(&self, marker_id: u64, insertion_type: InsertionType) {
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: chain walks live chain-owned MarkerObj pointers until null.
        unsafe {
            while !curr.is_null() {
                if (*curr).data.marker_id == Some(marker_id) {
                    (*curr).data.insertion_type = insertion_type == InsertionType::After;
                    return;
                }
                curr = (*curr).data.next_marker;
            }
        }
    }

    /// Return true iff a marker with `marker_id` is currently spliced
    /// into this buffer's chain. Used by BufferManager to pick the
    /// correct buffer when updating insertion type across buffers.
    pub fn has_marker(&self, marker_id: u64) -> bool {
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: chain walks live chain-owned MarkerObj pointers until null.
        unsafe {
            while !curr.is_null() {
                if (*curr).data.marker_id == Some(marker_id) {
                    return true;
                }
                curr = (*curr).data.next_marker;
            }
        }
        false
    }

    pub fn adjust_markers_for_insert(&self, insert_pos: usize, byte_len: usize, char_len: usize) {
        if byte_len == 0 {
            return;
        }
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: `curr` walks live chain-owned MarkerObj pointers from
        // `markers_head` until null. Each non-null node was spliced in via
        // `chain_splice_at_head`, so its `data.next_marker` is a valid
        // chain link or null.
        unsafe {
            while !curr.is_null() {
                let data = &mut (*curr).data;
                if data.bytepos > insert_pos {
                    data.bytepos += byte_len;
                    data.charpos += char_len;
                } else if data.bytepos == insert_pos && data.insertion_type {
                    // insertion_type == true means "after" in GNU terms.
                    data.bytepos += byte_len;
                    data.charpos += char_len;
                }
                curr = data.next_marker;
            }
        }
    }

    pub fn adjust_markers_for_delete(
        &self,
        start: usize,
        end: usize,
        start_char: usize,
        end_char: usize,
    ) {
        if start >= end {
            return;
        }
        let byte_len = end - start;
        let char_len = end_char - start_char;
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: same invariant as adjust_markers_for_insert.
        unsafe {
            while !curr.is_null() {
                let data = &mut (*curr).data;
                if data.bytepos >= end {
                    data.bytepos -= byte_len;
                    data.charpos -= char_len;
                } else if data.bytepos > start {
                    data.bytepos = start;
                    data.charpos = start_char;
                }
                curr = data.next_marker;
            }
        }
    }

    pub fn advance_markers_at(&self, pos: usize, byte_len: usize, char_len: usize) {
        if byte_len == 0 {
            return;
        }
        let storage = self.storage.borrow();
        let mut curr = storage.markers_head;
        // SAFETY: same invariant as adjust_markers_for_insert.
        unsafe {
            while !curr.is_null() {
                let data = &mut (*curr).data;
                if data.bytepos == pos {
                    data.bytepos += byte_len;
                    data.charpos += char_len;
                }
                curr = data.next_marker;
            }
        }
    }

    /// Unlink every chain node whose `MarkerData.buffer` is in `killed`.
    /// Sole entry point for kill-buffer marker cleanup: covers both the
    /// kill-root case (killed_set contains the root and all its
    /// indirects, so every marker on the shared chain matches) and the
    /// kill-indirect case (killed_set is just the dying indirect; root
    /// and sibling-indirect markers stay attached).
    pub fn remove_markers_for_buffers(&self, killed: &std::collections::HashSet<BufferId>) {
        let mut storage = self.storage.borrow_mut();
        let mut prev_slot: *mut *mut crate::tagged::header::MarkerObj = &mut storage.markers_head;
        // SAFETY: analogous to `chain_unlink`. Every non-null `*prev_slot`
        // was installed via `chain_splice_at_head`, i.e. a live GC-managed
        // MarkerObj with a valid `data.next_marker` link.
        unsafe {
            while !(*prev_slot).is_null() {
                let curr = *prev_slot;
                let data = &mut (*curr).data;
                let belongs_to_killed = data.buffer.map(|id| killed.contains(&id)).unwrap_or(false);
                if belongs_to_killed {
                    *prev_slot = data.next_marker;
                    data.next_marker = std::ptr::null_mut();
                    data.buffer = None;
                    data.bytepos = 0;
                    data.charpos = 0;
                } else {
                    prev_slot = &mut data.next_marker;
                }
            }
        }
    }

    /// Raw pointer to the `markers_head` slot inside this buffer's
    /// storage. ONLY for GC use — bypasses RefCell's runtime borrow
    /// checks. Callers must hold exclusive access to the tagged heap and
    /// have no outstanding storage borrows (GC is stop-the-world).
    ///
    /// Used by `TaggedHeap::unchain_dead_markers` to splice unmarked
    /// MarkerObj nodes out of the intrusive per-buffer chain before
    /// `sweep_objects` frees them.
    pub unsafe fn markers_head_slot_raw(&self) -> *mut *mut crate::tagged::header::MarkerObj {
        let storage_ptr: *mut BufferTextStorage = self.storage.as_ptr();
        unsafe { &mut (*storage_ptr).markers_head as *mut _ }
    }

    /// Splice `marker` at the head of this buffer's marker chain.
    /// Overwrites `marker.next_marker` with the old head.
    /// Caller sets `marker.buffer` / `marker.bytepos` / `marker.charpos` —
    /// this helper only manipulates chain topology.
    ///
    /// **Precondition:** `marker.next_marker` must be null (marker is not
    /// currently on any chain). Violating this silently truncates the
    /// other chain. `debug_assert!` enforces it in debug builds.
    pub fn chain_splice_at_head(&self, marker: *mut crate::tagged::header::MarkerObj) {
        let mut storage = self.storage.borrow_mut();
        let old_head = storage.markers_head;
        unsafe {
            // SAFETY: `marker` must be a live MarkerObj allocated via
            // TaggedHeap::alloc_marker and not currently on any other chain
            // (see precondition above). Writing through the pointer is sound
            // because the heap retains ownership for the lifetime of the
            // MarkerObj.
            debug_assert!(
                (*marker).data.next_marker.is_null(),
                "chain_splice_at_head: marker is already on a chain"
            );
            (*marker).data.next_marker = old_head;
        }
        storage.markers_head = marker;
    }

    /// Unlink `marker` from this buffer's chain. Silent no-op if not present.
    /// Does NOT clear `marker.buffer` / positions — caller owns semantic cleanup.
    ///
    /// Unlike GNU `unchain_marker` (marker.c:684), which hard-asserts that
    /// the marker is in the chain, we tolerate absent markers. This is
    /// defensive: callers currently include code paths that may be
    /// double-invoked during GC sweep and kill-buffer cleanup in T8/T9.
    pub fn chain_unlink(&self, marker: *mut crate::tagged::header::MarkerObj) {
        let mut storage = self.storage.borrow_mut();
        let mut prev_slot: *mut *mut crate::tagged::header::MarkerObj = &mut storage.markers_head;
        // SAFETY: `prev_slot` walks the intrusive chain starting at
        // `storage.markers_head`. Every non-null `*prev_slot` is a
        // `*mut MarkerObj` previously installed via `chain_splice_at_head`,
        // i.e. a live GC-managed allocation whose `.data.next_marker` is
        // the next chain slot. We never read past a null terminator, and
        // mutations only rewrite chain-owned `next_marker` fields.
        unsafe {
            while !(*prev_slot).is_null() {
                let curr = *prev_slot;
                if curr == marker {
                    *prev_slot = (*curr).data.next_marker;
                    (*curr).data.next_marker = std::ptr::null_mut();
                    return;
                }
                prev_slot = &mut (*curr).data.next_marker;
            }
        }
    }

    /// Walk the chain from head to tail, collecting raw pointers in order.
    /// Test-only helper.
    #[cfg(test)]
    pub fn chain_walk_collect(&self) -> Vec<*mut crate::tagged::header::MarkerObj> {
        let storage = self.storage.borrow();
        let mut out = Vec::new();
        let mut curr = storage.markers_head;
        // SAFETY: Same invariant as `chain_unlink` — `curr` walks live
        // chain-owned MarkerObj pointers from `storage.markers_head`
        // until a null terminator.
        unsafe {
            while !curr.is_null() {
                out.push(curr);
                curr = (*curr).data.next_marker;
            }
        }
        out
    }

    /// Convert a character position to a logical Emacs byte offset using an
    /// anchor-bracketed cached search. Mirrors GNU `buf_charpos_to_bytepos`
    /// (`src/marker.c:167`).
    pub fn buf_charpos_to_bytepos(&self, target: usize) -> usize {
        let storage = self.storage.borrow();
        let total_chars = storage.gap.char_count();
        let total_bytes = storage.gap.emacs_byte_len();

        if target >= total_chars {
            return storage.gap.len();
        }

        // Unibyte fast path: char == byte, no scan needed.
        if total_chars == total_bytes {
            return target;
        }

        // Wholesale-invalidate the anchor cache when the buffer changed.
        let current_key = (total_chars, total_bytes);
        if storage.anchor_cache_key.get() != current_key {
            storage.anchor_cache.borrow_mut().clear();
            storage.anchor_cache_key.set(current_key);
        }

        let mut best_below: (usize, usize) = (0, 0);
        let mut best_above: (usize, usize) = (total_chars, total_bytes);

        let gpt = storage.gap.gpt();
        let gpt_byte = storage.gap.gpt_byte();
        consider_anchor(target, (gpt, gpt_byte), &mut best_below, &mut best_above);

        let cached = storage.pos_cache.get();
        if cached.epoch_chars == total_chars
            && cached.epoch_bytes == total_bytes
            && (cached.epoch_chars != 0 || cached.epoch_bytes != 0)
        {
            consider_anchor(
                target,
                (cached.charpos, cached.bytepos),
                &mut best_below,
                &mut best_above,
            );
        }

        for &(cp, bp) in storage.anchor_cache.borrow().iter() {
            consider_anchor(target, (cp, bp), &mut best_below, &mut best_above);
        }

        let mut distance: usize = POSITION_DISTANCE_BASE;
        // T7: marker chain walk. The chain carries the same (char, byte)
        // pairs that the deleted Vec<MarkerEntry> used to.
        //
        // SAFETY: `curr` walks live chain-owned MarkerObj pointers from
        // `storage.markers_head` until null. Each non-null node was
        // spliced in via `chain_splice_at_head`, so its `data.next_marker`
        // is a valid chain link or null.
        let mut curr = storage.markers_head;
        unsafe {
            while !curr.is_null() {
                let data = &(*curr).data;
                consider_anchor(
                    target,
                    (data.charpos, data.bytepos),
                    &mut best_below,
                    &mut best_above,
                );
                if best_above.0.saturating_sub(target) < distance
                    || target.saturating_sub(best_below.0) < distance
                {
                    break;
                }
                distance = distance.saturating_add(POSITION_DISTANCE_INCR);
                curr = data.next_marker;
            }
        }

        let walked_below = target.saturating_sub(best_below.0);
        let walked_above = best_above.0.saturating_sub(target);
        let result = if walked_below <= walked_above {
            scan_forward(&storage.gap, best_below, target)
        } else {
            scan_backward(&storage.gap, best_above, target)
        };

        // Mirror GNU marker.c:238-241: insert an anchor when the scan actually
        // walked more than POSITION_ANCHOR_STRIDE positions.
        let walked = walked_below.min(walked_above);
        if walked > POSITION_ANCHOR_STRIDE {
            storage.anchor_cache.borrow_mut().push((target, result));
        }

        storage.pos_cache.set(PositionCache {
            epoch_chars: total_chars,
            epoch_bytes: total_bytes,
            charpos: target,
            bytepos: result,
        });
        result
    }

    /// Convert a logical Emacs byte position to a character position. Symmetric
    /// to `buf_charpos_to_bytepos` — shares the same anchor + cache machinery.
    pub fn buf_bytepos_to_charpos(&self, target: usize) -> usize {
        let storage = self.storage.borrow();
        let total_chars = storage.gap.char_count();
        let total_bytes = storage.gap.emacs_byte_len();

        if target >= total_bytes {
            return total_chars;
        }

        // Unibyte fast path: char == byte, no scan needed.
        if total_chars == total_bytes {
            return target;
        }

        // Wholesale-invalidate the anchor cache when the buffer changed.
        let current_key = (total_chars, total_bytes);
        if storage.anchor_cache_key.get() != current_key {
            storage.anchor_cache.borrow_mut().clear();
            storage.anchor_cache_key.set(current_key);
        }

        // Bracket is expressed as (bytepos, charpos) for this direction.
        let mut best_below: (usize, usize) = (0, 0);
        let mut best_above: (usize, usize) = (total_bytes, total_chars);

        let gpt = storage.gap.gpt();
        let gpt_byte = storage.gap.gpt_byte();
        consider_anchor_byte(target, (gpt_byte, gpt), &mut best_below, &mut best_above);

        let cached = storage.pos_cache.get();
        if cached.epoch_chars == total_chars
            && cached.epoch_bytes == total_bytes
            && (cached.epoch_chars != 0 || cached.epoch_bytes != 0)
        {
            consider_anchor_byte(
                target,
                (cached.bytepos, cached.charpos),
                &mut best_below,
                &mut best_above,
            );
        }

        for &(cp, bp) in storage.anchor_cache.borrow().iter() {
            consider_anchor_byte(target, (bp, cp), &mut best_below, &mut best_above);
        }

        let mut distance: usize = POSITION_DISTANCE_BASE;
        // T7: marker chain walk. See sibling comment in
        // `buf_charpos_to_bytepos` for the SAFETY rationale.
        let mut curr = storage.markers_head;
        unsafe {
            while !curr.is_null() {
                let data = &(*curr).data;
                consider_anchor_byte(
                    target,
                    (data.bytepos, data.charpos),
                    &mut best_below,
                    &mut best_above,
                );
                if best_above.0.saturating_sub(target) < distance
                    || target.saturating_sub(best_below.0) < distance
                {
                    break;
                }
                distance = distance.saturating_add(POSITION_DISTANCE_INCR);
                curr = data.next_marker;
            }
        }

        let walked_below = target.saturating_sub(best_below.0);
        let walked_above = best_above.0.saturating_sub(target);
        let result = if walked_below <= walked_above {
            scan_forward_bytes(&storage.gap, best_below, target)
        } else {
            scan_backward_bytes(&storage.gap, best_above, target)
        };

        // Mirror GNU marker.c:238-241: insert an anchor when the scan actually
        // walked more than POSITION_ANCHOR_STRIDE positions.
        // Store as (charpos, bytepos) like the char→byte direction to keep
        // anchor_cache entries in one canonical order.
        let walked = walked_below.min(walked_above);
        if walked > POSITION_ANCHOR_STRIDE {
            storage.anchor_cache.borrow_mut().push((result, target));
        }

        storage.pos_cache.set(PositionCache {
            epoch_chars: total_chars,
            epoch_bytes: total_bytes,
            charpos: result,
            bytepos: target,
        });
        result
    }

    #[cfg(test)]
    pub fn anchor_cache_len(&self) -> usize {
        self.storage.borrow().anchor_cache.borrow().len()
    }
}

impl fmt::Display for BufferText {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.storage.borrow().gap.fmt(f)
    }
}

impl fmt::Debug for BufferText {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BufferText")
            .field("len", &self.len())
            .field("chars", &self.char_count())
            .finish()
    }
}

// ---------------------------------------------------------------------------
// Position conversion helpers
// ---------------------------------------------------------------------------

/// GNU `marker.c:162` — initial bracket-bail distance.
const POSITION_DISTANCE_BASE: usize = 50;
/// GNU `marker.c:162` — bracket-bail distance grows by this per marker checked.
const POSITION_DISTANCE_INCR: usize = 50;
/// Auto-insert an anchor when a scan walks more than this many positions.
/// Mirrors GNU `marker.c:238-241` (5000-char threshold).
const POSITION_ANCHOR_STRIDE: usize = 5000;

/// Update `(best_below, best_above)` in place using a new `(charpos, bytepos)` anchor.
fn consider_anchor(
    target: usize,
    anchor: (usize, usize),
    best_below: &mut (usize, usize),
    best_above: &mut (usize, usize),
) {
    if anchor.0 <= target && anchor.0 > best_below.0 {
        *best_below = anchor;
    }
    if anchor.0 >= target && anchor.0 < best_above.0 {
        *best_above = anchor;
    }
}

/// Walk forward from `anchor = (charpos, bytepos)` to reach `target` chars.
/// Returns the byte position.
fn scan_forward(gap: &GapBuffer, anchor: (usize, usize), target: usize) -> usize {
    let (mut cp, mut bp) = anchor;
    while cp < target {
        if !gap.is_multibyte() {
            bp += 1;
            cp += 1;
            continue;
        }
        let mut tmp = [0u8; crate::emacs_core::emacs_char::MAX_MULTIBYTE_LENGTH];
        let available = (gap.len() - bp).min(tmp.len());
        for (i, slot) in tmp[..available].iter_mut().enumerate() {
            *slot = gap.byte_at(bp + i);
        }
        let (_, len) = crate::emacs_core::emacs_char::string_char(&tmp[..available]);
        bp += len;
        cp += 1;
    }
    bp
}

/// Walk backward from `anchor = (charpos, bytepos)` to reach `target` chars.
/// Returns the byte position.
fn scan_backward(gap: &GapBuffer, anchor: (usize, usize), target: usize) -> usize {
    let (mut cp, mut bp) = anchor;
    while cp > target {
        if !gap.is_multibyte() {
            bp -= 1;
            cp -= 1;
            continue;
        }
        let mut prev = bp - 1;
        while prev > 0 && (gap.byte_at(prev) & 0xC0) == 0x80 {
            prev -= 1;
        }
        bp = prev;
        cp -= 1;
    }
    bp
}

/// Update `(best_below, best_above)` in place using a new `(bytepos, charpos)` anchor.
fn consider_anchor_byte(
    target: usize,
    anchor: (usize, usize), // (bytepos, charpos)
    best_below: &mut (usize, usize),
    best_above: &mut (usize, usize),
) {
    if anchor.0 <= target && anchor.0 > best_below.0 {
        *best_below = anchor;
    }
    if anchor.0 >= target && anchor.0 < best_above.0 {
        *best_above = anchor;
    }
}

/// Walk forward from `anchor = (bytepos, charpos)` to reach `target` bytepos.
/// Returns the char position.
fn scan_forward_bytes(gap: &GapBuffer, anchor: (usize, usize), target: usize) -> usize {
    let (mut bp, mut cp) = anchor;
    while bp < target {
        if !gap.is_multibyte() {
            bp += 1;
            cp += 1;
            continue;
        }
        let mut tmp = [0u8; crate::emacs_core::emacs_char::MAX_MULTIBYTE_LENGTH];
        let available = (gap.len() - bp).min(tmp.len());
        for (i, slot) in tmp[..available].iter_mut().enumerate() {
            *slot = gap.byte_at(bp + i);
        }
        let (_, len) = crate::emacs_core::emacs_char::string_char(&tmp[..available]);
        bp += len;
        cp += 1;
    }
    cp
}

/// Walk backward from `anchor = (bytepos, charpos)` to reach `target` bytepos.
/// Returns the char position.
fn scan_backward_bytes(gap: &GapBuffer, anchor: (usize, usize), target: usize) -> usize {
    let (mut bp, mut cp) = anchor;
    while bp > target {
        if !gap.is_multibyte() {
            bp -= 1;
            cp -= 1;
            continue;
        }
        let mut prev = bp - 1;
        while prev > 0 && (gap.byte_at(prev) & 0xC0) == 0x80 {
            prev -= 1;
        }
        bp = prev;
        cp -= 1;
    }
    cp
}

#[cfg(test)]
#[path = "buffer_text_test.rs"]
mod tests;

#[cfg(test)]
#[path = "buffer_text_chain_test.rs"]
mod chain_test;