source: fmtext.c @ 8574801

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