Rocco Rutte:
[apps/madmutt.git] / curs_main.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include "mutt.h"
15 #include "mutt_curses.h"
16 #include "mx.h"
17 #include "mutt_menu.h"
18 #include "mailbox.h"
19 #include "mapping.h"
20 #include "sort.h"
21 #include "buffy.h"
22 #include "mx.h"
23 #include "sidebar.h"
24
25 #ifdef USE_POP
26 #include "pop.h"
27 #endif
28
29 #ifdef USE_IMAP
30 #include "imap_private.h"
31 #endif
32
33 #include "mutt_crypt.h"
34
35 #ifdef USE_NNTP
36 #include "nntp.h"
37 #endif
38
39
40 #include <ctype.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <sys/wait.h>
44 #include <string.h>
45 #include <sys/stat.h>
46 #include <errno.h>
47
48 static const char *No_mailbox_is_open = N_("No mailbox is open.");
49 static const char *There_are_no_messages = N_("There are no messages.");
50 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
51 static const char *Function_not_permitted_in_attach_message_mode =
52 N_("Function not permitted in attach-message mode.");
53 static const char *No_visible = N_("No visible messages.");
54
55 #define CHECK_MSGCOUNT if (!Context) \
56         { \
57                   mutt_flushinp (); \
58                 mutt_error(_(No_mailbox_is_open)); \
59                 break; \
60         } \
61         else if (!Context->msgcount) \
62         { \
63                   mutt_flushinp (); \
64                 mutt_error(_(There_are_no_messages)); \
65                 break; \
66         }
67
68 #define CHECK_VISIBLE if (Context && menu->current >= Context->vcount) \
69           {\
70                   mutt_flushinp (); \
71                   mutt_error(_(No_visible)); \
72                   break; \
73         }
74
75
76 #define CHECK_READONLY if (Context->readonly) \
77                         { \
78                                   mutt_flushinp (); \
79                                 mutt_error(_(Mailbox_is_read_only)); \
80                                 break; \
81                         }
82
83 #ifdef USE_IMAP
84 /* the error message returned here could be better. */
85 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
86                 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
87                 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
88                         mutt_flushinp(); \
89                         mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
90                         break; \
91                 }
92 #endif
93
94 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
95                      {\
96                         mutt_flushinp (); \
97                         mutt_error(_(Function_not_permitted_in_attach_message_mode)); \
98                         break; \
99                      }
100
101 #define CURHDR Context->hdrs[Context->v2r[menu->current]]
102 #define OLDHDR Context->hdrs[Context->v2r[menu->oldcurrent]]
103 #define UNREAD(h) mutt_thread_contains_unread (Context, h)
104
105 extern const char *ReleaseDate;
106 extern size_t UngetCount;
107
108 static void set_xterm_title_bar (char *title)
109 {
110   fputs ("\033]2;", stdout);
111   fputs (title, stdout);
112   fputs ("\007", stdout);
113   fflush (stdout);
114 }
115
116 static void set_xterm_icon_name (char *name)
117 {
118   fputs ("\033]1;", stdout);
119   fputs (name, stdout);
120   fputs ("\007", stdout);
121   fflush (stdout);
122 }
123
124 void index_make_entry (char *s, size_t l, MUTTMENU * menu, int num)
125 {
126   format_flag flag =
127     M_FORMAT_MAKEPRINT | M_FORMAT_ARROWCURSOR | M_FORMAT_INDEX;
128   int edgemsgno, reverse = Sort & SORT_REVERSE;
129   HEADER *h = Context->hdrs[Context->v2r[num]];
130   THREAD *tmp;
131
132   if ((Sort & SORT_MASK) == SORT_THREADS && h->tree) {
133     flag |= M_FORMAT_TREE;      /* display the thread tree */
134     if (h->display_subject)
135       flag |= M_FORMAT_FORCESUBJ;
136     else {
137       if (reverse) {
138         if (menu->top + menu->pagelen > menu->max)
139           edgemsgno = Context->v2r[menu->max - 1];
140         else
141           edgemsgno = Context->v2r[menu->top + menu->pagelen - 1];
142       }
143       else
144         edgemsgno = Context->v2r[menu->top];
145
146       for (tmp = h->thread->parent; tmp; tmp = tmp->parent) {
147         if (!tmp->message)
148           continue;
149
150         /* if no ancestor is visible on current screen, provisionally force
151          * subject... */
152         if (reverse ? tmp->message->msgno > edgemsgno : tmp->message->msgno <
153             edgemsgno) {
154           flag |= M_FORMAT_FORCESUBJ;
155           break;
156         }
157         else if (tmp->message->virtual >= 0)
158           break;
159       }
160       if (flag & M_FORMAT_FORCESUBJ) {
161         for (tmp = h->thread->prev; tmp; tmp = tmp->prev) {
162           if (!tmp->message)
163             continue;
164
165           /* ...but if a previous sibling is available, don't force it */
166           if (reverse ? tmp->message->msgno >
167               edgemsgno : tmp->message->msgno < edgemsgno)
168             break;
169           else if (tmp->message->virtual >= 0) {
170             flag &= ~M_FORMAT_FORCESUBJ;
171             break;
172           }
173         }
174       }
175     }
176   }
177
178   _mutt_make_string (s, l, NONULL (HdrFmt), Context, h, flag);
179 }
180
181 int index_color (int index_no)
182 {
183   HEADER *h = Context->hdrs[Context->v2r[index_no]];
184
185   if (h && h->pair)
186     return h->pair;
187
188   mutt_set_header_color (Context, h);
189   return h->pair;
190 }
191
192 static int ci_next_undeleted (int msgno)
193 {
194   int i;
195
196   for (i = msgno + 1; i < Context->vcount; i++)
197     if (!Context->hdrs[Context->v2r[i]]->deleted)
198       return (i);
199   return (-1);
200 }
201
202 static int ci_previous_undeleted (int msgno)
203 {
204   int i;
205
206   for (i = msgno - 1; i >= 0; i--)
207     if (!Context->hdrs[Context->v2r[i]]->deleted)
208       return (i);
209   return (-1);
210 }
211
212 /* Return the index of the first new message, or failing that, the first
213  * unread message.
214  */
215 static int ci_first_message (void)
216 {
217   int old = -1, i;
218
219   if (Context && Context->msgcount) {
220     for (i = 0; i < Context->vcount; i++) {
221       if (!Context->hdrs[Context->v2r[i]]->read &&
222           !Context->hdrs[Context->v2r[i]]->deleted) {
223         if (!Context->hdrs[Context->v2r[i]]->old)
224           return (i);
225         else if (old == -1)
226           old = i;
227       }
228     }
229     if (old != -1)
230       return (old);
231
232     /* If Sort is reverse and not threaded, the latest message is first.
233      * If Sort is threaded, the latest message is first iff exactly one
234      * of Sort and SortAux are reverse.
235      */
236     if (((Sort & SORT_REVERSE) && (Sort & SORT_MASK) != SORT_THREADS) ||
237         ((Sort & SORT_MASK) == SORT_THREADS &&
238          ((Sort ^ SortAux) & SORT_REVERSE)))
239       return 0;
240     else
241       return (Context->vcount ? Context->vcount - 1 : 0);
242   }
243   return 0;
244 }
245
246 /* This should be in mx.c, but it only gets used here. */
247 static int mx_toggle_write (CONTEXT * ctx)
248 {
249   if (!ctx)
250     return -1;
251
252   if (ctx->readonly) {
253     mutt_error (_("Cannot toggle write on a readonly mailbox!"));
254
255     return -1;
256   }
257
258   if (ctx->dontwrite) {
259     ctx->dontwrite = 0;
260     mutt_message (_("Changes to folder will be written on folder exit."));
261   }
262   else {
263     ctx->dontwrite = 1;
264     mutt_message (_("Changes to folder will not be written."));
265   }
266
267   return 0;
268 }
269
270 static void update_index (MUTTMENU * menu, CONTEXT * ctx, int check,
271                           int oldcount, int index_hint)
272 {
273   /* store pointers to the newly added messages */
274   HEADER **save_new = NULL;
275   int j;
276
277   /* take note of the current message */
278   if (oldcount) {
279     if (menu->current < Context->vcount)
280       menu->oldcurrent = index_hint;
281     else
282       oldcount = 0;             /* invalid message number! */
283   }
284
285   /* We are in a limited view. Check if the new message(s) satisfy
286    * the limit criteria. If they do, set their virtual msgno so that
287    * they will be visible in the limited view */
288   if (Context->pattern) {
289 #define THIS_BODY Context->hdrs[j]->content
290     if (oldcount || check == M_REOPENED) {
291       for (j = (check == M_REOPENED) ? 0 : oldcount; j < Context->msgcount;
292            j++) {
293         if (mutt_pattern_exec
294             (Context->limit_pattern, M_MATCH_FULL_ADDRESS, Context,
295              Context->hdrs[j])) {
296           Context->hdrs[j]->virtual = Context->vcount;
297           Context->v2r[Context->vcount] = j;
298           Context->hdrs[j]->limited = 1;
299           Context->vcount++;
300           Context->vsize +=
301             THIS_BODY->length + THIS_BODY->offset - THIS_BODY->hdr_offset;
302         }
303       }
304     }
305 #undef THIS_BODY
306   }
307
308   /* save the list of new messages */
309   if (oldcount && check != M_REOPENED && ((Sort & SORT_MASK) == SORT_THREADS)) {
310     save_new =
311       (HEADER **) safe_malloc (sizeof (HEADER *) *
312                                (Context->msgcount - oldcount));
313     for (j = oldcount; j < Context->msgcount; j++)
314       save_new[j - oldcount] = Context->hdrs[j];
315   }
316
317   /* if the mailbox was reopened, need to rethread from scratch */
318   mutt_sort_headers (Context, (check == M_REOPENED));
319
320   /* uncollapse threads with new mail */
321   if ((Sort & SORT_MASK) == SORT_THREADS) {
322     if (check == M_REOPENED) {
323       THREAD *h, *j;
324
325       Context->collapsed = 0;
326
327       for (h = Context->tree; h; h = h->next) {
328         for (j = h; !j->message; j = j->child);
329         mutt_uncollapse_thread (Context, j->message);
330       }
331       mutt_set_virtual (Context);
332     }
333     else if (oldcount) {
334       for (j = 0; j < Context->msgcount - oldcount; j++) {
335         int k;
336
337         for (k = 0; k < Context->msgcount; k++) {
338           HEADER *h = Context->hdrs[k];
339
340           if (h == save_new[j] && (!Context->pattern || h->limited))
341             mutt_uncollapse_thread (Context, h);
342         }
343       }
344       FREE (&save_new);
345       mutt_set_virtual (Context);
346     }
347   }
348
349   menu->current = -1;
350   if (oldcount) {
351     /* restore the current message to the message it was pointing to */
352     for (j = 0; j < Context->vcount; j++) {
353       if (Context->hdrs[Context->v2r[j]]->index == menu->oldcurrent) {
354         menu->current = j;
355         break;
356       }
357     }
358   }
359
360   if (menu->current < 0)
361     menu->current = ci_first_message ();
362
363 }
364
365 static void resort_index (MUTTMENU * menu)
366 {
367   int i;
368   HEADER *current = CURHDR;
369
370   menu->current = -1;
371   mutt_sort_headers (Context, 0);
372   /* Restore the current message */
373
374   for (i = 0; i < Context->vcount; i++) {
375     if (Context->hdrs[Context->v2r[i]] == current) {
376       menu->current = i;
377       break;
378     }
379   }
380
381   if ((Sort & SORT_MASK) == SORT_THREADS && menu->current < 0)
382     menu->current = mutt_parent_message (Context, current);
383
384   if (menu->current < 0)
385     menu->current = ci_first_message ();
386
387   menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
388 }
389
390 struct mapping_t IndexHelp[] = {
391   {N_("Quit"), OP_QUIT},
392   {N_("Del"), OP_DELETE},
393   {N_("Undel"), OP_UNDELETE},
394   {N_("Save"), OP_SAVE},
395   {N_("Mail"), OP_MAIL},
396   {N_("Reply"), OP_REPLY},
397   {N_("Group"), OP_GROUP_REPLY},
398   {N_("Help"), OP_HELP},
399   {NULL}
400 };
401
402 #ifdef USE_NNTP
403 struct mapping_t IndexNewsHelp[] = {
404   {N_("Quit"), OP_QUIT},
405   {N_("Del"), OP_DELETE},
406   {N_("Undel"), OP_UNDELETE},
407   {N_("Save"), OP_SAVE},
408   {N_("Post"), OP_POST},
409   {N_("Followup"), OP_FOLLOWUP},
410   {N_("Catchup"), OP_CATCHUP},
411   {N_("Help"), OP_HELP},
412   {NULL}
413 };
414 #endif
415
416 /* This function handles the message index window as well as commands returned
417  * from the pager (MENU_PAGER).
418  */
419 int mutt_index_menu (void)
420 {
421   char buf[LONG_STRING], helpstr[SHORT_STRING];
422   int flags;
423   int op = OP_NULL;
424   int done = 0;                 /* controls when to exit the "event" loop */
425   int i = 0, j;
426   int tag = 0;                  /* has the tag-prefix command been pressed? */
427   int newcount = -1;
428   int oldcount = -1;
429   int rc = -1;
430   MUTTMENU *menu;
431   char *cp;                     /* temporary variable. */
432   int index_hint;               /* used to restore cursor position */
433   int do_buffy_notify = 1;
434   int close = 0;                /* did we OP_QUIT or OP_EXIT out of this menu? */
435   int attach_msg = option (OPTATTACHMSG);
436
437   menu = mutt_new_menu ();
438   menu->menu = MENU_MAIN;
439   menu->offset = 1;
440   menu->pagelen = LINES - 3;
441   menu->make_entry = index_make_entry;
442   menu->color = index_color;
443   menu->current = ci_first_message ();
444   menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_MAIN,
445 #ifdef USE_NNTP
446                                   (Context
447                                    && (Context->magic ==
448                                        M_NNTP)) ? IndexNewsHelp :
449 #endif
450                                   IndexHelp);
451
452   if (!attach_msg) {
453     mutt_buffy_check (1);       /* force the buffy check after we enter the folder */
454     /* record folder we open to place sidebar indicator properly */
455     if (Context && Context->path)
456       set_curbuffy (Context->path);
457   }
458
459   FOREVER {
460     tag = 0;                    /* clear the tag-prefix */
461
462     menu->max = Context ? Context->vcount : 0;
463     oldcount = Context ? Context->msgcount : 0;
464
465     /* check if we need to resort the index because just about
466      * any 'op' below could do mutt_enter_command(), either here or
467      * from any new menu launched, and change $sort/$sort_aux
468      */
469     if (option (OPTNEEDRESORT) && Context && Context->msgcount)
470       resort_index (menu);
471
472     if (option (OPTREDRAWTREE) && Context && Context->msgcount
473         && (Sort & SORT_MASK) == SORT_THREADS) {
474       mutt_draw_tree (Context);
475       menu->redraw |= REDRAW_STATUS;
476       unset_option (OPTREDRAWTREE);
477     }
478
479     if (Context && !attach_msg) {
480       int check;
481
482       /* check for new mail in the mailbox.  If nonzero, then something has
483        * changed about the file (either we got new mail or the file was
484        * modified underneath us.)
485        */
486
487 #ifdef USE_IMAP
488       imap_allow_reopen (Context);
489 #endif
490
491       index_hint = (Context->vcount && menu->current >= 0
492                     && menu->current < Context->vcount) ? CURHDR->index : 0;
493
494       if ((check = mx_check_mailbox (Context, &index_hint, 0)) < 0) {
495         if (!Context->path) {
496           /* fatal error occurred */
497           FREE (&Context);
498           menu->redraw = REDRAW_FULL;
499         }
500         set_option (OPTSEARCHINVALID);
501       }
502       else if (check == M_NEW_MAIL || check == M_REOPENED || check == M_FLAGS) {
503         update_index (menu, Context, check, oldcount, index_hint);
504
505         /* notify the user of new mail */
506         if (check == M_REOPENED)
507           mutt_error (_
508                       ("Mailbox was externally modified.  Flags may be wrong."));
509         else if (check == M_NEW_MAIL) {
510           /* on new mail: redraw sidebar */
511           draw_sidebar (CurrentMenu);
512           mutt_message (_("New mail in this mailbox."));
513
514           if (option (OPTBEEPNEW))
515             beep ();
516         }
517         else if (check == M_FLAGS)
518           mutt_message (_("Mailbox was externally modified."));
519
520         /* avoid the message being overwritten by buffy */
521         do_buffy_notify = 0;
522
523         menu->redraw = REDRAW_FULL;
524         menu->max = Context->vcount;
525
526         set_option (OPTSEARCHINVALID);
527       }
528     }
529
530 #ifdef USE_IMAP
531     imap_keepalive ();
532     imap_disallow_reopen (Context);
533 #endif
534
535     if (!attach_msg) {
536       /* check for new mail in the incoming folders */
537       oldcount = newcount;
538       if ((newcount = mutt_buffy_check (0)) != oldcount) {
539         menu->redraw |= REDRAW_STATUS;
540         menu->redraw |= REDRAW_SIDEBAR;
541       }
542       if (do_buffy_notify) {
543         if (mutt_buffy_notify () && option (OPTBEEPNEW))
544           beep ();
545       }
546       else
547         do_buffy_notify = 1;
548     }
549
550     if (op != -1)
551       mutt_curs_set (0);
552     if (menu->redraw & REDRAW_SIDEBAR)
553       draw_sidebar (menu->menu);
554     if (menu->redraw & REDRAW_FULL) {
555       menu_redraw_full (menu);
556       draw_sidebar (menu->menu);
557       mutt_show_error ();
558     }
559
560     if (menu->menu == MENU_MAIN) {
561       if (Context && Context->hdrs && !(menu->current >= Context->vcount)) {
562         menu_check_recenter (menu);
563
564         if (menu->redraw & REDRAW_INDEX) {
565           menu_redraw_index (menu);
566           menu->redraw |= REDRAW_STATUS;
567         }
568         else if (menu->redraw & (REDRAW_MOTION_RESYNCH | REDRAW_MOTION))
569           menu_redraw_motion (menu);
570         else if (menu->redraw & REDRAW_CURRENT)
571           menu_redraw_current (menu);
572       }
573
574       if (menu->redraw & REDRAW_STATUS) {
575         DrawFullLine = 1;
576         menu_status_line (buf, sizeof (buf), menu, NONULL (Status));
577         DrawFullLine = 0;
578         CLEARLINE (option (OPTSTATUSONTOP) ? 0 : LINES - 2);
579         SETCOLOR (MT_COLOR_STATUS);
580         mutt_paddstr (COLS, buf);
581         SETCOLOR (MT_COLOR_NORMAL);
582         set_buffystats (Context);
583         menu->redraw &= ~REDRAW_STATUS;
584         if (option (OPTXTERMSETTITLES)) {
585           menu_status_line (buf, sizeof (buf), menu, NONULL (XtermTitle));
586           set_xterm_title_bar (buf);
587           menu_status_line (buf, sizeof (buf), menu, NONULL (XtermIcon));
588           set_xterm_icon_name (buf);
589         }
590       }
591
592       menu->redraw = 0;
593       if (menu->current < menu->max)
594         menu->oldcurrent = menu->current;
595       else
596         menu->oldcurrent = -1;
597
598       if (option (OPTARROWCURSOR))
599         move (menu->current - menu->top + menu->offset, 2);
600       else
601         move (menu->current - menu->top + menu->offset, COLS - 1);
602       mutt_refresh ();
603
604       op = km_dokey (MENU_MAIN);
605
606       dprint (4,
607               (debugfile, "mutt_index_menu[%d]: Got op %d\n", __LINE__, op));
608
609 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
610       if (SigWinch) {
611         mutt_flushinp ();
612         mutt_resize_screen ();
613         menu->redraw = REDRAW_FULL;
614         menu->menu = MENU_MAIN;
615         SigWinch = 0;
616         menu->top = 0;          /* so we scroll the right amount */
617         /*
618          * force a real complete redraw.  clrtobot() doesn't seem to be able
619          * to handle every case without this.
620          */
621         clearok (stdscr, TRUE);
622         continue;
623       }
624 #endif
625
626       if (op == -1)
627         continue;               /* either user abort or timeout */
628
629       mutt_curs_set (1);
630
631       /* special handling for the tag-prefix function */
632       if (op == OP_TAG_PREFIX) {
633         if (!Context) {
634           mutt_error (_("No mailbox is open."));
635
636           continue;
637         }
638
639         if (!Context->tagged) {
640           mutt_error (_("No tagged messages."));
641
642           continue;
643         }
644         tag = 1;
645
646         /* give visual indication that the next command is a tag- command */
647         mvaddstr (LINES - 1, 0, "tag-");
648         clrtoeol ();
649
650         /* get the real command */
651         if ((op = km_dokey (MENU_MAIN)) == OP_TAG_PREFIX) {
652           /* abort tag sequence */
653           CLEARLINE (LINES - 1);
654           continue;
655         }
656       }
657       else if (option (OPTAUTOTAG) && Context && Context->tagged)
658         tag = 1;
659
660       if (op == OP_TAG_PREFIX_COND) {
661         if (!Context) {
662           mutt_error (_("No mailbox is open."));
663
664           continue;
665         }
666
667         if (!Context->tagged) {
668           event_t tmp;
669
670           while (UngetCount > 0) {
671             tmp = mutt_getch ();
672             if (tmp.op == OP_END_COND)
673               break;
674           }
675           mutt_message (_("Nothing to do."));
676
677           continue;
678         }
679         tag = 1;
680
681         /* give visual indication that the next command is a tag- command */
682         mvaddstr (LINES - 1, 0, "tag-");
683         clrtoeol ();
684
685         /* get the real command */
686         if ((op = km_dokey (MENU_MAIN)) == OP_TAG_PREFIX) {
687           /* abort tag sequence */
688           CLEARLINE (LINES - 1);
689           continue;
690         }
691       }
692
693       mutt_clear_error ();
694     }
695     else {
696       if (menu->current < menu->max)
697         menu->oldcurrent = menu->current;
698       else
699         menu->oldcurrent = -1;
700
701       mutt_curs_set (1);        /* fallback from the pager */
702     }
703
704 #ifdef USE_NNTP
705     unset_option (OPTNEWS);     /* for any case */
706 #endif
707
708     switch (op) {
709
710       /* ----------------------------------------------------------------------
711        * movement commands
712        */
713
714     case OP_BOTTOM_PAGE:
715       menu_bottom_page (menu);
716       break;
717     case OP_FIRST_ENTRY:
718       menu_first_entry (menu);
719       break;
720     case OP_MIDDLE_PAGE:
721       menu_middle_page (menu);
722       break;
723     case OP_HALF_UP:
724       menu_half_up (menu);
725       break;
726     case OP_HALF_DOWN:
727       menu_half_down (menu);
728       break;
729     case OP_NEXT_LINE:
730       menu_next_line (menu);
731       break;
732     case OP_PREV_LINE:
733       menu_prev_line (menu);
734       break;
735     case OP_NEXT_PAGE:
736       menu_next_page (menu);
737       break;
738     case OP_PREV_PAGE:
739       menu_prev_page (menu);
740       break;
741     case OP_LAST_ENTRY:
742       menu_last_entry (menu);
743       break;
744     case OP_TOP_PAGE:
745       menu_top_page (menu);
746       break;
747     case OP_CURRENT_TOP:
748       menu_current_top (menu);
749       break;
750     case OP_CURRENT_MIDDLE:
751       menu_current_middle (menu);
752       break;
753     case OP_CURRENT_BOTTOM:
754       menu_current_bottom (menu);
755       break;
756
757 #ifdef USE_NNTP
758     case OP_GET_MESSAGE:
759     case OP_GET_PARENT:
760       CHECK_MSGCOUNT;
761       if (Context->magic == M_NNTP) {
762         HEADER *h;
763
764         if (op == OP_GET_MESSAGE) {
765           buf[0] = 0;
766           if (mutt_get_field (_("Enter Message-Id: "), buf, sizeof (buf), 0)
767               != 0 || !buf[0])
768             break;
769         }
770         else {
771           LIST *ref = CURHDR->env->references;
772
773           if (!ref) {
774             mutt_error (_("Article has no parent reference!"));
775
776             break;
777           }
778           strfcpy (buf, ref->data, sizeof (buf));
779         }
780         if (!Context->id_hash)
781           Context->id_hash = mutt_make_id_hash (Context);
782         if ((h = hash_find (Context->id_hash, buf))) {
783           if (h->virtual != -1) {
784             menu->current = h->virtual;
785             menu->redraw = REDRAW_MOTION_RESYNCH;
786           }
787           else if (h->collapsed) {
788             mutt_uncollapse_thread (Context, h);
789             mutt_set_virtual (Context);
790             menu->current = h->virtual;
791             menu->redraw = REDRAW_MOTION_RESYNCH;
792           }
793           else
794             mutt_error (_("Message not visible in limited view."));
795         }
796         else {
797           if (nntp_check_msgid (Context, buf) == 0) {
798             h = Context->hdrs[Context->msgcount - 1];
799             mutt_sort_headers (Context, 0);
800             menu->current = h->virtual;
801             menu->redraw = REDRAW_FULL;
802           }
803           else
804             mutt_error (_("Article %s not found on server"), buf);
805         }
806       }
807       break;
808
809     case OP_GET_CHILDREN:
810     case OP_RECONSTRUCT_THREAD:
811       CHECK_MSGCOUNT;
812       if (Context->magic == M_NNTP) {
813         HEADER *h;
814         int old = CURHDR->index, i;
815
816         if (!CURHDR->env->message_id) {
817           mutt_error (_("No Message-Id. Unable to perform operation"));
818
819           break;
820         }
821
822         if (!Context->id_hash)
823           Context->id_hash = mutt_make_id_hash (Context);
824         strfcpy (buf, CURHDR->env->message_id, sizeof (buf));
825
826         if (op == OP_RECONSTRUCT_THREAD) {
827           LIST *ref = CURHDR->env->references;
828
829           while (ref) {
830             nntp_check_msgid (Context, ref->data);
831             /* the last msgid in References is the root message */
832             if (!ref->next)
833               strfcpy (buf, ref->data, sizeof (buf));
834             ref = ref->next;
835           }
836         }
837         mutt_message (_("Check for children of message..."));
838
839         if (nntp_check_children (Context, buf) == 0) {
840           mutt_sort_headers (Context, (op == OP_RECONSTRUCT_THREAD));
841           h = hash_find (Context->id_hash, buf);
842           /* if the root message was retrieved, move to it */
843           if (h)
844             menu->current = h->virtual;
845           else                  /* try to restore old position */
846             for (i = 0; i < Context->msgcount; i++)
847               if (Context->hdrs[i]->index == old) {
848                 menu->current = Context->hdrs[i]->virtual;
849                 /* As an added courtesy, recenter the menu
850                  * with the current entry at the middle of the screen */
851                 menu_check_recenter (menu);
852                 menu_current_middle (menu);
853               }
854         }
855         menu->redraw = REDRAW_FULL;
856         mutt_clear_error ();
857       }
858       break;
859 #endif
860
861     case OP_JUMP:
862
863       CHECK_MSGCOUNT;
864       CHECK_VISIBLE;
865       if (isdigit (LastKey))
866         mutt_ungetch (LastKey, 0);
867       buf[0] = 0;
868       if (mutt_get_field (_("Jump to message: "), buf, sizeof (buf), 0) != 0
869           || !buf[0])
870         break;
871
872       if (!isdigit ((unsigned char) buf[0])) {
873         mutt_error (_("Argument must be a message number."));
874
875         break;
876       }
877
878       i = atoi (buf);
879       if (i > 0 && i <= Context->msgcount) {
880         for (j = i - 1; j < Context->msgcount; j++) {
881           if (Context->hdrs[j]->virtual != -1)
882             break;
883         }
884         if (j >= Context->msgcount) {
885           for (j = i - 2; j >= 0; j--) {
886             if (Context->hdrs[j]->virtual != -1)
887               break;
888           }
889         }
890
891         if (j >= 0) {
892           menu->current = Context->hdrs[j]->virtual;
893           if (menu->menu == MENU_PAGER) {
894             op = OP_DISPLAY_MESSAGE;
895             continue;
896           }
897           else
898             menu->redraw = REDRAW_MOTION;
899         }
900         else
901           mutt_error (_("That message is not visible."));
902       }
903       else
904         mutt_error (_("Invalid message number."));
905
906       break;
907
908       /* --------------------------------------------------------------------
909        * `index' specific commands
910        */
911
912     case OP_MAIN_DELETE_PATTERN:
913
914       CHECK_MSGCOUNT;
915       CHECK_VISIBLE;
916       CHECK_READONLY;
917
918 #ifdef USE_IMAP
919       CHECK_IMAP_ACL (IMAP_ACL_DELETE);
920 #endif
921
922       CHECK_ATTACH;
923       mutt_pattern_func (M_DELETE, _("Delete messages matching: "));
924       menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
925       break;
926
927 #ifdef USE_POP
928     case OP_MAIN_FETCH_MAIL:
929
930       CHECK_ATTACH;
931       pop_fetch_mail ();
932       menu->redraw = REDRAW_FULL;
933       break;
934 #endif /* USE_POP */
935
936     case OP_HELP:
937
938       mutt_help (MENU_MAIN);
939       menu->redraw = REDRAW_FULL;
940       break;
941
942     case OP_MAIN_SHOW_LIMIT:
943       CHECK_MSGCOUNT;
944       if (!Context->pattern)
945         mutt_message (_("No limit pattern is in effect."));
946
947       else {
948         char buf[STRING];
949
950         /* i18n: ask for a limit to apply */
951         snprintf (buf, sizeof (buf), _("Limit: %s"), Context->pattern);
952         mutt_message ("%s", buf);
953       }
954       break;
955
956     case OP_MAIN_LIMIT:
957     case OP_TOGGLE_READ:
958
959       CHECK_MSGCOUNT;
960       menu->oldcurrent = (Context->vcount && menu->current >= 0
961                           && menu->current <
962                           Context->vcount) ? CURHDR->index : -1;
963       if (op == OP_TOGGLE_READ) {
964         char buf[LONG_STRING];
965
966         if (!Context->pattern
967             || strncmp (Context->pattern, "!~R!~D~s", 8) != 0) {
968           snprintf (buf, sizeof (buf), "!~R!~D~s%s",
969                     Context->pattern ? Context->pattern : ".*");
970           set_option (OPTHIDEREAD);
971         }
972         else {
973           strfcpy (buf, Context->pattern + 8, sizeof (buf));
974           if (!*buf || strncmp (buf, ".*", 2) == 0)
975             snprintf (buf, sizeof (buf), "~A");
976           unset_option (OPTHIDEREAD);
977         }
978         FREE (&Context->pattern);
979         Context->pattern = safe_strdup (buf);
980       }
981       if ((op == OP_TOGGLE_READ && mutt_pattern_func (M_LIMIT, NULL) == 0) ||
982           mutt_pattern_func (M_LIMIT, _("Limit to messages matching: ")) == 0)
983       {
984         if (menu->oldcurrent >= 0) {
985           /* try to find what used to be the current message */
986           menu->current = -1;
987           for (i = 0; i < Context->vcount; i++)
988             if (Context->hdrs[Context->v2r[i]]->index == menu->oldcurrent) {
989               menu->current = i;
990               break;
991             }
992           if (menu->current < 0)
993             menu->current = 0;
994         }
995         else
996           menu->current = 0;
997         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
998         if ((Sort & SORT_MASK) == SORT_THREADS)
999           mutt_draw_tree (Context);
1000         menu->redraw = REDRAW_FULL;
1001       }
1002       break;
1003
1004     case OP_QUIT:
1005
1006       close = op;
1007       if (attach_msg) {
1008         done = 1;
1009         break;
1010       }
1011
1012       if (query_quadoption (OPT_QUIT, _("Quit Mutt-ng?")) == M_YES) {
1013         int check;
1014
1015         oldcount = Context ? Context->msgcount : 0;
1016
1017         if (!Context
1018             || (check = mx_close_mailbox (Context, &index_hint)) == 0)
1019           done = 1;
1020         else {
1021           if (check == M_NEW_MAIL || check == M_REOPENED)
1022             update_index (menu, Context, check, oldcount, index_hint);
1023
1024           menu->redraw = REDRAW_FULL;   /* new mail arrived? */
1025           set_option (OPTSEARCHINVALID);
1026         }
1027       }
1028       break;
1029
1030     case OP_REDRAW:
1031
1032       clearok (stdscr, TRUE);
1033       menu->redraw = REDRAW_FULL;
1034       break;
1035
1036     case OP_SEARCH:
1037     case OP_SEARCH_REVERSE:
1038     case OP_SEARCH_NEXT:
1039     case OP_SEARCH_OPPOSITE:
1040
1041       CHECK_MSGCOUNT;
1042       CHECK_VISIBLE;
1043       if ((menu->current = mutt_search_command (menu->current, op)) == -1)
1044         menu->current = menu->oldcurrent;
1045       else
1046         menu->redraw = REDRAW_MOTION;
1047       break;
1048
1049     case OP_SORT:
1050     case OP_SORT_REVERSE:
1051
1052       if (mutt_select_sort ((op == OP_SORT_REVERSE)) == 0) {
1053         if (Context && Context->msgcount) {
1054           resort_index (menu);
1055           set_option (OPTSEARCHINVALID);
1056         }
1057       }
1058       break;
1059
1060     case OP_TAG:
1061
1062       CHECK_MSGCOUNT;
1063       CHECK_VISIBLE;
1064       if (tag && !option (OPTAUTOTAG)) {
1065         for (j = 0; j < Context->vcount; j++)
1066           mutt_set_flag (Context, Context->hdrs[Context->v2r[j]], M_TAG, 0);
1067         menu->redraw = REDRAW_STATUS | REDRAW_INDEX;
1068       }
1069       else {
1070         mutt_set_flag (Context, CURHDR, M_TAG, !CURHDR->tagged);
1071         Context->last_tag = CURHDR->tagged ? CURHDR :
1072           ((Context->last_tag == CURHDR && !CURHDR->tagged)
1073            ? NULL : Context->last_tag);
1074         menu->redraw = REDRAW_STATUS;
1075         if (option (OPTRESOLVE) && menu->current < Context->vcount - 1) {
1076           menu->current++;
1077           menu->redraw |= REDRAW_MOTION_RESYNCH;
1078         }
1079         else
1080           menu->redraw |= REDRAW_CURRENT;
1081       }
1082       break;
1083
1084     case OP_MAIN_TAG_PATTERN:
1085
1086       CHECK_MSGCOUNT;
1087       CHECK_VISIBLE;
1088       mutt_pattern_func (M_TAG, _("Tag messages matching: "));
1089       menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1090       break;
1091
1092     case OP_MAIN_UNDELETE_PATTERN:
1093
1094       CHECK_MSGCOUNT;
1095       CHECK_VISIBLE;
1096       CHECK_READONLY;
1097
1098 #ifdef USE_IMAP
1099       CHECK_IMAP_ACL (IMAP_ACL_DELETE);
1100 #endif
1101
1102       if (mutt_pattern_func (M_UNDELETE, _("Undelete messages matching: ")) ==
1103           0)
1104         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1105       break;
1106
1107     case OP_MAIN_UNTAG_PATTERN:
1108
1109       CHECK_MSGCOUNT;
1110       CHECK_VISIBLE;
1111       if (mutt_pattern_func (M_UNTAG, _("Untag messages matching: ")) == 0)
1112         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1113       break;
1114
1115       /* --------------------------------------------------------------------
1116        * The following operations can be performed inside of the pager.
1117        */
1118
1119 #ifdef USE_IMAP
1120     case OP_MAIN_IMAP_FETCH:
1121       if (Context->magic == M_IMAP)
1122         imap_check_mailbox (Context, &index_hint, 1);
1123       break;
1124 #endif
1125
1126     case OP_MAIN_SYNC_FOLDER:
1127
1128       if (Context && !Context->msgcount)
1129         break;
1130
1131       CHECK_MSGCOUNT;
1132       CHECK_VISIBLE;
1133       CHECK_READONLY;
1134       {
1135         int oldvcount = Context->vcount;
1136         int oldcount = Context->msgcount;
1137         int dcount = 0;
1138         int check;
1139
1140         /* calculate the number of messages _above_ the cursor,
1141          * so we can keep the cursor on the current message
1142          */
1143         for (j = 0; j <= menu->current; j++) {
1144           if (Context->hdrs[Context->v2r[j]]->deleted)
1145             dcount++;
1146         }
1147
1148         if ((check = mx_sync_mailbox (Context, &index_hint)) == 0) {
1149           if (Context->vcount != oldvcount)
1150             menu->current -= dcount;
1151           set_option (OPTSEARCHINVALID);
1152         }
1153         else if (check == M_NEW_MAIL || check == M_REOPENED)
1154           update_index (menu, Context, check, oldcount, index_hint);
1155
1156         /* 
1157          * do a sanity check even if mx_sync_mailbox failed.
1158          */
1159
1160         if (menu->current < 0 || menu->current >= Context->vcount)
1161           menu->current = ci_first_message ();
1162       }
1163
1164       /* check for a fatal error, or all messages deleted */
1165       if (!Context->path)
1166         FREE (&Context);
1167
1168       /* if we were in the pager, redisplay the message */
1169       if (menu->menu == MENU_PAGER) {
1170         op = OP_DISPLAY_MESSAGE;
1171         continue;
1172       }
1173       else
1174         menu->redraw = REDRAW_FULL;
1175       break;
1176
1177     case OP_SIDEBAR_OPEN:
1178     case OP_MAIN_CHANGE_FOLDER:
1179     case OP_MAIN_CHANGE_FOLDER_READONLY:
1180 #ifdef USE_NNTP
1181     case OP_MAIN_CHANGE_GROUP:
1182     case OP_MAIN_CHANGE_GROUP_READONLY:
1183 #endif
1184       if (attach_msg || option (OPTREADONLY) ||
1185 #ifdef USE_NNTP
1186           op == OP_MAIN_CHANGE_GROUP_READONLY ||
1187 #endif
1188           op == OP_MAIN_CHANGE_FOLDER_READONLY)
1189         flags = M_READONLY;
1190       else
1191         flags = 0;
1192
1193       if (flags)
1194         cp = _("Open mailbox in read-only mode");
1195       else
1196         cp = _("Open mailbox");
1197
1198       buf[0] = '\0';
1199 #ifdef USE_NNTP
1200       unset_option (OPTNEWS);
1201       if (op == OP_MAIN_CHANGE_GROUP || op == OP_MAIN_CHANGE_GROUP_READONLY) {
1202         set_option (OPTNEWS);
1203         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)))
1204           break;
1205         if (flags)
1206           cp = _("Open newsgroup in read-only mode");
1207         else
1208           cp = _("Open newsgroup");
1209         nntp_buffy (buf);
1210       }
1211       else
1212 #endif
1213       {
1214         if (Context && Context->path)
1215           strncpy (buf, Context->path, sizeof (buf));
1216         mutt_buffy (buf, sizeof (buf));
1217       }
1218
1219       if (op == OP_SIDEBAR_OPEN) {
1220         if (!CurBuffy)
1221           break;
1222         strncpy (buf, CurBuffy->path, sizeof (buf));
1223       }
1224       else if (mutt_enter_fname (cp, buf, sizeof (buf), &menu->redraw, 1) ==
1225                -1)
1226         break;
1227       if (!buf[0]) {
1228         CLEARLINE (LINES - 1);
1229         break;
1230       }
1231
1232 #ifdef USE_NNTP
1233       if (option (OPTNEWS)) {
1234         unset_option (OPTNEWS);
1235         nntp_expand_path (buf, sizeof (buf), &CurrentNewsSrv->conn->account);
1236       }
1237       else
1238 #endif
1239         mutt_expand_path (buf, sizeof (buf));
1240       set_curbuffy (buf);
1241       if (mx_get_magic (buf) <= 0) {
1242         mutt_error (_("%s is not a mailbox."), buf);
1243         break;
1244       }
1245       mutt_str_replace (&CurrentFolder, buf);
1246
1247       if (Context) {
1248         int check;
1249
1250 #ifdef USE_COMPRESSED
1251         if (Context->compressinfo && Context->realpath)
1252           mutt_str_replace (&LastFolder, Context->realpath);
1253         else
1254 #endif
1255
1256           mutt_str_replace (&LastFolder, Context->path);
1257         oldcount = Context ? Context->msgcount : 0;
1258
1259         if ((check = mx_close_mailbox (Context, &index_hint)) != 0) {
1260           if (check == M_NEW_MAIL || check == M_REOPENED)
1261             update_index (menu, Context, check, oldcount, index_hint);
1262
1263           set_option (OPTSEARCHINVALID);
1264           menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1265           break;
1266         }
1267         FREE (&Context);
1268       }
1269
1270       mutt_sleep (0);
1271
1272       /* Set CurrentMenu to MENU_MAIN before executing any folder
1273        * hooks so that all the index menu functions are available to
1274        * the exec command.
1275        */
1276
1277       CurrentMenu = MENU_MAIN;
1278       mutt_folder_hook (buf);
1279
1280       if ((Context = mx_open_mailbox (buf, flags, NULL)) != NULL) {
1281         menu->current = ci_first_message ();
1282       }
1283       else
1284         menu->current = 0;
1285
1286 #ifdef USE_NNTP
1287       /* mutt_buffy_check() must be done with mail-reader mode! */
1288       menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_MAIN,
1289                                       (Context
1290                                        && (Context->magic ==
1291                                            M_NNTP)) ? IndexNewsHelp :
1292                                       IndexHelp);
1293 #endif
1294       mutt_clear_error ();
1295       mutt_buffy_check (1);     /* force the buffy check after we have changed
1296                                    the folder */
1297       menu->redraw = REDRAW_FULL;
1298       set_option (OPTSEARCHINVALID);
1299       break;
1300
1301     case OP_DISPLAY_MESSAGE:
1302     case OP_DISPLAY_HEADERS:   /* don't weed the headers */
1303
1304       CHECK_MSGCOUNT;
1305       CHECK_VISIBLE;
1306       /*
1307        * toggle the weeding of headers so that a user can press the key
1308        * again while reading the message.
1309        */
1310       if (op == OP_DISPLAY_HEADERS)
1311         toggle_option (OPTWEED);
1312
1313       unset_option (OPTNEEDRESORT);
1314
1315       if ((Sort & SORT_MASK) == SORT_THREADS && CURHDR->collapsed) {
1316         mutt_uncollapse_thread (Context, CURHDR);
1317         mutt_set_virtual (Context);
1318         if (option (OPTUNCOLLAPSEJUMP))
1319           menu->current = mutt_thread_next_unread (Context, CURHDR);
1320       }
1321
1322       if ((op = mutt_display_message (CURHDR)) == -1) {
1323         unset_option (OPTNEEDRESORT);
1324         break;
1325       }
1326
1327       menu->menu = MENU_PAGER;
1328       menu->oldcurrent = menu->current;
1329       continue;
1330
1331     case OP_EXIT:
1332
1333       close = op;
1334       if (menu->menu == MENU_MAIN && attach_msg) {
1335         done = 1;
1336         break;
1337       }
1338
1339       if ((menu->menu == MENU_MAIN)
1340           && (query_quadoption (OPT_QUIT,
1341                                 _("Exit Mutt-ng without saving?")) == M_YES))
1342       {
1343         if (Context) {
1344           mx_fastclose_mailbox (Context);
1345           FREE (&Context);
1346         }
1347         done = 1;
1348       }
1349       break;
1350
1351     case OP_EDIT_TYPE:
1352
1353       CHECK_MSGCOUNT;
1354       CHECK_VISIBLE;
1355       CHECK_ATTACH;
1356       mutt_edit_content_type (CURHDR, CURHDR->content, NULL);
1357       /* if we were in the pager, redisplay the message */
1358       if (menu->menu == MENU_PAGER) {
1359         op = OP_DISPLAY_MESSAGE;
1360         continue;
1361       }
1362       else
1363         menu->redraw = REDRAW_CURRENT;
1364       break;
1365
1366     case OP_MAIN_BREAK_THREAD:
1367
1368       CHECK_MSGCOUNT;
1369       CHECK_VISIBLE;
1370       CHECK_READONLY;
1371
1372       if ((Sort & SORT_MASK) != SORT_THREADS)
1373         mutt_error (_("Threading is not enabled."));
1374
1375       else {
1376         {
1377           HEADER *oldcur = CURHDR;
1378
1379           mutt_break_thread (CURHDR);
1380           mutt_sort_headers (Context, 1);
1381           menu->current = oldcur->virtual;
1382         }
1383
1384         Context->changed = 1;
1385         mutt_message _("Thread broken");
1386
1387         if (menu->menu == MENU_PAGER) {
1388           op = OP_DISPLAY_MESSAGE;
1389           continue;
1390         }
1391         else
1392           menu->redraw |= REDRAW_INDEX;
1393       }
1394       break;
1395
1396     case OP_MAIN_LINK_THREADS:
1397
1398       CHECK_MSGCOUNT;
1399       CHECK_VISIBLE;
1400       CHECK_READONLY;
1401
1402       if ((Sort & SORT_MASK) != SORT_THREADS)
1403         mutt_error (_("Threading is not enabled."));
1404
1405       else if (!CURHDR->env->message_id)
1406         mutt_error (_("No Message-ID: header available to link thread"));
1407
1408       else if (!tag && (!Context->last_tag || !Context->last_tag->tagged))
1409         mutt_error (_("First, please tag a message to be linked here"));
1410
1411       else {
1412         HEADER *oldcur = CURHDR;
1413
1414         if (mutt_link_threads (CURHDR, tag ? NULL : Context->last_tag,
1415                                Context)) {
1416           mutt_sort_headers (Context, 1);
1417           menu->current = oldcur->virtual;
1418
1419           Context->changed = 1;
1420           mutt_message _("Threads linked");
1421         }
1422         else
1423           mutt_error (_("No thread linked"));
1424       }
1425
1426       if (menu->menu == MENU_PAGER) {
1427         op = OP_DISPLAY_MESSAGE;
1428         continue;
1429       }
1430       else
1431         menu->redraw |= REDRAW_STATUS | REDRAW_INDEX;
1432       break;
1433
1434     case OP_MAIN_NEXT_UNDELETED:
1435
1436       CHECK_MSGCOUNT;
1437       CHECK_VISIBLE;
1438       if (menu->current >= Context->vcount - 1) {
1439         if (menu->menu == MENU_MAIN)
1440           mutt_error (_("You are on the last message."));
1441
1442         break;
1443       }
1444       if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1445         menu->current = menu->oldcurrent;
1446         if (menu->menu == MENU_MAIN)
1447           mutt_error (_("No undeleted messages."));
1448       }
1449       else if (menu->menu == MENU_PAGER) {
1450         op = OP_DISPLAY_MESSAGE;
1451         continue;
1452       }
1453       else
1454         menu->redraw = REDRAW_MOTION;
1455       break;
1456
1457     case OP_NEXT_ENTRY:
1458
1459       CHECK_MSGCOUNT;
1460       CHECK_VISIBLE;
1461       if (menu->current >= Context->vcount - 1) {
1462         if (menu->menu == MENU_MAIN)
1463           mutt_error (_("You are on the last message."));
1464
1465         break;
1466       }
1467       menu->current++;
1468       if (menu->menu == MENU_PAGER) {
1469         op = OP_DISPLAY_MESSAGE;
1470         continue;
1471       }
1472       else
1473         menu->redraw = REDRAW_MOTION;
1474       break;
1475
1476     case OP_MAIN_PREV_UNDELETED:
1477
1478       CHECK_MSGCOUNT;
1479       CHECK_VISIBLE;
1480       if (menu->current < 1) {
1481         mutt_error (_("You are on the first message."));
1482
1483         break;
1484       }
1485       if ((menu->current = ci_previous_undeleted (menu->current)) == -1) {
1486         menu->current = menu->oldcurrent;
1487         if (menu->menu == MENU_MAIN)
1488           mutt_error (_("No undeleted messages."));
1489       }
1490       else if (menu->menu == MENU_PAGER) {
1491         op = OP_DISPLAY_MESSAGE;
1492         continue;
1493       }
1494       else
1495         menu->redraw = REDRAW_MOTION;
1496       break;
1497
1498     case OP_PREV_ENTRY:
1499
1500       CHECK_MSGCOUNT;
1501       CHECK_VISIBLE;
1502       if (menu->current < 1) {
1503         if (menu->menu == MENU_MAIN)
1504           mutt_error (_("You are on the first message."));
1505
1506         break;
1507       }
1508       menu->current--;
1509       if (menu->menu == MENU_PAGER) {
1510         op = OP_DISPLAY_MESSAGE;
1511         continue;
1512       }
1513       else
1514         menu->redraw = REDRAW_MOTION;
1515       break;
1516
1517     case OP_DECRYPT_COPY:
1518     case OP_DECRYPT_SAVE:
1519       if (!WithCrypto)
1520         break;
1521       /* fall thru */
1522     case OP_COPY_MESSAGE:
1523     case OP_SAVE:
1524     case OP_DECODE_COPY:
1525     case OP_DECODE_SAVE:
1526       CHECK_MSGCOUNT;
1527       CHECK_VISIBLE;
1528       if (mutt_save_message (tag ? NULL : CURHDR,
1529                              (op == OP_DECRYPT_SAVE) ||
1530                              (op == OP_SAVE) || (op == OP_DECODE_SAVE),
1531                              (op == OP_DECODE_SAVE) || (op == OP_DECODE_COPY),
1532                              (op == OP_DECRYPT_SAVE)
1533                              || (op == OP_DECRYPT_COPY)
1534                              || 0, &menu->redraw) == 0 && (op == OP_SAVE
1535                                                            || op ==
1536                                                            OP_DECODE_SAVE
1537                                                            || op ==
1538                                                            OP_DECRYPT_SAVE)
1539         ) {
1540         if (tag)
1541           menu->redraw |= REDRAW_INDEX;
1542         else if (option (OPTRESOLVE)) {
1543           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1544             menu->current = menu->oldcurrent;
1545             menu->redraw |= REDRAW_CURRENT;
1546           }
1547           else
1548             menu->redraw |= REDRAW_MOTION_RESYNCH;
1549         }
1550         else
1551           menu->redraw |= REDRAW_CURRENT;
1552       }
1553       break;
1554
1555     case OP_MAIN_NEXT_NEW:
1556     case OP_MAIN_NEXT_UNREAD:
1557     case OP_MAIN_PREV_NEW:
1558     case OP_MAIN_PREV_UNREAD:
1559     case OP_MAIN_NEXT_NEW_THEN_UNREAD:
1560     case OP_MAIN_PREV_NEW_THEN_UNREAD:
1561
1562       {
1563         int first_unread = -1;
1564         int first_new = -1;
1565
1566         CHECK_MSGCOUNT;
1567         CHECK_VISIBLE;
1568
1569         i = menu->current;
1570         menu->current = -1;
1571         for (j = 0; j != Context->vcount; j++) {
1572 #define CURHDRi Context->hdrs[Context->v2r[i]]
1573           if (op == OP_MAIN_NEXT_NEW || op == OP_MAIN_NEXT_UNREAD
1574               || op == OP_MAIN_NEXT_NEW_THEN_UNREAD) {
1575             i++;
1576             if (i > Context->vcount - 1) {
1577               mutt_message _("Search wrapped to top.");
1578
1579               i = 0;
1580             }
1581           }
1582           else {
1583             i--;
1584             if (i < 0) {
1585               mutt_message _("Search wrapped to bottom.");
1586
1587               i = Context->vcount - 1;
1588             }
1589           }
1590
1591           if (CURHDRi->collapsed && (Sort & SORT_MASK) == SORT_THREADS) {
1592             if (UNREAD (CURHDRi) && first_unread == -1)
1593               first_unread = i;
1594             if (UNREAD (CURHDRi) == 1 && first_new == -1)
1595               first_new = i;
1596           }
1597           else if ((!CURHDRi->deleted && !CURHDRi->read)) {
1598             if (first_unread == -1)
1599               first_unread = i;
1600             if ((!CURHDRi->old) && first_new == -1)
1601               first_new = i;
1602           }
1603
1604           if ((op == OP_MAIN_NEXT_UNREAD || op == OP_MAIN_PREV_UNREAD) &&
1605               first_unread != -1)
1606             break;
1607           if ((op == OP_MAIN_NEXT_NEW || op == OP_MAIN_PREV_NEW ||
1608                op == OP_MAIN_NEXT_NEW_THEN_UNREAD
1609                || op == OP_MAIN_PREV_NEW_THEN_UNREAD)
1610               && first_new != -1)
1611             break;
1612         }
1613 #undef CURHDRi
1614         if ((op == OP_MAIN_NEXT_NEW || op == OP_MAIN_PREV_NEW ||
1615              op == OP_MAIN_NEXT_NEW_THEN_UNREAD
1616              || op == OP_MAIN_PREV_NEW_THEN_UNREAD)
1617             && first_new != -1)
1618           menu->current = first_new;
1619         else if ((op == OP_MAIN_NEXT_UNREAD || op == OP_MAIN_PREV_UNREAD ||
1620                   op == OP_MAIN_NEXT_NEW_THEN_UNREAD
1621                   || op == OP_MAIN_PREV_NEW_THEN_UNREAD)
1622                  && first_unread != -1)
1623           menu->current = first_unread;
1624
1625         if (menu->current == -1) {
1626           menu->current = menu->oldcurrent;
1627           mutt_error ("%s%s.",
1628                       (op == OP_MAIN_NEXT_NEW
1629                        || op ==
1630                        OP_MAIN_PREV_NEW) ? _("No new messages") :
1631                       _("No unread messages"),
1632                       Context->pattern ? _(" in this limited view") : "");
1633         }
1634         else if (menu->menu == MENU_PAGER) {
1635           op = OP_DISPLAY_MESSAGE;
1636           continue;
1637         }
1638         else
1639           menu->redraw = REDRAW_MOTION;
1640         break;
1641       }
1642     case OP_FLAG_MESSAGE:
1643
1644       CHECK_MSGCOUNT;
1645       CHECK_VISIBLE;
1646       CHECK_READONLY;
1647
1648 #ifdef USE_POP
1649       if (Context->magic == M_POP) {
1650         mutt_flushinp ();
1651         mutt_error (_("Can't change 'important' flag on POP server."));
1652
1653         break;
1654       }
1655 #endif
1656
1657 #ifdef USE_IMAP
1658       CHECK_IMAP_ACL (IMAP_ACL_WRITE);
1659 #endif
1660
1661 #ifdef USE_NNTP
1662       if (Context->magic == M_NNTP) {
1663         mutt_flushinp ();
1664         mutt_error (_("Can't change 'important' flag on NNTP server."));
1665
1666         break;
1667       }
1668 #endif
1669
1670       if (tag) {
1671         for (j = 0; j < Context->vcount; j++) {
1672           if (Context->hdrs[Context->v2r[j]]->tagged)
1673             mutt_set_flag (Context, Context->hdrs[Context->v2r[j]],
1674                            M_FLAG, !Context->hdrs[Context->v2r[j]]->flagged);
1675         }
1676
1677         menu->redraw |= REDRAW_INDEX;
1678       }
1679       else {
1680         mutt_set_flag (Context, CURHDR, M_FLAG, !CURHDR->flagged);
1681         if (option (OPTRESOLVE)) {
1682           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1683             menu->current = menu->oldcurrent;
1684             menu->redraw = REDRAW_CURRENT;
1685           }
1686           else
1687             menu->redraw = REDRAW_MOTION_RESYNCH;
1688         }
1689         else
1690           menu->redraw = REDRAW_CURRENT;
1691       }
1692       menu->redraw |= REDRAW_STATUS;
1693       break;
1694
1695     case OP_TOGGLE_NEW:
1696
1697       CHECK_MSGCOUNT;
1698       CHECK_VISIBLE;
1699       CHECK_READONLY;
1700
1701 #ifdef USE_IMAP
1702       CHECK_IMAP_ACL (IMAP_ACL_SEEN);
1703 #endif
1704
1705       if (tag) {
1706         for (j = 0; j < Context->vcount; j++) {
1707           if (Context->hdrs[Context->v2r[j]]->tagged) {
1708             if (Context->hdrs[Context->v2r[j]]->read ||
1709                 Context->hdrs[Context->v2r[j]]->old)
1710               mutt_set_flag (Context, Context->hdrs[Context->v2r[j]], M_NEW,
1711                              1);
1712             else
1713               mutt_set_flag (Context, Context->hdrs[Context->v2r[j]], M_READ,
1714                              1);
1715           }
1716         }
1717         menu->redraw = REDRAW_STATUS | REDRAW_INDEX;
1718       }
1719       else {
1720         if (CURHDR->read || CURHDR->old)
1721           mutt_set_flag (Context, CURHDR, M_NEW, 1);
1722         else
1723           mutt_set_flag (Context, CURHDR, M_READ, 1);
1724
1725         if (option (OPTRESOLVE)) {
1726           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1727             menu->current = menu->oldcurrent;
1728             menu->redraw = REDRAW_CURRENT;
1729           }
1730           else
1731             menu->redraw = REDRAW_MOTION_RESYNCH;
1732         }
1733         else
1734           menu->redraw = REDRAW_CURRENT;
1735         menu->redraw |= REDRAW_STATUS;
1736       }
1737       break;
1738
1739     case OP_TOGGLE_WRITE:
1740
1741       CHECK_MSGCOUNT;
1742       if (mx_toggle_write (Context) == 0)
1743         menu->redraw |= REDRAW_STATUS;
1744       break;
1745
1746     case OP_MAIN_NEXT_THREAD:
1747     case OP_MAIN_NEXT_SUBTHREAD:
1748     case OP_MAIN_PREV_THREAD:
1749     case OP_MAIN_PREV_SUBTHREAD:
1750
1751       CHECK_MSGCOUNT;
1752       CHECK_VISIBLE;
1753       switch (op) {
1754       case OP_MAIN_NEXT_THREAD:
1755         menu->current = mutt_next_thread (CURHDR);
1756         break;
1757
1758       case OP_MAIN_NEXT_SUBTHREAD:
1759         menu->current = mutt_next_subthread (CURHDR);
1760         break;
1761
1762       case OP_MAIN_PREV_THREAD:
1763         menu->current = mutt_previous_thread (CURHDR);
1764         break;
1765
1766       case OP_MAIN_PREV_SUBTHREAD:
1767         menu->current = mutt_previous_subthread (CURHDR);
1768         break;
1769       }
1770
1771       if (menu->current < 0) {
1772         menu->current = menu->oldcurrent;
1773         if (op == OP_MAIN_NEXT_THREAD || op == OP_MAIN_NEXT_SUBTHREAD)
1774           mutt_error (_("No more threads."));
1775
1776         else
1777           mutt_error (_("You are on the first thread."));
1778       }
1779       else if (menu->menu == MENU_PAGER) {
1780         op = OP_DISPLAY_MESSAGE;
1781         continue;
1782       }
1783       else
1784         menu->redraw = REDRAW_MOTION;
1785       break;
1786
1787     case OP_MAIN_PARENT_MESSAGE:
1788
1789       CHECK_MSGCOUNT;
1790       CHECK_VISIBLE;
1791
1792       if ((menu->current = mutt_parent_message (Context, CURHDR)) < 0) {
1793         menu->current = menu->oldcurrent;
1794       }
1795       else if (menu->menu == MENU_PAGER) {
1796         op = OP_DISPLAY_MESSAGE;
1797         continue;
1798       }
1799       else
1800         menu->redraw = REDRAW_MOTION;
1801       break;
1802
1803     case OP_MAIN_SET_FLAG:
1804     case OP_MAIN_CLEAR_FLAG:
1805
1806       CHECK_MSGCOUNT;
1807       CHECK_VISIBLE;
1808       CHECK_READONLY;
1809
1810 /* #ifdef USE_IMAP
1811 CHECK_IMAP_ACL(IMAP_ACL_WRITE);
1812 #endif */
1813
1814       if (mutt_change_flag (tag ? NULL : CURHDR, (op == OP_MAIN_SET_FLAG)) ==
1815           0) {
1816         menu->redraw = REDRAW_STATUS;
1817         if (tag)
1818           menu->redraw |= REDRAW_INDEX;
1819         else if (option (OPTRESOLVE)) {
1820           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1821             menu->current = menu->oldcurrent;
1822             menu->redraw |= REDRAW_CURRENT;
1823           }
1824           else
1825             menu->redraw |= REDRAW_MOTION_RESYNCH;
1826         }
1827         else
1828           menu->redraw |= REDRAW_CURRENT;
1829       }
1830       break;
1831
1832     case OP_MAIN_COLLAPSE_THREAD:
1833       CHECK_MSGCOUNT;
1834       CHECK_VISIBLE;
1835
1836       if ((Sort & SORT_MASK) != SORT_THREADS) {
1837         mutt_error (_("Threading is not enabled."));
1838
1839         break;
1840       }
1841
1842       if (CURHDR->collapsed) {
1843         menu->current = mutt_uncollapse_thread (Context, CURHDR);
1844         mutt_set_virtual (Context);
1845         if (option (OPTUNCOLLAPSEJUMP))
1846           menu->current = mutt_thread_next_unread (Context, CURHDR);
1847       }
1848       else if (option (OPTCOLLAPSEUNREAD) || !UNREAD (CURHDR)) {
1849         menu->current = mutt_collapse_thread (Context, CURHDR);
1850         mutt_set_virtual (Context);
1851       }
1852       else {
1853         mutt_error (_("Thread contains unread messages."));
1854
1855         break;
1856       }
1857
1858       menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1859
1860       break;
1861
1862     case OP_MAIN_COLLAPSE_ALL:
1863       CHECK_MSGCOUNT;
1864       CHECK_VISIBLE;
1865
1866       if ((Sort & SORT_MASK) != SORT_THREADS) {
1867         mutt_error (_("Threading is not enabled."));
1868
1869         break;
1870       }
1871
1872       {
1873         HEADER *h, *base;
1874         THREAD *thread, *top;
1875         int final;
1876
1877         if (CURHDR->collapsed)
1878           final = mutt_uncollapse_thread (Context, CURHDR);
1879         else if (option (OPTCOLLAPSEUNREAD) || !UNREAD (CURHDR))
1880           final = mutt_collapse_thread (Context, CURHDR);
1881         else
1882           final = CURHDR->virtual;
1883
1884         base = Context->hdrs[Context->v2r[final]];
1885
1886         top = Context->tree;
1887         Context->collapsed = !Context->collapsed;
1888         while ((thread = top) != NULL) {
1889           while (!thread->message)
1890             thread = thread->child;
1891           h = thread->message;
1892
1893           if (h->collapsed != Context->collapsed) {
1894             if (h->collapsed)
1895               mutt_uncollapse_thread (Context, h);
1896             else if (option (OPTCOLLAPSEUNREAD) || !UNREAD (h))
1897               mutt_collapse_thread (Context, h);
1898           }
1899           top = top->next;
1900         }
1901
1902         mutt_set_virtual (Context);
1903         for (j = 0; j < Context->vcount; j++) {
1904           if (Context->hdrs[Context->v2r[j]]->index == base->index) {
1905             menu->current = j;
1906             break;
1907           }
1908         }
1909
1910         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1911       }
1912       break;
1913
1914       /* --------------------------------------------------------------------
1915        * These functions are invoked directly from the internal-pager
1916        */
1917
1918     case OP_BOUNCE_MESSAGE:
1919
1920       CHECK_ATTACH;
1921       CHECK_MSGCOUNT;
1922       CHECK_VISIBLE;
1923       ci_bounce_message (tag ? NULL : CURHDR, &menu->redraw);
1924       break;
1925
1926     case OP_CREATE_ALIAS:
1927
1928       mutt_create_alias (Context
1929                          && Context->vcount ? CURHDR->env : NULL, NULL);
1930       MAYBE_REDRAW (menu->redraw);
1931       menu->redraw |= REDRAW_CURRENT;
1932       break;
1933
1934     case OP_QUERY:
1935       CHECK_ATTACH;
1936       mutt_query_menu (NULL, 0);
1937       MAYBE_REDRAW (menu->redraw);
1938       break;
1939
1940     case OP_PURGE_MESSAGE:
1941     case OP_DELETE:
1942
1943       CHECK_MSGCOUNT;
1944       CHECK_VISIBLE;
1945       CHECK_READONLY;
1946
1947 #ifdef USE_IMAP
1948       CHECK_IMAP_ACL (IMAP_ACL_DELETE);
1949 #endif
1950
1951       if (tag) {
1952         mutt_tag_set_flag (M_DELETE, 1);
1953         mutt_tag_set_flag (M_PURGED, (op != OP_PURGE_MESSAGE) ? 0 : 1);
1954         if (option (OPTDELETEUNTAG))
1955           mutt_tag_set_flag (M_TAG, 0);
1956         menu->redraw = REDRAW_INDEX;
1957       }
1958       else {
1959         mutt_set_flag (Context, CURHDR, M_DELETE, 1);
1960         mutt_set_flag (Context, CURHDR, M_PURGED,
1961                        (op != OP_PURGE_MESSAGE) ? 0 : 1);
1962         if (option (OPTDELETEUNTAG))
1963           mutt_set_flag (Context, CURHDR, M_TAG, 0);
1964         if (option (OPTRESOLVE)) {
1965           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1966             menu->current = menu->oldcurrent;
1967             menu->redraw = REDRAW_CURRENT;
1968           }
1969           else if (menu->menu == MENU_PAGER) {
1970             op = OP_DISPLAY_MESSAGE;
1971             continue;
1972           }
1973           else
1974             menu->redraw |= REDRAW_MOTION_RESYNCH;
1975         }
1976         else
1977           menu->redraw = REDRAW_CURRENT;
1978       }
1979       menu->redraw |= REDRAW_STATUS;
1980       break;
1981
1982     case OP_DELETE_THREAD:
1983     case OP_DELETE_SUBTHREAD:
1984
1985       CHECK_MSGCOUNT;
1986       CHECK_VISIBLE;
1987       CHECK_READONLY;
1988
1989 #ifdef USE_IMAP
1990       CHECK_IMAP_ACL (IMAP_ACL_DELETE);
1991 #endif
1992
1993       rc = mutt_thread_set_flag (CURHDR, M_DELETE, 1,
1994                                  op == OP_DELETE_THREAD ? 0 : 1);
1995
1996       if (rc != -1) {
1997         if (option (OPTDELETEUNTAG))
1998           mutt_thread_set_flag (CURHDR, M_TAG, 0,
1999                                 op == OP_DELETE_THREAD ? 0 : 1);
2000         if (option (OPTRESOLVE))
2001           if ((menu->current = ci_next_undeleted (menu->current)) == -1)
2002             menu->current = menu->oldcurrent;
2003         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2004       }
2005       break;
2006
2007 #ifdef USE_NNTP
2008     case OP_CATCHUP:
2009       if (Context && Context->magic == M_NNTP) {
2010         if (mutt_newsgroup_catchup (CurrentNewsSrv,
2011                                     ((NNTP_DATA *) Context->data)->group))
2012           menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2013       }
2014       break;
2015 #endif
2016
2017     case OP_DISPLAY_ADDRESS:
2018
2019       CHECK_MSGCOUNT;
2020       CHECK_VISIBLE;
2021       mutt_display_address (CURHDR->env);
2022       break;
2023
2024     case OP_ENTER_COMMAND:
2025
2026       CurrentMenu = MENU_MAIN;
2027       mutt_enter_command ();
2028       mutt_check_rescore (Context);
2029       if (option (OPTFORCEREDRAWINDEX))
2030         menu->redraw = REDRAW_FULL;
2031       unset_option (OPTFORCEREDRAWINDEX);
2032       unset_option (OPTFORCEREDRAWPAGER);
2033       break;
2034
2035     case OP_EDIT_MESSAGE:
2036
2037       CHECK_MSGCOUNT;
2038       CHECK_VISIBLE;
2039       CHECK_READONLY;
2040       CHECK_ATTACH;
2041
2042 #ifdef USE_POP
2043       if (Context->magic == M_POP) {
2044         mutt_flushinp ();
2045         mutt_error (_("Can't edit message on POP server."));
2046
2047         break;
2048       }
2049 #endif
2050
2051 #ifdef USE_IMAP
2052       CHECK_IMAP_ACL (IMAP_ACL_INSERT);
2053 #endif
2054
2055 #ifdef USE_NNTP
2056       if (Context->magic == M_NNTP) {
2057         mutt_flushinp ();
2058         mutt_error (_("Can't edit message on newsserver."));
2059
2060         break;
2061       }
2062 #endif
2063
2064       if (option (OPTPGPAUTODEC)
2065           && (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED)))
2066         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
2067       mutt_edit_message (Context, tag ? NULL : CURHDR);
2068       menu->redraw = REDRAW_FULL;
2069
2070       break;
2071
2072     case OP_FORWARD_MESSAGE:
2073
2074       CHECK_MSGCOUNT;
2075       CHECK_VISIBLE;
2076       CHECK_ATTACH;
2077       ci_send_message (SENDFORWARD, NULL, NULL, Context, tag ? NULL : CURHDR);
2078       menu->redraw = REDRAW_FULL;
2079       break;
2080
2081
2082     case OP_FORGET_PASSPHRASE:
2083       crypt_forget_passphrase ();
2084       break;
2085
2086     case OP_GROUP_REPLY:
2087
2088       CHECK_MSGCOUNT;
2089       CHECK_VISIBLE;
2090       CHECK_ATTACH;
2091       ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, Context,
2092                        tag ? NULL : CURHDR);
2093       menu->redraw = REDRAW_FULL;
2094       break;
2095
2096     case OP_LIST_REPLY:
2097
2098       CHECK_ATTACH;
2099       CHECK_MSGCOUNT;
2100       CHECK_VISIBLE;
2101       ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, Context,
2102                        tag ? NULL : CURHDR);
2103       menu->redraw = REDRAW_FULL;
2104       break;
2105
2106     case OP_MAIL:
2107
2108       CHECK_ATTACH;
2109       ci_send_message (0, NULL, NULL, Context, NULL);
2110       menu->redraw = REDRAW_FULL;
2111       break;
2112
2113     case OP_MAIL_KEY:
2114       if (!(WithCrypto & APPLICATION_PGP))
2115         break;
2116       CHECK_ATTACH;
2117       ci_send_message (SENDKEY, NULL, NULL, NULL, NULL);
2118       menu->redraw = REDRAW_FULL;
2119       break;
2120
2121
2122     case OP_EXTRACT_KEYS:
2123       if (!WithCrypto)
2124         break;
2125       CHECK_MSGCOUNT;
2126       CHECK_VISIBLE;
2127       crypt_extract_keys_from_messages (tag ? NULL : CURHDR);
2128       menu->redraw = REDRAW_FULL;
2129       break;
2130
2131
2132     case OP_CHECK_TRADITIONAL:
2133       if (!(WithCrypto & APPLICATION_PGP))
2134         break;
2135       CHECK_MSGCOUNT;
2136       CHECK_VISIBLE;
2137       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
2138         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
2139
2140       if (menu->menu == MENU_PAGER) {
2141         op = OP_DISPLAY_MESSAGE;
2142         continue;
2143       }
2144       break;
2145
2146     case OP_PIPE:
2147
2148       CHECK_MSGCOUNT;
2149       CHECK_VISIBLE;
2150       mutt_pipe_message (tag ? NULL : CURHDR);
2151       MAYBE_REDRAW (menu->redraw);
2152       break;
2153
2154     case OP_PRINT:
2155
2156       CHECK_MSGCOUNT;
2157       CHECK_VISIBLE;
2158       mutt_print_message (tag ? NULL : CURHDR);
2159       break;
2160
2161     case OP_MAIN_READ_THREAD:
2162     case OP_MAIN_READ_SUBTHREAD:
2163
2164       CHECK_MSGCOUNT;
2165       CHECK_VISIBLE;
2166       CHECK_READONLY;
2167
2168 #ifdef USE_IMAP
2169       CHECK_IMAP_ACL (IMAP_ACL_SEEN);
2170 #endif
2171
2172       rc = mutt_thread_set_flag (CURHDR, M_READ, 1,
2173                                  op == OP_MAIN_READ_THREAD ? 0 : 1);
2174
2175       if (rc != -1) {
2176         if (option (OPTRESOLVE)) {
2177           if ((menu->current = (op == OP_MAIN_READ_THREAD ?
2178                                 mutt_next_thread (CURHDR) :
2179                                 mutt_next_subthread (CURHDR))) == -1)
2180             menu->current = menu->oldcurrent;
2181         }
2182         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2183       }
2184       break;
2185
2186     case OP_RECALL_MESSAGE:
2187
2188       CHECK_ATTACH;
2189       ci_send_message (SENDPOSTPONED, NULL, NULL, Context, NULL);
2190       menu->redraw = REDRAW_FULL;
2191       break;
2192
2193     case OP_RESEND:
2194
2195       CHECK_ATTACH;
2196       CHECK_MSGCOUNT;
2197       CHECK_VISIBLE;
2198
2199       if (tag) {
2200         for (j = 0; j < Context->vcount; j++) {
2201           if (Context->hdrs[Context->v2r[j]]->tagged)
2202             mutt_resend_message (NULL, Context,
2203                                  Context->hdrs[Context->v2r[j]]);
2204         }
2205       }
2206       else
2207         mutt_resend_message (NULL, Context, CURHDR);
2208
2209       menu->redraw = REDRAW_FULL;
2210       break;
2211
2212 #ifdef USE_NNTP
2213     case OP_POST:
2214     case OP_FOLLOWUP:
2215     case OP_FORWARD_TO_GROUP:
2216
2217       CHECK_ATTACH;
2218       if ((op == OP_FOLLOWUP || op == OP_FORWARD_TO_GROUP) &&
2219           Context && Context->msgcount == 0) {
2220         mutt_error (_("There are no messages."));
2221         sleep (2);
2222       }
2223       else if (op != OP_FOLLOWUP || !CURHDR->env->followup_to ||
2224                mutt_strcasecmp (CURHDR->env->followup_to, "poster") ||
2225                query_quadoption (OPT_FOLLOWUPTOPOSTER,
2226                                  _("Reply by mail as poster prefers?")) !=
2227                M_YES) {
2228         if (Context && Context->magic == M_NNTP
2229             && !((NNTP_DATA *) Context->data)->allowed
2230             && query_quadoption (OPT_TOMODERATED,
2231                                  _
2232                                  ("Posting to this group not allowed, may be moderated. Continue?"))
2233             != M_YES)
2234           break;
2235         if (op == OP_POST)
2236           ci_send_message (SENDNEWS, NULL, NULL, Context, NULL);
2237         else {
2238           CHECK_MSGCOUNT;
2239           if (op == OP_FOLLOWUP)
2240             ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL, Context,
2241                              tag ? NULL : CURHDR);
2242           else
2243             ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, Context,
2244                              tag ? NULL : CURHDR);
2245         }
2246         menu->redraw = REDRAW_FULL;
2247         break;
2248       }
2249 #endif
2250
2251     case OP_REPLY:
2252
2253       CHECK_ATTACH;
2254       CHECK_MSGCOUNT;
2255       CHECK_VISIBLE;
2256       ci_send_message (SENDREPLY, NULL, NULL, Context, tag ? NULL : CURHDR);
2257       menu->redraw = REDRAW_FULL;
2258       break;
2259
2260     case OP_SHELL_ESCAPE:
2261
2262       mutt_shell_escape ();
2263       MAYBE_REDRAW (menu->redraw);
2264       break;
2265
2266     case OP_TAG_THREAD:
2267     case OP_TAG_SUBTHREAD:
2268
2269       CHECK_MSGCOUNT;
2270       CHECK_VISIBLE;
2271       rc = mutt_thread_set_flag (CURHDR, M_TAG, !CURHDR->tagged,
2272                                  op == OP_TAG_THREAD ? 0 : 1);
2273
2274       if (rc != -1) {
2275         if (option (OPTRESOLVE)) {
2276           menu->current = mutt_next_thread (CURHDR);
2277
2278           if (menu->current == -1)
2279             menu->current = menu->oldcurrent;
2280         }
2281         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2282       }
2283       break;
2284
2285     case OP_UNDELETE:
2286
2287       CHECK_MSGCOUNT;
2288       CHECK_VISIBLE;
2289       CHECK_READONLY;
2290
2291 #ifdef USE_IMAP
2292       CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2293 #endif
2294
2295       if (tag) {
2296         mutt_tag_set_flag (M_DELETE, 0);
2297         mutt_tag_set_flag (M_PURGED, 0);
2298         menu->redraw = REDRAW_INDEX;
2299       }
2300       else {
2301         mutt_set_flag (Context, CURHDR, M_DELETE, 0);
2302         mutt_set_flag (Context, CURHDR, M_PURGED, 0);
2303         if (option (OPTRESOLVE) && menu->current < Context->vcount - 1) {
2304           menu->current++;
2305           menu->redraw = REDRAW_MOTION_RESYNCH;
2306         }
2307         else
2308           menu->redraw = REDRAW_CURRENT;
2309       }
2310       menu->redraw |= REDRAW_STATUS;
2311       break;
2312
2313     case OP_UNDELETE_THREAD:
2314     case OP_UNDELETE_SUBTHREAD:
2315
2316       CHECK_MSGCOUNT;
2317       CHECK_VISIBLE;
2318       CHECK_READONLY;
2319
2320 #ifdef USE_IMAP
2321       CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2322 #endif
2323
2324       rc = mutt_thread_set_flag (CURHDR, M_DELETE, 0,
2325                                  op == OP_UNDELETE_THREAD ? 0 : 1)
2326         + mutt_thread_set_flag (CURHDR, M_PURGED, 0,
2327                                 op == OP_UNDELETE_THREAD ? 0 : 1);
2328
2329       if (rc > -1) {
2330         if (option (OPTRESOLVE)) {
2331           if (op == OP_UNDELETE_THREAD)
2332             menu->current = mutt_next_thread (CURHDR);
2333           else
2334             menu->current = mutt_next_subthread (CURHDR);
2335
2336           if (menu->current == -1)
2337             menu->current = menu->oldcurrent;
2338         }
2339         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2340       }
2341       break;
2342
2343     case OP_VERSION:
2344       mutt_version ();
2345       break;
2346
2347     case OP_BUFFY_LIST:
2348       mutt_buffy_list ();
2349       menu->redraw = REDRAW_FULL;
2350       break;
2351
2352     case OP_VIEW_ATTACHMENTS:
2353       CHECK_MSGCOUNT;
2354       CHECK_VISIBLE;
2355       mutt_view_attachments (CURHDR);
2356       if (CURHDR->attach_del)
2357         Context->changed = 1;
2358       menu->redraw = REDRAW_FULL;
2359       break;
2360
2361     case OP_END_COND:
2362       break;
2363
2364     case OP_WHAT_KEY:
2365       mutt_what_key ();
2366       break;
2367
2368     case OP_SIDEBAR_SCROLL_UP:
2369     case OP_SIDEBAR_SCROLL_DOWN:
2370     case OP_SIDEBAR_NEXT:
2371     case OP_SIDEBAR_PREV:
2372     case OP_SIDEBAR_NEXT_NEW:
2373     case OP_SIDEBAR_PREV_NEW:
2374       scroll_sidebar (op, menu->menu);
2375       break;
2376     default:
2377       if (menu->menu == MENU_MAIN)
2378         km_error_key (MENU_MAIN);
2379     }
2380
2381     if (menu->menu == MENU_PAGER) {
2382       menu->menu = MENU_MAIN;
2383       menu->redraw = REDRAW_FULL;
2384 #if 0
2385       set_option (OPTWEED);     /* turn header weeding back on. */
2386 #endif
2387     }
2388
2389     if (done)
2390       break;
2391   }
2392
2393 #ifdef USE_IMAP
2394   /* Close all open IMAP connections */
2395   if (!attach_msg)
2396     imap_logout_all ();
2397 #endif
2398 #ifdef USE_NNTP
2399   /* Close all open NNTP connections */
2400   if (!attach_msg)
2401     nntp_logout_all ();
2402 #endif
2403
2404   mutt_menuDestroy (&menu);
2405   return (close);
2406 }
2407
2408 void mutt_set_header_color (CONTEXT * ctx, HEADER * curhdr)
2409 {
2410   COLOR_LINE *color;
2411
2412   if (!curhdr)
2413     return;
2414
2415   for (color = ColorIndexList; color; color = color->next)
2416     if (mutt_pattern_exec
2417         (color->color_pattern, M_MATCH_FULL_ADDRESS, ctx, curhdr)) {
2418       curhdr->pair = color->pair;
2419       return;
2420     }
2421   curhdr->pair = ColorDefs[MT_COLOR_NORMAL];
2422 }