private version of automake no longer needed
[nvi.git] / vi / v_search.c
blob12cba1e4248456f33d208bc6b60b1b6042918456
1 /*-
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.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_search.c,v 10.28 2001/06/13 20:01:31 skimo Exp $ (Berkeley) $Date: 2001/06/13 20:01:31 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include "../common/common.h"
29 #include "vi.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_T *, 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 *));
41 int
42 v_searchb(sp, vp)
43 SCR *sp;
44 VICMD *vp;
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 *));
55 int
56 v_searchf(sp, vp)
57 SCR *sp;
58 VICMD *vp;
60 return (v_exaddr(sp, vp, FORWARD));
64 * v_exaddr --
65 * Do a vi search (which is really an ex address).
67 static int
68 v_exaddr(sp, vp, dir)
69 SCR *sp;
70 VICMD *vp;
71 dir_t dir;
73 static EXCMDLIST fake = { L("search") };
74 EXCMD *cmdp;
75 WIN *wp;
76 TEXT *tp;
77 db_recno_t s_lno;
78 size_t len, s_cno, tlen;
79 int err, nb, type;
80 char buf[20];
81 CHAR_T *cmd, *t;
82 CHAR_T *w;
83 size_t wlen;
86 * !!!
87 * If using the search command as a motion, any addressing components
88 * are lost, i.e. y/ptrn/+2, when repeated, is the same as y/ptrn/.
90 if (F_ISSET(vp, VC_ISDOT))
91 return (v_search(sp, vp,
92 NULL, 0, SEARCH_PARSE | SEARCH_MSG | SEARCH_SET, dir));
94 /* Get the search pattern. */
95 if (v_tcmd(sp, vp, dir == BACKWARD ? CH_BSEARCH : CH_FSEARCH,
96 TXT_BS | TXT_CR | TXT_ESCAPE | TXT_PROMPT |
97 (O_ISSET(sp, O_SEARCHINCR) ? TXT_SEARCHINCR : 0)))
98 return (1);
100 tp = sp->tiq.cqh_first;
102 /* If the user backspaced over the prompt, do nothing. */
103 if (tp->term == TERM_BS)
104 return (1);
107 * If the user was doing an incremental search, then we've already
108 * updated the cursor and moved to the right location. Return the
109 * correct values, we're done.
111 if (tp->term == TERM_SEARCH) {
112 vp->m_stop.lno = sp->lno;
113 vp->m_stop.cno = sp->cno;
114 if (ISMOTION(vp))
115 return (v_correct(sp, vp, 0));
116 vp->m_final = vp->m_stop;
117 return (0);
121 * If the user entered <escape> or <carriage-return>, the length is
122 * 1 and the right thing will happen, i.e. the prompt will be used
123 * as a command character.
125 * Build a fake ex command structure.
127 wp = sp->wp;
128 wp->excmd.cp = tp->lb;
129 wp->excmd.clen = tp->len;
130 F_INIT(&wp->excmd, E_VISEARCH);
133 * XXX
134 * Warn if the search wraps. This is a pretty special case, but it's
135 * nice feature that wasn't in the original implementations of ex/vi.
136 * (It was added at some point to System V's version.) This message
137 * is only displayed if there are no keys in the queue. The problem is
138 * the command is going to succeed, and the message is informational,
139 * not an error. If a macro displays it repeatedly, e.g., the pattern
140 * only occurs once in the file and wrapscan is set, you lose big. For
141 * example, if the macro does something like:
143 * :map K /pattern/^MjK
145 * Each search will display the message, but the following "/pattern/"
146 * will immediately overwrite it, with strange results. The System V
147 * vi displays the "wrapped" message multiple times, but because it's
148 * overwritten each time, it's not as noticeable. As we don't discard
149 * messages, it's a real problem for us.
151 if (!KEYS_WAITING(sp))
152 F_SET(&wp->excmd, E_SEARCH_WMSG);
154 /* Save the current line/column. */
155 s_lno = sp->lno;
156 s_cno = sp->cno;
159 * !!!
160 * Historically, vi / and ? commands were full-blown ex addresses,
161 * including ';' delimiters, trailing <blank>'s, multiple search
162 * strings (separated by semi-colons) and, finally, full-blown z
163 * commands after the / and ? search strings. (If the search was
164 * being used as a motion, the trailing z command was ignored.
165 * Also, we do some argument checking on the z command, to be sure
166 * that it's not some other random command.) For multiple search
167 * strings, leading <blank>'s at the second and subsequent strings
168 * were eaten as well. This has some (unintended?) side-effects:
169 * the command /ptrn/;3 is legal and results in moving to line 3.
170 * I suppose you could use it to optionally move to line 3...
172 * !!!
173 * Historically, if any part of the search command failed, the cursor
174 * remained unmodified (even if ; was used). We have to play games
175 * because the underlying ex parser thinks we're modifying the cursor
176 * as we go, but I think we're compatible with historic practice.
178 * !!!
179 * Historically, the command "/STRING/; " failed, apparently it
180 * confused the parser. We're not that compatible.
182 cmdp = &wp->excmd;
183 if (ex_range(sp, cmdp, &err))
184 return (1);
187 * Remember where any remaining command information is, and clean
188 * up the fake ex command.
190 cmd = cmdp->cp;
191 len = cmdp->clen;
192 wp->excmd.clen = 0;
194 if (err)
195 goto err2;
197 /* Copy out the new cursor position and make sure it's okay. */
198 switch (cmdp->addrcnt) {
199 case 1:
200 vp->m_stop = cmdp->addr1;
201 break;
202 case 2:
203 vp->m_stop = cmdp->addr2;
204 break;
206 if (!db_exist(sp, vp->m_stop.lno)) {
207 ex_badaddr(sp, &fake,
208 vp->m_stop.lno == 0 ? A_ZERO : A_EOF, NUM_OK);
209 goto err2;
213 * !!!
214 * Historic practice is that a trailing 'z' was ignored if it was a
215 * motion command. Should probably be an error, but not worth the
216 * effort.
218 if (ISMOTION(vp))
219 return (v_correct(sp, vp, F_ISSET(cmdp, E_DELTA)));
222 * !!!
223 * Historically, if it wasn't a motion command, a delta in the search
224 * pattern turns it into a first nonblank movement.
226 nb = F_ISSET(cmdp, E_DELTA);
228 /* Check for the 'z' command. */
229 if (len != 0) {
230 if (*cmd != 'z')
231 goto err1;
233 /* No blanks, just like the z command. */
234 for (t = cmd + 1, tlen = len - 1; tlen > 0; ++t, --tlen)
235 if (!isdigit(*t))
236 break;
237 if (tlen &&
238 (*t == '-' || *t == '.' || *t == '+' || *t == '^')) {
239 ++t;
240 --tlen;
241 type = 1;
242 } else
243 type = 0;
244 if (tlen)
245 goto err1;
247 /* The z command will do the nonblank for us. */
248 nb = 0;
250 /* Default to z+. */
251 if (!type &&
252 v_event_push(sp, NULL, L("+"), 1, CH_NOMAP | CH_QUOTED))
253 return (1);
255 /* Push the user's command. */
256 if (v_event_push(sp, NULL, cmd, len, CH_NOMAP | CH_QUOTED))
257 return (1);
259 /* Push line number so get correct z display. */
260 tlen = snprintf(buf,
261 sizeof(buf), "%lu", (u_long)vp->m_stop.lno);
262 CHAR2INT(sp, buf, tlen, w, wlen);
263 if (v_event_push(sp, NULL, w, wlen, CH_NOMAP | CH_QUOTED))
264 return (1);
266 /* Don't refresh until after 'z' happens. */
267 F_SET(VIP(sp), VIP_S_REFRESH);
270 /* Non-motion commands move to the end of the range. */
271 vp->m_final = vp->m_stop;
272 if (nb) {
273 F_CLR(vp, VM_RCM_MASK);
274 F_SET(vp, VM_RCM_SETFNB);
276 return (0);
278 err1: msgq(sp, M_ERR,
279 "188|Characters after search string, line offset and/or z command");
280 err2: vp->m_final.lno = s_lno;
281 vp->m_final.cno = s_cno;
282 return (1);
286 * v_searchN -- N
287 * Reverse last search.
289 * PUBLIC: int v_searchN __P((SCR *, VICMD *));
292 v_searchN(sp, vp)
293 SCR *sp;
294 VICMD *vp;
296 dir_t dir;
298 switch (sp->searchdir) {
299 case BACKWARD:
300 dir = FORWARD;
301 break;
302 case FORWARD:
303 dir = BACKWARD;
304 break;
305 default:
306 dir = sp->searchdir;
307 break;
309 return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, dir));
313 * v_searchn -- n
314 * Repeat last search.
316 * PUBLIC: int v_searchn __P((SCR *, VICMD *));
319 v_searchn(sp, vp)
320 SCR *sp;
321 VICMD *vp;
323 return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, sp->searchdir));
327 * v_searchw -- [count]^A
328 * Search for the word under the cursor.
330 * PUBLIC: int v_searchw __P((SCR *, VICMD *));
333 v_searchw(sp, vp)
334 SCR *sp;
335 VICMD *vp;
337 size_t blen, len;
338 int rval;
339 CHAR_T *bp, *p;
341 len = VIP(sp)->klen + RE_WSTART_LEN + RE_WSTOP_LEN;
342 GET_SPACE_RETW(sp, bp, blen, len);
343 MEMCPY(bp, RE_WSTART, RE_WSTART_LEN);
344 p = bp + sizeof(RE_WSTART)/sizeof(CHAR_T) - 1;
345 MEMCPY(p, VIP(sp)->keyw, VIP(sp)->klen);
346 p += VIP(sp)->klen;
347 MEMCPY(bp, RE_WSTOP, RE_WSTOP_LEN);
349 rval = v_search(sp, vp, bp, len, SEARCH_SET, FORWARD);
351 FREE_SPACEW(sp, bp, blen);
352 return (rval);
356 * v_esearch -- <dialog box>
357 * Search command from the screen.
359 * PUBLIC: int v_esearch __P((SCR *, VICMD *));
362 v_esearch(sp, vp)
363 SCR *sp;
364 VICMD *vp;
366 MARK m;
367 int flags;
369 m.lno = sp->lno;
370 m.cno = sp->cno;
372 LF_INIT(SEARCH_NOOPT);
373 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_EXT))
374 LF_SET(SEARCH_EXTEND);
375 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_IC))
376 LF_SET(SEARCH_IC);
377 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_ICL))
378 LF_SET(SEARCH_ICL);
379 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_INCR))
380 LF_SET(SEARCH_INCR);
381 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_LIT))
382 LF_SET(SEARCH_LITERAL);
383 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_WR))
384 LF_SET(SEARCH_WRAP);
385 return (v_search(sp, vp, vp->ev.e_csp, vp->ev.e_len, flags,
386 FL_ISSET(vp->ev.e_flags, VI_SEARCH_REV) ? BACKWARD : FORWARD));
390 * v_search --
391 * The search commands.
393 static int
394 v_search(sp, vp, ptrn, plen, flags, dir)
395 SCR *sp;
396 VICMD *vp;
397 u_int flags;
398 CHAR_T *ptrn;
399 size_t plen;
400 dir_t dir;
402 /* Display messages. */
403 LF_SET(SEARCH_MSG);
405 /* If it's a motion search, offset past end-of-line is okay. */
406 if (ISMOTION(vp))
407 LF_SET(SEARCH_EOL);
410 * XXX
411 * Warn if the search wraps. See the comment above, in v_exaddr().
413 if (!KEYS_WAITING(sp))
414 LF_SET(SEARCH_WMSG);
416 switch (dir) {
417 case BACKWARD:
418 if (b_search(sp,
419 &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
420 return (1);
421 break;
422 case FORWARD:
423 if (f_search(sp,
424 &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
425 return (1);
426 break;
427 case NOTSET:
428 msgq(sp, M_ERR, "189|No previous search pattern");
429 return (1);
430 default:
431 abort();
434 /* Correct motion commands, otherwise, simply move to the location. */
435 if (ISMOTION(vp)) {
436 if (v_correct(sp, vp, 0))
437 return(1);
438 } else
439 vp->m_final = vp->m_stop;
440 return (0);
444 * v_correct --
445 * Handle command with a search as the motion.
447 * !!!
448 * Historically, commands didn't affect the line searched to/from if the
449 * motion command was a search and the final position was the start/end
450 * of the line. There were some special cases and vi was not consistent;
451 * it was fairly easy to confuse it. For example, given the two lines:
453 * abcdefghi
454 * ABCDEFGHI
456 * placing the cursor on the 'A' and doing y?$ would so confuse it that 'h'
457 * 'k' and put would no longer work correctly. In any case, we try to do
458 * the right thing, but it's not going to exactly match historic practice.
460 * PUBLIC: int v_correct __P((SCR *, VICMD *, int));
463 v_correct(sp, vp, isdelta)
464 SCR *sp;
465 VICMD *vp;
466 int isdelta;
468 dir_t dir;
469 MARK m;
470 size_t len;
473 * !!!
474 * We may have wrapped if wrapscan was set, and we may have returned
475 * to the position where the cursor started. Historic vi didn't cope
476 * with this well. Yank wouldn't beep, but the first put after the
477 * yank would move the cursor right one column (without adding any
478 * text) and the second would put a copy of the current line. The
479 * change and delete commands would beep, but would leave the cursor
480 * on the colon command line. I believe that there are macros that
481 * depend on delete, at least, failing. For now, commands that use
482 * search as a motion component fail when the search returns to the
483 * original cursor position.
485 if (vp->m_start.lno == vp->m_stop.lno &&
486 vp->m_start.cno == vp->m_stop.cno) {
487 msgq(sp, M_BERR, "190|Search wrapped to original position");
488 return (1);
492 * !!!
493 * Searches become line mode operations if there was a delta specified
494 * to the search pattern.
496 if (isdelta)
497 F_SET(vp, VM_LMODE);
500 * If the motion is in the reverse direction, switch the start and
501 * stop MARK's so that it's in a forward direction. (There's no
502 * reason for this other than to make the tests below easier. The
503 * code in vi.c:vi() would have done the switch.) Both forward
504 * and backward motions can happen for any kind of search command
505 * because of the wrapscan option.
507 if (vp->m_start.lno > vp->m_stop.lno ||
508 vp->m_start.lno == vp->m_stop.lno &&
509 vp->m_start.cno > vp->m_stop.cno) {
510 m = vp->m_start;
511 vp->m_start = vp->m_stop;
512 vp->m_stop = m;
513 dir = BACKWARD;
514 } else
515 dir = FORWARD;
518 * BACKWARD:
519 * Delete and yank commands move to the end of the range.
520 * Ignore others.
522 * FORWARD:
523 * Delete and yank commands don't move. Ignore others.
525 vp->m_final = vp->m_start;
528 * !!!
529 * Delta'd searches don't correct based on column positions.
531 if (isdelta)
532 return (0);
535 * !!!
536 * Backward searches starting at column 0, and forward searches ending
537 * at column 0 are corrected to the last column of the previous line.
538 * Otherwise, adjust the starting/ending point to the character before
539 * the current one (this is safe because we know the search had to move
540 * to succeed).
542 * Searches become line mode operations if they start at the first
543 * nonblank and end at column 0 of another line.
545 if (vp->m_start.lno < vp->m_stop.lno && vp->m_stop.cno == 0) {
546 if (db_get(sp, --vp->m_stop.lno, DBG_FATAL, NULL, &len))
547 return (1);
548 vp->m_stop.cno = len ? len - 1 : 0;
549 len = 0;
550 if (nonblank(sp, vp->m_start.lno, &len))
551 return (1);
552 if (vp->m_start.cno <= len)
553 F_SET(vp, VM_LMODE);
554 } else
555 --vp->m_stop.cno;
557 return (0);