source: fmtext.c @ 237d02c

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