source: fmtext.c @ a85d225

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