source: fmtext.c @ c471e85

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