source: fmtext.c @ db1af5fa

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