source: fmtext.c @ 389d487

release-1.10release-1.9
Last change on this file since 389d487 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
RevLine 
[7d4fbcd]1#include "owl.h"
2
[b2b0773]3/* initialize an fmtext with no data */
[5789230]4void owl_fmtext_init_null(owl_fmtext *f)
5{
[7e111f4]6  f->buff = g_string_new("");
[7d4fbcd]7}
8
[a387d12e]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{
[7e111f4]13  g_string_truncate(f->buff, 0);
[7d4fbcd]14}
15
[c1522ec]16int owl_fmtext_is_format_char(gunichar c)
[9866c3a]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}
[b2b0773]22/* append text to the end of 'f' with attribute 'attr' and color
23 * 'color'
24 */
[e19eb97]25void owl_fmtext_append_attr(owl_fmtext *f, const char *text, char attr, short fgcolor, short bgcolor)
[5789230]26{
[7e111f4]27  int a = 0, fg = 0, bg = 0;
[9866c3a]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 */
[6c171f1]34  if (a)
[7e111f4]35    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr);
[6c171f1]36  if (fg)
[7e111f4]37    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor);
[6c171f1]38  if (bg)
[7e111f4]39    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor);
40
41  g_string_append(f->buff, text);
[9866c3a]42
43  /* Reset attributes */
[7659079]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);
[7d4fbcd]47}
48
[b2b0773]49/* Append normal, uncolored text 'text' to 'f' */
[e19eb97]50void owl_fmtext_append_normal(owl_fmtext *f, const char *text)
[5789230]51{
[8fa9562]52  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
[7d4fbcd]53}
54
[1490327]55/* Append normal, uncolored text specified by format string to 'f' */
[4479497]56void G_GNUC_PRINTF(2, 3) owl_fmtext_appendf_normal(owl_fmtext *f, const char *fmt, ...)
[1490327]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);
[ddbbcffa]67  g_free(buff);
[1490327]68}
69
[b2b0773]70/* Append normal text 'text' to 'f' with color 'color' */
[e19eb97]71void owl_fmtext_append_normal_color(owl_fmtext *f, const char *text, int fgcolor, int bgcolor)
[5789230]72{
[8fa9562]73  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor);
[7d4fbcd]74}
75
[b2b0773]76/* Append bold text 'text' to 'f' */
[e19eb97]77void owl_fmtext_append_bold(owl_fmtext *f, const char *text)
[5789230]78{
[8fa9562]79  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
[7d4fbcd]80}
81
[b2b0773]82/* Append reverse video text 'text' to 'f' */
[e19eb97]83void owl_fmtext_append_reverse(owl_fmtext *f, const char *text)
[5789230]84{
[8fa9562]85  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
[7d4fbcd]86}
87
[b2b0773]88/* Append reversed and bold, uncolored text 'text' to 'f' */
[e19eb97]89void owl_fmtext_append_reversebold(owl_fmtext *f, const char *text)
[5789230]90{
[8fa9562]91  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
[7d4fbcd]92}
93
[9866c3a]94/* Internal function. Parse attrbute character. */
[e9c6fc8]95static void _owl_fmtext_update_attributes(gunichar c, char *attr, short *fgcolor, short *bgcolor)
[9866c3a]96{
97  if ((c & OWL_FMTEXT_UC_ATTR) == OWL_FMTEXT_UC_ATTR) {
98    *attr = c & OWL_FMTEXT_UC_ATTR_MASK;
99  }
[6f6330b]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    }
[7d4fbcd]111  }
112}
113
[9866c3a]114/* Internal function. Scan for attribute characters. */
[e9c6fc8]115static void _owl_fmtext_scan_attributes(const owl_fmtext *f, int start, char *attr, short *fgcolor, short *bgcolor)
[9866c3a]116{
[e19eb97]117  const char *p;
[7e111f4]118  p = strchr(f->buff->str, OWL_FMTEXT_UC_STARTBYTE_UTF8);
119  while (p && p < f->buff->str + start) {
[9866c3a]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
[e0e0e5a]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.
[b2b0773]128 */
[e9c6fc8]129static void _owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in, int start, int stop)
[9866c3a]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
[cb6c9e1]137  if (attr != OWL_FMTEXT_ATTR_NONE)
[7e111f4]138    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_ATTR | attr);
[cb6c9e1]139  if (fgcolor != OWL_COLOR_DEFAULT)
[7e111f4]140    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_FGCOLOR | fgcolor);
[cb6c9e1]141  if (bgcolor != OWL_COLOR_DEFAULT)
[7e111f4]142    g_string_append_unichar(f->buff, OWL_FMTEXT_UC_BGCOLOR | bgcolor);
[9866c3a]143
[7e111f4]144  g_string_append_len(f->buff, in->buff->str+start, stop-start);
[9866c3a]145
146  /* Reset attributes */
[7659079]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);
[7d4fbcd]150}
151
[b2b0773]152/* append fmtext 'in' to 'f' */
[075ba92]153void owl_fmtext_append_fmtext(owl_fmtext *f, const owl_fmtext *in)
[5789230]154{
[7e111f4]155  _owl_fmtext_append_fmtext(f, in, 0, in->buff->len);
[5789230]156
157}
158
[b2b0773]159/* Append 'nspaces' number of spaces to the end of 'f' */
[5789230]160void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces)
161{
[7d4fbcd]162  int i;
163  for (i=0; i<nspaces; i++) {
164    owl_fmtext_append_normal(f, " ");
165  }
166}
167
[b2b0773]168/* Return a plain version of the fmtext.  Caller is responsible for
169 * freeing the return
170 */
[2560529]171CALLER_OWN char *owl_fmtext_print_plain(const owl_fmtext *f)
[5789230]172{
[7e111f4]173  return owl_strip_format_chars(f->buff->str);
[e1c4636]174}
175
[e9c6fc8]176static void _owl_fmtext_wattrset(WINDOW *w, int attrs)
[9866c3a]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}
[16c6cca]183
[e9c6fc8]184static void _owl_fmtext_wcolor_set(WINDOW *w, short pair)
[16c6cca]185{
[7abfcf2]186  cchar_t background;
187  wchar_t blank[2] = { ' ', 0 };
[7b4f3be]188  if (has_colors()) {
[7abfcf2]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);
[16c6cca]194  }
195}
196
[b2b0773]197/* add the formatted text to the curses window 'w'.  The window 'w'
198 * must already be initiatlized with curses
199 */
[237d02c]200static void _owl_fmtext_curs_waddstr(const owl_fmtext *f, WINDOW *w, int do_search, char default_attrs, short default_fgcolor, short default_bgcolor)
[9866c3a]201{
202  /* char *tmpbuff; */
203  /* int position, trans1, trans2, trans3, len, lastsame; */
204  char *s, *p;
205  char attr;
[18fb86ee]206  short fg, bg, pair = 0;
[9866c3a]207 
[f2f9314]208  if (w==NULL) {
209    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
210    return;
211  }
212
[7e111f4]213  s = f->buff->str;
[9866c3a]214  /* Set default attributes. */
[237d02c]215  attr = default_attrs;
216  fg = default_fgcolor;
217  bg = default_bgcolor;
[9866c3a]218  _owl_fmtext_wattrset(w, attr);
[7b4f3be]219  pair = owl_fmtext_get_colorpair(fg, bg);
[16c6cca]220  _owl_fmtext_wcolor_set(w, pair);
[7d4fbcd]221
[9866c3a]222  /* Find next possible format character. */
223  p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
224  while(p) {
[c1522ec]225    if (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
[9866c3a]226      /* Deal with all text from last insert to here. */
227      char tmp;
228   
229      tmp = p[0];
230      p[0] = '\0';
[449c682]231      if (do_search && owl_global_is_search_active(&g)) {
[9866c3a]232        /* Search is active, so highlight search results. */
[41c9a96]233        int start, end;
234        while (owl_regex_compare(owl_global_get_search_re(&g), s, &start, &end) == 0) {
[0ed5c57]235          /* Prevent an infinite loop matching the empty string. */
236          if (end == 0)
237            break;
238
[9866c3a]239          /* Found search string, highlight it. */
240
[d3c318b]241          waddnstr(w, s, start);
[9866c3a]242
[987a93c]243          _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE);
[16c6cca]244          _owl_fmtext_wcolor_set(w, pair);
245         
[d3c318b]246          waddnstr(w, s + start, end - start);
[9866c3a]247
[16c6cca]248          _owl_fmtext_wattrset(w, attr);
249          _owl_fmtext_wcolor_set(w, pair);
[9866c3a]250
[41c9a96]251          s += end;
[9866c3a]252        }
253      }
254      /* Deal with remaining part of string. */
255      waddstr(w, s);
256      p[0] = tmp;
257
[cb6c9e1]258      /* Deal with new attributes. Process all consecutive formatting
259       * characters, and then apply defaults where relevant. */
[f119757]260      while (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
[9866c3a]261        _owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg);
262        p = g_utf8_next_char(p);
[7d4fbcd]263      }
[cb6c9e1]264      attr |= default_attrs;
[237d02c]265      if (fg == OWL_COLOR_DEFAULT) fg = default_fgcolor;
266      if (bg == OWL_COLOR_DEFAULT) bg = default_bgcolor;
[cb6c9e1]267      _owl_fmtext_wattrset(w, attr);
[7b4f3be]268      pair = owl_fmtext_get_colorpair(fg, bg);
[16c6cca]269      _owl_fmtext_wcolor_set(w, pair);
270
[9866c3a]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);
[7d4fbcd]277    }
278  }
[9866c3a]279  if (s) {
280    waddstr(w, s);
281  }
[39cff48]282  wbkgdset(w, 0);
[7d4fbcd]283}
284
[237d02c]285void owl_fmtext_curs_waddstr(const owl_fmtext *f, WINDOW *w, char default_attrs, short default_fgcolor, short default_bgcolor)
[47519e1b]286{
[237d02c]287  _owl_fmtext_curs_waddstr(f, w, 1, default_attrs, default_fgcolor, default_bgcolor);
[47519e1b]288}
289
[237d02c]290void owl_fmtext_curs_waddstr_without_search(const owl_fmtext *f, WINDOW *w, char default_attrs, short default_fgcolor, short default_bgcolor)
[47519e1b]291{
[237d02c]292  _owl_fmtext_curs_waddstr(f, w, 0, default_attrs, default_fgcolor, default_bgcolor);
[47519e1b]293}
[7d4fbcd]294
[2b83ad6]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
[7e111f4]300  for (ptr = in->buff->str;
301       ptr < in->buff->str + in->buff->len;
[2b83ad6]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 */
[7e111f4]307      _owl_fmtext_append_fmtext(out, in, numcopied, ptr - in->buff->str);
[2b83ad6]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;
[7e111f4]312      numcopied = g_utf8_next_char(ptr) - in->buff->str;
[2b83ad6]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. */
[7e111f4]323  if (numcopied < in->buff->len)
324    _owl_fmtext_append_fmtext(out, in, numcopied, in->buff->len);
[2b83ad6]325}
326
[b2b0773]327/* start with line 'aline' (where the first line is 0) and print
328 * 'lines' number of lines into 'out'
329 */
[075ba92]330int owl_fmtext_truncate_lines(const owl_fmtext *in, int aline, int lines, owl_fmtext *out)
[5789230]331{
[e19eb97]332  const char *ptr1, *ptr2;
[7d4fbcd]333  int i, offset;
334 
335  /* find the starting line */
[7e111f4]336  ptr1 = in->buff->str;
[9866c3a]337  for (i = 0; i < aline; i++) {
338    ptr1 = strchr(ptr1, '\n');
339    if (!ptr1) return(-1);
340    ptr1++;
[7d4fbcd]341  }
[9866c3a]342 
[7d4fbcd]343  /* ptr1 now holds the starting point */
344
345  /* copy in the next 'lines' lines */
[9866c3a]346  if (lines < 1) return(-1);
[7d4fbcd]347
[9866c3a]348  for (i = 0; i < lines; i++) {
[7e111f4]349    offset = ptr1 - in->buff->str;
[9866c3a]350    ptr2 = strchr(ptr1, '\n');
[7d4fbcd]351    if (!ptr2) {
[e0e0e5a]352      /* Copy to the end of the buffer. */
[7e111f4]353      _owl_fmtext_append_fmtext(out, in, offset, in->buff->len);
[7d4fbcd]354      return(-1);
355    }
[e0e0e5a]356    /* Copy up to, and including, the new line. */
357    _owl_fmtext_append_fmtext(out, in, offset, (ptr2 - ptr1) + offset + 1);
[9866c3a]358    ptr1 = ptr2 + 1;
[7d4fbcd]359  }
360  return(0);
361}
362
[e0022d2]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)
[5789230]365{
[e19eb97]366  const char *ptr_s, *ptr_e, *ptr_c, *last;
[9866c3a]367  int col, st, padding, chwidth;
[7d4fbcd]368
[7e111f4]369  last = in->buff->str + in->buff->len - 1;
370  ptr_s = in->buff->str;
[9866c3a]371  while (ptr_s <= last) {
[28ee32b]372    ptr_e=strchr(ptr_s, '\n');
373    if (!ptr_e) {
[dd6af02]374      /* but this shouldn't happen if we end in a \n */
375      break;
[7d4fbcd]376    }
377   
[9866c3a]378    if (ptr_e == ptr_s) {
[7d4fbcd]379      owl_fmtext_append_normal(out, "\n");
[9866c3a]380      ++ptr_s;
[7d4fbcd]381      continue;
382    }
383
[28ee32b]384    col = 0;
[9866c3a]385    st = 0;
[dd24b6a]386    padding = 0;
[47519e1b]387    chwidth = 0;
[28ee32b]388    ptr_c = ptr_s;
[3a7cf49]389    while(ptr_c < ptr_e) {
[28ee32b]390      gunichar c = g_utf8_get_char(ptr_c);
[c1522ec]391      if (!owl_fmtext_is_format_char(c)) {
[47519e1b]392        chwidth = mk_wcwidth(c);
[3a7cf49]393        if (col + chwidth > bcol) break;
394       
[9866c3a]395        if (col >= acol) {
396          if (st == 0) {
397            ptr_s = ptr_c;
398            padding = col - acol;
399            ++st;
400          }
[dd24b6a]401        }
[9866c3a]402        col += chwidth;
403        chwidth = 0;
[28ee32b]404      }
[9866c3a]405      ptr_c = g_utf8_next_char(ptr_c);
[dd24b6a]406    }
[9866c3a]407    if (st) {
408      /* lead padding */
409      owl_fmtext_append_spaces(out, padding);
[3a7cf49]410      if (ptr_c == ptr_e) {
[e0e0e5a]411        /* We made it to the newline. Append up to, and including it. */
[7e111f4]412        _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str + 1);
[9866c3a]413      }
[db1af5f]414      else if (chwidth > 1) {
415        /* Last char is wide, truncate. */
[7e111f4]416        _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str);
[db1af5f]417        owl_fmtext_append_normal(out, "\n");
418      }
[9866c3a]419      else {
[e0e0e5a]420        /* Last char fits perfectly, We stop at the next char to make
421         * sure we get it all. */
[db1af5f]422        ptr_c = g_utf8_next_char(ptr_c);
[7e111f4]423        _owl_fmtext_append_fmtext(out, in, ptr_s - in->buff->str, ptr_c - in->buff->str);
[28ee32b]424      }
[dd24b6a]425    }
426    else {
427      owl_fmtext_append_normal(out, "\n");
[28ee32b]428    }
[dd6af02]429    ptr_s = g_utf8_next_char(ptr_e);
[7d4fbcd]430  }
431}
432
[e0022d2]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. */
[7e111f4]450  if (strchr(in->buff->str, '\t')) {
[e0022d2]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
[b2b0773]460/* Return the number of lines in 'f' */
[075ba92]461int owl_fmtext_num_lines(const owl_fmtext *f)
[5789230]462{
[7d4fbcd]463  int lines, i;
[c93b8b5]464  char *lastbreak, *p;
[7d4fbcd]465
466  lines=0;
[7e111f4]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;
[c93b8b5]471      lines++;
472    }
[7d4fbcd]473  }
474
[c93b8b5]475  /* Check if there's a trailing line; formatting characters don't count. */
476  for (p = g_utf8_next_char(lastbreak);
[7e111f4]477       p < f->buff->str + f->buff->len;
[c93b8b5]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  }
[7d4fbcd]484
485  return(lines);
486}
487
[f7456bc]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;
[7e111f4]496  if (offset >= f->buff->len)
497    offset = f->buff->len - 1;
[f7456bc]498  for (i = 0; i < offset; i++) {
[7e111f4]499    if (f->buff->str[i] == '\n')
[f7456bc]500      lineno++;
501  }
502  return lineno;
503}
504
[7ba2ad4]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;
[7e111f4]511  for (start = 0; lineno > 0 && start < f->buff->len; start++) {
512    if (f->buff->str[start] == '\n')
[7ba2ad4]513      lineno--;
514  }
[7e111f4]515  newline = strchr(f->buff->str + start, '\n');
[7ba2ad4]516  /* Include the newline, if it is there. */
[7e111f4]517  end = newline ? newline - f->buff->str + 1 : f->buff->len;
[7ba2ad4]518  if (o_start) *o_start = start;
519  if (o_end) *o_end = end;
520}
521
[075ba92]522const char *owl_fmtext_get_text(const owl_fmtext *f)
[5789230]523{
[7e111f4]524  return f->buff->str;
[7d4fbcd]525}
526
[6772d19]527int owl_fmtext_num_bytes(const owl_fmtext *f)
528{
[7e111f4]529  return f->buff->len;
[6772d19]530}
531
[b2b0773]532/* Make a copy of the fmtext 'src' into 'dst' */
[075ba92]533void owl_fmtext_copy(owl_fmtext *dst, const owl_fmtext *src)
[5789230]534{
[7e111f4]535  dst->buff = g_string_new(src->buff->str);
[1fd0b25]536}
537
[72f613a]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.
[5789230]541 */
[72f613a]542int owl_fmtext_search(const owl_fmtext *f, const owl_regex *re, int start)
[5789230]543{
[72f613a]544  int offset;
[7e111f4]545  if (start > f->buff->len ||
546      owl_regex_compare(re, f->buff->str + start, &offset, NULL) != 0)
[72f613a]547    return -1;
548  return offset + start;
[1fd0b25]549}
[12c35df]550
551
552/* Append the text 'text' to 'f' and interpret the zephyr style
553 * formatting syntax to set appropriate attributes.
554 */
[e19eb97]555void owl_fmtext_append_ztext(owl_fmtext *f, const char *text)
[12c35df]556{
557  int stacksize, curattrs, curcolor;
[e19eb97]558  const char *ptr, *txtptr, *tmpptr;
[65b2173]559  char *buff;
[d754b0a]560  int attrstack[32], chrstack[32], colorstack[32];
[12c35df]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 */
[8fa9562]570      owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT);
[12c35df]571      return;
572    } else if (ptr[0]=='@') {
573      /* add the text up to this point then deal with the stack */
[96828e4]574      buff=g_new(char, ptr-txtptr+20);
[12c35df]575      strncpy(buff, txtptr, ptr-txtptr);
576      buff[ptr-txtptr]='\0';
[8fa9562]577      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[ddbbcffa]578      g_free(buff);
[12c35df]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) {
[1bdffcb]587        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
588        txtptr++;
589        continue;
[12c35df]590      }
591
592      /* if it's an @@, print an @ and continue */
593      if (txtptr[1]=='@') {
[1bdffcb]594        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
595        txtptr+=2;
596        continue;
[12c35df]597      }
[1bdffcb]598       
[12c35df]599      /* if there's no opener, print the @ and continue */
600      tmpptr=strpbrk(txtptr, "(<[{ ");
601      if (!tmpptr || tmpptr[0]==' ') {
[1bdffcb]602        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
603        txtptr++;
604        continue;
[12c35df]605      }
606
607      /* check what command we've got, push it on the stack, start
[1bdffcb]608         using it, and continue ... unless it's a color command */
[96828e4]609      buff=g_new(char, tmpptr-ptr+20);
[12c35df]610      strncpy(buff, ptr, tmpptr-ptr);
611      buff[tmpptr-ptr]='\0';
612      if (!strcasecmp(buff, "@bold")) {
[1bdffcb]613        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
614        chrstack[stacksize]=tmpptr[0];
[d754b0a]615        colorstack[stacksize]=curcolor;
[1bdffcb]616        stacksize++;
617        curattrs|=OWL_FMTEXT_ATTR_BOLD;
618        txtptr+=6;
[ddbbcffa]619        g_free(buff);
[1bdffcb]620        continue;
[12c35df]621      } else if (!strcasecmp(buff, "@b")) {
[1bdffcb]622        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
623        chrstack[stacksize]=tmpptr[0];
[d754b0a]624        colorstack[stacksize]=curcolor;
[1bdffcb]625        stacksize++;
626        curattrs|=OWL_FMTEXT_ATTR_BOLD;
627        txtptr+=3;
[ddbbcffa]628        g_free(buff);
[1bdffcb]629        continue;
[12c35df]630      } else if (!strcasecmp(buff, "@i")) {
[1bdffcb]631        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
632        chrstack[stacksize]=tmpptr[0];
[d754b0a]633        colorstack[stacksize]=curcolor;
[1bdffcb]634        stacksize++;
635        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
636        txtptr+=3;
[ddbbcffa]637        g_free(buff);
[1bdffcb]638        continue;
[12c35df]639      } else if (!strcasecmp(buff, "@italic")) {
[1bdffcb]640        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
641        chrstack[stacksize]=tmpptr[0];
[d754b0a]642        colorstack[stacksize]=curcolor;
[1bdffcb]643        stacksize++;
644        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
645        txtptr+=8;
[ddbbcffa]646        g_free(buff);
[1bdffcb]647        continue;
[d754b0a]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;
[ddbbcffa]654        g_free(buff);
[d754b0a]655        continue;
[1bdffcb]656
657        /* if it's a color read the color, set the current color and
[12c35df]658           continue */
659      } else if (!strcasecmp(buff, "@color") 
[1bdffcb]660                 && owl_global_is_colorztext(&g)) {
[ddbbcffa]661        g_free(buff);
[1bdffcb]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 */
[96828e4]671          buff=g_new(char, tmpptr-txtptr+20);
[1bdffcb]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);
[1ee5c79]677          if (curcolor == OWL_COLOR_INVALID)
678              curcolor = OWL_COLOR_DEFAULT;
[ddbbcffa]679          g_free(buff);
[1bdffcb]680          txtptr=tmpptr+1;
681          continue;
682
683        } else {
684
685        }
[12c35df]686
687      } else {
[1bdffcb]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         */
[d8f22b6]691        g_free(buff);
[1bdffcb]692        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
693        txtptr++;
694        continue;
[12c35df]695      }
696
697    } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') {
698      /* add the text up to this point first */
[96828e4]699      buff=g_new(char, ptr-txtptr+20);
[12c35df]700      strncpy(buff, txtptr, ptr-txtptr);
701      buff[ptr-txtptr]='\0';
[8fa9562]702      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[ddbbcffa]703      g_free(buff);
[12c35df]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) {
[96828e4]710        buff=g_new(char, 5);
[1bdffcb]711        buff[0]=ptr[0];
712        buff[1]='\0';
713        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[ddbbcffa]714        g_free(buff);
[1bdffcb]715        txtptr++;
716        continue;
[12c35df]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]=='(') ||
[1bdffcb]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;
[d754b0a]728        curcolor = colorstack[stacksize];
[1bdffcb]729        for (i=0; i<stacksize; i++) {
730          curattrs|=attrstack[i];
731        }
732        txtptr+=1;
733        continue;
[12c35df]734      } else {
[1bdffcb]735        /* otherwise print and continue */
[96828e4]736        buff=g_new(char, 5);
[1bdffcb]737        buff[0]=ptr[0];
738        buff[1]='\0';
739        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[ddbbcffa]740        g_free(buff);
[1bdffcb]741        txtptr++;
742        continue;
[12c35df]743      }
744    } else {
745      /* we've found an unattached opener, print everything and move on */
[96828e4]746      buff=g_new(char, ptr-txtptr+20);
[12c35df]747      strncpy(buff, txtptr, ptr-txtptr+1);
748      buff[ptr-txtptr+1]='\0';
[8fa9562]749      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[ddbbcffa]750      g_free(buff);
[12c35df]751      txtptr=ptr+1;
752      continue;
753    }
754  }
755}
[a0a5179]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. */
[ce68f23]761void owl_fmtext_append_list(owl_fmtext *f, const GPtrArray *l, const char *join_with, char *(format_fn)(const char *))
[a0a5179]762{
[ce68f23]763  int i;
[e19eb97]764  const char *elem;
[a0a5179]765  char *text;
766
[ce68f23]767  for (i = 0; i < l->len; i++) {
768    elem = l->pdata[i];
[a0a5179]769    if (elem && format_fn) {
770      text = format_fn(elem);
771      if (text) {
[1bdffcb]772        owl_fmtext_append_normal(f, text);
[ddbbcffa]773        g_free(text);
[a0a5179]774      }
775    } else if (elem) {
776      owl_fmtext_append_normal(f, elem);
777    }
[ce68f23]778    if ((i < l->len - 1) && join_with) {
[a0a5179]779      owl_fmtext_append_normal(f, join_with);
780    }
781  }
782}
783
784/* Free all memory allocated by the object */
[7ab0020]785void owl_fmtext_cleanup(owl_fmtext *f)
[a0a5179]786{
[7e111f4]787  if (f->buff) g_string_free(f->buff, true);
788  f->buff = NULL;
[8fa9562]789}
790
791/*** Color Pair manager ***/
792void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr)
793{
[af1920fd]794  /* This could be a bitarray if we wanted to save memory. */
[e062f97]795  short i;
[af1920fd]796  /* The test is <= because we allocate COLORS+1 entries. */
[96828e4]797  cpmgr->pairs = g_new(short *, COLORS + 1);
[c2c5c77]798  for(i = 0; i <= COLORS; i++) {
[96828e4]799    cpmgr->pairs[i] = g_new(short, COLORS + 1);
[c2c5c77]800  }
[e062f97]801  owl_fmtext_reset_colorpairs(cpmgr);
[a0a5179]802}
803
[8fa9562]804/* Reset used list */
[e062f97]805void owl_fmtext_reset_colorpairs(owl_colorpair_mgr *cpmgr)
[8fa9562]806{
[e062f97]807  short i, j;
808
[099597c]809  cpmgr->overflow = false;
[e062f97]810  cpmgr->next = 8;
[099597c]811  /* The test is <= because we allocated COLORS+1 entries. */
[e062f97]812  for(i = 0; i <= COLORS; i++) {
813    for(j = 0; j <= COLORS; j++) {
814      cpmgr->pairs[i][j] = -1;
[fa3290d]815    }
[e062f97]816  }
[7b4f3be]817  if (has_colors()) {
[fa3290d]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;
[eeeef20]823    }
[c2c5c77]824  }
[8fa9562]825}
826
827/* Assign pairs by request */
[1bdffcb]828short owl_fmtext_get_colorpair(int fg, int bg)
[8fa9562]829{
830  owl_colorpair_mgr *cpmgr;
[423adcb]831  short pair;
[1bdffcb]832
[7b4f3be]833  if (!has_colors())
834    return 0;
835
[1b737a1]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           
[1bdffcb]840#ifdef HAVE_USE_DEFAULT_COLORS
[8fa9562]841  if (fg == OWL_COLOR_DEFAULT) fg = -1;
[1bdffcb]842#else
843  if (fg == OWL_COLOR_DEFAULT) fg = 0;
844  if (bg == OWL_COLOR_DEFAULT) bg = 0;
845#endif
846
[af1920fd]847  /* looking for a pair we already set up for this draw. */
[c2c5c77]848  cpmgr = owl_global_get_colorpair_mgr(&g);
849  pair = cpmgr->pairs[fg+1][bg+1];
850  if (!(pair != -1 && pair < cpmgr->next)) {
[af1920fd]851    /* If we didn't find a pair, search for a free one to assign. */
[9efa5bd]852    pair = (cpmgr->next < owl_util_get_colorpairs()) ? cpmgr->next : -1;
[c2c5c77]853    if (pair != -1) {
[af1920fd]854      /* We found a free pair, initialize it. */
[c2c5c77]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) {
[af1920fd]860      /* We still don't have a pair, drop the background color. Too bad. */
[c2c5c77]861      owl_function_debugmsg("colorpairs: color shortage - dropping background color.");
[099597c]862      cpmgr->overflow = true;
[c2c5c77]863      pair = owl_fmtext_get_colorpair(fg, OWL_COLOR_DEFAULT);
864    }
865    else {
[af1920fd]866      /* We still don't have a pair, defaults all around. */
[c2c5c77]867      owl_function_debugmsg("colorpairs: color shortage - dropping foreground and background color.");
[099597c]868      cpmgr->overflow = true;
[c2c5c77]869      pair = 0;
[8fa9562]870    }
871  }
872  return pair;
873}
Note: See TracBrowser for help on using the repository browser.