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