source: fmtext.c @ 6201646

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 6201646 was 5376a95, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 16 years ago
First pass at incoming zephyr -> UTF-8 sanitizing. This only operates on incoming data so far. We still need to clean outgoing data -- the plan is to attempt conversion to ISO-8859-1, and use that if it works.
  • Property mode set to 100644
File size: 23.7 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_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. */
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_fgcolor = 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) /*noproto*/
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_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. */
158void _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 */
171void _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' */
217void 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' */
224void 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 */
235char *owl_fmtext_print_plain(owl_fmtext *f)
236{
237  return owl_strip_format_chars(f->textbuff);
238}
239
240void _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/* add the formatted text to the curses window 'w'.  The window 'w'
248 * must already be initiatlized with curses
249 */
250void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w, int do_search)
251{
252  /* char *tmpbuff; */
253  /* int position, trans1, trans2, trans3, len, lastsame; */
254  char *s, *p;
255  char attr;
256  short fg, bg;
257  int search_results, search_len;
258 
259  if (w==NULL) {
260    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
261    return;
262  }
263
264  search_results = (do_search
265                    ? owl_fmtext_search(f, owl_global_get_search_string(&g))
266                    : NULL);
267  if (search_results) search_len = strlen(owl_global_get_search_string(&g));
268  s = f->textbuff;
269  /* Set default attributes. */
270  attr = f->default_attrs;
271  fg = f->default_fgcolor;
272  bg = f->default_bgcolor;
273  _owl_fmtext_wattrset(w, attr);
274  if (owl_global_get_hascolors(&g)) {
275    short pair;
276    pair = owl_fmtext_get_colorpair(fg, bg);
277    if (pair != -1) {
278      wcolor_set(w,pair,NULL);
279    }
280  }
281
282  /* Find next possible format character. */
283  p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
284  while(p) {
285    if (_owl_fmtext_is_format_char(g_utf8_get_char(p))) {
286      /* Deal with all text from last insert to here. */
287      char tmp;
288   
289      tmp = p[0];
290      p[0] = '\0';
291      if (search_results) {
292        /* Search is active, so highlight search results. */
293        char tmp2, *ss;
294        ss = stristr(s, owl_global_get_search_string(&g));
295        while (ss) {
296          /* Found search string, highlight it. */
297
298          tmp2 = ss[0];
299          ss[0] = '\0';
300          waddstr(w, s);
301          ss[0] = tmp2;
302
303          _owl_fmtext_wattrset(w,attr ^ A_REVERSE);
304
305          tmp2 = ss[search_len];
306          ss[search_len] = '\0';
307          waddstr(w, ss);
308          ss[search_len] = tmp2;
309
310          _owl_fmtext_wattrset(w,attr);
311
312          s = ss + search_len;
313          ss = stristr(s, owl_global_get_search_string(&g));
314        }
315      }
316      /* Deal with remaining part of string. */
317      waddstr(w, s);
318      p[0] = tmp;
319
320      /* Deal with new attributes. Initialize to defaults, then
321         process all consecutive formatting characters. */
322      attr = f->default_attrs;
323      fg = f->default_fgcolor;
324      bg = f->default_bgcolor;
325      while (p && _owl_fmtext_is_format_char(g_utf8_get_char(p))) {
326        _owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg);
327        p = g_utf8_next_char(p);
328      }
329      _owl_fmtext_wattrset(w, attr | f->default_attrs);
330      if (owl_global_get_hascolors(&g)) {
331        if (fg == OWL_COLOR_DEFAULT) fg = f->default_fgcolor;
332        if (bg == OWL_COLOR_DEFAULT) bg = f->default_bgcolor;
333        short pair;
334        pair = owl_fmtext_get_colorpair(fg, bg);
335        if (pair != -1) {
336          wcolor_set(w,pair,NULL);
337        }
338      }
339      /* Advance to next non-formatting character. */
340      s = p;
341      p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
342    }
343    else {
344      p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
345    }
346  }
347  if (s) {
348    waddstr(w, s);
349  }
350}
351
352
353/* start with line 'aline' (where the first line is 0) and print
354 * 'lines' number of lines into 'out'
355 */
356int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out)
357{
358  char *ptr1, *ptr2;
359  int i, offset;
360 
361  /* find the starting line */
362  ptr1 = in->textbuff;
363  for (i = 0; i < aline; i++) {
364    ptr1 = strchr(ptr1, '\n');
365    if (!ptr1) return(-1);
366    ptr1++;
367  }
368 
369  /* ptr1 now holds the starting point */
370
371  /* copy in the next 'lines' lines */
372  if (lines < 1) return(-1);
373
374  for (i = 0; i < lines; i++) {
375    offset = ptr1 - in->textbuff;
376    ptr2 = strchr(ptr1, '\n');
377    if (!ptr2) {
378      _owl_fmtext_append_fmtext(out, in, offset, (in->textlen) - 1);
379      return(-1);
380    }
381    _owl_fmtext_append_fmtext(out, in, offset, (ptr2 - ptr1) + offset);
382    ptr1 = ptr2 + 1;
383  }
384  return(0);
385}
386
387/* Truncate the message so that each line begins at column 'acol' and
388 * ends at 'bcol' or sooner.  The first column is number 0.  The new
389 * message is placed in 'out'.  The message is * expected to end in a
390 * new line for now
391 */
392void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
393{
394  char *ptr_s, *ptr_e, *ptr_c, *last;
395  int col, st, padding, chwidth;
396
397  last=in->textbuff+in->textlen-1;
398  ptr_s=in->textbuff;
399  while (ptr_s <= last) {
400    ptr_e=strchr(ptr_s, '\n');
401    if (!ptr_e) {
402      /* but this shouldn't happen if we end in a \n */
403      break;
404    }
405   
406    if (ptr_e == ptr_s) {
407      owl_fmtext_append_normal(out, "\n");
408      ++ptr_s;
409      continue;
410    }
411
412    col = 0;
413    st = 0;
414    padding = 0;
415    ptr_c = ptr_s;
416    while(col <= bcol && ptr_c < ptr_e) {
417      gunichar c = g_utf8_get_char(ptr_c);
418      if (!_owl_fmtext_is_format_char(c)) {
419        chwidth = wcwidth(c);
420     
421        if (col + chwidth > bcol)
422          break;
423        if (col >= acol) {
424          if (st == 0) {
425            ptr_s = ptr_c;
426            padding = col - acol;
427            ++st;
428          }
429        }
430        col += chwidth;
431        chwidth = 0;
432      }
433      ptr_c = g_utf8_next_char(ptr_c);
434    }
435    if (st) {
436      /* lead padding */
437      owl_fmtext_append_spaces(out, padding);
438      if (ptr_c[0] & 0x80) {
439        if (col + chwidth == bcol)
440          ptr_c = g_utf8_next_char(ptr_c);
441        _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - 1 - in->textbuff);     
442        owl_fmtext_append_normal(out, "\n");
443      }
444      else {
445        _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff);
446      }
447    }
448    else {
449      owl_fmtext_append_normal(out, "\n");
450    }
451    ptr_s = g_utf8_next_char(ptr_e);
452  }
453}
454
455/* Return the number of lines in 'f' */
456int owl_fmtext_num_lines(owl_fmtext *f)
457{
458  int lines, i;
459
460  if (f->textlen==0) return(0);
461 
462  lines=0;
463  for (i=0; i<f->textlen; i++) {
464    if (f->textbuff[i]=='\n') lines++;
465  }
466
467  /* if the last char wasn't a \n there's one more line */
468  if (f->textbuff[i-1]!='\n') lines++;
469
470  return(lines);
471}
472
473char *owl_fmtext_get_text(owl_fmtext *f)
474{
475  return(f->textbuff);
476}
477
478/* set the charater at 'index' to be 'char'.  If index is out of
479 * bounds don't do anything. If c or char at index is not ASCII, don't
480 * do anything because it's not UTF-8 safe. */
481void owl_fmtext_set_char(owl_fmtext *f, int index, char ch)
482{
483  if ((index < 0) || (index > f->textlen-1)) return;
484  /* NOT ASCII*/
485  if (f->textbuff[index] & 0x80 || ch & 0x80) return; 
486  f->textbuff[index]=ch;
487}
488
489/* Make a copy of the fmtext 'src' into 'dst' */
490void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src)
491{
492  int mallocsize;
493
494  if (src->textlen==0) {
495    mallocsize=5;
496  } else {
497    mallocsize=src->textlen+2;
498  }
499  dst->textlen=src->textlen;
500  dst->textbuff=owl_malloc(mallocsize);
501  memcpy(dst->textbuff, src->textbuff, src->textlen+1);
502  dst->default_attrs = src->default_attrs;
503  dst->default_fgcolor = src->default_fgcolor;
504  dst->default_bgcolor = src->default_bgcolor;
505}
506
507/* return 1 if the string is found, 0 if not.  This is a case
508 *  insensitive search.
509 */
510int owl_fmtext_search(owl_fmtext *f, char *string)
511{
512  if (stristr(f->textbuff, string)) return(1);
513  return(0);
514}
515
516
517/* Append the text 'text' to 'f' and interpret the zephyr style
518 * formatting syntax to set appropriate attributes.
519 */
520void owl_fmtext_append_ztext(owl_fmtext *f, char *text)
521{
522  int stacksize, curattrs, curcolor;
523  char *ptr, *txtptr, *buff, *tmpptr;
524  int attrstack[32], chrstack[32], colorstack[32];
525
526  curattrs=OWL_FMTEXT_ATTR_NONE;
527  curcolor=OWL_COLOR_DEFAULT;
528  stacksize=0;
529  txtptr=text;
530  while (1) {
531    ptr=strpbrk(txtptr, "@{[<()>]}");
532    if (!ptr) {
533      /* add all the rest of the text and exit */
534      owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT);
535      return;
536    } else if (ptr[0]=='@') {
537      /* add the text up to this point then deal with the stack */
538      buff=owl_malloc(ptr-txtptr+20);
539      strncpy(buff, txtptr, ptr-txtptr);
540      buff[ptr-txtptr]='\0';
541      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
542      owl_free(buff);
543
544      /* update pointer to point at the @ */
545      txtptr=ptr;
546
547      /* now the stack */
548
549      /* if we've hit our max stack depth, print the @ and move on */
550      if (stacksize==32) {
551        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
552        txtptr++;
553        continue;
554      }
555
556      /* if it's an @@, print an @ and continue */
557      if (txtptr[1]=='@') {
558        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
559        txtptr+=2;
560        continue;
561      }
562       
563      /* if there's no opener, print the @ and continue */
564      tmpptr=strpbrk(txtptr, "(<[{ ");
565      if (!tmpptr || tmpptr[0]==' ') {
566        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
567        txtptr++;
568        continue;
569      }
570
571      /* check what command we've got, push it on the stack, start
572         using it, and continue ... unless it's a color command */
573      buff=owl_malloc(tmpptr-ptr+20);
574      strncpy(buff, ptr, tmpptr-ptr);
575      buff[tmpptr-ptr]='\0';
576      if (!strcasecmp(buff, "@bold")) {
577        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
578        chrstack[stacksize]=tmpptr[0];
579        colorstack[stacksize]=curcolor;
580        stacksize++;
581        curattrs|=OWL_FMTEXT_ATTR_BOLD;
582        txtptr+=6;
583        owl_free(buff);
584        continue;
585      } else if (!strcasecmp(buff, "@b")) {
586        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
587        chrstack[stacksize]=tmpptr[0];
588        colorstack[stacksize]=curcolor;
589        stacksize++;
590        curattrs|=OWL_FMTEXT_ATTR_BOLD;
591        txtptr+=3;
592        owl_free(buff);
593        continue;
594      } else if (!strcasecmp(buff, "@i")) {
595        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
596        chrstack[stacksize]=tmpptr[0];
597        colorstack[stacksize]=curcolor;
598        stacksize++;
599        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
600        txtptr+=3;
601        owl_free(buff);
602        continue;
603      } else if (!strcasecmp(buff, "@italic")) {
604        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
605        chrstack[stacksize]=tmpptr[0];
606        colorstack[stacksize]=curcolor;
607        stacksize++;
608        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
609        txtptr+=8;
610        owl_free(buff);
611        continue;
612      } else if (!strcasecmp(buff, "@")) {
613        attrstack[stacksize]=OWL_FMTEXT_ATTR_NONE;
614        chrstack[stacksize]=tmpptr[0];
615        colorstack[stacksize]=curcolor;
616        stacksize++;
617        txtptr+=2;
618        owl_free(buff);
619        continue;
620
621        /* if it's a color read the color, set the current color and
622           continue */
623      } else if (!strcasecmp(buff, "@color") 
624                 && owl_global_get_hascolors(&g)
625                 && owl_global_is_colorztext(&g)) {
626        owl_free(buff);
627        txtptr+=7;
628        tmpptr=strpbrk(txtptr, "@{[<()>]}");
629        if (tmpptr &&
630            ((txtptr[-1]=='(' && tmpptr[0]==')') ||
631             (txtptr[-1]=='<' && tmpptr[0]=='>') ||
632             (txtptr[-1]=='[' && tmpptr[0]==']') ||
633             (txtptr[-1]=='{' && tmpptr[0]=='}'))) {
634
635          /* grab the color name */
636          buff=owl_malloc(tmpptr-txtptr+20);
637          strncpy(buff, txtptr, tmpptr-txtptr);
638          buff[tmpptr-txtptr]='\0';
639
640          /* set it as the current color */
641          curcolor=owl_util_string_to_color(buff);
642          if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT;
643          owl_free(buff);
644          txtptr=tmpptr+1;
645          continue;
646
647        } else {
648
649        }
650
651      } else {
652        /* if we didn't understand it, we'll print it.  This is different from zwgc
653         * but zwgc seems to be smarter about some screw cases than I am
654         */
655        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
656        txtptr++;
657        continue;
658      }
659
660    } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') {
661      /* add the text up to this point first */
662      buff=owl_malloc(ptr-txtptr+20);
663      strncpy(buff, txtptr, ptr-txtptr);
664      buff[ptr-txtptr]='\0';
665      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
666      owl_free(buff);
667
668      /* now deal with the closer */
669      txtptr=ptr;
670
671      /* first, if the stack is empty we must bail (just print and go) */
672      if (stacksize==0) {
673        buff=owl_malloc(5);
674        buff[0]=ptr[0];
675        buff[1]='\0';
676        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
677        owl_free(buff);
678        txtptr++;
679        continue;
680      }
681
682      /* if the closing char is what's on the stack, turn off the
683         attribue and pop the stack */
684      if ((ptr[0]==')' && chrstack[stacksize-1]=='(') ||
685          (ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
686          (ptr[0]==']' && chrstack[stacksize-1]=='[') ||
687          (ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
688        int i;
689        stacksize--;
690        curattrs=OWL_FMTEXT_ATTR_NONE;
691        curcolor = colorstack[stacksize];
692        for (i=0; i<stacksize; i++) {
693          curattrs|=attrstack[i];
694        }
695        txtptr+=1;
696        continue;
697      } else {
698        /* otherwise print and continue */
699        buff=owl_malloc(5);
700        buff[0]=ptr[0];
701        buff[1]='\0';
702        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
703        owl_free(buff);
704        txtptr++;
705        continue;
706      }
707    } else {
708      /* we've found an unattached opener, print everything and move on */
709      buff=owl_malloc(ptr-txtptr+20);
710      strncpy(buff, txtptr, ptr-txtptr+1);
711      buff[ptr-txtptr+1]='\0';
712      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
713      owl_free(buff);
714      txtptr=ptr+1;
715      continue;
716    }
717  }
718}
719
720/* requires that the list values are strings or NULL.
721 * joins the elements together with join_with.
722 * If format_fn is specified, passes it the list element value
723 * and it will return a string which this needs to free. */
724void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*))
725{
726  int i, size;
727  void *elem;
728  char *text;
729
730  size = owl_list_get_size(l);
731  for (i=0; i<size; i++) {
732    elem = (char*)owl_list_get_element(l,i);
733    if (elem && format_fn) {
734      text = format_fn(elem);
735      if (text) {
736        owl_fmtext_append_normal(f, text);
737        owl_free(text);
738      }
739    } else if (elem) {
740      owl_fmtext_append_normal(f, elem);
741    }
742    if ((i < size-1) && join_with) {
743      owl_fmtext_append_normal(f, join_with);
744    }
745  }
746}
747
748/* Free all memory allocated by the object */
749void owl_fmtext_free(owl_fmtext *f)
750{
751  if (f->textbuff) owl_free(f->textbuff);
752}
753
754/*** Color Pair manager ***/
755void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr)
756{
757  // This could be a bitarray if we wanted to save memory.
758  short i, j;
759  cpmgr->next = 8;
760 
761  // The test is <= because we allocate COLORS+1 entries.
762  cpmgr->pairs = owl_malloc((COLORS+1) * sizeof(short*));
763  for(i = 0; i <= COLORS; i++) {
764    cpmgr->pairs[i] = owl_malloc((COLORS+1) * sizeof(short));
765    for(j = 0; j <= COLORS; j++) {
766      cpmgr->pairs[i][j] = -1;
767    }
768  }
769  if (owl_global_get_hascolors(&g)) {
770    for(i = 0; i < 8; i++) {
771      short fg, bg;
772      if (i >= COLORS) continue;
773      pair_content(i, &fg, &bg);
774      cpmgr->pairs[fg+1][bg+1] = i;
775    }
776  }
777}
778
779/* Reset used list */
780void owl_fmtext_reset_colorpairs()
781{
782  if (owl_global_get_hascolors(&g)) {
783    short i, j;
784    owl_colorpair_mgr *cpmgr = owl_global_get_colorpair_mgr(&g);
785    cpmgr->next = 8;
786   
787    // The test is <= because we allocated COLORS+1 entries.
788    for(i = 0; i <= COLORS; i++) {
789      for(j = 0; j <= COLORS; j++) {
790        cpmgr->pairs[i][j] = -1;
791      }
792    }
793    for(i = 0; i < 8; i++) {
794      short fg, bg;
795      if (i >= COLORS) continue;
796      pair_content(i, &fg, &bg);
797      cpmgr->pairs[fg+1][bg+1] = i;
798    }
799  }
800}
801
802/* Assign pairs by request */
803short owl_fmtext_get_colorpair(int fg, int bg)
804{
805  owl_colorpair_mgr *cpmgr;
806  short pair, default_bg;
807
808#ifdef HAVE_USE_DEFAULT_COLORS
809  if (fg == OWL_COLOR_DEFAULT) fg = -1;
810  default_bg = OWL_COLOR_DEFAULT;
811#else
812  if (fg == OWL_COLOR_DEFAULT) fg = 0;
813  if (bg == OWL_COLOR_DEFAULT) bg = 0;
814  default_bg = COLOR_BLACK;
815#endif
816
817  // looking for a pair we already set up for this draw.
818  cpmgr = owl_global_get_colorpair_mgr(&g);
819  pair = cpmgr->pairs[fg+1][bg+1];
820  if (!(pair != -1 && pair < cpmgr->next)) {
821/*    owl_global_set_needrefresh(&g);*/
822    // If we didn't find a pair, search for a free one to assign.
823    pair = (cpmgr->next < COLOR_PAIRS) ? cpmgr->next : -1;
824    if (pair != -1) {
825      // We found a free pair, initialize it.
826      init_pair(pair, fg, bg);
827      cpmgr->pairs[fg+1][bg+1] = pair;
828      cpmgr->next++;
829    }
830    else if (bg != OWL_COLOR_DEFAULT) {
831      // We still don't have a pair, drop the background color. Too bad.
832      owl_function_debugmsg("colorpairs: color shortage - dropping background color.");
833      pair = owl_fmtext_get_colorpair(fg, OWL_COLOR_DEFAULT);
834    }
835    else {
836      // We still don't have a pair, defaults all around.
837      owl_function_debugmsg("colorpairs: color shortage - dropping foreground and background color.");
838      pair = 0;
839    }
840  }
841  return pair;
842}
Note: See TracBrowser for help on using the repository browser.