fix use of RE_W{START,STOP} in wide char version
[nvi.git] / vi / v_search.c
blob2a02e6eecb23ee0b4c5977da09a8e3085d2b6243
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.27 2000/09/02 13:14:16 skimo Exp $ (Berkeley) $Date: 2000/09/02 13:14:16 $";
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 = { "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 static CHAR_T plus[] = { '+', 0 };
83 CHAR_T *w;
84 size_t wlen;
87 * !!!
88 * If using the search command as a motion, any addressing components
89 * are lost, i.e. y/ptrn/+2, when repeated, is the same as y/ptrn/.
91 if (F_ISSET(vp, VC_ISDOT))
92 return (v_search(sp, vp,
93 NULL, 0, SEARCH_PARSE | SEARCH_MSG | SEARCH_SET, dir));
95 /* Get the search pattern. */
96 if (v_tcmd(sp, vp, dir == BACKWARD ? CH_BSEARCH : CH_FSEARCH,
97 TXT_BS | TXT_CR | TXT_ESCAPE | TXT_PROMPT |
98 (O_ISSET(sp, O_SEARCHINCR) ? TXT_SEARCHINCR : 0)))
99 return (1);
101 tp = sp->tiq.cqh_first;
103 /* If the user backspaced over the prompt, do nothing. */
104 if (tp->term == TERM_BS)
105 return (1);
108 * If the user was doing an incremental search, then we've already
109 * updated the cursor and moved to the right location. Return the
110 * correct values, we're done.
112 if (tp->term == TERM_SEARCH) {
113 vp->m_stop.lno = sp->lno;
114 vp->m_stop.cno = sp->cno;
115 if (ISMOTION(vp))
116 return (v_correct(sp, vp, 0));
117 vp->m_final = vp->m_stop;
118 return (0);
122 * If the user entered <escape> or <carriage-return>, the length is
123 * 1 and the right thing will happen, i.e. the prompt will be used
124 * as a command character.
126 * Build a fake ex command structure.
128 wp = sp->wp;
129 wp->excmd.cp = tp->lb;
130 wp->excmd.clen = tp->len;
131 F_INIT(&wp->excmd, E_VISEARCH);
134 * XXX
135 * Warn if the search wraps. This is a pretty special case, but it's
136 * nice feature that wasn't in the original implementations of ex/vi.
137 * (It was added at some point to System V's version.) This message
138 * is only displayed if there are no keys in the queue. The problem is
139 * the command is going to succeed, and the message is informational,
140 * not an error. If a macro displays it repeatedly, e.g., the pattern
141 * only occurs once in the file and wrapscan is set, you lose big. For
142 * example, if the macro does something like:
144 * :map K /pattern/^MjK
146 * Each search will display the message, but the following "/pattern/"
147 * will immediately overwrite it, with strange results. The System V
148 * vi displays the "wrapped" message multiple times, but because it's
149 * overwritten each time, it's not as noticeable. As we don't discard
150 * messages, it's a real problem for us.
152 if (!KEYS_WAITING(sp))
153 F_SET(&wp->excmd, E_SEARCH_WMSG);
155 /* Save the current line/column. */
156 s_lno = sp->lno;
157 s_cno = sp->cno;
160 * !!!
161 * Historically, vi / and ? commands were full-blown ex addresses,
162 * including ';' delimiters, trailing <blank>'s, multiple search
163 * strings (separated by semi-colons) and, finally, full-blown z
164 * commands after the / and ? search strings. (If the search was
165 * being used as a motion, the trailing z command was ignored.
166 * Also, we do some argument checking on the z command, to be sure
167 * that it's not some other random command.) For multiple search
168 * strings, leading <blank>'s at the second and subsequent strings
169 * were eaten as well. This has some (unintended?) side-effects:
170 * the command /ptrn/;3 is legal and results in moving to line 3.
171 * I suppose you could use it to optionally move to line 3...
173 * !!!
174 * Historically, if any part of the search command failed, the cursor
175 * remained unmodified (even if ; was used). We have to play games
176 * because the underlying ex parser thinks we're modifying the cursor
177 * as we go, but I think we're compatible with historic practice.
179 * !!!
180 * Historically, the command "/STRING/; " failed, apparently it
181 * confused the parser. We're not that compatible.
183 cmdp = &wp->excmd;
184 if (ex_range(sp, cmdp, &err))
185 return (1);
188 * Remember where any remaining command information is, and clean
189 * up the fake ex command.
191 cmd = cmdp->cp;
192 len = cmdp->clen;
193 wp->excmd.clen = 0;
195 if (err)
196 goto err2;
198 /* Copy out the new cursor position and make sure it's okay. */
199 switch (cmdp->addrcnt) {
200 case 1:
201 vp->m_stop = cmdp->addr1;
202 break;
203 case 2:
204 vp->m_stop = cmdp->addr2;
205 break;
207 if (!db_exist(sp, vp->m_stop.lno)) {
208 ex_badaddr(sp, &fake,
209 vp->m_stop.lno == 0 ? A_ZERO : A_EOF, NUM_OK);
210 goto err2;
214 * !!!
215 * Historic practice is that a trailing 'z' was ignored if it was a
216 * motion command. Should probably be an error, but not worth the
217 * effort.
219 if (ISMOTION(vp))
220 return (v_correct(sp, vp, F_ISSET(cmdp, E_DELTA)));
223 * !!!
224 * Historically, if it wasn't a motion command, a delta in the search
225 * pattern turns it into a first nonblank movement.
227 nb = F_ISSET(cmdp, E_DELTA);
229 /* Check for the 'z' command. */
230 if (len != 0) {
231 if (*cmd != 'z')
232 goto err1;
234 /* No blanks, just like the z command. */
235 for (t = cmd + 1, tlen = len - 1; tlen > 0; ++t, --tlen)
236 if (!isdigit(*t))
237 break;
238 if (tlen &&
239 (*t == '-' || *t == '.' || *t == '+' || *t == '^')) {
240 ++t;
241 --tlen;
242 type = 1;
243 } else
244 type = 0;
245 if (tlen)
246 goto err1;
248 /* The z command will do the nonblank for us. */
249 nb = 0;
251 /* Default to z+. */
252 if (!type &&
253 v_event_push(sp, NULL, plus, 1, CH_NOMAP | CH_QUOTED))
254 return (1);
256 /* Push the user's command. */
257 if (v_event_push(sp, NULL, cmd, len, CH_NOMAP | CH_QUOTED))
258 return (1);
260 /* Push line number so get correct z display. */
261 tlen = snprintf(buf,
262 sizeof(buf), "%lu", (u_long)vp->m_stop.lno);
263 CHAR2INT(sp, buf, tlen, w, wlen);
264 if (v_event_push(sp, NULL, w, wlen, CH_NOMAP | CH_QUOTED))
265 return (1);
267 /* Don't refresh until after 'z' happens. */
268 F_SET(VIP(sp), VIP_S_REFRESH);
271 /* Non-motion commands move to the end of the range. */
272 vp->m_final = vp->m_stop;
273 if (nb) {
274 F_CLR(vp, VM_RCM_MASK);
275 F_SET(vp, VM_RCM_SETFNB);
277 return (0);
279 err1: msgq(sp, M_ERR,
280 "188|Characters after search string, line offset and/or z command");
281 err2: vp->m_final.lno = s_lno;
282 vp->m_final.cno = s_cno;
283 return (1);
287 * v_searchN -- N
288 * Reverse last search.
290 * PUBLIC: int v_searchN __P((SCR *, VICMD *));
293 v_searchN(sp, vp)
294 SCR *sp;
295 VICMD *vp;
297 dir_t dir;
299 switch (sp->searchdir) {
300 case BACKWARD:
301 dir = FORWARD;
302 break;
303 case FORWARD:
304 dir = BACKWARD;
305 break;
306 default:
307 dir = sp->searchdir;
308 break;
310 return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, dir));
314 * v_searchn -- n
315 * Repeat last search.
317 * PUBLIC: int v_searchn __P((SCR *, VICMD *));
320 v_searchn(sp, vp)
321 SCR *sp;
322 VICMD *vp;
324 return (v_search(sp, vp, NULL, 0, SEARCH_PARSE, sp->searchdir));
328 * v_searchw -- [count]^A
329 * Search for the word under the cursor.
331 * PUBLIC: int v_searchw __P((SCR *, VICMD *));
334 v_searchw(sp, vp)
335 SCR *sp;
336 VICMD *vp;
338 size_t blen, len;
339 int rval;
340 CHAR_T *bp, *p;
342 len = VIP(sp)->klen + RE_WSTART_LEN + RE_WSTOP_LEN;
343 GET_SPACE_RETW(sp, bp, blen, len);
344 MEMCPY(bp, RE_WSTART, RE_WSTART_LEN);
345 p = bp + sizeof(RE_WSTART)/sizeof(CHAR_T) - 1;
346 MEMCPY(p, VIP(sp)->keyw, VIP(sp)->klen);
347 p += VIP(sp)->klen;
348 MEMCPY(bp, RE_WSTOP, RE_WSTOP_LEN);
350 rval = v_search(sp, vp, bp, len, SEARCH_SET, FORWARD);
352 FREE_SPACEW(sp, bp, blen);
353 return (rval);
357 * v_esearch -- <dialog box>
358 * Search command from the screen.
360 * PUBLIC: int v_esearch __P((SCR *, VICMD *));
363 v_esearch(sp, vp)
364 SCR *sp;
365 VICMD *vp;
367 MARK m;
368 int flags;
370 m.lno = sp->lno;
371 m.cno = sp->cno;
373 LF_INIT(SEARCH_NOOPT);
374 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_EXT))
375 LF_SET(SEARCH_EXTEND);
376 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_IC))
377 LF_SET(SEARCH_IC);
378 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_ICL))
379 LF_SET(SEARCH_ICL);
380 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_INCR))
381 LF_SET(SEARCH_INCR);
382 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_LIT))
383 LF_SET(SEARCH_LITERAL);
384 if (FL_ISSET(vp->ev.e_flags, VI_SEARCH_WR))
385 LF_SET(SEARCH_WRAP);
386 return (v_search(sp, vp, vp->ev.e_csp, vp->ev.e_len, flags,
387 FL_ISSET(vp->ev.e_flags, VI_SEARCH_REV) ? BACKWARD : FORWARD));
391 * v_search --
392 * The search commands.
394 static int
395 v_search(sp, vp, ptrn, plen, flags, dir)
396 SCR *sp;
397 VICMD *vp;
398 u_int flags;
399 CHAR_T *ptrn;
400 size_t plen;
401 dir_t dir;
403 /* Display messages. */
404 LF_SET(SEARCH_MSG);
406 /* If it's a motion search, offset past end-of-line is okay. */
407 if (ISMOTION(vp))
408 LF_SET(SEARCH_EOL);
411 * XXX
412 * Warn if the search wraps. See the comment above, in v_exaddr().
414 if (!KEYS_WAITING(sp))
415 LF_SET(SEARCH_WMSG);
417 switch (dir) {
418 case BACKWARD:
419 if (b_search(sp,
420 &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
421 return (1);
422 break;
423 case FORWARD:
424 if (f_search(sp,
425 &vp->m_start, &vp->m_stop, ptrn, plen, NULL, flags))
426 return (1);
427 break;
428 case NOTSET:
429 msgq(sp, M_ERR, "189|No previous search pattern");
430 return (1);
431 default:
432 abort();
435 /* Correct motion commands, otherwise, simply move to the location. */
436 if (ISMOTION(vp)) {
437 if (v_correct(sp, vp, 0))
438 return(1);
439 } else
440 vp->m_final = vp->m_stop;
441 return (0);
445 * v_correct --
446 * Handle command with a search as the motion.
448 * !!!
449 * Historically, commands didn't affect the line searched to/from if the
450 * motion command was a search and the final position was the start/end
451 * of the line. There were some special cases and vi was not consistent;
452 * it was fairly easy to confuse it. For example, given the two lines:
454 * abcdefghi
455 * ABCDEFGHI
457 * placing the cursor on the 'A' and doing y?$ would so confuse it that 'h'
458 * 'k' and put would no longer work correctly. In any case, we try to do
459 * the right thing, but it's not going to exactly match historic practice.
461 * PUBLIC: int v_correct __P((SCR *, VICMD *, int));
464 v_correct(sp, vp, isdelta)
465 SCR *sp;
466 VICMD *vp;
467 int isdelta;
469 dir_t dir;
470 MARK m;
471 size_t len;
474 * !!!
475 * We may have wrapped if wrapscan was set, and we may have returned
476 * to the position where the cursor started. Historic vi didn't cope
477 * with this well. Yank wouldn't beep, but the first put after the
478 * yank would move the cursor right one column (without adding any
479 * text) and the second would put a copy of the current line. The
480 * change and delete commands would beep, but would leave the cursor
481 * on the colon command line. I believe that there are macros that
482 * depend on delete, at least, failing. For now, commands that use
483 * search as a motion component fail when the search returns to the
484 * original cursor position.
486 if (vp->m_start.lno == vp->m_stop.lno &&
487 vp->m_start.cno == vp->m_stop.cno) {
488 msgq(sp, M_BERR, "190|Search wrapped to original position");
489 return (1);
493 * !!!
494 * Searches become line mode operations if there was a delta specified
495 * to the search pattern.
497 if (isdelta)
498 F_SET(vp, VM_LMODE);
501 * If the motion is in the reverse direction, switch the start and
502 * stop MARK's so that it's in a forward direction. (There's no
503 * reason for this other than to make the tests below easier. The
504 * code in vi.c:vi() would have done the switch.) Both forward
505 * and backward motions can happen for any kind of search command
506 * because of the wrapscan option.
508 if (vp->m_start.lno > vp->m_stop.lno ||
509 vp->m_start.lno == vp->m_stop.lno &&
510 vp->m_start.cno > vp->m_stop.cno) {
511 m = vp->m_start;
512 vp->m_start = vp->m_stop;
513 vp->m_stop = m;
514 dir = BACKWARD;
515 } else
516 dir = FORWARD;
519 * BACKWARD:
520 * Delete and yank commands move to the end of the range.
521 * Ignore others.
523 * FORWARD:
524 * Delete and yank commands don't move. Ignore others.
526 vp->m_final = vp->m_start;
529 * !!!
530 * Delta'd searches don't correct based on column positions.
532 if (isdelta)
533 return (0);
536 * !!!
537 * Backward searches starting at column 0, and forward searches ending
538 * at column 0 are corrected to the last column of the previous line.
539 * Otherwise, adjust the starting/ending point to the character before
540 * the current one (this is safe because we know the search had to move
541 * to succeed).
543 * Searches become line mode operations if they start at the first
544 * nonblank and end at column 0 of another line.
546 if (vp->m_start.lno < vp->m_stop.lno && vp->m_stop.cno == 0) {
547 if (db_get(sp, --vp->m_stop.lno, DBG_FATAL, NULL, &len))
548 return (1);
549 vp->m_stop.cno = len ? len - 1 : 0;
550 len = 0;
551 if (nonblank(sp, vp->m_start.lno, &len))
552 return (1);
553 if (vp->m_start.cno <= len)
554 F_SET(vp, VM_LMODE);
555 } else
556 --vp->m_stop.cno;
558 return (0);