140668cb3ce367aeabeee49d8ad08b7ecd8450aa
[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, option (OPTSTATUSONTOP) ? 0 : LINES - 2);
501         SETCOLOR(main_w, MT_COLOR_STATUS);
502         BKGDSET(main_w, MT_COLOR_STATUS);
503         wmove(main_w, option (OPTSTATUSONTOP) ? 0 : LINES - 2, 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
772       CHECK_MSGCOUNT;
773       CHECK_VISIBLE;
774       if (isdigit (LastKey))
775         mutt_ungetch (LastKey, 0);
776       buf[0] = 0;
777       if (mutt_get_field (_("Jump to message: "), buf, sizeof (buf), 0) != 0
778           || !buf[0])
779         break;
780
781       if (!isdigit ((unsigned char) buf[0])) {
782         mutt_error (_("Argument must be a message number."));
783
784         break;
785       }
786
787       i = atoi (buf);
788       if (i > 0 && i <= Context->msgcount) {
789         for (j = i - 1; j < Context->msgcount; j++) {
790           if (Context->hdrs[j]->virtual != -1)
791             break;
792         }
793         if (j >= Context->msgcount) {
794           for (j = i - 2; j >= 0; j--) {
795             if (Context->hdrs[j]->virtual != -1)
796               break;
797           }
798         }
799
800         if (j >= 0) {
801           menu->current = Context->hdrs[j]->virtual;
802           if (menu->menu == MENU_PAGER) {
803             op = OP_DISPLAY_MESSAGE;
804             continue;
805           }
806           else
807             menu->redraw = REDRAW_MOTION;
808         }
809         else
810           mutt_error (_("That message is not visible."));
811       }
812       else
813         mutt_error (_("Invalid message number."));
814
815       break;
816
817       /* --------------------------------------------------------------------
818        * `index' specific commands
819        */
820
821     case OP_MAIN_DELETE_PATTERN:
822
823       CHECK_MSGCOUNT;
824       CHECK_VISIBLE;
825       CHECK_READONLY;
826
827       CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
828
829       CHECK_ATTACH;
830       mutt_pattern_func (M_DELETE, _("Delete messages matching: "));
831       menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
832       break;
833
834     case OP_MAIN_FETCH_MAIL:
835
836       CHECK_ATTACH;
837       pop_fetch_mail ();
838       menu->redraw = REDRAW_FULL;
839       break;
840
841     case OP_HELP:
842
843       mutt_help (MENU_MAIN);
844       menu->redraw = REDRAW_FULL;
845       break;
846
847     case OP_MAIN_SHOW_LIMIT:
848       CHECK_IN_MAILBOX;
849       if (!Context->pattern)
850         mutt_message (_("No limit pattern is in effect."));
851
852       else {
853         char buffer[STRING];
854
855         /* i18n: ask for a limit to apply */
856         snprintf (buffer, sizeof (buffer), _("Limit: %s"), Context->pattern);
857         mutt_message ("%s", buffer);
858       }
859       break;
860
861     case OP_MAIN_LIMIT:
862     case OP_TOGGLE_READ:
863
864       CHECK_IN_MAILBOX;
865       menu->oldcurrent = (Context->vcount && menu->current >= 0
866                           && menu->current <
867                           Context->vcount) ? CURHDR->index : -1;
868       if (op == OP_TOGGLE_READ) {
869         char buffer[LONG_STRING];
870
871         if (m_strncmp (Context->pattern, "!~R!~D~s", 8) != 0) {
872           snprintf (buffer, sizeof (buffer), "!~R!~D~s%s",
873                     Context->pattern ? Context->pattern : ".*");
874           set_option (OPTHIDEREAD);
875         }
876         else {
877           m_strcpy(buf, sizeof(buf), Context->pattern + 8);
878           if (m_strncmp (buf, ".*", 2) == 0)
879             snprintf (buf, sizeof (buf), "~A");
880           unset_option (OPTHIDEREAD);
881         }
882         p_delete(&Context->pattern);
883         Context->pattern = m_strdup(buf);
884       }
885       if ((op == OP_TOGGLE_READ && mutt_pattern_func (M_LIMIT, NULL) == 0) ||
886           mutt_pattern_func (M_LIMIT, _("Limit to messages matching: ")) == 0)
887       {
888         if (menu->oldcurrent >= 0) {
889           /* try to find what used to be the current message */
890           menu->current = -1;
891           for (i = 0; i < Context->vcount; i++)
892             if (Context->hdrs[Context->v2r[i]]->index == menu->oldcurrent) {
893               menu->current = i;
894               break;
895             }
896           if (menu->current < 0)
897             menu->current = 0;
898         }
899         else
900           menu->current = 0;
901         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
902         if (Context->msgcount && (Sort & SORT_MASK) == SORT_THREADS)
903           mutt_draw_tree (Context);
904         menu->redraw = REDRAW_FULL;
905       }
906       if (Context->pattern)
907         mutt_message _("To view all messages, limit to \"all\".");
908       break;
909
910     case OP_QUIT:
911
912       closed = op;
913       if (attach_msg) {
914         done = 1;
915         break;
916       }
917
918       if (query_quadoption2(mod_core.quit, _("Quit Madmutt?")) == M_YES) {
919         int check;
920
921         oldcount = Context ? Context->msgcount : 0;
922
923         if (!Context
924             || (check = mx_close_mailbox (Context, &index_hint)) == 0)
925           done = 1;
926         else {
927           if (check == M_NEW_MAIL || check == M_REOPENED)
928             update_index (menu, Context, check, oldcount, index_hint);
929
930           menu->redraw = REDRAW_FULL;   /* new mail arrived? */
931           set_option (OPTSEARCHINVALID);
932         }
933       }
934       break;
935
936     case OP_REDRAW:
937       clearok (main_w, TRUE);
938       menu->redraw = REDRAW_FULL;
939       break;
940
941     case OP_SEARCH:
942     case OP_SEARCH_REVERSE:
943     case OP_SEARCH_NEXT:
944     case OP_SEARCH_OPPOSITE:
945
946       CHECK_MSGCOUNT;
947       CHECK_VISIBLE;
948       if ((menu->current = mutt_search_command (menu->current, op)) == -1)
949         menu->current = menu->oldcurrent;
950       else
951         menu->redraw = REDRAW_MOTION;
952       break;
953
954     case OP_SORT:
955     case OP_SORT_REVERSE:
956
957       if (mutt_select_sort ((op == OP_SORT_REVERSE)) == 0) {
958         if (Context && Context->msgcount) {
959           resort_index (menu);
960           set_option (OPTSEARCHINVALID);
961         }
962       }
963       break;
964
965     case OP_TAG:
966
967       CHECK_MSGCOUNT;
968       CHECK_VISIBLE;
969       if (tag && !option (OPTAUTOTAG)) {
970         for (j = 0; j < Context->vcount; j++)
971           mutt_set_flag (Context, Context->hdrs[Context->v2r[j]], M_TAG, 0);
972         menu->redraw = REDRAW_STATUS | REDRAW_INDEX;
973       }
974       else {
975         mutt_set_flag (Context, CURHDR, M_TAG, !CURHDR->tagged);
976         Context->last_tag = CURHDR->tagged ? CURHDR :
977           ((Context->last_tag == CURHDR && !CURHDR->tagged)
978            ? NULL : Context->last_tag);
979         menu->redraw = REDRAW_STATUS;
980         if (option (OPTRESOLVE) && menu->current < Context->vcount - 1) {
981           menu->current++;
982           menu->redraw |= REDRAW_MOTION_RESYNCH;
983         }
984         else
985           menu->redraw |= REDRAW_CURRENT;
986       }
987       break;
988
989     case OP_MAIN_TAG_PATTERN:
990
991       CHECK_MSGCOUNT;
992       CHECK_VISIBLE;
993       mutt_pattern_func (M_TAG, _("Tag messages matching: "));
994       menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
995       break;
996
997     case OP_MAIN_UNDELETE_PATTERN:
998
999       CHECK_MSGCOUNT;
1000       CHECK_VISIBLE;
1001       CHECK_READONLY;
1002
1003       CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
1004
1005       if (mutt_pattern_func (M_UNDELETE, _("Undelete messages matching: ")) ==
1006           0)
1007         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1008       break;
1009
1010     case OP_MAIN_UNTAG_PATTERN:
1011
1012       CHECK_MSGCOUNT;
1013       CHECK_VISIBLE;
1014       if (mutt_pattern_func (M_UNTAG, _("Untag messages matching: ")) == 0)
1015         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1016       break;
1017
1018       /* --------------------------------------------------------------------
1019        * The following operations can be performed inside of the pager.
1020        */
1021
1022     case OP_MAIN_IMAP_FETCH:
1023       if (Context->magic == M_IMAP)
1024         imap_check_mailbox (Context, &index_hint, 1);
1025       break;
1026
1027     case OP_MAIN_SYNC_FOLDER:
1028
1029       if (Context && !Context->msgcount)
1030         break;
1031
1032       CHECK_MSGCOUNT;
1033       CHECK_VISIBLE;
1034       CHECK_READONLY;
1035       {
1036         int oldvcount = Context->vcount;
1037         oldcount = Context->msgcount;
1038         int dcount = 0;
1039         int check;
1040
1041         /* calculate the number of messages _above_ the cursor,
1042          * so we can keep the cursor on the current message
1043          */
1044         for (j = 0; j <= menu->current; j++) {
1045           if (Context->hdrs[Context->v2r[j]]->deleted)
1046             dcount++;
1047         }
1048
1049         if ((check = mx_sync_mailbox (Context, &index_hint)) == 0) {
1050           if (Context->vcount != oldvcount)
1051             menu->current -= dcount;
1052           set_option (OPTSEARCHINVALID);
1053         }
1054         else if (check == M_NEW_MAIL || check == M_REOPENED)
1055           update_index (menu, Context, check, oldcount, index_hint);
1056
1057         /* 
1058          * do a sanity check even if mx_sync_mailbox failed.
1059          */
1060
1061         if (menu->current < 0 || menu->current >= Context->vcount)
1062           menu->current = ci_first_message ();
1063       }
1064
1065       /* check for a fatal error, or all messages deleted */
1066       if (!Context->path)
1067         p_delete(&Context);
1068
1069       /* if we were in the pager, redisplay the message */
1070       if (menu->menu == MENU_PAGER) {
1071         op = OP_DISPLAY_MESSAGE;
1072         continue;
1073       }
1074       else
1075         menu->redraw = REDRAW_FULL;
1076       break;
1077
1078     case OP_SIDEBAR_OPEN:
1079     case OP_MAIN_CHANGE_FOLDER:
1080     case OP_MAIN_CHANGE_FOLDER_READONLY:
1081 #ifdef USE_NNTP
1082     case OP_MAIN_CHANGE_GROUP:
1083     case OP_MAIN_CHANGE_GROUP_READONLY:
1084 #endif
1085       if (attach_msg || option (OPTREADONLY) ||
1086 #ifdef USE_NNTP
1087           op == OP_MAIN_CHANGE_GROUP_READONLY ||
1088 #endif
1089           op == OP_MAIN_CHANGE_FOLDER_READONLY)
1090         flags = M_READONLY;
1091       else
1092         flags = 0;
1093
1094       if (flags)
1095         cp = _("Open mailbox in read-only mode");
1096       else
1097         cp = _("Open mailbox");
1098
1099       buf[0] = '\0';
1100 #ifdef USE_NNTP
1101       unset_option (OPTNEWS);
1102       if (op == OP_MAIN_CHANGE_GROUP || op == OP_MAIN_CHANGE_GROUP_READONLY) {
1103         set_option (OPTNEWS);
1104         if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)))
1105           break;
1106         if (flags)
1107           cp = _("Open newsgroup in read-only mode");
1108         else
1109           cp = _("Open newsgroup");
1110         nntp_buffy (buf, sizeof (buf));
1111       }
1112       else
1113 #endif
1114       {
1115         if (Context && Context->path)
1116           m_strcpy(buf, sizeof(buf), Context->path);
1117         if (op != OP_SIDEBAR_OPEN)
1118           buffy_next (buf, sizeof (buf));
1119       }
1120
1121       if (op == OP_SIDEBAR_OPEN) {
1122         m_strcpy(buf, sizeof(buf), sidebar_get_current());
1123       }
1124       else if (mutt_enter_fname (cp, buf, sizeof (buf), &menu->redraw, 1) == -1) {
1125         if (menu->menu == MENU_PAGER) {
1126           op = OP_DISPLAY_MESSAGE;
1127           continue;
1128         } else
1129           break;
1130       }
1131       if (!buf[0]) {
1132         CLEARLINE(main_w, LINES - 1);
1133         break;
1134       }
1135
1136 #ifdef USE_NNTP
1137       if (option (OPTNEWS)) {
1138         unset_option (OPTNEWS);
1139         nntp_expand_path (buf, sizeof (buf), &CurrentNewsSrv->conn->account);
1140       }
1141       else
1142 #endif
1143         mutt_expand_path (buf, sizeof (buf));
1144       if (mx_get_magic (buf) <= 0) {
1145         mutt_error (_("%s is not a mailbox."), buf);
1146         break;
1147       }
1148       m_strreplace(&CurrentFolder, buf);
1149
1150       if (Context) {
1151         int check;
1152
1153         if (Context->cinfo && Context->realpath)
1154           m_strreplace(&LastFolder, Context->realpath);
1155         else
1156           m_strreplace(&LastFolder, Context->path);
1157         oldcount = Context ? Context->msgcount : 0;
1158
1159         if ((check = mx_close_mailbox (Context, &index_hint)) != 0) {
1160           if (check == M_NEW_MAIL || check == M_REOPENED)
1161             update_index (menu, Context, check, oldcount, index_hint);
1162
1163           set_option (OPTSEARCHINVALID);
1164           menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1165           break;
1166         }
1167         p_delete(&Context);
1168       }
1169
1170       mutt_sleep (0);
1171
1172       /* Set CurrentMenu to MENU_MAIN before executing any folder
1173        * hooks so that all the index menu functions are available to
1174        * the exec command.
1175        */
1176
1177       CurrentMenu = MENU_MAIN;
1178       mutt_folder_hook (buf);
1179
1180       if ((Context = mx_open_mailbox (buf, flags, NULL)) != NULL) {
1181         menu->current = ci_first_message ();
1182       }
1183       else
1184         menu->current = 0;
1185       sidebar_set_current (buf);
1186
1187       mutt_clear_error ();
1188       buffy_check (0);     /* force the buffy check after we have changed
1189                                    the folder */
1190       menu->redraw = REDRAW_FULL;
1191       set_option (OPTSEARCHINVALID);
1192       break;
1193
1194     case OP_DISPLAY_MESSAGE:
1195     case OP_DISPLAY_HEADERS:   /* don't weed the headers */
1196
1197       CHECK_MSGCOUNT;
1198       CHECK_VISIBLE;
1199       /*
1200        * toggle the weeding of headers so that a user can press the key
1201        * again while reading the message.
1202        */
1203       if (op == OP_DISPLAY_HEADERS)
1204         toggle_option (OPTWEED);
1205
1206       unset_option (OPTNEEDRESORT);
1207
1208       if ((Sort & SORT_MASK) == SORT_THREADS && CURHDR->collapsed) {
1209         mutt_uncollapse_thread (Context, CURHDR);
1210         mutt_set_virtual (Context);
1211         if (option (OPTUNCOLLAPSEJUMP))
1212           menu->current = mutt_thread_next_unread (Context, CURHDR);
1213       }
1214
1215       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
1216         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
1217
1218       if ((op = mutt_display_message (CURHDR)) == -1) {
1219         unset_option (OPTNEEDRESORT);
1220         break;
1221       }
1222
1223       menu->menu = MENU_PAGER;
1224       menu->oldcurrent = menu->current;
1225       continue;
1226
1227     case OP_EXIT:
1228
1229       closed = op;
1230       if (menu->menu == MENU_MAIN && attach_msg) {
1231         done = 1;
1232         break;
1233       }
1234
1235       if ((menu->menu == MENU_MAIN)
1236           && (query_quadoption2(mod_core.quit,
1237                                 _("Exit Madmutt without saving?")) == M_YES))
1238       {
1239         if (Context) {
1240           mx_fastclose_mailbox (Context);
1241           p_delete(&Context);
1242         }
1243         done = 1;
1244       }
1245       break;
1246
1247     case OP_EDIT_TYPE:
1248
1249       CHECK_MSGCOUNT;
1250       CHECK_VISIBLE;
1251       CHECK_ATTACH;
1252       mutt_edit_content_type (CURHDR, CURHDR->content, NULL);
1253       /* if we were in the pager, redisplay the message */
1254       if (menu->menu == MENU_PAGER) {
1255         op = OP_DISPLAY_MESSAGE;
1256         continue;
1257       }
1258       else
1259         menu->redraw = REDRAW_CURRENT;
1260       break;
1261
1262     case OP_MAIN_BREAK_THREAD:
1263
1264       CHECK_MSGCOUNT;
1265       CHECK_VISIBLE;
1266       CHECK_READONLY;
1267
1268       if ((Sort & SORT_MASK) != SORT_THREADS)
1269         mutt_error (_("Threading is not enabled."));
1270
1271       else {
1272         {
1273           HEADER *oldcur = CURHDR;
1274
1275           mutt_break_thread (CURHDR);
1276           mutt_sort_headers (Context, 1);
1277           menu->current = oldcur->virtual;
1278         }
1279
1280         Context->changed = 1;
1281         mutt_message _("Thread broken");
1282
1283         if (menu->menu == MENU_PAGER) {
1284           op = OP_DISPLAY_MESSAGE;
1285           continue;
1286         }
1287         else
1288           menu->redraw |= REDRAW_INDEX;
1289       }
1290       break;
1291
1292     case OP_MAIN_LINK_THREADS:
1293
1294       CHECK_MSGCOUNT;
1295       CHECK_VISIBLE;
1296       CHECK_READONLY;
1297
1298       if ((Sort & SORT_MASK) != SORT_THREADS)
1299         mutt_error (_("Threading is not enabled."));
1300
1301       else if (!CURHDR->env->message_id)
1302         mutt_error (_("No Message-ID: header available to link thread"));
1303
1304       else if (!tag && (!Context->last_tag || !Context->last_tag->tagged))
1305         mutt_error (_("First, please tag a message to be linked here"));
1306
1307       else {
1308         HEADER *oldcur = CURHDR;
1309
1310         if (mutt_link_threads (CURHDR, tag ? NULL : Context->last_tag,
1311                                Context)) {
1312           mutt_sort_headers (Context, 1);
1313           menu->current = oldcur->virtual;
1314
1315           Context->changed = 1;
1316           mutt_message _("Threads linked");
1317         }
1318         else
1319           mutt_error (_("No thread linked"));
1320       }
1321
1322       if (menu->menu == MENU_PAGER) {
1323         op = OP_DISPLAY_MESSAGE;
1324         continue;
1325       }
1326       else
1327         menu->redraw |= REDRAW_STATUS | REDRAW_INDEX;
1328       break;
1329
1330     case OP_MAIN_NEXT_UNDELETED:
1331
1332       CHECK_MSGCOUNT;
1333       CHECK_VISIBLE;
1334       if (menu->current >= Context->vcount - 1) {
1335         if (menu->menu == MENU_MAIN)
1336           mutt_error (_("You are on the last message."));
1337
1338         break;
1339       }
1340       if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1341         menu->current = menu->oldcurrent;
1342         if (menu->menu == MENU_MAIN)
1343           mutt_error (_("No undeleted messages."));
1344       }
1345       else 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_NEXT_ENTRY:
1354
1355       CHECK_MSGCOUNT;
1356       CHECK_VISIBLE;
1357       if (menu->current >= Context->vcount - 1) {
1358         if (menu->menu == MENU_MAIN)
1359           mutt_error (_("You are on the last message."));
1360
1361         break;
1362       }
1363       menu->current++;
1364       if (menu->menu == MENU_PAGER) {
1365         op = OP_DISPLAY_MESSAGE;
1366         continue;
1367       }
1368       else
1369         menu->redraw = REDRAW_MOTION;
1370       break;
1371
1372     case OP_MAIN_PREV_UNDELETED:
1373
1374       CHECK_MSGCOUNT;
1375       CHECK_VISIBLE;
1376       if (menu->current < 1) {
1377         mutt_error (_("You are on the first message."));
1378
1379         break;
1380       }
1381       if ((menu->current = ci_previous_undeleted (menu->current)) == -1) {
1382         menu->current = menu->oldcurrent;
1383         if (menu->menu == MENU_MAIN)
1384           mutt_error (_("No undeleted messages."));
1385       }
1386       else if (menu->menu == MENU_PAGER) {
1387         op = OP_DISPLAY_MESSAGE;
1388         continue;
1389       }
1390       else
1391         menu->redraw = REDRAW_MOTION;
1392       break;
1393
1394     case OP_PREV_ENTRY:
1395
1396       CHECK_MSGCOUNT;
1397       CHECK_VISIBLE;
1398       if (menu->current < 1) {
1399         if (menu->menu == MENU_MAIN)
1400           mutt_error (_("You are on the first message."));
1401
1402         break;
1403       }
1404       menu->current--;
1405       if (menu->menu == MENU_PAGER) {
1406         op = OP_DISPLAY_MESSAGE;
1407         continue;
1408       }
1409       else
1410         menu->redraw = REDRAW_MOTION;
1411       break;
1412
1413     case OP_DECRYPT_COPY:
1414     case OP_DECRYPT_SAVE:
1415     case OP_COPY_MESSAGE:
1416     case OP_SAVE:
1417     case OP_DECODE_COPY:
1418     case OP_DECODE_SAVE:
1419       CHECK_MSGCOUNT;
1420       CHECK_VISIBLE;
1421       if (mutt_save_message (tag ? NULL : CURHDR,
1422                              (op == OP_DECRYPT_SAVE) ||
1423                              (op == OP_SAVE) || (op == OP_DECODE_SAVE),
1424                              (op == OP_DECODE_SAVE) || (op == OP_DECODE_COPY),
1425                              (op == OP_DECRYPT_SAVE)
1426                              || (op == OP_DECRYPT_COPY)
1427                              || 0, &menu->redraw) == 0 && (op == OP_SAVE
1428                                                            || op ==
1429                                                            OP_DECODE_SAVE
1430                                                            || op ==
1431                                                            OP_DECRYPT_SAVE)
1432         ) {
1433         if (tag)
1434           menu->redraw |= REDRAW_INDEX;
1435         else if (option (OPTRESOLVE)) {
1436           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1437             menu->current = menu->oldcurrent;
1438             menu->redraw |= REDRAW_CURRENT;
1439           }
1440           else
1441             menu->redraw |= REDRAW_MOTION_RESYNCH;
1442         }
1443         else
1444           menu->redraw |= REDRAW_CURRENT;
1445       }
1446       break;
1447
1448     case OP_MAIN_NEXT_NEW:
1449     case OP_MAIN_NEXT_UNREAD:
1450     case OP_MAIN_PREV_NEW:
1451     case OP_MAIN_PREV_UNREAD:
1452     case OP_MAIN_NEXT_NEW_THEN_UNREAD:
1453     case OP_MAIN_PREV_NEW_THEN_UNREAD:
1454
1455       {
1456         int first_unread = -1;
1457         int first_new = -1;
1458
1459         CHECK_MSGCOUNT;
1460         CHECK_VISIBLE;
1461
1462         i = menu->current;
1463         menu->current = -1;
1464         for (j = 0; j != Context->vcount; j++) {
1465 #define CURHDRi Context->hdrs[Context->v2r[i]]
1466           if (op == OP_MAIN_NEXT_NEW || op == OP_MAIN_NEXT_UNREAD
1467               || op == OP_MAIN_NEXT_NEW_THEN_UNREAD) {
1468             i++;
1469             if (i > Context->vcount - 1) {
1470               mutt_message _("Search wrapped to top.");
1471
1472               i = 0;
1473             }
1474           }
1475           else {
1476             i--;
1477             if (i < 0) {
1478               mutt_message _("Search wrapped to bottom.");
1479
1480               i = Context->vcount - 1;
1481             }
1482           }
1483
1484           if (CURHDRi->collapsed && (Sort & SORT_MASK) == SORT_THREADS) {
1485             if (UNREAD (CURHDRi) && first_unread == -1)
1486               first_unread = i;
1487             if (UNREAD (CURHDRi) == 1 && first_new == -1)
1488               first_new = i;
1489           }
1490           else if ((!CURHDRi->deleted && !CURHDRi->read)) {
1491             if (first_unread == -1)
1492               first_unread = i;
1493             if ((!CURHDRi->old) && first_new == -1)
1494               first_new = i;
1495           }
1496
1497           if ((op == OP_MAIN_NEXT_UNREAD || op == OP_MAIN_PREV_UNREAD) &&
1498               first_unread != -1)
1499             break;
1500           if ((op == OP_MAIN_NEXT_NEW || op == OP_MAIN_PREV_NEW ||
1501                op == OP_MAIN_NEXT_NEW_THEN_UNREAD
1502                || op == OP_MAIN_PREV_NEW_THEN_UNREAD)
1503               && first_new != -1)
1504             break;
1505         }
1506 #undef CURHDRi
1507         if ((op == OP_MAIN_NEXT_NEW || op == OP_MAIN_PREV_NEW ||
1508              op == OP_MAIN_NEXT_NEW_THEN_UNREAD
1509              || op == OP_MAIN_PREV_NEW_THEN_UNREAD)
1510             && first_new != -1)
1511           menu->current = first_new;
1512         else if ((op == OP_MAIN_NEXT_UNREAD || op == OP_MAIN_PREV_UNREAD ||
1513                   op == OP_MAIN_NEXT_NEW_THEN_UNREAD
1514                   || op == OP_MAIN_PREV_NEW_THEN_UNREAD)
1515                  && first_unread != -1)
1516           menu->current = first_unread;
1517
1518         if (menu->current == -1) {
1519           menu->current = menu->oldcurrent;
1520           mutt_error ("%s%s.",
1521                       (op == OP_MAIN_NEXT_NEW
1522                        || op ==
1523                        OP_MAIN_PREV_NEW) ? _("No new messages") :
1524                       _("No unread messages"),
1525                       Context->pattern ? _(" in this limited view") : "");
1526         }
1527         else if (menu->menu == MENU_PAGER) {
1528           op = OP_DISPLAY_MESSAGE;
1529           continue;
1530         }
1531         else
1532           menu->redraw = REDRAW_MOTION;
1533         break;
1534       }
1535     case OP_FLAG_MESSAGE:
1536
1537       CHECK_MSGCOUNT;
1538       CHECK_VISIBLE;
1539       CHECK_READONLY;
1540
1541       CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
1542
1543       if (tag) {
1544         for (j = 0; j < Context->vcount; j++) {
1545           if (Context->hdrs[Context->v2r[j]]->tagged)
1546             mutt_set_flag (Context, Context->hdrs[Context->v2r[j]],
1547                            M_FLAG, !Context->hdrs[Context->v2r[j]]->flagged);
1548         }
1549
1550         menu->redraw |= REDRAW_INDEX;
1551       }
1552       else {
1553         mutt_set_flag (Context, CURHDR, M_FLAG, !CURHDR->flagged);
1554         if (option (OPTRESOLVE)) {
1555           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1556             menu->current = menu->oldcurrent;
1557             menu->redraw = REDRAW_CURRENT;
1558           }
1559           else
1560             menu->redraw = REDRAW_MOTION_RESYNCH;
1561         }
1562         else
1563           menu->redraw = REDRAW_CURRENT;
1564       }
1565       menu->redraw |= REDRAW_STATUS;
1566       break;
1567
1568     case OP_TOGGLE_NEW:
1569
1570       CHECK_MSGCOUNT;
1571       CHECK_VISIBLE;
1572       CHECK_READONLY;
1573
1574       CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
1575
1576       if (tag) {
1577         for (j = 0; j < Context->vcount; j++) {
1578           if (Context->hdrs[Context->v2r[j]]->tagged) {
1579             if (Context->hdrs[Context->v2r[j]]->read ||
1580                 Context->hdrs[Context->v2r[j]]->old)
1581               mutt_set_flag (Context, Context->hdrs[Context->v2r[j]], M_NEW,
1582                              1);
1583             else
1584               mutt_set_flag (Context, Context->hdrs[Context->v2r[j]], M_READ,
1585                              1);
1586           }
1587         }
1588         menu->redraw = REDRAW_STATUS | REDRAW_INDEX;
1589       }
1590       else {
1591         if (CURHDR->read || CURHDR->old)
1592           mutt_set_flag (Context, CURHDR, M_NEW, 1);
1593         else
1594           mutt_set_flag (Context, CURHDR, M_READ, 1);
1595
1596         if (option (OPTRESOLVE)) {
1597           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1598             menu->current = menu->oldcurrent;
1599             menu->redraw = REDRAW_CURRENT;
1600           }
1601           else
1602             menu->redraw = REDRAW_MOTION_RESYNCH;
1603         }
1604         else
1605           menu->redraw = REDRAW_CURRENT;
1606         menu->redraw |= REDRAW_STATUS;
1607       }
1608       break;
1609
1610     case OP_TOGGLE_WRITE:
1611
1612       CHECK_IN_MAILBOX;
1613       if (mx_toggle_write (Context) == 0)
1614         menu->redraw |= REDRAW_STATUS;
1615       break;
1616
1617     case OP_MAIN_NEXT_THREAD:
1618     case OP_MAIN_NEXT_SUBTHREAD:
1619     case OP_MAIN_PREV_THREAD:
1620     case OP_MAIN_PREV_SUBTHREAD:
1621
1622       CHECK_MSGCOUNT;
1623       CHECK_VISIBLE;
1624       switch (op) {
1625       case OP_MAIN_NEXT_THREAD:
1626         menu->current = mutt_next_thread (CURHDR);
1627         break;
1628
1629       case OP_MAIN_NEXT_SUBTHREAD:
1630         menu->current = mutt_next_subthread (CURHDR);
1631         break;
1632
1633       case OP_MAIN_PREV_THREAD:
1634         menu->current = mutt_previous_thread (CURHDR);
1635         break;
1636
1637       case OP_MAIN_PREV_SUBTHREAD:
1638         menu->current = mutt_previous_subthread (CURHDR);
1639         break;
1640       }
1641
1642       if (menu->current < 0) {
1643         menu->current = menu->oldcurrent;
1644         if (op == OP_MAIN_NEXT_THREAD || op == OP_MAIN_NEXT_SUBTHREAD)
1645           mutt_error (_("No more threads."));
1646
1647         else
1648           mutt_error (_("You are on the first thread."));
1649       }
1650       else if (menu->menu == MENU_PAGER) {
1651         op = OP_DISPLAY_MESSAGE;
1652         continue;
1653       }
1654       else
1655         menu->redraw = REDRAW_MOTION;
1656       break;
1657
1658     case OP_MAIN_PARENT_MESSAGE:
1659
1660       CHECK_MSGCOUNT;
1661       CHECK_VISIBLE;
1662
1663       if ((menu->current = mutt_parent_message (Context, CURHDR)) < 0) {
1664         menu->current = menu->oldcurrent;
1665       }
1666       else if (menu->menu == MENU_PAGER) {
1667         op = OP_DISPLAY_MESSAGE;
1668         continue;
1669       }
1670       else
1671         menu->redraw = REDRAW_MOTION;
1672       break;
1673
1674     case OP_MAIN_SET_FLAG:
1675     case OP_MAIN_CLEAR_FLAG:
1676
1677       CHECK_MSGCOUNT;
1678       CHECK_VISIBLE;
1679       CHECK_READONLY;
1680
1681       if (mutt_change_flag (tag ? NULL : CURHDR, (op == OP_MAIN_SET_FLAG)) == 0) {
1682         menu->redraw = REDRAW_STATUS;
1683         if (tag)
1684           menu->redraw |= REDRAW_INDEX;
1685         else if (option (OPTRESOLVE)) {
1686           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1687             menu->current = menu->oldcurrent;
1688             menu->redraw |= REDRAW_CURRENT;
1689           }
1690           else
1691             menu->redraw |= REDRAW_MOTION_RESYNCH;
1692         }
1693         else
1694           menu->redraw |= REDRAW_CURRENT;
1695       }
1696       break;
1697
1698     case OP_MAIN_COLLAPSE_THREAD:
1699       CHECK_MSGCOUNT;
1700       CHECK_VISIBLE;
1701
1702       if ((Sort & SORT_MASK) != SORT_THREADS) {
1703         mutt_error (_("Threading is not enabled."));
1704
1705         break;
1706       }
1707
1708       if (CURHDR->collapsed) {
1709         menu->current = mutt_uncollapse_thread (Context, CURHDR);
1710         mutt_set_virtual (Context);
1711         if (option (OPTUNCOLLAPSEJUMP))
1712           menu->current = mutt_thread_next_unread (Context, CURHDR);
1713       }
1714       else if (option (OPTCOLLAPSEUNREAD) || !UNREAD (CURHDR)) {
1715         menu->current = mutt_collapse_thread (Context, CURHDR);
1716         mutt_set_virtual (Context);
1717       }
1718       else {
1719         mutt_error (_("Thread contains unread messages."));
1720
1721         break;
1722       }
1723
1724       menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1725
1726       break;
1727
1728     case OP_MAIN_COLLAPSE_ALL:
1729       CHECK_MSGCOUNT;
1730       CHECK_VISIBLE;
1731
1732       if ((Sort & SORT_MASK) != SORT_THREADS) {
1733         mutt_error (_("Threading is not enabled."));
1734
1735         break;
1736       }
1737
1738       {
1739         HEADER *h, *base;
1740         THREAD *thread, *top;
1741         int final;
1742
1743         if (CURHDR->collapsed)
1744           final = mutt_uncollapse_thread (Context, CURHDR);
1745         else if (option (OPTCOLLAPSEUNREAD) || !UNREAD (CURHDR))
1746           final = mutt_collapse_thread (Context, CURHDR);
1747         else
1748           final = CURHDR->virtual;
1749
1750         base = Context->hdrs[Context->v2r[final]];
1751
1752         top = Context->tree;
1753         Context->collapsed = !Context->collapsed;
1754         while ((thread = top) != NULL) {
1755           while (!thread->message)
1756             thread = thread->child;
1757           h = thread->message;
1758
1759           if (h->collapsed != Context->collapsed) {
1760             if (h->collapsed)
1761               mutt_uncollapse_thread (Context, h);
1762             else if (option (OPTCOLLAPSEUNREAD) || !UNREAD (h))
1763               mutt_collapse_thread (Context, h);
1764           }
1765           top = top->next;
1766         }
1767
1768         mutt_set_virtual (Context);
1769         for (j = 0; j < Context->vcount; j++) {
1770           if (Context->hdrs[Context->v2r[j]]->index == base->index) {
1771             menu->current = j;
1772             break;
1773           }
1774         }
1775
1776         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1777       }
1778       break;
1779
1780       /* --------------------------------------------------------------------
1781        * These functions are invoked directly from the internal-pager
1782        */
1783
1784     case OP_BOUNCE_MESSAGE:
1785
1786       CHECK_ATTACH;
1787       CHECK_MSGCOUNT;
1788       CHECK_VISIBLE;
1789       ci_bounce_message (tag ? NULL : CURHDR, &menu->redraw);
1790       break;
1791
1792     case OP_CREATE_ALIAS:
1793
1794       mutt_create_alias (Context
1795                          && Context->vcount ? CURHDR->env : NULL, NULL);
1796       MAYBE_REDRAW (menu->redraw);
1797       menu->redraw |= REDRAW_CURRENT;
1798       break;
1799
1800     case OP_QUERY:
1801       CHECK_ATTACH;
1802       mutt_query_menu (NULL, 0);
1803       MAYBE_REDRAW (menu->redraw);
1804       break;
1805
1806     case OP_PURGE_MESSAGE:
1807     case OP_DELETE:
1808
1809       CHECK_MSGCOUNT;
1810       CHECK_VISIBLE;
1811       CHECK_READONLY;
1812
1813       CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
1814
1815       if (tag) {
1816         mutt_tag_set_flag (M_DELETE, 1);
1817         mutt_tag_set_flag (M_PURGED, (op != OP_PURGE_MESSAGE) ? 0 : 1);
1818         if (option (OPTDELETEUNTAG))
1819           mutt_tag_set_flag (M_TAG, 0);
1820         menu->redraw = REDRAW_INDEX;
1821       }
1822       else {
1823         mutt_set_flag (Context, CURHDR, M_DELETE, 1);
1824         mutt_set_flag (Context, CURHDR, M_PURGED,
1825                        (op != OP_PURGE_MESSAGE) ? 0 : 1);
1826         if (option (OPTDELETEUNTAG))
1827           mutt_set_flag (Context, CURHDR, M_TAG, 0);
1828         if (option (OPTRESOLVE)) {
1829           if ((menu->current = ci_next_undeleted (menu->current)) == -1) {
1830             menu->current = menu->oldcurrent;
1831             menu->redraw = REDRAW_CURRENT;
1832           }
1833           else if (menu->menu == MENU_PAGER) {
1834             op = OP_DISPLAY_MESSAGE;
1835             continue;
1836           }
1837           else
1838             menu->redraw |= REDRAW_MOTION_RESYNCH;
1839         }
1840         else
1841           menu->redraw = REDRAW_CURRENT;
1842       }
1843       menu->redraw |= REDRAW_STATUS;
1844       break;
1845
1846     case OP_DELETE_THREAD:
1847     case OP_DELETE_SUBTHREAD:
1848
1849       CHECK_MSGCOUNT;
1850       CHECK_VISIBLE;
1851       CHECK_READONLY;
1852
1853       CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
1854
1855       rc = mutt_thread_set_flag (CURHDR, M_DELETE, 1,
1856                                  op == OP_DELETE_THREAD ? 0 : 1);
1857
1858       if (rc != -1) {
1859         if (option (OPTDELETEUNTAG))
1860           mutt_thread_set_flag (CURHDR, M_TAG, 0,
1861                                 op == OP_DELETE_THREAD ? 0 : 1);
1862         if (option (OPTRESOLVE))
1863           if ((menu->current = ci_next_undeleted (menu->current)) == -1)
1864             menu->current = menu->oldcurrent;
1865         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1866       }
1867       break;
1868
1869 #ifdef USE_NNTP
1870     case OP_CATCHUP:
1871       if (Context && Context->magic == M_NNTP) {
1872         if (mutt_newsgroup_catchup (CurrentNewsSrv,
1873                                     ((nntp_data_t *) Context->data)->group))
1874           menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
1875       }
1876       break;
1877 #endif
1878
1879     case OP_DISPLAY_ADDRESS:
1880
1881       CHECK_MSGCOUNT;
1882       CHECK_VISIBLE;
1883       mutt_display_address (CURHDR->env);
1884       break;
1885
1886     case OP_ENTER_COMMAND:
1887
1888       CurrentMenu = MENU_MAIN;
1889       mutt_enter_command ();
1890       mutt_check_rescore (Context);
1891       if (option (OPTFORCEREDRAWINDEX))
1892         menu->redraw = REDRAW_FULL;
1893       unset_option (OPTFORCEREDRAWINDEX);
1894       unset_option (OPTFORCEREDRAWPAGER);
1895       break;
1896
1897     case OP_EDIT_MESSAGE:
1898
1899       CHECK_MSGCOUNT;
1900       CHECK_VISIBLE;
1901       CHECK_READONLY;
1902       CHECK_ATTACH;
1903
1904       CHECK_MX_ACL (Context, ACL_INSERT, _("Editing"));
1905
1906       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
1907         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
1908       mutt_edit_message (Context, tag ? NULL : CURHDR);
1909       menu->redraw = REDRAW_FULL;
1910
1911       break;
1912
1913     case OP_FORWARD_MESSAGE:
1914
1915       CHECK_MSGCOUNT;
1916       CHECK_VISIBLE;
1917       CHECK_ATTACH;
1918
1919       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
1920         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
1921       ci_send_message (SENDFORWARD, NULL, NULL, Context, tag ? NULL : CURHDR);
1922       menu->redraw = REDRAW_FULL;
1923       break;
1924
1925     case OP_GROUP_REPLY:
1926
1927       CHECK_MSGCOUNT;
1928       CHECK_VISIBLE;
1929       CHECK_ATTACH;
1930
1931       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
1932         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
1933
1934       ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, Context,
1935                        tag ? NULL : CURHDR);
1936       menu->redraw = REDRAW_FULL;
1937       break;
1938
1939     case OP_LIST_REPLY:
1940
1941       CHECK_ATTACH;
1942       CHECK_MSGCOUNT;
1943       CHECK_VISIBLE;
1944
1945       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
1946         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
1947
1948       ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, Context,
1949                        tag ? NULL : CURHDR);
1950       menu->redraw = REDRAW_FULL;
1951       break;
1952
1953     case OP_MAIL:
1954
1955       CHECK_ATTACH;
1956       ci_send_message (0, NULL, NULL, Context, NULL);
1957       menu->redraw = REDRAW_FULL;
1958       break;
1959
1960     case OP_EXTRACT_KEYS:
1961       CHECK_MSGCOUNT;
1962       CHECK_VISIBLE;
1963       crypt_extract_keys_from_messages (tag ? NULL : CURHDR);
1964       menu->redraw = REDRAW_FULL;
1965       break;
1966
1967
1968     case OP_CHECK_TRADITIONAL:
1969       CHECK_MSGCOUNT;
1970       CHECK_VISIBLE;
1971       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
1972         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
1973
1974       if (menu->menu == MENU_PAGER) {
1975         op = OP_DISPLAY_MESSAGE;
1976         continue;
1977       }
1978       break;
1979
1980     case OP_PIPE:
1981       CHECK_MSGCOUNT;
1982       CHECK_VISIBLE;
1983       mutt_pipe_message (tag ? NULL : CURHDR);
1984       MAYBE_REDRAW (menu->redraw);
1985       break;
1986
1987     case OP_PRINT:
1988       CHECK_MSGCOUNT;
1989       CHECK_VISIBLE;
1990       mutt_print_message (tag ? NULL : CURHDR);
1991       break;
1992
1993     case OP_MAIN_READ_THREAD:
1994     case OP_MAIN_READ_SUBTHREAD:
1995       CHECK_MSGCOUNT;
1996       CHECK_VISIBLE;
1997       CHECK_READONLY;
1998
1999       CHECK_MX_ACL (Context, ACL_SEEN, _("Marking as read"));
2000
2001       rc = mutt_thread_set_flag (CURHDR, M_READ, 1,
2002                                  op == OP_MAIN_READ_THREAD ? 0 : 1);
2003
2004       if (rc != -1) {
2005         if (option (OPTRESOLVE)) {
2006           if ((menu->current = (op == OP_MAIN_READ_THREAD ?
2007                                 mutt_next_thread (CURHDR) :
2008                                 mutt_next_subthread (CURHDR))) == -1)
2009             menu->current = menu->oldcurrent;
2010         }
2011         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2012       }
2013       break;
2014
2015     case OP_RECALL_MESSAGE:
2016
2017       CHECK_ATTACH;
2018       ci_send_message (SENDPOSTPONED, NULL, NULL, Context, NULL);
2019       menu->redraw = REDRAW_FULL;
2020       break;
2021
2022     case OP_RESEND:
2023
2024       CHECK_ATTACH;
2025       CHECK_MSGCOUNT;
2026       CHECK_VISIBLE;
2027
2028       if (tag) {
2029         for (j = 0; j < Context->vcount; j++) {
2030           if (Context->hdrs[Context->v2r[j]]->tagged)
2031             mutt_resend_message (NULL, Context,
2032                                  Context->hdrs[Context->v2r[j]]);
2033         }
2034       }
2035       else
2036         mutt_resend_message (NULL, Context, CURHDR);
2037
2038       menu->redraw = REDRAW_FULL;
2039       break;
2040
2041 #ifdef USE_NNTP
2042     case OP_POST:
2043     case OP_FOLLOWUP:
2044     case OP_FORWARD_TO_GROUP:
2045
2046       CHECK_ATTACH;
2047       if ((op == OP_FOLLOWUP || op == OP_FORWARD_TO_GROUP) &&
2048           Context && Context->msgcount == 0) {
2049         mutt_error (_("There are no messages."));
2050         sleep (2);
2051       }
2052       else if (op != OP_FOLLOWUP || !CURHDR->env->followup_to ||
2053                m_strcasecmp(CURHDR->env->followup_to, "poster") ||
2054                query_quadoption (OPT_FOLLOWUPTOPOSTER,
2055                                  _("Reply by mail as poster prefers?")) !=
2056                M_YES) {
2057         if (Context && Context->magic == M_NNTP
2058             && !((nntp_data_t *) Context->data)->allowed
2059             && query_quadoption (OPT_TOMODERATED,
2060                                  _
2061                                  ("Posting to this group not allowed, may be moderated. Continue?"))
2062             != M_YES)
2063           break;
2064         if (op == OP_POST)
2065           ci_send_message (SENDNEWS, NULL, NULL, Context, NULL);
2066         else {
2067           CHECK_MSGCOUNT;
2068           if (op == OP_FOLLOWUP)
2069             ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL, Context,
2070                              tag ? NULL : CURHDR);
2071           else
2072             ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, Context,
2073                              tag ? NULL : CURHDR);
2074         }
2075         menu->redraw = REDRAW_FULL;
2076         break;
2077       }
2078 #endif
2079
2080     case OP_REPLY:
2081
2082       CHECK_ATTACH;
2083       CHECK_MSGCOUNT;
2084       CHECK_VISIBLE;
2085
2086       if (tag || !(CURHDR->security & PGP_TRADITIONAL_CHECKED))
2087         mutt_check_traditional_pgp (tag ? NULL : CURHDR, &menu->redraw);
2088
2089       ci_send_message (SENDREPLY, NULL, NULL, Context, tag ? NULL : CURHDR);
2090       menu->redraw = REDRAW_FULL;
2091       break;
2092
2093     case OP_SHELL_ESCAPE:
2094
2095       mutt_shell_escape ();
2096       MAYBE_REDRAW (menu->redraw);
2097       break;
2098
2099     case OP_TAG_THREAD:
2100     case OP_TAG_SUBTHREAD:
2101
2102       CHECK_MSGCOUNT;
2103       CHECK_VISIBLE;
2104       rc = mutt_thread_set_flag (CURHDR, M_TAG, !CURHDR->tagged,
2105                                  op == OP_TAG_THREAD ? 0 : 1);
2106
2107       if (rc != -1) {
2108         if (option (OPTRESOLVE)) {
2109           menu->current = mutt_next_thread (CURHDR);
2110
2111           if (menu->current == -1)
2112             menu->current = menu->oldcurrent;
2113         }
2114         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2115       }
2116       break;
2117
2118     case OP_UNDELETE:
2119
2120       CHECK_MSGCOUNT;
2121       CHECK_VISIBLE;
2122       CHECK_READONLY;
2123
2124       CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2125
2126       if (tag) {
2127         mutt_tag_set_flag (M_DELETE, 0);
2128         mutt_tag_set_flag (M_PURGED, 0);
2129         menu->redraw = REDRAW_INDEX;
2130       }
2131       else {
2132         mutt_set_flag (Context, CURHDR, M_DELETE, 0);
2133         mutt_set_flag (Context, CURHDR, M_PURGED, 0);
2134         if (option (OPTRESOLVE) && menu->current < Context->vcount - 1) {
2135           menu->current++;
2136           menu->redraw = REDRAW_MOTION_RESYNCH;
2137         }
2138         else
2139           menu->redraw = REDRAW_CURRENT;
2140       }
2141       menu->redraw |= REDRAW_STATUS;
2142       break;
2143
2144     case OP_UNDELETE_THREAD:
2145     case OP_UNDELETE_SUBTHREAD:
2146
2147       CHECK_MSGCOUNT;
2148       CHECK_VISIBLE;
2149       CHECK_READONLY;
2150
2151       CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2152
2153       rc = mutt_thread_set_flag (CURHDR, M_DELETE, 0,
2154                                  op == OP_UNDELETE_THREAD ? 0 : 1)
2155         + mutt_thread_set_flag (CURHDR, M_PURGED, 0,
2156                                 op == OP_UNDELETE_THREAD ? 0 : 1);
2157
2158       if (rc > -1) {
2159         if (option (OPTRESOLVE)) {
2160           if (op == OP_UNDELETE_THREAD)
2161             menu->current = mutt_next_thread (CURHDR);
2162           else
2163             menu->current = mutt_next_subthread (CURHDR);
2164
2165           if (menu->current == -1)
2166             menu->current = menu->oldcurrent;
2167         }
2168         menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
2169       }
2170       break;
2171
2172     case OP_VERSION:
2173       mutt_version ();
2174       break;
2175
2176     case OP_BUFFY_LIST:
2177       if (option (OPTFORCEBUFFYCHECK))
2178         buffy_check (1);
2179       buffy_list ();
2180       menu->redraw = REDRAW_FULL;
2181       break;
2182
2183     case OP_VIEW_ATTACHMENTS:
2184       CHECK_MSGCOUNT;
2185       CHECK_VISIBLE;
2186       mutt_view_attachments (CURHDR);
2187       if (CURHDR->attach_del)
2188         Context->changed = 1;
2189       menu->redraw = REDRAW_FULL;
2190       break;
2191
2192     case OP_END_COND:
2193       break;
2194
2195     case OP_WHAT_KEY:
2196       mutt_what_key ();
2197       break;
2198
2199     case OP_SIDEBAR_SCROLL_UP:
2200     case OP_SIDEBAR_SCROLL_DOWN:
2201     case OP_SIDEBAR_NEXT:
2202     case OP_SIDEBAR_PREV:
2203     case OP_SIDEBAR_NEXT_NEW:
2204     case OP_SIDEBAR_PREV_NEW:
2205       sidebar_scroll (op);
2206       break;
2207     default:
2208       if (menu->menu == MENU_MAIN)
2209         km_error_key (MENU_MAIN);
2210     }
2211
2212     if (menu->menu == MENU_PAGER) {
2213       menu->menu = MENU_MAIN;
2214       menu->redraw = REDRAW_FULL;
2215     }
2216
2217     if (done)
2218       break;
2219   }
2220
2221   if (!attach_msg) {
2222   /* Close all open IMAP connections */
2223     imap_logout_all ();
2224 #ifdef USE_NNTP
2225   /* Close all open NNTP connections */
2226     nntp_logout_all ();
2227 #endif
2228   }
2229
2230   mutt_menuDestroy (&menu);
2231   return (closed);
2232 }
2233
2234 void mutt_set_header_color (CONTEXT * ctx, HEADER * curhdr)
2235 {
2236   COLOR_LINE *color;
2237
2238   if (!curhdr)
2239     return;
2240
2241   for (color = ColorIndexList; color; color = color->next)
2242     if (mutt_pattern_exec
2243         (color->color_pattern, M_MATCH_FULL_ADDRESS, ctx, curhdr)) {
2244       curhdr->pair = color->pair;
2245       return;
2246     }
2247   curhdr->pair = ColorDefs[MT_COLOR_NORMAL];
2248 }