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