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