* Fix an error in compilation when Alpine is not built with S/MIME
[alpine.git] / pith / editorial.c
blob80cb8c0e71d627d079b8edb4e53b7131b3611da0
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: editorial.c 768 2007-10-24 00:10:03Z hubert@u.washington.edu $";
3 #endif
5 /* ========================================================================
6 * Copyright 2006-2007 University of Washington
7 * Copyright 2013-2016 Eduardo Chappa
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * ========================================================================
18 /*======================================================================
20 editorial.c
21 Implements editorial text insertion/formatting
23 ====*/
26 #include "../pith/headers.h"
27 #include "../pith/conf.h"
28 #include "../pith/state.h"
29 #include "../pith/margin.h"
30 #include "../pith/filter.h"
31 #include "../pith/handle.h"
32 #include "../pith/mailview.h"
33 #include "../pith/editorial.h"
36 * Internal prototypes
38 int quote_editorial(long, char *, LT_INS_S **, void *);
42 * Struct to help with editorial comment insertion
44 #define EDITORIAL_MAX 128
45 typedef struct _editorial_s {
46 char prefix[EDITORIAL_MAX];
47 int prelen;
48 char postfix[EDITORIAL_MAX];
49 int postlen;
50 int do_color;
51 } EDITORIAL_S;
54 char *
55 format_editorial(char *s, int width, int flags, HANDLE_S **handlesp, gf_io_t pc)
57 gf_io_t gc;
58 int *margin;
59 EDITORIAL_S es;
60 URL_HILITE_S uh;
62 /* ASSUMPTION #2,341: All MIME-decoding is done by now */
63 gf_set_readc(&gc, s, strlen(s), CharStar, 0);
65 margin = format_view_margin();
66 if(flags & FM_NOINDENT)
67 margin[0] = margin[1] = 0;
69 /* safety net */
70 if(width - (margin[0] + margin[1]) < 5){
71 margin[0] = margin[1] = 0;
72 if(width < 5)
73 width = 80;
76 width -= (margin[0] + margin[1]);
78 if(width > 40){
79 width -= 12;
81 es.prelen = MAX(2, MIN(margin[0] + 6, sizeof(es.prefix) - 3));
82 snprintf(es.prefix, sizeof(es.prefix), "%s[ ", repeat_char(es.prelen - 2, ' '));
83 es.postlen = 2;
84 strncpy(es.postfix, " ]", sizeof(es.postfix));
85 es.postfix[sizeof(es.postfix)-1] = '\0';
87 else if(width > 20){
88 width -= 6;
90 es.prelen = MAX(2, MIN(margin[0] + 3, sizeof(es.prefix) - 3));
91 snprintf(es.prefix, sizeof(es.prefix), "%s[ ", repeat_char(es.prelen - 2, ' '));
92 es.postlen = 2;
93 strncpy(es.postfix, " ]", sizeof(es.postfix));
94 es.postfix[sizeof(es.postfix)-1] = '\0';
96 else{
97 width -= 2;
98 strncpy(es.prefix, "[", sizeof(es.prefix));
99 es.prefix[sizeof(es.prefix)-1] = '\0';
100 strncpy(es.postfix, "]", sizeof(es.postfix));
101 es.postfix[sizeof(es.postfix)-1] = '\0';
102 es.prelen = 1;
103 es.postlen = 1;
106 es.do_color = (!(flags & FM_NOCOLOR) && (flags & FM_DISPLAY) && pico_usingcolor());
108 gf_filter_init();
110 /* catch urls */
111 if((F_ON(F_VIEW_SEL_URL, ps_global)
112 || F_ON(F_VIEW_SEL_URL_HOST, ps_global)
113 || F_ON(F_SCAN_ADDR, ps_global))
114 && handlesp){
115 gf_link_filter(gf_line_test,
116 gf_line_test_opt(url_hilite,
117 gf_url_hilite_opt(&uh,handlesp,0)));
120 gf_link_filter(gf_wrap, gf_wrap_filter_opt(width, width, NULL, 0,
121 (handlesp ? GFW_HANDLES : GFW_NONE)));
122 gf_link_filter(gf_line_test, gf_line_test_opt(quote_editorial, &es));
124 /* If not for display, change to local end of line */
125 if(!(flags & FM_DISPLAY))
126 gf_link_filter(gf_nvtnl_local, NULL);
128 return(gf_pipe(gc, pc));
133 quote_editorial(long int linenum, char *line, LT_INS_S **ins, void *local)
135 COLOR_PAIR *col = NULL;
137 ins = gf_line_test_new_ins(ins, line,
138 ((EDITORIAL_S *)local)->prefix,
139 ((EDITORIAL_S *)local)->prelen);
140 if(((EDITORIAL_S *)local)->do_color
141 && ps_global->VAR_METAMSG_FORE_COLOR
142 && ps_global->VAR_METAMSG_BACK_COLOR
143 && (col = new_color_pair(ps_global->VAR_METAMSG_FORE_COLOR,
144 ps_global->VAR_METAMSG_BACK_COLOR))){
145 if(!pico_is_good_colorpair(col))
146 free_color_pair(&col);
148 if(col){
149 char *p;
150 char normal_embed[(2 * RGBLEN) + 5];
151 char quote_color_embed[(2 * RGBLEN) + 5];
153 strncpy(quote_color_embed,
154 color_embed(col->fg, col->bg),
155 sizeof(quote_color_embed));
156 quote_color_embed[sizeof(quote_color_embed)-1] = '\0';
158 ins = gf_line_test_new_ins(ins, line,
159 quote_color_embed, (2 * RGBLEN) + 4);
162 * If there was already a color change back to normal color
163 * in the line that was passed in, then instead of allowing
164 * that color change back to normal we want to change that
165 * to a color change back to our METAMSG color instead.
166 * Search line for that and modify it.
169 strncpy(normal_embed,
170 color_embed(ps_global->VAR_NORM_FORE_COLOR,
171 ps_global->VAR_NORM_BACK_COLOR),
172 sizeof(normal_embed));
173 normal_embed[sizeof(normal_embed)-1] = '\0';
175 for(p = line; (p = strstr(p, normal_embed)); p++){
178 * Replace the normal color with our special quoting
179 * color. No need to change it if there are no
180 * characters after the color change because we're
181 * going to change the color to normal right below
182 * this anyway.
184 if(strlen(p) > strlen(quote_color_embed))
185 rplstr(p, strlen(p)+1, strlen(quote_color_embed), quote_color_embed);
188 ins = gf_line_test_new_ins(ins, line+strlen(line),
189 normal_embed, (2 * RGBLEN) + 4);
190 free_color_pair(&col);
194 ins = gf_line_test_new_ins(ins, line + strlen(line),
195 ((EDITORIAL_S *)local)->postfix,
196 ((EDITORIAL_S *)local)->postlen);
197 return(0);