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