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