source: fmtext.c @ 4d86e06

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 4d86e06 was 4d86e06, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Get rid of a whole bunch of useless casts. 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 */
[9866c3a]45void owl_fmtext_append_attr(owl_fmtext *f, 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' */
[5789230]85void owl_fmtext_append_normal(owl_fmtext *f, char *text)
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' */
91void owl_fmtext_appendf_normal(owl_fmtext *f, char *fmt, ...)
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' */
[8fa9562]105void owl_fmtext_append_normal_color(owl_fmtext *f, 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' */
[5789230]111void owl_fmtext_append_bold(owl_fmtext *f, char *text)
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' */
[5789230]117void owl_fmtext_append_reverse(owl_fmtext *f, char *text)
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' */
[5789230]123void owl_fmtext_append_reversebold(owl_fmtext *f, char *text)
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. */
172void _owl_fmtext_scan_attributes(owl_fmtext *f, int start, char *attr, short *fgcolor, short *bgcolor) /*noproto*/
173{
174  char *p;
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 */
[9866c3a]186void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop) /*noproto*/
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' */
[5789230]232void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in)
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 */
[5789230]250char *owl_fmtext_print_plain(owl_fmtext *f)
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 */
[47519e1b]280void _owl_fmtext_curs_waddstr(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  int search_results, search_len;
288 
[f2f9314]289  if (w==NULL) {
290    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
291    return;
292  }
293
[9866c3a]294  search_results = (do_search
295                    ? owl_fmtext_search(f, owl_global_get_search_string(&g))
[47519e1b]296                    : 0);
297  search_len = (search_results
298                ? strlen(owl_global_get_search_string(&g))
299                : 0);
[9866c3a]300  s = f->textbuff;
301  /* Set default attributes. */
302  attr = f->default_attrs;
303  fg = f->default_fgcolor;
304  bg = f->default_bgcolor;
305  _owl_fmtext_wattrset(w, attr);
[16c6cca]306  _owl_fmtext_update_colorpair(fg, bg, &pair);
307  _owl_fmtext_wcolor_set(w, pair);
[7d4fbcd]308
[9866c3a]309  /* Find next possible format character. */
310  p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
311  while(p) {
[c1522ec]312    if (owl_fmtext_is_format_char(g_utf8_get_char(p))) {
[9866c3a]313      /* Deal with all text from last insert to here. */
314      char tmp;
315   
316      tmp = p[0];
317      p[0] = '\0';
318      if (search_results) {
319        /* Search is active, so highlight search results. */
320        char tmp2, *ss;
321        ss = stristr(s, owl_global_get_search_string(&g));
322        while (ss) {
323          /* Found search string, highlight it. */
324
325          tmp2 = ss[0];
326          ss[0] = '\0';
327          waddstr(w, s);
328          ss[0] = tmp2;
329
[987a93c]330          _owl_fmtext_wattrset(w, attr ^ OWL_FMTEXT_ATTR_REVERSE);
[16c6cca]331          _owl_fmtext_wcolor_set(w, pair);
332         
[9866c3a]333          tmp2 = ss[search_len];
334          ss[search_len] = '\0';
335          waddstr(w, ss);
336          ss[search_len] = tmp2;
337
[16c6cca]338          _owl_fmtext_wattrset(w, attr);
339          _owl_fmtext_wcolor_set(w, pair);
[9866c3a]340
341          s = ss + search_len;
342          ss = stristr(s, owl_global_get_search_string(&g));
343        }
344      }
345      /* Deal with remaining part of string. */
346      waddstr(w, s);
347      p[0] = tmp;
348
349      /* Deal with new attributes. Initialize to defaults, then
350         process all consecutive formatting characters. */
351      attr = f->default_attrs;
352      fg = f->default_fgcolor;
353      bg = f->default_bgcolor;
[c1522ec]354      while (p && owl_fmtext_is_format_char(g_utf8_get_char(p))) {
[9866c3a]355        _owl_fmtext_update_attributes(g_utf8_get_char(p), &attr, &fg, &bg);
356        p = g_utf8_next_char(p);
[7d4fbcd]357      }
[9866c3a]358      _owl_fmtext_wattrset(w, attr | f->default_attrs);
[16c6cca]359      if (fg == OWL_COLOR_DEFAULT) fg = f->default_fgcolor;
360      if (bg == OWL_COLOR_DEFAULT) bg = f->default_bgcolor;
361      _owl_fmtext_update_colorpair(fg, bg, &pair);
362      _owl_fmtext_wcolor_set(w, pair);
363
[9866c3a]364      /* Advance to next non-formatting character. */
365      s = p;
366      p = strchr(s, OWL_FMTEXT_UC_STARTBYTE_UTF8);
367    }
368    else {
369      p = strchr(p+1, OWL_FMTEXT_UC_STARTBYTE_UTF8);
[7d4fbcd]370    }
371  }
[9866c3a]372  if (s) {
373    waddstr(w, s);
374  }
[7d4fbcd]375}
376
[47519e1b]377void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w)
378{
379  _owl_fmtext_curs_waddstr(f, w, owl_global_is_search_active(&g));
380}
381
382void owl_fmtext_curs_waddstr_without_search(owl_fmtext *f, WINDOW *w)
383{
384  _owl_fmtext_curs_waddstr(f, w, 0);
385}
[7d4fbcd]386
[b2b0773]387/* start with line 'aline' (where the first line is 0) and print
388 * 'lines' number of lines into 'out'
389 */
[5789230]390int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out)
391{
[7d4fbcd]392  char *ptr1, *ptr2;
393  int i, offset;
394 
395  /* find the starting line */
[9866c3a]396  ptr1 = in->textbuff;
397  for (i = 0; i < aline; i++) {
398    ptr1 = strchr(ptr1, '\n');
399    if (!ptr1) return(-1);
400    ptr1++;
[7d4fbcd]401  }
[9866c3a]402 
[7d4fbcd]403  /* ptr1 now holds the starting point */
404
[c55ad84]405  /* copy the default attributes */
406  out->default_attrs = in->default_attrs;
407  out->default_fgcolor = in->default_fgcolor;
408  out->default_bgcolor = in->default_bgcolor;
409   
[7d4fbcd]410  /* copy in the next 'lines' lines */
[9866c3a]411  if (lines < 1) return(-1);
[7d4fbcd]412
[9866c3a]413  for (i = 0; i < lines; i++) {
414    offset = ptr1 - in->textbuff;
415    ptr2 = strchr(ptr1, '\n');
[7d4fbcd]416    if (!ptr2) {
[9866c3a]417      _owl_fmtext_append_fmtext(out, in, offset, (in->textlen) - 1);
[7d4fbcd]418      return(-1);
419    }
[9866c3a]420    _owl_fmtext_append_fmtext(out, in, offset, (ptr2 - ptr1) + offset);
421    ptr1 = ptr2 + 1;
[7d4fbcd]422  }
423  return(0);
424}
425
[b2b0773]426/* Truncate the message so that each line begins at column 'acol' and
427 * ends at 'bcol' or sooner.  The first column is number 0.  The new
428 * message is placed in 'out'.  The message is * expected to end in a
[3a7cf49]429 * new line for now. NOTE: This needs to be modified to deal with
430 * backing up if we find a SPACING COMBINING MARK at the end of a
431 * line. If that happens, we should back up to the last non-mark
432 * character and stop there.
[b2b0773]433 */
[5789230]434void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
435{
[28ee32b]436  char *ptr_s, *ptr_e, *ptr_c, *last;
[9866c3a]437  int col, st, padding, chwidth;
[7d4fbcd]438
[c55ad84]439  /* copy the default attributes */
440  out->default_attrs = in->default_attrs;
441  out->default_fgcolor = in->default_fgcolor;
442  out->default_bgcolor = in->default_bgcolor;
443
[7d4fbcd]444  last=in->textbuff+in->textlen-1;
[28ee32b]445  ptr_s=in->textbuff;
[9866c3a]446  while (ptr_s <= last) {
[28ee32b]447    ptr_e=strchr(ptr_s, '\n');
448    if (!ptr_e) {
[7d4fbcd]449      /* but this shouldn't happen if we end in a \n */
450      break;
451    }
452   
[9866c3a]453    if (ptr_e == ptr_s) {
[7d4fbcd]454      owl_fmtext_append_normal(out, "\n");
[9866c3a]455      ++ptr_s;
[7d4fbcd]456      continue;
457    }
458
[28ee32b]459    col = 0;
[9866c3a]460    st = 0;
[dd24b6a]461    padding = 0;
[47519e1b]462    chwidth = 0;
[28ee32b]463    ptr_c = ptr_s;
[3a7cf49]464    while(ptr_c < ptr_e) {
[28ee32b]465      gunichar c = g_utf8_get_char(ptr_c);
[c1522ec]466      if (!owl_fmtext_is_format_char(c)) {
[47519e1b]467        chwidth = mk_wcwidth(c);
[3a7cf49]468        if (col + chwidth > bcol) break;
469       
[9866c3a]470        if (col >= acol) {
471          if (st == 0) {
472            ptr_s = ptr_c;
473            padding = col - acol;
474            ++st;
475          }
[dd24b6a]476        }
[9866c3a]477        col += chwidth;
478        chwidth = 0;
[28ee32b]479      }
[9866c3a]480      ptr_c = g_utf8_next_char(ptr_c);
[dd24b6a]481    }
[9866c3a]482    if (st) {
483      /* lead padding */
484      owl_fmtext_append_spaces(out, padding);
[3a7cf49]485      if (ptr_c == ptr_e) {
486        /* We made it to the newline. */
487        _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff);
[9866c3a]488      }
[db1af5f]489      else if (chwidth > 1) {
490        /* Last char is wide, truncate. */
491        _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1);
492        owl_fmtext_append_normal(out, "\n");
493      }
[9866c3a]494      else {
[db1af5f]495        /* Last char fits perfectly, We skip to the next char and back
496         * up a byte to make sure we get it all.
497         */
498        ptr_c = g_utf8_next_char(ptr_c);
499        _owl_fmtext_append_fmtext(out, in, ptr_s - in->textbuff, ptr_c - in->textbuff - 1);
[28ee32b]500      }
[dd24b6a]501    }
502    else {
503      owl_fmtext_append_normal(out, "\n");
[28ee32b]504    }
[9866c3a]505    ptr_s = g_utf8_next_char(ptr_e);
[7d4fbcd]506  }
507}
508
[b2b0773]509/* Return the number of lines in 'f' */
[5789230]510int owl_fmtext_num_lines(owl_fmtext *f)
511{
[7d4fbcd]512  int lines, i;
513
[af2ca19]514  if (f->textlen==0) return(0);
515 
[7d4fbcd]516  lines=0;
[af2ca19]517  for (i=0; i<f->textlen; i++) {
[7d4fbcd]518    if (f->textbuff[i]=='\n') lines++;
519  }
520
521  /* if the last char wasn't a \n there's one more line */
522  if (f->textbuff[i-1]!='\n') lines++;
523
524  return(lines);
525}
526
[5789230]527char *owl_fmtext_get_text(owl_fmtext *f)
528{
[7d4fbcd]529  return(f->textbuff);
530}
531
[a0a5179]532/* set the charater at 'index' to be 'char'.  If index is out of
[9866c3a]533 * bounds don't do anything. If c or char at index is not ASCII, don't
534 * do anything because it's not UTF-8 safe. */
535void owl_fmtext_set_char(owl_fmtext *f, int index, char ch)
[5789230]536{
[4b464a4]537  if ((index < 0) || (index > f->textlen-1)) return;
[9866c3a]538  /* NOT ASCII*/
539  if (f->textbuff[index] & 0x80 || ch & 0x80) return; 
[4b464a4]540  f->textbuff[index]=ch;
541}
542
[b2b0773]543/* Make a copy of the fmtext 'src' into 'dst' */
[5789230]544void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src)
545{
[b2b0773]546  int mallocsize;
547
548  if (src->textlen==0) {
549    mallocsize=5;
550  } else {
551    mallocsize=src->textlen+2;
552  }
[7d4fbcd]553  dst->textlen=src->textlen;
[005fc22]554  dst->bufflen=mallocsize;
[bd3f232]555  dst->textbuff=owl_malloc(mallocsize);
[6bf73ce]556  memcpy(dst->textbuff, src->textbuff, src->textlen+1);
[9866c3a]557  dst->default_attrs = src->default_attrs;
558  dst->default_fgcolor = src->default_fgcolor;
559  dst->default_bgcolor = src->default_bgcolor;
[1fd0b25]560}
561
[b2b0773]562/* return 1 if the string is found, 0 if not.  This is a case
563 *  insensitive search.
[5789230]564 */
565int owl_fmtext_search(owl_fmtext *f, char *string)
566{
[1fd0b25]567  if (stristr(f->textbuff, string)) return(1);
568  return(0);
569}
[12c35df]570
571
572/* Append the text 'text' to 'f' and interpret the zephyr style
573 * formatting syntax to set appropriate attributes.
574 */
575void owl_fmtext_append_ztext(owl_fmtext *f, char *text)
576{
577  int stacksize, curattrs, curcolor;
578  char *ptr, *txtptr, *buff, *tmpptr;
[d754b0a]579  int attrstack[32], chrstack[32], colorstack[32];
[12c35df]580
581  curattrs=OWL_FMTEXT_ATTR_NONE;
582  curcolor=OWL_COLOR_DEFAULT;
583  stacksize=0;
584  txtptr=text;
585  while (1) {
586    ptr=strpbrk(txtptr, "@{[<()>]}");
587    if (!ptr) {
588      /* add all the rest of the text and exit */
[8fa9562]589      owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT);
[12c35df]590      return;
591    } else if (ptr[0]=='@') {
592      /* add the text up to this point then deal with the stack */
593      buff=owl_malloc(ptr-txtptr+20);
594      strncpy(buff, txtptr, ptr-txtptr);
595      buff[ptr-txtptr]='\0';
[8fa9562]596      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[12c35df]597      owl_free(buff);
598
599      /* update pointer to point at the @ */
600      txtptr=ptr;
601
602      /* now the stack */
603
604      /* if we've hit our max stack depth, print the @ and move on */
605      if (stacksize==32) {
[1bdffcb]606        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
607        txtptr++;
608        continue;
[12c35df]609      }
610
611      /* if it's an @@, print an @ and continue */
612      if (txtptr[1]=='@') {
[1bdffcb]613        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
614        txtptr+=2;
615        continue;
[12c35df]616      }
[1bdffcb]617       
[12c35df]618      /* if there's no opener, print the @ and continue */
619      tmpptr=strpbrk(txtptr, "(<[{ ");
620      if (!tmpptr || tmpptr[0]==' ') {
[1bdffcb]621        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
622        txtptr++;
623        continue;
[12c35df]624      }
625
626      /* check what command we've got, push it on the stack, start
[1bdffcb]627         using it, and continue ... unless it's a color command */
[12c35df]628      buff=owl_malloc(tmpptr-ptr+20);
629      strncpy(buff, ptr, tmpptr-ptr);
630      buff[tmpptr-ptr]='\0';
631      if (!strcasecmp(buff, "@bold")) {
[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+=6;
638        owl_free(buff);
639        continue;
[12c35df]640      } else if (!strcasecmp(buff, "@b")) {
[1bdffcb]641        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
642        chrstack[stacksize]=tmpptr[0];
[d754b0a]643        colorstack[stacksize]=curcolor;
[1bdffcb]644        stacksize++;
645        curattrs|=OWL_FMTEXT_ATTR_BOLD;
646        txtptr+=3;
647        owl_free(buff);
648        continue;
[12c35df]649      } else if (!strcasecmp(buff, "@i")) {
[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+=3;
656        owl_free(buff);
657        continue;
[12c35df]658      } else if (!strcasecmp(buff, "@italic")) {
[1bdffcb]659        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
660        chrstack[stacksize]=tmpptr[0];
[d754b0a]661        colorstack[stacksize]=curcolor;
[1bdffcb]662        stacksize++;
663        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
664        txtptr+=8;
665        owl_free(buff);
666        continue;
[d754b0a]667      } else if (!strcasecmp(buff, "@")) {
668        attrstack[stacksize]=OWL_FMTEXT_ATTR_NONE;
669        chrstack[stacksize]=tmpptr[0];
670        colorstack[stacksize]=curcolor;
671        stacksize++;
672        txtptr+=2;
673        owl_free(buff);
674        continue;
[1bdffcb]675
676        /* if it's a color read the color, set the current color and
[12c35df]677           continue */
678      } else if (!strcasecmp(buff, "@color") 
[1bdffcb]679                 && owl_global_get_hascolors(&g)
680                 && owl_global_is_colorztext(&g)) {
681        owl_free(buff);
682        txtptr+=7;
683        tmpptr=strpbrk(txtptr, "@{[<()>]}");
684        if (tmpptr &&
685            ((txtptr[-1]=='(' && tmpptr[0]==')') ||
686             (txtptr[-1]=='<' && tmpptr[0]=='>') ||
687             (txtptr[-1]=='[' && tmpptr[0]==']') ||
688             (txtptr[-1]=='{' && tmpptr[0]=='}'))) {
689
690          /* grab the color name */
691          buff=owl_malloc(tmpptr-txtptr+20);
692          strncpy(buff, txtptr, tmpptr-txtptr);
693          buff[tmpptr-txtptr]='\0';
694
695          /* set it as the current color */
696          curcolor=owl_util_string_to_color(buff);
697          if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT;
698          owl_free(buff);
699          txtptr=tmpptr+1;
700          continue;
701
702        } else {
703
704        }
[12c35df]705
706      } else {
[1bdffcb]707        /* if we didn't understand it, we'll print it.  This is different from zwgc
708         * but zwgc seems to be smarter about some screw cases than I am
709         */
710        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
711        txtptr++;
712        continue;
[12c35df]713      }
714
715    } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') {
716      /* add the text up to this point first */
717      buff=owl_malloc(ptr-txtptr+20);
718      strncpy(buff, txtptr, ptr-txtptr);
719      buff[ptr-txtptr]='\0';
[8fa9562]720      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[12c35df]721      owl_free(buff);
722
723      /* now deal with the closer */
724      txtptr=ptr;
725
726      /* first, if the stack is empty we must bail (just print and go) */
727      if (stacksize==0) {
[1bdffcb]728        buff=owl_malloc(5);
729        buff[0]=ptr[0];
730        buff[1]='\0';
731        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
732        owl_free(buff);
733        txtptr++;
734        continue;
[12c35df]735      }
736
737      /* if the closing char is what's on the stack, turn off the
738         attribue and pop the stack */
739      if ((ptr[0]==')' && chrstack[stacksize-1]=='(') ||
[1bdffcb]740          (ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
741          (ptr[0]==']' && chrstack[stacksize-1]=='[') ||
742          (ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
743        int i;
744        stacksize--;
745        curattrs=OWL_FMTEXT_ATTR_NONE;
[d754b0a]746        curcolor = colorstack[stacksize];
[1bdffcb]747        for (i=0; i<stacksize; i++) {
748          curattrs|=attrstack[i];
749        }
750        txtptr+=1;
751        continue;
[12c35df]752      } else {
[1bdffcb]753        /* otherwise print and continue */
754        buff=owl_malloc(5);
755        buff[0]=ptr[0];
756        buff[1]='\0';
757        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
758        owl_free(buff);
759        txtptr++;
760        continue;
[12c35df]761      }
762    } else {
763      /* we've found an unattached opener, print everything and move on */
764      buff=owl_malloc(ptr-txtptr+20);
765      strncpy(buff, txtptr, ptr-txtptr+1);
766      buff[ptr-txtptr+1]='\0';
[8fa9562]767      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
[12c35df]768      owl_free(buff);
769      txtptr=ptr+1;
770      continue;
771    }
772  }
773}
[a0a5179]774
775/* requires that the list values are strings or NULL.
776 * joins the elements together with join_with.
777 * If format_fn is specified, passes it the list element value
778 * and it will return a string which this needs to free. */
[2aaca94]779void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(char *))
[a0a5179]780{
781  int i, size;
[2aaca94]782  char *elem;
[a0a5179]783  char *text;
784
785  size = owl_list_get_size(l);
786  for (i=0; i<size; i++) {
[4d86e06]787    elem = owl_list_get_element(l,i);
[a0a5179]788    if (elem && format_fn) {
789      text = format_fn(elem);
790      if (text) {
[1bdffcb]791        owl_fmtext_append_normal(f, text);
792        owl_free(text);
[a0a5179]793      }
794    } else if (elem) {
795      owl_fmtext_append_normal(f, elem);
796    }
797    if ((i < size-1) && join_with) {
798      owl_fmtext_append_normal(f, join_with);
799    }
800  }
801}
802
803/* Free all memory allocated by the object */
804void owl_fmtext_free(owl_fmtext *f)
805{
806  if (f->textbuff) owl_free(f->textbuff);
[8fa9562]807}
808
809/*** Color Pair manager ***/
810void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr)
811{
[af1920fd]812  /* This could be a bitarray if we wanted to save memory. */
[c2c5c77]813  short i, j;
814  cpmgr->next = 8;
815 
[af1920fd]816  /* The test is <= because we allocate COLORS+1 entries. */
[c2c5c77]817  cpmgr->pairs = owl_malloc((COLORS+1) * sizeof(short*));
818  for(i = 0; i <= COLORS; i++) {
819    cpmgr->pairs[i] = owl_malloc((COLORS+1) * sizeof(short));
820    for(j = 0; j <= COLORS; j++) {
[8fa9562]821      cpmgr->pairs[i][j] = -1;
822    }
823  }
[fa3290d]824  if (owl_global_get_hascolors(&g)) {
825    for(i = 0; i < 8; i++) {
826      short fg, bg;
827      if (i >= COLORS) continue;
828      pair_content(i, &fg, &bg);
829      cpmgr->pairs[fg+1][bg+1] = i;
830    }
[c2c5c77]831  }
[a0a5179]832}
833
[8fa9562]834/* Reset used list */
835void owl_fmtext_reset_colorpairs()
836{
[fa3290d]837  if (owl_global_get_hascolors(&g)) {
838    short i, j;
839    owl_colorpair_mgr *cpmgr = owl_global_get_colorpair_mgr(&g);
840    cpmgr->next = 8;
841   
[af1920fd]842    /* The test is <= because we allocated COLORS+1 entries. */
[fa3290d]843    for(i = 0; i <= COLORS; i++) {
844      for(j = 0; j <= COLORS; j++) {
845        cpmgr->pairs[i][j] = -1;
846      }
847    }
848    for(i = 0; i < 8; i++) {
849      short fg, bg;
850      if (i >= COLORS) continue;
851      pair_content(i, &fg, &bg);
852      cpmgr->pairs[fg+1][bg+1] = i;
[eeeef20]853    }
[c2c5c77]854  }
[8fa9562]855}
856
857/* Assign pairs by request */
[1bdffcb]858short owl_fmtext_get_colorpair(int fg, int bg)
[8fa9562]859{
860  owl_colorpair_mgr *cpmgr;
[801b7ac]861  short pair, default_bg;
[1bdffcb]862
[1b737a1]863  /* Sanity (Bounds) Check */
864  if (fg > COLORS || fg < OWL_COLOR_DEFAULT) fg = OWL_COLOR_DEFAULT;
865  if (bg > COLORS || bg < OWL_COLOR_DEFAULT) bg = OWL_COLOR_DEFAULT;
866           
[1bdffcb]867#ifdef HAVE_USE_DEFAULT_COLORS
[8fa9562]868  if (fg == OWL_COLOR_DEFAULT) fg = -1;
[1bdffcb]869  default_bg = OWL_COLOR_DEFAULT;
870#else
871  if (fg == OWL_COLOR_DEFAULT) fg = 0;
872  if (bg == OWL_COLOR_DEFAULT) bg = 0;
873  default_bg = COLOR_BLACK;
874#endif
875
[af1920fd]876  /* looking for a pair we already set up for this draw. */
[c2c5c77]877  cpmgr = owl_global_get_colorpair_mgr(&g);
878  pair = cpmgr->pairs[fg+1][bg+1];
879  if (!(pair != -1 && pair < cpmgr->next)) {
[af1920fd]880    /* If we didn't find a pair, search for a free one to assign. */
[c2c5c77]881    pair = (cpmgr->next < COLOR_PAIRS) ? cpmgr->next : -1;
882    if (pair != -1) {
[af1920fd]883      /* We found a free pair, initialize it. */
[c2c5c77]884      init_pair(pair, fg, bg);
885      cpmgr->pairs[fg+1][bg+1] = pair;
886      cpmgr->next++;
887    }
888    else if (bg != OWL_COLOR_DEFAULT) {
[af1920fd]889      /* We still don't have a pair, drop the background color. Too bad. */
[c2c5c77]890      owl_function_debugmsg("colorpairs: color shortage - dropping background color.");
891      pair = owl_fmtext_get_colorpair(fg, OWL_COLOR_DEFAULT);
892    }
893    else {
[af1920fd]894      /* We still don't have a pair, defaults all around. */
[c2c5c77]895      owl_function_debugmsg("colorpairs: color shortage - dropping foreground and background color.");
896      pair = 0;
[8fa9562]897    }
898  }
899  return pair;
900}
Note: See TracBrowser for help on using the repository browser.