1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
/*-
* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
* Copyright (c) 1992, 1993, 1994, 1995, 1996
* Keith Bostic. All rights reserved.
*
* See the LICENSE file for redistribution information.
*/
#include "config.h"
#ifndef lint
static const char sccsid[] = "$Id: v_word.c,v 10.6 2001/06/25 15:19:36 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:36 $";
#endif /* not lint */
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <bitstring.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include "../common/common.h"
#include "vi.h"
/*
* There are two types of "words". Bigwords are easy -- groups of anything
* delimited by whitespace. Normal words are trickier. They are either a
* group of characters, numbers and underscores, or a group of anything but,
* delimited by whitespace. When for a word, if you're in whitespace, it's
* easy, just remove the whitespace and go to the beginning or end of the
* word. Otherwise, figure out if the next character is in a different group.
* If it is, go to the beginning or end of that group, otherwise, go to the
* beginning or end of the current group. The historic version of vi didn't
* get this right, so, for example, there were cases where "4e" was not the
* same as "eeee" -- in particular, single character words, and commands that
* began in whitespace were almost always handled incorrectly. To get it right
* you have to resolve the cursor after each search so that the look-ahead to
* figure out what type of "word" the cursor is in will be correct.
*
* Empty lines, and lines that consist of only white-space characters count
* as a single word, and the beginning and end of the file counts as an
* infinite number of words.
*
* Movements associated with commands are different than movement commands.
* For example, in "abc def", with the cursor on the 'a', "cw" is from
* 'a' to 'c', while "w" is from 'a' to 'd'. In general, trailing white
* space is discarded from the change movement. Another example is that,
* in the same string, a "cw" on any white space character replaces that
* single character, and nothing else. Ain't nothin' in here that's easy.
*
* One historic note -- in the original vi, the 'w', 'W' and 'B' commands
* would treat groups of empty lines as individual words, i.e. the command
* would move the cursor to each new empty line. The 'e' and 'E' commands
* would treat groups of empty lines as a single word, i.e. the first use
* would move past the group of lines. The 'b' command would just beep at
* you, or, if you did it from the start of the line as part of a motion
* command, go absolutely nuts. If the lines contained only white-space
* characters, the 'w' and 'W' commands would just beep at you, and the 'B',
* 'b', 'E' and 'e' commands would treat the group as a single word, and
* the 'B' and 'b' commands will treat the lines as individual words. This
* implementation treats all of these cases as a single white-space word.
*/
enum which {BIGWORD, LITTLEWORD};
static int bword __P((SCR *, VICMD *, enum which));
static int eword __P((SCR *, VICMD *, enum which));
static int fword __P((SCR *, VICMD *, enum which));
/*
* v_wordW -- [count]W
* Move forward a bigword at a time.
*
* PUBLIC: int v_wordW __P((SCR *, VICMD *));
*/
int
v_wordW(SCR *sp, VICMD *vp)
{
return (fword(sp, vp, BIGWORD));
}
/*
* v_wordw -- [count]w
* Move forward a word at a time.
*
* PUBLIC: int v_wordw __P((SCR *, VICMD *));
*/
int
v_wordw(SCR *sp, VICMD *vp)
{
return (fword(sp, vp, LITTLEWORD));
}
/*
* fword --
* Move forward by words.
*/
static int
fword(SCR *sp, VICMD *vp, enum which type)
{
enum { INWORD, NOTWORD } state;
VCS cs;
u_long cnt;
cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
cs.cs_lno = vp->m_start.lno;
cs.cs_cno = vp->m_start.cno;
if (cs_init(sp, &cs))
return (1);
/*
* If in white-space:
* If the count is 1, and it's a change command, we're done.
* Else, move to the first non-white-space character, which
* counts as a single word move. If it's a motion command,
* don't move off the end of the line.
*/
if (cs.cs_flags == CS_EMP || cs.cs_flags == 0 && isblank(cs.cs_ch)) {
if (ISMOTION(vp) && cs.cs_flags != CS_EMP && cnt == 1) {
if (ISCMD(vp->rkp, 'c'))
return (0);
if (ISCMD(vp->rkp, 'd') || ISCMD(vp->rkp, 'y')) {
if (cs_fspace(sp, &cs))
return (1);
goto ret;
}
}
if (cs_fblank(sp, &cs))
return (1);
--cnt;
}
/*
* Cyclically move to the next word -- this involves skipping
* over word characters and then any trailing non-word characters.
* Note, for the 'w' command, the definition of a word keeps
* switching.
*/
if (type == BIGWORD)
while (cnt--) {
for (;;) {
if (cs_next(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
break;
}
/*
* If a motion command and we're at the end of the
* last word, we're done. Delete and yank eat any
* trailing blanks, but we don't move off the end
* of the line regardless.
*/
if (cnt == 0 && ISMOTION(vp)) {
if ((ISCMD(vp->rkp, 'd') ||
ISCMD(vp->rkp, 'y')) &&
cs_fspace(sp, &cs))
return (1);
break;
}
/* Eat whitespace characters. */
if (cs_fblank(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
}
else
while (cnt--) {
state = cs.cs_flags == 0 &&
inword(cs.cs_ch) ? INWORD : NOTWORD;
for (;;) {
if (cs_next(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
break;
if (state == INWORD) {
if (!inword(cs.cs_ch))
break;
} else
if (inword(cs.cs_ch))
break;
}
/* See comment above. */
if (cnt == 0 && ISMOTION(vp)) {
if ((ISCMD(vp->rkp, 'd') ||
ISCMD(vp->rkp, 'y')) &&
cs_fspace(sp, &cs))
return (1);
break;
}
/* Eat whitespace characters. */
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
if (cs_fblank(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
}
/*
* If we didn't move, we must be at EOF.
*
* !!!
* That's okay for motion commands, however.
*/
ret: if (!ISMOTION(vp) &&
cs.cs_lno == vp->m_start.lno && cs.cs_cno == vp->m_start.cno) {
v_eof(sp, &vp->m_start);
return (1);
}
/* Adjust the end of the range for motion commands. */
vp->m_stop.lno = cs.cs_lno;
vp->m_stop.cno = cs.cs_cno;
if (ISMOTION(vp) && cs.cs_flags == 0)
--vp->m_stop.cno;
/*
* Non-motion commands move to the end of the range. Delete
* and yank stay at the start, ignore others.
*/
vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
return (0);
}
/*
* v_wordE -- [count]E
* Move forward to the end of the bigword.
*
* PUBLIC: int v_wordE __P((SCR *, VICMD *));
*/
int
v_wordE(SCR *sp, VICMD *vp)
{
return (eword(sp, vp, BIGWORD));
}
/*
* v_worde -- [count]e
* Move forward to the end of the word.
*
* PUBLIC: int v_worde __P((SCR *, VICMD *));
*/
int
v_worde(SCR *sp, VICMD *vp)
{
return (eword(sp, vp, LITTLEWORD));
}
/*
* eword --
* Move forward to the end of the word.
*/
static int
eword(SCR *sp, VICMD *vp, enum which type)
{
enum { INWORD, NOTWORD } state;
VCS cs;
u_long cnt;
cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
cs.cs_lno = vp->m_start.lno;
cs.cs_cno = vp->m_start.cno;
if (cs_init(sp, &cs))
return (1);
/*
* !!!
* If in whitespace, or the next character is whitespace, move past
* it. (This doesn't count as a word move.) Stay at the character
* past the current one, it sets word "state" for the 'e' command.
*/
if (cs.cs_flags == 0 && !isblank(cs.cs_ch)) {
if (cs_next(sp, &cs))
return (1);
if (cs.cs_flags == 0 && !isblank(cs.cs_ch))
goto start;
}
if (cs_fblank(sp, &cs))
return (1);
/*
* Cyclically move to the next word -- this involves skipping
* over word characters and then any trailing non-word characters.
* Note, for the 'e' command, the definition of a word keeps
* switching.
*/
start: if (type == BIGWORD)
while (cnt--) {
for (;;) {
if (cs_next(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
break;
}
/*
* When we reach the start of the word after the last
* word, we're done. If we changed state, back up one
* to the end of the previous word.
*/
if (cnt == 0) {
if (cs.cs_flags == 0 && cs_prev(sp, &cs))
return (1);
break;
}
/* Eat whitespace characters. */
if (cs_fblank(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
}
else
while (cnt--) {
state = cs.cs_flags == 0 &&
inword(cs.cs_ch) ? INWORD : NOTWORD;
for (;;) {
if (cs_next(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
break;
if (state == INWORD) {
if (!inword(cs.cs_ch))
break;
} else
if (inword(cs.cs_ch))
break;
}
/* See comment above. */
if (cnt == 0) {
if (cs.cs_flags == 0 && cs_prev(sp, &cs))
return (1);
break;
}
/* Eat whitespace characters. */
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
if (cs_fblank(sp, &cs))
return (1);
if (cs.cs_flags == CS_EOF)
goto ret;
}
/*
* If we didn't move, we must be at EOF.
*
* !!!
* That's okay for motion commands, however.
*/
ret: if (!ISMOTION(vp) &&
cs.cs_lno == vp->m_start.lno && cs.cs_cno == vp->m_start.cno) {
v_eof(sp, &vp->m_start);
return (1);
}
/* Set the end of the range for motion commands. */
vp->m_stop.lno = cs.cs_lno;
vp->m_stop.cno = cs.cs_cno;
/*
* Non-motion commands move to the end of the range.
* Delete and yank stay at the start, ignore others.
*/
vp->m_final = ISMOTION(vp) ? vp->m_start : vp->m_stop;
return (0);
}
/*
* v_WordB -- [count]B
* Move backward a bigword at a time.
*
* PUBLIC: int v_wordB __P((SCR *, VICMD *));
*/
int
v_wordB(SCR *sp, VICMD *vp)
{
return (bword(sp, vp, BIGWORD));
}
/*
* v_wordb -- [count]b
* Move backward a word at a time.
*
* PUBLIC: int v_wordb __P((SCR *, VICMD *));
*/
int
v_wordb(SCR *sp, VICMD *vp)
{
return (bword(sp, vp, LITTLEWORD));
}
/*
* bword --
* Move backward by words.
*/
static int
bword(SCR *sp, VICMD *vp, enum which type)
{
enum { INWORD, NOTWORD } state;
VCS cs;
u_long cnt;
cnt = F_ISSET(vp, VC_C1SET) ? vp->count : 1;
cs.cs_lno = vp->m_start.lno;
cs.cs_cno = vp->m_start.cno;
if (cs_init(sp, &cs))
return (1);
/*
* !!!
* If in whitespace, or the previous character is whitespace, move
* past it. (This doesn't count as a word move.) Stay at the
* character before the current one, it sets word "state" for the
* 'b' command.
*/
if (cs.cs_flags == 0 && !isblank(cs.cs_ch)) {
if (cs_prev(sp, &cs))
return (1);
if (cs.cs_flags == 0 && !isblank(cs.cs_ch))
goto start;
}
if (cs_bblank(sp, &cs))
return (1);
/*
* Cyclically move to the beginning of the previous word -- this
* involves skipping over word characters and then any trailing
* non-word characters. Note, for the 'b' command, the definition
* of a word keeps switching.
*/
start: if (type == BIGWORD)
while (cnt--) {
for (;;) {
if (cs_prev(sp, &cs))
return (1);
if (cs.cs_flags == CS_SOF)
goto ret;
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
break;
}
/*
* When we reach the end of the word before the last
* word, we're done. If we changed state, move forward
* one to the end of the next word.
*/
if (cnt == 0) {
if (cs.cs_flags == 0 && cs_next(sp, &cs))
return (1);
break;
}
/* Eat whitespace characters. */
if (cs_bblank(sp, &cs))
return (1);
if (cs.cs_flags == CS_SOF)
goto ret;
}
else
while (cnt--) {
state = cs.cs_flags == 0 &&
inword(cs.cs_ch) ? INWORD : NOTWORD;
for (;;) {
if (cs_prev(sp, &cs))
return (1);
if (cs.cs_flags == CS_SOF)
goto ret;
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
break;
if (state == INWORD) {
if (!inword(cs.cs_ch))
break;
} else
if (inword(cs.cs_ch))
break;
}
/* See comment above. */
if (cnt == 0) {
if (cs.cs_flags == 0 && cs_next(sp, &cs))
return (1);
break;
}
/* Eat whitespace characters. */
if (cs.cs_flags != 0 || isblank(cs.cs_ch))
if (cs_bblank(sp, &cs))
return (1);
if (cs.cs_flags == CS_SOF)
goto ret;
}
/* If we didn't move, we must be at SOF. */
ret: if (cs.cs_lno == vp->m_start.lno && cs.cs_cno == vp->m_start.cno) {
v_sof(sp, &vp->m_start);
return (1);
}
/* Set the end of the range for motion commands. */
vp->m_stop.lno = cs.cs_lno;
vp->m_stop.cno = cs.cs_cno;
/*
* All commands move to the end of the range. Motion commands
* adjust the starting point to the character before the current
* one.
*
* !!!
* The historic vi didn't get this right -- the `yb' command yanked
* the right stuff and even updated the cursor value, but the cursor
* was not actually updated on the screen.
*/
vp->m_final = vp->m_stop;
if (ISMOTION(vp))
--vp->m_start.cno;
return (0);
}
|