Annotation of researchv9/jtools/src/sux/ptofchar.c, revision 1.1

1.1     ! root        1: #include <jerq.h>
        !             2: #include "frame.h"
        !             3: Point
        !             4: ptofchar(t, cn)
        !             5:        register Frame *t;
        !             6:        register cn;
        !             7: {
        !             8:        register Nchar *np;
        !             9:        register l, c;  /* line number, char number */
        !            10:        /* nlines-1 because we must do the frameop */
        !            11:        for(l=0,c=0,np=t->cpl; l<t->nlines-1 && c+*np<cn; l++)
        !            12:                c+= *np++;
        !            13:        frameop(t, opnull, startline(t, l), t->str.s+c, cn-c);
        !            14:        return endpoint;
        !            15: }
        !            16: /*
        !            17:  * Index of character at position pt in frame t.
        !            18:  * Returns (efficiently!) number of chars in frame if pt is off end.
        !            19:  */
        !            20: int
        !            21: charofpt(t, pt)
        !            22:        register Frame *t;
        !            23:        Point pt;
        !            24: {
        !            25:        register l, c, starty;  /* line, char posn */
        !            26:        register i, y=pt.y;
        !            27:        register Nchar *np;
        !            28:        register char *s;
        !            29:        pt.y-=(y-t->rect.origin.y)%newlnsz;     /* on a line bdry */
        !            30:        y=pt.y-t->rect.origin.y;
        !            31:        if((y/=newlnsz) > t->nlines)    /* y is line containing pt */
        !            32:                y=t->nlines;    /* not nlines-1 because of upcoming if */
        !            33:        for(c=0,l=0,np=t->cpl; l<y; l++)
        !            34:                c+= *np++;
        !            35:        if(y < t->nlines){
        !            36:                endpoint=startline(t, y);
        !            37:                starty=endpoint.y;
        !            38:                s=t->str.s+c;
        !            39:                for(i=0; i<*np; i++,c++){
        !            40:                        frameop(t, opnull, endpoint, s++, 1/* sigh */);
        !            41:                        if(endpoint.x>pt.x || endpoint.y!=starty)
        !            42:                                break;
        !            43:                }
        !            44:        }
        !            45:        return c;
        !            46: }
        !            47: /*
        !            48:  * Point at start of line n in frame t
        !            49:  */
        !            50: Point
        !            51: startline(t, n)
        !            52:        register Frame *t;
        !            53:        register n;
        !            54: {
        !            55:        Point pt;
        !            56:        pt=t->rect.origin;
        !            57:        pt.y+=n*newlnsz;
        !            58:        return pt;
        !            59: }
        !            60: /*
        !            61:  * Calculate and set the number of characters per line in lines
        !            62:  * first through last, inclusive, in frame t.
        !            63:  */
        !            64: setcpl(t, first, last)
        !            65:        register Frame *t;
        !            66: {
        !            67:        register Nchar *np;
        !            68:        register l, c;  /* lines, characters */
        !            69:        
        !            70:        if(last>=t->nlines)
        !            71:                last=t->nlines-1;
        !            72:        for(c=0,l=0,np=t->cpl; l<first; l++)
        !            73:                c+= *np++;
        !            74:        /* c now tracks number of chars before line of interest */
        !            75:        while(l<=last && c<t->str.n)
        !            76:                c+= *np++=cpl(t, c, startline(t, l++));
        !            77:        while(l++ <= last)
        !            78:                *np++=0;
        !            79: }
        !            80: /* Frameop operator to calculate number of chars in a line */
        !            81: static nc, lasty, wrapped;
        !            82: /*ARGSUSED*/
        !            83: void
        !            84: opcpl(t, p, q, cp, n)
        !            85:        Frame *t;
        !            86:        Point p, q;
        !            87:        char *cp;
        !            88:        int n;
        !            89: {
        !            90:        if(p.y==lasty)
        !            91:                nc+=n;
        !            92:        else
        !            93:                wrapped=TRUE;
        !            94: }
        !            95: cpl(t, posn, pt)
        !            96:        register Frame *t;
        !            97:        int posn;
        !            98:        Point pt;
        !            99: {
        !           100:        register ntotry=(t->rect.corner.x-t->rect.origin.x)/cwidth('1');
        !           101:        /*
        !           102:         * If there are no control chars, you can't get more than
        !           103:         * (linewidth/charwidth) chars on a line, so to be safe we
        !           104:         * keep trying if the frameop didn't reach the end of the line.
        !           105:         */
        !           106:        lasty=pt.y;
        !           107:        do{
        !           108:                if((ntotry+=7) > t->str.n-posn) /* 7 is arbitrary */
        !           109:                        ntotry=t->str.n-posn;
        !           110:                nc=0;
        !           111:                wrapped=FALSE;
        !           112:                frameop(t, opcpl, pt, t->str.s+posn, ntotry);
        !           113:        }while(!wrapped && complete && posn+ntotry<t->str.n);
        !           114:        return nc;
        !           115: }
        !           116: /*
        !           117:  * Scroll first through last entries in the cpl array by n.
        !           118:  * n<0 ==> up screen, n>0 ==> down screen.  First and last must
        !           119:  * be in the correct order in the call, i.e. first>last for n>0,
        !           120:  * first<last for n<0.
        !           121:  */
        !           122: scrollcpl(t, first, last, n)
        !           123:        Frame *t;
        !           124:        register first, last, n;
        !           125: {
        !           126:        register i;
        !           127:        register Nchar *np= &t->cpl[first];
        !           128:        register delta=(n>0)? -1 : 1;
        !           129:        if(first>last){
        !           130:                i=first;
        !           131:                first=last;
        !           132:                last=i;
        !           133:        }
        !           134:        for(i=first; i<=last; i++,np+=delta)
        !           135:                np[n]=np[0];
        !           136: }
        !           137: lineno(t, y)
        !           138:        register Frame *t;
        !           139:        register y;
        !           140: {
        !           141:        return (y-t->rect.origin.y)/newlnsz;
        !           142: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.