source: fmtext.c @ 8240bce

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