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.
18 #include <lib-lib/mem.h>
22 #include "mutt_curses.h"
24 #include "mutt_menu.h"
29 #include "recvattach.h"
37 #include "imap_private.h"
40 #include "mutt_crypt.h"
46 #include "lib/debug.h"
56 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
58 #define IsAttach(x) (x && (x)->bdy)
59 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
60 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
61 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
62 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
63 #define SW (option(OPTMBOXPANE)?SidebarWidth:0)
65 static const char *Not_available_in_this_menu =
66 N_("Not available in this menu.");
67 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
68 static const char *Function_not_permitted_in_attach_message_mode =
69 N_("Function not permitted in attach-message mode.");
71 /* hack to return to position when returning from index to same message */
72 static int TopLine = 0;
73 static HEADER *OldHdr = NULL;
75 #define CHECK_MODE(x) if (!(x)) \
78 mutt_error _(Not_available_in_this_menu); \
82 #define CHECK_READONLY if (Context->readonly) \
85 mutt_error _(Mailbox_is_read_only); \
89 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
92 mutt_error _(Function_not_permitted_in_attach_message_mode); \
101 struct q_class_t *next, *prev;
102 struct q_class_t *down, *up;
117 struct syntax_t *syntax;
118 struct syntax_t *search;
119 struct q_class_t *quote;
122 #define ANSI_OFF (1<<0)
123 #define ANSI_BLINK (1<<1)
124 #define ANSI_BOLD (1<<2)
125 #define ANSI_UNDERLINE (1<<3)
126 #define ANSI_REVERSE (1<<4)
127 #define ANSI_COLOR (1<<5)
129 typedef struct _ansi_attr {
136 static short InHelp = 0;
138 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
139 static struct resize {
146 #define NumSigLines 4
148 static int check_sig (const char *s, struct line_t *info, int n)
152 while (n > 0 && count <= NumSigLines) {
153 if (info[n].type != MT_COLOR_SIGNATURE)
162 if (count > NumSigLines) {
163 /* check for a blank line */
177 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags,
178 int special, ansi_attr * a)
180 int def_color; /* color without syntax hilight */
181 int color; /* final color */
182 static int last_color; /* last color set */
183 int search = 0, i, m;
186 last_color = -1; /* force attrset() */
188 if (lineInfo[n].continuation) {
189 if (!cnt && option (OPTMARKERS)) {
190 SETCOLOR (MT_COLOR_MARKERS);
192 last_color = ColorDefs[MT_COLOR_MARKERS];
194 m = (lineInfo[n].syntax)[0].first;
195 cnt += (lineInfo[n].syntax)[0].last;
199 if (!(flags & M_SHOWCOLOR))
200 def_color = ColorDefs[MT_COLOR_NORMAL];
201 else if (lineInfo[m].type == MT_COLOR_HEADER)
202 def_color = (lineInfo[m].syntax)[0].color;
204 def_color = ColorDefs[lineInfo[m].type];
206 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED) {
207 struct q_class_t *class = lineInfo[m].quote;
210 def_color = class->color;
212 while (class && class->length > cnt) {
213 def_color = class->color;
220 if (flags & M_SHOWCOLOR) {
221 for (i = 0; i < lineInfo[m].chunks; i++) {
222 /* we assume the chunks are sorted */
223 if (cnt > (lineInfo[m].syntax)[i].last)
225 if (cnt < (lineInfo[m].syntax)[i].first)
227 if (cnt != (lineInfo[m].syntax)[i].last) {
228 color = (lineInfo[m].syntax)[i].color;
231 /* don't break here, as cnt might be
232 * in the next chunk as well */
236 if (flags & M_SEARCH) {
237 for (i = 0; i < lineInfo[m].search_cnt; i++) {
238 if (cnt > (lineInfo[m].search)[i].last)
240 if (cnt < (lineInfo[m].search)[i].first)
242 if (cnt != (lineInfo[m].search)[i].last) {
243 color = ColorDefs[MT_COLOR_SEARCH];
250 /* handle "special" bold & underlined characters */
251 if (special || a->attr) {
253 if ((a->attr & ANSI_COLOR)) {
255 a->pair = mutt_alloc_color (a->fg, a->bg);
257 if (a->attr & ANSI_BOLD)
262 if ((special & A_BOLD) || (a->attr & ANSI_BOLD)) {
263 if (ColorDefs[MT_COLOR_BOLD] && !search)
264 color = ColorDefs[MT_COLOR_BOLD];
268 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE)) {
269 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
270 color = ColorDefs[MT_COLOR_UNDERLINE];
272 color ^= A_UNDERLINE;
274 else if (a->attr & ANSI_REVERSE) {
277 else if (a->attr & ANSI_BLINK) {
280 else if (a->attr & ANSI_OFF) {
285 if (color != last_color) {
291 static void append_line (struct line_t *lineInfo, int n, int cnt)
295 lineInfo[n + 1].type = lineInfo[n].type;
296 (lineInfo[n + 1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
297 lineInfo[n + 1].continuation = 1;
299 /* find the real start of the line */
300 for (m = n; m >= 0; m--)
301 if (lineInfo[m].continuation == 0)
304 (lineInfo[n + 1].syntax)[0].first = m;
305 (lineInfo[n + 1].syntax)[0].last = (lineInfo[n].continuation) ?
306 cnt + (lineInfo[n].syntax)[0].last : cnt;
309 static void new_class_color (struct q_class_t *class, int *q_level)
311 class->index = (*q_level)++;
312 class->color = ColorQuote[class->index % ColorQuoteUsed];
316 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
317 int index, int *q_level)
319 struct q_class_t *q_list;
322 new_class->index = -1;
325 if (q_list->index >= index) {
327 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
330 q_list = q_list->down;
331 else if (q_list->next)
332 q_list = q_list->next;
334 while (!q_list->next) {
340 q_list = q_list->next;
344 new_class->index = index;
345 new_class->color = ColorQuote[index % ColorQuoteUsed];
349 static void cleanup_quote (struct q_class_t **QuoteList)
351 struct q_class_t *ptr;
354 if ((*QuoteList)->down)
355 cleanup_quote (&((*QuoteList)->down));
356 ptr = (*QuoteList)->next;
357 if ((*QuoteList)->prefix)
358 p_delete(&(*QuoteList)->prefix);
366 static struct q_class_t *classify_quote (struct q_class_t **QuoteList,
367 const char *qptr, int length,
368 int *force_redraw, int *q_level)
370 struct q_class_t *q_list = *QuoteList;
371 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
373 int offset, tail_lng;
376 if (ColorQuoteUsed <= 1) {
377 /* not much point in classifying quotes... */
379 if (*QuoteList == NULL) {
380 class = p_new(struct q_class_t, 1);
381 class->color = ColorQuote[0];
387 /* Did I mention how much I like emulating Lisp in C? */
389 /* classify quoting prefix */
391 if (length <= q_list->length) {
392 /* case 1: check the top level nodes */
394 if (str_ncmp (qptr, q_list->prefix, length) == 0) {
395 if (length == q_list->length)
396 return q_list; /* same prefix: return the current class */
398 /* found shorter prefix */
400 /* add a node above q_list */
401 tmp = p_new(struct q_class_t, 1);
402 tmp->prefix = p_dupstr(qptr, length);
403 tmp->length = length;
405 /* replace q_list by tmp in the top level list */
407 tmp->next = q_list->next;
408 q_list->next->prev = tmp;
411 tmp->prev = q_list->prev;
412 q_list->prev->next = tmp;
415 /* make q_list a child of tmp */
419 /* q_list has no siblings for now */
423 /* update the root if necessary */
424 if (q_list == *QuoteList)
427 index = q_list->index;
429 /* tmp should be the return class too */
432 /* next class to test; if tmp is a shorter prefix for another
433 * node, that node can only be in the top level list, so don't
434 * go down after this point
439 /* found another branch for which tmp is a shorter prefix */
441 /* save the next sibling for later */
444 /* unlink q_list from the top level list */
446 q_list->next->prev = q_list->prev;
448 q_list->prev->next = q_list->next;
450 /* at this point, we have a tmp->down; link q_list to it */
452 /* sibling order is important here, q_list should be linked last */
460 index = q_list->index;
462 /* next class to test; as above, we shouldn't go down */
466 /* we found a shorter prefix, so certain quotes have changed classes */
471 /* shorter, but not a substring of the current class: try next */
472 q_list = q_list->next;
477 /* case 2: try subclassing the current top level node */
479 /* tmp != NULL means we already found a shorter prefix at case 1 */
481 && str_ncmp (qptr, q_list->prefix, q_list->length) == 0) {
482 /* ok, it's a subclass somewhere on this branch */
485 offset = q_list->length;
487 q_list = q_list->down;
488 tail_lng = length - offset;
489 tail_qptr = (char *) qptr + offset;
492 if (length <= q_list->length) {
493 if (str_ncmp (tail_qptr, (q_list->prefix) + offset, tail_lng)
495 /* same prefix: return the current class */
496 if (length == q_list->length)
499 /* found shorter common prefix */
501 /* add a node above q_list */
502 tmp = p_new(struct q_class_t, 1);
503 tmp->prefix = p_dupstr(qptr, length);
504 tmp->length = length;
506 /* replace q_list by tmp */
508 tmp->next = q_list->next;
509 q_list->next->prev = tmp;
512 tmp->prev = q_list->prev;
513 q_list->prev->next = tmp;
516 /* make q_list a child of tmp */
518 tmp->up = q_list->up;
520 if (tmp->up->down == q_list)
523 /* q_list has no siblings */
527 index = q_list->index;
529 /* tmp should be the return class too */
532 /* next class to test */
536 /* found another branch for which tmp is a shorter prefix */
538 /* save the next sibling for later */
541 /* unlink q_list from the top level list */
543 q_list->next->prev = q_list->prev;
545 q_list->prev->next = q_list->next;
547 /* at this point, we have a tmp->down; link q_list to it */
556 index = q_list->index;
558 /* next class to test */
562 /* we found a shorter prefix, so we need a redraw */
567 q_list = q_list->next;
572 /* longer than the current prefix: try subclassing it */
574 && str_ncmp (tail_qptr, (q_list->prefix) + offset,
575 q_list->length - offset) == 0) {
576 /* still a subclass: go down one level */
578 offset = q_list->length;
580 q_list = q_list->down;
581 tail_lng = length - offset;
582 tail_qptr = (char *) qptr + offset;
587 /* nope, try the next prefix */
588 q_list = q_list->next;
594 /* still not found so far: add it as a sibling to the current node */
596 tmp = p_new(struct q_class_t, 1);
597 tmp->prefix = p_dupstr(qptr, length);
598 tmp->length = length;
601 tmp->next = ptr->down;
602 ptr->down->prev = tmp;
607 new_class_color (tmp, q_level);
613 shift_class_colors (*QuoteList, tmp, index, q_level);
619 /* nope, try the next prefix */
620 q_list = q_list->next;
627 /* not found so far: add it as a top level class */
628 class = p_new(struct q_class_t, 1);
629 class->prefix = p_dupstr(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 brailleLine = -1;
647 static int brailleCol = -1;
649 static int check_attachment_marker (char *);
652 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
653 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
656 COLOR_LINE *color_line;
657 regmatch_t pmatch[1], smatch[1];
658 int found, offset, null_rx, i;
660 if (n == 0 || ISHEADER (lineInfo[n - 1].type)) {
661 if (buf[0] == '\n') {
662 lineInfo[n].type = MT_COLOR_NORMAL;
663 getyx(stdscr, brailleLine, brailleCol);
665 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t')) {
666 lineInfo[n].type = lineInfo[n - 1].type; /* wrapped line */
667 (lineInfo[n].syntax)[0].color = (lineInfo[n - 1].syntax)[0].color;
670 lineInfo[n].type = MT_COLOR_HDEFAULT;
671 color_line = ColorHdrList;
673 if (REGEXEC (&color_line->rx, buf) == 0) {
674 lineInfo[n].type = MT_COLOR_HEADER;
675 lineInfo[n].syntax[0].color = color_line->pair;
678 color_line = color_line->next;
682 else if (str_ncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
683 lineInfo[n].type = MT_COLOR_NORMAL;
685 else if (str_ncmp ("[-- ", buf, 4) == 0)
686 lineInfo[n].type = MT_COLOR_ATTACHMENT;
688 else if (check_attachment_marker ((char *) raw) == 0)
689 lineInfo[n].type = MT_COLOR_ATTACHMENT;
691 else if (str_cmp ("-- \n", buf) == 0
692 || str_cmp ("-- \r\n", buf) == 0) {
695 lineInfo[n].type = MT_COLOR_SIGNATURE;
696 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
697 (lineInfo[i].type == MT_COLOR_NORMAL ||
698 lineInfo[i].type == MT_COLOR_QUOTED ||
699 lineInfo[i].type == MT_COLOR_HEADER)) {
701 if (lineInfo[i].chunks) {
702 lineInfo[i].chunks = 0;
703 mem_realloc (&(lineInfo[n].syntax), sizeof (struct syntax_t));
705 lineInfo[i++].type = MT_COLOR_SIGNATURE;
708 else if (check_sig (buf, lineInfo, n - 1) == 0)
709 lineInfo[n].type = MT_COLOR_SIGNATURE;
710 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
711 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0) {
712 if (smatch[0].rm_so > 0) {
715 /* hack to avoid making an extra copy of buf */
716 c = buf[smatch[0].rm_so];
717 buf[smatch[0].rm_so] = 0;
719 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0) {
720 if (q_classify && lineInfo[n].quote == NULL)
721 lineInfo[n].quote = classify_quote (QuoteList,
722 buf + pmatch[0].rm_so,
724 pmatch[0].rm_so, force_redraw,
726 lineInfo[n].type = MT_COLOR_QUOTED;
729 lineInfo[n].type = MT_COLOR_NORMAL;
731 buf[smatch[0].rm_so] = c;
734 lineInfo[n].type = MT_COLOR_NORMAL;
737 if (q_classify && lineInfo[n].quote == NULL)
738 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
739 pmatch[0].rm_eo - pmatch[0].rm_so,
740 force_redraw, q_level);
741 lineInfo[n].type = MT_COLOR_QUOTED;
745 lineInfo[n].type = MT_COLOR_NORMAL;
748 if (lineInfo[n].type == MT_COLOR_NORMAL ||
749 lineInfo[n].type == MT_COLOR_QUOTED) {
753 lineInfo[n].chunks = 0;
760 color_line = ColorBodyList;
762 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
763 (offset ? REG_NOTBOL : 0)) == 0) {
764 if (pmatch[0].rm_eo != pmatch[0].rm_so) {
766 if (++(lineInfo[n].chunks) > 1)
767 mem_realloc (&(lineInfo[n].syntax),
768 (lineInfo[n].chunks) *
769 sizeof (struct syntax_t));
771 i = lineInfo[n].chunks - 1;
772 pmatch[0].rm_so += offset;
773 pmatch[0].rm_eo += offset;
775 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
776 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
777 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last)) {
778 (lineInfo[n].syntax)[i].color = color_line->pair;
779 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
780 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
786 null_rx = 1; /* empty regexp; don't add it, but keep looking */
788 color_line = color_line->next;
792 offset++; /* avoid degenerate cases */
794 offset = (lineInfo[n].syntax)[i].last;
795 } while (found || null_rx);
799 static int is_ansi (unsigned char *buf)
801 while (*buf && (isdigit (*buf) || *buf == ';'))
803 return (*buf == 'm');
806 static int check_attachment_marker (char *p)
808 char *q = AttachmentMarker;
810 for (; *p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++);
811 return (int) (*p - *q);
814 static int grok_ansi (unsigned char *buf, int pos, ansi_attr * a)
818 while (isdigit (buf[x]) || buf[x] == ';')
821 /* Character Attributes */
822 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm') {
826 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] == ';')) {
855 else if (buf[pos] == '3' && isdigit (buf[pos + 1])) {
858 mutt_free_color (a->fg, a->bg);
861 a->attr |= ANSI_COLOR;
862 if (buf[pos + 1] != '9')
863 a->fg = buf[pos + 1] - '0';
866 else if (buf[pos] == '4' && isdigit (buf[pos + 1])) {
869 mutt_free_color (a->fg, a->bg);
872 a->attr |= ANSI_COLOR;
873 if (buf[pos + 1] != '9')
874 a->bg = buf[pos + 1] - '0';
878 while (pos < x && buf[pos] != ';')
888 /* trim tail of buf so that it contains complete multibyte characters */
889 static int trim_incomplete_mbyte(unsigned char *buf, size_t len) {
893 memset (&mbstate, 0, sizeof (mbstate));
894 for (; len > 0; buf += k, len -= k) {
895 k = mbrtowc (NULL, (char *) buf, len, &mbstate);
898 else if (k == -1 || k == 0)
907 fill_buffer (FILE * f, off_t *last_pos, off_t offset, unsigned char *buf,
908 unsigned char *fmt, size_t blen, int *buf_ready)
911 static int b_read = 0;
913 if (*buf_ready == 0) {
915 if (offset != *last_pos)
916 fseeko (f, offset, 0);
917 if (fgets ((char *) buf, blen - 1, f) == NULL) {
921 *last_pos = ftello (f);
922 b_read = (int) (*last_pos - offset);
925 /* incomplete mbyte characters trigger a segfault in regex processing for
926 * certain versions of glibc. Trim them if necessary. */
927 if (b_read == blen - 2)
928 b_read -= trim_incomplete_mbyte(buf, b_read);
930 /* copy "buf" to "fmt", but without bold and underline controls */
933 if (*p == '\010' && (p > buf)) {
934 if (*(p + 1) == '_') /* underline */
936 else if (*(p + 1)) { /* bold or overstrike */
937 *(fmt - 1) = *(p + 1);
943 else if (*p == '\033' && *(p + 1) == '[' && is_ansi (p + 2)) {
944 while (*p++ != 'm') /* skip ANSI sequence */
947 else if (*p == '\033' && *(p + 1) == ']'
948 && check_attachment_marker ((char *) p) == 0) {
949 debug_print (2, ("seen attachment marker.\n"));
950 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
967 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
968 int flags, ansi_attr * pa, int cnt,
969 int *pspace, int *pvch, int *pcol, int *pspecial)
971 int space = -1; /* index of the last space or TAB */
972 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
973 int ch, vch, k, last_special = -1, special = 0, t;
977 int wrap_cols = COLS;
979 if (!(flags & (M_SHOWFLAT)))
980 wrap_cols -= WrapMargin;
986 /* FIXME: this should come from lineInfo */
987 memset (&mbstate, 0, sizeof (mbstate));
989 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k) {
990 /* Handle ANSI sequences */
991 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == '[' &&
992 is_ansi (buf + ch + 2))
993 ch = grok_ansi (buf, ch + 2, pa) + 1;
995 while (cnt - ch >= 2 && buf[ch] == '\033' && buf[ch + 1] == ']' &&
996 check_attachment_marker ((char *) buf + ch) == 0) {
997 while (buf[ch++] != '\a')
1002 /* is anything left to do? */
1006 k = mbrtowc (&wc, (char *) buf + ch, cnt - ch, &mbstate);
1007 if (k == -2 || k == -1) {
1008 debug_print (1, ("mbrtowc returned %d; errno = %d.\n", k, errno));
1009 if (col + 4 > wrap_cols)
1013 printw ("\\%03o", buf[ch]);
1020 /* Handle backspace */
1022 if (IsWPrint (wc)) {
1027 while ((wc1 = 0, mbstate1 = mbstate,
1029 k + mbrtowc (&wc1, (char *) buf + ch + k, cnt - ch - k,
1030 &mbstate1), k1 - k > 0 && wc1 == '\b')
1032 mbrtowc (&wc1, (char *) buf + ch + k1, cnt - ch - k1,
1033 &mbstate1), k2 > 0 && IsWPrint (wc1))) {
1035 special |= (wc == '_' && special & A_UNDERLINE)
1036 ? A_UNDERLINE : A_BOLD;
1038 else if (wc == '_' || wc1 == '_') {
1039 special |= A_UNDERLINE;
1040 wc = (wc1 == '_') ? wc : wc1;
1043 /* special = 0; / * overstrike: nothing to do! */
1053 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1054 special || last_special || pa->attr)) {
1055 resolve_color (*lineInfo, n, vch, flags, special, pa);
1056 last_special = special;
1059 if (IsWPrint (wc)) {
1063 if (col + t > wrap_cols)
1069 else if (wc == '\n')
1071 else if (wc == '\t') {
1077 for (; col < t; col++)
1082 else if (wc < 0x20 || wc == 0x7f) {
1083 if (col + 2 > wrap_cols)
1087 printw ("^%c", ('@' + wc) & 0x7f);
1089 else if (wc < 0x100) {
1090 if (col + 4 > wrap_cols)
1094 printw ("\\%03o", wc);
1097 if (col + 1 > wrap_cols)
1101 addch (replacement_char ());
1107 *pspecial = special;
1113 * flags M_SHOWFLAT, show characters (used for displaying help)
1114 * M_SHOWCOLOR, show characters in color
1115 * otherwise don't show characters
1116 * M_HIDE, don't show quoted text
1117 * M_SEARCH, resolve search patterns
1118 * M_TYPES, compute line's type
1119 * M_PAGER_NSKIP, keeps leading whitespace
1120 * M_PAGER_MARKER, eventually show markers
1123 * -1 EOF was reached
1124 * 0 normal exit, line was not displayed
1125 * >0 normal exit, line was displayed
1129 display_line (FILE * f, off_t *last_pos, struct line_t **lineInfo, int n,
1130 int *last, int *max, int flags, struct q_class_t **QuoteList,
1131 int *q_level, int *force_redraw, regex_t * SearchRE)
1133 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1134 unsigned char *buf_ptr = buf;
1135 int ch, vch, col, cnt, b_read;
1136 int buf_ready = 0, change_last = 0;
1141 ansi_attr a = { 0, 0, 0, -1 };
1142 regmatch_t pmatch[1];
1149 if (*last == *max) {
1150 mem_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1151 for (ch = *last; ch < *max; ch++) {
1152 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1153 (*lineInfo)[ch].type = -1;
1154 (*lineInfo)[ch].search_cnt = -1;
1155 (*lineInfo)[ch].syntax = p_new(struct syntax_t, 1);
1156 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last =
1161 /* only do color hiliting if we are viewing a message */
1162 if (flags & (M_SHOWCOLOR | M_TYPES)) {
1163 if ((*lineInfo)[n].type == -1) {
1164 /* determine the line class */
1166 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1173 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1174 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1176 /* avoid race condition for continuation lines when scrolling up */
1178 m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation;
1180 (*lineInfo)[m].type = (*lineInfo)[n].type;
1183 /* this also prevents searching through the hidden lines */
1184 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1185 flags = 0; /* M_NOSHOW */
1188 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1189 * don't want to compute it every time M_TYPES is set, since this
1190 * would slow down the "bottom" function unacceptably. A compromise
1191 * solution is hence to call regexec() again, just to find out the
1192 * length of the quote prefix.
1194 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1195 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1198 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1204 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1205 (*lineInfo)[n].quote = classify_quote (QuoteList,
1206 (char *) fmt + pmatch[0].rm_so,
1207 pmatch[0].rm_eo - pmatch[0].rm_so,
1208 force_redraw, q_level);
1211 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation
1212 && (*lineInfo)[n].search_cnt == -1) {
1214 (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf),
1222 (*lineInfo)[n].search_cnt = 0;
1224 (SearchRE, (char *) fmt + offset, 1, pmatch,
1225 (offset ? REG_NOTBOL : 0)) == 0) {
1226 if (++((*lineInfo)[n].search_cnt) > 1)
1227 mem_realloc (&((*lineInfo)[n].search),
1228 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1230 (*lineInfo)[n].search = p_new(struct syntax_t, 1);
1231 pmatch[0].rm_so += offset;
1232 pmatch[0].rm_eo += offset;
1233 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first =
1235 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last =
1238 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1239 offset++; /* avoid degenerate cases */
1241 offset = pmatch[0].rm_eo;
1247 if (!(flags & M_SHOW) && (*lineInfo)[n + 1].offset > 0) {
1248 /* we've already scanned this line, so just exit */
1251 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n + 1].offset > 0) {
1252 /* no need to try to display this line... */
1253 return (1); /* fake display */
1256 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1257 sizeof (buf), &buf_ready)) < 0) {
1263 /* now chose a good place to break the line */
1265 format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col,
1267 buf_ptr = buf + cnt;
1269 /* move the break point only if smart_wrap is set */
1270 if (option (OPTWRAP)) {
1272 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n'
1273 && buf[cnt] != '\r') {
1275 /* skip trailing blanks */
1276 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1278 /* a very long word with leading spaces causes infinite wrapping */
1279 if ((!ch) && (flags & M_PAGER_NSKIP))
1280 buf_ptr = buf + cnt;
1285 buf_ptr = buf + cnt; /* a very long word... */
1287 if (!(flags & M_PAGER_NSKIP))
1288 /* skip leading blanks on the next line too */
1289 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1293 if (*buf_ptr == '\r')
1295 if (*buf_ptr == '\n')
1298 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n + 1].continuation)
1299 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1300 (*lineInfo)[n + 1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1302 /* if we don't need to display the line we are done */
1303 if (!(flags & M_SHOW))
1306 /* display the line */
1307 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1309 /* avoid a bug in ncurses... */
1310 #ifndef USE_SLANG_CURSES
1312 SETCOLOR (MT_COLOR_NORMAL);
1317 /* end the last color pattern (needed by S-Lang) */
1318 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1319 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1322 * Fill the blank space at the end of the line with the prevailing color.
1323 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1324 * to make sure to reset the color *after* that
1326 if (flags & M_SHOWCOLOR) {
1327 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1328 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1329 def_color = ((*lineInfo)[m].syntax)[0].color;
1331 def_color = ColorDefs[(*lineInfo)[m].type];
1333 attrset (def_color);
1335 bkgdset (def_color | ' ');
1339 /* ncurses always wraps lines when you get to the right side of the
1340 * screen, but S-Lang seems to only wrap if the next character is *not*
1343 #ifndef USE_SLANG_CURSES
1349 * reset the color back to normal. This *must* come after the
1350 * addch('\n'), otherwise the color for this line will not be
1351 * filled to the right margin.
1353 if (flags & M_SHOWCOLOR) {
1354 SETCOLOR (MT_COLOR_NORMAL);
1355 BKGDSET (MT_COLOR_NORMAL);
1358 /* build a return code */
1359 if (!(flags & M_SHOW))
1365 static int upNLines (int nlines, struct line_t *info, int cur, int hiding)
1367 while (cur > 0 && nlines > 0) {
1369 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1376 static struct mapping_t PagerHelp[] = {
1377 {N_("Exit"), OP_EXIT},
1378 {N_("PrevPg"), OP_PREV_PAGE},
1379 {N_("NextPg"), OP_NEXT_PAGE},
1382 static struct mapping_t PagerHelpExtra[] = {
1383 {N_("View Attachm."), OP_VIEW_ATTACHMENTS},
1384 {N_("Del"), OP_DELETE},
1385 {N_("Reply"), OP_REPLY},
1386 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1391 static struct mapping_t PagerNewsHelpExtra[] = {
1392 {N_("Post"), OP_POST},
1393 {N_("Followup"), OP_FOLLOWUP},
1394 {N_("Del"), OP_DELETE},
1395 {N_("Next"), OP_MAIN_NEXT_UNDELETED},
1402 /* This pager is actually not so simple as it once was. It now operates in
1403 two modes: one for viewing messages and the other for viewing help. These
1404 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1405 is there so that we can do operations on the current message without the
1406 need to pop back out to the main-menu. */
1408 mutt_pager (const char *banner, const char *fname, int flags, pager_t * extra)
1410 static char searchbuf[STRING];
1411 char buffer[LONG_STRING];
1412 char helpstr[SHORT_STRING * 2];
1413 char tmphelp[SHORT_STRING * 2];
1414 int maxLine, lastLine = 0;
1415 struct line_t *lineInfo;
1416 struct q_class_t *QuoteList = NULL;
1417 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1418 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1420 int redraw = REDRAW_FULL;
1422 off_t last_pos = 0, last_offset = 0;
1423 int old_smart_wrap, old_markers;
1426 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1427 int has_types = (IsHeader (extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1429 int bodyoffset = 1; /* offset of first line of real text */
1430 int statusoffset = 0; /* offset for the status bar */
1431 int helpoffset = LINES - 2; /* offset for the help bar. */
1432 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1434 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1435 int indexoffset = 0; /* offset for the PI */
1436 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1437 int indicator = indexlen / 3; /* the indicator line of the PI */
1438 int old_PagerIndexLines; /* some people want to resize it
1439 * while inside the pager... */
1445 if (!(flags & M_SHOWCOLOR))
1446 flags |= M_SHOWFLAT;
1448 if ((fp = fopen (fname, "r")) == NULL) {
1449 mutt_perror (fname);
1453 if (stat (fname, &sb) != 0) {
1454 mutt_perror (fname);
1460 /* Initialize variables */
1462 if (IsHeader (extra) && !extra->hdr->read) {
1463 Context->msgnotreadyet = extra->hdr->msgno;
1464 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1467 lineInfo = p_new(struct line_t, maxLine = LINES);
1468 for (i = 0; i < maxLine; i++) {
1469 memset (&lineInfo[i], 0, sizeof (struct line_t));
1470 lineInfo[i].type = -1;
1471 lineInfo[i].search_cnt = -1;
1472 lineInfo[i].syntax = p_new(struct syntax_t, 1);
1473 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1476 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1477 if (IsHeader (extra)) {
1478 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1479 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1482 && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1485 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1488 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1489 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1490 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1500 if (redraw & REDRAW_FULL) {
1501 SETCOLOR (MT_COLOR_NORMAL);
1502 /* clear() doesn't optimize screen redraws */
1506 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1507 indexlen = Context->vcount + 1;
1509 indexlen = PagerIndexLines;
1511 indicator = indexlen / 3;
1513 if (option (OPTSTATUSONTOP)) {
1515 statusoffset = IsHeader (extra) ? indexlen : 0;
1516 bodyoffset = statusoffset + 1;
1517 helpoffset = LINES - 2;
1518 bodylen = helpoffset - bodyoffset;
1519 if (!option (OPTHELP))
1525 statusoffset = LINES - 2;
1526 if (!option (OPTHELP))
1528 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1529 bodylen = statusoffset - bodyoffset;
1532 if (option (OPTHELP)) {
1533 SETCOLOR (MT_COLOR_STATUS);
1534 move (helpoffset, SW);
1535 mutt_paddstr (COLS-SW, helpstr);
1536 SETCOLOR (MT_COLOR_NORMAL);
1539 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1540 if (Resize != NULL) {
1541 if ((SearchCompiled = Resize->SearchCompiled)) {
1543 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1544 SearchFlag = M_SEARCH;
1545 SearchBack = Resize->SearchBack;
1547 lines = Resize->line;
1548 redraw |= REDRAW_SIGWINCH;
1554 if (IsHeader (extra) && PagerIndexLines) {
1555 if (index == NULL) {
1556 /* only allocate the space if/when we need the index.
1557 Initialise the menu as per the main index */
1558 index = mutt_new_menu ();
1559 index->menu = MENU_MAIN;
1560 index->make_entry = index_make_entry;
1561 index->color = index_color;
1562 index->max = Context->vcount;
1563 index->current = extra->hdr->virtual;
1566 SETCOLOR (MT_COLOR_NORMAL);
1567 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1569 index->pagelen = indexlen - 1;
1571 /* some fudge to work out where abouts the indicator should go */
1572 if (index->current - indicator < 0)
1574 else if (index->max - index->current < index->pagelen - indicator)
1575 index->top = index->max - index->pagelen;
1577 index->top = index->current - indicator;
1579 menu_redraw_index (index);
1582 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1586 if (redraw & REDRAW_SIGWINCH) {
1589 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1590 has_types | SearchFlag, &QuoteList, &q_level,
1591 &force_redraw, &SearchRE) == 0) {
1592 if (!lineInfo[i].continuation && ++j == lines) {
1597 redraw |= REDRAW_SIDEBAR;
1601 if ((redraw & REDRAW_BODY) || topline != oldtopline) {
1603 move (bodyoffset, SW);
1604 curline = oldtopline = topline;
1608 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1) {
1609 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1611 (flags & M_DISPLAYFLAGS) | hideQuoted |
1612 SearchFlag, &QuoteList, &q_level, &force_redraw,
1616 move (lines + bodyoffset, SW);
1617 redraw |= REDRAW_SIDEBAR;
1619 last_offset = lineInfo[curline].offset;
1620 } while (force_redraw);
1622 SETCOLOR (MT_COLOR_TILDE);
1623 BKGDSET (MT_COLOR_TILDE);
1624 while (lines < bodylen) {
1626 if (option (OPTTILDE))
1630 move (lines + bodyoffset, SW);
1632 /* We are going to update the pager status bar, so it isn't
1633 * necessary to reset to normal color now. */
1635 redraw |= REDRAW_STATUS; /* need to update the % seen */
1638 if (redraw & REDRAW_STATUS) {
1639 /* print out the pager status bar */
1640 SETCOLOR (MT_COLOR_STATUS);
1641 BKGDSET (MT_COLOR_STATUS);
1642 CLEARLINE_WIN (statusoffset);
1643 if (IsHeader (extra)) {
1644 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1645 size_t l2 = sizeof (buffer);
1647 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1648 Context, extra->hdr, M_FORMAT_MAKEPRINT);
1650 else if (IsMsgAttach (extra)) {
1651 size_t l1 = (COLS - 9) * MB_LEN_MAX;
1652 size_t l2 = sizeof (buffer);
1654 _mutt_make_string (buffer, l1 < l2 ? l1 : l2, NONULL (PagerFmt),
1655 Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1657 move(statusoffset,SW);
1658 mutt_paddstr (COLS - 10 - SW, IsHeader (extra) ||
1659 IsMsgAttach (extra) ? buffer : banner);
1662 if (last_pos < sb.st_size - 1)
1663 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1665 addstr (topline == 0 ? "all)" : "end)");
1666 BKGDSET (MT_COLOR_NORMAL);
1667 SETCOLOR (MT_COLOR_NORMAL);
1670 if (redraw & REDRAW_SIDEBAR)
1671 sidebar_draw (MENU_PAGER);
1673 if ((redraw & REDRAW_INDEX) && index) {
1674 /* redraw the pager_index indicator, because the
1675 * flags for this message might have changed. */
1676 menu_redraw_current (index);
1677 sidebar_draw (MENU_PAGER);
1678 /* print out the index status bar */
1679 menu_status_line (buffer, sizeof (buffer), index, NONULL (Status));
1680 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)), SW);
1681 SETCOLOR (MT_COLOR_STATUS);
1682 BKGDSET (MT_COLOR_STATUS);
1683 mutt_paddstr (COLS-SW, buffer);
1684 SETCOLOR (MT_COLOR_NORMAL);
1685 BKGDSET (MT_COLOR_NORMAL);
1687 /* if we're not using the index, update every time */
1689 sidebar_draw (MENU_PAGER);
1693 if (option(OPTBRAILLEFRIENDLY)) {
1694 if (brailleLine!=-1) {
1695 move(brailleLine+1, 0);
1699 move (statusoffset, COLS-1);
1702 if (IsHeader (extra) && OldHdr == extra->hdr && TopLine != topline
1703 && lineInfo[curline].offset < sb.st_size-1) {
1704 if (TopLine - topline > lines)
1713 ch = km_dokey (MENU_PAGER);
1715 mutt_clear_error ();
1722 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1723 else if (SigWinch) {
1724 mutt_resize_screen ();
1726 /* Store current position. */
1728 for (i = 0; i <= topline; i++)
1729 if (!lineInfo[i].continuation)
1732 if (flags & M_PAGER_RETWINCH) {
1733 Resize = p_new(struct resize, 1);
1735 Resize->line = lines;
1736 Resize->SearchCompiled = SearchCompiled;
1737 Resize->SearchBack = SearchBack;
1740 rc = OP_REFORMAT_WINCH;
1743 for (i = 0; i < maxLine; i++) {
1744 lineInfo[i].offset = 0;
1745 lineInfo[i].type = -1;
1746 lineInfo[i].continuation = 0;
1747 lineInfo[i].chunks = 0;
1748 lineInfo[i].search_cnt = -1;
1749 lineInfo[i].quote = NULL;
1751 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
1752 if (SearchCompiled && lineInfo[i].search)
1753 p_delete(&(lineInfo[i].search));
1759 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1764 clearok (stdscr, TRUE); /*force complete redraw */
1768 else if (ch == -1) {
1782 if (lineInfo[curline].offset < sb.st_size - 1) {
1783 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1785 else if (option (OPTPAGERSTOP)) {
1786 /* emulate "less -q" and don't go on to the next message. */
1787 mutt_error _("Bottom of message is shown.");
1790 /* end of the current message, so display the next message. */
1791 rc = OP_MAIN_NEXT_UNDELETED;
1799 upNLines (bodylen - PagerContext, lineInfo, topline, hideQuoted);
1802 mutt_error _("Top of message is shown.");
1806 if (lineInfo[curline].offset < sb.st_size - 1) {
1809 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1815 mutt_error _("Bottom of message is shown.");
1820 topline = upNLines (1, lineInfo, topline, hideQuoted);
1822 mutt_error _("Top of message is shown.");
1829 mutt_error _("Top of message is shown.");
1834 topline = upNLines (bodylen / 2, lineInfo, topline, hideQuoted);
1836 mutt_error _("Top of message is shown.");
1840 if (lineInfo[curline].offset < sb.st_size - 1) {
1841 topline = upNLines (bodylen / 2, lineInfo, curline, hideQuoted);
1843 else if (option (OPTPAGERSTOP)) {
1844 /* emulate "less -q" and don't go on to the next message. */
1845 mutt_error _("Bottom of message is shown.");
1848 /* end of the current message, so display the next message. */
1849 rc = OP_MAIN_NEXT_UNDELETED;
1854 case OP_SEARCH_NEXT:
1855 case OP_SEARCH_OPPOSITE:
1856 if (SearchCompiled) {
1858 if ((!SearchBack && ch == OP_SEARCH_NEXT) ||
1859 (SearchBack && ch == OP_SEARCH_OPPOSITE)) {
1860 /* searching forward */
1861 for (i = topline + 1; i < lastLine; i++) {
1862 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1863 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1870 mutt_error _("Not found.");
1873 /* searching backward */
1874 for (i = topline - 1; i >= 0; i--) {
1875 if ((!hideQuoted || (has_types &&
1876 lineInfo[i].type != MT_COLOR_QUOTED)) &&
1877 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1884 mutt_error _("Not found.");
1887 if (lineInfo[topline].search_cnt > 0)
1888 SearchFlag = M_SEARCH;
1892 /* no previous search pattern, so fall through to search */
1895 case OP_SEARCH_REVERSE:
1896 strfcpy (buffer, searchbuf, sizeof (buffer));
1897 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
1898 _("Search: ")), buffer, sizeof (buffer),
1902 if (!strcmp (buffer, searchbuf)) {
1903 if (SearchCompiled) {
1904 /* do an implicit search-next */
1905 if (ch == OP_SEARCH)
1906 ch = OP_SEARCH_NEXT;
1908 ch = OP_SEARCH_OPPOSITE;
1917 strfcpy (searchbuf, buffer, sizeof (searchbuf));
1919 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
1920 if (ch == OP_SEARCH)
1922 else if (ch == OP_SEARCH_REVERSE)
1925 if (SearchCompiled) {
1926 regfree (&SearchRE);
1927 for (i = 0; i < lastLine; i++) {
1928 if (lineInfo[i].search)
1929 p_delete(&(lineInfo[i].search));
1930 lineInfo[i].search_cnt = -1;
1935 REGCOMP (&SearchRE, searchbuf,
1936 REG_NEWLINE | mutt_which_case (searchbuf))) != 0) {
1937 regerror (err, &SearchRE, buffer, sizeof (buffer));
1938 mutt_error ("%s", buffer);
1939 regfree (&SearchRE);
1940 for (i = 0; i < maxLine; i++) {
1942 if (lineInfo[i].search)
1943 p_delete(&(lineInfo[i].search));
1944 lineInfo[i].search_cnt = -1;
1951 /* update the search pointers */
1953 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
1954 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
1955 &QuoteList, &q_level,
1956 &force_redraw, &SearchRE) == 0) {
1958 redraw |= REDRAW_SIDEBAR;
1962 /* searching forward */
1963 for (i = topline; i < lastLine; i++) {
1964 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1965 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1973 /* searching backward */
1974 for (i = topline; i >= 0; i--) {
1975 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
1976 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
1984 if (lineInfo[topline].search_cnt == 0) {
1986 mutt_error _("Not found.");
1989 SearchFlag = M_SEARCH;
1991 redraw = REDRAW_BODY;
1994 case OP_SEARCH_TOGGLE:
1995 if (SearchCompiled) {
1996 SearchFlag ^= M_SEARCH;
1997 redraw = REDRAW_BODY;
2002 /* don't let the user enter the help-menu from the help screen! */
2005 mutt_help (MENU_PAGER);
2006 redraw = REDRAW_FULL;
2010 mutt_error _("Help is currently being shown.");
2013 case OP_PAGER_HIDE_QUOTED:
2015 hideQuoted ^= M_HIDE;
2016 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2017 topline = upNLines (1, lineInfo, topline, hideQuoted);
2019 redraw = REDRAW_BODY;
2023 case OP_PAGER_SKIP_QUOTED:
2026 int new_topline = topline;
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) {
2035 redraw |= REDRAW_SIDEBAR;
2040 mutt_error _("No more quoted text.");
2045 while ((new_topline < lastLine ||
2046 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2047 new_topline, &lastLine,
2048 &maxLine, M_TYPES, &QuoteList,
2049 &q_level, &force_redraw,
2051 && lineInfo[new_topline].type == MT_COLOR_QUOTED) {
2053 redraw |= REDRAW_SIDEBAR;
2057 mutt_error _("No more unquoted text after quoted text.");
2061 topline = new_topline;
2065 case OP_PAGER_BOTTOM: /* move to the end of the file */
2066 if (lineInfo[curline].offset < sb.st_size - 1) {
2068 /* make sure the types are defined to the end of file */
2069 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2070 &maxLine, has_types,
2071 &QuoteList, &q_level, &force_redraw,
2074 redraw |= REDRAW_SIDEBAR;
2076 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2079 mutt_error _("Bottom of message is shown.");
2083 clearok (stdscr, TRUE);
2084 redraw = REDRAW_FULL;
2088 km_error_key (MENU_PAGER);
2091 /* --------------------------------------------------------------------
2092 * The following are operations on the current message rather than
2093 * adjusting the view of the message.
2096 case OP_BOUNCE_MESSAGE:
2097 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2099 if (IsMsgAttach (extra))
2100 mutt_attach_bounce (extra->fp, extra->hdr,
2101 extra->idx, extra->idxlen, extra->bdy);
2103 ci_bounce_message (extra->hdr, &redraw);
2107 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra))
2109 if (IsMsgAttach (extra))
2110 mutt_attach_resend (extra->fp, extra->hdr,
2111 extra->idx, extra->idxlen, extra->bdy);
2113 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2114 redraw = REDRAW_FULL;
2117 case OP_CHECK_TRADITIONAL:
2118 CHECK_MODE (IsHeader (extra));
2119 if (!(WithCrypto & APPLICATION_PGP))
2121 if (!(extra->hdr->security & PGP_TRADITIONAL_CHECKED)) {
2123 rc = OP_CHECK_TRADITIONAL;
2127 case OP_CREATE_ALIAS:
2128 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2129 if (IsMsgAttach (extra))
2130 mutt_create_alias (extra->bdy->hdr->env, NULL);
2132 mutt_create_alias (extra->hdr->env, NULL);
2133 MAYBE_REDRAW (redraw);
2136 case OP_PURGE_MESSAGE:
2138 CHECK_MODE (IsHeader (extra));
2141 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2143 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2144 mutt_set_flag (Context, extra->hdr, M_PURGED,
2145 ch != OP_PURGE_MESSAGE ? 0 : 1);
2146 if (option (OPTDELETEUNTAG))
2147 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2148 redraw = REDRAW_STATUS | REDRAW_INDEX;
2149 if (option (OPTRESOLVE)) {
2151 rc = OP_MAIN_NEXT_UNDELETED;
2155 case OP_DELETE_THREAD:
2156 case OP_DELETE_SUBTHREAD:
2157 CHECK_MODE (IsHeader (extra));
2160 CHECK_MX_ACL (Context, ACL_DELETE, _("Deletion"));
2162 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2163 ch == OP_DELETE_THREAD ? 0 : 1);
2166 if (option (OPTDELETEUNTAG))
2167 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2168 ch == OP_DELETE_THREAD ? 0 : 1);
2169 if (option (OPTRESOLVE)) {
2170 rc = OP_MAIN_NEXT_UNDELETED;
2174 if (!option (OPTRESOLVE) && PagerIndexLines)
2175 redraw = REDRAW_FULL;
2177 redraw = REDRAW_STATUS | REDRAW_INDEX;
2181 case OP_DISPLAY_ADDRESS:
2182 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2183 if (IsMsgAttach (extra))
2184 mutt_display_address (extra->bdy->hdr->env);
2186 mutt_display_address (extra->hdr->env);
2189 case OP_ENTER_COMMAND:
2190 old_smart_wrap = option (OPTWRAP);
2191 old_markers = option (OPTMARKERS);
2192 old_PagerIndexLines = PagerIndexLines;
2194 CurrentMenu = MENU_PAGER;
2195 mutt_enter_command ();
2197 if (option (OPTNEEDRESORT)) {
2198 unset_option (OPTNEEDRESORT);
2199 CHECK_MODE (IsHeader (extra));
2200 set_option (OPTNEEDRESORT);
2203 if (old_PagerIndexLines != PagerIndexLines) {
2205 mutt_menuDestroy (&index);
2209 if (option (OPTWRAP) != old_smart_wrap ||
2210 option (OPTMARKERS) != old_markers) {
2211 if (flags & M_PAGER_RETWINCH) {
2213 rc = OP_REFORMAT_WINCH;
2217 /* count the real lines above */
2219 for (i = 0; i <= topline; i++) {
2220 if (!lineInfo[i].continuation)
2224 /* we need to restart the whole thing */
2225 for (i = 0; i < maxLine; i++) {
2226 lineInfo[i].offset = 0;
2227 lineInfo[i].type = -1;
2228 lineInfo[i].continuation = 0;
2229 lineInfo[i].chunks = 0;
2230 lineInfo[i].search_cnt = -1;
2231 lineInfo[i].quote = NULL;
2233 mem_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2234 if (SearchCompiled && lineInfo[i].search)
2235 p_delete(&(lineInfo[i].search));
2238 if (SearchCompiled) {
2239 regfree (&SearchRE);
2244 /* try to keep the old position */
2247 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2248 &lastLine, &maxLine,
2249 (has_types ? M_TYPES : 0),
2250 &QuoteList, &q_level, &force_redraw,
2252 redraw |= REDRAW_SIDEBAR;
2253 if (!lineInfo[topline].continuation)
2262 if (option (OPTFORCEREDRAWPAGER))
2263 redraw = REDRAW_FULL;
2264 unset_option (OPTFORCEREDRAWINDEX);
2265 unset_option (OPTFORCEREDRAWPAGER);
2268 case OP_FLAG_MESSAGE:
2269 CHECK_MODE (IsHeader (extra));
2272 CHECK_MX_ACL (Context, ACL_WRITE, _("Flagging"));
2274 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2275 redraw = REDRAW_STATUS | REDRAW_INDEX;
2276 if (option (OPTRESOLVE)) {
2278 rc = OP_MAIN_NEXT_UNDELETED;
2283 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2284 if (IsAttach (extra))
2285 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2287 mutt_pipe_message (extra->hdr);
2288 MAYBE_REDRAW (redraw);
2292 CHECK_MODE (IsHeader (extra) || IsAttach (extra));
2293 if (IsAttach (extra))
2294 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2296 mutt_print_message (extra->hdr);
2300 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2302 ci_send_message (0, NULL, NULL, extra->ctx, NULL);
2303 redraw = REDRAW_FULL;
2308 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2310 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2311 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2312 query_quadoption (OPT_TOMODERATED,
2314 ("Posting to this group not allowed, may be moderated. Continue?"))
2317 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2318 redraw = REDRAW_FULL;
2321 case OP_FORWARD_TO_GROUP:
2322 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2324 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2325 !((NNTP_DATA *) extra->ctx->data)->allowed &&
2326 query_quadoption (OPT_TOMODERATED,
2328 ("Posting to this group not allowed, may be moderated. Continue?"))
2331 if (IsMsgAttach (extra))
2332 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2333 extra->idxlen, extra->bdy, SENDNEWS);
2335 ci_send_message (SENDNEWS | SENDFORWARD, NULL, NULL, extra->ctx,
2337 redraw = REDRAW_FULL;
2341 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2344 if (IsMsgAttach (extra))
2345 followup_to = extra->bdy->hdr->env->followup_to;
2347 followup_to = extra->hdr->env->followup_to;
2349 if (!followup_to || str_casecmp (followup_to, "poster") ||
2350 query_quadoption (OPT_FOLLOWUPTOPOSTER,
2351 _("Reply by mail as poster prefers?")) != M_YES) {
2352 if (extra->ctx && extra->ctx->magic == M_NNTP
2353 && !((NNTP_DATA *) extra->ctx->data)->allowed
2354 && query_quadoption (OPT_TOMODERATED,
2356 ("Posting to this group not allowed, may be moderated. Continue?"))
2359 if (IsMsgAttach (extra))
2360 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2361 extra->idxlen, extra->bdy, SENDNEWS | SENDREPLY);
2363 ci_send_message (SENDNEWS | SENDREPLY, NULL, NULL,
2364 extra->ctx, extra->hdr);
2365 redraw = REDRAW_FULL;
2371 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2373 if (IsMsgAttach (extra))
2374 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2375 extra->idxlen, extra->bdy, SENDREPLY);
2377 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2378 redraw = REDRAW_FULL;
2381 case OP_RECALL_MESSAGE:
2382 CHECK_MODE (IsHeader (extra) && !IsAttach (extra));
2384 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2385 redraw = REDRAW_FULL;
2388 case OP_GROUP_REPLY:
2389 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2391 if (IsMsgAttach (extra))
2392 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2393 extra->idxlen, extra->bdy,
2394 SENDREPLY | SENDGROUPREPLY);
2396 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx,
2398 redraw = REDRAW_FULL;
2402 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2404 if (IsMsgAttach (extra))
2405 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2406 extra->idxlen, extra->bdy,
2407 SENDREPLY | SENDLISTREPLY);
2409 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx,
2411 redraw = REDRAW_FULL;
2414 case OP_FORWARD_MESSAGE:
2415 CHECK_MODE (IsHeader (extra) || IsMsgAttach (extra));
2417 if (IsMsgAttach (extra))
2418 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2419 extra->idxlen, extra->bdy, 0);
2421 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2422 redraw = REDRAW_FULL;
2425 case OP_DECRYPT_SAVE:
2432 if (IsAttach (extra)) {
2433 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr,
2438 case OP_COPY_MESSAGE:
2439 case OP_DECODE_SAVE:
2440 case OP_DECODE_COPY:
2441 case OP_DECRYPT_COPY:
2442 if (!WithCrypto && ch == OP_DECRYPT_COPY) {
2446 CHECK_MODE (IsHeader (extra));
2447 if (mutt_save_message (extra->hdr,
2448 (ch == OP_DECRYPT_SAVE) ||
2449 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2450 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2451 (ch == OP_DECRYPT_SAVE)
2452 || (ch == OP_DECRYPT_COPY) || 0, &redraw) == 0
2453 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2454 || ch == OP_DECRYPT_SAVE)) {
2455 if (option (OPTRESOLVE)) {
2457 rc = OP_MAIN_NEXT_UNDELETED;
2460 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2462 MAYBE_REDRAW (redraw);
2465 case OP_SHELL_ESCAPE:
2466 mutt_shell_escape ();
2467 MAYBE_REDRAW (redraw);
2471 CHECK_MODE (IsHeader (extra));
2472 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2474 Context->last_tag = extra->hdr->tagged ? extra->hdr :
2475 ((Context->last_tag == extra->hdr && !extra->hdr->tagged)
2476 ? NULL : Context->last_tag);
2478 redraw = REDRAW_STATUS | REDRAW_INDEX;
2479 if (option (OPTRESOLVE)) {
2486 CHECK_MODE (IsHeader (extra));
2489 CHECK_MX_ACL (Context, ACL_SEEN, _("Toggling"));
2491 if (extra->hdr->read || extra->hdr->old)
2492 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2494 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2496 Context->msgnotreadyet = -1;
2497 redraw = REDRAW_STATUS | REDRAW_INDEX;
2498 if (option (OPTRESOLVE)) {
2500 rc = OP_MAIN_NEXT_UNDELETED;
2505 CHECK_MODE (IsHeader (extra));
2508 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2510 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2511 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2512 redraw = REDRAW_STATUS | REDRAW_INDEX;
2513 if (option (OPTRESOLVE)) {
2519 case OP_UNDELETE_THREAD:
2520 case OP_UNDELETE_SUBTHREAD:
2521 CHECK_MODE (IsHeader (extra));
2524 CHECK_MX_ACL (Context, ACL_DELETE, _("Undeletion"));
2526 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2527 ch == OP_UNDELETE_THREAD ? 0 : 1)
2528 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2529 ch == OP_UNDELETE_THREAD ? 0 : 1);
2532 if (option (OPTRESOLVE)) {
2533 rc = (ch == OP_DELETE_THREAD) ?
2534 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2538 if (!option (OPTRESOLVE) && PagerIndexLines)
2539 redraw = REDRAW_FULL;
2541 redraw = REDRAW_STATUS | REDRAW_INDEX;
2550 if (option (OPTFORCEBUFFYCHECK))
2553 redraw |= REDRAW_SIDEBAR;
2556 case OP_VIEW_ATTACHMENTS:
2557 if (flags & M_PAGER_ATTACHMENT) {
2559 rc = OP_ATTACH_COLLAPSE;
2562 CHECK_MODE (IsHeader (extra));
2563 mutt_view_attachments (extra->hdr);
2564 if (extra->hdr->attach_del)
2565 Context->changed = 1;
2566 redraw = REDRAW_FULL;
2571 if (!(WithCrypto & APPLICATION_PGP)) {
2575 CHECK_MODE (IsHeader (extra));
2577 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2578 redraw = REDRAW_FULL;
2582 case OP_FORGET_PASSPHRASE:
2583 crypt_forget_passphrase ();
2586 case OP_EXTRACT_KEYS:
2591 CHECK_MODE (IsHeader (extra));
2592 crypt_extract_keys_from_messages (extra->hdr);
2593 redraw = REDRAW_FULL;
2596 case OP_SIDEBAR_SCROLL_UP:
2597 case OP_SIDEBAR_SCROLL_DOWN:
2598 case OP_SIDEBAR_NEXT:
2599 case OP_SIDEBAR_NEXT_NEW:
2600 case OP_SIDEBAR_PREV:
2601 case OP_SIDEBAR_PREV_NEW:
2602 sidebar_scroll (ch, MENU_PAGER);
2611 if (IsHeader (extra)) {
2612 Context->msgnotreadyet = -1;
2617 OldHdr = extra->hdr;
2621 cleanup_quote (&QuoteList);
2623 for (i = 0; i < maxLine; i++) {
2624 p_delete(&(lineInfo[i].syntax));
2625 if (SearchCompiled && lineInfo[i].search)
2626 p_delete(&(lineInfo[i].search));
2628 if (SearchCompiled) {
2629 regfree (&SearchRE);
2632 p_delete(&lineInfo);
2634 mutt_menuDestroy (&index);
2635 return (rc != -1 ? rc : 0);