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.
20 #include "mutt_curses.h"
22 #include "mutt_menu.h"
27 #include "recvattach.h"
35 #include "imap_private.h"
38 #include "mutt_crypt.h"
44 #include "lib/debug.h"
53 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
55 #define IsAttach(x) (x && (x)->bdy)
56 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
57 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
58 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
59 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
61 static const char *Not_available_in_this_menu =
62 N_("Not available in this menu.");
63 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
64 static const char *Function_not_permitted_in_attach_message_mode =
65 N_("Function not permitted in attach-message mode.");
67 #define CHECK_MODE(x) if (!(x)) \
70 mutt_error _(Not_available_in_this_menu); \
74 #define CHECK_READONLY if (Context->readonly) \
77 mutt_error _(Mailbox_is_read_only); \
81 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
84 mutt_error _(Function_not_permitted_in_attach_message_mode); \
93 struct q_class_t *next, *prev;
94 struct q_class_t *down, *up;
109 struct syntax_t *syntax;
110 struct syntax_t *search;
111 struct q_class_t *quote;
114 #define ANSI_OFF (1<<0)
115 #define ANSI_BLINK (1<<1)
116 #define ANSI_BOLD (1<<2)
117 #define ANSI_UNDERLINE (1<<3)
118 #define ANSI_REVERSE (1<<4)
119 #define ANSI_COLOR (1<<5)
121 typedef struct _ansi_attr {
128 static short InHelp = 0;
130 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
131 static struct resize {
138 #define NumSigLines 4
140 static int check_sig (const char *s, struct line_t *info, int n)
144 while (n > 0 && count <= NumSigLines) {
145 if (info[n].type != MT_COLOR_SIGNATURE)
154 if (count > NumSigLines) {
155 /* check for a blank line */
169 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
170 int special, ansi_attr * a)
172 int def_color; /* color without syntax hilight */
173 int color; /* final color */
174 static int last_color; /* last color set */
175 int search = 0, i, m;
178 last_color = -1; /* force attrset() */
180 if (lineInfo[n].continuation) {
181 if (!cnt && option (OPTMARKERS)) {
182 SETCOLOR (MT_COLOR_MARKERS);
184 last_color = ColorDefs[MT_COLOR_MARKERS];
186 m = (lineInfo[n].syntax)[0].first;
187 cnt += (lineInfo[n].syntax)[0].last;
191 if (!(flags & M_SHOWCOLOR))
192 def_color = ColorDefs[MT_COLOR_NORMAL];
193 else if (lineInfo[m].type == MT_COLOR_HEADER)
194 def_color = (lineInfo[m].syntax)[0].color;
196 def_color = ColorDefs[lineInfo[m].type];
198 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
199 struct q_class_t *class = lineInfo[m].quote;
202 def_color = class->color;
204 while (class && class->length > cnt) {
205 def_color = class->color;
212 if (flags & M_SHOWCOLOR) {
213 for (i = 0; i < lineInfo[m].chunks; i++) {
214 /* we assume the chunks are sorted */
215 if (cnt > (lineInfo[m].syntax)[i].last)
217 if (cnt < (lineInfo[m].syntax)[i].first)
219 if (cnt != (lineInfo[m].syntax)[i].last) {
220 color = (lineInfo[m].syntax)[i].color;
223 /* don't break here, as cnt might be
224 * in the next chunk as well */
228 if (flags & M_SEARCH) {
229 for (i = 0; i < lineInfo[m].search_cnt; i++) {
230 if (cnt > (lineInfo[m].search)[i].last)
232 if (cnt < (lineInfo[m].search)[i].first)
234 if (cnt != (lineInfo[m].search)[i].last) {
235 color = ColorDefs[MT_COLOR_SEARCH];
242 /* handle "special" bold & underlined characters */
243 if (special || a->attr) {
245 if ((a->attr & ANSI_COLOR)) {
247 a->pair = mutt_alloc_color (a->fg, a->bg);
249 if (a->attr & ANSI_BOLD)
254 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
255 if (ColorDefs[MT_COLOR_BOLD] && !search)
256 color = ColorDefs[MT_COLOR_BOLD];
260 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
261 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
262 color = ColorDefs[MT_COLOR_UNDERLINE];
264 color ^= A_UNDERLINE;
266 else if (a->attr & ANSI_REVERSE) {
269 else if (a->attr & ANSI_BLINK) {
272 else if (a->attr & ANSI_OFF) {
277 if (color != last_color) {
283 static void append_line (struct line_t *lineInfo, int n, int cnt)
287 lineInfo[n + 1].type = lineInfo[n].type;
288 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
289 lineInfo[n + 1].continuation = 1;
291 /* find the real start of the line */
292 for (m = n; m >= 0; m--)
293 if (lineInfo[m].continuation == 0)
296 (lineInfo[n + 1].syntax)[0].first = m;
297 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
298 cnt + (lineInfo[n].syntax)[0].last : cnt;
301 static void new_class_color (struct q_class_t *class, int *q_level)
303 class->index = (*q_level)++;
304 class->color = ColorQuote[class->index % ColorQuoteUsed];
308 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
309 int index, int *q_level)
311 struct q_class_t *q_list;
314 new_class->index = -1;
317 if (q_list->index >= index) {
319 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
322 q_list = q_list->down;
323 else if (q_list->next)
324 q_list = q_list->next;
326 while (!q_list->next) {
332 q_list = q_list->next;
336 new_class->index = index;
337 new_class->color = ColorQuote[index % ColorQuoteUsed];
341 static void cleanup_quote (struct q_class_t **QuoteList)
343 struct q_class_t *ptr;
346 if ((*QuoteList)->down)
347 cleanup_quote (&((*QuoteList)->down));
348 ptr = (*QuoteList)->next;
349 if ((*QuoteList)->prefix)
350 mem_free (&(*QuoteList)->prefix);
351 mem_free (QuoteList);
358 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
359 const char *qptr, int length,
360 int *force_redraw, int *q_level)
362 struct q_class_t *q_list = *QuoteList;
363 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
365 int offset, tail_lng;
368 if (ColorQuoteUsed <= 1) {
369 /* not much point in classifying quotes... */
371 if (*QuoteList == NULL) {
372 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
373 class->color = ColorQuote[0];
379 /* Did I mention how much I like emulating Lisp in C? */
381 /* classify quoting prefix */
383 if (length <= q_list->length) {
384 /* case 1: check the top level nodes */
386 if (str_ncmp (qptr, q_list->prefix, length) == 0) {
387 if (length == q_list->length)
388 return q_list; /* same prefix: return the current class */
390 /* found shorter prefix */
392 /* add a node above q_list */
394 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
395 tmp->prefix = (char *) mem_calloc (1, length + 1);
396 strncpy (tmp->prefix, qptr, length);
397 tmp->length = length;
399 /* replace q_list by tmp in the top level list */
401 tmp->next = q_list->next;
402 q_list->next->prev = tmp;
405 tmp->prev = q_list->prev;
406 q_list->prev->next = tmp;
409 /* make q_list a child of tmp */
413 /* q_list has no siblings for now */
417 /* update the root if necessary */
418 if (q_list == *QuoteList)
421 index = q_list->index;
423 /* tmp should be the return class too */
426 /* next class to test; if tmp is a shorter prefix for another
427 * node, that node can only be in the top level list, so don't
428 * go down after this point
433 /* found another branch for which tmp is a shorter prefix */
435 /* save the next sibling for later */
438 /* unlink q_list from the top level list */
440 q_list->next->prev = q_list->prev;
442 q_list->prev->next = q_list->next;
444 /* at this point, we have a tmp->down; link q_list to it */
446 /* sibling order is important here, q_list should be linked last */
454 index = q_list->index;
456 /* next class to test; as above, we shouldn't go down */
460 /* we found a shorter prefix, so certain quotes have changed classes */
465 /* shorter, but not a substring of the current class: try next */
466 q_list = q_list->next;
471 /* case 2: try subclassing the current top level node */
473 /* tmp != NULL means we already found a shorter prefix at case 1 */
475 && str_ncmp (qptr, q_list->prefix, q_list->length) == 0) {
476 /* ok, it's a subclass somewhere on this branch */
479 offset = q_list->length;
481 q_list = q_list->down;
482 tail_lng = length - offset;
483 tail_qptr = (char *) qptr + offset;
486 if (length <= q_list->length) {
487 if (str_ncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
489 /* same prefix: return the current class */
490 if (length == q_list->length)
493 /* found shorter common prefix */
495 /* add a node above q_list */
496 tmp = (struct q_class_t *) mem_calloc (1,
499 tmp->prefix = (char *) mem_calloc (1, length + 1);
500 strncpy (tmp->prefix, qptr, length);
501 tmp->length = length;
503 /* replace q_list by tmp */
505 tmp->next = q_list->next;
506 q_list->next->prev = tmp;
509 tmp->prev = q_list->prev;
510 q_list->prev->next = tmp;
513 /* make q_list a child of tmp */
515 tmp->up = q_list->up;
517 if (tmp->up->down == q_list)
520 /* q_list has no siblings */
524 index = q_list->index;
526 /* tmp should be the return class too */
529 /* next class to test */
533 /* found another branch for which tmp is a shorter prefix */
535 /* save the next sibling for later */
538 /* unlink q_list from the top level list */
540 q_list->next->prev = q_list->prev;
542 q_list->prev->next = q_list->next;
544 /* at this point, we have a tmp->down; link q_list to it */
553 index = q_list->index;
555 /* next class to test */
559 /* we found a shorter prefix, so we need a redraw */
564 q_list = q_list->next;
569 /* longer than the current prefix: try subclassing it */
571 && str_ncmp (tail_qptr, (q_list->prefix) + offset,
572 q_list->length - offset) == 0) {
573 /* still a subclass: go down one level */
575 offset = q_list->length;
577 q_list = q_list->down;
578 tail_lng = length - offset;
579 tail_qptr = (char *) qptr + offset;
584 /* nope, try the next prefix */
585 q_list = q_list->next;
591 /* still not found so far: add it as a sibling to the current node */
594 (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
595 tmp->prefix = (char *) mem_calloc (1, length + 1);
596 strncpy (tmp->prefix, qptr, length);
597 tmp->length = length;
600 tmp->next = ptr->down;
601 ptr->down->prev = tmp;
606 new_class_color (tmp, q_level);
612 shift_class_colors (*QuoteList, tmp, index, q_level);
618 /* nope, try the next prefix */
619 q_list = q_list->next;
626 /* not found so far: add it as a top level class */
627 class = (struct q_class_t *) mem_calloc (1, sizeof (struct q_class_t));
628 class->prefix = (char *) mem_calloc (1, length + 1);
629 strncpy (class->prefix, qptr, length);
630 class->length = length;
631 new_class_color (class, q_level);
634 class->next = *QuoteList;
635 (*QuoteList)->prev = class;
641 shift_class_colors (*QuoteList, tmp, index, q_level);
646 static int check_attachment_marker (char *);
649 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
650 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
653 COLOR_LINE *color_line;
654 regmatch_t pmatch[1], smatch[1];
655 int found, offset, null_rx, i;
657 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
659 lineInfo[n].type = MT_COLOR_NORMAL;
660 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
661 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
662 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
665 lineInfo[n].type = MT_COLOR_HDEFAULT;
666 color_line = ColorHdrList;
668 if (REGEXEC (&color_line->rx, buf) == 0) {
669 lineInfo[n].type = MT_COLOR_HEADER;
670 lineInfo[n].syntax[0].color = color_line->pair;
673 color_line = color_line->next;
677 else if (str_ncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
678 lineInfo[n].type = MT_COLOR_NORMAL;
680 else if (str_ncmp ("[-- ", buf, 4) == 0)
681 lineInfo[n].type = MT_COLOR_ATTACHMENT;
683 else if (check_attachment_marker ((char *) raw) == 0)
684 lineInfo[n].type = MT_COLOR_ATTACHMENT;
686 else if (str_cmp ("-- \n", buf) == 0
687 || str_cmp ("-- \r\n", buf) == 0) {
690 lineInfo[n].type = MT_COLOR_SIGNATURE;
691 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
692 (lineInfo[i].type == MT_COLOR_NORMAL ||
693 lineInfo[i].type == MT_COLOR_QUOTED ||
694 lineInfo[i].type == MT_COLOR_HEADER)) {
696 if (lineInfo[i].chunks) {
697 lineInfo[i].chunks = 0;
698 mem_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
700 lineInfo[i++].type = MT_COLOR_SIGNATURE;
703 else if (check_sig (buf, lineInfo, n - 1) == 0)
704 lineInfo[n].type = MT_COLOR_SIGNATURE;
705 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
706 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
707 if (smatch[0].rm_so > 0) {
710 /* hack to avoid making an extra copy of buf */
711 c = buf[smatch[0].rm_so];
712 buf[smatch[0].rm_so] = 0;
714 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
715 if (q_classify && lineInfo[n].quote == NULL)
716 lineInfo[n].quote = classify_quote (QuoteList,
717 buf + pmatch[0].rm_so,
719 pmatch[0].rm_so, force_redraw,
721 lineInfo[n].type = MT_COLOR_QUOTED;
724 lineInfo[n].type = MT_COLOR_NORMAL;
726 buf[smatch[0].rm_so] = c;
729 lineInfo[n].type = MT_COLOR_NORMAL;
732 if (q_classify && lineInfo[n].quote == NULL)
733 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
734 pmatch[0].rm_eo - pmatch[0].rm_so,
735 force_redraw, q_level);
736 lineInfo[n].type = MT_COLOR_QUOTED;
740 lineInfo[n].type = MT_COLOR_NORMAL;
743 if (lineInfo[n].type == MT_COLOR_NORMAL ||
744 lineInfo[n].type == MT_COLOR_QUOTED) {
748 lineInfo[n].chunks = 0;
755 color_line = ColorBodyList;
757 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
758 (offset ? REG_NOTBOL : 0)) == 0) {
759 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
761 if (++(lineInfo[n].chunks) > 1)
762 mem_realloc (&(lineInfo[n].syntax),
763 (lineInfo[n].chunks) *
764 sizeof (struct syntax_t));
766 i = lineInfo[n].chunks - 1;
767 pmatch[0].rm_so += offset;
768 pmatch[0].rm_eo += offset;
770 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
771 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
772 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
773 (lineInfo[n].syntax)[i].color = color_line->pair;
774 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
775 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
781 null_rx = 1; /* empty regexp; don't add it, but keep looking */
783 color_line = color_line->next;
787 offset++; /* avoid degenerate cases */
789 offset = (lineInfo[n].syntax)[i].last;
790 } while (found || null_rx);
794 static int is_ansi (unsigned char *buf)
796 while (*buf && (isdigit (*buf) || *buf == ';'))
798 return (*buf == 'm');
801 static int check_attachment_marker (char *p)
803 char *q = AttachmentMarker;
805 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
806 return (int) (*p - *q);
809 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
813 while (isdigit (buf[x]) || buf[x] == ';')
816 /* Character Attributes */
817 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
821 mutt_free_color (a->fg, a->bg);
827 if (buf[pos] == '1' && (pos + 1 == x || buf[pos + 1] == ';')) {
828 a->attr |= ANSI_BOLD;
831 else if (buf[pos] == '4' && (pos + 1 == x || buf[pos + 1] == ';')) {
832 a->attr |= ANSI_UNDERLINE;
835 else if (buf[pos] == '5' && (pos + 1 == x || buf[pos + 1] == ';')) {
836 a->attr |= ANSI_BLINK;
839 else if (buf[pos] == '7' && (pos + 1 == x || buf[pos + 1] == ';')) {
840 a->attr |= ANSI_REVERSE;
843 else if (buf[pos] == '0' && (pos + 1 == x || buf[pos + 1] == ';')) {
846 mutt_free_color (a->fg, a->bg);
852 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
855 mutt_free_color (a->fg, a->bg);
858 a->attr |= ANSI_COLOR;
859 a->fg = buf[pos + 1] - '0';
862 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
865 mutt_free_color (a->fg, a->bg);
868 a->attr |= ANSI_COLOR;
869 a->bg = buf[pos + 1] - '0';
873 while (pos < x && buf[pos] != ';')
883 /* trim tail of buf so that it contains complete multibyte characters */
884 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
888 memset (&mbstate, 0, sizeof (mbstate));
889 for (; len > 0; buf += k, len -= k) {
890 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
893 else if (k == -1 || k == 0)
902 fill_buffer (FILE * f, long *last_pos, long offset, unsigned char *buf,
903 unsigned char *fmt, size_t blen, int *buf_ready)
906 static int b_read = 0;
908 if (*buf_ready == 0) {
910 if (offset != *last_pos)
911 fseek (f, offset, 0);
912 if (fgets ((char *) buf, blen - 1, f) == NULL) {
916 *last_pos = ftell (f);
917 b_read = (int) (*last_pos - offset);
920 /* incomplete mbyte characters trigger a segfault in regex processing for
921 * certain versions of glibc. Trim them if necessary. */
922 if (b_read == blen - 2)
923 b_read -= trim_incomplete_mbyte(buf, b_read);
925 /* copy "buf" to "fmt", but without bold and underline controls */
928 if (*p == '\010' && (p > buf)) {
929 if (*(p + 1) == '_') /* underline */
931 else if (*(p + 1)) { /* bold or overstrike */
932 *(fmt - 1) = *(p + 1);
938 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
939 while (*p++ != 'm') /* skip ANSI sequence */
942 else if (*p == '\033' && *(p + 1) == ']'
943 && check_attachment_marker ((char *) p) == 0) {
944 debug_print (2, ("seen attachment marker.\n"));
945 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
962 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
963 int flags, ansi_attr * pa, int cnt,
964 int *pspace, int *pvch, int *pcol, int *pspecial)
966 int space = -1; /* index of the last space or TAB */
967 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
968 int ch, vch, k, last_special = -1, special = 0, t;
972 int wrap_cols = COLS;
974 if (!(flags & (M_SHOWFLAT)))
975 wrap_cols -= WrapMargin;
976 wrap_cols -= SidebarWidth;
981 /* FIXME: this should come from lineInfo */
982 memset (&mbstate, 0, sizeof (mbstate));
984 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
985 /* Handle ANSI sequences */
986 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
987 is_ansi (buf + ch + 2))
988 ch = grok_ansi (buf, ch + 2, pa) + 1;
990 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
991 check_attachment_marker ((char *) buf + ch) == 0) {
992 while (buf[ch++] != '\a')
997 /* is anything left to do? */
1001 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
1002 if (k == -2 || k == -1) {
1003 debug_print (1, ("mbrtowc returned %d; errno = %d.\n", k, errno));
1004 if (col + 4 > wrap_cols)
1008 printw ("\\%03o", buf[ch]);
1015 /* Handle backspace */
1017 if (IsWPrint (wc)) {
1022 while ((wc1 = 0, mbstate1 = mbstate,
1024 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1025 &mbstate1), k1 - k > 0 && wc1 == '\b')
1027 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1028 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1030 special |= (wc == '_' && special & A_UNDERLINE)
1031 ? A_UNDERLINE : A_BOLD;
1033 else if (wc == '_' || wc1 == '_') {
1034 special |= A_UNDERLINE;
1035 wc = (wc1 == '_') ? wc : wc1;
1038 /* special = 0; / * overstrike: nothing to do! */
1048 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1049 special || last_special || pa->attr)) {
1050 resolve_color (*lineInfo, n, vch, flags, special, pa);
1051 last_special = special;
1054 if (IsWPrint (wc)) {
1058 if (col + t > wrap_cols)
1064 else if (wc == '\n')
1066 else if (wc == '\t') {
1072 for (; col < t; col++)
1077 else if (wc < 0x20 || wc == 0x7f) {
1078 if (col + 2 > wrap_cols)
1082 printw ("^%c", ('@' + wc) & 0x7f);
1084 else if (wc < 0x100) {
1085 if (col + 4 > wrap_cols)
1089 printw ("\\%03o", wc);
1092 if (col + 1 > wrap_cols)
1096 addch (replacement_char ());
1102 *pspecial = special;
1108 * flags M_SHOWFLAT, show characters (used for displaying help)
1109 * M_SHOWCOLOR, show characters in color
1110 * otherwise don't show characters
1111 * M_HIDE, don't show quoted text
1112 * M_SEARCH, resolve search patterns
1113 * M_TYPES, compute line's type
1114 * M_PAGER_NSKIP, keeps leading whitespace
1115 * M_PAGER_MARKER, eventually show markers
1118 * -1 EOF was reached
1119 * 0 normal exit, line was not displayed
1120 * >0 normal exit, line was displayed
1124 display_line (FILE * f, long *last_pos, struct line_t **lineInfo, int n,
1125 int *last, int *max, int flags, struct q_class_t **QuoteList,
1126 int *q_level, int *force_redraw, regex_t * SearchRE)
1128 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1129 unsigned char *buf_ptr = buf;
1130 int ch, vch, col, cnt, b_read;
1131 int buf_ready = 0, change_last = 0;
1136 ansi_attr a = { 0, 0, 0, -1 };
1137 regmatch_t pmatch[1];
1144 if (*last == *max) {
1145 mem_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1146 for (ch = *last; ch < *max; ch++) {
1147 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1148 (*lineInfo)[ch].type = -1;
1149 (*lineInfo)[ch].search_cnt = -1;
1150 (*lineInfo)[ch].syntax = mem_malloc (sizeof (struct syntax_t));
1151 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1156 /* only do color hiliting if we are viewing a message */
1157 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1158 if ((*lineInfo)[n].type == -1) {
1159 /* determine the line class */
1161 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1168 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1169 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1171 /* avoid race condition for continuation lines when scrolling up */
1173 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1175 (*lineInfo)[m].type = (*lineInfo)[n].type;
1178 /* this also prevents searching through the hidden lines */
1179 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1180 flags = 0; /* M_NOSHOW */
1183 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1184 * don't want to compute it every time M_TYPES is set, since this
1185 * would slow down the "bottom" function unacceptably. A compromise
1186 * solution is hence to call regexec() again, just to find out the
1187 * length of the quote prefix.
1189 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1190 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1193 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1199 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1200 (*lineInfo)[n].quote = classify_quote (QuoteList,
1201 (char *) fmt + pmatch[0].rm_so,
1202 pmatch[0].rm_eo - pmatch[0].rm_so,
1203 force_redraw, q_level);
1206 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1207 && (*lineInfo)[n].search_cnt == -1) {
1209 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1217 (*lineInfo)[n].search_cnt = 0;
1219 (SearchRE, (char *) fmt + offset, 1, pmatch,
1220 (offset ? REG_NOTBOL : 0)) == 0) {
1221 if (++((*lineInfo)[n].search_cnt) > 1)
1222 mem_realloc (&((*lineInfo)[n].search),
1223 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1225 (*lineInfo)[n].search = mem_malloc (sizeof (struct syntax_t));
1226 pmatch[0].rm_so += offset;
1227 pmatch[0].rm_eo += offset;
1228 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1230 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1233 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1234 offset++; /* avoid degenerate cases */
1236 offset = pmatch[0].rm_eo;
1242 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1243 /* we've already scanned this line, so just exit */
1246 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1247 /* no need to try to display this line... */
1248 return (1); /* fake display */
1251 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1252 sizeof (buf), &buf_ready)) < 0) {
1258 /* now chose a good place to break the line */
1260 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1262 buf_ptr = buf + cnt;
1264 /* move the break point only if smart_wrap is set */
1265 if (option (OPTWRAP)) {
1267 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1268 && buf[cnt] != '\r') {
1270 /* skip trailing blanks */
1271 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1273 /* a very long word with leading spaces causes infinite wrapping */
1274 if ((!ch) && (flags & M_PAGER_NSKIP))
1275 buf_ptr = buf + cnt;
1280 buf_ptr = buf + cnt; /* a very long word... */
1282 if (!(flags & M_PAGER_NSKIP))
1283 /* skip leading blanks on the next line too */
1284 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1288 if (*buf_ptr == '\r')
1290 if (*buf_ptr == '\n')
1293 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1294 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1295 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1297 /* if we don't need to display the line we are done */
1298 if (!(flags & M_SHOW))
1301 /* display the line */
1302 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1304 /* avoid a bug in ncurses... */
1305 #ifndef USE_SLANG_CURSES
1307 SETCOLOR (MT_COLOR_NORMAL);
1312 /* end the last color pattern (needed by S-Lang) */
1313 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1314 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1317 * Fill the blank space at the end of the line with the prevailing color.
1318 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1319 * to make sure to reset the color *after* that
1321 if (flags & M_SHOWCOLOR) {
1322 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1323 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1324 def_color = ((*lineInfo)[m].syntax)[0].color;
1326 def_color = ColorDefs[(*lineInfo)[m].type];
1328 attrset (def_color);
1330 bkgdset (def_color | ' ');
1334 /* ncurses always wraps lines when you get to the right side of the
1335 * screen, but S-Lang seems to only wrap if the next character is *not*
1338 #ifndef USE_SLANG_CURSES
1344 * reset the color back to normal. This *must* come after the
1345 * addch('\n'), otherwise the color for this line will not be
1346 * filled to the right margin.
1348 if (flags & M_SHOWCOLOR) {
1349 SETCOLOR (MT_COLOR_NORMAL);
1350 BKGDSET (MT_COLOR_NORMAL);
1353 /* build a return code */
1354 if (!(flags & M_SHOW))
1360 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1362 while (cur > 0 && nlines > 0) {
1364 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1371 static struct mapping_t PagerHelp[] = {
1372 {N_("Exit"), OP_EXIT},
1373 {N_("PrevPg"), OP_PREV_PAGE},
1374 {N_("NextPg"), OP_NEXT_PAGE},
1377 static struct mapping_t PagerHelpExtra[] = {
1378 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1379 {N_("Del"), OP_DELETE},
1380 {N_("Reply"), OP_REPLY},
1381 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1386 static struct mapping_t PagerNewsHelpExtra[] = {
1387 {N_("Post"), OP_POST},
1388 {N_("Followup"), OP_FOLLOWUP},
1389 {N_("Del"), OP_DELETE},
1390 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1397 /* This pager is actually not so simple as it once was. It now operates in
1398 two modes: one for viewing messages and the other for viewing help. These
1399 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1400 is there so that we can do operations on the current message without the
1401 need to pop back out to the main-menu. */
1403 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1405 static char searchbuf[STRING];
1406 char buffer[LONG_STRING];
1407 char helpstr[SHORT_STRING * 2];
1408 char tmphelp[SHORT_STRING * 2];
1409 int maxLine, lastLine = 0;
1410 struct line_t *lineInfo;
1411 struct q_class_t *QuoteList = NULL;
1412 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1413 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1415 int redraw = REDRAW_FULL;
1417 long last_pos = 0, last_offset = 0;
1418 int old_smart_wrap, old_markers;
1421 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1422 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1424 int bodyoffset = 1; /* offset of first line of real text */
1425 int statusoffset = 0; /* offset for the status bar */
1426 int helpoffset = LINES - 2; /* offset for the help bar. */
1427 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1429 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1430 int indexoffset = 0; /* offset for the PI */
1431 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1432 int indicator = indexlen / 3; /* the indicator line of the PI */
1433 int old_PagerIndexLines; /* some people want to resize it
1434 * while inside the pager... */
1440 if (!(flags & M_SHOWCOLOR))
1441 flags |= M_SHOWFLAT;
1443 if ((fp = fopen (fname, "r")) == NULL) {
1444 mutt_perror (fname);
1448 if (stat (fname, &sb) != 0) {
1449 mutt_perror (fname);
1455 /* Initialize variables */
1457 if (IsHeader (extra) && !extra->hdr->read) {
1458 Context->msgnotreadyet = extra->hdr->msgno;
1459 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1462 lineInfo = mem_malloc (sizeof (struct line_t) * (maxLine = LINES));
1463 for (i = 0; i < maxLine; i++) {
1464 memset (&lineInfo[i], 0, sizeof (struct line_t));
1465 lineInfo[i].type = -1;
1466 lineInfo[i].search_cnt = -1;
1467 lineInfo[i].syntax = mem_malloc (sizeof (struct syntax_t));
1468 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1471 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1472 if (IsHeader (extra)) {
1473 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1474 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1477 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1480 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1483 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1484 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1485 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1495 if (redraw & REDRAW_FULL) {
1496 SETCOLOR (MT_COLOR_NORMAL);
1497 /* clear() doesn't optimize screen redraws */
1501 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1502 indexlen = Context->vcount + 1;
1504 indexlen = PagerIndexLines;
1506 indicator = indexlen / 3;
1508 if (option (OPTSTATUSONTOP)) {
1510 statusoffset = IsHeader (extra) ? indexlen : 0;
1511 bodyoffset = statusoffset + 1;
1512 helpoffset = LINES - 2;
1513 bodylen = helpoffset - bodyoffset;
1514 if (!option (OPTHELP))
1520 statusoffset = LINES - 2;
1521 if (!option (OPTHELP))
1523 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1524 bodylen = statusoffset - bodyoffset;
1527 if (option (OPTHELP)) {
1528 SETCOLOR (MT_COLOR_STATUS);
1529 move (helpoffset, 0);
1530 mutt_paddstr (COLS, helpstr);
1531 SETCOLOR (MT_COLOR_NORMAL);
1534 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1535 if (Resize != NULL) {
1536 if ((SearchCompiled = Resize->SearchCompiled)) {
1538 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1539 SearchFlag = M_SEARCH;
1540 SearchBack = Resize->SearchBack;
1542 lines = Resize->line;
1543 redraw |= REDRAW_SIGWINCH;
1549 if (IsHeader (extra) && PagerIndexLines) {
1550 if (index == NULL) {
1551 /* only allocate the space if/when we need the index.
1552 Initialise the menu as per the main index */
1553 index = mutt_new_menu ();
1554 index->menu = MENU_MAIN;
1555 index->make_entry = index_make_entry;
1556 index->color = index_color;
1557 index->max = Context->vcount;
1558 index->current = extra->hdr->virtual;
1561 SETCOLOR (MT_COLOR_NORMAL);
1562 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1564 index->pagelen = indexlen - 1;
1566 /* some fudge to work out where abouts the indicator should go */
1567 if (index->current - indicator < 0)
1569 else if (index->max - index->current < index->pagelen - indicator)
1570 index->top = index->max - index->pagelen;
1572 index->top = index->current - indicator;
1574 menu_redraw_index (index);
1577 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1581 if (redraw & REDRAW_SIGWINCH) {
1584 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1585 has_types | SearchFlag, &QuoteList, &q_level,
1586 &force_redraw, &SearchRE) == 0) {
1587 if (!lineInfo[i].continuation && ++j == lines) {
1592 redraw |= REDRAW_SIDEBAR;
1596 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1598 move (bodyoffset, SidebarWidth);
1599 curline = oldtopline = topline;
1603 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1604 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1606 (flags & M_DISPLAYFLAGS) | hideQuoted |
1607 SearchFlag, &QuoteList, &q_level, &force_redraw,
1611 move (lines + bodyoffset, SidebarWidth);
1612 redraw |= REDRAW_SIDEBAR;
1614 last_offset = lineInfo[curline].offset;
1615 } while (force_redraw);
1617 SETCOLOR (MT_COLOR_TILDE);
1618 BKGDSET (MT_COLOR_TILDE);
1619 while (lines < bodylen) {
1621 if (option (OPTTILDE))
1625 move (lines + bodyoffset, SidebarWidth);
1627 /* We are going to update the pager status bar, so it isn't
1628 * necessary to reset to normal color now. */
1630 redraw |= REDRAW_STATUS; /* need to update the % seen */
1633 if (redraw & REDRAW_STATUS) {
1634 /* print out the pager status bar */
1635 SETCOLOR (MT_COLOR_STATUS);
1636 BKGDSET (MT_COLOR_STATUS);
1637 CLEARLINE_WIN (statusoffset);
1638 if (IsHeader (extra)) {
1639 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1640 size_t l2 = sizeof (buffer);
1642 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1643 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1645 else if (IsMsgAttach (extra)) {
1646 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1647 size_t l2 = sizeof (buffer);
1649 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1650 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1652 if (option(OPTSTATUSONTOP)) {
1655 /*move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (InHelp?(LINES-2):(indexlen - 1))),
1656 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);*/
1657 mutt_paddstr (COLS - 10 - (option(OPTSTATUSONTOP)?0:SidebarWidth), IsHeader (extra)
1658 || IsMsgAttach (extra) ? buffer : banner);
1661 if (last_pos < sb.st_size - 1)
1662 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1664 addstr (topline == 0 ? "all)" : "end)");
1665 BKGDSET (MT_COLOR_NORMAL);
1666 SETCOLOR (MT_COLOR_NORMAL);
1669 if (redraw & REDRAW_SIDEBAR)
1670 sidebar_draw (MENU_PAGER);
1672 if ((redraw & REDRAW_INDEX) && index) {
1673 /* redraw the pager_index indicator, because the
1674 * flags for this message might have changed. */
1675 menu_redraw_current (index);
1676 sidebar_draw (MENU_PAGER);
1677 /* print out the index status bar */
1678 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1679 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1680 option (OPTSTATUSONTOP) ? 0 : SidebarWidth);
1681 SETCOLOR (MT_COLOR_STATUS);
1682 BKGDSET (MT_COLOR_STATUS);
1683 mutt_paddstr (COLS - (option (OPTSTATUSONTOP) ? 0 : SidebarWidth),
1685 SETCOLOR (MT_COLOR_NORMAL);
1686 BKGDSET (MT_COLOR_NORMAL);
1688 /* if we're not using the index, update every time */
1690 sidebar_draw (MENU_PAGER);
1694 move (statusoffset, COLS - 1);
1696 ch = km_dokey (MENU_PAGER);
1698 mutt_clear_error ();
1705 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1706 else if (SigWinch) {
1707 mutt_resize_screen ();
1709 /* Store current position. */
1711 for (i = 0; i <= topline; i++)
1712 if (!lineInfo[i].continuation)
1715 if (flags & M_PAGER_RETWINCH) {
1716 Resize = mem_malloc (sizeof (struct resize));
1718 Resize->line = lines;
1719 Resize->SearchCompiled = SearchCompiled;
1720 Resize->SearchBack = SearchBack;
1723 rc = OP_REFORMAT_WINCH;
1726 for (i = 0; i < maxLine; i++) {
1727 lineInfo[i].offset = 0;
1728 lineInfo[i].type = -1;
1729 lineInfo[i].continuation = 0;
1730 lineInfo[i].chunks = 0;
1731 lineInfo[i].search_cnt = -1;
1732 lineInfo[i].quote = NULL;
1734 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1735 if (SearchCompiled && lineInfo[i].search)
1736 mem_free (&(lineInfo[i].search));
1742 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1747 clearok (stdscr, TRUE); /*force complete redraw */
1751 else if (ch == -1) {
1765 if (lineInfo[curline].offset < sb.st_size - 1) {
1766 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1768 else if (option (OPTPAGERSTOP)) {
1769 /* emulate "less -q" and don't go on to the next message. */
1770 mutt_error _("Bottom of message is shown.");
1773 /* end of the current message, so display the next message. */
1774 rc = OP_MAIN_NEXT_UNDELETED;
1782 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1785 mutt_error _("Top of message is shown.");
1789 if (lineInfo[curline].offset < sb.st_size - 1) {
1792 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1798 mutt_error _("Bottom of message is shown.");
1803 topline = upNLines (1, lineInfo, topline, hideQuoted);
1805 mutt_error _("Top of message is shown.");
1812 mutt_error _("Top of message is shown.");
1817 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1819 mutt_error _("Top of message is shown.");
1823 if (lineInfo[curline].offset < sb.st_size - 1) {
1824 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1826 else if (option (OPTPAGERSTOP)) {
1827 /* emulate "less -q" and don't go on to the next message. */
1828 mutt_error _("Bottom of message is shown.");
1831 /* end of the current message, so display the next message. */
1832 rc = OP_MAIN_NEXT_UNDELETED;
1837 case OP_SEARCH_NEXT:
1838 case OP_SEARCH_OPPOSITE:
1839 if (SearchCompiled) {
1841 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1842 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1843 /* searching forward */
1844 for (i = topline + 1; i < lastLine; i++) {
1845 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1846 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1853 mutt_error _("Not found.");
1856 /* searching backward */
1857 for (i = topline - 1; i >= 0; i--) {
1858 if ((!hideQuoted || (has_types &&
1859 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1860 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1867 mutt_error _("Not found.");
1870 if (lineInfo[topline].search_cnt > 0)
1871 SearchFlag = M_SEARCH;
1875 /* no previous search pattern, so fall through to search */
1878 case OP_SEARCH_REVERSE:
1879 strfcpy (buffer, searchbuf, sizeof (buffer));
1880 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1881 _("Search: ")), buffer, sizeof (buffer),
1885 if (!strcmp (buffer, searchbuf)) {
1886 if (SearchCompiled) {
1887 /* do an implicit search-next */
1888 if (ch == OP_SEARCH)
1889 ch = OP_SEARCH_NEXT;
1891 ch = OP_SEARCH_OPPOSITE;
1900 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1902 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1903 if (ch == OP_SEARCH)
1905 else if (ch == OP_SEARCH_REVERSE)
1908 if (SearchCompiled) {
1909 regfree (&SearchRE);
1910 for (i = 0; i < lastLine; i++) {
1911 if (lineInfo[i].search)
1912 mem_free (&(lineInfo[i].search));
1913 lineInfo[i].search_cnt = -1;
1918 REGCOMP (&SearchRE, searchbuf,
1919 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1920 regerror (err, &SearchRE, buffer, sizeof (buffer));
1921 mutt_error ("%s", buffer);
1922 regfree (&SearchRE);
1923 for (i = 0; i < maxLine; i++) {
1925 if (lineInfo[i].search)
1926 mem_free (&(lineInfo[i].search));
1927 lineInfo[i].search_cnt = -1;
1934 /* update the search pointers */
1936 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1937 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1938 &QuoteList, &q_level,
1939 &force_redraw, &SearchRE) == 0) {
1941 redraw |= REDRAW_SIDEBAR;
1945 /* searching forward */
1946 for (i = topline; i < lastLine; i++) {
1947 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1948 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1956 /* searching backward */
1957 for (i = topline; i >= 0; i--) {
1958 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1959 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1967 if (lineInfo[topline].search_cnt == 0) {
1969 mutt_error _("Not found.");
1972 SearchFlag = M_SEARCH;
1974 redraw = REDRAW_BODY;
1977 case OP_SEARCH_TOGGLE:
1978 if (SearchCompiled) {
1979 SearchFlag ^= M_SEARCH;
1980 redraw = REDRAW_BODY;
1985 /* don't let the user enter the help-menu from the help screen! */
1988 mutt_help (MENU_PAGER);
1989 redraw = REDRAW_FULL;
1993 mutt_error _("Help is currently being shown.");
1996 case OP_PAGER_HIDE_QUOTED:
1998 hideQuoted ^= M_HIDE;
1999 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2000 topline = upNLines (1, lineInfo, topline, hideQuoted);
2002 redraw = REDRAW_BODY;
2006 case OP_PAGER_SKIP_QUOTED:
2009 int new_topline = topline;
2011 while ((new_topline < lastLine ||
2012 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2013 new_topline, &lastLine,
2014 &maxLine, M_TYPES, &QuoteList,
2015 &q_level, &force_redraw,
2017 && lineInfo[new_topline].type != MT_COLOR_QUOTED) {
2018 redraw |= REDRAW_SIDEBAR;
2023 mutt_error _("No more quoted text.");
2028 while ((new_topline < lastLine ||
2029 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2030 new_topline, &lastLine,
2031 &maxLine, M_TYPES, &QuoteList,
2032 &q_level, &force_redraw,
2034 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2036 redraw |= REDRAW_SIDEBAR;
2040 mutt_error _("No more unquoted text after quoted text.");
2044 topline = new_topline;
2048 case OP_PAGER_BOTTOM: /* move to the end of the file */
2049 if (lineInfo[curline].offset < sb.st_size - 1) {
2051 /* make sure the types are defined to the end of file */
2052 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2053 &maxLine, has_types,
2054 &QuoteList, &q_level, &force_redraw,
2057 redraw |= REDRAW_SIDEBAR;
2059 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2062 mutt_error _("Bottom of message is shown.");
2066 clearok (stdscr, TRUE);
2067 redraw = REDRAW_FULL;
2071 km_error_key (MENU_PAGER);
2074 /* --------------------------------------------------------------------
2075 * The following are operations on the current message rather than
2076 * adjusting the view of the message.
2079 case OP_BOUNCE_MESSAGE:
2080 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2082 if (IsMsgAttach (extra))
2083 mutt_attach_bounce (extra->fp, extra->hdr,
2084 extra->idx, extra->idxlen, extra->bdy);
2086 ci_bounce_message (extra->hdr, &redraw);
2090 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2092 if (IsMsgAttach (extra))
2093 mutt_attach_resend (extra->fp, extra->hdr,
2094 extra->idx, extra->idxlen, extra->bdy);
2096 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2097 redraw = REDRAW_FULL;
2100 case OP_CHECK_TRADITIONAL:
2101 CHECK_MODE (IsHeader (extra));
2102 if (!(WithCrypto & APPLICATION_PGP))
2104 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2106 rc = OP_CHECK_TRADITIONAL;
2110 case OP_CREATE_ALIAS:
2111 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2112 if (IsMsgAttach (extra))
2113 mutt_create_alias (extra->bdy->hdr->env, NULL);
2115 mutt_create_alias (extra->hdr->env, NULL);
2116 MAYBE_REDRAW (redraw);
2119 case OP_PURGE_MESSAGE:
2121 CHECK_MODE (IsHeader (extra));
2124 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2126 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2127 mutt_set_flag (Context, extra->hdr, M_PURGED,
2128 ch != OP_PURGE_MESSAGE ? 0 : 1);
2129 if (option (OPTDELETEUNTAG))
2130 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2131 redraw = REDRAW_STATUS | REDRAW_INDEX;
2132 if (option (OPTRESOLVE)) {
2134 rc = OP_MAIN_NEXT_UNDELETED;
2138 case OP_DELETE_THREAD:
2139 case OP_DELETE_SUBTHREAD:
2140 CHECK_MODE (IsHeader (extra));
2143 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2145 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2146 ch == OP_DELETE_THREAD ? 0 : 1);
2149 if (option (OPTDELETEUNTAG))
2150 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2151 ch == OP_DELETE_THREAD ? 0 : 1);
2152 if (option (OPTRESOLVE)) {
2153 rc = OP_MAIN_NEXT_UNDELETED;
2157 if (!option (OPTRESOLVE) && PagerIndexLines)
2158 redraw = REDRAW_FULL;
2160 redraw = REDRAW_STATUS | REDRAW_INDEX;
2164 case OP_DISPLAY_ADDRESS:
2165 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2166 if (IsMsgAttach (extra))
2167 mutt_display_address (extra->bdy->hdr->env);
2169 mutt_display_address (extra->hdr->env);
2172 case OP_ENTER_COMMAND:
2173 old_smart_wrap = option (OPTWRAP);
2174 old_markers = option (OPTMARKERS);
2175 old_PagerIndexLines = PagerIndexLines;
2177 CurrentMenu = MENU_PAGER;
2178 mutt_enter_command ();
2180 if (option (OPTNEEDRESORT)) {
2181 unset_option (OPTNEEDRESORT);
2182 CHECK_MODE (IsHeader (extra));
2183 set_option (OPTNEEDRESORT);
2186 if (old_PagerIndexLines != PagerIndexLines) {
2188 mutt_menuDestroy (&index);
2192 if (option (OPTWRAP) != old_smart_wrap ||
2193 option (OPTMARKERS) != old_markers) {
2194 if (flags & M_PAGER_RETWINCH) {
2196 rc = OP_REFORMAT_WINCH;
2200 /* count the real lines above */
2202 for (i = 0; i <= topline; i++) {
2203 if (!lineInfo[i].continuation)
2207 /* we need to restart the whole thing */
2208 for (i = 0; i < maxLine; i++) {
2209 lineInfo[i].offset = 0;
2210 lineInfo[i].type = -1;
2211 lineInfo[i].continuation = 0;
2212 lineInfo[i].chunks = 0;
2213 lineInfo[i].search_cnt = -1;
2214 lineInfo[i].quote = NULL;
2216 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2217 if (SearchCompiled && lineInfo[i].search)
2218 mem_free (&(lineInfo[i].search));
2221 if (SearchCompiled) {
2222 regfree (&SearchRE);
2227 /* try to keep the old position */
2230 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2231 &lastLine, &maxLine,
2232 (has_types ? M_TYPES : 0),
2233 &QuoteList, &q_level, &force_redraw,
2235 redraw |= REDRAW_SIDEBAR;
2236 if (!lineInfo[topline].continuation)
2245 if (option (OPTFORCEREDRAWPAGER))
2246 redraw = REDRAW_FULL;
2247 unset_option (OPTFORCEREDRAWINDEX);
2248 unset_option (OPTFORCEREDRAWPAGER);
2251 case OP_FLAG_MESSAGE:
2252 CHECK_MODE (IsHeader (extra));
2255 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
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 || str_casecmp (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));
2472 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2474 if (extra->hdr->read || extra->hdr->old)
2475 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2477 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2479 Context->msgnotreadyet = -1;
2480 redraw = REDRAW_STATUS | REDRAW_INDEX;
2481 if (option (OPTRESOLVE)) {
2483 rc = OP_MAIN_NEXT_UNDELETED;
2488 CHECK_MODE (IsHeader (extra));
2491 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2493 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2494 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2495 redraw = REDRAW_STATUS | REDRAW_INDEX;
2496 if (option (OPTRESOLVE)) {
2502 case OP_UNDELETE_THREAD:
2503 case OP_UNDELETE_SUBTHREAD:
2504 CHECK_MODE (IsHeader (extra));
2507 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2509 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2510 ch == OP_UNDELETE_THREAD ? 0 : 1)
2511 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2512 ch == OP_UNDELETE_THREAD ? 0 : 1);
2515 if (option (OPTRESOLVE)) {
2516 rc = (ch == OP_DELETE_THREAD) ?
2517 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2521 if (!option (OPTRESOLVE) && PagerIndexLines)
2522 redraw = REDRAW_FULL;
2524 redraw = REDRAW_STATUS | REDRAW_INDEX;
2533 if (option (OPTFORCEBUFFYCHECK))
2536 redraw |= REDRAW_SIDEBAR;
2539 case OP_VIEW_ATTACHMENTS:
2540 if (flags & M_PAGER_ATTACHMENT) {
2542 rc = OP_ATTACH_COLLAPSE;
2545 CHECK_MODE (IsHeader (extra));
2546 mutt_view_attachments (extra->hdr);
2547 if (extra->hdr->attach_del)
2548 Context->changed = 1;
2549 redraw = REDRAW_FULL;
2554 if (!(WithCrypto & APPLICATION_PGP)) {
2558 CHECK_MODE (IsHeader (extra));
2560 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2561 redraw = REDRAW_FULL;
2565 case OP_FORGET_PASSPHRASE:
2566 crypt_forget_passphrase ();
2569 case OP_EXTRACT_KEYS:
2574 CHECK_MODE (IsHeader (extra));
2575 crypt_extract_keys_from_messages (extra->hdr);
2576 redraw = REDRAW_FULL;
2579 case OP_SIDEBAR_SCROLL_UP:
2580 case OP_SIDEBAR_SCROLL_DOWN:
2581 case OP_SIDEBAR_NEXT:
2582 case OP_SIDEBAR_NEXT_NEW:
2583 case OP_SIDEBAR_PREV:
2584 case OP_SIDEBAR_PREV_NEW:
2585 sidebar_scroll (ch, MENU_PAGER);
2594 if (IsHeader (extra))
2595 Context->msgnotreadyet = -1;
2597 cleanup_quote (&QuoteList);
2599 for (i = 0; i < maxLine; i++) {
2600 mem_free (&(lineInfo[i].syntax));
2601 if (SearchCompiled && lineInfo[i].search)
2602 mem_free (&(lineInfo[i].search));
2604 if (SearchCompiled) {
2605 regfree (&SearchRE);
2608 mem_free (&lineInfo);
2610 mutt_menuDestroy (&index);
2611 return (rc != -1 ? rc : 0);