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