[BACK]Return to paragraph.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/paragraph.c, Revision 1.16

1.16    ! kjell       1: /*     $OpenBSD: paragraph.c,v 1.15 2006/11/17 08:45:31 kjell Exp $    */
1.11      kjell       2:
                      3: /* This file is in the public domain. */
1.4       niklas      4:
1.1       deraadt     5: /*
                      6:  * Code for dealing with paragraphs and filling. Adapted from MicroEMACS 3.6
                      7:  * and GNU-ified by mwm@ucbvax.         Several bug fixes by blarson@usc-oberon.
                      8:  */
1.3       millert     9:
1.1       deraadt    10: #include "def.h"
                     11:
1.3       millert    12: static int     fillcol = 70;
                     13:
1.1       deraadt    14: #define MAXWORD 256
                     15:
                     16: /*
1.10      db         17:  * Move to start of paragraph.  Go back to the beginning of the current
1.6       mickey     18:  * paragraph here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
1.10      db         19:  * combination to delimit the beginning of a paragraph.
1.1       deraadt    20:  */
1.2       millert    21: /* ARGSUSED */
1.3       millert    22: int
1.8       cloder     23: gotobop(int f, int n)
1.1       deraadt    24: {
1.3       millert    25:        /* the other way... */
                     26:        if (n < 0)
1.10      db         27:                return (gotoeop(f, -n));
1.1       deraadt    28:
1.3       millert    29:        while (n-- > 0) {
1.1       deraadt    30:                /* first scan back until we are in a word */
1.9       avsm       31:                while (backchar(FFRAND, 1) && inword() == 0);
1.2       millert    32:
1.3       millert    33:                /* and go to the B-O-Line */
                     34:                curwp->w_doto = 0;
1.1       deraadt    35:
1.2       millert    36:                /*
                     37:                 * and scan back until we hit a <NL><SP> <NL><TAB> or
                     38:                 * <NL><NL>
                     39:                 */
1.14      kjell      40:                while (lback(curwp->w_dotp) != curbp->b_headp)
1.7       deraadt    41:                        if (llength(lback(curwp->w_dotp)) &&
                     42:                            lgetc(curwp->w_dotp, 0) != ' ' &&
                     43:                            lgetc(curwp->w_dotp, 0) != '.' &&
                     44:                            lgetc(curwp->w_dotp, 0) != '\t')
1.1       deraadt    45:                                curwp->w_dotp = lback(curwp->w_dotp);
                     46:                        else {
1.7       deraadt    47:                                if (llength(lback(curwp->w_dotp)) &&
                     48:                                    lgetc(curwp->w_dotp, 0) == '.') {
1.2       millert    49:                                        curwp->w_dotp = lforw(curwp->w_dotp);
1.14      kjell      50:                                        if (curwp->w_dotp == curbp->b_headp) {
1.2       millert    51:                                                /*
1.10      db         52:                                                 * beyond end of buffer,
1.2       millert    53:                                                 * cleanup time
                     54:                                                 */
1.6       mickey     55:                                                curwp->w_dotp =
1.3       millert    56:                                                    lback(curwp->w_dotp);
1.6       mickey     57:                                                curwp->w_doto =
1.3       millert    58:                                                    llength(curwp->w_dotp);
1.2       millert    59:                                        }
                     60:                                }
                     61:                                break;
                     62:                        }
1.1       deraadt    63:        }
1.3       millert    64:        /* force screen update */
                     65:        curwp->w_flag |= WFMOVE;
1.10      db         66:        return (TRUE);
1.1       deraadt    67: }
                     68:
                     69: /*
1.10      db         70:  * Move to end of paragraph.  Go forward to the end of the current paragraph
1.6       mickey     71:  * here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE> combination to
1.10      db         72:  * delimit the beginning of a paragraph.
1.1       deraadt    73:  */
1.2       millert    74: /* ARGSUSED */
1.3       millert    75: int
1.8       cloder     76: gotoeop(int f, int n)
1.1       deraadt    77: {
1.3       millert    78:        /* the other way... */
                     79:        if (n < 0)
1.10      db         80:                return (gotobop(f, -n));
1.1       deraadt    81:
1.3       millert    82:        /* for each one asked for */
                     83:        while (n-- > 0) {
1.1       deraadt    84:                /* Find the first word on/after the current line */
                     85:                curwp->w_doto = 0;
1.9       avsm       86:                while (forwchar(FFRAND, 1) && inword() == 0);
1.3       millert    87:
1.1       deraadt    88:                curwp->w_doto = 0;
                     89:                curwp->w_dotp = lforw(curwp->w_dotp);
1.3       millert    90:
1.1       deraadt    91:                /* and scan forword until we hit a <NL><SP> or ... */
1.14      kjell      92:                while (curwp->w_dotp != curbp->b_headp) {
1.7       deraadt    93:                        if (llength(curwp->w_dotp) &&
                     94:                            lgetc(curwp->w_dotp, 0) != ' ' &&
                     95:                            lgetc(curwp->w_dotp, 0) != '.' &&
                     96:                            lgetc(curwp->w_dotp, 0) != '\t')
1.1       deraadt    97:                                curwp->w_dotp = lforw(curwp->w_dotp);
                     98:                        else
                     99:                                break;
                    100:                }
1.14      kjell     101:                if (curwp->w_dotp == curbp->b_headp) {
1.10      db        102:                        /* beyond end of buffer, cleanup time */
1.1       deraadt   103:                        curwp->w_dotp = lback(curwp->w_dotp);
                    104:                        curwp->w_doto = llength(curwp->w_dotp);
1.2       millert   105:                        break;
1.1       deraadt   106:                }
                    107:        }
1.3       millert   108:        /* force screen update */
                    109:        curwp->w_flag |= WFMOVE;
1.10      db        110:        return (TRUE);
1.1       deraadt   111: }
                    112:
                    113: /*
1.6       mickey    114:  * Justify a paragraph.  Fill the current paragraph according to the current
1.3       millert   115:  * fill column.
1.1       deraadt   116:  */
1.2       millert   117: /* ARGSUSED */
1.3       millert   118: int
1.8       cloder    119: fillpara(int f, int n)
1.1       deraadt   120: {
1.10      db        121:        int      c;             /* current char during scan             */
1.3       millert   122:        int      wordlen;       /* length of current word               */
                    123:        int      clength;       /* position on line during fill         */
                    124:        int      i;             /* index during word copy               */
                    125:        int      eopflag;       /* Are we at the End-Of-Paragraph?      */
                    126:        int      firstflag;     /* first word? (needs no space)         */
                    127:        int      newlength;     /* tentative new line length            */
                    128:        int      eolflag;       /* was at end of line                   */
1.12      kjell     129:        int      retval;        /* return value                         */
1.13      deraadt   130:        struct line     *eopline;       /* pointer to line just past EOP        */
1.3       millert   131:        char     wbuf[MAXWORD]; /* buffer for current word              */
1.1       deraadt   132:
1.12      kjell     133:        undo_add_boundary();
1.15      kjell     134:        undo_boundary_enable(FALSE);
1.12      kjell     135:
1.1       deraadt   136:        /* record the pointer to the line just past the EOP */
1.5       art       137:        (void)gotoeop(FFRAND, 1);
1.2       millert   138:        if (curwp->w_doto != 0) {
1.1       deraadt   139:                /* paragraph ends at end of buffer */
1.5       art       140:                (void)lnewline();
1.1       deraadt   141:                eopline = lforw(curwp->w_dotp);
1.2       millert   142:        } else
                    143:                eopline = curwp->w_dotp;
1.1       deraadt   144:
                    145:        /* and back top the begining of the paragraph */
1.5       art       146:        (void)gotobop(FFRAND, 1);
1.1       deraadt   147:
                    148:        /* initialize various info */
1.9       avsm      149:        while (inword() == 0 && forwchar(FFRAND, 1));
1.3       millert   150:
1.1       deraadt   151:        clength = curwp->w_doto;
                    152:        wordlen = 0;
                    153:
                    154:        /* scan through lines, filling words */
                    155:        firstflag = TRUE;
                    156:        eopflag = FALSE;
                    157:        while (!eopflag) {
1.3       millert   158:
1.1       deraadt   159:                /* get the next character in the paragraph */
1.3       millert   160:                if ((eolflag = (curwp->w_doto == llength(curwp->w_dotp)))) {
1.1       deraadt   161:                        c = ' ';
                    162:                        if (lforw(curwp->w_dotp) == eopline)
                    163:                                eopflag = TRUE;
                    164:                } else
                    165:                        c = lgetc(curwp->w_dotp, curwp->w_doto);
                    166:
                    167:                /* and then delete it */
1.12      kjell     168:                if (ldelete((RSIZE) 1, KNONE) == FALSE && !eopflag) {
                    169:                        retval = FALSE;
                    170:                        goto cleanup;
                    171:                }
1.1       deraadt   172:
                    173:                /* if not a separator, just add it in */
                    174:                if (c != ' ' && c != '\t') {
                    175:                        if (wordlen < MAXWORD - 1)
                    176:                                wbuf[wordlen++] = c;
                    177:                        else {
1.2       millert   178:                                /*
                    179:                                 * You loose chars beyond MAXWORD if the word
1.1       deraadt   180:                                 * is to long. I'm to lazy to fix it now; it
1.2       millert   181:                                 * just silently truncated the word before,
                    182:                                 * so I get to feel smug.
1.1       deraadt   183:                                 */
                    184:                                ewprintf("Word too long!");
                    185:                        }
                    186:                } else if (wordlen) {
1.3       millert   187:
1.10      db        188:                        /* calculate tentative new length with word added */
1.1       deraadt   189:                        newlength = clength + 1 + wordlen;
1.3       millert   190:
1.2       millert   191:                        /*
                    192:                         * if at end of line or at doublespace and previous
1.1       deraadt   193:                         * character was one of '.','?','!' doublespace here.
                    194:                         */
1.6       mickey    195:                        if ((eolflag ||
                    196:                            curwp->w_doto == llength(curwp->w_dotp) ||
1.3       millert   197:                            (c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' '
1.6       mickey    198:                            || c == '\t') && ISEOSP(wbuf[wordlen - 1]) &&
1.3       millert   199:                            wordlen < MAXWORD - 1)
1.1       deraadt   200:                                wbuf[wordlen++] = ' ';
1.3       millert   201:
1.1       deraadt   202:                        /* at a word break with a word waiting */
                    203:                        if (newlength <= fillcol) {
                    204:                                /* add word to current line */
                    205:                                if (!firstflag) {
1.5       art       206:                                        (void)linsert(1, ' ');
1.1       deraadt   207:                                        ++clength;
                    208:                                }
                    209:                                firstflag = FALSE;
                    210:                        } else {
1.2       millert   211:                                if (curwp->w_doto > 0 &&
                    212:                                    lgetc(curwp->w_dotp, curwp->w_doto - 1) == ' ') {
1.1       deraadt   213:                                        curwp->w_doto -= 1;
1.5       art       214:                                        (void)ldelete((RSIZE) 1, KNONE);
1.1       deraadt   215:                                }
                    216:                                /* start a new line */
1.5       art       217:                                (void)lnewline();
1.1       deraadt   218:                                clength = 0;
                    219:                        }
                    220:
                    221:                        /* and add the word in in either case */
1.2       millert   222:                        for (i = 0; i < wordlen; i++) {
1.5       art       223:                                (void)linsert(1, wbuf[i]);
1.1       deraadt   224:                                ++clength;
                    225:                        }
                    226:                        wordlen = 0;
                    227:                }
                    228:        }
                    229:        /* and add a last newline for the end of our new paragraph */
1.5       art       230:        (void)lnewline();
1.3       millert   231:
1.2       millert   232:        /*
1.10      db        233:         * We really should wind up where we started, (which is hard to keep
1.1       deraadt   234:         * track of) but I think the end of the last line is better than the
1.10      db        235:         * beginning of the blank line.
1.2       millert   236:         */
1.5       art       237:        (void)backchar(FFRAND, 1);
1.12      kjell     238:        retval = TRUE;
                    239: cleanup:
1.15      kjell     240:        undo_boundary_enable(TRUE);
1.12      kjell     241:        undo_add_boundary();
                    242:        return (retval);
1.1       deraadt   243: }
                    244:
1.6       mickey    245: /*
1.3       millert   246:  * Delete a paragraph.  Delete n paragraphs starting with the current one.
                    247:  */
1.2       millert   248: /* ARGSUSED */
1.3       millert   249: int
1.8       cloder    250: killpara(int f, int n)
1.1       deraadt   251: {
1.3       millert   252:        int     status;         /* returned status of functions */
1.1       deraadt   253:
1.3       millert   254:        /* for each paragraph to delete */
                    255:        while (n--) {
1.1       deraadt   256:
1.10      db        257:                /* mark out the end and beginning of the para to delete */
1.5       art       258:                (void)gotoeop(FFRAND, 1);
1.1       deraadt   259:
                    260:                /* set the mark here */
                    261:                curwp->w_markp = curwp->w_dotp;
                    262:                curwp->w_marko = curwp->w_doto;
                    263:
1.10      db        264:                /* go to the beginning of the paragraph */
1.5       art       265:                (void)gotobop(FFRAND, 1);
1.3       millert   266:
                    267:                /* force us to the beginning of line */
                    268:                curwp->w_doto = 0;
1.1       deraadt   269:
                    270:                /* and delete it */
                    271:                if ((status = killregion(FFRAND, 1)) != TRUE)
1.10      db        272:                        return (status);
1.1       deraadt   273:
                    274:                /* and clean up the 2 extra lines */
1.5       art       275:                (void)ldelete((RSIZE) 1, KFORW);
1.1       deraadt   276:        }
1.10      db        277:        return (TRUE);
1.1       deraadt   278: }
                    279:
                    280: /*
1.3       millert   281:  * Insert char with work wrap.  Check to see if we're past fillcol, and if so,
                    282:  * justify this line.  As a last step, justify the line.
1.1       deraadt   283:  */
1.2       millert   284: /* ARGSUSED */
1.3       millert   285: int
1.8       cloder    286: fillword(int f, int n)
1.1       deraadt   287: {
1.3       millert   288:        char    c;
                    289:        int     col, i, nce;
1.1       deraadt   290:
                    291:        for (i = col = 0; col <= fillcol; ++i, ++col) {
1.2       millert   292:                if (i == curwp->w_doto)
                    293:                        return selfinsert(f, n);
1.1       deraadt   294:                c = lgetc(curwp->w_dotp, i);
                    295:                if (c == '\t'
1.3       millert   296: #ifdef NOTAB
1.2       millert   297:                    && !(curbp->b_flag & BFNOTAB)
1.1       deraadt   298: #endif
1.2       millert   299:                        )
                    300:                        col |= 0x07;
                    301:                else if (ISCTRL(c) != FALSE)
                    302:                        ++col;
1.1       deraadt   303:        }
                    304:        if (curwp->w_doto != llength(curwp->w_dotp)) {
1.5       art       305:                (void)selfinsert(f, n);
1.1       deraadt   306:                nce = llength(curwp->w_dotp) - curwp->w_doto;
1.2       millert   307:        } else
                    308:                nce = 0;
1.1       deraadt   309:        curwp->w_doto = i;
                    310:
                    311:        if ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' && c != '\t')
                    312:                do {
1.5       art       313:                        (void)backchar(FFRAND, 1);
1.7       deraadt   314:                } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' &&
                    315:                    c != '\t' && curwp->w_doto > 0);
1.1       deraadt   316:
                    317:        if (curwp->w_doto == 0)
                    318:                do {
1.5       art       319:                        (void)forwchar(FFRAND, 1);
1.7       deraadt   320:                } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) != ' ' &&
                    321:                    c != '\t' && curwp->w_doto < llength(curwp->w_dotp));
1.1       deraadt   322:
1.5       art       323:        (void)delwhite(FFRAND, 1);
                    324:        (void)lnewline();
1.1       deraadt   325:        i = llength(curwp->w_dotp) - nce;
1.2       millert   326:        curwp->w_doto = i > 0 ? i : 0;
1.1       deraadt   327:        curwp->w_flag |= WFMOVE;
1.2       millert   328:        if (nce == 0 && curwp->w_doto != 0)
1.10      db        329:                return (fillword(f, n));
                    330:        return (TRUE);
1.1       deraadt   331: }
                    332:
1.3       millert   333: /*
                    334:  * Set fill column to n for justify.
                    335:  */
                    336: int
1.8       cloder    337: setfillcol(int f, int n)
1.2       millert   338: {
1.16    ! kjell     339:        char buf[32], *rep;
        !           340:        const char *es;
        !           341:
        !           342:        if ((f & FFARG) != 0) {
        !           343:                fillcol = n;
        !           344:        } else {
        !           345:                if ((rep = eread("Set fill-column: ", buf, sizeof(buf),
        !           346:                    EFNEW | EFCR)) == NULL)
        !           347:                        return (ABORT);
        !           348:                else if (rep[0] == '\0')
        !           349:                        return (FALSE);
        !           350:                fillcol = strtonum(rep, 0, INT_MAX, &es);
        !           351:                if (es != NULL)
        !           352:                        return (FALSE);
        !           353:                ewprintf("Fill column set to %d", fillcol);
        !           354:        }
1.10      db        355:        return (TRUE);
1.1       deraadt   356: }