source: fmtext.c @ 85d1795

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 85d1795 was c2c5c77, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 18 years ago
Adding 256-color support. This requires a version of ncurses that supports ABI-6. Colors beyond the first eight are refered to by number. Perl now has the number of colors exposed to it by way of BarnOwl::getnumcolors() and also has a mechanism for undefining filters using BarnOwl::_remove_filter([filter-name]) You can't remove the 'all' filter or the current filter.
  • Property mode set to 100644
File size: 20.8 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <string.h>
4
5static const char fileIdent[] = "$Id$";
6
7/* initialize an fmtext with no data */
8void owl_fmtext_init_null(owl_fmtext *f)
9{
10  f->textlen=0;
11  f->bufflen=5;
12  f->textbuff=owl_malloc(5);
13  f->fmbuff=owl_malloc(5);
14  f->fgcolorbuff=owl_malloc(5 * sizeof(short));
15  f->bgcolorbuff=owl_malloc(5 * sizeof(short));
16  f->textbuff[0]=0;
17  f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE;
18  f->fgcolorbuff[0]=OWL_COLOR_DEFAULT;
19  f->bgcolorbuff[0]=OWL_COLOR_DEFAULT;
20}
21
22/* Clear the data from an fmtext, but don't deallocate memory. This
23   fmtext can then be appended to again. */
24void owl_fmtext_clear(owl_fmtext *f)
25{
26    f->textlen = 0;
27    f->textbuff[0] = 0;
28    f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE;
29    f->fgcolorbuff[0]=OWL_COLOR_DEFAULT;
30    f->bgcolorbuff[0]=OWL_COLOR_DEFAULT;
31}
32
33/* Internal function.  Set the attribute 'attr' from index 'first' to
34 * index 'last'
35 */
36void _owl_fmtext_set_attr(owl_fmtext *f, int attr, int first, int last)
37{
38  int i;
39  for (i=first; i<=last; i++) {
40    f->fmbuff[i]=(unsigned char) attr;
41  }
42}
43
44/* Internal function.  Add the attribute 'attr' to the existing
45 * attributes from index 'first' to index 'last'
46 */
47void _owl_fmtext_add_attr(owl_fmtext *f, int attr, int first, int last)
48{
49  int i;
50  for (i=first; i<=last; i++) {
51    f->fmbuff[i]|=(unsigned char) attr;
52  }
53}
54
55/* Internal function.  Set the color to be 'color' from index 'first'
56 * to index 'last
57 */
58void _owl_fmtext_set_fgcolor(owl_fmtext *f, int color, int first, int last)
59{
60  int i;
61  for (i=first; i<=last; i++) {
62    f->fgcolorbuff[i]=(short)color;
63  }
64}
65
66void _owl_fmtext_set_bgcolor(owl_fmtext *f, int color, int first, int last)
67{
68  int i;
69  for (i=first; i<=last; i++) {
70    f->bgcolorbuff[i]=(short)color;
71  }
72}
73
74void _owl_fmtext_realloc(owl_fmtext *f, int newlen) /*noproto*/
75{
76    if(newlen + 1 > f->bufflen) {
77      f->textbuff=owl_realloc(f->textbuff, newlen+1);
78      f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
79      f->fgcolorbuff=owl_realloc(f->fgcolorbuff, (newlen+1) * sizeof(short));
80      f->bgcolorbuff=owl_realloc(f->bgcolorbuff, (newlen+1) * sizeof(short));
81      f->bufflen = newlen+1;
82  }
83}
84
85/* append text to the end of 'f' with attribute 'attr' and color
86 * 'color'
87 */
88void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int fgcolor, int bgcolor)
89{
90  int newlen;
91  newlen=strlen(f->textbuff)+strlen(text);
92  _owl_fmtext_realloc(f, newlen);
93 
94  strcat(f->textbuff, text);
95  _owl_fmtext_set_attr(f, attr, f->textlen, newlen);
96  _owl_fmtext_set_fgcolor(f, fgcolor, f->textlen, newlen);
97  _owl_fmtext_set_bgcolor(f, bgcolor, f->textlen, newlen);
98  f->textlen=newlen;
99}
100
101/* Append normal, uncolored text 'text' to 'f' */
102void owl_fmtext_append_normal(owl_fmtext *f, char *text)
103{
104  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
105}
106
107/* Append normal text 'text' to 'f' with color 'color' */
108void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int fgcolor, int bgcolor)
109{
110  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, fgcolor, bgcolor);
111}
112
113/* Append bold text 'text' to 'f' */
114void owl_fmtext_append_bold(owl_fmtext *f, char *text)
115{
116  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
117}
118
119/* Append reverse video text 'text' to 'f' */
120void owl_fmtext_append_reverse(owl_fmtext *f, char *text)
121{
122  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
123}
124
125/* Append reversed and bold, uncolored text 'text' to 'f' */
126void owl_fmtext_append_reversebold(owl_fmtext *f, char *text)
127{
128  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT, OWL_COLOR_DEFAULT);
129}
130
131/* Add the attribute 'attr' to all text in 'f' */
132void owl_fmtext_addattr(owl_fmtext *f, int attr)
133{
134  /* add the attribute to all text */
135  int i, j;
136
137  j=f->textlen;
138  for (i=0; i<j; i++) {
139    f->fmbuff[i] |= attr;
140  }
141}
142
143/* Anywhere the color is NOT ALREDY SET, set the color to 'color'.
144 * Other colors are left unchanged
145 */
146void owl_fmtext_colorize(owl_fmtext *f, int color)
147{
148  /* everywhere the fgcolor is OWL_COLOR_DEFAULT, change it to be 'color' */
149  int i, j;
150
151  j=f->textlen;
152  for(i=0; i<j; i++) {
153    if (f->fgcolorbuff[i]==OWL_COLOR_DEFAULT) f->fgcolorbuff[i] = (short)color;
154  }
155}
156
157void owl_fmtext_colorizebg(owl_fmtext *f, int color)
158{
159  /* everywhere the bgcolor is OWL_COLOR_DEFAULT, change it to be 'color' */
160  int i, j;
161
162  j=f->textlen;
163  for(i=0; i<j; i++) {
164    if (f->bgcolorbuff[i]==OWL_COLOR_DEFAULT) f->bgcolorbuff[i] = (short)color;
165  }
166}
167
168/* Internal function.  Append text from 'in' between index 'start' and
169 * 'stop' to the end of 'f'
170 */
171void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop)
172{
173  int newlen, i;
174
175  newlen=strlen(f->textbuff)+(stop-start+1);
176  _owl_fmtext_realloc(f, newlen);
177
178  strncat(f->textbuff, in->textbuff+start, stop-start+1);
179  f->textbuff[newlen]='\0';
180  for (i=start; i<=stop; i++) {
181    f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i];
182    f->fgcolorbuff[f->textlen+(i-start)]=in->fgcolorbuff[i];
183    f->bgcolorbuff[f->textlen+(i-start)]=in->bgcolorbuff[i];
184  }
185  f->textlen=newlen;
186}
187
188/* append fmtext 'in' to 'f' */
189void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in)
190{
191  _owl_fmtext_append_fmtext(f, in, 0, in->textlen);
192
193}
194
195/* Append 'nspaces' number of spaces to the end of 'f' */
196void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces)
197{
198  int i;
199  for (i=0; i<nspaces; i++) {
200    owl_fmtext_append_normal(f, " ");
201  }
202}
203
204/* Return a plain version of the fmtext.  Caller is responsible for
205 * freeing the return
206 */
207char *owl_fmtext_print_plain(owl_fmtext *f)
208{
209  return(owl_strdup(f->textbuff));
210}
211
212/* add the formatted text to the curses window 'w'.  The window 'w'
213 * must already be initiatlized with curses
214 */
215void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w)
216{
217  char *tmpbuff;
218  int position, trans1, trans2, trans3, len, lastsame;
219
220  if (w==NULL) {
221    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
222    return;
223  }
224
225  tmpbuff=owl_malloc(f->textlen+10);
226
227  position=0;
228  len=f->textlen;
229  while (position<=len) {
230    /* find the last char with the current format and color */
231    trans1=owl_util_find_trans(f->fmbuff+position, len-position);
232    trans2=owl_util_find_trans_short(f->fgcolorbuff+position, len-position);
233    trans3=owl_util_find_trans_short(f->bgcolorbuff+position, len-position);
234
235    lastsame = (trans1 < trans2) ? trans1 : trans2;
236    lastsame = (lastsame < trans3) ? lastsame : trans3;
237    lastsame += position;
238
239    /* set the format */
240    wattrset(w, A_NORMAL);
241    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_BOLD) {
242      wattron(w, A_BOLD);
243    }
244    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_REVERSE) {
245      wattron(w, A_REVERSE);
246    }
247    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_UNDERLINE) {
248      wattron(w, A_UNDERLINE);
249    }
250
251    /* set the color */
252    /* warning, this is sort of a hack */
253    if (owl_global_get_hascolors(&g)) {
254      short fg, bg, pair;
255      fg = f->fgcolorbuff[position];
256      bg = f->bgcolorbuff[position];
257
258      pair = owl_fmtext_get_colorpair(fg, bg);
259      if (pair != -1) {
260        wcolor_set(w,pair,NULL);
261      }
262    }
263
264    /* add the text */
265    strncpy(tmpbuff, f->textbuff + position, lastsame-position+1);
266    tmpbuff[lastsame-position+1]='\0';
267    waddstr(w, tmpbuff);
268
269    position=lastsame+1;
270  }
271  owl_free(tmpbuff);
272}
273
274
275/* start with line 'aline' (where the first line is 0) and print
276 * 'lines' number of lines into 'out'
277 */
278int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out)
279{
280  char *ptr1, *ptr2;
281  int i, offset;
282 
283  /* find the starting line */
284  ptr1=in->textbuff;
285  if (aline!=0) {
286    for (i=0; i<aline; i++) {
287      ptr1=strchr(ptr1, '\n');
288      if (!ptr1) return(-1);
289      ptr1++;
290    }
291  }
292  /* ptr1 now holds the starting point */
293
294  /* copy in the next 'lines' lines */
295  if (lines<1) return(-1);
296
297  for (i=0; i<lines; i++) {
298    offset=ptr1-in->textbuff;
299    ptr2=strchr(ptr1, '\n');
300    if (!ptr2) {
301      _owl_fmtext_append_fmtext(out, in, offset, (in->textlen)-1);
302      return(-1);
303    }
304    _owl_fmtext_append_fmtext(out, in, offset, (ptr2-ptr1)+offset);
305    ptr1=ptr2+1;
306  }
307  return(0);
308}
309
310/* Truncate the message so that each line begins at column 'acol' and
311 * ends at 'bcol' or sooner.  The first column is number 0.  The new
312 * message is placed in 'out'.  The message is * expected to end in a
313 * new line for now
314 */
315void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
316{
317  char *ptr1, *ptr2, *last;
318  int len, offset;
319
320  last=in->textbuff+in->textlen-1;
321  ptr1=in->textbuff;
322  while (ptr1<=last) {
323    ptr2=strchr(ptr1, '\n');
324    if (!ptr2) {
325      /* but this shouldn't happen if we end in a \n */
326      break;
327    }
328   
329    if (ptr2==ptr1) {
330      owl_fmtext_append_normal(out, "\n");
331      ptr1++;
332      continue;
333    }
334
335    /* we need to check that we won't run over here */
336    len=bcol-acol;
337    if (len > (ptr2-(ptr1+acol))) {
338      /* the whole line fits with room to spare, don't take a full 'len' */
339      len=ptr2-(ptr1+acol);
340    }
341    if (len>last-ptr1) {
342      /* the whole rest of the text fits with room to spare, adjust for it */
343      len-=(last-ptr1);
344    }
345    if (len<=0) {
346      /* saftey check */
347      owl_fmtext_append_normal(out, "\n");
348      ptr1=ptr2+1;
349      continue;
350    }
351
352    offset=ptr1-in->textbuff;
353    _owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len);
354
355    ptr1=ptr2+1;
356  }
357}
358
359/* Return the number of lines in 'f' */
360int owl_fmtext_num_lines(owl_fmtext *f)
361{
362  int lines, i;
363
364  if (f->textlen==0) return(0);
365 
366  lines=0;
367  for (i=0; i<f->textlen; i++) {
368    if (f->textbuff[i]=='\n') lines++;
369  }
370
371  /* if the last char wasn't a \n there's one more line */
372  if (f->textbuff[i-1]!='\n') lines++;
373
374  return(lines);
375}
376
377char *owl_fmtext_get_text(owl_fmtext *f)
378{
379  return(f->textbuff);
380}
381
382/* set the charater at 'index' to be 'char'.  If index is out of
383 * bounds don't do anything */
384void owl_fmtext_set_char(owl_fmtext *f, int index, int ch)
385{
386  if ((index < 0) || (index > f->textlen-1)) return;
387  f->textbuff[index]=ch;
388}
389
390/* Make a copy of the fmtext 'src' into 'dst' */
391void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src)
392{
393  int mallocsize;
394
395  if (src->textlen==0) {
396    mallocsize=5;
397  } else {
398    mallocsize=src->textlen+2;
399  }
400  dst->textlen=src->textlen;
401  dst->textbuff=owl_malloc(mallocsize);
402  dst->fmbuff=owl_malloc(mallocsize);
403  dst->fgcolorbuff=owl_malloc(mallocsize * sizeof(short));
404  dst->bgcolorbuff=owl_malloc(mallocsize * sizeof(short));
405  memcpy(dst->textbuff, src->textbuff, src->textlen+1);
406  memcpy(dst->fmbuff, src->fmbuff, src->textlen);
407  memcpy(dst->fgcolorbuff, src->fgcolorbuff, src->textlen * sizeof(short));
408  memcpy(dst->bgcolorbuff, src->bgcolorbuff, src->textlen * sizeof(short));
409}
410
411/* highlight all instances of "string".  Return the number of
412 * instances found.  This is a case insensitive search.
413 */
414int owl_fmtext_search_and_highlight(owl_fmtext *f, char *string)
415{
416
417  int found, len;
418  char *ptr1, *ptr2;
419
420  len=strlen(string);
421  found=0;
422  ptr1=f->textbuff;
423  while (ptr1-f->textbuff <= f->textlen) {
424    ptr2=stristr(ptr1, string);
425    if (!ptr2) return(found);
426
427    found++;
428    _owl_fmtext_add_attr(f, OWL_FMTEXT_ATTR_REVERSE,
429                         ptr2 - f->textbuff,
430                         ptr2 - f->textbuff + len - 1);
431
432    ptr1=ptr2+len;
433  }
434  return(found);
435}
436
437/* return 1 if the string is found, 0 if not.  This is a case
438 *  insensitive search.
439 */
440int owl_fmtext_search(owl_fmtext *f, char *string)
441{
442
443  if (stristr(f->textbuff, string)) return(1);
444  return(0);
445}
446
447
448/* Append the text 'text' to 'f' and interpret the zephyr style
449 * formatting syntax to set appropriate attributes.
450 */
451void owl_fmtext_append_ztext(owl_fmtext *f, char *text)
452{
453  int stacksize, curattrs, curcolor;
454  char *ptr, *txtptr, *buff, *tmpptr;
455  int attrstack[32], chrstack[32];
456
457  curattrs=OWL_FMTEXT_ATTR_NONE;
458  curcolor=OWL_COLOR_DEFAULT;
459  stacksize=0;
460  txtptr=text;
461  while (1) {
462    ptr=strpbrk(txtptr, "@{[<()>]}");
463    if (!ptr) {
464      /* add all the rest of the text and exit */
465      owl_fmtext_append_attr(f, txtptr, curattrs, curcolor, OWL_COLOR_DEFAULT);
466      return;
467    } else if (ptr[0]=='@') {
468      /* add the text up to this point then deal with the stack */
469      buff=owl_malloc(ptr-txtptr+20);
470      strncpy(buff, txtptr, ptr-txtptr);
471      buff[ptr-txtptr]='\0';
472      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
473      owl_free(buff);
474
475      /* update pointer to point at the @ */
476      txtptr=ptr;
477
478      /* now the stack */
479
480      /* if we've hit our max stack depth, print the @ and move on */
481      if (stacksize==32) {
482        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
483        txtptr++;
484        continue;
485      }
486
487      /* if it's an @@, print an @ and continue */
488      if (txtptr[1]=='@') {
489        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
490        txtptr+=2;
491        continue;
492      }
493       
494      /* if there's no opener, print the @ and continue */
495      tmpptr=strpbrk(txtptr, "(<[{ ");
496      if (!tmpptr || tmpptr[0]==' ') {
497        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
498        txtptr++;
499        continue;
500      }
501
502      /* check what command we've got, push it on the stack, start
503         using it, and continue ... unless it's a color command */
504      buff=owl_malloc(tmpptr-ptr+20);
505      strncpy(buff, ptr, tmpptr-ptr);
506      buff[tmpptr-ptr]='\0';
507      if (!strcasecmp(buff, "@bold")) {
508        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
509        chrstack[stacksize]=tmpptr[0];
510        stacksize++;
511        curattrs|=OWL_FMTEXT_ATTR_BOLD;
512        txtptr+=6;
513        owl_free(buff);
514        continue;
515      } else if (!strcasecmp(buff, "@b")) {
516        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
517        chrstack[stacksize]=tmpptr[0];
518        stacksize++;
519        curattrs|=OWL_FMTEXT_ATTR_BOLD;
520        txtptr+=3;
521        owl_free(buff);
522        continue;
523      } else if (!strcasecmp(buff, "@i")) {
524        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
525        chrstack[stacksize]=tmpptr[0];
526        stacksize++;
527        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
528        txtptr+=3;
529        owl_free(buff);
530        continue;
531      } else if (!strcasecmp(buff, "@italic")) {
532        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
533        chrstack[stacksize]=tmpptr[0];
534        stacksize++;
535        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
536        txtptr+=8;
537        owl_free(buff);
538        continue;
539
540        /* if it's a color read the color, set the current color and
541           continue */
542      } else if (!strcasecmp(buff, "@color") 
543                 && owl_global_get_hascolors(&g)
544                 && owl_global_is_colorztext(&g)) {
545        owl_free(buff);
546        txtptr+=7;
547        tmpptr=strpbrk(txtptr, "@{[<()>]}");
548        if (tmpptr &&
549            ((txtptr[-1]=='(' && tmpptr[0]==')') ||
550             (txtptr[-1]=='<' && tmpptr[0]=='>') ||
551             (txtptr[-1]=='[' && tmpptr[0]==']') ||
552             (txtptr[-1]=='{' && tmpptr[0]=='}'))) {
553
554          /* grab the color name */
555          buff=owl_malloc(tmpptr-txtptr+20);
556          strncpy(buff, txtptr, tmpptr-txtptr);
557          buff[tmpptr-txtptr]='\0';
558
559          /* set it as the current color */
560          curcolor=owl_util_string_to_color(buff);
561          if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT;
562          owl_free(buff);
563          txtptr=tmpptr+1;
564          continue;
565
566        } else {
567
568        }
569
570      } else {
571        /* if we didn't understand it, we'll print it.  This is different from zwgc
572         * but zwgc seems to be smarter about some screw cases than I am
573         */
574        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
575        txtptr++;
576        continue;
577      }
578
579    } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') {
580      /* add the text up to this point first */
581      buff=owl_malloc(ptr-txtptr+20);
582      strncpy(buff, txtptr, ptr-txtptr);
583      buff[ptr-txtptr]='\0';
584      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
585      owl_free(buff);
586
587      /* now deal with the closer */
588      txtptr=ptr;
589
590      /* first, if the stack is empty we must bail (just print and go) */
591      if (stacksize==0) {
592        buff=owl_malloc(5);
593        buff[0]=ptr[0];
594        buff[1]='\0';
595        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
596        owl_free(buff);
597        txtptr++;
598        continue;
599      }
600
601      /* if the closing char is what's on the stack, turn off the
602         attribue and pop the stack */
603      if ((ptr[0]==')' && chrstack[stacksize-1]=='(') ||
604          (ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
605          (ptr[0]==']' && chrstack[stacksize-1]=='[') ||
606          (ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
607        int i;
608        stacksize--;
609        curattrs=OWL_FMTEXT_ATTR_NONE;
610        for (i=0; i<stacksize; i++) {
611          curattrs|=attrstack[i];
612        }
613        txtptr+=1;
614        continue;
615      } else {
616        /* otherwise print and continue */
617        buff=owl_malloc(5);
618        buff[0]=ptr[0];
619        buff[1]='\0';
620        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
621        owl_free(buff);
622        txtptr++;
623        continue;
624      }
625    } else {
626      /* we've found an unattached opener, print everything and move on */
627      buff=owl_malloc(ptr-txtptr+20);
628      strncpy(buff, txtptr, ptr-txtptr+1);
629      buff[ptr-txtptr+1]='\0';
630      owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
631      owl_free(buff);
632      txtptr=ptr+1;
633      continue;
634    }
635  }
636}
637
638/* requires that the list values are strings or NULL.
639 * joins the elements together with join_with.
640 * If format_fn is specified, passes it the list element value
641 * and it will return a string which this needs to free. */
642void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*))
643{
644  int i, size;
645  void *elem;
646  char *text;
647
648  size = owl_list_get_size(l);
649  for (i=0; i<size; i++) {
650    elem = (char*)owl_list_get_element(l,i);
651    if (elem && format_fn) {
652      text = format_fn(elem);
653      if (text) {
654        owl_fmtext_append_normal(f, text);
655        owl_free(text);
656      }
657    } else if (elem) {
658      owl_fmtext_append_normal(f, elem);
659    }
660    if ((i < size-1) && join_with) {
661      owl_fmtext_append_normal(f, join_with);
662    }
663  }
664}
665
666/* Free all memory allocated by the object */
667void owl_fmtext_free(owl_fmtext *f)
668{
669  if (f->textbuff) owl_free(f->textbuff);
670  if (f->fmbuff) owl_free(f->fmbuff);
671  if (f->fgcolorbuff) owl_free(f->fgcolorbuff);
672  if (f->bgcolorbuff) owl_free(f->bgcolorbuff);
673}
674
675/*** Color Pair manager ***/
676void owl_fmtext_init_colorpair_mgr(owl_colorpair_mgr *cpmgr)
677{
678  // This could be a bitarray if we wanted to save memory.
679  short i, j;
680  cpmgr->next = 8;
681 
682  // The test is <= because we allocate COLORS+1 entries.
683  cpmgr->pairs = owl_malloc((COLORS+1) * sizeof(short*));
684  for(i = 0; i <= COLORS; i++) {
685    cpmgr->pairs[i] = owl_malloc((COLORS+1) * sizeof(short));
686    for(j = 0; j <= COLORS; j++) {
687      cpmgr->pairs[i][j] = -1;
688    }
689  }
690  for(i = 0; i < 8; i++) {
691    short fg, bg;
692    pair_content(i, &fg, &bg);
693    cpmgr->pairs[fg+1][bg+1] = i;
694  }
695}
696
697/* Reset used list */
698void owl_fmtext_reset_colorpairs()
699{
700  short i, j;
701  owl_colorpair_mgr *cpmgr = owl_global_get_colorpair_mgr(&g);
702  cpmgr->next = 8;
703
704  // The test is <= because we allocated COLORS+1 entries.
705  for(i = 0; i <= COLORS; i++) {
706    for(j = 0; j <= COLORS; j++) {
707      cpmgr->pairs[i][j] = -1;
708    }
709  }
710  for(i = 0; i < 8; i++) {
711    short fg, bg;
712    pair_content(i, &fg, &bg);
713    cpmgr->pairs[fg+1][bg+1] = i;
714  }
715}
716
717/* Assign pairs by request */
718short owl_fmtext_get_colorpair(int fg, int bg)
719{
720  owl_colorpair_mgr *cpmgr;
721  short pair, default_bg;
722
723#ifdef HAVE_USE_DEFAULT_COLORS
724  if (fg == OWL_COLOR_DEFAULT) fg = -1;
725  default_bg = OWL_COLOR_DEFAULT;
726#else
727  if (fg == OWL_COLOR_DEFAULT) fg = 0;
728  if (bg == OWL_COLOR_DEFAULT) bg = 0;
729  default_bg = COLOR_BLACK;
730#endif
731
732  // looking for a pair we already set up for this draw.
733  cpmgr = owl_global_get_colorpair_mgr(&g);
734  pair = cpmgr->pairs[fg+1][bg+1];
735  if (!(pair != -1 && pair < cpmgr->next)) {
736/*    owl_global_set_needrefresh(&g);*/
737    // If we didn't find a pair, search for a free one to assign.
738    pair = (cpmgr->next < COLOR_PAIRS) ? cpmgr->next : -1;
739    if (pair != -1) {
740      // We found a free pair, initialize it.
741      init_pair(pair, fg, bg);
742      cpmgr->pairs[fg+1][bg+1] = pair;
743      cpmgr->next++;
744    }
745    else if (bg != OWL_COLOR_DEFAULT) {
746      // We still don't have a pair, drop the background color. Too bad.
747      owl_function_debugmsg("colorpairs: color shortage - dropping background color.");
748      pair = owl_fmtext_get_colorpair(fg, OWL_COLOR_DEFAULT);
749    }
750    else {
751      // We still don't have a pair, defaults all around.
752      owl_function_debugmsg("colorpairs: color shortage - dropping foreground and background color.");
753      pair = 0;
754    }
755  }
756  return pair;
757}
Note: See TracBrowser for help on using the repository browser.