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

Annotation of src/usr.bin/mg/util.c, Revision 1.30

1.30    ! kjell       1: /*     $OpenBSD: random.c,v 1.29 2011/01/19 16:11:38 kjell Exp $       */
1.12      kjell       2:
                      3: /* This file is in the public domain. */
1.4       niklas      4:
1.1       deraadt     5: /*
                      6:  *             Assorted commands.
1.6       mickey      7:  * This file contains the command processors for a large assortment of
                      8:  * unrelated commands.  The only thing they have in common is that they
1.3       millert     9:  * are all command processors.
                     10:  */
                     11:
                     12: #include "def.h"
1.30    ! kjell      13:
1.9       vincent    14: #include <ctype.h>
1.1       deraadt    15:
                     16: /*
1.6       mickey     17:  * Display a bunch of useful information about the current location of dot.
                     18:  * The character under the cursor (in octal), the current line, row, and
                     19:  * column, and approximate position of the cursor in the file (as a
                     20:  * percentage) is displayed.  The column position assumes an infinite
1.3       millert    21:  * position display; it does not truncate just because the screen does.
1.1       deraadt    22:  * This is normally bound to "C-X =".
                     23:  */
1.2       millert    24: /* ARGSUSED */
1.3       millert    25: int
1.10      cloder     26: showcpos(int f, int n)
1.1       deraadt    27: {
1.15      deraadt    28:        struct line     *clp;
1.11      db         29:        long     nchar, cchar;
1.3       millert    30:        int      nline, row;
                     31:        int      cline, cbyte;          /* Current line/char/byte */
                     32:        int      ratio;
                     33:
                     34:        /* collect the data */
1.22      kjell      35:        clp = bfirstlp(curbp);
1.3       millert    36:        cchar = 0;
                     37:        cline = 0;
                     38:        cbyte = 0;
1.1       deraadt    39:        nchar = 0;
                     40:        nline = 0;
1.6       mickey     41:        for (;;) {
1.3       millert    42:                /* count this line */
                     43:                ++nline;
1.1       deraadt    44:                if (clp == curwp->w_dotp) {
1.3       millert    45:                        /* mark line */
                     46:                        cline = nline;
1.1       deraadt    47:                        cchar = nchar + curwp->w_doto;
                     48:                        if (curwp->w_doto == llength(clp))
                     49:                                cbyte = '\n';
                     50:                        else
                     51:                                cbyte = lgetc(clp, curwp->w_doto);
                     52:                }
1.3       millert    53:                /* now count the chars */
                     54:                nchar += llength(clp);
1.1       deraadt    55:                clp = lforw(clp);
1.21      kjell      56:                if (clp == curbp->b_headp)
1.2       millert    57:                        break;
1.3       millert    58:                /* count the newline */
                     59:                nchar++;
1.1       deraadt    60:        }
1.3       millert    61:        /* determine row */
                     62:        row = curwp->w_toprow + 1;
1.1       deraadt    63:        clp = curwp->w_linep;
1.21      kjell      64:        while (clp != curbp->b_headp && clp != curwp->w_dotp) {
1.1       deraadt    65:                ++row;
                     66:                clp = lforw(clp);
                     67:        }
1.2       millert    68:        /* NOSTRICT */
                     69:        ratio = nchar ? (100L * cchar) / nchar : 100;
1.1       deraadt    70:        ewprintf("Char: %c (0%o)  point=%ld(%d%%)  line=%d  row=%d  col=%d",
1.3       millert    71:            cbyte, cbyte, cchar, ratio, cline, row, getcolpos());
1.11      db         72:        return (TRUE);
1.1       deraadt    73: }
                     74:
1.3       millert    75: int
1.9       vincent    76: getcolpos(void)
1.2       millert    77: {
1.3       millert    78:        int     col, i, c;
1.20      kjell      79:        char tmp[5];
1.3       millert    80:
                     81:        /* determine column */
1.9       vincent    82:        col = 0;
1.1       deraadt    83:
1.2       millert    84:        for (i = 0; i < curwp->w_doto; ++i) {
1.1       deraadt    85:                c = lgetc(curwp->w_dotp, i);
                     86:                if (c == '\t'
1.3       millert    87: #ifdef NOTAB
1.2       millert    88:                    && !(curbp->b_flag & BFNOTAB)
1.3       millert    89: #endif /* NOTAB */
1.1       deraadt    90:                        ) {
1.2       millert    91:                        col |= 0x07;
1.9       vincent    92:                        col++;
1.1       deraadt    93:                } else if (ISCTRL(c) != FALSE)
1.9       vincent    94:                        col += 2;
1.20      kjell      95:                else if (isprint(c)) {
1.9       vincent    96:                        col++;
1.20      kjell      97:                } else {
                     98:                        col += snprintf(tmp, sizeof(tmp), "\\%o", c);
1.9       vincent    99:                }
                    100:
1.1       deraadt   101:        }
1.11      db        102:        return (col);
1.1       deraadt   103: }
1.3       millert   104:
1.1       deraadt   105: /*
1.6       mickey    106:  * Twiddle the two characters on either side of dot.  If dot is at the end
                    107:  * of the line twiddle the two characters before it.  Return with an error
                    108:  * if dot is at the beginning of line; it seems to be a bit pointless to
                    109:  * make this work.  This fixes up a very common typo with a single stroke.
                    110:  * Normally bound to "C-T".  This always works within a line, so "WFEDIT"
1.3       millert   111:  * is good enough.
1.1       deraadt   112:  */
1.2       millert   113: /* ARGSUSED */
1.3       millert   114: int
1.10      cloder    115: twiddle(int f, int n)
1.1       deraadt   116: {
1.15      deraadt   117:        struct line     *dotp;
1.3       millert   118:        int      doto, cr;
1.18      kjell     119:        int      fudge = FALSE;
1.1       deraadt   120:
                    121:        dotp = curwp->w_dotp;
                    122:        doto = curwp->w_doto;
1.2       millert   123:        if (doto == llength(dotp)) {
                    124:                if (--doto <= 0)
1.11      db        125:                        return (FALSE);
1.19      kjell     126:                (void)backchar(FFRAND, 1);
1.18      kjell     127:                fudge = TRUE;
1.1       deraadt   128:        } else {
1.2       millert   129:                if (doto == 0)
1.11      db        130:                        return (FALSE);
1.1       deraadt   131:        }
1.29      kjell     132:        undo_boundary_enable(FFRAND, 0);
1.18      kjell     133:        cr = lgetc(dotp, doto - 1);
1.19      kjell     134:        (void)backdel(FFRAND, 1);
                    135:        (void)forwchar(FFRAND, 1);
1.18      kjell     136:        linsert(1, cr);
                    137:        if (fudge != TRUE)
1.19      kjell     138:                (void)backchar(FFRAND, 1);
1.26      kjell     139:        undo_boundary_enable(FFRAND, 1);
1.1       deraadt   140:        lchange(WFEDIT);
1.11      db        141:        return (TRUE);
1.1       deraadt   142: }
                    143:
                    144: /*
1.6       mickey    145:  * Open up some blank space.  The basic plan is to insert a bunch of
                    146:  * newlines, and then back up over them.  Everything is done by the
1.11      db        147:  * subcommand processors.  They even handle the looping.  Normally this
1.3       millert   148:  * is bound to "C-O".
1.1       deraadt   149:  */
1.2       millert   150: /* ARGSUSED */
1.3       millert   151: int
1.10      cloder    152: openline(int f, int n)
1.1       deraadt   153: {
1.11      db        154:        int     i, s;
1.1       deraadt   155:
                    156:        if (n < 0)
1.11      db        157:                return (FALSE);
1.1       deraadt   158:        if (n == 0)
1.11      db        159:                return (TRUE);
1.3       millert   160:
                    161:        /* insert newlines */
1.29      kjell     162:        undo_boundary_enable(FFRAND, 0);
1.3       millert   163:        i = n;
1.1       deraadt   164:        do {
                    165:                s = lnewline();
1.2       millert   166:        } while (s == TRUE && --i);
1.3       millert   167:
                    168:        /* then go back up overtop of them all */
                    169:        if (s == TRUE)
                    170:                s = backchar(f | FFRAND, n);
1.29      kjell     171:        undo_boundary_enable(FFRAND, 1);
1.11      db        172:        return (s);
1.1       deraadt   173: }
                    174:
                    175: /*
1.14      kjell     176:  * Insert a newline.
1.1       deraadt   177:  */
1.2       millert   178: /* ARGSUSED */
1.3       millert   179: int
1.10      cloder    180: newline(int f, int n)
1.1       deraadt   181: {
1.3       millert   182:        int      s;
1.1       deraadt   183:
1.2       millert   184:        if (n < 0)
1.11      db        185:                return (FALSE);
1.3       millert   186:
1.1       deraadt   187:        while (n--) {
1.2       millert   188:                if ((s = lnewline()) != TRUE)
1.11      db        189:                        return (s);
1.1       deraadt   190:        }
1.11      db        191:        return (TRUE);
1.1       deraadt   192: }
                    193:
                    194: /*
1.3       millert   195:  * Delete blank lines around dot. What this command does depends if dot is
1.6       mickey    196:  * sitting on a blank line. If dot is sitting on a blank line, this command
                    197:  * deletes all the blank lines above and below the current line. If it is
                    198:  * sitting on a non blank line then it deletes all of the blank lines after
                    199:  * the line. Normally this command is bound to "C-X C-O". Any argument is
1.3       millert   200:  * ignored.
1.1       deraadt   201:  */
1.2       millert   202: /* ARGSUSED */
1.3       millert   203: int
1.10      cloder    204: deblank(int f, int n)
1.1       deraadt   205: {
1.15      deraadt   206:        struct line     *lp1, *lp2;
1.3       millert   207:        RSIZE    nld;
1.1       deraadt   208:
                    209:        lp1 = curwp->w_dotp;
1.21      kjell     210:        while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_headp)
1.1       deraadt   211:                lp1 = lp2;
                    212:        lp2 = lp1;
1.3       millert   213:        nld = (RSIZE)0;
1.21      kjell     214:        while ((lp2 = lforw(lp2)) != curbp->b_headp && llength(lp2) == 0)
1.1       deraadt   215:                ++nld;
                    216:        if (nld == 0)
                    217:                return (TRUE);
                    218:        curwp->w_dotp = lforw(lp1);
                    219:        curwp->w_doto = 0;
1.11      db        220:        return (ldelete((RSIZE)nld, KNONE));
1.1       deraadt   221: }
                    222:
                    223: /*
                    224:  * Delete any whitespace around dot, then insert a space.
                    225:  */
1.3       millert   226: int
1.10      cloder    227: justone(int f, int n)
1.2       millert   228: {
1.29      kjell     229:        undo_boundary_enable(FFRAND, 0);
1.5       art       230:        (void)delwhite(f, n);
1.29      kjell     231:        linsert(1, ' ');
                    232:        undo_boundary_enable(FFRAND, 1);
                    233:        return (TRUE);
1.1       deraadt   234: }
1.3       millert   235:
1.1       deraadt   236: /*
                    237:  * Delete any whitespace around dot.
                    238:  */
1.2       millert   239: /* ARGSUSED */
1.3       millert   240: int
1.10      cloder    241: delwhite(int f, int n)
1.1       deraadt   242: {
1.25      kjell     243:        int     col, s;
1.1       deraadt   244:
                    245:        col = curwp->w_doto;
1.3       millert   246:
1.8       vincent   247:        while (col < llength(curwp->w_dotp) &&
1.25      kjell     248:            (isspace(lgetc(curwp->w_dotp, col))))
1.1       deraadt   249:                ++col;
                    250:        do {
                    251:                if (curwp->w_doto == 0) {
                    252:                        s = FALSE;
                    253:                        break;
                    254:                }
1.2       millert   255:                if ((s = backchar(FFRAND, 1)) != TRUE)
                    256:                        break;
1.25      kjell     257:        } while (isspace(lgetc(curwp->w_dotp, curwp->w_doto)));
1.1       deraadt   258:
1.2       millert   259:        if (s == TRUE)
1.5       art       260:                (void)forwchar(FFRAND, 1);
                    261:        (void)ldelete((RSIZE)(col - curwp->w_doto), KNONE);
1.11      db        262:        return (TRUE);
1.1       deraadt   263: }
1.3       millert   264:
1.1       deraadt   265: /*
1.25      kjell     266:  * Delete any leading whitespace on the current line
                    267:  */
                    268: int
                    269: delleadwhite(int f, int n)
                    270: {
                    271:        int soff, ls;
                    272:        struct line *slp;
                    273:
                    274:        /* Save current position */
                    275:        slp = curwp->w_dotp;
                    276:        soff = curwp->w_doto;
                    277:
                    278:        for (ls = 0; ls < llength(slp); ls++)
                    279:                  if (!isspace(lgetc(slp, ls)))
                    280:                         break;
                    281:        gotobol(FFRAND, 1);
                    282:        forwdel(FFRAND, ls);
                    283:        soff -= ls;
                    284:        if (soff < 0)
                    285:                soff = 0;
                    286:        forwchar(FFRAND, soff);
                    287:
                    288:        return (TRUE);
                    289: }
                    290:
                    291: /*
                    292:  * Delete any trailing whitespace on the current line
                    293:  */
                    294: int
                    295: deltrailwhite(int f, int n)
                    296: {
                    297:        int soff;
                    298:
                    299:        /* Save current position */
                    300:        soff = curwp->w_doto;
                    301:
                    302:        gotoeol(FFRAND, 1);
                    303:        delwhite(FFRAND, 1);
                    304:
                    305:        /* restore original position, if possible */
                    306:        if (soff < curwp->w_doto)
                    307:                curwp->w_doto = soff;
                    308:
                    309:        return (TRUE);
                    310: }
                    311:
                    312:
                    313:
                    314: /*
1.3       millert   315:  * Insert a newline, then enough tabs and spaces to duplicate the indentation
1.6       mickey    316:  * of the previous line.  Assumes tabs are every eight characters.  Quite
                    317:  * simple.  Figure out the indentation of the current line.  Insert a newline
                    318:  * by calling the standard routine.  Insert the indentation by inserting the
                    319:  * right number of tabs and spaces.  Return TRUE if all ok.  Return FALSE if
1.24      kjell     320:  * one of the subcommands failed. Normally bound to "C-M".
1.1       deraadt   321:  */
1.2       millert   322: /* ARGSUSED */
1.3       millert   323: int
1.25      kjell     324: lfindent(int f, int n)
1.1       deraadt   325: {
1.11      db        326:        int     c, i, nicol;
1.29      kjell     327:        int     s = TRUE;
1.1       deraadt   328:
1.2       millert   329:        if (n < 0)
                    330:                return (FALSE);
1.3       millert   331:
1.29      kjell     332:        undo_boundary_enable(FFRAND, 0);
1.1       deraadt   333:        while (n--) {
                    334:                nicol = 0;
1.2       millert   335:                for (i = 0; i < llength(curwp->w_dotp); ++i) {
1.1       deraadt   336:                        c = lgetc(curwp->w_dotp, i);
1.2       millert   337:                        if (c != ' ' && c != '\t')
1.1       deraadt   338:                                break;
                    339:                        if (c == '\t')
                    340:                                nicol |= 0x07;
                    341:                        ++nicol;
                    342:                }
                    343:                if (lnewline() == FALSE || ((
                    344: #ifdef NOTAB
1.3       millert   345:                    curbp->b_flag & BFNOTAB) ? linsert(nicol, ' ') == FALSE : (
                    346: #endif /* NOTAB */
                    347:                    ((i = nicol / 8) != 0 && linsert(i, '\t') == FALSE) ||
1.29      kjell     348:                    ((i = nicol % 8) != 0 && linsert(i, ' ') == FALSE)))) {
                    349:                        s = FALSE;
                    350:                        break;
                    351:                }
1.1       deraadt   352:        }
1.29      kjell     353:        undo_boundary_enable(FFRAND, 1);
                    354:        return (s);
1.1       deraadt   355: }
1.25      kjell     356:
                    357: /*
                    358:  * Indent the current line. Delete existing leading whitespace,
                    359:  * and use tabs/spaces to achieve correct indentation. Try
                    360:  * to leave dot where it started.
                    361:  */
                    362: int
                    363: indent(int f, int n)
                    364: {
                    365:        int soff, i;
                    366:
                    367:        if (n < 0)
                    368:                return (FALSE);
                    369:
                    370:        delleadwhite(FFRAND, 1);
                    371:
                    372:        /* If not invoked with a numerical argument, done */
                    373:        if (!(f & FFARG))
                    374:                return (TRUE);
                    375:
                    376:        /* insert appropriate whitespace */
                    377:        soff = curwp->w_doto;
                    378:        (void)gotobol(FFRAND, 1);
                    379:        if (
                    380: #ifdef NOTAB
                    381:            curbp->b_flag & BFNOTAB) ? linsert(n, ' ') == FALSE :
                    382: #endif /* NOTAB */
                    383:            (((i = n / 8) != 0 && linsert(i, '\t') == FALSE) ||
                    384:            ((i = n % 8) != 0 && linsert(i, ' ') == FALSE)))
                    385:                return (FALSE);
                    386:
                    387:        forwchar(FFRAND, soff);
                    388:
                    389:        return (TRUE);
                    390: }
                    391:
1.1       deraadt   392:
                    393: /*
1.3       millert   394:  * Delete forward.  This is real easy, because the basic delete routine does
1.6       mickey    395:  * all of the work.  Watches for negative arguments, and does the right thing.
                    396:  * If any argument is present, it kills rather than deletes, to prevent loss
1.3       millert   397:  * of text if typed with a big argument.  Normally bound to "C-D".
1.1       deraadt   398:  */
1.2       millert   399: /* ARGSUSED */
1.3       millert   400: int
1.10      cloder    401: forwdel(int f, int n)
1.1       deraadt   402: {
                    403:        if (n < 0)
1.11      db        404:                return (backdel(f | FFRAND, -n));
1.3       millert   405:
                    406:        /* really a kill */
                    407:        if (f & FFARG) {
1.2       millert   408:                if ((lastflag & CFKILL) == 0)
1.1       deraadt   409:                        kdelete();
                    410:                thisflag |= CFKILL;
                    411:        }
1.3       millert   412:
1.11      db        413:        return (ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE));
1.1       deraadt   414: }
                    415:
                    416: /*
1.6       mickey    417:  * Delete backwards.  This is quite easy too, because it's all done with
                    418:  * other functions.  Just move the cursor back, and delete forwards.  Like
1.3       millert   419:  * delete forward, this actually does a kill if presented with an argument.
1.1       deraadt   420:  */
1.2       millert   421: /* ARGSUSED */
1.3       millert   422: int
1.10      cloder    423: backdel(int f, int n)
1.1       deraadt   424: {
1.3       millert   425:        int     s;
1.1       deraadt   426:
                    427:        if (n < 0)
1.11      db        428:                return (forwdel(f | FFRAND, -n));
1.3       millert   429:
                    430:        /* really a kill */
                    431:        if (f & FFARG) {
1.2       millert   432:                if ((lastflag & CFKILL) == 0)
1.1       deraadt   433:                        kdelete();
                    434:                thisflag |= CFKILL;
                    435:        }
1.2       millert   436:        if ((s = backchar(f | FFRAND, n)) == TRUE)
1.3       millert   437:                s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE);
                    438:
1.11      db        439:        return (s);
1.1       deraadt   440: }
                    441:
                    442: #ifdef NOTAB
1.2       millert   443: /* ARGSUSED */
1.3       millert   444: int
1.10      cloder    445: space_to_tabstop(int f, int n)
1.1       deraadt   446: {
1.2       millert   447:        if (n < 0)
1.11      db        448:                return (FALSE);
1.2       millert   449:        if (n == 0)
1.11      db        450:                return (TRUE);
                    451:        return (linsert((n << 3) - (curwp->w_doto & 7), ' '));
1.1       deraadt   452: }
1.3       millert   453: #endif /* NOTAB */
1.27      kjell     454:
                    455: /*
                    456:  * Move the dot to the first non-whitespace character of the current line.
                    457:  */
                    458: int
                    459: backtoindent(int f, int n)
                    460: {
                    461:        gotobol(FFRAND, 1);
                    462:        while (curwp->w_doto < llength(curwp->w_dotp) &&
                    463:            (isspace(lgetc(curwp->w_dotp, curwp->w_doto))))
                    464:                ++curwp->w_doto;
1.28      kjell     465:        return (TRUE);
                    466: }
                    467:
                    468: /*
                    469:  * Join the current line to the previous, or with arg, the next line
                    470:  * to the current one.  If the former line is not empty, leave exactly
                    471:  * one space at the joint.  Otherwise, leave no whitespace.
                    472:  */
                    473: int
                    474: joinline(int f, int n)
                    475: {
                    476:        int doto;
                    477:
                    478:        undo_boundary_enable(FFRAND, 0);
                    479:        if (f & FFARG) {
                    480:                gotoeol(FFRAND, 1);
                    481:                forwdel(FFRAND, 1);
                    482:        } else {
                    483:                gotobol(FFRAND, 1);
                    484:                backdel(FFRAND, 1);
                    485:        }
                    486:
                    487:        delwhite(FFRAND, 1);
                    488:
                    489:        if ((doto = curwp->w_doto) > 0) {
                    490:                linsert(1, ' ');
                    491:                curwp->w_doto = doto;
                    492:        }
                    493:        undo_boundary_enable(FFRAND, 1);
                    494:
1.27      kjell     495:        return (TRUE);
                    496: }