egui_richedit 0.6.0

Rich-text editing for egui over paragraphs the application lays out itself: caret, selection and typing on styled galleys
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
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
//! The editor: where the caret is, what the keys and the pointer do to it,
//! and how it and the selection are painted over the application's galleys.
//
// Author: David M. Anderson
// Built with AI assistance (Claude, Anthropic)

use std::collections::HashMap;
use std::sync::Arc;

use egui::accesskit::Role;
use egui::emath::TSTransform;
use egui::output::IMEOutput;
use egui::text::{CCursor, CCursorRange, CharIndex};
use egui::text_selection::accesskit_text::update_accesskit_for_text_widget;
use egui::text_selection::text_cursor_state::{
    ccursor_next_word, ccursor_previous_word, is_word_char,
};
use egui::text_selection::visuals::{paint_text_cursor, paint_text_selection};
use egui::{
    CursorIcon, Event, EventFilter, Galley, IMEPurpose, Id, ImeEvent, Key, Modifiers, Pos2,
    Response, Sense, Ui, vec2,
};

use crate::{Edit, Fragment, Mark, Model, OffsetMap, Position, Selection};

/// Keys the editor keeps while it has the focus, rather than letting egui
/// move the focus to another widget with them. Escape is not kept: it is how
/// a person leaves the page.
const FILTER: EventFilter = EventFilter {
    tab: true,
    horizontal_arrows: true,
    vertical_arrows: true,
    escape: false,
};

/// A paragraph as the application laid it out and where it is on screen.
pub struct Laid {
    /// The galley egui made of the paragraph's [`crate::ParagraphJob`].
    pub galley: Arc<Galley>,
    /// The map that came out of the same job.
    pub map: OffsetMap,
    /// Where the galley is painted: the point its rows are positioned from.
    pub origin: Pos2,
}

/// What kind of edit the open undo step is made of. Consecutive edits of one
/// kind share a step; anything else begins one.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Group {
    Typing,
    Deleting,
    /// A paste, a cut or a split, each its own step however many edits it
    /// takes.
    Other,
}

/// The editor's state, kept by the application from one frame to the next.
///
/// One per editable surface. It holds the selection, which way Up and Down
/// are aiming, which undo step is open, and where each paragraph was drawn
/// last frame, which is what Up, Down, Home and End move by.
pub struct RichEdit<P> {
    id: Id,
    selection: Option<Selection<P>>,
    /// At a row's wrap point, whether the caret is drawn at the start of the
    /// next row rather than at the end of this one.
    next_row: bool,
    /// The screen x that a run of Up and Down keeps to.
    column: Option<f32>,
    group: Option<Group>,
    /// Last frame's paragraphs.
    placed: HashMap<P, Placed>,
    /// This frame's, as they are reported.
    placing: Placing<P>,
    /// What the next frame owes the caret.
    owed: Owed,
    /// How tall the view the paragraphs are drawn in is, which is how far
    /// Page Up and Page Down move.
    page_height: f32,
    /// The pointer went down on a paragraph and has not come up.
    dragging: bool,
    /// When the caret last moved or the text last changed, which is when its
    /// blink restarts.
    last_interaction: f64,
    /// Marks given or taken off at a caret, for the text typed there next.
    pending: Option<Pending<P>>,
    /// The last copy, with the plain text it put on the clipboard: a paste of
    /// that text is a paste of the copy, formatting and all.
    clip: Option<(String, Fragment)>,
}

/// Marks given or taken off with a caret and nothing selected. The next text
/// typed at that caret takes them; once the caret is anywhere else, or the
/// document has changed, they are forgotten.
struct Pending<P> {
    at: Position<P>,
    marks: Vec<(Mark, bool)>,
}

/// What the caret is owed by the next frame that draws it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Owed {
    Nothing,
    /// It moved, and is scrolled into view where it is drawn.
    Reveal,
    /// It was put somewhere from outside, and takes the keyboard too.
    FocusAndReveal,
}

struct Placed {
    galley: Arc<Galley>,
    map: OffsetMap,
    origin: Pos2,
    /// Where the paragraph came in document order.
    index: usize,
}

/// The paragraphs reported so far this frame.
struct Placing<P> {
    placed: HashMap<P, Placed>,
    /// Whether the selection began in a paragraph already painted and has
    /// not yet ended.
    inside: bool,
}

impl<P> Default for Placing<P> {
    fn default() -> Self {
        Self {
            placed: HashMap::new(),
            inside: false,
        }
    }
}

impl<P: Clone + Eq + std::hash::Hash + std::fmt::Debug> RichEdit<P> {
    /// An editor with no selection, which takes the focus under an id.
    pub fn new(id: Id) -> Self {
        Self {
            id,
            selection: None,
            next_row: false,
            column: None,
            group: None,
            placed: HashMap::new(),
            placing: Placing::default(),
            owed: Owed::Nothing,
            page_height: 0.0,
            dragging: false,
            last_interaction: 0.0,
            pending: None,
            clip: None,
        }
    }

    /// The selection, or `None` when there is no caret.
    pub fn selection(&self) -> Option<&Selection<P>> {
        self.selection.as_ref()
    }

    /// Put the caret or the selection somewhere. The next frame scrolls it
    /// into view and gives the editor the keyboard.
    pub fn select(&mut self, selection: Selection<P>) {
        self.selection = Some(selection);
        self.group = None;
        self.column = None;
        self.pending = None;
        self.owed = Owed::FocusAndReveal;
    }

    /// Take the caret away.
    pub fn clear(&mut self) {
        self.selection = None;
        self.group = None;
        self.column = None;
        self.dragging = false;
        self.pending = None;
        self.clip = None;
    }

    /// The document was changed or replaced by something other than this
    /// editor, as an undo is. The next edit begins a new undo step, and the
    /// selection is checked against the model before it is used again.
    pub fn document_replaced(&mut self) {
        self.group = None;
        self.column = None;
        self.pending = None;
    }

    /// Whether the selection carries a mark, as a toolbar shows it: `None`
    /// where part of it does, and for a caret what text typed there would
    /// take, a mark given or taken off at the caret included. `None` too when
    /// there is no caret.
    pub fn marked<M: Model<Paragraph = P>>(&self, model: &M, mark: Mark) -> Option<bool> {
        let selection = self.selection.as_ref()?;
        if let Some(on) = self.pending_mark(mark) {
            return Some(on);
        }
        let (from, to) = self.ordered(selection);
        model.marked(&from, &to, mark)
    }

    /// Give the selection a mark, or take it off where all of it has it, in
    /// one undo step; with a caret and nothing selected, give it to or take it
    /// off what is typed next at the caret. Answers whether the document
    /// changed. The editor takes the keyboard back from whatever asked, as a
    /// toolbar's button does.
    pub fn toggle<M: Model<Paragraph = P>>(&mut self, model: &mut M, mark: Mark) -> bool {
        let Some(selection) = self.selection.clone() else {
            return false;
        };
        self.owed = Owed::FocusAndReveal;
        let on = self.marked(model, mark) != Some(true);
        if selection.is_caret() {
            let at = selection.focus;
            let mut marks = match self.pending.take() {
                Some(pending) if pending.at == at => pending.marks,
                _ => Vec::new(),
            };
            marks.retain(|(m, _)| *m != mark);
            if model.marked(&at, &at, mark) != Some(on) {
                marks.push((mark, on));
            }
            self.pending = (!marks.is_empty()).then_some(Pending { at, marks });
            return false;
        }
        let (from, to) = self.ordered(&selection);
        self.group = None;
        let changed = self.format(model, from, to, mark, on, Group::Other);
        self.group = None;
        changed
    }

    /// What a mark given or taken off at the caret says, while the caret is
    /// where it was then.
    fn pending_mark(&self, mark: Mark) -> Option<bool> {
        let selection = self.selection.as_ref().filter(|s| s.is_caret())?;
        let pending = self.pending.as_ref().filter(|p| p.at == selection.focus)?;
        pending
            .marks
            .iter()
            .find(|(m, _)| *m == mark)
            .map(|(_, on)| *on)
    }

    /// Take this frame's events and apply them to the model. Called once a
    /// frame, before the paragraphs are laid out, whether or not the editor
    /// has the keyboard. Answers whether the document changed.
    pub fn input<M: Model<Paragraph = P>>(&mut self, ui: &mut Ui, model: &mut M) -> bool {
        self.placed = std::mem::take(&mut self.placing).placed;

        // Registered every frame, because egui takes the focus from a widget
        // that did not appear in the last one. It senses no pointer, so the
        // paragraphs over it still get theirs.
        ui.interact(ui.clip_rect(), self.id, Sense::focusable_noninteractive());
        self.page_height = ui.clip_rect().height();
        if self.owed == Owed::FocusAndReveal {
            self.owed = Owed::Reveal;
            ui.memory_mut(|memory| memory.request_focus(self.id));
        }
        if !ui.memory(|memory| memory.has_focus(self.id)) {
            self.dragging = false;
            return false;
        }
        ui.memory_mut(|memory| memory.set_focus_lock_filter(self.id, FILTER));
        if self.dragging {
            scroll_toward_pointer(ui);
            self.extend_past_the_ends(ui);
        }
        self.settle(model);

        let events = ui.input(|input| input.filtered_events(&FILTER));
        let mut changed = false;
        for event in &events {
            let (moved, edited) = self.event(ui, model, event);
            if moved || edited {
                self.last_interaction = ui.input(|input| input.time);
                self.owed = Owed::Reveal;
            }
            changed |= edited;
        }
        changed
    }

    /// Paint a paragraph with the selection and the caret over it, and take
    /// what the pointer does on it. Called for every editable paragraph in
    /// document order, on screen or not, after [`Self::input`].
    ///
    /// `response` is the paragraph's, allocated to sense clicks and, for a
    /// drag to select, drags.
    pub fn paragraph(&mut self, ui: &Ui, response: &Response, paragraph: &P, laid: Laid) {
        let Laid {
            mut galley,
            map,
            origin,
        } = laid;
        let focused = ui.memory(|memory| memory.has_focus(self.id));

        if response.hovered() {
            ui.ctx().set_cursor_icon(CursorIcon::Text);
        }
        self.pointer(ui, response, paragraph, &galley, &map, origin);

        if let Some(range) = self.selected_here(paragraph, &map)
            && focused
        {
            paint_text_selection(&mut galley, ui.visuals(), &range, None);
        }
        ui.painter()
            .galley(origin, galley.clone(), ui.visuals().text_color());
        self.expose(ui, response, paragraph, &galley, &map, origin, focused);

        if focused
            && let Some(selection) = &self.selection
            && selection.focus.paragraph == *paragraph
        {
            let cursor = CCursor {
                index: CharIndex(map.to_galley(selection.focus.offset)),
                prefer_next_row: self.next_row,
            };
            let caret = galley.pos_from_cursor(cursor).translate(origin.to_vec2());
            let since = ui.input(|input| input.time) - self.last_interaction;
            paint_text_cursor(ui, ui.painter(), caret, since);
            if self.owed == Owed::Reveal {
                self.owed = Owed::Nothing;
                ui.scroll_to_rect(caret.expand(4.0), None);
            }
            // Where an input method draws what is being composed.
            let to_global = ui
                .ctx()
                .layer_transform_to_global(ui.layer_id())
                .unwrap_or_default();
            ui.output_mut(|output| {
                output.ime = Some(IMEOutput {
                    purpose: IMEPurpose::Normal,
                    rect: to_global * response.rect,
                    cursor_rect: to_global * caret,
                    should_interrupt_composition: false,
                });
            });
        }

        let index = self.placing.placed.len();
        self.placing.placed.insert(
            paragraph.clone(),
            Placed {
                galley,
                map,
                origin,
                index,
            },
        );
    }

    /// Tell assistive technology what the paragraph says and where the caret
    /// or the selection is in it, as a screen reader reads a paragraph of text.
    /// A selection that began in another paragraph is reported as a caret at
    /// its end here, because one node cannot name a position in another.
    #[allow(clippy::too_many_arguments)]
    fn expose(
        &self,
        ui: &Ui,
        response: &Response,
        paragraph: &P,
        galley: &Galley,
        map: &OffsetMap,
        origin: Pos2,
        focused: bool,
    ) {
        let cursor_range = self
            .selection
            .as_ref()
            .filter(|selection| focused && selection.focus.paragraph == *paragraph)
            .map(|selection| {
                let at = |position: &Position<P>| CCursor::new(map.to_galley(position.offset));
                let anchor = if selection.anchor.paragraph == *paragraph {
                    &selection.anchor
                } else {
                    &selection.focus
                };
                CCursorRange::two(at(anchor), at(&selection.focus))
            });
        let to_global = ui
            .ctx()
            .layer_transform_to_global(ui.layer_id())
            .unwrap_or_default();
        update_accesskit_for_text_widget(
            ui.ctx(),
            response.id,
            cursor_range,
            Role::Paragraph,
            to_global * TSTransform::from_translation(origin.to_vec2()),
            galley,
        );
    }

    /// A drag whose pointer is below every paragraph takes the selection to
    /// the end of the last, and above every paragraph to the start of the
    /// first. Only a paragraph the pointer is over takes it otherwise, so
    /// without this a drag stops one short of an end it has run past.
    fn extend_past_the_ends(&mut self, ui: &Ui) {
        let Some(pointer) = ui.input(|input| input.pointer.latest_pos()) else {
            return;
        };
        let first = self.placed.iter().min_by_key(|(_, placed)| placed.index);
        let last = self.placed.iter().max_by_key(|(_, placed)| placed.index);
        let (Some((first, above)), Some((last, below))) = (first, last) else {
            return;
        };
        let to = if pointer.y > below.origin.y + below.galley.rect.bottom() {
            Position::new(last.clone(), below.map.model_len())
        } else if pointer.y < above.origin.y + above.galley.rect.top() {
            Position::new(first.clone(), 0)
        } else {
            return;
        };
        if let Some(selection) = &mut self.selection {
            selection.focus = to;
        }
    }

    /// Drop a selection whose paragraph is gone, and pull one past the end of
    /// its paragraph back to it, as an undo can leave either.
    fn settle<M: Model<Paragraph = P>>(&mut self, model: &M) {
        let Some(selection) = &mut self.selection else {
            return;
        };
        let kept = [&mut selection.anchor, &mut selection.focus]
            .into_iter()
            .all(|position| {
                len(model, &position.paragraph)
                    .map(|len| position.offset = position.offset.min(len))
                    .is_some()
            });
        if !kept {
            self.selection = None;
        }
    }

    /// One event. Answers whether it moved the caret and whether it changed
    /// the document.
    fn event<M: Model<Paragraph = P>>(
        &mut self,
        ui: &Ui,
        model: &mut M,
        event: &Event,
    ) -> (bool, bool) {
        match event {
            Event::Text(text) | Event::Ime(ImeEvent::Commit(text))
                if !text.is_empty() && text != "\n" && text != "\r" =>
            {
                (false, self.type_text(model, text))
            }
            Event::Paste(text) => (false, self.paste(model, text)),
            Event::Copy => {
                self.copy(ui, model);
                (false, false)
            }
            Event::Cut => {
                if !self.copy(ui, model) {
                    return (false, false);
                }
                self.group = None;
                let cut = self.delete_selection(model, Group::Other);
                self.group = None;
                (false, cut)
            }
            Event::Key {
                key,
                pressed: true,
                modifiers,
                ..
            } => match shortcut(*key).filter(|_| modifiers.command) {
                Some(mark) => (false, self.toggle(model, mark)),
                None => self.key(model, *key, *modifiers),
            },
            _ => (false, false),
        }
    }

    fn key<M: Model<Paragraph = P>>(
        &mut self,
        model: &mut M,
        key: Key,
        modifiers: Modifiers,
    ) -> (bool, bool) {
        let Some(selection) = self.selection.clone() else {
            return (false, false);
        };
        // Alt on a Mac, Ctrl elsewhere, and egui's own text box takes either.
        let word = modifiers.alt || modifiers.ctrl;
        let extend = modifiers.shift;
        match key {
            Key::ArrowLeft | Key::ArrowRight => {
                let right = key == Key::ArrowRight;
                let to = if !selection.is_caret() && !extend {
                    // An arrow on a selection goes to its near end.
                    let (from, to) = self.ordered(&selection);
                    if right { to } else { from }
                } else if modifiers.mac_cmd {
                    self.row_edge(model, &selection.focus, right)
                } else if right {
                    step_right(model, &selection.focus, word)
                } else {
                    step_left(model, &selection.focus, word)
                };
                self.next_row = false;
                self.move_to(to, extend);
                (true, false)
            }
            Key::A if modifiers.command => {
                let (Some(first), Some(last)) = (model.first(), model.last()) else {
                    return (false, false);
                };
                self.move_to(Position::new(first, 0), false);
                self.move_to(edge(model, last, true), true);
                (true, false)
            }
            Key::Home | Key::End if modifiers.command => {
                let end = key == Key::End;
                let paragraph = if end { model.last() } else { model.first() };
                let Some(paragraph) = paragraph else {
                    return (false, false);
                };
                self.next_row = false;
                self.move_to(edge(model, paragraph, end), extend);
                (true, false)
            }
            Key::Home | Key::End => {
                let to = self.row_edge(model, &selection.focus, key == Key::End);
                self.move_to(to, extend);
                (true, false)
            }
            Key::PageUp | Key::PageDown => {
                let Some(to) = self.page(&selection.focus, key == Key::PageDown) else {
                    return (false, false);
                };
                let column = self.column;
                self.move_to(to, extend);
                self.column = column;
                (true, false)
            }
            Key::ArrowUp | Key::ArrowDown => {
                let to = self.vertical(model, &selection.focus, key == Key::ArrowDown);
                // The column `vertical` aimed at, kept across the run, which
                // `move_to` would forget.
                let column = self.column;
                self.move_to(to, extend);
                self.column = column;
                (true, false)
            }
            Key::Backspace | Key::Delete => {
                if !selection.is_caret() {
                    return (false, self.delete_selection(model, Group::Deleting));
                }
                let at = selection.focus;
                let (from, to) = if key == Key::Backspace {
                    (step_left(model, &at, word), at)
                } else {
                    let to = step_right(model, &at, word);
                    (at, to)
                };
                if from == to {
                    return (false, false);
                }
                let edit = Edit::Replace { from, to, text: "" };
                (false, self.apply(model, edit, Group::Deleting))
            }
            Key::Enter if modifiers.shift => (false, self.type_text(model, "\n")),
            Key::Enter => {
                self.group = None;
                // A selection the model will not delete is not split in the
                // middle of either.
                let changed = self.delete_selection(model, Group::Other);
                if !changed && !selection.is_caret() {
                    return (false, false);
                }
                let mut changed = changed;
                let at = self.selection.clone().map(|s| s.focus);
                if let Some(at) = at {
                    changed |= self.apply(model, Edit::Split { at }, Group::Other);
                }
                self.group = None;
                (false, changed)
            }
            Key::Tab if modifiers.is_none() => (false, self.type_text(model, "\t")),
            _ => (false, false),
        }
    }

    /// Put the caret at a position, or take the selection's focus there.
    fn move_to(&mut self, to: Position<P>, extend: bool) {
        self.group = None;
        self.column = None;
        match (&mut self.selection, extend) {
            (Some(selection), true) => selection.focus = to,
            _ => self.selection = Some(Selection::caret(to)),
        }
    }

    /// Replace the selection with text, as typing does, and give what was
    /// typed the marks given or taken off at the caret.
    fn type_text<M: Model<Paragraph = P>>(&mut self, model: &mut M, text: &str) -> bool {
        let Some(selection) = &self.selection else {
            return false;
        };
        let (from, to) = self.ordered(selection);
        let marks: Vec<(Mark, bool)> = Mark::ALL
            .into_iter()
            .filter_map(|mark| self.pending_mark(mark).map(|on| (mark, on)))
            .collect();
        let edit = Edit::Replace {
            from: from.clone(),
            to,
            text,
        };
        if !self.apply(model, edit, Group::Typing) {
            return false;
        }
        if let Some(end) = self.selection.as_ref().map(|s| s.focus.clone()) {
            for (mark, on) in marks {
                self.format(model, from.clone(), end.clone(), mark, on, Group::Typing);
            }
        }
        true
    }

    /// Paste text, each of its lines after the first a paragraph of its own,
    /// in one undo step. It stops at the first part the model refuses, so a
    /// selection that cannot be replaced is left as it was.
    /// Paste the last copy where the caret is, in place of the selection,
    /// when the clipboard still holds the text it put there. `None` when it
    /// does not or the model will not, and the paste is plain.
    fn paste_copy<M: Model<Paragraph = P>>(&mut self, model: &mut M, text: &str) -> Option<bool> {
        let fragment = self
            .clip
            .as_ref()
            .filter(|(plain, _)| plain == text)
            .map(|(_, fragment)| fragment.clone())?;
        self.selection.as_ref()?;
        let deleted = self.delete_selection(model, Group::Other);
        let at = self.selection.as_ref().map(|s| s.focus.clone())?;
        let after = model.paste_fragment(&at, &fragment, !deleted)?;
        self.selection = Some(Selection::caret(after));
        self.owed = Owed::Reveal;
        Some(true)
    }

    /// Put the selection on the clipboard as text, and keep a copy of it that
    /// has its formatting for a paste of that text. Answers whether there was a
    /// selection.
    fn copy<M: Model<Paragraph = P>>(&mut self, ui: &Ui, model: &M) -> bool {
        let Some(text) = self.selected_text(model) else {
            return false;
        };
        ui.ctx().copy_text(text.clone());
        self.clip = self
            .selection
            .as_ref()
            .map(|selection| self.ordered(selection))
            .and_then(|(from, to)| model.fragment(&from, &to))
            .map(|fragment| (text, fragment));
        true
    }

    /// Paste what the clipboard holds, as the copy it came from if it is that.
    fn paste<M: Model<Paragraph = P>>(&mut self, model: &mut M, text: &str) -> bool {
        let text = text.replace("\r\n", "\n").replace('\r', "\n");
        self.group = None;
        if let Some(changed) = self.paste_copy(model, &text) {
            self.group = None;
            return changed;
        }
        let mut changed = false;
        for (index, line) in text.split('\n').enumerate() {
            if index > 0 {
                let Some(at) = self.selection.clone().map(|s| s.focus) else {
                    break;
                };
                if !self.apply(model, Edit::Split { at }, Group::Other) {
                    break;
                }
                changed = true;
            }
            if index == 0 || !line.is_empty() {
                let Some(selection) = &self.selection else {
                    break;
                };
                let (from, to) = self.ordered(selection);
                let edit = Edit::Replace {
                    from,
                    to,
                    text: line,
                };
                if !self.apply(model, edit, Group::Other) {
                    break;
                }
                changed = true;
            }
        }
        self.group = None;
        changed
    }

    /// Delete what is selected, if anything is.
    fn delete_selection<M: Model<Paragraph = P>>(&mut self, model: &mut M, group: Group) -> bool {
        let Some(selection) = self.selection.as_ref().filter(|s| !s.is_caret()) else {
            return false;
        };
        let (from, to) = self.ordered(selection);
        self.apply(model, Edit::Replace { from, to, text: "" }, group)
    }

    /// Hand an edit to the model, beginning an undo step unless it continues
    /// the kind of edit before it, and put the caret where the model says.
    fn apply<M: Model<Paragraph = P>>(
        &mut self,
        model: &mut M,
        edit: Edit<'_, P>,
        group: Group,
    ) -> bool {
        let new_step = self.group != Some(group);
        let Some(at) = model.apply(edit, new_step) else {
            return false;
        };
        self.group = Some(group);
        self.selection = Some(Selection::caret(at));
        self.next_row = false;
        self.column = None;
        self.pending = None;
        true
    }

    /// Hand a format to the model, as [`Self::apply`] does an edit, leaving
    /// the selection where it is.
    fn format<M: Model<Paragraph = P>>(
        &mut self,
        model: &mut M,
        from: Position<P>,
        to: Position<P>,
        mark: Mark,
        on: bool,
        group: Group,
    ) -> bool {
        let new_step = self.group != Some(group);
        let edit = Edit::Format { from, to, mark, on };
        if model.apply(edit, new_step).is_none() {
            return false;
        }
        self.group = Some(group);
        true
    }

    /// The selection's two ends, the earlier first. Across paragraphs, which
    /// is earlier is the order they were drawn in last frame.
    fn ordered(&self, selection: &Selection<P>) -> (Position<P>, Position<P>) {
        let Selection { anchor, focus } = selection;
        let anchor_first = if anchor.paragraph == focus.paragraph {
            anchor.offset <= focus.offset
        } else {
            match (
                self.placed.get(&anchor.paragraph),
                self.placed.get(&focus.paragraph),
            ) {
                (Some(a), Some(f)) => a.index < f.index,
                _ => true,
            }
        };
        if anchor_first {
            (anchor.clone(), focus.clone())
        } else {
            (focus.clone(), anchor.clone())
        }
    }

    /// The selected text, paragraphs separated by newlines. `None` for a
    /// caret.
    fn selected_text<M: Model<Paragraph = P>>(&self, model: &M) -> Option<String> {
        let selection = self.selection.as_ref().filter(|s| !s.is_caret())?;
        let (from, to) = self.ordered(selection);
        let mut out = String::new();
        let mut paragraph = from.paragraph;
        let mut start = from.offset;
        loop {
            let text = model.text(&paragraph)?;
            if paragraph == to.paragraph {
                out.extend(
                    text.chars()
                        .skip(start)
                        .take(to.offset.saturating_sub(start)),
                );
                return Some(out);
            }
            out.extend(text.chars().skip(start));
            out.push('\n');
            paragraph = model.next(&paragraph)?;
            start = 0;
        }
    }

    /// The start or the end of the row a position is drawn on. Without last
    /// frame's layout of its paragraph, the paragraph's own start or end.
    fn row_edge<M: Model<Paragraph = P>>(
        &mut self,
        model: &M,
        at: &Position<P>,
        end: bool,
    ) -> Position<P> {
        let Some(placed) = self.placed.get(&at.paragraph) else {
            return edge(model, at.paragraph.clone(), end);
        };
        let cursor = CCursor {
            index: CharIndex(placed.map.to_galley(at.offset)),
            prefer_next_row: self.next_row,
        };
        let edge = if end {
            placed.galley.cursor_end_of_row(&cursor)
        } else {
            placed.galley.cursor_begin_of_row(&cursor)
        };
        // The end of a wrapped row and the start of the next are the same
        // offset; which row the caret is drawn on is what was asked for.
        self.next_row = !end;
        Position::new(at.paragraph.clone(), placed.map.to_model(edge.index.0))
    }

    /// The position a view's height above or below another, keeping to the
    /// column, in whichever paragraph last frame drew nearest there. `None`
    /// without last frame's layout of the paragraph the caret is in.
    fn page(&mut self, at: &Position<P>, down: bool) -> Option<Position<P>> {
        let placed = self.placed.get(&at.paragraph)?;
        let cursor = CCursor {
            index: CharIndex(placed.map.to_galley(at.offset)),
            prefer_next_row: self.next_row,
        };
        let caret = placed
            .galley
            .pos_from_cursor(cursor)
            .translate(placed.origin.to_vec2());
        let x = self.column.unwrap_or_else(|| caret.center().x);
        let y = caret.center().y
            + if down {
                self.page_height
            } else {
                -self.page_height
            };
        let distance = |placed: &Placed| {
            let top = placed.origin.y + placed.galley.rect.top();
            let bottom = placed.origin.y + placed.galley.rect.bottom();
            (top - y).max(y - bottom).max(0.0)
        };
        let (paragraph, there) = self
            .placed
            .iter()
            .min_by(|(_, a), (_, b)| distance(a).total_cmp(&distance(b)))?;
        let landed = there
            .galley
            .cursor_from_pos(vec2(x - there.origin.x, y - there.origin.y));
        let to = Position::new(paragraph.clone(), there.map.to_model(landed.index.0));
        self.column = Some(x);
        self.next_row = landed.prefer_next_row;
        Some(to)
    }

    /// The position one row up or down from another, keeping to the column
    /// a run of Up and Down began in, and crossing into the paragraph before
    /// or after at the first or last row.
    fn vertical<M: Model<Paragraph = P>>(
        &mut self,
        model: &M,
        at: &Position<P>,
        down: bool,
    ) -> Position<P> {
        let neighbour = if down {
            model.next(&at.paragraph)
        } else {
            model.previous(&at.paragraph)
        };
        let edge_of_this = || edge(model, at.paragraph.clone(), down);
        let Some(placed) = self.placed.get(&at.paragraph) else {
            return neighbour.map_or_else(edge_of_this, |p| Position::new(p, 0));
        };
        let galley = &placed.galley;
        let cursor = CCursor {
            index: CharIndex(placed.map.to_galley(at.offset)),
            prefer_next_row: self.next_row,
        };
        let x = self
            .column
            .unwrap_or_else(|| placed.origin.x + galley.pos_from_cursor(cursor).center().x);
        self.column = Some(x);

        let row = galley.layout_from_cursor(cursor).row;
        let last_row = galley.rows.len().saturating_sub(1);
        if (down && row < last_row) || (!down && row > 0) {
            let h = Some(x - placed.origin.x);
            let (moved, _) = if down {
                galley.cursor_down_one_row(&cursor, h)
            } else {
                galley.cursor_up_one_row(&cursor, h)
            };
            self.next_row = moved.prefer_next_row;
            return Position::new(at.paragraph.clone(), placed.map.to_model(moved.index.0));
        }

        let Some(neighbour) = neighbour else {
            return edge_of_this();
        };
        let Some(there) = self.placed.get(&neighbour) else {
            return edge(model, neighbour, !down);
        };
        let target_row = if down {
            there.galley.rows.first()
        } else {
            there.galley.rows.last()
        };
        let y = target_row.map_or(0.0, |row| row.rect().center().y);
        let landed = there.galley.cursor_from_pos(vec2(x - there.origin.x, y));
        self.next_row = landed.prefer_next_row;
        Position::new(neighbour.clone(), there.map.to_model(landed.index.0))
    }

    /// What the pointer does on a paragraph: a press puts the caret down,
    /// with Shift extends the selection, a drag extends it through whichever
    /// paragraph the pointer is over, a double-click takes a word and a
    /// triple-click the paragraph.
    fn pointer(
        &mut self,
        ui: &Ui,
        response: &Response,
        paragraph: &P,
        galley: &Galley,
        map: &OffsetMap,
        origin: Pos2,
    ) {
        let at_pointer = |pos: Pos2| galley.cursor_from_pos(pos - origin);
        let (pressed, down, shift, pos) = ui.input(|input| {
            (
                input.pointer.primary_pressed(),
                input.pointer.primary_down(),
                input.modifiers.shift,
                input.pointer.interact_pos(),
            )
        });

        // A press and its release can arrive in one frame, a quick click on a
        // slow frame, and then the button is no longer down on anything; the
        // click says where it was. A paragraph that leaves drags to what is
        // under it takes the click alone, so a press that becomes a drag puts
        // no caret down.
        let placed = if response.sense.senses_drag() {
            pressed && (response.is_pointer_button_down_on() || response.clicked())
        } else {
            response.clicked()
        };
        if placed && let Some(pos) = pos {
            let cursor = at_pointer(pos);
            let to = Position::new(paragraph.clone(), map.to_model(cursor.index.0));
            match &mut self.selection {
                Some(selection) if shift => selection.focus = to,
                _ => self.selection = Some(Selection::caret(to)),
            }
            self.next_row = cursor.prefer_next_row;
            self.group = None;
            self.column = None;
            // A paragraph that does not sense drags leaves them to what is
            // under it, as a slide does to move the shape.
            self.dragging = response.sense.senses_drag();
            self.last_interaction = ui.input(|input| input.time);
            ui.memory_mut(|memory| memory.request_focus(self.id));
        } else if self.dragging && !down {
            self.dragging = false;
        } else if self.dragging
            && let Some(pos) = pos
            // The paragraph the pointer is over, and not merely level with:
            // cells in a table row share their height.
            && response.rect.contains(pos)
            && let Some(selection) = &mut self.selection
        {
            let cursor = at_pointer(pos);
            selection.focus = Position::new(paragraph.clone(), map.to_model(cursor.index.0));
            self.next_row = cursor.prefer_next_row;
            self.last_interaction = ui.input(|input| input.time);
        }

        // egui takes the focus from a widget on any click it does not hover,
        // and the editor's own is under the paragraphs, so a click ending on
        // one would take the keyboard from the caret it just put down. The
        // paragraph is the editor's, and the focus comes back to it.
        if response.clicked() {
            ui.memory_mut(|memory| memory.request_focus(self.id));
        }

        if (response.double_clicked() || response.triple_clicked())
            && let Some(pos) = pos
        {
            let (start, end) = if response.triple_clicked() {
                (0, map.model_len())
            } else {
                let (start, end) = word_around(&galley.job.text, at_pointer(pos).index.0);
                (map.to_model(start), map.to_model(end))
            };
            self.selection = Some(Selection {
                anchor: Position::new(paragraph.clone(), start),
                focus: Position::new(paragraph.clone(), end),
            });
            self.next_row = false;
        }
    }

    /// The part of the selection in this paragraph, in galley characters.
    ///
    /// Paragraphs are painted in document order, so the first end of the
    /// selection met is its start, and every paragraph until the other end is
    /// selected whole.
    fn selected_here(&mut self, paragraph: &P, map: &OffsetMap) -> Option<CCursorRange> {
        let selection = self.selection.as_ref().filter(|s| !s.is_caret())?;
        let (anchor, focus) = (&selection.anchor, &selection.focus);
        let (start, end) = match (
            anchor.paragraph == *paragraph,
            focus.paragraph == *paragraph,
        ) {
            (true, true) => (
                anchor.offset.min(focus.offset),
                anchor.offset.max(focus.offset),
            ),
            (true, false) | (false, true) => {
                let at = if anchor.paragraph == *paragraph {
                    anchor.offset
                } else {
                    focus.offset
                };
                self.placing.inside = !self.placing.inside;
                if self.placing.inside {
                    (at, map.model_len())
                } else {
                    (0, at)
                }
            }
            (false, false) if self.placing.inside => (0, map.model_len()),
            (false, false) => return None,
        };
        Some(CCursorRange::two(
            CCursor::new(map.to_galley(start)),
            CCursor::new(map.to_galley(end)),
        ))
    }
}

/// The mark a key toggles with Ctrl, or Command on a Mac. Strikethrough has
/// no shortcut that people share, so it has none.
fn shortcut(key: Key) -> Option<Mark> {
    match key {
        Key::B => Some(Mark::Bold),
        Key::I => Some(Mark::Italic),
        Key::U => Some(Mark::Underline),
        _ => None,
    }
}

/// How many characters a paragraph holds, or `None` when it is gone.
fn len<M: Model>(model: &M, paragraph: &M::Paragraph) -> Option<usize> {
    model.text(paragraph).map(|text| text.chars().count())
}

/// The start of a paragraph, or its end.
fn edge<M: Model>(model: &M, paragraph: M::Paragraph, end: bool) -> Position<M::Paragraph> {
    let offset = if end {
        len(model, &paragraph).unwrap_or(0)
    } else {
        0
    };
    Position::new(paragraph, offset)
}

/// One character or one word left, into the end of the paragraph before at
/// the start of this one.
fn step_left<M: Model>(
    model: &M,
    at: &Position<M::Paragraph>,
    word: bool,
) -> Position<M::Paragraph> {
    if at.offset == 0 {
        return model
            .previous(&at.paragraph)
            .map_or_else(|| at.clone(), |p| edge(model, p, true));
    }
    let offset = if word {
        let text = model.text(&at.paragraph).unwrap_or_default();
        ccursor_previous_word(&text, CCursor::new(at.offset))
            .index
            .0
    } else {
        at.offset - 1
    };
    Position::new(at.paragraph.clone(), offset)
}

/// One character or one word right, into the start of the paragraph after at
/// the end of this one.
fn step_right<M: Model>(
    model: &M,
    at: &Position<M::Paragraph>,
    word: bool,
) -> Position<M::Paragraph> {
    let text = model.text(&at.paragraph).unwrap_or_default();
    let len = text.chars().count();
    if at.offset >= len {
        return model
            .next(&at.paragraph)
            .map_or_else(|| at.clone(), |p| Position::new(p, 0));
    }
    let offset = if word {
        ccursor_next_word(&text, CCursor::new(at.offset)).index.0
    } else {
        at.offset + 1
    };
    Position::new(at.paragraph.clone(), offset.min(len))
}

/// The word a character index falls in or beside, as a range of indices.
fn word_around(text: &str, index: usize) -> (usize, usize) {
    let chars: Vec<char> = text.chars().collect();
    let index = index.min(chars.len());
    let mut start = index;
    while start > 0 && is_word_char(chars[start - 1]) {
        start -= 1;
    }
    let mut end = index;
    while end < chars.len() && is_word_char(chars[end]) {
        end += 1;
    }
    (start, end)
}

/// A drag that has left the view scrolls it, faster the further out the
/// pointer is, so that a selection can be taken on to where the pointer cannot
/// reach.
fn scroll_toward_pointer(ui: &Ui) {
    const SPEED: f32 = 0.2;
    const SLOWEST: f32 = 2.0;
    const FASTEST: f32 = 30.0;
    let Some(pointer) = ui.input(|input| input.pointer.latest_pos()) else {
        return;
    };
    let view = ui.clip_rect();
    let past = (pointer.y - view.bottom()).max(0.0) - (view.top() - pointer.y).max(0.0);
    if past == 0.0 {
        return;
    }
    let step = (past.abs() * SPEED).clamp(SLOWEST, FASTEST);
    ui.scroll_with_delta(vec2(0.0, -step.copysign(past)));
    ui.ctx().request_repaint();
}