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