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