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