[w3m-dev 01532] ANSI color support (was Re: w3m-m17n-0.4)

From: Hironori Sakamoto (hsaka@mth.biglobe.ne.jp)
Date: Tue Dec 19 2000 - 11:34:59 CST

  • Next message: Hironori Sakamoto: "[w3m-dev 01533] Re: Patch for anonymizer.com"

    $B:dK\$G$9!#(B

    >> $B$H$3$m$G!"%Z!<%8%c%b!<%I$G(B ANSI color $B%(%9%1!<%W%7!<%1%s%9$K(B
    >> $BBP1~$9$k$H4r$7$$?M$C$F$$$^$9$+!)(B

    aito$BO"MmD"$GM_$7$$$H$$$&J}$,$$$i$C$7$c$$$^$7$?$N$G!"(B
    $BF~$l$F$_$^$7$?!#(Bkokb23 $B$+$i$N:9J,$G$9!#(B
    configure $B$G(B
      Do you want ANSI color escape sequences supprot?
    $B$K(B "y" $B$HEz$($F$/$@$5$$!#(B
    # Buffer $B9=B$BN$,JQ$o$j$^$9$N$G!"0lEY(B touch *.c $B$7$F$+$i(B make $B$9$k(B
    # $B$3$H$r?d>)$7$^$9!#(B

    $B$J$*!"(BHTML $B$N;~$d(B escape sequence $B$,L5$$>l9g$O%a%b%j$r3NJ]$7$J$$$N$G!"(B
    $BFC$K0-1F6A$O$J$$$H;W$$$^$9!#(B
    # <font color=...> $B$K1~MQ$G$-$k!)(B
    -----------------------------------
    $B:dK\(B $B9@B'(B <hsaka@mth.biglobe.ne.jp>
     http://www2u.biglobe.ne.jp/~hsaka/

    diff -u buffer.c.orig buffer.c
    --- buffer.c.orig Sun Dec 17 02:22:35 2000
    +++ buffer.c Wed Dec 20 01:13:27 2000
    @@ -548,6 +548,9 @@
         Str tmp;
         FILE *cache = NULL;
         Line *l;
    +#ifdef ANSI_COLOR
    + int colorflag;
    +#endif
     
         if (buf->savecache)
             return -1;
    @@ -570,8 +573,17 @@
                 fwrite1(l->usrflags, cache) ||
                 fwrite1(l->len, cache) ||
                 fwrite(l->lineBuf, 1, l->len, cache) < l->len ||
    - fwrite(l->propBuf, sizeof(short), l->len, cache) < l->len)
    + fwrite(l->propBuf, sizeof(Lineprop), l->len, cache) < l->len)
                 goto _error;
    +#ifdef ANSI_COLOR
    + colorflag = l->colorBuf ? 1 : 0;
    + if (fwrite1(colorflag, cache))
    + goto _error;
    + if (colorflag) {
    + if (fwrite(l->colorBuf, sizeof(Linecolor), l->len, cache) < l->len)
    + goto _error;
    + }
    +#endif
         }
     
         fclose(cache);
    @@ -590,6 +602,9 @@
         FILE *cache;
         Line *l = NULL, *prevl;
         long lnum = 0, clnum, tlnum;
    +#ifdef ANSI_COLOR
    + int colorflag;
    +#endif
     
         if (buf->savecache == NULL)
             return -1;
    @@ -623,8 +638,18 @@
             l->lineBuf = New_N(char, l->len + 1);
             fread(l->lineBuf, 1, l->len, cache);
             l->lineBuf[l->len] = '\0';
    - l->propBuf = New_N(short, l->len);
    - fread(l->propBuf, sizeof(short), l->len, cache);
    + l->propBuf = New_N(Lineprop, l->len);
    + fread(l->propBuf, sizeof(Lineprop), l->len, cache);
    +#ifdef ANSI_COLOR
    + if (fread1(colorflag, cache))
    + break;
    + if (colorflag) {
    + l->colorBuf = New_N(Linecolor, l->len);
    + fread(l->colorBuf, sizeof(Linecolor), l->len, cache);
    + } else {
    + l->colorBuf = NULL;
    + }
    +#endif
         }
         buf->lastLine = prevl;
         buf->lastLine->next = NULL;
    diff -u configure.orig configure
    --- configure.orig Sat Dec 16 20:19:15 2000
    +++ configure Wed Dec 20 00:45:33 2000
    @@ -435,6 +435,15 @@
       def_use_nntp="#undef USE_NNTP"
     fi
     
    +echo "Do you want ANSI color escape sequences supprot?"
    +yesno ansi_color "$ansi_color" n
    +echo "ansi_color=$ansi_color" >> config.param
    +if [ "$ansi_color" = y ]; then
    + def_ansi_color="#define ANSI_COLOR"
    +else
    + def_ansi_color="#undef ANSI_COLOR"
    +fi
    +
     echo ""
     echo "Let's do some configurations. Choose config option among the list."
     echo ""
    @@ -1517,6 +1526,11 @@
      * Support NNTP
      */
     $def_use_nntp
    +
    +/*
    + * Support ANSI color escape sequences
    + */
    +$def_ansi_color
     
     /*
      * Enable id attribute
    diff -u display.c.orig display.c
    --- display.c.orig Sun Dec 17 02:22:35 2000
    +++ display.c Wed Dec 20 01:13:27 2000
    @@ -163,6 +163,9 @@
     #ifndef KANJI_SYMBOLS
     static int graph_mode = 0;
     #endif /* not KANJI_SYMBOLS */
    +#ifdef ANSI_COLOR
    +static Linecolor color_mode = 0;
    +#endif
     
     #ifdef BUFINFO
     static Buffer *save_current_buf = NULL;
    @@ -316,6 +319,9 @@
         int column = buf->currentColumn;
         char *p;
         Lineprop *pr;
    +#ifdef ANSI_COLOR
    + Linecolor *pc;
    +#endif
     
         if (l == NULL) {
             if (buf->pagerSource) {
    @@ -335,6 +341,12 @@
         pos = columnPos(l, column);
         p = &(l->lineBuf[pos]);
         pr = &(l->propBuf[pos]);
    +#ifdef ANSI_COLOR
    + if (useColor && l->colorBuf)
    + pc = &(l->colorBuf[pos]);
    + else
    + pc = NULL;
    +#endif
         rcol = COLPOS(l, pos);
     
     #ifndef JP_CHARSET
    @@ -350,6 +362,10 @@
             ncol = COLPOS(l, pos + j + delta);
             if (ncol - column > COLS)
                 break;
    +#ifdef ANSI_COLOR
    + if (pc)
    + do_color(pc[j]);
    +#endif
             if (rcol < column) {
                 for (rcol = column; rcol < ncol; rcol++)
                     addChar(' ', 0);
    @@ -409,6 +425,10 @@
             graphend();
         }
     #endif /* not KANJI_SYMBOLS */
    +#ifdef ANSI_COLOR
    + if (color_mode)
    + do_color(0);
    +#endif
         if (rcol - column < COLS)
             clrtoeolx();
         return l->next;
    @@ -421,6 +441,9 @@
         int column = buf->currentColumn;
         char *p;
         Lineprop *pr;
    +#ifdef ANSI_COLOR
    + Linecolor *pc;
    +#endif
         int bcol, ecol;
     
         if (l == NULL)
    @@ -428,6 +451,12 @@
         pos = columnPos(l, column);
         p = &(l->lineBuf[pos]);
         pr = &(l->propBuf[pos]);
    +#ifdef ANSI_COLOR
    + if (useColor && l->colorBuf)
    + pc = &(l->colorBuf[pos]);
    + else
    + pc = NULL;
    +#endif
         rcol = COLPOS(l, pos);
         bcol = bpos - pos;
         ecol = epos - pos;
    @@ -445,6 +474,10 @@
             ncol = COLPOS(l, pos + j + delta);
             if (ncol - column > COLS)
                 break;
    +#ifdef ANSI_COLOR
    + if (pc)
    + do_color(pc[j]);
    +#endif
             if (j >= bcol && j < ecol) {
                 if (rcol < column) {
                     move(i, 0);
    @@ -507,6 +540,10 @@
             graphend();
         }
     #endif /* not KANJI_SYMBOLS */
    +#ifdef ANSI_COLOR
    + if (color_mode)
    + do_color(0);
    +#endif
         return rcol - column;
     }
     
    @@ -561,6 +598,24 @@
         }
     #endif /* not KANJI_SYMBOLS */
     }
    +
    +#ifdef ANSI_COLOR
    +void
    +do_color(Linecolor c)
    +{
    + if (c & 0x8)
    + setfcolor(c & 0x7);
    + else if (color_mode & 0x8)
    + setfcolor(basic_color);
    +#ifdef BG_COLOR
    + if (c & 0x80)
    + setbcolor((c >> 4) & 0x7);
    + else if (color_mode & 0x80)
    + setbcolor(bg_color);
    +#endif
    + color_mode = c;
    +}
    +#endif
     
     void
     addChar(char c, Lineprop mode)
    diff -u etc.c.orig etc.c
    --- etc.c.orig Sun Dec 17 02:22:35 2000
    +++ etc.c Wed Dec 20 01:47:36 2000
    @@ -263,24 +263,119 @@
         return allocStr(arg, p - arg);
     }
     
    +#ifdef ANSI_COLOR
    +static int
    +parse_ansi_color(char **str, Lineprop *effect, Linecolor *color)
    +{
    + char *p = *str, *q;
    + Lineprop e = *effect;
    + Linecolor c = *color;
    + int i;
    +
    + if (*p != ESC_CODE || *(p+1) != '[')
    + return 0;
    + p += 2;
    + for (q = p; IS_DIGIT(*q) || *q == ';'; q++)
    + ;
    + if (*q != 'm')
    + return 0;
    + *str = q + 1;
    + while (1) {
    + if (*p == 'm') {
    + e = PE_NORMAL;
    + c = 0;
    + break;
    + }
    + if (IS_DIGIT(*p)) {
    + q = p;
    + for (p++; IS_DIGIT(*p); p++)
    + ;
    + i = atoi(allocStr(q, p - q));
    + switch (i) {
    + case 0:
    + e = PE_NORMAL;
    + c = 0;
    + break;
    + case 1:
    + case 5:
    + e = PE_BOLD;
    + break;
    + case 4:
    + e = PE_UNDER;
    + break;
    + case 7:
    + e = PE_STAND;
    + break;
    + case 100: /* for EWS4800 kterm */
    + c = 0;
    + break;
    + case 39:
    + c &= 0xf0;
    + break;
    + case 49:
    + c &= 0x0f;
    + break;
    + default:
    + if (i >= 30 && i <= 37)
    + c = (c & 0xf0) | (i - 30) | 0x08;
    + else if (i >= 40 && i <= 47)
    + c = (c & 0x0f) | ((i - 40) << 4) | 0x80;
    + break;
    + }
    + if (*p == 'm')
    + break;
    + } else {
    + e = PE_NORMAL;
    + c = 0;
    + break;
    + }
    + p++; /* *p == ';' */
    + }
    + *effect = e;
    + *color = c;
    + return 1;
    +}
    +#endif
    +
     /*
      * Check character type
      */
     
     Str
    -checkType(Str s, Lineprop * oprop, int len)
    +checkType(Str s, Lineprop * oprop, Linecolor ** ocolor, int len)
     {
         Lineprop mode;
         Lineprop effect = PE_NORMAL;
         Lineprop *prop = oprop;
         char *str = s->ptr, *endp = &s->ptr[s->length], *bs = NULL;
    +#ifdef ANSI_COLOR
    + Lineprop ceffect = PE_NORMAL;
    + Linecolor cmode = 0;
    + Linecolor *color = NULL;
    + char *es = NULL;
    +#endif
         int do_copy = FALSE;
         int size = (len < s->length) ? len : s->length;
     
    - if (ShowEffect && (bs = memchr(str, '\b', s->length)) ||
    - s->length > size) {
    - s = Strnew_size(size);
    - do_copy = TRUE;
    + if (ShowEffect) {
    + bs = memchr(str, '\b', s->length);
    +#ifdef ANSI_COLOR
    + if (ocolor && *ocolor) {
    + es = memchr(str, ESC_CODE, s->length);
    + if (es)
    + color = *ocolor;
    + else
    + *ocolor = NULL;
    + }
    +#endif
    + if (s->length > size || (bs != NULL)
    +#ifdef ANSI_COLOR
    + || (es != NULL)
    +#endif
    + ) {
    + s = Strnew_size(size);
    + do_copy = TRUE;
    + }
         }
     
         while (str < endp) {
    @@ -403,9 +498,28 @@
                     bs = memchr(str, '\b', endp - str);
                     continue;
                 }
    +#ifdef ANSI_COLOR
    + else if (str > bs)
    + bs = memchr(str, '\b', endp - str);
    +#endif
    + }
    +#ifdef ANSI_COLOR
    + if (es != NULL) {
    + if (str == es) {
    + int ok = parse_ansi_color(&str, &ceffect, &cmode);
    + es = memchr(str, ESC_CODE, endp - str);
    + if (ok)
    + continue;
    + }
    + else if (str > es)
    + es = memchr(str, ESC_CODE, endp - str);
             }
    +#endif
     
             mode = get_mctype(str);
    +#ifdef ANSI_COLOR
    + effect |= ceffect;
    +#endif
     #ifdef JP_CHARSET
             if (mode == PC_KANJI) {
                 prop[0] = (effect | PC_KANJI1);
    @@ -416,6 +530,13 @@
                 }
                 prop += 2;
                 str += 2;
    +#ifdef ANSI_COLOR
    + if (color) {
    + color[0] = cmode;
    + color[1] = cmode;
    + color += 2;
    + }
    +#endif
             }
             else
     #endif /* JP_CHARSET */
    @@ -425,6 +546,12 @@
                     Strcat_char(s, *str);
                 prop++;
                 str++;
    +#ifdef ANSI_COLOR
    + if (color) {
    + *color = cmode;
    + color++;
    + }
    +#endif
             }
             effect = PE_NORMAL;
         }
    diff -u file.c.orig file.c
    --- file.c.orig Sun Dec 17 17:50:13 2000
    +++ file.c Wed Dec 20 01:46:17 2000
    @@ -34,9 +34,13 @@
                               Buffer * (*loadproc) (URLFile *, Buffer *),
                               Buffer *defaultbuf);
     static void close_textarea(struct html_feed_environ *h_env);
    -static void addnewline(Buffer * buf, char *line, Lineprop * prop, int pos, int nlines);
    +static void addnewline(Buffer * buf, char *line, Lineprop * prop, Linecolor * color, int pos, int nlines);
     
     static Lineprop propBuffer[LINELEN];
    +#ifdef ANSI_COLOR
    +static Linecolor colorBuffer[LINELEN];
    +#endif
    +static Linecolor *colorBufferPtr = NULL;
     
     static JMP_BUF AbortLoading;
     
    @@ -493,11 +497,11 @@
                 tmp = Strnew_size(lineBuf2->length);
                 for (p = lineBuf2->ptr; *p; p = q) {
                     for (q = p; *q && *q != '\r' && *q != '\n'; q++);
    - lineBuf2 = checkType(Strnew_charp(p), propBuffer,
    + lineBuf2 = checkType(Strnew_charp(p), propBuffer, NULL,
                                          min(LINELEN, q - p));
                     Strcat(tmp, lineBuf2);
                     if (thru)
    - addnewline(newBuf, lineBuf2->ptr, propBuffer,
    + addnewline(newBuf, lineBuf2->ptr, propBuffer, NULL,
                                    lineBuf2->length, -1);
                     for (; *q && (*q == '\r' || *q == '\n'); q++);
                 }
    @@ -697,7 +701,7 @@
             lineBuf2 = NULL;
         }
         if (thru)
    - addnewline(newBuf, "\n", propBuffer, 1, -1);
    + addnewline(newBuf, "\n", propBuffer, NULL, 1, -1);
     }
     
     char *
    @@ -3844,7 +3848,7 @@
                 }
             }
             /* end of processing for one line */
    - addnewline(buf, outc, outp, pos, nlines);
    + addnewline(buf, outc, outp, NULL, pos, nlines);
             if (str != endp) {
                 line = Strsubstr(line, str - line->ptr, endp - str);
                 goto proc_again;
    @@ -4324,7 +4328,7 @@
     extern Lineprop NullProp[];
     
     static void
    -addnewline(Buffer * buf, char *line, Lineprop * prop, int pos, int nlines)
    +addnewline(Buffer * buf, char *line, Lineprop * prop, Linecolor * color, int pos, int nlines)
     {
         Line *l;
         l = New(Line);
    @@ -4332,12 +4336,20 @@
         if (pos > 0) {
             l->lineBuf = allocStr(line, pos);
             l->propBuf = New_N(Lineprop, pos);
    + bcopy((void *) prop, (void *) l->propBuf, pos * sizeof(Lineprop));
         }
         else {
             l->lineBuf = NullLine;
             l->propBuf = NullProp;
         }
    - bcopy((void *) prop, (void *) l->propBuf, pos * sizeof(Lineprop));
    +#ifdef ANSI_COLOR
    + if (pos > 0 && color) {
    + l->colorBuf = New_N(Linecolor, pos);
    + bcopy((void *) color, (void *) l->colorBuf, pos * sizeof(Linecolor));
    + } else {
    + l->colorBuf = NULL;
    + }
    +#endif
         l->len = pos;
         l->prev = buf->currentLine;
         if (buf->currentLine) {
    @@ -4903,8 +4915,12 @@
             }
     #endif /* USE_NNTP */
             Strchop(lineBuf2);
    - lineBuf2 = checkType(lineBuf2, propBuffer, LINELEN);
    - addnewline(newBuf, lineBuf2->ptr, propBuffer, lineBuf2->length, nlines);
    +#ifdef ANSI_COLOR
    + colorBufferPtr = ShowEffect ? colorBuffer : NULL;
    +#endif
    + lineBuf2 = checkType(lineBuf2, propBuffer, &colorBufferPtr, LINELEN);
    + addnewline(newBuf, lineBuf2->ptr, propBuffer, colorBufferPtr,
    + lineBuf2->length, nlines);
         }
       _end:
         if (fmInitialized) {
    @@ -5164,12 +5180,23 @@
                 lineBuf2 = tmp;
             }
             Strchop(lineBuf2);
    - lineBuf2 = checkType(lineBuf2, propBuffer, LINELEN);
    +#ifdef ANSI_COLOR
    + colorBufferPtr = ShowEffect ? colorBuffer : NULL;
    +#endif
    + lineBuf2 = checkType(lineBuf2, propBuffer, &colorBufferPtr, LINELEN);
             len = lineBuf2->length;
             l = New(Line);
             l->lineBuf = lineBuf2->ptr;
             l->propBuf = New_N(Lineprop, len);
             bcopy((void *) propBuffer, (void *) l->propBuf, len * sizeof(Lineprop));
    +#ifdef ANSI_COLOR
    + if (colorBufferPtr) {
    + l->colorBuf = New_N(Linecolor, len);
    + bcopy((void *) colorBuffer, (void *) l->colorBuf, len * sizeof(Linecolor));
    + } else {
    + l->colorBuf = NULL;
    + }
    +#endif
             l->len = len;
             l->prev = pl;
             if (squeezeBlankLine) {
    diff -u fm.h.orig fm.h
    --- fm.h.orig Sun Dec 17 02:15:16 2000
    +++ fm.h Wed Dec 20 00:45:34 2000
    @@ -21,6 +21,10 @@
     #define MENU_SELECT
     #endif /* MENU */
     
    +#ifndef COLOR
    +#undef ANSI_COLOR
    +#endif
    +
     #include "ctrlcode.h"
     #include "html.h"
     #include "gc.h"
    @@ -248,6 +252,7 @@
      */
     
     typedef unsigned short Lineprop;
    +typedef unsigned char Linecolor;
     
     typedef struct _MapList {
         Str name;
    @@ -258,6 +263,9 @@
     typedef struct _Line {
         char *lineBuf;
         Lineprop *propBuf;
    +#ifdef ANSI_COLOR
    + Linecolor *colorBuf;
    +#endif
         struct _Line *next;
         struct _Line *prev;
         short len;
    diff -u proto.h.orig proto.h
    --- proto.h.orig Sun Dec 17 01:59:35 2000
    +++ proto.h Wed Dec 20 01:47:06 2000
    @@ -168,6 +168,9 @@
     extern Line *redrawLine(Buffer * buf, Line * l, int i);
     extern int redrawLineRegion(Buffer * buf, Line * l, int i, int bpos, int epos);
     extern void do_effects(Lineprop m);
    +#ifdef ANSI_COLOR
    +extern void do_color(Linecolor c);
    +#endif
     extern void addChar(char c, Lineprop mode);
     extern void message(char *s, int return_x, int return_y);
     extern void disp_message_nsec(char *s, int redraw_current, int sec, int purge, int mouse);
    @@ -192,7 +195,7 @@
     extern Line *lineSkip(Buffer * buf, Line * line, int offset, int last);
     extern int gethtmlcmd(char **s, int *status);
     extern char *getAnchor(char *arg, char **arg_return);
    -extern Str checkType(Str s, Lineprop * oprop, int len);
    +extern Str checkType(Str s, Lineprop * oprop, Linecolor ** ocolor, int len);
     extern int calcPosition(char *l, Lineprop *pr, int len, int pos, int bpos, int mode);
     extern char *lastFileName(char *path);
     extern char *mybasename(char *s);



    This archive was generated by hypermail 2b29 : Tue Dec 19 2000 - 11:41:19 CST