deckcraft-model 0.6.1

DeckCraft document model: presentations, masters, layouts, slides, shapes, text, themes, transitions, animations
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
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
//! Text editing on a [`TextBody`]: positions are (paragraph, character offset). Inserting text
//! with `\n` splits paragraphs; formatting a range splits runs at its ends.

use crate::text::{Paragraph, Run, RunKind, RunProps, TextBody};
use crate::words;

/// (paragraph, char offset in the paragraph).
pub type Pos = (usize, usize);

fn clamp(body: &TextBody, p: Pos) -> Pos {
    if body.paragraphs.is_empty() {
        return (0, 0);
    }
    let pi = p.0.min(body.paragraphs.len() - 1);
    let n = body
        .paragraphs
        .get(pi)
        .map(Paragraph::char_len)
        .unwrap_or(0);
    (pi, p.1.min(n))
}

fn order(a: Pos, b: Pos) -> (Pos, Pos) {
    if a <= b { (a, b) } else { (b, a) }
}

/// Split the paragraph's runs so a run boundary falls at char `at`; returns the index of the
/// first run starting at or after `at`.
fn split_at(p: &mut Paragraph, at: usize) -> usize {
    let mut pos = 0;
    let mut i = 0;
    while i < p.runs.len() {
        let len = p.runs.get(i).map(Run::char_len).unwrap_or(0);
        if pos == at {
            return i;
        }
        if at < pos + len {
            let Some(r) = p.runs.get(i) else { return i };
            if r.kind != RunKind::Text {
                return i + 1;
            }
            let k = at - pos;
            let byte = r
                .text
                .char_indices()
                .nth(k)
                .map(|(b, _)| b)
                .unwrap_or(r.text.len());
            let (a, b) = r.text.split_at(byte);
            let second = Run {
                text: b.to_string(),
                props: r.props.clone(),
                kind: RunKind::Text,
            };
            let first = a.to_string();
            if let Some(r) = p.runs.get_mut(i) {
                r.text = first;
            }
            p.runs.insert(i + 1, second);
            return i + 1;
        }
        pos += len;
        i += 1;
    }
    p.runs.len()
}

/// The explicit run properties in effect at `pos` (the run before the caret, or the end mark).
pub fn props_at(body: &TextBody, pos: Pos) -> RunProps {
    let (pi, ch) = clamp(body, pos);
    let Some(p) = body.paragraphs.get(pi) else {
        return RunProps::default();
    };
    let mut acc = 0;
    let mut last = None;
    for r in &p.runs {
        let len = r.char_len();
        if ch <= acc + len && ch > acc {
            return r.props.clone();
        }
        if acc + len >= ch && ch == 0 {
            return r.props.clone();
        }
        acc += len;
        last = Some(&r.props);
    }
    last.cloned().unwrap_or_else(|| p.end_props.clone())
}

/// Insert `text` at `pos` (with `props`, else the props at `pos`). `\n` starts a new paragraph
/// (same level and paragraph props); `\u{b}` inserts a line break. Returns the caret after it.
pub fn insert(body: &mut TextBody, pos: Pos, text: &str, props: Option<RunProps>) -> Pos {
    if body.paragraphs.is_empty() {
        body.paragraphs.push(Paragraph::default());
    }
    let (mut pi, mut ch) = clamp(body, pos);
    let props = props.unwrap_or_else(|| props_at(body, (pi, ch)));
    let text = text.replace("\r\n", "\n").replace('\r', "\n");
    let mut first = true;
    for part in text.split('\n') {
        if !first {
            // Split paragraph pi at ch.
            let Some(p) = body.paragraphs.get_mut(pi) else {
                break;
            };
            let idx = split_at(p, ch);
            let tail: Vec<Run> = p.runs.drain(idx..).collect();
            let newp = Paragraph {
                level: p.level,
                props: p.props.clone(),
                runs: tail,
                end_props: p.end_props.clone(),
            };
            p.end_props = props.clone();
            body.paragraphs.insert(pi + 1, newp);
            pi += 1;
            ch = 0;
        }
        first = false;
        for (k, seg) in part.split('\u{b}').enumerate() {
            let Some(p) = body.paragraphs.get_mut(pi) else {
                break;
            };
            if k > 0 {
                let idx = split_at(p, ch);
                p.runs.insert(
                    idx,
                    Run {
                        text: String::new(),
                        props: props.clone(),
                        kind: RunKind::Break,
                    },
                );
                ch += 1;
            }
            if seg.is_empty() {
                continue;
            }
            let idx = split_at(p, ch);
            p.runs.insert(idx, Run::with(seg, props.clone()));
            ch += seg.chars().count();
        }
        if let Some(p) = body.paragraphs.get_mut(pi) {
            p.normalize();
            if p.runs.is_empty() {
                p.end_props = props.clone();
            }
        }
    }
    (pi, ch)
}

/// Delete the range between `a` and `b`; returns the caret at the start.
pub fn delete(body: &mut TextBody, a: Pos, b: Pos) -> Pos {
    if body.paragraphs.is_empty() {
        return (0, 0);
    }
    let (s, e) = order(clamp(body, a), clamp(body, b));
    if s == e {
        return s;
    }
    if s.0 == e.0 {
        if let Some(p) = body.paragraphs.get_mut(s.0) {
            let end_props = props_of_range(p, s.1);
            let i0 = split_at(p, s.1);
            let i1 = split_at(p, e.1);
            if i1 > i0 {
                p.runs.drain(i0..i1);
            }
            p.normalize();
            if p.runs.is_empty() {
                p.end_props = end_props;
            }
        }
        return s;
    }
    // Keep the head of s.0 and the tail of e.0, joined; drop paragraphs between.
    let tail: Vec<Run> = match body.paragraphs.get_mut(e.0) {
        Some(p) => {
            let i = split_at(p, e.1);
            p.runs.drain(i..).collect()
        }
        None => vec![],
    };
    if let Some(p) = body.paragraphs.get_mut(s.0) {
        let i = split_at(p, s.1);
        p.runs.truncate(i);
        p.runs.extend(tail);
        p.normalize();
    }
    if e.0 > s.0 && e.0 < body.paragraphs.len() {
        body.paragraphs.drain(s.0 + 1..=e.0);
    }
    s
}

fn props_of_range(p: &Paragraph, at: usize) -> RunProps {
    let mut acc = 0;
    for r in &p.runs {
        let len = r.char_len();
        if at < acc + len || (at == acc + len && at > 0) {
            return r.props.clone();
        }
        acc += len;
    }
    p.runs
        .last()
        .map(|r| r.props.clone())
        .unwrap_or_else(|| p.end_props.clone())
}

/// Apply `f` to the run properties of every run in [a, b). An empty range changes the empty
/// paragraph's end mark (so typing there picks the formatting up).
pub fn format(body: &mut TextBody, a: Pos, b: Pos, f: &dyn Fn(&mut RunProps)) {
    if body.paragraphs.is_empty() {
        return;
    }
    let (s, e) = order(clamp(body, a), clamp(body, b));
    for pi in s.0..=e.0 {
        let Some(p) = body.paragraphs.get_mut(pi) else {
            continue;
        };
        let from = if pi == s.0 { s.1 } else { 0 };
        let to = if pi == e.0 { e.1 } else { p.char_len() };
        if p.runs.is_empty() || (from == to && s == e) {
            f(&mut p.end_props);
            continue;
        }
        let i0 = split_at(p, from);
        let i1 = split_at(p, to);
        for r in p.runs.iter_mut().take(i1).skip(i0) {
            f(&mut r.props);
        }
        if to == p.char_len() {
            f(&mut p.end_props);
        }
        p.normalize();
    }
}

/// Apply `f` to every run in the body (and end marks).
pub fn format_all(body: &mut TextBody, f: &dyn Fn(&mut RunProps)) {
    for p in &mut body.paragraphs {
        for r in &mut p.runs {
            f(&mut r.props);
        }
        f(&mut p.end_props);
        p.normalize();
    }
}

/// The text between two positions (`\n` between paragraphs).
pub fn text_range(body: &TextBody, a: Pos, b: Pos) -> String {
    let (s, e) = order(clamp(body, a), clamp(body, b));
    let mut out = String::new();
    for pi in s.0..=e.0 {
        let Some(p) = body.paragraphs.get(pi) else {
            continue;
        };
        let t: Vec<char> = p.text().chars().collect();
        let from = if pi == s.0 { s.1 } else { 0 };
        let to = if pi == e.0 { e.1 } else { t.len() };
        out.extend(t.get(from.min(t.len())..to.min(t.len())).unwrap_or(&[]));
        if pi != e.0 {
            out.push('\n');
        }
    }
    out
}

/// A formatted copy of the range.
pub fn slice(body: &TextBody, a: Pos, b: Pos) -> TextBody {
    let (s, e) = order(clamp(body, a), clamp(body, b));
    let mut out = TextBody {
        body: body.body.clone(),
        list_style: body.list_style.clone(),
        paragraphs: vec![],
    };
    for pi in s.0..=e.0 {
        let Some(p) = body.paragraphs.get(pi) else {
            continue;
        };
        let mut p = p.clone();
        let to = if pi == e.0 { e.1 } else { p.char_len() };
        let i1 = split_at(&mut p, to);
        p.runs.truncate(i1);
        let from = if pi == s.0 { s.1 } else { 0 };
        let i0 = split_at(&mut p, from);
        p.runs.drain(..i0);
        out.paragraphs.push(p);
    }
    out
}

/// Insert a formatted body at `pos` (paste): first paragraph merges into the current one.
pub fn insert_body(body: &mut TextBody, pos: Pos, src: &TextBody) -> Pos {
    let mut caret = clamp(body, pos);
    for (k, sp) in src.paragraphs.iter().enumerate() {
        if k > 0 {
            caret = insert(body, caret, "\n", None);
            if let Some(p) = body.paragraphs.get_mut(caret.0) {
                p.level = sp.level;
                p.props = sp.props.clone();
            }
        }
        for r in &sp.runs {
            match r.kind {
                RunKind::Text => caret = insert(body, caret, &r.text, Some(r.props.clone())),
                RunKind::Break => caret = insert(body, caret, "\u{b}", Some(r.props.clone())),
                _ => {
                    if let Some(p) = body.paragraphs.get_mut(caret.0) {
                        let idx = split_at(p, caret.1);
                        p.runs.insert(idx, r.clone());
                        caret.1 += r.char_len();
                    }
                }
            }
        }
    }
    caret
}

fn para_text(body: &TextBody, pi: usize) -> String {
    body.paragraphs
        .get(pi)
        .map(Paragraph::text)
        .unwrap_or_default()
}

/// The word a double-click selects around `pos` (see [`words::word_at`]): the word without its
/// trailing spaces, as PowerPoint selects it.
pub fn word_at(body: &TextBody, pos: Pos) -> (Pos, Pos) {
    let (pi, ch) = clamp(body, pos);
    let (s, e) = words::word_at(&para_text(body, pi), ch);
    ((pi, s), (pi, e))
}

/// Next/previous word stop (Ctrl+Right/Left on Windows, Option+arrows on the Mac). At the end of
/// a paragraph Ctrl+Right goes to the start of the next one; at its start Ctrl+Left goes to the
/// end of the previous one.
pub fn word_move(body: &TextBody, pos: Pos, forward: bool) -> Pos {
    let (pi, ch) = clamp(body, pos);
    let t = para_text(body, pi);
    if forward {
        match words::next_stop(&t, ch) {
            Some(s) => (pi, s),
            None if pi + 1 < body.paragraphs.len() => (pi + 1, 0),
            None => (pi, ch),
        }
    } else {
        match words::prev_stop(&t, ch) {
            Some(s) => (pi, s),
            None if pi > 0 => (
                pi - 1,
                body.paragraphs
                    .get(pi - 1)
                    .map(Paragraph::char_len)
                    .unwrap_or(0),
            ),
            None => (pi, 0),
        }
    }
}

/// Ctrl+Up: the start of the paragraph, or of the previous one when already there.
pub fn para_up(body: &TextBody, pos: Pos) -> Pos {
    let (pi, ch) = clamp(body, pos);
    if ch > 0 {
        (pi, 0)
    } else {
        (pi.saturating_sub(1), 0)
    }
}

/// Ctrl+Down: the start of the next paragraph, or the end of the last one.
pub fn para_down(body: &TextBody, pos: Pos) -> Pos {
    let (pi, _) = clamp(body, pos);
    if pi + 1 < body.paragraphs.len() {
        (pi + 1, 0)
    } else {
        (
            pi,
            body.paragraphs
                .get(pi)
                .map(Paragraph::char_len)
                .unwrap_or(0),
        )
    }
}

/// Delete a word from `pos` as Ctrl+Backspace (`forward == false`) or Ctrl+Delete does in
/// PowerPoint; returns the caret.
///
/// Ctrl+Backspace deletes back to the previous word stop (at a paragraph's start it joins the
/// paragraph to the one before). Ctrl+Delete deletes the rest of the word, or the spaces after a
/// word and the next word, or up to the next stop (see [`words::delete_forward_end`]), then
/// tidies the spacing as PowerPoint's smart cut does: spaces left before closing punctuation or
/// at the end of the paragraph after a word go, and sentence punctuation left against a word
/// gets a space. At a paragraph's end it joins the next paragraph.
pub fn delete_word(body: &mut TextBody, pos: Pos, forward: bool) -> Pos {
    if body.paragraphs.is_empty() {
        return (0, 0);
    }
    let (pi, ch) = clamp(body, pos);
    let t = para_text(body, pi);
    if !forward {
        let to = match words::prev_stop(&t, ch) {
            Some(s) => (pi, s),
            None => char_move(body, (pi, ch), false),
        };
        return delete(body, to, (pi, ch));
    }
    let Some(e) = words::delete_forward_end(&t, ch) else {
        let to = char_move(body, (pi, ch), true);
        return delete(body, (pi, ch), to);
    };
    let at = delete(body, (pi, ch), (pi, e));
    smart_space(body, at)
}

/// PowerPoint's smart-cut spacing after a word was deleted at `at`; returns the caret.
fn smart_space(body: &mut TextBody, at: Pos) -> Pos {
    let (pi, s) = at;
    let chars: Vec<char> = para_text(body, pi).chars().collect();
    let after = chars.get(s).copied();
    let before = s.checked_sub(1).and_then(|i| chars.get(i)).copied();
    if after.is_none_or(words::is_closing_punct) && before.is_some_and(|c| c == ' ') {
        let mut w = s;
        while w > 0 && chars.get(w - 1) == Some(&' ') {
            w -= 1;
        }
        if w > 0 && chars.get(w - 1).is_some_and(|c| c.is_alphanumeric()) {
            return delete(body, (pi, w), (pi, s));
        }
        return at;
    }
    if let (Some(b), Some(a)) = (before, after)
        && words::is_sentence_punct(b)
        && a.is_alphanumeric()
    {
        let digits = s >= 2 && chars.get(s - 2).is_some_and(|c| c.is_numeric()) && a.is_numeric();
        if !digits {
            return insert(body, at, " ", None);
        }
    }
    at
}

/// One character left/right across paragraph boundaries.
pub fn char_move(body: &TextBody, pos: Pos, forward: bool) -> Pos {
    let (pi, ch) = clamp(body, pos);
    let n = body
        .paragraphs
        .get(pi)
        .map(Paragraph::char_len)
        .unwrap_or(0);
    if forward {
        if ch < n {
            (pi, ch + 1)
        } else if pi + 1 < body.paragraphs.len() {
            (pi + 1, 0)
        } else {
            (pi, ch)
        }
    } else if ch > 0 {
        (pi, ch - 1)
    } else if pi > 0 {
        (
            pi - 1,
            body.paragraphs
                .get(pi - 1)
                .map(Paragraph::char_len)
                .unwrap_or(0),
        )
    } else {
        (0, 0)
    }
}

/// End of the body.
pub fn end(body: &TextBody) -> Pos {
    let pi = body.paragraphs.len().saturating_sub(1);
    (
        pi,
        body.paragraphs
            .get(pi)
            .map(Paragraph::char_len)
            .unwrap_or(0),
    )
}

/// The case Shift+F3 changes `s` to next, as PowerPoint cycles it: lowercase → Capitalize Each
/// Word → UPPERCASE → lowercase; any other mix goes to UPPERCASE first.
pub fn next_case(s: &str) -> &'static str {
    if s == s.to_lowercase() && s.chars().any(char::is_alphabetic) {
        "title"
    } else if s == s.to_uppercase() && s.chars().any(char::is_alphabetic) {
        "lower"
    } else {
        "upper"
    }
}

/// Change case: `upper`, `lower`, `sentence`, `title`, `toggle`.
pub fn change_case(s: &str, mode: &str) -> String {
    match mode {
        "upper" => s.to_uppercase(),
        "lower" => s.to_lowercase(),
        "toggle" => s
            .chars()
            .map(|c| {
                if c.is_uppercase() {
                    c.to_lowercase().collect::<String>()
                } else {
                    c.to_uppercase().collect()
                }
            })
            .collect(),
        "title" => {
            let mut out = String::new();
            let mut start = true;
            for c in s.chars() {
                if start && c.is_alphabetic() {
                    out.extend(c.to_uppercase());
                    start = false;
                } else {
                    out.extend(c.to_lowercase());
                }
                if c.is_whitespace() {
                    start = true;
                }
            }
            out
        }
        _ => {
            // sentence
            let mut out = String::new();
            let mut start = true;
            for c in s.chars() {
                if start && c.is_alphabetic() {
                    out.extend(c.to_uppercase());
                    start = false;
                } else {
                    out.extend(c.to_lowercase());
                }
                if matches!(c, '.' | '!' | '?') {
                    start = true;
                }
            }
            out
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn body(t: &str) -> TextBody {
        TextBody::from_text(t)
    }

    #[test]
    fn insert_and_split() {
        let mut b = body("Hello");
        let c = insert(&mut b, (0, 5), " world\nNext", None);
        assert_eq!(b.text(), "Hello world\nNext");
        assert_eq!(c, (1, 4));
        let c = insert(&mut b, (0, 0), ">", None);
        assert_eq!(c, (0, 1));
        assert_eq!(b.paragraphs[0].runs.len(), 1);
    }

    #[test]
    fn delete_within_and_across() {
        let mut b = body("abc\ndef\nghi");
        assert_eq!(delete(&mut b, (0, 1), (2, 1)), (0, 1));
        assert_eq!(b.text(), "ahi");
        let mut b = body("abcdef");
        delete(&mut b, (0, 4), (0, 2));
        assert_eq!(b.text(), "abef");
        // hostile positions
        delete(&mut b, (99, 99), (0, 0));
        assert_eq!(b.text(), "");
    }

    #[test]
    fn format_splits_runs() {
        let mut b = body("abcdef");
        format(&mut b, (0, 2), (0, 4), &|p| p.bold = Some(true));
        let p = &b.paragraphs[0];
        assert_eq!(p.runs.len(), 3);
        assert_eq!(p.runs[1].text, "cd");
        assert_eq!(p.runs[1].props.bold, Some(true));
        assert_eq!(props_at(&b, (0, 3)).bold, Some(true));
        assert_eq!(text_range(&b, (0, 1), (0, 5)), "bcde");
        let s = slice(&b, (0, 1), (0, 5));
        assert_eq!(s.text(), "bcde");
        let mut c = body("XY");
        let caret = insert_body(&mut c, (0, 1), &s);
        assert_eq!(c.text(), "XbcdeY");
        assert_eq!(caret, (0, 5));
    }

    #[test]
    fn words_and_moves() {
        let b = body("Hello brave world");
        assert_eq!(word_at(&b, (0, 7)), ((0, 6), (0, 11)));
        assert_eq!(word_move(&b, (0, 0), true), (0, 6));
        assert_eq!(word_move(&b, (0, 8), false), (0, 6));
        assert_eq!(char_move(&b, (0, 17), true), (0, 17));
        assert_eq!(
            change_case("hello world. again", "sentence"),
            "Hello world. Again"
        );
        assert_eq!(change_case("hello world", "title"), "Hello World");
    }

    #[test]
    fn line_break_insert() {
        let mut b = body("ab");
        let c = insert(&mut b, (0, 1), "\u{b}", None);
        assert_eq!(c, (0, 2));
        assert_eq!(b.paragraphs[0].char_len(), 3);
        assert_eq!(b.paragraphs.len(), 1);
    }

    /// Ctrl+Backspace / Ctrl+Delete from `at`: the text and caret after (PowerPoint's results).
    fn del_word(text: &str, at: Pos, forward: bool) -> (String, Pos) {
        let mut b = body(text);
        let c = delete_word(&mut b, at, forward);
        (b.text(), c)
    }

    #[test]
    fn ctrl_backspace_like_powerpoint() {
        let t = "ab   cd...ef gh";
        let want = [
            ("ab   cd...ef gh", 0),
            ("b   cd...ef gh", 0),
            ("   cd...ef gh", 0),
            ("  cd...ef gh", 0),
            (" cd...ef gh", 0),
            ("cd...ef gh", 0),
            ("ab   d...ef gh", 5),
            ("ab   ...ef gh", 5),
            ("ab   cd..ef gh", 7),
            ("ab   cd.ef gh", 7),
            ("ab   cdef gh", 7),
            ("ab   cd...f gh", 10),
            ("ab   cd... gh", 10),
            ("ab   cd...gh", 10),
            ("ab   cd...ef h", 13),
            ("ab   cd...ef ", 13),
        ];
        for (c, (txt, caret)) in want.iter().enumerate() {
            assert_eq!(
                del_word(t, (0, c), false),
                (txt.to_string(), (0, *caret)),
                "caret {c}"
            );
        }
        let w = "Hello world  foo, bar\nnext para";
        assert_eq!(
            del_word(w, (0, 12), false),
            ("Hello  foo, bar\nnext para".into(), (0, 6))
        );
        assert_eq!(
            del_word(w, (0, 17), false),
            ("Hello world  foo bar\nnext para".into(), (0, 16))
        );
        // At a paragraph's start it joins the paragraph before.
        assert_eq!(
            del_word(w, (1, 0), false),
            ("Hello world  foo, barnext para".into(), (0, 21))
        );
        assert_eq!(
            del_word("ab\n  cd", (1, 2), false),
            ("ab\ncd".into(), (1, 0))
        );
        // A line break is a word of its own.
        let mut b = body("ab");
        insert(&mut b, (0, 2), "\u{b}cd", None);
        assert_eq!(delete_word(&mut b, (0, 3), false), (0, 2));
        assert_eq!(b.text(), "abcd");
        let mut b = body("ab");
        insert(&mut b, (0, 2), "\u{b}cd", None);
        assert_eq!(delete_word(&mut b, (0, 2), true), (0, 2));
        assert_eq!(b.text(), "abcd");
        assert_eq!(del_word("x", (0, 0), false), ("x".into(), (0, 0)));
        // Emoji sequences and combining marks are never split.
        assert_eq!(
            del_word("a 👨\u{200d}👩\u{200d}👧", (0, 7), false),
            ("a ".into(), (0, 2))
        );
        assert_eq!(
            del_word("ce\u{301}te x", (0, 5), false),
            (" x".into(), (0, 0))
        );
        assert_eq!(
            del_word("ce\u{301}te x", (0, 6), false),
            ("x".into(), (0, 0))
        );
        // Each CJK character is a word.
        assert_eq!(del_word("日本語", (0, 3), false), ("日本".into(), (0, 2)));
    }

    #[test]
    fn ctrl_delete_like_powerpoint() {
        let t = "ab   cd...ef gh";
        let want = [
            ("cd...ef gh", 0),
            ("a   cd...ef gh", 1),
            ("ab...ef gh", 2),
            ("ab cd...ef gh", 3),
            ("ab  cd...ef gh", 4),
            ("ab...ef gh", 2),
            ("ab   c...ef gh", 6),
            ("ab   cdef gh", 7),
            ("ab   cd. ef gh", 9),
            ("ab   cd.. ef gh", 10),
            ("ab   cd... gh", 11),
            ("ab   cd...e gh", 11),
            ("ab   cd...ef", 12),
            ("ab   cd...ef", 12),
            ("ab   cd...ef g", 14),
            ("ab   cd...ef gh", 15),
        ];
        for (c, (txt, caret)) in want.iter().enumerate() {
            assert_eq!(
                del_word(t, (0, c), true),
                (txt.to_string(), (0, *caret)),
                "caret {c}"
            );
        }
        let w = "Hello world  foo, bar\nnext para";
        let want = [
            (0, "world  foo, bar\nnext para", 0),
            (2, "He world  foo, bar\nnext para", 2),
            (5, "Hello  foo, bar\nnext para", 5),
            (6, "Hello foo, bar\nnext para", 6),
            (8, "Hello wo  foo, bar\nnext para", 8),
            (11, "Hello world, bar\nnext para", 11),
            (12, "Hello world foo, bar\nnext para", 12),
            (16, "Hello world  foobar\nnext para", 16),
            (17, "Hello world  foo,\nnext para", 17),
            (18, "Hello world  foo, \nnext para", 18),
            (21, "Hello world  foo, barnext para", 21),
        ];
        for (c, txt, caret) in want {
            assert_eq!(
                del_word(w, (0, c), true),
                (txt.to_string(), (0, caret)),
                "caret {c}"
            );
        }
        assert_eq!(
            del_word(w, (1, 0), true),
            ("Hello world  foo, bar\npara".into(), (1, 0))
        );
        assert_eq!(
            del_word(w, (1, 2), true),
            ("Hello world  foo, bar\nne para".into(), (1, 2))
        );
        assert_eq!(
            del_word("ab  \ncd", (0, 2), true),
            ("ab\ncd".into(), (0, 2))
        );
        assert_eq!(
            del_word("ab  \ncd", (0, 3), true),
            ("ab\ncd".into(), (0, 2))
        );
        assert_eq!(del_word("ab\t cd", (0, 2), true), ("ab cd".into(), (0, 2)));
        assert_eq!(
            del_word("ab cd(x) ef", (0, 3), true),
            ("ab (x) ef".into(), (0, 3))
        );
        assert_eq!(
            del_word("ab cd, ef", (0, 3), true),
            ("ab, ef".into(), (0, 2))
        );
        assert_eq!(
            del_word("ab. (cd) ef", (0, 5), true),
            ("ab. () ef".into(), (0, 5))
        );
        assert_eq!(
            del_word("ab \"cd\" ef", (0, 4), true),
            ("ab \"\" ef".into(), (0, 4))
        );
        assert_eq!(del_word("ab, cd", (0, 4), true), ("ab, ".into(), (0, 4)));
        // Right-to-left text deletes in logical order like any other.
        assert_eq!(
            del_word("مرحبا بالعالم hello", (0, 6), true),
            ("مرحبا hello".into(), (0, 6))
        );
        assert_eq!(
            del_word("مرحبا بالعالم hello", (0, 8), false),
            ("مرحبا لعالم hello".into(), (0, 6))
        );
        // Numbers keep their separators: no space is pushed into "3.14".
        assert_eq!(del_word("3.x 14", (0, 2), true), ("3.14".into(), (0, 2)));
        // Hostile positions clamp.
        assert_eq!(del_word("ab", (9, 9), true).0, "ab");
        assert_eq!(del_word("", (0, 0), false).0, "");
        let mut empty = TextBody::default();
        assert_eq!(delete_word(&mut empty, (3, 3), true), (0, 0));
    }

    #[test]
    fn paragraph_moves() {
        let b = body("one two\nthree\n\nfour");
        assert_eq!(para_down(&b, (0, 2)), (1, 0));
        assert_eq!(para_down(&b, (1, 0)), (2, 0));
        assert_eq!(para_down(&b, (3, 1)), (3, 4));
        assert_eq!(para_up(&b, (3, 2)), (3, 0));
        assert_eq!(para_up(&b, (3, 0)), (2, 0));
        assert_eq!(para_up(&b, (0, 0)), (0, 0));
        assert_eq!(word_move(&b, (0, 4), true), (0, 7));
        assert_eq!(word_move(&b, (0, 7), true), (1, 0));
        assert_eq!(word_move(&b, (1, 0), false), (0, 7));
        assert_eq!(word_move(&b, (2, 0), true), (3, 0));
        assert_eq!(word_move(&b, (3, 4), true), (3, 4));
        assert_eq!(word_move(&b, (0, 0), false), (0, 0));
    }

    #[test]
    fn case_cycle() {
        assert_eq!(next_case("hello world"), "title");
        assert_eq!(next_case("Hello World"), "upper");
        assert_eq!(next_case("HELLO WORLD"), "lower");
        assert_eq!(next_case("Hello world"), "upper");
        assert_eq!(next_case("hELLO wORLD"), "upper");
        assert_eq!(next_case("hello. world is HERE"), "upper");
        assert_eq!(next_case("123"), "upper");
    }
}