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
[] = "$Id: v_search.c,v 10.22 1996/12/18 10:24:41 bostic Exp $ (Berkeley) $Date: 1996/12/18 10:24:41 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
28 #include "../common/common.h"
30 #include "../ipc/ip.h"
32 static int v_exaddr
__P((SCR
*, VICMD
*, dir_t
));
33 static int v_search
__P((SCR
*, VICMD
*, char *, size_t, u_int
, dir_t
));
36 * v_srch -- [count]?RE[? offset]
37 * Ex address search backward.
39 * PUBLIC: int v_searchb __P((SCR *, VICMD *));
46 return (v_exaddr(sp
, vp
, BACKWARD
));
50 * v_searchf -- [count]/RE[/ offset]
51 * Ex address search forward.
53 * PUBLIC: int v_searchf __P((SCR *, VICMD *));
60 return (v_exaddr(sp
, vp
, FORWARD
));
65 * Do a vi search (which is really an ex address).
73 static EXCMDLIST fake
= { "search" };
78 size_t len
, s_cno
, tlen
;
80 char *cmd
, *t
, buf
[20];
84 * If using the search command as a motion, any addressing components
85 * are lost, i.e. y/ptrn/+2, when repeated, is the same as y/ptrn/.
87 if (F_ISSET(vp
, VC_ISDOT
))
88 return (v_search(sp
, vp
,
89 NULL
, 0, SEARCH_PARSE
| SEARCH_MSG
| SEARCH_SET
, dir
));
91 /* Get the search pattern. */
92 if (v_tcmd(sp
, vp
, dir
== BACKWARD
? CH_BSEARCH
: CH_FSEARCH
,
93 TXT_BS
| TXT_CR
| TXT_ESCAPE
| TXT_PROMPT
|
94 (O_ISSET(sp
, O_SEARCHINCR
) ? TXT_SEARCHINCR
: 0)))
97 tp
= sp
->tiq
.cqh_first
;
99 /* If the user backspaced over the prompt, do nothing. */
100 if (tp
->term
== TERM_BS
)
104 * If the user was doing an incremental search, then we've already
105 * updated the cursor and moved to the right location. Return the
106 * correct values, we're done.
108 if (tp
->term
== TERM_SEARCH
) {
109 vp
->m_stop
.lno
= sp
->lno
;
110 vp
->m_stop
.cno
= sp
->cno
;
112 return (v_correct(sp
, vp
, 0));
113 vp
->m_final
= vp
->m_stop
;
118 * If the user entered <escape> or <carriage-return>, the length is
119 * 1 and the right thing will happen, i.e. the prompt will be used
120 * as a command character.
122 * Build a fake ex command structure.
125 gp
->excmd
.cp
= tp
->lb
;
126 gp
->excmd
.clen
= tp
->len
;
127 F_INIT(&gp
->excmd
, E_VISEARCH
);
131 * Warn if the search wraps. This is a pretty special case, but it's
132 * nice feature that wasn't in the original implementations of ex/vi.
133 * (It was added at some point to System V's version.) This message
134 * is only displayed if there are no keys in the queue. The problem is
135 * the command is going to succeed, and the message is informational,
136 * not an error. If a macro displays it repeatedly, e.g., the pattern
137 * only occurs once in the file and wrapscan is set, you lose big. For
138 * example, if the macro does something like:
140 * :map K /pattern/^MjK
142 * Each search will display the message, but the following "/pattern/"
143 * will immediately overwrite it, with strange results. The System V
144 * vi displays the "wrapped" message multiple times, but because it's
145 * overwritten each time, it's not as noticeable. As we don't discard
146 * messages, it's a real problem for us.
148 if (!KEYS_WAITING(sp
))
149 F_SET(&gp
->excmd
, E_SEARCH_WMSG
);
151 /* Save the current line/column. */
157 * Historically, vi / and ? commands were full-blown ex addresses,
158 * including ';' delimiters, trailing <blank>'s, multiple search
159 * strings (separated by semi-colons) and, finally, full-blown z
160 * commands after the / and ? search strings. (If the search was
161 * being used as a motion, the trailing z command was ignored.
162 * Also, we do some argument checking on the z command, to be sure
163 * that it's not some other random command.) For multiple search
164 * strings, leading <blank>'s at the second and subsequent strings
165 * were eaten as well. This has some (unintended?) side-effects:
166 * the command /ptrn/;3 is legal and results in moving to line 3.
167 * I suppose you could use it to optionally move to line 3...
170 * Historically, if any part of the search command failed, the cursor
171 * remained unmodified (even if ; was used). We have to play games
172 * because the underlying ex parser thinks we're modifying the cursor
173 * as we go, but I think we're compatible with historic practice.
176 * Historically, the command "/STRING/; " failed, apparently it
177 * confused the parser. We're not that compatible.
180 if (ex_range(sp
, cmdp
, &err
))
184 * Remember where any remaining command information is, and clean
185 * up the fake ex command.
194 /* Copy out the new cursor position and make sure it's okay. */
195 switch (cmdp
->addrcnt
) {
197 vp
->m_stop
= cmdp
->addr1
;
200 vp
->m_stop
= cmdp
->addr2
;
203 if (!db_exist(sp
, vp
->m_stop
.lno
)) {
204 ex_badaddr(sp
, &fake
,
205 vp
->m_stop
.lno
== 0 ? A_ZERO
: A_EOF
, NUM_OK
);
211 * Historic practice is that a trailing 'z' was ignored if it was a
212 * motion command. Should probably be an error, but not worth the
216 return (v_correct(sp
, vp
, F_ISSET(cmdp
, E_DELTA
)));
220 * Historically, if it wasn't a motion command, a delta in the search
221 * pattern turns it into a first nonblank movement.
223 nb
= F_ISSET(cmdp
, E_DELTA
);
225 /* Check for the 'z' command. */
230 /* No blanks, just like the z command. */
231 for (t
= cmd
+ 1, tlen
= len
- 1; tlen
> 0; ++t
, --tlen
)
235 (*t
== '-' || *t
== '.' || *t
== '+' || *t
== '^')) {
244 /* The z command will do the nonblank for us. */
249 v_event_push(sp
, NULL
, "+", 1, CH_NOMAP
| CH_QUOTED
))
252 /* Push the user's command. */
253 if (v_event_push(sp
, NULL
, cmd
, len
, CH_NOMAP
| CH_QUOTED
))
256 /* Push line number so get correct z display. */
258 sizeof(buf
), "%lu", (u_long
)vp
->m_stop
.lno
);
259 if (v_event_push(sp
, NULL
, buf
, tlen
, CH_NOMAP
| CH_QUOTED
))
262 /* Don't refresh until after 'z' happens. */
263 F_SET(VIP(sp
), VIP_S_REFRESH
);
266 /* Non-motion commands move to the end of the range. */
267 vp
->m_final
= vp
->m_stop
;
269 F_CLR(vp
, VM_RCM_MASK
);
270 F_SET(vp
, VM_RCM_SETFNB
);
274 err1
: msgq(sp
, M_ERR
,
275 "188|Characters after search string, line offset and/or z command");
276 err2
: vp
->m_final
.lno
= s_lno
;
277 vp
->m_final
.cno
= s_cno
;
283 * Reverse last search.
285 * PUBLIC: int v_searchN __P((SCR *, VICMD *));
294 switch (sp
->searchdir
) {
305 return (v_search(sp
, vp
, NULL
, 0, SEARCH_PARSE
, dir
));
310 * Repeat last search.
312 * PUBLIC: int v_searchn __P((SCR *, VICMD *));
319 return (v_search(sp
, vp
, NULL
, 0, SEARCH_PARSE
, sp
->searchdir
));
323 * v_searchw -- [count]^A
324 * Search for the word under the cursor.
326 * PUBLIC: int v_searchw __P((SCR *, VICMD *));
337 len
= VIP(sp
)->klen
+ sizeof(RE_WSTART
) + sizeof(RE_WSTOP
);
338 GET_SPACE_RET(sp
, bp
, blen
, len
);
339 len
= snprintf(bp
, blen
, "%s%s%s", RE_WSTART
, VIP(sp
)->keyw
, RE_WSTOP
);
341 rval
= v_search(sp
, vp
, bp
, len
, SEARCH_SET
, FORWARD
);
343 FREE_SPACE(sp
, bp
, blen
);
348 * v_esearch -- <dialog box>
349 * Search command from the screen.
351 * PUBLIC: int v_esearch __P((SCR *, VICMD *));
364 LF_INIT(SEARCH_NOOPT
);
365 if (FL_ISSET(vp
->ev
.e_flags
, VI_SEARCH_EXT
))
366 LF_SET(SEARCH_EXTEND
);
367 if (FL_ISSET(vp
->ev
.e_flags
, VI_SEARCH_IC
))
369 if (FL_ISSET(vp
->ev
.e_flags
, VI_SEARCH_ICL
))
371 if (FL_ISSET(vp
->ev
.e_flags
, VI_SEARCH_INCR
))
373 if (FL_ISSET(vp
->ev
.e_flags
, VI_SEARCH_LIT
))
374 LF_SET(SEARCH_LITERAL
);
375 if (FL_ISSET(vp
->ev
.e_flags
, VI_SEARCH_WR
))
377 return (v_search(sp
, vp
, vp
->ev
.e_csp
, vp
->ev
.e_len
, flags
,
378 FL_ISSET(vp
->ev
.e_flags
, VI_SEARCH_REV
) ? BACKWARD
: FORWARD
));
383 * The search commands.
386 v_search(sp
, vp
, ptrn
, plen
, flags
, dir
)
394 /* Display messages. */
397 /* If it's a motion search, offset past end-of-line is okay. */
403 * Warn if the search wraps. See the comment above, in v_exaddr().
405 if (!KEYS_WAITING(sp
))
411 &vp
->m_start
, &vp
->m_stop
, ptrn
, plen
, NULL
, flags
))
416 &vp
->m_start
, &vp
->m_stop
, ptrn
, plen
, NULL
, flags
))
420 msgq(sp
, M_ERR
, "189|No previous search pattern");
426 /* Correct motion commands, otherwise, simply move to the location. */
428 if (v_correct(sp
, vp
, 0))
431 vp
->m_final
= vp
->m_stop
;
437 * Handle command with a search as the motion.
440 * Historically, commands didn't affect the line searched to/from if the
441 * motion command was a search and the final position was the start/end
442 * of the line. There were some special cases and vi was not consistent;
443 * it was fairly easy to confuse it. For example, given the two lines:
448 * placing the cursor on the 'A' and doing y?$ would so confuse it that 'h'
449 * 'k' and put would no longer work correctly. In any case, we try to do
450 * the right thing, but it's not going to exactly match historic practice.
452 * PUBLIC: int v_correct __P((SCR *, VICMD *, int));
455 v_correct(sp
, vp
, isdelta
)
466 * We may have wrapped if wrapscan was set, and we may have returned
467 * to the position where the cursor started. Historic vi didn't cope
468 * with this well. Yank wouldn't beep, but the first put after the
469 * yank would move the cursor right one column (without adding any
470 * text) and the second would put a copy of the current line. The
471 * change and delete commands would beep, but would leave the cursor
472 * on the colon command line. I believe that there are macros that
473 * depend on delete, at least, failing. For now, commands that use
474 * search as a motion component fail when the search returns to the
475 * original cursor position.
477 if (vp
->m_start
.lno
== vp
->m_stop
.lno
&&
478 vp
->m_start
.cno
== vp
->m_stop
.cno
) {
479 msgq(sp
, M_BERR
, "190|Search wrapped to original position");
485 * Searches become line mode operations if there was a delta specified
486 * to the search pattern.
492 * If the motion is in the reverse direction, switch the start and
493 * stop MARK's so that it's in a forward direction. (There's no
494 * reason for this other than to make the tests below easier. The
495 * code in vi.c:vi() would have done the switch.) Both forward
496 * and backward motions can happen for any kind of search command
497 * because of the wrapscan option.
499 if (vp
->m_start
.lno
> vp
->m_stop
.lno
||
500 vp
->m_start
.lno
== vp
->m_stop
.lno
&&
501 vp
->m_start
.cno
> vp
->m_stop
.cno
) {
503 vp
->m_start
= vp
->m_stop
;
511 * Delete and yank commands move to the end of the range.
515 * Delete and yank commands don't move. Ignore others.
517 vp
->m_final
= vp
->m_start
;
521 * Delta'd searches don't correct based on column positions.
528 * Backward searches starting at column 0, and forward searches ending
529 * at column 0 are corrected to the last column of the previous line.
530 * Otherwise, adjust the starting/ending point to the character before
531 * the current one (this is safe because we know the search had to move
534 * Searches become line mode operations if they start at the first
535 * nonblank and end at column 0 of another line.
537 if (vp
->m_start
.lno
< vp
->m_stop
.lno
&& vp
->m_stop
.cno
== 0) {
538 if (db_get(sp
, --vp
->m_stop
.lno
, DBG_FATAL
, NULL
, &len
))
540 vp
->m_stop
.cno
= len
? len
- 1 : 0;
542 if (nonblank(sp
, vp
->m_start
.lno
, &len
))
544 if (vp
->m_start
.cno
<= len
)