source: fmtext.c @ c10fef0

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