Changeset 1bdffcb for fmtext.c


Ignore:
Timestamp:
Jan 23, 2007, 11:49:14 PM (14 years ago)
Author:
Alejandro R. Sedeño <asedeno@mit.edu>
Branches:
master, barnowl_perlaim, debian, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
0c8ab5e
Parents:
4d17916
Message:
owl.h     - moved curses.h back up so building on Solaris would work.
configure - added test for resizeterm
global.c  - conditionally include resizeterm
fmtext.c  - remove tabs, rewrite parts of the colorpair manager
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fmtext.c

    racee046 r1bdffcb  
    235235    /* warning, this is sort of a hack */
    236236    if (owl_global_get_hascolors(&g)) {
    237       int pair, fg, bg;
     237      int fg, bg;
     238      short pair;
    238239      fg = f->fgcolorbuff[position];
    239240      bg = f->bgcolorbuff[position];
     
    242243      pair = owl_fmtext_get_colorpair(fg, bg);
    243244      if (pair != -1) {
    244         wattron(w, COLOR_PAIR(pair));
     245        wattron(w, COLOR_PAIR(pair));
    245246      }
    246247    }
     
    411412    found++;
    412413    _owl_fmtext_add_attr(f, OWL_FMTEXT_ATTR_REVERSE,
    413                         ptr2 - f->textbuff,
    414                         ptr2 - f->textbuff + len - 1);
     414                        ptr2 - f->textbuff,
     415                        ptr2 - f->textbuff + len - 1);
    415416
    416417    ptr1=ptr2+len;
     
    464465      /* if we've hit our max stack depth, print the @ and move on */
    465466      if (stacksize==32) {
    466         owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    467         txtptr++;
    468         continue;
     467        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
     468        txtptr++;
     469        continue;
    469470      }
    470471
    471472      /* if it's an @@, print an @ and continue */
    472473      if (txtptr[1]=='@') {
    473         owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    474         txtptr+=2;
    475         continue;
    476       }
    477        
     474        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
     475        txtptr+=2;
     476        continue;
     477      }
     478       
    478479      /* if there's no opener, print the @ and continue */
    479480      tmpptr=strpbrk(txtptr, "(<[{ ");
    480481      if (!tmpptr || tmpptr[0]==' ') {
    481         owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    482         txtptr++;
    483         continue;
     482        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
     483        txtptr++;
     484        continue;
    484485      }
    485486
    486487      /* check what command we've got, push it on the stack, start
    487         using it, and continue ... unless it's a color command */
     488        using it, and continue ... unless it's a color command */
    488489      buff=owl_malloc(tmpptr-ptr+20);
    489490      strncpy(buff, ptr, tmpptr-ptr);
    490491      buff[tmpptr-ptr]='\0';
    491492      if (!strcasecmp(buff, "@bold")) {
    492         attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
    493         chrstack[stacksize]=tmpptr[0];
    494         stacksize++;
    495         curattrs|=OWL_FMTEXT_ATTR_BOLD;
    496         txtptr+=6;
    497         owl_free(buff);
    498         continue;
     493        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
     494        chrstack[stacksize]=tmpptr[0];
     495        stacksize++;
     496        curattrs|=OWL_FMTEXT_ATTR_BOLD;
     497        txtptr+=6;
     498        owl_free(buff);
     499        continue;
    499500      } else if (!strcasecmp(buff, "@b")) {
    500         attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
    501         chrstack[stacksize]=tmpptr[0];
    502         stacksize++;
    503         curattrs|=OWL_FMTEXT_ATTR_BOLD;
    504         txtptr+=3;
    505         owl_free(buff);
    506         continue;
     501        attrstack[stacksize]=OWL_FMTEXT_ATTR_BOLD;
     502        chrstack[stacksize]=tmpptr[0];
     503        stacksize++;
     504        curattrs|=OWL_FMTEXT_ATTR_BOLD;
     505        txtptr+=3;
     506        owl_free(buff);
     507        continue;
    507508      } else if (!strcasecmp(buff, "@i")) {
    508         attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
    509         chrstack[stacksize]=tmpptr[0];
    510         stacksize++;
    511         curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
    512         txtptr+=3;
    513         owl_free(buff);
    514         continue;
     509        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
     510        chrstack[stacksize]=tmpptr[0];
     511        stacksize++;
     512        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
     513        txtptr+=3;
     514        owl_free(buff);
     515        continue;
    515516      } else if (!strcasecmp(buff, "@italic")) {
    516         attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
    517         chrstack[stacksize]=tmpptr[0];
    518         stacksize++;
    519         curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
    520         txtptr+=8;
    521         owl_free(buff);
    522         continue;
    523 
    524         /* if it's a color read the color, set the current color and
     517        attrstack[stacksize]=OWL_FMTEXT_ATTR_UNDERLINE;
     518        chrstack[stacksize]=tmpptr[0];
     519        stacksize++;
     520        curattrs|=OWL_FMTEXT_ATTR_UNDERLINE;
     521        txtptr+=8;
     522        owl_free(buff);
     523        continue;
     524
     525        /* if it's a color read the color, set the current color and
    525526           continue */
    526527      } else if (!strcasecmp(buff, "@color")
    527                 && owl_global_get_hascolors(&g)
    528                 && owl_global_is_colorztext(&g)) {
    529         owl_free(buff);
    530         txtptr+=7;
    531         tmpptr=strpbrk(txtptr, "@{[<()>]}");
    532         if (tmpptr &&
    533             ((txtptr[-1]=='(' && tmpptr[0]==')') ||
    534              (txtptr[-1]=='<' && tmpptr[0]=='>') ||
    535              (txtptr[-1]=='[' && tmpptr[0]==']') ||
    536              (txtptr[-1]=='{' && tmpptr[0]=='}'))) {
    537 
    538           /* grab the color name */
    539           buff=owl_malloc(tmpptr-txtptr+20);
    540           strncpy(buff, txtptr, tmpptr-txtptr);
    541           buff[tmpptr-txtptr]='\0';
    542 
    543           /* set it as the current color */
    544           curcolor=owl_util_string_to_color(buff);
    545           if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT;
    546           owl_free(buff);
    547           txtptr=tmpptr+1;
    548           continue;
    549 
    550         } else {
    551 
    552         }
     528                && owl_global_get_hascolors(&g)
     529                && owl_global_is_colorztext(&g)) {
     530        owl_free(buff);
     531        txtptr+=7;
     532        tmpptr=strpbrk(txtptr, "@{[<()>]}");
     533        if (tmpptr &&
     534            ((txtptr[-1]=='(' && tmpptr[0]==')') ||
     535             (txtptr[-1]=='<' && tmpptr[0]=='>') ||
     536             (txtptr[-1]=='[' && tmpptr[0]==']') ||
     537             (txtptr[-1]=='{' && tmpptr[0]=='}'))) {
     538
     539          /* grab the color name */
     540          buff=owl_malloc(tmpptr-txtptr+20);
     541          strncpy(buff, txtptr, tmpptr-txtptr);
     542          buff[tmpptr-txtptr]='\0';
     543
     544          /* set it as the current color */
     545          curcolor=owl_util_string_to_color(buff);
     546          if (curcolor==-1) curcolor=OWL_COLOR_DEFAULT;
     547          owl_free(buff);
     548          txtptr=tmpptr+1;
     549          continue;
     550
     551        } else {
     552
     553        }
    553554
    554555      } else {
    555         /* if we didn't understand it, we'll print it.  This is different from zwgc
    556         * but zwgc seems to be smarter about some screw cases than I am
    557         */
    558         owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
    559         txtptr++;
    560         continue;
     556        /* if we didn't understand it, we'll print it.  This is different from zwgc
     557        * but zwgc seems to be smarter about some screw cases than I am
     558        */
     559        owl_fmtext_append_attr(f, "@", curattrs, curcolor, OWL_COLOR_DEFAULT);
     560        txtptr++;
     561        continue;
    561562      }
    562563
     
    574575      /* first, if the stack is empty we must bail (just print and go) */
    575576      if (stacksize==0) {
    576         buff=owl_malloc(5);
    577         buff[0]=ptr[0];
    578         buff[1]='\0';
    579         owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
    580         owl_free(buff);
    581         txtptr++;
    582         continue;
     577        buff=owl_malloc(5);
     578        buff[0]=ptr[0];
     579        buff[1]='\0';
     580        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
     581        owl_free(buff);
     582        txtptr++;
     583        continue;
    583584      }
    584585
     
    586587         attribue and pop the stack */
    587588      if ((ptr[0]==')' && chrstack[stacksize-1]=='(') ||
    588           (ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
    589           (ptr[0]==']' && chrstack[stacksize-1]=='[') ||
    590           (ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
    591         int i;
    592         stacksize--;
    593         curattrs=OWL_FMTEXT_ATTR_NONE;
    594         for (i=0; i<stacksize; i++) {
    595           curattrs|=attrstack[i];
    596         }
    597         txtptr+=1;
    598         continue;
     589          (ptr[0]=='>' && chrstack[stacksize-1]=='<') ||
     590          (ptr[0]==']' && chrstack[stacksize-1]=='[') ||
     591          (ptr[0]=='}' && chrstack[stacksize-1]=='{')) {
     592        int i;
     593        stacksize--;
     594        curattrs=OWL_FMTEXT_ATTR_NONE;
     595        for (i=0; i<stacksize; i++) {
     596          curattrs|=attrstack[i];
     597        }
     598        txtptr+=1;
     599        continue;
    599600      } else {
    600         /* otherwise print and continue */
    601         buff=owl_malloc(5);
    602         buff[0]=ptr[0];
    603         buff[1]='\0';
    604         owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
    605         owl_free(buff);
    606         txtptr++;
    607         continue;
     601        /* otherwise print and continue */
     602        buff=owl_malloc(5);
     603        buff[0]=ptr[0];
     604        buff[1]='\0';
     605        owl_fmtext_append_attr(f, buff, curattrs, curcolor, OWL_COLOR_DEFAULT);
     606        owl_free(buff);
     607        txtptr++;
     608        continue;
    608609      }
    609610    } else {
     
    636637      text = format_fn(elem);
    637638      if (text) {
    638         owl_fmtext_append_normal(f, text);
    639         owl_free(text);
     639        owl_fmtext_append_normal(f, text);
     640        owl_free(text);
    640641      }
    641642    } else if (elem) {
     
    684685
    685686/* Assign pairs by request */
    686 int owl_fmtext_get_colorpair(int fg, int bg)
     687short owl_fmtext_get_colorpair(int fg, int bg)
    687688{
    688689  owl_colorpair_mgr *cpmgr;
    689   short pair, i;
     690  short pair, i, default_bg;
     691
     692#ifdef HAVE_USE_DEFAULT_COLORS
    690693  if (fg == OWL_COLOR_DEFAULT) fg = -1;
    691   if (bg == OWL_COLOR_DEFAULT) {
     694  default_bg = OWL_COLOR_DEFAULT;
     695#else
     696  if (fg == OWL_COLOR_DEFAULT) fg = 0;
     697  if (bg == OWL_COLOR_DEFAULT) bg = 0;
     698  default_bg = COLOR_BLACK;
     699#endif
     700
     701  if (bg == default_bg) {
     702    // default bg -> use color pairs initialized by owl.c
    692703    pair = fg;
    693704  } else {
     705    // looking for a pair we already set up for this draw.
    694706    cpmgr = owl_global_get_colorpair_mgr(&g);
    695707    pair = cpmgr->pairs[fg+1][bg];
    696     if (pair != -1) {
    697       short oldfg, oldbg;
    698       pair_content(pair, &oldfg, &oldbg);
    699       if (cpmgr->pairs[oldfg+1][oldbg] == pair) {
    700         cpmgr->pairs[oldfg+1][oldbg] = -1;
    701       }
    702       init_pair(pair, fg, bg);
    703       cpmgr->pairs[fg+1][bg] = pair;
    704       cpmgr->used[pair] = 1;
    705     } else {
     708    if (!(pair != -1 && cpmgr->used[pair])) {
     709      // If we didn't find a pair, search for a free one to assign.
    706710      // Skip the first COLORS, since they're static.
     711      // If we ever get 256 color curses, this will need more thought.
    707712      for(i = COLORS; i < COLOR_PAIRS; i++) {
    708         if (0 == cpmgr->used[i]) {
     713        if (!cpmgr->used[i]) {
     714          // Found a free pair
    709715          pair = i;
    710716          break;
     
    712718      }
    713719      if (pair != -1) {
    714         short oldfg, oldbg;
    715         pair_content(pair, &oldfg, &oldbg);
    716         if (cpmgr->pairs[oldfg+1][oldbg] == pair) {
    717           cpmgr->pairs[oldfg+1][oldbg] = -1;
    718         }
     720        // We found a free pair, initialize it.
    719721        init_pair(pair, fg, bg);
    720722        cpmgr->pairs[fg+1][bg] = pair;
    721723        cpmgr->used[pair] = 1;
    722724      } else {
    723         // Fail to skipping background.
     725        // We still don't have a pair, drop the background color. Too bad.
    724726        pair = fg;
    725727      }
Note: See TracChangeset for help on using the changeset viewer.