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