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