source: fmtext.c @ 8ba9313

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