rmut-session 2.2.0

rmut's mail session: an open mailbox and the operations over it, with no screen attached
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
//! What the index can be told to do, and the doing of it.
//!
//! mutt names every operation (`<delete-message>`, `<group-reply>`)
//! and binds keys to the names, which is what makes a keymap a
//! configuration rather than a program. rmut had the names, but they
//! lived in the terminal front end next to the dispatch, so the only
//! way to reach `delete` was to press a key on a pty: 300 lines of
//! mailbox logic that no test could name.
//!
//! The names and the dispatch live here now. A front end resolves
//! whatever it has (a keystroke, a menu item, `:exec`) to a
//! [`Function`], hands it to [`Session::run_function`], and reads the
//! [`Outcome`]. Most functions finish inside the session; some stop
//! to [`Ask`]; the rest come back as a [`FrontOp`], which is the
//! honest list of what a session cannot do for itself because it owns
//! neither a screen nor an editor.

use crate::ask::{Ask, PatternOp};
use crate::{ComposeKind, Session, ThreadOp};

/// One thing the index can be told to do, under the name mutt gives
/// it. A front end binds keys or menu items to these; `bind` and
/// `:exec` name them straight out.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Function {
    Quit,
    Abort,
    Down,
    Up,
    PageDown,
    PageUp,
    First,
    Last,
    View,
    Delete,
    Undelete,
    Flag,
    ToggleNew,
    Sync,
    Compose,
    Reply,
    GroupReply,
    ListReply,
    Forward,
    Sort,
    Limit,
    Search,
    SearchReverse,
    SearchNext,
    NextNew,
    PrevNew,
    ChangeMailbox,
    ChangeMailboxReadOnly,
    Folders,
    Attachments,
    FoldThread,
    FoldAll,
    Print,
    Tag,
    TagPrefix,
    DeleteThread,
    UndeleteThread,
    TagThread,
    DeleteSubthread,
    UndeleteSubthread,
    NextThread,
    PrevThread,
    BreakThread,
    LinkThreads,
    ReadThread,
    ReadSubthread,
    TagSubthread,
    ParentMessage,
    RootMessage,
    EditLabel,
    ShowVersion,
    ShowLimit,
    ToggleWrite,
    DisplayAddress,
    PageTop,
    PageMiddle,
    PageBottom,
    Undo,
    DeletePattern,
    UndeletePattern,
    TagPattern,
    UntagPattern,
    FetchMail,
    Save,
    Copy,
    DecodeSave,
    DecodeCopy,
    Pipe,
    Bounce,
    Resend,
    Edit,
    SidebarToggle,
    SidebarNext,
    SidebarPrev,
    SidebarOpen,
    CreateAlias,
    Query,
    Notmuch,
    EnterCommand,
    Shell,
    Redraw,
    Suspend,
    Help,
    /// mutt's next-unread-mailbox: open the next configured mailbox
    /// holding new mail.
    NextUnreadMailbox,
    /// mutt's purge-message: delete, bypassing $trash.
    PurgeMessage,
    /// mutt's mark-message: a hotkey that jumps back here.
    MarkMessage,
    /// mutt's error-history: the recent complaints on a screen.
    ErrorHistory,
    /// mutt's what-key: say what the next keys are.
    WhatKey,
    /// mutt's list-action: RFC 2369 List-* actions of the message.
    ListAction,
}

impl Function {
    pub fn name(self) -> &'static str {
        use Function::*;
        match self {
            Quit => "quit",
            Abort => "abort",
            Down => "down",
            Up => "up",
            PageDown => "page-down",
            PageUp => "page-up",
            First => "first",
            Last => "last",
            View => "view",
            Delete => "delete",
            Undelete => "undelete",
            Flag => "flag",
            ToggleNew => "toggle-new",
            Sync => "sync",
            Compose => "compose",
            Reply => "reply",
            GroupReply => "group-reply",
            ListReply => "list-reply",
            Forward => "forward",
            Sort => "sort",
            Limit => "limit",
            Search => "search",
            SearchReverse => "search-reverse",
            SearchNext => "search-next",
            NextNew => "next-new",
            PrevNew => "previous-new",
            ChangeMailbox => "change-mailbox",
            ChangeMailboxReadOnly => "change-mailbox-readonly",
            Folders => "folders",
            Attachments => "attachments",
            FoldThread => "fold-thread",
            FoldAll => "fold-all",
            Print => "print",
            Tag => "tag",
            TagPrefix => "tag-prefix",
            DeleteThread => "delete-thread",
            UndeleteThread => "undelete-thread",
            TagThread => "tag-thread",
            DeleteSubthread => "delete-subthread",
            UndeleteSubthread => "undelete-subthread",
            NextThread => "next-thread",
            BreakThread => "break-thread",
            LinkThreads => "link-threads",
            ReadThread => "read-thread",
            ReadSubthread => "read-subthread",
            TagSubthread => "tag-subthread",
            ParentMessage => "parent-message",
            RootMessage => "root-message",
            EditLabel => "edit-label",
            ShowVersion => "show-version",
            ShowLimit => "show-limit",
            ToggleWrite => "toggle-write",
            DisplayAddress => "display-address",
            PageTop => "top-page",
            PageMiddle => "middle-page",
            PageBottom => "bottom-page",
            PrevThread => "previous-thread",
            Undo => "undo",
            DeletePattern => "delete-pattern",
            UndeletePattern => "undelete-pattern",
            TagPattern => "tag-pattern",
            UntagPattern => "untag-pattern",
            FetchMail => "fetch-mail",
            Save => "save",
            Copy => "copy",
            DecodeSave => "decode-save",
            DecodeCopy => "decode-copy",
            Pipe => "pipe",
            Bounce => "bounce",
            Resend => "resend",
            Edit => "edit",
            SidebarToggle => "sidebar-toggle",
            SidebarNext => "sidebar-next",
            SidebarPrev => "sidebar-prev",
            SidebarOpen => "sidebar-open",
            CreateAlias => "create-alias",
            Query => "query",
            Notmuch => "notmuch",
            EnterCommand => "enter-command",
            Shell => "shell-escape",
            Redraw => "refresh",
            Suspend => "suspend",
            Help => "help",
            NextUnreadMailbox => "next-unread-mailbox",
            PurgeMessage => "purge-message",
            MarkMessage => "mark-message",
            ErrorHistory => "error-history",
            WhatKey => "what-key",
            ListAction => "list-action",
        }
    }

    pub fn describe(self) -> &'static str {
        use Function::*;
        match self {
            Quit => "quit (writes changes; asks before purging deletions)",
            Abort => "quit without saving changes",
            Down => "next message",
            Up => "previous message",
            PageDown => "page down",
            PageUp => "page up",
            First => "first message",
            Last => "last message",
            View => "view message",
            Delete => "mark for deletion",
            Undelete => "unmark deletion",
            Flag => "toggle flagged mark",
            ToggleNew => "toggle read/unread",
            Sync => "write changes to the maildir",
            Compose => "compose a new message",
            Reply => "reply to sender",
            GroupReply => "reply to all",
            ListReply => "reply to the mailing list only",
            Forward => "forward message",
            Sort => "choose sort order",
            Limit => "limit index by pattern",
            Search => "search messages by pattern (the pager / searches its text)",
            SearchReverse => "search backwards; n then repeats backwards too",
            SearchNext => "repeat last search, the way it was going",
            NextNew => "jump to the next new or unread message",
            PrevNew => "jump to the previous new or unread message",
            ChangeMailbox => "open a mailbox by path",
            ChangeMailboxReadOnly => "open a mailbox read-only (Alt+c)",
            Folders => "browse nearby mailboxes",
            Attachments => "list message parts",
            FoldThread => "fold/unfold current thread",
            FoldAll => "fold/unfold all threads",
            Print => "pipe message to the print command",
            Tag => "toggle the tag on this message",
            TagPrefix => "apply the next function to tagged messages",
            DeleteThread => "mark the whole thread for deletion",
            UndeleteThread => "unmark the whole thread",
            TagThread => "tag/untag the whole thread",
            DeleteSubthread => "mark this message and its replies for deletion",
            UndeleteSubthread => "unmark this message and its replies",
            NextThread => "jump to the next thread",
            BreakThread => "break the thread in two at this message",
            LinkThreads => "link the tagged messages under this one",
            ReadThread => "mark the whole thread read",
            ReadSubthread => "mark this message and its replies read",
            TagSubthread => "tag this message and its replies",
            ParentMessage => "jump to the parent message",
            RootMessage => "jump to the thread's root message",
            EditLabel => "add, change or clear the X-Label",
            ShowVersion => "show the rmut version",
            ShowLimit => "show the active limit pattern",
            ToggleWrite => "toggle the mailbox's read-only state",
            DisplayAddress => "show the sender's full address",
            PageTop => "move to the top of the page",
            PageMiddle => "move to the middle of the page",
            PageBottom => "move to the bottom of the page",
            PrevThread => "jump to the previous thread",
            Undo => "cancel a held send, else undo the last delete/flag/tag/save",
            DeletePattern => "delete every message matching a pattern",
            UndeletePattern => "undelete every message matching a pattern",
            TagPattern => "tag every message matching a pattern",
            UntagPattern => "untag every message matching a pattern",
            FetchMail => "check for new mail now",
            Save => "save (copy + mark deleted) to a mailbox",
            DecodeSave => "decode-save: the decoded message, original deleted",
            DecodeCopy => "decode-copy: the decoded message",
            Copy => "copy to a mailbox (original stays)",
            Pipe => "pipe raw message to a shell command",
            Bounce => "bounce (resend) message to new recipients",
            Resend => "edit the message as a new draft",
            Edit => "edit the raw message and replace it",
            SidebarToggle => "show/hide the mailbox sidebar",
            SidebarNext => "highlight the next sidebar mailbox",
            SidebarPrev => "highlight the previous sidebar mailbox",
            SidebarOpen => "open the highlighted sidebar mailbox",
            CreateAlias => "add the sender to the alias file",
            Query => "look up addresses with query_command",
            Notmuch => "notmuch search into a read-only view",
            EnterCommand => "run a config command (set/bind/macro/color/...)",
            Shell => "run a shell command",
            Redraw => "repaint the screen",
            Suspend => "suspend rmut (fg brings it back)",
            Help => "this help",
            NextUnreadMailbox => "open the next mailbox holding new mail",
            PurgeMessage => "mark for deletion, bypassing the trash",
            MarkMessage => "bind a key that jumps back to this message",
            ErrorHistory => "show the recent errors",
            WhatKey => "say what a key is (Ctrl+G ends it)",
            ListAction => "act on the message's List-* headers (subscribe, help, ...)",
        }
    }

    /// Every function, in the order the help screen and a menu bar
    /// want them.
    pub fn all() -> &'static [Function] {
        use Function::*;
        &[
            Quit,
            Abort,
            Down,
            Up,
            PageDown,
            PageUp,
            First,
            Last,
            View,
            Delete,
            Undelete,
            Flag,
            ToggleNew,
            Sync,
            Compose,
            Reply,
            GroupReply,
            ListReply,
            Forward,
            Sort,
            Limit,
            Search,
            SearchReverse,
            SearchNext,
            NextNew,
            PrevNew,
            ChangeMailbox,
            ChangeMailboxReadOnly,
            Folders,
            Attachments,
            FoldThread,
            FoldAll,
            Print,
            Tag,
            TagPrefix,
            DeleteThread,
            UndeleteThread,
            TagThread,
            DeleteSubthread,
            UndeleteSubthread,
            NextThread,
            PrevThread,
            BreakThread,
            LinkThreads,
            ReadThread,
            ReadSubthread,
            TagSubthread,
            ParentMessage,
            RootMessage,
            EditLabel,
            ShowVersion,
            ShowLimit,
            ToggleWrite,
            DisplayAddress,
            PageTop,
            PageMiddle,
            PageBottom,
            Undo,
            DeletePattern,
            UndeletePattern,
            TagPattern,
            UntagPattern,
            FetchMail,
            Save,
            Copy,
            DecodeSave,
            DecodeCopy,
            Pipe,
            Bounce,
            Resend,
            Edit,
            SidebarToggle,
            SidebarNext,
            SidebarPrev,
            SidebarOpen,
            CreateAlias,
            Query,
            Notmuch,
            EnterCommand,
            Shell,
            Redraw,
            Suspend,
            Help,
            NextUnreadMailbox,
            PurgeMessage,
            MarkMessage,
            ErrorHistory,
            WhatKey,
            ListAction,
        ]
    }

    pub fn from_name(name: &str) -> Option<Function> {
        Function::all().iter().copied().find(|a| a.name() == name)
    }

    /// Which functions `;` (tag-prefix) can hand the tagged set to.
    /// The rest say so rather than quietly acting on one message:
    /// resend and edit open a draft or an editor, of which rmut has
    /// one at a time.
    pub fn takes_tagged(self) -> bool {
        use Function::*;
        matches!(
            self,
            Delete
                | Undelete
                | Flag
                | ToggleNew
                | Tag
                | Save
                | Copy
                | DecodeSave
                | DecodeCopy
                | Pipe
                | Print
                | Bounce
                | EditLabel
        )
    }
}

/// What came of running a function.
pub enum Outcome {
    /// The session did it. Whatever there was to say went out as a
    /// [`Notice`](rmut_core::notice::Notice); whatever the front end
    /// must do next is waiting in [`Session::take_request`].
    Done,
    /// It cannot finish without an answer. The front end collects one
    /// however it likes and hands it back to [`Session::answer`].
    Ask(Ask),
    /// Only the front end can do this one.
    Front(FrontOp),
}

impl From<Option<Ask>> for Outcome {
    /// The `ask_*` helpers return `None` when the question does not
    /// arise (nothing to act on, a read-only mailbox), having already
    /// said why.
    fn from(ask: Option<Ask>) -> Outcome {
        match ask {
            Some(ask) => Outcome::Ask(ask),
            None => Outcome::Done,
        }
    }
}

/// The functions a session cannot carry out, because they are about
/// the display rather than the mail: a menu to open, a screen to
/// repaint, an editor to hand the terminal to. The session has done
/// whatever checking it can (a mailbox with unsaved changes will not
/// be left, an unconfigured query will not be prompted for) before
/// handing one of these back, so a front end can act on it directly.
pub enum FrontOp {
    /// mutt's `x`: leave without writing anything back.
    Exit,
    /// Open this mailbox spec (mutt's next-unread-mailbox found it).
    OpenMailbox(String),
    /// Put these lines on a screen of their own: the error history,
    /// oldest first.
    ErrorHistory(Vec<String>),
    /// Read keys and say what they are until Ctrl+G.
    WhatKey,
    /// Read the selected message, however messages are read.
    OpenSelected,
    /// Start a draft. The session asks for the recipients once the
    /// front end has settled `$recall`.
    Compose(ComposeKind),
    /// mutt's resend-message: the selected message as a new draft.
    Resend,
    /// mutt's `e`: the selected message's own bytes, in an editor.
    RawEdit,
    /// The attachment menu for the selected message.
    Attachments,
    /// The folder browser.
    Folders,
    /// `$query_command`, which is configured: ask for the terms.
    Query,
    /// notmuch, which is not disabled: ask for the query.
    Notmuch,
    /// Somewhere else to open; the open mailbox is ready to be left.
    ChangeMailbox { read_only: bool },
    /// The `:` command line.
    CommandPrompt,
    /// mutt's `;`: the next function applies to the tagged set. There
    /// are tagged messages, or this would have been a complaint.
    TagPrefix,
    /// The mailbox pane.
    Sidebar(SidebarOp),
    /// Move the cursor by where it sits on screen, which only the
    /// front end knows: mutt's H, M and L.
    PageMove(PageSpot),
    /// mutt's help screen.
    Help,
    /// mutt's Ctrl+L: repaint.
    Redraw,
}

/// What to do with the mailbox pane.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SidebarOp {
    Toggle,
    Next,
    Prev,
    Open,
}

/// Where on the visible page the cursor should land.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum PageSpot {
    Top,
    Middle,
    Bottom,
}

impl Session {
    /// Do one thing, whatever asked for it: a key, a macro replay, a
    /// `:exec`, a menu item.
    ///
    /// `tagged` is mutt's tag-prefix, and is only ever true for a
    /// function that [takes it](Function::takes_tagged); `page` is how
    /// many messages the front end is showing at once, which is all
    /// the geometry the session needs to know.
    pub fn run_function(&mut self, function: Function, tagged: bool, page: usize) -> Outcome {
        use Function::*;
        match function {
            // ---- motion ----
            Down => self.select(self.sel.saturating_add(1)),
            Up => self.select(self.sel.saturating_sub(1)),
            PageDown => self.select(self.sel.saturating_add(page)),
            PageUp => self.select(self.sel.saturating_sub(page)),
            First => self.select(0),
            Last => self.select(usize::MAX),
            NextNew => self.jump_new(true),
            PrevNew => self.jump_new(false),
            NextThread => self.jump_thread(true),
            PrevThread => self.jump_thread(false),
            ParentMessage => self.jump_parent(false),
            RootMessage => self.jump_parent(true),
            SearchNext => self.search_next(),

            // ---- marks ----
            Tag => {
                if let Some(&i) = self.visible.get(self.sel) {
                    self.push_undo("tag", &[i]);
                    self.msgs[i].env.tagged = !self.msgs[i].env.tagged;
                    self.select(self.sel.saturating_add(1));
                }
            }
            Delete => {
                let rules = self.delete_rules();
                self.mark_selected(tagged, "delete", move |m| {
                    rules.mark(m);
                })
            }
            Undelete => self.mark_selected(tagged, "undelete", |m| {
                m.env.file.flags.deleted = false;
                m.purge = false;
            }),
            PurgeMessage => {
                let rules = self.delete_rules();
                self.mark_selected(tagged, "purge", move |m| {
                    if rules.mark(m) {
                        m.purge = true;
                    }
                })
            }
            NextUnreadMailbox => match self.next_unread_mailbox() {
                Some(spec) => return Outcome::Front(FrontOp::OpenMailbox(spec)),
                None => self.error("No mailboxes have new mail"),
            },
            MarkMessage => return self.ask_mark_message().into(),
            ErrorHistory => {
                if self.config.ui.error_history == 0 {
                    self.error("Error History is disabled.");
                } else {
                    return Outcome::Front(FrontOp::ErrorHistory(self.error_history()));
                }
            }
            WhatKey => return Outcome::Front(FrontOp::WhatKey),
            ListAction => return self.ask_list_action().into(),
            Flag => self.mark_selected(tagged, "flag", |m| {
                m.env.file.flags.flagged = !m.env.file.flags.flagged
            }),
            ToggleNew => self.mark_selected(tagged, "toggle read", |m| {
                m.env.file.flags.seen = !m.env.file.flags.seen;
                m.env.file.is_new = false;
            }),
            Undo => {
                // A message still inside its $undo_send window is the
                // most recent thing done, so it is what undo takes
                // back first.
                if !self.cancel_send() {
                    self.undo_last();
                }
            }

            // ---- threads ----
            DeleteThread => self.thread_mark(false, ThreadOp::Delete),
            UndeleteThread => self.thread_mark(false, ThreadOp::Undelete),
            TagThread => self.thread_mark(false, ThreadOp::Tag),
            ReadThread => self.thread_mark(false, ThreadOp::Read),
            DeleteSubthread => self.thread_mark(true, ThreadOp::Delete),
            UndeleteSubthread => self.thread_mark(true, ThreadOp::Undelete),
            TagSubthread => self.thread_mark(true, ThreadOp::Tag),
            ReadSubthread => self.thread_mark(true, ThreadOp::Read),
            BreakThread => self.break_thread(),
            LinkThreads => self.link_threads(),
            FoldThread => self.toggle_collapse(false),
            FoldAll => self.toggle_collapse(true),

            // ---- the mailbox ----
            Sync => {
                if self.deleted_count() > 0 {
                    return self.ask_purge(false).into();
                }
                self.sync(true);
            }
            Quit => return self.leave().into(),
            FetchMail => {
                self.check_new_mail();
                if self.notice().is_none() {
                    self.note("checked for new mail");
                }
            }
            ToggleWrite => self.toggle_write(),
            ShowLimit => self.show_limit(),
            ShowVersion => self.note(concat!("rmut ", env!("CARGO_PKG_VERSION"))),
            DisplayAddress => {
                let from = self
                    .visible
                    .get(self.sel)
                    .map(|&i| self.msgs[i].env.from_full.clone());
                match from {
                    Some(from) if !from.trim().is_empty() => self.note(from),
                    _ => self.note("(no From address)"),
                }
            }

            // ---- questions ----
            Limit => return Outcome::Ask(self.ask_limit()),
            Search => return Outcome::Ask(self.ask_search(false)),
            SearchReverse => return Outcome::Ask(self.ask_search(true)),
            Sort => return Outcome::Ask(self.ask_sort()),
            Shell => return Outcome::Ask(self.ask_shell()),
            DeletePattern => return self.ask_pattern(PatternOp::Delete).into(),
            UndeletePattern => return self.ask_pattern(PatternOp::Undelete).into(),
            TagPattern => return self.ask_pattern(PatternOp::Tag).into(),
            UntagPattern => return self.ask_pattern(PatternOp::Untag).into(),
            Save => return self.ask_copy(true, tagged).into(),
            Copy => return self.ask_copy(false, tagged).into(),
            DecodeSave => return self.ask_copy_decode(true, tagged, true).into(),
            DecodeCopy => return self.ask_copy_decode(false, tagged, true).into(),
            Pipe => return self.ask_pipe(tagged).into(),
            Bounce => return self.ask_bounce(tagged).into(),
            Print => return self.ask_print(tagged).into(),
            EditLabel => return self.ask_edit_label(tagged).into(),
            CreateAlias => return self.ask_alias().into(),
            ListReply => return self.start_list_reply().into(),

            // ---- off to the front end ----
            Suspend => self.request_suspend(),
            Abort => return Outcome::Front(FrontOp::Exit),
            View => return Outcome::Front(FrontOp::OpenSelected),
            Compose => return Outcome::Front(FrontOp::Compose(ComposeKind::New)),
            Reply => return Outcome::Front(FrontOp::Compose(ComposeKind::Reply)),
            GroupReply => return Outcome::Front(FrontOp::Compose(ComposeKind::GroupReply)),
            Forward => return Outcome::Front(FrontOp::Compose(ComposeKind::Forward)),
            Resend => return Outcome::Front(FrontOp::Resend),
            Edit => return Outcome::Front(FrontOp::RawEdit),
            Attachments => return Outcome::Front(FrontOp::Attachments),
            Folders => return Outcome::Front(FrontOp::Folders),
            EnterCommand => return Outcome::Front(FrontOp::CommandPrompt),
            Help => return Outcome::Front(FrontOp::Help),
            Redraw => return Outcome::Front(FrontOp::Redraw),
            PageTop => return Outcome::Front(FrontOp::PageMove(PageSpot::Top)),
            PageMiddle => return Outcome::Front(FrontOp::PageMove(PageSpot::Middle)),
            PageBottom => return Outcome::Front(FrontOp::PageMove(PageSpot::Bottom)),
            SidebarToggle => return Outcome::Front(FrontOp::Sidebar(SidebarOp::Toggle)),
            SidebarNext => return Outcome::Front(FrontOp::Sidebar(SidebarOp::Next)),
            SidebarPrev => return Outcome::Front(FrontOp::Sidebar(SidebarOp::Prev)),
            SidebarOpen => return Outcome::Front(FrontOp::Sidebar(SidebarOp::Open)),
            TagPrefix => {
                if !self.msgs.iter().any(|m| m.env.tagged) {
                    self.note("no tagged messages");
                    return Outcome::Done;
                }
                // mutt writes "Tag-" on its message line and waits;
                // rmut's one bottom line appends it to the status bar,
                // which then stays readable.
                self.note("Tag-");
                return Outcome::Front(FrontOp::TagPrefix);
            }
            Query => {
                if self.config.mail.query_command.is_none() {
                    self.error("no query_command configured");
                    return Outcome::Done;
                }
                return Outcome::Front(FrontOp::Query);
            }
            Notmuch => {
                if self.config.mail.notmuch == Some(false) {
                    self.error("notmuch is disabled in the config");
                    return Outcome::Done;
                }
                return Outcome::Front(FrontOp::Notmuch);
            }
            ChangeMailbox | ChangeMailboxReadOnly => {
                if !self.ready_to_leave() {
                    return Outcome::Done;
                }
                return Outcome::Front(FrontOp::ChangeMailbox {
                    read_only: function == ChangeMailboxReadOnly,
                });
            }
        }
        Outcome::Done
    }

    /// One flag change, on the tagged set or on the message under the
    /// cursor. mutt's $resolve (on by default) advances afterwards.
    fn mark_selected(&mut self, tagged: bool, what: &'static str, f: impl Fn(&mut crate::Msg)) {
        if self.deny_readonly() {
            return;
        }
        if tagged {
            self.each_tagged(what, f);
        } else if let Some(&i) = self.visible.get(self.sel) {
            self.push_undo(what, &[i]);
            f(&mut self.msgs[i]);
            self.msgs[i].dirty = true;
            self.select(self.sel.saturating_add(1));
        }
    }
}