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