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

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