source: fmtext.c @ 2101a50

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