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