source: fmtext.c @ b8a3e00

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