2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * Parts were written/modified by:
6 * Nico Golde <nico@ngolde.de>
7 * Andreas Krennmair <ak@synflood.at>
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.
19 #include "mutt_curses.h"
20 #include "mutt_regex.h"
22 #include "mutt_menu.h"
33 #include "imap_private.h"
36 #include "mutt_crypt.h"
49 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
51 #define IsAttach(x) (x && (x)->bdy)
52 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
53 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
54 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
55 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
57 static const char *Not_available_in_this_menu =
58 N_("Not available in this menu.");
59 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
60 static const char *Function_not_permitted_in_attach_message_mode =
61 N_("Function not permitted in attach-message mode.");
63 #define CHECK_MODE(x) if (!(x)) \
66 mutt_error _(Not_available_in_this_menu); \
70 #define CHECK_READONLY if (Context->readonly) \
73 mutt_error _(Mailbox_is_read_only); \
77 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
80 mutt_error _(Function_not_permitted_in_attach_message_mode); \
85 /* the error message returned here could be better. */
86 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
87 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
88 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
90 mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
100 struct q_class_t *next, *prev;
101 struct q_class_t *down, *up;
116 struct syntax_t *syntax;
117 struct syntax_t *search;
118 struct q_class_t *quote;
121 #define ANSI_OFF (1<<0)
122 #define ANSI_BLINK (1<<1)
123 #define ANSI_BOLD (1<<2)
124 #define ANSI_UNDERLINE (1<<3)
125 #define ANSI_REVERSE (1<<4)
126 #define ANSI_COLOR (1<<5)
128 typedef struct _ansi_attr {
135 static short InHelp = 0;
137 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
138 static struct resize {
145 #define NumSigLines 4
147 static int check_sig (const char *s, struct line_t *info, int n)
151 while (n > 0 && count <= NumSigLines) {
152 if (info[n].type != MT_COLOR_SIGNATURE)
161 if (count > NumSigLines) {
162 /* check for a blank line */
176 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
177 int special, ansi_attr * a)
179 int def_color; /* color without syntax hilight */
180 int color; /* final color */
181 static int last_color; /* last color set */
182 int search = 0, i, m;
185 last_color = -1; /* force attrset() */
187 if (lineInfo[n].continuation) {
188 if (!cnt && option (OPTMARKERS)) {
189 SETCOLOR (MT_COLOR_MARKERS);
191 last_color = ColorDefs[MT_COLOR_MARKERS];
193 m = (lineInfo[n].syntax)[0].first;
194 cnt += (lineInfo[n].syntax)[0].last;
198 if (!(flags & M_SHOWCOLOR))
199 def_color = ColorDefs[MT_COLOR_NORMAL];
200 else if (lineInfo[m].type == MT_COLOR_HEADER)
201 def_color = (lineInfo[m].syntax)[0].color;
203 def_color = ColorDefs[lineInfo[m].type];
205 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
206 struct q_class_t *class = lineInfo[m].quote;
209 def_color = class->color;
211 while (class && class->length > cnt) {
212 def_color = class->color;
219 if (flags & M_SHOWCOLOR) {
220 for (i = 0; i < lineInfo[m].chunks; i++) {
221 /* we assume the chunks are sorted */
222 if (cnt > (lineInfo[m].syntax)[i].last)
224 if (cnt < (lineInfo[m].syntax)[i].first)
226 if (cnt != (lineInfo[m].syntax)[i].last) {
227 color = (lineInfo[m].syntax)[i].color;
230 /* don't break here, as cnt might be
231 * in the next chunk as well */
235 if (flags & M_SEARCH) {
236 for (i = 0; i < lineInfo[m].search_cnt; i++) {
237 if (cnt > (lineInfo[m].search)[i].last)
239 if (cnt < (lineInfo[m].search)[i].first)
241 if (cnt != (lineInfo[m].search)[i].last) {
242 color = ColorDefs[MT_COLOR_SEARCH];
249 /* handle "special" bold & underlined characters */
250 if (special || a->attr) {
252 if ((a->attr & ANSI_COLOR)) {
254 a->pair = mutt_alloc_color (a->fg, a->bg);
256 if (a->attr & ANSI_BOLD)
261 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
262 if (ColorDefs[MT_COLOR_BOLD] && !search)
263 color = ColorDefs[MT_COLOR_BOLD];
267 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
268 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
269 color = ColorDefs[MT_COLOR_UNDERLINE];
271 color ^= A_UNDERLINE;
273 else if (a->attr & ANSI_REVERSE) {
276 else if (a->attr & ANSI_BLINK) {
279 else if (a->attr & ANSI_OFF) {
284 if (color != last_color) {
290 static void append_line (struct line_t *lineInfo, int n, int cnt)
294 lineInfo[n + 1].type = lineInfo[n].type;
295 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
296 lineInfo[n + 1].continuation = 1;
298 /* find the real start of the line */
299 for (m = n; m >= 0; m--)
300 if (lineInfo[m].continuation == 0)
303 (lineInfo[n + 1].syntax)[0].first = m;
304 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
305 cnt + (lineInfo[n].syntax)[0].last : cnt;
308 static void new_class_color (struct q_class_t *class, int *q_level)
310 class->index = (*q_level)++;
311 class->color = ColorQuote[class->index % ColorQuoteUsed];
315 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
316 int index, int *q_level)
318 struct q_class_t *q_list;
321 new_class->index = -1;
324 if (q_list->index >= index) {
326 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
329 q_list = q_list->down;
330 else if (q_list->next)
331 q_list = q_list->next;
333 while (!q_list->next) {
339 q_list = q_list->next;
343 new_class->index = index;
344 new_class->color = ColorQuote[index % ColorQuoteUsed];
348 static void cleanup_quote (struct q_class_t **QuoteList)
350 struct q_class_t *ptr;
353 if ((*QuoteList)->down)
354 cleanup_quote (&((*QuoteList)->down));
355 ptr = (*QuoteList)->next;
356 if ((*QuoteList)->prefix)
357 FREE (&(*QuoteList)->prefix);
365 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
366 const char *qptr, int length,
367 int *force_redraw, int *q_level)
369 struct q_class_t *q_list = *QuoteList;
370 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
372 int offset, tail_lng;
375 if (ColorQuoteUsed <= 1) {
376 /* not much point in classifying quotes... */
378 if (*QuoteList == NULL) {
379 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
380 class->color = ColorQuote[0];
386 /* Did I mention how much I like emulating Lisp in C? */
388 /* classify quoting prefix */
390 if (length <= q_list->length) {
391 /* case 1: check the top level nodes */
393 if (mutt_strncmp (qptr, q_list->prefix, length) == 0) {
394 if (length == q_list->length)
395 return q_list; /* same prefix: return the current class */
397 /* found shorter prefix */
399 /* add a node above q_list */
401 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
402 tmp->prefix = (char *) safe_calloc (1, length + 1);
403 strncpy (tmp->prefix, qptr, length);
404 tmp->length = length;
406 /* replace q_list by tmp in the top level list */
408 tmp->next = q_list->next;
409 q_list->next->prev = tmp;
412 tmp->prev = q_list->prev;
413 q_list->prev->next = tmp;
416 /* make q_list a child of tmp */
420 /* q_list has no siblings for now */
424 /* update the root if necessary */
425 if (q_list == *QuoteList)
428 index = q_list->index;
430 /* tmp should be the return class too */
433 /* next class to test; if tmp is a shorter prefix for another
434 * node, that node can only be in the top level list, so don't
435 * go down after this point
440 /* found another branch for which tmp is a shorter prefix */
442 /* save the next sibling for later */
445 /* unlink q_list from the top level list */
447 q_list->next->prev = q_list->prev;
449 q_list->prev->next = q_list->next;
451 /* at this point, we have a tmp->down; link q_list to it */
453 /* sibling order is important here, q_list should be linked last */
461 index = q_list->index;
463 /* next class to test; as above, we shouldn't go down */
467 /* we found a shorter prefix, so certain quotes have changed classes */
472 /* shorter, but not a substring of the current class: try next */
473 q_list = q_list->next;
478 /* case 2: try subclassing the current top level node */
480 /* tmp != NULL means we already found a shorter prefix at case 1 */
482 && mutt_strncmp (qptr, q_list->prefix, q_list->length) == 0) {
483 /* ok, it's a subclass somewhere on this branch */
486 offset = q_list->length;
488 q_list = q_list->down;
489 tail_lng = length - offset;
490 tail_qptr = (char *) qptr + offset;
493 if (length <= q_list->length) {
494 if (mutt_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
496 /* same prefix: return the current class */
497 if (length == q_list->length)
500 /* found shorter common prefix */
502 /* add a node above q_list */
503 tmp = (struct q_class_t *) safe_calloc (1,
506 tmp->prefix = (char *) safe_calloc (1, length + 1);
507 strncpy (tmp->prefix, qptr, length);
508 tmp->length = length;
510 /* replace q_list by tmp */
512 tmp->next = q_list->next;
513 q_list->next->prev = tmp;
516 tmp->prev = q_list->prev;
517 q_list->prev->next = tmp;
520 /* make q_list a child of tmp */
522 tmp->up = q_list->up;
524 if (tmp->up->down == q_list)
527 /* q_list has no siblings */
531 index = q_list->index;
533 /* tmp should be the return class too */
536 /* next class to test */
540 /* found another branch for which tmp is a shorter prefix */
542 /* save the next sibling for later */
545 /* unlink q_list from the top level list */
547 q_list->next->prev = q_list->prev;
549 q_list->prev->next = q_list->next;
551 /* at this point, we have a tmp->down; link q_list to it */
560 index = q_list->index;
562 /* next class to test */
566 /* we found a shorter prefix, so we need a redraw */
571 q_list = q_list->next;
576 /* longer than the current prefix: try subclassing it */
578 && mutt_strncmp (tail_qptr, (q_list->prefix) + offset,
579 q_list->length - offset) == 0) {
580 /* still a subclass: go down one level */
582 offset = q_list->length;
584 q_list = q_list->down;
585 tail_lng = length - offset;
586 tail_qptr = (char *) qptr + offset;
591 /* nope, try the next prefix */
592 q_list = q_list->next;
598 /* still not found so far: add it as a sibling to the current node */
601 (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
602 tmp->prefix = (char *) safe_calloc (1, length + 1);
603 strncpy (tmp->prefix, qptr, length);
604 tmp->length = length;
607 tmp->next = ptr->down;
608 ptr->down->prev = tmp;
613 new_class_color (tmp, q_level);
619 shift_class_colors (*QuoteList, tmp, index, q_level);
625 /* nope, try the next prefix */
626 q_list = q_list->next;
633 /* not found so far: add it as a top level class */
634 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
635 class->prefix = (char *) safe_calloc (1, length + 1);
636 strncpy (class->prefix, qptr, length);
637 class->length = length;
638 new_class_color (class, q_level);
641 class->next = *QuoteList;
642 (*QuoteList)->prev = class;
648 shift_class_colors (*QuoteList, tmp, index, q_level);
653 static int check_attachment_marker (char *);
656 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
657 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
660 COLOR_LINE *color_line;
661 regmatch_t pmatch[1], smatch[1];
662 int found, offset, null_rx, i;
664 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
666 lineInfo[n].type = MT_COLOR_NORMAL;
667 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
668 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
669 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
672 lineInfo[n].type = MT_COLOR_HDEFAULT;
673 color_line = ColorHdrList;
675 if (REGEXEC (color_line->rx, buf) == 0) {
676 lineInfo[n].type = MT_COLOR_HEADER;
677 lineInfo[n].syntax[0].color = color_line->pair;
680 color_line = color_line->next;
684 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
685 lineInfo[n].type = MT_COLOR_NORMAL;
687 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
688 lineInfo[n].type = MT_COLOR_ATTACHMENT;
690 else if (check_attachment_marker ((char *) raw) == 0)
691 lineInfo[n].type = MT_COLOR_ATTACHMENT;
693 else if (mutt_strcmp ("-- \n", buf) == 0
694 || mutt_strcmp ("-- \r\n", buf) == 0) {
697 lineInfo[n].type = MT_COLOR_SIGNATURE;
698 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
699 (lineInfo[i].type == MT_COLOR_NORMAL ||
700 lineInfo[i].type == MT_COLOR_QUOTED ||
701 lineInfo[i].type == MT_COLOR_HEADER)) {
703 if (lineInfo[i].chunks) {
704 lineInfo[i].chunks = 0;
705 safe_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
707 lineInfo[i++].type = MT_COLOR_SIGNATURE;
710 else if (check_sig (buf, lineInfo, n - 1) == 0)
711 lineInfo[n].type = MT_COLOR_SIGNATURE;
712 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
713 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
714 if (smatch[0].rm_so > 0) {
717 /* hack to avoid making an extra copy of buf */
718 c = buf[smatch[0].rm_so];
719 buf[smatch[0].rm_so] = 0;
721 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
722 if (q_classify && lineInfo[n].quote == NULL)
723 lineInfo[n].quote = classify_quote (QuoteList,
724 buf + pmatch[0].rm_so,
726 pmatch[0].rm_so, force_redraw,
728 lineInfo[n].type = MT_COLOR_QUOTED;
731 lineInfo[n].type = MT_COLOR_NORMAL;
733 buf[smatch[0].rm_so] = c;
736 lineInfo[n].type = MT_COLOR_NORMAL;
739 if (q_classify && lineInfo[n].quote == NULL)
740 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
741 pmatch[0].rm_eo - pmatch[0].rm_so,
742 force_redraw, q_level);
743 lineInfo[n].type = MT_COLOR_QUOTED;
747 lineInfo[n].type = MT_COLOR_NORMAL;
750 if (lineInfo[n].type == MT_COLOR_NORMAL ||
751 lineInfo[n].type == MT_COLOR_QUOTED) {
755 lineInfo[n].chunks = 0;
762 color_line = ColorBodyList;
764 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
765 (offset ? REG_NOTBOL : 0)) == 0) {
766 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
768 if (++(lineInfo[n].chunks) > 1)
769 safe_realloc (&(lineInfo[n].syntax),
770 (lineInfo[n].chunks) *
771 sizeof (struct syntax_t));
773 i = lineInfo[n].chunks - 1;
774 pmatch[0].rm_so += offset;
775 pmatch[0].rm_eo += offset;
777 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
778 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
779 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
780 (lineInfo[n].syntax)[i].color = color_line->pair;
781 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
782 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
788 null_rx = 1; /* empty regexp; don't add it, but keep looking */
790 color_line = color_line->next;
794 offset++; /* avoid degenerate cases */
796 offset = (lineInfo[n].syntax)[i].last;
797 } while (found || null_rx);
801 static int is_ansi (unsigned char *buf)
803 while (*buf && (isdigit (*buf) || *buf == ';'))
805 return (*buf == 'm');
808 static int check_attachment_marker (char *p)
810 char *q = AttachmentMarker;
812 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
813 return (int) (*p - *q);
816 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
820 while (isdigit (buf[x]) || buf[x] == ';')
823 /* Character Attributes */
824 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
828 mutt_free_color (a->fg, a->bg);
834 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
835 a->attr |= ANSI_BOLD;
838 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
839 a->attr |= ANSI_UNDERLINE;
842 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
843 a->attr |= ANSI_BLINK;
846 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
847 a->attr |= ANSI_REVERSE;
850 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
853 mutt_free_color (a->fg, a->bg);
859 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
862 mutt_free_color (a->fg, a->bg);
865 a->attr |= ANSI_COLOR;
866 a->fg = buf[pos + 1] - '0';
869 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
872 mutt_free_color (a->fg, a->bg);
875 a->attr |= ANSI_COLOR;
876 a->bg = buf[pos + 1] - '0';
880 while (pos < x && buf[pos] != ';')
891 fill_buffer (FILE * f, long *last_pos, long offset, unsigned char *buf,
892 unsigned char *fmt, size_t blen, int *buf_ready)
895 static int b_read = 0;
897 if (*buf_ready == 0) {
899 if (offset != *last_pos)
900 fseek (f, offset, 0);
901 if (fgets ((char *) buf, blen - 1, f) == NULL) {
905 *last_pos = ftell (f);
906 b_read = (int) (*last_pos - offset);
909 /* copy "buf" to "fmt", but without bold and underline controls */
912 if (*p == '\010' && (p > buf)) {
913 if (*(p + 1) == '_') /* underline */
915 else if (*(p + 1)) { /* bold or overstrike */
916 *(fmt - 1) = *(p + 1);
922 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
923 while (*p++ != 'm') /* skip ANSI sequence */
926 else if (*p == '\033' && *(p + 1) == ']'
927 && check_attachment_marker ((char *) p) == 0) {
928 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
929 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
946 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
947 int flags, ansi_attr * pa, int cnt,
948 int *pspace, int *pvch, int *pcol, int *pspecial)
950 int space = -1; /* index of the last space or TAB */
951 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
952 int ch, vch, k, last_special = -1, special = 0, t;
956 int wrap_cols = COLS;
958 if (!(flags & (M_SHOWFLAT)))
959 wrap_cols -= WrapMargin;
960 wrap_cols -= SidebarWidth;
965 /* FIXME: this should come from lineInfo */
966 memset (&mbstate, 0, sizeof (mbstate));
968 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
969 /* Handle ANSI sequences */
970 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
971 is_ansi (buf + ch + 2))
972 ch = grok_ansi (buf, ch + 2, pa) + 1;
974 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
975 check_attachment_marker ((char *) buf + ch) == 0) {
976 while (buf[ch++] != '\a')
981 /* is anything left to do? */
985 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
986 if (k == -2 || k == -1) {
987 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
988 __FILE__, __LINE__, k, errno));
989 if (col + 4 > wrap_cols)
993 printw ("\\%03o", buf[ch]);
1000 /* Handle backspace */
1002 if (IsWPrint (wc)) {
1007 while ((wc1 = 0, mbstate1 = mbstate,
1009 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1010 &mbstate1), k1 - k > 0 && wc1 == '\b')
1012 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1013 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1015 special |= (wc == '_' && special & A_UNDERLINE)
1016 ? A_UNDERLINE : A_BOLD;
1018 else if (wc == '_' || wc1 == '_') {
1019 special |= A_UNDERLINE;
1020 wc = (wc1 == '_') ? wc : wc1;
1023 /* special = 0; / * overstrike: nothing to do! */
1033 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1034 special || last_special || pa->attr)) {
1035 resolve_color (*lineInfo, n, vch, flags, special, pa);
1036 last_special = special;
1039 if (IsWPrint (wc)) {
1043 if (col + t > wrap_cols)
1049 else if (wc == '\n')
1051 else if (wc == '\t') {
1057 for (; col < t; col++)
1062 else if (wc < 0x20 || wc == 0x7f) {
1063 if (col + 2 > wrap_cols)
1067 printw ("^%c", ('@' + wc) & 0x7f);
1069 else if (wc < 0x100) {
1070 if (col + 4 > wrap_cols)
1074 printw ("\\%03o", wc);
1077 if (col + 1 > wrap_cols)
1081 addch (replacement_char ());
1087 *pspecial = special;
1093 * flags M_SHOWFLAT, show characters (used for displaying help)
1094 * M_SHOWCOLOR, show characters in color
1095 * otherwise don't show characters
1096 * M_HIDE, don't show quoted text
1097 * M_SEARCH, resolve search patterns
1098 * M_TYPES, compute line's type
1099 * M_PAGER_NSKIP, keeps leading whitespace
1100 * M_PAGER_MARKER, eventually show markers
1103 * -1 EOF was reached
1104 * 0 normal exit, line was not displayed
1105 * >0 normal exit, line was displayed
1109 display_line (FILE * f, long *last_pos, struct line_t **lineInfo, int n,
1110 int *last, int *max, int flags, struct q_class_t **QuoteList,
1111 int *q_level, int *force_redraw, regex_t * SearchRE)
1113 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1114 unsigned char *buf_ptr = buf;
1115 int ch, vch, col, cnt, b_read;
1116 int buf_ready = 0, change_last = 0;
1121 ansi_attr a = { 0, 0, 0, -1 };
1122 regmatch_t pmatch[1];
1129 if (*last == *max) {
1130 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1131 for (ch = *last; ch < *max; ch++) {
1132 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1133 (*lineInfo)[ch].type = -1;
1134 (*lineInfo)[ch].search_cnt = -1;
1135 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1136 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1141 /* only do color hiliting if we are viewing a message */
1142 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1143 if ((*lineInfo)[n].type == -1) {
1144 /* determine the line class */
1146 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1153 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1154 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1156 /* avoid race condition for continuation lines when scrolling up */
1158 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1160 (*lineInfo)[m].type = (*lineInfo)[n].type;
1163 /* this also prevents searching through the hidden lines */
1164 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1165 flags = 0; /* M_NOSHOW */
1168 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1169 * don't want to compute it every time M_TYPES is set, since this
1170 * would slow down the "bottom" function unacceptably. A compromise
1171 * solution is hence to call regexec() again, just to find out the
1172 * length of the quote prefix.
1174 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1175 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1178 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1184 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1185 (*lineInfo)[n].quote = classify_quote (QuoteList,
1186 (char *) fmt + pmatch[0].rm_so,
1187 pmatch[0].rm_eo - pmatch[0].rm_so,
1188 force_redraw, q_level);
1191 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1192 && (*lineInfo)[n].search_cnt == -1) {
1194 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1202 (*lineInfo)[n].search_cnt = 0;
1204 (SearchRE, (char *) fmt + offset, 1, pmatch,
1205 (offset ? REG_NOTBOL : 0)) == 0) {
1206 if (++((*lineInfo)[n].search_cnt) > 1)
1207 safe_realloc (&((*lineInfo)[n].search),
1208 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1210 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1211 pmatch[0].rm_so += offset;
1212 pmatch[0].rm_eo += offset;
1213 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1215 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1218 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1219 offset++; /* avoid degenerate cases */
1221 offset = pmatch[0].rm_eo;
1227 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1228 /* we've already scanned this line, so just exit */
1231 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1232 /* no need to try to display this line... */
1233 return (1); /* fake display */
1236 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1237 sizeof (buf), &buf_ready)) < 0) {
1243 /* now chose a good place to break the line */
1245 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1247 buf_ptr = buf + cnt;
1249 /* move the break point only if smart_wrap is set */
1250 if (option (OPTWRAP)) {
1252 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1253 && buf[cnt] != '\r') {
1255 /* skip trailing blanks */
1256 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1261 buf_ptr = buf + cnt; /* a very long word... */
1263 if (!(flags & M_PAGER_NSKIP))
1264 /* skip leading blanks on the next line too */
1265 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1269 if (*buf_ptr == '\r')
1271 if (*buf_ptr == '\n')
1274 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1275 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1276 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1278 /* if we don't need to display the line we are done */
1279 if (!(flags & M_SHOW))
1282 /* display the line */
1283 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1285 /* avoid a bug in ncurses... */
1286 #ifndef USE_SLANG_CURSES
1288 SETCOLOR (MT_COLOR_NORMAL);
1293 /* end the last color pattern (needed by S-Lang) */
1294 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1295 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1298 * Fill the blank space at the end of the line with the prevailing color.
1299 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1300 * to make sure to reset the color *after* that
1302 if (flags & M_SHOWCOLOR) {
1303 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1304 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1305 def_color = ((*lineInfo)[m].syntax)[0].color;
1307 def_color = ColorDefs[(*lineInfo)[m].type];
1309 attrset (def_color);
1311 bkgdset (def_color | ' ');
1315 /* ncurses always wraps lines when you get to the right side of the
1316 * screen, but S-Lang seems to only wrap if the next character is *not*
1319 #ifndef USE_SLANG_CURSES
1325 * reset the color back to normal. This *must* come after the
1326 * addch('\n'), otherwise the color for this line will not be
1327 * filled to the right margin.
1329 if (flags & M_SHOWCOLOR) {
1330 SETCOLOR (MT_COLOR_NORMAL);
1331 BKGDSET (MT_COLOR_NORMAL);
1334 /* build a return code */
1335 if (!(flags & M_SHOW))
1341 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1343 while (cur > 0 && nlines > 0) {
1345 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1352 static struct mapping_t PagerHelp[] = {
1353 {N_("Exit"), OP_EXIT},
1354 {N_("PrevPg"), OP_PREV_PAGE},
1355 {N_("NextPg"), OP_NEXT_PAGE},
1358 static struct mapping_t PagerHelpExtra[] = {
1359 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1360 {N_("Del"), OP_DELETE},
1361 {N_("Reply"), OP_REPLY},
1362 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1367 static struct mapping_t PagerNewsHelpExtra[] = {
1368 {N_("Post"), OP_POST},
1369 {N_("Followup"), OP_FOLLOWUP},
1370 {N_("Del"), OP_DELETE},
1371 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1378 /* This pager is actually not so simple as it once was. It now operates in
1379 two modes: one for viewing messages and the other for viewing help. These
1380 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1381 is there so that we can do operations on the current message without the
1382 need to pop back out to the main-menu. */
1384 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1386 static char searchbuf[STRING];
1387 char buffer[LONG_STRING];
1388 char helpstr[SHORT_STRING * 2];
1389 char tmphelp[SHORT_STRING * 2];
1390 int maxLine, lastLine = 0;
1391 struct line_t *lineInfo;
1392 struct q_class_t *QuoteList = NULL;
1393 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1394 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1396 int redraw = REDRAW_FULL;
1398 long last_pos = 0, last_offset = 0;
1399 int old_smart_wrap, old_markers;
1402 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1403 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1405 int bodyoffset = 1; /* offset of first line of real text */
1406 int statusoffset = 0; /* offset for the status bar */
1407 int helpoffset = LINES - 2; /* offset for the help bar. */
1408 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1410 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1411 int indexoffset = 0; /* offset for the PI */
1412 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1413 int indicator = indexlen / 3; /* the indicator line of the PI */
1414 int old_PagerIndexLines; /* some people want to resize it
1415 * while inside the pager... */
1421 if (!(flags & M_SHOWCOLOR))
1422 flags |= M_SHOWFLAT;
1424 if ((fp = fopen (fname, "r")) == NULL) {
1425 mutt_perror (fname);
1429 if (stat (fname, &sb) != 0) {
1430 mutt_perror (fname);
1436 /* Initialize variables */
1438 if (IsHeader (extra) && !extra->hdr->read) {
1439 Context->msgnotreadyet = extra->hdr->msgno;
1440 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1443 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1444 for (i = 0; i < maxLine; i++) {
1445 memset (&lineInfo[i], 0, sizeof (struct line_t));
1446 lineInfo[i].type = -1;
1447 lineInfo[i].search_cnt = -1;
1448 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1449 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1452 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1453 if (IsHeader (extra)) {
1454 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1455 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1458 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1461 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1464 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1465 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1466 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1476 if (redraw & REDRAW_FULL) {
1477 SETCOLOR (MT_COLOR_NORMAL);
1478 /* clear() doesn't optimize screen redraws */
1482 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1483 indexlen = Context->vcount + 1;
1485 indexlen = PagerIndexLines;
1487 indicator = indexlen / 3;
1489 if (option (OPTSTATUSONTOP)) {
1491 statusoffset = IsHeader (extra) ? indexlen : 0;
1492 bodyoffset = statusoffset + 1;
1493 helpoffset = LINES - 2;
1494 bodylen = helpoffset - bodyoffset;
1495 if (!option (OPTHELP))
1501 statusoffset = LINES - 2;
1502 if (!option (OPTHELP))
1504 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1505 bodylen = statusoffset - bodyoffset;
1508 if (option (OPTHELP)) {
1509 SETCOLOR (MT_COLOR_STATUS);
1510 move (helpoffset, 0);
1511 mutt_paddstr (COLS, helpstr);
1512 SETCOLOR (MT_COLOR_NORMAL);
1515 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1516 if (Resize != NULL) {
1517 if ((SearchCompiled = Resize->SearchCompiled)) {
1519 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1520 SearchFlag = M_SEARCH;
1521 SearchBack = Resize->SearchBack;
1523 lines = Resize->line;
1524 redraw |= REDRAW_SIGWINCH;
1530 if (IsHeader (extra) && PagerIndexLines) {
1531 if (index == NULL) {
1532 /* only allocate the space if/when we need the index.
1533 Initialise the menu as per the main index */
1534 index = mutt_new_menu ();
1535 index->menu = MENU_MAIN;
1536 index->make_entry = index_make_entry;
1537 index->color = index_color;
1538 index->max = Context->vcount;
1539 index->current = extra->hdr->virtual;
1542 SETCOLOR (MT_COLOR_NORMAL);
1543 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1545 index->pagelen = indexlen - 1;
1547 /* some fudge to work out where abouts the indicator should go */
1548 if (index->current - indicator < 0)
1550 else if (index->max - index->current < index->pagelen - indicator)
1551 index->top = index->max - index->pagelen;
1553 index->top = index->current - indicator;
1555 menu_redraw_index (index);
1558 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1562 if (redraw & REDRAW_SIGWINCH) {
1565 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1566 has_types | SearchFlag, &QuoteList, &q_level,
1567 &force_redraw, &SearchRE) == 0) {
1568 if (!lineInfo[i].continuation && ++j == lines) {
1573 redraw |= REDRAW_SIDEBAR;
1577 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1579 move (bodyoffset, SidebarWidth);
1580 curline = oldtopline = topline;
1584 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1585 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1587 (flags & M_DISPLAYFLAGS) | hideQuoted |
1588 SearchFlag, &QuoteList, &q_level, &force_redraw,
1592 move (lines + bodyoffset, SidebarWidth);
1593 redraw |= REDRAW_SIDEBAR;
1595 last_offset = lineInfo[curline].offset;
1596 } while (force_redraw);
1598 SETCOLOR (MT_COLOR_TILDE);
1599 BKGDSET (MT_COLOR_TILDE);
1600 while (lines < bodylen) {
1602 if (option (OPTTILDE))
1606 move (lines + bodyoffset, SidebarWidth);
1608 /* We are going to update the pager status bar, so it isn't
1609 * necessary to reset to normal color now. */
1611 redraw |= REDRAW_STATUS; /* need to update the % seen */
1614 if (redraw & REDRAW_STATUS) {
1615 /* print out the pager status bar */
1616 SETCOLOR (MT_COLOR_STATUS);
1617 BKGDSET (MT_COLOR_STATUS);
1618 CLEARLINE_WIN (statusoffset);
1619 if (IsHeader (extra)) {
1620 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1621 size_t l2 = sizeof (buffer);
1623 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1624 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1626 else if (IsMsgAttach (extra)) {
1627 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1628 size_t l2 = sizeof (buffer);
1630 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1631 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1633 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1634 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1635 mutt_paddstr (COLS - 10, IsHeader (extra)
1636 || IsMsgAttach (extra) ? buffer : banner);
1639 if (last_pos < sb.st_size - 1)
1640 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1642 addstr (topline == 0 ? "all)" : "end)");
1643 BKGDSET (MT_COLOR_NORMAL);
1644 SETCOLOR (MT_COLOR_NORMAL);
1647 if (redraw & REDRAW_SIDEBAR)
1648 draw_sidebar (MENU_PAGER);
1650 if ((redraw & REDRAW_INDEX) && index) {
1651 /* redraw the pager_index indicator, because the
1652 * flags for this message might have changed. */
1653 menu_redraw_current (index);
1654 draw_sidebar (MENU_PAGER);
1655 /* print out the index status bar */
1656 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1657 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1658 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1659 SETCOLOR (MT_COLOR_STATUS);
1660 mutt_paddstr (COLS - (option (OPTSTATUSONTOP) ? 0 : SidebarWidth),
1662 SETCOLOR (MT_COLOR_NORMAL);
1664 /* if we're not using the index, update every time */
1666 draw_sidebar (MENU_PAGER);
1670 move (statusoffset, COLS - 1);
1672 ch = km_dokey (MENU_PAGER);
1674 mutt_clear_error ();
1681 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1682 else if (SigWinch) {
1683 mutt_resize_screen ();
1685 /* Store current position. */
1687 for (i = 0; i <= topline; i++)
1688 if (!lineInfo[i].continuation)
1691 if (flags & M_PAGER_RETWINCH) {
1692 Resize = safe_malloc (sizeof (struct resize));
1694 Resize->line = lines;
1695 Resize->SearchCompiled = SearchCompiled;
1696 Resize->SearchBack = SearchBack;
1699 rc = OP_REFORMAT_WINCH;
1702 for (i = 0; i < maxLine; i++) {
1703 lineInfo[i].offset = 0;
1704 lineInfo[i].type = -1;
1705 lineInfo[i].continuation = 0;
1706 lineInfo[i].chunks = 0;
1707 lineInfo[i].search_cnt = -1;
1708 lineInfo[i].quote = NULL;
1710 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1711 if (SearchCompiled && lineInfo[i].search)
1712 FREE (&(lineInfo[i].search));
1718 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1723 clearok (stdscr, TRUE); /*force complete redraw */
1727 else if (ch == -1) {
1741 if (lineInfo[curline].offset < sb.st_size - 1) {
1742 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1744 else if (option (OPTPAGERSTOP)) {
1745 /* emulate "less -q" and don't go on to the next message. */
1746 mutt_error _("Bottom of message is shown.");
1749 /* end of the current message, so display the next message. */
1750 rc = OP_MAIN_NEXT_UNDELETED;
1758 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1761 mutt_error _("Top of message is shown.");
1765 if (lineInfo[curline].offset < sb.st_size - 1) {
1768 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1774 mutt_error _("Bottom of message is shown.");
1779 topline = upNLines (1, lineInfo, topline, hideQuoted);
1781 mutt_error _("Top of message is shown.");
1788 mutt_error _("Top of message is shown.");
1793 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1795 mutt_error _("Top of message is shown.");
1799 if (lineInfo[curline].offset < sb.st_size - 1) {
1800 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1802 else if (option (OPTPAGERSTOP)) {
1803 /* emulate "less -q" and don't go on to the next message. */
1804 mutt_error _("Bottom of message is shown.");
1807 /* end of the current message, so display the next message. */
1808 rc = OP_MAIN_NEXT_UNDELETED;
1813 case OP_SEARCH_NEXT:
1814 case OP_SEARCH_OPPOSITE:
1815 if (SearchCompiled) {
1817 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1818 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1819 /* searching forward */
1820 for (i = topline + 1; i < lastLine; i++) {
1821 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1822 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1829 mutt_error _("Not found.");
1832 /* searching backward */
1833 for (i = topline - 1; i >= 0; i--) {
1834 if ((!hideQuoted || (has_types &&
1835 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1836 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1843 mutt_error _("Not found.");
1846 if (lineInfo[topline].search_cnt > 0)
1847 SearchFlag = M_SEARCH;
1851 /* no previous search pattern, so fall through to search */
1854 case OP_SEARCH_REVERSE:
1855 strfcpy (buffer, searchbuf, sizeof (buffer));
1856 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1857 _("Search: ")), buffer, sizeof (buffer),
1861 if (!strcmp (buffer, searchbuf)) {
1862 if (SearchCompiled) {
1863 /* do an implicit search-next */
1864 if (ch == OP_SEARCH)
1865 ch = OP_SEARCH_NEXT;
1867 ch = OP_SEARCH_OPPOSITE;
1876 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1878 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1879 if (ch == OP_SEARCH)
1881 else if (ch == OP_SEARCH_REVERSE)
1884 if (SearchCompiled) {
1885 regfree (&SearchRE);
1886 for (i = 0; i < lastLine; i++) {
1887 if (lineInfo[i].search)
1888 FREE (&(lineInfo[i].search));
1889 lineInfo[i].search_cnt = -1;
1894 REGCOMP (&SearchRE, searchbuf,
1895 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1896 regerror (err, &SearchRE, buffer, sizeof (buffer));
1897 mutt_error ("%s", buffer);
1898 regfree (&SearchRE);
1899 for (i = 0; i < maxLine; i++) {
1901 if (lineInfo[i].search)
1902 FREE (&(lineInfo[i].search));
1903 lineInfo[i].search_cnt = -1;
1910 /* update the search pointers */
1912 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1913 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1914 &QuoteList, &q_level,
1915 &force_redraw, &SearchRE) == 0) {
1917 redraw |= REDRAW_SIDEBAR;
1921 /* searching forward */
1922 for (i = topline; i < lastLine; i++) {
1923 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1924 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1932 /* searching backward */
1933 for (i = topline; i >= 0; i--) {
1934 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1935 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1943 if (lineInfo[topline].search_cnt == 0) {
1945 mutt_error _("Not found.");
1948 SearchFlag = M_SEARCH;
1950 redraw = REDRAW_BODY;
1953 case OP_SEARCH_TOGGLE:
1954 if (SearchCompiled) {
1955 SearchFlag ^= M_SEARCH;
1956 redraw = REDRAW_BODY;
1961 /* don't let the user enter the help-menu from the help screen! */
1964 mutt_help (MENU_PAGER);
1965 redraw = REDRAW_FULL;
1969 mutt_error _("Help is currently being shown.");
1972 case OP_PAGER_HIDE_QUOTED:
1974 hideQuoted ^= M_HIDE;
1975 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
1976 topline = upNLines (1, lineInfo, topline, hideQuoted);
1978 redraw = REDRAW_BODY;
1982 case OP_PAGER_SKIP_QUOTED:
1985 int new_topline = topline;
1987 while ((new_topline < lastLine ||
1988 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
1989 new_topline, &lastLine,
1990 &maxLine, M_TYPES, &QuoteList,
1991 &q_level, &force_redraw,
1993 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
1994 redraw |= REDRAW_SIDEBAR;
1999 mutt_error _("No more quoted text.");
2004 while ((new_topline < lastLine ||
2005 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2006 new_topline, &lastLine,
2007 &maxLine, M_TYPES, &QuoteList,
2008 &q_level, &force_redraw,
2010 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2012 redraw |= REDRAW_SIDEBAR;
2016 mutt_error _("No more unquoted text after quoted text.");
2020 topline = new_topline;
2024 case OP_PAGER_BOTTOM: /* move to the end of the file */
2025 if (lineInfo[curline].offset < sb.st_size - 1) {
2027 /* make sure the types are defined to the end of file */
2028 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2029 &maxLine, has_types,
2030 &QuoteList, &q_level, &force_redraw,
2033 redraw |= REDRAW_SIDEBAR;
2035 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2038 mutt_error _("Bottom of message is shown.");
2042 clearok (stdscr, TRUE);
2043 redraw = REDRAW_FULL;
2047 km_error_key (MENU_PAGER);
2050 /* --------------------------------------------------------------------
2051 * The following are operations on the current message rather than
2052 * adjusting the view of the message.
2055 case OP_BOUNCE_MESSAGE:
2056 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2058 if (IsMsgAttach (extra))
2059 mutt_attach_bounce (extra->fp, extra->hdr,
2060 extra->idx, extra->idxlen, extra->bdy);
2062 ci_bounce_message (extra->hdr, &redraw);
2066 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2068 if (IsMsgAttach (extra))
2069 mutt_attach_resend (extra->fp, extra->hdr,
2070 extra->idx, extra->idxlen, extra->bdy);
2072 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2073 redraw = REDRAW_FULL;
2076 case OP_CHECK_TRADITIONAL:
2077 CHECK_MODE (IsHeader (extra));
2078 if (!(WithCrypto & APPLICATION_PGP))
2080 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2082 rc = OP_CHECK_TRADITIONAL;
2086 case OP_CREATE_ALIAS:
2087 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2088 if (IsMsgAttach (extra))
2089 mutt_create_alias (extra->bdy->hdr->env, NULL);
2091 mutt_create_alias (extra->hdr->env, NULL);
2092 MAYBE_REDRAW (redraw);
2095 case OP_PURGE_MESSAGE:
2097 CHECK_MODE (IsHeader (extra));
2101 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2104 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2105 mutt_set_flag (Context, extra->hdr, M_PURGED,
2106 ch != OP_PURGE_MESSAGE ? 0 : 1);
2107 if (option (OPTDELETEUNTAG))
2108 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2109 redraw = REDRAW_STATUS | REDRAW_INDEX;
2110 if (option (OPTRESOLVE)) {
2112 rc = OP_MAIN_NEXT_UNDELETED;
2116 case OP_DELETE_THREAD:
2117 case OP_DELETE_SUBTHREAD:
2118 CHECK_MODE (IsHeader (extra));
2122 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2125 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2126 ch == OP_DELETE_THREAD ? 0 : 1);
2129 if (option (OPTDELETEUNTAG))
2130 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2131 ch == OP_DELETE_THREAD ? 0 : 1);
2132 if (option (OPTRESOLVE)) {
2133 rc = OP_MAIN_NEXT_UNDELETED;
2137 if (!option (OPTRESOLVE) && PagerIndexLines)
2138 redraw = REDRAW_FULL;
2140 redraw = REDRAW_STATUS | REDRAW_INDEX;
2144 case OP_DISPLAY_ADDRESS:
2145 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2146 if (IsMsgAttach (extra))
2147 mutt_display_address (extra->bdy->hdr->env);
2149 mutt_display_address (extra->hdr->env);
2152 case OP_ENTER_COMMAND:
2153 old_smart_wrap = option (OPTWRAP);
2154 old_markers = option (OPTMARKERS);
2155 old_PagerIndexLines = PagerIndexLines;
2157 CurrentMenu = MENU_PAGER;
2158 mutt_enter_command ();
2160 if (option (OPTNEEDRESORT)) {
2161 unset_option (OPTNEEDRESORT);
2162 CHECK_MODE (IsHeader (extra));
2163 set_option (OPTNEEDRESORT);
2166 if (old_PagerIndexLines != PagerIndexLines) {
2168 mutt_menuDestroy (&index);
2172 if (option (OPTWRAP) != old_smart_wrap ||
2173 option (OPTMARKERS) != old_markers) {
2174 if (flags & M_PAGER_RETWINCH) {
2176 rc = OP_REFORMAT_WINCH;
2180 /* count the real lines above */
2182 for (i = 0; i <= topline; i++) {
2183 if (!lineInfo[i].continuation)
2187 /* we need to restart the whole thing */
2188 for (i = 0; i < maxLine; i++) {
2189 lineInfo[i].offset = 0;
2190 lineInfo[i].type = -1;
2191 lineInfo[i].continuation = 0;
2192 lineInfo[i].chunks = 0;
2193 lineInfo[i].search_cnt = -1;
2194 lineInfo[i].quote = NULL;
2196 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2197 if (SearchCompiled && lineInfo[i].search)
2198 FREE (&(lineInfo[i].search));
2201 if (SearchCompiled) {
2202 regfree (&SearchRE);
2207 /* try to keep the old position */
2210 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2211 &lastLine, &maxLine,
2212 (has_types ? M_TYPES : 0),
2213 &QuoteList, &q_level, &force_redraw,
2215 redraw |= REDRAW_SIDEBAR;
2216 if (!lineInfo[topline].continuation)
2225 if (option (OPTFORCEREDRAWPAGER))
2226 redraw = REDRAW_FULL;
2227 unset_option (OPTFORCEREDRAWINDEX);
2228 unset_option (OPTFORCEREDRAWPAGER);
2231 case OP_FLAG_MESSAGE:
2232 CHECK_MODE (IsHeader (extra));
2236 if (Context->magic == M_POP) {
2238 mutt_error _("Can't change 'important' flag on POP server.");
2245 CHECK_IMAP_ACL (IMAP_ACL_WRITE);
2249 if (Context->magic == M_NNTP) {
2251 mutt_error _("Can't change 'important' flag on NNTP server.");
2257 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2258 redraw = REDRAW_STATUS | REDRAW_INDEX;
2259 if (option (OPTRESOLVE)) {
2261 rc = OP_MAIN_NEXT_UNDELETED;
2266 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2267 if (IsAttach (extra))
2268 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2270 mutt_pipe_message (extra->hdr);
2271 MAYBE_REDRAW (redraw);
2275 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2276 if (IsAttach (extra))
2277 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2279 mutt_print_message (extra->hdr);
2283 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2285 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2286 redraw = REDRAW_FULL;
2291 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2293 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2294 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2295 query_quadoption (OPT_TOMODERATED,
2297 ("Posting to this group not allowed, may be moderated. Continue?"))
2300 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2301 redraw = REDRAW_FULL;
2304 case OP_FORWARD_TO_GROUP:
2305 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2307 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2308 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2309 query_quadoption (OPT_TOMODERATED,
2311 ("Posting to this group not allowed, may be moderated. Continue?"))
2314 if (IsMsgAttach (extra))
2315 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2316 extra->idxlen, extra->bdy, SENDNEWS);
2318 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2320 redraw = REDRAW_FULL;
2324 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2327 if (IsMsgAttach (extra))
2328 followup_to = extra->bdy->hdr->env->followup_to;
2330 followup_to = extra->hdr->env->followup_to;
2332 if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
2333 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2334 _("Reply by mail as poster prefers?")) != M_YES) {
2335 if (extra->ctx && extra->ctx->magic == M_NNTP
2336 && !((NNTP_DATA *) extra->ctx->data)->allowed
2337 && query_quadoption (OPT_TOMODERATED,
2339 ("Posting to this group not allowed, may be moderated. Continue?"))
2342 if (IsMsgAttach (extra))
2343 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2344 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2346 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2347 extra->ctx, extra->hdr);
2348 redraw = REDRAW_FULL;
2354 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2356 if (IsMsgAttach (extra))
2357 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2358 extra->idxlen, extra->bdy, SENDREPLY);
2360 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2361 redraw = REDRAW_FULL;
2364 case OP_RECALL_MESSAGE:
2365 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2367 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2368 redraw = REDRAW_FULL;
2371 case OP_GROUP_REPLY:
2372 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2374 if (IsMsgAttach (extra))
2375 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2376 extra->idxlen, extra->bdy,
2377 SENDREPLY | SENDGROUPREPLY);
2379 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2381 redraw = REDRAW_FULL;
2385 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2387 if (IsMsgAttach (extra))
2388 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2389 extra->idxlen, extra->bdy,
2390 SENDREPLY | SENDLISTREPLY);
2392 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2394 redraw = REDRAW_FULL;
2397 case OP_FORWARD_MESSAGE:
2398 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2400 if (IsMsgAttach (extra))
2401 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2402 extra->idxlen, extra->bdy, 0);
2404 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2405 redraw = REDRAW_FULL;
2408 case OP_DECRYPT_SAVE:
2415 if (IsAttach (extra)) {
2416 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2421 case OP_COPY_MESSAGE:
2422 case OP_DECODE_SAVE:
2423 case OP_DECODE_COPY:
2424 case OP_DECRYPT_COPY:
2425 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2429 CHECK_MODE (IsHeader (extra));
2430 if (mutt_save_message (extra->hdr,
2431 (ch == OP_DECRYPT_SAVE) ||
2432 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2433 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2434 (ch == OP_DECRYPT_SAVE)
2435 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2436 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2437 || ch == OP_DECRYPT_SAVE)) {
2438 if (option (OPTRESOLVE)) {
2440 rc = OP_MAIN_NEXT_UNDELETED;
2443 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2445 MAYBE_REDRAW (redraw);
2448 case OP_SHELL_ESCAPE:
2449 mutt_shell_escape ();
2450 MAYBE_REDRAW (redraw);
2454 CHECK_MODE (IsHeader (extra));
2455 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2457 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2458 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2459 ? NULL : Context->last_tag);
2461 redraw = REDRAW_STATUS | REDRAW_INDEX;
2462 if (option (OPTRESOLVE)) {
2469 CHECK_MODE (IsHeader (extra));
2473 CHECK_IMAP_ACL (IMAP_ACL_SEEN);
2476 if (extra->hdr->read || extra->hdr->old)
2477 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2479 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2481 Context->msgnotreadyet = -1;
2482 redraw = REDRAW_STATUS | REDRAW_INDEX;
2483 if (option (OPTRESOLVE)) {
2485 rc = OP_MAIN_NEXT_UNDELETED;
2490 CHECK_MODE (IsHeader (extra));
2494 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2497 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2498 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2499 redraw = REDRAW_STATUS | REDRAW_INDEX;
2500 if (option (OPTRESOLVE)) {
2506 case OP_UNDELETE_THREAD:
2507 case OP_UNDELETE_SUBTHREAD:
2508 CHECK_MODE (IsHeader (extra));
2512 CHECK_IMAP_ACL (IMAP_ACL_DELETE);
2515 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2516 ch == OP_UNDELETE_THREAD ? 0 : 1)
2517 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2518 ch == OP_UNDELETE_THREAD ? 0 : 1);
2521 if (option (OPTRESOLVE)) {
2522 rc = (ch == OP_DELETE_THREAD) ?
2523 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2527 if (!option (OPTRESOLVE) && PagerIndexLines)
2528 redraw = REDRAW_FULL;
2530 redraw = REDRAW_STATUS | REDRAW_INDEX;
2540 redraw |= REDRAW_SIDEBAR;
2543 case OP_VIEW_ATTACHMENTS:
2544 if (flags & M_PAGER_ATTACHMENT) {
2546 rc = OP_ATTACH_COLLAPSE;
2549 CHECK_MODE (IsHeader (extra));
2550 mutt_view_attachments (extra->hdr);
2551 if (extra->hdr->attach_del)
2552 Context->changed = 1;
2553 redraw = REDRAW_FULL;
2558 if (!(WithCrypto & APPLICATION_PGP)) {
2562 CHECK_MODE (IsHeader (extra));
2564 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2565 redraw = REDRAW_FULL;
2569 case OP_FORGET_PASSPHRASE:
2570 crypt_forget_passphrase ();
2573 case OP_EXTRACT_KEYS:
2578 CHECK_MODE (IsHeader (extra));
2579 crypt_extract_keys_from_messages (extra->hdr);
2580 redraw = REDRAW_FULL;
2583 case OP_SIDEBAR_SCROLL_UP:
2584 case OP_SIDEBAR_SCROLL_DOWN:
2585 case OP_SIDEBAR_NEXT:
2586 case OP_SIDEBAR_NEXT_NEW:
2587 case OP_SIDEBAR_PREV:
2588 case OP_SIDEBAR_PREV_NEW:
2589 scroll_sidebar (ch, MENU_PAGER);
2598 if (IsHeader (extra))
2599 Context->msgnotreadyet = -1;
2601 cleanup_quote (&QuoteList);
2603 for (i = 0; i < maxLine; i++) {
2604 FREE (&(lineInfo[i].syntax));
2605 if (SearchCompiled && lineInfo[i].search)
2606 FREE (&(lineInfo[i].search));
2608 if (SearchCompiled) {
2609 regfree (&SearchRE);
2614 mutt_menuDestroy (&index);
2615 return (rc != -1 ? rc : 0);