perfectstar2k 0.4.0

A modern TUI homage to WordStar & WordPerfect for DOS — a real writing tool built on WordStar's touch-typist command language and long-hand-page metaphor.
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
use ratatui::Frame;
use ratatui::layout::{Alignment, Position, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use crate::app::{App, Mode};
use crate::buffer::{grapheme_width, wrap_segments};
use crate::keymap;
use crate::markdown::{self, MdKind};
use crate::outline;
use crate::pane::Pane;
use crate::search::ReplacePhase;
use crate::spellcheck;
use crate::splash;

pub fn draw(frame: &mut Frame, app: &mut App) {
    let area = frame.area();
    if app.splash {
        draw_splash(frame, app, area);
        return;
    }
    if area.height < 2 {
        return;
    }
    let mut text_area = Rect {
        height: area.height - 1,
        ..area
    };
    let status_area = Rect {
        y: area.y + area.height - 1,
        height: 1,
        ..area
    };

    // Hint bar at help level 2.
    let hint_area = if app.help_level >= 2 && text_area.height >= 4 {
        text_area.height -= 1;
        Some(Rect {
            y: text_area.y + text_area.height,
            height: 1,
            ..text_area
        })
    } else {
        None
    };

    // Reveal Codes: carve a pane off the bottom of the text area.
    let reveal_area = if app.reveal && text_area.height >= 8 {
        let pane_h = (text_area.height * 2 / 5).clamp(4, 12);
        text_area.height -= pane_h;
        Some(Rect {
            y: text_area.y + text_area.height,
            height: pane_h,
            ..text_area
        })
    } else {
        None
    };

    // Window layout: one pane fills the text area; two stack vertically,
    // each over its own modeline. On a terminal too short to split, only
    // the active pane is shown full-size.
    let split = app.panes.len() == 2 && text_area.height >= 6;
    let (pane_ids, pane_areas, modelines) = if split {
        let h = text_area.height;
        let p0_h = (h - 2) / 2;
        let p1_h = h - 2 - p0_h;
        let a0 = Rect {
            height: p0_h,
            ..text_area
        };
        let m0 = Rect {
            y: text_area.y + p0_h,
            height: 1,
            ..text_area
        };
        let a1 = Rect {
            y: m0.y + 1,
            height: p1_h,
            ..text_area
        };
        let m1 = Rect {
            y: a1.y + p1_h,
            height: 1,
            ..text_area
        };
        (vec![0usize, 1], vec![a0, a1], vec![m0, m1])
    } else {
        (vec![app.active], vec![text_area], Vec::new())
    };

    for (slot, &pid) in pane_ids.iter().enumerate() {
        app.panes[pid].view_rows = pane_areas[slot].height as usize;
        app.panes[pid].view_cols = pane_areas[slot].width as usize;
    }
    app.ensure_visible();

    for (slot, &pid) in pane_ids.iter().enumerate() {
        draw_text(frame, app, pid, pane_areas[slot]);
    }
    for (slot, &pid) in pane_ids.iter().enumerate() {
        if let Some(m) = modelines.get(slot) {
            draw_modeline(frame, app, pid, *m);
        }
    }
    if let Some(ra) = reveal_area {
        draw_reveal(frame, app, ra);
    }
    if let Some(ha) = hint_area {
        draw_hints(frame, app, ha);
    }
    draw_status(frame, app, status_area);
    if app.help_level >= 1 {
        draw_prefix_menu(frame, app, text_area);
    }
    if matches!(app.mode, Mode::Palette { .. }) {
        draw_palette(frame, app, text_area);
    }
    if matches!(app.mode, Mode::Outline { .. }) {
        draw_outline(frame, app, text_area);
    }
    if matches!(app.mode, Mode::Binder { .. }) {
        draw_binder(frame, app, text_area);
    }
    if matches!(app.mode, Mode::Stats) {
        draw_stats_overlay(frame, app, text_area);
    }
    if matches!(app.mode, Mode::ProjectSearch { .. }) {
        draw_project_search(frame, app, text_area);
    }
    let active_slot = pane_ids.iter().position(|&p| p == app.active).unwrap_or(0);
    place_cursor(frame, app, pane_areas[active_slot]);
}

/// The divider under each window when split: filename, dirty dot, and line
/// number, drawn as a solid bar (bright for the focused window, dim for the
/// other) so it always reads which window has the keyboard.
fn draw_modeline(frame: &mut Frame, app: &App, pane_idx: usize, area: Rect) {
    let pane = &app.panes[pane_idx];
    let is_active = pane_idx == app.active;
    let dirty = if pane.buf.dirty { "" } else { "" };
    let line_no = pane.buf.line_of(pane.cursor) + 1;
    let marker = if is_active { "" } else { "" };
    let label = format!("{marker} {}{dirty} ─ Ln {line_no} ", pane.buf.file_name());
    let fill =
        "".repeat((area.width as usize).saturating_sub(UnicodeWidthStr::width(label.as_str())));
    let style = if is_active {
        app.theme.status
    } else {
        app.theme.dim
    };
    frame.render_widget(
        Paragraph::new(Line::from(format!("{label}{fill}"))).style(style),
        area,
    );
}

/// The startup splash: a big block-letter banner in a double-bordered box,
/// dismissed by any keypress. Falls back to a single centered line if the
/// terminal is too small for the full banner.
fn draw_splash(frame: &mut Frame, app: &App, area: Rect) {
    if area.width == 0 || area.height == 0 {
        return;
    }
    frame.render_widget(Block::new().style(app.theme.base), area);

    const TAGLINE: &str = "A Writer's Word Processor";
    const BYLINE: &str = "for WordStar & WordPerfect";
    const PROMPT: &str = "Press any key to continue";

    let banner = splash::banner("PERFECTSTAR");
    let banner_width = banner[0].chars().count() as u16;
    let content_width = banner_width
        .max(TAGLINE.len() as u16)
        .max(BYLINE.len() as u16)
        .max(PROMPT.len() as u16);

    let mut lines: Vec<Line> = vec![Line::default()];
    for row in &banner {
        lines.push(Line::styled(row.clone(), app.theme.md_heading));
    }
    lines.push(Line::default());
    lines.push(Line::styled(TAGLINE, app.theme.dim));
    lines.push(Line::styled(BYLINE, app.theme.dim));
    lines.push(Line::default());
    lines.push(Line::styled(
        PROMPT,
        app.theme
            .base
            .add_modifier(Modifier::BOLD | Modifier::SLOW_BLINK),
    ));
    let content_height = lines.len() as u16;

    // Too small for the block banner: fall back to plain centered text.
    if area.width < content_width + 6 || area.height < content_height + 2 {
        let y = area.y + area.height.saturating_sub(1) / 2;
        let fallback_area = Rect {
            x: area.x,
            y,
            width: area.width,
            height: 1,
        };
        frame.render_widget(
            Paragraph::new(Line::styled("PerfectStar", app.theme.md_heading))
                .alignment(Alignment::Center)
                .style(app.theme.base),
            fallback_area,
        );
        return;
    }

    let box_width = (content_width + 8).min(area.width);
    let box_height = (content_height + 4).min(area.height);
    let box_area = Rect {
        x: area.x + (area.width - box_width) / 2,
        y: area.y + (area.height - box_height) / 2,
        width: box_width,
        height: box_height,
    };

    frame.render_widget(Clear, box_area);
    frame.render_widget(
        Paragraph::new(lines)
            .alignment(Alignment::Center)
            .style(app.theme.base)
            .block(
                Block::new()
                    .borders(Borders::ALL)
                    .border_type(BorderType::Double)
                    .style(app.theme.base),
            ),
        box_area,
    );
}

fn draw_hints(frame: &mut Frame, app: &App, area: Rect) {
    let hints = " ^KD save · ^KQ quit · ^QF find · ^KB/^KK mark · ^KC copy · ^KV move · ^KP put · ^U undo · Esc commands";
    frame.render_widget(Paragraph::new(Line::from(hints)).style(app.theme.dim), area);
}

fn draw_text(frame: &mut Frame, app: &App, pane_idx: usize, area: Rect) {
    let pane = &app.panes[pane_idx];
    let is_active = pane_idx == app.active;
    // Search-match highlighting follows the keyboard: only the focused
    // window shows live matches. Each window paints its own marked block.
    let query = if is_active {
        app.active_query().map(str::to_owned)
    } else {
        None
    };
    let block_range = pane.blocks.visible_range();
    let height = area.height as usize;
    let last = pane.buf.len_lines();
    let mut lines: Vec<Line> = Vec::with_capacity(height);

    match app.wrap_width_of(pane) {
        Some(width) => {
            let mut doc_line = pane.top_line;
            while lines.len() < height && doc_line < last {
                let (text, styles) =
                    line_styles(app, pane, doc_line, query.as_deref(), block_range);
                for (s, e) in wrap_segments(&text, width) {
                    if lines.len() >= height {
                        break;
                    }
                    let (seg_text, seg_styles) = char_slice(&text, &styles, s, e);
                    lines.push(styled_clip(&seg_text, &seg_styles, 0, width));
                }
                doc_line += 1;
            }
        }
        None => {
            for row in 0..height {
                let doc_line = pane.top_line + row;
                if doc_line >= last {
                    break;
                }
                let (text, styles) =
                    line_styles(app, pane, doc_line, query.as_deref(), block_range);
                lines.push(styled_clip(
                    &text,
                    &styles,
                    pane.left_col,
                    area.width as usize,
                ));
            }
        }
    }
    while lines.len() < height {
        lines.push(Line::default());
    }
    frame.render_widget(Paragraph::new(lines).style(app.theme.base), area);
}

/// The line's text plus one resolved style per char (markdown, then search
/// matches, then the marked block on top). Note lines are wholly dimmed.
fn line_styles(
    app: &App,
    pane: &Pane,
    doc_line: usize,
    query: Option<&str>,
    block_range: Option<(usize, usize)>,
) -> (String, Vec<Style>) {
    let text = pane.buf.line_text(doc_line).into_owned();
    let n_chars = text.chars().count();

    if crate::normalize::is_note_line(&text) {
        let styles = vec![app.theme.dim; n_chars];
        return (text, styles);
    }

    let line_start = pane.buf.line_start(doc_line);
    let mut styles: Vec<Style> = vec![Style::default(); n_chars];

    for (s, e, kind) in markdown::scan_line(&text) {
        let style = md_style(app, kind);
        for st in styles.iter_mut().take(e.min(n_chars)).skip(s) {
            *st = style;
        }
    }

    if app.spell_enabled {
        for (s, e) in spellcheck::word_spans(&text) {
            let word: String = text.chars().skip(s).take(e - s).collect();
            if !app.spell.check(&word) {
                for st in styles.iter_mut().take(e.min(n_chars)).skip(s) {
                    *st = st.patch(app.theme.misspelled);
                }
            }
        }
    }

    if let Some(q) = query {
        for (s, e) in match_ranges(&text, q) {
            for st in styles.iter_mut().take(e.min(n_chars)).skip(s) {
                *st = app.theme.highlight;
            }
        }
    }

    if let Some((b, e)) = block_range {
        if e > line_start && b < line_start + n_chars {
            let from = b.saturating_sub(line_start).min(n_chars);
            let to = e.saturating_sub(line_start).min(n_chars);
            for st in styles.iter_mut().take(to).skip(from) {
                *st = app.theme.block;
            }
        }
    }

    (text, styles)
}

/// Slice text + parallel styles by char range.
fn char_slice(text: &str, styles: &[Style], s: usize, e: usize) -> (String, Vec<Style>) {
    let seg: String = text.chars().skip(s).take(e - s).collect();
    let seg_styles = styles[s.min(styles.len())..e.min(styles.len())].to_vec();
    (seg, seg_styles)
}

fn md_style(app: &App, kind: MdKind) -> Style {
    match kind {
        MdKind::Marker => app.theme.md_marker,
        MdKind::Bold => app.theme.md_bold,
        MdKind::Italic => app.theme.md_italic,
        MdKind::Code => app.theme.md_code,
        MdKind::Heading => app.theme.md_heading,
    }
}

/// Walk graphemes, clipping to the window and grouping runs of equal style.
fn styled_clip(text: &str, styles: &[Style], left: usize, width: usize) -> Line<'static> {
    let mut spans: Vec<Span<'static>> = Vec::new();
    let mut run = String::new();
    let mut run_style = Style::default();
    let mut vcol = 0usize;
    let mut char_idx = 0usize;
    let flush = |run: &mut String, style: Style, spans: &mut Vec<Span<'static>>| {
        if !run.is_empty() {
            spans.push(Span::styled(std::mem::take(run), style));
        }
    };
    for g in text.graphemes(true) {
        let w = grapheme_width(g, vcol);
        let g_start = vcol;
        let g_chars = g.chars().count();
        let style = styles.get(char_idx).copied().unwrap_or_default();
        vcol += w;
        char_idx += g_chars;
        if vcol <= left {
            continue;
        }
        if g_start >= left + width {
            break;
        }
        if style != run_style {
            flush(&mut run, run_style, &mut spans);
            run_style = style;
        }
        if g == "\t" {
            let from = g_start.max(left);
            let to = vcol.min(left + width);
            run.push_str(&" ".repeat(to - from));
        } else if g_start < left {
            run.push_str(&" ".repeat(vcol - left));
        } else {
            run.push_str(g);
        }
    }
    flush(&mut run, run_style, &mut spans);
    Line::from(spans)
}

/// Char ranges of `query` matches within `text` (smartcase).
fn match_ranges(text: &str, query: &str) -> Vec<(usize, usize)> {
    if query.is_empty() {
        return Vec::new();
    }
    let fold = !query.chars().any(|c| c.is_uppercase());
    let hay: Vec<char> = if fold {
        text.chars()
            .map(|c| c.to_lowercase().next().unwrap_or(c))
            .collect()
    } else {
        text.chars().collect()
    };
    let needle: Vec<char> = if fold {
        query
            .chars()
            .map(|c| c.to_lowercase().next().unwrap_or(c))
            .collect()
    } else {
        query.chars().collect()
    };
    let mut out = Vec::new();
    if needle.len() > hay.len() {
        return out;
    }
    let mut i = 0;
    while i + needle.len() <= hay.len() {
        if hay[i..i + needle.len()] == needle[..] {
            out.push((i, i + needle.len()));
            i += needle.len();
        } else {
            i += 1;
        }
    }
    out
}

/// The Reveal Codes pane (^OD): the lines around the cursor with every
/// markup character shown in inverse video, WP 5.1 style.
fn draw_reveal(frame: &mut Frame, app: &App, area: Rect) {
    let title_area = Rect { height: 1, ..area };
    let body = Rect {
        y: area.y + 1,
        height: area.height - 1,
        ..area
    };

    let title = format!(
        "─ Reveal Codes ─ Ln {} {}",
        app.buf.line_of(app.cursor) + 1,
        "".repeat(area.width as usize),
    );
    frame.render_widget(
        Paragraph::new(Line::from(title)).style(app.theme.status),
        title_area,
    );

    let rows = body.height as usize;
    let cursor_line = app.buf.line_of(app.cursor);
    let first = cursor_line.saturating_sub(rows / 2);
    let last_line = app.buf.len_lines();
    let mut lines: Vec<Line> = Vec::with_capacity(rows);
    for row in 0..rows {
        let doc_line = first + row;
        if doc_line >= last_line {
            lines.push(Line::default());
            continue;
        }
        let text = app.buf.line_text(doc_line).into_owned();
        let n_chars = text.chars().count();
        let mut styles: Vec<Style> = vec![Style::default(); n_chars];
        for (s, e, kind) in markdown::scan_line(&text) {
            if kind == MdKind::Marker {
                for st in styles.iter_mut().take(e.min(n_chars)).skip(s) {
                    *st = app.theme.block;
                }
            }
        }
        lines.push(styled_clip(
            &text,
            &styles,
            app.left_col,
            body.width as usize,
        ));
    }
    frame.render_widget(Paragraph::new(lines).style(app.theme.base), body);
}

fn status_left(app: &App) -> String {
    match &app.mode {
        Mode::ConfirmAbandon => {
            format!(" Abandon changes to {}? (y/N)", app.buf.file_name())
        }
        Mode::ConfirmRecover => format!(
            " Recover unsaved changes to {}? (y/N · Esc decline)",
            app.buf.file_name()
        ),
        Mode::Search(s) => {
            format!(" Find: {}▌  (Enter accept · ^L next · Esc cancel)", s.query)
        }
        Mode::Replace(r) => match r.phase {
            ReplacePhase::EnterFind => format!(" Replace: {}", r.find),
            ReplacePhase::EnterWith => {
                format!(" Replace: {}  With: {}", r.find, r.with)
            }
            ReplacePhase::EnterOptions => format!(
                " Options (g=from top, n=no ask, w=whole words): {}",
                r.options
            ),
            ReplacePhase::Confirm(_) => String::from(" Replace? (Y/n/a=all/q=quit)"),
        },
        Mode::Input { label, value, .. } => format!(" {label}: {value}"),
        Mode::Palette { .. } => String::from(" ↑↓ select · Enter run · Esc close"),
        Mode::Outline { .. } => String::from(" ↑↓ select · Enter go to heading · Esc close"),
        Mode::Binder { .. } => String::from(" ↑↓ select · Enter open document · Esc close"),
        Mode::Stats => String::from(" Writing Stats · press any key to close"),
        Mode::ProjectSearch { query, results, .. } => {
            format!(
                " Project search: \"{}\"{} match(es) · ↑↓ navigate · Enter open · Esc close",
                query,
                results.len()
            )
        }
        Mode::Normal => match &app.status_msg {
            Some(msg) => format!(" {msg}"),
            None => {
                let dirty = if app.buf.dirty { "" } else { "" };
                format!(" {}{}", app.buf.file_name(), dirty)
            }
        },
    }
}

fn draw_status(frame: &mut Frame, app: &App, area: Rect) {
    let line_no = app.buf.line_of(app.cursor) + 1;
    let col_no = app.buf.visual_col(app.cursor) + 1;

    let left = status_left(app);

    let pending = match app.prefix {
        Some((p, _)) => match p {
            keymap::Prefix::K => "^K ",
            keymap::Prefix::Q => "^Q ",
            keymap::Prefix::O => "^O ",
            keymap::Prefix::P => "^P ",
        },
        None => "",
    };
    let rec = if app.recording { "● REC  " } else { "" };
    let ins = if app.overtype { "Ovr" } else { "Ins" };

    let words_part = if app.show_word_count {
        format!("  {} words", app.doc_stats.words)
    } else {
        String::new()
    };

    // Selection count (R2.2): show when a block is active.
    let sel_part = if let Some((b, e)) = app.blocks.range() {
        let (sw, sc) = crate::stats::count_slice(&app.buf.rope, b, e);
        format!("  sel: {sw}w/{sc}c")
    } else {
        String::new()
    };

    // Goal progress (R2.3).
    let goal_part = if let Some(ref goal) = app.goal {
        let (current, target) = goal.progress(app.doc_stats.words);
        format!("  [{current}/{target}]")
    } else {
        String::new()
    };

    let right = format!(
        "{rec}{pending}Ln {line_no}  Col {col_no}{words_part}{sel_part}{goal_part}  {ins} "
    );

    let width = area.width as usize;
    let left_w = UnicodeWidthStr::width(left.as_str());
    let right_w = UnicodeWidthStr::width(right.as_str());
    let pad = width.saturating_sub(left_w + right_w);
    let content = format!("{left}{}{right}", " ".repeat(pad));

    frame.render_widget(
        Paragraph::new(Line::from(content)).style(app.theme.status),
        area,
    );
}

/// The WordStar delayed menu: once a prefix key has been held pending longer
/// than MENU_DELAY, show what the second key could be.
fn draw_prefix_menu(frame: &mut Frame, app: &App, text_area: Rect) {
    let Some((prefix, since)) = app.prefix else {
        return;
    };
    if since.elapsed() < app.menu_delay {
        return;
    }
    let entries = keymap::menu_entries(prefix);
    if entries.is_empty() {
        return;
    }

    const COLS: usize = 4;
    let rows = entries.len().div_ceil(COLS);
    let col_width = (text_area.width as usize / COLS).max(12);
    let height = (rows + 2) as u16; // borders
    if text_area.height < height {
        return;
    }
    let area = Rect {
        x: text_area.x,
        y: text_area.y + text_area.height - height,
        width: text_area.width,
        height,
    };

    let mut lines: Vec<Line> = Vec::with_capacity(rows);
    for r in 0..rows {
        let mut spans = Vec::new();
        for c in 0..COLS {
            let i = c * rows + r;
            if let Some((key, name)) = entries.get(i) {
                let label = format!(" {}", key.to_ascii_uppercase());
                let desc = format!(" {name}");
                let used = label.len() + desc.width();
                spans.push(Span::styled(label, app.theme.block));
                spans.push(Span::raw(format!(
                    "{desc}{}",
                    " ".repeat(col_width.saturating_sub(used))
                )));
            }
        }
        lines.push(Line::from(spans));
    }

    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new(lines).style(app.theme.status).block(
            Block::new()
                .borders(Borders::ALL)
                .title(prefix.label())
                .style(app.theme.status),
        ),
        area,
    );
}

fn place_cursor(frame: &mut Frame, app: &App, area: Rect) {
    // While an overlay is open, the terminal cursor stays out of the text.
    if matches!(app.mode, Mode::Palette { .. } | Mode::Outline { .. }) {
        return;
    }
    let line = app.buf.line_of(app.cursor);
    if line < app.top_line {
        return;
    }

    let (row, col) = match app.wrap_width() {
        Some(width) => {
            let mut rows = 0usize;
            for l in app.top_line..line {
                rows += wrap_segments(&app.buf.line_text(l), width).len();
                if rows > area.height as usize {
                    return;
                }
            }
            let (seg_idx, vcol) = app.cursor_segment(width);
            (rows + seg_idx, vcol)
        }
        None => {
            let vcol = app.buf.visual_col(app.cursor);
            if vcol < app.left_col {
                return;
            }
            (line - app.top_line, vcol - app.left_col)
        }
    };
    if row >= area.height as usize || col >= area.width as usize {
        return;
    }
    frame.set_cursor_position(Position::new(area.x + col as u16, area.y + row as u16));
}

/// The command palette: a searchable list of every command (Esc / F1).
fn draw_palette(frame: &mut Frame, app: &App, text_area: Rect) {
    let Mode::Palette { query, selected } = &app.mode else {
        return;
    };
    let entries = keymap::filtered_entries(query);

    let width = (text_area.width.saturating_sub(8)).clamp(30, 60);
    let max_list = (text_area.height as usize).saturating_sub(4).clamp(3, 14);
    let height = (entries.len().clamp(1, max_list) + 3) as u16;
    let area = Rect {
        x: text_area.x + (text_area.width - width) / 2,
        y: text_area.y + 1,
        width,
        height: height.min(text_area.height),
    };

    let visible = (area.height as usize).saturating_sub(3);
    let first = selected.saturating_sub(visible.saturating_sub(1));
    let mut lines: Vec<Line> = Vec::new();
    lines.push(Line::from(format!(" > {query}")));
    for (i, (_, name, chord)) in entries.iter().enumerate().skip(first).take(visible) {
        let inner = area.width.saturating_sub(2) as usize;
        let pad = inner.saturating_sub(name.len() + chord.len() + 3);
        let row = format!(" {name}{}{chord}  ", " ".repeat(pad));
        if i == *selected {
            lines.push(Line::from(Span::styled(row, app.theme.block)));
        } else {
            lines.push(Line::from(row));
        }
    }
    if entries.is_empty() {
        lines.push(Line::from(Span::styled(
            " no matching command",
            app.theme.dim,
        )));
    }

    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new(lines).style(app.theme.status).block(
            Block::new()
                .borders(Borders::ALL)
                .title(" Commands ")
                .style(app.theme.status),
        ),
        area,
    );
}

/// The outline: Markdown headings in document order, filtered by title,
/// indented by level. Enter jumps the cursor to the heading.
fn draw_outline(frame: &mut Frame, app: &App, text_area: Rect) {
    let Mode::Outline {
        entries,
        query,
        selected,
    } = &app.mode
    else {
        return;
    };
    let q = query.to_lowercase();
    let matches: Vec<&outline::Entry> = entries
        .iter()
        .filter(|e| q.is_empty() || e.title.to_lowercase().contains(&q))
        .collect();

    let width = (text_area.width.saturating_sub(8)).clamp(30, 60);
    let max_list = (text_area.height as usize).saturating_sub(4).clamp(3, 14);
    let height = (matches.len().clamp(1, max_list) + 3) as u16;
    let area = Rect {
        x: text_area.x + (text_area.width - width) / 2,
        y: text_area.y + 1,
        width,
        height: height.min(text_area.height),
    };

    let visible = (area.height as usize).saturating_sub(3);
    let first = selected.saturating_sub(visible.saturating_sub(1));
    let mut lines: Vec<Line> = Vec::new();
    lines.push(Line::from(format!(" > {query}")));
    for (i, e) in matches.iter().enumerate().skip(first).take(visible) {
        let indent = "  ".repeat(e.level.saturating_sub(1) as usize);
        let label = format!("{indent}{}", e.title);
        let line_no = format!("{}", e.line + 1);
        let inner = area.width.saturating_sub(2) as usize;
        let pad = inner.saturating_sub(label.len() + line_no.len() + 3).max(1);
        let row = format!(" {label}{}{line_no}  ", " ".repeat(pad));
        if i == *selected {
            lines.push(Line::from(Span::styled(row, app.theme.block)));
        } else {
            lines.push(Line::from(row));
        }
    }
    if entries.is_empty() {
        lines.push(Line::from(Span::styled(
            " no headings in this document",
            app.theme.dim,
        )));
    } else if matches.is_empty() {
        lines.push(Line::from(Span::styled(
            " no matching heading",
            app.theme.dim,
        )));
    }

    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new(lines).style(app.theme.status).block(
            Block::new()
                .borders(Borders::ALL)
                .title(" Outline ")
                .style(app.theme.status),
        ),
        area,
    );
}

/// The binder: project document list with title and word count (^PB, task 1.3).
fn draw_binder(frame: &mut Frame, app: &App, text_area: Rect) {
    let Mode::Binder { entries, selected } = &app.mode else {
        return;
    };

    let width = (text_area.width.saturating_sub(8)).clamp(40, 70);
    let max_list = (text_area.height as usize).saturating_sub(4).clamp(3, 14);
    let height = (entries.len().clamp(1, max_list) + 2) as u16;
    let area = Rect {
        x: text_area.x + (text_area.width - width) / 2,
        y: text_area.y + 1,
        width,
        height: height.min(text_area.height),
    };

    let visible = (area.height as usize).saturating_sub(2);
    let first = selected.saturating_sub(visible.saturating_sub(1));
    let mut lines: Vec<Line> = Vec::new();

    for (i, entry) in entries.iter().enumerate().skip(first).take(visible) {
        let wc_str = match entry.word_count {
            Some(wc) => format!("{wc} words"),
            None => String::from(""),
        };
        let missing_marker = if !entry.exists { " [MISSING]" } else { "" };
        let inner = area.width.saturating_sub(2) as usize;
        let pad = inner
            .saturating_sub(entry.title.len() + wc_str.len() + missing_marker.len() + 3)
            .max(1);
        let row = format!(
            " {}{missing_marker}{}{wc_str}  ",
            entry.title,
            " ".repeat(pad)
        );

        if i == *selected {
            lines.push(Line::from(Span::styled(row, app.theme.block)));
        } else if !entry.exists {
            lines.push(Line::from(Span::styled(row, app.theme.dim)));
        } else {
            lines.push(Line::from(row));
        }
    }

    if entries.is_empty() {
        lines.push(Line::from(Span::styled(
            " (no documents in project)",
            app.theme.dim,
        )));
    }

    let title = if let Some(ref project) = app.project {
        format!(" Binder: {} ", project.manifest.name)
    } else {
        String::from(" Binder ")
    };

    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new(lines).style(app.theme.status).block(
            Block::new()
                .borders(Borders::ALL)
                .title(title)
                .style(app.theme.status),
        ),
        area,
    );
}

/// The writing statistics overlay (^OI): daily history and current counts.
fn draw_stats_overlay(frame: &mut Frame, app: &App, area: Rect) {
    let width = 40u16.min(area.width.saturating_sub(4));
    let height = 14u16.min(area.height.saturating_sub(2));
    let x = (area.width.saturating_sub(width)) / 2 + area.x;
    let y = (area.height.saturating_sub(height)) / 2 + area.y;
    let overlay = Rect::new(x, y, width, height);

    frame.render_widget(Clear, overlay);

    let mut lines: Vec<Line<'static>> = Vec::new();
    lines.push(Line::from(format!(
        " Words: {}  Chars: {}",
        app.doc_stats.words, app.doc_stats.chars
    )));
    lines.push(Line::from(""));
    if let Some(ref goal) = app.goal {
        let (current, target) = goal.progress(app.doc_stats.words);
        let kind = match goal.kind {
            crate::stats::GoalKind::Words => "words",
            crate::stats::GoalKind::Minutes => "min",
        };
        let status = if goal.reached { "" } else { "" };
        lines.push(Line::from(format!(
            " Goal: {current}/{target} {kind}{status}"
        )));
    } else {
        lines.push(Line::from(" Goal: none (^OG to set)"));
    }
    lines.push(Line::from(""));
    lines.push(Line::from(" Today's net words:"));
    lines.push(Line::from(format!("   {}", app.daily_history.today())));
    lines.push(Line::from(""));
    lines.push(Line::from(" Recent days:"));
    for (date, words) in app.daily_history.recent(5) {
        lines.push(Line::from(format!("   {date}: {words:+}")));
    }

    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .title(" Writing Stats ");
    let para = Paragraph::new(lines).block(block).style(app.theme.status);
    frame.render_widget(para, overlay);
}

/// Project search results list (^PS, R6.1).
fn draw_project_search(frame: &mut Frame, app: &App, area: Rect) {
    let Mode::ProjectSearch {
        results,
        selected,
        replace_with,
        ..
    } = &app.mode
    else {
        return;
    };

    let width = area.width.saturating_sub(4).min(72);
    let height = area.height.saturating_sub(2).min(20);
    let x = (area.width.saturating_sub(width)) / 2 + area.x;
    let y = (area.height.saturating_sub(height)) / 2 + area.y;
    let overlay = Rect::new(x, y, width, height);

    frame.render_widget(Clear, overlay);

    let visible = (height as usize).saturating_sub(2);
    let first = if *selected >= visible {
        selected - visible + 1
    } else {
        0
    };

    let mut lines: Vec<Line<'static>> = Vec::new();
    for (i, m) in results.iter().enumerate().skip(first).take(visible) {
        let marker = if i == *selected { "" } else { "  " };
        let ctx: String = m
            .context
            .chars()
            .take((width as usize).saturating_sub(20))
            .collect();
        let entry = format!("{marker}{}: L{}{}", m.title, m.line, ctx);
        let style = if i == *selected {
            app.theme.highlight
        } else {
            Style::default()
        };
        lines.push(Line::styled(entry, style));
    }

    let title = if replace_with.is_some() {
        " Project Replace (^R replace · ^A all · Esc cancel) "
    } else {
        " Project Search (Enter open · Esc close) "
    };
    let block = Block::default()
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .title(title);
    let para = Paragraph::new(lines).block(block).style(app.theme.status);
    frame.render_widget(para, overlay);
}

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

    #[test]
    fn recovery_mode_renders_keyboard_confirmation_prompt() {
        let mut app = App::new(None).unwrap();
        app.mode = Mode::ConfirmRecover;

        let prompt = status_left(&app);
        assert!(prompt.contains("Recover unsaved changes"));
        assert!(prompt.contains("y/N"));
        assert!(prompt.contains("Esc decline"));
    }
}