source: fmtext.c @ 290f290

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 290f290 was bd3f232, checked in by James M. Kretchmar <kretch@mit.edu>, 21 years ago
Styles implemented It's still a little buggy ... if a format_msg(); is used in perl admin messages (or maybe just the first admin message) are not formatted.
  • Property mode set to 100644
File size: 15.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->textbuff=owl_strdup("");
12  f->fmbuff=owl_malloc(5);
13  f->colorbuff=owl_malloc(5);
14  f->fmbuff[0]=OWL_FMTEXT_ATTR_NONE;
15  f->colorbuff[0]=OWL_COLOR_DEFAULT;
16}
17
18/* Internal function.  Set the attribute 'attr' from index 'first' to
19 * index 'last'
20 */
21void _owl_fmtext_set_attr(owl_fmtext *f, int attr, int first, int last)
22{
23  int i;
24  for (i=first; i<=last; i++) {
25    f->fmbuff[i]=(unsigned char) attr;
26  }
27}
28
29/* Internal function.  Add the attribute 'attr' to the existing
30 * attributes from index 'first' to index 'last'
31 */
32void _owl_fmtext_add_attr(owl_fmtext *f, int attr, int first, int last)
33{
34  int i;
35  for (i=first; i<=last; i++) {
36    f->fmbuff[i]|=(unsigned char) attr;
37  }
38}
39
40/* Internal function.  Set the color to be 'color' from index 'first'
41 * to index 'last
42 */
43void _owl_fmtext_set_color(owl_fmtext *f, int color, int first, int last)
44{
45  int i;
46  for (i=first; i<=last; i++) {
47    f->colorbuff[i]=(unsigned char) color;
48  }
49}
50
51/* append text to the end of 'f' with attribute 'attr' and color
52 * 'color'
53 */
54void owl_fmtext_append_attr(owl_fmtext *f, char *text, int attr, int color)
55{
56  int newlen;
57
58  newlen=strlen(f->textbuff)+strlen(text);
59  f->textbuff=owl_realloc(f->textbuff, newlen+2);
60  f->fmbuff=owl_realloc(f->fmbuff, newlen+2);
61  f->colorbuff=owl_realloc(f->colorbuff, newlen+2);
62
63  strcat(f->textbuff, text);
64  _owl_fmtext_set_attr(f, attr, f->textlen, newlen);
65  _owl_fmtext_set_color(f, color, f->textlen, newlen);
66  f->textlen=newlen;
67}
68
69/* Append normal, uncolored text 'text' to 'f' */
70void owl_fmtext_append_normal(owl_fmtext *f, char *text)
71{
72  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, OWL_COLOR_DEFAULT);
73}
74
75/* Append normal text 'text' to 'f' with color 'color' */
76void owl_fmtext_append_normal_color(owl_fmtext *f, char *text, int color)
77{
78  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_NONE, color);
79}
80
81/* Append bold text 'text' to 'f' */
82void owl_fmtext_append_bold(owl_fmtext *f, char *text)
83{
84  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT);
85}
86
87/* Append reverse video text 'text' to 'f' */
88void owl_fmtext_append_reverse(owl_fmtext *f, char *text)
89{
90  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE, OWL_COLOR_DEFAULT);
91}
92
93/* Append reversed and bold, uncolored text 'text' to 'f' */
94void owl_fmtext_append_reversebold(owl_fmtext *f, char *text)
95{
96  owl_fmtext_append_attr(f, text, OWL_FMTEXT_ATTR_REVERSE | OWL_FMTEXT_ATTR_BOLD, OWL_COLOR_DEFAULT);
97}
98
99/* Add the attribute 'attr' to all text in 'f' */
100void owl_fmtext_addattr(owl_fmtext *f, int attr)
101{
102  /* add the attribute to all text */
103  int i, j;
104
105  j=f->textlen;
106  for (i=0; i<j; i++) {
107    f->fmbuff[i] |= attr;
108  }
109}
110
111/* Anywhere the color is NOT ALREDY SET, set the color to 'color'.
112 * Other colors are left unchanged
113 */
114void owl_fmtext_colorize(owl_fmtext *f, int color)
115{
116  /* everywhere the color is OWL_COLOR_DEFAULT, change it to be 'color' */
117  int i, j;
118
119  j=f->textlen;
120  for(i=0; i<j; i++) {
121    if (f->colorbuff[i]==OWL_COLOR_DEFAULT) f->colorbuff[i] = color;
122  }
123}
124
125/* Append the text 'text' to 'f' and interpret the zephyr style
126 * formatting syntax to set appropriate attributes.
127 */
128void owl_fmtext_append_ztext(owl_fmtext *f, char *text)
129{
130  int stacksize, curattrs, curcolor;
131  char *ptr, *txtptr, *buff, *tmpptr;
132  int attrstack[32], chrstack[32];
133
134  curattrs=OWL_FMTEXT_ATTR_NONE;
135  curcolor=OWL_COLOR_DEFAULT;
136  stacksize=0;
137  txtptr=text;
138  while (1) {
139    ptr=strpbrk(txtptr, "@{[<()>]}");
140    if (!ptr) {
141      /* add all the rest of the text and exit */
142      owl_fmtext_append_attr(f, txtptr, curattrs, curcolor);
143      return;
144    } else if (ptr[0]=='@') {
145      /* add the text up to this point then deal with the stack */
146      buff=owl_malloc(ptr-txtptr+20);
147      strncpy(buff, txtptr, ptr-txtptr);
148      buff[ptr-txtptr]='\0';
149      owl_fmtext_append_attr(f, buff, curattrs, curcolor);
150      owl_free(buff);
151
152      /* update pointer to point at the @ */
153      txtptr=ptr;
154
155      /* now the stack */
156
157      /* if we've hit our max stack depth, print the @ and move on */
158      if (stacksize==32) {
159        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
160        txtptr++;
161        continue;
162      }
163
164      /* if it's an @@, print an @ and continue */
165      if (txtptr[1]=='@') {
166        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
167        txtptr+=2;
168        continue;
169      }
170       
171      /* if there's no opener, print the @ and continue */
172      tmpptr=strpbrk(txtptr, "(<[{ ");
173      if (!tmpptr || tmpptr[0]==' ') {
174        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
175        txtptr++;
176        continue;
177      }
178
179      /* check what command we've got, push it on the stack, start
180         using it, and continue ... unless it's a color command */
181      buff=owl_malloc(tmpptr-ptr+20);
182      strncpy(buff, ptr, tmpptr-ptr);
183      buff[tmpptr-ptr]='\0';
184      if (!strcasecmp(buff, "@bold")) {
185        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
186        chrstack[stacksize]=tmpptr[0];
187        stacksize++;
188        curattrs|=OWL_FMTEXT_ATTR_BOLD;
189        txtptr+=6;
190        owl_free(buff);
191        continue;
192      } else if (!strcasecmp(buff, "@b")) {
193        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
194        chrstack[stacksize]=tmpptr[0];
195        stacksize++;
196        curattrs|=OWL_FMTEXT_ATTR_BOLD;
197        txtptr+=3;
198        owl_free(buff);
199        continue;
200      } else if (!strcasecmp(buff, "@i")) {
201        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
202        chrstack[stacksize]=tmpptr[0];
203        stacksize++;
204        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
205        txtptr+=3;
206        owl_free(buff);
207        continue;
208      } else if (!strcasecmp(buff, "@italic")) {
209        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
210        chrstack[stacksize]=tmpptr[0];
211        stacksize++;
212        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
213        txtptr+=8;
214        owl_free(buff);
215        continue;
216
217        /* if it's a color read the color, set the current color and
218           continue */
219      } else if (!strcasecmp(buff, "@color") 
220                 && owl_global_get_hascolors(&g)
221                 && owl_global_is_colorztext(&g)) {
222        owl_free(buff);
223        txtptr+=7;
224        tmpptr=strpbrk(txtptr, "@{[<()>]}");
225        if (tmpptr &&
226            ((txtptr[-1]=='(' && tmpptr[0]==')') ||
227             (txtptr[-1]=='<' && tmpptr[0]=='>') ||
228             (txtptr[-1]=='[' && tmpptr[0]==']') ||
229             (txtptr[-1]=='{' && tmpptr[0]=='}'))) {
230
231          /* grab the color name */
232          buff=owl_malloc(tmpptr-txtptr+20);
233          strncpy(buff, txtptr, tmpptr-txtptr);
234          buff[tmpptr-txtptr]='\0';
235
236          /* set it as the current color */
237          curcolor=owl_util_string_to_color(buff);
238          owl_free(buff);
239          txtptr=tmpptr+1;
240          continue;
241
242        } else {
243
244        }
245
246      } else {
247        /* if we didn't understand it, we'll print it.  This is different from zwgc
248         * but zwgc seems to be smarter about some screw cases than I am
249         */
250        owl_fmtext_append_attr(f, "@", curattrs, curcolor);
251        txtptr++;
252        continue;
253      }
254
255    } else if (ptr[0]=='}' || ptr[0]==']' || ptr[0]==')' || ptr[0]=='>') {
256      /* add the text up to this point first */
257      buff=owl_malloc(ptr-txtptr+20);
258      strncpy(buff, txtptr, ptr-txtptr);
259      buff[ptr-txtptr]='\0';
260      owl_fmtext_append_attr(f, buff, curattrs, curcolor);
261      owl_free(buff);
262
263      /* now deal with the closer */
264      txtptr=ptr;
265
266      /* first, if the stack is empty we must bail (just print and go) */
267      if (stacksize==0) {
268        buff=owl_malloc(5);
269        buff[0]=ptr[0];
270        buff[1]='\0';
271        owl_fmtext_append_attr(f, buff, curattrs, curcolor);
272        owl_free(buff);
273        txtptr++;
274        continue;
275      }
276
277      /* if the closing char is what's on the stack, turn off the
278         attribue and pop the stack */
279      if ((ptr[0]==')' && chrstack[stacksize-1]=='(') ||
280          (ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
281          (ptr[0]==']' && chrstack[stacksize-1]=='[') ||
282          (ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
283        int i;
284        stacksize--;
285        curattrs=OWL_FMTEXT_ATTR_NONE;
286        for (i=0; i<stacksize; i++) {
287          curattrs|=attrstack[i];
288        }
289        txtptr+=1;
290        continue;
291      } else {
292        /* otherwise print and continue */
293        buff=owl_malloc(5);
294        buff[0]=ptr[0];
295        buff[1]='\0';
296        owl_fmtext_append_attr(f, buff, curattrs, curcolor);
297        owl_free(buff);
298        txtptr++;
299        continue;
300      }
301    } else {
302      /* we've found an unattached opener, print everything and move on */
303      buff=owl_malloc(ptr-txtptr+20);
304      strncpy(buff, txtptr, ptr-txtptr+1);
305      buff[ptr-txtptr+1]='\0';
306      owl_fmtext_append_attr(f, buff, curattrs, curcolor);
307      owl_free(buff);
308      txtptr=ptr+1;
309      continue;
310    }
311  }
312}
313
314/* Internal function.  Append text from 'in' between index 'start' and
315 * 'stop' to the end of 'f'
316 */
317void _owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in, int start, int stop)
318{
319  int newlen, i;
320
321  newlen=strlen(f->textbuff)+(stop-start+1);
322  f->textbuff=owl_realloc(f->textbuff, newlen+1);
323  f->fmbuff=owl_realloc(f->fmbuff, newlen+1);
324  f->colorbuff=owl_realloc(f->colorbuff, newlen+1);
325
326  strncat(f->textbuff, in->textbuff+start, stop-start+1);
327  f->textbuff[newlen]='\0';
328  for (i=start; i<=stop; i++) {
329    f->fmbuff[f->textlen+(i-start)]=in->fmbuff[i];
330    f->colorbuff[f->textlen+(i-start)]=in->colorbuff[i];
331  }
332  f->textlen=newlen;
333}
334
335/* append fmtext 'in' to 'f' */
336void owl_fmtext_append_fmtext(owl_fmtext *f, owl_fmtext *in)
337{
338  _owl_fmtext_append_fmtext(f, in, 0, in->textlen);
339
340}
341
342/* Append 'nspaces' number of spaces to the end of 'f' */
343void owl_fmtext_append_spaces(owl_fmtext *f, int nspaces)
344{
345  int i;
346  for (i=0; i<nspaces; i++) {
347    owl_fmtext_append_normal(f, " ");
348  }
349}
350
351/* requires that the list values are strings or NULL.
352 * joins the elements together with join_with.
353 * If format_fn is specified, passes it the list element value
354 * and it will return a string which this needs to free. */
355void owl_fmtext_append_list(owl_fmtext *f, owl_list *l, char *join_with, char *(format_fn)(void*))
356{
357  int i, size;
358  void *elem;
359  char *text;
360
361  size = owl_list_get_size(l);
362  for (i=0; i<size; i++) {
363    elem = (char*)owl_list_get_element(l,i);
364    if (elem && format_fn) {
365      text = format_fn(elem);
366      if (text) {
367        owl_fmtext_append_normal(f, text);
368        owl_free(text);
369      }
370    } else if (elem) {
371      owl_fmtext_append_normal(f, elem);
372    }
373    if ((i < size-1) && join_with) {
374      owl_fmtext_append_normal(f, join_with);
375    }
376  }
377}
378
379/* Return a plain version of the fmtext.  Caller is responsible for
380 * freeing the return
381 */
382char *owl_fmtext_print_plain(owl_fmtext *f)
383{
384  return(owl_strdup(f->textbuff));
385}
386
387/* add the formatted text to the curses window 'w'.  The window 'w'
388 * must already be initiatlized with curses
389 */
390void owl_fmtext_curs_waddstr(owl_fmtext *f, WINDOW *w)
391{
392  char *tmpbuff;
393  int position, trans1, trans2, len, lastsame;
394
395  if (w==NULL) {
396    owl_function_debugmsg("Hit a null window in owl_fmtext_curs_waddstr.");
397    return;
398  }
399
400  tmpbuff=owl_malloc(f->textlen+10);
401
402  position=0;
403  len=f->textlen;
404  while (position<=len) {
405    /* find the last char with the current format and color */
406    trans1=owl_util_find_trans(f->fmbuff+position, len-position);
407    trans2=owl_util_find_trans(f->colorbuff+position, len-position);
408
409    if (trans1<trans2) {
410      lastsame=position+trans1;
411    } else {
412      lastsame=position+trans2;
413    }
414
415    /* set the format */
416    wattrset(w, A_NORMAL);
417    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_BOLD) {
418      wattron(w, A_BOLD);
419    }
420    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_REVERSE) {
421      wattron(w, A_REVERSE);
422    }
423    if (f->fmbuff[position] & OWL_FMTEXT_ATTR_UNDERLINE) {
424      wattron(w, A_UNDERLINE);
425    }
426
427    /* set the color */
428    /* warning, this is sort of a hack */
429    if (owl_global_get_hascolors(&g)) {
430      if (f->colorbuff[position]!=OWL_COLOR_DEFAULT) {
431        wattron(w, COLOR_PAIR(f->colorbuff[position]));
432      }
433    }
434
435    /* add the text */
436    strncpy(tmpbuff, f->textbuff + position, lastsame-position+1);
437    tmpbuff[lastsame-position+1]='\0';
438    waddstr(w, tmpbuff);
439
440    position=lastsame+1;
441  }
442  owl_free(tmpbuff);
443}
444
445
446/* start with line 'aline' (where the first line is 0) and print
447 * 'lines' number of lines into 'out'
448 */
449int owl_fmtext_truncate_lines(owl_fmtext *in, int aline, int lines, owl_fmtext *out)
450{
451  char *ptr1, *ptr2;
452  int i, offset;
453 
454  /* find the starting line */
455  ptr1=in->textbuff;
456  if (aline!=0) {
457    for (i=0; i<aline; i++) {
458      ptr1=strchr(ptr1, '\n');
459      if (!ptr1) return(-1);
460      ptr1++;
461    }
462  }
463  /* ptr1 now holds the starting point */
464
465  /* copy in the next 'lines' lines */
466  if (lines<1) return(-1);
467
468  for (i=0; i<lines; i++) {
469    offset=ptr1-in->textbuff;
470    ptr2=strchr(ptr1, '\n');
471    if (!ptr2) {
472      _owl_fmtext_append_fmtext(out, in, offset, (in->textlen)-1);
473      return(-1);
474    }
475    _owl_fmtext_append_fmtext(out, in, offset, (ptr2-ptr1)+offset);
476    ptr1=ptr2+1;
477  }
478  return(0);
479}
480
481/* Truncate the message so that each line begins at column 'acol' and
482 * ends at 'bcol' or sooner.  The first column is number 0.  The new
483 * message is placed in 'out'.  The message is * expected to end in a
484 * new line for now
485 */
486void owl_fmtext_truncate_cols(owl_fmtext *in, int acol, int bcol, owl_fmtext *out)
487{
488  char *ptr1, *ptr2, *last;
489  int len, offset;
490
491  last=in->textbuff+in->textlen-1;
492  ptr1=in->textbuff;
493  while (ptr1<=last) {
494    ptr2=strchr(ptr1, '\n');
495    if (!ptr2) {
496      /* but this shouldn't happen if we end in a \n */
497      break;
498    }
499   
500    if (ptr2==ptr1) {
501      owl_fmtext_append_normal(out, "\n");
502      ptr1++;
503      continue;
504    }
505
506    /* we need to check that we won't run over here */
507    len=bcol-acol;
508    if (len > (ptr2-(ptr1+acol))) {
509      /* the whole line fits with room to spare, don't take a full 'len' */
510      len=ptr2-(ptr1+acol);
511    }
512    if (len>last-ptr1) {
513      /* the whole rest of the text fits with room to spare, adjust for it */
514      len-=(last-ptr1);
515    }
516    if (len<=0) {
517      /* saftey check */
518      owl_fmtext_append_normal(out, "\n");
519      ptr1=ptr2+1;
520      continue;
521    }
522
523    offset=ptr1-in->textbuff;
524    _owl_fmtext_append_fmtext(out, in, offset+acol, offset+acol+len);
525
526    ptr1=ptr2+1;
527  }
528}
529
530/* Return the number of lines in 'f' */
531int owl_fmtext_num_lines(owl_fmtext *f)
532{
533  int lines, i;
534
535  if (f->textlen==0) return(0);
536 
537  lines=0;
538  for (i=0; i<f->textlen; i++) {
539    if (f->textbuff[i]=='\n') lines++;
540  }
541
542  /* if the last char wasn't a \n there's one more line */
543  if (f->textbuff[i-1]!='\n') lines++;
544
545  return(lines);
546}
547
548char *owl_fmtext_get_text(owl_fmtext *f)
549{
550  return(f->textbuff);
551}
552
553void owl_fmtext_set_char(owl_fmtext *f, int index, int ch)
554{
555  /* set the charater at 'index' to be 'char'.  If index is out of
556   * bounds don't do anything */
557  if ((index < 0) || (index > f->textlen-1)) return;
558  f->textbuff[index]=ch;
559}
560
561/* Free all memory allocated by the object */
562void owl_fmtext_free(owl_fmtext *f)
563{
564  if (f->textbuff) owl_free(f->textbuff);
565  if (f->fmbuff) owl_free(f->fmbuff);
566  if (f->colorbuff) owl_free(f->colorbuff);
567}
568
569/* Make a copy of the fmtext 'src' into 'dst' */
570void owl_fmtext_copy(owl_fmtext *dst, owl_fmtext *src)
571{
572  int mallocsize;
573
574  if (src->textlen==0) {
575    mallocsize=5;
576  } else {
577    mallocsize=src->textlen+2;
578  }
579  dst->textlen=src->textlen;
580  dst->textbuff=owl_malloc(mallocsize);
581  dst->fmbuff=owl_malloc(mallocsize);
582  dst->colorbuff=owl_malloc(mallocsize);
583  memcpy(dst->textbuff, src->textbuff, src->textlen+1);
584  memcpy(dst->fmbuff, src->fmbuff, src->textlen);
585  memcpy(dst->colorbuff, src->colorbuff, src->textlen);
586}
587
588/* highlight all instance of "string".  Return the number of
589 * instances found.  This is a case insensitive search.
590 */
591int owl_fmtext_search_and_highlight(owl_fmtext *f, char *string)
592{
593
594  int found, len;
595  char *ptr1, *ptr2;
596
597  len=strlen(string);
598  found=0;
599  ptr1=f->textbuff;
600  while (ptr1-f->textbuff <= f->textlen) {
601    ptr2=stristr(ptr1, string);
602    if (!ptr2) return(found);
603
604    found++;
605    _owl_fmtext_add_attr(f, OWL_FMTEXT_ATTR_REVERSE,
606                         ptr2 - f->textbuff,
607                         ptr2 - f->textbuff + len - 1);
608
609    ptr1=ptr2+len;
610  }
611  return(found);
612}
613
614/* return 1 if the string is found, 0 if not.  This is a case
615 *  insensitive search.
616 */
617int owl_fmtext_search(owl_fmtext *f, char *string)
618{
619
620  if (stristr(f->textbuff, string)) return(1);
621  return(0);
622}
Note: See TracBrowser for help on using the repository browser.