optionally use bundled db
[nvi.git] / vi / v_search.c
blobf9848998d587d4fdf657bd3f8ccf47d9de081c54
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.30 2001/09/11 20:52:46 skimo Exp $ (Berkeley) $Date: 2001/09/11 20:52:46 $";
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(SCR *sp, VICMD *vp)
44 return (v_exaddr(sp, vp, BACKWARD));
48 * v_searchf -- [count]/RE[/ offset]
49 * Ex address search forward.
51 * PUBLIC: int v_searchf __P((SCR *, VICMD *));
53 int
54 v_searchf(SCR *sp, VICMD *vp)
56 return (v_exaddr(sp, vp, FORWARD));
60 * v_exaddr --
61 * Do a vi search (which is really an ex address).
63 static int
64 v_exaddr(SCR *sp, VICMD *vp, dir_t dir)
66 static EXCMDLIST fake = { L("search") };
67 EXCMD *cmdp;
68 WIN *wp;
69 TEXT *tp;
70 db_recno_t s_lno;
71 size_t len, s_cno, tlen;
72 int err, nb, type;
73 char buf[20];
74 CHAR_T *cmd, *t;
75 CHAR_T *w;
76 size_t wlen;
79 * !!!
80 * If using the search command as a motion, any addressing components
81 * are lost, i.e. y/ptrn/+2, when repeated, is the same as y/ptrn/.
83 if (F_ISSET(vp, VC_ISDOT))
84 return (v_search(sp, vp,
85 NULL, 0, SEARCH_PARSE | SEARCH_MSG | SEARCH_SET, dir));
87 /* Get the search pattern. */
88 if (v_tcmd(sp, vp, dir == BACKWARD ? CH_BSEARCH : CH_FSEARCH,
89 TXT_BS | TXT_CR | TXT_ESCAPE | TXT_PROMPT |
90 (O_ISSET(sp, O_SEARCHINCR) ? TXT_SEARCHINCR : 0)))
91 return (1);
93 tp = sp->tiq.cqh_first;
95 /* If the user backspaced over the prompt, do nothing. */
96 if (tp->term == TERM_BS)
97 return (1);
100 * If the user was doing an incremental search, then we've already
101 * updated the cursor and moved to the right location. Return the
102 * correct values, we're done.
104 if (tp->term == TERM_SEARCH) {
105 vp->m_stop.lno = sp->lno;
106 vp->m_stop.cno = sp->cno;
107 if (ISMOTION(vp))
108 return (v_correct(sp, vp, 0));
109 vp->m_final = vp->m_stop;
110 return (0);
114 * If the user entered <escape> or <carriage-return>, the length is
115 * 1 and the right thing will happen, i.e. the prompt will be used
116 * as a command character.
118 * Build a fake ex command structure.
120 wp = sp->wp;
121 wp->excmd.cp = tp->lb;
122 wp->excmd.clen = tp->len;
123 F_INIT(&wp->excmd, E_VISEARCH);
126 * XXX
127 * Warn if the search wraps. This is a pretty special case, but it's
128 * nice feature that wasn't in the original implementations of ex/vi.
129 * (It was added at some point to System V's version.) This message
130 * is only displayed if there are no keys in the queue. The problem is
131 * the command is going to succeed, and the message is informational,
132 * not an error. If a macro displays it repeatedly, e.g., the pattern
133 * only occurs once in the file and wrapscan is set, you lose big. For
134 * example, if the macro does something like:
136 * :map K /pattern/^MjK
138 * Each search will display the message, but the following "/pattern/"
139 * will immediately overwrite it, with strange results. The System V
140 * vi displays the "wrapped" message multiple times, but because it's
141 * overwritten each time, it's not as noticeable. As we don't discard
142 * messages, it's a real problem for us.
144 if (!KEYS_WAITING(sp))
145 F_SET(&wp->excmd, E_SEARCH_WMSG);
147 /* Save the current line/column. */
148 s_lno = sp->lno;
149 s_cno = sp->cno;
152 * !!!
153 * Historically, vi / and ? commands were full-blown ex addresses,
154 * including ';' delimiters, trailing <blank>'s, multiple search
155 * strings (separated by semi-colons) and, finally, full-blown z
156 * commands after the / and ? search strings. (If the search was
157 * being used as a motion, the trailing z command was ignored.
158 * Also, we do some argument checking on the z command, to be sure
159 * that it's not some other random command.) For multiple search
160 * strings, leading <blank>'s at the second and subsequent strings
161 * were eaten as well. This has some (unintended?) side-effects:
162 * the command /ptrn/;3 is legal and results in moving to line 3.
163 * I suppose you could use it to optionally move to line 3...
165 * !!!
166 * Historically, if any part of the search command failed, the cursor
167 * remained unmodified (even if ; was used). We have to play games
168 * because the underlying ex parser thinks we're modifying the cursor
169 * as we go, but I think we're compatible with historic practice.
171 * !!!
172 * Historically, the command "/STRING/; " failed, apparently it
173 * confused the parser. We're not that compatible.
175 cmdp = &wp->excmd;
176 if (ex_range(sp, cmdp, &err))
177 return (1);
180 * Remember where any remaining command information is, and clean
181 * up the fake ex command.
183 cmd = cmdp->cp;
184 len = cmdp->clen;
185 wp->excmd.clen = 0;
187 if (err)
188 goto err2;
190 /* Copy out the new cursor position and make sure it's okay. */
191 switch (cmdp->addrcnt) {
192 case 1:
193 vp->m_stop = cmdp->addr1;
194 break;
195 case 2:
196 vp->m_stop = cmdp->addr2;
197 break;
199 if (!db_exist(sp, vp->m_stop.lno)) {
200 ex_badaddr(sp, &fake,
201 vp->m_stop.lno == 0 ? A_ZERO : A_EOF, NUM_OK);
202 goto err2;
206 * !!!
207 * Historic practice is that a trailing 'z' was ignored if it was a
208 * motion command. Should probably be an error, but not worth the
209 * effort.
211 if (ISMOTION(vp))
212 return (v_correct(sp, vp, F_ISSET(cmdp, E_DELTA)));
215 * !!!
216 * Historically, if it wasn't a motion command, a delta in the search
217 * pattern turns it into a first nonblank movement.
219 nb = F_ISSET(cmdp, E_DELTA);
221 /* Check for the 'z' command. */
222 if (len != 0) {
223 if (*cmd != 'z')
224 goto err1;
226 /* No blanks, just like the z command. */
227 for (t = cmd + 1, tlen = len - 1; tlen > 0; ++t, --tlen)
228 if (!isdigit(*t))
229 break;
230 if (tlen &&
231 (*t == '-' || *t == '.' || *t == '+' || *t == '^')) {
232 ++t;
233 --tlen;
234 type = 1;
235 } else
236 type = 0;
237 if (tlen)
238 goto err1;
240 /* The z command will do the nonblank for us. */
241 nb = 0;
243 /* Default to z+. */
244 if (!type &&
245 v_event_push(sp, NULL, L("+"), 1, CH_NOMAP | CH_QUOTED))
246 return (1);
248 /* Push the user's command. */
249 if (v_event_push(sp, NULL, cmd, len, CH_NOMAP | CH_QUOTED))
250 return (1);
252 /* Push line number so get correct z display. */
253 tlen = snprintf(buf,
254 sizeof(buf), "%lu", (u_long)vp->m_stop.lno);
255 CHAR2INT(sp, buf, tlen, w, wlen);
256 if (v_event_push(sp, NULL, w, wlen, CH_NOMAP | CH_QUOTED))
257 return (1);
259 /* Don't refresh until after 'z' happens. */
260 F_SET(VIP(sp), VIP_S_REFRESH);
263 /* Non-motion commands move to the end of the range. */
264 vp->m_final = vp->m_stop;
265 if (nb) {
266 F_CLR(vp, VM_RCM_MASK);
267 F_SET(vp, VM_RCM_SETFNB);
269 return (0);
271 err1: msgq(sp, M_ERR,
272 "188|Characters after search string, line offset and/or z command");
273 err2: vp->m_final.lno = s_lno;
274 vp->m_final.cno = s_cno;
275 return (1);
279 * v_searchN -- N
280 * Reverse last search.
282 * PUBLIC: int v_searchN __P((SCR *, VICMD *));
285 v_searchN(SCR *sp, VICMD *vp)
287 dir_t dir;
289 switch (sp->searchdir) {
290 case BACKWARD:
291 dir = FORWARD;
292 break;
293 case FORWARD:
294 dir = BACKWARD;
295 break;
296 default:
297 dir = sp->searchdir;
298 break;
300 return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, dir));
304 * v_searchn -- n
305 * Repeat last search.
307 * PUBLIC: int v_searchn __P((SCR *, VICMD *));
310 v_searchn(SCR *sp, VICMD *vp)
312 return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, sp->searchdir));
316 * is_especial --
317 * Test if the character is special in an extended RE.
319 static int
320 is_especial(CHAR_T c)
323 * !!!
324 * Right-brace is not an ERE special according to IEEE 1003.1-2001.
325 * Right-parenthesis is a special character (so quoting doesn't hurt),
326 * though it has no special meaning in this context, viz. at the
327 * beginning of the string. So we need not quote it. Then again,
328 * see the BUGS section in regex/re_format.7.
329 * The tilde is vi-specific, of course.
331 return (STRCHR(L(".[\\()*+?{|^$~"), c) && c);
335 * Rear delimiter for word search when the keyword ends in
336 * (i.e., consists of) a non-word character. See v_searchw below.
338 #define RE_NWSTOP L("([^[:alnum:]_]|$)")
339 #define RE_NWSTOP_LEN (SIZE(RE_NWSTOP) - 1)
342 * v_searchw -- [count]^A
343 * Search for the word under the cursor.
345 * PUBLIC: int v_searchw __P((SCR *, VICMD *));
348 v_searchw(SCR *sp, VICMD *vp)
350 size_t blen;
351 /* An upper bound for the SIZE of the RE under construction. */
352 size_t len = VIP(sp)->klen + MAX(RE_WSTART_LEN, 1)
353 + MAX(RE_WSTOP_LEN, RE_NWSTOP_LEN);
354 int rval;
355 CHAR_T *bp, *p;
357 GET_SPACE_RETW(sp, bp, blen, len);
358 p = bp;
360 /* Only the first character can be non-word, see v_curword. */
361 if (inword(VIP(sp)->keyw[0]))
362 p = MEMPCPY(p, RE_WSTART, RE_WSTART_LEN);
363 else if (is_especial(VIP(sp)->keyw[0]))
364 p = MEMPCPY(p, L("\\"), 1);
366 p = MEMPCPY(p, VIP(sp)->keyw, VIP(sp)->klen);
368 if (inword(p[-1]))
369 p = MEMPCPY(p, RE_WSTOP, RE_WSTOP_LEN);
370 else
372 * The keyword is a single non-word character.
373 * We want it to stay the same when typing ^A several times
374 * in a row, just the way the other cases behave.
376 p = MEMPCPY(p, RE_NWSTOP, RE_NWSTOP_LEN);
378 len = p - bp;
379 rval = v_search(sp, vp, bp, len, SEARCH_SET | SEARCH_EXTEND, FORWARD);
381 FREE_SPACEW(sp, bp, blen);
382 return (rval);
386 * v_esearch -- <dialog box>
387 * Search command from the screen.
389 * PUBLIC: int v_esearch __P((SCR *, VICMD *));
392 v_esearch(SCR *sp, VICMD *vp)
394 MARK m;
395 int flags;
397 m.lno = sp->lno;
398 m.cno = sp->cno;
400 LF_INIT(SEARCH_NOOPT);
401 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_EXT))
402 LF_SET(SEARCH_EXTEND);
403 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_IC))
404 LF_SET(SEARCH_IC);
405 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_ICL))
406 LF_SET(SEARCH_ICL);
407 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_INCR))
408 LF_SET(SEARCH_INCR);
409 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_LIT))
410 LF_SET(SEARCH_LITERAL);
411 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_WR))
412 LF_SET(SEARCH_WRAP);
413 return (v_search(sp, vp, vp->ev.e_csp, vp->ev.e_len, flags,
414 FL_ISSET(vp->ev.e_flags, VI_SEARCH_REV) ? BACKWARD : FORWARD));
418 * v_search --
419 * The search commands.
421 static int
422 v_search(SCR *sp, VICMD *vp, CHAR_T *ptrn, size_t plen, u_int flags, dir_t dir)
424 /* Display messages. */
425 LF_SET(SEARCH_MSG);
427 /* If it's a motion search, offset past end-of-line is okay. */
428 if (ISMOTION(vp))
429 LF_SET(SEARCH_EOL);
432 * XXX
433 * Warn if the search wraps. See the comment above, in v_exaddr().
435 if (!KEYS_WAITING(sp))
436 LF_SET(SEARCH_WMSG);
438 switch (dir) {
439 case BACKWARD:
440 if (b_search(sp,
441 &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
442 return (1);
443 break;
444 case FORWARD:
445 if (f_search(sp,
446 &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
447 return (1);
448 break;
449 case NOTSET:
450 msgq(sp, M_ERR, "189|No previous search pattern");
451 return (1);
452 default:
453 abort();
456 /* Correct motion commands, otherwise, simply move to the location. */
457 if (ISMOTION(vp)) {
458 if (v_correct(sp, vp, 0))
459 return(1);
460 } else
461 vp->m_final = vp->m_stop;
462 return (0);
466 * v_correct --
467 * Handle command with a search as the motion.
469 * !!!
470 * Historically, commands didn't affect the line searched to/from if the
471 * motion command was a search and the final position was the start/end
472 * of the line. There were some special cases and vi was not consistent;
473 * it was fairly easy to confuse it. For example, given the two lines:
475 * abcdefghi
476 * ABCDEFGHI
478 * placing the cursor on the 'A' and doing y?$ would so confuse it that 'h'
479 * 'k' and put would no longer work correctly. In any case, we try to do
480 * the right thing, but it's not going to exactly match historic practice.
482 * PUBLIC: int v_correct __P((SCR *, VICMD *, int));
485 v_correct(SCR *sp, VICMD *vp, int isdelta)
487 dir_t dir;
488 MARK m;
489 size_t len;
492 * !!!
493 * We may have wrapped if wrapscan was set, and we may have returned
494 * to the position where the cursor started. Historic vi didn't cope
495 * with this well. Yank wouldn't beep, but the first put after the
496 * yank would move the cursor right one column (without adding any
497 * text) and the second would put a copy of the current line. The
498 * change and delete commands would beep, but would leave the cursor
499 * on the colon command line. I believe that there are macros that
500 * depend on delete, at least, failing. For now, commands that use
501 * search as a motion component fail when the search returns to the
502 * original cursor position.
504 if (vp->m_start.lno == vp->m_stop.lno &&
505 vp->m_start.cno == vp->m_stop.cno) {
506 msgq(sp, M_BERR, "190|Search wrapped to original position");
507 return (1);
511 * !!!
512 * Searches become line mode operations if there was a delta specified
513 * to the search pattern.
515 if (isdelta)
516 F_SET(vp, VM_LMODE);
519 * If the motion is in the reverse direction, switch the start and
520 * stop MARK's so that it's in a forward direction. (There's no
521 * reason for this other than to make the tests below easier. The
522 * code in vi.c:vi() would have done the switch.) Both forward
523 * and backward motions can happen for any kind of search command
524 * because of the wrapscan option.
526 if (vp->m_start.lno > vp->m_stop.lno ||
527 vp->m_start.lno == vp->m_stop.lno &&
528 vp->m_start.cno > vp->m_stop.cno) {
529 m = vp->m_start;
530 vp->m_start = vp->m_stop;
531 vp->m_stop = m;
532 dir = BACKWARD;
533 } else
534 dir = FORWARD;
537 * BACKWARD:
538 * Delete and yank commands move to the end of the range.
539 * Ignore others.
541 * FORWARD:
542 * Delete and yank commands don't move. Ignore others.
544 vp->m_final = vp->m_start;
547 * !!!
548 * Delta'd searches don't correct based on column positions.
550 if (isdelta)
551 return (0);
554 * !!!
555 * Backward searches starting at column 0, and forward searches ending
556 * at column 0 are corrected to the last column of the previous line.
557 * Otherwise, adjust the starting/ending point to the character before
558 * the current one (this is safe because we know the search had to move
559 * to succeed).
561 * Searches become line mode operations if they start at the first
562 * nonblank and end at column 0 of another line.
564 if (vp->m_start.lno < vp->m_stop.lno && vp->m_stop.cno == 0) {
565 if (db_get(sp, --vp->m_stop.lno, DBG_FATAL, NULL, &len))
566 return (1);
567 vp->m_stop.cno = len ? len - 1 : 0;
568 len = 0;
569 if (nonblank(sp, vp->m_start.lno, &len))
570 return (1);
571 if (vp->m_start.cno <= len)
572 F_SET(vp, VM_LMODE);
573 } else
574 --vp->m_stop.cno;
576 return (0);