source: fmtext.c @ dafd919

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