source: fmtext.c @ cfca761

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