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