Annotation of researchv9/jerq/src/mux/term/frame/ptofchar.c, revision 1.1

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

unix.superglobalmegacorp.com

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