source: fmtext.c @ cee1f25

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