blob: af3215b3186b1773877f559b733429eaf8b8b528 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * search.c: code for normal mode searching commands
11 */
12
13#include "vim.h"
14
15static void save_re_pat __ARGS((int idx, char_u *pat, int magic));
16#ifdef FEAT_EVAL
17static int first_submatch __ARGS((regmmatch_T *rp));
18#endif
19static int check_prevcol __ARGS((char_u *linep, int col, int ch, int *prevcol));
20static int inmacro __ARGS((char_u *, char_u *));
21static int check_linecomment __ARGS((char_u *line));
22static int cls __ARGS((void));
23static int skip_chars __ARGS((int, int));
24#ifdef FEAT_TEXTOBJ
25static void back_in_line __ARGS((void));
26static void find_first_blank __ARGS((pos_T *));
27static void findsent_forward __ARGS((long count, int at_start_sent));
28#endif
29#ifdef FEAT_FIND_ID
30static void show_pat_in_path __ARGS((char_u *, int,
31 int, int, FILE *, linenr_T *, long));
32#endif
33#ifdef FEAT_VIMINFO
34static void wvsp_one __ARGS((FILE *fp, int idx, char *s, int sc));
35#endif
36
37static char_u *top_bot_msg = (char_u *)N_("search hit TOP, continuing at BOTTOM");
38static char_u *bot_top_msg = (char_u *)N_("search hit BOTTOM, continuing at TOP");
39
40/*
41 * This file contains various searching-related routines. These fall into
42 * three groups:
43 * 1. string searches (for /, ?, n, and N)
44 * 2. character searches within a single line (for f, F, t, T, etc)
45 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
46 */
47
48/*
49 * String searches
50 *
51 * The string search functions are divided into two levels:
52 * lowest: searchit(); uses an pos_T for starting position and found match.
53 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
54 *
55 * The last search pattern is remembered for repeating the same search.
56 * This pattern is shared between the :g, :s, ? and / commands.
57 * This is in search_regcomp().
58 *
59 * The actual string matching is done using a heavily modified version of
60 * Henry Spencer's regular expression library. See regexp.c.
61 */
62
63/* The offset for a search command is store in a soff struct */
64/* Note: only spats[0].off is really used */
65struct soffset
66{
67 int dir; /* search direction */
68 int line; /* search has line offset */
69 int end; /* search set cursor at end */
70 long off; /* line or char offset */
71};
72
73/* A search pattern and its attributes are stored in a spat struct */
74struct spat
75{
76 char_u *pat; /* the pattern (in allocated memory) or NULL */
77 int magic; /* magicness of the pattern */
78 int no_scs; /* no smarcase for this pattern */
79 struct soffset off;
80};
81
82/*
83 * Two search patterns are remembered: One for the :substitute command and
84 * one for other searches. last_idx points to the one that was used the last
85 * time.
86 */
87static struct spat spats[2] =
88{
89 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
90 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
91};
92
93static int last_idx = 0; /* index in spats[] for RE_LAST */
94
95#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
96/* copy of spats[], for keeping the search patterns while executing autocmds */
97static struct spat saved_spats[2];
98static int saved_last_idx = 0;
99# ifdef FEAT_SEARCH_EXTRA
100static int saved_no_hlsearch = 0;
101# endif
102#endif
103
104static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
105#ifdef FEAT_RIGHTLEFT
106static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
107static char_u *reverse_text __ARGS((char_u *s));
108#endif
109
110#ifdef FEAT_FIND_ID
111/*
112 * Type used by find_pattern_in_path() to remember which included files have
113 * been searched already.
114 */
115typedef struct SearchedFile
116{
117 FILE *fp; /* File pointer */
118 char_u *name; /* Full name of file */
119 linenr_T lnum; /* Line we were up to in file */
120 int matched; /* Found a match in this file */
121} SearchedFile;
122#endif
123
124/*
125 * translate search pattern for vim_regcomp()
126 *
127 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
128 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
129 * pat_save == RE_BOTH: save pat in both patterns (:global command)
130 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
131 * pat_use == RE_SUBST: use previous sustitute pattern if "pat" is NULL
132 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
133 * options & SEARCH_HIS: put search string in history
134 * options & SEARCH_KEEP: keep previous search pattern
135 *
136 * returns FAIL if failed, OK otherwise.
137 */
138 int
139search_regcomp(pat, pat_save, pat_use, options, regmatch)
140 char_u *pat;
141 int pat_save;
142 int pat_use;
143 int options;
144 regmmatch_T *regmatch; /* return: pattern and ignore-case flag */
145{
146 int magic;
147 int i;
148
149 rc_did_emsg = FALSE;
150 magic = p_magic;
151
152 /*
153 * If no pattern given, use a previously defined pattern.
154 */
155 if (pat == NULL || *pat == NUL)
156 {
157 if (pat_use == RE_LAST)
158 i = last_idx;
159 else
160 i = pat_use;
161 if (spats[i].pat == NULL) /* pattern was never defined */
162 {
163 if (pat_use == RE_SUBST)
164 EMSG(_(e_nopresub));
165 else
166 EMSG(_(e_noprevre));
167 rc_did_emsg = TRUE;
168 return FAIL;
169 }
170 pat = spats[i].pat;
171 magic = spats[i].magic;
172 no_smartcase = spats[i].no_scs;
173 }
174#ifdef FEAT_CMDHIST
175 else if (options & SEARCH_HIS) /* put new pattern in history */
176 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
177#endif
178
179#ifdef FEAT_RIGHTLEFT
180 if (mr_pattern_alloced)
181 {
182 vim_free(mr_pattern);
183 mr_pattern_alloced = FALSE;
184 }
185
186 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
187 {
188 char_u *rev_pattern;
189
190 rev_pattern = reverse_text(pat);
191 if (rev_pattern == NULL)
192 mr_pattern = pat; /* out of memory, keep normal pattern. */
193 else
194 {
195 mr_pattern = rev_pattern;
196 mr_pattern_alloced = TRUE;
197 }
198 }
199 else
200#endif
201 mr_pattern = pat;
202
203 /*
204 * Save the currently used pattern in the appropriate place,
205 * unless the pattern should not be remembered.
206 */
207 if (!(options & SEARCH_KEEP))
208 {
209 /* search or global command */
210 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
211 save_re_pat(RE_SEARCH, pat, magic);
212 /* substitute or global command */
213 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
214 save_re_pat(RE_SUBST, pat, magic);
215 }
216
217 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000218 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
220 if (regmatch->regprog == NULL)
221 return FAIL;
222 return OK;
223}
224
225/*
226 * Get search pattern used by search_regcomp().
227 */
228 char_u *
229get_search_pat()
230{
231 return mr_pattern;
232}
233
234#ifdef FEAT_RIGHTLEFT
235/*
236 * Reverse text into allocated memory.
237 * Returns the allocated string, NULL when out of memory.
238 */
239 static char_u *
240reverse_text(s)
241 char_u *s;
242{
243 unsigned len;
244 unsigned s_i, rev_i;
245 char_u *rev;
246
247 /*
248 * Reverse the pattern.
249 */
250 len = (unsigned)STRLEN(s);
251 rev = alloc(len + 1);
252 if (rev != NULL)
253 {
254 rev_i = len;
255 for (s_i = 0; s_i < len; ++s_i)
256 {
257# ifdef FEAT_MBYTE
258 if (has_mbyte)
259 {
260 int mb_len;
261
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000262 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 rev_i -= mb_len;
264 mch_memmove(rev + rev_i, s + s_i, mb_len);
265 s_i += mb_len - 1;
266 }
267 else
268# endif
269 rev[--rev_i] = s[s_i];
270
271 }
272 rev[len] = NUL;
273 }
274 return rev;
275}
276#endif
277
278 static void
279save_re_pat(idx, pat, magic)
280 int idx;
281 char_u *pat;
282 int magic;
283{
284 if (spats[idx].pat != pat)
285 {
286 vim_free(spats[idx].pat);
287 spats[idx].pat = vim_strsave(pat);
288 spats[idx].magic = magic;
289 spats[idx].no_scs = no_smartcase;
290 last_idx = idx;
291#ifdef FEAT_SEARCH_EXTRA
292 /* If 'hlsearch' set and search pat changed: need redraw. */
293 if (p_hls)
294 redraw_all_later(NOT_VALID);
295 no_hlsearch = FALSE;
296#endif
297 }
298}
299
300#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
301/*
302 * Save the search patterns, so they can be restored later.
303 * Used before/after executing autocommands and user functions.
304 */
305static int save_level = 0;
306
307 void
308save_search_patterns()
309{
310 if (save_level++ == 0)
311 {
312 saved_spats[0] = spats[0];
313 if (spats[0].pat != NULL)
314 saved_spats[0].pat = vim_strsave(spats[0].pat);
315 saved_spats[1] = spats[1];
316 if (spats[1].pat != NULL)
317 saved_spats[1].pat = vim_strsave(spats[1].pat);
318 saved_last_idx = last_idx;
319# ifdef FEAT_SEARCH_EXTRA
320 saved_no_hlsearch = no_hlsearch;
321# endif
322 }
323}
324
325 void
326restore_search_patterns()
327{
328 if (--save_level == 0)
329 {
330 vim_free(spats[0].pat);
331 spats[0] = saved_spats[0];
332 vim_free(spats[1].pat);
333 spats[1] = saved_spats[1];
334 last_idx = saved_last_idx;
335# ifdef FEAT_SEARCH_EXTRA
336 no_hlsearch = saved_no_hlsearch;
337# endif
338 }
339}
340#endif
341
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000342#if defined(EXITFREE) || defined(PROTO)
343 void
344free_search_patterns()
345{
346 vim_free(spats[0].pat);
347 vim_free(spats[1].pat);
348}
349#endif
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351/*
352 * Return TRUE when case should be ignored for search pattern "pat".
353 * Uses the 'ignorecase' and 'smartcase' options.
354 */
355 int
356ignorecase(pat)
357 char_u *pat;
358{
359 char_u *p;
360 int ic;
361
362 ic = p_ic;
363 if (ic && !no_smartcase && p_scs
364#ifdef FEAT_INS_EXPAND
365 && !(ctrl_x_mode && curbuf->b_p_inf)
366#endif
367 )
368 {
369 /* don't ignore case if pattern has uppercase */
370 for (p = pat; *p; )
371 {
372#ifdef FEAT_MBYTE
373 int l;
374
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000375 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 {
377 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
378 {
379 ic = FALSE;
380 break;
381 }
382 p += l;
383 }
384 else
385#endif
386 if (*p == '\\' && p[1] != NUL) /* skip "\S" et al. */
387 p += 2;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000388 else if (isupper(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
390 ic = FALSE;
391 break;
392 }
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000393 else
394 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 }
396 }
397 no_smartcase = FALSE;
398
399 return ic;
400}
401
402 char_u *
403last_search_pat()
404{
405 return spats[last_idx].pat;
406}
407
408/*
409 * Reset search direction to forward. For "gd" and "gD" commands.
410 */
411 void
412reset_search_dir()
413{
414 spats[0].off.dir = '/';
415}
416
417#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
418/*
419 * Set the last search pattern. For ":let @/ =" and viminfo.
420 * Also set the saved search pattern, so that this works in an autocommand.
421 */
422 void
423set_last_search_pat(s, idx, magic, setlast)
424 char_u *s;
425 int idx;
426 int magic;
427 int setlast;
428{
429 vim_free(spats[idx].pat);
430 /* An empty string means that nothing should be matched. */
431 if (*s == NUL)
432 spats[idx].pat = NULL;
433 else
434 spats[idx].pat = vim_strsave(s);
435 spats[idx].magic = magic;
436 spats[idx].no_scs = FALSE;
437 spats[idx].off.dir = '/';
438 spats[idx].off.line = FALSE;
439 spats[idx].off.end = FALSE;
440 spats[idx].off.off = 0;
441 if (setlast)
442 last_idx = idx;
443 if (save_level)
444 {
445 vim_free(saved_spats[idx].pat);
446 saved_spats[idx] = spats[0];
447 if (spats[idx].pat == NULL)
448 saved_spats[idx].pat = NULL;
449 else
450 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
451 saved_last_idx = last_idx;
452 }
453# ifdef FEAT_SEARCH_EXTRA
454 /* If 'hlsearch' set and search pat changed: need redraw. */
455 if (p_hls && idx == last_idx && !no_hlsearch)
456 redraw_all_later(NOT_VALID);
457# endif
458}
459#endif
460
461#ifdef FEAT_SEARCH_EXTRA
462/*
463 * Get a regexp program for the last used search pattern.
464 * This is used for highlighting all matches in a window.
465 * Values returned in regmatch->regprog and regmatch->rmm_ic.
466 */
467 void
468last_pat_prog(regmatch)
469 regmmatch_T *regmatch;
470{
471 if (spats[last_idx].pat == NULL)
472 {
473 regmatch->regprog = NULL;
474 return;
475 }
476 ++emsg_off; /* So it doesn't beep if bad expr */
477 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
478 --emsg_off;
479}
480#endif
481
482/*
483 * lowest level search function.
484 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
485 * Start at position 'pos' and return the found position in 'pos'.
486 *
487 * if (options & SEARCH_MSG) == 0 don't give any messages
488 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
489 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
490 * if (options & SEARCH_HIS) put search pattern in history
491 * if (options & SEARCH_END) return position at end of match
492 * if (options & SEARCH_START) accept match at pos itself
493 * if (options & SEARCH_KEEP) keep previous search pattern
494 * if (options & SEARCH_FOLD) match only once in a closed fold
495 * if (options & SEARCH_PEEK) check for typed char, cancel search
496 *
497 * Return FAIL (zero) for failure, non-zero for success.
498 * When FEAT_EVAL is defined, returns the index of the first matching
499 * subpattern plus one; one if there was none.
500 */
501 int
502searchit(win, buf, pos, dir, pat, count, options, pat_use)
503 win_T *win; /* window to search in; can be NULL for a
504 buffer without a window! */
505 buf_T *buf;
506 pos_T *pos;
507 int dir;
508 char_u *pat;
509 long count;
510 int options;
511 int pat_use;
512{
513 int found;
514 linenr_T lnum; /* no init to shut up Apollo cc */
515 regmmatch_T regmatch;
516 char_u *ptr;
517 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000519 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 int loop;
521 pos_T start_pos;
522 int at_first_line;
523 int extra_col;
524 int match_ok;
525 long nmatched;
526 int submatch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527#ifdef FEAT_SEARCH_EXTRA
528 int break_loop = FALSE;
529#else
530# define break_loop FALSE
531#endif
532
533 if (search_regcomp(pat, RE_SEARCH, pat_use,
534 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
535 {
536 if ((options & SEARCH_MSG) && !rc_did_emsg)
537 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
538 return FAIL;
539 }
540
541 if (options & SEARCH_START)
542 extra_col = 0;
543#ifdef FEAT_MBYTE
544 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
545 else if (has_mbyte && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
546 && pos->col < MAXCOL - 2)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000547 {
548 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
549 if (*ptr == NUL)
550 extra_col = 1;
551 else
552 extra_col = (*mb_ptr2len)(ptr);
553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#endif
555 else
556 extra_col = 1;
557
558/*
559 * find the string
560 */
561 called_emsg = FALSE;
562 do /* loop for count */
563 {
564 start_pos = *pos; /* remember start pos for detecting no match */
565 found = 0; /* default: not found */
566 at_first_line = TRUE; /* default: start in first line */
567 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
568 {
569 pos->lnum = 1;
570 pos->col = 0;
571 at_first_line = FALSE; /* not in first line now */
572 }
573
574 /*
575 * Start searching in current line, unless searching backwards and
576 * we're in column 0.
577 */
578 if (dir == BACKWARD && start_pos.col == 0)
579 {
580 lnum = pos->lnum - 1;
581 at_first_line = FALSE;
582 }
583 else
584 lnum = pos->lnum;
585
586 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
587 {
588 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
589 lnum += dir, at_first_line = FALSE)
590 {
591 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000592 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 nmatched = vim_regexec_multi(&regmatch, win, buf,
595 lnum, (colnr_T)0);
596 /* Abort searching on an error (e.g., out of stack). */
597 if (called_emsg)
598 break;
599 if (nmatched > 0)
600 {
601 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000602 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 endpos = regmatch.endpos[0];
604# ifdef FEAT_EVAL
605 submatch = first_submatch(&regmatch);
606# endif
Bram Moolenaar677ee682005-01-27 14:41:15 +0000607 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
609 /*
610 * Forward search in the first line: match should be after
611 * the start position. If not, continue at the end of the
612 * match (this is vi compatible) or on the next char.
613 */
614 if (dir == FORWARD && at_first_line)
615 {
616 match_ok = TRUE;
617 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000618 * When the match starts in a next line it's certainly
619 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 * When match lands on a NUL the cursor will be put
621 * one back afterwards, compare with that position,
622 * otherwise "/$" will get stuck on end of line.
623 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000624 while (matchpos.lnum == 0
625 && ((options & SEARCH_END)
626 ? (nmatched == 1
627 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000629 : ((int)matchpos.col
630 - (ptr[matchpos.col] == NUL)
631 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
633 /*
634 * If vi-compatible searching, continue at the end
635 * of the match, otherwise continue one position
636 * forward.
637 */
638 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
639 {
640 if (nmatched > 1)
641 {
642 /* end is in next line, thus no match in
643 * this line */
644 match_ok = FALSE;
645 break;
646 }
647 matchcol = endpos.col;
648 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000649 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 && ptr[matchcol] != NUL)
651 {
652#ifdef FEAT_MBYTE
653 if (has_mbyte)
654 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000655 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 else
657#endif
658 ++matchcol;
659 }
660 }
661 else
662 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000663 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (ptr[matchcol] != NUL)
665 {
666#ifdef FEAT_MBYTE
667 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000668 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 + matchcol);
670 else
671#endif
672 ++matchcol;
673 }
674 }
675 if (ptr[matchcol] == NUL
676 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000677 win, buf, lnum + matchpos.lnum,
678 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 match_ok = FALSE;
681 break;
682 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000683 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 endpos = regmatch.endpos[0];
685# ifdef FEAT_EVAL
686 submatch = first_submatch(&regmatch);
687# endif
688
689 /* Need to get the line pointer again, a
690 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000691 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 }
693 if (!match_ok)
694 continue;
695 }
696 if (dir == BACKWARD)
697 {
698 /*
699 * Now, if there are multiple matches on this line,
700 * we have to get the last one. Or the last one before
701 * the cursor, if we're on that line.
702 * When putting the new cursor at the end, compare
703 * relative to the end of the match.
704 */
705 match_ok = FALSE;
706 for (;;)
707 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000708 /* Remember a position that is before the start
709 * position, we use it if it's the last match in
710 * the line. Always accept a position after
711 * wrapping around. */
712 if (loop
713 || ((options & SEARCH_END)
714 ? (lnum + regmatch.endpos[0].lnum
715 < start_pos.lnum
716 || (lnum + regmatch.endpos[0].lnum
717 == start_pos.lnum
718 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000720 <= (int)start_pos.col))
721 : (lnum + regmatch.startpos[0].lnum
722 < start_pos.lnum
723 || (lnum + regmatch.startpos[0].lnum
724 == start_pos.lnum
725 && (int)regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000727 <= (int)start_pos.col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000730 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 endpos = regmatch.endpos[0];
732# ifdef FEAT_EVAL
733 submatch = first_submatch(&regmatch);
734# endif
735 }
736 else
737 break;
738
739 /*
740 * We found a valid match, now check if there is
741 * another one after it.
742 * If vi-compatible searching, continue at the end
743 * of the match, otherwise continue one position
744 * forward.
745 */
746 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
747 {
748 if (nmatched > 1)
749 break;
750 matchcol = endpos.col;
751 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000752 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 && ptr[matchcol] != NUL)
754 {
755#ifdef FEAT_MBYTE
756 if (has_mbyte)
757 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000758 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 else
760#endif
761 ++matchcol;
762 }
763 }
764 else
765 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000766 /* Stop when the match is in a next line. */
767 if (matchpos.lnum > 0)
768 break;
769 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (ptr[matchcol] != NUL)
771 {
772#ifdef FEAT_MBYTE
773 if (has_mbyte)
774 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000775 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 else
777#endif
778 ++matchcol;
779 }
780 }
781 if (ptr[matchcol] == NUL
782 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000783 win, buf, lnum + matchpos.lnum,
784 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 break;
786
787 /* Need to get the line pointer again, a
788 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000789 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791
792 /*
793 * If there is only a match after the cursor, skip
794 * this match.
795 */
796 if (!match_ok)
797 continue;
798 }
799
800 if (options & SEARCH_END && !(options & SEARCH_NOOF))
801 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000802 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 pos->col = endpos.col - 1;
804 }
805 else
806 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000807 pos->lnum = lnum + matchpos.lnum;
808 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
810#ifdef FEAT_VIRTUALEDIT
811 pos->coladd = 0;
812#endif
813 found = 1;
814
815 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000816 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 search_match_endcol = endpos.col;
818 break;
819 }
820 line_breakcheck(); /* stop if ctrl-C typed */
821 if (got_int)
822 break;
823
824#ifdef FEAT_SEARCH_EXTRA
825 /* Cancel searching if a character was typed. Used for
826 * 'incsearch'. Don't check too often, that would slowdown
827 * searching too much. */
828 if ((options & SEARCH_PEEK)
829 && ((lnum - pos->lnum) & 0x3f) == 0
830 && char_avail())
831 {
832 break_loop = TRUE;
833 break;
834 }
835#endif
836
837 if (loop && lnum == start_pos.lnum)
838 break; /* if second loop, stop where started */
839 }
840 at_first_line = FALSE;
841
842 /*
843 * Stop the search if wrapscan isn't set, after an interrupt,
844 * after a match and after looping twice.
845 */
846 if (!p_ws || got_int || called_emsg || break_loop || found || loop)
847 break;
848
849 /*
850 * If 'wrapscan' is set we continue at the other end of the file.
851 * If 'shortmess' does not contain 's', we give a message.
852 * This message is also remembered in keep_msg for when the screen
853 * is redrawn. The keep_msg is cleared whenever another message is
854 * written.
855 */
856 if (dir == BACKWARD) /* start second loop at the other end */
857 {
858 lnum = buf->b_ml.ml_line_count;
859 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
860 give_warning((char_u *)_(top_bot_msg), TRUE);
861 }
862 else
863 {
864 lnum = 1;
865 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
866 give_warning((char_u *)_(bot_top_msg), TRUE);
867 }
868 }
869 if (got_int || called_emsg || break_loop)
870 break;
871 }
872 while (--count > 0 && found); /* stop after count matches or no match */
873
874 vim_free(regmatch.regprog);
875
876 if (!found) /* did not find it */
877 {
878 if (got_int)
879 EMSG(_(e_interr));
880 else if ((options & SEARCH_MSG) == SEARCH_MSG)
881 {
882 if (p_ws)
883 EMSG2(_(e_patnotf2), mr_pattern);
884 else if (lnum == 0)
885 EMSG2(_("E384: search hit TOP without match for: %s"),
886 mr_pattern);
887 else
888 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
889 mr_pattern);
890 }
891 return FAIL;
892 }
893
894 return submatch + 1;
895}
896
897#ifdef FEAT_EVAL
898/*
899 * Return the number of the first subpat that matched.
900 */
901 static int
902first_submatch(rp)
903 regmmatch_T *rp;
904{
905 int submatch;
906
907 for (submatch = 1; ; ++submatch)
908 {
909 if (rp->startpos[submatch].lnum >= 0)
910 break;
911 if (submatch == 9)
912 {
913 submatch = 0;
914 break;
915 }
916 }
917 return submatch;
918}
919#endif
920
921/*
922 * Highest level string search function.
923 * Search for the 'count'th occurence of pattern 'pat' in direction 'dirc'
924 * If 'dirc' is 0: use previous dir.
925 * If 'pat' is NULL or empty : use previous string.
926 * If 'options & SEARCH_REV' : go in reverse of previous dir.
927 * If 'options & SEARCH_ECHO': echo the search command and handle options
928 * If 'options & SEARCH_MSG' : may give error message
929 * If 'options & SEARCH_OPT' : interpret optional flags
930 * If 'options & SEARCH_HIS' : put search pattern in history
931 * If 'options & SEARCH_NOOF': don't add offset to position
932 * If 'options & SEARCH_MARK': set previous context mark
933 * If 'options & SEARCH_KEEP': keep previous search pattern
934 * If 'options & SEARCH_START': accept match at curpos itself
935 * If 'options & SEARCH_PEEK': check for typed char, cancel search
936 *
937 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
938 * makes the movement linewise without moving the match position.
939 *
940 * return 0 for failure, 1 for found, 2 for found and line offset added
941 */
942 int
943do_search(oap, dirc, pat, count, options)
944 oparg_T *oap; /* can be NULL */
945 int dirc; /* '/' or '?' */
946 char_u *pat;
947 long count;
948 int options;
949{
950 pos_T pos; /* position of the last match */
951 char_u *searchstr;
952 struct soffset old_off;
953 int retval; /* Return value */
954 char_u *p;
955 long c;
956 char_u *dircp;
957 char_u *strcopy = NULL;
958 char_u *ps;
959
960 /*
961 * A line offset is not remembered, this is vi compatible.
962 */
963 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
964 {
965 spats[0].off.line = FALSE;
966 spats[0].off.off = 0;
967 }
968
969 /*
970 * Save the values for when (options & SEARCH_KEEP) is used.
971 * (there is no "if ()" around this because gcc wants them initialized)
972 */
973 old_off = spats[0].off;
974
975 pos = curwin->w_cursor; /* start searching at the cursor position */
976
977 /*
978 * Find out the direction of the search.
979 */
980 if (dirc == 0)
981 dirc = spats[0].off.dir;
982 else
983 spats[0].off.dir = dirc;
984 if (options & SEARCH_REV)
985 {
986#ifdef WIN32
987 /* There is a bug in the Visual C++ 2.2 compiler which means that
988 * dirc always ends up being '/' */
989 dirc = (dirc == '/') ? '?' : '/';
990#else
991 if (dirc == '/')
992 dirc = '?';
993 else
994 dirc = '/';
995#endif
996 }
997
998#ifdef FEAT_FOLDING
999 /* If the cursor is in a closed fold, don't find another match in the same
1000 * fold. */
1001 if (dirc == '/')
1002 {
1003 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1004 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1005 }
1006 else
1007 {
1008 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1009 pos.col = 0;
1010 }
1011#endif
1012
1013#ifdef FEAT_SEARCH_EXTRA
1014 /*
1015 * Turn 'hlsearch' highlighting back on.
1016 */
1017 if (no_hlsearch && !(options & SEARCH_KEEP))
1018 {
1019 redraw_all_later(NOT_VALID);
1020 no_hlsearch = FALSE;
1021 }
1022#endif
1023
1024 /*
1025 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1026 */
1027 for (;;)
1028 {
1029 searchstr = pat;
1030 dircp = NULL;
1031 /* use previous pattern */
1032 if (pat == NULL || *pat == NUL || *pat == dirc)
1033 {
1034 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1035 {
1036 EMSG(_(e_noprevre));
1037 retval = 0;
1038 goto end_do_search;
1039 }
1040 /* make search_regcomp() use spats[RE_SEARCH].pat */
1041 searchstr = (char_u *)"";
1042 }
1043
1044 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1045 {
1046 /*
1047 * Find end of regular expression.
1048 * If there is a matching '/' or '?', toss it.
1049 */
1050 ps = strcopy;
1051 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1052 if (strcopy != ps)
1053 {
1054 /* made a copy of "pat" to change "\?" to "?" */
1055 searchcmdlen += STRLEN(pat) - STRLEN(strcopy);
1056 pat = strcopy;
1057 searchstr = strcopy;
1058 }
1059 if (*p == dirc)
1060 {
1061 dircp = p; /* remember where we put the NUL */
1062 *p++ = NUL;
1063 }
1064 spats[0].off.line = FALSE;
1065 spats[0].off.end = FALSE;
1066 spats[0].off.off = 0;
1067 /*
1068 * Check for a line offset or a character offset.
1069 * For get_address (echo off) we don't check for a character
1070 * offset, because it is meaningless and the 's' could be a
1071 * substitute command.
1072 */
1073 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1074 spats[0].off.line = TRUE;
1075 else if ((options & SEARCH_OPT) &&
1076 (*p == 'e' || *p == 's' || *p == 'b'))
1077 {
1078 if (*p == 'e') /* end */
1079 spats[0].off.end = SEARCH_END;
1080 ++p;
1081 }
1082 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1083 {
1084 /* 'nr' or '+nr' or '-nr' */
1085 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1086 spats[0].off.off = atol((char *)p);
1087 else if (*p == '-') /* single '-' */
1088 spats[0].off.off = -1;
1089 else /* single '+' */
1090 spats[0].off.off = 1;
1091 ++p;
1092 while (VIM_ISDIGIT(*p)) /* skip number */
1093 ++p;
1094 }
1095
1096 /* compute length of search command for get_address() */
1097 searchcmdlen += (int)(p - pat);
1098
1099 pat = p; /* put pat after search command */
1100 }
1101
1102 if ((options & SEARCH_ECHO) && messaging()
1103 && !cmd_silent && msg_silent == 0)
1104 {
1105 char_u *msgbuf;
1106 char_u *trunc;
1107
1108 if (*searchstr == NUL)
1109 p = spats[last_idx].pat;
1110 else
1111 p = searchstr;
1112 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1113 if (msgbuf != NULL)
1114 {
1115 msgbuf[0] = dirc;
1116 STRCPY(msgbuf + 1, p);
1117 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1118 {
1119 p = msgbuf + STRLEN(msgbuf);
1120 *p++ = dirc;
1121 if (spats[0].off.end)
1122 *p++ = 'e';
1123 else if (!spats[0].off.line)
1124 *p++ = 's';
1125 if (spats[0].off.off > 0 || spats[0].off.line)
1126 *p++ = '+';
1127 if (spats[0].off.off != 0 || spats[0].off.line)
1128 sprintf((char *)p, "%ld", spats[0].off.off);
1129 else
1130 *p = NUL;
1131 }
1132
1133 msg_start();
1134 trunc = msg_strtrunc(msgbuf);
1135
1136#ifdef FEAT_RIGHTLEFT
1137 /* The search pattern could be shown on the right in rightleft
1138 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1139 * it would be blanked out again very soon. Show it on the
1140 * left, but do reverse the text. */
1141 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1142 {
1143 char_u *r;
1144
1145 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1146 if (r != NULL)
1147 {
1148 vim_free(trunc);
1149 trunc = r;
1150 }
1151 }
1152#endif
1153 if (trunc != NULL)
1154 {
1155 msg_outtrans(trunc);
1156 vim_free(trunc);
1157 }
1158 else
1159 msg_outtrans(msgbuf);
1160 msg_clr_eos();
1161 msg_check();
1162 vim_free(msgbuf);
1163
1164 gotocmdline(FALSE);
1165 out_flush();
1166 msg_nowait = TRUE; /* don't wait for this message */
1167 }
1168 }
1169
1170 /*
1171 * If there is a character offset, subtract it from the current
1172 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001173 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 * This is not done for a line offset, because then we would not be vi
1175 * compatible.
1176 */
Bram Moolenaared203462004-06-16 11:19:22 +00001177 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 if (spats[0].off.off > 0)
1180 {
1181 for (c = spats[0].off.off; c; --c)
1182 if (decl(&pos) == -1)
1183 break;
1184 if (c) /* at start of buffer */
1185 {
1186 pos.lnum = 0; /* allow lnum == 0 here */
1187 pos.col = MAXCOL;
1188 }
1189 }
1190 else
1191 {
1192 for (c = spats[0].off.off; c; ++c)
1193 if (incl(&pos) == -1)
1194 break;
1195 if (c) /* at end of buffer */
1196 {
1197 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1198 pos.col = 0;
1199 }
1200 }
1201 }
1202
1203#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1204 if (p_altkeymap && curwin->w_p_rl)
1205 lrFswap(searchstr,0);
1206#endif
1207
1208 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1209 searchstr, count, spats[0].off.end + (options &
1210 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1211 + SEARCH_MSG + SEARCH_START
1212 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
1213 RE_LAST);
1214
1215 if (dircp != NULL)
1216 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1217 if (c == FAIL)
1218 {
1219 retval = 0;
1220 goto end_do_search;
1221 }
1222 if (spats[0].off.end && oap != NULL)
1223 oap->inclusive = TRUE; /* 'e' includes last character */
1224
1225 retval = 1; /* pattern found */
1226
1227 /*
1228 * Add character and/or line offset
1229 */
1230 if (!(options & SEARCH_NOOF) || *pat == ';')
1231 {
1232 if (spats[0].off.line) /* Add the offset to the line number. */
1233 {
1234 c = pos.lnum + spats[0].off.off;
1235 if (c < 1)
1236 pos.lnum = 1;
1237 else if (c > curbuf->b_ml.ml_line_count)
1238 pos.lnum = curbuf->b_ml.ml_line_count;
1239 else
1240 pos.lnum = c;
1241 pos.col = 0;
1242
1243 retval = 2; /* pattern found, line offset added */
1244 }
Bram Moolenaared203462004-06-16 11:19:22 +00001245 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
1247 /* to the right, check for end of file */
1248 if (spats[0].off.off > 0)
1249 {
1250 for (c = spats[0].off.off; c; --c)
1251 if (incl(&pos) == -1)
1252 break;
1253 }
1254 /* to the left, check for start of file */
1255 else
1256 {
1257 if ((c = pos.col + spats[0].off.off) >= 0)
1258 pos.col = c;
1259 else
1260 for (c = spats[0].off.off; c; ++c)
1261 if (decl(&pos) == -1)
1262 break;
1263 }
1264 }
1265 }
1266
1267 /*
1268 * The search command can be followed by a ';' to do another search.
1269 * For example: "/pat/;/foo/+3;?bar"
1270 * This is like doing another search command, except:
1271 * - The remembered direction '/' or '?' is from the first search.
1272 * - When an error happens the cursor isn't moved at all.
1273 * Don't do this when called by get_address() (it handles ';' itself).
1274 */
1275 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1276 break;
1277
1278 dirc = *++pat;
1279 if (dirc != '?' && dirc != '/')
1280 {
1281 retval = 0;
1282 EMSG(_("E386: Expected '?' or '/' after ';'"));
1283 goto end_do_search;
1284 }
1285 ++pat;
1286 }
1287
1288 if (options & SEARCH_MARK)
1289 setpcmark();
1290 curwin->w_cursor = pos;
1291 curwin->w_set_curswant = TRUE;
1292
1293end_do_search:
1294 if (options & SEARCH_KEEP)
1295 spats[0].off = old_off;
1296 vim_free(strcopy);
1297
1298 return retval;
1299}
1300
1301#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1302/*
1303 * search_for_exact_line(buf, pos, dir, pat)
1304 *
1305 * Search for a line starting with the given pattern (ignoring leading
1306 * white-space), starting from pos and going in direction dir. pos will
1307 * contain the position of the match found. Blank lines match only if
1308 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1309 * Return OK for success, or FAIL if no line found.
1310 */
1311 int
1312search_for_exact_line(buf, pos, dir, pat)
1313 buf_T *buf;
1314 pos_T *pos;
1315 int dir;
1316 char_u *pat;
1317{
1318 linenr_T start = 0;
1319 char_u *ptr;
1320 char_u *p;
1321
1322 if (buf->b_ml.ml_line_count == 0)
1323 return FAIL;
1324 for (;;)
1325 {
1326 pos->lnum += dir;
1327 if (pos->lnum < 1)
1328 {
1329 if (p_ws)
1330 {
1331 pos->lnum = buf->b_ml.ml_line_count;
1332 if (!shortmess(SHM_SEARCH))
1333 give_warning((char_u *)_(top_bot_msg), TRUE);
1334 }
1335 else
1336 {
1337 pos->lnum = 1;
1338 break;
1339 }
1340 }
1341 else if (pos->lnum > buf->b_ml.ml_line_count)
1342 {
1343 if (p_ws)
1344 {
1345 pos->lnum = 1;
1346 if (!shortmess(SHM_SEARCH))
1347 give_warning((char_u *)_(bot_top_msg), TRUE);
1348 }
1349 else
1350 {
1351 pos->lnum = 1;
1352 break;
1353 }
1354 }
1355 if (pos->lnum == start)
1356 break;
1357 if (start == 0)
1358 start = pos->lnum;
1359 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1360 p = skipwhite(ptr);
1361 pos->col = (colnr_T) (p - ptr);
1362
1363 /* when adding lines the matching line may be empty but it is not
1364 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001365 if ((compl_cont_status & CONT_ADDING)
1366 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
1368 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1369 return OK;
1370 }
1371 else if (*p != NUL) /* ignore empty lines */
1372 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001373 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1374 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 return OK;
1376 }
1377 }
1378 return FAIL;
1379}
1380#endif /* FEAT_INS_EXPAND */
1381
1382/*
1383 * Character Searches
1384 */
1385
1386/*
1387 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1388 * position of the character, otherwise move to just before the char.
1389 * Do this "cap->count1" times.
1390 * Return FAIL or OK.
1391 */
1392 int
1393searchc(cap, t_cmd)
1394 cmdarg_T *cap;
1395 int t_cmd;
1396{
1397 int c = cap->nchar; /* char to search for */
1398 int dir = cap->arg; /* TRUE for searching forward */
1399 long count = cap->count1; /* repeat count */
1400 static int lastc = NUL; /* last character searched for */
1401 static int lastcdir; /* last direction of character search */
1402 static int last_t_cmd; /* last search t_cmd */
1403 int col;
1404 char_u *p;
1405 int len;
1406#ifdef FEAT_MBYTE
1407 static char_u bytes[MB_MAXBYTES];
1408 static int bytelen = 1; /* >1 for multi-byte char */
1409#endif
1410
1411 if (c != NUL) /* normal search: remember args for repeat */
1412 {
1413 if (!KeyStuffed) /* don't remember when redoing */
1414 {
1415 lastc = c;
1416 lastcdir = dir;
1417 last_t_cmd = t_cmd;
1418#ifdef FEAT_MBYTE
1419 bytelen = (*mb_char2bytes)(c, bytes);
1420 if (cap->ncharC1 != 0)
1421 {
1422 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1423 if (cap->ncharC2 != 0)
1424 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1425 }
1426#endif
1427 }
1428 }
1429 else /* repeat previous search */
1430 {
1431 if (lastc == NUL)
1432 return FAIL;
1433 if (dir) /* repeat in opposite direction */
1434 dir = -lastcdir;
1435 else
1436 dir = lastcdir;
1437 t_cmd = last_t_cmd;
1438 c = lastc;
1439 /* For multi-byte re-use last bytes[] and bytelen. */
1440 }
1441
1442 p = ml_get_curline();
1443 col = curwin->w_cursor.col;
1444 len = (int)STRLEN(p);
1445
1446 while (count--)
1447 {
1448#ifdef FEAT_MBYTE
1449 if (has_mbyte)
1450 {
1451 for (;;)
1452 {
1453 if (dir > 0)
1454 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001455 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (col >= len)
1457 return FAIL;
1458 }
1459 else
1460 {
1461 if (col == 0)
1462 return FAIL;
1463 col -= (*mb_head_off)(p, p + col - 1) + 1;
1464 }
1465 if (bytelen == 1)
1466 {
1467 if (p[col] == c)
1468 break;
1469 }
1470 else
1471 {
1472 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1473 break;
1474 }
1475 }
1476 }
1477 else
1478#endif
1479 {
1480 for (;;)
1481 {
1482 if ((col += dir) < 0 || col >= len)
1483 return FAIL;
1484 if (p[col] == c)
1485 break;
1486 }
1487 }
1488 }
1489
1490 if (t_cmd)
1491 {
1492 /* backup to before the character (possibly double-byte) */
1493 col -= dir;
1494#ifdef FEAT_MBYTE
1495 if (has_mbyte)
1496 {
1497 if (dir < 0)
1498 /* Landed on the search char which is bytelen long */
1499 col += bytelen - 1;
1500 else
1501 /* To previous char, which may be multi-byte. */
1502 col -= (*mb_head_off)(p, p + col);
1503 }
1504#endif
1505 }
1506 curwin->w_cursor.col = col;
1507
1508 return OK;
1509}
1510
1511/*
1512 * "Other" Searches
1513 */
1514
1515/*
1516 * findmatch - find the matching paren or brace
1517 *
1518 * Improvement over vi: Braces inside quotes are ignored.
1519 */
1520 pos_T *
1521findmatch(oap, initc)
1522 oparg_T *oap;
1523 int initc;
1524{
1525 return findmatchlimit(oap, initc, 0, 0);
1526}
1527
1528/*
1529 * Return TRUE if the character before "linep[col]" equals "ch".
1530 * Return FALSE if "col" is zero.
1531 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1532 * is NULL.
1533 * Handles multibyte string correctly.
1534 */
1535 static int
1536check_prevcol(linep, col, ch, prevcol)
1537 char_u *linep;
1538 int col;
1539 int ch;
1540 int *prevcol;
1541{
1542 --col;
1543#ifdef FEAT_MBYTE
1544 if (col > 0 && has_mbyte)
1545 col -= (*mb_head_off)(linep, linep + col);
1546#endif
1547 if (prevcol)
1548 *prevcol = col;
1549 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1550}
1551
1552/*
1553 * findmatchlimit -- find the matching paren or brace, if it exists within
1554 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1555 * the edge of the file.
1556 *
1557 * "initc" is the character to find a match for. NUL means to find the
1558 * character at or after the cursor.
1559 *
1560 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1561 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1562 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1563 * FM_SKIPCOMM skip comments (not implemented yet!)
1564 */
1565
1566 pos_T *
1567findmatchlimit(oap, initc, flags, maxtravel)
1568 oparg_T *oap;
1569 int initc;
1570 int flags;
1571 int maxtravel;
1572{
1573 static pos_T pos; /* current search position */
1574 int findc = 0; /* matching brace */
1575 int c;
1576 int count = 0; /* cumulative number of braces */
1577 int backwards = FALSE; /* init for gcc */
1578 int inquote = FALSE; /* TRUE when inside quotes */
1579 char_u *linep; /* pointer to current line */
1580 char_u *ptr;
1581 int do_quotes; /* check for quotes in current line */
1582 int at_start; /* do_quotes value at start position */
1583 int hash_dir = 0; /* Direction searched for # things */
1584 int comment_dir = 0; /* Direction searched for comments */
1585 pos_T match_pos; /* Where last slash-star was found */
1586 int start_in_quotes; /* start position is in quotes */
1587 int traveled = 0; /* how far we've searched so far */
1588 int ignore_cend = FALSE; /* ignore comment end */
1589 int cpo_match; /* vi compatible matching */
1590 int cpo_bsl; /* don't recognize backslashes */
1591 int match_escaped = 0; /* search for escaped match */
1592 int dir; /* Direction to search */
1593 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001594#ifdef FEAT_LISP
1595 int lispcomm = FALSE; /* inside of Lisp-style comment */
1596 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
1599 pos = curwin->w_cursor;
1600 linep = ml_get(pos.lnum);
1601
1602 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1603 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1604
1605 /* Direction to search when initc is '/', '*' or '#' */
1606 if (flags & FM_BACKWARD)
1607 dir = BACKWARD;
1608 else if (flags & FM_FORWARD)
1609 dir = FORWARD;
1610 else
1611 dir = 0;
1612
1613 /*
1614 * if initc given, look in the table for the matching character
1615 * '/' and '*' are special cases: look for start or end of comment.
1616 * When '/' is used, we ignore running backwards into an star-slash, for
1617 * "[*" command, we just want to find any comment.
1618 */
1619 if (initc == '/' || initc == '*')
1620 {
1621 comment_dir = dir;
1622 if (initc == '/')
1623 ignore_cend = TRUE;
1624 backwards = (dir == FORWARD) ? FALSE : TRUE;
1625 initc = NUL;
1626 }
1627 else if (initc != '#' && initc != NUL)
1628 {
1629 /* 'matchpairs' is "x:y,x:y" */
1630 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1631 {
1632 if (*ptr == initc)
1633 {
1634 findc = initc;
1635 initc = ptr[2];
1636 backwards = TRUE;
1637 break;
1638 }
1639 ptr += 2;
1640 if (*ptr == initc)
1641 {
1642 findc = initc;
1643 initc = ptr[-2];
1644 backwards = FALSE;
1645 break;
1646 }
1647 if (ptr[1] != ',')
1648 break;
1649 }
1650 if (!findc) /* invalid initc! */
1651 return NULL;
1652 }
1653 /*
1654 * Either initc is '#', or no initc was given and we need to look under the
1655 * cursor.
1656 */
1657 else
1658 {
1659 if (initc == '#')
1660 {
1661 hash_dir = dir;
1662 }
1663 else
1664 {
1665 /*
1666 * initc was not given, must look for something to match under
1667 * or near the cursor.
1668 * Only check for special things when 'cpo' doesn't have '%'.
1669 */
1670 if (!cpo_match)
1671 {
1672 /* Are we before or at #if, #else etc.? */
1673 ptr = skipwhite(linep);
1674 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1675 {
1676 ptr = skipwhite(ptr + 1);
1677 if ( STRNCMP(ptr, "if", 2) == 0
1678 || STRNCMP(ptr, "endif", 5) == 0
1679 || STRNCMP(ptr, "el", 2) == 0)
1680 hash_dir = 1;
1681 }
1682
1683 /* Are we on a comment? */
1684 else if (linep[pos.col] == '/')
1685 {
1686 if (linep[pos.col + 1] == '*')
1687 {
1688 comment_dir = FORWARD;
1689 backwards = FALSE;
1690 pos.col++;
1691 }
1692 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1693 {
1694 comment_dir = BACKWARD;
1695 backwards = TRUE;
1696 pos.col--;
1697 }
1698 }
1699 else if (linep[pos.col] == '*')
1700 {
1701 if (linep[pos.col + 1] == '/')
1702 {
1703 comment_dir = BACKWARD;
1704 backwards = TRUE;
1705 }
1706 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1707 {
1708 comment_dir = FORWARD;
1709 backwards = FALSE;
1710 }
1711 }
1712 }
1713
1714 /*
1715 * If we are not on a comment or the # at the start of a line, then
1716 * look for brace anywhere on this line after the cursor.
1717 */
1718 if (!hash_dir && !comment_dir)
1719 {
1720 /*
1721 * Find the brace under or after the cursor.
1722 * If beyond the end of the line, use the last character in
1723 * the line.
1724 */
1725 if (linep[pos.col] == NUL && pos.col)
1726 --pos.col;
1727 for (;;)
1728 {
1729 initc = linep[pos.col];
1730 if (initc == NUL)
1731 break;
1732
1733 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1734 {
1735 if (*ptr == initc)
1736 {
1737 findc = ptr[2];
1738 backwards = FALSE;
1739 break;
1740 }
1741 ptr += 2;
1742 if (*ptr == initc)
1743 {
1744 findc = ptr[-2];
1745 backwards = TRUE;
1746 break;
1747 }
1748 if (!*++ptr)
1749 break;
1750 }
1751 if (findc)
1752 break;
1753#ifdef FEAT_MBYTE
1754 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001755 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 else
1757#endif
1758 ++pos.col;
1759 }
1760 if (!findc)
1761 {
1762 /* no brace in the line, maybe use " #if" then */
1763 if (!cpo_match && *skipwhite(linep) == '#')
1764 hash_dir = 1;
1765 else
1766 return NULL;
1767 }
1768 else if (!cpo_bsl)
1769 {
1770 int col, bslcnt = 0;
1771
1772 /* Set "match_escaped" if there are an odd number of
1773 * backslashes. */
1774 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1775 bslcnt++;
1776 match_escaped = (bslcnt & 1);
1777 }
1778 }
1779 }
1780 if (hash_dir)
1781 {
1782 /*
1783 * Look for matching #if, #else, #elif, or #endif
1784 */
1785 if (oap != NULL)
1786 oap->motion_type = MLINE; /* Linewise for this case only */
1787 if (initc != '#')
1788 {
1789 ptr = skipwhite(skipwhite(linep) + 1);
1790 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1791 hash_dir = 1;
1792 else if (STRNCMP(ptr, "endif", 5) == 0)
1793 hash_dir = -1;
1794 else
1795 return NULL;
1796 }
1797 pos.col = 0;
1798 while (!got_int)
1799 {
1800 if (hash_dir > 0)
1801 {
1802 if (pos.lnum == curbuf->b_ml.ml_line_count)
1803 break;
1804 }
1805 else if (pos.lnum == 1)
1806 break;
1807 pos.lnum += hash_dir;
1808 linep = ml_get(pos.lnum);
1809 line_breakcheck(); /* check for CTRL-C typed */
1810 ptr = skipwhite(linep);
1811 if (*ptr != '#')
1812 continue;
1813 pos.col = (colnr_T) (ptr - linep);
1814 ptr = skipwhite(ptr + 1);
1815 if (hash_dir > 0)
1816 {
1817 if (STRNCMP(ptr, "if", 2) == 0)
1818 count++;
1819 else if (STRNCMP(ptr, "el", 2) == 0)
1820 {
1821 if (count == 0)
1822 return &pos;
1823 }
1824 else if (STRNCMP(ptr, "endif", 5) == 0)
1825 {
1826 if (count == 0)
1827 return &pos;
1828 count--;
1829 }
1830 }
1831 else
1832 {
1833 if (STRNCMP(ptr, "if", 2) == 0)
1834 {
1835 if (count == 0)
1836 return &pos;
1837 count--;
1838 }
1839 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1840 {
1841 if (count == 0)
1842 return &pos;
1843 }
1844 else if (STRNCMP(ptr, "endif", 5) == 0)
1845 count++;
1846 }
1847 }
1848 return NULL;
1849 }
1850 }
1851
1852#ifdef FEAT_RIGHTLEFT
1853 /* This is just guessing: when 'rightleft' is set, search for a maching
1854 * paren/brace in the other direction. */
1855 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1856 backwards = !backwards;
1857#endif
1858
1859 do_quotes = -1;
1860 start_in_quotes = MAYBE;
1861 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001862 if ((backwards && comment_dir)
1863#ifdef FEAT_LISP
1864 || lisp
1865#endif
1866 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001868#ifdef FEAT_LISP
1869 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1870 lispcomm = TRUE; /* find match inside this comment */
1871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 while (!got_int)
1873 {
1874 /*
1875 * Go to the next position, forward or backward. We could use
1876 * inc() and dec() here, but that is much slower
1877 */
1878 if (backwards)
1879 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001880#ifdef FEAT_LISP
1881 /* char to match is inside of comment, don't search outside */
1882 if (lispcomm && pos.col < (colnr_T)comment_col)
1883 break;
1884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (pos.col == 0) /* at start of line, go to prev. one */
1886 {
1887 if (pos.lnum == 1) /* start of file */
1888 break;
1889 --pos.lnum;
1890
1891 if (maxtravel && traveled++ > maxtravel)
1892 break;
1893
1894 linep = ml_get(pos.lnum);
1895 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1896 do_quotes = -1;
1897 line_breakcheck();
1898
1899 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001900 if (comment_dir
1901#ifdef FEAT_LISP
1902 || lisp
1903#endif
1904 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001906#ifdef FEAT_LISP
1907 /* skip comment */
1908 if (lisp && comment_col != MAXCOL)
1909 pos.col = comment_col;
1910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 }
1912 else
1913 {
1914 --pos.col;
1915#ifdef FEAT_MBYTE
1916 if (has_mbyte)
1917 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1918#endif
1919 }
1920 }
1921 else /* forward search */
1922 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001923 if (linep[pos.col] == NUL
1924 /* at end of line, go to next one */
1925#ifdef FEAT_LISP
1926 /* don't search for match in comment */
1927 || (lisp && comment_col != MAXCOL
1928 && pos.col == (colnr_T)comment_col)
1929#endif
1930 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001932 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
1933#ifdef FEAT_LISP
1934 /* line is exhausted and comment with it,
1935 * don't search for match in code */
1936 || lispcomm
1937#endif
1938 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 break;
1940 ++pos.lnum;
1941
1942 if (maxtravel && traveled++ > maxtravel)
1943 break;
1944
1945 linep = ml_get(pos.lnum);
1946 pos.col = 0;
1947 do_quotes = -1;
1948 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001949#ifdef FEAT_LISP
1950 if (lisp) /* find comment pos in new line */
1951 comment_col = check_linecomment(linep);
1952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 else
1955 {
1956#ifdef FEAT_MBYTE
1957 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001958 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 else
1960#endif
1961 ++pos.col;
1962 }
1963 }
1964
1965 /*
1966 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
1967 */
1968 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
1969 (linep[0] == '{' || linep[0] == '}'))
1970 {
1971 if (linep[0] == findc && count == 0) /* match! */
1972 return &pos;
1973 break; /* out of scope */
1974 }
1975
1976 if (comment_dir)
1977 {
1978 /* Note: comments do not nest, and we ignore quotes in them */
1979 /* TODO: ignore comment brackets inside strings */
1980 if (comment_dir == FORWARD)
1981 {
1982 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
1983 {
1984 pos.col++;
1985 return &pos;
1986 }
1987 }
1988 else /* Searching backwards */
1989 {
1990 /*
1991 * A comment may contain / * or / /, it may also start or end
1992 * with / * /. Ignore a / * after / /.
1993 */
1994 if (pos.col == 0)
1995 continue;
1996 else if ( linep[pos.col - 1] == '/'
1997 && linep[pos.col] == '*'
1998 && (int)pos.col < comment_col)
1999 {
2000 count++;
2001 match_pos = pos;
2002 match_pos.col--;
2003 }
2004 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2005 {
2006 if (count > 0)
2007 pos = match_pos;
2008 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2009 && (int)pos.col <= comment_col)
2010 pos.col -= 2;
2011 else if (ignore_cend)
2012 continue;
2013 else
2014 return NULL;
2015 return &pos;
2016 }
2017 }
2018 continue;
2019 }
2020
2021 /*
2022 * If smart matching ('cpoptions' does not contain '%'), braces inside
2023 * of quotes are ignored, but only if there is an even number of
2024 * quotes in the line.
2025 */
2026 if (cpo_match)
2027 do_quotes = 0;
2028 else if (do_quotes == -1)
2029 {
2030 /*
2031 * Count the number of quotes in the line, skipping \" and '"'.
2032 * Watch out for "\\".
2033 */
2034 at_start = do_quotes;
2035 for (ptr = linep; *ptr; ++ptr)
2036 {
2037 if (ptr == linep + pos.col + backwards)
2038 at_start = (do_quotes & 1);
2039 if (*ptr == '"'
2040 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2041 ++do_quotes;
2042 if (*ptr == '\\' && ptr[1] != NUL)
2043 ++ptr;
2044 }
2045 do_quotes &= 1; /* result is 1 with even number of quotes */
2046
2047 /*
2048 * If we find an uneven count, check current line and previous
2049 * one for a '\' at the end.
2050 */
2051 if (!do_quotes)
2052 {
2053 inquote = FALSE;
2054 if (ptr[-1] == '\\')
2055 {
2056 do_quotes = 1;
2057 if (start_in_quotes == MAYBE)
2058 {
2059 /* Do we need to use at_start here? */
2060 inquote = TRUE;
2061 start_in_quotes = TRUE;
2062 }
2063 else if (backwards)
2064 inquote = TRUE;
2065 }
2066 if (pos.lnum > 1)
2067 {
2068 ptr = ml_get(pos.lnum - 1);
2069 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2070 {
2071 do_quotes = 1;
2072 if (start_in_quotes == MAYBE)
2073 {
2074 inquote = at_start;
2075 if (inquote)
2076 start_in_quotes = TRUE;
2077 }
2078 else if (!backwards)
2079 inquote = TRUE;
2080 }
2081 }
2082 }
2083 }
2084 if (start_in_quotes == MAYBE)
2085 start_in_quotes = FALSE;
2086
2087 /*
2088 * If 'smartmatch' is set:
2089 * Things inside quotes are ignored by setting 'inquote'. If we
2090 * find a quote without a preceding '\' invert 'inquote'. At the
2091 * end of a line not ending in '\' we reset 'inquote'.
2092 *
2093 * In lines with an uneven number of quotes (without preceding '\')
2094 * we do not know which part to ignore. Therefore we only set
2095 * inquote if the number of quotes in a line is even, unless this
2096 * line or the previous one ends in a '\'. Complicated, isn't it?
2097 */
2098 switch (c = linep[pos.col])
2099 {
2100 case NUL:
2101 /* at end of line without trailing backslash, reset inquote */
2102 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2103 {
2104 inquote = FALSE;
2105 start_in_quotes = FALSE;
2106 }
2107 break;
2108
2109 case '"':
2110 /* a quote that is preceded with an odd number of backslashes is
2111 * ignored */
2112 if (do_quotes)
2113 {
2114 int col;
2115
2116 for (col = pos.col - 1; col >= 0; --col)
2117 if (linep[col] != '\\')
2118 break;
2119 if ((((int)pos.col - 1 - col) & 1) == 0)
2120 {
2121 inquote = !inquote;
2122 start_in_quotes = FALSE;
2123 }
2124 }
2125 break;
2126
2127 /*
2128 * If smart matching ('cpoptions' does not contain '%'):
2129 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2130 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2131 * skipped, there is never a brace in them.
2132 * Ignore this when finding matches for `'.
2133 */
2134 case '\'':
2135 if (!cpo_match && initc != '\'' && findc != '\'')
2136 {
2137 if (backwards)
2138 {
2139 if (pos.col > 1)
2140 {
2141 if (linep[pos.col - 2] == '\'')
2142 {
2143 pos.col -= 2;
2144 break;
2145 }
2146 else if (linep[pos.col - 2] == '\\' &&
2147 pos.col > 2 && linep[pos.col - 3] == '\'')
2148 {
2149 pos.col -= 3;
2150 break;
2151 }
2152 }
2153 }
2154 else if (linep[pos.col + 1]) /* forward search */
2155 {
2156 if (linep[pos.col + 1] == '\\' &&
2157 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2158 {
2159 pos.col += 3;
2160 break;
2161 }
2162 else if (linep[pos.col + 2] == '\'')
2163 {
2164 pos.col += 2;
2165 break;
2166 }
2167 }
2168 }
2169 /* FALLTHROUGH */
2170
2171 default:
2172#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002173 /*
2174 * For Lisp skip over backslashed (), {} and [].
2175 * (actually, we skip #\( et al)
2176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (curbuf->b_p_lisp
2178 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002179 && pos.col > 1
2180 && check_prevcol(linep, pos.col, '\\', NULL)
2181 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 break;
2183#endif
2184
2185 /* Check for match outside of quotes, and inside of
2186 * quotes when the start is also inside of quotes. */
2187 if ((!inquote || start_in_quotes == TRUE)
2188 && (c == initc || c == findc))
2189 {
2190 int col, bslcnt = 0;
2191
2192 if (!cpo_bsl)
2193 {
2194 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2195 bslcnt++;
2196 }
2197 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2198 * what we expect. */
2199 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2200 {
2201 if (c == initc)
2202 count++;
2203 else
2204 {
2205 if (count == 0)
2206 return &pos;
2207 count--;
2208 }
2209 }
2210 }
2211 }
2212 }
2213
2214 if (comment_dir == BACKWARD && count > 0)
2215 {
2216 pos = match_pos;
2217 return &pos;
2218 }
2219 return (pos_T *)NULL; /* never found it */
2220}
2221
2222/*
2223 * Check if line[] contains a / / comment.
2224 * Return MAXCOL if not, otherwise return the column.
2225 * TODO: skip strings.
2226 */
2227 static int
2228check_linecomment(line)
2229 char_u *line;
2230{
2231 char_u *p;
2232
2233 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002234#ifdef FEAT_LISP
2235 /* skip Lispish one-line comments */
2236 if (curbuf->b_p_lisp)
2237 {
2238 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2239 {
2240 int instr = FALSE; /* inside of string */
2241
2242 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002243 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002244 {
2245 if (*p == '"')
2246 {
2247 if (instr)
2248 {
2249 if (*(p - 1) != '\\') /* skip escaped quote */
2250 instr = FALSE;
2251 }
2252 else if (p == line || ((p - line) >= 2
2253 /* skip #\" form */
2254 && *(p - 1) != '\\' && *(p - 2) != '#'))
2255 instr = TRUE;
2256 }
2257 else if (!instr && ((p - line) < 2
2258 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2259 break; /* found! */
2260 ++p;
2261 }
2262 }
2263 else
2264 p = NULL;
2265 }
2266 else
2267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 while ((p = vim_strchr(p, '/')) != NULL)
2269 {
2270 if (p[1] == '/')
2271 break;
2272 ++p;
2273 }
2274
2275 if (p == NULL)
2276 return MAXCOL;
2277 return (int)(p - line);
2278}
2279
2280/*
2281 * Move cursor briefly to character matching the one under the cursor.
2282 * Used for Insert mode and "r" command.
2283 * Show the match only if it is visible on the screen.
2284 * If there isn't a match, then beep.
2285 */
2286 void
2287showmatch(c)
2288 int c; /* char to show match for */
2289{
2290 pos_T *lpos, save_cursor;
2291 pos_T mpos;
2292 colnr_T vcol;
2293 long save_so;
2294 long save_siso;
2295#ifdef CURSOR_SHAPE
2296 int save_state;
2297#endif
2298 colnr_T save_dollar_vcol;
2299 char_u *p;
2300
2301 /*
2302 * Only show match for chars in the 'matchpairs' option.
2303 */
2304 /* 'matchpairs' is "x:y,x:y" */
2305 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2306 {
2307#ifdef FEAT_RIGHTLEFT
2308 if (*p == c && (curwin->w_p_rl ^ p_ri))
2309 break;
2310#endif
2311 p += 2;
2312 if (*p == c
2313#ifdef FEAT_RIGHTLEFT
2314 && !(curwin->w_p_rl ^ p_ri)
2315#endif
2316 )
2317 break;
2318 if (p[1] != ',')
2319 return;
2320 }
2321
2322 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2323 vim_beep();
2324 else if (lpos->lnum >= curwin->w_topline)
2325 {
2326 if (!curwin->w_p_wrap)
2327 getvcol(curwin, lpos, NULL, &vcol, NULL);
2328 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2329 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2330 {
2331 mpos = *lpos; /* save the pos, update_screen() may change it */
2332 save_cursor = curwin->w_cursor;
2333 save_so = p_so;
2334 save_siso = p_siso;
2335 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2336 * stop displaying the "$". */
2337 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2338 dollar_vcol = 0;
2339 ++curwin->w_virtcol; /* do display ')' just before "$" */
2340 update_screen(VALID); /* show the new char first */
2341
2342 save_dollar_vcol = dollar_vcol;
2343#ifdef CURSOR_SHAPE
2344 save_state = State;
2345 State = SHOWMATCH;
2346 ui_cursor_shape(); /* may show different cursor shape */
2347#endif
2348 curwin->w_cursor = mpos; /* move to matching char */
2349 p_so = 0; /* don't use 'scrolloff' here */
2350 p_siso = 0; /* don't use 'sidescrolloff' here */
2351 showruler(FALSE);
2352 setcursor();
2353 cursor_on(); /* make sure that the cursor is shown */
2354 out_flush();
2355#ifdef FEAT_GUI
2356 if (gui.in_use)
2357 {
2358 gui_update_cursor(TRUE, FALSE);
2359 gui_mch_flush();
2360 }
2361#endif
2362 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2363 * which resets it if the matching position is in a previous line
2364 * and has a higher column number. */
2365 dollar_vcol = save_dollar_vcol;
2366
2367 /*
2368 * brief pause, unless 'm' is present in 'cpo' and a character is
2369 * available.
2370 */
2371 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2372 ui_delay(p_mat * 100L, TRUE);
2373 else if (!char_avail())
2374 ui_delay(p_mat * 100L, FALSE);
2375 curwin->w_cursor = save_cursor; /* restore cursor position */
2376 p_so = save_so;
2377 p_siso = save_siso;
2378#ifdef CURSOR_SHAPE
2379 State = save_state;
2380 ui_cursor_shape(); /* may show different cursor shape */
2381#endif
2382 }
2383 }
2384}
2385
2386/*
2387 * findsent(dir, count) - Find the start of the next sentence in direction
2388 * 'dir' Sentences are supposed to end in ".", "!" or "?" followed by white
2389 * space or a line break. Also stop at an empty line.
2390 * Return OK if the next sentence was found.
2391 */
2392 int
2393findsent(dir, count)
2394 int dir;
2395 long count;
2396{
2397 pos_T pos, tpos;
2398 int c;
2399 int (*func) __ARGS((pos_T *));
2400 int startlnum;
2401 int noskip = FALSE; /* do not skip blanks */
2402 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002403 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405 pos = curwin->w_cursor;
2406 if (dir == FORWARD)
2407 func = incl;
2408 else
2409 func = decl;
2410
2411 while (count--)
2412 {
2413 /*
2414 * if on an empty line, skip upto a non-empty line
2415 */
2416 if (gchar_pos(&pos) == NUL)
2417 {
2418 do
2419 if ((*func)(&pos) == -1)
2420 break;
2421 while (gchar_pos(&pos) == NUL);
2422 if (dir == FORWARD)
2423 goto found;
2424 }
2425 /*
2426 * if on the start of a paragraph or a section and searching forward,
2427 * go to the next line
2428 */
2429 else if (dir == FORWARD && pos.col == 0 &&
2430 startPS(pos.lnum, NUL, FALSE))
2431 {
2432 if (pos.lnum == curbuf->b_ml.ml_line_count)
2433 return FAIL;
2434 ++pos.lnum;
2435 goto found;
2436 }
2437 else if (dir == BACKWARD)
2438 decl(&pos);
2439
2440 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002441 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2443 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2444 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002445 if (vim_strchr((char_u *)".!?", c) != NULL)
2446 {
2447 /* Only skip over a '.', '!' and '?' once. */
2448 if (found_dot)
2449 break;
2450 found_dot = TRUE;
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (decl(&pos) == -1)
2453 break;
2454 /* when going forward: Stop in front of empty line */
2455 if (lineempty(pos.lnum) && dir == FORWARD)
2456 {
2457 incl(&pos);
2458 goto found;
2459 }
2460 }
2461
2462 /* remember the line where the search started */
2463 startlnum = pos.lnum;
2464 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2465
2466 for (;;) /* find end of sentence */
2467 {
2468 c = gchar_pos(&pos);
2469 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2470 {
2471 if (dir == BACKWARD && pos.lnum != startlnum)
2472 ++pos.lnum;
2473 break;
2474 }
2475 if (c == '.' || c == '!' || c == '?')
2476 {
2477 tpos = pos;
2478 do
2479 if ((c = inc(&tpos)) == -1)
2480 break;
2481 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2482 != NULL);
2483 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2484 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2485 && gchar_pos(&tpos) == ' ')))
2486 {
2487 pos = tpos;
2488 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2489 inc(&pos);
2490 break;
2491 }
2492 }
2493 if ((*func)(&pos) == -1)
2494 {
2495 if (count)
2496 return FAIL;
2497 noskip = TRUE;
2498 break;
2499 }
2500 }
2501found:
2502 /* skip white space */
2503 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2504 if (incl(&pos) == -1)
2505 break;
2506 }
2507
2508 setpcmark();
2509 curwin->w_cursor = pos;
2510 return OK;
2511}
2512
2513/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002514 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002516 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 * If 'what' is '{' or '}' we go to the next section.
2518 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002519 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
2521 int
2522findpar(oap, dir, count, what, both)
2523 oparg_T *oap;
2524 int dir;
2525 long count;
2526 int what;
2527 int both;
2528{
2529 linenr_T curr;
2530 int did_skip; /* TRUE after separating lines have been skipped */
2531 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002532 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#ifdef FEAT_FOLDING
2534 linenr_T fold_first; /* first line of a closed fold */
2535 linenr_T fold_last; /* last line of a closed fold */
2536 int fold_skipped; /* TRUE if a closed fold was skipped this
2537 iteration */
2538#endif
2539
2540 curr = curwin->w_cursor.lnum;
2541
2542 while (count--)
2543 {
2544 did_skip = FALSE;
2545 for (first = TRUE; ; first = FALSE)
2546 {
2547 if (*ml_get(curr) != NUL)
2548 did_skip = TRUE;
2549
2550#ifdef FEAT_FOLDING
2551 /* skip folded lines */
2552 fold_skipped = FALSE;
2553 if (first && hasFolding(curr, &fold_first, &fold_last))
2554 {
2555 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2556 fold_skipped = TRUE;
2557 }
2558#endif
2559
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002560 /* POSIX has it's own ideas of what a paragraph boundary is and it
2561 * doesn't match historical Vi: It also stops at a "{" in the
2562 * first column and at an empty line. */
2563 if (!first && did_skip && (startPS(curr, what, both)
2564 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 break;
2566
2567#ifdef FEAT_FOLDING
2568 if (fold_skipped)
2569 curr -= dir;
2570#endif
2571 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2572 {
2573 if (count)
2574 return FALSE;
2575 curr -= dir;
2576 break;
2577 }
2578 }
2579 }
2580 setpcmark();
2581 if (both && *ml_get(curr) == '}') /* include line with '}' */
2582 ++curr;
2583 curwin->w_cursor.lnum = curr;
2584 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2585 {
2586 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2587 {
2588 --curwin->w_cursor.col;
2589 oap->inclusive = TRUE;
2590 }
2591 }
2592 else
2593 curwin->w_cursor.col = 0;
2594 return TRUE;
2595}
2596
2597/*
2598 * check if the string 's' is a nroff macro that is in option 'opt'
2599 */
2600 static int
2601inmacro(opt, s)
2602 char_u *opt;
2603 char_u *s;
2604{
2605 char_u *macro;
2606
2607 for (macro = opt; macro[0]; ++macro)
2608 {
2609 /* Accept two characters in the option being equal to two characters
2610 * in the line. A space in the option matches with a space in the
2611 * line or the line having ended. */
2612 if ( (macro[0] == s[0]
2613 || (macro[0] == ' '
2614 && (s[0] == NUL || s[0] == ' ')))
2615 && (macro[1] == s[1]
2616 || ((macro[1] == NUL || macro[1] == ' ')
2617 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2618 break;
2619 ++macro;
2620 if (macro[0] == NUL)
2621 break;
2622 }
2623 return (macro[0] != NUL);
2624}
2625
2626/*
2627 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2628 * If 'para' is '{' or '}' only check for sections.
2629 * If 'both' is TRUE also stop at '}'
2630 */
2631 int
2632startPS(lnum, para, both)
2633 linenr_T lnum;
2634 int para;
2635 int both;
2636{
2637 char_u *s;
2638
2639 s = ml_get(lnum);
2640 if (*s == para || *s == '\f' || (both && *s == '}'))
2641 return TRUE;
2642 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2643 (!para && inmacro(p_para, s + 1))))
2644 return TRUE;
2645 return FALSE;
2646}
2647
2648/*
2649 * The following routines do the word searches performed by the 'w', 'W',
2650 * 'b', 'B', 'e', and 'E' commands.
2651 */
2652
2653/*
2654 * To perform these searches, characters are placed into one of three
2655 * classes, and transitions between classes determine word boundaries.
2656 *
2657 * The classes are:
2658 *
2659 * 0 - white space
2660 * 1 - punctuation
2661 * 2 or higher - keyword characters (letters, digits and underscore)
2662 */
2663
2664static int cls_bigword; /* TRUE for "W", "B" or "E" */
2665
2666/*
2667 * cls() - returns the class of character at curwin->w_cursor
2668 *
2669 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2670 * from class 2 and higher are reported as class 1 since only white space
2671 * boundaries are of interest.
2672 */
2673 static int
2674cls()
2675{
2676 int c;
2677
2678 c = gchar_cursor();
2679#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2680 if (p_altkeymap && c == F_BLANK)
2681 return 0;
2682#endif
2683 if (c == ' ' || c == '\t' || c == NUL)
2684 return 0;
2685#ifdef FEAT_MBYTE
2686 if (enc_dbcs != 0 && c > 0xFF)
2687 {
2688 /* If cls_bigword, report multi-byte chars as class 1. */
2689 if (enc_dbcs == DBCS_KOR && cls_bigword)
2690 return 1;
2691
2692 /* process code leading/trailing bytes */
2693 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2694 }
2695 if (enc_utf8)
2696 {
2697 c = utf_class(c);
2698 if (c != 0 && cls_bigword)
2699 return 1;
2700 return c;
2701 }
2702#endif
2703
2704 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2705 if (cls_bigword)
2706 return 1;
2707
2708 if (vim_iswordc(c))
2709 return 2;
2710 return 1;
2711}
2712
2713
2714/*
2715 * fwd_word(count, type, eol) - move forward one word
2716 *
2717 * Returns FAIL if the cursor was already at the end of the file.
2718 * If eol is TRUE, last word stops at end of line (for operators).
2719 */
2720 int
2721fwd_word(count, bigword, eol)
2722 long count;
2723 int bigword; /* "W", "E" or "B" */
2724 int eol;
2725{
2726 int sclass; /* starting class */
2727 int i;
2728 int last_line;
2729
2730#ifdef FEAT_VIRTUALEDIT
2731 curwin->w_cursor.coladd = 0;
2732#endif
2733 cls_bigword = bigword;
2734 while (--count >= 0)
2735 {
2736#ifdef FEAT_FOLDING
2737 /* When inside a range of folded lines, move to the last char of the
2738 * last line. */
2739 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2740 coladvance((colnr_T)MAXCOL);
2741#endif
2742 sclass = cls();
2743
2744 /*
2745 * We always move at least one character, unless on the last
2746 * character in the buffer.
2747 */
2748 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2749 i = inc_cursor();
2750 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2751 return FAIL;
2752 if (i == 1 && eol && count == 0) /* started at last char in line */
2753 return OK;
2754
2755 /*
2756 * Go one char past end of current word (if any)
2757 */
2758 if (sclass != 0)
2759 while (cls() == sclass)
2760 {
2761 i = inc_cursor();
2762 if (i == -1 || (i >= 1 && eol && count == 0))
2763 return OK;
2764 }
2765
2766 /*
2767 * go to next non-white
2768 */
2769 while (cls() == 0)
2770 {
2771 /*
2772 * We'll stop if we land on a blank line
2773 */
2774 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2775 break;
2776
2777 i = inc_cursor();
2778 if (i == -1 || (i >= 1 && eol && count == 0))
2779 return OK;
2780 }
2781 }
2782 return OK;
2783}
2784
2785/*
2786 * bck_word() - move backward 'count' words
2787 *
2788 * If stop is TRUE and we are already on the start of a word, move one less.
2789 *
2790 * Returns FAIL if top of the file was reached.
2791 */
2792 int
2793bck_word(count, bigword, stop)
2794 long count;
2795 int bigword;
2796 int stop;
2797{
2798 int sclass; /* starting class */
2799
2800#ifdef FEAT_VIRTUALEDIT
2801 curwin->w_cursor.coladd = 0;
2802#endif
2803 cls_bigword = bigword;
2804 while (--count >= 0)
2805 {
2806#ifdef FEAT_FOLDING
2807 /* When inside a range of folded lines, move to the first char of the
2808 * first line. */
2809 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2810 curwin->w_cursor.col = 0;
2811#endif
2812 sclass = cls();
2813 if (dec_cursor() == -1) /* started at start of file */
2814 return FAIL;
2815
2816 if (!stop || sclass == cls() || sclass == 0)
2817 {
2818 /*
2819 * Skip white space before the word.
2820 * Stop on an empty line.
2821 */
2822 while (cls() == 0)
2823 {
2824 if (curwin->w_cursor.col == 0
2825 && lineempty(curwin->w_cursor.lnum))
2826 goto finished;
2827 if (dec_cursor() == -1) /* hit start of file, stop here */
2828 return OK;
2829 }
2830
2831 /*
2832 * Move backward to start of this word.
2833 */
2834 if (skip_chars(cls(), BACKWARD))
2835 return OK;
2836 }
2837
2838 inc_cursor(); /* overshot - forward one */
2839finished:
2840 stop = FALSE;
2841 }
2842 return OK;
2843}
2844
2845/*
2846 * end_word() - move to the end of the word
2847 *
2848 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2849 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2850 * motion crosses blank lines. When the real vi crosses a blank line in an
2851 * 'e' motion, the cursor is placed on the FIRST character of the next
2852 * non-blank line. The 'E' command, however, works correctly. Since this
2853 * appears to be a bug, I have not duplicated it here.
2854 *
2855 * Returns FAIL if end of the file was reached.
2856 *
2857 * If stop is TRUE and we are already on the end of a word, move one less.
2858 * If empty is TRUE stop on an empty line.
2859 */
2860 int
2861end_word(count, bigword, stop, empty)
2862 long count;
2863 int bigword;
2864 int stop;
2865 int empty;
2866{
2867 int sclass; /* starting class */
2868
2869#ifdef FEAT_VIRTUALEDIT
2870 curwin->w_cursor.coladd = 0;
2871#endif
2872 cls_bigword = bigword;
2873 while (--count >= 0)
2874 {
2875#ifdef FEAT_FOLDING
2876 /* When inside a range of folded lines, move to the last char of the
2877 * last line. */
2878 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2879 coladvance((colnr_T)MAXCOL);
2880#endif
2881 sclass = cls();
2882 if (inc_cursor() == -1)
2883 return FAIL;
2884
2885 /*
2886 * If we're in the middle of a word, we just have to move to the end
2887 * of it.
2888 */
2889 if (cls() == sclass && sclass != 0)
2890 {
2891 /*
2892 * Move forward to end of the current word
2893 */
2894 if (skip_chars(sclass, FORWARD))
2895 return FAIL;
2896 }
2897 else if (!stop || sclass == 0)
2898 {
2899 /*
2900 * We were at the end of a word. Go to the end of the next word.
2901 * First skip white space, if 'empty' is TRUE, stop at empty line.
2902 */
2903 while (cls() == 0)
2904 {
2905 if (empty && curwin->w_cursor.col == 0
2906 && lineempty(curwin->w_cursor.lnum))
2907 goto finished;
2908 if (inc_cursor() == -1) /* hit end of file, stop here */
2909 return FAIL;
2910 }
2911
2912 /*
2913 * Move forward to the end of this word.
2914 */
2915 if (skip_chars(cls(), FORWARD))
2916 return FAIL;
2917 }
2918 dec_cursor(); /* overshot - one char backward */
2919finished:
2920 stop = FALSE; /* we move only one word less */
2921 }
2922 return OK;
2923}
2924
2925/*
2926 * Move back to the end of the word.
2927 *
2928 * Returns FAIL if start of the file was reached.
2929 */
2930 int
2931bckend_word(count, bigword, eol)
2932 long count;
2933 int bigword; /* TRUE for "B" */
2934 int eol; /* TRUE: stop at end of line. */
2935{
2936 int sclass; /* starting class */
2937 int i;
2938
2939#ifdef FEAT_VIRTUALEDIT
2940 curwin->w_cursor.coladd = 0;
2941#endif
2942 cls_bigword = bigword;
2943 while (--count >= 0)
2944 {
2945 sclass = cls();
2946 if ((i = dec_cursor()) == -1)
2947 return FAIL;
2948 if (eol && i == 1)
2949 return OK;
2950
2951 /*
2952 * Move backward to before the start of this word.
2953 */
2954 if (sclass != 0)
2955 {
2956 while (cls() == sclass)
2957 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2958 return OK;
2959 }
2960
2961 /*
2962 * Move backward to end of the previous word
2963 */
2964 while (cls() == 0)
2965 {
2966 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
2967 break;
2968 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2969 return OK;
2970 }
2971 }
2972 return OK;
2973}
2974
2975/*
2976 * Skip a row of characters of the same class.
2977 * Return TRUE when end-of-file reached, FALSE otherwise.
2978 */
2979 static int
2980skip_chars(cclass, dir)
2981 int cclass;
2982 int dir;
2983{
2984 while (cls() == cclass)
2985 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
2986 return TRUE;
2987 return FALSE;
2988}
2989
2990#ifdef FEAT_TEXTOBJ
2991/*
2992 * Go back to the start of the word or the start of white space
2993 */
2994 static void
2995back_in_line()
2996{
2997 int sclass; /* starting class */
2998
2999 sclass = cls();
3000 for (;;)
3001 {
3002 if (curwin->w_cursor.col == 0) /* stop at start of line */
3003 break;
3004 dec_cursor();
3005 if (cls() != sclass) /* stop at start of word */
3006 {
3007 inc_cursor();
3008 break;
3009 }
3010 }
3011}
3012
3013 static void
3014find_first_blank(posp)
3015 pos_T *posp;
3016{
3017 int c;
3018
3019 while (decl(posp) != -1)
3020 {
3021 c = gchar_pos(posp);
3022 if (!vim_iswhite(c))
3023 {
3024 incl(posp);
3025 break;
3026 }
3027 }
3028}
3029
3030/*
3031 * Skip count/2 sentences and count/2 separating white spaces.
3032 */
3033 static void
3034findsent_forward(count, at_start_sent)
3035 long count;
3036 int at_start_sent; /* cursor is at start of sentence */
3037{
3038 while (count--)
3039 {
3040 findsent(FORWARD, 1L);
3041 if (at_start_sent)
3042 find_first_blank(&curwin->w_cursor);
3043 if (count == 0 || at_start_sent)
3044 decl(&curwin->w_cursor);
3045 at_start_sent = !at_start_sent;
3046 }
3047}
3048
3049/*
3050 * Find word under cursor, cursor at end.
3051 * Used while an operator is pending, and in Visual mode.
3052 */
3053 int
3054current_word(oap, count, include, bigword)
3055 oparg_T *oap;
3056 long count;
3057 int include; /* TRUE: include word and white space */
3058 int bigword; /* FALSE == word, TRUE == WORD */
3059{
3060 pos_T start_pos;
3061 pos_T pos;
3062 int inclusive = TRUE;
3063 int include_white = FALSE;
3064
3065 cls_bigword = bigword;
3066
3067#ifdef FEAT_VISUAL
3068 /* Correct cursor when 'selection' is exclusive */
3069 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3070 dec_cursor();
3071
3072 /*
3073 * When Visual mode is not active, or when the VIsual area is only one
3074 * character, select the word and/or white space under the cursor.
3075 */
3076 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3077#endif
3078 {
3079 /*
3080 * Go to start of current word or white space.
3081 */
3082 back_in_line();
3083 start_pos = curwin->w_cursor;
3084
3085 /*
3086 * If the start is on white space, and white space should be included
3087 * (" word"), or start is not on white space, and white space should
3088 * not be included ("word"), find end of word.
3089 */
3090 if ((cls() == 0) == include)
3091 {
3092 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3093 return FAIL;
3094 }
3095 else
3096 {
3097 /*
3098 * If the start is not on white space, and white space should be
3099 * included ("word "), or start is on white space and white
3100 * space should not be included (" "), find start of word.
3101 * If we end up in the first column of the next line (single char
3102 * word) back up to end of the line.
3103 */
3104 fwd_word(1L, bigword, TRUE);
3105 if (curwin->w_cursor.col == 0)
3106 decl(&curwin->w_cursor);
3107 else
3108 oneleft();
3109
3110 if (include)
3111 include_white = TRUE;
3112 }
3113
3114#ifdef FEAT_VISUAL
3115 if (VIsual_active)
3116 {
3117 /* should do something when inclusive == FALSE ! */
3118 VIsual = start_pos;
3119 redraw_curbuf_later(INVERTED); /* update the inversion */
3120 }
3121 else
3122#endif
3123 {
3124 oap->start = start_pos;
3125 oap->motion_type = MCHAR;
3126 }
3127 --count;
3128 }
3129
3130 /*
3131 * When count is still > 0, extend with more objects.
3132 */
3133 while (count > 0)
3134 {
3135 inclusive = TRUE;
3136#ifdef FEAT_VISUAL
3137 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3138 {
3139 /*
3140 * In Visual mode, with cursor at start: move cursor back.
3141 */
3142 if (decl(&curwin->w_cursor) == -1)
3143 return FAIL;
3144 if (include != (cls() != 0))
3145 {
3146 if (bck_word(1L, bigword, TRUE) == FAIL)
3147 return FAIL;
3148 }
3149 else
3150 {
3151 if (bckend_word(1L, bigword, TRUE) == FAIL)
3152 return FAIL;
3153 (void)incl(&curwin->w_cursor);
3154 }
3155 }
3156 else
3157#endif
3158 {
3159 /*
3160 * Move cursor forward one word and/or white area.
3161 */
3162 if (incl(&curwin->w_cursor) == -1)
3163 return FAIL;
3164 if (include != (cls() == 0))
3165 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003166 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 return FAIL;
3168 /*
3169 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003170 * the first character on the line.
3171 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003173 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 inclusive = FALSE;
3175 }
3176 else
3177 {
3178 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3179 return FAIL;
3180 }
3181 }
3182 --count;
3183 }
3184
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003185 if (include_white && (cls() != 0
3186 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 /*
3189 * If we don't include white space at the end, move the start
3190 * to include some white space there. This makes "daw" work
3191 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003192 * word). Also when "2daw" deletes "word." at the end of the line
3193 * (cursor is at start of next line).
3194 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
3196 pos = curwin->w_cursor; /* save cursor position */
3197 curwin->w_cursor = start_pos;
3198 if (oneleft() == OK)
3199 {
3200 back_in_line();
3201 if (cls() == 0 && curwin->w_cursor.col > 0)
3202 {
3203#ifdef FEAT_VISUAL
3204 if (VIsual_active)
3205 VIsual = curwin->w_cursor;
3206 else
3207#endif
3208 oap->start = curwin->w_cursor;
3209 }
3210 }
3211 curwin->w_cursor = pos; /* put cursor back at end */
3212 }
3213
3214#ifdef FEAT_VISUAL
3215 if (VIsual_active)
3216 {
3217 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3218 inc_cursor();
3219 if (VIsual_mode == 'V')
3220 {
3221 VIsual_mode = 'v';
3222 redraw_cmdline = TRUE; /* show mode later */
3223 }
3224 }
3225 else
3226#endif
3227 oap->inclusive = inclusive;
3228
3229 return OK;
3230}
3231
3232/*
3233 * Find sentence(s) under the cursor, cursor at end.
3234 * When Visual active, extend it by one or more sentences.
3235 */
3236 int
3237current_sent(oap, count, include)
3238 oparg_T *oap;
3239 long count;
3240 int include;
3241{
3242 pos_T start_pos;
3243 pos_T pos;
3244 int start_blank;
3245 int c;
3246 int at_start_sent;
3247 long ncount;
3248
3249 start_pos = curwin->w_cursor;
3250 pos = start_pos;
3251 findsent(FORWARD, 1L); /* Find start of next sentence. */
3252
3253#ifdef FEAT_VISUAL
3254 /*
3255 * When visual area is bigger than one character: Extend it.
3256 */
3257 if (VIsual_active && !equalpos(start_pos, VIsual))
3258 {
3259extend:
3260 if (lt(start_pos, VIsual))
3261 {
3262 /*
3263 * Cursor at start of Visual area.
3264 * Find out where we are:
3265 * - in the white space before a sentence
3266 * - in a sentence or just after it
3267 * - at the start of a sentence
3268 */
3269 at_start_sent = TRUE;
3270 decl(&pos);
3271 while (lt(pos, curwin->w_cursor))
3272 {
3273 c = gchar_pos(&pos);
3274 if (!vim_iswhite(c))
3275 {
3276 at_start_sent = FALSE;
3277 break;
3278 }
3279 incl(&pos);
3280 }
3281 if (!at_start_sent)
3282 {
3283 findsent(BACKWARD, 1L);
3284 if (equalpos(curwin->w_cursor, start_pos))
3285 at_start_sent = TRUE; /* exactly at start of sentence */
3286 else
3287 /* inside a sentence, go to its end (start of next) */
3288 findsent(FORWARD, 1L);
3289 }
3290 if (include) /* "as" gets twice as much as "is" */
3291 count *= 2;
3292 while (count--)
3293 {
3294 if (at_start_sent)
3295 find_first_blank(&curwin->w_cursor);
3296 c = gchar_cursor();
3297 if (!at_start_sent || (!include && !vim_iswhite(c)))
3298 findsent(BACKWARD, 1L);
3299 at_start_sent = !at_start_sent;
3300 }
3301 }
3302 else
3303 {
3304 /*
3305 * Cursor at end of Visual area.
3306 * Find out where we are:
3307 * - just before a sentence
3308 * - just before or in the white space before a sentence
3309 * - in a sentence
3310 */
3311 incl(&pos);
3312 at_start_sent = TRUE;
3313 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3314 {
3315 at_start_sent = FALSE;
3316 while (lt(pos, curwin->w_cursor))
3317 {
3318 c = gchar_pos(&pos);
3319 if (!vim_iswhite(c))
3320 {
3321 at_start_sent = TRUE;
3322 break;
3323 }
3324 incl(&pos);
3325 }
3326 if (at_start_sent) /* in the sentence */
3327 findsent(BACKWARD, 1L);
3328 else /* in/before white before a sentence */
3329 curwin->w_cursor = start_pos;
3330 }
3331
3332 if (include) /* "as" gets twice as much as "is" */
3333 count *= 2;
3334 findsent_forward(count, at_start_sent);
3335 if (*p_sel == 'e')
3336 ++curwin->w_cursor.col;
3337 }
3338 return OK;
3339 }
3340#endif
3341
3342 /*
3343 * If cursor started on blank, check if it is just before the start of the
3344 * next sentence.
3345 */
3346 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3347 incl(&pos);
3348 if (equalpos(pos, curwin->w_cursor))
3349 {
3350 start_blank = TRUE;
3351 find_first_blank(&start_pos); /* go back to first blank */
3352 }
3353 else
3354 {
3355 start_blank = FALSE;
3356 findsent(BACKWARD, 1L);
3357 start_pos = curwin->w_cursor;
3358 }
3359 if (include)
3360 ncount = count * 2;
3361 else
3362 {
3363 ncount = count;
3364 if (start_blank)
3365 --ncount;
3366 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003367 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 findsent_forward(ncount, TRUE);
3369 else
3370 decl(&curwin->w_cursor);
3371
3372 if (include)
3373 {
3374 /*
3375 * If the blank in front of the sentence is included, exclude the
3376 * blanks at the end of the sentence, go back to the first blank.
3377 * If there are no trailing blanks, try to include leading blanks.
3378 */
3379 if (start_blank)
3380 {
3381 find_first_blank(&curwin->w_cursor);
3382 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3383 if (vim_iswhite(c))
3384 decl(&curwin->w_cursor);
3385 }
3386 else if (c = gchar_cursor(), !vim_iswhite(c))
3387 find_first_blank(&start_pos);
3388 }
3389
3390#ifdef FEAT_VISUAL
3391 if (VIsual_active)
3392 {
3393 /* avoid getting stuck with "is" on a single space before a sent. */
3394 if (equalpos(start_pos, curwin->w_cursor))
3395 goto extend;
3396 if (*p_sel == 'e')
3397 ++curwin->w_cursor.col;
3398 VIsual = start_pos;
3399 VIsual_mode = 'v';
3400 redraw_curbuf_later(INVERTED); /* update the inversion */
3401 }
3402 else
3403#endif
3404 {
3405 /* include a newline after the sentence, if there is one */
3406 if (incl(&curwin->w_cursor) == -1)
3407 oap->inclusive = TRUE;
3408 else
3409 oap->inclusive = FALSE;
3410 oap->start = start_pos;
3411 oap->motion_type = MCHAR;
3412 }
3413 return OK;
3414}
3415
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003416/*
3417 * Find block under the cursor, cursor at end.
3418 * "what" and "other" are two matching parenthesis/paren/etc.
3419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 int
3421current_block(oap, count, include, what, other)
3422 oparg_T *oap;
3423 long count;
3424 int include; /* TRUE == include white space */
3425 int what; /* '(', '{', etc. */
3426 int other; /* ')', '}', etc. */
3427{
3428 pos_T old_pos;
3429 pos_T *pos = NULL;
3430 pos_T start_pos;
3431 pos_T *end_pos;
3432 pos_T old_start, old_end;
3433 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003434 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
3436 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003437 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 old_start = old_end;
3439
3440 /*
3441 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3442 */
3443#ifdef FEAT_VISUAL
3444 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3445#endif
3446 {
3447 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003448 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 while (inindent(1))
3450 if (inc_cursor() != 0)
3451 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003452 if (gchar_cursor() == what)
3453 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 ++curwin->w_cursor.col;
3455 }
3456#ifdef FEAT_VISUAL
3457 else if (lt(VIsual, curwin->w_cursor))
3458 {
3459 old_start = VIsual;
3460 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3461 }
3462 else
3463 old_end = VIsual;
3464#endif
3465
3466 /*
3467 * Search backwards for unclosed '(', '{', etc..
3468 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003469 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 */
3471 save_cpo = p_cpo;
3472 p_cpo = (char_u *)"%";
3473 while (count-- > 0)
3474 {
3475 if ((pos = findmatch(NULL, what)) == NULL)
3476 break;
3477 curwin->w_cursor = *pos;
3478 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3479 }
3480 p_cpo = save_cpo;
3481
3482 /*
3483 * Search for matching ')', '}', etc.
3484 * Put this position in curwin->w_cursor.
3485 */
3486 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3487 {
3488 curwin->w_cursor = old_pos;
3489 return FAIL;
3490 }
3491 curwin->w_cursor = *end_pos;
3492
3493 /*
3494 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3495 * If the ending '}' is only preceded by indent, skip that indent.
3496 * But only if the resulting area is not smaller than what we started with.
3497 */
3498 while (!include)
3499 {
3500 incl(&start_pos);
3501 sol = (curwin->w_cursor.col == 0);
3502 decl(&curwin->w_cursor);
3503 if (what == '{')
3504 while (inindent(1))
3505 {
3506 sol = TRUE;
3507 if (decl(&curwin->w_cursor) != 0)
3508 break;
3509 }
3510#ifdef FEAT_VISUAL
3511 /*
3512 * In Visual mode, when the resulting area is not bigger than what we
3513 * started with, extend it to the next block, and then exclude again.
3514 */
3515 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3516 && VIsual_active)
3517 {
3518 curwin->w_cursor = old_start;
3519 decl(&curwin->w_cursor);
3520 if ((pos = findmatch(NULL, what)) == NULL)
3521 {
3522 curwin->w_cursor = old_pos;
3523 return FAIL;
3524 }
3525 start_pos = *pos;
3526 curwin->w_cursor = *pos;
3527 if ((end_pos = findmatch(NULL, other)) == NULL)
3528 {
3529 curwin->w_cursor = old_pos;
3530 return FAIL;
3531 }
3532 curwin->w_cursor = *end_pos;
3533 }
3534 else
3535#endif
3536 break;
3537 }
3538
3539#ifdef FEAT_VISUAL
3540 if (VIsual_active)
3541 {
3542 if (*p_sel == 'e')
3543 ++curwin->w_cursor.col;
3544 if (sol)
3545 inc(&curwin->w_cursor); /* include the line break */
3546 VIsual = start_pos;
3547 VIsual_mode = 'v';
3548 redraw_curbuf_later(INVERTED); /* update the inversion */
3549 showmode();
3550 }
3551 else
3552#endif
3553 {
3554 oap->start = start_pos;
3555 oap->motion_type = MCHAR;
3556 if (sol)
3557 {
3558 incl(&curwin->w_cursor);
3559 oap->inclusive = FALSE;
3560 }
3561 else
3562 oap->inclusive = TRUE;
3563 }
3564
3565 return OK;
3566}
3567
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003568static int in_html_tag __ARGS((int));
3569
3570/*
3571 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3572 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3573 */
3574 static int
3575in_html_tag(end_tag)
3576 int end_tag;
3577{
3578 char_u *line = ml_get_curline();
3579 char_u *p;
3580 int c;
3581 int lc = NUL;
3582 pos_T pos;
3583
3584#ifdef FEAT_MBYTE
3585 if (enc_dbcs)
3586 {
3587 char_u *lp = NULL;
3588
3589 /* We search forward until the cursor, because searching backwards is
3590 * very slow for DBCS encodings. */
3591 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3592 if (*p == '>' || *p == '<')
3593 {
3594 lc = *p;
3595 lp = p;
3596 }
3597 if (*p != '<') /* check for '<' under cursor */
3598 {
3599 if (lc != '<')
3600 return FALSE;
3601 p = lp;
3602 }
3603 }
3604 else
3605#endif
3606 {
3607 for (p = line + curwin->w_cursor.col; p > line; )
3608 {
3609 if (*p == '<') /* find '<' under/before cursor */
3610 break;
3611 mb_ptr_back(line, p);
3612 if (*p == '>') /* find '>' before cursor */
3613 break;
3614 }
3615 if (*p != '<')
3616 return FALSE;
3617 }
3618
3619 pos.lnum = curwin->w_cursor.lnum;
3620 pos.col = p - line;
3621
3622 mb_ptr_adv(p);
3623 if (end_tag)
3624 /* check that there is a '/' after the '<' */
3625 return *p == '/';
3626
3627 /* check that there is no '/' after the '<' */
3628 if (*p == '/')
3629 return FALSE;
3630
3631 /* check that the matching '>' is not preceded by '/' */
3632 for (;;)
3633 {
3634 if (inc(&pos) < 0)
3635 return FALSE;
3636 c = *ml_get_pos(&pos);
3637 if (c == '>')
3638 break;
3639 lc = c;
3640 }
3641 return lc != '/';
3642}
3643
3644/*
3645 * Find tag block under the cursor, cursor at end.
3646 */
3647 int
3648current_tagblock(oap, count_arg, include)
3649 oparg_T *oap;
3650 long count_arg;
3651 int include; /* TRUE == include white space */
3652{
3653 long count = count_arg;
3654 long n;
3655 pos_T old_pos;
3656 pos_T start_pos;
3657 pos_T end_pos;
3658 pos_T old_start, old_end;
3659 char_u *spat, *epat;
3660 char_u *p;
3661 char_u *cp;
3662 int len;
3663 int r;
3664 int do_include = include;
3665 int save_p_ws = p_ws;
3666 int retval = FAIL;
3667
3668 p_ws = FALSE;
3669
3670 old_pos = curwin->w_cursor;
3671 old_end = curwin->w_cursor; /* remember where we started */
3672 old_start = old_end;
3673
3674 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003675 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003676 */
3677#ifdef FEAT_VISUAL
3678 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3679#endif
3680 {
3681 setpcmark();
3682
3683 /* ignore indent */
3684 while (inindent(1))
3685 if (inc_cursor() != 0)
3686 break;
3687
3688 if (in_html_tag(FALSE))
3689 {
3690 /* cursor on start tag, move to just after it */
3691 while (*ml_get_cursor() != '>')
3692 if (inc_cursor() < 0)
3693 break;
3694 }
3695 else if (in_html_tag(TRUE))
3696 {
3697 /* cursor on end tag, move to just before it */
3698 while (*ml_get_cursor() != '<')
3699 if (dec_cursor() < 0)
3700 break;
3701 dec_cursor();
3702 old_end = curwin->w_cursor;
3703 }
3704 }
3705#ifdef FEAT_VISUAL
3706 else if (lt(VIsual, curwin->w_cursor))
3707 {
3708 old_start = VIsual;
3709 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3710 }
3711 else
3712 old_end = VIsual;
3713#endif
3714
3715again:
3716 /*
3717 * Search backwards for unclosed "<aaa>".
3718 * Put this position in start_pos.
3719 */
3720 for (n = 0; n < count; ++n)
3721 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003722 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003723 (char_u *)"",
3724 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0) <= 0)
3725 {
3726 curwin->w_cursor = old_pos;
3727 goto theend;
3728 }
3729 }
3730 start_pos = curwin->w_cursor;
3731
3732 /*
3733 * Search for matching "</aaa>". First isolate the "aaa".
3734 */
3735 inc_cursor();
3736 p = ml_get_cursor();
3737 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3738 ;
3739 len = cp - p;
3740 if (len == 0)
3741 {
3742 curwin->w_cursor = old_pos;
3743 goto theend;
3744 }
3745 spat = alloc(len + 29);
3746 epat = alloc(len + 9);
3747 if (spat == NULL || epat == NULL)
3748 {
3749 vim_free(spat);
3750 vim_free(epat);
3751 curwin->w_cursor = old_pos;
3752 goto theend;
3753 }
3754 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3755 sprintf((char *)epat, "</%.*s>\\c", len, p);
3756
3757 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"", 0);
3758
3759 vim_free(spat);
3760 vim_free(epat);
3761
3762 if (r < 1 || lt(curwin->w_cursor, old_end))
3763 {
3764 /* Can't find other end or it's before the previous end. Could be a
3765 * HTML tag that doesn't have a matching end. Search backwards for
3766 * another starting tag. */
3767 count = 1;
3768 curwin->w_cursor = start_pos;
3769 goto again;
3770 }
3771
3772 if (do_include || r < 1)
3773 {
3774 /* Include up to the '>'. */
3775 while (*ml_get_cursor() != '>')
3776 if (inc_cursor() < 0)
3777 break;
3778 }
3779 else
3780 {
3781 /* Exclude the '<' of the end tag. */
3782 if (*ml_get_cursor() == '<')
3783 dec_cursor();
3784 }
3785 end_pos = curwin->w_cursor;
3786
3787 if (!do_include)
3788 {
3789 /* Exclude the start tag. */
3790 curwin->w_cursor = start_pos;
3791 while (inc_cursor() >= 0)
3792 if (*ml_get_cursor() == '>' && lt(curwin->w_cursor, end_pos))
3793 {
3794 inc_cursor();
3795 start_pos = curwin->w_cursor;
3796 break;
3797 }
3798 curwin->w_cursor = end_pos;
3799
Bram Moolenaar45360022005-07-21 21:08:21 +00003800 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003801 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003802 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003803 {
3804 do_include = TRUE;
3805 curwin->w_cursor = old_start;
3806 count = count_arg;
3807 goto again;
3808 }
3809 }
3810
3811#ifdef FEAT_VISUAL
3812 if (VIsual_active)
3813 {
3814 if (*p_sel == 'e')
3815 ++curwin->w_cursor.col;
3816 VIsual = start_pos;
3817 VIsual_mode = 'v';
3818 redraw_curbuf_later(INVERTED); /* update the inversion */
3819 showmode();
3820 }
3821 else
3822#endif
3823 {
3824 oap->start = start_pos;
3825 oap->motion_type = MCHAR;
3826 oap->inclusive = TRUE;
3827 }
3828 retval = OK;
3829
3830theend:
3831 p_ws = save_p_ws;
3832 return retval;
3833}
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 int
3836current_par(oap, count, include, type)
3837 oparg_T *oap;
3838 long count;
3839 int include; /* TRUE == include white space */
3840 int type; /* 'p' for paragraph, 'S' for section */
3841{
3842 linenr_T start_lnum;
3843 linenr_T end_lnum;
3844 int white_in_front;
3845 int dir;
3846 int start_is_white;
3847 int prev_start_is_white;
3848 int retval = OK;
3849 int do_white = FALSE;
3850 int t;
3851 int i;
3852
3853 if (type == 'S') /* not implemented yet */
3854 return FAIL;
3855
3856 start_lnum = curwin->w_cursor.lnum;
3857
3858#ifdef FEAT_VISUAL
3859 /*
3860 * When visual area is more than one line: extend it.
3861 */
3862 if (VIsual_active && start_lnum != VIsual.lnum)
3863 {
3864extend:
3865 if (start_lnum < VIsual.lnum)
3866 dir = BACKWARD;
3867 else
3868 dir = FORWARD;
3869 for (i = count; --i >= 0; )
3870 {
3871 if (start_lnum ==
3872 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3873 {
3874 retval = FAIL;
3875 break;
3876 }
3877
3878 prev_start_is_white = -1;
3879 for (t = 0; t < 2; ++t)
3880 {
3881 start_lnum += dir;
3882 start_is_white = linewhite(start_lnum);
3883 if (prev_start_is_white == start_is_white)
3884 {
3885 start_lnum -= dir;
3886 break;
3887 }
3888 for (;;)
3889 {
3890 if (start_lnum == (dir == BACKWARD
3891 ? 1 : curbuf->b_ml.ml_line_count))
3892 break;
3893 if (start_is_white != linewhite(start_lnum + dir)
3894 || (!start_is_white
3895 && startPS(start_lnum + (dir > 0
3896 ? 1 : 0), 0, 0)))
3897 break;
3898 start_lnum += dir;
3899 }
3900 if (!include)
3901 break;
3902 if (start_lnum == (dir == BACKWARD
3903 ? 1 : curbuf->b_ml.ml_line_count))
3904 break;
3905 prev_start_is_white = start_is_white;
3906 }
3907 }
3908 curwin->w_cursor.lnum = start_lnum;
3909 curwin->w_cursor.col = 0;
3910 return retval;
3911 }
3912#endif
3913
3914 /*
3915 * First move back to the start_lnum of the paragraph or white lines
3916 */
3917 white_in_front = linewhite(start_lnum);
3918 while (start_lnum > 1)
3919 {
3920 if (white_in_front) /* stop at first white line */
3921 {
3922 if (!linewhite(start_lnum - 1))
3923 break;
3924 }
3925 else /* stop at first non-white line of start of paragraph */
3926 {
3927 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
3928 break;
3929 }
3930 --start_lnum;
3931 }
3932
3933 /*
3934 * Move past the end of any white lines.
3935 */
3936 end_lnum = start_lnum;
3937 while (linewhite(end_lnum) && end_lnum < curbuf->b_ml.ml_line_count)
3938 ++end_lnum;
3939
3940 --end_lnum;
3941 i = count;
3942 if (!include && white_in_front)
3943 --i;
3944 while (i--)
3945 {
3946 if (end_lnum == curbuf->b_ml.ml_line_count)
3947 return FAIL;
3948
3949 if (!include)
3950 do_white = linewhite(end_lnum + 1);
3951
3952 if (include || !do_white)
3953 {
3954 ++end_lnum;
3955 /*
3956 * skip to end of paragraph
3957 */
3958 while (end_lnum < curbuf->b_ml.ml_line_count
3959 && !linewhite(end_lnum + 1)
3960 && !startPS(end_lnum + 1, 0, 0))
3961 ++end_lnum;
3962 }
3963
3964 if (i == 0 && white_in_front && include)
3965 break;
3966
3967 /*
3968 * skip to end of white lines after paragraph
3969 */
3970 if (include || do_white)
3971 while (end_lnum < curbuf->b_ml.ml_line_count
3972 && linewhite(end_lnum + 1))
3973 ++end_lnum;
3974 }
3975
3976 /*
3977 * If there are no empty lines at the end, try to find some empty lines at
3978 * the start (unless that has been done already).
3979 */
3980 if (!white_in_front && !linewhite(end_lnum) && include)
3981 while (start_lnum > 1 && linewhite(start_lnum - 1))
3982 --start_lnum;
3983
3984#ifdef FEAT_VISUAL
3985 if (VIsual_active)
3986 {
3987 /* Problem: when doing "Vipipip" nothing happens in a single white
3988 * line, we get stuck there. Trap this here. */
3989 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
3990 goto extend;
3991 VIsual.lnum = start_lnum;
3992 VIsual_mode = 'V';
3993 redraw_curbuf_later(INVERTED); /* update the inversion */
3994 showmode();
3995 }
3996 else
3997#endif
3998 {
3999 oap->start.lnum = start_lnum;
4000 oap->start.col = 0;
4001 oap->motion_type = MLINE;
4002 }
4003 curwin->w_cursor.lnum = end_lnum;
4004 curwin->w_cursor.col = 0;
4005
4006 return OK;
4007}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004008
4009static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4010static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4011
4012/*
4013 * Search quote char from string line[col].
4014 * Quote character escaped by one of the characters in "escape" is not counted
4015 * as a quote.
4016 * Returns column number of "quotechar" or -1 when not found.
4017 */
4018 static int
4019find_next_quote(line, col, quotechar, escape)
4020 char_u *line;
4021 int col;
4022 int quotechar;
4023 char_u *escape; /* escape characters, can be NULL */
4024{
4025 int c;
4026
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004027 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004028 {
4029 c = line[col];
4030 if (c == NUL)
4031 return -1;
4032 else if (escape != NULL && vim_strchr(escape, c))
4033 ++col;
4034 else if (c == quotechar)
4035 break;
4036#ifdef FEAT_MBYTE
4037 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004038 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004041 ++col;
4042 }
4043 return col;
4044}
4045
4046/*
4047 * Search backwards in "line" from column "col_start" to find "quotechar".
4048 * Quote character escaped by one of the characters in "escape" is not counted
4049 * as a quote.
4050 * Return the found column or zero.
4051 */
4052 static int
4053find_prev_quote(line, col_start, quotechar, escape)
4054 char_u *line;
4055 int col_start;
4056 int quotechar;
4057 char_u *escape; /* escape characters, can be NULL */
4058{
4059 int n;
4060
4061 while (col_start > 0)
4062 {
4063 --col_start;
4064#ifdef FEAT_MBYTE
4065 col_start -= (*mb_head_off)(line, line + col_start);
4066#endif
4067 n = 0;
4068 if (escape != NULL)
4069 while (col_start - n > 0 && vim_strchr(escape,
4070 line[col_start - n - 1]) != NULL)
4071 ++n;
4072 if (n & 1)
4073 col_start -= n; /* uneven number of escape chars, skip it */
4074 else if (line[col_start] == quotechar)
4075 break;
4076 }
4077 return col_start;
4078}
4079
4080/*
4081 * Find quote under the cursor, cursor at end.
4082 * Returns TRUE if found, else FALSE.
4083 */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004084/*ARGSUSED*/
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004085 int
4086current_quote(oap, count, include, quotechar)
4087 oparg_T *oap;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004088 long count; /* not used */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004089 int include; /* TRUE == include quote char */
4090 int quotechar; /* Quote character */
4091{
4092 char_u *line = ml_get_curline();
4093 int col_end;
4094 int col_start = curwin->w_cursor.col;
4095 int inclusive = FALSE;
4096#ifdef FEAT_VISUAL
4097 int vis_empty = TRUE; /* Visual selection <= 1 char */
4098 int vis_bef_curs = FALSE; /* Visual starts before cursor */
4099
4100 /* Correct cursor when 'selection' is exclusive */
4101 if (VIsual_active)
4102 {
4103 if (*p_sel == 'e' && vis_bef_curs)
4104 dec_cursor();
4105 vis_empty = equalpos(VIsual, curwin->w_cursor);
4106 vis_bef_curs = lt(VIsual, curwin->w_cursor);
4107 }
4108 if (!vis_empty && line[col_start] == quotechar)
4109 {
4110 /* Already selecting something and on a quote character. Find the
4111 * next quoted string. */
4112 if (vis_bef_curs)
4113 {
4114 /* Assume we are on a closing quote: move to after the next
4115 * opening quote. */
4116 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4117 if (col_start < 0)
4118 return FALSE;
4119 col_end = find_next_quote(line, col_start + 1, quotechar,
4120 curbuf->b_p_qe);
4121 if (col_end < 0)
4122 {
4123 /* We were on a starting quote perhaps? */
4124 col_end = col_start;
4125 col_start = curwin->w_cursor.col;
4126 }
4127 }
4128 else
4129 {
4130 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4131 if (line[col_end] != quotechar)
4132 return FALSE;
4133 col_start = find_prev_quote(line, col_end, quotechar,
4134 curbuf->b_p_qe);
4135 if (line[col_start] != quotechar)
4136 {
4137 /* We were on an ending quote perhaps? */
4138 col_start = col_end;
4139 col_end = curwin->w_cursor.col;
4140 }
4141 }
4142 }
4143 else
4144#endif
4145
4146 if (line[col_start] == quotechar
4147#ifdef FEAT_VISUAL
4148 || !vis_empty
4149#endif
4150 )
4151 {
4152 int first_col = col_start;
4153
4154#ifdef FEAT_VISUAL
4155 if (!vis_empty)
4156 {
4157 if (vis_bef_curs)
4158 first_col = find_next_quote(line, col_start, quotechar, NULL);
4159 else
4160 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4161 }
4162#endif
4163 /* The cursor is on a quote, we don't know if it's the opening or
4164 * closing quote. Search from the start of the line to find out.
4165 * Also do this when there is a Visual area, a' may leave the cursor
4166 * in between two strings. */
4167 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004168 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004169 {
4170 /* Find open quote character. */
4171 col_start = find_next_quote(line, col_start, quotechar, NULL);
4172 if (col_start < 0 || col_start > first_col)
4173 return FALSE;
4174 /* Find close quote character. */
4175 col_end = find_next_quote(line, col_start + 1, quotechar,
4176 curbuf->b_p_qe);
4177 if (col_end < 0)
4178 return FALSE;
4179 /* If is cursor between start and end quote character, it is
4180 * target text object. */
4181 if (col_start <= first_col && first_col <= col_end)
4182 break;
4183 col_start = col_end + 1;
4184 }
4185 }
4186 else
4187 {
4188 /* Search backward for a starting quote. */
4189 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4190 if (line[col_start] != quotechar)
4191 {
4192 /* No quote before the cursor, look after the cursor. */
4193 col_start = find_next_quote(line, col_start, quotechar, NULL);
4194 if (col_start < 0)
4195 return FALSE;
4196 }
4197
4198 /* Find close quote character. */
4199 col_end = find_next_quote(line, col_start + 1, quotechar,
4200 curbuf->b_p_qe);
4201 if (col_end < 0)
4202 return FALSE;
4203 }
4204
4205 /* When "include" is TRUE, include spaces after closing quote or before
4206 * the starting quote. */
4207 if (include)
4208 {
4209 if (vim_iswhite(line[col_end + 1]))
4210 while (vim_iswhite(line[col_end + 1]))
4211 ++col_end;
4212 else
4213 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4214 --col_start;
4215 }
4216
4217 /* Set start position */
4218 if (!include)
4219 ++col_start;
4220 curwin->w_cursor.col = col_start;
4221#ifdef FEAT_VISUAL
4222 if (VIsual_active)
4223 {
4224 if (vis_empty)
4225 {
4226 VIsual = curwin->w_cursor;
4227 redraw_curbuf_later(INVERTED);
4228 }
4229 }
4230 else
4231#endif
4232 {
4233 oap->start = curwin->w_cursor;
4234 oap->motion_type = MCHAR;
4235 }
4236
4237 /* Set end position. */
4238 curwin->w_cursor.col = col_end;
4239 if (include && inc_cursor() == 2)
4240 inclusive = TRUE;
4241#ifdef FEAT_VISUAL
4242 if (VIsual_active)
4243 {
4244 if (vis_empty || vis_bef_curs)
4245 {
4246 /* decrement cursor when 'selection' is not exclusive */
4247 if (*p_sel != 'e')
4248 dec_cursor();
4249 }
4250 else
4251 {
4252 /* Cursor is at start of Visual area. */
4253 curwin->w_cursor.col = col_start;
4254 }
4255 if (VIsual_mode == 'V')
4256 {
4257 VIsual_mode = 'v';
4258 redraw_cmdline = TRUE; /* show mode later */
4259 }
4260 }
4261 else
4262#endif
4263 {
4264 /* Set inclusive and other oap's flags. */
4265 oap->inclusive = inclusive;
4266 }
4267
4268 return OK;
4269}
4270
4271#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4274 || defined(PROTO)
4275/*
4276 * return TRUE if line 'lnum' is empty or has white chars only.
4277 */
4278 int
4279linewhite(lnum)
4280 linenr_T lnum;
4281{
4282 char_u *p;
4283
4284 p = skipwhite(ml_get(lnum));
4285 return (*p == NUL);
4286}
4287#endif
4288
4289#if defined(FEAT_FIND_ID) || defined(PROTO)
4290/*
4291 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 */
4294/*ARGSUSED*/
4295 void
4296find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4297 type, count, action, start_lnum, end_lnum)
4298 char_u *ptr; /* pointer to search pattern */
4299 int dir; /* direction of expansion */
4300 int len; /* length of search pattern */
4301 int whole; /* match whole words only */
4302 int skip_comments; /* don't match inside comments */
4303 int type; /* Type of search; are we looking for a type?
4304 a macro? */
4305 long count;
4306 int action; /* What to do when we find it */
4307 linenr_T start_lnum; /* first line to start searching */
4308 linenr_T end_lnum; /* last line for searching */
4309{
4310 SearchedFile *files; /* Stack of included files */
4311 SearchedFile *bigger; /* When we need more space */
4312 int max_path_depth = 50;
4313 long match_count = 1;
4314
4315 char_u *pat;
4316 char_u *new_fname;
4317 char_u *curr_fname = curbuf->b_fname;
4318 char_u *prev_fname = NULL;
4319 linenr_T lnum;
4320 int depth;
4321 int depth_displayed; /* For type==CHECK_PATH */
4322 int old_files;
4323 int already_searched;
4324 char_u *file_line;
4325 char_u *line;
4326 char_u *p;
4327 char_u save_char;
4328 int define_matched;
4329 regmatch_T regmatch;
4330 regmatch_T incl_regmatch;
4331 regmatch_T def_regmatch;
4332 int matched = FALSE;
4333 int did_show = FALSE;
4334 int found = FALSE;
4335 int i;
4336 char_u *already = NULL;
4337 char_u *startp = NULL;
4338#ifdef RISCOS
4339 int previous_munging = __riscosify_control;
4340#endif
4341#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4342 win_T *curwin_save = NULL;
4343#endif
4344
4345 regmatch.regprog = NULL;
4346 incl_regmatch.regprog = NULL;
4347 def_regmatch.regprog = NULL;
4348
4349 file_line = alloc(LSIZE);
4350 if (file_line == NULL)
4351 return;
4352
4353#ifdef RISCOS
4354 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004355 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#endif
4357
4358 if (type != CHECK_PATH && type != FIND_DEFINE
4359#ifdef FEAT_INS_EXPAND
4360 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4361 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004362 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363#endif
4364 )
4365 {
4366 pat = alloc(len + 5);
4367 if (pat == NULL)
4368 goto fpip_end;
4369 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4370 /* ignore case according to p_ic, p_scs and pat */
4371 regmatch.rm_ic = ignorecase(pat);
4372 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4373 vim_free(pat);
4374 if (regmatch.regprog == NULL)
4375 goto fpip_end;
4376 }
4377 if (*curbuf->b_p_inc != NUL || *p_inc != NUL)
4378 {
4379 incl_regmatch.regprog = vim_regcomp(*curbuf->b_p_inc == NUL
4380 ? p_inc : curbuf->b_p_inc, p_magic ? RE_MAGIC : 0);
4381 if (incl_regmatch.regprog == NULL)
4382 goto fpip_end;
4383 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4384 }
4385 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4386 {
4387 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4388 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4389 if (def_regmatch.regprog == NULL)
4390 goto fpip_end;
4391 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4392 }
4393 files = (SearchedFile *)lalloc_clear((long_u)
4394 (max_path_depth * sizeof(SearchedFile)), TRUE);
4395 if (files == NULL)
4396 goto fpip_end;
4397 old_files = max_path_depth;
4398 depth = depth_displayed = -1;
4399
4400 lnum = start_lnum;
4401 if (end_lnum > curbuf->b_ml.ml_line_count)
4402 end_lnum = curbuf->b_ml.ml_line_count;
4403 if (lnum > end_lnum) /* do at least one line */
4404 lnum = end_lnum;
4405 line = ml_get(lnum);
4406
4407 for (;;)
4408 {
4409 if (incl_regmatch.regprog != NULL
4410 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4411 {
4412 new_fname = file_name_in_line(incl_regmatch.endp[0],
4413 0, FNAME_EXP|FNAME_INCL|FNAME_REL, 1L,
4414 curr_fname == curbuf->b_fname
4415 ? curbuf->b_ffname : curr_fname);
4416 already_searched = FALSE;
4417 if (new_fname != NULL)
4418 {
4419 /* Check whether we have already searched in this file */
4420 for (i = 0;; i++)
4421 {
4422 if (i == depth + 1)
4423 i = old_files;
4424 if (i == max_path_depth)
4425 break;
4426 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4427 {
4428 if (type != CHECK_PATH &&
4429 action == ACTION_SHOW_ALL && files[i].matched)
4430 {
4431 msg_putchar('\n'); /* cursor below last one */
4432 if (!got_int) /* don't display if 'q'
4433 typed at "--more--"
4434 mesage */
4435 {
4436 msg_home_replace_hl(new_fname);
4437 MSG_PUTS(_(" (includes previously listed match)"));
4438 prev_fname = NULL;
4439 }
4440 }
4441 vim_free(new_fname);
4442 new_fname = NULL;
4443 already_searched = TRUE;
4444 break;
4445 }
4446 }
4447 }
4448
4449 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4450 || (new_fname == NULL && !already_searched)))
4451 {
4452 if (did_show)
4453 msg_putchar('\n'); /* cursor below last one */
4454 else
4455 {
4456 gotocmdline(TRUE); /* cursor at status line */
4457 MSG_PUTS_TITLE(_("--- Included files "));
4458 if (action != ACTION_SHOW_ALL)
4459 MSG_PUTS_TITLE(_("not found "));
4460 MSG_PUTS_TITLE(_("in path ---\n"));
4461 }
4462 did_show = TRUE;
4463 while (depth_displayed < depth && !got_int)
4464 {
4465 ++depth_displayed;
4466 for (i = 0; i < depth_displayed; i++)
4467 MSG_PUTS(" ");
4468 msg_home_replace(files[depth_displayed].name);
4469 MSG_PUTS(" -->\n");
4470 }
4471 if (!got_int) /* don't display if 'q' typed
4472 for "--more--" message */
4473 {
4474 for (i = 0; i <= depth_displayed; i++)
4475 MSG_PUTS(" ");
4476 if (new_fname != NULL)
4477 {
4478 /* using "new_fname" is more reliable, e.g., when
4479 * 'includeexpr' is set. */
4480 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4481 }
4482 else
4483 {
4484 /*
4485 * Isolate the file name.
4486 * Include the surrounding "" or <> if present.
4487 */
4488 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4489 ;
4490 for (i = 0; vim_isfilec(p[i]); i++)
4491 ;
4492 if (i == 0)
4493 {
4494 /* Nothing found, use the rest of the line. */
4495 p = incl_regmatch.endp[0];
4496 i = STRLEN(p);
4497 }
4498 else
4499 {
4500 if (p[-1] == '"' || p[-1] == '<')
4501 {
4502 --p;
4503 ++i;
4504 }
4505 if (p[i] == '"' || p[i] == '>')
4506 ++i;
4507 }
4508 save_char = p[i];
4509 p[i] = NUL;
4510 msg_outtrans_attr(p, hl_attr(HLF_D));
4511 p[i] = save_char;
4512 }
4513
4514 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4515 {
4516 if (already_searched)
4517 MSG_PUTS(_(" (Already listed)"));
4518 else
4519 MSG_PUTS(_(" NOT FOUND"));
4520 }
4521 }
4522 out_flush(); /* output each line directly */
4523 }
4524
4525 if (new_fname != NULL)
4526 {
4527 /* Push the new file onto the file stack */
4528 if (depth + 1 == old_files)
4529 {
4530 bigger = (SearchedFile *)lalloc((long_u)(
4531 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4532 if (bigger != NULL)
4533 {
4534 for (i = 0; i <= depth; i++)
4535 bigger[i] = files[i];
4536 for (i = depth + 1; i < old_files + max_path_depth; i++)
4537 {
4538 bigger[i].fp = NULL;
4539 bigger[i].name = NULL;
4540 bigger[i].lnum = 0;
4541 bigger[i].matched = FALSE;
4542 }
4543 for (i = old_files; i < max_path_depth; i++)
4544 bigger[i + max_path_depth] = files[i];
4545 old_files += max_path_depth;
4546 max_path_depth *= 2;
4547 vim_free(files);
4548 files = bigger;
4549 }
4550 }
4551 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4552 == NULL)
4553 vim_free(new_fname);
4554 else
4555 {
4556 if (++depth == old_files)
4557 {
4558 /*
4559 * lalloc() for 'bigger' must have failed above. We
4560 * will forget one of our already visited files now.
4561 */
4562 vim_free(files[old_files].name);
4563 ++old_files;
4564 }
4565 files[depth].name = curr_fname = new_fname;
4566 files[depth].lnum = 0;
4567 files[depth].matched = FALSE;
4568#ifdef FEAT_INS_EXPAND
4569 if (action == ACTION_EXPAND)
4570 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004571 vim_snprintf((char*)IObuff, IOSIZE,
4572 _("Scanning included file: %s"),
4573 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4575 }
4576#endif
4577 }
4578 }
4579 }
4580 else
4581 {
4582 /*
4583 * Check if the line is a define (type == FIND_DEFINE)
4584 */
4585 p = line;
4586search_line:
4587 define_matched = FALSE;
4588 if (def_regmatch.regprog != NULL
4589 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4590 {
4591 /*
4592 * Pattern must be first identifier after 'define', so skip
4593 * to that position before checking for match of pattern. Also
4594 * don't let it match beyond the end of this identifier.
4595 */
4596 p = def_regmatch.endp[0];
4597 while (*p && !vim_iswordc(*p))
4598 p++;
4599 define_matched = TRUE;
4600 }
4601
4602 /*
4603 * Look for a match. Don't do this if we are looking for a
4604 * define and this line didn't match define_prog above.
4605 */
4606 if (def_regmatch.regprog == NULL || define_matched)
4607 {
4608 if (define_matched
4609#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004610 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611#endif
4612 )
4613 {
4614 /* compare the first "len" chars from "ptr" */
4615 startp = skipwhite(p);
4616 if (p_ic)
4617 matched = !MB_STRNICMP(startp, ptr, len);
4618 else
4619 matched = !STRNCMP(startp, ptr, len);
4620 if (matched && define_matched && whole
4621 && vim_iswordc(startp[len]))
4622 matched = FALSE;
4623 }
4624 else if (regmatch.regprog != NULL
4625 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4626 {
4627 matched = TRUE;
4628 startp = regmatch.startp[0];
4629 /*
4630 * Check if the line is not a comment line (unless we are
4631 * looking for a define). A line starting with "# define"
4632 * is not considered to be a comment line.
4633 */
4634 if (!define_matched && skip_comments)
4635 {
4636#ifdef FEAT_COMMENTS
4637 if ((*line != '#' ||
4638 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4639 && get_leader_len(line, NULL, FALSE))
4640 matched = FALSE;
4641
4642 /*
4643 * Also check for a "/ *" or "/ /" before the match.
4644 * Skips lines like "int backwards; / * normal index
4645 * * /" when looking for "normal".
4646 * Note: Doesn't skip "/ *" in comments.
4647 */
4648 p = skipwhite(line);
4649 if (matched
4650 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4651#endif
4652 for (p = line; *p && p < startp; ++p)
4653 {
4654 if (matched
4655 && p[0] == '/'
4656 && (p[1] == '*' || p[1] == '/'))
4657 {
4658 matched = FALSE;
4659 /* After "//" all text is comment */
4660 if (p[1] == '/')
4661 break;
4662 ++p;
4663 }
4664 else if (!matched && p[0] == '*' && p[1] == '/')
4665 {
4666 /* Can find match after "* /". */
4667 matched = TRUE;
4668 ++p;
4669 }
4670 }
4671 }
4672 }
4673 }
4674 }
4675 if (matched)
4676 {
4677#ifdef FEAT_INS_EXPAND
4678 if (action == ACTION_EXPAND)
4679 {
4680 int reuse = 0;
4681 int add_r;
4682 char_u *aux;
4683
4684 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4685 break;
4686 found = TRUE;
4687 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004688 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (vim_iswordp(p))
4692 goto exit_matched;
4693 p = find_word_start(p);
4694 }
4695 p = find_word_end(p);
4696 i = (int)(p - aux);
4697
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004698 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
4700 /* get the next line */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004701 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 STRNCPY(IObuff, aux, i);
4703 if (!( depth < 0
4704 && lnum < end_lnum
4705 && (line = ml_get(++lnum)) != NULL)
4706 && !( depth >= 0
4707 && !vim_fgets(line = file_line,
4708 LSIZE, files[depth].fp)))
4709 goto exit_matched;
4710
4711 /* we read a line, set "already" to check this "line" later
4712 * if depth >= 0 we'll increase files[depth].lnum far
4713 * bellow -- Acevedo */
4714 already = aux = p = skipwhite(line);
4715 p = find_word_start(p);
4716 p = find_word_end(p);
4717 if (p > aux)
4718 {
4719 if (*aux != ')' && IObuff[i-1] != TAB)
4720 {
4721 if (IObuff[i-1] != ' ')
4722 IObuff[i++] = ' ';
4723 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4724 if (p_js
4725 && (IObuff[i-2] == '.'
4726 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4727 && (IObuff[i-2] == '?'
4728 || IObuff[i-2] == '!'))))
4729 IObuff[i++] = ' ';
4730 }
4731 /* copy as much as posible of the new word */
4732 if (p - aux >= IOSIZE - i)
4733 p = aux + IOSIZE - i - 1;
4734 STRNCPY(IObuff + i, aux, p - aux);
4735 i += (int)(p - aux);
4736 reuse |= CONT_S_IPOS;
4737 }
4738 IObuff[i] = NUL;
4739 aux = IObuff;
4740
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004741 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 goto exit_matched;
4743 }
4744
4745 add_r = ins_compl_add_infercase(aux, i,
4746 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4747 dir, reuse);
4748 if (add_r == OK)
4749 /* if dir was BACKWARD then honor it just once */
4750 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004751 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 break;
4753 }
4754 else
4755#endif
4756 if (action == ACTION_SHOW_ALL)
4757 {
4758 found = TRUE;
4759 if (!did_show)
4760 gotocmdline(TRUE); /* cursor at status line */
4761 if (curr_fname != prev_fname)
4762 {
4763 if (did_show)
4764 msg_putchar('\n'); /* cursor below last one */
4765 if (!got_int) /* don't display if 'q' typed
4766 at "--more--" mesage */
4767 msg_home_replace_hl(curr_fname);
4768 prev_fname = curr_fname;
4769 }
4770 did_show = TRUE;
4771 if (!got_int)
4772 show_pat_in_path(line, type, TRUE, action,
4773 (depth == -1) ? NULL : files[depth].fp,
4774 (depth == -1) ? &lnum : &files[depth].lnum,
4775 match_count++);
4776
4777 /* Set matched flag for this file and all the ones that
4778 * include it */
4779 for (i = 0; i <= depth; ++i)
4780 files[i].matched = TRUE;
4781 }
4782 else if (--count <= 0)
4783 {
4784 found = TRUE;
4785 if (depth == -1 && lnum == curwin->w_cursor.lnum
4786#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4787 && g_do_tagpreview == 0
4788#endif
4789 )
4790 EMSG(_("E387: Match is on current line"));
4791 else if (action == ACTION_SHOW)
4792 {
4793 show_pat_in_path(line, type, did_show, action,
4794 (depth == -1) ? NULL : files[depth].fp,
4795 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4796 did_show = TRUE;
4797 }
4798 else
4799 {
4800#ifdef FEAT_GUI
4801 need_mouse_correct = TRUE;
4802#endif
4803#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4804 /* ":psearch" uses the preview window */
4805 if (g_do_tagpreview != 0)
4806 {
4807 curwin_save = curwin;
4808 prepare_tagpreview();
4809 }
4810#endif
4811 if (action == ACTION_SPLIT)
4812 {
4813#ifdef FEAT_WINDOWS
4814 if (win_split(0, 0) == FAIL)
4815#endif
4816 break;
4817#ifdef FEAT_SCROLLBIND
4818 curwin->w_p_scb = FALSE;
4819#endif
4820 }
4821 if (depth == -1)
4822 {
4823 /* match in current file */
4824#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4825 if (g_do_tagpreview != 0)
4826 {
4827 if (getfile(0, curwin_save->w_buffer->b_fname,
4828 NULL, TRUE, lnum, FALSE) > 0)
4829 break; /* failed to jump to file */
4830 }
4831 else
4832#endif
4833 setpcmark();
4834 curwin->w_cursor.lnum = lnum;
4835 }
4836 else
4837 {
4838 if (getfile(0, files[depth].name, NULL, TRUE,
4839 files[depth].lnum, FALSE) > 0)
4840 break; /* failed to jump to file */
4841 /* autocommands may have changed the lnum, we don't
4842 * want that here */
4843 curwin->w_cursor.lnum = files[depth].lnum;
4844 }
4845 }
4846 if (action != ACTION_SHOW)
4847 {
4848 curwin->w_cursor.col = (colnr_T) (startp - line);
4849 curwin->w_set_curswant = TRUE;
4850 }
4851
4852#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4853 if (g_do_tagpreview != 0
4854 && curwin != curwin_save && win_valid(curwin_save))
4855 {
4856 /* Return cursor to where we were */
4857 validate_cursor();
4858 redraw_later(VALID);
4859 win_enter(curwin_save, TRUE);
4860 }
4861#endif
4862 break;
4863 }
4864#ifdef FEAT_INS_EXPAND
4865exit_matched:
4866#endif
4867 matched = FALSE;
4868 /* look for other matches in the rest of the line if we
4869 * are not at the end of it already */
4870 if (def_regmatch.regprog == NULL
4871#ifdef FEAT_INS_EXPAND
4872 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004873 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#endif
4875 && *(p = startp + 1))
4876 goto search_line;
4877 }
4878 line_breakcheck();
4879#ifdef FEAT_INS_EXPAND
4880 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00004881 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004882 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#else
4884 if (got_int)
4885#endif
4886 break;
4887
4888 /*
4889 * Read the next line. When reading an included file and encountering
4890 * end-of-file, close the file and continue in the file that included
4891 * it.
4892 */
4893 while (depth >= 0 && !already
4894 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4895 {
4896 fclose(files[depth].fp);
4897 --old_files;
4898 files[old_files].name = files[depth].name;
4899 files[old_files].matched = files[depth].matched;
4900 --depth;
4901 curr_fname = (depth == -1) ? curbuf->b_fname
4902 : files[depth].name;
4903 if (depth < depth_displayed)
4904 depth_displayed = depth;
4905 }
4906 if (depth >= 0) /* we could read the line */
4907 files[depth].lnum++;
4908 else if (!already)
4909 {
4910 if (++lnum > end_lnum)
4911 break;
4912 line = ml_get(lnum);
4913 }
4914 already = NULL;
4915 }
4916 /* End of big for (;;) loop. */
4917
4918 /* Close any files that are still open. */
4919 for (i = 0; i <= depth; i++)
4920 {
4921 fclose(files[i].fp);
4922 vim_free(files[i].name);
4923 }
4924 for (i = old_files; i < max_path_depth; i++)
4925 vim_free(files[i].name);
4926 vim_free(files);
4927
4928 if (type == CHECK_PATH)
4929 {
4930 if (!did_show)
4931 {
4932 if (action != ACTION_SHOW_ALL)
4933 MSG(_("All included files were found"));
4934 else
4935 MSG(_("No included files"));
4936 }
4937 }
4938 else if (!found
4939#ifdef FEAT_INS_EXPAND
4940 && action != ACTION_EXPAND
4941#endif
4942 )
4943 {
4944#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004945 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#else
4947 if (got_int)
4948#endif
4949 EMSG(_(e_interr));
4950 else if (type == FIND_DEFINE)
4951 EMSG(_("E388: Couldn't find definition"));
4952 else
4953 EMSG(_("E389: Couldn't find pattern"));
4954 }
4955 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4956 msg_end();
4957
4958fpip_end:
4959 vim_free(file_line);
4960 vim_free(regmatch.regprog);
4961 vim_free(incl_regmatch.regprog);
4962 vim_free(def_regmatch.regprog);
4963
4964#ifdef RISCOS
4965 /* Restore previous file munging state. */
4966 __riscosify_control = previous_munging;
4967#endif
4968}
4969
4970 static void
4971show_pat_in_path(line, type, did_show, action, fp, lnum, count)
4972 char_u *line;
4973 int type;
4974 int did_show;
4975 int action;
4976 FILE *fp;
4977 linenr_T *lnum;
4978 long count;
4979{
4980 char_u *p;
4981
4982 if (did_show)
4983 msg_putchar('\n'); /* cursor below last one */
4984 else
4985 gotocmdline(TRUE); /* cursor at status line */
4986 if (got_int) /* 'q' typed at "--more--" message */
4987 return;
4988 for (;;)
4989 {
4990 p = line + STRLEN(line) - 1;
4991 if (fp != NULL)
4992 {
4993 /* We used fgets(), so get rid of newline at end */
4994 if (p >= line && *p == '\n')
4995 --p;
4996 if (p >= line && *p == '\r')
4997 --p;
4998 *(p + 1) = NUL;
4999 }
5000 if (action == ACTION_SHOW_ALL)
5001 {
5002 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5003 msg_puts(IObuff);
5004 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5005 /* Highlight line numbers */
5006 msg_puts_attr(IObuff, hl_attr(HLF_N));
5007 MSG_PUTS(" ");
5008 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005009 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 out_flush(); /* show one line at a time */
5011
5012 /* Definition continues until line that doesn't end with '\' */
5013 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5014 break;
5015
5016 if (fp != NULL)
5017 {
5018 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5019 break;
5020 ++*lnum;
5021 }
5022 else
5023 {
5024 if (++*lnum > curbuf->b_ml.ml_line_count)
5025 break;
5026 line = ml_get(*lnum);
5027 }
5028 msg_putchar('\n');
5029 }
5030}
5031#endif
5032
5033#ifdef FEAT_VIMINFO
5034 int
5035read_viminfo_search_pattern(virp, force)
5036 vir_T *virp;
5037 int force;
5038{
5039 char_u *lp;
5040 int idx = -1;
5041 int magic = FALSE;
5042 int no_scs = FALSE;
5043 int off_line = FALSE;
5044 int off_end = FALSE;
5045 long off = 0;
5046 int setlast = FALSE;
5047#ifdef FEAT_SEARCH_EXTRA
5048 static int hlsearch_on = FALSE;
5049#endif
5050 char_u *val;
5051
5052 /*
5053 * Old line types:
5054 * "/pat", "&pat": search/subst. pat
5055 * "~/pat", "~&pat": last used search/subst. pat
5056 * New line types:
5057 * "~h", "~H": hlsearch highlighting off/on
5058 * "~<magic><smartcase><line><end><off><last><which>pat"
5059 * <magic>: 'm' off, 'M' on
5060 * <smartcase>: 's' off, 'S' on
5061 * <line>: 'L' line offset, 'l' char offset
5062 * <end>: 'E' from end, 'e' from start
5063 * <off>: decimal, offset
5064 * <last>: '~' last used pattern
5065 * <which>: '/' search pat, '&' subst. pat
5066 */
5067 lp = virp->vir_line;
5068 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5069 {
5070 if (lp[1] == 'M') /* magic on */
5071 magic = TRUE;
5072 if (lp[2] == 's')
5073 no_scs = TRUE;
5074 if (lp[3] == 'L')
5075 off_line = TRUE;
5076 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005077 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 lp += 5;
5079 off = getdigits(&lp);
5080 }
5081 if (lp[0] == '~') /* use this pattern for last-used pattern */
5082 {
5083 setlast = TRUE;
5084 lp++;
5085 }
5086 if (lp[0] == '/')
5087 idx = RE_SEARCH;
5088 else if (lp[0] == '&')
5089 idx = RE_SUBST;
5090#ifdef FEAT_SEARCH_EXTRA
5091 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5092 hlsearch_on = FALSE;
5093 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5094 hlsearch_on = TRUE;
5095#endif
5096 if (idx >= 0)
5097 {
5098 if (force || spats[idx].pat == NULL)
5099 {
5100 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5101 TRUE);
5102 if (val != NULL)
5103 {
5104 set_last_search_pat(val, idx, magic, setlast);
5105 vim_free(val);
5106 spats[idx].no_scs = no_scs;
5107 spats[idx].off.line = off_line;
5108 spats[idx].off.end = off_end;
5109 spats[idx].off.off = off;
5110#ifdef FEAT_SEARCH_EXTRA
5111 if (setlast)
5112 no_hlsearch = !hlsearch_on;
5113#endif
5114 }
5115 }
5116 }
5117 return viminfo_readline(virp);
5118}
5119
5120 void
5121write_viminfo_search_pattern(fp)
5122 FILE *fp;
5123{
5124 if (get_viminfo_parameter('/') != 0)
5125 {
5126#ifdef FEAT_SEARCH_EXTRA
5127 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5128 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5129#endif
5130 wvsp_one(fp, RE_SEARCH, "", '/');
5131 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5132 }
5133}
5134
5135 static void
5136wvsp_one(fp, idx, s, sc)
5137 FILE *fp; /* file to write to */
5138 int idx; /* spats[] index */
5139 char *s; /* search pat */
5140 int sc; /* dir char */
5141{
5142 if (spats[idx].pat != NULL)
5143 {
5144 fprintf(fp, "\n# Last %sSearch Pattern:\n~", s);
5145 /* off.dir is not stored, it's reset to forward */
5146 fprintf(fp, "%c%c%c%c%ld%s%c",
5147 spats[idx].magic ? 'M' : 'm', /* magic */
5148 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5149 spats[idx].off.line ? 'L' : 'l', /* line offset */
5150 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5151 spats[idx].off.off, /* offset */
5152 last_idx == idx ? "~" : "", /* last used pat */
5153 sc);
5154 viminfo_writestring(fp, spats[idx].pat);
5155 }
5156}
5157#endif /* FEAT_VIMINFO */