blob: 238fc8160884ec5af9da8b26700ca6440d699016 [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
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016static void set_vv_searchforward __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000037/*
38 * This file contains various searching-related routines. These fall into
39 * three groups:
40 * 1. string searches (for /, ?, n, and N)
41 * 2. character searches within a single line (for f, F, t, T, etc)
42 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
43 */
44
45/*
46 * String searches
47 *
48 * The string search functions are divided into two levels:
49 * lowest: searchit(); uses an pos_T for starting position and found match.
50 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
51 *
52 * The last search pattern is remembered for repeating the same search.
53 * This pattern is shared between the :g, :s, ? and / commands.
54 * This is in search_regcomp().
55 *
56 * The actual string matching is done using a heavily modified version of
57 * Henry Spencer's regular expression library. See regexp.c.
58 */
59
60/* The offset for a search command is store in a soff struct */
61/* Note: only spats[0].off is really used */
62struct soffset
63{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000064 int dir; /* search direction, '/' or '?' */
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 int line; /* search has line offset */
66 int end; /* search set cursor at end */
67 long off; /* line or char offset */
68};
69
70/* A search pattern and its attributes are stored in a spat struct */
71struct spat
72{
73 char_u *pat; /* the pattern (in allocated memory) or NULL */
74 int magic; /* magicness of the pattern */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020075 int no_scs; /* no smartcase for this pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 struct soffset off;
77};
78
79/*
80 * Two search patterns are remembered: One for the :substitute command and
81 * one for other searches. last_idx points to the one that was used the last
82 * time.
83 */
84static struct spat spats[2] =
85{
86 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
87 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
88};
89
90static int last_idx = 0; /* index in spats[] for RE_LAST */
91
92#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
93/* copy of spats[], for keeping the search patterns while executing autocmds */
94static struct spat saved_spats[2];
95static int saved_last_idx = 0;
96# ifdef FEAT_SEARCH_EXTRA
97static int saved_no_hlsearch = 0;
98# endif
99#endif
100
101static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
102#ifdef FEAT_RIGHTLEFT
103static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
106#ifdef FEAT_FIND_ID
107/*
108 * Type used by find_pattern_in_path() to remember which included files have
109 * been searched already.
110 */
111typedef struct SearchedFile
112{
113 FILE *fp; /* File pointer */
114 char_u *name; /* Full name of file */
115 linenr_T lnum; /* Line we were up to in file */
116 int matched; /* Found a match in this file */
117} SearchedFile;
118#endif
119
120/*
121 * translate search pattern for vim_regcomp()
122 *
123 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
124 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
125 * pat_save == RE_BOTH: save pat in both patterns (:global command)
126 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000127 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
129 * options & SEARCH_HIS: put search string in history
130 * options & SEARCH_KEEP: keep previous search pattern
131 *
132 * returns FAIL if failed, OK otherwise.
133 */
134 int
135search_regcomp(pat, pat_save, pat_use, options, regmatch)
136 char_u *pat;
137 int pat_save;
138 int pat_use;
139 int options;
140 regmmatch_T *regmatch; /* return: pattern and ignore-case flag */
141{
142 int magic;
143 int i;
144
145 rc_did_emsg = FALSE;
146 magic = p_magic;
147
148 /*
149 * If no pattern given, use a previously defined pattern.
150 */
151 if (pat == NULL || *pat == NUL)
152 {
153 if (pat_use == RE_LAST)
154 i = last_idx;
155 else
156 i = pat_use;
157 if (spats[i].pat == NULL) /* pattern was never defined */
158 {
159 if (pat_use == RE_SUBST)
160 EMSG(_(e_nopresub));
161 else
162 EMSG(_(e_noprevre));
163 rc_did_emsg = TRUE;
164 return FAIL;
165 }
166 pat = spats[i].pat;
167 magic = spats[i].magic;
168 no_smartcase = spats[i].no_scs;
169 }
170#ifdef FEAT_CMDHIST
171 else if (options & SEARCH_HIS) /* put new pattern in history */
172 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
173#endif
174
175#ifdef FEAT_RIGHTLEFT
176 if (mr_pattern_alloced)
177 {
178 vim_free(mr_pattern);
179 mr_pattern_alloced = FALSE;
180 }
181
182 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
183 {
184 char_u *rev_pattern;
185
186 rev_pattern = reverse_text(pat);
187 if (rev_pattern == NULL)
188 mr_pattern = pat; /* out of memory, keep normal pattern. */
189 else
190 {
191 mr_pattern = rev_pattern;
192 mr_pattern_alloced = TRUE;
193 }
194 }
195 else
196#endif
197 mr_pattern = pat;
198
199 /*
200 * Save the currently used pattern in the appropriate place,
201 * unless the pattern should not be remembered.
202 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100203 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 {
205 /* search or global command */
206 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
207 save_re_pat(RE_SEARCH, pat, magic);
208 /* substitute or global command */
209 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
210 save_re_pat(RE_SUBST, pat, magic);
211 }
212
213 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000214 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
216 if (regmatch->regprog == NULL)
217 return FAIL;
218 return OK;
219}
220
221/*
222 * Get search pattern used by search_regcomp().
223 */
224 char_u *
225get_search_pat()
226{
227 return mr_pattern;
228}
229
Bram Moolenaarabc97732007-08-08 20:49:37 +0000230#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/*
232 * Reverse text into allocated memory.
233 * Returns the allocated string, NULL when out of memory.
234 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000235 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236reverse_text(s)
237 char_u *s;
238{
239 unsigned len;
240 unsigned s_i, rev_i;
241 char_u *rev;
242
243 /*
244 * Reverse the pattern.
245 */
246 len = (unsigned)STRLEN(s);
247 rev = alloc(len + 1);
248 if (rev != NULL)
249 {
250 rev_i = len;
251 for (s_i = 0; s_i < len; ++s_i)
252 {
253# ifdef FEAT_MBYTE
254 if (has_mbyte)
255 {
256 int mb_len;
257
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000258 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 rev_i -= mb_len;
260 mch_memmove(rev + rev_i, s + s_i, mb_len);
261 s_i += mb_len - 1;
262 }
263 else
264# endif
265 rev[--rev_i] = s[s_i];
266
267 }
268 rev[len] = NUL;
269 }
270 return rev;
271}
272#endif
273
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100274 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275save_re_pat(idx, pat, magic)
276 int idx;
277 char_u *pat;
278 int magic;
279{
280 if (spats[idx].pat != pat)
281 {
282 vim_free(spats[idx].pat);
283 spats[idx].pat = vim_strsave(pat);
284 spats[idx].magic = magic;
285 spats[idx].no_scs = no_smartcase;
286 last_idx = idx;
287#ifdef FEAT_SEARCH_EXTRA
288 /* If 'hlsearch' set and search pat changed: need redraw. */
289 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000290 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100291 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
293 }
294}
295
296#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
297/*
298 * Save the search patterns, so they can be restored later.
299 * Used before/after executing autocommands and user functions.
300 */
301static int save_level = 0;
302
303 void
304save_search_patterns()
305{
306 if (save_level++ == 0)
307 {
308 saved_spats[0] = spats[0];
309 if (spats[0].pat != NULL)
310 saved_spats[0].pat = vim_strsave(spats[0].pat);
311 saved_spats[1] = spats[1];
312 if (spats[1].pat != NULL)
313 saved_spats[1].pat = vim_strsave(spats[1].pat);
314 saved_last_idx = last_idx;
315# ifdef FEAT_SEARCH_EXTRA
316 saved_no_hlsearch = no_hlsearch;
317# endif
318 }
319}
320
321 void
322restore_search_patterns()
323{
324 if (--save_level == 0)
325 {
326 vim_free(spats[0].pat);
327 spats[0] = saved_spats[0];
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000328#if defined(FEAT_EVAL)
329 set_vv_searchforward();
330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 vim_free(spats[1].pat);
332 spats[1] = saved_spats[1];
333 last_idx = saved_last_idx;
334# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100335 SET_NO_HLSEARCH(saved_no_hlsearch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336# endif
337 }
338}
339#endif
340
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000341#if defined(EXITFREE) || defined(PROTO)
342 void
343free_search_patterns()
344{
345 vim_free(spats[0].pat);
346 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000347
348# ifdef FEAT_RIGHTLEFT
349 if (mr_pattern_alloced)
350 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200351 vim_free(mr_pattern);
352 mr_pattern_alloced = FALSE;
353 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000354 }
355# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000356}
357#endif
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359/*
360 * Return TRUE when case should be ignored for search pattern "pat".
361 * Uses the 'ignorecase' and 'smartcase' options.
362 */
363 int
364ignorecase(pat)
365 char_u *pat;
366{
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200367 int ic = p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 if (ic && !no_smartcase && p_scs
370#ifdef FEAT_INS_EXPAND
371 && !(ctrl_x_mode && curbuf->b_p_inf)
372#endif
373 )
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200374 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 no_smartcase = FALSE;
376
377 return ic;
378}
379
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200380/*
381 * Return TRUE if patter "pat" has an uppercase character.
382 */
383 int
384pat_has_uppercase(pat)
385 char_u *pat;
386{
387 char_u *p = pat;
388
389 while (*p != NUL)
390 {
391#ifdef FEAT_MBYTE
392 int l;
393
394 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
395 {
396 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
397 return TRUE;
398 p += l;
399 }
400 else
401#endif
402 if (*p == '\\')
403 {
404 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
405 p += 3;
406 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
407 p += 3;
408 else if (p[1] != NUL) /* skip "\X" */
409 p += 2;
410 else
411 p += 1;
412 }
413 else if (MB_ISUPPER(*p))
414 return TRUE;
415 else
416 ++p;
417 }
418 return FALSE;
419}
420
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 char_u *
422last_search_pat()
423{
424 return spats[last_idx].pat;
425}
426
427/*
428 * Reset search direction to forward. For "gd" and "gD" commands.
429 */
430 void
431reset_search_dir()
432{
433 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000434#if defined(FEAT_EVAL)
435 set_vv_searchforward();
436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437}
438
439#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
440/*
441 * Set the last search pattern. For ":let @/ =" and viminfo.
442 * Also set the saved search pattern, so that this works in an autocommand.
443 */
444 void
445set_last_search_pat(s, idx, magic, setlast)
446 char_u *s;
447 int idx;
448 int magic;
449 int setlast;
450{
451 vim_free(spats[idx].pat);
452 /* An empty string means that nothing should be matched. */
453 if (*s == NUL)
454 spats[idx].pat = NULL;
455 else
456 spats[idx].pat = vim_strsave(s);
457 spats[idx].magic = magic;
458 spats[idx].no_scs = FALSE;
459 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#if defined(FEAT_EVAL)
461 set_vv_searchforward();
462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 spats[idx].off.line = FALSE;
464 spats[idx].off.end = FALSE;
465 spats[idx].off.off = 0;
466 if (setlast)
467 last_idx = idx;
468 if (save_level)
469 {
470 vim_free(saved_spats[idx].pat);
471 saved_spats[idx] = spats[0];
472 if (spats[idx].pat == NULL)
473 saved_spats[idx].pat = NULL;
474 else
475 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
476 saved_last_idx = last_idx;
477 }
478# ifdef FEAT_SEARCH_EXTRA
479 /* If 'hlsearch' set and search pat changed: need redraw. */
480 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000481 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482# endif
483}
484#endif
485
486#ifdef FEAT_SEARCH_EXTRA
487/*
488 * Get a regexp program for the last used search pattern.
489 * This is used for highlighting all matches in a window.
490 * Values returned in regmatch->regprog and regmatch->rmm_ic.
491 */
492 void
493last_pat_prog(regmatch)
494 regmmatch_T *regmatch;
495{
496 if (spats[last_idx].pat == NULL)
497 {
498 regmatch->regprog = NULL;
499 return;
500 }
501 ++emsg_off; /* So it doesn't beep if bad expr */
502 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
503 --emsg_off;
504}
505#endif
506
507/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100508 * Lowest level search function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
510 * Start at position 'pos' and return the found position in 'pos'.
511 *
512 * if (options & SEARCH_MSG) == 0 don't give any messages
513 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
514 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
515 * if (options & SEARCH_HIS) put search pattern in history
516 * if (options & SEARCH_END) return position at end of match
517 * if (options & SEARCH_START) accept match at pos itself
518 * if (options & SEARCH_KEEP) keep previous search pattern
519 * if (options & SEARCH_FOLD) match only once in a closed fold
520 * if (options & SEARCH_PEEK) check for typed char, cancel search
521 *
522 * Return FAIL (zero) for failure, non-zero for success.
523 * When FEAT_EVAL is defined, returns the index of the first matching
524 * subpattern plus one; one if there was none.
525 */
526 int
Bram Moolenaar76929292008-01-06 19:07:36 +0000527searchit(win, buf, pos, dir, pat, count, options, pat_use, stop_lnum, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 win_T *win; /* window to search in; can be NULL for a
529 buffer without a window! */
530 buf_T *buf;
531 pos_T *pos;
532 int dir;
533 char_u *pat;
534 long count;
535 int options;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000536 int pat_use; /* which pattern to use when "pat" is empty */
537 linenr_T stop_lnum; /* stop after this line number when != 0 */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000538 proftime_T *tm UNUSED; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 int found;
541 linenr_T lnum; /* no init to shut up Apollo cc */
542 regmmatch_T regmatch;
543 char_u *ptr;
544 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000546 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 int loop;
548 pos_T start_pos;
549 int at_first_line;
550 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200551 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 int match_ok;
553 long nmatched;
554 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100555 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000556 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#ifdef FEAT_SEARCH_EXTRA
558 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560
561 if (search_regcomp(pat, RE_SEARCH, pat_use,
562 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
563 {
564 if ((options & SEARCH_MSG) && !rc_did_emsg)
565 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
566 return FAIL;
567 }
568
Bram Moolenaar280f1262006-01-30 00:14:18 +0000569 /*
570 * find the string
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 called_emsg = FALSE;
573 do /* loop for count */
574 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100575 /* When not accepting a match at the start position set "extra_col" to
576 * a non-zero value. Don't do that when starting at MAXCOL, since
577 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200578 if (pos->col == MAXCOL)
579 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100580#ifdef FEAT_MBYTE
581 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200582 else if (has_mbyte
583 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
584 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100585 {
586 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
587 if (*ptr == NUL)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200588 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100589 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200590 start_char_len = (*mb_ptr2len)(ptr);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100591 }
592#endif
593 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200594 start_char_len = 1;
595 if (dir == FORWARD)
596 {
597 if (options & SEARCH_START)
598 extra_col = 0;
599 else
600 extra_col = start_char_len;
601 }
602 else
603 {
604 if (options & SEARCH_START)
605 extra_col = start_char_len;
606 else
607 extra_col = 0;
608 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 start_pos = *pos; /* remember start pos for detecting no match */
611 found = 0; /* default: not found */
612 at_first_line = TRUE; /* default: start in first line */
613 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
614 {
615 pos->lnum = 1;
616 pos->col = 0;
617 at_first_line = FALSE; /* not in first line now */
618 }
619
620 /*
621 * Start searching in current line, unless searching backwards and
622 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000623 * If we are searching backwards, in column 0, and not including the
624 * current position, gain some efficiency by skipping back a line.
625 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000627 if (dir == BACKWARD && start_pos.col == 0
628 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {
630 lnum = pos->lnum - 1;
631 at_first_line = FALSE;
632 }
633 else
634 lnum = pos->lnum;
635
636 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
637 {
638 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
639 lnum += dir, at_first_line = FALSE)
640 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000641 /* Stop after checking "stop_lnum", if it's set. */
642 if (stop_lnum != 0 && (dir == FORWARD
643 ? lnum > stop_lnum : lnum < stop_lnum))
644 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000645#ifdef FEAT_RELTIME
646 /* Stop after passing the "tm" time limit. */
647 if (tm != NULL && profile_passed_limit(tm))
648 break;
649#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000652 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000655 lnum, (colnr_T)0,
656#ifdef FEAT_RELTIME
657 tm
658#else
659 NULL
660#endif
661 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 /* Abort searching on an error (e.g., out of stack). */
663 if (called_emsg)
664 break;
665 if (nmatched > 0)
666 {
667 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000668 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000670#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000672#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000673 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000674 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
675 ptr = (char_u *)"";
676 else
677 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 /*
680 * Forward search in the first line: match should be after
681 * the start position. If not, continue at the end of the
682 * match (this is vi compatible) or on the next char.
683 */
684 if (dir == FORWARD && at_first_line)
685 {
686 match_ok = TRUE;
687 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000688 * When the match starts in a next line it's certainly
689 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 * When match lands on a NUL the cursor will be put
691 * one back afterwards, compare with that position,
692 * otherwise "/$" will get stuck on end of line.
693 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000694 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100695 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000696 ? (nmatched == 1
697 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000699 : ((int)matchpos.col
700 - (ptr[matchpos.col] == NUL)
701 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 /*
704 * If vi-compatible searching, continue at the end
705 * of the match, otherwise continue one position
706 * forward.
707 */
708 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
709 {
710 if (nmatched > 1)
711 {
712 /* end is in next line, thus no match in
713 * this line */
714 match_ok = FALSE;
715 break;
716 }
717 matchcol = endpos.col;
718 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000719 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 && ptr[matchcol] != NUL)
721 {
722#ifdef FEAT_MBYTE
723 if (has_mbyte)
724 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000725 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 else
727#endif
728 ++matchcol;
729 }
730 }
731 else
732 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000733 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 if (ptr[matchcol] != NUL)
735 {
736#ifdef FEAT_MBYTE
737 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000738 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 + matchcol);
740 else
741#endif
742 ++matchcol;
743 }
744 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200745 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 if (ptr[matchcol] == NUL
748 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000749 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000750 matchcol,
751#ifdef FEAT_RELTIME
752 tm
753#else
754 NULL
755#endif
756 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
758 match_ok = FALSE;
759 break;
760 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000761 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 endpos = regmatch.endpos[0];
763# ifdef FEAT_EVAL
764 submatch = first_submatch(&regmatch);
765# endif
766
767 /* Need to get the line pointer again, a
768 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000769 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771 if (!match_ok)
772 continue;
773 }
774 if (dir == BACKWARD)
775 {
776 /*
777 * Now, if there are multiple matches on this line,
778 * we have to get the last one. Or the last one before
779 * the cursor, if we're on that line.
780 * When putting the new cursor at the end, compare
781 * relative to the end of the match.
782 */
783 match_ok = FALSE;
784 for (;;)
785 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000786 /* Remember a position that is before the start
787 * position, we use it if it's the last match in
788 * the line. Always accept a position after
789 * wrapping around. */
790 if (loop
791 || ((options & SEARCH_END)
792 ? (lnum + regmatch.endpos[0].lnum
793 < start_pos.lnum
794 || (lnum + regmatch.endpos[0].lnum
795 == start_pos.lnum
796 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200797 < (int)start_pos.col
798 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000799 : (lnum + regmatch.startpos[0].lnum
800 < start_pos.lnum
801 || (lnum + regmatch.startpos[0].lnum
802 == start_pos.lnum
803 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200804 < (int)start_pos.col
805 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000808 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 endpos = regmatch.endpos[0];
810# ifdef FEAT_EVAL
811 submatch = first_submatch(&regmatch);
812# endif
813 }
814 else
815 break;
816
817 /*
818 * We found a valid match, now check if there is
819 * another one after it.
820 * If vi-compatible searching, continue at the end
821 * of the match, otherwise continue one position
822 * forward.
823 */
824 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
825 {
826 if (nmatched > 1)
827 break;
828 matchcol = endpos.col;
829 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000830 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 && ptr[matchcol] != NUL)
832 {
833#ifdef FEAT_MBYTE
834 if (has_mbyte)
835 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000836 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 else
838#endif
839 ++matchcol;
840 }
841 }
842 else
843 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000844 /* Stop when the match is in a next line. */
845 if (matchpos.lnum > 0)
846 break;
847 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (ptr[matchcol] != NUL)
849 {
850#ifdef FEAT_MBYTE
851 if (has_mbyte)
852 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000853 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 else
855#endif
856 ++matchcol;
857 }
858 }
859 if (ptr[matchcol] == NUL
860 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000861 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000862 matchcol,
863#ifdef FEAT_RELTIME
864 tm
865#else
866 NULL
867#endif
868 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 break;
870
871 /* Need to get the line pointer again, a
872 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000873 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875
876 /*
877 * If there is only a match after the cursor, skip
878 * this match.
879 */
880 if (!match_ok)
881 continue;
882 }
883
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000884 /* With the SEARCH_END option move to the last character
885 * of the match. Don't do it for an empty match, end
886 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200887 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000888 && !(matchpos.lnum == endpos.lnum
889 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000891 /* For a match in the first column, set the position
892 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000893 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000894 pos->col = endpos.col;
895 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000896 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000897 if (pos->lnum > 1) /* just in case */
898 {
899 --pos->lnum;
900 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
901 pos->lnum, FALSE));
902 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000903 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000904 else
905 {
906 --pos->col;
907#ifdef FEAT_MBYTE
908 if (has_mbyte
909 && pos->lnum <= buf->b_ml.ml_line_count)
910 {
911 ptr = ml_get_buf(buf, pos->lnum, FALSE);
912 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
913 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000914#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917 else
918 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000919 pos->lnum = lnum + matchpos.lnum;
920 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922#ifdef FEAT_VIRTUALEDIT
923 pos->coladd = 0;
924#endif
925 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100926 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
928 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000929 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 search_match_endcol = endpos.col;
931 break;
932 }
933 line_breakcheck(); /* stop if ctrl-C typed */
934 if (got_int)
935 break;
936
937#ifdef FEAT_SEARCH_EXTRA
938 /* Cancel searching if a character was typed. Used for
939 * 'incsearch'. Don't check too often, that would slowdown
940 * searching too much. */
941 if ((options & SEARCH_PEEK)
942 && ((lnum - pos->lnum) & 0x3f) == 0
943 && char_avail())
944 {
945 break_loop = TRUE;
946 break;
947 }
948#endif
949
950 if (loop && lnum == start_pos.lnum)
951 break; /* if second loop, stop where started */
952 }
953 at_first_line = FALSE;
954
955 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000956 * Stop the search if wrapscan isn't set, "stop_lnum" is
957 * specified, after an interrupt, after a match and after looping
958 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000960 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaar78a15312009-05-15 19:33:18 +0000961#ifdef FEAT_SEARCH_EXTRA
962 || break_loop
963#endif
964 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 break;
966
967 /*
968 * If 'wrapscan' is set we continue at the other end of the file.
969 * If 'shortmess' does not contain 's', we give a message.
970 * This message is also remembered in keep_msg for when the screen
971 * is redrawn. The keep_msg is cleared whenever another message is
972 * written.
973 */
974 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +0000978 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
979 give_warning((char_u *)_(dir == BACKWARD
980 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
Bram Moolenaar78a15312009-05-15 19:33:18 +0000982 if (got_int || called_emsg
983#ifdef FEAT_SEARCH_EXTRA
984 || break_loop
985#endif
986 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 break;
988 }
989 while (--count > 0 && found); /* stop after count matches or no match */
990
Bram Moolenaar473de612013-06-08 18:19:48 +0200991 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar280f1262006-01-30 00:14:18 +0000993 called_emsg |= save_called_emsg;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (!found) /* did not find it */
996 {
997 if (got_int)
998 EMSG(_(e_interr));
999 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1000 {
1001 if (p_ws)
1002 EMSG2(_(e_patnotf2), mr_pattern);
1003 else if (lnum == 0)
1004 EMSG2(_("E384: search hit TOP without match for: %s"),
1005 mr_pattern);
1006 else
1007 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
1008 mr_pattern);
1009 }
1010 return FAIL;
1011 }
1012
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001013 /* A pattern like "\n\zs" may go past the last line. */
1014 if (pos->lnum > buf->b_ml.ml_line_count)
1015 {
1016 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001017 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001018 if (pos->col > 0)
1019 --pos->col;
1020 }
1021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 return submatch + 1;
1023}
1024
1025#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001026 void
1027set_search_direction(cdir)
1028 int cdir;
1029{
1030 spats[0].off.dir = cdir;
1031}
1032
1033 static void
1034set_vv_searchforward()
1035{
1036 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1037}
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039/*
1040 * Return the number of the first subpat that matched.
1041 */
1042 static int
1043first_submatch(rp)
1044 regmmatch_T *rp;
1045{
1046 int submatch;
1047
1048 for (submatch = 1; ; ++submatch)
1049 {
1050 if (rp->startpos[submatch].lnum >= 0)
1051 break;
1052 if (submatch == 9)
1053 {
1054 submatch = 0;
1055 break;
1056 }
1057 }
1058 return submatch;
1059}
1060#endif
1061
1062/*
1063 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001064 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 * If 'dirc' is 0: use previous dir.
1066 * If 'pat' is NULL or empty : use previous string.
1067 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1068 * If 'options & SEARCH_ECHO': echo the search command and handle options
1069 * If 'options & SEARCH_MSG' : may give error message
1070 * If 'options & SEARCH_OPT' : interpret optional flags
1071 * If 'options & SEARCH_HIS' : put search pattern in history
1072 * If 'options & SEARCH_NOOF': don't add offset to position
1073 * If 'options & SEARCH_MARK': set previous context mark
1074 * If 'options & SEARCH_KEEP': keep previous search pattern
1075 * If 'options & SEARCH_START': accept match at curpos itself
1076 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1077 *
1078 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1079 * makes the movement linewise without moving the match position.
1080 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001081 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 */
1083 int
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001084do_search(oap, dirc, pat, count, options, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 oparg_T *oap; /* can be NULL */
1086 int dirc; /* '/' or '?' */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001087 char_u *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 long count;
1089 int options;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001090 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 pos_T pos; /* position of the last match */
1093 char_u *searchstr;
1094 struct soffset old_off;
1095 int retval; /* Return value */
1096 char_u *p;
1097 long c;
1098 char_u *dircp;
1099 char_u *strcopy = NULL;
1100 char_u *ps;
1101
1102 /*
1103 * A line offset is not remembered, this is vi compatible.
1104 */
1105 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1106 {
1107 spats[0].off.line = FALSE;
1108 spats[0].off.off = 0;
1109 }
1110
1111 /*
1112 * Save the values for when (options & SEARCH_KEEP) is used.
1113 * (there is no "if ()" around this because gcc wants them initialized)
1114 */
1115 old_off = spats[0].off;
1116
1117 pos = curwin->w_cursor; /* start searching at the cursor position */
1118
1119 /*
1120 * Find out the direction of the search.
1121 */
1122 if (dirc == 0)
1123 dirc = spats[0].off.dir;
1124 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001127#if defined(FEAT_EVAL)
1128 set_vv_searchforward();
1129#endif
1130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (options & SEARCH_REV)
1132 {
1133#ifdef WIN32
1134 /* There is a bug in the Visual C++ 2.2 compiler which means that
1135 * dirc always ends up being '/' */
1136 dirc = (dirc == '/') ? '?' : '/';
1137#else
1138 if (dirc == '/')
1139 dirc = '?';
1140 else
1141 dirc = '/';
1142#endif
1143 }
1144
1145#ifdef FEAT_FOLDING
1146 /* If the cursor is in a closed fold, don't find another match in the same
1147 * fold. */
1148 if (dirc == '/')
1149 {
1150 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1151 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1152 }
1153 else
1154 {
1155 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1156 pos.col = 0;
1157 }
1158#endif
1159
1160#ifdef FEAT_SEARCH_EXTRA
1161 /*
1162 * Turn 'hlsearch' highlighting back on.
1163 */
1164 if (no_hlsearch && !(options & SEARCH_KEEP))
1165 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001166 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01001167 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169#endif
1170
1171 /*
1172 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1173 */
1174 for (;;)
1175 {
1176 searchstr = pat;
1177 dircp = NULL;
1178 /* use previous pattern */
1179 if (pat == NULL || *pat == NUL || *pat == dirc)
1180 {
1181 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1182 {
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001183 pat = spats[RE_SUBST].pat;
1184 if (pat == NULL)
1185 {
1186 EMSG(_(e_noprevre));
1187 retval = 0;
1188 goto end_do_search;
1189 }
1190 searchstr = pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001192 else
1193 {
1194 /* make search_regcomp() use spats[RE_SEARCH].pat */
1195 searchstr = (char_u *)"";
1196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198
1199 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1200 {
1201 /*
1202 * Find end of regular expression.
1203 * If there is a matching '/' or '?', toss it.
1204 */
1205 ps = strcopy;
1206 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1207 if (strcopy != ps)
1208 {
1209 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001210 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 pat = strcopy;
1212 searchstr = strcopy;
1213 }
1214 if (*p == dirc)
1215 {
1216 dircp = p; /* remember where we put the NUL */
1217 *p++ = NUL;
1218 }
1219 spats[0].off.line = FALSE;
1220 spats[0].off.end = FALSE;
1221 spats[0].off.off = 0;
1222 /*
1223 * Check for a line offset or a character offset.
1224 * For get_address (echo off) we don't check for a character
1225 * offset, because it is meaningless and the 's' could be a
1226 * substitute command.
1227 */
1228 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1229 spats[0].off.line = TRUE;
1230 else if ((options & SEARCH_OPT) &&
1231 (*p == 'e' || *p == 's' || *p == 'b'))
1232 {
1233 if (*p == 'e') /* end */
1234 spats[0].off.end = SEARCH_END;
1235 ++p;
1236 }
1237 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1238 {
1239 /* 'nr' or '+nr' or '-nr' */
1240 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1241 spats[0].off.off = atol((char *)p);
1242 else if (*p == '-') /* single '-' */
1243 spats[0].off.off = -1;
1244 else /* single '+' */
1245 spats[0].off.off = 1;
1246 ++p;
1247 while (VIM_ISDIGIT(*p)) /* skip number */
1248 ++p;
1249 }
1250
1251 /* compute length of search command for get_address() */
1252 searchcmdlen += (int)(p - pat);
1253
1254 pat = p; /* put pat after search command */
1255 }
1256
1257 if ((options & SEARCH_ECHO) && messaging()
1258 && !cmd_silent && msg_silent == 0)
1259 {
1260 char_u *msgbuf;
1261 char_u *trunc;
1262
1263 if (*searchstr == NUL)
1264 p = spats[last_idx].pat;
1265 else
1266 p = searchstr;
1267 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1268 if (msgbuf != NULL)
1269 {
1270 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001271#ifdef FEAT_MBYTE
1272 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1273 {
1274 /* Use a space to draw the composing char on. */
1275 msgbuf[1] = ' ';
1276 STRCPY(msgbuf + 2, p);
1277 }
1278 else
1279#endif
1280 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1282 {
1283 p = msgbuf + STRLEN(msgbuf);
1284 *p++ = dirc;
1285 if (spats[0].off.end)
1286 *p++ = 'e';
1287 else if (!spats[0].off.line)
1288 *p++ = 's';
1289 if (spats[0].off.off > 0 || spats[0].off.line)
1290 *p++ = '+';
1291 if (spats[0].off.off != 0 || spats[0].off.line)
1292 sprintf((char *)p, "%ld", spats[0].off.off);
1293 else
1294 *p = NUL;
1295 }
1296
1297 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001298 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300#ifdef FEAT_RIGHTLEFT
1301 /* The search pattern could be shown on the right in rightleft
1302 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1303 * it would be blanked out again very soon. Show it on the
1304 * left, but do reverse the text. */
1305 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1306 {
1307 char_u *r;
1308
1309 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1310 if (r != NULL)
1311 {
1312 vim_free(trunc);
1313 trunc = r;
1314 }
1315 }
1316#endif
1317 if (trunc != NULL)
1318 {
1319 msg_outtrans(trunc);
1320 vim_free(trunc);
1321 }
1322 else
1323 msg_outtrans(msgbuf);
1324 msg_clr_eos();
1325 msg_check();
1326 vim_free(msgbuf);
1327
1328 gotocmdline(FALSE);
1329 out_flush();
1330 msg_nowait = TRUE; /* don't wait for this message */
1331 }
1332 }
1333
1334 /*
1335 * If there is a character offset, subtract it from the current
1336 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001337 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * This is not done for a line offset, because then we would not be vi
1339 * compatible.
1340 */
Bram Moolenaared203462004-06-16 11:19:22 +00001341 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
1343 if (spats[0].off.off > 0)
1344 {
1345 for (c = spats[0].off.off; c; --c)
1346 if (decl(&pos) == -1)
1347 break;
1348 if (c) /* at start of buffer */
1349 {
1350 pos.lnum = 0; /* allow lnum == 0 here */
1351 pos.col = MAXCOL;
1352 }
1353 }
1354 else
1355 {
1356 for (c = spats[0].off.off; c; ++c)
1357 if (incl(&pos) == -1)
1358 break;
1359 if (c) /* at end of buffer */
1360 {
1361 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1362 pos.col = 0;
1363 }
1364 }
1365 }
1366
1367#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1368 if (p_altkeymap && curwin->w_p_rl)
1369 lrFswap(searchstr,0);
1370#endif
1371
1372 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1373 searchstr, count, spats[0].off.end + (options &
1374 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1375 + SEARCH_MSG + SEARCH_START
1376 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001377 RE_LAST, (linenr_T)0, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 if (dircp != NULL)
1380 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1381 if (c == FAIL)
1382 {
1383 retval = 0;
1384 goto end_do_search;
1385 }
1386 if (spats[0].off.end && oap != NULL)
1387 oap->inclusive = TRUE; /* 'e' includes last character */
1388
1389 retval = 1; /* pattern found */
1390
1391 /*
1392 * Add character and/or line offset
1393 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001394 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 if (spats[0].off.line) /* Add the offset to the line number. */
1397 {
1398 c = pos.lnum + spats[0].off.off;
1399 if (c < 1)
1400 pos.lnum = 1;
1401 else if (c > curbuf->b_ml.ml_line_count)
1402 pos.lnum = curbuf->b_ml.ml_line_count;
1403 else
1404 pos.lnum = c;
1405 pos.col = 0;
1406
1407 retval = 2; /* pattern found, line offset added */
1408 }
Bram Moolenaared203462004-06-16 11:19:22 +00001409 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
1411 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001412 c = spats[0].off.off;
1413 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001415 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (incl(&pos) == -1)
1417 break;
1418 }
1419 /* to the left, check for start of file */
1420 else
1421 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001422 while (c++ < 0)
1423 if (decl(&pos) == -1)
1424 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 }
1427 }
1428
1429 /*
1430 * The search command can be followed by a ';' to do another search.
1431 * For example: "/pat/;/foo/+3;?bar"
1432 * This is like doing another search command, except:
1433 * - The remembered direction '/' or '?' is from the first search.
1434 * - When an error happens the cursor isn't moved at all.
1435 * Don't do this when called by get_address() (it handles ';' itself).
1436 */
1437 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1438 break;
1439
1440 dirc = *++pat;
1441 if (dirc != '?' && dirc != '/')
1442 {
1443 retval = 0;
1444 EMSG(_("E386: Expected '?' or '/' after ';'"));
1445 goto end_do_search;
1446 }
1447 ++pat;
1448 }
1449
1450 if (options & SEARCH_MARK)
1451 setpcmark();
1452 curwin->w_cursor = pos;
1453 curwin->w_set_curswant = TRUE;
1454
1455end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001456 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 spats[0].off = old_off;
1458 vim_free(strcopy);
1459
1460 return retval;
1461}
1462
1463#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1464/*
1465 * search_for_exact_line(buf, pos, dir, pat)
1466 *
1467 * Search for a line starting with the given pattern (ignoring leading
1468 * white-space), starting from pos and going in direction dir. pos will
1469 * contain the position of the match found. Blank lines match only if
1470 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1471 * Return OK for success, or FAIL if no line found.
1472 */
1473 int
1474search_for_exact_line(buf, pos, dir, pat)
1475 buf_T *buf;
1476 pos_T *pos;
1477 int dir;
1478 char_u *pat;
1479{
1480 linenr_T start = 0;
1481 char_u *ptr;
1482 char_u *p;
1483
1484 if (buf->b_ml.ml_line_count == 0)
1485 return FAIL;
1486 for (;;)
1487 {
1488 pos->lnum += dir;
1489 if (pos->lnum < 1)
1490 {
1491 if (p_ws)
1492 {
1493 pos->lnum = buf->b_ml.ml_line_count;
1494 if (!shortmess(SHM_SEARCH))
1495 give_warning((char_u *)_(top_bot_msg), TRUE);
1496 }
1497 else
1498 {
1499 pos->lnum = 1;
1500 break;
1501 }
1502 }
1503 else if (pos->lnum > buf->b_ml.ml_line_count)
1504 {
1505 if (p_ws)
1506 {
1507 pos->lnum = 1;
1508 if (!shortmess(SHM_SEARCH))
1509 give_warning((char_u *)_(bot_top_msg), TRUE);
1510 }
1511 else
1512 {
1513 pos->lnum = 1;
1514 break;
1515 }
1516 }
1517 if (pos->lnum == start)
1518 break;
1519 if (start == 0)
1520 start = pos->lnum;
1521 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1522 p = skipwhite(ptr);
1523 pos->col = (colnr_T) (p - ptr);
1524
1525 /* when adding lines the matching line may be empty but it is not
1526 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001527 if ((compl_cont_status & CONT_ADDING)
1528 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1531 return OK;
1532 }
1533 else if (*p != NUL) /* ignore empty lines */
1534 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001535 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1536 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return OK;
1538 }
1539 }
1540 return FAIL;
1541}
1542#endif /* FEAT_INS_EXPAND */
1543
1544/*
1545 * Character Searches
1546 */
1547
1548/*
1549 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1550 * position of the character, otherwise move to just before the char.
1551 * Do this "cap->count1" times.
1552 * Return FAIL or OK.
1553 */
1554 int
1555searchc(cap, t_cmd)
1556 cmdarg_T *cap;
1557 int t_cmd;
1558{
1559 int c = cap->nchar; /* char to search for */
1560 int dir = cap->arg; /* TRUE for searching forward */
1561 long count = cap->count1; /* repeat count */
1562 static int lastc = NUL; /* last character searched for */
1563 static int lastcdir; /* last direction of character search */
1564 static int last_t_cmd; /* last search t_cmd */
1565 int col;
1566 char_u *p;
1567 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001568 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#ifdef FEAT_MBYTE
Bram Moolenaar81340392012-06-06 16:12:59 +02001570 static char_u bytes[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 static int bytelen = 1; /* >1 for multi-byte char */
1572#endif
1573
1574 if (c != NUL) /* normal search: remember args for repeat */
1575 {
1576 if (!KeyStuffed) /* don't remember when redoing */
1577 {
1578 lastc = c;
1579 lastcdir = dir;
1580 last_t_cmd = t_cmd;
1581#ifdef FEAT_MBYTE
1582 bytelen = (*mb_char2bytes)(c, bytes);
1583 if (cap->ncharC1 != 0)
1584 {
1585 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1586 if (cap->ncharC2 != 0)
1587 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1588 }
1589#endif
1590 }
1591 }
1592 else /* repeat previous search */
1593 {
1594 if (lastc == NUL)
1595 return FAIL;
1596 if (dir) /* repeat in opposite direction */
1597 dir = -lastcdir;
1598 else
1599 dir = lastcdir;
1600 t_cmd = last_t_cmd;
1601 c = lastc;
1602 /* For multi-byte re-use last bytes[] and bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001603
1604 /* Force a move of at least one char, so ";" and "," will move the
1605 * cursor, even if the cursor is right in front of char we are looking
1606 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001607 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001608 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001611 if (dir == BACKWARD)
1612 cap->oap->inclusive = FALSE;
1613 else
1614 cap->oap->inclusive = TRUE;
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 p = ml_get_curline();
1617 col = curwin->w_cursor.col;
1618 len = (int)STRLEN(p);
1619
1620 while (count--)
1621 {
1622#ifdef FEAT_MBYTE
1623 if (has_mbyte)
1624 {
1625 for (;;)
1626 {
1627 if (dir > 0)
1628 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001629 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (col >= len)
1631 return FAIL;
1632 }
1633 else
1634 {
1635 if (col == 0)
1636 return FAIL;
1637 col -= (*mb_head_off)(p, p + col - 1) + 1;
1638 }
1639 if (bytelen == 1)
1640 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001641 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 break;
1643 }
1644 else
1645 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001646 if (vim_memcmp(p + col, bytes, bytelen) == 0 && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 break;
1648 }
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001649 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 }
1652 else
1653#endif
1654 {
1655 for (;;)
1656 {
1657 if ((col += dir) < 0 || col >= len)
1658 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001659 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001661 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 }
1664 }
1665
1666 if (t_cmd)
1667 {
1668 /* backup to before the character (possibly double-byte) */
1669 col -= dir;
1670#ifdef FEAT_MBYTE
1671 if (has_mbyte)
1672 {
1673 if (dir < 0)
1674 /* Landed on the search char which is bytelen long */
1675 col += bytelen - 1;
1676 else
1677 /* To previous char, which may be multi-byte. */
1678 col -= (*mb_head_off)(p, p + col);
1679 }
1680#endif
1681 }
1682 curwin->w_cursor.col = col;
1683
1684 return OK;
1685}
1686
1687/*
1688 * "Other" Searches
1689 */
1690
1691/*
1692 * findmatch - find the matching paren or brace
1693 *
1694 * Improvement over vi: Braces inside quotes are ignored.
1695 */
1696 pos_T *
1697findmatch(oap, initc)
1698 oparg_T *oap;
1699 int initc;
1700{
1701 return findmatchlimit(oap, initc, 0, 0);
1702}
1703
1704/*
1705 * Return TRUE if the character before "linep[col]" equals "ch".
1706 * Return FALSE if "col" is zero.
1707 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1708 * is NULL.
1709 * Handles multibyte string correctly.
1710 */
1711 static int
1712check_prevcol(linep, col, ch, prevcol)
1713 char_u *linep;
1714 int col;
1715 int ch;
1716 int *prevcol;
1717{
1718 --col;
1719#ifdef FEAT_MBYTE
1720 if (col > 0 && has_mbyte)
1721 col -= (*mb_head_off)(linep, linep + col);
1722#endif
1723 if (prevcol)
1724 *prevcol = col;
1725 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1726}
1727
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001728static int find_rawstring_end __ARGS((char_u *linep, pos_T *startpos, pos_T *endpos));
1729
1730/*
1731 * Raw string start is found at linep[startpos.col - 1].
1732 * Return TRUE if the matching end can be found between startpos and endpos.
1733 */
1734 static int
1735find_rawstring_end(linep, startpos, endpos)
1736 char_u *linep;
1737 pos_T *startpos;
1738 pos_T *endpos;
1739{
1740 char_u *p;
1741 char_u *delim_copy;
1742 size_t delim_len;
1743 linenr_T lnum;
1744 int found = FALSE;
1745
1746 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1747 ;
1748 delim_len = (p - linep) - startpos->col - 1;
1749 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
1750 if (delim_copy == NULL)
1751 return FALSE;
1752 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1753 {
1754 char_u *line = ml_get(lnum);
1755
1756 for (p = line + (lnum == startpos->lnum
1757 ? startpos->col + 1 : 0); *p; ++p)
1758 {
1759 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1760 break;
1761 if (*p == ')' && p[delim_len + 1] == '"'
1762 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1763 {
1764 found = TRUE;
1765 break;
1766 }
1767 }
1768 if (found)
1769 break;
1770 }
1771 vim_free(delim_copy);
1772 return found;
1773}
1774
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775/*
1776 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001777 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1778 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 *
1780 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001781 * character at or after the cursor. Special values:
1782 * '*' look for C-style comment / *
1783 * '/' look for C-style comment / *, ignoring comment-end
1784 * '#' look for preprocessor directives
1785 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 *
1787 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1788 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1789 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1790 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001791 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001792 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001793 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 */
1795
1796 pos_T *
1797findmatchlimit(oap, initc, flags, maxtravel)
1798 oparg_T *oap;
1799 int initc;
1800 int flags;
1801 int maxtravel;
1802{
1803 static pos_T pos; /* current search position */
1804 int findc = 0; /* matching brace */
1805 int c;
1806 int count = 0; /* cumulative number of braces */
1807 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001808 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 int inquote = FALSE; /* TRUE when inside quotes */
1810 char_u *linep; /* pointer to current line */
1811 char_u *ptr;
1812 int do_quotes; /* check for quotes in current line */
1813 int at_start; /* do_quotes value at start position */
1814 int hash_dir = 0; /* Direction searched for # things */
1815 int comment_dir = 0; /* Direction searched for comments */
1816 pos_T match_pos; /* Where last slash-star was found */
1817 int start_in_quotes; /* start position is in quotes */
1818 int traveled = 0; /* how far we've searched so far */
1819 int ignore_cend = FALSE; /* ignore comment end */
1820 int cpo_match; /* vi compatible matching */
1821 int cpo_bsl; /* don't recognize backslashes */
1822 int match_escaped = 0; /* search for escaped match */
1823 int dir; /* Direction to search */
1824 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001825#ifdef FEAT_LISP
1826 int lispcomm = FALSE; /* inside of Lisp-style comment */
1827 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
1830 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001831#ifdef FEAT_VIRTUALEDIT
1832 pos.coladd = 0;
1833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 linep = ml_get(pos.lnum);
1835
1836 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1837 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1838
1839 /* Direction to search when initc is '/', '*' or '#' */
1840 if (flags & FM_BACKWARD)
1841 dir = BACKWARD;
1842 else if (flags & FM_FORWARD)
1843 dir = FORWARD;
1844 else
1845 dir = 0;
1846
1847 /*
1848 * if initc given, look in the table for the matching character
1849 * '/' and '*' are special cases: look for start or end of comment.
1850 * When '/' is used, we ignore running backwards into an star-slash, for
1851 * "[*" command, we just want to find any comment.
1852 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001853 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 comment_dir = dir;
1856 if (initc == '/')
1857 ignore_cend = TRUE;
1858 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001859 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 initc = NUL;
1861 }
1862 else if (initc != '#' && initc != NUL)
1863 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001864 find_mps_values(&initc, &findc, &backwards, TRUE);
1865 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 return NULL;
1867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 else
1869 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001870 /*
1871 * Either initc is '#', or no initc was given and we need to look
1872 * under the cursor.
1873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (initc == '#')
1875 {
1876 hash_dir = dir;
1877 }
1878 else
1879 {
1880 /*
1881 * initc was not given, must look for something to match under
1882 * or near the cursor.
1883 * Only check for special things when 'cpo' doesn't have '%'.
1884 */
1885 if (!cpo_match)
1886 {
1887 /* Are we before or at #if, #else etc.? */
1888 ptr = skipwhite(linep);
1889 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1890 {
1891 ptr = skipwhite(ptr + 1);
1892 if ( STRNCMP(ptr, "if", 2) == 0
1893 || STRNCMP(ptr, "endif", 5) == 0
1894 || STRNCMP(ptr, "el", 2) == 0)
1895 hash_dir = 1;
1896 }
1897
1898 /* Are we on a comment? */
1899 else if (linep[pos.col] == '/')
1900 {
1901 if (linep[pos.col + 1] == '*')
1902 {
1903 comment_dir = FORWARD;
1904 backwards = FALSE;
1905 pos.col++;
1906 }
1907 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1908 {
1909 comment_dir = BACKWARD;
1910 backwards = TRUE;
1911 pos.col--;
1912 }
1913 }
1914 else if (linep[pos.col] == '*')
1915 {
1916 if (linep[pos.col + 1] == '/')
1917 {
1918 comment_dir = BACKWARD;
1919 backwards = TRUE;
1920 }
1921 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1922 {
1923 comment_dir = FORWARD;
1924 backwards = FALSE;
1925 }
1926 }
1927 }
1928
1929 /*
1930 * If we are not on a comment or the # at the start of a line, then
1931 * look for brace anywhere on this line after the cursor.
1932 */
1933 if (!hash_dir && !comment_dir)
1934 {
1935 /*
1936 * Find the brace under or after the cursor.
1937 * If beyond the end of the line, use the last character in
1938 * the line.
1939 */
1940 if (linep[pos.col] == NUL && pos.col)
1941 --pos.col;
1942 for (;;)
1943 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001944 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 if (initc == NUL)
1946 break;
1947
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001948 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (findc)
1950 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001951 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953 if (!findc)
1954 {
1955 /* no brace in the line, maybe use " #if" then */
1956 if (!cpo_match && *skipwhite(linep) == '#')
1957 hash_dir = 1;
1958 else
1959 return NULL;
1960 }
1961 else if (!cpo_bsl)
1962 {
1963 int col, bslcnt = 0;
1964
1965 /* Set "match_escaped" if there are an odd number of
1966 * backslashes. */
1967 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1968 bslcnt++;
1969 match_escaped = (bslcnt & 1);
1970 }
1971 }
1972 }
1973 if (hash_dir)
1974 {
1975 /*
1976 * Look for matching #if, #else, #elif, or #endif
1977 */
1978 if (oap != NULL)
1979 oap->motion_type = MLINE; /* Linewise for this case only */
1980 if (initc != '#')
1981 {
1982 ptr = skipwhite(skipwhite(linep) + 1);
1983 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1984 hash_dir = 1;
1985 else if (STRNCMP(ptr, "endif", 5) == 0)
1986 hash_dir = -1;
1987 else
1988 return NULL;
1989 }
1990 pos.col = 0;
1991 while (!got_int)
1992 {
1993 if (hash_dir > 0)
1994 {
1995 if (pos.lnum == curbuf->b_ml.ml_line_count)
1996 break;
1997 }
1998 else if (pos.lnum == 1)
1999 break;
2000 pos.lnum += hash_dir;
2001 linep = ml_get(pos.lnum);
2002 line_breakcheck(); /* check for CTRL-C typed */
2003 ptr = skipwhite(linep);
2004 if (*ptr != '#')
2005 continue;
2006 pos.col = (colnr_T) (ptr - linep);
2007 ptr = skipwhite(ptr + 1);
2008 if (hash_dir > 0)
2009 {
2010 if (STRNCMP(ptr, "if", 2) == 0)
2011 count++;
2012 else if (STRNCMP(ptr, "el", 2) == 0)
2013 {
2014 if (count == 0)
2015 return &pos;
2016 }
2017 else if (STRNCMP(ptr, "endif", 5) == 0)
2018 {
2019 if (count == 0)
2020 return &pos;
2021 count--;
2022 }
2023 }
2024 else
2025 {
2026 if (STRNCMP(ptr, "if", 2) == 0)
2027 {
2028 if (count == 0)
2029 return &pos;
2030 count--;
2031 }
2032 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2033 {
2034 if (count == 0)
2035 return &pos;
2036 }
2037 else if (STRNCMP(ptr, "endif", 5) == 0)
2038 count++;
2039 }
2040 }
2041 return NULL;
2042 }
2043 }
2044
2045#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002046 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 * paren/brace in the other direction. */
2048 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2049 backwards = !backwards;
2050#endif
2051
2052 do_quotes = -1;
2053 start_in_quotes = MAYBE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002054 clearpos(&match_pos);
2055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002057 if ((backwards && comment_dir)
2058#ifdef FEAT_LISP
2059 || lisp
2060#endif
2061 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002063#ifdef FEAT_LISP
2064 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2065 lispcomm = TRUE; /* find match inside this comment */
2066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 while (!got_int)
2068 {
2069 /*
2070 * Go to the next position, forward or backward. We could use
2071 * inc() and dec() here, but that is much slower
2072 */
2073 if (backwards)
2074 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002075#ifdef FEAT_LISP
2076 /* char to match is inside of comment, don't search outside */
2077 if (lispcomm && pos.col < (colnr_T)comment_col)
2078 break;
2079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 if (pos.col == 0) /* at start of line, go to prev. one */
2081 {
2082 if (pos.lnum == 1) /* start of file */
2083 break;
2084 --pos.lnum;
2085
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002086 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 break;
2088
2089 linep = ml_get(pos.lnum);
2090 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2091 do_quotes = -1;
2092 line_breakcheck();
2093
2094 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002095 if (comment_dir
2096#ifdef FEAT_LISP
2097 || lisp
2098#endif
2099 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002101#ifdef FEAT_LISP
2102 /* skip comment */
2103 if (lisp && comment_col != MAXCOL)
2104 pos.col = comment_col;
2105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 }
2107 else
2108 {
2109 --pos.col;
2110#ifdef FEAT_MBYTE
2111 if (has_mbyte)
2112 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2113#endif
2114 }
2115 }
2116 else /* forward search */
2117 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002118 if (linep[pos.col] == NUL
2119 /* at end of line, go to next one */
2120#ifdef FEAT_LISP
2121 /* don't search for match in comment */
2122 || (lisp && comment_col != MAXCOL
2123 && pos.col == (colnr_T)comment_col)
2124#endif
2125 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002127 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2128#ifdef FEAT_LISP
2129 /* line is exhausted and comment with it,
2130 * don't search for match in code */
2131 || lispcomm
2132#endif
2133 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 break;
2135 ++pos.lnum;
2136
2137 if (maxtravel && traveled++ > maxtravel)
2138 break;
2139
2140 linep = ml_get(pos.lnum);
2141 pos.col = 0;
2142 do_quotes = -1;
2143 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002144#ifdef FEAT_LISP
2145 if (lisp) /* find comment pos in new line */
2146 comment_col = check_linecomment(linep);
2147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 }
2149 else
2150 {
2151#ifdef FEAT_MBYTE
2152 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002153 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 else
2155#endif
2156 ++pos.col;
2157 }
2158 }
2159
2160 /*
2161 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2162 */
2163 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2164 (linep[0] == '{' || linep[0] == '}'))
2165 {
2166 if (linep[0] == findc && count == 0) /* match! */
2167 return &pos;
2168 break; /* out of scope */
2169 }
2170
2171 if (comment_dir)
2172 {
2173 /* Note: comments do not nest, and we ignore quotes in them */
2174 /* TODO: ignore comment brackets inside strings */
2175 if (comment_dir == FORWARD)
2176 {
2177 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2178 {
2179 pos.col++;
2180 return &pos;
2181 }
2182 }
2183 else /* Searching backwards */
2184 {
2185 /*
2186 * A comment may contain / * or / /, it may also start or end
2187 * with / * /. Ignore a / * after / /.
2188 */
2189 if (pos.col == 0)
2190 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002191 else if (raw_string)
2192 {
2193 if (linep[pos.col - 1] == 'R'
2194 && linep[pos.col] == '"'
2195 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2196 {
2197 /* Possible start of raw string. Now that we have the
2198 * delimiter we can check if it ends before where we
2199 * started searching, or before the previously found
2200 * raw string start. */
2201 if (!find_rawstring_end(linep, &pos,
2202 count > 0 ? &match_pos : &curwin->w_cursor))
2203 {
2204 count++;
2205 match_pos = pos;
2206 match_pos.col--;
2207 }
2208 linep = ml_get(pos.lnum); /* may have been released */
2209 }
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 else if ( linep[pos.col - 1] == '/'
2212 && linep[pos.col] == '*'
2213 && (int)pos.col < comment_col)
2214 {
2215 count++;
2216 match_pos = pos;
2217 match_pos.col--;
2218 }
2219 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2220 {
2221 if (count > 0)
2222 pos = match_pos;
2223 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2224 && (int)pos.col <= comment_col)
2225 pos.col -= 2;
2226 else if (ignore_cend)
2227 continue;
2228 else
2229 return NULL;
2230 return &pos;
2231 }
2232 }
2233 continue;
2234 }
2235
2236 /*
2237 * If smart matching ('cpoptions' does not contain '%'), braces inside
2238 * of quotes are ignored, but only if there is an even number of
2239 * quotes in the line.
2240 */
2241 if (cpo_match)
2242 do_quotes = 0;
2243 else if (do_quotes == -1)
2244 {
2245 /*
2246 * Count the number of quotes in the line, skipping \" and '"'.
2247 * Watch out for "\\".
2248 */
2249 at_start = do_quotes;
2250 for (ptr = linep; *ptr; ++ptr)
2251 {
2252 if (ptr == linep + pos.col + backwards)
2253 at_start = (do_quotes & 1);
2254 if (*ptr == '"'
2255 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2256 ++do_quotes;
2257 if (*ptr == '\\' && ptr[1] != NUL)
2258 ++ptr;
2259 }
2260 do_quotes &= 1; /* result is 1 with even number of quotes */
2261
2262 /*
2263 * If we find an uneven count, check current line and previous
2264 * one for a '\' at the end.
2265 */
2266 if (!do_quotes)
2267 {
2268 inquote = FALSE;
2269 if (ptr[-1] == '\\')
2270 {
2271 do_quotes = 1;
2272 if (start_in_quotes == MAYBE)
2273 {
2274 /* Do we need to use at_start here? */
2275 inquote = TRUE;
2276 start_in_quotes = TRUE;
2277 }
2278 else if (backwards)
2279 inquote = TRUE;
2280 }
2281 if (pos.lnum > 1)
2282 {
2283 ptr = ml_get(pos.lnum - 1);
2284 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2285 {
2286 do_quotes = 1;
2287 if (start_in_quotes == MAYBE)
2288 {
2289 inquote = at_start;
2290 if (inquote)
2291 start_in_quotes = TRUE;
2292 }
2293 else if (!backwards)
2294 inquote = TRUE;
2295 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002296
2297 /* ml_get() only keeps one line, need to get linep again */
2298 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 }
2300 }
2301 }
2302 if (start_in_quotes == MAYBE)
2303 start_in_quotes = FALSE;
2304
2305 /*
2306 * If 'smartmatch' is set:
2307 * Things inside quotes are ignored by setting 'inquote'. If we
2308 * find a quote without a preceding '\' invert 'inquote'. At the
2309 * end of a line not ending in '\' we reset 'inquote'.
2310 *
2311 * In lines with an uneven number of quotes (without preceding '\')
2312 * we do not know which part to ignore. Therefore we only set
2313 * inquote if the number of quotes in a line is even, unless this
2314 * line or the previous one ends in a '\'. Complicated, isn't it?
2315 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002316 c = PTR2CHAR(linep + pos.col);
2317 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
2319 case NUL:
2320 /* at end of line without trailing backslash, reset inquote */
2321 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2322 {
2323 inquote = FALSE;
2324 start_in_quotes = FALSE;
2325 }
2326 break;
2327
2328 case '"':
2329 /* a quote that is preceded with an odd number of backslashes is
2330 * ignored */
2331 if (do_quotes)
2332 {
2333 int col;
2334
2335 for (col = pos.col - 1; col >= 0; --col)
2336 if (linep[col] != '\\')
2337 break;
2338 if ((((int)pos.col - 1 - col) & 1) == 0)
2339 {
2340 inquote = !inquote;
2341 start_in_quotes = FALSE;
2342 }
2343 }
2344 break;
2345
2346 /*
2347 * If smart matching ('cpoptions' does not contain '%'):
2348 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2349 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2350 * skipped, there is never a brace in them.
2351 * Ignore this when finding matches for `'.
2352 */
2353 case '\'':
2354 if (!cpo_match && initc != '\'' && findc != '\'')
2355 {
2356 if (backwards)
2357 {
2358 if (pos.col > 1)
2359 {
2360 if (linep[pos.col - 2] == '\'')
2361 {
2362 pos.col -= 2;
2363 break;
2364 }
2365 else if (linep[pos.col - 2] == '\\' &&
2366 pos.col > 2 && linep[pos.col - 3] == '\'')
2367 {
2368 pos.col -= 3;
2369 break;
2370 }
2371 }
2372 }
2373 else if (linep[pos.col + 1]) /* forward search */
2374 {
2375 if (linep[pos.col + 1] == '\\' &&
2376 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2377 {
2378 pos.col += 3;
2379 break;
2380 }
2381 else if (linep[pos.col + 2] == '\'')
2382 {
2383 pos.col += 2;
2384 break;
2385 }
2386 }
2387 }
2388 /* FALLTHROUGH */
2389
2390 default:
2391#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002392 /*
2393 * For Lisp skip over backslashed (), {} and [].
2394 * (actually, we skip #\( et al)
2395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 if (curbuf->b_p_lisp
2397 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002398 && pos.col > 1
2399 && check_prevcol(linep, pos.col, '\\', NULL)
2400 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 break;
2402#endif
2403
2404 /* Check for match outside of quotes, and inside of
2405 * quotes when the start is also inside of quotes. */
2406 if ((!inquote || start_in_quotes == TRUE)
2407 && (c == initc || c == findc))
2408 {
2409 int col, bslcnt = 0;
2410
2411 if (!cpo_bsl)
2412 {
2413 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2414 bslcnt++;
2415 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002416 /* Only accept a match when 'M' is in 'cpo' or when escaping
2417 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2419 {
2420 if (c == initc)
2421 count++;
2422 else
2423 {
2424 if (count == 0)
2425 return &pos;
2426 count--;
2427 }
2428 }
2429 }
2430 }
2431 }
2432
2433 if (comment_dir == BACKWARD && count > 0)
2434 {
2435 pos = match_pos;
2436 return &pos;
2437 }
2438 return (pos_T *)NULL; /* never found it */
2439}
2440
2441/*
2442 * Check if line[] contains a / / comment.
2443 * Return MAXCOL if not, otherwise return the column.
2444 * TODO: skip strings.
2445 */
2446 static int
2447check_linecomment(line)
2448 char_u *line;
2449{
2450 char_u *p;
2451
2452 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002453#ifdef FEAT_LISP
2454 /* skip Lispish one-line comments */
2455 if (curbuf->b_p_lisp)
2456 {
2457 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2458 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002459 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002460
2461 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002462 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002463 {
2464 if (*p == '"')
2465 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002466 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002467 {
2468 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002469 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002470 }
2471 else if (p == line || ((p - line) >= 2
2472 /* skip #\" form */
2473 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002474 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002475 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002476 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002477 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2478 break; /* found! */
2479 ++p;
2480 }
2481 }
2482 else
2483 p = NULL;
2484 }
2485 else
2486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 while ((p = vim_strchr(p, '/')) != NULL)
2488 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002489 /* accept a double /, unless it's preceded with * and followed by *,
2490 * because * / / * is an end and start of a C comment */
2491 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 break;
2493 ++p;
2494 }
2495
2496 if (p == NULL)
2497 return MAXCOL;
2498 return (int)(p - line);
2499}
2500
2501/*
2502 * Move cursor briefly to character matching the one under the cursor.
2503 * Used for Insert mode and "r" command.
2504 * Show the match only if it is visible on the screen.
2505 * If there isn't a match, then beep.
2506 */
2507 void
2508showmatch(c)
2509 int c; /* char to show match for */
2510{
2511 pos_T *lpos, save_cursor;
2512 pos_T mpos;
2513 colnr_T vcol;
2514 long save_so;
2515 long save_siso;
2516#ifdef CURSOR_SHAPE
2517 int save_state;
2518#endif
2519 colnr_T save_dollar_vcol;
2520 char_u *p;
2521
2522 /*
2523 * Only show match for chars in the 'matchpairs' option.
2524 */
2525 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002526 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
2528#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002529 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002530 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002531#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002532 p += MB_PTR2LEN(p) + 1;
2533 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534#ifdef FEAT_RIGHTLEFT
2535 && !(curwin->w_p_rl ^ p_ri)
2536#endif
2537 )
2538 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002539 p += MB_PTR2LEN(p);
2540 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 return;
2542 }
2543
2544 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002545 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002546 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
2548 if (!curwin->w_p_wrap)
2549 getvcol(curwin, lpos, NULL, &vcol, NULL);
2550 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2551 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2552 {
2553 mpos = *lpos; /* save the pos, update_screen() may change it */
2554 save_cursor = curwin->w_cursor;
2555 save_so = p_so;
2556 save_siso = p_siso;
2557 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2558 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002559 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2560 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 ++curwin->w_virtcol; /* do display ')' just before "$" */
2562 update_screen(VALID); /* show the new char first */
2563
2564 save_dollar_vcol = dollar_vcol;
2565#ifdef CURSOR_SHAPE
2566 save_state = State;
2567 State = SHOWMATCH;
2568 ui_cursor_shape(); /* may show different cursor shape */
2569#endif
2570 curwin->w_cursor = mpos; /* move to matching char */
2571 p_so = 0; /* don't use 'scrolloff' here */
2572 p_siso = 0; /* don't use 'sidescrolloff' here */
2573 showruler(FALSE);
2574 setcursor();
2575 cursor_on(); /* make sure that the cursor is shown */
2576 out_flush();
2577#ifdef FEAT_GUI
2578 if (gui.in_use)
2579 {
2580 gui_update_cursor(TRUE, FALSE);
2581 gui_mch_flush();
2582 }
2583#endif
2584 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2585 * which resets it if the matching position is in a previous line
2586 * and has a higher column number. */
2587 dollar_vcol = save_dollar_vcol;
2588
2589 /*
2590 * brief pause, unless 'm' is present in 'cpo' and a character is
2591 * available.
2592 */
2593 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2594 ui_delay(p_mat * 100L, TRUE);
2595 else if (!char_avail())
2596 ui_delay(p_mat * 100L, FALSE);
2597 curwin->w_cursor = save_cursor; /* restore cursor position */
2598 p_so = save_so;
2599 p_siso = save_siso;
2600#ifdef CURSOR_SHAPE
2601 State = save_state;
2602 ui_cursor_shape(); /* may show different cursor shape */
2603#endif
2604 }
2605 }
2606}
2607
2608/*
2609 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002610 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 * space or a line break. Also stop at an empty line.
2612 * Return OK if the next sentence was found.
2613 */
2614 int
2615findsent(dir, count)
2616 int dir;
2617 long count;
2618{
2619 pos_T pos, tpos;
2620 int c;
2621 int (*func) __ARGS((pos_T *));
2622 int startlnum;
2623 int noskip = FALSE; /* do not skip blanks */
2624 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002625 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 pos = curwin->w_cursor;
2628 if (dir == FORWARD)
2629 func = incl;
2630 else
2631 func = decl;
2632
2633 while (count--)
2634 {
2635 /*
2636 * if on an empty line, skip upto a non-empty line
2637 */
2638 if (gchar_pos(&pos) == NUL)
2639 {
2640 do
2641 if ((*func)(&pos) == -1)
2642 break;
2643 while (gchar_pos(&pos) == NUL);
2644 if (dir == FORWARD)
2645 goto found;
2646 }
2647 /*
2648 * if on the start of a paragraph or a section and searching forward,
2649 * go to the next line
2650 */
2651 else if (dir == FORWARD && pos.col == 0 &&
2652 startPS(pos.lnum, NUL, FALSE))
2653 {
2654 if (pos.lnum == curbuf->b_ml.ml_line_count)
2655 return FAIL;
2656 ++pos.lnum;
2657 goto found;
2658 }
2659 else if (dir == BACKWARD)
2660 decl(&pos);
2661
2662 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002663 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2665 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2666 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002667 if (vim_strchr((char_u *)".!?", c) != NULL)
2668 {
2669 /* Only skip over a '.', '!' and '?' once. */
2670 if (found_dot)
2671 break;
2672 found_dot = TRUE;
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 if (decl(&pos) == -1)
2675 break;
2676 /* when going forward: Stop in front of empty line */
2677 if (lineempty(pos.lnum) && dir == FORWARD)
2678 {
2679 incl(&pos);
2680 goto found;
2681 }
2682 }
2683
2684 /* remember the line where the search started */
2685 startlnum = pos.lnum;
2686 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2687
2688 for (;;) /* find end of sentence */
2689 {
2690 c = gchar_pos(&pos);
2691 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2692 {
2693 if (dir == BACKWARD && pos.lnum != startlnum)
2694 ++pos.lnum;
2695 break;
2696 }
2697 if (c == '.' || c == '!' || c == '?')
2698 {
2699 tpos = pos;
2700 do
2701 if ((c = inc(&tpos)) == -1)
2702 break;
2703 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2704 != NULL);
2705 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2706 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2707 && gchar_pos(&tpos) == ' ')))
2708 {
2709 pos = tpos;
2710 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2711 inc(&pos);
2712 break;
2713 }
2714 }
2715 if ((*func)(&pos) == -1)
2716 {
2717 if (count)
2718 return FAIL;
2719 noskip = TRUE;
2720 break;
2721 }
2722 }
2723found:
2724 /* skip white space */
2725 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2726 if (incl(&pos) == -1)
2727 break;
2728 }
2729
2730 setpcmark();
2731 curwin->w_cursor = pos;
2732 return OK;
2733}
2734
2735/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002736 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002738 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * If 'what' is '{' or '}' we go to the next section.
2740 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002741 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 */
2743 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002744findpar(pincl, dir, count, what, both)
2745 int *pincl; /* Return: TRUE if last char is to be included */
2746 int dir;
2747 long count;
2748 int what;
2749 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 linenr_T curr;
2752 int did_skip; /* TRUE after separating lines have been skipped */
2753 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002754 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755#ifdef FEAT_FOLDING
2756 linenr_T fold_first; /* first line of a closed fold */
2757 linenr_T fold_last; /* last line of a closed fold */
2758 int fold_skipped; /* TRUE if a closed fold was skipped this
2759 iteration */
2760#endif
2761
2762 curr = curwin->w_cursor.lnum;
2763
2764 while (count--)
2765 {
2766 did_skip = FALSE;
2767 for (first = TRUE; ; first = FALSE)
2768 {
2769 if (*ml_get(curr) != NUL)
2770 did_skip = TRUE;
2771
2772#ifdef FEAT_FOLDING
2773 /* skip folded lines */
2774 fold_skipped = FALSE;
2775 if (first && hasFolding(curr, &fold_first, &fold_last))
2776 {
2777 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2778 fold_skipped = TRUE;
2779 }
2780#endif
2781
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002782 /* POSIX has it's own ideas of what a paragraph boundary is and it
2783 * doesn't match historical Vi: It also stops at a "{" in the
2784 * first column and at an empty line. */
2785 if (!first && did_skip && (startPS(curr, what, both)
2786 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 break;
2788
2789#ifdef FEAT_FOLDING
2790 if (fold_skipped)
2791 curr -= dir;
2792#endif
2793 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2794 {
2795 if (count)
2796 return FALSE;
2797 curr -= dir;
2798 break;
2799 }
2800 }
2801 }
2802 setpcmark();
2803 if (both && *ml_get(curr) == '}') /* include line with '}' */
2804 ++curr;
2805 curwin->w_cursor.lnum = curr;
2806 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2807 {
2808 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2809 {
2810 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002811 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 }
2813 }
2814 else
2815 curwin->w_cursor.col = 0;
2816 return TRUE;
2817}
2818
2819/*
2820 * check if the string 's' is a nroff macro that is in option 'opt'
2821 */
2822 static int
2823inmacro(opt, s)
2824 char_u *opt;
2825 char_u *s;
2826{
2827 char_u *macro;
2828
2829 for (macro = opt; macro[0]; ++macro)
2830 {
2831 /* Accept two characters in the option being equal to two characters
2832 * in the line. A space in the option matches with a space in the
2833 * line or the line having ended. */
2834 if ( (macro[0] == s[0]
2835 || (macro[0] == ' '
2836 && (s[0] == NUL || s[0] == ' ')))
2837 && (macro[1] == s[1]
2838 || ((macro[1] == NUL || macro[1] == ' ')
2839 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2840 break;
2841 ++macro;
2842 if (macro[0] == NUL)
2843 break;
2844 }
2845 return (macro[0] != NUL);
2846}
2847
2848/*
2849 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2850 * If 'para' is '{' or '}' only check for sections.
2851 * If 'both' is TRUE also stop at '}'
2852 */
2853 int
2854startPS(lnum, para, both)
2855 linenr_T lnum;
2856 int para;
2857 int both;
2858{
2859 char_u *s;
2860
2861 s = ml_get(lnum);
2862 if (*s == para || *s == '\f' || (both && *s == '}'))
2863 return TRUE;
2864 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2865 (!para && inmacro(p_para, s + 1))))
2866 return TRUE;
2867 return FALSE;
2868}
2869
2870/*
2871 * The following routines do the word searches performed by the 'w', 'W',
2872 * 'b', 'B', 'e', and 'E' commands.
2873 */
2874
2875/*
2876 * To perform these searches, characters are placed into one of three
2877 * classes, and transitions between classes determine word boundaries.
2878 *
2879 * The classes are:
2880 *
2881 * 0 - white space
2882 * 1 - punctuation
2883 * 2 or higher - keyword characters (letters, digits and underscore)
2884 */
2885
2886static int cls_bigword; /* TRUE for "W", "B" or "E" */
2887
2888/*
2889 * cls() - returns the class of character at curwin->w_cursor
2890 *
2891 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2892 * from class 2 and higher are reported as class 1 since only white space
2893 * boundaries are of interest.
2894 */
2895 static int
2896cls()
2897{
2898 int c;
2899
2900 c = gchar_cursor();
2901#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2902 if (p_altkeymap && c == F_BLANK)
2903 return 0;
2904#endif
2905 if (c == ' ' || c == '\t' || c == NUL)
2906 return 0;
2907#ifdef FEAT_MBYTE
2908 if (enc_dbcs != 0 && c > 0xFF)
2909 {
2910 /* If cls_bigword, report multi-byte chars as class 1. */
2911 if (enc_dbcs == DBCS_KOR && cls_bigword)
2912 return 1;
2913
2914 /* process code leading/trailing bytes */
2915 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2916 }
2917 if (enc_utf8)
2918 {
2919 c = utf_class(c);
2920 if (c != 0 && cls_bigword)
2921 return 1;
2922 return c;
2923 }
2924#endif
2925
2926 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2927 if (cls_bigword)
2928 return 1;
2929
2930 if (vim_iswordc(c))
2931 return 2;
2932 return 1;
2933}
2934
2935
2936/*
2937 * fwd_word(count, type, eol) - move forward one word
2938 *
2939 * Returns FAIL if the cursor was already at the end of the file.
2940 * If eol is TRUE, last word stops at end of line (for operators).
2941 */
2942 int
2943fwd_word(count, bigword, eol)
2944 long count;
2945 int bigword; /* "W", "E" or "B" */
2946 int eol;
2947{
2948 int sclass; /* starting class */
2949 int i;
2950 int last_line;
2951
2952#ifdef FEAT_VIRTUALEDIT
2953 curwin->w_cursor.coladd = 0;
2954#endif
2955 cls_bigword = bigword;
2956 while (--count >= 0)
2957 {
2958#ifdef FEAT_FOLDING
2959 /* When inside a range of folded lines, move to the last char of the
2960 * last line. */
2961 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2962 coladvance((colnr_T)MAXCOL);
2963#endif
2964 sclass = cls();
2965
2966 /*
2967 * We always move at least one character, unless on the last
2968 * character in the buffer.
2969 */
2970 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2971 i = inc_cursor();
2972 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2973 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00002974 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 return OK;
2976
2977 /*
2978 * Go one char past end of current word (if any)
2979 */
2980 if (sclass != 0)
2981 while (cls() == sclass)
2982 {
2983 i = inc_cursor();
2984 if (i == -1 || (i >= 1 && eol && count == 0))
2985 return OK;
2986 }
2987
2988 /*
2989 * go to next non-white
2990 */
2991 while (cls() == 0)
2992 {
2993 /*
2994 * We'll stop if we land on a blank line
2995 */
2996 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2997 break;
2998
2999 i = inc_cursor();
3000 if (i == -1 || (i >= 1 && eol && count == 0))
3001 return OK;
3002 }
3003 }
3004 return OK;
3005}
3006
3007/*
3008 * bck_word() - move backward 'count' words
3009 *
3010 * If stop is TRUE and we are already on the start of a word, move one less.
3011 *
3012 * Returns FAIL if top of the file was reached.
3013 */
3014 int
3015bck_word(count, bigword, stop)
3016 long count;
3017 int bigword;
3018 int stop;
3019{
3020 int sclass; /* starting class */
3021
3022#ifdef FEAT_VIRTUALEDIT
3023 curwin->w_cursor.coladd = 0;
3024#endif
3025 cls_bigword = bigword;
3026 while (--count >= 0)
3027 {
3028#ifdef FEAT_FOLDING
3029 /* When inside a range of folded lines, move to the first char of the
3030 * first line. */
3031 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3032 curwin->w_cursor.col = 0;
3033#endif
3034 sclass = cls();
3035 if (dec_cursor() == -1) /* started at start of file */
3036 return FAIL;
3037
3038 if (!stop || sclass == cls() || sclass == 0)
3039 {
3040 /*
3041 * Skip white space before the word.
3042 * Stop on an empty line.
3043 */
3044 while (cls() == 0)
3045 {
3046 if (curwin->w_cursor.col == 0
3047 && lineempty(curwin->w_cursor.lnum))
3048 goto finished;
3049 if (dec_cursor() == -1) /* hit start of file, stop here */
3050 return OK;
3051 }
3052
3053 /*
3054 * Move backward to start of this word.
3055 */
3056 if (skip_chars(cls(), BACKWARD))
3057 return OK;
3058 }
3059
3060 inc_cursor(); /* overshot - forward one */
3061finished:
3062 stop = FALSE;
3063 }
3064 return OK;
3065}
3066
3067/*
3068 * end_word() - move to the end of the word
3069 *
3070 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3071 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3072 * motion crosses blank lines. When the real vi crosses a blank line in an
3073 * 'e' motion, the cursor is placed on the FIRST character of the next
3074 * non-blank line. The 'E' command, however, works correctly. Since this
3075 * appears to be a bug, I have not duplicated it here.
3076 *
3077 * Returns FAIL if end of the file was reached.
3078 *
3079 * If stop is TRUE and we are already on the end of a word, move one less.
3080 * If empty is TRUE stop on an empty line.
3081 */
3082 int
3083end_word(count, bigword, stop, empty)
3084 long count;
3085 int bigword;
3086 int stop;
3087 int empty;
3088{
3089 int sclass; /* starting class */
3090
3091#ifdef FEAT_VIRTUALEDIT
3092 curwin->w_cursor.coladd = 0;
3093#endif
3094 cls_bigword = bigword;
3095 while (--count >= 0)
3096 {
3097#ifdef FEAT_FOLDING
3098 /* When inside a range of folded lines, move to the last char of the
3099 * last line. */
3100 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3101 coladvance((colnr_T)MAXCOL);
3102#endif
3103 sclass = cls();
3104 if (inc_cursor() == -1)
3105 return FAIL;
3106
3107 /*
3108 * If we're in the middle of a word, we just have to move to the end
3109 * of it.
3110 */
3111 if (cls() == sclass && sclass != 0)
3112 {
3113 /*
3114 * Move forward to end of the current word
3115 */
3116 if (skip_chars(sclass, FORWARD))
3117 return FAIL;
3118 }
3119 else if (!stop || sclass == 0)
3120 {
3121 /*
3122 * We were at the end of a word. Go to the end of the next word.
3123 * First skip white space, if 'empty' is TRUE, stop at empty line.
3124 */
3125 while (cls() == 0)
3126 {
3127 if (empty && curwin->w_cursor.col == 0
3128 && lineempty(curwin->w_cursor.lnum))
3129 goto finished;
3130 if (inc_cursor() == -1) /* hit end of file, stop here */
3131 return FAIL;
3132 }
3133
3134 /*
3135 * Move forward to the end of this word.
3136 */
3137 if (skip_chars(cls(), FORWARD))
3138 return FAIL;
3139 }
3140 dec_cursor(); /* overshot - one char backward */
3141finished:
3142 stop = FALSE; /* we move only one word less */
3143 }
3144 return OK;
3145}
3146
3147/*
3148 * Move back to the end of the word.
3149 *
3150 * Returns FAIL if start of the file was reached.
3151 */
3152 int
3153bckend_word(count, bigword, eol)
3154 long count;
3155 int bigword; /* TRUE for "B" */
3156 int eol; /* TRUE: stop at end of line. */
3157{
3158 int sclass; /* starting class */
3159 int i;
3160
3161#ifdef FEAT_VIRTUALEDIT
3162 curwin->w_cursor.coladd = 0;
3163#endif
3164 cls_bigword = bigword;
3165 while (--count >= 0)
3166 {
3167 sclass = cls();
3168 if ((i = dec_cursor()) == -1)
3169 return FAIL;
3170 if (eol && i == 1)
3171 return OK;
3172
3173 /*
3174 * Move backward to before the start of this word.
3175 */
3176 if (sclass != 0)
3177 {
3178 while (cls() == sclass)
3179 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3180 return OK;
3181 }
3182
3183 /*
3184 * Move backward to end of the previous word
3185 */
3186 while (cls() == 0)
3187 {
3188 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3189 break;
3190 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3191 return OK;
3192 }
3193 }
3194 return OK;
3195}
3196
3197/*
3198 * Skip a row of characters of the same class.
3199 * Return TRUE when end-of-file reached, FALSE otherwise.
3200 */
3201 static int
3202skip_chars(cclass, dir)
3203 int cclass;
3204 int dir;
3205{
3206 while (cls() == cclass)
3207 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3208 return TRUE;
3209 return FALSE;
3210}
3211
3212#ifdef FEAT_TEXTOBJ
3213/*
3214 * Go back to the start of the word or the start of white space
3215 */
3216 static void
3217back_in_line()
3218{
3219 int sclass; /* starting class */
3220
3221 sclass = cls();
3222 for (;;)
3223 {
3224 if (curwin->w_cursor.col == 0) /* stop at start of line */
3225 break;
3226 dec_cursor();
3227 if (cls() != sclass) /* stop at start of word */
3228 {
3229 inc_cursor();
3230 break;
3231 }
3232 }
3233}
3234
3235 static void
3236find_first_blank(posp)
3237 pos_T *posp;
3238{
3239 int c;
3240
3241 while (decl(posp) != -1)
3242 {
3243 c = gchar_pos(posp);
3244 if (!vim_iswhite(c))
3245 {
3246 incl(posp);
3247 break;
3248 }
3249 }
3250}
3251
3252/*
3253 * Skip count/2 sentences and count/2 separating white spaces.
3254 */
3255 static void
3256findsent_forward(count, at_start_sent)
3257 long count;
3258 int at_start_sent; /* cursor is at start of sentence */
3259{
3260 while (count--)
3261 {
3262 findsent(FORWARD, 1L);
3263 if (at_start_sent)
3264 find_first_blank(&curwin->w_cursor);
3265 if (count == 0 || at_start_sent)
3266 decl(&curwin->w_cursor);
3267 at_start_sent = !at_start_sent;
3268 }
3269}
3270
3271/*
3272 * Find word under cursor, cursor at end.
3273 * Used while an operator is pending, and in Visual mode.
3274 */
3275 int
3276current_word(oap, count, include, bigword)
3277 oparg_T *oap;
3278 long count;
3279 int include; /* TRUE: include word and white space */
3280 int bigword; /* FALSE == word, TRUE == WORD */
3281{
3282 pos_T start_pos;
3283 pos_T pos;
3284 int inclusive = TRUE;
3285 int include_white = FALSE;
3286
3287 cls_bigword = bigword;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003288 clearpos(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 /* Correct cursor when 'selection' is exclusive */
3291 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3292 dec_cursor();
3293
3294 /*
3295 * When Visual mode is not active, or when the VIsual area is only one
3296 * character, select the word and/or white space under the cursor.
3297 */
3298 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 /*
3301 * Go to start of current word or white space.
3302 */
3303 back_in_line();
3304 start_pos = curwin->w_cursor;
3305
3306 /*
3307 * If the start is on white space, and white space should be included
3308 * (" word"), or start is not on white space, and white space should
3309 * not be included ("word"), find end of word.
3310 */
3311 if ((cls() == 0) == include)
3312 {
3313 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3314 return FAIL;
3315 }
3316 else
3317 {
3318 /*
3319 * If the start is not on white space, and white space should be
3320 * included ("word "), or start is on white space and white
3321 * space should not be included (" "), find start of word.
3322 * If we end up in the first column of the next line (single char
3323 * word) back up to end of the line.
3324 */
3325 fwd_word(1L, bigword, TRUE);
3326 if (curwin->w_cursor.col == 0)
3327 decl(&curwin->w_cursor);
3328 else
3329 oneleft();
3330
3331 if (include)
3332 include_white = TRUE;
3333 }
3334
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 if (VIsual_active)
3336 {
3337 /* should do something when inclusive == FALSE ! */
3338 VIsual = start_pos;
3339 redraw_curbuf_later(INVERTED); /* update the inversion */
3340 }
3341 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 oap->start = start_pos;
3344 oap->motion_type = MCHAR;
3345 }
3346 --count;
3347 }
3348
3349 /*
3350 * When count is still > 0, extend with more objects.
3351 */
3352 while (count > 0)
3353 {
3354 inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3356 {
3357 /*
3358 * In Visual mode, with cursor at start: move cursor back.
3359 */
3360 if (decl(&curwin->w_cursor) == -1)
3361 return FAIL;
3362 if (include != (cls() != 0))
3363 {
3364 if (bck_word(1L, bigword, TRUE) == FAIL)
3365 return FAIL;
3366 }
3367 else
3368 {
3369 if (bckend_word(1L, bigword, TRUE) == FAIL)
3370 return FAIL;
3371 (void)incl(&curwin->w_cursor);
3372 }
3373 }
3374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
3376 /*
3377 * Move cursor forward one word and/or white area.
3378 */
3379 if (incl(&curwin->w_cursor) == -1)
3380 return FAIL;
3381 if (include != (cls() == 0))
3382 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003383 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 return FAIL;
3385 /*
3386 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003387 * the first character on the line.
3388 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003390 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 inclusive = FALSE;
3392 }
3393 else
3394 {
3395 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3396 return FAIL;
3397 }
3398 }
3399 --count;
3400 }
3401
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003402 if (include_white && (cls() != 0
3403 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
3405 /*
3406 * If we don't include white space at the end, move the start
3407 * to include some white space there. This makes "daw" work
3408 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003409 * word). Also when "2daw" deletes "word." at the end of the line
3410 * (cursor is at start of next line).
3411 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 */
3413 pos = curwin->w_cursor; /* save cursor position */
3414 curwin->w_cursor = start_pos;
3415 if (oneleft() == OK)
3416 {
3417 back_in_line();
3418 if (cls() == 0 && curwin->w_cursor.col > 0)
3419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (VIsual_active)
3421 VIsual = curwin->w_cursor;
3422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 oap->start = curwin->w_cursor;
3424 }
3425 }
3426 curwin->w_cursor = pos; /* put cursor back at end */
3427 }
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (VIsual_active)
3430 {
3431 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3432 inc_cursor();
3433 if (VIsual_mode == 'V')
3434 {
3435 VIsual_mode = 'v';
3436 redraw_cmdline = TRUE; /* show mode later */
3437 }
3438 }
3439 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 oap->inclusive = inclusive;
3441
3442 return OK;
3443}
3444
3445/*
3446 * Find sentence(s) under the cursor, cursor at end.
3447 * When Visual active, extend it by one or more sentences.
3448 */
3449 int
3450current_sent(oap, count, include)
3451 oparg_T *oap;
3452 long count;
3453 int include;
3454{
3455 pos_T start_pos;
3456 pos_T pos;
3457 int start_blank;
3458 int c;
3459 int at_start_sent;
3460 long ncount;
3461
3462 start_pos = curwin->w_cursor;
3463 pos = start_pos;
3464 findsent(FORWARD, 1L); /* Find start of next sentence. */
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003467 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
3469 if (VIsual_active && !equalpos(start_pos, VIsual))
3470 {
3471extend:
3472 if (lt(start_pos, VIsual))
3473 {
3474 /*
3475 * Cursor at start of Visual area.
3476 * Find out where we are:
3477 * - in the white space before a sentence
3478 * - in a sentence or just after it
3479 * - at the start of a sentence
3480 */
3481 at_start_sent = TRUE;
3482 decl(&pos);
3483 while (lt(pos, curwin->w_cursor))
3484 {
3485 c = gchar_pos(&pos);
3486 if (!vim_iswhite(c))
3487 {
3488 at_start_sent = FALSE;
3489 break;
3490 }
3491 incl(&pos);
3492 }
3493 if (!at_start_sent)
3494 {
3495 findsent(BACKWARD, 1L);
3496 if (equalpos(curwin->w_cursor, start_pos))
3497 at_start_sent = TRUE; /* exactly at start of sentence */
3498 else
3499 /* inside a sentence, go to its end (start of next) */
3500 findsent(FORWARD, 1L);
3501 }
3502 if (include) /* "as" gets twice as much as "is" */
3503 count *= 2;
3504 while (count--)
3505 {
3506 if (at_start_sent)
3507 find_first_blank(&curwin->w_cursor);
3508 c = gchar_cursor();
3509 if (!at_start_sent || (!include && !vim_iswhite(c)))
3510 findsent(BACKWARD, 1L);
3511 at_start_sent = !at_start_sent;
3512 }
3513 }
3514 else
3515 {
3516 /*
3517 * Cursor at end of Visual area.
3518 * Find out where we are:
3519 * - just before a sentence
3520 * - just before or in the white space before a sentence
3521 * - in a sentence
3522 */
3523 incl(&pos);
3524 at_start_sent = TRUE;
3525 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3526 {
3527 at_start_sent = FALSE;
3528 while (lt(pos, curwin->w_cursor))
3529 {
3530 c = gchar_pos(&pos);
3531 if (!vim_iswhite(c))
3532 {
3533 at_start_sent = TRUE;
3534 break;
3535 }
3536 incl(&pos);
3537 }
3538 if (at_start_sent) /* in the sentence */
3539 findsent(BACKWARD, 1L);
3540 else /* in/before white before a sentence */
3541 curwin->w_cursor = start_pos;
3542 }
3543
3544 if (include) /* "as" gets twice as much as "is" */
3545 count *= 2;
3546 findsent_forward(count, at_start_sent);
3547 if (*p_sel == 'e')
3548 ++curwin->w_cursor.col;
3549 }
3550 return OK;
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552
3553 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003554 * If the cursor started on a blank, check if it is just before the start
3555 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
3557 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3558 incl(&pos);
3559 if (equalpos(pos, curwin->w_cursor))
3560 {
3561 start_blank = TRUE;
3562 find_first_blank(&start_pos); /* go back to first blank */
3563 }
3564 else
3565 {
3566 start_blank = FALSE;
3567 findsent(BACKWARD, 1L);
3568 start_pos = curwin->w_cursor;
3569 }
3570 if (include)
3571 ncount = count * 2;
3572 else
3573 {
3574 ncount = count;
3575 if (start_blank)
3576 --ncount;
3577 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003578 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 findsent_forward(ncount, TRUE);
3580 else
3581 decl(&curwin->w_cursor);
3582
3583 if (include)
3584 {
3585 /*
3586 * If the blank in front of the sentence is included, exclude the
3587 * blanks at the end of the sentence, go back to the first blank.
3588 * If there are no trailing blanks, try to include leading blanks.
3589 */
3590 if (start_blank)
3591 {
3592 find_first_blank(&curwin->w_cursor);
3593 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3594 if (vim_iswhite(c))
3595 decl(&curwin->w_cursor);
3596 }
3597 else if (c = gchar_cursor(), !vim_iswhite(c))
3598 find_first_blank(&start_pos);
3599 }
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (VIsual_active)
3602 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003603 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 if (equalpos(start_pos, curwin->w_cursor))
3605 goto extend;
3606 if (*p_sel == 'e')
3607 ++curwin->w_cursor.col;
3608 VIsual = start_pos;
3609 VIsual_mode = 'v';
3610 redraw_curbuf_later(INVERTED); /* update the inversion */
3611 }
3612 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
3614 /* include a newline after the sentence, if there is one */
3615 if (incl(&curwin->w_cursor) == -1)
3616 oap->inclusive = TRUE;
3617 else
3618 oap->inclusive = FALSE;
3619 oap->start = start_pos;
3620 oap->motion_type = MCHAR;
3621 }
3622 return OK;
3623}
3624
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003625/*
3626 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003627 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 int
3630current_block(oap, count, include, what, other)
3631 oparg_T *oap;
3632 long count;
3633 int include; /* TRUE == include white space */
3634 int what; /* '(', '{', etc. */
3635 int other; /* ')', '}', etc. */
3636{
3637 pos_T old_pos;
3638 pos_T *pos = NULL;
3639 pos_T start_pos;
3640 pos_T *end_pos;
3641 pos_T old_start, old_end;
3642 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003643 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003646 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 old_start = old_end;
3648
3649 /*
3650 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003655 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 while (inindent(1))
3657 if (inc_cursor() != 0)
3658 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003659 if (gchar_cursor() == what)
3660 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 ++curwin->w_cursor.col;
3662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 else if (lt(VIsual, curwin->w_cursor))
3664 {
3665 old_start = VIsual;
3666 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3667 }
3668 else
3669 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671 /*
3672 * Search backwards for unclosed '(', '{', etc..
3673 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003674 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3675 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 */
3677 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003678 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 while (count-- > 0)
3680 {
3681 if ((pos = findmatch(NULL, what)) == NULL)
3682 break;
3683 curwin->w_cursor = *pos;
3684 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3685 }
3686 p_cpo = save_cpo;
3687
3688 /*
3689 * Search for matching ')', '}', etc.
3690 * Put this position in curwin->w_cursor.
3691 */
3692 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3693 {
3694 curwin->w_cursor = old_pos;
3695 return FAIL;
3696 }
3697 curwin->w_cursor = *end_pos;
3698
3699 /*
3700 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003701 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3702 * indent. But only if the resulting area is not smaller than what we
3703 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 */
3705 while (!include)
3706 {
3707 incl(&start_pos);
3708 sol = (curwin->w_cursor.col == 0);
3709 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003710 while (inindent(1))
3711 {
3712 sol = TRUE;
3713 if (decl(&curwin->w_cursor) != 0)
3714 break;
3715 }
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 /*
3718 * In Visual mode, when the resulting area is not bigger than what we
3719 * started with, extend it to the next block, and then exclude again.
3720 */
3721 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3722 && VIsual_active)
3723 {
3724 curwin->w_cursor = old_start;
3725 decl(&curwin->w_cursor);
3726 if ((pos = findmatch(NULL, what)) == NULL)
3727 {
3728 curwin->w_cursor = old_pos;
3729 return FAIL;
3730 }
3731 start_pos = *pos;
3732 curwin->w_cursor = *pos;
3733 if ((end_pos = findmatch(NULL, other)) == NULL)
3734 {
3735 curwin->w_cursor = old_pos;
3736 return FAIL;
3737 }
3738 curwin->w_cursor = *end_pos;
3739 }
3740 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 break;
3742 }
3743
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (VIsual_active)
3745 {
3746 if (*p_sel == 'e')
3747 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003748 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 inc(&curwin->w_cursor); /* include the line break */
3750 VIsual = start_pos;
3751 VIsual_mode = 'v';
3752 redraw_curbuf_later(INVERTED); /* update the inversion */
3753 showmode();
3754 }
3755 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
3757 oap->start = start_pos;
3758 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003759 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 incl(&curwin->w_cursor);
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003762 else if (ltoreq(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003763 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003765 else
3766 /* End is before the start (no text in between <>, [], etc.): don't
3767 * operate on any text. */
3768 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
3770
3771 return OK;
3772}
3773
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003774static int in_html_tag __ARGS((int));
3775
3776/*
3777 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3778 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3779 */
3780 static int
3781in_html_tag(end_tag)
3782 int end_tag;
3783{
3784 char_u *line = ml_get_curline();
3785 char_u *p;
3786 int c;
3787 int lc = NUL;
3788 pos_T pos;
3789
3790#ifdef FEAT_MBYTE
3791 if (enc_dbcs)
3792 {
3793 char_u *lp = NULL;
3794
3795 /* We search forward until the cursor, because searching backwards is
3796 * very slow for DBCS encodings. */
3797 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3798 if (*p == '>' || *p == '<')
3799 {
3800 lc = *p;
3801 lp = p;
3802 }
3803 if (*p != '<') /* check for '<' under cursor */
3804 {
3805 if (lc != '<')
3806 return FALSE;
3807 p = lp;
3808 }
3809 }
3810 else
3811#endif
3812 {
3813 for (p = line + curwin->w_cursor.col; p > line; )
3814 {
3815 if (*p == '<') /* find '<' under/before cursor */
3816 break;
3817 mb_ptr_back(line, p);
3818 if (*p == '>') /* find '>' before cursor */
3819 break;
3820 }
3821 if (*p != '<')
3822 return FALSE;
3823 }
3824
3825 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003826 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003827
3828 mb_ptr_adv(p);
3829 if (end_tag)
3830 /* check that there is a '/' after the '<' */
3831 return *p == '/';
3832
3833 /* check that there is no '/' after the '<' */
3834 if (*p == '/')
3835 return FALSE;
3836
3837 /* check that the matching '>' is not preceded by '/' */
3838 for (;;)
3839 {
3840 if (inc(&pos) < 0)
3841 return FALSE;
3842 c = *ml_get_pos(&pos);
3843 if (c == '>')
3844 break;
3845 lc = c;
3846 }
3847 return lc != '/';
3848}
3849
3850/*
3851 * Find tag block under the cursor, cursor at end.
3852 */
3853 int
3854current_tagblock(oap, count_arg, include)
3855 oparg_T *oap;
3856 long count_arg;
3857 int include; /* TRUE == include white space */
3858{
3859 long count = count_arg;
3860 long n;
3861 pos_T old_pos;
3862 pos_T start_pos;
3863 pos_T end_pos;
3864 pos_T old_start, old_end;
3865 char_u *spat, *epat;
3866 char_u *p;
3867 char_u *cp;
3868 int len;
3869 int r;
3870 int do_include = include;
3871 int save_p_ws = p_ws;
3872 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003873 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003874
3875 p_ws = FALSE;
3876
3877 old_pos = curwin->w_cursor;
3878 old_end = curwin->w_cursor; /* remember where we started */
3879 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003880 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003881 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003882
3883 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003884 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003885 */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003886 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003887 {
3888 setpcmark();
3889
3890 /* ignore indent */
3891 while (inindent(1))
3892 if (inc_cursor() != 0)
3893 break;
3894
3895 if (in_html_tag(FALSE))
3896 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003897 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003898 while (*ml_get_cursor() != '>')
3899 if (inc_cursor() < 0)
3900 break;
3901 }
3902 else if (in_html_tag(TRUE))
3903 {
3904 /* cursor on end tag, move to just before it */
3905 while (*ml_get_cursor() != '<')
3906 if (dec_cursor() < 0)
3907 break;
3908 dec_cursor();
3909 old_end = curwin->w_cursor;
3910 }
3911 }
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003912 else if (lt(VIsual, curwin->w_cursor))
3913 {
3914 old_start = VIsual;
3915 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3916 }
3917 else
3918 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003919
3920again:
3921 /*
3922 * Search backwards for unclosed "<aaa>".
3923 * Put this position in start_pos.
3924 */
3925 for (n = 0; n < count; ++n)
3926 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003927 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003928 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003929 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00003930 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003931 {
3932 curwin->w_cursor = old_pos;
3933 goto theend;
3934 }
3935 }
3936 start_pos = curwin->w_cursor;
3937
3938 /*
3939 * Search for matching "</aaa>". First isolate the "aaa".
3940 */
3941 inc_cursor();
3942 p = ml_get_cursor();
3943 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3944 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003946 if (len == 0)
3947 {
3948 curwin->w_cursor = old_pos;
3949 goto theend;
3950 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01003951 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003952 epat = alloc(len + 9);
3953 if (spat == NULL || epat == NULL)
3954 {
3955 vim_free(spat);
3956 vim_free(epat);
3957 curwin->w_cursor = old_pos;
3958 goto theend;
3959 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01003960 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003961 sprintf((char *)epat, "</%.*s>\\c", len, p);
3962
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003963 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
Bram Moolenaar76929292008-01-06 19:07:36 +00003964 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003965
3966 vim_free(spat);
3967 vim_free(epat);
3968
3969 if (r < 1 || lt(curwin->w_cursor, old_end))
3970 {
3971 /* Can't find other end or it's before the previous end. Could be a
3972 * HTML tag that doesn't have a matching end. Search backwards for
3973 * another starting tag. */
3974 count = 1;
3975 curwin->w_cursor = start_pos;
3976 goto again;
3977 }
3978
3979 if (do_include || r < 1)
3980 {
3981 /* Include up to the '>'. */
3982 while (*ml_get_cursor() != '>')
3983 if (inc_cursor() < 0)
3984 break;
3985 }
3986 else
3987 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003988 char_u *c = ml_get_cursor();
3989
3990 /* Exclude the '<' of the end tag.
3991 * If the closing tag is on new line, do not decrement cursor, but
3992 * make operation exclusive, so that the linefeed will be selected */
3993 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
3994 /* do not decrement cursor */
3995 is_inclusive = FALSE;
3996 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003997 dec_cursor();
3998 }
3999 end_pos = curwin->w_cursor;
4000
4001 if (!do_include)
4002 {
4003 /* Exclude the start tag. */
4004 curwin->w_cursor = start_pos;
4005 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004006 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004007 {
4008 inc_cursor();
4009 start_pos = curwin->w_cursor;
4010 break;
4011 }
4012 curwin->w_cursor = end_pos;
4013
Bram Moolenaar45360022005-07-21 21:08:21 +00004014 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004015 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00004016 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004017 {
4018 do_include = TRUE;
4019 curwin->w_cursor = old_start;
4020 count = count_arg;
4021 goto again;
4022 }
4023 }
4024
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004025 if (VIsual_active)
4026 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004027 /* If the end is before the start there is no text between tags, select
4028 * the char under the cursor. */
4029 if (lt(end_pos, start_pos))
4030 curwin->w_cursor = start_pos;
4031 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004032 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004033 VIsual = start_pos;
4034 VIsual_mode = 'v';
4035 redraw_curbuf_later(INVERTED); /* update the inversion */
4036 showmode();
4037 }
4038 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004039 {
4040 oap->start = start_pos;
4041 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004042 if (lt(end_pos, start_pos))
4043 {
4044 /* End is before the start: there is no text between tags; operate
4045 * on an empty area. */
4046 curwin->w_cursor = start_pos;
4047 oap->inclusive = FALSE;
4048 }
4049 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004050 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004051 }
4052 retval = OK;
4053
4054theend:
4055 p_ws = save_p_ws;
4056 return retval;
4057}
4058
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 int
4060current_par(oap, count, include, type)
4061 oparg_T *oap;
4062 long count;
4063 int include; /* TRUE == include white space */
4064 int type; /* 'p' for paragraph, 'S' for section */
4065{
4066 linenr_T start_lnum;
4067 linenr_T end_lnum;
4068 int white_in_front;
4069 int dir;
4070 int start_is_white;
4071 int prev_start_is_white;
4072 int retval = OK;
4073 int do_white = FALSE;
4074 int t;
4075 int i;
4076
4077 if (type == 'S') /* not implemented yet */
4078 return FAIL;
4079
4080 start_lnum = curwin->w_cursor.lnum;
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 /*
4083 * When visual area is more than one line: extend it.
4084 */
4085 if (VIsual_active && start_lnum != VIsual.lnum)
4086 {
4087extend:
4088 if (start_lnum < VIsual.lnum)
4089 dir = BACKWARD;
4090 else
4091 dir = FORWARD;
4092 for (i = count; --i >= 0; )
4093 {
4094 if (start_lnum ==
4095 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4096 {
4097 retval = FAIL;
4098 break;
4099 }
4100
4101 prev_start_is_white = -1;
4102 for (t = 0; t < 2; ++t)
4103 {
4104 start_lnum += dir;
4105 start_is_white = linewhite(start_lnum);
4106 if (prev_start_is_white == start_is_white)
4107 {
4108 start_lnum -= dir;
4109 break;
4110 }
4111 for (;;)
4112 {
4113 if (start_lnum == (dir == BACKWARD
4114 ? 1 : curbuf->b_ml.ml_line_count))
4115 break;
4116 if (start_is_white != linewhite(start_lnum + dir)
4117 || (!start_is_white
4118 && startPS(start_lnum + (dir > 0
4119 ? 1 : 0), 0, 0)))
4120 break;
4121 start_lnum += dir;
4122 }
4123 if (!include)
4124 break;
4125 if (start_lnum == (dir == BACKWARD
4126 ? 1 : curbuf->b_ml.ml_line_count))
4127 break;
4128 prev_start_is_white = start_is_white;
4129 }
4130 }
4131 curwin->w_cursor.lnum = start_lnum;
4132 curwin->w_cursor.col = 0;
4133 return retval;
4134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
4136 /*
4137 * First move back to the start_lnum of the paragraph or white lines
4138 */
4139 white_in_front = linewhite(start_lnum);
4140 while (start_lnum > 1)
4141 {
4142 if (white_in_front) /* stop at first white line */
4143 {
4144 if (!linewhite(start_lnum - 1))
4145 break;
4146 }
4147 else /* stop at first non-white line of start of paragraph */
4148 {
4149 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4150 break;
4151 }
4152 --start_lnum;
4153 }
4154
4155 /*
4156 * Move past the end of any white lines.
4157 */
4158 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004159 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4160 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 --end_lnum;
4163 i = count;
4164 if (!include && white_in_front)
4165 --i;
4166 while (i--)
4167 {
4168 if (end_lnum == curbuf->b_ml.ml_line_count)
4169 return FAIL;
4170
4171 if (!include)
4172 do_white = linewhite(end_lnum + 1);
4173
4174 if (include || !do_white)
4175 {
4176 ++end_lnum;
4177 /*
4178 * skip to end of paragraph
4179 */
4180 while (end_lnum < curbuf->b_ml.ml_line_count
4181 && !linewhite(end_lnum + 1)
4182 && !startPS(end_lnum + 1, 0, 0))
4183 ++end_lnum;
4184 }
4185
4186 if (i == 0 && white_in_front && include)
4187 break;
4188
4189 /*
4190 * skip to end of white lines after paragraph
4191 */
4192 if (include || do_white)
4193 while (end_lnum < curbuf->b_ml.ml_line_count
4194 && linewhite(end_lnum + 1))
4195 ++end_lnum;
4196 }
4197
4198 /*
4199 * If there are no empty lines at the end, try to find some empty lines at
4200 * the start (unless that has been done already).
4201 */
4202 if (!white_in_front && !linewhite(end_lnum) && include)
4203 while (start_lnum > 1 && linewhite(start_lnum - 1))
4204 --start_lnum;
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (VIsual_active)
4207 {
4208 /* Problem: when doing "Vipipip" nothing happens in a single white
4209 * line, we get stuck there. Trap this here. */
4210 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4211 goto extend;
4212 VIsual.lnum = start_lnum;
4213 VIsual_mode = 'V';
4214 redraw_curbuf_later(INVERTED); /* update the inversion */
4215 showmode();
4216 }
4217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 {
4219 oap->start.lnum = start_lnum;
4220 oap->start.col = 0;
4221 oap->motion_type = MLINE;
4222 }
4223 curwin->w_cursor.lnum = end_lnum;
4224 curwin->w_cursor.col = 0;
4225
4226 return OK;
4227}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004228
4229static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4230static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4231
4232/*
4233 * Search quote char from string line[col].
4234 * Quote character escaped by one of the characters in "escape" is not counted
4235 * as a quote.
4236 * Returns column number of "quotechar" or -1 when not found.
4237 */
4238 static int
4239find_next_quote(line, col, quotechar, escape)
4240 char_u *line;
4241 int col;
4242 int quotechar;
4243 char_u *escape; /* escape characters, can be NULL */
4244{
4245 int c;
4246
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004247 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004248 {
4249 c = line[col];
4250 if (c == NUL)
4251 return -1;
4252 else if (escape != NULL && vim_strchr(escape, c))
4253 ++col;
4254 else if (c == quotechar)
4255 break;
4256#ifdef FEAT_MBYTE
4257 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004258 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004261 ++col;
4262 }
4263 return col;
4264}
4265
4266/*
4267 * Search backwards in "line" from column "col_start" to find "quotechar".
4268 * Quote character escaped by one of the characters in "escape" is not counted
4269 * as a quote.
4270 * Return the found column or zero.
4271 */
4272 static int
4273find_prev_quote(line, col_start, quotechar, escape)
4274 char_u *line;
4275 int col_start;
4276 int quotechar;
4277 char_u *escape; /* escape characters, can be NULL */
4278{
4279 int n;
4280
4281 while (col_start > 0)
4282 {
4283 --col_start;
4284#ifdef FEAT_MBYTE
4285 col_start -= (*mb_head_off)(line, line + col_start);
4286#endif
4287 n = 0;
4288 if (escape != NULL)
4289 while (col_start - n > 0 && vim_strchr(escape,
4290 line[col_start - n - 1]) != NULL)
4291 ++n;
4292 if (n & 1)
4293 col_start -= n; /* uneven number of escape chars, skip it */
4294 else if (line[col_start] == quotechar)
4295 break;
4296 }
4297 return col_start;
4298}
4299
4300/*
4301 * Find quote under the cursor, cursor at end.
4302 * Returns TRUE if found, else FALSE.
4303 */
4304 int
4305current_quote(oap, count, include, quotechar)
4306 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004307 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004308 int include; /* TRUE == include quote char */
4309 int quotechar; /* Quote character */
4310{
4311 char_u *line = ml_get_curline();
4312 int col_end;
4313 int col_start = curwin->w_cursor.col;
4314 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004315 int vis_empty = TRUE; /* Visual selection <= 1 char */
4316 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004317 int inside_quotes = FALSE; /* Looks like "i'" done before */
4318 int selected_quote = FALSE; /* Has quote inside selection */
4319 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004320
4321 /* Correct cursor when 'selection' is exclusive */
4322 if (VIsual_active)
4323 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004324 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004325 if (*p_sel == 'e' && vis_bef_curs)
4326 dec_cursor();
4327 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004328 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004329
4330 if (!vis_empty)
4331 {
4332 /* Check if the existing selection exactly spans the text inside
4333 * quotes. */
4334 if (vis_bef_curs)
4335 {
4336 inside_quotes = VIsual.col > 0
4337 && line[VIsual.col - 1] == quotechar
4338 && line[curwin->w_cursor.col] != NUL
4339 && line[curwin->w_cursor.col + 1] == quotechar;
4340 i = VIsual.col;
4341 col_end = curwin->w_cursor.col;
4342 }
4343 else
4344 {
4345 inside_quotes = curwin->w_cursor.col > 0
4346 && line[curwin->w_cursor.col - 1] == quotechar
4347 && line[VIsual.col] != NUL
4348 && line[VIsual.col + 1] == quotechar;
4349 i = curwin->w_cursor.col;
4350 col_end = VIsual.col;
4351 }
4352
4353 /* Find out if we have a quote in the selection. */
4354 while (i <= col_end)
4355 if (line[i++] == quotechar)
4356 {
4357 selected_quote = TRUE;
4358 break;
4359 }
4360 }
4361
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004362 if (!vis_empty && line[col_start] == quotechar)
4363 {
4364 /* Already selecting something and on a quote character. Find the
4365 * next quoted string. */
4366 if (vis_bef_curs)
4367 {
4368 /* Assume we are on a closing quote: move to after the next
4369 * opening quote. */
4370 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4371 if (col_start < 0)
4372 return FALSE;
4373 col_end = find_next_quote(line, col_start + 1, quotechar,
4374 curbuf->b_p_qe);
4375 if (col_end < 0)
4376 {
4377 /* We were on a starting quote perhaps? */
4378 col_end = col_start;
4379 col_start = curwin->w_cursor.col;
4380 }
4381 }
4382 else
4383 {
4384 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4385 if (line[col_end] != quotechar)
4386 return FALSE;
4387 col_start = find_prev_quote(line, col_end, quotechar,
4388 curbuf->b_p_qe);
4389 if (line[col_start] != quotechar)
4390 {
4391 /* We were on an ending quote perhaps? */
4392 col_start = col_end;
4393 col_end = curwin->w_cursor.col;
4394 }
4395 }
4396 }
4397 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004398
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004399 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004400 {
4401 int first_col = col_start;
4402
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004403 if (!vis_empty)
4404 {
4405 if (vis_bef_curs)
4406 first_col = find_next_quote(line, col_start, quotechar, NULL);
4407 else
4408 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4409 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004410
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004411 /* The cursor is on a quote, we don't know if it's the opening or
4412 * closing quote. Search from the start of the line to find out.
4413 * Also do this when there is a Visual area, a' may leave the cursor
4414 * in between two strings. */
4415 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004416 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004417 {
4418 /* Find open quote character. */
4419 col_start = find_next_quote(line, col_start, quotechar, NULL);
4420 if (col_start < 0 || col_start > first_col)
4421 return FALSE;
4422 /* Find close quote character. */
4423 col_end = find_next_quote(line, col_start + 1, quotechar,
4424 curbuf->b_p_qe);
4425 if (col_end < 0)
4426 return FALSE;
4427 /* If is cursor between start and end quote character, it is
4428 * target text object. */
4429 if (col_start <= first_col && first_col <= col_end)
4430 break;
4431 col_start = col_end + 1;
4432 }
4433 }
4434 else
4435 {
4436 /* Search backward for a starting quote. */
4437 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4438 if (line[col_start] != quotechar)
4439 {
4440 /* No quote before the cursor, look after the cursor. */
4441 col_start = find_next_quote(line, col_start, quotechar, NULL);
4442 if (col_start < 0)
4443 return FALSE;
4444 }
4445
4446 /* Find close quote character. */
4447 col_end = find_next_quote(line, col_start + 1, quotechar,
4448 curbuf->b_p_qe);
4449 if (col_end < 0)
4450 return FALSE;
4451 }
4452
4453 /* When "include" is TRUE, include spaces after closing quote or before
4454 * the starting quote. */
4455 if (include)
4456 {
4457 if (vim_iswhite(line[col_end + 1]))
4458 while (vim_iswhite(line[col_end + 1]))
4459 ++col_end;
4460 else
4461 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4462 --col_start;
4463 }
4464
Bram Moolenaarab194812005-09-14 21:40:12 +00004465 /* Set start position. After vi" another i" must include the ".
4466 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004467 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004468 ++col_start;
4469 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004470 if (VIsual_active)
4471 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004472 /* Set the start of the Visual area when the Visual area was empty, we
4473 * were just inside quotes or the Visual area didn't start at a quote
4474 * and didn't include a quote.
4475 */
4476 if (vis_empty
4477 || (vis_bef_curs
4478 && !selected_quote
4479 && (inside_quotes
4480 || (line[VIsual.col] != quotechar
4481 && (VIsual.col == 0
4482 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004483 {
4484 VIsual = curwin->w_cursor;
4485 redraw_curbuf_later(INVERTED);
4486 }
4487 }
4488 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004489 {
4490 oap->start = curwin->w_cursor;
4491 oap->motion_type = MCHAR;
4492 }
4493
4494 /* Set end position. */
4495 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004496 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004497 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004498 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004499 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004500 if (VIsual_active)
4501 {
4502 if (vis_empty || vis_bef_curs)
4503 {
4504 /* decrement cursor when 'selection' is not exclusive */
4505 if (*p_sel != 'e')
4506 dec_cursor();
4507 }
4508 else
4509 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004510 /* Cursor is at start of Visual area. Set the end of the Visual
4511 * area when it was just inside quotes or it didn't end at a
4512 * quote. */
4513 if (inside_quotes
4514 || (!selected_quote
4515 && line[VIsual.col] != quotechar
4516 && (line[VIsual.col] == NUL
4517 || line[VIsual.col + 1] != quotechar)))
4518 {
4519 dec_cursor();
4520 VIsual = curwin->w_cursor;
4521 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004522 curwin->w_cursor.col = col_start;
4523 }
4524 if (VIsual_mode == 'V')
4525 {
4526 VIsual_mode = 'v';
4527 redraw_cmdline = TRUE; /* show mode later */
4528 }
4529 }
4530 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004531 {
4532 /* Set inclusive and other oap's flags. */
4533 oap->inclusive = inclusive;
4534 }
4535
4536 return OK;
4537}
4538
4539#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004541static int is_one_char __ARGS((char_u *pattern, int move));
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004542
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004543/*
4544 * Find next search match under cursor, cursor at end.
4545 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004546 */
4547 int
4548current_search(count, forward)
4549 long count;
4550 int forward; /* move forward or backwards */
4551{
4552 pos_T start_pos; /* position before the pattern */
4553 pos_T orig_pos; /* position of the cursor at beginning */
4554 pos_T pos; /* position after the pattern */
4555 int i;
4556 int dir;
4557 int result; /* result of various function calls */
4558 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004559 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004560 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004561 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004562
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004563 /* wrapping should not occur */
4564 p_ws = FALSE;
4565
4566 /* Correct cursor when 'selection' is exclusive */
4567 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
4568 dec_cursor();
4569
4570 if (VIsual_active)
4571 {
4572 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004573
4574 pos = curwin->w_cursor;
4575 start_pos = VIsual;
4576
4577 /* make sure, searching further will extend the match */
4578 if (VIsual_active)
4579 {
4580 if (forward)
4581 incl(&pos);
4582 else
4583 decl(&pos);
4584 }
4585 }
4586 else
4587 orig_pos = pos = start_pos = curwin->w_cursor;
4588
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004589 /* Is the pattern is zero-width? */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004590 one_char = is_one_char(spats[last_idx].pat, TRUE);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004591 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004592 {
4593 p_ws = old_p_ws;
4594 return FAIL; /* pattern not found */
4595 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004596
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004597 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004598 * The trick is to first search backwards and then search forward again,
4599 * so that a match at the current cursor position will be correctly
4600 * captured.
4601 */
4602 for (i = 0; i < 2; i++)
4603 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004604 if (forward)
4605 dir = i;
4606 else
4607 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004608
4609 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004610 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004611 flags = SEARCH_END;
4612
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004613 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4614 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004615 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004616
4617 /* First search may fail, but then start searching from the
4618 * beginning of the file (cursor might be on the search match)
4619 * except when Visual mode is active, so that extending the visual
4620 * selection works. */
4621 if (!result && i) /* not found, abort */
4622 {
4623 curwin->w_cursor = orig_pos;
4624 if (VIsual_active)
4625 VIsual = save_VIsual;
4626 p_ws = old_p_ws;
4627 return FAIL;
4628 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004629 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004630 {
4631 if (forward) /* try again from start of buffer */
4632 {
4633 clearpos(&pos);
4634 }
4635 else /* try again from end of buffer */
4636 {
4637 /* searching backwards, so set pos to last line and col */
4638 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004639 pos.col = (colnr_T)STRLEN(
4640 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004641 }
4642 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004643 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004644 }
4645
4646 start_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004647 flags = forward ? SEARCH_END : 0;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004648
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004649 /* Check again from the current cursor position,
4650 * since the next match might actually by only one char wide */
4651 one_char = is_one_char(spats[last_idx].pat, FALSE);
4652
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004653 /* move to match, except for zero-width matches, in which case, we are
4654 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004655 if (!one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004656 result = searchit(curwin, curbuf, &pos, (forward ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004657 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0, NULL);
4658
4659 if (!VIsual_active)
4660 VIsual = start_pos;
4661
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004662 curwin->w_cursor = pos;
4663 VIsual_active = TRUE;
4664 VIsual_mode = 'v';
4665
4666 if (VIsual_active)
4667 {
4668 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004669 if (*p_sel == 'e')
4670 {
4671 /* Correction for exclusive selection depends on the direction. */
4672 if (forward && ltoreq(VIsual, curwin->w_cursor))
4673 inc_cursor();
4674 else if (!forward && ltoreq(curwin->w_cursor, VIsual))
4675 inc(&VIsual);
4676 }
4677
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004678 }
4679
4680#ifdef FEAT_FOLDING
4681 if (fdo_flags & FDO_SEARCH && KeyTyped)
4682 foldOpenCursor();
4683#endif
4684
4685 may_start_select('c');
4686#ifdef FEAT_MOUSE
4687 setmouse();
4688#endif
4689#ifdef FEAT_CLIPBOARD
4690 /* Make sure the clipboard gets updated. Needed because start and
4691 * end are still the same, and the selection needs to be owned */
4692 clip_star.vmode = NUL;
4693#endif
4694 redraw_curbuf_later(INVERTED);
4695 showmode();
4696
4697 return OK;
4698}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004699
4700/*
Bram Moolenaare78495d2013-06-30 14:46:53 +02004701 * Check if the pattern is one character or zero-width.
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004702 * If move is TRUE, check from the beginning of the buffer, else from the
4703 * current cursor position.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004704 * Returns TRUE, FALSE or -1 for failure.
4705 */
4706 static int
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004707is_one_char(pattern, move)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004708 char_u *pattern;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004709 int move;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004710{
4711 regmmatch_T regmatch;
4712 int nmatched = 0;
4713 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004714 pos_T pos;
4715 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004716 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004717
4718 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4719 SEARCH_KEEP, &regmatch) == FAIL)
4720 return -1;
4721
4722 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004723 if (move)
4724 clearpos(&pos)
4725 else
4726 {
4727 pos = curwin->w_cursor;
4728 /* accept a match at the cursor position */
4729 flag = SEARCH_START;
4730 }
4731
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004732 if (searchit(curwin, curbuf, &pos, FORWARD, spats[last_idx].pat, 1,
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004733 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004734 {
4735 /* Zero-width pattern should match somewhere, then we can check if
4736 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004737 called_emsg = FALSE;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004738 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
4739 pos.lnum, (colnr_T)0, NULL);
4740
4741 if (!called_emsg)
4742 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004743 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4744 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004745
Bram Moolenaar4c7cb6b2013-10-02 21:55:02 +02004746 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4747 result = TRUE;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004748 }
4749
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004750 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004751 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004752 return result;
4753}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4756 || defined(PROTO)
4757/*
4758 * return TRUE if line 'lnum' is empty or has white chars only.
4759 */
4760 int
4761linewhite(lnum)
4762 linenr_T lnum;
4763{
4764 char_u *p;
4765
4766 p = skipwhite(ml_get(lnum));
4767 return (*p == NUL);
4768}
4769#endif
4770
4771#if defined(FEAT_FIND_ID) || defined(PROTO)
4772/*
4773 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004774 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 void
4777find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4778 type, count, action, start_lnum, end_lnum)
4779 char_u *ptr; /* pointer to search pattern */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004780 int dir UNUSED; /* direction of expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 int len; /* length of search pattern */
4782 int whole; /* match whole words only */
4783 int skip_comments; /* don't match inside comments */
4784 int type; /* Type of search; are we looking for a type?
4785 a macro? */
4786 long count;
4787 int action; /* What to do when we find it */
4788 linenr_T start_lnum; /* first line to start searching */
4789 linenr_T end_lnum; /* last line for searching */
4790{
4791 SearchedFile *files; /* Stack of included files */
4792 SearchedFile *bigger; /* When we need more space */
4793 int max_path_depth = 50;
4794 long match_count = 1;
4795
4796 char_u *pat;
4797 char_u *new_fname;
4798 char_u *curr_fname = curbuf->b_fname;
4799 char_u *prev_fname = NULL;
4800 linenr_T lnum;
4801 int depth;
4802 int depth_displayed; /* For type==CHECK_PATH */
4803 int old_files;
4804 int already_searched;
4805 char_u *file_line;
4806 char_u *line;
4807 char_u *p;
4808 char_u save_char;
4809 int define_matched;
4810 regmatch_T regmatch;
4811 regmatch_T incl_regmatch;
4812 regmatch_T def_regmatch;
4813 int matched = FALSE;
4814 int did_show = FALSE;
4815 int found = FALSE;
4816 int i;
4817 char_u *already = NULL;
4818 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004819 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4821 win_T *curwin_save = NULL;
4822#endif
4823
4824 regmatch.regprog = NULL;
4825 incl_regmatch.regprog = NULL;
4826 def_regmatch.regprog = NULL;
4827
4828 file_line = alloc(LSIZE);
4829 if (file_line == NULL)
4830 return;
4831
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (type != CHECK_PATH && type != FIND_DEFINE
4833#ifdef FEAT_INS_EXPAND
4834 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4835 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004836 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837#endif
4838 )
4839 {
4840 pat = alloc(len + 5);
4841 if (pat == NULL)
4842 goto fpip_end;
4843 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4844 /* ignore case according to p_ic, p_scs and pat */
4845 regmatch.rm_ic = ignorecase(pat);
4846 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4847 vim_free(pat);
4848 if (regmatch.regprog == NULL)
4849 goto fpip_end;
4850 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004851 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4852 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004854 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (incl_regmatch.regprog == NULL)
4856 goto fpip_end;
4857 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4858 }
4859 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4860 {
4861 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4862 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4863 if (def_regmatch.regprog == NULL)
4864 goto fpip_end;
4865 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4866 }
4867 files = (SearchedFile *)lalloc_clear((long_u)
4868 (max_path_depth * sizeof(SearchedFile)), TRUE);
4869 if (files == NULL)
4870 goto fpip_end;
4871 old_files = max_path_depth;
4872 depth = depth_displayed = -1;
4873
4874 lnum = start_lnum;
4875 if (end_lnum > curbuf->b_ml.ml_line_count)
4876 end_lnum = curbuf->b_ml.ml_line_count;
4877 if (lnum > end_lnum) /* do at least one line */
4878 lnum = end_lnum;
4879 line = ml_get(lnum);
4880
4881 for (;;)
4882 {
4883 if (incl_regmatch.regprog != NULL
4884 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4885 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004886 char_u *p_fname = (curr_fname == curbuf->b_fname)
4887 ? curbuf->b_ffname : curr_fname;
4888
4889 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4890 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4891 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004892 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004893 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4894 else
4895 /* Use text after match with 'include'. */
4896 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004897 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 already_searched = FALSE;
4899 if (new_fname != NULL)
4900 {
4901 /* Check whether we have already searched in this file */
4902 for (i = 0;; i++)
4903 {
4904 if (i == depth + 1)
4905 i = old_files;
4906 if (i == max_path_depth)
4907 break;
4908 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4909 {
4910 if (type != CHECK_PATH &&
4911 action == ACTION_SHOW_ALL && files[i].matched)
4912 {
4913 msg_putchar('\n'); /* cursor below last one */
4914 if (!got_int) /* don't display if 'q'
4915 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00004916 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 msg_home_replace_hl(new_fname);
4919 MSG_PUTS(_(" (includes previously listed match)"));
4920 prev_fname = NULL;
4921 }
4922 }
4923 vim_free(new_fname);
4924 new_fname = NULL;
4925 already_searched = TRUE;
4926 break;
4927 }
4928 }
4929 }
4930
4931 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4932 || (new_fname == NULL && !already_searched)))
4933 {
4934 if (did_show)
4935 msg_putchar('\n'); /* cursor below last one */
4936 else
4937 {
4938 gotocmdline(TRUE); /* cursor at status line */
4939 MSG_PUTS_TITLE(_("--- Included files "));
4940 if (action != ACTION_SHOW_ALL)
4941 MSG_PUTS_TITLE(_("not found "));
4942 MSG_PUTS_TITLE(_("in path ---\n"));
4943 }
4944 did_show = TRUE;
4945 while (depth_displayed < depth && !got_int)
4946 {
4947 ++depth_displayed;
4948 for (i = 0; i < depth_displayed; i++)
4949 MSG_PUTS(" ");
4950 msg_home_replace(files[depth_displayed].name);
4951 MSG_PUTS(" -->\n");
4952 }
4953 if (!got_int) /* don't display if 'q' typed
4954 for "--more--" message */
4955 {
4956 for (i = 0; i <= depth_displayed; i++)
4957 MSG_PUTS(" ");
4958 if (new_fname != NULL)
4959 {
4960 /* using "new_fname" is more reliable, e.g., when
4961 * 'includeexpr' is set. */
4962 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4963 }
4964 else
4965 {
4966 /*
4967 * Isolate the file name.
4968 * Include the surrounding "" or <> if present.
4969 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02004970 if (inc_opt != NULL
4971 && strstr((char *)inc_opt, "\\zs") != NULL)
4972 {
4973 /* pattern contains \zs, use the match */
4974 p = incl_regmatch.startp[0];
4975 i = (int)(incl_regmatch.endp[0]
4976 - incl_regmatch.startp[0]);
4977 }
4978 else
4979 {
4980 /* find the file name after the end of the match */
4981 for (p = incl_regmatch.endp[0];
4982 *p && !vim_isfilec(*p); p++)
4983 ;
4984 for (i = 0; vim_isfilec(p[i]); i++)
4985 ;
4986 }
4987
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (i == 0)
4989 {
4990 /* Nothing found, use the rest of the line. */
4991 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004992 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02004994 /* Avoid checking before the start of the line, can
4995 * happen if \zs appears in the regexp. */
4996 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
4998 if (p[-1] == '"' || p[-1] == '<')
4999 {
5000 --p;
5001 ++i;
5002 }
5003 if (p[i] == '"' || p[i] == '>')
5004 ++i;
5005 }
5006 save_char = p[i];
5007 p[i] = NUL;
5008 msg_outtrans_attr(p, hl_attr(HLF_D));
5009 p[i] = save_char;
5010 }
5011
5012 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5013 {
5014 if (already_searched)
5015 MSG_PUTS(_(" (Already listed)"));
5016 else
5017 MSG_PUTS(_(" NOT FOUND"));
5018 }
5019 }
5020 out_flush(); /* output each line directly */
5021 }
5022
5023 if (new_fname != NULL)
5024 {
5025 /* Push the new file onto the file stack */
5026 if (depth + 1 == old_files)
5027 {
5028 bigger = (SearchedFile *)lalloc((long_u)(
5029 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5030 if (bigger != NULL)
5031 {
5032 for (i = 0; i <= depth; i++)
5033 bigger[i] = files[i];
5034 for (i = depth + 1; i < old_files + max_path_depth; i++)
5035 {
5036 bigger[i].fp = NULL;
5037 bigger[i].name = NULL;
5038 bigger[i].lnum = 0;
5039 bigger[i].matched = FALSE;
5040 }
5041 for (i = old_files; i < max_path_depth; i++)
5042 bigger[i + max_path_depth] = files[i];
5043 old_files += max_path_depth;
5044 max_path_depth *= 2;
5045 vim_free(files);
5046 files = bigger;
5047 }
5048 }
5049 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5050 == NULL)
5051 vim_free(new_fname);
5052 else
5053 {
5054 if (++depth == old_files)
5055 {
5056 /*
5057 * lalloc() for 'bigger' must have failed above. We
5058 * will forget one of our already visited files now.
5059 */
5060 vim_free(files[old_files].name);
5061 ++old_files;
5062 }
5063 files[depth].name = curr_fname = new_fname;
5064 files[depth].lnum = 0;
5065 files[depth].matched = FALSE;
5066#ifdef FEAT_INS_EXPAND
5067 if (action == ACTION_EXPAND)
5068 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005069 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005070 vim_snprintf((char*)IObuff, IOSIZE,
5071 _("Scanning included file: %s"),
5072 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
5074 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005075 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005077 if (p_verbose >= 5)
5078 {
5079 verbose_enter();
5080 smsg((char_u *)_("Searching included file %s"),
5081 (char *)new_fname);
5082 verbose_leave();
5083 }
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 }
5087 }
5088 else
5089 {
5090 /*
5091 * Check if the line is a define (type == FIND_DEFINE)
5092 */
5093 p = line;
5094search_line:
5095 define_matched = FALSE;
5096 if (def_regmatch.regprog != NULL
5097 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5098 {
5099 /*
5100 * Pattern must be first identifier after 'define', so skip
5101 * to that position before checking for match of pattern. Also
5102 * don't let it match beyond the end of this identifier.
5103 */
5104 p = def_regmatch.endp[0];
5105 while (*p && !vim_iswordc(*p))
5106 p++;
5107 define_matched = TRUE;
5108 }
5109
5110 /*
5111 * Look for a match. Don't do this if we are looking for a
5112 * define and this line didn't match define_prog above.
5113 */
5114 if (def_regmatch.regprog == NULL || define_matched)
5115 {
5116 if (define_matched
5117#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005118 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119#endif
5120 )
5121 {
5122 /* compare the first "len" chars from "ptr" */
5123 startp = skipwhite(p);
5124 if (p_ic)
5125 matched = !MB_STRNICMP(startp, ptr, len);
5126 else
5127 matched = !STRNCMP(startp, ptr, len);
5128 if (matched && define_matched && whole
5129 && vim_iswordc(startp[len]))
5130 matched = FALSE;
5131 }
5132 else if (regmatch.regprog != NULL
5133 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5134 {
5135 matched = TRUE;
5136 startp = regmatch.startp[0];
5137 /*
5138 * Check if the line is not a comment line (unless we are
5139 * looking for a define). A line starting with "# define"
5140 * is not considered to be a comment line.
5141 */
5142 if (!define_matched && skip_comments)
5143 {
5144#ifdef FEAT_COMMENTS
5145 if ((*line != '#' ||
5146 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005147 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 matched = FALSE;
5149
5150 /*
5151 * Also check for a "/ *" or "/ /" before the match.
5152 * Skips lines like "int backwards; / * normal index
5153 * * /" when looking for "normal".
5154 * Note: Doesn't skip "/ *" in comments.
5155 */
5156 p = skipwhite(line);
5157 if (matched
5158 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5159#endif
5160 for (p = line; *p && p < startp; ++p)
5161 {
5162 if (matched
5163 && p[0] == '/'
5164 && (p[1] == '*' || p[1] == '/'))
5165 {
5166 matched = FALSE;
5167 /* After "//" all text is comment */
5168 if (p[1] == '/')
5169 break;
5170 ++p;
5171 }
5172 else if (!matched && p[0] == '*' && p[1] == '/')
5173 {
5174 /* Can find match after "* /". */
5175 matched = TRUE;
5176 ++p;
5177 }
5178 }
5179 }
5180 }
5181 }
5182 }
5183 if (matched)
5184 {
5185#ifdef FEAT_INS_EXPAND
5186 if (action == ACTION_EXPAND)
5187 {
5188 int reuse = 0;
5189 int add_r;
5190 char_u *aux;
5191
5192 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5193 break;
5194 found = TRUE;
5195 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005198 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (vim_iswordp(p))
5200 goto exit_matched;
5201 p = find_word_start(p);
5202 }
5203 p = find_word_end(p);
5204 i = (int)(p - aux);
5205
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005206 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005210
5211 /* Get the next line: when "depth" < 0 from the current
5212 * buffer, otherwise from the included file. Jump to
5213 * exit_matched when past the last line. */
5214 if (depth < 0)
5215 {
5216 if (lnum >= end_lnum)
5217 goto exit_matched;
5218 line = ml_get(++lnum);
5219 }
5220 else if (vim_fgets(line = file_line,
5221 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 goto exit_matched;
5223
5224 /* we read a line, set "already" to check this "line" later
5225 * if depth >= 0 we'll increase files[depth].lnum far
5226 * bellow -- Acevedo */
5227 already = aux = p = skipwhite(line);
5228 p = find_word_start(p);
5229 p = find_word_end(p);
5230 if (p > aux)
5231 {
5232 if (*aux != ')' && IObuff[i-1] != TAB)
5233 {
5234 if (IObuff[i-1] != ' ')
5235 IObuff[i++] = ' ';
5236 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5237 if (p_js
5238 && (IObuff[i-2] == '.'
5239 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5240 && (IObuff[i-2] == '?'
5241 || IObuff[i-2] == '!'))))
5242 IObuff[i++] = ' ';
5243 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005244 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if (p - aux >= IOSIZE - i)
5246 p = aux + IOSIZE - i - 1;
5247 STRNCPY(IObuff + i, aux, p - aux);
5248 i += (int)(p - aux);
5249 reuse |= CONT_S_IPOS;
5250 }
5251 IObuff[i] = NUL;
5252 aux = IObuff;
5253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005254 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 goto exit_matched;
5256 }
5257
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005258 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5260 dir, reuse);
5261 if (add_r == OK)
5262 /* if dir was BACKWARD then honor it just once */
5263 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005264 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 break;
5266 }
5267 else
5268#endif
5269 if (action == ACTION_SHOW_ALL)
5270 {
5271 found = TRUE;
5272 if (!did_show)
5273 gotocmdline(TRUE); /* cursor at status line */
5274 if (curr_fname != prev_fname)
5275 {
5276 if (did_show)
5277 msg_putchar('\n'); /* cursor below last one */
5278 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005279 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 msg_home_replace_hl(curr_fname);
5281 prev_fname = curr_fname;
5282 }
5283 did_show = TRUE;
5284 if (!got_int)
5285 show_pat_in_path(line, type, TRUE, action,
5286 (depth == -1) ? NULL : files[depth].fp,
5287 (depth == -1) ? &lnum : &files[depth].lnum,
5288 match_count++);
5289
5290 /* Set matched flag for this file and all the ones that
5291 * include it */
5292 for (i = 0; i <= depth; ++i)
5293 files[i].matched = TRUE;
5294 }
5295 else if (--count <= 0)
5296 {
5297 found = TRUE;
5298 if (depth == -1 && lnum == curwin->w_cursor.lnum
5299#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5300 && g_do_tagpreview == 0
5301#endif
5302 )
5303 EMSG(_("E387: Match is on current line"));
5304 else if (action == ACTION_SHOW)
5305 {
5306 show_pat_in_path(line, type, did_show, action,
5307 (depth == -1) ? NULL : files[depth].fp,
5308 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5309 did_show = TRUE;
5310 }
5311 else
5312 {
5313#ifdef FEAT_GUI
5314 need_mouse_correct = TRUE;
5315#endif
5316#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5317 /* ":psearch" uses the preview window */
5318 if (g_do_tagpreview != 0)
5319 {
5320 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005321 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 }
5323#endif
5324 if (action == ACTION_SPLIT)
5325 {
5326#ifdef FEAT_WINDOWS
5327 if (win_split(0, 0) == FAIL)
5328#endif
5329 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005330 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 }
5332 if (depth == -1)
5333 {
5334 /* match in current file */
5335#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5336 if (g_do_tagpreview != 0)
5337 {
5338 if (getfile(0, curwin_save->w_buffer->b_fname,
5339 NULL, TRUE, lnum, FALSE) > 0)
5340 break; /* failed to jump to file */
5341 }
5342 else
5343#endif
5344 setpcmark();
5345 curwin->w_cursor.lnum = lnum;
5346 }
5347 else
5348 {
5349 if (getfile(0, files[depth].name, NULL, TRUE,
5350 files[depth].lnum, FALSE) > 0)
5351 break; /* failed to jump to file */
5352 /* autocommands may have changed the lnum, we don't
5353 * want that here */
5354 curwin->w_cursor.lnum = files[depth].lnum;
5355 }
5356 }
5357 if (action != ACTION_SHOW)
5358 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005359 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 curwin->w_set_curswant = TRUE;
5361 }
5362
5363#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5364 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005365 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
5367 /* Return cursor to where we were */
5368 validate_cursor();
5369 redraw_later(VALID);
5370 win_enter(curwin_save, TRUE);
5371 }
5372#endif
5373 break;
5374 }
5375#ifdef FEAT_INS_EXPAND
5376exit_matched:
5377#endif
5378 matched = FALSE;
5379 /* look for other matches in the rest of the line if we
5380 * are not at the end of it already */
5381 if (def_regmatch.regprog == NULL
5382#ifdef FEAT_INS_EXPAND
5383 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005386 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005387 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 goto search_line;
5389 }
5390 line_breakcheck();
5391#ifdef FEAT_INS_EXPAND
5392 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00005393 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395#else
5396 if (got_int)
5397#endif
5398 break;
5399
5400 /*
5401 * Read the next line. When reading an included file and encountering
5402 * end-of-file, close the file and continue in the file that included
5403 * it.
5404 */
5405 while (depth >= 0 && !already
5406 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5407 {
5408 fclose(files[depth].fp);
5409 --old_files;
5410 files[old_files].name = files[depth].name;
5411 files[old_files].matched = files[depth].matched;
5412 --depth;
5413 curr_fname = (depth == -1) ? curbuf->b_fname
5414 : files[depth].name;
5415 if (depth < depth_displayed)
5416 depth_displayed = depth;
5417 }
5418 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005421 /* Remove any CR and LF from the line. */
5422 i = (int)STRLEN(line);
5423 if (i > 0 && line[i - 1] == '\n')
5424 line[--i] = NUL;
5425 if (i > 0 && line[i - 1] == '\r')
5426 line[--i] = NUL;
5427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 else if (!already)
5429 {
5430 if (++lnum > end_lnum)
5431 break;
5432 line = ml_get(lnum);
5433 }
5434 already = NULL;
5435 }
5436 /* End of big for (;;) loop. */
5437
5438 /* Close any files that are still open. */
5439 for (i = 0; i <= depth; i++)
5440 {
5441 fclose(files[i].fp);
5442 vim_free(files[i].name);
5443 }
5444 for (i = old_files; i < max_path_depth; i++)
5445 vim_free(files[i].name);
5446 vim_free(files);
5447
5448 if (type == CHECK_PATH)
5449 {
5450 if (!did_show)
5451 {
5452 if (action != ACTION_SHOW_ALL)
5453 MSG(_("All included files were found"));
5454 else
5455 MSG(_("No included files"));
5456 }
5457 }
5458 else if (!found
5459#ifdef FEAT_INS_EXPAND
5460 && action != ACTION_EXPAND
5461#endif
5462 )
5463 {
5464#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005465 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466#else
5467 if (got_int)
5468#endif
5469 EMSG(_(e_interr));
5470 else if (type == FIND_DEFINE)
5471 EMSG(_("E388: Couldn't find definition"));
5472 else
5473 EMSG(_("E389: Couldn't find pattern"));
5474 }
5475 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5476 msg_end();
5477
5478fpip_end:
5479 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005480 vim_regfree(regmatch.regprog);
5481 vim_regfree(incl_regmatch.regprog);
5482 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483}
5484
5485 static void
5486show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5487 char_u *line;
5488 int type;
5489 int did_show;
5490 int action;
5491 FILE *fp;
5492 linenr_T *lnum;
5493 long count;
5494{
5495 char_u *p;
5496
5497 if (did_show)
5498 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005499 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 gotocmdline(TRUE); /* cursor at status line */
5501 if (got_int) /* 'q' typed at "--more--" message */
5502 return;
5503 for (;;)
5504 {
5505 p = line + STRLEN(line) - 1;
5506 if (fp != NULL)
5507 {
5508 /* We used fgets(), so get rid of newline at end */
5509 if (p >= line && *p == '\n')
5510 --p;
5511 if (p >= line && *p == '\r')
5512 --p;
5513 *(p + 1) = NUL;
5514 }
5515 if (action == ACTION_SHOW_ALL)
5516 {
5517 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5518 msg_puts(IObuff);
5519 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5520 /* Highlight line numbers */
5521 msg_puts_attr(IObuff, hl_attr(HLF_N));
5522 MSG_PUTS(" ");
5523 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005524 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 out_flush(); /* show one line at a time */
5526
5527 /* Definition continues until line that doesn't end with '\' */
5528 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5529 break;
5530
5531 if (fp != NULL)
5532 {
5533 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5534 break;
5535 ++*lnum;
5536 }
5537 else
5538 {
5539 if (++*lnum > curbuf->b_ml.ml_line_count)
5540 break;
5541 line = ml_get(*lnum);
5542 }
5543 msg_putchar('\n');
5544 }
5545}
5546#endif
5547
5548#ifdef FEAT_VIMINFO
5549 int
5550read_viminfo_search_pattern(virp, force)
5551 vir_T *virp;
5552 int force;
5553{
5554 char_u *lp;
5555 int idx = -1;
5556 int magic = FALSE;
5557 int no_scs = FALSE;
5558 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005559 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 long off = 0;
5561 int setlast = FALSE;
5562#ifdef FEAT_SEARCH_EXTRA
5563 static int hlsearch_on = FALSE;
5564#endif
5565 char_u *val;
5566
5567 /*
5568 * Old line types:
5569 * "/pat", "&pat": search/subst. pat
5570 * "~/pat", "~&pat": last used search/subst. pat
5571 * New line types:
5572 * "~h", "~H": hlsearch highlighting off/on
5573 * "~<magic><smartcase><line><end><off><last><which>pat"
5574 * <magic>: 'm' off, 'M' on
5575 * <smartcase>: 's' off, 'S' on
5576 * <line>: 'L' line offset, 'l' char offset
5577 * <end>: 'E' from end, 'e' from start
5578 * <off>: decimal, offset
5579 * <last>: '~' last used pattern
5580 * <which>: '/' search pat, '&' subst. pat
5581 */
5582 lp = virp->vir_line;
5583 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5584 {
5585 if (lp[1] == 'M') /* magic on */
5586 magic = TRUE;
5587 if (lp[2] == 's')
5588 no_scs = TRUE;
5589 if (lp[3] == 'L')
5590 off_line = TRUE;
5591 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005592 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 lp += 5;
5594 off = getdigits(&lp);
5595 }
5596 if (lp[0] == '~') /* use this pattern for last-used pattern */
5597 {
5598 setlast = TRUE;
5599 lp++;
5600 }
5601 if (lp[0] == '/')
5602 idx = RE_SEARCH;
5603 else if (lp[0] == '&')
5604 idx = RE_SUBST;
5605#ifdef FEAT_SEARCH_EXTRA
5606 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5607 hlsearch_on = FALSE;
5608 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5609 hlsearch_on = TRUE;
5610#endif
5611 if (idx >= 0)
5612 {
5613 if (force || spats[idx].pat == NULL)
5614 {
5615 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5616 TRUE);
5617 if (val != NULL)
5618 {
5619 set_last_search_pat(val, idx, magic, setlast);
5620 vim_free(val);
5621 spats[idx].no_scs = no_scs;
5622 spats[idx].off.line = off_line;
5623 spats[idx].off.end = off_end;
5624 spats[idx].off.off = off;
5625#ifdef FEAT_SEARCH_EXTRA
5626 if (setlast)
Bram Moolenaar8050efa2013-11-08 04:30:20 +01005627 {
5628 SET_NO_HLSEARCH(!hlsearch_on);
5629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630#endif
5631 }
5632 }
5633 }
5634 return viminfo_readline(virp);
5635}
5636
5637 void
5638write_viminfo_search_pattern(fp)
5639 FILE *fp;
5640{
5641 if (get_viminfo_parameter('/') != 0)
5642 {
5643#ifdef FEAT_SEARCH_EXTRA
5644 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5645 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5646#endif
5647 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005648 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 }
5650}
5651
5652 static void
5653wvsp_one(fp, idx, s, sc)
5654 FILE *fp; /* file to write to */
5655 int idx; /* spats[] index */
5656 char *s; /* search pat */
5657 int sc; /* dir char */
5658{
5659 if (spats[idx].pat != NULL)
5660 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005661 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 /* off.dir is not stored, it's reset to forward */
5663 fprintf(fp, "%c%c%c%c%ld%s%c",
5664 spats[idx].magic ? 'M' : 'm', /* magic */
5665 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5666 spats[idx].off.line ? 'L' : 'l', /* line offset */
5667 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5668 spats[idx].off.off, /* offset */
5669 last_idx == idx ? "~" : "", /* last used pat */
5670 sc);
5671 viminfo_writestring(fp, spats[idx].pat);
5672 }
5673}
5674#endif /* FEAT_VIMINFO */