eee9b8393290123f17fe8aa42e11db5e0471a45b
[apps/madmutt.git] / pager.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4  *
5  * Parts were written/modified by:
6  * Nico Golde <nico@ngolde.de>
7  * Andreas Krennmair <ak@synflood.at>
8  *
9  * This file is part of mutt-ng, see http://www.muttng.org/.
10  * It's licensed under the GNU General Public License,
11  * please see the file GPL in the top level source directory.
12  */
13
14 #include <lib-lib/lib-lib.h>
15
16 #include <lib-ui/lib-ui.h>
17 #include <lib-ui/enter.h>
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
20 #include <lib-mx/mx.h>
21
22 #include "mutt.h"
23 #include "crypt.h"
24 #include "alias.h"
25 #include "keymap.h"
26 #include "sort.h"
27 #include "pager.h"
28 #include "attach.h"
29 #include "recvattach.h"
30 #include "charset.h"
31 #include "buffy.h"
32
33 #include <imap/imap_private.h>
34
35 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
36
37 #define IsAttach(x)      (x && (x)->bdy)
38 #define IsRecvAttach(x)  (x && (x)->bdy && (x)->fp)
39 #define IsSendAttach(x)  (x && (x)->bdy && !(x)->fp)
40 #define IsMsgAttach(x)   (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
41 #define IsHeader(x)      (x && (x)->hdr && !(x)->bdy)
42
43 /* hack to return to position when returning from index to same message */
44 static int TopLine = 0;
45 static HEADER *OldHdr = NULL;
46
47 #define CHECK_MODE(x) \
48     if (!(x)) {                                         \
49         mutt_flushinp();                                \
50         mutt_error _("Not available in this menu.");    \
51         break;                                          \
52     }
53
54 #define CHECK_READONLY \
55     if (Context->readonly) {                            \
56         mutt_flushinp();                                \
57         mutt_error _("Mailbox is read-only.");          \
58         break;                                          \
59     }
60
61 #define CHECK_ATTACH \
62     if (option(OPTATTACHMSG)) {                         \
63         mutt_flushinp();                                \
64         mutt_error _("Function not permitted in attach-message mode."); \
65         break;                                          \
66     }
67
68 struct q_class_t {
69   int length;
70   int index;
71   int color;
72   char *prefix;
73   struct q_class_t *next, *prev;
74   struct q_class_t *down, *up;
75 };
76
77 struct syntax_t {
78   int color;
79   int first;
80   int last;
81 };
82
83 struct line_t {
84   off_t offset;
85   short type;
86   short continuation;
87   short chunks;
88   short search_cnt;
89   struct syntax_t *syntax;
90   struct syntax_t *search;
91   struct q_class_t *quote;
92 };
93
94 #define ANSI_OFF       (1<<0)
95 #define ANSI_BLINK     (1<<1)
96 #define ANSI_BOLD      (1<<2)
97 #define ANSI_UNDERLINE (1<<3)
98 #define ANSI_REVERSE   (1<<4)
99 #define ANSI_COLOR     (1<<5)
100
101 typedef struct _ansi_attr {
102   int attr;
103   int fg;
104   int bg;
105   int pair;
106 } ansi_attr;
107
108 static short InHelp = 0;
109
110 static struct resize {
111   int line;
112   int SearchCompiled;
113   int SearchBack;
114 } *Resize = NULL;
115
116 #define NumSigLines 4
117
118 static int check_sig(const char *s, struct line_t *info, int n)
119 {
120     int count = 0;
121
122     while (n > 0 && count <= NumSigLines) {
123         if (info[n].type != MT_COLOR_SIGNATURE)
124             break;
125         count++;
126         n--;
127     }
128
129     if (count == 0)
130         return -1;
131
132     if (count > NumSigLines) {
133         /* check for a blank line */
134         s = skipspaces(s);
135         return *s ? -1 : 0;
136     }
137
138     return 0;
139 }
140
141 static void
142 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
143                int special, ansi_attr * a)
144 {
145   int def_color;                /* color without syntax hilight */
146   int color;                    /* final color */
147   static int last_color;        /* last color set */
148   int search = 0, i, m;
149
150   if (!cnt)
151     last_color = -1;            /* force wattrset(main_w) */
152
153   if (lineInfo[n].continuation) {
154     if (!cnt && option (OPTMARKERS)) {
155       SETCOLOR(main_w, MT_COLOR_MARKERS);
156       waddch(main_w, '+');
157       last_color = ColorDefs[MT_COLOR_MARKERS];
158     }
159     m = (lineInfo[n].syntax)[0].first;
160     cnt += (lineInfo[n].syntax)[0].last;
161   } else {
162     m = n;
163   }
164
165   if (!(flags & M_SHOWCOLOR))
166     def_color = ColorDefs[MT_COLOR_NORMAL];
167   else if (lineInfo[m].type == MT_COLOR_HEADER)
168     def_color = (lineInfo[m].syntax)[0].color;
169   else
170     def_color = ColorDefs[lineInfo[m].type];
171
172   if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
173     struct q_class_t *class = lineInfo[m].quote;
174
175     if (class) {
176       def_color = class->color;
177
178       while (class && class->length > cnt) {
179         def_color = class->color;
180         class = class->up;
181       }
182     }
183   }
184
185   color = def_color;
186   if (flags & M_SHOWCOLOR) {
187     for (i = 0; i < lineInfo[m].chunks; i++) {
188       /* we assume the chunks are sorted */
189       if (cnt > (lineInfo[m].syntax)[i].last)
190         continue;
191       if (cnt < (lineInfo[m].syntax)[i].first)
192         break;
193       if (cnt != (lineInfo[m].syntax)[i].last) {
194         color = (lineInfo[m].syntax)[i].color;
195         break;
196       }
197       /* don't break here, as cnt might be in the next chunk as well */
198     }
199   }
200
201   if (flags & M_SEARCH) {
202     for (i = 0; i < lineInfo[m].search_cnt; i++) {
203       if (cnt > (lineInfo[m].search)[i].last)
204         continue;
205       if (cnt < (lineInfo[m].search)[i].first)
206         break;
207       if (cnt != (lineInfo[m].search)[i].last) {
208         color = ColorDefs[MT_COLOR_SEARCH];
209         search = 1;
210         break;
211       }
212     }
213   }
214
215   /* handle "special" bold & underlined characters */
216   if (special || a->attr) {
217     if ((a->attr & ANSI_COLOR)) {
218       if (a->pair == -1)
219         a->pair = mutt_alloc_color (a->fg, a->bg);
220       color = a->pair;
221       if (a->attr & ANSI_BOLD)
222         color |= A_BOLD;
223     } else
224     if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
225       if (ColorDefs[MT_COLOR_BOLD] && !search)
226         color = ColorDefs[MT_COLOR_BOLD];
227       else
228         color ^= A_BOLD;
229     }
230     if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
231       if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
232         color = ColorDefs[MT_COLOR_UNDERLINE];
233       else
234         color ^= A_UNDERLINE;
235     }
236     else if (a->attr & ANSI_REVERSE) {
237       color ^= A_REVERSE;
238     }
239     else if (a->attr & ANSI_BLINK) {
240       color ^= A_BLINK;
241     }
242     else if (a->attr & ANSI_OFF) {
243       a->attr = 0;
244     }
245   }
246
247   if (color != last_color) {
248     wattrset(main_w, color);
249     last_color = color;
250   }
251 }
252
253 static void append_line (struct line_t *lineInfo, int n, int cnt)
254 {
255   int m;
256
257   lineInfo[n + 1].type = lineInfo[n].type;
258   lineInfo[n + 1].syntax[0].color = lineInfo[n].syntax[0].color;
259   lineInfo[n + 1].continuation = 1;
260
261   /* find the real start of the line */
262   for (m = n; m >= 0; m--)
263       if (lineInfo[m].continuation == 0)
264           break;
265
266   (lineInfo[n + 1].syntax)[0].first = m;
267   (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
268       cnt + (lineInfo[n].syntax)[0].last : cnt;
269 }
270
271 static void new_class_color (struct q_class_t *class, int *q_level)
272 {
273     class->index = (*q_level)++;
274     class->color = ColorQuote[class->index % ColorQuoteUsed];
275 }
276
277 static void
278 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
279                     int lindex, int *q_level)
280 {
281     struct q_class_t *q_list;
282
283     q_list = QuoteList;
284     new_class->index = -1;
285
286     while (q_list) {
287         if (q_list->index >= lindex) {
288             q_list->index++;
289             q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
290         }
291         if (q_list->down)
292             q_list = q_list->down;
293         else if (q_list->next)
294             q_list = q_list->next;
295         else {
296             while (!q_list->next) {
297                 q_list = q_list->up;
298                 if (q_list == NULL)
299                     break;
300             }
301             if (q_list)
302                 q_list = q_list->next;
303         }
304     }
305
306     new_class->index = lindex;
307     new_class->color = ColorQuote[lindex % ColorQuoteUsed];
308     (*q_level)++;
309 }
310
311 static void cleanup_quote(struct q_class_t **QuoteList)
312 {
313     while (*QuoteList) {
314         struct q_class_t *ptr = (*QuoteList)->next;
315         cleanup_quote(&(*QuoteList)->down);
316         p_delete(&(*QuoteList)->prefix);
317         p_delete(QuoteList);
318         *QuoteList = ptr;
319     }
320 }
321
322 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
323                                          const char *qptr, int length,
324                                          int *force_redraw, int *q_level)
325 {
326     struct q_class_t *q_list = *QuoteList;
327     struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
328     char *tail_qptr;
329     int offset, tail_lng;
330     int lindex = -1;
331
332     if (ColorQuoteUsed <= 1) {
333         /* not much point in classifying quotes... */
334
335         if (*QuoteList == NULL) {
336             class = p_new(struct q_class_t, 1);
337             class->color = ColorQuote[0];
338             *QuoteList = class;
339         }
340         return (*QuoteList);
341     }
342
343     /* Did I mention how much I like emulating Lisp in C? */
344
345     /* classify quoting prefix */
346     while (q_list) {
347         if (length <= q_list->length) {
348             /* case 1: check the top level nodes */
349
350             if (m_strncmp(qptr, q_list->prefix, length) == 0) {
351                 if (length == q_list->length)
352                     return q_list;        /* same prefix: return the current class */
353
354                 /* found shorter prefix */
355                 if (tmp == NULL) {
356                     /* add a node above q_list */
357                     tmp = p_new(struct q_class_t, 1);
358                     tmp->prefix = p_dupstr(qptr, length);
359                     tmp->length = length;
360
361                     /* replace q_list by tmp in the top level list */
362                     if (q_list->next) {
363                         tmp->next = q_list->next;
364                         q_list->next->prev = tmp;
365                     }
366                     if (q_list->prev) {
367                         tmp->prev = q_list->prev;
368                         q_list->prev->next = tmp;
369                     }
370
371                     /* make q_list a child of tmp */
372                     tmp->down = q_list;
373                     q_list->up = tmp;
374
375                     /* q_list has no siblings for now */
376                     q_list->next = NULL;
377                     q_list->prev = NULL;
378
379                     /* update the root if necessary */
380                     if (q_list == *QuoteList)
381                         *QuoteList = tmp;
382
383                     lindex = q_list->index;
384
385                     /* tmp should be the return class too */
386                     class = tmp;
387
388                     /* next class to test; if tmp is a shorter prefix for another
389                      * node, that node can only be in the top level list, so don't
390                      * go down after this point
391                      */
392                     q_list = tmp->next;
393                 } else {
394                     /* found another branch for which tmp is a shorter prefix */
395
396                     /* save the next sibling for later */
397                     save = q_list->next;
398
399                     /* unlink q_list from the top level list */
400                     if (q_list->next)
401                         q_list->next->prev = q_list->prev;
402                     if (q_list->prev)
403                         q_list->prev->next = q_list->next;
404
405                     /* at this point, we have a tmp->down; link q_list to it */
406                     ptr = tmp->down;
407                     /* sibling order is important here, q_list should be linked last */
408                     while (ptr->next)
409                         ptr = ptr->next;
410                     ptr->next = q_list;
411                     q_list->next = NULL;
412                     q_list->prev = ptr;
413                     q_list->up = tmp;
414
415                     lindex = q_list->index;
416
417                     /* next class to test; as above, we shouldn't go down */
418                     q_list = save;
419                 }
420
421                 /* we found a shorter prefix, so certain quotes have changed classes */
422                 *force_redraw = 1;
423                 continue;
424             } else {
425                 /* shorter, but not a substring of the current class: try next */
426                 q_list = q_list->next;
427                 continue;
428             }
429         } else {
430             /* case 2: try subclassing the current top level node */
431
432             /* tmp != NULL means we already found a shorter prefix at case 1 */
433             if (tmp == NULL
434                 && m_strncmp(qptr, q_list->prefix, q_list->length) == 0) {
435                 /* ok, it's a subclass somewhere on this branch */
436
437                 ptr = q_list;
438                 offset = q_list->length;
439
440                 q_list = q_list->down;
441                 tail_lng = length - offset;
442                 tail_qptr = (char *) qptr + offset;
443
444                 while (q_list) {
445                     if (length <= q_list->length) {
446                         if (m_strncmp(tail_qptr, (q_list->prefix) + offset, tail_lng)
447                             == 0) {
448                             /* same prefix: return the current class */
449                             if (length == q_list->length)
450                                 return q_list;
451
452                             /* found shorter common prefix */
453                             if (tmp == NULL) {
454                                 /* add a node above q_list */
455                                 tmp = p_new(struct q_class_t, 1);
456                                 tmp->prefix = p_dupstr(qptr, length);
457                                 tmp->length = length;
458
459                                 /* replace q_list by tmp */
460                                 if (q_list->next) {
461                                     tmp->next = q_list->next;
462                                     q_list->next->prev = tmp;
463                                 }
464                                 if (q_list->prev) {
465                                     tmp->prev = q_list->prev;
466                                     q_list->prev->next = tmp;
467                                 }
468
469                                 /* make q_list a child of tmp */
470                                 tmp->down = q_list;
471                                 tmp->up = q_list->up;
472                                 q_list->up = tmp;
473                                 if (tmp->up->down == q_list)
474                                     tmp->up->down = tmp;
475
476                                 /* q_list has no siblings */
477                                 q_list->next = NULL;
478                                 q_list->prev = NULL;
479
480                                 lindex = q_list->index;
481
482                                 /* tmp should be the return class too */
483                                 class = tmp;
484
485                                 /* next class to test */
486                                 q_list = tmp->next;
487                             } else {
488                                 /* found another branch for which tmp is a shorter prefix */
489
490                                 /* save the next sibling for later */
491                                 save = q_list->next;
492
493                                 /* unlink q_list from the top level list */
494                                 if (q_list->next)
495                                     q_list->next->prev = q_list->prev;
496                                 if (q_list->prev)
497                                     q_list->prev->next = q_list->next;
498
499                                 /* at this point, we have a tmp->down; link q_list to it */
500                                 ptr = tmp->down;
501                                 while (ptr->next)
502                                     ptr = ptr->next;
503                                 ptr->next = q_list;
504                                 q_list->next = NULL;
505                                 q_list->prev = ptr;
506                                 q_list->up = tmp;
507
508                                 lindex = q_list->index;
509
510                                 /* next class to test */
511                                 q_list = save;
512                             }
513
514                             /* we found a shorter prefix, so we need a redraw */
515                             *force_redraw = 1;
516                             continue;
517                         } else {
518                             q_list = q_list->next;
519                             continue;
520                         }
521                     } else {
522                         /* longer than the current prefix: try subclassing it */
523                         if (tmp == NULL
524                             && m_strncmp(tail_qptr, (q_list->prefix) + offset,
525                                          q_list->length - offset) == 0) {
526                             /* still a subclass: go down one level */
527                             ptr = q_list;
528                             offset = q_list->length;
529
530                             q_list = q_list->down;
531                             tail_lng = length - offset;
532                             tail_qptr = (char *) qptr + offset;
533
534                             continue;
535                         } else {
536                             /* nope, try the next prefix */
537                             q_list = q_list->next;
538                             continue;
539                         }
540                     }
541                 }
542
543                 /* still not found so far: add it as a sibling to the current node */
544                 if (class == NULL) {
545                     tmp = p_new(struct q_class_t, 1);
546                     tmp->prefix = p_dupstr(qptr, length);
547                     tmp->length = length;
548
549                     if (ptr->down) {
550                         tmp->next = ptr->down;
551                         ptr->down->prev = tmp;
552                     }
553                     ptr->down = tmp;
554                     tmp->up = ptr;
555
556                     new_class_color (tmp, q_level);
557
558                     return tmp;
559                 } else {
560                     if (lindex != -1)
561                         shift_class_colors (*QuoteList, tmp, lindex, q_level);
562
563                     return class;
564                 }
565             } else {
566                 /* nope, try the next prefix */
567                 q_list = q_list->next;
568                 continue;
569             }
570         }
571     }
572
573     if (class == NULL) {
574         /* not found so far: add it as a top level class */
575         class = p_new(struct q_class_t, 1);
576         class->prefix = p_dupstr(qptr, length);
577         class->length = length;
578         new_class_color (class, q_level);
579
580         if (*QuoteList) {
581             class->next = *QuoteList;
582             (*QuoteList)->prev = class;
583         }
584         *QuoteList = class;
585     }
586
587     if (lindex != -1)
588         shift_class_colors (*QuoteList, tmp, lindex, q_level);
589
590     return class;
591 }
592
593 static int check_attachment_marker (char *);
594
595 static void
596 resolve_types (char *buf, char *rawbuf, struct line_t *lineInfo, int n, int last,
597                struct q_class_t **QuoteList, int *q_level, int *force_redraw,
598                int q_classify)
599 {
600     COLOR_LINE *color_line;
601     regmatch_t pmatch[1], smatch[1];
602     int found, offset, null_rx, i;
603
604     if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
605         if (buf[0] == '\n') {
606             lineInfo[n].type = MT_COLOR_NORMAL;
607         }
608         else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
609             lineInfo[n].type = lineInfo[n - 1].type;  /* wrapped line */
610             (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
611         } else {
612             lineInfo[n].type = MT_COLOR_HDEFAULT;
613             color_line = ColorHdrList;
614             while (color_line) {
615                 if (REGEXEC (&color_line->rx, buf) == 0) {
616                     lineInfo[n].type = MT_COLOR_HEADER;
617                     lineInfo[n].syntax[0].color = color_line->pair;
618                     break;
619                 }
620                 color_line = color_line->next;
621             }
622         }
623     }
624     else if (m_strncmp("\033[0m", rawbuf, 4) == 0)       /* a little hack... */
625         lineInfo[n].type = MT_COLOR_NORMAL;
626     else if (check_attachment_marker ((char *) rawbuf) == 0)
627         lineInfo[n].type = MT_COLOR_ATTACHMENT;
628     else if (m_strcmp("-- \n", buf) == 0
629              || m_strcmp("-- \r\n", buf) == 0) {
630         i = n + 1;
631
632         lineInfo[n].type = MT_COLOR_SIGNATURE;
633         while (i < last && check_sig(buf, lineInfo, i - 1) == 0 &&
634                (lineInfo[i].type == MT_COLOR_NORMAL ||
635                 lineInfo[i].type == MT_COLOR_QUOTED ||
636                 lineInfo[i].type == MT_COLOR_HEADER)) {
637             /* oops... */
638             if (lineInfo[i].chunks) {
639                 lineInfo[i].chunks = 0;
640                 p_realloc(&(lineInfo[n].syntax), 1);
641             }
642             lineInfo[i++].type = MT_COLOR_SIGNATURE;
643         }
644     }
645     else if (check_sig(buf, lineInfo, n - 1) == 0)
646         lineInfo[n].type = MT_COLOR_SIGNATURE;
647     else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
648         if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
649             if (smatch[0].rm_so > 0) {
650                 char c;
651
652                 /* hack to avoid making an extra copy of buf */
653                 c = buf[smatch[0].rm_so];
654                 buf[smatch[0].rm_so] = 0;
655
656                 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
657                     if (q_classify && lineInfo[n].quote == NULL)
658                         lineInfo[n].quote = classify_quote (QuoteList,
659                                                             buf + pmatch[0].rm_so,
660                                                             pmatch[0].rm_eo -
661                                                             pmatch[0].rm_so, force_redraw,
662                                                             q_level);
663                     lineInfo[n].type = MT_COLOR_QUOTED;
664                 } else {
665                     lineInfo[n].type = MT_COLOR_NORMAL;
666                 }
667
668                 buf[smatch[0].rm_so] = c;
669             } else {
670                 lineInfo[n].type = MT_COLOR_NORMAL;
671             }
672         } else {
673             if (q_classify && lineInfo[n].quote == NULL)
674                 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
675                                                     pmatch[0].rm_eo - pmatch[0].rm_so,
676                                                     force_redraw, q_level);
677             lineInfo[n].type = MT_COLOR_QUOTED;
678         }
679     } else {
680         lineInfo[n].type = MT_COLOR_NORMAL;
681     }
682
683     /* body patterns */
684     if (lineInfo[n].type == MT_COLOR_NORMAL ||
685         lineInfo[n].type == MT_COLOR_QUOTED) {
686         i = 0;
687
688         offset = 0;
689         lineInfo[n].chunks = 0;
690         do {
691             if (!buf[offset])
692                 break;
693
694             found = 0;
695             null_rx = 0;
696             color_line = ColorBodyList;
697             while (color_line) {
698                 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
699                              (offset ? REG_NOTBOL : 0)) == 0) {
700                     if (pmatch[0].rm_eo != pmatch[0].rm_so) {
701                         if (!found) {
702                             if (++(lineInfo[n].chunks) > 1)
703                                 p_realloc(&(lineInfo[n].syntax), lineInfo[n].chunks);
704                         }
705                         i = lineInfo[n].chunks - 1;
706                         pmatch[0].rm_so += offset;
707                         pmatch[0].rm_eo += offset;
708                         if (!found ||
709                             pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
710                             (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
711                              pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
712                             (lineInfo[n].syntax)[i].color = color_line->pair;
713                             (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
714                             (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
715                         }
716                         found = 1;
717                         null_rx = 0;
718                     } else {
719                         null_rx = 1;        /* empty regexp; don't add it, but keep looking */
720                     }
721                 }
722                 color_line = color_line->next;
723             }
724
725             if (null_rx)
726                 offset++;               /* avoid degenerate cases */
727             else
728                 offset = lineInfo[n].syntax[i].last;
729         } while (found || null_rx);
730     }
731 }
732
733 static int is_ansi(unsigned char *buf)
734 {
735     while (isdigit(*buf) || *buf == ';')
736         buf++;
737     return (*buf == 'm');
738 }
739
740 static int check_attachment_marker(char *p)
741 {
742     char *q = AttachmentMarker;
743
744     for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
745     return (int)(*p - *q);
746 }
747
748 /* trim tail of buf so that it contains complete multibyte characters */
749 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
750     mbstate_t mbstate;
751     ssize_t k;
752
753     p_clear(&mbstate, 1);
754     for (; len > 0; buf += k, len -= k) {
755         k = mbrtowc (NULL, (char *) buf, len, &mbstate);
756         if (k == -2)
757             break;
758         if (k == -1 || k == 0)
759             k = 1;
760     }
761     *buf = '\0';
762
763     return len;
764 }
765
766 static int
767 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
768              unsigned char *fmt, ssize_t blen, int *buf_ready)
769 {
770     unsigned char *p;
771     static int b_read = 0;
772
773     if (*buf_ready == 0) {
774         buf[blen - 1] = 0;
775         if (offset != *last_pos)
776             fseeko (f, offset, 0);
777         if (fgets ((char *) buf, blen - 1, f) == NULL) {
778             fmt[0] = 0;
779             return (-1);
780         }
781         *last_pos = ftello (f);
782         b_read = (int) (*last_pos - offset);
783         *buf_ready = 1;
784
785         /* incomplete mbyte characters trigger a segfault in regex processing for
786          * certain versions of glibc. Trim them if necessary. */
787         if (b_read == blen - 2)
788             b_read -= trim_incomplete_mbyte(buf, b_read);
789
790         /* copy "buf" to "fmt", but without bold and underline controls */
791         p = buf;
792         while (*p) {
793             if (*p == '\010' && (p > buf)) {
794                 if (*(p + 1) == '_')    /* underline */
795                     p += 2;
796                 else if (*(p + 1)) {    /* bold or overstrike */
797                     *(fmt - 1) = *(p + 1);
798                     p += 2;
799                 } else {                  /* ^H */
800                     *fmt++ = *p++;
801                 }
802             }
803             else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
804                 while (*p++ != 'm')     /* skip ANSI sequence */
805                     ;
806             }
807             else if (*p == '\033' && *(p + 1) == ']'
808                      && check_attachment_marker ((char *) p) == 0) {
809                 while (*p++ != '\a')    /* skip pseudo-ANSI sequence */
810                     ;
811             } else {
812                 *fmt++ = *p++;
813             }
814         }
815         *fmt = 0;
816     }
817     return b_read;
818 }
819
820 #ifdef USE_NNTP
821 #include "nntp.h"
822 #endif
823
824 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
825                         int flags, ansi_attr * pa, int cnt,
826                         int *pspace, int *pvch, int *pcol, int *pspecial)
827 {
828   int space = -1;               /* index of the last space or TAB */
829   int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
830   int ch, vch, k, last_special = -1, special = 0, t;
831   wchar_t wc;
832   mbstate_t mbstate;
833
834   int wrap_cols = getmaxx(main_w);
835
836   if (!(flags & (M_SHOWFLAT)))
837     wrap_cols -= WrapMargin;
838
839   if (wrap_cols <= 0)
840     wrap_cols = getmaxx(main_w);
841
842   /* FIXME: this should come from lineInfo */
843   p_clear(&mbstate, 1);
844
845   for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
846     /* Handle ANSI sequences */
847     while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
848            is_ansi (buf + ch + 2)) {
849       while (isdigit(buf[ch]) || buf[ch] == ';')
850         ch++;
851     }
852
853     while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
854            check_attachment_marker ((char *) buf + ch) == 0) {
855       while (buf[ch++] != '\a')
856         if (ch >= cnt)
857           break;
858     }
859
860     /* is anything left to do? */
861     if (ch >= cnt)
862       break;
863
864     k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
865     if (k == -2 || k == -1) {
866       if (col + 4 > wrap_cols)
867         break;
868       col += 4;
869       if (pa)
870         wprintw(main_w, "\\%03o", buf[ch]);
871       k = 1;
872       continue;
873     }
874     if (k == 0)
875       k = 1;
876
877     /* Handle backspace */
878     special = 0;
879     if (iswprint(wc)) {
880       wchar_t wc1;
881       mbstate_t mbstate1;
882       int k1, k2;
883
884       while ((wc1 = 0, mbstate1 = mbstate,
885               k1 =
886               k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
887                            &mbstate1), k1 - k > 0 && wc1 == '\b')
888              && (wc1 = 0, k2 =
889                  mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
890                           &mbstate1), k2 > 0 && iswprint(wc1))) {
891         if (wc == wc1) {
892           special |= (wc == '_' && special & A_UNDERLINE)
893             ? A_UNDERLINE : A_BOLD;
894         }
895         else if (wc == '_' || wc1 == '_') {
896           special |= A_UNDERLINE;
897           wc = (wc1 == '_') ? wc : wc1;
898         } else {
899           /* special = 0; / * overstrike: nothing to do! */
900           wc = wc1;
901         }
902         ch += k1;
903         k = k2;
904         mbstate = mbstate1;
905       }
906     }
907
908     if (pa &&
909         ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
910          special || last_special || pa->attr)) {
911       resolve_color (*lineInfo, n, vch, flags, special, pa);
912       last_special = special;
913     }
914
915     if (iswprint(wc)) {
916       if (wc == ' ')
917         space = ch;
918       t = wcwidth (wc);
919       if (col + t > wrap_cols)
920         break;
921       col += t;
922       if (pa)
923         waddwch(main_w, wc);
924     }
925     else if (wc == '\n')
926       break;
927     else if (wc == '\t') {
928       space = ch;
929       t = (col & ~7) + 8;
930       if (t > wrap_cols)
931         break;
932       if (pa)
933         for (; col < t; col++)
934           waddch(main_w, ' ');
935       else
936         col = t;
937     }
938     else if (wc < 0x20 || wc == 0x7f) {
939       if (col + 2 > wrap_cols)
940         break;
941       col += 2;
942       if (pa)
943         wprintw(main_w, "^%c", ('@' + wc) & 0x7f);
944     }
945     else if (wc < 0x100) {
946       if (col + 4 > wrap_cols)
947         break;
948       col += 4;
949       if (pa)
950         wprintw(main_w, "\\%03o", wc);
951     } else {
952       if (col + 1 > wrap_cols)
953         break;
954       ++col;
955       if (pa)
956         waddch(main_w, CharsetReplacement);
957     }
958   }
959   *pspace = space;
960   *pcol = col;
961   *pvch = vch;
962   *pspecial = special;
963   return ch;
964 }
965
966 /*
967  * Args:
968  *      flags   M_SHOWFLAT, show characters (used for displaying help)
969  *              M_SHOWCOLOR, show characters in color
970  *                      otherwise don't show characters
971  *              M_HIDE, don't show quoted text
972  *              M_SEARCH, resolve search patterns
973  *              M_TYPES, compute line's type
974  *              M_PAGER_NSKIP, keeps leading whitespace
975  *              M_PAGER_MARKER, eventually show markers
976  *
977  * Return values:
978  *      -1      EOF was reached
979  *      0       normal exit, line was not displayed
980  *      >0      normal exit, line was displayed
981  */
982 static int
983 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
984               int *last, int *max, int flags, struct q_class_t **QuoteList,
985               int *q_level, int *force_redraw, regex_t * SearchRE)
986 {
987   unsigned char buf[LONG_STRING], fmt[LONG_STRING];
988   unsigned char *buf_ptr = buf;
989   int ch, vch, col, cnt, b_read;
990   int buf_ready = 0, change_last = 0;
991   int special;
992   int offset;
993   int def_color;
994   int m;
995   ansi_attr a = { 0, 0, 0, -1 };
996   regmatch_t pmatch[1];
997
998   if (n == *last) {
999     (*last)++;
1000     change_last = 1;
1001   }
1002
1003   if (*last == *max) {
1004     p_realloc(lineInfo, *max += LINES);
1005     for (ch = *last; ch < *max; ch++) {
1006       p_clear(&(*lineInfo)[ch], 1);
1007       (*lineInfo)[ch].type = -1;
1008       (*lineInfo)[ch].search_cnt = -1;
1009       (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1010       ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1011         -1;
1012     }
1013   }
1014
1015   /* only do color hiliting if we are viewing a message */
1016   if (flags & (M_SHOWCOLOR | M_TYPES)) {
1017     if ((*lineInfo)[n].type == -1) {
1018       /* determine the line class */
1019       if (fill_buffer
1020           (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1021            &buf_ready) < 0) {
1022         if (change_last)
1023           (*last)--;
1024         return (-1);
1025       }
1026
1027       resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1028                      QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1029
1030       /* avoid race condition for continuation lines when scrolling up */
1031       for (m = n + 1;
1032            m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1033            m++)
1034         (*lineInfo)[m].type = (*lineInfo)[n].type;
1035     }
1036
1037     /* this also prevents searching through the hidden lines */
1038     if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1039       flags = 0;                /* M_NOSHOW */
1040   }
1041
1042   /* At this point, (*lineInfo[n]).quote may still be undefined. We 
1043    * don't want to compute it every time M_TYPES is set, since this
1044    * would slow down the "bottom" function unacceptably. A compromise
1045    * solution is hence to call regexec() again, just to find out the
1046    * length of the quote prefix.
1047    */
1048   if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1049       (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1050   {
1051     if (fill_buffer
1052         (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1053          &buf_ready) < 0) {
1054       if (change_last)
1055         (*last)--;
1056       return (-1);
1057     }
1058     regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1059     (*lineInfo)[n].quote = classify_quote (QuoteList,
1060                                            (char *) fmt + pmatch[0].rm_so,
1061                                            pmatch[0].rm_eo - pmatch[0].rm_so,
1062                                            force_redraw, q_level);
1063   }
1064
1065   if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1066       && (*lineInfo)[n].search_cnt == -1) {
1067     if (fill_buffer
1068         (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1069          &buf_ready) < 0) {
1070       if (change_last)
1071         (*last)--;
1072       return (-1);
1073     }
1074
1075     offset = 0;
1076     (*lineInfo)[n].search_cnt = 0;
1077     while (regexec
1078            (SearchRE, (char *) fmt + offset, 1, pmatch,
1079             (offset ? REG_NOTBOL : 0)) == 0) {
1080       if (++((*lineInfo)[n].search_cnt) > 1)
1081         p_realloc(&(*lineInfo)[n].search, (*lineInfo)[n].search_cnt);
1082       else
1083         (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1084       pmatch[0].rm_so += offset;
1085       pmatch[0].rm_eo += offset;
1086       ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1087         pmatch[0].rm_so;
1088       ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1089         pmatch[0].rm_eo;
1090
1091       if (pmatch[0].rm_eo == pmatch[0].rm_so)
1092         offset++;               /* avoid degenerate cases */
1093       else
1094         offset = pmatch[0].rm_eo;
1095       if (!fmt[offset])
1096         break;
1097     }
1098   }
1099
1100   if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1101     /* we've already scanned this line, so just exit */
1102     return (0);
1103   }
1104   if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1105     /* no need to try to display this line... */
1106     return (1);                 /* fake display */
1107   }
1108
1109   if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1110                              sizeof (buf), &buf_ready)) < 0) {
1111     if (change_last)
1112       (*last)--;
1113     return (-1);
1114   }
1115
1116   /* now chose a good place to break the line */
1117   cnt = format_line(lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1118                     &special);
1119   buf_ptr = buf + cnt;
1120
1121   /* move the break point only if smart_wrap is set */
1122   if (option (OPTWRAP)) {
1123     if (cnt < b_read) {
1124       if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1125           && buf[cnt] != '\r') {
1126         buf_ptr = buf + ch;
1127         /* skip trailing blanks */
1128         while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1129           ch--;
1130         /* a very long word with leading spaces causes infinite wrapping */
1131         if ((!ch) && (flags & M_PAGER_NSKIP))
1132           buf_ptr = buf + cnt;
1133         else
1134           cnt = ch + 1;
1135       } else {
1136         buf_ptr = buf + cnt;    /* a very long word... */
1137       }
1138     }
1139     if (!(flags & M_PAGER_NSKIP))
1140       /* skip leading blanks on the next line too */
1141       while (*buf_ptr == ' ' || *buf_ptr == '\t')
1142         buf_ptr++;
1143   }
1144
1145   if (*buf_ptr == '\r')
1146     buf_ptr++;
1147   if (*buf_ptr == '\n')
1148     buf_ptr++;
1149
1150   if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1151     append_line (*lineInfo, n, (int) (buf_ptr - buf));
1152   (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1153
1154   /* if we don't need to display the line we are done */
1155   if (!(flags & M_SHOW))
1156     return 0;
1157
1158   /* display the line */
1159   format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1160
1161   /* end the last color pattern (needed by S-Lang) */
1162   if (special || (col != getmaxx(main_w) && (flags & (M_SHOWCOLOR | M_SEARCH))))
1163     resolve_color(*lineInfo, n, vch, flags, 0, &a);
1164
1165   /*
1166    * Fill the blank space at the end of the line with the prevailing color.
1167    * ncurses does an implicit wclrtoeol(main_w) when you do waddch(main_w,
1168    * '\n') so we have to make sure to reset the color *after* that
1169    */
1170   if (flags & M_SHOWCOLOR) {
1171     m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1172     if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1173       def_color = ((*lineInfo)[m].syntax)[0].color;
1174     else
1175       def_color = ColorDefs[(*lineInfo)[m].type];
1176
1177     wattrset(main_w, def_color);
1178     wbkgdset(main_w, def_color | ' ');
1179   }
1180   waddch(main_w, '\n');
1181
1182   /*
1183    * reset the color back to normal.  This *must* come after the
1184    * waddch(main_w, '\n'), otherwise the color for this line will not be
1185    * filled to the right margin.
1186    */
1187   if (flags & M_SHOWCOLOR) {
1188     SETCOLOR(main_w, MT_COLOR_NORMAL);
1189     BKGDSET(main_w, MT_COLOR_NORMAL);
1190   }
1191
1192   /* build a return code */
1193   if (!(flags & M_SHOW))
1194     flags = 0;
1195
1196   return (flags);
1197 }
1198
1199 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1200 {
1201   while (cur > 0 && nlines > 0) {
1202     cur--;
1203     if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1204       nlines--;
1205   }
1206
1207   return cur;
1208 }
1209
1210 /* This pager is actually not so simple as it once was.  It now operates in
1211    two modes: one for viewing messages and the other for viewing help.  These
1212    can be distinguished by whether or not ``hdr'' is NULL.  The ``hdr'' arg
1213    is there so that we can do operations on the current message without the
1214    need to pop back out to the main-menu.  */
1215 int
1216 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1217 {
1218   static char searchbuf[STRING];
1219   char buffer[LONG_STRING];
1220   int maxLine, lastLine = 0;
1221   struct line_t *lineInfo;
1222   struct q_class_t *QuoteList = NULL;
1223   int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1224   int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1225   int r = -1;
1226   int redraw = REDRAW_FULL;
1227   FILE *fp = NULL;
1228   off_t last_pos = 0, last_offset = 0;
1229   int old_smart_wrap, old_markers;
1230   struct stat sb;
1231   regex_t SearchRE;
1232   int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1233   int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0;    /* main message or rfc822 attachment */
1234
1235   int bodyoffset = 1;           /* offset of first line of real text */
1236   int statusoffset = 0;         /* offset for the status bar */
1237   int helpoffset = LINES - 2;   /* offset for the help bar. */
1238   int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1239
1240   MUTTMENU *pager_index = NULL;       /* the Pager Index (PI) */
1241   int indexoffset = 0;          /* offset for the PI */
1242   int indexlen = PagerIndexLines;       /* indexlen not always == PIL */
1243   int indicator = indexlen / 3; /* the indicator line of the PI */
1244   int old_PagerIndexLines;      /* some people want to resize it
1245                                  * while inside the pager... */
1246
1247 #ifdef USE_NNTP
1248   char *followup_to;
1249 #endif
1250
1251   if (!(flags & M_SHOWCOLOR))
1252     flags |= M_SHOWFLAT;
1253
1254   if ((fp = fopen (fname, "r")) == NULL) {
1255     mutt_perror (fname);
1256     return (-1);
1257   }
1258
1259   if (stat (fname, &sb) != 0) {
1260     mutt_perror (fname);
1261     m_fclose(&fp);
1262     return (-1);
1263   }
1264   unlink (fname);
1265
1266   /* Initialize variables */
1267
1268   if (IsHeader (extra) && !extra->hdr->read) {
1269     Context->msgnotreadyet = extra->hdr->msgno;
1270     mutt_set_flag (Context, extra->hdr, M_READ, 1);
1271   }
1272
1273   lineInfo = p_new(struct line_t, maxLine = LINES);
1274   for (i = 0; i < maxLine; i++) {
1275     p_clear(&lineInfo[i], 1);
1276     lineInfo[i].type = -1;
1277     lineInfo[i].search_cnt = -1;
1278     lineInfo[i].syntax = p_new(struct syntax_t, 1);
1279     (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1280   }
1281
1282   while (ch != -1) {
1283     mutt_curs_set (0);
1284     imap_keepalive ();
1285
1286     if (redraw & REDRAW_FULL) {
1287       SETCOLOR(main_w, MT_COLOR_NORMAL);
1288       /* wclear(main_w) doesn't optimize screen redraws */
1289       wmove(main_w, 0, 0);
1290       wclrtobot(main_w);
1291
1292       if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1293         indexlen = Context->vcount + 1;
1294       else
1295         indexlen = PagerIndexLines;
1296
1297       indicator = indexlen / 3;
1298
1299       indexoffset = 0;
1300       statusoffset = IsHeader (extra) ? indexlen : 0;
1301       bodyoffset = statusoffset + 1;
1302       helpoffset = LINES - 2;
1303       bodylen = helpoffset - bodyoffset;
1304
1305       SETCOLOR(main_w, MT_COLOR_STATUS);
1306       wmove(main_w, helpoffset, 0);
1307       mutt_paddstr(main_w, getmaxx(main_w), "");
1308       SETCOLOR(main_w, MT_COLOR_NORMAL);
1309
1310       if (Resize != NULL) {
1311         if ((SearchCompiled = Resize->SearchCompiled)) {
1312           REGCOMP
1313             (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1314           SearchFlag = M_SEARCH;
1315           SearchBack = Resize->SearchBack;
1316         }
1317         lines = Resize->line;
1318         redraw |= REDRAW_SIGWINCH;
1319
1320         p_delete(&Resize);
1321       }
1322
1323       if (IsHeader (extra) && PagerIndexLines) {
1324         if (pager_index == NULL) {
1325           /* only allocate the space if/when we need the index.
1326              Initialise the menu as per the main index */
1327           pager_index = mutt_new_menu ();
1328           pager_index->menu = MENU_MAIN;
1329           pager_index->make_entry = index_make_entry;
1330           pager_index->color = index_color;
1331           pager_index->max = Context->vcount;
1332           pager_index->current = extra->hdr->virtual;
1333         }
1334
1335         SETCOLOR(main_w, MT_COLOR_NORMAL);
1336         pager_index->offset = indexoffset + 1;
1337
1338         pager_index->pagelen = indexlen - 1;
1339
1340         /* some fudge to work out where abouts the indicator should go */
1341         if (pager_index->current - indicator < 0)
1342           pager_index->top = 0;
1343         else if (pager_index->max - pager_index->current < pager_index->pagelen - indicator)
1344           pager_index->top = pager_index->max - pager_index->pagelen;
1345         else
1346           pager_index->top = pager_index->current - indicator;
1347
1348         menu_redraw_index (pager_index);
1349       }
1350
1351       redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1352       mutt_show_error ();
1353     }
1354
1355     if (redraw & REDRAW_SIGWINCH) {
1356       i = -1;
1357       j = -1;
1358       while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1359                            has_types | SearchFlag, &QuoteList, &q_level,
1360                            &force_redraw, &SearchRE) == 0) {
1361         if (!lineInfo[i].continuation && ++j == lines) {
1362           topline = i;
1363           if (!SearchFlag)
1364             break;
1365         }
1366         redraw |= REDRAW_SIDEBAR;
1367       }                         /* while */
1368     }
1369
1370     if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1371       do {
1372         wmove(main_w, bodyoffset, 0);
1373         curline = oldtopline = topline;
1374         lines = 0;
1375         force_redraw = 0;
1376
1377         while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1378           if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1379                             &maxLine,
1380                             (flags & M_DISPLAYFLAGS) | hideQuoted |
1381                             SearchFlag, &QuoteList, &q_level, &force_redraw,
1382                             &SearchRE) > 0)
1383             lines++;
1384           curline++;
1385           wmove(main_w, lines + bodyoffset, 0);
1386           redraw |= REDRAW_SIDEBAR;
1387         }
1388         last_offset = lineInfo[curline].offset;
1389       } while (force_redraw);
1390
1391       SETCOLOR(main_w, MT_COLOR_TILDE);
1392       BKGDSET(main_w, MT_COLOR_TILDE);
1393       while (lines < bodylen) {
1394         wclrtoeol(main_w);
1395         if (option (OPTTILDE))
1396           waddch(main_w, '~');
1397         waddch(main_w, '\n');
1398         lines++;
1399         wmove(main_w, lines + bodyoffset, 0);
1400       }
1401       /* We are going to update the pager status bar, so it isn't
1402        * necessary to reset to normal color now. */
1403
1404       redraw |= REDRAW_STATUS;  /* need to update the % seen */
1405     }
1406
1407     if (redraw & REDRAW_STATUS) {
1408       /* print out the pager status bar */
1409       SETCOLOR(main_w, MT_COLOR_STATUS);
1410       BKGDSET(main_w, MT_COLOR_STATUS);
1411       wmove(main_w, statusoffset, 0);
1412       wclrtoeol(main_w);
1413       if (IsHeader (extra)) {
1414         size_t l1 = (getmaxx(main_w) - 9) * MB_LEN_MAX;
1415         size_t l2 = sizeof (buffer);
1416
1417         _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1418                            Context, extra->hdr, M_FORMAT_MAKEPRINT);
1419       }
1420       else if (IsMsgAttach (extra)) {
1421         size_t l1 = (getmaxx(main_w) - 9) * MB_LEN_MAX;
1422         size_t l2 = sizeof (buffer);
1423
1424         _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1425                            Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1426       }
1427       wmove(main_w, statusoffset, 0);
1428       mutt_paddstr(main_w, getmaxx(main_w) - 10,
1429                    IsHeader(extra) || IsMsgAttach(extra) ? buffer : banner);
1430
1431       waddstr(main_w, " -- (");
1432       if (last_pos < sb.st_size - 1)
1433         wprintw(main_w, "%d%%)", (int) (100 * last_offset / sb.st_size));
1434       else
1435         waddstr(main_w, topline == 0 ? "all)" : "end)");
1436       BKGDSET(main_w, MT_COLOR_NORMAL);
1437       SETCOLOR(main_w, MT_COLOR_NORMAL);
1438     }
1439
1440     if (redraw & REDRAW_SIDEBAR)
1441       sidebar_draw ();
1442
1443     if ((redraw & REDRAW_INDEX) && pager_index) {
1444       /* redraw the pager_index indicator, because the
1445        * flags for this message might have changed. */
1446       menu_redraw_current (pager_index);
1447       sidebar_draw ();
1448       /* print out the pager_index status bar */
1449       menu_status_line (buffer, sizeof (buffer), pager_index, NONULL (Status));
1450       wmove(main_w, indexoffset + 0, 0);
1451       SETCOLOR(main_w, MT_COLOR_STATUS);
1452       BKGDSET(main_w, MT_COLOR_STATUS);
1453       mutt_paddstr(main_w, getmaxx(main_w), buffer);
1454       SETCOLOR(main_w, MT_COLOR_NORMAL);
1455       BKGDSET(main_w, MT_COLOR_NORMAL);
1456     }
1457     /* if we're not using the index, update every time */
1458     if (index == 0)
1459       sidebar_draw ();
1460
1461     redraw = 0;
1462     mutt_refresh();
1463
1464     if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1465         && lineInfo[curline].offset < sb.st_size-1) {
1466       if (TopLine - topline > lines)
1467         topline += lines;
1468       else
1469         topline = TopLine;
1470       continue;
1471     } else {
1472       OldHdr = NULL;
1473     }
1474
1475     ch = km_dokey (MENU_PAGER);
1476     if (ch != -1)
1477       mutt_clear_error ();
1478     mutt_curs_set (1);
1479
1480     if (SigInt) {
1481       mutt_query_exit ();
1482       continue;
1483     }
1484
1485     if (SigWinch) {
1486       ui_layout_resize();
1487
1488       /* Store current position. */
1489       lines = -1;
1490       for (i = 0; i <= topline; i++)
1491         if (!lineInfo[i].continuation)
1492           lines++;
1493
1494       if (flags & M_PAGER_RETWINCH) {
1495         Resize = p_new(struct resize, 1);
1496
1497         Resize->line = lines;
1498         Resize->SearchCompiled = SearchCompiled;
1499         Resize->SearchBack = SearchBack;
1500
1501         ch = -1;
1502         rc = OP_REFORMAT_WINCH;
1503       } else {
1504         for (i = 0; i < maxLine; i++) {
1505           lineInfo[i].offset = 0;
1506           lineInfo[i].type = -1;
1507           lineInfo[i].continuation = 0;
1508           lineInfo[i].chunks = 0;
1509           lineInfo[i].search_cnt = -1;
1510           lineInfo[i].quote = NULL;
1511
1512           p_realloc(&lineInfo[i].syntax, 1);
1513           if (SearchCompiled)
1514             p_delete(&lineInfo[i].search);
1515         }
1516
1517         lastLine = 0;
1518         topline = 0;
1519
1520         redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1521         ch = 0;
1522       }
1523       continue;
1524     }
1525
1526     if (ch == -1) {
1527       ch = 0;
1528       continue;
1529     }
1530
1531     rc = ch;
1532
1533     switch (ch) {
1534     case OP_EXIT:
1535       rc = -1;
1536       ch = -1;
1537       break;
1538
1539     case OP_NEXT_PAGE:
1540       if (lineInfo[curline].offset < sb.st_size - 1) {
1541         topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1542       }
1543       else if (option (OPTPAGERSTOP)) {
1544         /* emulate "less -q" and don't go on to the next message. */
1545         mutt_error _("Bottom of message is shown.");
1546       } else {
1547         /* end of the current message, so display the next message. */
1548         rc = OP_MAIN_NEXT_UNDELETED;
1549         ch = -1;
1550       }
1551       break;
1552
1553     case OP_PREV_PAGE:
1554       if (topline != 0) {
1555         topline =
1556           upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1557       }
1558       else
1559         mutt_error _("Top of message is shown.");
1560       break;
1561
1562     case OP_NEXT_LINE:
1563       if (lineInfo[curline].offset < sb.st_size - 1) {
1564         topline++;
1565         if (hideQuoted) {
1566           while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1567                  topline < lastLine)
1568             topline++;
1569         }
1570       }
1571       else
1572         mutt_error _("Bottom of message is shown.");
1573       break;
1574
1575     case OP_PREV_LINE:
1576       if (topline)
1577         topline = upNLines (1, lineInfo, topline, hideQuoted);
1578       else
1579         mutt_error _("Top of message is shown.");
1580       break;
1581
1582     case OP_PAGER_TOP:
1583       if (topline)
1584         topline = 0;
1585       else
1586         mutt_error _("Top of message is shown.");
1587       break;
1588
1589     case OP_HALF_UP:
1590       if (topline)
1591         topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1592       else
1593         mutt_error _("Top of message is shown.");
1594       break;
1595
1596     case OP_HALF_DOWN:
1597       if (lineInfo[curline].offset < sb.st_size - 1) {
1598         topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1599       }
1600       else if (option (OPTPAGERSTOP)) {
1601         /* emulate "less -q" and don't go on to the next message. */
1602         mutt_error _("Bottom of message is shown.");
1603       } else {
1604         /* end of the current message, so display the next message. */
1605         rc = OP_MAIN_NEXT_UNDELETED;
1606         ch = -1;
1607       }
1608       break;
1609
1610     case OP_SEARCH_NEXT:
1611     case OP_SEARCH_OPPOSITE:
1612       if (SearchCompiled) {
1613       search_next:
1614         if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1615             (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1616           /* searching forward */
1617           for (i = topline + 1; i < lastLine; i++) {
1618             if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1619                 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1620               break;
1621           }
1622
1623           if (i < lastLine)
1624             topline = i;
1625           else
1626             mutt_error _("Not found.");
1627         }
1628         else {
1629           /* searching backward */
1630           for (i = topline - 1; i >= 0; i--) {
1631             if ((!hideQuoted || (has_types &&
1632                                  lineInfo[i].type != MT_COLOR_QUOTED)) &&
1633                 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1634               break;
1635           }
1636
1637           if (i >= 0)
1638             topline = i;
1639           else
1640             mutt_error _("Not found.");
1641         }
1642
1643         if (lineInfo[topline].search_cnt > 0)
1644           SearchFlag = M_SEARCH;
1645
1646         break;
1647       }
1648       /* no previous search pattern, so fall through to search */
1649
1650     case OP_SEARCH:
1651     case OP_SEARCH_REVERSE:
1652       m_strcpy(buffer, sizeof(buffer), searchbuf);
1653       if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1654                            _("Search: ")), buffer, sizeof (buffer),
1655                           M_CLEAR) != 0)
1656         break;
1657
1658       if (!m_strcmp (buffer, searchbuf)) {
1659         if (SearchCompiled) {
1660           /* do an implicit search-next */
1661           if (ch == OP_SEARCH)
1662             ch = OP_SEARCH_NEXT;
1663           else
1664             ch = OP_SEARCH_OPPOSITE;
1665
1666           goto search_next;
1667         }
1668       }
1669
1670       if (!buffer[0])
1671         break;
1672
1673       m_strcpy(searchbuf, sizeof(searchbuf), buffer);
1674
1675       /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1676       if (ch == OP_SEARCH)
1677         SearchBack = 0;
1678       else if (ch == OP_SEARCH_REVERSE)
1679         SearchBack = 1;
1680
1681       if (SearchCompiled) {
1682         regfree (&SearchRE);
1683         for (i = 0; i < lastLine; i++) {
1684           p_delete(&lineInfo[i].search);
1685           lineInfo[i].search_cnt = -1;
1686         }
1687       }
1688
1689       if ((err =
1690            REGCOMP (&SearchRE, searchbuf,
1691                     REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1692         regerror (err, &SearchRE, buffer, sizeof (buffer));
1693         mutt_error ("%s", buffer);
1694         regfree (&SearchRE);
1695         for (i = 0; i < maxLine; i++) {
1696           /* cleanup */
1697           p_delete(&lineInfo[i].search);
1698           lineInfo[i].search_cnt = -1;
1699         }
1700         SearchFlag = 0;
1701         SearchCompiled = 0;
1702       }
1703       else {
1704         SearchCompiled = 1;
1705         /* update the search pointers */
1706         i = 0;
1707         while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1708                              &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1709                              &QuoteList, &q_level,
1710                              &force_redraw, &SearchRE) == 0) {
1711           i++;
1712           redraw |= REDRAW_SIDEBAR;
1713         }
1714
1715         if (!SearchBack) {
1716           /* searching forward */
1717           for (i = topline; i < lastLine; i++) {
1718             if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1719                 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1720               break;
1721           }
1722
1723           if (i < lastLine)
1724             topline = i;
1725         }
1726         else {
1727           /* searching backward */
1728           for (i = topline; i >= 0; i--) {
1729             if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1730                 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1731               break;
1732           }
1733
1734           if (i >= 0)
1735             topline = i;
1736         }
1737
1738         if (lineInfo[topline].search_cnt == 0) {
1739           SearchFlag = 0;
1740           mutt_error _("Not found.");
1741         }
1742         else
1743           SearchFlag = M_SEARCH;
1744       }
1745       redraw = REDRAW_BODY;
1746       break;
1747
1748     case OP_SEARCH_TOGGLE:
1749       if (SearchCompiled) {
1750         SearchFlag ^= M_SEARCH;
1751         redraw = REDRAW_BODY;
1752       }
1753       break;
1754
1755     case OP_HELP:
1756       /* don't let the user enter the help-menu from the help screen! */
1757       if (!InHelp) {
1758         InHelp = 1;
1759         mutt_help (MENU_PAGER);
1760         redraw = REDRAW_FULL;
1761         InHelp = 0;
1762       }
1763       else
1764         mutt_error _("Help is currently being shown.");
1765       break;
1766
1767     case OP_PAGER_HIDE_QUOTED:
1768       if (has_types) {
1769         hideQuoted ^= M_HIDE;
1770         if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1771           topline = upNLines (1, lineInfo, topline, hideQuoted);
1772         else
1773           redraw = REDRAW_BODY;
1774       }
1775       break;
1776
1777     case OP_PAGER_SKIP_QUOTED:
1778       if (has_types) {
1779         int dretval = 0;
1780         int new_topline = topline;
1781
1782         while ((new_topline < lastLine ||
1783                 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1784                                                new_topline, &lastLine,
1785                                                &maxLine, M_TYPES, &QuoteList,
1786                                                &q_level, &force_redraw,
1787                                                &SearchRE))))
1788                && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1789           redraw |= REDRAW_SIDEBAR;
1790           new_topline++;
1791         }
1792
1793         if (dretval < 0) {
1794           mutt_error _("No more quoted text.");
1795
1796           break;
1797         }
1798
1799         while ((new_topline < lastLine ||
1800                 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1801                                                new_topline, &lastLine,
1802                                                &maxLine, M_TYPES, &QuoteList,
1803                                                &q_level, &force_redraw,
1804                                                &SearchRE))))
1805                && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
1806           new_topline++;
1807           redraw |= REDRAW_SIDEBAR;
1808         }
1809
1810         if (dretval < 0) {
1811           mutt_error _("No more unquoted text after quoted text.");
1812
1813           break;
1814         }
1815         topline = new_topline;
1816       }
1817       break;
1818
1819     case OP_PAGER_BOTTOM:      /* move to the end of the file */
1820       if (lineInfo[curline].offset < sb.st_size - 1) {
1821         i = curline;
1822         /* make sure the types are defined to the end of file */
1823         while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1824                              &maxLine, has_types,
1825                              &QuoteList, &q_level, &force_redraw,
1826                              &SearchRE) == 0) {
1827           i++;
1828           redraw |= REDRAW_SIDEBAR;
1829         }
1830         topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
1831       }
1832       else
1833         mutt_error _("Bottom of message is shown.");
1834       break;
1835
1836     case OP_REDRAW:
1837       clearok(main_w, TRUE);
1838       redraw = REDRAW_FULL;
1839       break;
1840
1841     case OP_NULL:
1842       km_error_key (MENU_PAGER);
1843       break;
1844
1845       /* --------------------------------------------------------------------
1846        * The following are operations on the current message rather than
1847        * adjusting the view of the message.
1848        */
1849
1850     case OP_BOUNCE_MESSAGE:
1851       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1852         CHECK_ATTACH;
1853       if (IsMsgAttach (extra))
1854         mutt_attach_bounce (extra->fp, extra->hdr,
1855                             extra->idx, extra->idxlen, extra->bdy);
1856       else
1857         ci_bounce_message (extra->hdr, &redraw);
1858       break;
1859
1860     case OP_RESEND:
1861       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
1862         CHECK_ATTACH;
1863       if (IsMsgAttach (extra))
1864         mutt_attach_resend (extra->fp, extra->hdr,
1865                             extra->idx, extra->idxlen, extra->bdy);
1866       else
1867         mutt_resend_message (NULL, extra->ctx, extra->hdr);
1868       redraw = REDRAW_FULL;
1869       break;
1870
1871     case OP_CHECK_TRADITIONAL:
1872       CHECK_MODE (IsHeader (extra));
1873       if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
1874         ch = -1;
1875         rc = OP_CHECK_TRADITIONAL;
1876       }
1877       break;
1878
1879     case OP_CREATE_ALIAS:
1880       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
1881       if (IsMsgAttach (extra))
1882         mutt_create_alias (extra->bdy->hdr->env, NULL);
1883       else
1884         mutt_create_alias (extra->hdr->env, NULL);
1885       MAYBE_REDRAW (redraw);
1886       break;
1887
1888     case OP_PURGE_MESSAGE:
1889     case OP_DELETE:
1890       CHECK_MODE (IsHeader (extra));
1891       CHECK_READONLY;
1892
1893       CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
1894
1895       mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
1896       mutt_set_flag (Context, extra->hdr, M_PURGED,
1897                      ch != OP_PURGE_MESSAGE ? 0 : 1);
1898       if (option (OPTDELETEUNTAG))
1899         mutt_set_flag (Context, extra->hdr, M_TAG, 0);
1900       redraw = REDRAW_STATUS | REDRAW_INDEX;
1901       if (option (OPTRESOLVE)) {
1902         ch = -1;
1903         rc = OP_MAIN_NEXT_UNDELETED;
1904       }
1905       break;
1906
1907     case OP_DELETE_THREAD:
1908     case OP_DELETE_SUBTHREAD:
1909       CHECK_MODE (IsHeader (extra));
1910       CHECK_READONLY;
1911
1912       CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
1913
1914       r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
1915                                 ch == OP_DELETE_THREAD ? 0 : 1);
1916
1917       if (r != -1) {
1918         if (option (OPTDELETEUNTAG))
1919           mutt_thread_set_flag (extra->hdr, M_TAG, 0,
1920                                 ch == OP_DELETE_THREAD ? 0 : 1);
1921         if (option (OPTRESOLVE)) {
1922           rc = OP_MAIN_NEXT_UNDELETED;
1923           ch = -1;
1924         }
1925
1926         if (!option (OPTRESOLVE) && PagerIndexLines)
1927           redraw = REDRAW_FULL;
1928         else
1929           redraw = REDRAW_STATUS | REDRAW_INDEX;
1930       }
1931       break;
1932
1933     case OP_DISPLAY_ADDRESS:
1934       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
1935       if (IsMsgAttach (extra))
1936         mutt_display_address (extra->bdy->hdr->env);
1937       else
1938         mutt_display_address (extra->hdr->env);
1939       break;
1940
1941     case OP_ENTER_COMMAND:
1942       old_smart_wrap = option (OPTWRAP);
1943       old_markers = option (OPTMARKERS);
1944       old_PagerIndexLines = PagerIndexLines;
1945
1946       CurrentMenu = MENU_PAGER;
1947       mutt_enter_command ();
1948
1949       if (option (OPTNEEDRESORT)) {
1950         unset_option (OPTNEEDRESORT);
1951         CHECK_MODE (IsHeader (extra));
1952         set_option (OPTNEEDRESORT);
1953       }
1954
1955       if (old_PagerIndexLines != PagerIndexLines) {
1956         if (pager_index)
1957           mutt_menuDestroy (&pager_index);
1958         pager_index = NULL;
1959       }
1960
1961       if (option (OPTWRAP) != old_smart_wrap ||
1962           option (OPTMARKERS) != old_markers) {
1963         if (flags & M_PAGER_RETWINCH) {
1964           ch = -1;
1965           rc = OP_REFORMAT_WINCH;
1966           continue;
1967         }
1968
1969         /* count the real lines above */
1970         j = 0;
1971         for (i = 0; i <= topline; i++) {
1972           if (!lineInfo[i].continuation)
1973             j++;
1974         }
1975
1976         /* we need to restart the whole thing */
1977         for (i = 0; i < maxLine; i++) {
1978           lineInfo[i].offset = 0;
1979           lineInfo[i].type = -1;
1980           lineInfo[i].continuation = 0;
1981           lineInfo[i].chunks = 0;
1982           lineInfo[i].search_cnt = -1;
1983           lineInfo[i].quote = NULL;
1984
1985           p_realloc(&(lineInfo[i].syntax), 1);
1986           if (SearchCompiled)
1987             p_delete(&lineInfo[i].search);
1988         }
1989
1990         if (SearchCompiled) {
1991           regfree (&SearchRE);
1992           SearchCompiled = 0;
1993         }
1994         SearchFlag = 0;
1995
1996         /* try to keep the old position */
1997         topline = 0;
1998         lastLine = 0;
1999         while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2000                                       &lastLine, &maxLine,
2001                                       (has_types ? M_TYPES : 0),
2002                                       &QuoteList, &q_level, &force_redraw,
2003                                       &SearchRE) == 0) {
2004           redraw |= REDRAW_SIDEBAR;
2005           if (!lineInfo[topline].continuation)
2006             j--;
2007           if (j > 0)
2008             topline++;
2009         }
2010
2011         ch = 0;
2012       }
2013
2014       if (option (OPTFORCEREDRAWPAGER))
2015         redraw = REDRAW_FULL;
2016       unset_option (OPTFORCEREDRAWINDEX);
2017       unset_option (OPTFORCEREDRAWPAGER);
2018       break;
2019
2020     case OP_FLAG_MESSAGE:
2021       CHECK_MODE (IsHeader (extra));
2022       CHECK_READONLY;
2023
2024       CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2025
2026       mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2027       redraw = REDRAW_STATUS | REDRAW_INDEX;
2028       if (option (OPTRESOLVE)) {
2029         ch = -1;
2030         rc = OP_MAIN_NEXT_UNDELETED;
2031       }
2032       break;
2033
2034     case OP_PIPE:
2035       CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2036       if (IsAttach (extra))
2037         mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2038       else
2039         mutt_pipe_message (extra->hdr);
2040       MAYBE_REDRAW (redraw);
2041       break;
2042
2043     case OP_PRINT:
2044       CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2045       if (IsAttach (extra))
2046         mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2047       else
2048         mutt_print_message (extra->hdr);
2049       break;
2050
2051     case OP_MAIL:
2052       CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2053       CHECK_ATTACH;
2054       ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2055       redraw = REDRAW_FULL;
2056       break;
2057
2058 #ifdef USE_NNTP
2059     case OP_POST:
2060       CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2061       CHECK_ATTACH;
2062       if (extra->ctx && extra->ctx->magic == M_NNTP &&
2063           !((nntp_data_t *) extra->ctx->data)->allowed &&
2064           query_quadoption (OPT_TOMODERATED,
2065                             _
2066                             ("Posting to this group not allowed, may be moderated. Continue?"))
2067           != M_YES)
2068         break;
2069       ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2070       redraw = REDRAW_FULL;
2071       break;
2072
2073     case OP_FORWARD_TO_GROUP:
2074       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2075       CHECK_ATTACH;
2076       if (extra->ctx && extra->ctx->magic == M_NNTP &&
2077           !((nntp_data_t *) extra->ctx->data)->allowed &&
2078           query_quadoption (OPT_TOMODERATED,
2079                             _
2080                             ("Posting to this group not allowed, may be moderated. Continue?"))
2081           != M_YES)
2082         break;
2083       if (IsMsgAttach (extra))
2084         mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2085                              extra->idxlen, extra->bdy, SENDNEWS);
2086       else
2087         ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2088                          extra->hdr);
2089       redraw = REDRAW_FULL;
2090       break;
2091
2092     case OP_FOLLOWUP:
2093       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2094       CHECK_ATTACH;
2095
2096       if (IsMsgAttach (extra))
2097         followup_to = extra->bdy->hdr->env->followup_to;
2098       else
2099         followup_to = extra->hdr->env->followup_to;
2100
2101       if (!followup_to || m_strcasecmp(followup_to, "poster") ||
2102           query_quadoption (OPT_FOLLOWUPTOPOSTER,
2103                             _("Reply by mail as poster prefers?")) != M_YES) {
2104         if (extra->ctx && extra->ctx->magic == M_NNTP
2105             && !((nntp_data_t *) extra->ctx->data)->allowed
2106             && query_quadoption (OPT_TOMODERATED,
2107                                  _
2108                                  ("Posting to this group not allowed, may be moderated. Continue?"))
2109             != M_YES)
2110           break;
2111         if (IsMsgAttach (extra))
2112           mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2113                              extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2114         else
2115           ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2116                            extra->ctx, extra->hdr);
2117         redraw = REDRAW_FULL;
2118         break;
2119       }
2120 #endif
2121
2122     case OP_REPLY:
2123       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2124       CHECK_ATTACH;
2125       if (IsMsgAttach (extra))
2126         mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2127                            extra->idxlen, extra->bdy, SENDREPLY);
2128       else
2129         ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2130       redraw = REDRAW_FULL;
2131       break;
2132
2133     case OP_RECALL_MESSAGE:
2134       CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2135       CHECK_ATTACH;
2136       ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2137       redraw = REDRAW_FULL;
2138       break;
2139
2140     case OP_GROUP_REPLY:
2141       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2142       CHECK_ATTACH;
2143       if (IsMsgAttach (extra))
2144         mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2145                            extra->idxlen, extra->bdy,
2146                            SENDREPLY | SENDGROUPREPLY);
2147       else
2148         ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2149                          extra->hdr);
2150       redraw = REDRAW_FULL;
2151       break;
2152
2153     case OP_LIST_REPLY:
2154       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2155       CHECK_ATTACH;
2156       if (IsMsgAttach (extra))
2157         mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2158                            extra->idxlen, extra->bdy,
2159                            SENDREPLY | SENDLISTREPLY);
2160       else
2161         ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2162                          extra->hdr);
2163       redraw = REDRAW_FULL;
2164       break;
2165
2166     case OP_FORWARD_MESSAGE:
2167       CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2168       CHECK_ATTACH;
2169       if (IsMsgAttach (extra))
2170         mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2171                              extra->idxlen, extra->bdy, 0);
2172       else
2173         ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2174       redraw = REDRAW_FULL;
2175       break;
2176
2177     case OP_DECRYPT_SAVE:
2178     case OP_SAVE:
2179       if (IsAttach (extra)) {
2180         mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2181                                    NULL);
2182         break;
2183       }
2184       /* fall through */
2185     case OP_COPY_MESSAGE:
2186     case OP_DECODE_SAVE:
2187     case OP_DECODE_COPY:
2188     case OP_DECRYPT_COPY:
2189       CHECK_MODE (IsHeader (extra));
2190       if (mutt_save_message (extra->hdr,
2191                              (ch == OP_DECRYPT_SAVE) ||
2192                              (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2193                              (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2194                              (ch == OP_DECRYPT_SAVE)
2195                              || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2196           && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2197               || ch == OP_DECRYPT_SAVE)) {
2198         if (option (OPTRESOLVE)) {
2199           ch = -1;
2200           rc = OP_MAIN_NEXT_UNDELETED;
2201         }
2202         else
2203           redraw |= REDRAW_STATUS | REDRAW_INDEX;
2204       }
2205       MAYBE_REDRAW (redraw);
2206       break;
2207
2208     case OP_SHELL_ESCAPE:
2209       mutt_shell_escape ();
2210       MAYBE_REDRAW (redraw);
2211       break;
2212
2213     case OP_TAG:
2214       CHECK_MODE (IsHeader (extra));
2215       mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2216
2217       Context->last_tag = extra->hdr->tagged ? extra->hdr :
2218         ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2219          ? NULL : Context->last_tag);
2220
2221       redraw = REDRAW_STATUS | REDRAW_INDEX;
2222       if (option (OPTRESOLVE)) {
2223         ch = -1;
2224         rc = OP_NEXT_ENTRY;
2225       }
2226       break;
2227
2228     case OP_TOGGLE_NEW:
2229       CHECK_MODE (IsHeader (extra));
2230       CHECK_READONLY;
2231
2232       CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2233
2234       if (extra->hdr->read || extra->hdr->old)
2235         mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2236       else if (!first)
2237         mutt_set_flag (Context, extra->hdr, M_READ, 1);
2238       first = 0;
2239       Context->msgnotreadyet = -1;
2240       redraw = REDRAW_STATUS | REDRAW_INDEX;
2241       if (option (OPTRESOLVE)) {
2242         ch = -1;
2243         rc = OP_MAIN_NEXT_UNDELETED;
2244       }
2245       break;
2246
2247     case OP_UNDELETE:
2248       CHECK_MODE (IsHeader (extra));
2249       CHECK_READONLY;
2250
2251       CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2252
2253       mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2254       mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2255       redraw = REDRAW_STATUS | REDRAW_INDEX;
2256       if (option (OPTRESOLVE)) {
2257         ch = -1;
2258         rc = OP_NEXT_ENTRY;
2259       }
2260       break;
2261
2262     case OP_UNDELETE_THREAD:
2263     case OP_UNDELETE_SUBTHREAD:
2264       CHECK_MODE (IsHeader (extra));
2265       CHECK_READONLY;
2266
2267       CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2268
2269       r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2270                                 ch == OP_UNDELETE_THREAD ? 0 : 1)
2271         + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2272                                 ch == OP_UNDELETE_THREAD ? 0 : 1);
2273
2274       if (r > -1) {
2275         if (option (OPTRESOLVE)) {
2276           rc = (ch == OP_DELETE_THREAD) ?
2277             OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2278           ch = -1;
2279         }
2280
2281         if (!option (OPTRESOLVE) && PagerIndexLines)
2282           redraw = REDRAW_FULL;
2283         else
2284           redraw = REDRAW_STATUS | REDRAW_INDEX;
2285       }
2286       break;
2287
2288     case OP_VERSION:
2289       mutt_version ();
2290       break;
2291
2292     case OP_BUFFY_LIST:
2293       if (option (OPTFORCEBUFFYCHECK))
2294         buffy_check (1);
2295       buffy_list ();
2296       redraw |= REDRAW_SIDEBAR;
2297       break;
2298
2299     case OP_VIEW_ATTACHMENTS:
2300       if (flags & M_PAGER_ATTACHMENT) {
2301         ch = -1;
2302         rc = OP_ATTACH_COLLAPSE;
2303         break;
2304       }
2305       CHECK_MODE (IsHeader (extra));
2306       mutt_view_attachments (extra->hdr);
2307       if (extra->hdr->attach_del)
2308         Context->changed = 1;
2309       redraw = REDRAW_FULL;
2310       break;
2311
2312     case OP_EXTRACT_KEYS:
2313       CHECK_MODE (IsHeader (extra));
2314       crypt_extract_keys_from_messages (extra->hdr);
2315       redraw = REDRAW_FULL;
2316       break;
2317
2318     case OP_SIDEBAR_SCROLL_UP:
2319     case OP_SIDEBAR_SCROLL_DOWN:
2320     case OP_SIDEBAR_NEXT:
2321     case OP_SIDEBAR_NEXT_NEW:
2322     case OP_SIDEBAR_PREV:
2323     case OP_SIDEBAR_PREV_NEW:
2324       sidebar_scroll (ch);
2325       break;
2326     default:
2327       ch = -1;
2328       break;
2329     }
2330   }
2331
2332   m_fclose(&fp);
2333   if (IsHeader (extra)) {
2334     Context->msgnotreadyet = -1;
2335     if (rc == -1)
2336       OldHdr = NULL;
2337     else {
2338       TopLine = topline;
2339       OldHdr = extra->hdr;
2340     }
2341   }
2342
2343   cleanup_quote(&QuoteList);
2344
2345   for (i = 0; i < maxLine; i++) {
2346     p_delete(&lineInfo[i].syntax);
2347     if (SearchCompiled)
2348       p_delete(&lineInfo[i].search);
2349   }
2350   if (SearchCompiled) {
2351     regfree (&SearchRE);
2352     SearchCompiled = 0;
2353   }
2354   p_delete(&lineInfo);
2355   if (pager_index)
2356     mutt_menuDestroy (&pager_index);
2357   return (rc != -1 ? rc : 0);
2358 }
2359
2360 #undef CHECK_ATTACH
2361 #undef CHECK_READONLY
2362 #undef CHECK_MODE