1 /* $OpenBSD: word.c,v 1.15 2008/09/15 16:11:35 kjell Exp $ */
3 /* This file is in the public domain. */
7 * The routines in this file implement commands that work word at a time.
8 * There are all sorts of word mode commands.
13 RSIZE countfword(void);
16 * Move the cursor backward by "n" words. All of the details of motion are
17 * performed by the "backchar" and "forwchar" routines.
21 backword(int f, int n)
24 return (forwword(f | FFRAND, -n));
25 if (backchar(FFRAND, 1) == FALSE)
28 while (inword() == FALSE) {
29 if (backchar(FFRAND, 1) == FALSE)
32 while (inword() != FALSE) {
33 if (backchar(FFRAND, 1) == FALSE)
37 return (forwchar(FFRAND, 1));
41 * Move the cursor forward by the specified number of words. All of the
42 * motion is done by "forwchar".
46 forwword(int f, int n)
49 return (backword(f | FFRAND, -n));
51 while (inword() == FALSE) {
52 if (forwchar(FFRAND, 1) == FALSE)
55 while (inword() != FALSE) {
56 if (forwchar(FFRAND, 1) == FALSE)
64 * Move the cursor forward by the specified number of words. As you move,
65 * convert any characters to upper case.
69 upperword(int f, int n)
74 if ((s = checkdirty(curbp)) != TRUE)
76 if (curbp->b_flag & BFREADONLY) {
77 ewprintf("Buffer is read-only");
84 while (inword() == FALSE) {
85 if (forwchar(FFRAND, 1) == FALSE)
89 undo_add_change(curwp->w_dotp, curwp->w_doto, size);
91 while (inword() != FALSE) {
92 c = lgetc(curwp->w_dotp, curwp->w_doto);
93 if (ISLOWER(c) != FALSE) {
95 lputc(curwp->w_dotp, curwp->w_doto, c);
98 if (forwchar(FFRAND, 1) == FALSE)
106 * Move the cursor forward by the specified number of words. As you move
107 * convert characters to lower case.
111 lowerword(int f, int n)
116 if ((s = checkdirty(curbp)) != TRUE)
118 if (curbp->b_flag & BFREADONLY) {
119 ewprintf("Buffer is read-only");
125 while (inword() == FALSE) {
126 if (forwchar(FFRAND, 1) == FALSE)
130 undo_add_change(curwp->w_dotp, curwp->w_doto, size);
132 while (inword() != FALSE) {
133 c = lgetc(curwp->w_dotp, curwp->w_doto);
134 if (ISUPPER(c) != FALSE) {
136 lputc(curwp->w_dotp, curwp->w_doto, c);
139 if (forwchar(FFRAND, 1) == FALSE)
147 * Move the cursor forward by the specified number of words. As you move
148 * convert the first character of the word to upper case, and subsequent
149 * characters to lower case. Error if you try to move past the end of the
154 capword(int f, int n)
159 if ((s = checkdirty(curbp)) != TRUE)
161 if (curbp->b_flag & BFREADONLY) {
162 ewprintf("Buffer is read-only");
169 while (inword() == FALSE) {
170 if (forwchar(FFRAND, 1) == FALSE)
174 undo_add_change(curwp->w_dotp, curwp->w_doto, size);
176 if (inword() != FALSE) {
177 c = lgetc(curwp->w_dotp, curwp->w_doto);
178 if (ISLOWER(c) != FALSE) {
180 lputc(curwp->w_dotp, curwp->w_doto, c);
183 if (forwchar(FFRAND, 1) == FALSE)
185 while (inword() != FALSE) {
186 c = lgetc(curwp->w_dotp, curwp->w_doto);
187 if (ISUPPER(c) != FALSE) {
189 lputc(curwp->w_dotp, curwp->w_doto, c);
192 if (forwchar(FFRAND, 1) == FALSE)
201 * Count characters in word, from current position
210 dotp = curwp->w_dotp;
211 doto = curwp->w_doto;
214 while (inword() != FALSE) {
215 if (forwchar(FFRAND, 1) == FALSE)
216 /* hit the end of the buffer */
221 curwp->w_dotp = dotp;
222 curwp->w_doto = doto;
228 * Kill forward by "n" words.
232 delfword(int f, int n)
239 if ((s = checkdirty(curbp)) != TRUE)
241 if (curbp->b_flag & BFREADONLY) {
242 ewprintf("Buffer is read-only");
248 /* purge kill buffer */
249 if ((lastflag & CFKILL) == 0)
253 dotp = curwp->w_dotp;
254 doto = curwp->w_doto;
258 while (inword() == FALSE) {
259 if (forwchar(FFRAND, 1) == FALSE)
260 /* hit the end of the buffer */
264 while (inword() != FALSE) {
265 if (forwchar(FFRAND, 1) == FALSE)
266 /* hit the end of the buffer */
272 curwp->w_dotp = dotp;
273 curwp->w_doto = doto;
274 return (ldelete(size, KFORW));
278 * Kill backwards by "n" words. The rules for success and failure are now
279 * different, to prevent strange behavior at the start of the buffer. The
280 * command only fails if something goes wrong with the actual delete of the
281 * characters. It is successful even if no characters are deleted, or if you
282 * say delete 5 words, and there are only 4 words left. I considered making
283 * the first call to "backchar" special, but decided that that would just be
284 * weird. Normally this is bound to "M-Rubout" and to "M-Backspace".
288 delbword(int f, int n)
293 if ((s = checkdirty(curbp)) != TRUE)
295 if (curbp->b_flag & BFREADONLY) {
296 ewprintf("Buffer is read-only");
303 /* purge kill buffer */
304 if ((lastflag & CFKILL) == 0)
307 if (backchar(FFRAND, 1) == FALSE)
308 /* hit buffer start */
314 while (inword() == FALSE) {
315 if (backchar(FFRAND, 1) == FALSE)
316 /* hit buffer start */
320 while (inword() != FALSE) {
321 if (backchar(FFRAND, 1) == FALSE)
322 /* hit buffer start */
327 if (forwchar(FFRAND, 1) == FALSE)
330 /* undo assumed delete */
333 return (ldelete(size, KBACK));
337 * Return TRUE if the character at dot is a character that is considered to be
338 * part of a word. The word character list is hard coded. Should be settable.
343 /* can't use lgetc in ISWORD due to bug in OSK cpp */
344 return (curwp->w_doto != llength(curwp->w_dotp) &&
345 ISWORD(curwp->w_dotp->l_text[curwp->w_doto]));