blob: f21d72b3290b2d04bbbd5dc30b19970c07575336 [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 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 if ((compl_cont_status & CONT_ADDING)
1361 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
1363 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1364 return OK;
1365 }
1366 else if (*p != NUL) /* ignore empty lines */
1367 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1369 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 return OK;
1371 }
1372 }
1373 return FAIL;
1374}
1375#endif /* FEAT_INS_EXPAND */
1376
1377/*
1378 * Character Searches
1379 */
1380
1381/*
1382 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1383 * position of the character, otherwise move to just before the char.
1384 * Do this "cap->count1" times.
1385 * Return FAIL or OK.
1386 */
1387 int
1388searchc(cap, t_cmd)
1389 cmdarg_T *cap;
1390 int t_cmd;
1391{
1392 int c = cap->nchar; /* char to search for */
1393 int dir = cap->arg; /* TRUE for searching forward */
1394 long count = cap->count1; /* repeat count */
1395 static int lastc = NUL; /* last character searched for */
1396 static int lastcdir; /* last direction of character search */
1397 static int last_t_cmd; /* last search t_cmd */
1398 int col;
1399 char_u *p;
1400 int len;
1401#ifdef FEAT_MBYTE
1402 static char_u bytes[MB_MAXBYTES];
1403 static int bytelen = 1; /* >1 for multi-byte char */
1404#endif
1405
1406 if (c != NUL) /* normal search: remember args for repeat */
1407 {
1408 if (!KeyStuffed) /* don't remember when redoing */
1409 {
1410 lastc = c;
1411 lastcdir = dir;
1412 last_t_cmd = t_cmd;
1413#ifdef FEAT_MBYTE
1414 bytelen = (*mb_char2bytes)(c, bytes);
1415 if (cap->ncharC1 != 0)
1416 {
1417 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1418 if (cap->ncharC2 != 0)
1419 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1420 }
1421#endif
1422 }
1423 }
1424 else /* repeat previous search */
1425 {
1426 if (lastc == NUL)
1427 return FAIL;
1428 if (dir) /* repeat in opposite direction */
1429 dir = -lastcdir;
1430 else
1431 dir = lastcdir;
1432 t_cmd = last_t_cmd;
1433 c = lastc;
1434 /* For multi-byte re-use last bytes[] and bytelen. */
1435 }
1436
1437 p = ml_get_curline();
1438 col = curwin->w_cursor.col;
1439 len = (int)STRLEN(p);
1440
1441 while (count--)
1442 {
1443#ifdef FEAT_MBYTE
1444 if (has_mbyte)
1445 {
1446 for (;;)
1447 {
1448 if (dir > 0)
1449 {
1450 col += (*mb_ptr2len_check)(p + col);
1451 if (col >= len)
1452 return FAIL;
1453 }
1454 else
1455 {
1456 if (col == 0)
1457 return FAIL;
1458 col -= (*mb_head_off)(p, p + col - 1) + 1;
1459 }
1460 if (bytelen == 1)
1461 {
1462 if (p[col] == c)
1463 break;
1464 }
1465 else
1466 {
1467 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1468 break;
1469 }
1470 }
1471 }
1472 else
1473#endif
1474 {
1475 for (;;)
1476 {
1477 if ((col += dir) < 0 || col >= len)
1478 return FAIL;
1479 if (p[col] == c)
1480 break;
1481 }
1482 }
1483 }
1484
1485 if (t_cmd)
1486 {
1487 /* backup to before the character (possibly double-byte) */
1488 col -= dir;
1489#ifdef FEAT_MBYTE
1490 if (has_mbyte)
1491 {
1492 if (dir < 0)
1493 /* Landed on the search char which is bytelen long */
1494 col += bytelen - 1;
1495 else
1496 /* To previous char, which may be multi-byte. */
1497 col -= (*mb_head_off)(p, p + col);
1498 }
1499#endif
1500 }
1501 curwin->w_cursor.col = col;
1502
1503 return OK;
1504}
1505
1506/*
1507 * "Other" Searches
1508 */
1509
1510/*
1511 * findmatch - find the matching paren or brace
1512 *
1513 * Improvement over vi: Braces inside quotes are ignored.
1514 */
1515 pos_T *
1516findmatch(oap, initc)
1517 oparg_T *oap;
1518 int initc;
1519{
1520 return findmatchlimit(oap, initc, 0, 0);
1521}
1522
1523/*
1524 * Return TRUE if the character before "linep[col]" equals "ch".
1525 * Return FALSE if "col" is zero.
1526 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1527 * is NULL.
1528 * Handles multibyte string correctly.
1529 */
1530 static int
1531check_prevcol(linep, col, ch, prevcol)
1532 char_u *linep;
1533 int col;
1534 int ch;
1535 int *prevcol;
1536{
1537 --col;
1538#ifdef FEAT_MBYTE
1539 if (col > 0 && has_mbyte)
1540 col -= (*mb_head_off)(linep, linep + col);
1541#endif
1542 if (prevcol)
1543 *prevcol = col;
1544 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1545}
1546
1547/*
1548 * findmatchlimit -- find the matching paren or brace, if it exists within
1549 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1550 * the edge of the file.
1551 *
1552 * "initc" is the character to find a match for. NUL means to find the
1553 * character at or after the cursor.
1554 *
1555 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1556 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1557 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1558 * FM_SKIPCOMM skip comments (not implemented yet!)
1559 */
1560
1561 pos_T *
1562findmatchlimit(oap, initc, flags, maxtravel)
1563 oparg_T *oap;
1564 int initc;
1565 int flags;
1566 int maxtravel;
1567{
1568 static pos_T pos; /* current search position */
1569 int findc = 0; /* matching brace */
1570 int c;
1571 int count = 0; /* cumulative number of braces */
1572 int backwards = FALSE; /* init for gcc */
1573 int inquote = FALSE; /* TRUE when inside quotes */
1574 char_u *linep; /* pointer to current line */
1575 char_u *ptr;
1576 int do_quotes; /* check for quotes in current line */
1577 int at_start; /* do_quotes value at start position */
1578 int hash_dir = 0; /* Direction searched for # things */
1579 int comment_dir = 0; /* Direction searched for comments */
1580 pos_T match_pos; /* Where last slash-star was found */
1581 int start_in_quotes; /* start position is in quotes */
1582 int traveled = 0; /* how far we've searched so far */
1583 int ignore_cend = FALSE; /* ignore comment end */
1584 int cpo_match; /* vi compatible matching */
1585 int cpo_bsl; /* don't recognize backslashes */
1586 int match_escaped = 0; /* search for escaped match */
1587 int dir; /* Direction to search */
1588 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001589#ifdef FEAT_LISP
1590 int lispcomm = FALSE; /* inside of Lisp-style comment */
1591 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
1594 pos = curwin->w_cursor;
1595 linep = ml_get(pos.lnum);
1596
1597 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1598 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1599
1600 /* Direction to search when initc is '/', '*' or '#' */
1601 if (flags & FM_BACKWARD)
1602 dir = BACKWARD;
1603 else if (flags & FM_FORWARD)
1604 dir = FORWARD;
1605 else
1606 dir = 0;
1607
1608 /*
1609 * if initc given, look in the table for the matching character
1610 * '/' and '*' are special cases: look for start or end of comment.
1611 * When '/' is used, we ignore running backwards into an star-slash, for
1612 * "[*" command, we just want to find any comment.
1613 */
1614 if (initc == '/' || initc == '*')
1615 {
1616 comment_dir = dir;
1617 if (initc == '/')
1618 ignore_cend = TRUE;
1619 backwards = (dir == FORWARD) ? FALSE : TRUE;
1620 initc = NUL;
1621 }
1622 else if (initc != '#' && initc != NUL)
1623 {
1624 /* 'matchpairs' is "x:y,x:y" */
1625 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1626 {
1627 if (*ptr == initc)
1628 {
1629 findc = initc;
1630 initc = ptr[2];
1631 backwards = TRUE;
1632 break;
1633 }
1634 ptr += 2;
1635 if (*ptr == initc)
1636 {
1637 findc = initc;
1638 initc = ptr[-2];
1639 backwards = FALSE;
1640 break;
1641 }
1642 if (ptr[1] != ',')
1643 break;
1644 }
1645 if (!findc) /* invalid initc! */
1646 return NULL;
1647 }
1648 /*
1649 * Either initc is '#', or no initc was given and we need to look under the
1650 * cursor.
1651 */
1652 else
1653 {
1654 if (initc == '#')
1655 {
1656 hash_dir = dir;
1657 }
1658 else
1659 {
1660 /*
1661 * initc was not given, must look for something to match under
1662 * or near the cursor.
1663 * Only check for special things when 'cpo' doesn't have '%'.
1664 */
1665 if (!cpo_match)
1666 {
1667 /* Are we before or at #if, #else etc.? */
1668 ptr = skipwhite(linep);
1669 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1670 {
1671 ptr = skipwhite(ptr + 1);
1672 if ( STRNCMP(ptr, "if", 2) == 0
1673 || STRNCMP(ptr, "endif", 5) == 0
1674 || STRNCMP(ptr, "el", 2) == 0)
1675 hash_dir = 1;
1676 }
1677
1678 /* Are we on a comment? */
1679 else if (linep[pos.col] == '/')
1680 {
1681 if (linep[pos.col + 1] == '*')
1682 {
1683 comment_dir = FORWARD;
1684 backwards = FALSE;
1685 pos.col++;
1686 }
1687 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1688 {
1689 comment_dir = BACKWARD;
1690 backwards = TRUE;
1691 pos.col--;
1692 }
1693 }
1694 else if (linep[pos.col] == '*')
1695 {
1696 if (linep[pos.col + 1] == '/')
1697 {
1698 comment_dir = BACKWARD;
1699 backwards = TRUE;
1700 }
1701 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1702 {
1703 comment_dir = FORWARD;
1704 backwards = FALSE;
1705 }
1706 }
1707 }
1708
1709 /*
1710 * If we are not on a comment or the # at the start of a line, then
1711 * look for brace anywhere on this line after the cursor.
1712 */
1713 if (!hash_dir && !comment_dir)
1714 {
1715 /*
1716 * Find the brace under or after the cursor.
1717 * If beyond the end of the line, use the last character in
1718 * the line.
1719 */
1720 if (linep[pos.col] == NUL && pos.col)
1721 --pos.col;
1722 for (;;)
1723 {
1724 initc = linep[pos.col];
1725 if (initc == NUL)
1726 break;
1727
1728 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1729 {
1730 if (*ptr == initc)
1731 {
1732 findc = ptr[2];
1733 backwards = FALSE;
1734 break;
1735 }
1736 ptr += 2;
1737 if (*ptr == initc)
1738 {
1739 findc = ptr[-2];
1740 backwards = TRUE;
1741 break;
1742 }
1743 if (!*++ptr)
1744 break;
1745 }
1746 if (findc)
1747 break;
1748#ifdef FEAT_MBYTE
1749 if (has_mbyte)
1750 pos.col += (*mb_ptr2len_check)(linep + pos.col);
1751 else
1752#endif
1753 ++pos.col;
1754 }
1755 if (!findc)
1756 {
1757 /* no brace in the line, maybe use " #if" then */
1758 if (!cpo_match && *skipwhite(linep) == '#')
1759 hash_dir = 1;
1760 else
1761 return NULL;
1762 }
1763 else if (!cpo_bsl)
1764 {
1765 int col, bslcnt = 0;
1766
1767 /* Set "match_escaped" if there are an odd number of
1768 * backslashes. */
1769 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1770 bslcnt++;
1771 match_escaped = (bslcnt & 1);
1772 }
1773 }
1774 }
1775 if (hash_dir)
1776 {
1777 /*
1778 * Look for matching #if, #else, #elif, or #endif
1779 */
1780 if (oap != NULL)
1781 oap->motion_type = MLINE; /* Linewise for this case only */
1782 if (initc != '#')
1783 {
1784 ptr = skipwhite(skipwhite(linep) + 1);
1785 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1786 hash_dir = 1;
1787 else if (STRNCMP(ptr, "endif", 5) == 0)
1788 hash_dir = -1;
1789 else
1790 return NULL;
1791 }
1792 pos.col = 0;
1793 while (!got_int)
1794 {
1795 if (hash_dir > 0)
1796 {
1797 if (pos.lnum == curbuf->b_ml.ml_line_count)
1798 break;
1799 }
1800 else if (pos.lnum == 1)
1801 break;
1802 pos.lnum += hash_dir;
1803 linep = ml_get(pos.lnum);
1804 line_breakcheck(); /* check for CTRL-C typed */
1805 ptr = skipwhite(linep);
1806 if (*ptr != '#')
1807 continue;
1808 pos.col = (colnr_T) (ptr - linep);
1809 ptr = skipwhite(ptr + 1);
1810 if (hash_dir > 0)
1811 {
1812 if (STRNCMP(ptr, "if", 2) == 0)
1813 count++;
1814 else if (STRNCMP(ptr, "el", 2) == 0)
1815 {
1816 if (count == 0)
1817 return &pos;
1818 }
1819 else if (STRNCMP(ptr, "endif", 5) == 0)
1820 {
1821 if (count == 0)
1822 return &pos;
1823 count--;
1824 }
1825 }
1826 else
1827 {
1828 if (STRNCMP(ptr, "if", 2) == 0)
1829 {
1830 if (count == 0)
1831 return &pos;
1832 count--;
1833 }
1834 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1835 {
1836 if (count == 0)
1837 return &pos;
1838 }
1839 else if (STRNCMP(ptr, "endif", 5) == 0)
1840 count++;
1841 }
1842 }
1843 return NULL;
1844 }
1845 }
1846
1847#ifdef FEAT_RIGHTLEFT
1848 /* This is just guessing: when 'rightleft' is set, search for a maching
1849 * paren/brace in the other direction. */
1850 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1851 backwards = !backwards;
1852#endif
1853
1854 do_quotes = -1;
1855 start_in_quotes = MAYBE;
1856 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001857 if ((backwards && comment_dir)
1858#ifdef FEAT_LISP
1859 || lisp
1860#endif
1861 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001863#ifdef FEAT_LISP
1864 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1865 lispcomm = TRUE; /* find match inside this comment */
1866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 while (!got_int)
1868 {
1869 /*
1870 * Go to the next position, forward or backward. We could use
1871 * inc() and dec() here, but that is much slower
1872 */
1873 if (backwards)
1874 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001875#ifdef FEAT_LISP
1876 /* char to match is inside of comment, don't search outside */
1877 if (lispcomm && pos.col < (colnr_T)comment_col)
1878 break;
1879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (pos.col == 0) /* at start of line, go to prev. one */
1881 {
1882 if (pos.lnum == 1) /* start of file */
1883 break;
1884 --pos.lnum;
1885
1886 if (maxtravel && traveled++ > maxtravel)
1887 break;
1888
1889 linep = ml_get(pos.lnum);
1890 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1891 do_quotes = -1;
1892 line_breakcheck();
1893
1894 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001895 if (comment_dir
1896#ifdef FEAT_LISP
1897 || lisp
1898#endif
1899 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001901#ifdef FEAT_LISP
1902 /* skip comment */
1903 if (lisp && comment_col != MAXCOL)
1904 pos.col = comment_col;
1905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 else
1908 {
1909 --pos.col;
1910#ifdef FEAT_MBYTE
1911 if (has_mbyte)
1912 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1913#endif
1914 }
1915 }
1916 else /* forward search */
1917 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001918 if (linep[pos.col] == NUL
1919 /* at end of line, go to next one */
1920#ifdef FEAT_LISP
1921 /* don't search for match in comment */
1922 || (lisp && comment_col != MAXCOL
1923 && pos.col == (colnr_T)comment_col)
1924#endif
1925 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001927 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
1928#ifdef FEAT_LISP
1929 /* line is exhausted and comment with it,
1930 * don't search for match in code */
1931 || lispcomm
1932#endif
1933 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 break;
1935 ++pos.lnum;
1936
1937 if (maxtravel && traveled++ > maxtravel)
1938 break;
1939
1940 linep = ml_get(pos.lnum);
1941 pos.col = 0;
1942 do_quotes = -1;
1943 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001944#ifdef FEAT_LISP
1945 if (lisp) /* find comment pos in new line */
1946 comment_col = check_linecomment(linep);
1947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
1949 else
1950 {
1951#ifdef FEAT_MBYTE
1952 if (has_mbyte)
1953 pos.col += (*mb_ptr2len_check)(linep + pos.col);
1954 else
1955#endif
1956 ++pos.col;
1957 }
1958 }
1959
1960 /*
1961 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
1962 */
1963 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
1964 (linep[0] == '{' || linep[0] == '}'))
1965 {
1966 if (linep[0] == findc && count == 0) /* match! */
1967 return &pos;
1968 break; /* out of scope */
1969 }
1970
1971 if (comment_dir)
1972 {
1973 /* Note: comments do not nest, and we ignore quotes in them */
1974 /* TODO: ignore comment brackets inside strings */
1975 if (comment_dir == FORWARD)
1976 {
1977 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
1978 {
1979 pos.col++;
1980 return &pos;
1981 }
1982 }
1983 else /* Searching backwards */
1984 {
1985 /*
1986 * A comment may contain / * or / /, it may also start or end
1987 * with / * /. Ignore a / * after / /.
1988 */
1989 if (pos.col == 0)
1990 continue;
1991 else if ( linep[pos.col - 1] == '/'
1992 && linep[pos.col] == '*'
1993 && (int)pos.col < comment_col)
1994 {
1995 count++;
1996 match_pos = pos;
1997 match_pos.col--;
1998 }
1999 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2000 {
2001 if (count > 0)
2002 pos = match_pos;
2003 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2004 && (int)pos.col <= comment_col)
2005 pos.col -= 2;
2006 else if (ignore_cend)
2007 continue;
2008 else
2009 return NULL;
2010 return &pos;
2011 }
2012 }
2013 continue;
2014 }
2015
2016 /*
2017 * If smart matching ('cpoptions' does not contain '%'), braces inside
2018 * of quotes are ignored, but only if there is an even number of
2019 * quotes in the line.
2020 */
2021 if (cpo_match)
2022 do_quotes = 0;
2023 else if (do_quotes == -1)
2024 {
2025 /*
2026 * Count the number of quotes in the line, skipping \" and '"'.
2027 * Watch out for "\\".
2028 */
2029 at_start = do_quotes;
2030 for (ptr = linep; *ptr; ++ptr)
2031 {
2032 if (ptr == linep + pos.col + backwards)
2033 at_start = (do_quotes & 1);
2034 if (*ptr == '"'
2035 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2036 ++do_quotes;
2037 if (*ptr == '\\' && ptr[1] != NUL)
2038 ++ptr;
2039 }
2040 do_quotes &= 1; /* result is 1 with even number of quotes */
2041
2042 /*
2043 * If we find an uneven count, check current line and previous
2044 * one for a '\' at the end.
2045 */
2046 if (!do_quotes)
2047 {
2048 inquote = FALSE;
2049 if (ptr[-1] == '\\')
2050 {
2051 do_quotes = 1;
2052 if (start_in_quotes == MAYBE)
2053 {
2054 /* Do we need to use at_start here? */
2055 inquote = TRUE;
2056 start_in_quotes = TRUE;
2057 }
2058 else if (backwards)
2059 inquote = TRUE;
2060 }
2061 if (pos.lnum > 1)
2062 {
2063 ptr = ml_get(pos.lnum - 1);
2064 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2065 {
2066 do_quotes = 1;
2067 if (start_in_quotes == MAYBE)
2068 {
2069 inquote = at_start;
2070 if (inquote)
2071 start_in_quotes = TRUE;
2072 }
2073 else if (!backwards)
2074 inquote = TRUE;
2075 }
2076 }
2077 }
2078 }
2079 if (start_in_quotes == MAYBE)
2080 start_in_quotes = FALSE;
2081
2082 /*
2083 * If 'smartmatch' is set:
2084 * Things inside quotes are ignored by setting 'inquote'. If we
2085 * find a quote without a preceding '\' invert 'inquote'. At the
2086 * end of a line not ending in '\' we reset 'inquote'.
2087 *
2088 * In lines with an uneven number of quotes (without preceding '\')
2089 * we do not know which part to ignore. Therefore we only set
2090 * inquote if the number of quotes in a line is even, unless this
2091 * line or the previous one ends in a '\'. Complicated, isn't it?
2092 */
2093 switch (c = linep[pos.col])
2094 {
2095 case NUL:
2096 /* at end of line without trailing backslash, reset inquote */
2097 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2098 {
2099 inquote = FALSE;
2100 start_in_quotes = FALSE;
2101 }
2102 break;
2103
2104 case '"':
2105 /* a quote that is preceded with an odd number of backslashes is
2106 * ignored */
2107 if (do_quotes)
2108 {
2109 int col;
2110
2111 for (col = pos.col - 1; col >= 0; --col)
2112 if (linep[col] != '\\')
2113 break;
2114 if ((((int)pos.col - 1 - col) & 1) == 0)
2115 {
2116 inquote = !inquote;
2117 start_in_quotes = FALSE;
2118 }
2119 }
2120 break;
2121
2122 /*
2123 * If smart matching ('cpoptions' does not contain '%'):
2124 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2125 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2126 * skipped, there is never a brace in them.
2127 * Ignore this when finding matches for `'.
2128 */
2129 case '\'':
2130 if (!cpo_match && initc != '\'' && findc != '\'')
2131 {
2132 if (backwards)
2133 {
2134 if (pos.col > 1)
2135 {
2136 if (linep[pos.col - 2] == '\'')
2137 {
2138 pos.col -= 2;
2139 break;
2140 }
2141 else if (linep[pos.col - 2] == '\\' &&
2142 pos.col > 2 && linep[pos.col - 3] == '\'')
2143 {
2144 pos.col -= 3;
2145 break;
2146 }
2147 }
2148 }
2149 else if (linep[pos.col + 1]) /* forward search */
2150 {
2151 if (linep[pos.col + 1] == '\\' &&
2152 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2153 {
2154 pos.col += 3;
2155 break;
2156 }
2157 else if (linep[pos.col + 2] == '\'')
2158 {
2159 pos.col += 2;
2160 break;
2161 }
2162 }
2163 }
2164 /* FALLTHROUGH */
2165
2166 default:
2167#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002168 /*
2169 * For Lisp skip over backslashed (), {} and [].
2170 * (actually, we skip #\( et al)
2171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (curbuf->b_p_lisp
2173 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002174 && pos.col > 1
2175 && check_prevcol(linep, pos.col, '\\', NULL)
2176 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 break;
2178#endif
2179
2180 /* Check for match outside of quotes, and inside of
2181 * quotes when the start is also inside of quotes. */
2182 if ((!inquote || start_in_quotes == TRUE)
2183 && (c == initc || c == findc))
2184 {
2185 int col, bslcnt = 0;
2186
2187 if (!cpo_bsl)
2188 {
2189 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2190 bslcnt++;
2191 }
2192 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2193 * what we expect. */
2194 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2195 {
2196 if (c == initc)
2197 count++;
2198 else
2199 {
2200 if (count == 0)
2201 return &pos;
2202 count--;
2203 }
2204 }
2205 }
2206 }
2207 }
2208
2209 if (comment_dir == BACKWARD && count > 0)
2210 {
2211 pos = match_pos;
2212 return &pos;
2213 }
2214 return (pos_T *)NULL; /* never found it */
2215}
2216
2217/*
2218 * Check if line[] contains a / / comment.
2219 * Return MAXCOL if not, otherwise return the column.
2220 * TODO: skip strings.
2221 */
2222 static int
2223check_linecomment(line)
2224 char_u *line;
2225{
2226 char_u *p;
2227
2228 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002229#ifdef FEAT_LISP
2230 /* skip Lispish one-line comments */
2231 if (curbuf->b_p_lisp)
2232 {
2233 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2234 {
2235 int instr = FALSE; /* inside of string */
2236
2237 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002238 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002239 {
2240 if (*p == '"')
2241 {
2242 if (instr)
2243 {
2244 if (*(p - 1) != '\\') /* skip escaped quote */
2245 instr = FALSE;
2246 }
2247 else if (p == line || ((p - line) >= 2
2248 /* skip #\" form */
2249 && *(p - 1) != '\\' && *(p - 2) != '#'))
2250 instr = TRUE;
2251 }
2252 else if (!instr && ((p - line) < 2
2253 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2254 break; /* found! */
2255 ++p;
2256 }
2257 }
2258 else
2259 p = NULL;
2260 }
2261 else
2262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 while ((p = vim_strchr(p, '/')) != NULL)
2264 {
2265 if (p[1] == '/')
2266 break;
2267 ++p;
2268 }
2269
2270 if (p == NULL)
2271 return MAXCOL;
2272 return (int)(p - line);
2273}
2274
2275/*
2276 * Move cursor briefly to character matching the one under the cursor.
2277 * Used for Insert mode and "r" command.
2278 * Show the match only if it is visible on the screen.
2279 * If there isn't a match, then beep.
2280 */
2281 void
2282showmatch(c)
2283 int c; /* char to show match for */
2284{
2285 pos_T *lpos, save_cursor;
2286 pos_T mpos;
2287 colnr_T vcol;
2288 long save_so;
2289 long save_siso;
2290#ifdef CURSOR_SHAPE
2291 int save_state;
2292#endif
2293 colnr_T save_dollar_vcol;
2294 char_u *p;
2295
2296 /*
2297 * Only show match for chars in the 'matchpairs' option.
2298 */
2299 /* 'matchpairs' is "x:y,x:y" */
2300 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2301 {
2302#ifdef FEAT_RIGHTLEFT
2303 if (*p == c && (curwin->w_p_rl ^ p_ri))
2304 break;
2305#endif
2306 p += 2;
2307 if (*p == c
2308#ifdef FEAT_RIGHTLEFT
2309 && !(curwin->w_p_rl ^ p_ri)
2310#endif
2311 )
2312 break;
2313 if (p[1] != ',')
2314 return;
2315 }
2316
2317 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2318 vim_beep();
2319 else if (lpos->lnum >= curwin->w_topline)
2320 {
2321 if (!curwin->w_p_wrap)
2322 getvcol(curwin, lpos, NULL, &vcol, NULL);
2323 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2324 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2325 {
2326 mpos = *lpos; /* save the pos, update_screen() may change it */
2327 save_cursor = curwin->w_cursor;
2328 save_so = p_so;
2329 save_siso = p_siso;
2330 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2331 * stop displaying the "$". */
2332 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2333 dollar_vcol = 0;
2334 ++curwin->w_virtcol; /* do display ')' just before "$" */
2335 update_screen(VALID); /* show the new char first */
2336
2337 save_dollar_vcol = dollar_vcol;
2338#ifdef CURSOR_SHAPE
2339 save_state = State;
2340 State = SHOWMATCH;
2341 ui_cursor_shape(); /* may show different cursor shape */
2342#endif
2343 curwin->w_cursor = mpos; /* move to matching char */
2344 p_so = 0; /* don't use 'scrolloff' here */
2345 p_siso = 0; /* don't use 'sidescrolloff' here */
2346 showruler(FALSE);
2347 setcursor();
2348 cursor_on(); /* make sure that the cursor is shown */
2349 out_flush();
2350#ifdef FEAT_GUI
2351 if (gui.in_use)
2352 {
2353 gui_update_cursor(TRUE, FALSE);
2354 gui_mch_flush();
2355 }
2356#endif
2357 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2358 * which resets it if the matching position is in a previous line
2359 * and has a higher column number. */
2360 dollar_vcol = save_dollar_vcol;
2361
2362 /*
2363 * brief pause, unless 'm' is present in 'cpo' and a character is
2364 * available.
2365 */
2366 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2367 ui_delay(p_mat * 100L, TRUE);
2368 else if (!char_avail())
2369 ui_delay(p_mat * 100L, FALSE);
2370 curwin->w_cursor = save_cursor; /* restore cursor position */
2371 p_so = save_so;
2372 p_siso = save_siso;
2373#ifdef CURSOR_SHAPE
2374 State = save_state;
2375 ui_cursor_shape(); /* may show different cursor shape */
2376#endif
2377 }
2378 }
2379}
2380
2381/*
2382 * findsent(dir, count) - Find the start of the next sentence in direction
2383 * 'dir' Sentences are supposed to end in ".", "!" or "?" followed by white
2384 * space or a line break. Also stop at an empty line.
2385 * Return OK if the next sentence was found.
2386 */
2387 int
2388findsent(dir, count)
2389 int dir;
2390 long count;
2391{
2392 pos_T pos, tpos;
2393 int c;
2394 int (*func) __ARGS((pos_T *));
2395 int startlnum;
2396 int noskip = FALSE; /* do not skip blanks */
2397 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002398 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 pos = curwin->w_cursor;
2401 if (dir == FORWARD)
2402 func = incl;
2403 else
2404 func = decl;
2405
2406 while (count--)
2407 {
2408 /*
2409 * if on an empty line, skip upto a non-empty line
2410 */
2411 if (gchar_pos(&pos) == NUL)
2412 {
2413 do
2414 if ((*func)(&pos) == -1)
2415 break;
2416 while (gchar_pos(&pos) == NUL);
2417 if (dir == FORWARD)
2418 goto found;
2419 }
2420 /*
2421 * if on the start of a paragraph or a section and searching forward,
2422 * go to the next line
2423 */
2424 else if (dir == FORWARD && pos.col == 0 &&
2425 startPS(pos.lnum, NUL, FALSE))
2426 {
2427 if (pos.lnum == curbuf->b_ml.ml_line_count)
2428 return FAIL;
2429 ++pos.lnum;
2430 goto found;
2431 }
2432 else if (dir == BACKWARD)
2433 decl(&pos);
2434
2435 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002436 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2438 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2439 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002440 if (vim_strchr((char_u *)".!?", c) != NULL)
2441 {
2442 /* Only skip over a '.', '!' and '?' once. */
2443 if (found_dot)
2444 break;
2445 found_dot = TRUE;
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 if (decl(&pos) == -1)
2448 break;
2449 /* when going forward: Stop in front of empty line */
2450 if (lineempty(pos.lnum) && dir == FORWARD)
2451 {
2452 incl(&pos);
2453 goto found;
2454 }
2455 }
2456
2457 /* remember the line where the search started */
2458 startlnum = pos.lnum;
2459 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2460
2461 for (;;) /* find end of sentence */
2462 {
2463 c = gchar_pos(&pos);
2464 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2465 {
2466 if (dir == BACKWARD && pos.lnum != startlnum)
2467 ++pos.lnum;
2468 break;
2469 }
2470 if (c == '.' || c == '!' || c == '?')
2471 {
2472 tpos = pos;
2473 do
2474 if ((c = inc(&tpos)) == -1)
2475 break;
2476 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2477 != NULL);
2478 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2479 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2480 && gchar_pos(&tpos) == ' ')))
2481 {
2482 pos = tpos;
2483 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2484 inc(&pos);
2485 break;
2486 }
2487 }
2488 if ((*func)(&pos) == -1)
2489 {
2490 if (count)
2491 return FAIL;
2492 noskip = TRUE;
2493 break;
2494 }
2495 }
2496found:
2497 /* skip white space */
2498 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2499 if (incl(&pos) == -1)
2500 break;
2501 }
2502
2503 setpcmark();
2504 curwin->w_cursor = pos;
2505 return OK;
2506}
2507
2508/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002509 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002511 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 * If 'what' is '{' or '}' we go to the next section.
2513 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002514 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 */
2516 int
2517findpar(oap, dir, count, what, both)
2518 oparg_T *oap;
2519 int dir;
2520 long count;
2521 int what;
2522 int both;
2523{
2524 linenr_T curr;
2525 int did_skip; /* TRUE after separating lines have been skipped */
2526 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002527 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528#ifdef FEAT_FOLDING
2529 linenr_T fold_first; /* first line of a closed fold */
2530 linenr_T fold_last; /* last line of a closed fold */
2531 int fold_skipped; /* TRUE if a closed fold was skipped this
2532 iteration */
2533#endif
2534
2535 curr = curwin->w_cursor.lnum;
2536
2537 while (count--)
2538 {
2539 did_skip = FALSE;
2540 for (first = TRUE; ; first = FALSE)
2541 {
2542 if (*ml_get(curr) != NUL)
2543 did_skip = TRUE;
2544
2545#ifdef FEAT_FOLDING
2546 /* skip folded lines */
2547 fold_skipped = FALSE;
2548 if (first && hasFolding(curr, &fold_first, &fold_last))
2549 {
2550 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2551 fold_skipped = TRUE;
2552 }
2553#endif
2554
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002555 /* POSIX has it's own ideas of what a paragraph boundary is and it
2556 * doesn't match historical Vi: It also stops at a "{" in the
2557 * first column and at an empty line. */
2558 if (!first && did_skip && (startPS(curr, what, both)
2559 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 break;
2561
2562#ifdef FEAT_FOLDING
2563 if (fold_skipped)
2564 curr -= dir;
2565#endif
2566 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2567 {
2568 if (count)
2569 return FALSE;
2570 curr -= dir;
2571 break;
2572 }
2573 }
2574 }
2575 setpcmark();
2576 if (both && *ml_get(curr) == '}') /* include line with '}' */
2577 ++curr;
2578 curwin->w_cursor.lnum = curr;
2579 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2580 {
2581 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2582 {
2583 --curwin->w_cursor.col;
2584 oap->inclusive = TRUE;
2585 }
2586 }
2587 else
2588 curwin->w_cursor.col = 0;
2589 return TRUE;
2590}
2591
2592/*
2593 * check if the string 's' is a nroff macro that is in option 'opt'
2594 */
2595 static int
2596inmacro(opt, s)
2597 char_u *opt;
2598 char_u *s;
2599{
2600 char_u *macro;
2601
2602 for (macro = opt; macro[0]; ++macro)
2603 {
2604 /* Accept two characters in the option being equal to two characters
2605 * in the line. A space in the option matches with a space in the
2606 * line or the line having ended. */
2607 if ( (macro[0] == s[0]
2608 || (macro[0] == ' '
2609 && (s[0] == NUL || s[0] == ' ')))
2610 && (macro[1] == s[1]
2611 || ((macro[1] == NUL || macro[1] == ' ')
2612 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2613 break;
2614 ++macro;
2615 if (macro[0] == NUL)
2616 break;
2617 }
2618 return (macro[0] != NUL);
2619}
2620
2621/*
2622 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2623 * If 'para' is '{' or '}' only check for sections.
2624 * If 'both' is TRUE also stop at '}'
2625 */
2626 int
2627startPS(lnum, para, both)
2628 linenr_T lnum;
2629 int para;
2630 int both;
2631{
2632 char_u *s;
2633
2634 s = ml_get(lnum);
2635 if (*s == para || *s == '\f' || (both && *s == '}'))
2636 return TRUE;
2637 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2638 (!para && inmacro(p_para, s + 1))))
2639 return TRUE;
2640 return FALSE;
2641}
2642
2643/*
2644 * The following routines do the word searches performed by the 'w', 'W',
2645 * 'b', 'B', 'e', and 'E' commands.
2646 */
2647
2648/*
2649 * To perform these searches, characters are placed into one of three
2650 * classes, and transitions between classes determine word boundaries.
2651 *
2652 * The classes are:
2653 *
2654 * 0 - white space
2655 * 1 - punctuation
2656 * 2 or higher - keyword characters (letters, digits and underscore)
2657 */
2658
2659static int cls_bigword; /* TRUE for "W", "B" or "E" */
2660
2661/*
2662 * cls() - returns the class of character at curwin->w_cursor
2663 *
2664 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2665 * from class 2 and higher are reported as class 1 since only white space
2666 * boundaries are of interest.
2667 */
2668 static int
2669cls()
2670{
2671 int c;
2672
2673 c = gchar_cursor();
2674#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2675 if (p_altkeymap && c == F_BLANK)
2676 return 0;
2677#endif
2678 if (c == ' ' || c == '\t' || c == NUL)
2679 return 0;
2680#ifdef FEAT_MBYTE
2681 if (enc_dbcs != 0 && c > 0xFF)
2682 {
2683 /* If cls_bigword, report multi-byte chars as class 1. */
2684 if (enc_dbcs == DBCS_KOR && cls_bigword)
2685 return 1;
2686
2687 /* process code leading/trailing bytes */
2688 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2689 }
2690 if (enc_utf8)
2691 {
2692 c = utf_class(c);
2693 if (c != 0 && cls_bigword)
2694 return 1;
2695 return c;
2696 }
2697#endif
2698
2699 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2700 if (cls_bigword)
2701 return 1;
2702
2703 if (vim_iswordc(c))
2704 return 2;
2705 return 1;
2706}
2707
2708
2709/*
2710 * fwd_word(count, type, eol) - move forward one word
2711 *
2712 * Returns FAIL if the cursor was already at the end of the file.
2713 * If eol is TRUE, last word stops at end of line (for operators).
2714 */
2715 int
2716fwd_word(count, bigword, eol)
2717 long count;
2718 int bigword; /* "W", "E" or "B" */
2719 int eol;
2720{
2721 int sclass; /* starting class */
2722 int i;
2723 int last_line;
2724
2725#ifdef FEAT_VIRTUALEDIT
2726 curwin->w_cursor.coladd = 0;
2727#endif
2728 cls_bigword = bigword;
2729 while (--count >= 0)
2730 {
2731#ifdef FEAT_FOLDING
2732 /* When inside a range of folded lines, move to the last char of the
2733 * last line. */
2734 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2735 coladvance((colnr_T)MAXCOL);
2736#endif
2737 sclass = cls();
2738
2739 /*
2740 * We always move at least one character, unless on the last
2741 * character in the buffer.
2742 */
2743 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2744 i = inc_cursor();
2745 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2746 return FAIL;
2747 if (i == 1 && eol && count == 0) /* started at last char in line */
2748 return OK;
2749
2750 /*
2751 * Go one char past end of current word (if any)
2752 */
2753 if (sclass != 0)
2754 while (cls() == sclass)
2755 {
2756 i = inc_cursor();
2757 if (i == -1 || (i >= 1 && eol && count == 0))
2758 return OK;
2759 }
2760
2761 /*
2762 * go to next non-white
2763 */
2764 while (cls() == 0)
2765 {
2766 /*
2767 * We'll stop if we land on a blank line
2768 */
2769 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2770 break;
2771
2772 i = inc_cursor();
2773 if (i == -1 || (i >= 1 && eol && count == 0))
2774 return OK;
2775 }
2776 }
2777 return OK;
2778}
2779
2780/*
2781 * bck_word() - move backward 'count' words
2782 *
2783 * If stop is TRUE and we are already on the start of a word, move one less.
2784 *
2785 * Returns FAIL if top of the file was reached.
2786 */
2787 int
2788bck_word(count, bigword, stop)
2789 long count;
2790 int bigword;
2791 int stop;
2792{
2793 int sclass; /* starting class */
2794
2795#ifdef FEAT_VIRTUALEDIT
2796 curwin->w_cursor.coladd = 0;
2797#endif
2798 cls_bigword = bigword;
2799 while (--count >= 0)
2800 {
2801#ifdef FEAT_FOLDING
2802 /* When inside a range of folded lines, move to the first char of the
2803 * first line. */
2804 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2805 curwin->w_cursor.col = 0;
2806#endif
2807 sclass = cls();
2808 if (dec_cursor() == -1) /* started at start of file */
2809 return FAIL;
2810
2811 if (!stop || sclass == cls() || sclass == 0)
2812 {
2813 /*
2814 * Skip white space before the word.
2815 * Stop on an empty line.
2816 */
2817 while (cls() == 0)
2818 {
2819 if (curwin->w_cursor.col == 0
2820 && lineempty(curwin->w_cursor.lnum))
2821 goto finished;
2822 if (dec_cursor() == -1) /* hit start of file, stop here */
2823 return OK;
2824 }
2825
2826 /*
2827 * Move backward to start of this word.
2828 */
2829 if (skip_chars(cls(), BACKWARD))
2830 return OK;
2831 }
2832
2833 inc_cursor(); /* overshot - forward one */
2834finished:
2835 stop = FALSE;
2836 }
2837 return OK;
2838}
2839
2840/*
2841 * end_word() - move to the end of the word
2842 *
2843 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2844 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2845 * motion crosses blank lines. When the real vi crosses a blank line in an
2846 * 'e' motion, the cursor is placed on the FIRST character of the next
2847 * non-blank line. The 'E' command, however, works correctly. Since this
2848 * appears to be a bug, I have not duplicated it here.
2849 *
2850 * Returns FAIL if end of the file was reached.
2851 *
2852 * If stop is TRUE and we are already on the end of a word, move one less.
2853 * If empty is TRUE stop on an empty line.
2854 */
2855 int
2856end_word(count, bigword, stop, empty)
2857 long count;
2858 int bigword;
2859 int stop;
2860 int empty;
2861{
2862 int sclass; /* starting class */
2863
2864#ifdef FEAT_VIRTUALEDIT
2865 curwin->w_cursor.coladd = 0;
2866#endif
2867 cls_bigword = bigword;
2868 while (--count >= 0)
2869 {
2870#ifdef FEAT_FOLDING
2871 /* When inside a range of folded lines, move to the last char of the
2872 * last line. */
2873 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2874 coladvance((colnr_T)MAXCOL);
2875#endif
2876 sclass = cls();
2877 if (inc_cursor() == -1)
2878 return FAIL;
2879
2880 /*
2881 * If we're in the middle of a word, we just have to move to the end
2882 * of it.
2883 */
2884 if (cls() == sclass && sclass != 0)
2885 {
2886 /*
2887 * Move forward to end of the current word
2888 */
2889 if (skip_chars(sclass, FORWARD))
2890 return FAIL;
2891 }
2892 else if (!stop || sclass == 0)
2893 {
2894 /*
2895 * We were at the end of a word. Go to the end of the next word.
2896 * First skip white space, if 'empty' is TRUE, stop at empty line.
2897 */
2898 while (cls() == 0)
2899 {
2900 if (empty && curwin->w_cursor.col == 0
2901 && lineempty(curwin->w_cursor.lnum))
2902 goto finished;
2903 if (inc_cursor() == -1) /* hit end of file, stop here */
2904 return FAIL;
2905 }
2906
2907 /*
2908 * Move forward to the end of this word.
2909 */
2910 if (skip_chars(cls(), FORWARD))
2911 return FAIL;
2912 }
2913 dec_cursor(); /* overshot - one char backward */
2914finished:
2915 stop = FALSE; /* we move only one word less */
2916 }
2917 return OK;
2918}
2919
2920/*
2921 * Move back to the end of the word.
2922 *
2923 * Returns FAIL if start of the file was reached.
2924 */
2925 int
2926bckend_word(count, bigword, eol)
2927 long count;
2928 int bigword; /* TRUE for "B" */
2929 int eol; /* TRUE: stop at end of line. */
2930{
2931 int sclass; /* starting class */
2932 int i;
2933
2934#ifdef FEAT_VIRTUALEDIT
2935 curwin->w_cursor.coladd = 0;
2936#endif
2937 cls_bigword = bigword;
2938 while (--count >= 0)
2939 {
2940 sclass = cls();
2941 if ((i = dec_cursor()) == -1)
2942 return FAIL;
2943 if (eol && i == 1)
2944 return OK;
2945
2946 /*
2947 * Move backward to before the start of this word.
2948 */
2949 if (sclass != 0)
2950 {
2951 while (cls() == sclass)
2952 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2953 return OK;
2954 }
2955
2956 /*
2957 * Move backward to end of the previous word
2958 */
2959 while (cls() == 0)
2960 {
2961 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
2962 break;
2963 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2964 return OK;
2965 }
2966 }
2967 return OK;
2968}
2969
2970/*
2971 * Skip a row of characters of the same class.
2972 * Return TRUE when end-of-file reached, FALSE otherwise.
2973 */
2974 static int
2975skip_chars(cclass, dir)
2976 int cclass;
2977 int dir;
2978{
2979 while (cls() == cclass)
2980 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
2981 return TRUE;
2982 return FALSE;
2983}
2984
2985#ifdef FEAT_TEXTOBJ
2986/*
2987 * Go back to the start of the word or the start of white space
2988 */
2989 static void
2990back_in_line()
2991{
2992 int sclass; /* starting class */
2993
2994 sclass = cls();
2995 for (;;)
2996 {
2997 if (curwin->w_cursor.col == 0) /* stop at start of line */
2998 break;
2999 dec_cursor();
3000 if (cls() != sclass) /* stop at start of word */
3001 {
3002 inc_cursor();
3003 break;
3004 }
3005 }
3006}
3007
3008 static void
3009find_first_blank(posp)
3010 pos_T *posp;
3011{
3012 int c;
3013
3014 while (decl(posp) != -1)
3015 {
3016 c = gchar_pos(posp);
3017 if (!vim_iswhite(c))
3018 {
3019 incl(posp);
3020 break;
3021 }
3022 }
3023}
3024
3025/*
3026 * Skip count/2 sentences and count/2 separating white spaces.
3027 */
3028 static void
3029findsent_forward(count, at_start_sent)
3030 long count;
3031 int at_start_sent; /* cursor is at start of sentence */
3032{
3033 while (count--)
3034 {
3035 findsent(FORWARD, 1L);
3036 if (at_start_sent)
3037 find_first_blank(&curwin->w_cursor);
3038 if (count == 0 || at_start_sent)
3039 decl(&curwin->w_cursor);
3040 at_start_sent = !at_start_sent;
3041 }
3042}
3043
3044/*
3045 * Find word under cursor, cursor at end.
3046 * Used while an operator is pending, and in Visual mode.
3047 */
3048 int
3049current_word(oap, count, include, bigword)
3050 oparg_T *oap;
3051 long count;
3052 int include; /* TRUE: include word and white space */
3053 int bigword; /* FALSE == word, TRUE == WORD */
3054{
3055 pos_T start_pos;
3056 pos_T pos;
3057 int inclusive = TRUE;
3058 int include_white = FALSE;
3059
3060 cls_bigword = bigword;
3061
3062#ifdef FEAT_VISUAL
3063 /* Correct cursor when 'selection' is exclusive */
3064 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3065 dec_cursor();
3066
3067 /*
3068 * When Visual mode is not active, or when the VIsual area is only one
3069 * character, select the word and/or white space under the cursor.
3070 */
3071 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3072#endif
3073 {
3074 /*
3075 * Go to start of current word or white space.
3076 */
3077 back_in_line();
3078 start_pos = curwin->w_cursor;
3079
3080 /*
3081 * If the start is on white space, and white space should be included
3082 * (" word"), or start is not on white space, and white space should
3083 * not be included ("word"), find end of word.
3084 */
3085 if ((cls() == 0) == include)
3086 {
3087 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3088 return FAIL;
3089 }
3090 else
3091 {
3092 /*
3093 * If the start is not on white space, and white space should be
3094 * included ("word "), or start is on white space and white
3095 * space should not be included (" "), find start of word.
3096 * If we end up in the first column of the next line (single char
3097 * word) back up to end of the line.
3098 */
3099 fwd_word(1L, bigword, TRUE);
3100 if (curwin->w_cursor.col == 0)
3101 decl(&curwin->w_cursor);
3102 else
3103 oneleft();
3104
3105 if (include)
3106 include_white = TRUE;
3107 }
3108
3109#ifdef FEAT_VISUAL
3110 if (VIsual_active)
3111 {
3112 /* should do something when inclusive == FALSE ! */
3113 VIsual = start_pos;
3114 redraw_curbuf_later(INVERTED); /* update the inversion */
3115 }
3116 else
3117#endif
3118 {
3119 oap->start = start_pos;
3120 oap->motion_type = MCHAR;
3121 }
3122 --count;
3123 }
3124
3125 /*
3126 * When count is still > 0, extend with more objects.
3127 */
3128 while (count > 0)
3129 {
3130 inclusive = TRUE;
3131#ifdef FEAT_VISUAL
3132 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3133 {
3134 /*
3135 * In Visual mode, with cursor at start: move cursor back.
3136 */
3137 if (decl(&curwin->w_cursor) == -1)
3138 return FAIL;
3139 if (include != (cls() != 0))
3140 {
3141 if (bck_word(1L, bigword, TRUE) == FAIL)
3142 return FAIL;
3143 }
3144 else
3145 {
3146 if (bckend_word(1L, bigword, TRUE) == FAIL)
3147 return FAIL;
3148 (void)incl(&curwin->w_cursor);
3149 }
3150 }
3151 else
3152#endif
3153 {
3154 /*
3155 * Move cursor forward one word and/or white area.
3156 */
3157 if (incl(&curwin->w_cursor) == -1)
3158 return FAIL;
3159 if (include != (cls() == 0))
3160 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003161 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 return FAIL;
3163 /*
3164 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003165 * the first character on the line.
3166 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003168 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 inclusive = FALSE;
3170 }
3171 else
3172 {
3173 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3174 return FAIL;
3175 }
3176 }
3177 --count;
3178 }
3179
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003180 if (include_white && (cls() != 0
3181 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 {
3183 /*
3184 * If we don't include white space at the end, move the start
3185 * to include some white space there. This makes "daw" work
3186 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003187 * word). Also when "2daw" deletes "word." at the end of the line
3188 * (cursor is at start of next line).
3189 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 */
3191 pos = curwin->w_cursor; /* save cursor position */
3192 curwin->w_cursor = start_pos;
3193 if (oneleft() == OK)
3194 {
3195 back_in_line();
3196 if (cls() == 0 && curwin->w_cursor.col > 0)
3197 {
3198#ifdef FEAT_VISUAL
3199 if (VIsual_active)
3200 VIsual = curwin->w_cursor;
3201 else
3202#endif
3203 oap->start = curwin->w_cursor;
3204 }
3205 }
3206 curwin->w_cursor = pos; /* put cursor back at end */
3207 }
3208
3209#ifdef FEAT_VISUAL
3210 if (VIsual_active)
3211 {
3212 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3213 inc_cursor();
3214 if (VIsual_mode == 'V')
3215 {
3216 VIsual_mode = 'v';
3217 redraw_cmdline = TRUE; /* show mode later */
3218 }
3219 }
3220 else
3221#endif
3222 oap->inclusive = inclusive;
3223
3224 return OK;
3225}
3226
3227/*
3228 * Find sentence(s) under the cursor, cursor at end.
3229 * When Visual active, extend it by one or more sentences.
3230 */
3231 int
3232current_sent(oap, count, include)
3233 oparg_T *oap;
3234 long count;
3235 int include;
3236{
3237 pos_T start_pos;
3238 pos_T pos;
3239 int start_blank;
3240 int c;
3241 int at_start_sent;
3242 long ncount;
3243
3244 start_pos = curwin->w_cursor;
3245 pos = start_pos;
3246 findsent(FORWARD, 1L); /* Find start of next sentence. */
3247
3248#ifdef FEAT_VISUAL
3249 /*
3250 * When visual area is bigger than one character: Extend it.
3251 */
3252 if (VIsual_active && !equalpos(start_pos, VIsual))
3253 {
3254extend:
3255 if (lt(start_pos, VIsual))
3256 {
3257 /*
3258 * Cursor at start of Visual area.
3259 * Find out where we are:
3260 * - in the white space before a sentence
3261 * - in a sentence or just after it
3262 * - at the start of a sentence
3263 */
3264 at_start_sent = TRUE;
3265 decl(&pos);
3266 while (lt(pos, curwin->w_cursor))
3267 {
3268 c = gchar_pos(&pos);
3269 if (!vim_iswhite(c))
3270 {
3271 at_start_sent = FALSE;
3272 break;
3273 }
3274 incl(&pos);
3275 }
3276 if (!at_start_sent)
3277 {
3278 findsent(BACKWARD, 1L);
3279 if (equalpos(curwin->w_cursor, start_pos))
3280 at_start_sent = TRUE; /* exactly at start of sentence */
3281 else
3282 /* inside a sentence, go to its end (start of next) */
3283 findsent(FORWARD, 1L);
3284 }
3285 if (include) /* "as" gets twice as much as "is" */
3286 count *= 2;
3287 while (count--)
3288 {
3289 if (at_start_sent)
3290 find_first_blank(&curwin->w_cursor);
3291 c = gchar_cursor();
3292 if (!at_start_sent || (!include && !vim_iswhite(c)))
3293 findsent(BACKWARD, 1L);
3294 at_start_sent = !at_start_sent;
3295 }
3296 }
3297 else
3298 {
3299 /*
3300 * Cursor at end of Visual area.
3301 * Find out where we are:
3302 * - just before a sentence
3303 * - just before or in the white space before a sentence
3304 * - in a sentence
3305 */
3306 incl(&pos);
3307 at_start_sent = TRUE;
3308 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3309 {
3310 at_start_sent = FALSE;
3311 while (lt(pos, curwin->w_cursor))
3312 {
3313 c = gchar_pos(&pos);
3314 if (!vim_iswhite(c))
3315 {
3316 at_start_sent = TRUE;
3317 break;
3318 }
3319 incl(&pos);
3320 }
3321 if (at_start_sent) /* in the sentence */
3322 findsent(BACKWARD, 1L);
3323 else /* in/before white before a sentence */
3324 curwin->w_cursor = start_pos;
3325 }
3326
3327 if (include) /* "as" gets twice as much as "is" */
3328 count *= 2;
3329 findsent_forward(count, at_start_sent);
3330 if (*p_sel == 'e')
3331 ++curwin->w_cursor.col;
3332 }
3333 return OK;
3334 }
3335#endif
3336
3337 /*
3338 * If cursor started on blank, check if it is just before the start of the
3339 * next sentence.
3340 */
3341 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3342 incl(&pos);
3343 if (equalpos(pos, curwin->w_cursor))
3344 {
3345 start_blank = TRUE;
3346 find_first_blank(&start_pos); /* go back to first blank */
3347 }
3348 else
3349 {
3350 start_blank = FALSE;
3351 findsent(BACKWARD, 1L);
3352 start_pos = curwin->w_cursor;
3353 }
3354 if (include)
3355 ncount = count * 2;
3356 else
3357 {
3358 ncount = count;
3359 if (start_blank)
3360 --ncount;
3361 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003362 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 findsent_forward(ncount, TRUE);
3364 else
3365 decl(&curwin->w_cursor);
3366
3367 if (include)
3368 {
3369 /*
3370 * If the blank in front of the sentence is included, exclude the
3371 * blanks at the end of the sentence, go back to the first blank.
3372 * If there are no trailing blanks, try to include leading blanks.
3373 */
3374 if (start_blank)
3375 {
3376 find_first_blank(&curwin->w_cursor);
3377 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3378 if (vim_iswhite(c))
3379 decl(&curwin->w_cursor);
3380 }
3381 else if (c = gchar_cursor(), !vim_iswhite(c))
3382 find_first_blank(&start_pos);
3383 }
3384
3385#ifdef FEAT_VISUAL
3386 if (VIsual_active)
3387 {
3388 /* avoid getting stuck with "is" on a single space before a sent. */
3389 if (equalpos(start_pos, curwin->w_cursor))
3390 goto extend;
3391 if (*p_sel == 'e')
3392 ++curwin->w_cursor.col;
3393 VIsual = start_pos;
3394 VIsual_mode = 'v';
3395 redraw_curbuf_later(INVERTED); /* update the inversion */
3396 }
3397 else
3398#endif
3399 {
3400 /* include a newline after the sentence, if there is one */
3401 if (incl(&curwin->w_cursor) == -1)
3402 oap->inclusive = TRUE;
3403 else
3404 oap->inclusive = FALSE;
3405 oap->start = start_pos;
3406 oap->motion_type = MCHAR;
3407 }
3408 return OK;
3409}
3410
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003411/*
3412 * Find block under the cursor, cursor at end.
3413 * "what" and "other" are two matching parenthesis/paren/etc.
3414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 int
3416current_block(oap, count, include, what, other)
3417 oparg_T *oap;
3418 long count;
3419 int include; /* TRUE == include white space */
3420 int what; /* '(', '{', etc. */
3421 int other; /* ')', '}', etc. */
3422{
3423 pos_T old_pos;
3424 pos_T *pos = NULL;
3425 pos_T start_pos;
3426 pos_T *end_pos;
3427 pos_T old_start, old_end;
3428 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003429 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003432 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 old_start = old_end;
3434
3435 /*
3436 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3437 */
3438#ifdef FEAT_VISUAL
3439 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3440#endif
3441 {
3442 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003443 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 while (inindent(1))
3445 if (inc_cursor() != 0)
3446 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003447 if (gchar_cursor() == what)
3448 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 ++curwin->w_cursor.col;
3450 }
3451#ifdef FEAT_VISUAL
3452 else if (lt(VIsual, curwin->w_cursor))
3453 {
3454 old_start = VIsual;
3455 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3456 }
3457 else
3458 old_end = VIsual;
3459#endif
3460
3461 /*
3462 * Search backwards for unclosed '(', '{', etc..
3463 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003464 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 */
3466 save_cpo = p_cpo;
3467 p_cpo = (char_u *)"%";
3468 while (count-- > 0)
3469 {
3470 if ((pos = findmatch(NULL, what)) == NULL)
3471 break;
3472 curwin->w_cursor = *pos;
3473 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3474 }
3475 p_cpo = save_cpo;
3476
3477 /*
3478 * Search for matching ')', '}', etc.
3479 * Put this position in curwin->w_cursor.
3480 */
3481 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3482 {
3483 curwin->w_cursor = old_pos;
3484 return FAIL;
3485 }
3486 curwin->w_cursor = *end_pos;
3487
3488 /*
3489 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3490 * If the ending '}' is only preceded by indent, skip that indent.
3491 * But only if the resulting area is not smaller than what we started with.
3492 */
3493 while (!include)
3494 {
3495 incl(&start_pos);
3496 sol = (curwin->w_cursor.col == 0);
3497 decl(&curwin->w_cursor);
3498 if (what == '{')
3499 while (inindent(1))
3500 {
3501 sol = TRUE;
3502 if (decl(&curwin->w_cursor) != 0)
3503 break;
3504 }
3505#ifdef FEAT_VISUAL
3506 /*
3507 * In Visual mode, when the resulting area is not bigger than what we
3508 * started with, extend it to the next block, and then exclude again.
3509 */
3510 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3511 && VIsual_active)
3512 {
3513 curwin->w_cursor = old_start;
3514 decl(&curwin->w_cursor);
3515 if ((pos = findmatch(NULL, what)) == NULL)
3516 {
3517 curwin->w_cursor = old_pos;
3518 return FAIL;
3519 }
3520 start_pos = *pos;
3521 curwin->w_cursor = *pos;
3522 if ((end_pos = findmatch(NULL, other)) == NULL)
3523 {
3524 curwin->w_cursor = old_pos;
3525 return FAIL;
3526 }
3527 curwin->w_cursor = *end_pos;
3528 }
3529 else
3530#endif
3531 break;
3532 }
3533
3534#ifdef FEAT_VISUAL
3535 if (VIsual_active)
3536 {
3537 if (*p_sel == 'e')
3538 ++curwin->w_cursor.col;
3539 if (sol)
3540 inc(&curwin->w_cursor); /* include the line break */
3541 VIsual = start_pos;
3542 VIsual_mode = 'v';
3543 redraw_curbuf_later(INVERTED); /* update the inversion */
3544 showmode();
3545 }
3546 else
3547#endif
3548 {
3549 oap->start = start_pos;
3550 oap->motion_type = MCHAR;
3551 if (sol)
3552 {
3553 incl(&curwin->w_cursor);
3554 oap->inclusive = FALSE;
3555 }
3556 else
3557 oap->inclusive = TRUE;
3558 }
3559
3560 return OK;
3561}
3562
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003563static int in_html_tag __ARGS((int));
3564
3565/*
3566 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3567 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3568 */
3569 static int
3570in_html_tag(end_tag)
3571 int end_tag;
3572{
3573 char_u *line = ml_get_curline();
3574 char_u *p;
3575 int c;
3576 int lc = NUL;
3577 pos_T pos;
3578
3579#ifdef FEAT_MBYTE
3580 if (enc_dbcs)
3581 {
3582 char_u *lp = NULL;
3583
3584 /* We search forward until the cursor, because searching backwards is
3585 * very slow for DBCS encodings. */
3586 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3587 if (*p == '>' || *p == '<')
3588 {
3589 lc = *p;
3590 lp = p;
3591 }
3592 if (*p != '<') /* check for '<' under cursor */
3593 {
3594 if (lc != '<')
3595 return FALSE;
3596 p = lp;
3597 }
3598 }
3599 else
3600#endif
3601 {
3602 for (p = line + curwin->w_cursor.col; p > line; )
3603 {
3604 if (*p == '<') /* find '<' under/before cursor */
3605 break;
3606 mb_ptr_back(line, p);
3607 if (*p == '>') /* find '>' before cursor */
3608 break;
3609 }
3610 if (*p != '<')
3611 return FALSE;
3612 }
3613
3614 pos.lnum = curwin->w_cursor.lnum;
3615 pos.col = p - line;
3616
3617 mb_ptr_adv(p);
3618 if (end_tag)
3619 /* check that there is a '/' after the '<' */
3620 return *p == '/';
3621
3622 /* check that there is no '/' after the '<' */
3623 if (*p == '/')
3624 return FALSE;
3625
3626 /* check that the matching '>' is not preceded by '/' */
3627 for (;;)
3628 {
3629 if (inc(&pos) < 0)
3630 return FALSE;
3631 c = *ml_get_pos(&pos);
3632 if (c == '>')
3633 break;
3634 lc = c;
3635 }
3636 return lc != '/';
3637}
3638
3639/*
3640 * Find tag block under the cursor, cursor at end.
3641 */
3642 int
3643current_tagblock(oap, count_arg, include)
3644 oparg_T *oap;
3645 long count_arg;
3646 int include; /* TRUE == include white space */
3647{
3648 long count = count_arg;
3649 long n;
3650 pos_T old_pos;
3651 pos_T start_pos;
3652 pos_T end_pos;
3653 pos_T old_start, old_end;
3654 char_u *spat, *epat;
3655 char_u *p;
3656 char_u *cp;
3657 int len;
3658 int r;
3659 int do_include = include;
3660 int save_p_ws = p_ws;
3661 int retval = FAIL;
3662
3663 p_ws = FALSE;
3664
3665 old_pos = curwin->w_cursor;
3666 old_end = curwin->w_cursor; /* remember where we started */
3667 old_start = old_end;
3668
3669 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003670 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003671 */
3672#ifdef FEAT_VISUAL
3673 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3674#endif
3675 {
3676 setpcmark();
3677
3678 /* ignore indent */
3679 while (inindent(1))
3680 if (inc_cursor() != 0)
3681 break;
3682
3683 if (in_html_tag(FALSE))
3684 {
3685 /* cursor on start tag, move to just after it */
3686 while (*ml_get_cursor() != '>')
3687 if (inc_cursor() < 0)
3688 break;
3689 }
3690 else if (in_html_tag(TRUE))
3691 {
3692 /* cursor on end tag, move to just before it */
3693 while (*ml_get_cursor() != '<')
3694 if (dec_cursor() < 0)
3695 break;
3696 dec_cursor();
3697 old_end = curwin->w_cursor;
3698 }
3699 }
3700#ifdef FEAT_VISUAL
3701 else if (lt(VIsual, curwin->w_cursor))
3702 {
3703 old_start = VIsual;
3704 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3705 }
3706 else
3707 old_end = VIsual;
3708#endif
3709
3710again:
3711 /*
3712 * Search backwards for unclosed "<aaa>".
3713 * Put this position in start_pos.
3714 */
3715 for (n = 0; n < count; ++n)
3716 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003717 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003718 (char_u *)"",
3719 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0) <= 0)
3720 {
3721 curwin->w_cursor = old_pos;
3722 goto theend;
3723 }
3724 }
3725 start_pos = curwin->w_cursor;
3726
3727 /*
3728 * Search for matching "</aaa>". First isolate the "aaa".
3729 */
3730 inc_cursor();
3731 p = ml_get_cursor();
3732 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3733 ;
3734 len = cp - p;
3735 if (len == 0)
3736 {
3737 curwin->w_cursor = old_pos;
3738 goto theend;
3739 }
3740 spat = alloc(len + 29);
3741 epat = alloc(len + 9);
3742 if (spat == NULL || epat == NULL)
3743 {
3744 vim_free(spat);
3745 vim_free(epat);
3746 curwin->w_cursor = old_pos;
3747 goto theend;
3748 }
3749 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3750 sprintf((char *)epat, "</%.*s>\\c", len, p);
3751
3752 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"", 0);
3753
3754 vim_free(spat);
3755 vim_free(epat);
3756
3757 if (r < 1 || lt(curwin->w_cursor, old_end))
3758 {
3759 /* Can't find other end or it's before the previous end. Could be a
3760 * HTML tag that doesn't have a matching end. Search backwards for
3761 * another starting tag. */
3762 count = 1;
3763 curwin->w_cursor = start_pos;
3764 goto again;
3765 }
3766
3767 if (do_include || r < 1)
3768 {
3769 /* Include up to the '>'. */
3770 while (*ml_get_cursor() != '>')
3771 if (inc_cursor() < 0)
3772 break;
3773 }
3774 else
3775 {
3776 /* Exclude the '<' of the end tag. */
3777 if (*ml_get_cursor() == '<')
3778 dec_cursor();
3779 }
3780 end_pos = curwin->w_cursor;
3781
3782 if (!do_include)
3783 {
3784 /* Exclude the start tag. */
3785 curwin->w_cursor = start_pos;
3786 while (inc_cursor() >= 0)
3787 if (*ml_get_cursor() == '>' && lt(curwin->w_cursor, end_pos))
3788 {
3789 inc_cursor();
3790 start_pos = curwin->w_cursor;
3791 break;
3792 }
3793 curwin->w_cursor = end_pos;
3794
Bram Moolenaar45360022005-07-21 21:08:21 +00003795 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003796 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003797 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003798 {
3799 do_include = TRUE;
3800 curwin->w_cursor = old_start;
3801 count = count_arg;
3802 goto again;
3803 }
3804 }
3805
3806#ifdef FEAT_VISUAL
3807 if (VIsual_active)
3808 {
3809 if (*p_sel == 'e')
3810 ++curwin->w_cursor.col;
3811 VIsual = start_pos;
3812 VIsual_mode = 'v';
3813 redraw_curbuf_later(INVERTED); /* update the inversion */
3814 showmode();
3815 }
3816 else
3817#endif
3818 {
3819 oap->start = start_pos;
3820 oap->motion_type = MCHAR;
3821 oap->inclusive = TRUE;
3822 }
3823 retval = OK;
3824
3825theend:
3826 p_ws = save_p_ws;
3827 return retval;
3828}
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 int
3831current_par(oap, count, include, type)
3832 oparg_T *oap;
3833 long count;
3834 int include; /* TRUE == include white space */
3835 int type; /* 'p' for paragraph, 'S' for section */
3836{
3837 linenr_T start_lnum;
3838 linenr_T end_lnum;
3839 int white_in_front;
3840 int dir;
3841 int start_is_white;
3842 int prev_start_is_white;
3843 int retval = OK;
3844 int do_white = FALSE;
3845 int t;
3846 int i;
3847
3848 if (type == 'S') /* not implemented yet */
3849 return FAIL;
3850
3851 start_lnum = curwin->w_cursor.lnum;
3852
3853#ifdef FEAT_VISUAL
3854 /*
3855 * When visual area is more than one line: extend it.
3856 */
3857 if (VIsual_active && start_lnum != VIsual.lnum)
3858 {
3859extend:
3860 if (start_lnum < VIsual.lnum)
3861 dir = BACKWARD;
3862 else
3863 dir = FORWARD;
3864 for (i = count; --i >= 0; )
3865 {
3866 if (start_lnum ==
3867 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3868 {
3869 retval = FAIL;
3870 break;
3871 }
3872
3873 prev_start_is_white = -1;
3874 for (t = 0; t < 2; ++t)
3875 {
3876 start_lnum += dir;
3877 start_is_white = linewhite(start_lnum);
3878 if (prev_start_is_white == start_is_white)
3879 {
3880 start_lnum -= dir;
3881 break;
3882 }
3883 for (;;)
3884 {
3885 if (start_lnum == (dir == BACKWARD
3886 ? 1 : curbuf->b_ml.ml_line_count))
3887 break;
3888 if (start_is_white != linewhite(start_lnum + dir)
3889 || (!start_is_white
3890 && startPS(start_lnum + (dir > 0
3891 ? 1 : 0), 0, 0)))
3892 break;
3893 start_lnum += dir;
3894 }
3895 if (!include)
3896 break;
3897 if (start_lnum == (dir == BACKWARD
3898 ? 1 : curbuf->b_ml.ml_line_count))
3899 break;
3900 prev_start_is_white = start_is_white;
3901 }
3902 }
3903 curwin->w_cursor.lnum = start_lnum;
3904 curwin->w_cursor.col = 0;
3905 return retval;
3906 }
3907#endif
3908
3909 /*
3910 * First move back to the start_lnum of the paragraph or white lines
3911 */
3912 white_in_front = linewhite(start_lnum);
3913 while (start_lnum > 1)
3914 {
3915 if (white_in_front) /* stop at first white line */
3916 {
3917 if (!linewhite(start_lnum - 1))
3918 break;
3919 }
3920 else /* stop at first non-white line of start of paragraph */
3921 {
3922 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
3923 break;
3924 }
3925 --start_lnum;
3926 }
3927
3928 /*
3929 * Move past the end of any white lines.
3930 */
3931 end_lnum = start_lnum;
3932 while (linewhite(end_lnum) && end_lnum < curbuf->b_ml.ml_line_count)
3933 ++end_lnum;
3934
3935 --end_lnum;
3936 i = count;
3937 if (!include && white_in_front)
3938 --i;
3939 while (i--)
3940 {
3941 if (end_lnum == curbuf->b_ml.ml_line_count)
3942 return FAIL;
3943
3944 if (!include)
3945 do_white = linewhite(end_lnum + 1);
3946
3947 if (include || !do_white)
3948 {
3949 ++end_lnum;
3950 /*
3951 * skip to end of paragraph
3952 */
3953 while (end_lnum < curbuf->b_ml.ml_line_count
3954 && !linewhite(end_lnum + 1)
3955 && !startPS(end_lnum + 1, 0, 0))
3956 ++end_lnum;
3957 }
3958
3959 if (i == 0 && white_in_front && include)
3960 break;
3961
3962 /*
3963 * skip to end of white lines after paragraph
3964 */
3965 if (include || do_white)
3966 while (end_lnum < curbuf->b_ml.ml_line_count
3967 && linewhite(end_lnum + 1))
3968 ++end_lnum;
3969 }
3970
3971 /*
3972 * If there are no empty lines at the end, try to find some empty lines at
3973 * the start (unless that has been done already).
3974 */
3975 if (!white_in_front && !linewhite(end_lnum) && include)
3976 while (start_lnum > 1 && linewhite(start_lnum - 1))
3977 --start_lnum;
3978
3979#ifdef FEAT_VISUAL
3980 if (VIsual_active)
3981 {
3982 /* Problem: when doing "Vipipip" nothing happens in a single white
3983 * line, we get stuck there. Trap this here. */
3984 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
3985 goto extend;
3986 VIsual.lnum = start_lnum;
3987 VIsual_mode = 'V';
3988 redraw_curbuf_later(INVERTED); /* update the inversion */
3989 showmode();
3990 }
3991 else
3992#endif
3993 {
3994 oap->start.lnum = start_lnum;
3995 oap->start.col = 0;
3996 oap->motion_type = MLINE;
3997 }
3998 curwin->w_cursor.lnum = end_lnum;
3999 curwin->w_cursor.col = 0;
4000
4001 return OK;
4002}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004003
4004static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4005static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4006
4007/*
4008 * Search quote char from string line[col].
4009 * Quote character escaped by one of the characters in "escape" is not counted
4010 * as a quote.
4011 * Returns column number of "quotechar" or -1 when not found.
4012 */
4013 static int
4014find_next_quote(line, col, quotechar, escape)
4015 char_u *line;
4016 int col;
4017 int quotechar;
4018 char_u *escape; /* escape characters, can be NULL */
4019{
4020 int c;
4021
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004022 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004023 {
4024 c = line[col];
4025 if (c == NUL)
4026 return -1;
4027 else if (escape != NULL && vim_strchr(escape, c))
4028 ++col;
4029 else if (c == quotechar)
4030 break;
4031#ifdef FEAT_MBYTE
4032 if (has_mbyte)
4033 col += (*mb_ptr2len_check)(line + col);
4034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004036 ++col;
4037 }
4038 return col;
4039}
4040
4041/*
4042 * Search backwards in "line" from column "col_start" to find "quotechar".
4043 * Quote character escaped by one of the characters in "escape" is not counted
4044 * as a quote.
4045 * Return the found column or zero.
4046 */
4047 static int
4048find_prev_quote(line, col_start, quotechar, escape)
4049 char_u *line;
4050 int col_start;
4051 int quotechar;
4052 char_u *escape; /* escape characters, can be NULL */
4053{
4054 int n;
4055
4056 while (col_start > 0)
4057 {
4058 --col_start;
4059#ifdef FEAT_MBYTE
4060 col_start -= (*mb_head_off)(line, line + col_start);
4061#endif
4062 n = 0;
4063 if (escape != NULL)
4064 while (col_start - n > 0 && vim_strchr(escape,
4065 line[col_start - n - 1]) != NULL)
4066 ++n;
4067 if (n & 1)
4068 col_start -= n; /* uneven number of escape chars, skip it */
4069 else if (line[col_start] == quotechar)
4070 break;
4071 }
4072 return col_start;
4073}
4074
4075/*
4076 * Find quote under the cursor, cursor at end.
4077 * Returns TRUE if found, else FALSE.
4078 */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004079/*ARGSUSED*/
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004080 int
4081current_quote(oap, count, include, quotechar)
4082 oparg_T *oap;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004083 long count; /* not used */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004084 int include; /* TRUE == include quote char */
4085 int quotechar; /* Quote character */
4086{
4087 char_u *line = ml_get_curline();
4088 int col_end;
4089 int col_start = curwin->w_cursor.col;
4090 int inclusive = FALSE;
4091#ifdef FEAT_VISUAL
4092 int vis_empty = TRUE; /* Visual selection <= 1 char */
4093 int vis_bef_curs = FALSE; /* Visual starts before cursor */
4094
4095 /* Correct cursor when 'selection' is exclusive */
4096 if (VIsual_active)
4097 {
4098 if (*p_sel == 'e' && vis_bef_curs)
4099 dec_cursor();
4100 vis_empty = equalpos(VIsual, curwin->w_cursor);
4101 vis_bef_curs = lt(VIsual, curwin->w_cursor);
4102 }
4103 if (!vis_empty && line[col_start] == quotechar)
4104 {
4105 /* Already selecting something and on a quote character. Find the
4106 * next quoted string. */
4107 if (vis_bef_curs)
4108 {
4109 /* Assume we are on a closing quote: move to after the next
4110 * opening quote. */
4111 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4112 if (col_start < 0)
4113 return FALSE;
4114 col_end = find_next_quote(line, col_start + 1, quotechar,
4115 curbuf->b_p_qe);
4116 if (col_end < 0)
4117 {
4118 /* We were on a starting quote perhaps? */
4119 col_end = col_start;
4120 col_start = curwin->w_cursor.col;
4121 }
4122 }
4123 else
4124 {
4125 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4126 if (line[col_end] != quotechar)
4127 return FALSE;
4128 col_start = find_prev_quote(line, col_end, quotechar,
4129 curbuf->b_p_qe);
4130 if (line[col_start] != quotechar)
4131 {
4132 /* We were on an ending quote perhaps? */
4133 col_start = col_end;
4134 col_end = curwin->w_cursor.col;
4135 }
4136 }
4137 }
4138 else
4139#endif
4140
4141 if (line[col_start] == quotechar
4142#ifdef FEAT_VISUAL
4143 || !vis_empty
4144#endif
4145 )
4146 {
4147 int first_col = col_start;
4148
4149#ifdef FEAT_VISUAL
4150 if (!vis_empty)
4151 {
4152 if (vis_bef_curs)
4153 first_col = find_next_quote(line, col_start, quotechar, NULL);
4154 else
4155 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4156 }
4157#endif
4158 /* The cursor is on a quote, we don't know if it's the opening or
4159 * closing quote. Search from the start of the line to find out.
4160 * Also do this when there is a Visual area, a' may leave the cursor
4161 * in between two strings. */
4162 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004163 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004164 {
4165 /* Find open quote character. */
4166 col_start = find_next_quote(line, col_start, quotechar, NULL);
4167 if (col_start < 0 || col_start > first_col)
4168 return FALSE;
4169 /* Find close quote character. */
4170 col_end = find_next_quote(line, col_start + 1, quotechar,
4171 curbuf->b_p_qe);
4172 if (col_end < 0)
4173 return FALSE;
4174 /* If is cursor between start and end quote character, it is
4175 * target text object. */
4176 if (col_start <= first_col && first_col <= col_end)
4177 break;
4178 col_start = col_end + 1;
4179 }
4180 }
4181 else
4182 {
4183 /* Search backward for a starting quote. */
4184 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4185 if (line[col_start] != quotechar)
4186 {
4187 /* No quote before the cursor, look after the cursor. */
4188 col_start = find_next_quote(line, col_start, quotechar, NULL);
4189 if (col_start < 0)
4190 return FALSE;
4191 }
4192
4193 /* Find close quote character. */
4194 col_end = find_next_quote(line, col_start + 1, quotechar,
4195 curbuf->b_p_qe);
4196 if (col_end < 0)
4197 return FALSE;
4198 }
4199
4200 /* When "include" is TRUE, include spaces after closing quote or before
4201 * the starting quote. */
4202 if (include)
4203 {
4204 if (vim_iswhite(line[col_end + 1]))
4205 while (vim_iswhite(line[col_end + 1]))
4206 ++col_end;
4207 else
4208 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4209 --col_start;
4210 }
4211
4212 /* Set start position */
4213 if (!include)
4214 ++col_start;
4215 curwin->w_cursor.col = col_start;
4216#ifdef FEAT_VISUAL
4217 if (VIsual_active)
4218 {
4219 if (vis_empty)
4220 {
4221 VIsual = curwin->w_cursor;
4222 redraw_curbuf_later(INVERTED);
4223 }
4224 }
4225 else
4226#endif
4227 {
4228 oap->start = curwin->w_cursor;
4229 oap->motion_type = MCHAR;
4230 }
4231
4232 /* Set end position. */
4233 curwin->w_cursor.col = col_end;
4234 if (include && inc_cursor() == 2)
4235 inclusive = TRUE;
4236#ifdef FEAT_VISUAL
4237 if (VIsual_active)
4238 {
4239 if (vis_empty || vis_bef_curs)
4240 {
4241 /* decrement cursor when 'selection' is not exclusive */
4242 if (*p_sel != 'e')
4243 dec_cursor();
4244 }
4245 else
4246 {
4247 /* Cursor is at start of Visual area. */
4248 curwin->w_cursor.col = col_start;
4249 }
4250 if (VIsual_mode == 'V')
4251 {
4252 VIsual_mode = 'v';
4253 redraw_cmdline = TRUE; /* show mode later */
4254 }
4255 }
4256 else
4257#endif
4258 {
4259 /* Set inclusive and other oap's flags. */
4260 oap->inclusive = inclusive;
4261 }
4262
4263 return OK;
4264}
4265
4266#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4269 || defined(PROTO)
4270/*
4271 * return TRUE if line 'lnum' is empty or has white chars only.
4272 */
4273 int
4274linewhite(lnum)
4275 linenr_T lnum;
4276{
4277 char_u *p;
4278
4279 p = skipwhite(ml_get(lnum));
4280 return (*p == NUL);
4281}
4282#endif
4283
4284#if defined(FEAT_FIND_ID) || defined(PROTO)
4285/*
4286 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 */
4289/*ARGSUSED*/
4290 void
4291find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4292 type, count, action, start_lnum, end_lnum)
4293 char_u *ptr; /* pointer to search pattern */
4294 int dir; /* direction of expansion */
4295 int len; /* length of search pattern */
4296 int whole; /* match whole words only */
4297 int skip_comments; /* don't match inside comments */
4298 int type; /* Type of search; are we looking for a type?
4299 a macro? */
4300 long count;
4301 int action; /* What to do when we find it */
4302 linenr_T start_lnum; /* first line to start searching */
4303 linenr_T end_lnum; /* last line for searching */
4304{
4305 SearchedFile *files; /* Stack of included files */
4306 SearchedFile *bigger; /* When we need more space */
4307 int max_path_depth = 50;
4308 long match_count = 1;
4309
4310 char_u *pat;
4311 char_u *new_fname;
4312 char_u *curr_fname = curbuf->b_fname;
4313 char_u *prev_fname = NULL;
4314 linenr_T lnum;
4315 int depth;
4316 int depth_displayed; /* For type==CHECK_PATH */
4317 int old_files;
4318 int already_searched;
4319 char_u *file_line;
4320 char_u *line;
4321 char_u *p;
4322 char_u save_char;
4323 int define_matched;
4324 regmatch_T regmatch;
4325 regmatch_T incl_regmatch;
4326 regmatch_T def_regmatch;
4327 int matched = FALSE;
4328 int did_show = FALSE;
4329 int found = FALSE;
4330 int i;
4331 char_u *already = NULL;
4332 char_u *startp = NULL;
4333#ifdef RISCOS
4334 int previous_munging = __riscosify_control;
4335#endif
4336#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4337 win_T *curwin_save = NULL;
4338#endif
4339
4340 regmatch.regprog = NULL;
4341 incl_regmatch.regprog = NULL;
4342 def_regmatch.regprog = NULL;
4343
4344 file_line = alloc(LSIZE);
4345 if (file_line == NULL)
4346 return;
4347
4348#ifdef RISCOS
4349 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004350 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351#endif
4352
4353 if (type != CHECK_PATH && type != FIND_DEFINE
4354#ifdef FEAT_INS_EXPAND
4355 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4356 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#endif
4359 )
4360 {
4361 pat = alloc(len + 5);
4362 if (pat == NULL)
4363 goto fpip_end;
4364 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4365 /* ignore case according to p_ic, p_scs and pat */
4366 regmatch.rm_ic = ignorecase(pat);
4367 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4368 vim_free(pat);
4369 if (regmatch.regprog == NULL)
4370 goto fpip_end;
4371 }
4372 if (*curbuf->b_p_inc != NUL || *p_inc != NUL)
4373 {
4374 incl_regmatch.regprog = vim_regcomp(*curbuf->b_p_inc == NUL
4375 ? p_inc : curbuf->b_p_inc, p_magic ? RE_MAGIC : 0);
4376 if (incl_regmatch.regprog == NULL)
4377 goto fpip_end;
4378 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4379 }
4380 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4381 {
4382 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4383 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4384 if (def_regmatch.regprog == NULL)
4385 goto fpip_end;
4386 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4387 }
4388 files = (SearchedFile *)lalloc_clear((long_u)
4389 (max_path_depth * sizeof(SearchedFile)), TRUE);
4390 if (files == NULL)
4391 goto fpip_end;
4392 old_files = max_path_depth;
4393 depth = depth_displayed = -1;
4394
4395 lnum = start_lnum;
4396 if (end_lnum > curbuf->b_ml.ml_line_count)
4397 end_lnum = curbuf->b_ml.ml_line_count;
4398 if (lnum > end_lnum) /* do at least one line */
4399 lnum = end_lnum;
4400 line = ml_get(lnum);
4401
4402 for (;;)
4403 {
4404 if (incl_regmatch.regprog != NULL
4405 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4406 {
4407 new_fname = file_name_in_line(incl_regmatch.endp[0],
4408 0, FNAME_EXP|FNAME_INCL|FNAME_REL, 1L,
4409 curr_fname == curbuf->b_fname
4410 ? curbuf->b_ffname : curr_fname);
4411 already_searched = FALSE;
4412 if (new_fname != NULL)
4413 {
4414 /* Check whether we have already searched in this file */
4415 for (i = 0;; i++)
4416 {
4417 if (i == depth + 1)
4418 i = old_files;
4419 if (i == max_path_depth)
4420 break;
4421 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4422 {
4423 if (type != CHECK_PATH &&
4424 action == ACTION_SHOW_ALL && files[i].matched)
4425 {
4426 msg_putchar('\n'); /* cursor below last one */
4427 if (!got_int) /* don't display if 'q'
4428 typed at "--more--"
4429 mesage */
4430 {
4431 msg_home_replace_hl(new_fname);
4432 MSG_PUTS(_(" (includes previously listed match)"));
4433 prev_fname = NULL;
4434 }
4435 }
4436 vim_free(new_fname);
4437 new_fname = NULL;
4438 already_searched = TRUE;
4439 break;
4440 }
4441 }
4442 }
4443
4444 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4445 || (new_fname == NULL && !already_searched)))
4446 {
4447 if (did_show)
4448 msg_putchar('\n'); /* cursor below last one */
4449 else
4450 {
4451 gotocmdline(TRUE); /* cursor at status line */
4452 MSG_PUTS_TITLE(_("--- Included files "));
4453 if (action != ACTION_SHOW_ALL)
4454 MSG_PUTS_TITLE(_("not found "));
4455 MSG_PUTS_TITLE(_("in path ---\n"));
4456 }
4457 did_show = TRUE;
4458 while (depth_displayed < depth && !got_int)
4459 {
4460 ++depth_displayed;
4461 for (i = 0; i < depth_displayed; i++)
4462 MSG_PUTS(" ");
4463 msg_home_replace(files[depth_displayed].name);
4464 MSG_PUTS(" -->\n");
4465 }
4466 if (!got_int) /* don't display if 'q' typed
4467 for "--more--" message */
4468 {
4469 for (i = 0; i <= depth_displayed; i++)
4470 MSG_PUTS(" ");
4471 if (new_fname != NULL)
4472 {
4473 /* using "new_fname" is more reliable, e.g., when
4474 * 'includeexpr' is set. */
4475 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4476 }
4477 else
4478 {
4479 /*
4480 * Isolate the file name.
4481 * Include the surrounding "" or <> if present.
4482 */
4483 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4484 ;
4485 for (i = 0; vim_isfilec(p[i]); i++)
4486 ;
4487 if (i == 0)
4488 {
4489 /* Nothing found, use the rest of the line. */
4490 p = incl_regmatch.endp[0];
4491 i = STRLEN(p);
4492 }
4493 else
4494 {
4495 if (p[-1] == '"' || p[-1] == '<')
4496 {
4497 --p;
4498 ++i;
4499 }
4500 if (p[i] == '"' || p[i] == '>')
4501 ++i;
4502 }
4503 save_char = p[i];
4504 p[i] = NUL;
4505 msg_outtrans_attr(p, hl_attr(HLF_D));
4506 p[i] = save_char;
4507 }
4508
4509 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4510 {
4511 if (already_searched)
4512 MSG_PUTS(_(" (Already listed)"));
4513 else
4514 MSG_PUTS(_(" NOT FOUND"));
4515 }
4516 }
4517 out_flush(); /* output each line directly */
4518 }
4519
4520 if (new_fname != NULL)
4521 {
4522 /* Push the new file onto the file stack */
4523 if (depth + 1 == old_files)
4524 {
4525 bigger = (SearchedFile *)lalloc((long_u)(
4526 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4527 if (bigger != NULL)
4528 {
4529 for (i = 0; i <= depth; i++)
4530 bigger[i] = files[i];
4531 for (i = depth + 1; i < old_files + max_path_depth; i++)
4532 {
4533 bigger[i].fp = NULL;
4534 bigger[i].name = NULL;
4535 bigger[i].lnum = 0;
4536 bigger[i].matched = FALSE;
4537 }
4538 for (i = old_files; i < max_path_depth; i++)
4539 bigger[i + max_path_depth] = files[i];
4540 old_files += max_path_depth;
4541 max_path_depth *= 2;
4542 vim_free(files);
4543 files = bigger;
4544 }
4545 }
4546 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4547 == NULL)
4548 vim_free(new_fname);
4549 else
4550 {
4551 if (++depth == old_files)
4552 {
4553 /*
4554 * lalloc() for 'bigger' must have failed above. We
4555 * will forget one of our already visited files now.
4556 */
4557 vim_free(files[old_files].name);
4558 ++old_files;
4559 }
4560 files[depth].name = curr_fname = new_fname;
4561 files[depth].lnum = 0;
4562 files[depth].matched = FALSE;
4563#ifdef FEAT_INS_EXPAND
4564 if (action == ACTION_EXPAND)
4565 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004566 vim_snprintf((char*)IObuff, IOSIZE,
4567 _("Scanning included file: %s"),
4568 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4570 }
4571#endif
4572 }
4573 }
4574 }
4575 else
4576 {
4577 /*
4578 * Check if the line is a define (type == FIND_DEFINE)
4579 */
4580 p = line;
4581search_line:
4582 define_matched = FALSE;
4583 if (def_regmatch.regprog != NULL
4584 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4585 {
4586 /*
4587 * Pattern must be first identifier after 'define', so skip
4588 * to that position before checking for match of pattern. Also
4589 * don't let it match beyond the end of this identifier.
4590 */
4591 p = def_regmatch.endp[0];
4592 while (*p && !vim_iswordc(*p))
4593 p++;
4594 define_matched = TRUE;
4595 }
4596
4597 /*
4598 * Look for a match. Don't do this if we are looking for a
4599 * define and this line didn't match define_prog above.
4600 */
4601 if (def_regmatch.regprog == NULL || define_matched)
4602 {
4603 if (define_matched
4604#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606#endif
4607 )
4608 {
4609 /* compare the first "len" chars from "ptr" */
4610 startp = skipwhite(p);
4611 if (p_ic)
4612 matched = !MB_STRNICMP(startp, ptr, len);
4613 else
4614 matched = !STRNCMP(startp, ptr, len);
4615 if (matched && define_matched && whole
4616 && vim_iswordc(startp[len]))
4617 matched = FALSE;
4618 }
4619 else if (regmatch.regprog != NULL
4620 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4621 {
4622 matched = TRUE;
4623 startp = regmatch.startp[0];
4624 /*
4625 * Check if the line is not a comment line (unless we are
4626 * looking for a define). A line starting with "# define"
4627 * is not considered to be a comment line.
4628 */
4629 if (!define_matched && skip_comments)
4630 {
4631#ifdef FEAT_COMMENTS
4632 if ((*line != '#' ||
4633 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4634 && get_leader_len(line, NULL, FALSE))
4635 matched = FALSE;
4636
4637 /*
4638 * Also check for a "/ *" or "/ /" before the match.
4639 * Skips lines like "int backwards; / * normal index
4640 * * /" when looking for "normal".
4641 * Note: Doesn't skip "/ *" in comments.
4642 */
4643 p = skipwhite(line);
4644 if (matched
4645 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4646#endif
4647 for (p = line; *p && p < startp; ++p)
4648 {
4649 if (matched
4650 && p[0] == '/'
4651 && (p[1] == '*' || p[1] == '/'))
4652 {
4653 matched = FALSE;
4654 /* After "//" all text is comment */
4655 if (p[1] == '/')
4656 break;
4657 ++p;
4658 }
4659 else if (!matched && p[0] == '*' && p[1] == '/')
4660 {
4661 /* Can find match after "* /". */
4662 matched = TRUE;
4663 ++p;
4664 }
4665 }
4666 }
4667 }
4668 }
4669 }
4670 if (matched)
4671 {
4672#ifdef FEAT_INS_EXPAND
4673 if (action == ACTION_EXPAND)
4674 {
4675 int reuse = 0;
4676 int add_r;
4677 char_u *aux;
4678
4679 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4680 break;
4681 found = TRUE;
4682 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004685 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 if (vim_iswordp(p))
4687 goto exit_matched;
4688 p = find_word_start(p);
4689 }
4690 p = find_word_end(p);
4691 i = (int)(p - aux);
4692
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004693 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 /* get the next line */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004696 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 STRNCPY(IObuff, aux, i);
4698 if (!( depth < 0
4699 && lnum < end_lnum
4700 && (line = ml_get(++lnum)) != NULL)
4701 && !( depth >= 0
4702 && !vim_fgets(line = file_line,
4703 LSIZE, files[depth].fp)))
4704 goto exit_matched;
4705
4706 /* we read a line, set "already" to check this "line" later
4707 * if depth >= 0 we'll increase files[depth].lnum far
4708 * bellow -- Acevedo */
4709 already = aux = p = skipwhite(line);
4710 p = find_word_start(p);
4711 p = find_word_end(p);
4712 if (p > aux)
4713 {
4714 if (*aux != ')' && IObuff[i-1] != TAB)
4715 {
4716 if (IObuff[i-1] != ' ')
4717 IObuff[i++] = ' ';
4718 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4719 if (p_js
4720 && (IObuff[i-2] == '.'
4721 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4722 && (IObuff[i-2] == '?'
4723 || IObuff[i-2] == '!'))))
4724 IObuff[i++] = ' ';
4725 }
4726 /* copy as much as posible of the new word */
4727 if (p - aux >= IOSIZE - i)
4728 p = aux + IOSIZE - i - 1;
4729 STRNCPY(IObuff + i, aux, p - aux);
4730 i += (int)(p - aux);
4731 reuse |= CONT_S_IPOS;
4732 }
4733 IObuff[i] = NUL;
4734 aux = IObuff;
4735
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 goto exit_matched;
4738 }
4739
4740 add_r = ins_compl_add_infercase(aux, i,
4741 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4742 dir, reuse);
4743 if (add_r == OK)
4744 /* if dir was BACKWARD then honor it just once */
4745 dir = FORWARD;
4746 else if (add_r == RET_ERROR)
4747 break;
4748 }
4749 else
4750#endif
4751 if (action == ACTION_SHOW_ALL)
4752 {
4753 found = TRUE;
4754 if (!did_show)
4755 gotocmdline(TRUE); /* cursor at status line */
4756 if (curr_fname != prev_fname)
4757 {
4758 if (did_show)
4759 msg_putchar('\n'); /* cursor below last one */
4760 if (!got_int) /* don't display if 'q' typed
4761 at "--more--" mesage */
4762 msg_home_replace_hl(curr_fname);
4763 prev_fname = curr_fname;
4764 }
4765 did_show = TRUE;
4766 if (!got_int)
4767 show_pat_in_path(line, type, TRUE, action,
4768 (depth == -1) ? NULL : files[depth].fp,
4769 (depth == -1) ? &lnum : &files[depth].lnum,
4770 match_count++);
4771
4772 /* Set matched flag for this file and all the ones that
4773 * include it */
4774 for (i = 0; i <= depth; ++i)
4775 files[i].matched = TRUE;
4776 }
4777 else if (--count <= 0)
4778 {
4779 found = TRUE;
4780 if (depth == -1 && lnum == curwin->w_cursor.lnum
4781#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4782 && g_do_tagpreview == 0
4783#endif
4784 )
4785 EMSG(_("E387: Match is on current line"));
4786 else if (action == ACTION_SHOW)
4787 {
4788 show_pat_in_path(line, type, did_show, action,
4789 (depth == -1) ? NULL : files[depth].fp,
4790 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4791 did_show = TRUE;
4792 }
4793 else
4794 {
4795#ifdef FEAT_GUI
4796 need_mouse_correct = TRUE;
4797#endif
4798#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4799 /* ":psearch" uses the preview window */
4800 if (g_do_tagpreview != 0)
4801 {
4802 curwin_save = curwin;
4803 prepare_tagpreview();
4804 }
4805#endif
4806 if (action == ACTION_SPLIT)
4807 {
4808#ifdef FEAT_WINDOWS
4809 if (win_split(0, 0) == FAIL)
4810#endif
4811 break;
4812#ifdef FEAT_SCROLLBIND
4813 curwin->w_p_scb = FALSE;
4814#endif
4815 }
4816 if (depth == -1)
4817 {
4818 /* match in current file */
4819#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4820 if (g_do_tagpreview != 0)
4821 {
4822 if (getfile(0, curwin_save->w_buffer->b_fname,
4823 NULL, TRUE, lnum, FALSE) > 0)
4824 break; /* failed to jump to file */
4825 }
4826 else
4827#endif
4828 setpcmark();
4829 curwin->w_cursor.lnum = lnum;
4830 }
4831 else
4832 {
4833 if (getfile(0, files[depth].name, NULL, TRUE,
4834 files[depth].lnum, FALSE) > 0)
4835 break; /* failed to jump to file */
4836 /* autocommands may have changed the lnum, we don't
4837 * want that here */
4838 curwin->w_cursor.lnum = files[depth].lnum;
4839 }
4840 }
4841 if (action != ACTION_SHOW)
4842 {
4843 curwin->w_cursor.col = (colnr_T) (startp - line);
4844 curwin->w_set_curswant = TRUE;
4845 }
4846
4847#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4848 if (g_do_tagpreview != 0
4849 && curwin != curwin_save && win_valid(curwin_save))
4850 {
4851 /* Return cursor to where we were */
4852 validate_cursor();
4853 redraw_later(VALID);
4854 win_enter(curwin_save, TRUE);
4855 }
4856#endif
4857 break;
4858 }
4859#ifdef FEAT_INS_EXPAND
4860exit_matched:
4861#endif
4862 matched = FALSE;
4863 /* look for other matches in the rest of the line if we
4864 * are not at the end of it already */
4865 if (def_regmatch.regprog == NULL
4866#ifdef FEAT_INS_EXPAND
4867 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004868 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#endif
4870 && *(p = startp + 1))
4871 goto search_line;
4872 }
4873 line_breakcheck();
4874#ifdef FEAT_INS_EXPAND
4875 if (action == ACTION_EXPAND)
4876 ins_compl_check_keys();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#else
4879 if (got_int)
4880#endif
4881 break;
4882
4883 /*
4884 * Read the next line. When reading an included file and encountering
4885 * end-of-file, close the file and continue in the file that included
4886 * it.
4887 */
4888 while (depth >= 0 && !already
4889 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4890 {
4891 fclose(files[depth].fp);
4892 --old_files;
4893 files[old_files].name = files[depth].name;
4894 files[old_files].matched = files[depth].matched;
4895 --depth;
4896 curr_fname = (depth == -1) ? curbuf->b_fname
4897 : files[depth].name;
4898 if (depth < depth_displayed)
4899 depth_displayed = depth;
4900 }
4901 if (depth >= 0) /* we could read the line */
4902 files[depth].lnum++;
4903 else if (!already)
4904 {
4905 if (++lnum > end_lnum)
4906 break;
4907 line = ml_get(lnum);
4908 }
4909 already = NULL;
4910 }
4911 /* End of big for (;;) loop. */
4912
4913 /* Close any files that are still open. */
4914 for (i = 0; i <= depth; i++)
4915 {
4916 fclose(files[i].fp);
4917 vim_free(files[i].name);
4918 }
4919 for (i = old_files; i < max_path_depth; i++)
4920 vim_free(files[i].name);
4921 vim_free(files);
4922
4923 if (type == CHECK_PATH)
4924 {
4925 if (!did_show)
4926 {
4927 if (action != ACTION_SHOW_ALL)
4928 MSG(_("All included files were found"));
4929 else
4930 MSG(_("No included files"));
4931 }
4932 }
4933 else if (!found
4934#ifdef FEAT_INS_EXPAND
4935 && action != ACTION_EXPAND
4936#endif
4937 )
4938 {
4939#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004940 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#else
4942 if (got_int)
4943#endif
4944 EMSG(_(e_interr));
4945 else if (type == FIND_DEFINE)
4946 EMSG(_("E388: Couldn't find definition"));
4947 else
4948 EMSG(_("E389: Couldn't find pattern"));
4949 }
4950 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4951 msg_end();
4952
4953fpip_end:
4954 vim_free(file_line);
4955 vim_free(regmatch.regprog);
4956 vim_free(incl_regmatch.regprog);
4957 vim_free(def_regmatch.regprog);
4958
4959#ifdef RISCOS
4960 /* Restore previous file munging state. */
4961 __riscosify_control = previous_munging;
4962#endif
4963}
4964
4965 static void
4966show_pat_in_path(line, type, did_show, action, fp, lnum, count)
4967 char_u *line;
4968 int type;
4969 int did_show;
4970 int action;
4971 FILE *fp;
4972 linenr_T *lnum;
4973 long count;
4974{
4975 char_u *p;
4976
4977 if (did_show)
4978 msg_putchar('\n'); /* cursor below last one */
4979 else
4980 gotocmdline(TRUE); /* cursor at status line */
4981 if (got_int) /* 'q' typed at "--more--" message */
4982 return;
4983 for (;;)
4984 {
4985 p = line + STRLEN(line) - 1;
4986 if (fp != NULL)
4987 {
4988 /* We used fgets(), so get rid of newline at end */
4989 if (p >= line && *p == '\n')
4990 --p;
4991 if (p >= line && *p == '\r')
4992 --p;
4993 *(p + 1) = NUL;
4994 }
4995 if (action == ACTION_SHOW_ALL)
4996 {
4997 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
4998 msg_puts(IObuff);
4999 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5000 /* Highlight line numbers */
5001 msg_puts_attr(IObuff, hl_attr(HLF_N));
5002 MSG_PUTS(" ");
5003 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005004 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 out_flush(); /* show one line at a time */
5006
5007 /* Definition continues until line that doesn't end with '\' */
5008 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5009 break;
5010
5011 if (fp != NULL)
5012 {
5013 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5014 break;
5015 ++*lnum;
5016 }
5017 else
5018 {
5019 if (++*lnum > curbuf->b_ml.ml_line_count)
5020 break;
5021 line = ml_get(*lnum);
5022 }
5023 msg_putchar('\n');
5024 }
5025}
5026#endif
5027
5028#ifdef FEAT_VIMINFO
5029 int
5030read_viminfo_search_pattern(virp, force)
5031 vir_T *virp;
5032 int force;
5033{
5034 char_u *lp;
5035 int idx = -1;
5036 int magic = FALSE;
5037 int no_scs = FALSE;
5038 int off_line = FALSE;
5039 int off_end = FALSE;
5040 long off = 0;
5041 int setlast = FALSE;
5042#ifdef FEAT_SEARCH_EXTRA
5043 static int hlsearch_on = FALSE;
5044#endif
5045 char_u *val;
5046
5047 /*
5048 * Old line types:
5049 * "/pat", "&pat": search/subst. pat
5050 * "~/pat", "~&pat": last used search/subst. pat
5051 * New line types:
5052 * "~h", "~H": hlsearch highlighting off/on
5053 * "~<magic><smartcase><line><end><off><last><which>pat"
5054 * <magic>: 'm' off, 'M' on
5055 * <smartcase>: 's' off, 'S' on
5056 * <line>: 'L' line offset, 'l' char offset
5057 * <end>: 'E' from end, 'e' from start
5058 * <off>: decimal, offset
5059 * <last>: '~' last used pattern
5060 * <which>: '/' search pat, '&' subst. pat
5061 */
5062 lp = virp->vir_line;
5063 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5064 {
5065 if (lp[1] == 'M') /* magic on */
5066 magic = TRUE;
5067 if (lp[2] == 's')
5068 no_scs = TRUE;
5069 if (lp[3] == 'L')
5070 off_line = TRUE;
5071 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005072 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 lp += 5;
5074 off = getdigits(&lp);
5075 }
5076 if (lp[0] == '~') /* use this pattern for last-used pattern */
5077 {
5078 setlast = TRUE;
5079 lp++;
5080 }
5081 if (lp[0] == '/')
5082 idx = RE_SEARCH;
5083 else if (lp[0] == '&')
5084 idx = RE_SUBST;
5085#ifdef FEAT_SEARCH_EXTRA
5086 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5087 hlsearch_on = FALSE;
5088 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5089 hlsearch_on = TRUE;
5090#endif
5091 if (idx >= 0)
5092 {
5093 if (force || spats[idx].pat == NULL)
5094 {
5095 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5096 TRUE);
5097 if (val != NULL)
5098 {
5099 set_last_search_pat(val, idx, magic, setlast);
5100 vim_free(val);
5101 spats[idx].no_scs = no_scs;
5102 spats[idx].off.line = off_line;
5103 spats[idx].off.end = off_end;
5104 spats[idx].off.off = off;
5105#ifdef FEAT_SEARCH_EXTRA
5106 if (setlast)
5107 no_hlsearch = !hlsearch_on;
5108#endif
5109 }
5110 }
5111 }
5112 return viminfo_readline(virp);
5113}
5114
5115 void
5116write_viminfo_search_pattern(fp)
5117 FILE *fp;
5118{
5119 if (get_viminfo_parameter('/') != 0)
5120 {
5121#ifdef FEAT_SEARCH_EXTRA
5122 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5123 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5124#endif
5125 wvsp_one(fp, RE_SEARCH, "", '/');
5126 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5127 }
5128}
5129
5130 static void
5131wvsp_one(fp, idx, s, sc)
5132 FILE *fp; /* file to write to */
5133 int idx; /* spats[] index */
5134 char *s; /* search pat */
5135 int sc; /* dir char */
5136{
5137 if (spats[idx].pat != NULL)
5138 {
5139 fprintf(fp, "\n# Last %sSearch Pattern:\n~", s);
5140 /* off.dir is not stored, it's reset to forward */
5141 fprintf(fp, "%c%c%c%c%ld%s%c",
5142 spats[idx].magic ? 'M' : 'm', /* magic */
5143 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5144 spats[idx].off.line ? 'L' : 'l', /* line offset */
5145 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5146 spats[idx].off.off, /* offset */
5147 last_idx == idx ? "~" : "", /* last used pat */
5148 sc);
5149 viminfo_writestring(fp, spats[idx].pat);
5150 }
5151}
5152#endif /* FEAT_VIMINFO */