2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "@(#)v_search.c 10.18 (Berkeley) 9/19/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
28 #include "../common/common.h"
31 static int v_exaddr
__P((SCR
*, VICMD
*, dir_t
));
32 static int v_search
__P((SCR
*, VICMD
*, char *, size_t, u_int
, dir_t
));
35 * v_srch -- [count]?RE[? offset]
36 * Ex address search backward.
38 * PUBLIC: int v_searchb __P((SCR *, VICMD *));
45 return (v_exaddr(sp
, vp
, BACKWARD
));
49 * v_searchf -- [count]/RE[/ offset]
50 * Ex address search forward.
52 * PUBLIC: int v_searchf __P((SCR *, VICMD *));
59 return (v_exaddr(sp
, vp
, FORWARD
));
64 * Do a vi search (which is really an ex address).
72 static EXCMDLIST fake
= { "search" };
77 size_t len
, s_cno
, tlen
;
79 char *cmd
, *t
, buf
[20];
83 * If using the search command as a motion, any addressing components
84 * are lost, i.e. y/ptrn/+2, when repeated, is the same as y/ptrn/.
86 if (F_ISSET(vp
, VC_ISDOT
))
87 return (v_search(sp
, vp
,
88 NULL
, 0, SEARCH_PARSE
| SEARCH_MSG
| SEARCH_SET
, dir
));
90 /* Get the search pattern. */
91 if (v_tcmd(sp
, vp
, dir
== BACKWARD
? CH_BSEARCH
: CH_FSEARCH
,
92 TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_PROMPT
|
93 (O_ISSET(sp
, O_SEARCHINCR
) ? TXT_SEARCHINCR
: 0)))
96 tp
= sp
->tiq
.cqh_first
;
98 /* If the user backspaced over the prompt, do nothing. */
99 if (tp
->term
== TERM_BS
)
103 * If the user was doing an incremental search, then we've already
104 * updated the cursor and moved to the right location. Return the
105 * correct values, we're done.
107 if (tp
->term
== TERM_SEARCH
) {
108 vp
->m_stop
.lno
= sp
->lno
;
109 vp
->m_stop
.cno
= sp
->cno
;
111 return (v_correct(sp
, vp
, 0));
112 vp
->m_final
= vp
->m_stop
;
117 * If the user entered <escape> or <carriage-return>, the length is
118 * 1 and the right thing will happen, i.e. the prompt will be used
119 * as a command character.
121 * Build a fake ex command structure.
124 gp
->excmd
.cp
= tp
->lb
;
125 gp
->excmd
.clen
= tp
->len
;
126 F_INIT(&gp
->excmd
, E_VISEARCH
);
130 * Warn if the search wraps. This is a pretty special case, but it's
131 * nice feature that wasn't in the original implementations of ex/vi.
132 * (It was added at some point to System V's version.) This message
133 * is only displayed if there are no keys in the queue. The problem is
134 * the command is going to succeed, and the message is informational,
135 * not an error. If a macro displays it repeatedly, e.g., the pattern
136 * only occurs once in the file and wrapscan is set, you lose big. For
137 * example, if the macro does something like:
139 * :map K /pattern/^MjK
141 * Each search will display the message, but the following "/pattern/"
142 * will immediately overwrite it, with strange results. The System V
143 * vi displays the "wrapped" message multiple times, but because it's
144 * overwritten each time, it's not as noticeable. As we don't discard
145 * messages, it's a real problem for us.
147 if (!KEYS_WAITING(sp
))
148 F_SET(&gp
->excmd
, E_SEARCH_WMSG
);
150 /* Save the current line/column. */
156 * Historically, vi / and ? commands were full-blown ex addresses,
157 * including ';' delimiters, trailing <blank>'s, multiple search
158 * strings (separated by semi-colons) and, finally, full-blown z
159 * commands after the / and ? search strings. (If the search was
160 * being used as a motion, the trailing z command was ignored.
161 * Also, we do some argument checking on the z command, to be sure
162 * that it's not some other random command.) For multiple search
163 * strings, leading <blank>'s at the second and subsequent strings
164 * were eaten as well. This has some (unintended?) side-effects:
165 * the command /ptrn/;3 is legal and results in moving to line 3.
166 * I suppose you could use it to optionally move to line 3...
169 * Historically, if any part of the search command failed, the cursor
170 * remained unmodified (even if ; was used). We have to play games
171 * because the underlying ex parser thinks we're modifying the cursor
172 * as we go, but I think we're compatible with historic practice.
175 * Historically, the command "/STRING/; " failed, apparently it
176 * confused the parser. We're not that compatible.
179 if (ex_range(sp
, cmdp
, &err
))
183 * Remember where any remaining command information is, and clean
184 * up the fake ex command.
193 /* Copy out the new cursor position and make sure it's okay. */
194 switch (cmdp
->addrcnt
) {
196 vp
->m_stop
= cmdp
->addr1
;
199 vp
->m_stop
= cmdp
->addr2
;
202 if (!db_exist(sp
, vp
->m_stop
.lno
)) {
203 ex_badaddr(sp
, &fake
,
204 vp
->m_stop
.lno
== 0 ? A_ZERO
: A_EOF
, NUM_OK
);
210 * Historic practice is that a trailing 'z' was ignored if it was a
211 * motion command. Should probably be an error, but not worth the
215 return (v_correct(sp
, vp
, F_ISSET(cmdp
, E_DELTA
)));
219 * Historically, if it wasn't a motion command, a delta in the search
220 * pattern turns it into a first nonblank movement.
222 nb
= F_ISSET(cmdp
, E_DELTA
);
224 /* Check for the 'z' command. */
229 /* No blanks, just like the z command. */
230 for (t
= cmd
+ 1, tlen
= len
- 1; tlen
> 0; ++t
, --tlen
)
234 (*t
== '-' || *t
== '.' || *t
== '+' || *t
== '^')) {
243 /* The z command will do the nonblank for us. */
248 v_event_push(sp
, NULL
, "+", 1, CH_NOMAP
| CH_QUOTED
))
251 /* Push the user's command. */
252 if (v_event_push(sp
, NULL
, cmd
, len
, CH_NOMAP
| CH_QUOTED
))
255 /* Push line number so get correct z display. */
257 sizeof(buf
), "%lu", (u_long
)vp
->m_stop
.lno
);
258 if (v_event_push(sp
, NULL
, buf
, tlen
, CH_NOMAP
| CH_QUOTED
))
261 /* Don't refresh until after 'z' happens. */
262 F_SET(VIP(sp
), VIP_S_REFRESH
);
265 /* Non-motion commands move to the end of the range. */
266 vp
->m_final
= vp
->m_stop
;
268 F_CLR(vp
, VM_RCM_MASK
);
269 F_SET(vp
, VM_RCM_SETFNB
);
273 err1
: msgq(sp
, M_ERR
,
274 "188|Characters after search string, line offset and/or z command");
275 err2
: vp
->m_final
.lno
= s_lno
;
276 vp
->m_final
.cno
= s_cno
;
282 * Reverse last search.
284 * PUBLIC: int v_searchN __P((SCR *, VICMD *));
293 switch (sp
->searchdir
) {
304 return (v_search(sp
, vp
, NULL
, 0, SEARCH_PARSE
, dir
));
309 * Repeat last search.
311 * PUBLIC: int v_searchn __P((SCR *, VICMD *));
318 return (v_search(sp
, vp
, NULL
, 0, SEARCH_PARSE
, sp
->searchdir
));
322 * v_searchw -- [count]^A
323 * Search for the word under the cursor.
325 * PUBLIC: int v_searchw __P((SCR *, VICMD *));
336 len
= VIP(sp
)->klen
+ sizeof(RE_WSTART
) + sizeof(RE_WSTOP
);
337 GET_SPACE_RET(sp
, bp
, blen
, len
);
338 len
= snprintf(bp
, blen
, "%s%s%s", RE_WSTART
, VIP(sp
)->keyw
, RE_WSTOP
);
340 rval
= v_search(sp
, vp
, bp
, len
, SEARCH_SET
, FORWARD
);
342 FREE_SPACE(sp
, bp
, blen
);
348 * The search commands.
351 v_search(sp
, vp
, ptrn
, plen
, flags
, dir
)
359 /* Display messages. */
362 /* If it's a motion search, offset past end-of-line is okay. */
368 * Warn if the search wraps. See the comment above, in v_exaddr().
370 if (!KEYS_WAITING(sp
))
376 &vp
->m_start
, &vp
->m_stop
, ptrn
, plen
, NULL
, flags
))
381 &vp
->m_start
, &vp
->m_stop
, ptrn
, plen
, NULL
, flags
))
385 msgq(sp
, M_ERR
, "189|No previous search pattern");
391 /* Correct motion commands, otherwise, simply move to the location. */
393 if (v_correct(sp
, vp
, 0))
396 vp
->m_final
= vp
->m_stop
;
402 * Handle command with a search as the motion.
405 * Historically, commands didn't affect the line searched to/from if the
406 * motion command was a search and the final position was the start/end
407 * of the line. There were some special cases and vi was not consistent;
408 * it was fairly easy to confuse it. For example, given the two lines:
413 * placing the cursor on the 'A' and doing y?$ would so confuse it that 'h'
414 * 'k' and put would no longer work correctly. In any case, we try to do
415 * the right thing, but it's not going to exactly match historic practice.
417 * PUBLIC: int v_correct __P((SCR *, VICMD *, int));
420 v_correct(sp
, vp
, isdelta
)
431 * We may have wrapped if wrapscan was set, and we may have returned
432 * to the position where the cursor started. Historic vi didn't cope
433 * with this well. Yank wouldn't beep, but the first put after the
434 * yank would move the cursor right one column (without adding any
435 * text) and the second would put a copy of the current line. The
436 * change and delete commands would beep, but would leave the cursor
437 * on the colon command line. I believe that there are macros that
438 * depend on delete, at least, failing. For now, commands that use
439 * search as a motion component fail when the search returns to the
440 * original cursor position.
442 if (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
443 vp
->m_start
.cno
== vp
->m_stop
.cno
) {
444 msgq(sp
, M_BERR
, "190|Search wrapped to original position");
450 * Searches become line mode operations if there was a delta specified
451 * to the search pattern.
457 * If the motion is in the reverse direction, switch the start and
458 * stop MARK's so that it's in a forward direction. (There's no
459 * reason for this other than to make the tests below easier. The
460 * code in vi.c:vi() would have done the switch.) Both forward
461 * and backward motions can happen for any kind of search command
462 * because of the wrapscan option.
464 if (vp
->m_start
.lno
> vp
->m_stop
.lno
||
465 vp
->m_start
.lno
== vp
->m_stop
.lno
&&
466 vp
->m_start
.cno
> vp
->m_stop
.cno
) {
468 vp
->m_start
= vp
->m_stop
;
476 * Delete and yank commands move to the end of the range.
480 * Delete and yank commands don't move. Ignore others.
482 vp
->m_final
= vp
->m_start
;
486 * Delta'd searches don't correct based on column positions.
493 * Backward searches starting at column 0, and forward searches ending
494 * at column 0 are corrected to the last column of the previous line.
495 * Otherwise, adjust the starting/ending point to the character before
496 * the current one (this is safe because we know the search had to move
499 * Searches become line mode operations if they start at the first
500 * nonblank and end at column 0 of another line.
502 if (vp
->m_start
.lno
< vp
->m_stop
.lno
&& vp
->m_stop
.cno
== 0) {
503 if (db_get(sp
, --vp
->m_stop
.lno
, DBG_FATAL
, NULL
, &len
))
505 vp
->m_stop
.cno
= len
? len
- 1 : 0;
507 if (nonblank(sp
, vp
->m_start
.lno
, &len
))
509 if (vp
->m_start
.cno
<= len
)