[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.9

1.9     ! vincent     1: /*     $OpenBSD: random.c,v 1.8 2002/05/30 16:07:57 vincent Exp $      */
1.4       niklas      2:
1.1       deraadt     3: /*
                      4:  *             Assorted commands.
1.6       mickey      5:  * This file contains the command processors for a large assortment of
                      6:  * unrelated commands.  The only thing they have in common is that they
1.3       millert     7:  * are all command processors.
                      8:  */
                      9:
                     10: #include "def.h"
1.9     ! vincent    11: #include <ctype.h>
1.1       deraadt    12:
                     13: /*
1.6       mickey     14:  * Display a bunch of useful information about the current location of dot.
                     15:  * The character under the cursor (in octal), the current line, row, and
                     16:  * column, and approximate position of the cursor in the file (as a
                     17:  * percentage) is displayed.  The column position assumes an infinite
1.3       millert    18:  * position display; it does not truncate just because the screen does.
1.1       deraadt    19:  * This is normally bound to "C-X =".
                     20:  */
1.2       millert    21: /* ARGSUSED */
1.3       millert    22: int
1.1       deraadt    23: showcpos(f, n)
1.3       millert    24:        int f, n;
1.1       deraadt    25: {
1.3       millert    26:        LINE    *clp;
1.6       mickey     27:        long     nchar;
1.3       millert    28:        long     cchar;
                     29:        int      nline, row;
                     30:        int      cline, cbyte;          /* Current line/char/byte */
                     31:        int      ratio;
                     32:
                     33:        /* collect the data */
                     34:        clp = lforw(curbp->b_linep);
                     35:        cchar = 0;
                     36:        cline = 0;
                     37:        cbyte = 0;
1.1       deraadt    38:        nchar = 0;
                     39:        nline = 0;
1.6       mickey     40:        for (;;) {
1.3       millert    41:                /* count this line */
                     42:                ++nline;
1.1       deraadt    43:                if (clp == curwp->w_dotp) {
1.3       millert    44:                        /* mark line */
                     45:                        cline = nline;
1.1       deraadt    46:                        cchar = nchar + curwp->w_doto;
                     47:                        if (curwp->w_doto == llength(clp))
                     48:                                cbyte = '\n';
                     49:                        else
                     50:                                cbyte = lgetc(clp, curwp->w_doto);
                     51:                }
1.3       millert    52:                /* now count the chars */
                     53:                nchar += llength(clp);
1.1       deraadt    54:                clp = lforw(clp);
1.2       millert    55:                if (clp == curbp->b_linep)
                     56:                        break;
1.3       millert    57:                /* count the newline */
                     58:                nchar++;
1.1       deraadt    59:        }
1.3       millert    60:        /* determine row */
                     61:        row = curwp->w_toprow + 1;
1.1       deraadt    62:        clp = curwp->w_linep;
1.2       millert    63:        while (clp != curbp->b_linep && clp != curwp->w_dotp) {
1.1       deraadt    64:                ++row;
                     65:                clp = lforw(clp);
                     66:        }
1.2       millert    67:        /* NOSTRICT */
                     68:        ratio = nchar ? (100L * cchar) / nchar : 100;
1.1       deraadt    69:        ewprintf("Char: %c (0%o)  point=%ld(%d%%)  line=%d  row=%d  col=%d",
1.3       millert    70:            cbyte, cbyte, cchar, ratio, cline, row, getcolpos());
1.1       deraadt    71:        return TRUE;
                     72: }
                     73:
1.3       millert    74: int
1.9     ! vincent    75: getcolpos(void)
1.2       millert    76: {
1.3       millert    77:        int     col, i, c;
                     78:
                     79:        /* determine column */
1.9     ! vincent    80:        col = 0;
1.1       deraadt    81:
1.2       millert    82:        for (i = 0; i < curwp->w_doto; ++i) {
1.1       deraadt    83:                c = lgetc(curwp->w_dotp, i);
                     84:                if (c == '\t'
1.3       millert    85: #ifdef NOTAB
1.2       millert    86:                    && !(curbp->b_flag & BFNOTAB)
1.3       millert    87: #endif /* NOTAB */
1.1       deraadt    88:                        ) {
1.2       millert    89:                        col |= 0x07;
1.9     ! vincent    90:                        col++;
1.1       deraadt    91:                } else if (ISCTRL(c) != FALSE)
1.9     ! vincent    92:                        col += 2;
        !            93:                else if (isprint(c))
        !            94:                        col++;
        !            95:                else {
        !            96:                        char tmp[5];
        !            97:                        snprintf(tmp, sizeof tmp, "\\%o", c);
        !            98:                        col += strlen(tmp);
        !            99:                }
        !           100:
1.1       deraadt   101:        }
                    102:        return col;
                    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.1       deraadt   115: twiddle(f, n)
1.3       millert   116:        int f, n;
1.1       deraadt   117: {
1.3       millert   118:        LINE    *dotp;
                    119:        int      doto, cr;
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)
                    125:                        return FALSE;
1.1       deraadt   126:        } else {
1.2       millert   127:                if (doto == 0)
                    128:                        return FALSE;
1.1       deraadt   129:                ++curwp->w_doto;
                    130:        }
                    131:        cr = lgetc(dotp, doto--);
1.2       millert   132:        lputc(dotp, doto + 1, lgetc(dotp, doto));
1.1       deraadt   133:        lputc(dotp, doto, cr);
                    134:        lchange(WFEDIT);
                    135:        return TRUE;
                    136: }
                    137:
                    138: /*
1.6       mickey    139:  * Open up some blank space.  The basic plan is to insert a bunch of
                    140:  * newlines, and then back up over them.  Everything is done by the
                    141:  * subcommand procerssors.  They even handle the looping.  Normally this
1.3       millert   142:  * is bound to "C-O".
1.1       deraadt   143:  */
1.2       millert   144: /* ARGSUSED */
1.3       millert   145: int
1.1       deraadt   146: openline(f, n)
1.3       millert   147:        int f, n;
1.1       deraadt   148: {
1.3       millert   149:        int     i;
                    150:        int     s;
1.1       deraadt   151:
                    152:        if (n < 0)
                    153:                return FALSE;
                    154:        if (n == 0)
                    155:                return TRUE;
1.3       millert   156:
                    157:        /* insert newlines */
                    158:        i = n;
1.1       deraadt   159:        do {
                    160:                s = lnewline();
1.2       millert   161:        } while (s == TRUE && --i);
1.3       millert   162:
                    163:        /* then go back up overtop of them all */
                    164:        if (s == TRUE)
                    165:                s = backchar(f | FFRAND, n);
1.1       deraadt   166:        return s;
                    167: }
                    168:
                    169: /*
1.3       millert   170:  * Insert a newline.  [following "feature" not present in current version of
                    171:  * Gnu, and now disabled here too] If you are at the end of the line and the
1.6       mickey    172:  * next line is a blank line, just move into the blank line.  This makes
                    173:  * "C-O" and "C-X C-O" work nicely, and reduces the ammount of screen update
                    174:  * that has to be done.  This would not be as critical if screen update were a
1.3       millert   175:  * lot more efficient.
1.1       deraadt   176:  */
1.2       millert   177: /* ARGSUSED */
1.3       millert   178: int
1.1       deraadt   179: newline(f, n)
1.3       millert   180:        int f, n;
1.1       deraadt   181: {
1.3       millert   182:        LINE    *lp;
                    183:        int      s;
1.1       deraadt   184:
1.2       millert   185:        if (n < 0)
                    186:                return FALSE;
1.3       millert   187:
1.1       deraadt   188:        while (n--) {
                    189:                lp = curwp->w_dotp;
                    190: #ifdef undef
1.7       deraadt   191:                if (llength(lp) == curwp->w_doto &&
                    192:                    lforw(lp) != curbp->b_linep &&
                    193:                    llength(lforw(lp)) == 0) {
1.2       millert   194:                        if ((s = forwchar(FFRAND, 1)) != TRUE)
1.1       deraadt   195:                                return s;
                    196:                } else
1.3       millert   197: #endif /* undef */
1.2       millert   198:                if ((s = lnewline()) != TRUE)
                    199:                        return s;
1.1       deraadt   200:        }
                    201:        return TRUE;
                    202: }
                    203:
                    204: /*
1.3       millert   205:  * Delete blank lines around dot. What this command does depends if dot is
1.6       mickey    206:  * sitting on a blank line. If dot is sitting on a blank line, this command
                    207:  * deletes all the blank lines above and below the current line. If it is
                    208:  * sitting on a non blank line then it deletes all of the blank lines after
                    209:  * the line. Normally this command is bound to "C-X C-O". Any argument is
1.3       millert   210:  * ignored.
1.1       deraadt   211:  */
1.2       millert   212: /* ARGSUSED */
1.3       millert   213: int
1.1       deraadt   214: deblank(f, n)
1.3       millert   215:        int f, n;
1.1       deraadt   216: {
1.3       millert   217:        LINE    *lp1, *lp2;
                    218:        RSIZE    nld;
1.1       deraadt   219:
                    220:        lp1 = curwp->w_dotp;
1.2       millert   221:        while (llength(lp1) == 0 && (lp2 = lback(lp1)) != curbp->b_linep)
1.1       deraadt   222:                lp1 = lp2;
                    223:        lp2 = lp1;
1.3       millert   224:        nld = (RSIZE)0;
1.2       millert   225:        while ((lp2 = lforw(lp2)) != curbp->b_linep && llength(lp2) == 0)
1.1       deraadt   226:                ++nld;
                    227:        if (nld == 0)
                    228:                return (TRUE);
                    229:        curwp->w_dotp = lforw(lp1);
                    230:        curwp->w_doto = 0;
1.3       millert   231:        return ldelete((RSIZE)nld, KNONE);
1.1       deraadt   232: }
                    233:
                    234: /*
                    235:  * Delete any whitespace around dot, then insert a space.
                    236:  */
1.3       millert   237: int
1.2       millert   238: justone(f, n)
1.3       millert   239:        int f, n;
1.2       millert   240: {
1.5       art       241:        (void)delwhite(f, n);
1.1       deraadt   242:        return linsert(1, ' ');
                    243: }
1.3       millert   244:
1.1       deraadt   245: /*
                    246:  * Delete any whitespace around dot.
                    247:  */
1.2       millert   248: /* ARGSUSED */
1.3       millert   249: int
1.1       deraadt   250: delwhite(f, n)
1.3       millert   251:        int f, n;
1.1       deraadt   252: {
1.3       millert   253:        int     col, c, s;
1.1       deraadt   254:
                    255:        col = curwp->w_doto;
1.3       millert   256:
1.8       vincent   257:        while (col < llength(curwp->w_dotp) &&
                    258:            ((c = lgetc(curwp->w_dotp, col)) == ' ' || c == '\t'))
1.1       deraadt   259:                ++col;
                    260:        do {
                    261:                if (curwp->w_doto == 0) {
                    262:                        s = FALSE;
                    263:                        break;
                    264:                }
1.2       millert   265:                if ((s = backchar(FFRAND, 1)) != TRUE)
                    266:                        break;
1.1       deraadt   267:        } while ((c = lgetc(curwp->w_dotp, curwp->w_doto)) == ' ' || c == '\t');
                    268:
1.2       millert   269:        if (s == TRUE)
1.5       art       270:                (void)forwchar(FFRAND, 1);
                    271:        (void)ldelete((RSIZE)(col - curwp->w_doto), KNONE);
1.1       deraadt   272:        return TRUE;
                    273: }
1.3       millert   274:
1.1       deraadt   275: /*
1.3       millert   276:  * Insert a newline, then enough tabs and spaces to duplicate the indentation
1.6       mickey    277:  * of the previous line.  Assumes tabs are every eight characters.  Quite
                    278:  * simple.  Figure out the indentation of the current line.  Insert a newline
                    279:  * by calling the standard routine.  Insert the indentation by inserting the
                    280:  * right number of tabs and spaces.  Return TRUE if all ok.  Return FALSE if
1.3       millert   281:  * one of the subcomands failed. Normally bound to "C-J".
1.1       deraadt   282:  */
1.2       millert   283: /* ARGSUSED */
1.3       millert   284: int
1.1       deraadt   285: indent(f, n)
1.3       millert   286:        int f, n;
1.1       deraadt   287: {
1.3       millert   288:        int     nicol;
                    289:        int     c;
                    290:        int     i;
1.1       deraadt   291:
1.2       millert   292:        if (n < 0)
                    293:                return (FALSE);
1.3       millert   294:
1.1       deraadt   295:        while (n--) {
                    296:                nicol = 0;
1.2       millert   297:                for (i = 0; i < llength(curwp->w_dotp); ++i) {
1.1       deraadt   298:                        c = lgetc(curwp->w_dotp, i);
1.2       millert   299:                        if (c != ' ' && c != '\t')
1.1       deraadt   300:                                break;
                    301:                        if (c == '\t')
                    302:                                nicol |= 0x07;
                    303:                        ++nicol;
                    304:                }
                    305:                if (lnewline() == FALSE || ((
                    306: #ifdef NOTAB
1.3       millert   307:                    curbp->b_flag & BFNOTAB) ? linsert(nicol, ' ') == FALSE : (
                    308: #endif /* NOTAB */
                    309:                    ((i = nicol / 8) != 0 && linsert(i, '\t') == FALSE) ||
                    310:                    ((i = nicol % 8) != 0 && linsert(i, ' ') == FALSE))))
1.1       deraadt   311:                        return FALSE;
                    312:        }
                    313:        return TRUE;
                    314: }
                    315:
                    316: /*
1.3       millert   317:  * Delete forward.  This is real easy, because the basic delete routine does
1.6       mickey    318:  * all of the work.  Watches for negative arguments, and does the right thing.
                    319:  * If any argument is present, it kills rather than deletes, to prevent loss
1.3       millert   320:  * of text if typed with a big argument.  Normally bound to "C-D".
1.1       deraadt   321:  */
1.2       millert   322: /* ARGSUSED */
1.3       millert   323: int
1.1       deraadt   324: forwdel(f, n)
1.3       millert   325:        int f, n;
1.1       deraadt   326: {
                    327:        if (n < 0)
                    328:                return backdel(f | FFRAND, -n);
1.3       millert   329:
                    330:        /* really a kill */
                    331:        if (f & FFARG) {
1.2       millert   332:                if ((lastflag & CFKILL) == 0)
1.1       deraadt   333:                        kdelete();
                    334:                thisflag |= CFKILL;
                    335:        }
1.3       millert   336:
1.1       deraadt   337:        return ldelete((RSIZE) n, (f & FFARG) ? KFORW : KNONE);
                    338: }
                    339:
                    340: /*
1.6       mickey    341:  * Delete backwards.  This is quite easy too, because it's all done with
                    342:  * other functions.  Just move the cursor back, and delete forwards.  Like
1.3       millert   343:  * delete forward, this actually does a kill if presented with an argument.
1.1       deraadt   344:  */
1.2       millert   345: /* ARGSUSED */
1.3       millert   346: int
1.1       deraadt   347: backdel(f, n)
1.3       millert   348:        int f, n;
1.1       deraadt   349: {
1.3       millert   350:        int     s;
1.1       deraadt   351:
                    352:        if (n < 0)
                    353:                return forwdel(f | FFRAND, -n);
1.3       millert   354:
                    355:        /* really a kill */
                    356:        if (f & FFARG) {
1.2       millert   357:                if ((lastflag & CFKILL) == 0)
1.1       deraadt   358:                        kdelete();
                    359:                thisflag |= CFKILL;
                    360:        }
1.2       millert   361:        if ((s = backchar(f | FFRAND, n)) == TRUE)
1.3       millert   362:                s = ldelete((RSIZE)n, (f & FFARG) ? KFORW : KNONE);
                    363:
1.1       deraadt   364:        return s;
                    365: }
                    366:
                    367: /*
1.6       mickey    368:  * Kill line.  If called without an argument, it kills from dot to the end
                    369:  * of the line, unless it is at the end of the line, when it kills the
                    370:  * newline.  If called with an argument of 0, it kills from the start of the
                    371:  * line to dot.  If called with a positive argument, it kills from dot
                    372:  * forward over that number of newlines.  If called with a negative argument
                    373:  * it kills any text before dot on the current line, then it kills back
1.3       millert   374:  * abs(arg) lines.
1.1       deraadt   375:  */
1.2       millert   376: /* ARGSUSED */
1.3       millert   377: int
1.2       millert   378: killline(f, n)
1.3       millert   379:        int f, n;
1.2       millert   380: {
1.3       millert   381:        LINE    *nextp;
                    382:        RSIZE    chunk;
                    383:        int      i, c;
                    384:
                    385:        /* clear kill buffer if last wasn't a kill */
                    386:        if ((lastflag & CFKILL) == 0)
                    387:                kdelete();
1.1       deraadt   388:        thisflag |= CFKILL;
                    389:        if (!(f & FFARG)) {
                    390:                for (i = curwp->w_doto; i < llength(curwp->w_dotp); ++i)
                    391:                        if ((c = lgetc(curwp->w_dotp, i)) != ' ' && c != '\t')
                    392:                                break;
                    393:                if (i == llength(curwp->w_dotp))
1.2       millert   394:                        chunk = llength(curwp->w_dotp) - curwp->w_doto + 1;
1.1       deraadt   395:                else {
1.2       millert   396:                        chunk = llength(curwp->w_dotp) - curwp->w_doto;
1.1       deraadt   397:                        if (chunk == 0)
                    398:                                chunk = 1;
                    399:                }
                    400:        } else if (n > 0) {
1.2       millert   401:                chunk = llength(curwp->w_dotp) - curwp->w_doto + 1;
1.1       deraadt   402:                nextp = lforw(curwp->w_dotp);
                    403:                i = n;
                    404:                while (--i) {
                    405:                        if (nextp == curbp->b_linep)
                    406:                                break;
1.2       millert   407:                        chunk += llength(nextp) + 1;
1.1       deraadt   408:                        nextp = lforw(nextp);
                    409:                }
1.3       millert   410:        } else {
                    411:                /* n <= 0 */
1.1       deraadt   412:                chunk = curwp->w_doto;
                    413:                curwp->w_doto = 0;
                    414:                i = n;
                    415:                while (i++) {
                    416:                        if (lback(curwp->w_dotp) == curbp->b_linep)
                    417:                                break;
                    418:                        curwp->w_dotp = lback(curwp->w_dotp);
                    419:                        curwp->w_flag |= WFMOVE;
1.2       millert   420:                        chunk += llength(curwp->w_dotp) + 1;
1.1       deraadt   421:                }
                    422:        }
                    423:        /*
1.3       millert   424:         * KFORW here is a bug.  Should be KBACK/KFORW, but we need to
1.1       deraadt   425:         * rewrite the ldelete code (later)?
                    426:         */
1.2       millert   427:        return (ldelete(chunk, KFORW));
1.1       deraadt   428: }
                    429:
                    430: /*
1.6       mickey    431:  * Yank text back from the kill buffer.  This is really easy.  All of the work
                    432:  * is done by the standard insert routines.  All you do is run the loop, and
1.3       millert   433:  * check for errors.  The blank lines are inserted with a call to "newline"
1.6       mickey    434:  * instead of a call to "lnewline" so that the magic stuff that happens when
                    435:  * you type a carriage return also happens when a carriage return is yanked
                    436:  * back from the kill buffer.  An attempt has been made to fix the cosmetic
                    437:  * bug associated with a yank when dot is on the top line of the window
1.3       millert   438:  * (nothing moves, because all of the new text landed off screen).
1.1       deraadt   439:  */
1.2       millert   440: /* ARGSUSED */
1.3       millert   441: int
1.1       deraadt   442: yank(f, n)
1.3       millert   443:        int f, n;
1.1       deraadt   444: {
1.3       millert   445:        LINE    *lp;
                    446:        int      c, i, nline;
1.1       deraadt   447:
1.2       millert   448:        if (n < 0)
                    449:                return FALSE;
1.3       millert   450:
                    451:        /* newline counting */
                    452:        nline = 0;
                    453:
1.1       deraadt   454:        while (n--) {
1.3       millert   455:                /* mark around last yank */
                    456:                isetmark();
1.1       deraadt   457:                i = 0;
1.2       millert   458:                while ((c = kremove(i)) >= 0) {
1.1       deraadt   459:                        if (c == '\n') {
                    460:                                if (newline(FFRAND, 1) == FALSE)
                    461:                                        return FALSE;
                    462:                                ++nline;
                    463:                        } else {
                    464:                                if (linsert(1, c) == FALSE)
                    465:                                        return FALSE;
                    466:                        }
                    467:                        ++i;
                    468:                }
                    469:        }
1.3       millert   470:        /* cosmetic adjustment */
                    471:        lp = curwp->w_linep;
                    472:
                    473:        /* if offscreen insert */
                    474:        if (curwp->w_dotp == lp) {
1.2       millert   475:                while (nline-- && lback(lp) != curbp->b_linep)
1.1       deraadt   476:                        lp = lback(lp);
1.3       millert   477:                /* adjust framing */
                    478:                curwp->w_linep = lp;
1.1       deraadt   479:                curwp->w_flag |= WFHARD;
                    480:        }
                    481:        return TRUE;
                    482: }
                    483:
                    484: #ifdef NOTAB
1.2       millert   485: /* ARGSUSED */
1.3       millert   486: int
1.1       deraadt   487: space_to_tabstop(f, n)
1.3       millert   488:        int f, n;
1.1       deraadt   489: {
1.2       millert   490:        if (n < 0)
                    491:                return FALSE;
                    492:        if (n == 0)
                    493:                return TRUE;
                    494:        return linsert((n << 3) - (curwp->w_doto & 7), ' ');
1.1       deraadt   495: }
1.3       millert   496: #endif /* NOTAB */