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