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