elisprs 0.1.20

Emacs Lisp as a fusevm frontend: a reader and compiler to fusevm::Chunk on an ElispHost object heap, with no bespoke VM or JIT
Documentation
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
//! Lisp that runs around a buffer-modifying or field-aware primitive.
//!
//! A subr body holds the host borrow, so it cannot call Lisp. The C primitives
//! these port do call Lisp in the middle of their work: insdel.c's
//! `prepare_to_modify_buffer` runs `before-change-functions` (and
//! `first-change-hook`) before the text changes and `signal_after_change` runs
//! `after-change-functions` after it; editfns.c's `line-beginning-position`
//! and friends call `constrain-to-field`; `insert-char` with INHERIT and
//! `indent-to` inherit text properties. [`around`] is called by
//! `host::call_function` for every subr, after its arity check and outside the
//! borrow; for the primitives below it does the Lisp part itself and runs the
//! subr body for the rest. A user's redefinition of one of these names is a
//! closure, not a subr, so it never comes here.
//!
//! Each modifying primitive's region is the one its C computes for
//! `prepare_to_modify_buffer`, taken after argument validation: an invalid
//! call runs the body alone, which signals as before and runs no hook.

use crate::builtins;
use crate::host::{call_function, el_nil, el_truthy, with_host, ElispHost, SubrFn};
use fusevm::Value;

type R = Result<Value, String>;

/// The Lisp-side part of NAME, when NAME is one of the primitives this module
/// wraps; `None` for every other subr, which then runs as usual.
pub fn around(name: &str, f: SubrFn, args: &[Value]) -> Option<R> {
    let r = match name {
        "insert" | "--insert-before-markers--" => insert(f, args),
        "insert-char" => insert_char(f, args),
        "indent-to" => indent_to(args),
        "delete-region" | "delete-and-extract-region" => delete_region(f, args),
        "delete-char" => delete_char(f, args),
        "erase-buffer" => erase_buffer(f, args),
        "elisprs--replace-chars-in-place" => replace_chars(f, args),
        "transpose-regions" => transpose_regions(f, args),
        "replace-match" => replace_match(f, args),
        "insert-file-contents" => {
            let pt = point();
            modify(pt, pt, None, || run(f, args))
        }
        "put-text-property"
        | "add-text-properties"
        | "remove-text-properties"
        | "set-text-properties"
        | "add-face-text-property" => text_props(name, f, args),
        "line-beginning-position" => line_position(f, args, true),
        "line-end-position" => line_position(f, args, false),
        "beginning-of-line" => line_motion(f, args, true),
        "end-of-line" => line_motion(f, args, false),
        "set-window-buffer" => set_window_buffer(f, args),
        "princ" | "prin1" | "print" | "terpri" | "write-char" => print_to_buffer(name, f, args),
        "move-to-column" => move_to_column(f, args),
        "call-process" => call_process(f, args),
        "subst-char-in-region" => subst_char(f, args),
        "read" => read_stream(f, args),
        "kill-buffer" => kill_buffer(f, args),
        "set-window-configuration" => set_window_configuration(f, args),
        "split-window-internal" => split_window_internal(f, args),
        "delete-other-windows-internal" => crate::window::delete_other_windows_internal(args),
        _ => return None,
    };
    Some(r)
}

/// Run the subr body itself.
fn run(f: SubrFn, args: &[Value]) -> R {
    with_host(|h| f(h, args))
}

fn point() -> usize {
    with_host(|h| h.cur_buf_ref().point)
}

/// The value of variable NAME in the current buffer, nil when void.
fn var(h: &mut ElispHost, name: &str) -> Value {
    let sym = h.intern(name);
    h.get_value(&sym).unwrap_or(Value::Undef)
}

/// What a change to the current buffer has Lisp to run for
/// (`prepare_to_modify_buffer_1` / `signal_after_change`).
struct ModState {
    inhibit: bool,
    /// `run_undoable_change`, `verify_interval_modification`, or
    /// `signal_before_change` has something to do.
    prepare: bool,
}

fn mod_state(h: &mut ElispHost) -> ModState {
    let inhibit = el_truthy(&var(h, "inhibit-modification-hooks"));
    let b = h.cur_buf_ref();
    let unmodified = b.mods.save_modiff >= b.mods.modiff;
    let (intervals, overlays) = (b.has_intervals, !b.overlays.is_empty());
    let undo = !matches!(var(h, "buffer-undo-list"), Value::Bool(true));
    let before = !inhibit
        && (el_truthy(&var(h, "before-change-functions"))
            || (unmodified && el_truthy(&var(h, "first-change-hook")))
            || overlays);
    ModState {
        inhibit,
        prepare: undo || intervals || before,
    }
}

/// insdel.c's frame around one change of the text `[beg, end)`.
fn modify(beg: usize, end: usize, buffer: Option<Value>, body: impl FnOnce() -> R) -> R {
    modify_with(beg, end, buffer, None, body)
}

/// `modify`, with the after-change span AFTER `(BEG END OLD-LEN)` given
/// rather than taken from the size change (`subst-char-in-region` reports
/// only the stretch it changed).
fn modify_with(
    beg: usize,
    end: usize,
    buffer: Option<Value>,
    after: Option<(usize, usize, usize)>,
    body: impl FnOnce() -> R,
) -> R {
    change(beg, end, buffer, after, true, body)
}

/// [`modify_with`]; with TEXT_CHANGES false nothing is inserted or deleted
/// and only the after-change half runs, as print.c's `print_finish` calls
/// `signal_after_change` after an `insert_1_both` of no characters (which
/// returns before `prepare_to_modify_buffer`).
fn change(
    beg: usize,
    end: usize,
    buffer: Option<Value>,
    after: Option<(usize, usize, usize)>,
    text_changes: bool,
    body: impl FnOnce() -> R,
) -> R {
    let in_buffer = |f: &mut dyn FnMut(&mut ElispHost) -> ModState| {
        with_host(|h| {
            let saved = h.current;
            if let Some(bi) = buffer.as_ref().and_then(|b| h.buffer_slot(b)) {
                h.current = bi;
            }
            let r = f(h);
            h.current = saved;
            r
        })
    };
    // No text changes: no `prepare_to_modify_buffer` and no `deactivate-mark`.
    let state = if text_changes {
        in_buffer(&mut mod_state)
    } else {
        ModState {
            inhibit: true,
            prepare: false,
        }
    };
    let buf_arg = buffer.clone().unwrap_or(Value::Undef);
    if state.prepare {
        let fname = with_host(|h| h.intern("elisprs--prepare-to-modify"));
        call_function(
            &fname,
            &[
                Value::Int(beg as i64),
                Value::Int(end as i64),
                buf_arg.clone(),
            ],
        )?;
    }
    if !state.inhibit {
        in_buffer(&mut |h| {
            let sym = h.intern("deactivate-mark");
            let _ = h.set_value(&sym, Value::Bool(true));
            ModState {
                inhibit: false,
                prepare: false,
            }
        });
    }
    let size = |h: &mut ElispHost| -> i64 {
        let saved = h.current;
        if let Some(bi) = buffer.as_ref().and_then(|b| h.buffer_slot(b)) {
            h.current = bi;
        }
        let n = h.cur_buf_ref().text.len() as i64;
        h.current = saved;
        n
    };
    let size_before = with_host(size);
    let r = body()?;
    let size_after = with_host(size);
    let run_after = with_host(|h| {
        let saved = h.current;
        if let Some(bi) = buffer.as_ref().and_then(|b| h.buffer_slot(b)) {
            h.current = bi;
        }
        let b = h.cur_buf_ref();
        let (intervals, overlays) = (b.has_intervals, !b.overlays.is_empty());
        let out = !el_truthy(&var(h, "inhibit-modification-hooks"))
            && (!el_nil(&var(h, "after-change-functions")) || intervals || overlays);
        h.current = saved;
        out
    });
    if run_after {
        let (a_beg, a_end, a_len) = after.map_or(
            (
                beg as i64,
                end as i64 + (size_after - size_before),
                (end - beg) as i64,
            ),
            |(b, e, l)| (b as i64, e as i64, l as i64),
        );
        let fname = with_host(|h| h.intern("elisprs--signal-after-change"));
        call_function(
            &fname,
            &[
                Value::Int(a_beg),
                Value::Int(a_end),
                Value::Int(a_len),
                buf_arg,
            ],
        )?;
    }
    Ok(r)
}

/// The number of characters inserting V adds, `None` when V is not a string
/// or character (the body then signals).
fn insert_len(h: &ElispHost, v: &Value) -> Option<usize> {
    if let Some(s) = h.str_text(v) {
        return Some(s.chars().count());
    }
    matches!(v, Value::Int(_)).then_some(1)
}

/// `insert`: each argument is its own insertion (`general_insert_function`),
/// with its own pair of hook calls; an empty one runs none.
fn insert(f: SubrFn, args: &[Value]) -> R {
    for arg in args {
        let one = std::slice::from_ref(arg);
        match with_host(|h| insert_len(h, arg)) {
            Some(n) if n > 0 => {
                let pt = point();
                modify(pt, pt, None, || run(f, one))?;
            }
            _ => {
                run(f, one)?;
            }
        }
    }
    Ok(Value::Undef)
}

/// `insert-char`: COUNT copies of CHARACTER, inheriting the surrounding text
/// properties when INHERIT (as `insert-and-inherit` does), no hooks when COUNT
/// is not positive.
fn insert_char(f: SubrFn, args: &[Value]) -> R {
    let (Some(Value::Int(c)), count) = (args.first(), args.get(1)) else {
        return run(f, args);
    };
    let count = match count {
        None | Some(Value::Undef) | Some(Value::Bool(false)) => 1,
        Some(Value::Int(n)) => *n,
        Some(_) => return run(f, args),
    };
    if count <= 0 || char::from_u32(*c as u32).is_none() {
        return run(f, args);
    }
    let inherit = args.get(2).is_some_and(el_truthy);
    if inherit && with_host(|h| h.cur_buf_ref().has_intervals) {
        let ch = char::from_u32(*c as u32).unwrap_or('\u{fffd}');
        let (fname, text) = with_host(|h| {
            let text: String = std::iter::repeat_n(ch, count as usize).collect();
            (h.intern("insert-and-inherit"), h.new_string(text))
        });
        call_function(&fname, &[text])?;
        return Ok(Value::Undef);
    }
    let pt = point();
    modify(pt, pt, None, || run(f, args))
}

/// `indent-to`: `Findent_to` inserts its tabs, then its spaces, each with
/// `Finsert_char (…, Qt)`.
fn indent_to(args: &[Value]) -> R {
    let (mincol, tabs, spaces) = with_host(|h| builtins::indent_to_plan(h, args))?;
    for (c, n) in [('\t', tabs), (' ', spaces)] {
        if n > 0 {
            let ins = [
                Value::Int(c as i64),
                Value::Int(n as i64),
                Value::Bool(true),
            ];
            insert_char(builtins::insert_char, &ins)?;
        }
    }
    Ok(Value::Int(mincol))
}

/// `delete-region` / `delete-and-extract-region`: `validate_region`, then a
/// change over `[START, END)` unless it is empty.
fn delete_region(f: SubrFn, args: &[Value]) -> R {
    let region = with_host(|h| builtins::validate_region(h, &args[0], &args[1]));
    match region {
        Ok((lo, hi)) if lo < hi => modify(lo, hi, None, || run(f, args)),
        _ => run(f, args),
    }
}

/// `delete-char`: N characters from point; KILLFLAG kills them instead
/// (`kill-forward-chars`, cmds.c).
fn delete_char(f: SubrFn, args: &[Value]) -> R {
    let Some(Value::Int(n)) = args.first() else {
        return run(f, args);
    };
    let n = *n;
    let (pt, begv, zv) = with_host(|h| {
        let b = h.cur_buf_ref();
        (b.point as i64, b.begv as i64, b.zv as i64)
    });
    let pos = pt + n;
    if pos < begv || pos > zv {
        return run(f, args);
    }
    if args.get(1).is_some_and(el_truthy) {
        let fname = with_host(|h| h.intern("kill-forward-chars"));
        return call_function(&fname, &[Value::Int(n)]);
    }
    let (lo, hi) = if n < 0 { (pos, pt) } else { (pt, pos) };
    if lo == hi {
        return run(f, args);
    }
    modify(lo as usize, hi as usize, None, || run(f, args))
}

/// `erase-buffer`: `Fwiden`, then a change over the whole text.
fn erase_buffer(f: SubrFn, args: &[Value]) -> R {
    let len = with_host(|h| {
        h.widen();
        h.cur_buf_ref().text.len()
    });
    if len == 0 {
        return run(f, args);
    }
    modify(1, len + 1, None, || run(f, args))
}

/// `elisprs--replace-chars-in-place`: `modify_text` over `[START, END)` when
/// the replacement has as many characters.
fn replace_chars(f: SubrFn, args: &[Value]) -> R {
    let region = with_host(|h| {
        let (lo, hi) = builtins::validate_region(h, &args[0], &args[1]).ok()?;
        let n = h.str_text(&args[2])?.chars().count();
        (lo < hi && n == hi - lo).then_some((lo, hi))
    });
    match region {
        Some((lo, hi)) => modify(lo, hi, None, || run(f, args)),
        None => run(f, args),
    }
}

/// `transpose-regions`: `modify_text (start1, end2)` over the span from the
/// earlier region's start to the later one's end.
fn transpose_regions(f: SubrFn, args: &[Value]) -> R {
    if args.len() < 4 {
        return run(f, args);
    }
    let span = with_host(|h| {
        let (mut s1, mut e1) = builtins::validate_region(h, &args[0], &args[1]).ok()?;
        let (mut s2, mut e2) = builtins::validate_region(h, &args[2], &args[3]).ok()?;
        if s2 < e1 {
            std::mem::swap(&mut s1, &mut s2);
            std::mem::swap(&mut e1, &mut e2);
        }
        let noop = (s1 == e1 || s2 == e2) && e1 == s2;
        (s2 >= e1 && !noop).then_some((s1, e2))
    });
    match span {
        Some((lo, hi)) => modify(lo, hi, None, || run(f, args)),
        None => run(f, args),
    }
}

/// `replace-match` in the buffer: `replace_range` over the matched SUBEXP.
fn replace_match(f: SubrFn, args: &[Value]) -> R {
    if args.get(3).is_some_and(el_truthy) {
        return run(f, args);
    }
    let span = with_host(|h| {
        let subexp = match args.get(4) {
            None | Some(Value::Undef) | Some(Value::Bool(false)) => 0,
            Some(Value::Int(n)) if *n >= 0 => *n as usize,
            Some(_) => return None,
        };
        let (b, e) = h
            .match_data
            .as_ref()?
            .spans
            .get(subexp)
            .copied()
            .flatten()?;
        let buf = h.cur_buf_ref();
        (buf.begv <= b && b <= e && e <= buf.zv).then_some((b, e))
    });
    match span {
        Some((b, e)) => modify(b, e, None, || run(f, args)),
        None => run(f, args),
    }
}

/// The text-property primitives on a buffer: `modify_text_properties` over
/// `[START, END)` when the call changes something (textprop.c returns early,
/// running no hook, when it would not).
fn text_props(name: &str, f: SubrFn, args: &[Value]) -> R {
    let object_index = match name {
        "put-text-property" => 4,
        "set-text-properties" => 3,
        "add-face-text-property" => 4,
        _ => 3,
    };
    let span = with_host(|h| builtins::text_prop_change_span(h, name, args, object_index));
    match span {
        Some((bi, lo, hi)) => {
            let buffer = with_host(|h| h.buffers[bi].self_obj.clone());
            modify(lo, hi, Some(buffer), || run(f, args))
        }
        None => run(f, args),
    }
}

/// Whether the current buffer can have a `field` property anywhere: without
/// text properties or overlays, `constrain-to-field` cannot move a position.
fn may_have_fields(h: &ElispHost) -> bool {
    let b = h.cur_buf_ref();
    b.has_intervals || !b.overlays.is_empty()
}

/// `constrain-to-field` of NEW-POS against point, as editfns.c's line
/// functions call it (ONLY-IN-LINE t, no capture property).
fn constrain(new_pos: Value, escape: bool) -> R {
    let (fname, pt) = with_host(|h| (h.intern("constrain-to-field"), h.cur_buf_ref().point));
    call_function(
        &fname,
        &[
            new_pos,
            Value::Int(pt as i64),
            Value::Bool(escape),
            Value::Bool(true),
            Value::Undef,
        ],
    )
}

/// ESCAPE-FROM-EDGE for `line-beginning-position`: `bol`'s count is non-zero,
/// i.e. N is given and is not 1.
fn bol_escapes(args: &[Value]) -> bool {
    match args.first() {
        None | Some(Value::Undef) | Some(Value::Bool(false)) => false,
        Some(Value::Int(n)) => *n != 1,
        Some(_) => true,
    }
}

/// `line-beginning-position` / `line-end-position`: the `pos-bol` / `pos-eol`
/// position constrained to point's field.
fn line_position(f: SubrFn, args: &[Value], bol: bool) -> R {
    let pos = run(f, args)?;
    if !with_host(|h| may_have_fields(h)) {
        return Ok(pos);
    }
    constrain(pos, bol && bol_escapes(args))
}

/// `beginning-of-line` / `end-of-line` (cmds.c): N nil is 1, anything else
/// must be a fixnum; point goes to the field-constrained line position.
fn line_motion(f: SubrFn, args: &[Value], bol: bool) -> R {
    let (orig, may) = with_host(|h| (h.cur_buf_ref().point, may_have_fields(h)));
    run(f, args)?;
    if !may {
        return Ok(Value::Undef);
    }
    let target = with_host(|h| {
        let p = h.cur_buf_ref().point;
        h.cur_buf().point = orig;
        p
    });
    let pos = constrain(Value::Int(target as i64), bol && bol_escapes(args))?;
    with_host(|h| {
        if let Value::Int(p) = pos {
            let b = h.cur_buf();
            b.point = (p as usize).clamp(b.begv, b.zv);
        }
    });
    Ok(Value::Undef)
}

/// `set-window-buffer`: before WINDOW shows a different buffer, window.c
/// calls `record-window-buffer` (unless the window is strongly dedicated,
/// which the body then refuses).
fn set_window_buffer(f: SubrFn, args: &[Value]) -> R {
    let changes = with_host(|h| {
        let w = match args.first().map(|v| h.obj(v)) {
            None | Some(None) => Some(h.selected_window),
            Some(Some(crate::host::Obj::Window(w))) => Some(*w),
            _ => None,
        }?;
        let wd = &h.windows[w];
        let current = wd.buffer?;
        let target = h.resolve_buffer(args.get(1)?)?;
        (current != target && !matches!(wd.dedicated, Value::Bool(true)))
            .then(|| wd.self_obj.clone())
    });
    if let Some(window) = changes {
        let fname = with_host(|h| h.intern("record-window-buffer"));
        call_function(&fname, &[window])?;
    }
    let r = run(f, args)?;
    let window = args.first().cloned().unwrap_or(Value::Undef);
    run_window_scroll_functions(window)?;
    Ok(r)
}

/// `set_window_buffer` with RUN_HOOKS_P: `window-scroll-functions` for
/// WINDOW, which now shows its new buffer.
fn run_window_scroll_functions(window: Value) -> R {
    let fname = with_host(|h| h.intern("run-window-scroll-functions"));
    call_function(&fname, &[window])
}

/// `split-window-internal`: the new window's `set_window_buffer` runs
/// `window-scroll-functions`.
fn split_window_internal(f: SubrFn, args: &[Value]) -> R {
    let new = run(f, args)?;
    run_window_scroll_functions(new.clone())?;
    Ok(new)
}

/// The printing primitives with a buffer or marker destination: print.c
/// collects the text and inserts it once (`PRINTFINISH`), one change at the
/// destination's insertion position in its buffer; printing nothing runs only
/// the after-change half.
fn print_to_buffer(name: &str, f: SubrFn, args: &[Value]) -> R {
    let pcf = usize::from(name != "terpri");
    let target = with_host(|h| match builtins::print_dest(h, args.get(pcf)) {
        Ok(builtins::PrintDest::Buffer(bi)) => Some((bi, h.buffers[bi].point)),
        Ok(builtins::PrintDest::Marker(m)) => match h.obj(&m) {
            Some(crate::host::Obj::Marker(md)) => {
                let md = md.borrow();
                md.buffer.map(|bi| (bi, md.pos))
            }
            _ => None,
        },
        _ => None,
    });
    match target {
        Some((bi, pos)) => {
            // `print_finish` inserts what was printed and then always runs
            // `signal_after_change`; an empty output inserts nothing, so it
            // runs no before-change half.
            let (buffer, inserts) = with_host(|h| {
                let b = &h.buffers[bi];
                let inserts = match name {
                    "terpri" if args.get(1).is_some_and(el_truthy) => {
                        // Fterpri's ENSURE: `Fbolp` at the print position.
                        !(pos <= b.begv || b.text.get(pos - 2) == Some(&'\n'))
                    }
                    "princ" => match args.first() {
                        Some(v) => h
                            .str_text(v)
                            .map(str::to_string)
                            .or_else(|| h.sym_name(v))
                            .is_none_or(|s| !s.is_empty()),
                        None => true,
                    },
                    _ => true,
                };
                (b.self_obj.clone(), inserts)
            });
            change(pos, pos, Some(buffer), None, inserts, || run(f, args))
        }
        None => run(f, args),
    }
}

/// `move-to-column` with FORCE (indent.c): a tab straddling COLUMN becomes
/// spaces — `insert-char` of spaces before it, the tab deleted, `indent-to`
/// its old end column — and with FORCE t a short line is padded with
/// `indent-to`. Each step is a change of its own.
fn move_to_column(f: SubrFn, args: &[Value]) -> R {
    let (Some(Value::Int(goal)), Some(force)) = (args.first(), args.get(1)) else {
        return run(f, args);
    };
    if *goal < 0 || el_nil(force) {
        return run(f, args);
    }
    let goal = *goal;
    let force_t = matches!(force, Value::Bool(true));
    let (pos, mut col, prev_pos, prev_col) = with_host(|h| {
        let zv = h.cur_buf_ref().zv;
        let r = builtins::scan_for_column(h, zv, goal);
        h.cur_buf().point = r.0;
        r
    });
    if col > goal {
        let tab = with_host(|h| h.cur_buf_ref().text[prev_pos - 1] == '\t');
        if tab && prev_col < goal && prev_pos < pos {
            with_host(|h| h.cur_buf().point = prev_pos);
            let spaces = [
                Value::Int(' ' as i64),
                Value::Int(goal - prev_col),
                Value::Bool(true),
            ];
            insert_char(builtins::insert_char, &spaces)?;
            let at = point();
            let del = [Value::Int(at as i64), Value::Int(at as i64 + 1)];
            delete_region(builtins::delete_region, &del)?;
            indent_to(&[Value::Int(col)])?;
            with_host(|h| h.cur_buf().point = at);
            col = goal;
        }
    }
    if col < goal && force_t {
        col = goal;
        indent_to(&[Value::Int(col)])?;
    }
    Ok(Value::Int(col))
}

/// `call-process` inserting its output: one change at point of the buffer
/// the output goes to (DESTINATION t is the current buffer).
fn call_process(f: SubrFn, args: &[Value]) -> R {
    let inserts = args
        .get(2)
        .is_some_and(|d| el_truthy(d) && !matches!(d, Value::Int(0)));
    if !inserts {
        return run(f, args);
    }
    let pt = point();
    modify(pt, pt, None, || run(f, args))
}

/// `subst-char-in-region`: `modify_text` from the first changed character to
/// END before, `signal_after_change` over the changed stretch after. With
/// NOUNDO the C sets `buffer-undo-list` to t for the duration, so nothing is
/// recorded (the first-change record and `undo-auto--undoable-change` neither).
fn subst_char(f: SubrFn, args: &[Value]) -> R {
    let Some((_, hi, first, last)) = with_host(|h| builtins::subst_char_span(h, args)) else {
        return run(f, args);
    };
    let noundo = args.get(4).is_some_and(el_truthy);
    let saved = noundo.then(|| {
        with_host(|h| {
            let sym = h.intern("buffer-undo-list");
            let old = h.get_value(&sym).unwrap_or(Value::Undef);
            let _ = h.set_value(&sym, Value::Bool(true));
            old
        })
    });
    let r = modify_with(first, hi, None, Some((first, last, last - first)), || {
        run(f, args)
    });
    if let Some(old) = saved {
        with_host(|h| {
            let sym = h.intern("buffer-undo-list");
            let _ = h.set_value(&sym, old);
        });
    }
    r
}

/// `read` from a function: `crate::lread_fn` drives lread.c's `read0`
/// through the function's `readchar` / `unreadchar` calls. A nil STREAM is
/// `standard-input`, as in `Fread`.
fn read_stream(f: SubrFn, args: &[Value]) -> R {
    let stream = match args.first() {
        Some(s) if !el_nil(s) => s.clone(),
        _ => with_host(|h| {
            let sym = h.intern("standard-input");
            h.get_value(&sym).unwrap_or(Value::Bool(true))
        }),
    };
    let is_function = with_host(|h| {
        !el_nil(&stream)
            && !matches!(stream, Value::Bool(true))
            && !h.is_string(&stream)
            && !matches!(
                h.obj(&stream),
                Some(crate::host::Obj::Buffer(_)) | Some(crate::host::Obj::Marker(_))
            )
            && h.sym_name(&stream).as_deref() != Some("read-char")
    });
    if !is_function {
        return run(f, args);
    }
    crate::lread_fn::read_from_function(&stream)
}

/// `set-window-configuration` (window.c): `record-window-buffer` for every
/// window that is to show another buffer, the restore itself, then the Lisp
/// that resizes a root window with children and deletes dedicated windows
/// whose buffer was killed.
fn set_window_configuration(f: SubrFn, args: &[Value]) -> R {
    let windows = with_host(|h| {
        args.first()
            .map(|c| crate::window::windows_changing_buffer(h, c))
            .unwrap_or_default()
    });
    let record = with_host(|h| h.intern("record-window-buffer"));
    for w in windows {
        call_function(&record, &[w])?;
    }
    let r = run(f, args)?;
    crate::window::after_set_window_configuration()?;
    Ok(r)
}

/// buffer.c `Fkill_buffer`'s Lisp: with the buffer current,
/// `kill-buffer-query-functions` may refuse and `kill-buffer-hook` runs
/// (neither for a buffer made with INHIBIT-BUFFER-HOOKS); then window.el's
/// `replace-buffer-in-windows` gives its windows other buffers. The subr
/// does the killing.
fn kill_buffer(f: SubrFn, args: &[Value]) -> R {
    let target = with_host(|h| match args.first() {
        Some(v) if el_truthy(v) => h.resolve_buffer(v),
        _ => Some(h.current),
    });
    let Some(b) = target.filter(|&b| with_host(|h| h.buffers[b].name.is_some())) else {
        return run(f, args);
    };
    let (buffer, inhibit) =
        with_host(|h| (h.buffers[b].self_obj.clone(), h.buffers[b].inhibit_hooks));
    if !inhibit {
        let saved = with_host(|h| std::mem::replace(&mut h.current, b));
        let restore = |saved: usize| {
            with_host(|h| {
                if h.buffers[saved].name.is_some() {
                    h.current = saved;
                }
            })
        };
        let query = with_host(|h| {
            let run = h.intern("run-hook-with-args-until-failure");
            let hook = h.intern("kill-buffer-query-functions");
            (run, hook)
        });
        let ok = call_function(&query.0, &[query.1]);
        match ok {
            Ok(v) if el_nil(&v) => {
                restore(saved);
                return Ok(Value::Undef);
            }
            Ok(_) => {}
            Err(e) => {
                restore(saved);
                return Err(e);
            }
        }
        let alive = with_host(|h| h.buffers[b].name.is_some());
        if alive {
            let (run_hooks, hook) =
                with_host(|h| (h.intern("run-hooks"), h.intern("kill-buffer-hook")));
            if let Err(e) = call_function(&run_hooks, &[hook]) {
                restore(saved);
                return Err(e);
            }
        }
        restore(saved);
        if !with_host(|h| h.buffers[b].name.is_some()) {
            return Ok(Value::Bool(true));
        }
    }
    let (fbound, minibuf) = with_host(|h| {
        let sym = h.intern("replace-buffer-in-windows");
        let mini = h.frames[h.selected_frame].mini;
        (h.is_fbound(&sym), h.windows[mini].buffer == Some(b))
    });
    if minibuf {
        return Ok(Value::Undef);
    }
    if fbound {
        let fname = with_host(|h| h.intern("replace-buffer-in-windows"));
        call_function(&fname, std::slice::from_ref(&buffer))?;
        if !with_host(|h| h.buffers[b].name.is_some()) {
            return Ok(Value::Bool(true));
        }
    }
    // The buffer named at the start: the hooks may have made another current.
    run(f, std::slice::from_ref(&buffer))
}