2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 #include "mutt_curses.h"
21 #include "mutt_regex.h"
23 #include "mutt_menu.h"
34 #include "imap_private.h"
37 #include "mutt_crypt.h"
46 #define ISHEADER(x) ((x) == MT_COLOR_HEADER || (x) == MT_COLOR_HDEFAULT)
48 #define IsAttach(x) (x && (x)->bdy)
49 #define IsRecvAttach(x) (x && (x)->bdy && (x)->fp)
50 #define IsSendAttach(x) (x && (x)->bdy && !(x)->fp)
51 #define IsMsgAttach(x) (x && (x)->fp && (x)->bdy && (x)->bdy->hdr)
52 #define IsHeader(x) (x && (x)->hdr && !(x)->bdy)
54 static const char *Not_available_in_this_menu = N_("Not available in this menu.");
55 static const char *Mailbox_is_read_only = N_("Mailbox is read-only.");
56 static const char *Function_not_permitted_in_attach_message_mode = N_("Function not permitted in attach-message mode.");
58 #define CHECK_MODE(x) if (!(x)) \
61 mutt_error _(Not_available_in_this_menu); \
65 #define CHECK_READONLY if (Context->readonly) \
68 mutt_error _(Mailbox_is_read_only); \
72 #define CHECK_ATTACH if(option(OPTATTACHMSG)) \
75 mutt_error _(Function_not_permitted_in_attach_message_mode); \
80 /* the error message returned here could be better. */
81 #define CHECK_IMAP_ACL(aclbit) if (Context->magic == M_IMAP) \
82 if (mutt_bit_isset (((IMAP_DATA *)Context->data)->capabilities, ACL) \
83 && !mutt_bit_isset(((IMAP_DATA *)Context->data)->rights,aclbit)){ \
85 mutt_error ("Operation not permitted by the IMAP ACL for this mailbox"); \
96 struct q_class_t *next, *prev;
97 struct q_class_t *down, *up;
114 struct syntax_t *syntax;
115 struct syntax_t *search;
116 struct q_class_t *quote;
119 #define ANSI_OFF (1<<0)
120 #define ANSI_BLINK (1<<1)
121 #define ANSI_BOLD (1<<2)
122 #define ANSI_UNDERLINE (1<<3)
123 #define ANSI_REVERSE (1<<4)
124 #define ANSI_COLOR (1<<5)
126 typedef struct _ansi_attr {
133 static short InHelp = 0;
135 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
136 static struct resize {
143 #define NumSigLines 4
145 static int check_sig (const char *s, struct line_t *info, int n)
149 while (n > 0 && count <= NumSigLines)
151 if (info[n].type != MT_COLOR_SIGNATURE)
160 if (count > NumSigLines)
162 /* check for a blank line */
177 resolve_color (struct line_t *lineInfo, int n, int cnt, int flags, int special,
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)
190 if (!cnt && option (OPTMARKERS))
192 SETCOLOR (MT_COLOR_MARKERS);
194 last_color = ColorDefs[MT_COLOR_MARKERS];
196 m = (lineInfo[n].syntax)[0].first;
197 cnt += (lineInfo[n].syntax)[0].last;
201 if (!(flags & M_SHOWCOLOR))
202 def_color = ColorDefs[MT_COLOR_NORMAL];
203 else if (lineInfo[m].type == MT_COLOR_HEADER)
204 def_color = (lineInfo[m].syntax)[0].color;
206 def_color = ColorDefs[lineInfo[m].type];
208 if ((flags & M_SHOWCOLOR) && lineInfo[m].type == MT_COLOR_QUOTED)
210 struct q_class_t *class = lineInfo[m].quote;
214 def_color = class->color;
216 while (class && class->length > cnt)
218 def_color = class->color;
225 if (flags & M_SHOWCOLOR)
227 for (i = 0; i < lineInfo[m].chunks; i++)
229 /* we assume the chunks are sorted */
230 if (cnt > (lineInfo[m].syntax)[i].last)
232 if (cnt < (lineInfo[m].syntax)[i].first)
234 if (cnt != (lineInfo[m].syntax)[i].last)
236 color = (lineInfo[m].syntax)[i].color;
239 /* don't break here, as cnt might be
240 * in the next chunk as well */
244 if (flags & M_SEARCH)
246 for (i = 0; i < lineInfo[m].search_cnt; i++)
248 if (cnt > (lineInfo[m].search)[i].last)
250 if (cnt < (lineInfo[m].search)[i].first)
252 if (cnt != (lineInfo[m].search)[i].last)
254 color = ColorDefs[MT_COLOR_SEARCH];
261 /* handle "special" bold & underlined characters */
262 if (special || a->attr)
265 if ((a->attr & ANSI_COLOR))
268 a->pair = mutt_alloc_color (a->fg, a->bg);
270 if (a->attr & ANSI_BOLD)
275 if ((special & A_BOLD) || (a->attr & ANSI_BOLD))
277 if (ColorDefs[MT_COLOR_BOLD] && !search)
278 color = ColorDefs[MT_COLOR_BOLD];
282 if ((special & A_UNDERLINE) || (a->attr & ANSI_UNDERLINE))
284 if (ColorDefs[MT_COLOR_UNDERLINE] && !search)
285 color = ColorDefs[MT_COLOR_UNDERLINE];
287 color ^= A_UNDERLINE;
289 else if (a->attr & ANSI_REVERSE)
293 else if (a->attr & ANSI_BLINK)
297 else if (a->attr & ANSI_OFF)
303 if (color != last_color)
311 append_line (struct line_t *lineInfo, int n, int cnt)
315 lineInfo[n+1].type = lineInfo[n].type;
316 (lineInfo[n+1].syntax)[0].color = (lineInfo[n].syntax)[0].color;
317 lineInfo[n+1].continuation = 1;
319 /* find the real start of the line */
320 for (m = n; m >= 0; m--)
321 if (lineInfo[m].continuation == 0) break;
323 (lineInfo[n+1].syntax)[0].first = m;
324 (lineInfo[n+1].syntax)[0].last = (lineInfo[n].continuation) ?
325 cnt + (lineInfo[n].syntax)[0].last : cnt;
329 new_class_color (struct q_class_t *class, int *q_level)
331 class->index = (*q_level)++;
332 class->color = ColorQuote[class->index % ColorQuoteUsed];
336 shift_class_colors (struct q_class_t *QuoteList, struct q_class_t *new_class,
337 int index, int *q_level)
339 struct q_class_t * q_list;
342 new_class->index = -1;
346 if (q_list->index >= index)
349 q_list->color = ColorQuote[q_list->index % ColorQuoteUsed];
352 q_list = q_list->down;
353 else if (q_list->next)
354 q_list = q_list->next;
357 while (!q_list->next)
364 q_list = q_list->next;
368 new_class->index = index;
369 new_class->color = ColorQuote[index % ColorQuoteUsed];
374 cleanup_quote (struct q_class_t **QuoteList)
376 struct q_class_t *ptr;
380 if ((*QuoteList)->down)
381 cleanup_quote (&((*QuoteList)->down));
382 ptr = (*QuoteList)->next;
383 if ((*QuoteList)->prefix)
384 FREE (&(*QuoteList)->prefix);
392 static struct q_class_t *
393 classify_quote (struct q_class_t **QuoteList, const char *qptr,
394 int length, int *force_redraw, int *q_level)
396 struct q_class_t *q_list = *QuoteList;
397 struct q_class_t *class = NULL, *tmp = NULL, *ptr, *save;
399 int offset, tail_lng;
402 if (ColorQuoteUsed <= 1)
404 /* not much point in classifying quotes... */
406 if (*QuoteList == NULL)
408 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
409 class->color = ColorQuote[0];
415 /* Did I mention how much I like emulating Lisp in C? */
417 /* classify quoting prefix */
420 if (length <= q_list->length)
422 /* case 1: check the top level nodes */
424 if (mutt_strncmp (qptr, q_list->prefix, length) == 0)
426 if (length == q_list->length)
427 return q_list; /* same prefix: return the current class */
429 /* found shorter prefix */
432 /* add a node above q_list */
433 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
434 tmp->prefix = (char *) safe_calloc (1, length + 1);
435 strncpy (tmp->prefix, qptr, length);
436 tmp->length = length;
438 /* replace q_list by tmp in the top level list */
441 tmp->next = q_list->next;
442 q_list->next->prev = tmp;
446 tmp->prev = q_list->prev;
447 q_list->prev->next = tmp;
450 /* make q_list a child of tmp */
454 /* q_list has no siblings for now */
458 /* update the root if necessary */
459 if (q_list == *QuoteList)
462 index = q_list->index;
464 /* tmp should be the return class too */
467 /* next class to test; if tmp is a shorter prefix for another
468 * node, that node can only be in the top level list, so don't
469 * go down after this point
475 /* found another branch for which tmp is a shorter prefix */
477 /* save the next sibling for later */
480 /* unlink q_list from the top level list */
482 q_list->next->prev = q_list->prev;
484 q_list->prev->next = q_list->next;
486 /* at this point, we have a tmp->down; link q_list to it */
488 /* sibling order is important here, q_list should be linked last */
496 index = q_list->index;
498 /* next class to test; as above, we shouldn't go down */
502 /* we found a shorter prefix, so certain quotes have changed classes */
508 /* shorter, but not a substring of the current class: try next */
509 q_list = q_list->next;
515 /* case 2: try subclassing the current top level node */
517 /* tmp != NULL means we already found a shorter prefix at case 1 */
518 if (tmp == NULL && mutt_strncmp (qptr, q_list->prefix, q_list->length) == 0)
520 /* ok, it's a subclass somewhere on this branch */
523 offset = q_list->length;
525 q_list = q_list->down;
526 tail_lng = length - offset;
527 tail_qptr = (char *) qptr + offset;
531 if (length <= q_list->length)
533 if (mutt_strncmp (tail_qptr, (q_list->prefix) + offset, tail_lng) == 0)
535 /* same prefix: return the current class */
536 if (length == q_list->length)
539 /* found shorter common prefix */
542 /* add a node above q_list */
543 tmp = (struct q_class_t *) safe_calloc (1,
544 sizeof (struct q_class_t));
545 tmp->prefix = (char *) safe_calloc (1, length + 1);
546 strncpy (tmp->prefix, qptr, length);
547 tmp->length = length;
549 /* replace q_list by tmp */
552 tmp->next = q_list->next;
553 q_list->next->prev = tmp;
557 tmp->prev = q_list->prev;
558 q_list->prev->next = tmp;
561 /* make q_list a child of tmp */
563 tmp->up = q_list->up;
565 if (tmp->up->down == q_list)
568 /* q_list has no siblings */
572 index = q_list->index;
574 /* tmp should be the return class too */
577 /* next class to test */
582 /* found another branch for which tmp is a shorter prefix */
584 /* save the next sibling for later */
587 /* unlink q_list from the top level list */
589 q_list->next->prev = q_list->prev;
591 q_list->prev->next = q_list->next;
593 /* at this point, we have a tmp->down; link q_list to it */
602 index = q_list->index;
604 /* next class to test */
608 /* we found a shorter prefix, so we need a redraw */
614 q_list = q_list->next;
620 /* longer than the current prefix: try subclassing it */
621 if (tmp == NULL && mutt_strncmp (tail_qptr, (q_list->prefix) + offset,
622 q_list->length - offset) == 0)
624 /* still a subclass: go down one level */
626 offset = q_list->length;
628 q_list = q_list->down;
629 tail_lng = length - offset;
630 tail_qptr = (char *) qptr + offset;
636 /* nope, try the next prefix */
637 q_list = q_list->next;
643 /* still not found so far: add it as a sibling to the current node */
646 tmp = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
647 tmp->prefix = (char *) safe_calloc (1, length + 1);
648 strncpy (tmp->prefix, qptr, length);
649 tmp->length = length;
653 tmp->next = ptr->down;
654 ptr->down->prev = tmp;
659 new_class_color (tmp, q_level);
666 shift_class_colors (*QuoteList, tmp, index, q_level);
673 /* nope, try the next prefix */
674 q_list = q_list->next;
682 /* not found so far: add it as a top level class */
683 class = (struct q_class_t *) safe_calloc (1, sizeof (struct q_class_t));
684 class->prefix = (char *) safe_calloc (1, length + 1);
685 strncpy (class->prefix, qptr, length);
686 class->length = length;
687 new_class_color (class, q_level);
691 class->next = *QuoteList;
692 (*QuoteList)->prev = class;
698 shift_class_colors (*QuoteList, tmp, index, q_level);
703 static int check_attachment_marker (char *);
706 resolve_types (char *buf, char *raw, struct line_t *lineInfo, int n, int last,
707 struct q_class_t **QuoteList, int *q_level, int *force_redraw,
710 COLOR_LINE *color_line;
711 regmatch_t pmatch[1], smatch[1];
712 int found, offset, null_rx, i;
714 if (n == 0 || ISHEADER (lineInfo[n-1].type))
717 lineInfo[n].type = MT_COLOR_NORMAL;
718 else if (n > 0 && (buf[0] == ' ' || buf[0] == '\t'))
720 lineInfo[n].type = lineInfo[n-1].type; /* wrapped line */
721 (lineInfo[n].syntax)[0].color = (lineInfo[n-1].syntax)[0].color;
725 lineInfo[n].type = MT_COLOR_HDEFAULT;
726 color_line = ColorHdrList;
729 if (REGEXEC (color_line->rx, buf) == 0)
731 lineInfo[n].type = MT_COLOR_HEADER;
732 lineInfo[n].syntax[0].color = color_line->pair;
735 color_line = color_line->next;
739 else if (mutt_strncmp ("\033[0m", raw, 4) == 0) /* a little hack... */
740 lineInfo[n].type = MT_COLOR_NORMAL;
742 else if (mutt_strncmp ("[-- ", buf, 4) == 0)
743 lineInfo[n].type = MT_COLOR_ATTACHMENT;
745 else if (check_attachment_marker ((char *) raw) == 0)
746 lineInfo[n].type = MT_COLOR_ATTACHMENT;
748 else if (mutt_strcmp ("-- \n", buf) == 0 || mutt_strcmp ("-- \r\n", buf) == 0)
752 lineInfo[n].type = MT_COLOR_SIGNATURE;
753 while (i < last && check_sig (buf, lineInfo, i - 1) == 0 &&
754 (lineInfo[i].type == MT_COLOR_NORMAL ||
755 lineInfo[i].type == MT_COLOR_QUOTED ||
756 lineInfo[i].type == MT_COLOR_HEADER))
759 if (lineInfo[i].chunks)
761 lineInfo[i].chunks = 0;
762 safe_realloc (&(lineInfo[n].syntax),
763 sizeof (struct syntax_t));
765 lineInfo[i++].type = MT_COLOR_SIGNATURE;
768 else if (check_sig (buf, lineInfo, n - 1) == 0)
769 lineInfo[n].type = MT_COLOR_SIGNATURE;
770 else if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
772 if (regexec ((regex_t *) Smileys.rx, buf, 1, smatch, 0) == 0)
774 if (smatch[0].rm_so > 0)
778 /* hack to avoid making an extra copy of buf */
779 c = buf[smatch[0].rm_so];
780 buf[smatch[0].rm_so] = 0;
782 if (regexec ((regex_t *) QuoteRegexp.rx, buf, 1, pmatch, 0) == 0)
784 if (q_classify && lineInfo[n].quote == NULL)
785 lineInfo[n].quote = classify_quote (QuoteList,
786 buf + pmatch[0].rm_so,
787 pmatch[0].rm_eo - pmatch[0].rm_so,
788 force_redraw, q_level);
789 lineInfo[n].type = MT_COLOR_QUOTED;
792 lineInfo[n].type = MT_COLOR_NORMAL;
794 buf[smatch[0].rm_so] = c;
797 lineInfo[n].type = MT_COLOR_NORMAL;
801 if (q_classify && lineInfo[n].quote == NULL)
802 lineInfo[n].quote = classify_quote (QuoteList, buf + pmatch[0].rm_so,
803 pmatch[0].rm_eo - pmatch[0].rm_so,
804 force_redraw, q_level);
805 lineInfo[n].type = MT_COLOR_QUOTED;
809 lineInfo[n].type = MT_COLOR_NORMAL;
812 if (lineInfo[n].type == MT_COLOR_NORMAL ||
813 lineInfo[n].type == MT_COLOR_QUOTED)
818 lineInfo[n].chunks = 0;
826 color_line = ColorBodyList;
829 if (regexec (&color_line->rx, buf + offset, 1, pmatch,
830 (offset ? REG_NOTBOL : 0)) == 0)
832 if (pmatch[0].rm_eo != pmatch[0].rm_so)
836 if (++(lineInfo[n].chunks) > 1)
837 safe_realloc (&(lineInfo[n].syntax),
838 (lineInfo[n].chunks) * sizeof (struct syntax_t));
840 i = lineInfo[n].chunks - 1;
841 pmatch[0].rm_so += offset;
842 pmatch[0].rm_eo += offset;
844 pmatch[0].rm_so < (lineInfo[n].syntax)[i].first ||
845 (pmatch[0].rm_so == (lineInfo[n].syntax)[i].first &&
846 pmatch[0].rm_eo > (lineInfo[n].syntax)[i].last))
848 (lineInfo[n].syntax)[i].color = color_line->pair;
849 (lineInfo[n].syntax)[i].first = pmatch[0].rm_so;
850 (lineInfo[n].syntax)[i].last = pmatch[0].rm_eo;
856 null_rx = 1; /* empty regexp; don't add it, but keep looking */
858 color_line = color_line->next;
862 offset++; /* avoid degenerate cases */
864 offset = (lineInfo[n].syntax)[i].last;
865 } while (found || null_rx);
869 static int is_ansi (unsigned char *buf)
871 while (*buf && (isdigit(*buf) || *buf == ';'))
873 return (*buf == 'm');
876 static int check_attachment_marker (char *p)
878 char *q = AttachmentMarker;
880 for (;*p == *q && *q && *p && *q != '\a' && *p != '\a'; p++, q++)
882 return (int) (*p - *q);
885 static int grok_ansi(unsigned char *buf, int pos, ansi_attr *a)
889 while (isdigit(buf[x]) || buf[x] == ';')
892 /* Character Attributes */
893 if (option (OPTALLOWANSI) && a != NULL && buf[x] == 'm')
899 mutt_free_color (a->fg, a->bg);
906 if (buf[pos] == '1' && (pos+1 == x || buf[pos+1] == ';'))
908 a->attr |= ANSI_BOLD;
911 else if (buf[pos] == '4' && (pos+1 == x || buf[pos+1] == ';'))
913 a->attr |= ANSI_UNDERLINE;
916 else if (buf[pos] == '5' && (pos+1 == x || buf[pos+1] == ';'))
918 a->attr |= ANSI_BLINK;
921 else if (buf[pos] == '7' && (pos+1 == x || buf[pos+1] == ';'))
923 a->attr |= ANSI_REVERSE;
926 else if (buf[pos] == '0' && (pos+1 == x || buf[pos+1] == ';'))
930 mutt_free_color(a->fg,a->bg);
936 else if (buf[pos] == '3' && isdigit(buf[pos+1]))
940 mutt_free_color(a->fg,a->bg);
943 a->attr |= ANSI_COLOR;
944 a->fg = buf[pos+1] - '0';
947 else if (buf[pos] == '4' && isdigit(buf[pos+1]))
951 mutt_free_color(a->fg,a->bg);
954 a->attr |= ANSI_COLOR;
955 a->bg = buf[pos+1] - '0';
960 while (pos < x && buf[pos] != ';') pos++;
970 fill_buffer (FILE *f, long *last_pos, long offset, unsigned char *buf,
971 unsigned char *fmt, size_t blen, int *buf_ready)
979 if (offset != *last_pos)
980 fseek (f, offset, 0);
981 if (fgets ((char *) buf, blen - 1, f) == NULL)
986 *last_pos = ftell (f);
987 b_read = (int) (*last_pos - offset);
990 /* copy "buf" to "fmt", but without bold and underline controls */
994 if (*p == '\010' && (p > buf))
996 if (*(p+1) == '_') /* underline */
998 else if (*(p+1)) /* bold or overstrike */
1006 else if (*p == '\033' && *(p+1) == '[' && is_ansi (p + 2))
1008 while (*p++ != 'm') /* skip ANSI sequence */
1011 else if (*p == '\033' && *(p+1) == ']' && check_attachment_marker ((char *) p) == 0)
1013 dprint (2, (debugfile, "fill_buffer: Seen attachment marker.\n"));
1014 while (*p++ != '\a') /* skip pseudo-ANSI sequence */
1031 static int format_line (struct line_t **lineInfo, int n, unsigned char *buf,
1032 int flags, ansi_attr *pa, int cnt,
1033 int *pspace, int *pvch, int *pcol, int *pspecial)
1035 int space = -1; /* index of the last space or TAB */
1036 int col = option (OPTMARKERS) ? (*lineInfo)[n].continuation : 0;
1037 int ch, vch, k, last_special = -1, special = 0, t;
1041 int wrap_cols = COLS - WrapMargin - SidebarWidth;
1046 /* FIXME: this should come from lineInfo */
1047 memset(&mbstate, 0, sizeof(mbstate));
1049 for (ch = 0, vch = 0; ch < cnt; ch += k, vch += k)
1051 /* Handle ANSI sequences */
1052 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == '[' &&
1054 ch = grok_ansi (buf, ch+2, pa) + 1;
1056 while (cnt-ch >= 2 && buf[ch] == '\033' && buf[ch+1] == ']' &&
1057 check_attachment_marker ((char *) buf+ch) == 0)
1059 while (buf[ch++] != '\a')
1064 /* is anything left to do? */
1068 k = mbrtowc (&wc, (char *)buf+ch, cnt-ch, &mbstate);
1069 if (k == -2 || k == -1)
1071 dprint (1, (debugfile, "%s:%d: mbrtowc returned %d; errno = %d.\n",
1072 __FILE__, __LINE__, k, errno));
1073 if (col + 4 > wrap_cols)
1077 printw ("\\%03o", buf[ch]);
1084 /* Handle backspace */
1092 while ((wc1 = 0, mbstate1 = mbstate,
1093 k1 = k + mbrtowc (&wc1, (char *)buf+ch+k, cnt-ch-k, &mbstate1),
1094 k1 - k > 0 && wc1 == '\b') &&
1096 k2 = mbrtowc (&wc1, (char *)buf+ch+k1, cnt-ch-k1, &mbstate1),
1097 k2 > 0 && IsWPrint (wc1)))
1101 special |= (wc == '_' && special & A_UNDERLINE)
1102 ? A_UNDERLINE : A_BOLD;
1104 else if (wc == '_' || wc1 == '_')
1106 special |= A_UNDERLINE;
1107 wc = (wc1 == '_') ? wc : wc1;
1111 /* special = 0; / * overstrike: nothing to do! */
1121 ((flags & (M_SHOWCOLOR | M_SEARCH | M_PAGER_MARKER)) ||
1122 special || last_special || pa->attr))
1124 resolve_color (*lineInfo, n, vch, flags, special, pa);
1125 last_special = special;
1133 if (col + t > wrap_cols)
1139 else if (wc == '\n')
1141 else if (wc == '\t')
1148 for (; col < t; col++)
1153 else if (wc < 0x20 || wc == 0x7f)
1155 if (col + 2 > wrap_cols)
1159 printw ("^%c", ('@' + wc) & 0x7f);
1161 else if (wc < 0x100)
1163 if (col + 4 > wrap_cols)
1167 printw ("\\%03o", wc);
1171 if (col + 1 > wrap_cols)
1175 addch (replacement_char ());
1181 *pspecial = special;
1187 * flags M_SHOWFLAT, show characters (used for displaying help)
1188 * M_SHOWCOLOR, show characters in color
1189 * otherwise don't show characters
1190 * M_HIDE, don't show quoted text
1191 * M_SEARCH, resolve search patterns
1192 * M_TYPES, compute line's type
1193 * M_PAGER_NSKIP, keeps leading whitespace
1194 * M_PAGER_MARKER, eventually show markers
1197 * -1 EOF was reached
1198 * 0 normal exit, line was not displayed
1199 * >0 normal exit, line was displayed
1203 display_line (FILE *f, long *last_pos, struct line_t **lineInfo, int n,
1204 int *last, int *max, int flags, struct q_class_t **QuoteList,
1205 int *q_level, int *force_redraw, regex_t *SearchRE)
1207 unsigned char buf[LONG_STRING], fmt[LONG_STRING];
1208 unsigned char *buf_ptr = buf;
1209 int ch, vch, col, cnt, b_read;
1210 int buf_ready = 0, change_last = 0;
1215 ansi_attr a = {0,0,0,-1};
1216 regmatch_t pmatch[1];
1226 safe_realloc (lineInfo, sizeof (struct line_t) * (*max += LINES));
1227 for (ch = *last; ch < *max ; ch++)
1229 memset (&((*lineInfo)[ch]), 0, sizeof (struct line_t));
1230 (*lineInfo)[ch].type = -1;
1231 (*lineInfo)[ch].search_cnt = -1;
1232 (*lineInfo)[ch].syntax = safe_malloc (sizeof (struct syntax_t));
1233 ((*lineInfo)[ch].syntax)[0].first = ((*lineInfo)[ch].syntax)[0].last = -1;
1237 /* only do color hiliting if we are viewing a message */
1238 if (flags & (M_SHOWCOLOR | M_TYPES))
1240 if ((*lineInfo)[n].type == -1)
1242 /* determine the line class */
1243 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1250 resolve_types ((char *) fmt, (char *) buf, *lineInfo, n, *last,
1251 QuoteList, q_level, force_redraw, flags & M_SHOWCOLOR);
1253 /* avoid race condition for continuation lines when scrolling up */
1254 for (m = n + 1; m < *last && (*lineInfo)[m].offset && (*lineInfo)[m].continuation; m++)
1255 (*lineInfo)[m].type = (*lineInfo)[n].type;
1258 /* this also prevents searching through the hidden lines */
1259 if ((flags & M_HIDE) && (*lineInfo)[n].type == MT_COLOR_QUOTED)
1260 flags = 0; /* M_NOSHOW */
1263 /* At this point, (*lineInfo[n]).quote may still be undefined. We
1264 * don't want to compute it every time M_TYPES is set, since this
1265 * would slow down the "bottom" function unacceptably. A compromise
1266 * solution is hence to call regexec() again, just to find out the
1267 * length of the quote prefix.
1269 if ((flags & M_SHOWCOLOR) && !(*lineInfo)[n].continuation &&
1270 (*lineInfo)[n].type == MT_COLOR_QUOTED && (*lineInfo)[n].quote == NULL)
1272 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1278 regexec ((regex_t *) QuoteRegexp.rx, (char *) fmt, 1, pmatch, 0);
1279 (*lineInfo)[n].quote = classify_quote (QuoteList,
1280 (char *) fmt + pmatch[0].rm_so,
1281 pmatch[0].rm_eo - pmatch[0].rm_so,
1282 force_redraw, q_level);
1285 if ((flags & M_SEARCH) && !(*lineInfo)[n].continuation && (*lineInfo)[n].search_cnt == -1)
1287 if (fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt, sizeof (buf), &buf_ready) < 0)
1295 (*lineInfo)[n].search_cnt = 0;
1296 while (regexec (SearchRE, (char *) fmt + offset, 1, pmatch, (offset ? REG_NOTBOL : 0)) == 0)
1298 if (++((*lineInfo)[n].search_cnt) > 1)
1299 safe_realloc (&((*lineInfo)[n].search),
1300 ((*lineInfo)[n].search_cnt) * sizeof (struct syntax_t));
1302 (*lineInfo)[n].search = safe_malloc (sizeof (struct syntax_t));
1303 pmatch[0].rm_so += offset;
1304 pmatch[0].rm_eo += offset;
1305 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].first = pmatch[0].rm_so;
1306 ((*lineInfo)[n].search)[(*lineInfo)[n].search_cnt - 1].last = pmatch[0].rm_eo;
1308 if (pmatch[0].rm_eo == pmatch[0].rm_so)
1309 offset++; /* avoid degenerate cases */
1311 offset = pmatch[0].rm_eo;
1317 if (!(flags & M_SHOW) && (*lineInfo)[n+1].offset > 0)
1319 /* we've already scanned this line, so just exit */
1322 if ((flags & M_SHOWCOLOR) && *force_redraw && (*lineInfo)[n+1].offset > 0)
1324 /* no need to try to display this line... */
1325 return (1); /* fake display */
1328 if ((b_read = fill_buffer (f, last_pos, (*lineInfo)[n].offset, buf, fmt,
1329 sizeof (buf), &buf_ready)) < 0)
1336 /* now chose a good place to break the line */
1337 cnt = format_line (lineInfo, n, buf, flags, 0, b_read, &ch, &vch, &col, &special);
1338 buf_ptr = buf + cnt;
1340 /* move the break point only if smart_wrap is set */
1341 if (option (OPTWRAP))
1345 if (ch != -1 && buf[cnt] != ' ' && buf[cnt] != '\t' && buf[cnt] != '\n' && buf[cnt] != '\r')
1348 /* skip trailing blanks */
1349 while (ch && (buf[ch] == ' ' || buf[ch] == '\t' || buf[ch] == '\r'))
1354 buf_ptr = buf + cnt; /* a very long word... */
1356 if (!(flags & M_PAGER_NSKIP))
1357 /* skip leading blanks on the next line too */
1358 while (*buf_ptr == ' ' || *buf_ptr == '\t')
1362 if (*buf_ptr == '\r')
1364 if (*buf_ptr == '\n')
1367 if ((int) (buf_ptr - buf) < b_read && !(*lineInfo)[n+1].continuation)
1368 append_line (*lineInfo, n, (int) (buf_ptr - buf));
1369 (*lineInfo)[n+1].offset = (*lineInfo)[n].offset + (long) (buf_ptr - buf);
1371 /* if we don't need to display the line we are done */
1372 if (!(flags & M_SHOW))
1375 /* display the line */
1376 format_line (lineInfo, n, buf, flags, &a, cnt, &ch, &vch, &col, &special);
1378 /* avoid a bug in ncurses... */
1379 #ifndef USE_SLANG_CURSES
1382 SETCOLOR (MT_COLOR_NORMAL);
1387 /* end the last color pattern (needed by S-Lang) */
1388 if (special || (col != COLS && (flags & (M_SHOWCOLOR | M_SEARCH))))
1389 resolve_color (*lineInfo, n, vch, flags, 0, &a);
1392 * Fill the blank space at the end of the line with the prevailing color.
1393 * ncurses does an implicit clrtoeol() when you do addch('\n') so we have
1394 * to make sure to reset the color *after* that
1396 if (flags & M_SHOWCOLOR)
1398 m = ((*lineInfo)[n].continuation) ? ((*lineInfo)[n].syntax)[0].first : n;
1399 if ((*lineInfo)[m].type == MT_COLOR_HEADER)
1400 def_color = ((*lineInfo)[m].syntax)[0].color;
1402 def_color = ColorDefs[ (*lineInfo)[m].type ];
1404 attrset (def_color);
1406 bkgdset (def_color | ' ');
1410 /* ncurses always wraps lines when you get to the right side of the
1411 * screen, but S-Lang seems to only wrap if the next character is *not*
1414 #ifndef USE_SLANG_CURSES
1420 * reset the color back to normal. This *must* come after the
1421 * addch('\n'), otherwise the color for this line will not be
1422 * filled to the right margin.
1424 if (flags & M_SHOWCOLOR)
1426 SETCOLOR(MT_COLOR_NORMAL);
1427 BKGDSET(MT_COLOR_NORMAL);
1430 /* build a return code */
1431 if (!(flags & M_SHOW))
1438 upNLines (int nlines, struct line_t *info, int cur, int hiding)
1440 while (cur > 0 && nlines > 0)
1443 if (!hiding || info[cur].type != MT_COLOR_QUOTED)
1451 mutt_display_xface (HEADER *hdr)
1456 if (slrnface_fd < 0)
1462 face = hdr->env->x_face;
1464 if (face == NULL || face->data == NULL)
1465 write(slrnface_fd, "clear\n", sizeof "clear");
1470 len = snprintf (buf, sizeof (buf), "xface %s\n", face->data);
1471 if (len <= sizeof (buf))
1473 write (slrnface_fd, buf, len);
1477 * slrnface will ignore X-Faces larger than approx. 2000 chars, so
1478 * try the next one, if it exists.
1480 } while (face = face->next);
1484 mutt_clear_xface (void)
1486 if (slrnface_fd < 0)
1489 write(slrnface_fd, "clear\n", sizeof "clear");
1493 mutt_show_xface (void)
1495 if (slrnface_fd < 0)
1498 write(slrnface_fd, "show\n", sizeof "show");
1501 static struct mapping_t PagerHelp[] = {
1502 { N_("Exit"), OP_EXIT },
1503 { N_("PrevPg"), OP_PREV_PAGE },
1504 { N_("NextPg"), OP_NEXT_PAGE },
1507 static struct mapping_t PagerHelpExtra[] = {
1508 { N_("View Attachm."), OP_VIEW_ATTACHMENTS },
1509 { N_("Del"), OP_DELETE },
1510 { N_("Reply"), OP_REPLY },
1511 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1516 static struct mapping_t PagerNewsHelpExtra[] = {
1517 { N_("Post"), OP_POST },
1518 { N_("Followup"), OP_FOLLOWUP },
1519 { N_("Del"), OP_DELETE },
1520 { N_("Next"), OP_MAIN_NEXT_UNDELETED },
1527 /* This pager is actually not so simple as it once was. It now operates in
1528 two modes: one for viewing messages and the other for viewing help. These
1529 can be distinguished by whether or not ``hdr'' is NULL. The ``hdr'' arg
1530 is there so that we can do operations on the current message without the
1531 need to pop back out to the main-menu. */
1533 mutt_pager (const char *banner, const char *fname, int flags, pager_t *extra)
1535 static char searchbuf[STRING];
1536 char buffer[LONG_STRING];
1537 char helpstr[SHORT_STRING*2];
1538 char tmphelp[SHORT_STRING*2];
1539 int maxLine, lastLine = 0;
1540 struct line_t *lineInfo;
1541 struct q_class_t *QuoteList = NULL;
1542 int i, j, ch = 0, rc = -1, hideQuoted = 0, q_level = 0, force_redraw = 0;
1543 int lines = 0, curline = 0, topline = 0, oldtopline = 0, err, first = 1;
1545 int redraw = REDRAW_FULL;
1547 long last_pos = 0, last_offset = 0;
1548 int old_smart_wrap, old_markers;
1551 int SearchCompiled = 0, SearchFlag = 0, SearchBack = 0;
1552 int has_types = (IsHeader(extra) || (flags & M_SHOWCOLOR)) ? M_TYPES : 0; /* main message or rfc822 attachment */
1554 int bodyoffset = 1; /* offset of first line of real text */
1555 int statusoffset = 0; /* offset for the status bar */
1556 int helpoffset = LINES - 2; /* offset for the help bar. */
1557 int bodylen = LINES - 2 - bodyoffset; /* length of displayable area */
1559 MUTTMENU *index = NULL; /* the Pager Index (PI) */
1560 int indexoffset = 0; /* offset for the PI */
1561 int indexlen = PagerIndexLines; /* indexlen not always == PIL */
1562 int indicator = indexlen / 3; /* the indicator line of the PI */
1563 int old_PagerIndexLines; /* some people want to resize it
1564 * while inside the pager... */
1570 if (!(flags & M_SHOWCOLOR))
1571 flags |= M_SHOWFLAT;
1573 if ((fp = fopen (fname, "r")) == NULL)
1575 mutt_perror (fname);
1579 if (stat (fname, &sb) != 0)
1581 mutt_perror (fname);
1587 /* Initialize variables */
1589 if (IsHeader (extra) && !extra->hdr->read)
1591 Context->msgnotreadyet = extra->hdr->msgno;
1592 mutt_set_flag (Context, extra->hdr, M_READ, 1);
1595 lineInfo = safe_malloc (sizeof (struct line_t) * (maxLine = LINES));
1596 for (i = 0 ; i < maxLine ; i++)
1598 memset (&lineInfo[i], 0, sizeof (struct line_t));
1599 lineInfo[i].type = -1;
1600 lineInfo[i].search_cnt = -1;
1601 lineInfo[i].syntax = safe_malloc (sizeof (struct syntax_t));
1602 (lineInfo[i].syntax)[0].first = (lineInfo[i].syntax)[0].last = -1;
1605 mutt_compile_help (helpstr, sizeof (helpstr), MENU_PAGER, PagerHelp);
1606 if (IsHeader (extra))
1608 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1609 mutt_compile_help (buffer, sizeof (buffer), MENU_PAGER,
1611 (Context && (Context->magic == M_NNTP)) ? PagerNewsHelpExtra :
1614 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1618 strfcpy (tmphelp, helpstr, sizeof (tmphelp));
1619 mutt_make_help (buffer, sizeof (buffer), _("Help"), MENU_PAGER, OP_HELP);
1620 snprintf (helpstr, sizeof (helpstr), "%s %s", tmphelp, buffer);
1623 if (IsHeader (extra))
1624 mutt_display_xface(extra->hdr);
1634 if (redraw & REDRAW_FULL)
1636 SETCOLOR (MT_COLOR_NORMAL);
1637 /* clear() doesn't optimize screen redraws */
1641 if (IsHeader (extra) && Context->vcount + 1 < PagerIndexLines)
1642 indexlen = Context->vcount + 1;
1644 indexlen = PagerIndexLines;
1646 indicator = indexlen / 3;
1648 if (option (OPTSTATUSONTOP))
1651 statusoffset = IsHeader (extra) ? indexlen : 0;
1652 bodyoffset = statusoffset + 1;
1653 helpoffset = LINES - 2;
1654 bodylen = helpoffset - bodyoffset;
1655 if (!option (OPTHELP))
1662 statusoffset = LINES - 2;
1663 if (!option (OPTHELP))
1665 bodyoffset = indexoffset + (IsHeader (extra) ? indexlen : 0);
1666 bodylen = statusoffset - bodyoffset;
1669 if (option (OPTHELP))
1671 SETCOLOR (MT_COLOR_STATUS);
1672 move (helpoffset, 0);
1673 mutt_paddstr (COLS, helpstr);
1674 SETCOLOR (MT_COLOR_NORMAL);
1677 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1680 if ((SearchCompiled = Resize->SearchCompiled))
1683 (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf));
1684 SearchFlag = M_SEARCH;
1685 SearchBack = Resize->SearchBack;
1687 lines = Resize->line;
1688 redraw |= REDRAW_SIGWINCH;
1694 if (IsHeader (extra) && PagerIndexLines)
1698 /* only allocate the space if/when we need the index.
1699 Initialise the menu as per the main index */
1700 index = mutt_new_menu();
1701 index->menu = MENU_MAIN;
1702 index->make_entry = index_make_entry;
1703 index->color = index_color;
1704 index->max = Context->vcount;
1705 index->current = extra->hdr->virtual;
1708 SETCOLOR (MT_COLOR_NORMAL);
1709 index->offset = indexoffset + (option (OPTSTATUSONTOP) ? 1 : 0);
1711 index->pagelen = indexlen - 1;
1713 /* some fudge to work out where abouts the indicator should go */
1714 if (index->current - indicator < 0)
1716 else if (index->max - index->current < index->pagelen - indicator)
1717 index->top = index->max - index->pagelen;
1719 index->top = index->current - indicator;
1721 menu_redraw_index(index);
1724 redraw |= REDRAW_BODY | REDRAW_INDEX | REDRAW_STATUS;
1728 if (redraw & REDRAW_SIGWINCH)
1732 while (display_line (fp, &last_pos, &lineInfo, ++i, &lastLine, &maxLine,
1733 has_types | SearchFlag, &QuoteList, &q_level, &force_redraw,
1735 if (!lineInfo[i].continuation && ++j == lines)
1743 if ((redraw & REDRAW_BODY) || topline != oldtopline)
1746 move (bodyoffset, SidebarWidth);
1747 curline = oldtopline = topline;
1751 while (lines < bodylen && lineInfo[curline].offset <= sb.st_size - 1)
1753 if (display_line (fp, &last_pos, &lineInfo, curline, &lastLine,
1755 (flags & M_DISPLAYFLAGS) | hideQuoted | SearchFlag,
1756 &QuoteList, &q_level, &force_redraw, &SearchRE) > 0)
1759 move(lines + bodyoffset, SidebarWidth);
1761 last_offset = lineInfo[curline].offset;
1762 } while (force_redraw);
1764 SETCOLOR (MT_COLOR_TILDE);
1765 BKGDSET (MT_COLOR_TILDE);
1766 while (lines < bodylen)
1769 if (option (OPTTILDE))
1773 move(lines + bodyoffset, SidebarWidth);
1775 /* We are going to update the pager status bar, so it isn't
1776 * necessary to reset to normal color now. */
1778 redraw |= REDRAW_STATUS; /* need to update the % seen */
1781 if (redraw & REDRAW_STATUS)
1783 /* print out the pager status bar */
1784 SETCOLOR (MT_COLOR_STATUS);
1785 BKGDSET (MT_COLOR_STATUS);
1786 CLEARLINE_WIN(statusoffset);
1787 if (IsHeader (extra))
1789 _mutt_make_string (buffer,
1790 COLS-9-SidebarWidth < sizeof (buffer) ?
1791 COLS-9-SidebarWidth : sizeof (buffer),
1792 NONULL (PagerFmt), Context, extra->hdr, M_FORMAT_MAKEPRINT);
1794 else if (IsMsgAttach (extra))
1796 _mutt_make_string (buffer,
1797 COLS - 9 - SidebarWidth < sizeof (buffer) ?
1798 COLS - 9 - SidebarWidth : sizeof (buffer),
1799 NONULL (PagerFmt), Context, extra->bdy->hdr, M_FORMAT_MAKEPRINT);
1801 mutt_paddstr (COLS-10-SidebarWidth, IsHeader (extra) || IsMsgAttach (extra) ?
1804 if (last_pos < sb.st_size - 1)
1805 printw ("%d%%)", (int) (100 * last_offset / sb.st_size));
1807 addstr (topline == 0 ? "all)" : "end)");
1808 BKGDSET (MT_COLOR_NORMAL);
1809 SETCOLOR (MT_COLOR_NORMAL);
1812 if ((redraw & REDRAW_INDEX) && index)
1814 /* redraw the pager_index indicator, because the
1815 * flags for this message might have changed. */
1816 menu_redraw_current (index);
1817 draw_sidebar(MENU_PAGER);
1818 /* print out the index status bar */
1819 menu_status_line (buffer, sizeof (buffer), index, NONULL(Status));
1820 move (indexoffset + (option (OPTSTATUSONTOP) ? 0 : (indexlen - 1)),
1822 SETCOLOR (MT_COLOR_STATUS);
1823 mutt_paddstr (COLS-SidebarWidth, buffer);
1824 SETCOLOR (MT_COLOR_NORMAL);
1826 /* if we're not using the index, update every time */
1828 draw_sidebar(MENU_PAGER);
1832 move (statusoffset, COLS-1);
1834 ch = km_dokey (MENU_PAGER);
1836 mutt_clear_error ();
1844 #if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
1847 mutt_resize_screen ();
1849 /* Store current position. */
1851 for (i = 0; i <= topline; i++)
1852 if (!lineInfo[i].continuation)
1855 if (flags & M_PAGER_RETWINCH)
1857 Resize = safe_malloc (sizeof (struct resize));
1859 Resize->line = lines;
1860 Resize->SearchCompiled = SearchCompiled;
1861 Resize->SearchBack = SearchBack;
1864 rc = OP_REFORMAT_WINCH;
1868 for (i = 0; i < maxLine; i++)
1870 lineInfo[i].offset = 0;
1871 lineInfo[i].type = -1;
1872 lineInfo[i].continuation = 0;
1873 lineInfo[i].chunks = 0;
1874 lineInfo[i].search_cnt = -1;
1875 lineInfo[i].quote = NULL;
1877 safe_realloc (&(lineInfo[i].syntax),
1878 sizeof (struct syntax_t));
1879 if (SearchCompiled && lineInfo[i].search)
1880 FREE (&(lineInfo[i].search));
1886 redraw = REDRAW_FULL | REDRAW_SIGWINCH;
1891 clearok(stdscr,TRUE);/*force complete redraw*/
1911 if (lineInfo[curline].offset < sb.st_size-1)
1913 topline = upNLines (PagerContext, lineInfo, curline, hideQuoted);
1915 else if (option (OPTPAGERSTOP))
1917 /* emulate "less -q" and don't go on to the next message. */
1918 mutt_error _("Bottom of message is shown.");
1922 /* end of the current message, so display the next message. */
1923 rc = OP_MAIN_NEXT_UNDELETED;
1931 topline = upNLines (bodylen-PagerContext, lineInfo, topline, hideQuoted);
1934 mutt_error _("Top of message is shown.");
1938 if (lineInfo[curline].offset < sb.st_size-1)
1943 while (lineInfo[topline].type == MT_COLOR_QUOTED &&
1949 mutt_error _("Bottom of message is shown.");
1954 topline = upNLines (1, lineInfo, topline, hideQuoted);
1956 mutt_error _("Top of message is shown.");
1963 mutt_error _("Top of message is shown.");
1968 topline = upNLines (bodylen/2, lineInfo, topline, hideQuoted);
1970 mutt_error _("Top of message is shown.");
1974 if (lineInfo[curline].offset < sb.st_size-1)
1976 topline = upNLines (bodylen/2, lineInfo, curline, hideQuoted);
1978 else if (option (OPTPAGERSTOP))
1980 /* emulate "less -q" and don't go on to the next message. */
1981 mutt_error _("Bottom of message is shown.");
1985 /* end of the current message, so display the next message. */
1986 rc = OP_MAIN_NEXT_UNDELETED;
1991 case OP_SEARCH_NEXT:
1992 case OP_SEARCH_OPPOSITE:
1996 if ((!SearchBack && ch==OP_SEARCH_NEXT) ||
1997 (SearchBack &&ch==OP_SEARCH_OPPOSITE))
1999 /* searching forward */
2000 for (i = topline + 1; i < lastLine; i++)
2002 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2003 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2010 mutt_error _("Not found.");
2014 /* searching backward */
2015 for (i = topline - 1; i >= 0; i--)
2017 if ((!hideQuoted || (has_types &&
2018 lineInfo[i].type != MT_COLOR_QUOTED)) &&
2019 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2026 mutt_error _("Not found.");
2029 if (lineInfo[topline].search_cnt > 0)
2030 SearchFlag = M_SEARCH;
2034 /* no previous search pattern, so fall through to search */
2037 case OP_SEARCH_REVERSE:
2038 strfcpy (buffer, searchbuf, sizeof (buffer));
2039 if (mutt_get_field ((SearchBack ? _("Reverse search: ") :
2040 _("Search: ")), buffer, sizeof (buffer),
2044 if (!strcmp (buffer, searchbuf))
2048 /* do an implicit search-next */
2049 if (ch == OP_SEARCH)
2050 ch = OP_SEARCH_NEXT;
2052 ch = OP_SEARCH_OPPOSITE;
2061 strfcpy (searchbuf, buffer, sizeof (searchbuf));
2063 /* leave SearchBack alone if ch == OP_SEARCH_NEXT */
2064 if (ch == OP_SEARCH)
2066 else if (ch == OP_SEARCH_REVERSE)
2071 regfree (&SearchRE);
2072 for (i = 0; i < lastLine; i++)
2074 if (lineInfo[i].search)
2075 FREE (&(lineInfo[i].search));
2076 lineInfo[i].search_cnt = -1;
2080 if ((err = REGCOMP (&SearchRE, searchbuf, REG_NEWLINE | mutt_which_case (searchbuf))) != 0)
2082 regerror (err, &SearchRE, buffer, sizeof (buffer));
2083 mutt_error ("%s", buffer);
2084 regfree (&SearchRE);
2085 for (i = 0; i < maxLine ; i++)
2088 if (lineInfo[i].search)
2089 FREE (&(lineInfo[i].search));
2090 lineInfo[i].search_cnt = -1;
2098 /* update the search pointers */
2100 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2101 &maxLine, M_SEARCH | (flags & M_PAGER_NSKIP),
2102 &QuoteList, &q_level,
2103 &force_redraw, &SearchRE) == 0)
2108 /* searching forward */
2109 for (i = topline; i < lastLine; i++)
2111 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2112 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2116 if (i < lastLine) topline = i;
2120 /* searching backward */
2121 for (i = topline; i >= 0; i--)
2123 if ((!hideQuoted || lineInfo[i].type != MT_COLOR_QUOTED) &&
2124 !lineInfo[i].continuation && lineInfo[i].search_cnt > 0)
2128 if (i >= 0) topline = i;
2131 if (lineInfo[topline].search_cnt == 0)
2134 mutt_error _("Not found.");
2137 SearchFlag = M_SEARCH;
2139 redraw = REDRAW_BODY;
2142 case OP_SEARCH_TOGGLE:
2145 SearchFlag ^= M_SEARCH;
2146 redraw = REDRAW_BODY;
2151 /* don't let the user enter the help-menu from the help screen! */
2155 mutt_clear_xface ();
2156 mutt_help (MENU_PAGER);
2158 redraw = REDRAW_FULL;
2162 mutt_error _("Help is currently being shown.");
2165 case OP_PAGER_HIDE_QUOTED:
2168 hideQuoted ^= M_HIDE;
2169 if (hideQuoted && lineInfo[topline].type == MT_COLOR_QUOTED)
2170 topline = upNLines (1, lineInfo, topline, hideQuoted);
2172 redraw = REDRAW_BODY;
2176 case OP_PAGER_SKIP_QUOTED:
2180 int new_topline = topline;
2182 while ((new_topline < lastLine ||
2183 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2184 new_topline, &lastLine, &maxLine, M_TYPES,
2185 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2186 && lineInfo[new_topline].type != MT_COLOR_QUOTED)
2191 mutt_error _("No more quoted text.");
2195 while ((new_topline < lastLine ||
2196 (0 == (dretval = display_line (fp, &last_pos, &lineInfo,
2197 new_topline, &lastLine, &maxLine, M_TYPES,
2198 &QuoteList, &q_level, &force_redraw, &SearchRE))))
2199 && lineInfo[new_topline].type == MT_COLOR_QUOTED)
2204 mutt_error _("No more unquoted text after quoted text.");
2207 topline = new_topline;
2211 case OP_PAGER_BOTTOM: /* move to the end of the file */
2212 if (lineInfo[curline].offset < sb.st_size - 1)
2215 /* make sure the types are defined to the end of file */
2216 while (display_line (fp, &last_pos, &lineInfo, i, &lastLine,
2217 &maxLine, has_types,
2218 &QuoteList, &q_level, &force_redraw,
2221 topline = upNLines (bodylen, lineInfo, lastLine, hideQuoted);
2224 mutt_error _("Bottom of message is shown.");
2228 clearok (stdscr, TRUE);
2229 redraw = REDRAW_FULL;
2233 km_error_key (MENU_PAGER);
2236 /* --------------------------------------------------------------------
2237 * The following are operations on the current message rather than
2238 * adjusting the view of the message.
2241 case OP_BOUNCE_MESSAGE:
2242 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2244 if (IsMsgAttach (extra))
2245 mutt_attach_bounce (extra->fp, extra->hdr,
2246 extra->idx, extra->idxlen,
2249 ci_bounce_message (extra->hdr, &redraw);
2253 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra))
2255 if (IsMsgAttach (extra))
2256 mutt_attach_resend (extra->fp, extra->hdr,
2257 extra->idx, extra->idxlen,
2260 mutt_resend_message (NULL, extra->ctx, extra->hdr);
2261 redraw = REDRAW_FULL;
2264 case OP_CREATE_ALIAS:
2265 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2266 if (IsMsgAttach (extra))
2267 mutt_create_alias (extra->bdy->hdr->env, NULL);
2269 mutt_create_alias (extra->hdr->env, NULL);
2270 MAYBE_REDRAW (redraw);
2273 case OP_PURGE_MESSAGE:
2275 CHECK_MODE(IsHeader (extra));
2279 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2282 mutt_set_flag (Context, extra->hdr, M_DELETE, 1);
2283 mutt_set_flag (Context, extra->hdr, M_PURGED,
2284 ch != OP_PURGE_MESSAGE ? 0 : 1);
2285 if (option (OPTDELETEUNTAG))
2286 mutt_set_flag (Context, extra->hdr, M_TAG, 0);
2287 redraw = REDRAW_STATUS | REDRAW_INDEX;
2288 if (option (OPTRESOLVE))
2291 rc = OP_MAIN_NEXT_UNDELETED;
2295 case OP_DELETE_THREAD:
2296 case OP_DELETE_SUBTHREAD:
2297 CHECK_MODE(IsHeader (extra));
2301 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2304 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 1,
2305 ch == OP_DELETE_THREAD ? 0 : 1);
2309 if (option (OPTDELETEUNTAG))
2310 mutt_thread_set_flag (extra->hdr, M_TAG, 0,
2311 ch == OP_DELETE_THREAD ? 0 : 1);
2312 if (option (OPTRESOLVE))
2314 rc = OP_MAIN_NEXT_UNDELETED;
2318 if (!option (OPTRESOLVE) && PagerIndexLines)
2319 redraw = REDRAW_FULL;
2321 redraw = REDRAW_STATUS | REDRAW_INDEX;
2325 case OP_DISPLAY_ADDRESS:
2326 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2327 if (IsMsgAttach (extra))
2328 mutt_display_address (extra->bdy->hdr->env);
2330 mutt_display_address (extra->hdr->env);
2333 case OP_ENTER_COMMAND:
2334 old_smart_wrap = option (OPTWRAP);
2335 old_markers = option (OPTMARKERS);
2336 old_PagerIndexLines = PagerIndexLines;
2338 CurrentMenu = MENU_PAGER;
2339 mutt_enter_command ();
2341 if (option (OPTNEEDRESORT))
2343 unset_option (OPTNEEDRESORT);
2344 CHECK_MODE(IsHeader (extra));
2345 set_option (OPTNEEDRESORT);
2348 if (old_PagerIndexLines != PagerIndexLines)
2351 mutt_menuDestroy (&index);
2355 if (option (OPTWRAP) != old_smart_wrap ||
2356 option (OPTMARKERS) != old_markers)
2358 if (flags & M_PAGER_RETWINCH)
2361 rc = OP_REFORMAT_WINCH;
2365 /* count the real lines above */
2367 for (i = 0; i <= topline; i++)
2369 if (!lineInfo[i].continuation)
2373 /* we need to restart the whole thing */
2374 for (i = 0; i < maxLine; i++)
2376 lineInfo[i].offset = 0;
2377 lineInfo[i].type = -1;
2378 lineInfo[i].continuation = 0;
2379 lineInfo[i].chunks = 0;
2380 lineInfo[i].search_cnt = -1;
2381 lineInfo[i].quote = NULL;
2383 safe_realloc (&(lineInfo[i].syntax), sizeof (struct syntax_t));
2384 if (SearchCompiled && lineInfo[i].search)
2385 FREE (&(lineInfo[i].search));
2390 regfree (&SearchRE);
2395 /* try to keep the old position */
2398 while (j > 0 && display_line (fp, &last_pos, &lineInfo, topline,
2399 &lastLine, &maxLine,
2400 (has_types ? M_TYPES : 0),
2401 &QuoteList, &q_level, &force_redraw,
2404 if (! lineInfo[topline].continuation)
2413 if (option (OPTFORCEREDRAWPAGER))
2414 redraw = REDRAW_FULL;
2415 unset_option (OPTFORCEREDRAWINDEX);
2416 unset_option (OPTFORCEREDRAWPAGER);
2419 case OP_FLAG_MESSAGE:
2420 CHECK_MODE(IsHeader (extra));
2424 if (Context->magic == M_POP)
2427 mutt_error _("Can't change 'important' flag on POP server.");
2433 CHECK_IMAP_ACL(IMAP_ACL_WRITE);
2437 if (Context->magic == M_NNTP)
2440 mutt_error _("Can't change 'important' flag on NNTP server.");
2445 mutt_set_flag (Context, extra->hdr, M_FLAG, !extra->hdr->flagged);
2446 redraw = REDRAW_STATUS | REDRAW_INDEX;
2447 if (option (OPTRESOLVE))
2450 rc = OP_MAIN_NEXT_UNDELETED;
2455 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2456 if (IsAttach (extra))
2457 mutt_pipe_attachment_list (extra->fp, 0, extra->bdy, 0);
2459 mutt_pipe_message (extra->hdr);
2460 MAYBE_REDRAW (redraw);
2464 CHECK_MODE(IsHeader (extra) || IsAttach (extra));
2465 if (IsAttach (extra))
2466 mutt_print_attachment_list (extra->fp, 0, extra->bdy);
2468 mutt_print_message (extra->hdr);
2472 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2475 ci_send_message (0, NULL, NULL, extra->ctx, extra->hdr);
2477 redraw = REDRAW_FULL;
2482 CHECK_MODE(IsHeader (extra) && !IsAttach (extra));
2485 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2486 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2487 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2489 ci_send_message (SENDNEWS, NULL, NULL, extra->ctx, NULL);
2491 redraw = REDRAW_FULL;
2494 case OP_FORWARD_TO_GROUP:
2495 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2498 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2499 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2500 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2502 if (IsMsgAttach (extra))
2503 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2504 extra->idxlen, extra->bdy, SENDNEWS);
2506 ci_send_message (SENDNEWS|SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2508 redraw = REDRAW_FULL;
2512 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2516 if (IsMsgAttach (extra))
2517 followup_to = extra->bdy->hdr->env->followup_to;
2519 followup_to = extra->hdr->env->followup_to;
2521 if (!followup_to || mutt_strcasecmp (followup_to, "poster") ||
2522 query_quadoption (OPT_FOLLOWUPTOPOSTER,_("Reply by mail as poster prefers?")) != M_YES)
2524 if (extra->ctx && extra->ctx->magic == M_NNTP &&
2525 !((NNTP_DATA *)extra->ctx->data)->allowed &&
2526 query_quadoption (OPT_TOMODERATED,_("Posting to this group not allowed, may be moderated. Continue?")) != M_YES)
2528 if (IsMsgAttach (extra))
2529 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2530 extra->idxlen, extra->bdy, SENDNEWS|SENDREPLY);
2532 ci_send_message (SENDNEWS|SENDREPLY, NULL, NULL,
2533 extra->ctx, extra->hdr);
2535 redraw = REDRAW_FULL;
2541 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2544 if (IsMsgAttach (extra))
2545 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2546 extra->idxlen, extra->bdy,
2549 ci_send_message (SENDREPLY, NULL, NULL, extra->ctx, extra->hdr);
2551 redraw = REDRAW_FULL;
2554 case OP_RECALL_MESSAGE:
2555 CHECK_MODE(IsHeader (extra));
2558 ci_send_message (SENDPOSTPONED, NULL, NULL, extra->ctx, extra->hdr);
2560 redraw = REDRAW_FULL;
2563 case OP_GROUP_REPLY:
2564 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2567 if (IsMsgAttach (extra))
2568 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2569 extra->idxlen, extra->bdy, SENDREPLY|SENDGROUPREPLY);
2571 ci_send_message (SENDREPLY | SENDGROUPREPLY, NULL, NULL, extra->ctx, extra->hdr);
2573 redraw = REDRAW_FULL;
2577 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2580 if (IsMsgAttach (extra))
2581 mutt_attach_reply (extra->fp, extra->hdr, extra->idx,
2582 extra->idxlen, extra->bdy, SENDREPLY|SENDLISTREPLY);
2584 ci_send_message (SENDREPLY | SENDLISTREPLY, NULL, NULL, extra->ctx, extra->hdr);
2586 redraw = REDRAW_FULL;
2589 case OP_FORWARD_MESSAGE:
2590 CHECK_MODE(IsHeader (extra) || IsMsgAttach (extra));
2593 if (IsMsgAttach (extra))
2594 mutt_attach_forward (extra->fp, extra->hdr, extra->idx,
2595 extra->idxlen, extra->bdy, 0);
2597 ci_send_message (SENDFORWARD, NULL, NULL, extra->ctx, extra->hdr);
2599 redraw = REDRAW_FULL;
2602 case OP_DECRYPT_SAVE:
2610 if (IsAttach (extra))
2612 mutt_save_attachment_list (extra->fp, 0, extra->bdy, extra->hdr, NULL);
2616 case OP_COPY_MESSAGE:
2617 case OP_DECODE_SAVE:
2618 case OP_DECODE_COPY:
2619 case OP_DECRYPT_COPY:
2620 if (!WithCrypto && ch == OP_DECRYPT_COPY)
2625 CHECK_MODE(IsHeader (extra));
2626 if (mutt_save_message (extra->hdr,
2627 (ch == OP_DECRYPT_SAVE) ||
2628 (ch == OP_SAVE) || (ch == OP_DECODE_SAVE),
2629 (ch == OP_DECODE_SAVE) || (ch == OP_DECODE_COPY),
2630 (ch == OP_DECRYPT_SAVE) || (ch == OP_DECRYPT_COPY) ||
2632 &redraw) == 0 && (ch == OP_SAVE || ch == OP_DECODE_SAVE
2633 || ch == OP_DECRYPT_SAVE
2636 if (option (OPTRESOLVE))
2639 rc = OP_MAIN_NEXT_UNDELETED;
2642 redraw |= REDRAW_STATUS | REDRAW_INDEX;
2644 MAYBE_REDRAW (redraw);
2647 case OP_SHELL_ESCAPE:
2648 mutt_clear_xface ();
2649 mutt_shell_escape ();
2651 MAYBE_REDRAW (redraw);
2655 CHECK_MODE(IsHeader (extra));
2656 mutt_set_flag (Context, extra->hdr, M_TAG, !extra->hdr->tagged);
2657 redraw = REDRAW_STATUS | REDRAW_INDEX;
2658 if (option (OPTRESOLVE))
2666 CHECK_MODE(IsHeader (extra));
2670 CHECK_IMAP_ACL(IMAP_ACL_SEEN);
2673 if (extra->hdr->read || extra->hdr->old)
2674 mutt_set_flag (Context, extra->hdr, M_NEW, 1);
2676 mutt_set_flag (Context, extra->hdr, M_READ, 1);
2678 Context->msgnotreadyet = -1;
2679 redraw = REDRAW_STATUS | REDRAW_INDEX;
2680 if (option (OPTRESOLVE))
2683 rc = OP_MAIN_NEXT_UNDELETED;
2688 CHECK_MODE(IsHeader (extra));
2692 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2695 mutt_set_flag (Context, extra->hdr, M_DELETE, 0);
2696 mutt_set_flag (Context, extra->hdr, M_PURGED, 0);
2697 redraw = REDRAW_STATUS | REDRAW_INDEX;
2698 if (option (OPTRESOLVE))
2705 case OP_UNDELETE_THREAD:
2706 case OP_UNDELETE_SUBTHREAD:
2707 CHECK_MODE(IsHeader (extra));
2711 CHECK_IMAP_ACL(IMAP_ACL_DELETE);
2714 r = mutt_thread_set_flag (extra->hdr, M_DELETE, 0,
2715 ch == OP_UNDELETE_THREAD ? 0 : 1)
2716 + mutt_thread_set_flag (extra->hdr, M_PURGED, 0,
2717 ch == OP_UNDELETE_THREAD ? 0 : 1);
2721 if (option (OPTRESOLVE))
2723 rc = (ch == OP_DELETE_THREAD) ?
2724 OP_MAIN_NEXT_THREAD : OP_MAIN_NEXT_SUBTHREAD;
2728 if (!option (OPTRESOLVE) && PagerIndexLines)
2729 redraw = REDRAW_FULL;
2731 redraw = REDRAW_STATUS | REDRAW_INDEX;
2743 case OP_VIEW_ATTACHMENTS:
2744 if (flags & M_PAGER_ATTACHMENT)
2747 rc = OP_ATTACH_COLLAPSE;
2750 CHECK_MODE(IsHeader (extra));
2751 mutt_view_attachments (extra->hdr);
2752 if (extra->hdr->attach_del)
2753 Context->changed = 1;
2754 redraw = REDRAW_FULL;
2759 if (!(WithCrypto & APPLICATION_PGP))
2764 CHECK_MODE(IsHeader(extra));
2766 ci_send_message (SENDKEY, NULL, NULL, extra->ctx, extra->hdr);
2767 redraw = REDRAW_FULL;
2771 case OP_FORGET_PASSPHRASE:
2772 crypt_forget_passphrase ();
2775 case OP_EXTRACT_KEYS:
2781 CHECK_MODE(IsHeader(extra));
2782 crypt_extract_keys_from_messages(extra->hdr);
2783 redraw = REDRAW_FULL;
2786 case OP_SIDEBAR_SCROLL_UP:
2787 case OP_SIDEBAR_SCROLL_DOWN:
2788 case OP_SIDEBAR_NEXT:
2789 case OP_SIDEBAR_PREV:
2790 scroll_sidebar(ch, MENU_PAGER);
2799 if (IsHeader (extra))
2800 Context->msgnotreadyet = -1;
2802 cleanup_quote (&QuoteList);
2804 for (i = 0; i < maxLine ; i++)
2806 FREE (&(lineInfo[i].syntax));
2807 if (SearchCompiled && lineInfo[i].search)
2808 FREE (&(lineInfo[i].search));
2812 regfree (&SearchRE);
2817 mutt_menuDestroy(&index);
2818 mutt_clear_xface ();
2819 return (rc != -1 ? rc : 0);