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

Annotation of src/usr.bin/mg/word.c, Revision 1.3

1.1       deraadt     1: /*
                      2:  *             Word mode commands.
1.3     ! millert     3:  * The routines in this file implement commands that work word at a time.
        !             4:  * There are all sorts of word mode commands.
        !             5:  */
        !             6:
        !             7: #include "def.h"
1.1       deraadt     8:
                      9: /*
1.3     ! millert    10:  * Move the cursor backward by "n" words. All of the details of motion are
        !            11:  * performed by the "backchar" and "forwchar" routines.
1.1       deraadt    12:  */
1.2       millert    13: /* ARGSUSED */
1.3     ! millert    14: int
1.1       deraadt    15: backword(f, n)
1.3     ! millert    16:        int f, n;
1.1       deraadt    17: {
1.2       millert    18:        if (n < 0)
                     19:                return forwword(f | FFRAND, -n);
1.1       deraadt    20:        if (backchar(FFRAND, 1) == FALSE)
                     21:                return FALSE;
                     22:        while (n--) {
                     23:                while (inword() == FALSE) {
                     24:                        if (backchar(FFRAND, 1) == FALSE)
                     25:                                return TRUE;
                     26:                }
                     27:                while (inword() != FALSE) {
                     28:                        if (backchar(FFRAND, 1) == FALSE)
                     29:                                return TRUE;
                     30:                }
                     31:        }
                     32:        return forwchar(FFRAND, 1);
                     33: }
                     34:
                     35: /*
1.3     ! millert    36:  * Move the cursor forward by the specified number of words.  All of the
1.1       deraadt    37:  * motion is done by "forwchar".
                     38:  */
1.2       millert    39: /* ARGSUSED */
1.3     ! millert    40: int
1.1       deraadt    41: forwword(f, n)
1.3     ! millert    42:        int f, n;
1.1       deraadt    43: {
                     44:        if (n < 0)
                     45:                return backword(f | FFRAND, -n);
                     46:        while (n--) {
                     47:                while (inword() == FALSE) {
                     48:                        if (forwchar(FFRAND, 1) == FALSE)
                     49:                                return TRUE;
                     50:                }
                     51:                while (inword() != FALSE) {
                     52:                        if (forwchar(FFRAND, 1) == FALSE)
                     53:                                return TRUE;
                     54:                }
                     55:        }
                     56:        return TRUE;
                     57: }
                     58:
                     59: /*
1.3     ! millert    60:  * Move the cursor forward by the specified number of words.  As you move,
1.1       deraadt    61:  * convert any characters to upper case.
                     62:  */
1.2       millert    63: /* ARGSUSED */
1.3     ! millert    64: int
1.1       deraadt    65: upperword(f, n)
1.3     ! millert    66:        int f, n;
1.1       deraadt    67: {
1.3     ! millert    68:        int     c;
1.1       deraadt    69:
1.2       millert    70:        if (n < 0)
                     71:                return FALSE;
1.1       deraadt    72:        while (n--) {
                     73:                while (inword() == FALSE) {
                     74:                        if (forwchar(FFRAND, 1) == FALSE)
                     75:                                return TRUE;
                     76:                }
                     77:                while (inword() != FALSE) {
                     78:                        c = lgetc(curwp->w_dotp, curwp->w_doto);
                     79:                        if (ISLOWER(c) != FALSE) {
                     80:                                c = TOUPPER(c);
                     81:                                lputc(curwp->w_dotp, curwp->w_doto, c);
                     82:                                lchange(WFHARD);
                     83:                        }
                     84:                        if (forwchar(FFRAND, 1) == FALSE)
                     85:                                return TRUE;
                     86:                }
                     87:        }
                     88:        return TRUE;
                     89: }
                     90:
                     91: /*
1.3     ! millert    92:  * Move the cursor forward by the specified number of words.  As you move
1.1       deraadt    93:  * convert characters to lower case.
                     94:  */
1.2       millert    95: /* ARGSUSED */
1.3     ! millert    96: int
1.1       deraadt    97: lowerword(f, n)
1.3     ! millert    98:        int f, n;
1.1       deraadt    99: {
1.3     ! millert   100:        int     c;
1.1       deraadt   101:
1.2       millert   102:        if (n < 0)
                    103:                return FALSE;
1.1       deraadt   104:        while (n--) {
                    105:                while (inword() == FALSE) {
                    106:                        if (forwchar(FFRAND, 1) == FALSE)
                    107:                                return TRUE;
                    108:                }
                    109:                while (inword() != FALSE) {
                    110:                        c = lgetc(curwp->w_dotp, curwp->w_doto);
                    111:                        if (ISUPPER(c) != FALSE) {
                    112:                                c = TOLOWER(c);
                    113:                                lputc(curwp->w_dotp, curwp->w_doto, c);
                    114:                                lchange(WFHARD);
                    115:                        }
                    116:                        if (forwchar(FFRAND, 1) == FALSE)
                    117:                                return TRUE;
                    118:                }
                    119:        }
                    120:        return TRUE;
                    121: }
                    122:
                    123: /*
1.3     ! millert   124:  * Move the cursor forward by the specified number of words.  As you move
        !           125:  * convert the first character of the word to upper case, and subsequent
        !           126:  * characters to lower case.  Error if you try to move past the end of the
        !           127:  * buffer.
1.1       deraadt   128:  */
1.2       millert   129: /* ARGSUSED */
1.3     ! millert   130: int
1.1       deraadt   131: capword(f, n)
1.3     ! millert   132:        int f, n;
1.1       deraadt   133: {
1.3     ! millert   134:        int     c;
1.1       deraadt   135:
1.2       millert   136:        if (n < 0)
                    137:                return FALSE;
1.1       deraadt   138:        while (n--) {
                    139:                while (inword() == FALSE) {
                    140:                        if (forwchar(FFRAND, 1) == FALSE)
                    141:                                return TRUE;
                    142:                }
                    143:                if (inword() != FALSE) {
                    144:                        c = lgetc(curwp->w_dotp, curwp->w_doto);
                    145:                        if (ISLOWER(c) != FALSE) {
                    146:                                c = TOUPPER(c);
                    147:                                lputc(curwp->w_dotp, curwp->w_doto, c);
                    148:                                lchange(WFHARD);
                    149:                        }
                    150:                        if (forwchar(FFRAND, 1) == FALSE)
                    151:                                return TRUE;
                    152:                        while (inword() != FALSE) {
                    153:                                c = lgetc(curwp->w_dotp, curwp->w_doto);
                    154:                                if (ISUPPER(c) != FALSE) {
                    155:                                        c = TOLOWER(c);
                    156:                                        lputc(curwp->w_dotp, curwp->w_doto, c);
                    157:                                        lchange(WFHARD);
                    158:                                }
                    159:                                if (forwchar(FFRAND, 1) == FALSE)
                    160:                                        return TRUE;
                    161:                        }
                    162:                }
                    163:        }
                    164:        return TRUE;
                    165: }
                    166:
                    167: /*
                    168:  * Kill forward by "n" words.
                    169:  */
1.2       millert   170: /* ARGSUSED */
1.3     ! millert   171: int
1.1       deraadt   172: delfword(f, n)
1.3     ! millert   173:        int f, n;
1.1       deraadt   174: {
1.3     ! millert   175:        RSIZE    size;
        !           176:        LINE    *dotp;
        !           177:        int      doto;
1.1       deraadt   178:
                    179:        if (n < 0)
                    180:                return FALSE;
1.3     ! millert   181:
        !           182:        /* purge kill buffer */
        !           183:        if ((lastflag & CFKILL) == 0)
1.1       deraadt   184:                kdelete();
1.3     ! millert   185:
1.1       deraadt   186:        thisflag |= CFKILL;
                    187:        dotp = curwp->w_dotp;
                    188:        doto = curwp->w_doto;
                    189:        size = 0;
1.3     ! millert   190:
1.1       deraadt   191:        while (n--) {
                    192:                while (inword() == FALSE) {
                    193:                        if (forwchar(FFRAND, 1) == FALSE)
1.3     ! millert   194:                                /* hit the end of the buffer */
        !           195:                                goto out;
1.1       deraadt   196:                        ++size;
                    197:                }
                    198:                while (inword() != FALSE) {
                    199:                        if (forwchar(FFRAND, 1) == FALSE)
1.3     ! millert   200:                                /* hit the end of the buffer */
        !           201:                                goto out;
1.1       deraadt   202:                        ++size;
                    203:                }
                    204:        }
                    205: out:
                    206:        curwp->w_dotp = dotp;
                    207:        curwp->w_doto = doto;
                    208:        return (ldelete(size, KFORW));
                    209: }
                    210:
                    211: /*
1.3     ! millert   212:  * Kill backwards by "n" words.  The rules for success and failure are now
        !           213:  * different, to prevent strange behavior at the start of the buffer.  The
        !           214:  * command only fails if something goes wrong with the actual delete of the
        !           215:  * characters.  It is successful even if no characters are deleted, or if you
        !           216:  * say delete 5 words, and there are only 4 words left.  I considered making
        !           217:  * the first call to "backchar" special, but decided that that would just be
        !           218:  * weird. Normally this is bound to "M-Rubout" and to "M-Backspace".
1.1       deraadt   219:  */
1.2       millert   220: /* ARGSUSED */
1.3     ! millert   221: int
1.1       deraadt   222: delbword(f, n)
1.3     ! millert   223:        int f, n;
1.1       deraadt   224: {
1.3     ! millert   225:        RSIZE   size;
1.1       deraadt   226:
1.2       millert   227:        if (n < 0)
                    228:                return FALSE;
1.3     ! millert   229:
        !           230:        /* purge kill buffer */
        !           231:        if ((lastflag & CFKILL) == 0)
1.1       deraadt   232:                kdelete();
                    233:        thisflag |= CFKILL;
                    234:        if (backchar(FFRAND, 1) == FALSE)
1.3     ! millert   235:                /* hit buffer start */
        !           236:                return (TRUE);
        !           237:
        !           238:        /* one deleted */
        !           239:        size = 1;
1.1       deraadt   240:        while (n--) {
                    241:                while (inword() == FALSE) {
                    242:                        if (backchar(FFRAND, 1) == FALSE)
1.3     ! millert   243:                                /* hit buffer start */
        !           244:                                goto out;
1.1       deraadt   245:                        ++size;
                    246:                }
                    247:                while (inword() != FALSE) {
                    248:                        if (backchar(FFRAND, 1) == FALSE)
1.3     ! millert   249:                                /* hit buffer start */
        !           250:                                goto out;
1.1       deraadt   251:                        ++size;
                    252:                }
                    253:        }
                    254:        if (forwchar(FFRAND, 1) == FALSE)
                    255:                return FALSE;
1.3     ! millert   256:
        !           257:        /* undo assumed delete */
        !           258:        --size;
1.1       deraadt   259: out:
                    260:        return ldelete(size, KBACK);
                    261: }
                    262:
                    263: /*
1.3     ! millert   264:  * Return TRUE if the character at dot is a character that is considered to be
        !           265:  * part of a word. The word character list is hard coded. Should be setable.
1.1       deraadt   266:  */
1.3     ! millert   267: int
1.2       millert   268: inword()
                    269: {
                    270:        /* can't use lgetc in ISWORD due to bug in OSK cpp */
                    271:        return curwp->w_doto != llength(curwp->w_dotp) &&
1.1       deraadt   272:                ISWORD(curwp->w_dotp->l_text[curwp->w_doto]);
                    273: }