source: fmtext.c @ 6af4068

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