source: fmtext.c @ 94ef58c

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