1 | #include "owl.h" |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | static const char fileIdent[] = "$Id$"; |
---|
6 | |
---|
7 | /* initialize an fmtext with no data */ |
---|
8 | void owl_fmtext_init_null(owl_fmtext *f) |
---|
9 | { |
---|
10 | f->textlen = 0; |
---|
11 | f->bufflen = 5; |
---|
12 | f->textbuff = owl_malloc(5); |
---|
13 | f->textbuff[0] = 0; |
---|
14 | f->default_attrs = OWL_FMTEXT_ATTR_NONE; |
---|
15 | f->default_fgcolor = OWL_COLOR_DEFAULT; |
---|
16 | f->default_fgcolor = OWL_COLOR_DEFAULT; |
---|
17 | } |
---|
18 | |
---|
19 | /* Clear the data from an fmtext, but don't deallocate memory. This |
---|
20 | fmtext can then be appended to again. */ |
---|
21 | void owl_fmtext_clear(owl_fmtext *f) |
---|
22 | { |
---|
23 | f->textlen = 0; |
---|
24 | f->textbuff[0] = 0; |
---|
25 | f->default_attrs = OWL_FMTEXT_ATTR_NONE; |
---|
26 | f->default_fgcolor = OWL_COLOR_DEFAULT; |
---|
27 | f->default_fgcolor = OWL_COLOR_DEFAULT; |
---|
28 | } |
---|
29 | |
---|
30 | void _owl_fmtext_realloc(owl_fmtext *f, int newlen) /*noproto*/ |
---|
31 | { |
---|
32 | if(newlen + 1 > f->bufflen) { |
---|
33 | f->textbuff = owl_realloc(f->textbuff, newlen + 1); |
---|
34 | f->bufflen = newlen+1; |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | int owl_fmtext_is_format_char(gunichar c) |
---|
39 | { |
---|
40 | if ((c & ~OWL_FMTEXT_UC_ATTR_MASK) == OWL_FMTEXT_UC_ATTR) return 1; |
---|
41 | if ((c & ~(OWL_FMTEXT_UC_ALLCOLOR_MASK)) == OWL_FMTEXT_UC_COLOR_BASE) return 1; |
---|
42 | return 0; |
---|
43 | } |
---|
44 | /* append text to the end of 'f' with attribute 'attr' and color |
---|
45 | * 'color' |
---|
46 | */ |
---|
47 | void owl_fmtext_append_attr(owl_fmtext *f, char *text, char attr, short fgcolor, short bgcolor) |
---|
48 | { |
---|
49 | char attrbuff[6]; |
---|
50 | int newlen, a = 0, fg = 0, bg = 0; |
---|
51 | |
---|
52 | if (attr != OWL_FMTEXT_ATTR_NONE) a=1; |
---|
53 | if (fgcolor != OWL_COLOR_DEFAULT) fg=1; |
---|
54 | if (bgcolor != OWL_COLOR_DEFAULT) bg=1; |
---|
55 | |
---|
56 | /* Plane-16 characters in UTF-8 are 4 bytes long. */ |
---|
57 | newlen = strlen(f->textbuff) + strlen(text) + (8 * (a + fg + bg)); |
---|
58 | _owl_fmtext_realloc(f, newlen); |
---|
59 | |
---|
60 | /* Set attributes */ |
---|
61 | if (a) { |
---|
62 | memset(attrbuff,0,6); |
---|
63 | g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff); |
---|
64 | strcat(f->textbuff, attrbuff); |
---|
65 | } |
---|
66 | if (fg) { |
---|
67 | memset(attrbuff,0,6); |
---|
68 | g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff); |
---|
69 | strcat(f->textbuff, attrbuff); |
---|
70 | } |
---|
71 | if (bg) { |
---|
72 | memset(attrbuff,0,6); |
---|
73 | g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff); |
---|
74 | strcat(f->textbuff, attrbuff); |
---|
75 | } |
---|
76 | |
---|
77 | strcat(f->textbuff, text); |
---|
78 | |
---|
79 | /* Reset attributes */ |
---|
80 | if (bg) strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT); |
---|
81 | if (fg) strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT); |
---|
82 | if (a) strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE); |
---|
83 | f->textlen=newlen; |
---|
84 | } |
---|
85 | |
---|
86 | /* Append normal, uncolored text 'text' to 'f' */ |
---|
87 | void owl_fmtext_append_normal(owl_fmtext *f, char *text) |
---|
88 | { |
---|
89 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
90 | } |
---|
91 | |
---|
92 | /* Append normal text 'text' to 'f' with color 'color' */ |
---|
93 | void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int fgcolor, int bgcolor) |
---|
94 | { |
---|
95 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor); |
---|
96 | } |
---|
97 | |
---|
98 | /* Append bold text 'text' to 'f' */ |
---|
99 | void owl_fmtext_append_bold(owl_fmtext *f, char *text) |
---|
100 | { |
---|
101 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
102 | } |
---|
103 | |
---|
104 | /* Append reverse video text 'text' to 'f' */ |
---|
105 | void owl_fmtext_append_reverse(owl_fmtext *f, char *text) |
---|
106 | { |
---|
107 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
108 | } |
---|
109 | |
---|
110 | /* Append reversed and bold, uncolored text 'text' to 'f' */ |
---|
111 | void owl_fmtext_append_reversebold(owl_fmtext *f, char *text) |
---|
112 | { |
---|
113 | owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT); |
---|
114 | } |
---|
115 | |
---|
116 | /* Add the attribute 'attr' to the default atts for the text in 'f' */ |
---|
117 | void owl_fmtext_addattr(owl_fmtext *f, char attr) |
---|
118 | { |
---|
119 | /* add the attribute to all text */ |
---|
120 | f->default_attrs |= attr; |
---|
121 | } |
---|
122 | |
---|
123 | /* Set the default foreground color for this fmtext to 'color'. |
---|
124 | * Only affects text that is colored default. |
---|
125 | */ |
---|
126 | void owl_fmtext_colorize(owl_fmtext *f, int color) |
---|
127 | { |
---|
128 | f->default_fgcolor = color; |
---|
129 | } |
---|
130 | |
---|
131 | /* Set the default foreground color for this fmtext to 'color'. |
---|
132 | * Only affects text that is colored default. |
---|
133 | */ |
---|
134 | void owl_fmtext_colorizebg(owl_fmtext *f, int color) |
---|
135 | { |
---|
136 | f->default_bgcolor = color; |
---|
137 | } |
---|
138 | |
---|
139 | /* Internal function. Parse attrbute character. */ |
---|
140 | void _owl_fmtext_update_attributes(gunichar c, char *attr, short *fgcolor, short *bgcolor) /*noproto*/ |
---|
141 | { |
---|
142 | if ((c & OWL_FMTEXT_UC_ATTR) == OWL_FMTEXT_UC_ATTR) { |
---|
143 | *attr = c & OWL_FMTEXT_UC_ATTR_MASK; |
---|
144 | } |
---|
145 | else if ((c & OWL_FMTEXT_UC_FGCOLOR) == OWL_FMTEXT_UC_FGCOLOR) { |
---|
146 | *fgcolor = (c == OWL_FMTEXT_UC_FGDEFAULT |
---|
147 | ? OWL_COLOR_DEFAULT |
---|
148 | : c & OWL_FMTEXT_UC_COLOR_MASK); |
---|
149 | } |
---|
150 | else if ((c & OWL_FMTEXT_UC_BGCOLOR) == OWL_FMTEXT_UC_BGCOLOR) { |
---|
151 | *bgcolor = (c == OWL_FMTEXT_UC_BGDEFAULT |
---|
152 | ? OWL_COLOR_DEFAULT |
---|
153 | : c & OWL_FMTEXT_UC_COLOR_MASK); |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | /* Internal function. Scan for attribute characters. */ |
---|
158 | void _owl_fmtext_scan_attributes(owl_fmtext *f, int start, char *attr, short *fgcolor, short *bgcolor) /*noproto*/ |
---|
159 | { |
---|
160 | char *p; |
---|
161 | p = strchr(f->textbuff, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
162 | while (p && p < f->textbuff + start) { |
---|
163 | _owl_fmtext_update_attributes(g_utf8_get_char(p), attr, fgcolor, bgcolor); |
---|
164 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | /* Internal function. Append text from 'in' between index 'start' and |
---|
169 | * 'stop' to the end of 'f' |
---|
170 | */ |
---|
171 | void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop) /*noproto*/ |
---|
172 | { |
---|
173 | char attrbuff[6]; |
---|
174 | int newlen, a = 0, fg = 0, bg = 0; |
---|
175 | char attr = 0; |
---|
176 | short fgcolor = OWL_COLOR_DEFAULT; |
---|
177 | short bgcolor = OWL_COLOR_DEFAULT; |
---|
178 | |
---|
179 | _owl_fmtext_scan_attributes(in, start, &attr, &fgcolor, &bgcolor); |
---|
180 | if (attr != OWL_FMTEXT_ATTR_NONE) a=1; |
---|
181 | if (fgcolor != OWL_COLOR_DEFAULT) fg=1; |
---|
182 | if (bgcolor != OWL_COLOR_DEFAULT) bg=1; |
---|
183 | |
---|
184 | /* We will reset to defaults after appending the text. We may need |
---|
185 | to set initial attributes. */ |
---|
186 | newlen=strlen(f->textbuff)+(stop-start+1) + (4 * (a + fg + bg)) + 12; |
---|
187 | _owl_fmtext_realloc(f, newlen); |
---|
188 | |
---|
189 | if (a) { |
---|
190 | memset(attrbuff,0,6); |
---|
191 | g_unichar_to_utf8(OWL_FMTEXT_UC_ATTR | attr, attrbuff); |
---|
192 | strcat(f->textbuff, attrbuff); |
---|
193 | } |
---|
194 | if (fg) { |
---|
195 | memset(attrbuff,0,6); |
---|
196 | g_unichar_to_utf8(OWL_FMTEXT_UC_FGCOLOR | fgcolor, attrbuff); |
---|
197 | strcat(f->textbuff, attrbuff); |
---|
198 | } |
---|
199 | if (bg) { |
---|
200 | memset(attrbuff,0,6); |
---|
201 | g_unichar_to_utf8(OWL_FMTEXT_UC_BGCOLOR | bgcolor, attrbuff); |
---|
202 | strcat(f->textbuff, attrbuff); |
---|
203 | } |
---|
204 | |
---|
205 | strncat(f->textbuff, in->textbuff+start, stop-start+1); |
---|
206 | |
---|
207 | /* Reset attributes */ |
---|
208 | strcat(f->textbuff, OWL_FMTEXT_UTF8_BGDEFAULT); |
---|
209 | strcat(f->textbuff, OWL_FMTEXT_UTF8_FGDEFAULT); |
---|
210 | strcat(f->textbuff, OWL_FMTEXT_UTF8_ATTR_NONE); |
---|
211 | |
---|
212 | f->textbuff[newlen]='\0'; |
---|
213 | f->textlen=newlen; |
---|
214 | } |
---|
215 | |
---|
216 | /* append fmtext 'in' to 'f' */ |
---|
217 | void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in) |
---|
218 | { |
---|
219 | _owl_fmtext_append_fmtext(f, in, 0, in->textlen); |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | /* Append 'nspaces' number of spaces to the end of 'f' */ |
---|
224 | void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces) |
---|
225 | { |
---|
226 | int i; |
---|
227 | for (i=0; i<nspaces; i++) { |
---|
228 | owl_fmtext_append_normal(f, " "); |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | /* Return a plain version of the fmtext. Caller is responsible for |
---|
233 | * freeing the return |
---|
234 | */ |
---|
235 | char *owl_fmtext_print_plain(owl_fmtext *f) |
---|
236 | { |
---|
237 | return owl_strip_format_chars(f->textbuff); |
---|
238 | } |
---|
239 | |
---|
240 | void _owl_fmtext_wattrset(WINDOW *w, int attrs) /*noproto*/ |
---|
241 | { |
---|
242 | wattrset(w, A_NORMAL); |
---|
243 | if (attrs & OWL_FMTEXT_ATTR_BOLD) wattron(w, A_BOLD); |
---|
244 | if (attrs & OWL_FMTEXT_ATTR_REVERSE) wattron(w, A_REVERSE); |
---|
245 | if (attrs & OWL_FMTEXT_ATTR_UNDERLINE) wattron(w, A_UNDERLINE); |
---|
246 | } |
---|
247 | |
---|
248 | void _owl_fmtext_update_colorpair(short fg, short bg, short *pair) /*noproto*/ |
---|
249 | { |
---|
250 | if (owl_global_get_hascolors(&g)) { |
---|
251 | *pair = owl_fmtext_get_colorpair(fg, bg); |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | void _owl_fmtext_wcolor_set(WINDOW *w, short pair) /*noproto*/ |
---|
256 | { |
---|
257 | if (owl_global_get_hascolors(&g)) { |
---|
258 | wcolor_set(w,pair,NULL); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | /* add the formatted text to the curses window 'w'. The window 'w' |
---|
263 | * must already be initiatlized with curses |
---|
264 | */ |
---|
265 | void _owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w, int do_search) /*noproto*/ |
---|
266 | { |
---|
267 | /* char *tmpbuff; */ |
---|
268 | /* int position, trans1, trans2, trans3, len, lastsame; */ |
---|
269 | char *s, *p; |
---|
270 | char attr; |
---|
271 | short fg, bg, pair; |
---|
272 | int search_results, search_len; |
---|
273 | |
---|
274 | if (w==NULL) { |
---|
275 | owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr."); |
---|
276 | return; |
---|
277 | } |
---|
278 | |
---|
279 | search_results = (do_search |
---|
280 | ? owl_fmtext_search(f, owl_global_get_search_string(&g)) |
---|
281 | : 0); |
---|
282 | search_len = (search_results |
---|
283 | ? strlen(owl_global_get_search_string(&g)) |
---|
284 | : 0); |
---|
285 | s = f->textbuff; |
---|
286 | /* Set default attributes. */ |
---|
287 | attr = f->default_attrs; |
---|
288 | fg = f->default_fgcolor; |
---|
289 | bg = f->default_bgcolor; |
---|
290 | _owl_fmtext_wattrset(w, attr); |
---|
291 | _owl_fmtext_update_colorpair(fg, bg, &pair); |
---|
292 | _owl_fmtext_wcolor_set(w, pair); |
---|
293 | |
---|
294 | /* Find next possible format character. */ |
---|
295 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
296 | while(p) { |
---|
297 | if (owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
298 | /* Deal with all text from last insert to here. */ |
---|
299 | char tmp; |
---|
300 | |
---|
301 | tmp = p[0]; |
---|
302 | p[0] = '\0'; |
---|
303 | if (search_results) { |
---|
304 | /* Search is active, so highlight search results. */ |
---|
305 | char tmp2, *ss; |
---|
306 | ss = stristr(s, owl_global_get_search_string(&g)); |
---|
307 | while (ss) { |
---|
308 | /* Found search string, highlight it. */ |
---|
309 | |
---|
310 | tmp2 = ss[0]; |
---|
311 | ss[0] = '\0'; |
---|
312 | waddstr(w, s); |
---|
313 | ss[0] = tmp2; |
---|
314 | |
---|
315 | _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE); |
---|
316 | _owl_fmtext_wcolor_set(w, pair); |
---|
317 | |
---|
318 | tmp2 = ss[search_len]; |
---|
319 | ss[search_len] = '\0'; |
---|
320 | waddstr(w, ss); |
---|
321 | ss[search_len] = tmp2; |
---|
322 | |
---|
323 | _owl_fmtext_wattrset(w, attr); |
---|
324 | _owl_fmtext_wcolor_set(w, pair); |
---|
325 | |
---|
326 | s = ss + search_len; |
---|
327 | ss = stristr(s, owl_global_get_search_string(&g)); |
---|
328 | } |
---|
329 | } |
---|
330 | /* Deal with remaining part of string. */ |
---|
331 | waddstr(w, s); |
---|
332 | p[0] = tmp; |
---|
333 | |
---|
334 | /* Deal with new attributes. Initialize to defaults, then |
---|
335 | process all consecutive formatting characters. */ |
---|
336 | attr = f->default_attrs; |
---|
337 | fg = f->default_fgcolor; |
---|
338 | bg = f->default_bgcolor; |
---|
339 | while (p && owl_fmtext_is_format_char(g_utf8_get_char(p))) { |
---|
340 | _owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg); |
---|
341 | p = g_utf8_next_char(p); |
---|
342 | } |
---|
343 | _owl_fmtext_wattrset(w, attr | f->default_attrs); |
---|
344 | if (fg == OWL_COLOR_DEFAULT) fg = f->default_fgcolor; |
---|
345 | if (bg == OWL_COLOR_DEFAULT) bg = f->default_bgcolor; |
---|
346 | _owl_fmtext_update_colorpair(fg, bg, &pair); |
---|
347 | _owl_fmtext_wcolor_set(w, pair); |
---|
348 | |
---|
349 | /* Advance to next non-formatting character. */ |
---|
350 | s = p; |
---|
351 | p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
352 | } |
---|
353 | else { |
---|
354 | p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8); |
---|
355 | } |
---|
356 | } |
---|
357 | if (s) { |
---|
358 | waddstr(w, s); |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w) |
---|
363 | { |
---|
364 | _owl_fmtext_curs_waddstr(f, w, owl_global_is_search_active(&g)); |
---|
365 | } |
---|
366 | |
---|
367 | void owl_fmtext_curs_waddstr_without_search(owl_fmtext *f, WINDOW *w) |
---|
368 | { |
---|
369 | _owl_fmtext_curs_waddstr(f, w, 0); |
---|
370 | } |
---|
371 | |
---|
372 | /* start with line 'aline' (where the first line is 0) and print |
---|
373 | * 'lines' number of lines into 'out' |
---|
374 | */ |
---|
375 | int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out) |
---|
376 | { |
---|
377 | char *ptr1, *ptr2; |
---|
378 | int i, offset; |
---|
379 | |
---|
380 | /* find the starting line */ |
---|
381 | ptr1 = in->textbuff; |
---|
382 | for (i = 0; i < aline; i++) { |
---|
383 | ptr1 = strchr(ptr1, '\n'); |
---|
384 | if (!ptr1) return(-1); |
---|
385 | ptr1++; |
---|
386 | } |
---|
387 | |
---|
388 | /* ptr1 now holds the starting point */ |
---|
389 | |
---|
390 | /* copy in the next 'lines' lines */ |
---|
391 | if (lines < 1) return(-1); |
---|
392 | |
---|
393 | for (i = 0; i < lines; i++) { |
---|
394 | offset = ptr1 - in->textbuff; |
---|
395 | ptr2 = strchr(ptr1, '\n'); |
---|
396 | if (!ptr2) { |
---|
397 | _owl_fmtext_append_fmtext(out, in, offset, (in->textlen) - 1); |
---|
398 | return(-1); |
---|
399 | } |
---|
400 | _owl_fmtext_append_fmtext(out, in, offset, (ptr2 - ptr1) + offset); |
---|
401 | ptr1 = ptr2 + 1; |
---|
402 | } |
---|
403 | return(0); |
---|
404 | } |
---|
405 | |
---|
406 | /* Truncate the message so that each line begins at column 'acol' and |
---|
407 | * ends at 'bcol' or sooner. The first column is number 0. The new |
---|
408 | * message is placed in 'out'. The message is * expected to end in a |
---|
409 | * new line for now. NOTE: This needs to be modified to deal with |
---|
410 | * backing up if we find a SPACING COMBINING MARK at the end of a |
---|
411 | * line. If that happens, we should back up to the last non-mark |
---|
412 | * character and stop there. |
---|
413 | */ |
---|
414 | void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out) |
---|
415 | { |
---|
416 | char *ptr_s, *ptr_e, *ptr_c, *last; |
---|
417 | int col, st, padding, chwidth; |
---|
418 | |
---|
419 | last=in->textbuff+in->textlen-1; |
---|
420 | ptr_s=in->textbuff; |
---|
421 | while (ptr_s <= last) { |
---|
422 | ptr_e=strchr(ptr_s, '\n'); |
---|
423 | if (!ptr_e) { |
---|
424 | /* but this shouldn't happen if we end in a \n */ |
---|
425 | break; |
---|
426 | } |
---|
427 | |
---|
428 | if (ptr_e == ptr_s) { |
---|
429 | owl_fmtext_append_normal(out, "\n"); |
---|
430 | ++ptr_s; |
---|
431 | continue; |
---|
432 | } |
---|
433 | |
---|
434 | col = 0; |
---|
435 | st = 0; |
---|
436 | padding = 0; |
---|
437 | chwidth = 0; |
---|
438 | ptr_c = ptr_s; |
---|
439 | while(ptr_c < ptr_e) { |
---|
440 | gunichar c = g_utf8_get_char(ptr_c); |
---|
441 | if (!owl_fmtext_is_format_char(c)) { |
---|
442 | chwidth = mk_wcwidth(c); |
---|
443 | if (col + chwidth > bcol) break; |
---|
444 | |
---|
445 | if (col >= acol) { |
---|
446 | if (st == 0) { |
---|
447 | ptr_s = ptr_c; |
---|
448 | padding = col - acol; |
---|
449 | ++st; |
---|
450 | } |
---|
451 | } |
---|
452 | col += chwidth; |
---|
453 | chwidth = 0; |
---|
454 | } |
---|
455 | ptr_c = g_utf8_next_char(ptr_c); |
---|
456 | } |
---|
457 | if (st) { |
---|
458 | /* lead padding */ |
---|
459 | owl_fmtext_append_spaces(out, padding); |
---|
460 | if (ptr_c == ptr_e) { |
---|
461 | /* We made it to the newline. */ |
---|
462 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff); |
---|
463 | } |
---|
464 | else { |
---|
465 | if (chwidth > 1) { |
---|
466 | /* Last char is wide, truncate. */ |
---|
467 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1); |
---|
468 | owl_fmtext_append_normal(out, "\n"); |
---|
469 | } |
---|
470 | else { |
---|
471 | /* Last char fits perfectly, leave alone.*/ |
---|
472 | _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff); |
---|
473 | } |
---|
474 | } |
---|
475 | } |
---|
476 | else { |
---|
477 | owl_fmtext_append_normal(out, "\n"); |
---|
478 | } |
---|
479 | ptr_s = g_utf8_next_char(ptr_e); |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | /* Return the number of lines in 'f' */ |
---|
484 | int owl_fmtext_num_lines(owl_fmtext *f) |
---|
485 | { |
---|
486 | int lines, i; |
---|
487 | |
---|
488 | if (f->textlen==0) return(0); |
---|
489 | |
---|
490 | lines=0; |
---|
491 | for (i=0; i<f->textlen; i++) { |
---|
492 | if (f->textbuff[i]=='\n') lines++; |
---|
493 | } |
---|
494 | |
---|
495 | /* if the last char wasn't a \n there's one more line */ |
---|
496 | if (f->textbuff[i-1]!='\n') lines++; |
---|
497 | |
---|
498 | return(lines); |
---|
499 | } |
---|
500 | |
---|
501 | char *owl_fmtext_get_text(owl_fmtext *f) |
---|
502 | { |
---|
503 | return(f->textbuff); |
---|
504 | } |
---|
505 | |
---|
506 | /* set the charater at 'index' to be 'char'. If index is out of |
---|
507 | * bounds don't do anything. If c or char at index is not ASCII, don't |
---|
508 | * do anything because it's not UTF-8 safe. */ |
---|
509 | void owl_fmtext_set_char(owl_fmtext *f, int index, char ch) |
---|
510 | { |
---|
511 | if ((index < 0) || (index > f->textlen-1)) return; |
---|
512 | /* NOT ASCII*/ |
---|
513 | if (f->textbuff[index] & 0x80 || ch & 0x80) return; |
---|
514 | f->textbuff[index]=ch; |
---|
515 | } |
---|
516 | |
---|
517 | /* Make a copy of the fmtext 'src' into 'dst' */ |
---|
518 | void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src) |
---|
519 | { |
---|
520 | int mallocsize; |
---|
521 | |
---|
522 | if (src->textlen==0) { |
---|
523 | mallocsize=5; |
---|
524 | } else { |
---|
525 | mallocsize=src->textlen+2; |
---|
526 | } |
---|
527 | dst->textlen=src->textlen; |
---|
528 | dst->textbuff=owl_malloc(mallocsize); |
---|
529 | memcpy(dst->textbuff, src->textbuff, src->textlen+1); |
---|
530 | dst->default_attrs = src->default_attrs; |
---|
531 | dst->default_fgcolor = src->default_fgcolor; |
---|
532 | dst->default_bgcolor = src->default_bgcolor; |
---|
533 | } |
---|
534 | |
---|
535 | /* return 1 if the string is found, 0 if not. This is a case |
---|
536 | * insensitive search. |
---|
537 | */ |
---|
538 | int owl_fmtext_search(owl_fmtext *f, char *string) |
---|
539 | { |
---|
540 | if (stristr(f->textbuff, string)) return(1); |
---|
541 | return(0); |
---|
542 | } |
---|
543 | |
---|
544 | |
---|
545 | /* Append the text 'text' to 'f' and interpret the zephyr style |
---|
546 | * formatting syntax to set appropriate attributes. |
---|
547 | */ |
---|
548 | void owl_fmtext_append_ztext(owl_fmtext *f, char *text) |
---|
549 | { |
---|
550 | int stacksize, curattrs, curcolor; |
---|
551 | char *ptr, *txtptr, *buff, *tmpptr; |
---|
552 | int attrstack[32], chrstack[32], colorstack[32]; |
---|
553 | |
---|
554 | curattrs=OWL_FMTEXT_ATTR_NONE; |
---|
555 | curcolor=OWL_COLOR_DEFAULT; |
---|
556 | stacksize=0; |
---|
557 | txtptr=text; |
---|
558 | while (1) { |
---|
559 | ptr=strpbrk(txtptr, "@{[<()>]}"); |
---|
560 | if (!ptr) { |
---|
561 | /* add all the rest of the text and exit */ |
---|
562 | owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
563 | return; |
---|
564 | } else if (ptr[0]=='@') { |
---|
565 | /* add the text up to this point then deal with the stack */ |
---|
566 | buff=owl_malloc(ptr-txtptr+20); |
---|
567 | strncpy(buff, txtptr, ptr-txtptr); |
---|
568 | buff[ptr-txtptr]='\0'; |
---|
569 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
570 | owl_free(buff); |
---|
571 | |
---|
572 | /* update pointer to point at the @ */ |
---|
573 | txtptr=ptr; |
---|
574 | |
---|
575 | /* now the stack */ |
---|
576 | |
---|
577 | /* if we've hit our max stack depth, print the @ and move on */ |
---|
578 | if (stacksize==32) { |
---|
579 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
580 | txtptr++; |
---|
581 | continue; |
---|
582 | } |
---|
583 | |
---|
584 | /* if it's an @@, print an @ and continue */ |
---|
585 | if (txtptr[1]=='@') { |
---|
586 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
587 | txtptr+=2; |
---|
588 | continue; |
---|
589 | } |
---|
590 | |
---|
591 | /* if there's no opener, print the @ and continue */ |
---|
592 | tmpptr=strpbrk(txtptr, "(<[{ "); |
---|
593 | if (!tmpptr || tmpptr[0]==' ') { |
---|
594 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
595 | txtptr++; |
---|
596 | continue; |
---|
597 | } |
---|
598 | |
---|
599 | /* check what command we've got, push it on the stack, start |
---|
600 | using it, and continue ... unless it's a color command */ |
---|
601 | buff=owl_malloc(tmpptr-ptr+20); |
---|
602 | strncpy(buff, ptr, tmpptr-ptr); |
---|
603 | buff[tmpptr-ptr]='\0'; |
---|
604 | if (!strcasecmp(buff, "@bold")) { |
---|
605 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
---|
606 | chrstack[stacksize]=tmpptr[0]; |
---|
607 | colorstack[stacksize]=curcolor; |
---|
608 | stacksize++; |
---|
609 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
---|
610 | txtptr+=6; |
---|
611 | owl_free(buff); |
---|
612 | continue; |
---|
613 | } else if (!strcasecmp(buff, "@b")) { |
---|
614 | attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD; |
---|
615 | chrstack[stacksize]=tmpptr[0]; |
---|
616 | colorstack[stacksize]=curcolor; |
---|
617 | stacksize++; |
---|
618 | curattrs|=OWL_FMTEXT_ATTR_BOLD; |
---|
619 | txtptr+=3; |
---|
620 | owl_free(buff); |
---|
621 | continue; |
---|
622 | } else if (!strcasecmp(buff, "@i")) { |
---|
623 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
624 | chrstack[stacksize]=tmpptr[0]; |
---|
625 | colorstack[stacksize]=curcolor; |
---|
626 | stacksize++; |
---|
627 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
628 | txtptr+=3; |
---|
629 | owl_free(buff); |
---|
630 | continue; |
---|
631 | } else if (!strcasecmp(buff, "@italic")) { |
---|
632 | attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
633 | chrstack[stacksize]=tmpptr[0]; |
---|
634 | colorstack[stacksize]=curcolor; |
---|
635 | stacksize++; |
---|
636 | curattrs|=OWL_FMTEXT_ATTR_UNDERLINE; |
---|
637 | txtptr+=8; |
---|
638 | owl_free(buff); |
---|
639 | continue; |
---|
640 | } else if (!strcasecmp(buff, "@")) { |
---|
641 | attrstack[stacksize]=OWL_FMTEXT_ATTR_NONE; |
---|
642 | chrstack[stacksize]=tmpptr[0]; |
---|
643 | colorstack[stacksize]=curcolor; |
---|
644 | stacksize++; |
---|
645 | txtptr+=2; |
---|
646 | owl_free(buff); |
---|
647 | continue; |
---|
648 | |
---|
649 | /* if it's a color read the color, set the current color and |
---|
650 | continue */ |
---|
651 | } else if (!strcasecmp(buff, "@color") |
---|
652 | && owl_global_get_hascolors(&g) |
---|
653 | && owl_global_is_colorztext(&g)) { |
---|
654 | owl_free(buff); |
---|
655 | txtptr+=7; |
---|
656 | tmpptr=strpbrk(txtptr, "@{[<()>]}"); |
---|
657 | if (tmpptr && |
---|
658 | ((txtptr[-1]=='(' && tmpptr[0]==')') || |
---|
659 | (txtptr[-1]=='<' && tmpptr[0]=='>') || |
---|
660 | (txtptr[-1]=='[' && tmpptr[0]==']') || |
---|
661 | (txtptr[-1]=='{' && tmpptr[0]=='}'))) { |
---|
662 | |
---|
663 | /* grab the color name */ |
---|
664 | buff=owl_malloc(tmpptr-txtptr+20); |
---|
665 | strncpy(buff, txtptr, tmpptr-txtptr); |
---|
666 | buff[tmpptr-txtptr]='\0'; |
---|
667 | |
---|
668 | /* set it as the current color */ |
---|
669 | curcolor=owl_util_string_to_color(buff); |
---|
670 | if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT; |
---|
671 | owl_free(buff); |
---|
672 | txtptr=tmpptr+1; |
---|
673 | continue; |
---|
674 | |
---|
675 | } else { |
---|
676 | |
---|
677 | } |
---|
678 | |
---|
679 | } else { |
---|
680 | /* if we didn't understand it, we'll print it. This is different from zwgc |
---|
681 | * but zwgc seems to be smarter about some screw cases than I am |
---|
682 | */ |
---|
683 | owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
684 | txtptr++; |
---|
685 | continue; |
---|
686 | } |
---|
687 | |
---|
688 | } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') { |
---|
689 | /* add the text up to this point first */ |
---|
690 | buff=owl_malloc(ptr-txtptr+20); |
---|
691 | strncpy(buff, txtptr, ptr-txtptr); |
---|
692 | buff[ptr-txtptr]='\0'; |
---|
693 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
694 | owl_free(buff); |
---|
695 | |
---|
696 | /* now deal with the closer */ |
---|
697 | txtptr=ptr; |
---|
698 | |
---|
699 | /* first, if the stack is empty we must bail (just print and go) */ |
---|
700 | if (stacksize==0) { |
---|
701 | buff=owl_malloc(5); |
---|
702 | buff[0]=ptr[0]; |
---|
703 | buff[1]='\0'; |
---|
704 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
705 | owl_free(buff); |
---|
706 | txtptr++; |
---|
707 | continue; |
---|
708 | } |
---|
709 | |
---|
710 | /* if the closing char is what's on the stack, turn off the |
---|
711 | attribue and pop the stack */ |
---|
712 | if ((ptr[0]==')' && chrstack[stacksize-1]=='(') || |
---|
713 | (ptr[0]=='>' && chrstack[stacksize-1]=='<') || |
---|
714 | (ptr[0]==']' && chrstack[stacksize-1]=='[') || |
---|
715 | (ptr[0]=='}' && chrstack[stacksize-1]=='{')) { |
---|
716 | int i; |
---|
717 | stacksize--; |
---|
718 | curattrs=OWL_FMTEXT_ATTR_NONE; |
---|
719 | curcolor = colorstack[stacksize]; |
---|
720 | for (i=0; i<stacksize; i++) { |
---|
721 | curattrs|=attrstack[i]; |
---|
722 | } |
---|
723 | txtptr+=1; |
---|
724 | continue; |
---|
725 | } else { |
---|
726 | /* otherwise print and continue */ |
---|
727 | buff=owl_malloc(5); |
---|
728 | buff[0]=ptr[0]; |
---|
729 | buff[1]='\0'; |
---|
730 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
731 | owl_free(buff); |
---|
732 | txtptr++; |
---|
733 | continue; |
---|
734 | } |
---|
735 | } else { |
---|
736 | /* we've found an unattached opener, print everything and move on */ |
---|
737 | buff=owl_malloc(ptr-txtptr+20); |
---|
738 | strncpy(buff, txtptr, ptr-txtptr+1); |
---|
739 | buff[ptr-txtptr+1]='\0'; |
---|
740 | owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT); |
---|
741 | owl_free(buff); |
---|
742 | txtptr=ptr+1; |
---|
743 | continue; |
---|
744 | } |
---|
745 | } |
---|
746 | } |
---|
747 | |
---|
748 | /* requires that the list values are strings or NULL. |
---|
749 | * joins the elements together with join_with. |
---|
750 | * If format_fn is specified, passes it the list element value |
---|
751 | * and it will return a string which this needs to free. */ |
---|
752 | void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*)) |
---|
753 | { |
---|
754 | int i, size; |
---|
755 | void *elem; |
---|
756 | char *text; |
---|
757 | |
---|
758 | size = owl_list_get_size(l); |
---|
759 | for (i=0; i<size; i++) { |
---|
760 | elem = (char*)owl_list_get_element(l,i); |
---|
761 | if (elem && format_fn) { |
---|
762 | text = format_fn(elem); |
---|
763 | if (text) { |
---|
764 | owl_fmtext_append_normal(f, text); |
---|
765 | owl_free(text); |
---|
766 | } |
---|
767 | } else if (elem) { |
---|
768 | owl_fmtext_append_normal(f, elem); |
---|
769 | } |
---|
770 | if ((i < size-1) && join_with) { |
---|
771 | owl_fmtext_append_normal(f, join_with); |
---|
772 | } |
---|
773 | } |
---|
774 | } |
---|
775 | |
---|
776 | /* Free all memory allocated by the object */ |
---|
777 | void owl_fmtext_free(owl_fmtext *f) |
---|
778 | { |
---|
779 | if (f->textbuff) owl_free(f->textbuff); |
---|
780 | } |
---|
781 | |
---|
782 | /*** Color Pair manager ***/ |
---|
783 | void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr) |
---|
784 | { |
---|
785 | // This could be a bitarray if we wanted to save memory. |
---|
786 | short i, j; |
---|
787 | cpmgr->next = 8; |
---|
788 | |
---|
789 | // The test is <= because we allocate COLORS+1 entries. |
---|
790 | cpmgr->pairs = owl_malloc((COLORS+1) * sizeof(short*)); |
---|
791 | for(i = 0; i <= COLORS; i++) { |
---|
792 | cpmgr->pairs[i] = owl_malloc((COLORS+1) * sizeof(short)); |
---|
793 | for(j = 0; j <= COLORS; j++) { |
---|
794 | cpmgr->pairs[i][j] = -1; |
---|
795 | } |
---|
796 | } |
---|
797 | if (owl_global_get_hascolors(&g)) { |
---|
798 | for(i = 0; i < 8; i++) { |
---|
799 | short fg, bg; |
---|
800 | if (i >= COLORS) continue; |
---|
801 | pair_content(i, &fg, &bg); |
---|
802 | cpmgr->pairs[fg+1][bg+1] = i; |
---|
803 | } |
---|
804 | } |
---|
805 | } |
---|
806 | |
---|
807 | /* Reset used list */ |
---|
808 | void owl_fmtext_reset_colorpairs() |
---|
809 | { |
---|
810 | if (owl_global_get_hascolors(&g)) { |
---|
811 | short i, j; |
---|
812 | owl_colorpair_mgr *cpmgr = owl_global_get_colorpair_mgr(&g); |
---|
813 | cpmgr->next = 8; |
---|
814 | |
---|
815 | // The test is <= because we allocated COLORS+1 entries. |
---|
816 | for(i = 0; i <= COLORS; i++) { |
---|
817 | for(j = 0; j <= COLORS; j++) { |
---|
818 | cpmgr->pairs[i][j] = -1; |
---|
819 | } |
---|
820 | } |
---|
821 | for(i = 0; i < 8; i++) { |
---|
822 | short fg, bg; |
---|
823 | if (i >= COLORS) continue; |
---|
824 | pair_content(i, &fg, &bg); |
---|
825 | cpmgr->pairs[fg+1][bg+1] = i; |
---|
826 | } |
---|
827 | } |
---|
828 | } |
---|
829 | |
---|
830 | /* Assign pairs by request */ |
---|
831 | short owl_fmtext_get_colorpair(int fg, int bg) |
---|
832 | { |
---|
833 | owl_colorpair_mgr *cpmgr; |
---|
834 | short pair, default_bg; |
---|
835 | |
---|
836 | #ifdef HAVE_USE_DEFAULT_COLORS |
---|
837 | if (fg == OWL_COLOR_DEFAULT) fg = -1; |
---|
838 | default_bg = OWL_COLOR_DEFAULT; |
---|
839 | #else |
---|
840 | if (fg == OWL_COLOR_DEFAULT) fg = 0; |
---|
841 | if (bg == OWL_COLOR_DEFAULT) bg = 0; |
---|
842 | default_bg = COLOR_BLACK; |
---|
843 | #endif |
---|
844 | |
---|
845 | // looking for a pair we already set up for this draw. |
---|
846 | cpmgr = owl_global_get_colorpair_mgr(&g); |
---|
847 | pair = cpmgr->pairs[fg+1][bg+1]; |
---|
848 | if (!(pair != -1 && pair < cpmgr->next)) { |
---|
849 | /* owl_global_set_needrefresh(&g);*/ |
---|
850 | // If we didn't find a pair, search for a free one to assign. |
---|
851 | pair = (cpmgr->next < COLOR_PAIRS) ? cpmgr->next : -1; |
---|
852 | if (pair != -1) { |
---|
853 | // We found a free pair, initialize it. |
---|
854 | init_pair(pair, fg, bg); |
---|
855 | cpmgr->pairs[fg+1][bg+1] = pair; |
---|
856 | cpmgr->next++; |
---|
857 | } |
---|
858 | else if (bg != OWL_COLOR_DEFAULT) { |
---|
859 | // We still don't have a pair, drop the background color. Too bad. |
---|
860 | owl_function_debugmsg("colorpairs: color shortage - dropping background color."); |
---|
861 | pair = owl_fmtext_get_colorpair(fg, OWL_COLOR_DEFAULT); |
---|
862 | } |
---|
863 | else { |
---|
864 | // We still don't have a pair, defaults all around. |
---|
865 | owl_function_debugmsg("colorpairs: color shortage - dropping foreground and background color."); |
---|
866 | pair = 0; |
---|
867 | } |
---|
868 | } |
---|
869 | return pair; |
---|
870 | } |
---|