blob: 0cbbf4fefb94a3e617505d765498b9ef1a8bc662 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaarbaaa7e92016-01-29 22:47:03 +010016static void set_vv_searchforward(void);
17static int first_submatch(regmmatch_T *rp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static int check_linecomment(char_u *line);
20static int cls(void);
21static int skip_chars(int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_FIND_ID
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010023static void show_pat_in_path(char_u *, int,
24 int, int, FILE *, linenr_T *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
26#ifdef FEAT_VIMINFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010027static void wvsp_one(FILE *fp, int idx, char *s, int sc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#endif
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/*
31 * This file contains various searching-related routines. These fall into
32 * three groups:
33 * 1. string searches (for /, ?, n, and N)
34 * 2. character searches within a single line (for f, F, t, T, etc)
35 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
36 */
37
38/*
39 * String searches
40 *
41 * The string search functions are divided into two levels:
42 * lowest: searchit(); uses an pos_T for starting position and found match.
43 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
44 *
45 * The last search pattern is remembered for repeating the same search.
46 * This pattern is shared between the :g, :s, ? and / commands.
47 * This is in search_regcomp().
48 *
49 * The actual string matching is done using a heavily modified version of
50 * Henry Spencer's regular expression library. See regexp.c.
51 */
52
53/* The offset for a search command is store in a soff struct */
54/* Note: only spats[0].off is really used */
55struct soffset
56{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000057 int dir; /* search direction, '/' or '?' */
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 int line; /* search has line offset */
59 int end; /* search set cursor at end */
60 long off; /* line or char offset */
61};
62
63/* A search pattern and its attributes are stored in a spat struct */
64struct spat
65{
66 char_u *pat; /* the pattern (in allocated memory) or NULL */
67 int magic; /* magicness of the pattern */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020068 int no_scs; /* no smartcase for this pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 struct soffset off;
70};
71
72/*
73 * Two search patterns are remembered: One for the :substitute command and
74 * one for other searches. last_idx points to the one that was used the last
75 * time.
76 */
77static struct spat spats[2] =
78{
79 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
80 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
81};
82
83static int last_idx = 0; /* index in spats[] for RE_LAST */
84
Bram Moolenaardbd24b52015-08-11 14:26:19 +020085static char_u lastc[2] = {NUL, NUL}; /* last character searched for */
86static int lastcdir = FORWARD; /* last direction of character search */
87static int last_t_cmd = TRUE; /* last search t_cmd */
88#ifdef FEAT_MBYTE
89static char_u lastc_bytes[MB_MAXBYTES + 1];
90static int lastc_bytelen = 1; /* >1 for multi-byte char */
91#endif
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093/* copy of spats[], for keeping the search patterns while executing autocmds */
94static struct spat saved_spats[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000095# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +010096static int saved_spats_last_idx = 0;
97static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
100static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
101#ifdef FEAT_RIGHTLEFT
102static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
104
105#ifdef FEAT_FIND_ID
106/*
107 * Type used by find_pattern_in_path() to remember which included files have
108 * been searched already.
109 */
110typedef struct SearchedFile
111{
112 FILE *fp; /* File pointer */
113 char_u *name; /* Full name of file */
114 linenr_T lnum; /* Line we were up to in file */
115 int matched; /* Found a match in this file */
116} SearchedFile;
117#endif
118
119/*
120 * translate search pattern for vim_regcomp()
121 *
122 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
123 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
124 * pat_save == RE_BOTH: save pat in both patterns (:global command)
125 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000126 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
128 * options & SEARCH_HIS: put search string in history
129 * options & SEARCH_KEEP: keep previous search pattern
130 *
131 * returns FAIL if failed, OK otherwise.
132 */
133 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100134search_regcomp(
135 char_u *pat,
136 int pat_save,
137 int pat_use,
138 int options,
139 regmmatch_T *regmatch) /* return: pattern and ignore-case flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140{
141 int magic;
142 int i;
143
144 rc_did_emsg = FALSE;
145 magic = p_magic;
146
147 /*
148 * If no pattern given, use a previously defined pattern.
149 */
150 if (pat == NULL || *pat == NUL)
151 {
152 if (pat_use == RE_LAST)
153 i = last_idx;
154 else
155 i = pat_use;
156 if (spats[i].pat == NULL) /* pattern was never defined */
157 {
158 if (pat_use == RE_SUBST)
159 EMSG(_(e_nopresub));
160 else
161 EMSG(_(e_noprevre));
162 rc_did_emsg = TRUE;
163 return FAIL;
164 }
165 pat = spats[i].pat;
166 magic = spats[i].magic;
167 no_smartcase = spats[i].no_scs;
168 }
169#ifdef FEAT_CMDHIST
170 else if (options & SEARCH_HIS) /* put new pattern in history */
171 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
172#endif
173
174#ifdef FEAT_RIGHTLEFT
175 if (mr_pattern_alloced)
176 {
177 vim_free(mr_pattern);
178 mr_pattern_alloced = FALSE;
179 }
180
181 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
182 {
183 char_u *rev_pattern;
184
185 rev_pattern = reverse_text(pat);
186 if (rev_pattern == NULL)
187 mr_pattern = pat; /* out of memory, keep normal pattern. */
188 else
189 {
190 mr_pattern = rev_pattern;
191 mr_pattern_alloced = TRUE;
192 }
193 }
194 else
195#endif
196 mr_pattern = pat;
197
198 /*
199 * Save the currently used pattern in the appropriate place,
200 * unless the pattern should not be remembered.
201 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100202 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 {
204 /* search or global command */
205 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
206 save_re_pat(RE_SEARCH, pat, magic);
207 /* substitute or global command */
208 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
209 save_re_pat(RE_SUBST, pat, magic);
210 }
211
212 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000213 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
215 if (regmatch->regprog == NULL)
216 return FAIL;
217 return OK;
218}
219
220/*
221 * Get search pattern used by search_regcomp().
222 */
223 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100224get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225{
226 return mr_pattern;
227}
228
Bram Moolenaarabc97732007-08-08 20:49:37 +0000229#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230/*
231 * Reverse text into allocated memory.
232 * Returns the allocated string, NULL when out of memory.
233 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000234 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100235reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 unsigned len;
238 unsigned s_i, rev_i;
239 char_u *rev;
240
241 /*
242 * Reverse the pattern.
243 */
244 len = (unsigned)STRLEN(s);
245 rev = alloc(len + 1);
246 if (rev != NULL)
247 {
248 rev_i = len;
249 for (s_i = 0; s_i < len; ++s_i)
250 {
251# ifdef FEAT_MBYTE
252 if (has_mbyte)
253 {
254 int mb_len;
255
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000256 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 rev_i -= mb_len;
258 mch_memmove(rev + rev_i, s + s_i, mb_len);
259 s_i += mb_len - 1;
260 }
261 else
262# endif
263 rev[--rev_i] = s[s_i];
264
265 }
266 rev[len] = NUL;
267 }
268 return rev;
269}
270#endif
271
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100272 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100273save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274{
275 if (spats[idx].pat != pat)
276 {
277 vim_free(spats[idx].pat);
278 spats[idx].pat = vim_strsave(pat);
279 spats[idx].magic = magic;
280 spats[idx].no_scs = no_smartcase;
281 last_idx = idx;
282#ifdef FEAT_SEARCH_EXTRA
283 /* If 'hlsearch' set and search pat changed: need redraw. */
284 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000285 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200286 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287#endif
288 }
289}
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291/*
292 * Save the search patterns, so they can be restored later.
293 * Used before/after executing autocommands and user functions.
294 */
295static int save_level = 0;
296
297 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100298save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299{
300 if (save_level++ == 0)
301 {
302 saved_spats[0] = spats[0];
303 if (spats[0].pat != NULL)
304 saved_spats[0].pat = vim_strsave(spats[0].pat);
305 saved_spats[1] = spats[1];
306 if (spats[1].pat != NULL)
307 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100308#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100309 saved_spats_last_idx = last_idx;
310 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 }
313}
314
315 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100316restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317{
318 if (--save_level == 0)
319 {
320 vim_free(spats[0].pat);
321 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100322#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000323 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 vim_free(spats[1].pat);
326 spats[1] = saved_spats[1];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100327#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100328 last_idx = saved_spats_last_idx;
329 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 }
332}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000334#if defined(EXITFREE) || defined(PROTO)
335 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100336free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000337{
338 vim_free(spats[0].pat);
339 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000340
341# ifdef FEAT_RIGHTLEFT
342 if (mr_pattern_alloced)
343 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200344 vim_free(mr_pattern);
345 mr_pattern_alloced = FALSE;
346 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000347 }
348# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000349}
350#endif
351
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100352#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100353// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
354// searching
355static struct spat saved_last_search_spat;
356static int did_save_last_search_spat = 0;
357static int saved_last_idx = 0;
358static int saved_no_hlsearch = 0;
359
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100360/*
361 * Save and restore the search pattern for incremental highlight search
362 * feature.
363 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100364 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100365 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100366 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100367 */
368 void
369save_last_search_pattern(void)
370{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100371 if (did_save_last_search_spat != 0)
372 IEMSG("did_save_last_search_spat is not zero");
373 else
374 ++did_save_last_search_spat;
375
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100376 saved_last_search_spat = spats[RE_SEARCH];
377 if (spats[RE_SEARCH].pat != NULL)
378 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
379 saved_last_idx = last_idx;
380 saved_no_hlsearch = no_hlsearch;
381}
382
383 void
384restore_last_search_pattern(void)
385{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100386 if (did_save_last_search_spat != 1)
387 {
388 IEMSG("did_save_last_search_spat is not one");
389 return;
390 }
391 --did_save_last_search_spat;
392
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100393 vim_free(spats[RE_SEARCH].pat);
394 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100395 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100396# if defined(FEAT_EVAL)
397 set_vv_searchforward();
398# endif
399 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200400 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100401}
Bram Moolenaard0480092017-11-16 22:20:39 +0100402
403 char_u *
404last_search_pattern(void)
405{
406 return spats[RE_SEARCH].pat;
407}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100408#endif
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410/*
411 * Return TRUE when case should be ignored for search pattern "pat".
412 * Uses the 'ignorecase' and 'smartcase' options.
413 */
414 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100415ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200417 return ignorecase_opt(pat, p_ic, p_scs);
418}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200420/*
421 * As ignorecase() put pass the "ic" and "scs" flags.
422 */
423 int
424ignorecase_opt(char_u *pat, int ic_in, int scs)
425{
426 int ic = ic_in;
427
428 if (ic && !no_smartcase && scs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429#ifdef FEAT_INS_EXPAND
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100430 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#endif
432 )
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200433 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 no_smartcase = FALSE;
435
436 return ic;
437}
438
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200439/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200440 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200441 */
442 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100443pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200444{
445 char_u *p = pat;
446
447 while (*p != NUL)
448 {
449#ifdef FEAT_MBYTE
450 int l;
451
452 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
453 {
454 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
455 return TRUE;
456 p += l;
457 }
458 else
459#endif
460 if (*p == '\\')
461 {
462 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
463 p += 3;
464 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
465 p += 3;
466 else if (p[1] != NUL) /* skip "\X" */
467 p += 2;
468 else
469 p += 1;
470 }
471 else if (MB_ISUPPER(*p))
472 return TRUE;
473 else
474 ++p;
475 }
476 return FALSE;
477}
478
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100480last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200481{
482#ifdef FEAT_MBYTE
483 return lastc_bytes;
484#else
485 return lastc;
486#endif
487}
488
489 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100490last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200491{
492 return lastcdir == FORWARD;
493}
494
495 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100496last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200497{
498 return last_t_cmd == TRUE;
499}
500
501 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100502set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200503{
504 *lastc = c;
505#ifdef FEAT_MBYTE
506 lastc_bytelen = len;
507 if (len)
508 memcpy(lastc_bytes, s, len);
509 else
510 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
511#endif
512}
513
514 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100515set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200516{
517 lastcdir = cdir;
518}
519
520 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100521set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200522{
523 last_t_cmd = t_cmd;
524}
525
526 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100527last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
529 return spats[last_idx].pat;
530}
531
532/*
533 * Reset search direction to forward. For "gd" and "gD" commands.
534 */
535 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100536reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000539#if defined(FEAT_EVAL)
540 set_vv_searchforward();
541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542}
543
544#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
545/*
546 * Set the last search pattern. For ":let @/ =" and viminfo.
547 * Also set the saved search pattern, so that this works in an autocommand.
548 */
549 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100550set_last_search_pat(
551 char_u *s,
552 int idx,
553 int magic,
554 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 vim_free(spats[idx].pat);
557 /* An empty string means that nothing should be matched. */
558 if (*s == NUL)
559 spats[idx].pat = NULL;
560 else
561 spats[idx].pat = vim_strsave(s);
562 spats[idx].magic = magic;
563 spats[idx].no_scs = FALSE;
564 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000565#if defined(FEAT_EVAL)
566 set_vv_searchforward();
567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 spats[idx].off.line = FALSE;
569 spats[idx].off.end = FALSE;
570 spats[idx].off.off = 0;
571 if (setlast)
572 last_idx = idx;
573 if (save_level)
574 {
575 vim_free(saved_spats[idx].pat);
576 saved_spats[idx] = spats[0];
577 if (spats[idx].pat == NULL)
578 saved_spats[idx].pat = NULL;
579 else
580 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaared8bc782018-12-01 21:08:21 +0100581 saved_spats_last_idx = last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583# ifdef FEAT_SEARCH_EXTRA
584 /* If 'hlsearch' set and search pat changed: need redraw. */
585 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000586 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587# endif
588}
589#endif
590
591#ifdef FEAT_SEARCH_EXTRA
592/*
593 * Get a regexp program for the last used search pattern.
594 * This is used for highlighting all matches in a window.
595 * Values returned in regmatch->regprog and regmatch->rmm_ic.
596 */
597 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100598last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 if (spats[last_idx].pat == NULL)
601 {
602 regmatch->regprog = NULL;
603 return;
604 }
605 ++emsg_off; /* So it doesn't beep if bad expr */
606 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
607 --emsg_off;
608}
609#endif
610
611/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100612 * Lowest level search function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
614 * Start at position 'pos' and return the found position in 'pos'.
615 *
616 * if (options & SEARCH_MSG) == 0 don't give any messages
617 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
618 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
619 * if (options & SEARCH_HIS) put search pattern in history
620 * if (options & SEARCH_END) return position at end of match
621 * if (options & SEARCH_START) accept match at pos itself
622 * if (options & SEARCH_KEEP) keep previous search pattern
623 * if (options & SEARCH_FOLD) match only once in a closed fold
624 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100625 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 *
627 * Return FAIL (zero) for failure, non-zero for success.
628 * When FEAT_EVAL is defined, returns the index of the first matching
629 * subpattern plus one; one if there was none.
630 */
631 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100632searchit(
633 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100635 buf_T *buf,
636 pos_T *pos,
637 int dir,
638 char_u *pat,
639 long count,
640 int options,
641 int pat_use, /* which pattern to use when "pat" is empty */
642 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200643 proftime_T *tm UNUSED, /* timeout limit or NULL */
644 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 int found;
647 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100648 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 regmmatch_T regmatch;
650 char_u *ptr;
651 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000653 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 int loop;
655 pos_T start_pos;
656 int at_first_line;
657 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200658 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 int match_ok;
660 long nmatched;
661 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100662 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000663 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664#ifdef FEAT_SEARCH_EXTRA
665 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
667
668 if (search_regcomp(pat, RE_SEARCH, pat_use,
669 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
670 {
671 if ((options & SEARCH_MSG) && !rc_did_emsg)
672 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
673 return FAIL;
674 }
675
Bram Moolenaar280f1262006-01-30 00:14:18 +0000676 /*
677 * find the string
678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 called_emsg = FALSE;
680 do /* loop for count */
681 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100682 /* When not accepting a match at the start position set "extra_col" to
683 * a non-zero value. Don't do that when starting at MAXCOL, since
684 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200685 if (pos->col == MAXCOL)
686 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100687#ifdef FEAT_MBYTE
688 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200689 else if (has_mbyte
690 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
691 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100692 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100693 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100694 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200695 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100696 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100697 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100698 }
699#endif
700 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200701 start_char_len = 1;
702 if (dir == FORWARD)
703 {
704 if (options & SEARCH_START)
705 extra_col = 0;
706 else
707 extra_col = start_char_len;
708 }
709 else
710 {
711 if (options & SEARCH_START)
712 extra_col = start_char_len;
713 else
714 extra_col = 0;
715 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 start_pos = *pos; /* remember start pos for detecting no match */
718 found = 0; /* default: not found */
719 at_first_line = TRUE; /* default: start in first line */
720 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
721 {
722 pos->lnum = 1;
723 pos->col = 0;
724 at_first_line = FALSE; /* not in first line now */
725 }
726
727 /*
728 * Start searching in current line, unless searching backwards and
729 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000730 * If we are searching backwards, in column 0, and not including the
731 * current position, gain some efficiency by skipping back a line.
732 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000734 if (dir == BACKWARD && start_pos.col == 0
735 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {
737 lnum = pos->lnum - 1;
738 at_first_line = FALSE;
739 }
740 else
741 lnum = pos->lnum;
742
743 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
744 {
745 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
746 lnum += dir, at_first_line = FALSE)
747 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000748 /* Stop after checking "stop_lnum", if it's set. */
749 if (stop_lnum != 0 && (dir == FORWARD
750 ? lnum > stop_lnum : lnum < stop_lnum))
751 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000752#ifdef FEAT_RELTIME
753 /* Stop after passing the "tm" time limit. */
754 if (tm != NULL && profile_passed_limit(tm))
755 break;
756#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000759 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100761 col = at_first_line && (options & SEARCH_COL) ? pos->col
762 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100764 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000765#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200766 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000767#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200768 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000769#endif
770 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200772 if (called_emsg
773#ifdef FEAT_RELTIME
774 || (timed_out != NULL && *timed_out)
775#endif
776 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 break;
778 if (nmatched > 0)
779 {
780 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000781 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000783#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000785#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000786 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000787 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
788 ptr = (char_u *)"";
789 else
790 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 /*
793 * Forward search in the first line: match should be after
794 * the start position. If not, continue at the end of the
795 * match (this is vi compatible) or on the next char.
796 */
797 if (dir == FORWARD && at_first_line)
798 {
799 match_ok = TRUE;
800 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000801 * When the match starts in a next line it's certainly
802 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * When match lands on a NUL the cursor will be put
804 * one back afterwards, compare with that position,
805 * otherwise "/$" will get stuck on end of line.
806 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000807 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100808 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000809 ? (nmatched == 1
810 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000812 : ((int)matchpos.col
813 - (ptr[matchpos.col] == NUL)
814 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 /*
817 * If vi-compatible searching, continue at the end
818 * of the match, otherwise continue one position
819 * forward.
820 */
821 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
822 {
823 if (nmatched > 1)
824 {
825 /* end is in next line, thus no match in
826 * this line */
827 match_ok = FALSE;
828 break;
829 }
830 matchcol = endpos.col;
831 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000832 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 && ptr[matchcol] != NUL)
834 {
835#ifdef FEAT_MBYTE
836 if (has_mbyte)
837 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000838 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 else
840#endif
841 ++matchcol;
842 }
843 }
844 else
845 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000846 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (ptr[matchcol] != NUL)
848 {
849#ifdef FEAT_MBYTE
850 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000851 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 + matchcol);
853 else
854#endif
855 ++matchcol;
856 }
857 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200858 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (ptr[matchcol] == NUL
861 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000862 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000863 matchcol,
864#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200865 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000866#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200867 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000868#endif
869 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
871 match_ok = FALSE;
872 break;
873 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000874 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 endpos = regmatch.endpos[0];
876# ifdef FEAT_EVAL
877 submatch = first_submatch(&regmatch);
878# endif
879
880 /* Need to get the line pointer again, a
881 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000882 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884 if (!match_ok)
885 continue;
886 }
887 if (dir == BACKWARD)
888 {
889 /*
890 * Now, if there are multiple matches on this line,
891 * we have to get the last one. Or the last one before
892 * the cursor, if we're on that line.
893 * When putting the new cursor at the end, compare
894 * relative to the end of the match.
895 */
896 match_ok = FALSE;
897 for (;;)
898 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000899 /* Remember a position that is before the start
900 * position, we use it if it's the last match in
901 * the line. Always accept a position after
902 * wrapping around. */
903 if (loop
904 || ((options & SEARCH_END)
905 ? (lnum + regmatch.endpos[0].lnum
906 < start_pos.lnum
907 || (lnum + regmatch.endpos[0].lnum
908 == start_pos.lnum
909 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200910 < (int)start_pos.col
911 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000912 : (lnum + regmatch.startpos[0].lnum
913 < start_pos.lnum
914 || (lnum + regmatch.startpos[0].lnum
915 == start_pos.lnum
916 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200917 < (int)start_pos.col
918 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000921 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 endpos = regmatch.endpos[0];
923# ifdef FEAT_EVAL
924 submatch = first_submatch(&regmatch);
925# endif
926 }
927 else
928 break;
929
930 /*
931 * We found a valid match, now check if there is
932 * another one after it.
933 * If vi-compatible searching, continue at the end
934 * of the match, otherwise continue one position
935 * forward.
936 */
937 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
938 {
939 if (nmatched > 1)
940 break;
941 matchcol = endpos.col;
942 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000943 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 && ptr[matchcol] != NUL)
945 {
946#ifdef FEAT_MBYTE
947 if (has_mbyte)
948 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000949 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 else
951#endif
952 ++matchcol;
953 }
954 }
955 else
956 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000957 /* Stop when the match is in a next line. */
958 if (matchpos.lnum > 0)
959 break;
960 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (ptr[matchcol] != NUL)
962 {
963#ifdef FEAT_MBYTE
964 if (has_mbyte)
965 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000966 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 else
968#endif
969 ++matchcol;
970 }
971 }
972 if (ptr[matchcol] == NUL
973 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000974 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000975 matchcol,
976#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200977 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000978#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200979 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000980#endif
981 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100982 {
983#ifdef FEAT_RELTIME
984 /* If the search timed out, we did find a match
985 * but it might be the wrong one, so that's not
986 * OK. */
987 if (timed_out != NULL && *timed_out)
988 match_ok = FALSE;
989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
993 /* Need to get the line pointer again, a
994 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000995 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997
998 /*
999 * If there is only a match after the cursor, skip
1000 * this match.
1001 */
1002 if (!match_ok)
1003 continue;
1004 }
1005
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001006 /* With the SEARCH_END option move to the last character
1007 * of the match. Don't do it for an empty match, end
1008 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +02001009 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001010 && !(matchpos.lnum == endpos.lnum
1011 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001013 /* For a match in the first column, set the position
1014 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001015 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001016 pos->col = endpos.col;
1017 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001018 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001019 if (pos->lnum > 1) /* just in case */
1020 {
1021 --pos->lnum;
1022 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1023 pos->lnum, FALSE));
1024 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001025 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001026 else
1027 {
1028 --pos->col;
1029#ifdef FEAT_MBYTE
1030 if (has_mbyte
1031 && pos->lnum <= buf->b_ml.ml_line_count)
1032 {
1033 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1034 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1035 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001036#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039 else
1040 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001041 pos->lnum = lnum + matchpos.lnum;
1042 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
1044#ifdef FEAT_VIRTUALEDIT
1045 pos->coladd = 0;
1046#endif
1047 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001048 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001051 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 search_match_endcol = endpos.col;
1053 break;
1054 }
1055 line_breakcheck(); /* stop if ctrl-C typed */
1056 if (got_int)
1057 break;
1058
1059#ifdef FEAT_SEARCH_EXTRA
1060 /* Cancel searching if a character was typed. Used for
1061 * 'incsearch'. Don't check too often, that would slowdown
1062 * searching too much. */
1063 if ((options & SEARCH_PEEK)
1064 && ((lnum - pos->lnum) & 0x3f) == 0
1065 && char_avail())
1066 {
1067 break_loop = TRUE;
1068 break;
1069 }
1070#endif
1071
1072 if (loop && lnum == start_pos.lnum)
1073 break; /* if second loop, stop where started */
1074 }
1075 at_first_line = FALSE;
1076
1077 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001078 * Stop the search if wrapscan isn't set, "stop_lnum" is
1079 * specified, after an interrupt, after a match and after looping
1080 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001082 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001083#ifdef FEAT_RELTIME
1084 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001085#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001086#ifdef FEAT_SEARCH_EXTRA
1087 || break_loop
1088#endif
1089 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 break;
1091
1092 /*
1093 * If 'wrapscan' is set we continue at the other end of the file.
1094 * If 'shortmess' does not contain 's', we give a message.
1095 * This message is also remembered in keep_msg for when the screen
1096 * is redrawn. The keep_msg is cleared whenever another message is
1097 * written.
1098 */
1099 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001103 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1104 give_warning((char_u *)_(dir == BACKWARD
1105 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001107 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001108#ifdef FEAT_RELTIME
1109 || (timed_out != NULL && *timed_out)
1110#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001111#ifdef FEAT_SEARCH_EXTRA
1112 || break_loop
1113#endif
1114 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 break;
1116 }
1117 while (--count > 0 && found); /* stop after count matches or no match */
1118
Bram Moolenaar473de612013-06-08 18:19:48 +02001119 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120
Bram Moolenaar280f1262006-01-30 00:14:18 +00001121 called_emsg |= save_called_emsg;
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 if (!found) /* did not find it */
1124 {
1125 if (got_int)
1126 EMSG(_(e_interr));
1127 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1128 {
1129 if (p_ws)
1130 EMSG2(_(e_patnotf2), mr_pattern);
1131 else if (lnum == 0)
1132 EMSG2(_("E384: search hit TOP without match for: %s"),
1133 mr_pattern);
1134 else
1135 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
1136 mr_pattern);
1137 }
1138 return FAIL;
1139 }
1140
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001141 /* A pattern like "\n\zs" may go past the last line. */
1142 if (pos->lnum > buf->b_ml.ml_line_count)
1143 {
1144 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001145 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001146 if (pos->col > 0)
1147 --pos->col;
1148 }
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 return submatch + 1;
1151}
1152
1153#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001154 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001155set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001156{
1157 spats[0].off.dir = cdir;
1158}
1159
1160 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001161set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001162{
1163 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1164}
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166/*
1167 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001168 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
1170 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001171first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172{
1173 int submatch;
1174
1175 for (submatch = 1; ; ++submatch)
1176 {
1177 if (rp->startpos[submatch].lnum >= 0)
1178 break;
1179 if (submatch == 9)
1180 {
1181 submatch = 0;
1182 break;
1183 }
1184 }
1185 return submatch;
1186}
1187#endif
1188
1189/*
1190 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001191 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 * If 'dirc' is 0: use previous dir.
1193 * If 'pat' is NULL or empty : use previous string.
1194 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1195 * If 'options & SEARCH_ECHO': echo the search command and handle options
1196 * If 'options & SEARCH_MSG' : may give error message
1197 * If 'options & SEARCH_OPT' : interpret optional flags
1198 * If 'options & SEARCH_HIS' : put search pattern in history
1199 * If 'options & SEARCH_NOOF': don't add offset to position
1200 * If 'options & SEARCH_MARK': set previous context mark
1201 * If 'options & SEARCH_KEEP': keep previous search pattern
1202 * If 'options & SEARCH_START': accept match at curpos itself
1203 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1204 *
1205 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1206 * makes the movement linewise without moving the match position.
1207 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001208 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
1210 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001211do_search(
1212 oparg_T *oap, /* can be NULL */
1213 int dirc, /* '/' or '?' */
1214 char_u *pat,
1215 long count,
1216 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001217 proftime_T *tm, /* timeout limit or NULL */
1218 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219{
1220 pos_T pos; /* position of the last match */
1221 char_u *searchstr;
1222 struct soffset old_off;
1223 int retval; /* Return value */
1224 char_u *p;
1225 long c;
1226 char_u *dircp;
1227 char_u *strcopy = NULL;
1228 char_u *ps;
1229
1230 /*
1231 * A line offset is not remembered, this is vi compatible.
1232 */
1233 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1234 {
1235 spats[0].off.line = FALSE;
1236 spats[0].off.off = 0;
1237 }
1238
1239 /*
1240 * Save the values for when (options & SEARCH_KEEP) is used.
1241 * (there is no "if ()" around this because gcc wants them initialized)
1242 */
1243 old_off = spats[0].off;
1244
1245 pos = curwin->w_cursor; /* start searching at the cursor position */
1246
1247 /*
1248 * Find out the direction of the search.
1249 */
1250 if (dirc == 0)
1251 dirc = spats[0].off.dir;
1252 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001255#if defined(FEAT_EVAL)
1256 set_vv_searchforward();
1257#endif
1258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 if (options & SEARCH_REV)
1260 {
1261#ifdef WIN32
1262 /* There is a bug in the Visual C++ 2.2 compiler which means that
1263 * dirc always ends up being '/' */
1264 dirc = (dirc == '/') ? '?' : '/';
1265#else
1266 if (dirc == '/')
1267 dirc = '?';
1268 else
1269 dirc = '/';
1270#endif
1271 }
1272
1273#ifdef FEAT_FOLDING
1274 /* If the cursor is in a closed fold, don't find another match in the same
1275 * fold. */
1276 if (dirc == '/')
1277 {
1278 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1279 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1280 }
1281 else
1282 {
1283 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1284 pos.col = 0;
1285 }
1286#endif
1287
1288#ifdef FEAT_SEARCH_EXTRA
1289 /*
1290 * Turn 'hlsearch' highlighting back on.
1291 */
1292 if (no_hlsearch && !(options & SEARCH_KEEP))
1293 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001294 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001295 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 }
1297#endif
1298
1299 /*
1300 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1301 */
1302 for (;;)
1303 {
1304 searchstr = pat;
1305 dircp = NULL;
1306 /* use previous pattern */
1307 if (pat == NULL || *pat == NUL || *pat == dirc)
1308 {
1309 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1310 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001311 searchstr = spats[RE_SUBST].pat;
1312 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001313 {
1314 EMSG(_(e_noprevre));
1315 retval = 0;
1316 goto end_do_search;
1317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001319 else
1320 {
1321 /* make search_regcomp() use spats[RE_SEARCH].pat */
1322 searchstr = (char_u *)"";
1323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 }
1325
1326 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1327 {
1328 /*
1329 * Find end of regular expression.
1330 * If there is a matching '/' or '?', toss it.
1331 */
1332 ps = strcopy;
1333 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1334 if (strcopy != ps)
1335 {
1336 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001337 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 pat = strcopy;
1339 searchstr = strcopy;
1340 }
1341 if (*p == dirc)
1342 {
1343 dircp = p; /* remember where we put the NUL */
1344 *p++ = NUL;
1345 }
1346 spats[0].off.line = FALSE;
1347 spats[0].off.end = FALSE;
1348 spats[0].off.off = 0;
1349 /*
1350 * Check for a line offset or a character offset.
1351 * For get_address (echo off) we don't check for a character
1352 * offset, because it is meaningless and the 's' could be a
1353 * substitute command.
1354 */
1355 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1356 spats[0].off.line = TRUE;
1357 else if ((options & SEARCH_OPT) &&
1358 (*p == 'e' || *p == 's' || *p == 'b'))
1359 {
1360 if (*p == 'e') /* end */
1361 spats[0].off.end = SEARCH_END;
1362 ++p;
1363 }
1364 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1365 {
1366 /* 'nr' or '+nr' or '-nr' */
1367 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1368 spats[0].off.off = atol((char *)p);
1369 else if (*p == '-') /* single '-' */
1370 spats[0].off.off = -1;
1371 else /* single '+' */
1372 spats[0].off.off = 1;
1373 ++p;
1374 while (VIM_ISDIGIT(*p)) /* skip number */
1375 ++p;
1376 }
1377
1378 /* compute length of search command for get_address() */
1379 searchcmdlen += (int)(p - pat);
1380
1381 pat = p; /* put pat after search command */
1382 }
1383
1384 if ((options & SEARCH_ECHO) && messaging()
1385 && !cmd_silent && msg_silent == 0)
1386 {
1387 char_u *msgbuf;
1388 char_u *trunc;
1389
1390 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001391 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 else
1393 p = searchstr;
1394 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1395 if (msgbuf != NULL)
1396 {
1397 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001398#ifdef FEAT_MBYTE
1399 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1400 {
1401 /* Use a space to draw the composing char on. */
1402 msgbuf[1] = ' ';
1403 STRCPY(msgbuf + 2, p);
1404 }
1405 else
1406#endif
1407 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1409 {
1410 p = msgbuf + STRLEN(msgbuf);
1411 *p++ = dirc;
1412 if (spats[0].off.end)
1413 *p++ = 'e';
1414 else if (!spats[0].off.line)
1415 *p++ = 's';
1416 if (spats[0].off.off > 0 || spats[0].off.line)
1417 *p++ = '+';
1418 if (spats[0].off.off != 0 || spats[0].off.line)
1419 sprintf((char *)p, "%ld", spats[0].off.off);
1420 else
1421 *p = NUL;
1422 }
1423
1424 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001425 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
1427#ifdef FEAT_RIGHTLEFT
1428 /* The search pattern could be shown on the right in rightleft
1429 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1430 * it would be blanked out again very soon. Show it on the
1431 * left, but do reverse the text. */
1432 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1433 {
1434 char_u *r;
1435
1436 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1437 if (r != NULL)
1438 {
1439 vim_free(trunc);
1440 trunc = r;
1441 }
1442 }
1443#endif
1444 if (trunc != NULL)
1445 {
1446 msg_outtrans(trunc);
1447 vim_free(trunc);
1448 }
1449 else
1450 msg_outtrans(msgbuf);
1451 msg_clr_eos();
1452 msg_check();
1453 vim_free(msgbuf);
1454
1455 gotocmdline(FALSE);
1456 out_flush();
1457 msg_nowait = TRUE; /* don't wait for this message */
1458 }
1459 }
1460
1461 /*
1462 * If there is a character offset, subtract it from the current
1463 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001464 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 * This is not done for a line offset, because then we would not be vi
1466 * compatible.
1467 */
Bram Moolenaared203462004-06-16 11:19:22 +00001468 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 {
1470 if (spats[0].off.off > 0)
1471 {
1472 for (c = spats[0].off.off; c; --c)
1473 if (decl(&pos) == -1)
1474 break;
1475 if (c) /* at start of buffer */
1476 {
1477 pos.lnum = 0; /* allow lnum == 0 here */
1478 pos.col = MAXCOL;
1479 }
1480 }
1481 else
1482 {
1483 for (c = spats[0].off.off; c; ++c)
1484 if (incl(&pos) == -1)
1485 break;
1486 if (c) /* at end of buffer */
1487 {
1488 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1489 pos.col = 0;
1490 }
1491 }
1492 }
1493
1494#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1495 if (p_altkeymap && curwin->w_p_rl)
1496 lrFswap(searchstr,0);
1497#endif
1498
1499 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1500 searchstr, count, spats[0].off.end + (options &
1501 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1502 + SEARCH_MSG + SEARCH_START
1503 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001504 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
1506 if (dircp != NULL)
1507 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1508 if (c == FAIL)
1509 {
1510 retval = 0;
1511 goto end_do_search;
1512 }
1513 if (spats[0].off.end && oap != NULL)
1514 oap->inclusive = TRUE; /* 'e' includes last character */
1515
1516 retval = 1; /* pattern found */
1517
1518 /*
1519 * Add character and/or line offset
1520 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001521 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 if (spats[0].off.line) /* Add the offset to the line number. */
1524 {
1525 c = pos.lnum + spats[0].off.off;
1526 if (c < 1)
1527 pos.lnum = 1;
1528 else if (c > curbuf->b_ml.ml_line_count)
1529 pos.lnum = curbuf->b_ml.ml_line_count;
1530 else
1531 pos.lnum = c;
1532 pos.col = 0;
1533
1534 retval = 2; /* pattern found, line offset added */
1535 }
Bram Moolenaared203462004-06-16 11:19:22 +00001536 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001539 c = spats[0].off.off;
1540 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001542 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if (incl(&pos) == -1)
1544 break;
1545 }
1546 /* to the left, check for start of file */
1547 else
1548 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001549 while (c++ < 0)
1550 if (decl(&pos) == -1)
1551 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 }
1553 }
1554 }
1555
1556 /*
1557 * The search command can be followed by a ';' to do another search.
1558 * For example: "/pat/;/foo/+3;?bar"
1559 * This is like doing another search command, except:
1560 * - The remembered direction '/' or '?' is from the first search.
1561 * - When an error happens the cursor isn't moved at all.
1562 * Don't do this when called by get_address() (it handles ';' itself).
1563 */
1564 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1565 break;
1566
1567 dirc = *++pat;
1568 if (dirc != '?' && dirc != '/')
1569 {
1570 retval = 0;
1571 EMSG(_("E386: Expected '?' or '/' after ';'"));
1572 goto end_do_search;
1573 }
1574 ++pat;
1575 }
1576
1577 if (options & SEARCH_MARK)
1578 setpcmark();
1579 curwin->w_cursor = pos;
1580 curwin->w_set_curswant = TRUE;
1581
1582end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001583 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 spats[0].off = old_off;
1585 vim_free(strcopy);
1586
1587 return retval;
1588}
1589
1590#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1591/*
1592 * search_for_exact_line(buf, pos, dir, pat)
1593 *
1594 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001595 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001597 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 * Return OK for success, or FAIL if no line found.
1599 */
1600 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001601search_for_exact_line(
1602 buf_T *buf,
1603 pos_T *pos,
1604 int dir,
1605 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606{
1607 linenr_T start = 0;
1608 char_u *ptr;
1609 char_u *p;
1610
1611 if (buf->b_ml.ml_line_count == 0)
1612 return FAIL;
1613 for (;;)
1614 {
1615 pos->lnum += dir;
1616 if (pos->lnum < 1)
1617 {
1618 if (p_ws)
1619 {
1620 pos->lnum = buf->b_ml.ml_line_count;
1621 if (!shortmess(SHM_SEARCH))
1622 give_warning((char_u *)_(top_bot_msg), TRUE);
1623 }
1624 else
1625 {
1626 pos->lnum = 1;
1627 break;
1628 }
1629 }
1630 else if (pos->lnum > buf->b_ml.ml_line_count)
1631 {
1632 if (p_ws)
1633 {
1634 pos->lnum = 1;
1635 if (!shortmess(SHM_SEARCH))
1636 give_warning((char_u *)_(bot_top_msg), TRUE);
1637 }
1638 else
1639 {
1640 pos->lnum = 1;
1641 break;
1642 }
1643 }
1644 if (pos->lnum == start)
1645 break;
1646 if (start == 0)
1647 start = pos->lnum;
1648 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1649 p = skipwhite(ptr);
1650 pos->col = (colnr_T) (p - ptr);
1651
1652 /* when adding lines the matching line may be empty but it is not
1653 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001654 if ((compl_cont_status & CONT_ADDING)
1655 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
1657 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1658 return OK;
1659 }
1660 else if (*p != NUL) /* ignore empty lines */
1661 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001662 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1663 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 return OK;
1665 }
1666 }
1667 return FAIL;
1668}
1669#endif /* FEAT_INS_EXPAND */
1670
1671/*
1672 * Character Searches
1673 */
1674
1675/*
1676 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1677 * position of the character, otherwise move to just before the char.
1678 * Do this "cap->count1" times.
1679 * Return FAIL or OK.
1680 */
1681 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001682searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683{
1684 int c = cap->nchar; /* char to search for */
1685 int dir = cap->arg; /* TRUE for searching forward */
1686 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 int col;
1688 char_u *p;
1689 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001690 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 if (c != NUL) /* normal search: remember args for repeat */
1693 {
1694 if (!KeyStuffed) /* don't remember when redoing */
1695 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001696 *lastc = c;
1697 set_csearch_direction(dir);
1698 set_csearch_until(t_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001700 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (cap->ncharC1 != 0)
1702 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001703 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1704 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001706 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1707 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
1709#endif
1710 }
1711 }
1712 else /* repeat previous search */
1713 {
Bram Moolenaar454709b2017-03-12 16:37:14 +01001714 if (*lastc == NUL
1715#ifdef FEAT_MBYTE
1716 && lastc_bytelen == 1
1717#endif
1718 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return FAIL;
1720 if (dir) /* repeat in opposite direction */
1721 dir = -lastcdir;
1722 else
1723 dir = lastcdir;
1724 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001725 c = *lastc;
1726 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001727
1728 /* Force a move of at least one char, so ";" and "," will move the
1729 * cursor, even if the cursor is right in front of char we are looking
1730 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001731 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001732 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 }
1734
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001735 if (dir == BACKWARD)
1736 cap->oap->inclusive = FALSE;
1737 else
1738 cap->oap->inclusive = TRUE;
1739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 p = ml_get_curline();
1741 col = curwin->w_cursor.col;
1742 len = (int)STRLEN(p);
1743
1744 while (count--)
1745 {
1746#ifdef FEAT_MBYTE
1747 if (has_mbyte)
1748 {
1749 for (;;)
1750 {
1751 if (dir > 0)
1752 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001753 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if (col >= len)
1755 return FAIL;
1756 }
1757 else
1758 {
1759 if (col == 0)
1760 return FAIL;
1761 col -= (*mb_head_off)(p, p + col - 1) + 1;
1762 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001763 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001765 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 break;
1767 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001768 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001769 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001770 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001771 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 }
1773 }
1774 else
1775#endif
1776 {
1777 for (;;)
1778 {
1779 if ((col += dir) < 0 || col >= len)
1780 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001781 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001783 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785 }
1786 }
1787
1788 if (t_cmd)
1789 {
1790 /* backup to before the character (possibly double-byte) */
1791 col -= dir;
1792#ifdef FEAT_MBYTE
1793 if (has_mbyte)
1794 {
1795 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001796 /* Landed on the search char which is lastc_bytelen long */
1797 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 else
1799 /* To previous char, which may be multi-byte. */
1800 col -= (*mb_head_off)(p, p + col);
1801 }
1802#endif
1803 }
1804 curwin->w_cursor.col = col;
1805
1806 return OK;
1807}
1808
1809/*
1810 * "Other" Searches
1811 */
1812
1813/*
1814 * findmatch - find the matching paren or brace
1815 *
1816 * Improvement over vi: Braces inside quotes are ignored.
1817 */
1818 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001819findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
1821 return findmatchlimit(oap, initc, 0, 0);
1822}
1823
1824/*
1825 * Return TRUE if the character before "linep[col]" equals "ch".
1826 * Return FALSE if "col" is zero.
1827 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1828 * is NULL.
1829 * Handles multibyte string correctly.
1830 */
1831 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001832check_prevcol(
1833 char_u *linep,
1834 int col,
1835 int ch,
1836 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 --col;
1839#ifdef FEAT_MBYTE
1840 if (col > 0 && has_mbyte)
1841 col -= (*mb_head_off)(linep, linep + col);
1842#endif
1843 if (prevcol)
1844 *prevcol = col;
1845 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1846}
1847
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001848/*
1849 * Raw string start is found at linep[startpos.col - 1].
1850 * Return TRUE if the matching end can be found between startpos and endpos.
1851 */
1852 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001853find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001854{
1855 char_u *p;
1856 char_u *delim_copy;
1857 size_t delim_len;
1858 linenr_T lnum;
1859 int found = FALSE;
1860
1861 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1862 ;
1863 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001864 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001865 if (delim_copy == NULL)
1866 return FALSE;
1867 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1868 {
1869 char_u *line = ml_get(lnum);
1870
1871 for (p = line + (lnum == startpos->lnum
1872 ? startpos->col + 1 : 0); *p; ++p)
1873 {
1874 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1875 break;
1876 if (*p == ')' && p[delim_len + 1] == '"'
1877 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1878 {
1879 found = TRUE;
1880 break;
1881 }
1882 }
1883 if (found)
1884 break;
1885 }
1886 vim_free(delim_copy);
1887 return found;
1888}
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890/*
1891 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001892 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1893 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 *
1895 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001896 * character at or after the cursor. Special values:
1897 * '*' look for C-style comment / *
1898 * '/' look for C-style comment / *, ignoring comment-end
1899 * '#' look for preprocessor directives
1900 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 *
1902 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1903 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1904 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1905 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001906 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001907 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001908 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 */
1910
1911 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001912findmatchlimit(
1913 oparg_T *oap,
1914 int initc,
1915 int flags,
1916 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917{
1918 static pos_T pos; /* current search position */
1919 int findc = 0; /* matching brace */
1920 int c;
1921 int count = 0; /* cumulative number of braces */
1922 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001923 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 int inquote = FALSE; /* TRUE when inside quotes */
1925 char_u *linep; /* pointer to current line */
1926 char_u *ptr;
1927 int do_quotes; /* check for quotes in current line */
1928 int at_start; /* do_quotes value at start position */
1929 int hash_dir = 0; /* Direction searched for # things */
1930 int comment_dir = 0; /* Direction searched for comments */
1931 pos_T match_pos; /* Where last slash-star was found */
1932 int start_in_quotes; /* start position is in quotes */
1933 int traveled = 0; /* how far we've searched so far */
1934 int ignore_cend = FALSE; /* ignore comment end */
1935 int cpo_match; /* vi compatible matching */
1936 int cpo_bsl; /* don't recognize backslashes */
1937 int match_escaped = 0; /* search for escaped match */
1938 int dir; /* Direction to search */
1939 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001940#ifdef FEAT_LISP
1941 int lispcomm = FALSE; /* inside of Lisp-style comment */
1942 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001946#ifdef FEAT_VIRTUALEDIT
1947 pos.coladd = 0;
1948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 linep = ml_get(pos.lnum);
1950
1951 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1952 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1953
1954 /* Direction to search when initc is '/', '*' or '#' */
1955 if (flags & FM_BACKWARD)
1956 dir = BACKWARD;
1957 else if (flags & FM_FORWARD)
1958 dir = FORWARD;
1959 else
1960 dir = 0;
1961
1962 /*
1963 * if initc given, look in the table for the matching character
1964 * '/' and '*' are special cases: look for start or end of comment.
1965 * When '/' is used, we ignore running backwards into an star-slash, for
1966 * "[*" command, we just want to find any comment.
1967 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001968 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
1970 comment_dir = dir;
1971 if (initc == '/')
1972 ignore_cend = TRUE;
1973 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001974 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 initc = NUL;
1976 }
1977 else if (initc != '#' && initc != NUL)
1978 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001979 find_mps_values(&initc, &findc, &backwards, TRUE);
1980 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 return NULL;
1982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 else
1984 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001985 /*
1986 * Either initc is '#', or no initc was given and we need to look
1987 * under the cursor.
1988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (initc == '#')
1990 {
1991 hash_dir = dir;
1992 }
1993 else
1994 {
1995 /*
1996 * initc was not given, must look for something to match under
1997 * or near the cursor.
1998 * Only check for special things when 'cpo' doesn't have '%'.
1999 */
2000 if (!cpo_match)
2001 {
2002 /* Are we before or at #if, #else etc.? */
2003 ptr = skipwhite(linep);
2004 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2005 {
2006 ptr = skipwhite(ptr + 1);
2007 if ( STRNCMP(ptr, "if", 2) == 0
2008 || STRNCMP(ptr, "endif", 5) == 0
2009 || STRNCMP(ptr, "el", 2) == 0)
2010 hash_dir = 1;
2011 }
2012
2013 /* Are we on a comment? */
2014 else if (linep[pos.col] == '/')
2015 {
2016 if (linep[pos.col + 1] == '*')
2017 {
2018 comment_dir = FORWARD;
2019 backwards = FALSE;
2020 pos.col++;
2021 }
2022 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2023 {
2024 comment_dir = BACKWARD;
2025 backwards = TRUE;
2026 pos.col--;
2027 }
2028 }
2029 else if (linep[pos.col] == '*')
2030 {
2031 if (linep[pos.col + 1] == '/')
2032 {
2033 comment_dir = BACKWARD;
2034 backwards = TRUE;
2035 }
2036 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2037 {
2038 comment_dir = FORWARD;
2039 backwards = FALSE;
2040 }
2041 }
2042 }
2043
2044 /*
2045 * If we are not on a comment or the # at the start of a line, then
2046 * look for brace anywhere on this line after the cursor.
2047 */
2048 if (!hash_dir && !comment_dir)
2049 {
2050 /*
2051 * Find the brace under or after the cursor.
2052 * If beyond the end of the line, use the last character in
2053 * the line.
2054 */
2055 if (linep[pos.col] == NUL && pos.col)
2056 --pos.col;
2057 for (;;)
2058 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002059 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 if (initc == NUL)
2061 break;
2062
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002063 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 if (findc)
2065 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002066 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068 if (!findc)
2069 {
2070 /* no brace in the line, maybe use " #if" then */
2071 if (!cpo_match && *skipwhite(linep) == '#')
2072 hash_dir = 1;
2073 else
2074 return NULL;
2075 }
2076 else if (!cpo_bsl)
2077 {
2078 int col, bslcnt = 0;
2079
2080 /* Set "match_escaped" if there are an odd number of
2081 * backslashes. */
2082 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2083 bslcnt++;
2084 match_escaped = (bslcnt & 1);
2085 }
2086 }
2087 }
2088 if (hash_dir)
2089 {
2090 /*
2091 * Look for matching #if, #else, #elif, or #endif
2092 */
2093 if (oap != NULL)
2094 oap->motion_type = MLINE; /* Linewise for this case only */
2095 if (initc != '#')
2096 {
2097 ptr = skipwhite(skipwhite(linep) + 1);
2098 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2099 hash_dir = 1;
2100 else if (STRNCMP(ptr, "endif", 5) == 0)
2101 hash_dir = -1;
2102 else
2103 return NULL;
2104 }
2105 pos.col = 0;
2106 while (!got_int)
2107 {
2108 if (hash_dir > 0)
2109 {
2110 if (pos.lnum == curbuf->b_ml.ml_line_count)
2111 break;
2112 }
2113 else if (pos.lnum == 1)
2114 break;
2115 pos.lnum += hash_dir;
2116 linep = ml_get(pos.lnum);
2117 line_breakcheck(); /* check for CTRL-C typed */
2118 ptr = skipwhite(linep);
2119 if (*ptr != '#')
2120 continue;
2121 pos.col = (colnr_T) (ptr - linep);
2122 ptr = skipwhite(ptr + 1);
2123 if (hash_dir > 0)
2124 {
2125 if (STRNCMP(ptr, "if", 2) == 0)
2126 count++;
2127 else if (STRNCMP(ptr, "el", 2) == 0)
2128 {
2129 if (count == 0)
2130 return &pos;
2131 }
2132 else if (STRNCMP(ptr, "endif", 5) == 0)
2133 {
2134 if (count == 0)
2135 return &pos;
2136 count--;
2137 }
2138 }
2139 else
2140 {
2141 if (STRNCMP(ptr, "if", 2) == 0)
2142 {
2143 if (count == 0)
2144 return &pos;
2145 count--;
2146 }
2147 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2148 {
2149 if (count == 0)
2150 return &pos;
2151 }
2152 else if (STRNCMP(ptr, "endif", 5) == 0)
2153 count++;
2154 }
2155 }
2156 return NULL;
2157 }
2158 }
2159
2160#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002161 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 * paren/brace in the other direction. */
2163 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2164 backwards = !backwards;
2165#endif
2166
2167 do_quotes = -1;
2168 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002169 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002172 if ((backwards && comment_dir)
2173#ifdef FEAT_LISP
2174 || lisp
2175#endif
2176 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002178#ifdef FEAT_LISP
2179 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2180 lispcomm = TRUE; /* find match inside this comment */
2181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 while (!got_int)
2183 {
2184 /*
2185 * Go to the next position, forward or backward. We could use
2186 * inc() and dec() here, but that is much slower
2187 */
2188 if (backwards)
2189 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002190#ifdef FEAT_LISP
2191 /* char to match is inside of comment, don't search outside */
2192 if (lispcomm && pos.col < (colnr_T)comment_col)
2193 break;
2194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 if (pos.col == 0) /* at start of line, go to prev. one */
2196 {
2197 if (pos.lnum == 1) /* start of file */
2198 break;
2199 --pos.lnum;
2200
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002201 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 break;
2203
2204 linep = ml_get(pos.lnum);
2205 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2206 do_quotes = -1;
2207 line_breakcheck();
2208
2209 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002210 if (comment_dir
2211#ifdef FEAT_LISP
2212 || lisp
2213#endif
2214 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002216#ifdef FEAT_LISP
2217 /* skip comment */
2218 if (lisp && comment_col != MAXCOL)
2219 pos.col = comment_col;
2220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
2222 else
2223 {
2224 --pos.col;
2225#ifdef FEAT_MBYTE
2226 if (has_mbyte)
2227 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2228#endif
2229 }
2230 }
2231 else /* forward search */
2232 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002233 if (linep[pos.col] == NUL
2234 /* at end of line, go to next one */
2235#ifdef FEAT_LISP
2236 /* don't search for match in comment */
2237 || (lisp && comment_col != MAXCOL
2238 && pos.col == (colnr_T)comment_col)
2239#endif
2240 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002242 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2243#ifdef FEAT_LISP
2244 /* line is exhausted and comment with it,
2245 * don't search for match in code */
2246 || lispcomm
2247#endif
2248 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 break;
2250 ++pos.lnum;
2251
2252 if (maxtravel && traveled++ > maxtravel)
2253 break;
2254
2255 linep = ml_get(pos.lnum);
2256 pos.col = 0;
2257 do_quotes = -1;
2258 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002259#ifdef FEAT_LISP
2260 if (lisp) /* find comment pos in new line */
2261 comment_col = check_linecomment(linep);
2262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 else
2265 {
2266#ifdef FEAT_MBYTE
2267 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002268 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 else
2270#endif
2271 ++pos.col;
2272 }
2273 }
2274
2275 /*
2276 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2277 */
2278 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2279 (linep[0] == '{' || linep[0] == '}'))
2280 {
2281 if (linep[0] == findc && count == 0) /* match! */
2282 return &pos;
2283 break; /* out of scope */
2284 }
2285
2286 if (comment_dir)
2287 {
2288 /* Note: comments do not nest, and we ignore quotes in them */
2289 /* TODO: ignore comment brackets inside strings */
2290 if (comment_dir == FORWARD)
2291 {
2292 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2293 {
2294 pos.col++;
2295 return &pos;
2296 }
2297 }
2298 else /* Searching backwards */
2299 {
2300 /*
2301 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002302 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 */
2304 if (pos.col == 0)
2305 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002306 else if (raw_string)
2307 {
2308 if (linep[pos.col - 1] == 'R'
2309 && linep[pos.col] == '"'
2310 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2311 {
2312 /* Possible start of raw string. Now that we have the
2313 * delimiter we can check if it ends before where we
2314 * started searching, or before the previously found
2315 * raw string start. */
2316 if (!find_rawstring_end(linep, &pos,
2317 count > 0 ? &match_pos : &curwin->w_cursor))
2318 {
2319 count++;
2320 match_pos = pos;
2321 match_pos.col--;
2322 }
2323 linep = ml_get(pos.lnum); /* may have been released */
2324 }
2325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 else if ( linep[pos.col - 1] == '/'
2327 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002328 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 && (int)pos.col < comment_col)
2330 {
2331 count++;
2332 match_pos = pos;
2333 match_pos.col--;
2334 }
2335 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2336 {
2337 if (count > 0)
2338 pos = match_pos;
2339 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2340 && (int)pos.col <= comment_col)
2341 pos.col -= 2;
2342 else if (ignore_cend)
2343 continue;
2344 else
2345 return NULL;
2346 return &pos;
2347 }
2348 }
2349 continue;
2350 }
2351
2352 /*
2353 * If smart matching ('cpoptions' does not contain '%'), braces inside
2354 * of quotes are ignored, but only if there is an even number of
2355 * quotes in the line.
2356 */
2357 if (cpo_match)
2358 do_quotes = 0;
2359 else if (do_quotes == -1)
2360 {
2361 /*
2362 * Count the number of quotes in the line, skipping \" and '"'.
2363 * Watch out for "\\".
2364 */
2365 at_start = do_quotes;
2366 for (ptr = linep; *ptr; ++ptr)
2367 {
2368 if (ptr == linep + pos.col + backwards)
2369 at_start = (do_quotes & 1);
2370 if (*ptr == '"'
2371 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2372 ++do_quotes;
2373 if (*ptr == '\\' && ptr[1] != NUL)
2374 ++ptr;
2375 }
2376 do_quotes &= 1; /* result is 1 with even number of quotes */
2377
2378 /*
2379 * If we find an uneven count, check current line and previous
2380 * one for a '\' at the end.
2381 */
2382 if (!do_quotes)
2383 {
2384 inquote = FALSE;
2385 if (ptr[-1] == '\\')
2386 {
2387 do_quotes = 1;
2388 if (start_in_quotes == MAYBE)
2389 {
2390 /* Do we need to use at_start here? */
2391 inquote = TRUE;
2392 start_in_quotes = TRUE;
2393 }
2394 else if (backwards)
2395 inquote = TRUE;
2396 }
2397 if (pos.lnum > 1)
2398 {
2399 ptr = ml_get(pos.lnum - 1);
2400 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2401 {
2402 do_quotes = 1;
2403 if (start_in_quotes == MAYBE)
2404 {
2405 inquote = at_start;
2406 if (inquote)
2407 start_in_quotes = TRUE;
2408 }
2409 else if (!backwards)
2410 inquote = TRUE;
2411 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002412
2413 /* ml_get() only keeps one line, need to get linep again */
2414 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 }
2417 }
2418 if (start_in_quotes == MAYBE)
2419 start_in_quotes = FALSE;
2420
2421 /*
2422 * If 'smartmatch' is set:
2423 * Things inside quotes are ignored by setting 'inquote'. If we
2424 * find a quote without a preceding '\' invert 'inquote'. At the
2425 * end of a line not ending in '\' we reset 'inquote'.
2426 *
2427 * In lines with an uneven number of quotes (without preceding '\')
2428 * we do not know which part to ignore. Therefore we only set
2429 * inquote if the number of quotes in a line is even, unless this
2430 * line or the previous one ends in a '\'. Complicated, isn't it?
2431 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002432 c = PTR2CHAR(linep + pos.col);
2433 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 case NUL:
2436 /* at end of line without trailing backslash, reset inquote */
2437 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2438 {
2439 inquote = FALSE;
2440 start_in_quotes = FALSE;
2441 }
2442 break;
2443
2444 case '"':
2445 /* a quote that is preceded with an odd number of backslashes is
2446 * ignored */
2447 if (do_quotes)
2448 {
2449 int col;
2450
2451 for (col = pos.col - 1; col >= 0; --col)
2452 if (linep[col] != '\\')
2453 break;
2454 if ((((int)pos.col - 1 - col) & 1) == 0)
2455 {
2456 inquote = !inquote;
2457 start_in_quotes = FALSE;
2458 }
2459 }
2460 break;
2461
2462 /*
2463 * If smart matching ('cpoptions' does not contain '%'):
2464 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2465 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2466 * skipped, there is never a brace in them.
2467 * Ignore this when finding matches for `'.
2468 */
2469 case '\'':
2470 if (!cpo_match && initc != '\'' && findc != '\'')
2471 {
2472 if (backwards)
2473 {
2474 if (pos.col > 1)
2475 {
2476 if (linep[pos.col - 2] == '\'')
2477 {
2478 pos.col -= 2;
2479 break;
2480 }
2481 else if (linep[pos.col - 2] == '\\' &&
2482 pos.col > 2 && linep[pos.col - 3] == '\'')
2483 {
2484 pos.col -= 3;
2485 break;
2486 }
2487 }
2488 }
2489 else if (linep[pos.col + 1]) /* forward search */
2490 {
2491 if (linep[pos.col + 1] == '\\' &&
2492 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2493 {
2494 pos.col += 3;
2495 break;
2496 }
2497 else if (linep[pos.col + 2] == '\'')
2498 {
2499 pos.col += 2;
2500 break;
2501 }
2502 }
2503 }
2504 /* FALLTHROUGH */
2505
2506 default:
2507#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002508 /*
2509 * For Lisp skip over backslashed (), {} and [].
2510 * (actually, we skip #\( et al)
2511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (curbuf->b_p_lisp
2513 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002514 && pos.col > 1
2515 && check_prevcol(linep, pos.col, '\\', NULL)
2516 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 break;
2518#endif
2519
2520 /* Check for match outside of quotes, and inside of
2521 * quotes when the start is also inside of quotes. */
2522 if ((!inquote || start_in_quotes == TRUE)
2523 && (c == initc || c == findc))
2524 {
2525 int col, bslcnt = 0;
2526
2527 if (!cpo_bsl)
2528 {
2529 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2530 bslcnt++;
2531 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002532 /* Only accept a match when 'M' is in 'cpo' or when escaping
2533 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2535 {
2536 if (c == initc)
2537 count++;
2538 else
2539 {
2540 if (count == 0)
2541 return &pos;
2542 count--;
2543 }
2544 }
2545 }
2546 }
2547 }
2548
2549 if (comment_dir == BACKWARD && count > 0)
2550 {
2551 pos = match_pos;
2552 return &pos;
2553 }
2554 return (pos_T *)NULL; /* never found it */
2555}
2556
2557/*
2558 * Check if line[] contains a / / comment.
2559 * Return MAXCOL if not, otherwise return the column.
2560 * TODO: skip strings.
2561 */
2562 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002563check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 char_u *p;
2566
2567 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002568#ifdef FEAT_LISP
2569 /* skip Lispish one-line comments */
2570 if (curbuf->b_p_lisp)
2571 {
2572 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2573 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002574 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002575
2576 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002577 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002578 {
2579 if (*p == '"')
2580 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002581 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002582 {
2583 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002584 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002585 }
2586 else if (p == line || ((p - line) >= 2
2587 /* skip #\" form */
2588 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002589 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002590 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002591 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002592 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2593 break; /* found! */
2594 ++p;
2595 }
2596 }
2597 else
2598 p = NULL;
2599 }
2600 else
2601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 while ((p = vim_strchr(p, '/')) != NULL)
2603 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002604 /* accept a double /, unless it's preceded with * and followed by *,
2605 * because * / / * is an end and start of a C comment */
2606 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 break;
2608 ++p;
2609 }
2610
2611 if (p == NULL)
2612 return MAXCOL;
2613 return (int)(p - line);
2614}
2615
2616/*
2617 * Move cursor briefly to character matching the one under the cursor.
2618 * Used for Insert mode and "r" command.
2619 * Show the match only if it is visible on the screen.
2620 * If there isn't a match, then beep.
2621 */
2622 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002623showmatch(
2624 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 pos_T *lpos, save_cursor;
2627 pos_T mpos;
2628 colnr_T vcol;
2629 long save_so;
2630 long save_siso;
2631#ifdef CURSOR_SHAPE
2632 int save_state;
2633#endif
2634 colnr_T save_dollar_vcol;
2635 char_u *p;
2636
2637 /*
2638 * Only show match for chars in the 'matchpairs' option.
2639 */
2640 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002641 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
2643#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002644 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002645 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002646#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002647 p += MB_PTR2LEN(p) + 1;
2648 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649#ifdef FEAT_RIGHTLEFT
2650 && !(curwin->w_p_rl ^ p_ri)
2651#endif
2652 )
2653 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002654 p += MB_PTR2LEN(p);
2655 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 return;
2657 }
2658
2659 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002660 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002661 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 {
2663 if (!curwin->w_p_wrap)
2664 getvcol(curwin, lpos, NULL, &vcol, NULL);
2665 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002666 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 mpos = *lpos; /* save the pos, update_screen() may change it */
2669 save_cursor = curwin->w_cursor;
2670 save_so = p_so;
2671 save_siso = p_siso;
2672 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2673 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002674 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2675 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 ++curwin->w_virtcol; /* do display ')' just before "$" */
2677 update_screen(VALID); /* show the new char first */
2678
2679 save_dollar_vcol = dollar_vcol;
2680#ifdef CURSOR_SHAPE
2681 save_state = State;
2682 State = SHOWMATCH;
2683 ui_cursor_shape(); /* may show different cursor shape */
2684#endif
2685 curwin->w_cursor = mpos; /* move to matching char */
2686 p_so = 0; /* don't use 'scrolloff' here */
2687 p_siso = 0; /* don't use 'sidescrolloff' here */
2688 showruler(FALSE);
2689 setcursor();
2690 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002691 out_flush_cursor(TRUE, FALSE);
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2694 * which resets it if the matching position is in a previous line
2695 * and has a higher column number. */
2696 dollar_vcol = save_dollar_vcol;
2697
2698 /*
2699 * brief pause, unless 'm' is present in 'cpo' and a character is
2700 * available.
2701 */
2702 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2703 ui_delay(p_mat * 100L, TRUE);
2704 else if (!char_avail())
2705 ui_delay(p_mat * 100L, FALSE);
2706 curwin->w_cursor = save_cursor; /* restore cursor position */
2707 p_so = save_so;
2708 p_siso = save_siso;
2709#ifdef CURSOR_SHAPE
2710 State = save_state;
2711 ui_cursor_shape(); /* may show different cursor shape */
2712#endif
2713 }
2714 }
2715}
2716
2717/*
Bram Moolenaar85160712018-06-19 18:27:41 +02002718 * Find the start of the next sentence, searching in the direction specified
2719 * by the "dir" argument. The cursor is positioned on the start of the next
2720 * sentence when found. If the next sentence is found, return OK. Return FAIL
2721 * otherwise. See ":h sentence" for the precise definition of a "sentence"
2722 * text object.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
2724 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002725findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 pos_T pos, tpos;
2728 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002729 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 int startlnum;
2731 int noskip = FALSE; /* do not skip blanks */
2732 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002733 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734
2735 pos = curwin->w_cursor;
2736 if (dir == FORWARD)
2737 func = incl;
2738 else
2739 func = decl;
2740
2741 while (count--)
2742 {
2743 /*
2744 * if on an empty line, skip upto a non-empty line
2745 */
2746 if (gchar_pos(&pos) == NUL)
2747 {
2748 do
2749 if ((*func)(&pos) == -1)
2750 break;
2751 while (gchar_pos(&pos) == NUL);
2752 if (dir == FORWARD)
2753 goto found;
2754 }
2755 /*
2756 * if on the start of a paragraph or a section and searching forward,
2757 * go to the next line
2758 */
2759 else if (dir == FORWARD && pos.col == 0 &&
2760 startPS(pos.lnum, NUL, FALSE))
2761 {
2762 if (pos.lnum == curbuf->b_ml.ml_line_count)
2763 return FAIL;
2764 ++pos.lnum;
2765 goto found;
2766 }
2767 else if (dir == BACKWARD)
2768 decl(&pos);
2769
Bram Moolenaar85160712018-06-19 18:27:41 +02002770 // go back to the previous non-white non-punctuation character
Bram Moolenaardef9e822004-12-31 20:58:58 +00002771 found_dot = FALSE;
Bram Moolenaar85160712018-06-19 18:27:41 +02002772 while (c = gchar_pos(&pos), VIM_ISWHITE(c)
2773 || vim_strchr((char_u *)".!?)]\"'", c) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaar85160712018-06-19 18:27:41 +02002775 tpos = pos;
2776 if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 break;
Bram Moolenaar85160712018-06-19 18:27:41 +02002778
2779 if (found_dot)
2780 break;
2781 if (vim_strchr((char_u *) ".!?", c) != NULL)
2782 found_dot = TRUE;
2783
2784 if (vim_strchr((char_u *) ")]\"'", c) != NULL
2785 && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
2786 break;
2787
2788 decl(&pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790
2791 /* remember the line where the search started */
2792 startlnum = pos.lnum;
2793 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2794
2795 for (;;) /* find end of sentence */
2796 {
2797 c = gchar_pos(&pos);
2798 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2799 {
2800 if (dir == BACKWARD && pos.lnum != startlnum)
2801 ++pos.lnum;
2802 break;
2803 }
2804 if (c == '.' || c == '!' || c == '?')
2805 {
2806 tpos = pos;
2807 do
2808 if ((c = inc(&tpos)) == -1)
2809 break;
2810 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2811 != NULL);
2812 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2813 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2814 && gchar_pos(&tpos) == ' ')))
2815 {
2816 pos = tpos;
2817 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2818 inc(&pos);
2819 break;
2820 }
2821 }
2822 if ((*func)(&pos) == -1)
2823 {
2824 if (count)
2825 return FAIL;
2826 noskip = TRUE;
2827 break;
2828 }
2829 }
2830found:
2831 /* skip white space */
2832 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2833 if (incl(&pos) == -1)
2834 break;
2835 }
2836
2837 setpcmark();
2838 curwin->w_cursor = pos;
2839 return OK;
2840}
2841
2842/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002843 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002845 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * If 'what' is '{' or '}' we go to the next section.
2847 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002848 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
2850 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002851findpar(
2852 int *pincl, /* Return: TRUE if last char is to be included */
2853 int dir,
2854 long count,
2855 int what,
2856 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 linenr_T curr;
2859 int did_skip; /* TRUE after separating lines have been skipped */
2860 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002861 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#ifdef FEAT_FOLDING
2863 linenr_T fold_first; /* first line of a closed fold */
2864 linenr_T fold_last; /* last line of a closed fold */
2865 int fold_skipped; /* TRUE if a closed fold was skipped this
2866 iteration */
2867#endif
2868
2869 curr = curwin->w_cursor.lnum;
2870
2871 while (count--)
2872 {
2873 did_skip = FALSE;
2874 for (first = TRUE; ; first = FALSE)
2875 {
2876 if (*ml_get(curr) != NUL)
2877 did_skip = TRUE;
2878
2879#ifdef FEAT_FOLDING
2880 /* skip folded lines */
2881 fold_skipped = FALSE;
2882 if (first && hasFolding(curr, &fold_first, &fold_last))
2883 {
2884 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2885 fold_skipped = TRUE;
2886 }
2887#endif
2888
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002889 /* POSIX has its own ideas of what a paragraph boundary is and it
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002890 * doesn't match historical Vi: It also stops at a "{" in the
2891 * first column and at an empty line. */
2892 if (!first && did_skip && (startPS(curr, what, both)
2893 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 break;
2895
2896#ifdef FEAT_FOLDING
2897 if (fold_skipped)
2898 curr -= dir;
2899#endif
2900 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2901 {
2902 if (count)
2903 return FALSE;
2904 curr -= dir;
2905 break;
2906 }
2907 }
2908 }
2909 setpcmark();
2910 if (both && *ml_get(curr) == '}') /* include line with '}' */
2911 ++curr;
2912 curwin->w_cursor.lnum = curr;
2913 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2914 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002915 char_u *line = ml_get(curr);
2916
2917 /* Put the cursor on the last character in the last line and make the
2918 * motion inclusive. */
2919 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002922#ifdef FEAT_MBYTE
2923 curwin->w_cursor.col -=
2924 (*mb_head_off)(line, line + curwin->w_cursor.col);
2925#endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002926 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 }
2928 }
2929 else
2930 curwin->w_cursor.col = 0;
2931 return TRUE;
2932}
2933
2934/*
2935 * check if the string 's' is a nroff macro that is in option 'opt'
2936 */
2937 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002938inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 char_u *macro;
2941
2942 for (macro = opt; macro[0]; ++macro)
2943 {
2944 /* Accept two characters in the option being equal to two characters
2945 * in the line. A space in the option matches with a space in the
2946 * line or the line having ended. */
2947 if ( (macro[0] == s[0]
2948 || (macro[0] == ' '
2949 && (s[0] == NUL || s[0] == ' ')))
2950 && (macro[1] == s[1]
2951 || ((macro[1] == NUL || macro[1] == ' ')
2952 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2953 break;
2954 ++macro;
2955 if (macro[0] == NUL)
2956 break;
2957 }
2958 return (macro[0] != NUL);
2959}
2960
2961/*
2962 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2963 * If 'para' is '{' or '}' only check for sections.
2964 * If 'both' is TRUE also stop at '}'
2965 */
2966 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002967startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 char_u *s;
2970
2971 s = ml_get(lnum);
2972 if (*s == para || *s == '\f' || (both && *s == '}'))
2973 return TRUE;
2974 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2975 (!para && inmacro(p_para, s + 1))))
2976 return TRUE;
2977 return FALSE;
2978}
2979
2980/*
2981 * The following routines do the word searches performed by the 'w', 'W',
2982 * 'b', 'B', 'e', and 'E' commands.
2983 */
2984
2985/*
2986 * To perform these searches, characters are placed into one of three
2987 * classes, and transitions between classes determine word boundaries.
2988 *
2989 * The classes are:
2990 *
2991 * 0 - white space
2992 * 1 - punctuation
2993 * 2 or higher - keyword characters (letters, digits and underscore)
2994 */
2995
2996static int cls_bigword; /* TRUE for "W", "B" or "E" */
2997
2998/*
2999 * cls() - returns the class of character at curwin->w_cursor
3000 *
3001 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
3002 * from class 2 and higher are reported as class 1 since only white space
3003 * boundaries are of interest.
3004 */
3005 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003006cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
3008 int c;
3009
3010 c = gchar_cursor();
3011#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
3012 if (p_altkeymap && c == F_BLANK)
3013 return 0;
3014#endif
3015 if (c == ' ' || c == '\t' || c == NUL)
3016 return 0;
3017#ifdef FEAT_MBYTE
3018 if (enc_dbcs != 0 && c > 0xFF)
3019 {
3020 /* If cls_bigword, report multi-byte chars as class 1. */
3021 if (enc_dbcs == DBCS_KOR && cls_bigword)
3022 return 1;
3023
3024 /* process code leading/trailing bytes */
3025 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3026 }
3027 if (enc_utf8)
3028 {
3029 c = utf_class(c);
3030 if (c != 0 && cls_bigword)
3031 return 1;
3032 return c;
3033 }
3034#endif
3035
3036 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3037 if (cls_bigword)
3038 return 1;
3039
3040 if (vim_iswordc(c))
3041 return 2;
3042 return 1;
3043}
3044
3045
3046/*
3047 * fwd_word(count, type, eol) - move forward one word
3048 *
3049 * Returns FAIL if the cursor was already at the end of the file.
3050 * If eol is TRUE, last word stops at end of line (for operators).
3051 */
3052 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003053fwd_word(
3054 long count,
3055 int bigword, /* "W", "E" or "B" */
3056 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
3058 int sclass; /* starting class */
3059 int i;
3060 int last_line;
3061
3062#ifdef FEAT_VIRTUALEDIT
3063 curwin->w_cursor.coladd = 0;
3064#endif
3065 cls_bigword = bigword;
3066 while (--count >= 0)
3067 {
3068#ifdef FEAT_FOLDING
3069 /* When inside a range of folded lines, move to the last char of the
3070 * last line. */
3071 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3072 coladvance((colnr_T)MAXCOL);
3073#endif
3074 sclass = cls();
3075
3076 /*
3077 * We always move at least one character, unless on the last
3078 * character in the buffer.
3079 */
3080 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3081 i = inc_cursor();
3082 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3083 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003084 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 return OK;
3086
3087 /*
3088 * Go one char past end of current word (if any)
3089 */
3090 if (sclass != 0)
3091 while (cls() == sclass)
3092 {
3093 i = inc_cursor();
3094 if (i == -1 || (i >= 1 && eol && count == 0))
3095 return OK;
3096 }
3097
3098 /*
3099 * go to next non-white
3100 */
3101 while (cls() == 0)
3102 {
3103 /*
3104 * We'll stop if we land on a blank line
3105 */
3106 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3107 break;
3108
3109 i = inc_cursor();
3110 if (i == -1 || (i >= 1 && eol && count == 0))
3111 return OK;
3112 }
3113 }
3114 return OK;
3115}
3116
3117/*
3118 * bck_word() - move backward 'count' words
3119 *
3120 * If stop is TRUE and we are already on the start of a word, move one less.
3121 *
3122 * Returns FAIL if top of the file was reached.
3123 */
3124 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003125bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 int sclass; /* starting class */
3128
3129#ifdef FEAT_VIRTUALEDIT
3130 curwin->w_cursor.coladd = 0;
3131#endif
3132 cls_bigword = bigword;
3133 while (--count >= 0)
3134 {
3135#ifdef FEAT_FOLDING
3136 /* When inside a range of folded lines, move to the first char of the
3137 * first line. */
3138 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3139 curwin->w_cursor.col = 0;
3140#endif
3141 sclass = cls();
3142 if (dec_cursor() == -1) /* started at start of file */
3143 return FAIL;
3144
3145 if (!stop || sclass == cls() || sclass == 0)
3146 {
3147 /*
3148 * Skip white space before the word.
3149 * Stop on an empty line.
3150 */
3151 while (cls() == 0)
3152 {
3153 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003154 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 goto finished;
3156 if (dec_cursor() == -1) /* hit start of file, stop here */
3157 return OK;
3158 }
3159
3160 /*
3161 * Move backward to start of this word.
3162 */
3163 if (skip_chars(cls(), BACKWARD))
3164 return OK;
3165 }
3166
3167 inc_cursor(); /* overshot - forward one */
3168finished:
3169 stop = FALSE;
3170 }
3171 return OK;
3172}
3173
3174/*
3175 * end_word() - move to the end of the word
3176 *
3177 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3178 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3179 * motion crosses blank lines. When the real vi crosses a blank line in an
3180 * 'e' motion, the cursor is placed on the FIRST character of the next
3181 * non-blank line. The 'E' command, however, works correctly. Since this
3182 * appears to be a bug, I have not duplicated it here.
3183 *
3184 * Returns FAIL if end of the file was reached.
3185 *
3186 * If stop is TRUE and we are already on the end of a word, move one less.
3187 * If empty is TRUE stop on an empty line.
3188 */
3189 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003190end_word(
3191 long count,
3192 int bigword,
3193 int stop,
3194 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
3196 int sclass; /* starting class */
3197
3198#ifdef FEAT_VIRTUALEDIT
3199 curwin->w_cursor.coladd = 0;
3200#endif
3201 cls_bigword = bigword;
3202 while (--count >= 0)
3203 {
3204#ifdef FEAT_FOLDING
3205 /* When inside a range of folded lines, move to the last char of the
3206 * last line. */
3207 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3208 coladvance((colnr_T)MAXCOL);
3209#endif
3210 sclass = cls();
3211 if (inc_cursor() == -1)
3212 return FAIL;
3213
3214 /*
3215 * If we're in the middle of a word, we just have to move to the end
3216 * of it.
3217 */
3218 if (cls() == sclass && sclass != 0)
3219 {
3220 /*
3221 * Move forward to end of the current word
3222 */
3223 if (skip_chars(sclass, FORWARD))
3224 return FAIL;
3225 }
3226 else if (!stop || sclass == 0)
3227 {
3228 /*
3229 * We were at the end of a word. Go to the end of the next word.
3230 * First skip white space, if 'empty' is TRUE, stop at empty line.
3231 */
3232 while (cls() == 0)
3233 {
3234 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003235 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 goto finished;
3237 if (inc_cursor() == -1) /* hit end of file, stop here */
3238 return FAIL;
3239 }
3240
3241 /*
3242 * Move forward to the end of this word.
3243 */
3244 if (skip_chars(cls(), FORWARD))
3245 return FAIL;
3246 }
3247 dec_cursor(); /* overshot - one char backward */
3248finished:
3249 stop = FALSE; /* we move only one word less */
3250 }
3251 return OK;
3252}
3253
3254/*
3255 * Move back to the end of the word.
3256 *
3257 * Returns FAIL if start of the file was reached.
3258 */
3259 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003260bckend_word(
3261 long count,
3262 int bigword, /* TRUE for "B" */
3263 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
3265 int sclass; /* starting class */
3266 int i;
3267
3268#ifdef FEAT_VIRTUALEDIT
3269 curwin->w_cursor.coladd = 0;
3270#endif
3271 cls_bigword = bigword;
3272 while (--count >= 0)
3273 {
3274 sclass = cls();
3275 if ((i = dec_cursor()) == -1)
3276 return FAIL;
3277 if (eol && i == 1)
3278 return OK;
3279
3280 /*
3281 * Move backward to before the start of this word.
3282 */
3283 if (sclass != 0)
3284 {
3285 while (cls() == sclass)
3286 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3287 return OK;
3288 }
3289
3290 /*
3291 * Move backward to end of the previous word
3292 */
3293 while (cls() == 0)
3294 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003295 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 break;
3297 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3298 return OK;
3299 }
3300 }
3301 return OK;
3302}
3303
3304/*
3305 * Skip a row of characters of the same class.
3306 * Return TRUE when end-of-file reached, FALSE otherwise.
3307 */
3308 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003309skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 while (cls() == cclass)
3312 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3313 return TRUE;
3314 return FALSE;
3315}
3316
3317#ifdef FEAT_TEXTOBJ
3318/*
3319 * Go back to the start of the word or the start of white space
3320 */
3321 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003322back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 int sclass; /* starting class */
3325
3326 sclass = cls();
3327 for (;;)
3328 {
3329 if (curwin->w_cursor.col == 0) /* stop at start of line */
3330 break;
3331 dec_cursor();
3332 if (cls() != sclass) /* stop at start of word */
3333 {
3334 inc_cursor();
3335 break;
3336 }
3337 }
3338}
3339
3340 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003341find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 int c;
3344
3345 while (decl(posp) != -1)
3346 {
3347 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003348 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 incl(posp);
3351 break;
3352 }
3353 }
3354}
3355
3356/*
3357 * Skip count/2 sentences and count/2 separating white spaces.
3358 */
3359 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003360findsent_forward(
3361 long count,
3362 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
3364 while (count--)
3365 {
3366 findsent(FORWARD, 1L);
3367 if (at_start_sent)
3368 find_first_blank(&curwin->w_cursor);
3369 if (count == 0 || at_start_sent)
3370 decl(&curwin->w_cursor);
3371 at_start_sent = !at_start_sent;
3372 }
3373}
3374
3375/*
3376 * Find word under cursor, cursor at end.
3377 * Used while an operator is pending, and in Visual mode.
3378 */
3379 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003380current_word(
3381 oparg_T *oap,
3382 long count,
3383 int include, /* TRUE: include word and white space */
3384 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385{
3386 pos_T start_pos;
3387 pos_T pos;
3388 int inclusive = TRUE;
3389 int include_white = FALSE;
3390
3391 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003392 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003395 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 dec_cursor();
3397
3398 /*
3399 * When Visual mode is not active, or when the VIsual area is only one
3400 * character, select the word and/or white space under the cursor.
3401 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003402 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 /*
3405 * Go to start of current word or white space.
3406 */
3407 back_in_line();
3408 start_pos = curwin->w_cursor;
3409
3410 /*
3411 * If the start is on white space, and white space should be included
3412 * (" word"), or start is not on white space, and white space should
3413 * not be included ("word"), find end of word.
3414 */
3415 if ((cls() == 0) == include)
3416 {
3417 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3418 return FAIL;
3419 }
3420 else
3421 {
3422 /*
3423 * If the start is not on white space, and white space should be
3424 * included ("word "), or start is on white space and white
3425 * space should not be included (" "), find start of word.
3426 * If we end up in the first column of the next line (single char
3427 * word) back up to end of the line.
3428 */
3429 fwd_word(1L, bigword, TRUE);
3430 if (curwin->w_cursor.col == 0)
3431 decl(&curwin->w_cursor);
3432 else
3433 oneleft();
3434
3435 if (include)
3436 include_white = TRUE;
3437 }
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (VIsual_active)
3440 {
3441 /* should do something when inclusive == FALSE ! */
3442 VIsual = start_pos;
3443 redraw_curbuf_later(INVERTED); /* update the inversion */
3444 }
3445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 oap->start = start_pos;
3448 oap->motion_type = MCHAR;
3449 }
3450 --count;
3451 }
3452
3453 /*
3454 * When count is still > 0, extend with more objects.
3455 */
3456 while (count > 0)
3457 {
3458 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003459 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 /*
3462 * In Visual mode, with cursor at start: move cursor back.
3463 */
3464 if (decl(&curwin->w_cursor) == -1)
3465 return FAIL;
3466 if (include != (cls() != 0))
3467 {
3468 if (bck_word(1L, bigword, TRUE) == FAIL)
3469 return FAIL;
3470 }
3471 else
3472 {
3473 if (bckend_word(1L, bigword, TRUE) == FAIL)
3474 return FAIL;
3475 (void)incl(&curwin->w_cursor);
3476 }
3477 }
3478 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
3480 /*
3481 * Move cursor forward one word and/or white area.
3482 */
3483 if (incl(&curwin->w_cursor) == -1)
3484 return FAIL;
3485 if (include != (cls() == 0))
3486 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003487 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 return FAIL;
3489 /*
3490 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003491 * the first character on the line.
3492 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003494 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 inclusive = FALSE;
3496 }
3497 else
3498 {
3499 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3500 return FAIL;
3501 }
3502 }
3503 --count;
3504 }
3505
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003506 if (include_white && (cls() != 0
3507 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 /*
3510 * If we don't include white space at the end, move the start
3511 * to include some white space there. This makes "daw" work
3512 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003513 * word). Also when "2daw" deletes "word." at the end of the line
3514 * (cursor is at start of next line).
3515 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 */
3517 pos = curwin->w_cursor; /* save cursor position */
3518 curwin->w_cursor = start_pos;
3519 if (oneleft() == OK)
3520 {
3521 back_in_line();
3522 if (cls() == 0 && curwin->w_cursor.col > 0)
3523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 if (VIsual_active)
3525 VIsual = curwin->w_cursor;
3526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 oap->start = curwin->w_cursor;
3528 }
3529 }
3530 curwin->w_cursor = pos; /* put cursor back at end */
3531 }
3532
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (VIsual_active)
3534 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003535 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 inc_cursor();
3537 if (VIsual_mode == 'V')
3538 {
3539 VIsual_mode = 'v';
3540 redraw_cmdline = TRUE; /* show mode later */
3541 }
3542 }
3543 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 oap->inclusive = inclusive;
3545
3546 return OK;
3547}
3548
3549/*
3550 * Find sentence(s) under the cursor, cursor at end.
3551 * When Visual active, extend it by one or more sentences.
3552 */
3553 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003554current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555{
3556 pos_T start_pos;
3557 pos_T pos;
3558 int start_blank;
3559 int c;
3560 int at_start_sent;
3561 long ncount;
3562
3563 start_pos = curwin->w_cursor;
3564 pos = start_pos;
3565 findsent(FORWARD, 1L); /* Find start of next sentence. */
3566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003568 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003570 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003573 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 /*
3576 * Cursor at start of Visual area.
3577 * Find out where we are:
3578 * - in the white space before a sentence
3579 * - in a sentence or just after it
3580 * - at the start of a sentence
3581 */
3582 at_start_sent = TRUE;
3583 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003584 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003587 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 {
3589 at_start_sent = FALSE;
3590 break;
3591 }
3592 incl(&pos);
3593 }
3594 if (!at_start_sent)
3595 {
3596 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003597 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 at_start_sent = TRUE; /* exactly at start of sentence */
3599 else
3600 /* inside a sentence, go to its end (start of next) */
3601 findsent(FORWARD, 1L);
3602 }
3603 if (include) /* "as" gets twice as much as "is" */
3604 count *= 2;
3605 while (count--)
3606 {
3607 if (at_start_sent)
3608 find_first_blank(&curwin->w_cursor);
3609 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003610 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 findsent(BACKWARD, 1L);
3612 at_start_sent = !at_start_sent;
3613 }
3614 }
3615 else
3616 {
3617 /*
3618 * Cursor at end of Visual area.
3619 * Find out where we are:
3620 * - just before a sentence
3621 * - just before or in the white space before a sentence
3622 * - in a sentence
3623 */
3624 incl(&pos);
3625 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003626 /* not just before a sentence */
3627 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003630 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003633 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
3635 at_start_sent = TRUE;
3636 break;
3637 }
3638 incl(&pos);
3639 }
3640 if (at_start_sent) /* in the sentence */
3641 findsent(BACKWARD, 1L);
3642 else /* in/before white before a sentence */
3643 curwin->w_cursor = start_pos;
3644 }
3645
3646 if (include) /* "as" gets twice as much as "is" */
3647 count *= 2;
3648 findsent_forward(count, at_start_sent);
3649 if (*p_sel == 'e')
3650 ++curwin->w_cursor.col;
3651 }
3652 return OK;
3653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003656 * If the cursor started on a blank, check if it is just before the start
3657 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003659 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003661 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 start_blank = TRUE;
3664 find_first_blank(&start_pos); /* go back to first blank */
3665 }
3666 else
3667 {
3668 start_blank = FALSE;
3669 findsent(BACKWARD, 1L);
3670 start_pos = curwin->w_cursor;
3671 }
3672 if (include)
3673 ncount = count * 2;
3674 else
3675 {
3676 ncount = count;
3677 if (start_blank)
3678 --ncount;
3679 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003680 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 findsent_forward(ncount, TRUE);
3682 else
3683 decl(&curwin->w_cursor);
3684
3685 if (include)
3686 {
3687 /*
3688 * If the blank in front of the sentence is included, exclude the
3689 * blanks at the end of the sentence, go back to the first blank.
3690 * If there are no trailing blanks, try to include leading blanks.
3691 */
3692 if (start_blank)
3693 {
3694 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003695 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3696 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 decl(&curwin->w_cursor);
3698 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003699 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 find_first_blank(&start_pos);
3701 }
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 if (VIsual_active)
3704 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003705 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003706 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 goto extend;
3708 if (*p_sel == 'e')
3709 ++curwin->w_cursor.col;
3710 VIsual = start_pos;
3711 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003712 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 redraw_curbuf_later(INVERTED); /* update the inversion */
3714 }
3715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
3717 /* include a newline after the sentence, if there is one */
3718 if (incl(&curwin->w_cursor) == -1)
3719 oap->inclusive = TRUE;
3720 else
3721 oap->inclusive = FALSE;
3722 oap->start = start_pos;
3723 oap->motion_type = MCHAR;
3724 }
3725 return OK;
3726}
3727
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003728/*
3729 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003730 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003733current_block(
3734 oparg_T *oap,
3735 long count,
3736 int include, /* TRUE == include white space */
3737 int what, /* '(', '{', etc. */
3738 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739{
3740 pos_T old_pos;
3741 pos_T *pos = NULL;
3742 pos_T start_pos;
3743 pos_T *end_pos;
3744 pos_T old_start, old_end;
3745 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003746 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003749 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 old_start = old_end;
3751
3752 /*
3753 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3754 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003755 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
3757 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003758 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 while (inindent(1))
3760 if (inc_cursor() != 0)
3761 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003762 if (gchar_cursor() == what)
3763 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 ++curwin->w_cursor.col;
3765 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003766 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
3768 old_start = VIsual;
3769 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3770 }
3771 else
3772 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
3774 /*
3775 * Search backwards for unclosed '(', '{', etc..
3776 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003777 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3778 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 */
3780 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003781 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 while (count-- > 0)
3783 {
3784 if ((pos = findmatch(NULL, what)) == NULL)
3785 break;
3786 curwin->w_cursor = *pos;
3787 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3788 }
3789 p_cpo = save_cpo;
3790
3791 /*
3792 * Search for matching ')', '}', etc.
3793 * Put this position in curwin->w_cursor.
3794 */
3795 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3796 {
3797 curwin->w_cursor = old_pos;
3798 return FAIL;
3799 }
3800 curwin->w_cursor = *end_pos;
3801
3802 /*
3803 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003804 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3805 * indent. But only if the resulting area is not smaller than what we
3806 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 */
3808 while (!include)
3809 {
3810 incl(&start_pos);
3811 sol = (curwin->w_cursor.col == 0);
3812 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003813 while (inindent(1))
3814 {
3815 sol = TRUE;
3816 if (decl(&curwin->w_cursor) != 0)
3817 break;
3818 }
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 /*
3821 * In Visual mode, when the resulting area is not bigger than what we
3822 * started with, extend it to the next block, and then exclude again.
3823 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003824 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 && VIsual_active)
3826 {
3827 curwin->w_cursor = old_start;
3828 decl(&curwin->w_cursor);
3829 if ((pos = findmatch(NULL, what)) == NULL)
3830 {
3831 curwin->w_cursor = old_pos;
3832 return FAIL;
3833 }
3834 start_pos = *pos;
3835 curwin->w_cursor = *pos;
3836 if ((end_pos = findmatch(NULL, other)) == NULL)
3837 {
3838 curwin->w_cursor = old_pos;
3839 return FAIL;
3840 }
3841 curwin->w_cursor = *end_pos;
3842 }
3843 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 break;
3845 }
3846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (VIsual_active)
3848 {
3849 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003850 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003851 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 inc(&curwin->w_cursor); /* include the line break */
3853 VIsual = start_pos;
3854 VIsual_mode = 'v';
3855 redraw_curbuf_later(INVERTED); /* update the inversion */
3856 showmode();
3857 }
3858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
3860 oap->start = start_pos;
3861 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003862 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003865 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003866 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003868 else
3869 /* End is before the start (no text in between <>, [], etc.): don't
3870 * operate on any text. */
3871 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873
3874 return OK;
3875}
3876
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003877/*
3878 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3879 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3880 */
3881 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003882in_html_tag(
3883 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003884{
3885 char_u *line = ml_get_curline();
3886 char_u *p;
3887 int c;
3888 int lc = NUL;
3889 pos_T pos;
3890
3891#ifdef FEAT_MBYTE
3892 if (enc_dbcs)
3893 {
3894 char_u *lp = NULL;
3895
3896 /* We search forward until the cursor, because searching backwards is
3897 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003898 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003899 if (*p == '>' || *p == '<')
3900 {
3901 lc = *p;
3902 lp = p;
3903 }
3904 if (*p != '<') /* check for '<' under cursor */
3905 {
3906 if (lc != '<')
3907 return FALSE;
3908 p = lp;
3909 }
3910 }
3911 else
3912#endif
3913 {
3914 for (p = line + curwin->w_cursor.col; p > line; )
3915 {
3916 if (*p == '<') /* find '<' under/before cursor */
3917 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003918 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003919 if (*p == '>') /* find '>' before cursor */
3920 break;
3921 }
3922 if (*p != '<')
3923 return FALSE;
3924 }
3925
3926 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003927 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003928
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003929 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003930 if (end_tag)
3931 /* check that there is a '/' after the '<' */
3932 return *p == '/';
3933
3934 /* check that there is no '/' after the '<' */
3935 if (*p == '/')
3936 return FALSE;
3937
3938 /* check that the matching '>' is not preceded by '/' */
3939 for (;;)
3940 {
3941 if (inc(&pos) < 0)
3942 return FALSE;
3943 c = *ml_get_pos(&pos);
3944 if (c == '>')
3945 break;
3946 lc = c;
3947 }
3948 return lc != '/';
3949}
3950
3951/*
3952 * Find tag block under the cursor, cursor at end.
3953 */
3954 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003955current_tagblock(
3956 oparg_T *oap,
3957 long count_arg,
3958 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003959{
3960 long count = count_arg;
3961 long n;
3962 pos_T old_pos;
3963 pos_T start_pos;
3964 pos_T end_pos;
3965 pos_T old_start, old_end;
3966 char_u *spat, *epat;
3967 char_u *p;
3968 char_u *cp;
3969 int len;
3970 int r;
3971 int do_include = include;
3972 int save_p_ws = p_ws;
3973 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003974 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003975
3976 p_ws = FALSE;
3977
3978 old_pos = curwin->w_cursor;
3979 old_end = curwin->w_cursor; /* remember where we started */
3980 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003981 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003982 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003983
3984 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003985 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003986 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003987 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003988 {
3989 setpcmark();
3990
3991 /* ignore indent */
3992 while (inindent(1))
3993 if (inc_cursor() != 0)
3994 break;
3995
3996 if (in_html_tag(FALSE))
3997 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003998 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003999 while (*ml_get_cursor() != '>')
4000 if (inc_cursor() < 0)
4001 break;
4002 }
4003 else if (in_html_tag(TRUE))
4004 {
4005 /* cursor on end tag, move to just before it */
4006 while (*ml_get_cursor() != '<')
4007 if (dec_cursor() < 0)
4008 break;
4009 dec_cursor();
4010 old_end = curwin->w_cursor;
4011 }
4012 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004013 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004014 {
4015 old_start = VIsual;
4016 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4017 }
4018 else
4019 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004020
4021again:
4022 /*
4023 * Search backwards for unclosed "<aaa>".
4024 * Put this position in start_pos.
4025 */
4026 for (n = 0; n < count; ++n)
4027 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004028 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004029 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004030 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004031 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004032 {
4033 curwin->w_cursor = old_pos;
4034 goto theend;
4035 }
4036 }
4037 start_pos = curwin->w_cursor;
4038
4039 /*
4040 * Search for matching "</aaa>". First isolate the "aaa".
4041 */
4042 inc_cursor();
4043 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004044 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004045 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004046 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004047 if (len == 0)
4048 {
4049 curwin->w_cursor = old_pos;
4050 goto theend;
4051 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004052 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004053 epat = alloc(len + 9);
4054 if (spat == NULL || epat == NULL)
4055 {
4056 vim_free(spat);
4057 vim_free(epat);
4058 curwin->w_cursor = old_pos;
4059 goto theend;
4060 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004061 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004062 sprintf((char *)epat, "</%.*s>\\c", len, p);
4063
Bram Moolenaar48570482017-10-30 21:48:41 +01004064 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004065 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004066
4067 vim_free(spat);
4068 vim_free(epat);
4069
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004070 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004071 {
4072 /* Can't find other end or it's before the previous end. Could be a
4073 * HTML tag that doesn't have a matching end. Search backwards for
4074 * another starting tag. */
4075 count = 1;
4076 curwin->w_cursor = start_pos;
4077 goto again;
4078 }
4079
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02004080 if (do_include)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004081 {
4082 /* Include up to the '>'. */
4083 while (*ml_get_cursor() != '>')
4084 if (inc_cursor() < 0)
4085 break;
4086 }
4087 else
4088 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004089 char_u *c = ml_get_cursor();
4090
4091 /* Exclude the '<' of the end tag.
4092 * If the closing tag is on new line, do not decrement cursor, but
4093 * make operation exclusive, so that the linefeed will be selected */
4094 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4095 /* do not decrement cursor */
4096 is_inclusive = FALSE;
4097 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004098 dec_cursor();
4099 }
4100 end_pos = curwin->w_cursor;
4101
4102 if (!do_include)
4103 {
4104 /* Exclude the start tag. */
4105 curwin->w_cursor = start_pos;
4106 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004107 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004108 {
4109 inc_cursor();
4110 start_pos = curwin->w_cursor;
4111 break;
4112 }
4113 curwin->w_cursor = end_pos;
4114
Bram Moolenaarb476cb72018-08-16 21:37:50 +02004115 // If we are in Visual mode and now have the same text as before set
4116 // "do_include" and try again.
4117 if (VIsual_active && EQUAL_POS(start_pos, old_start)
4118 && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004119 {
4120 do_include = TRUE;
4121 curwin->w_cursor = old_start;
4122 count = count_arg;
4123 goto again;
4124 }
4125 }
4126
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004127 if (VIsual_active)
4128 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004129 /* If the end is before the start there is no text between tags, select
4130 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004131 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004132 curwin->w_cursor = start_pos;
4133 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004134 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004135 VIsual = start_pos;
4136 VIsual_mode = 'v';
4137 redraw_curbuf_later(INVERTED); /* update the inversion */
4138 showmode();
4139 }
4140 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004141 {
4142 oap->start = start_pos;
4143 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004144 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004145 {
4146 /* End is before the start: there is no text between tags; operate
4147 * on an empty area. */
4148 curwin->w_cursor = start_pos;
4149 oap->inclusive = FALSE;
4150 }
4151 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004152 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004153 }
4154 retval = OK;
4155
4156theend:
4157 p_ws = save_p_ws;
4158 return retval;
4159}
4160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004162current_par(
4163 oparg_T *oap,
4164 long count,
4165 int include, /* TRUE == include white space */
4166 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
4168 linenr_T start_lnum;
4169 linenr_T end_lnum;
4170 int white_in_front;
4171 int dir;
4172 int start_is_white;
4173 int prev_start_is_white;
4174 int retval = OK;
4175 int do_white = FALSE;
4176 int t;
4177 int i;
4178
4179 if (type == 'S') /* not implemented yet */
4180 return FAIL;
4181
4182 start_lnum = curwin->w_cursor.lnum;
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 /*
4185 * When visual area is more than one line: extend it.
4186 */
4187 if (VIsual_active && start_lnum != VIsual.lnum)
4188 {
4189extend:
4190 if (start_lnum < VIsual.lnum)
4191 dir = BACKWARD;
4192 else
4193 dir = FORWARD;
4194 for (i = count; --i >= 0; )
4195 {
4196 if (start_lnum ==
4197 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4198 {
4199 retval = FAIL;
4200 break;
4201 }
4202
4203 prev_start_is_white = -1;
4204 for (t = 0; t < 2; ++t)
4205 {
4206 start_lnum += dir;
4207 start_is_white = linewhite(start_lnum);
4208 if (prev_start_is_white == start_is_white)
4209 {
4210 start_lnum -= dir;
4211 break;
4212 }
4213 for (;;)
4214 {
4215 if (start_lnum == (dir == BACKWARD
4216 ? 1 : curbuf->b_ml.ml_line_count))
4217 break;
4218 if (start_is_white != linewhite(start_lnum + dir)
4219 || (!start_is_white
4220 && startPS(start_lnum + (dir > 0
4221 ? 1 : 0), 0, 0)))
4222 break;
4223 start_lnum += dir;
4224 }
4225 if (!include)
4226 break;
4227 if (start_lnum == (dir == BACKWARD
4228 ? 1 : curbuf->b_ml.ml_line_count))
4229 break;
4230 prev_start_is_white = start_is_white;
4231 }
4232 }
4233 curwin->w_cursor.lnum = start_lnum;
4234 curwin->w_cursor.col = 0;
4235 return retval;
4236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
4238 /*
4239 * First move back to the start_lnum of the paragraph or white lines
4240 */
4241 white_in_front = linewhite(start_lnum);
4242 while (start_lnum > 1)
4243 {
4244 if (white_in_front) /* stop at first white line */
4245 {
4246 if (!linewhite(start_lnum - 1))
4247 break;
4248 }
4249 else /* stop at first non-white line of start of paragraph */
4250 {
4251 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4252 break;
4253 }
4254 --start_lnum;
4255 }
4256
4257 /*
4258 * Move past the end of any white lines.
4259 */
4260 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004261 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4262 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 --end_lnum;
4265 i = count;
4266 if (!include && white_in_front)
4267 --i;
4268 while (i--)
4269 {
4270 if (end_lnum == curbuf->b_ml.ml_line_count)
4271 return FAIL;
4272
4273 if (!include)
4274 do_white = linewhite(end_lnum + 1);
4275
4276 if (include || !do_white)
4277 {
4278 ++end_lnum;
4279 /*
4280 * skip to end of paragraph
4281 */
4282 while (end_lnum < curbuf->b_ml.ml_line_count
4283 && !linewhite(end_lnum + 1)
4284 && !startPS(end_lnum + 1, 0, 0))
4285 ++end_lnum;
4286 }
4287
4288 if (i == 0 && white_in_front && include)
4289 break;
4290
4291 /*
4292 * skip to end of white lines after paragraph
4293 */
4294 if (include || do_white)
4295 while (end_lnum < curbuf->b_ml.ml_line_count
4296 && linewhite(end_lnum + 1))
4297 ++end_lnum;
4298 }
4299
4300 /*
4301 * If there are no empty lines at the end, try to find some empty lines at
4302 * the start (unless that has been done already).
4303 */
4304 if (!white_in_front && !linewhite(end_lnum) && include)
4305 while (start_lnum > 1 && linewhite(start_lnum - 1))
4306 --start_lnum;
4307
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 if (VIsual_active)
4309 {
4310 /* Problem: when doing "Vipipip" nothing happens in a single white
4311 * line, we get stuck there. Trap this here. */
4312 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4313 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004314 if (VIsual.lnum != start_lnum)
4315 {
4316 VIsual.lnum = start_lnum;
4317 VIsual.col = 0;
4318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 VIsual_mode = 'V';
4320 redraw_curbuf_later(INVERTED); /* update the inversion */
4321 showmode();
4322 }
4323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
4325 oap->start.lnum = start_lnum;
4326 oap->start.col = 0;
4327 oap->motion_type = MLINE;
4328 }
4329 curwin->w_cursor.lnum = end_lnum;
4330 curwin->w_cursor.col = 0;
4331
4332 return OK;
4333}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004334
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004335/*
4336 * Search quote char from string line[col].
4337 * Quote character escaped by one of the characters in "escape" is not counted
4338 * as a quote.
4339 * Returns column number of "quotechar" or -1 when not found.
4340 */
4341 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004342find_next_quote(
4343 char_u *line,
4344 int col,
4345 int quotechar,
4346 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004347{
4348 int c;
4349
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004350 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004351 {
4352 c = line[col];
4353 if (c == NUL)
4354 return -1;
4355 else if (escape != NULL && vim_strchr(escape, c))
4356 ++col;
4357 else if (c == quotechar)
4358 break;
4359#ifdef FEAT_MBYTE
4360 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004361 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004364 ++col;
4365 }
4366 return col;
4367}
4368
4369/*
4370 * Search backwards in "line" from column "col_start" to find "quotechar".
4371 * Quote character escaped by one of the characters in "escape" is not counted
4372 * as a quote.
4373 * Return the found column or zero.
4374 */
4375 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004376find_prev_quote(
4377 char_u *line,
4378 int col_start,
4379 int quotechar,
4380 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004381{
4382 int n;
4383
4384 while (col_start > 0)
4385 {
4386 --col_start;
4387#ifdef FEAT_MBYTE
4388 col_start -= (*mb_head_off)(line, line + col_start);
4389#endif
4390 n = 0;
4391 if (escape != NULL)
4392 while (col_start - n > 0 && vim_strchr(escape,
4393 line[col_start - n - 1]) != NULL)
4394 ++n;
4395 if (n & 1)
4396 col_start -= n; /* uneven number of escape chars, skip it */
4397 else if (line[col_start] == quotechar)
4398 break;
4399 }
4400 return col_start;
4401}
4402
4403/*
4404 * Find quote under the cursor, cursor at end.
4405 * Returns TRUE if found, else FALSE.
4406 */
4407 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004408current_quote(
4409 oparg_T *oap,
4410 long count,
4411 int include, /* TRUE == include quote char */
4412 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004413{
4414 char_u *line = ml_get_curline();
4415 int col_end;
4416 int col_start = curwin->w_cursor.col;
4417 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004418 int vis_empty = TRUE; /* Visual selection <= 1 char */
4419 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004420 int inside_quotes = FALSE; /* Looks like "i'" done before */
4421 int selected_quote = FALSE; /* Has quote inside selection */
4422 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004423
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004424 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004425 if (VIsual_active)
4426 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004427 /* this only works within one line */
4428 if (VIsual.lnum != curwin->w_cursor.lnum)
4429 return FALSE;
4430
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004431 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004432 if (*p_sel == 'e')
4433 {
4434 if (!vis_bef_curs)
4435 {
4436 /* VIsual needs to be start of Visual selection. */
4437 pos_T t = curwin->w_cursor;
4438
4439 curwin->w_cursor = VIsual;
4440 VIsual = t;
4441 vis_bef_curs = TRUE;
4442 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004443 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004444 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004445 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004446 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004447
4448 if (!vis_empty)
4449 {
4450 /* Check if the existing selection exactly spans the text inside
4451 * quotes. */
4452 if (vis_bef_curs)
4453 {
4454 inside_quotes = VIsual.col > 0
4455 && line[VIsual.col - 1] == quotechar
4456 && line[curwin->w_cursor.col] != NUL
4457 && line[curwin->w_cursor.col + 1] == quotechar;
4458 i = VIsual.col;
4459 col_end = curwin->w_cursor.col;
4460 }
4461 else
4462 {
4463 inside_quotes = curwin->w_cursor.col > 0
4464 && line[curwin->w_cursor.col - 1] == quotechar
4465 && line[VIsual.col] != NUL
4466 && line[VIsual.col + 1] == quotechar;
4467 i = curwin->w_cursor.col;
4468 col_end = VIsual.col;
4469 }
4470
4471 /* Find out if we have a quote in the selection. */
4472 while (i <= col_end)
4473 if (line[i++] == quotechar)
4474 {
4475 selected_quote = TRUE;
4476 break;
4477 }
4478 }
4479
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004480 if (!vis_empty && line[col_start] == quotechar)
4481 {
4482 /* Already selecting something and on a quote character. Find the
4483 * next quoted string. */
4484 if (vis_bef_curs)
4485 {
4486 /* Assume we are on a closing quote: move to after the next
4487 * opening quote. */
4488 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4489 if (col_start < 0)
4490 return FALSE;
4491 col_end = find_next_quote(line, col_start + 1, quotechar,
4492 curbuf->b_p_qe);
4493 if (col_end < 0)
4494 {
4495 /* We were on a starting quote perhaps? */
4496 col_end = col_start;
4497 col_start = curwin->w_cursor.col;
4498 }
4499 }
4500 else
4501 {
4502 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4503 if (line[col_end] != quotechar)
4504 return FALSE;
4505 col_start = find_prev_quote(line, col_end, quotechar,
4506 curbuf->b_p_qe);
4507 if (line[col_start] != quotechar)
4508 {
4509 /* We were on an ending quote perhaps? */
4510 col_start = col_end;
4511 col_end = curwin->w_cursor.col;
4512 }
4513 }
4514 }
4515 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004516
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004517 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004518 {
4519 int first_col = col_start;
4520
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004521 if (!vis_empty)
4522 {
4523 if (vis_bef_curs)
4524 first_col = find_next_quote(line, col_start, quotechar, NULL);
4525 else
4526 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4527 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004528
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004529 /* The cursor is on a quote, we don't know if it's the opening or
4530 * closing quote. Search from the start of the line to find out.
4531 * Also do this when there is a Visual area, a' may leave the cursor
4532 * in between two strings. */
4533 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004534 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004535 {
4536 /* Find open quote character. */
4537 col_start = find_next_quote(line, col_start, quotechar, NULL);
4538 if (col_start < 0 || col_start > first_col)
4539 return FALSE;
4540 /* Find close quote character. */
4541 col_end = find_next_quote(line, col_start + 1, quotechar,
4542 curbuf->b_p_qe);
4543 if (col_end < 0)
4544 return FALSE;
4545 /* If is cursor between start and end quote character, it is
4546 * target text object. */
4547 if (col_start <= first_col && first_col <= col_end)
4548 break;
4549 col_start = col_end + 1;
4550 }
4551 }
4552 else
4553 {
4554 /* Search backward for a starting quote. */
4555 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4556 if (line[col_start] != quotechar)
4557 {
4558 /* No quote before the cursor, look after the cursor. */
4559 col_start = find_next_quote(line, col_start, quotechar, NULL);
4560 if (col_start < 0)
4561 return FALSE;
4562 }
4563
4564 /* Find close quote character. */
4565 col_end = find_next_quote(line, col_start + 1, quotechar,
4566 curbuf->b_p_qe);
4567 if (col_end < 0)
4568 return FALSE;
4569 }
4570
4571 /* When "include" is TRUE, include spaces after closing quote or before
4572 * the starting quote. */
4573 if (include)
4574 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004575 if (VIM_ISWHITE(line[col_end + 1]))
4576 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004577 ++col_end;
4578 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004579 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004580 --col_start;
4581 }
4582
Bram Moolenaarab194812005-09-14 21:40:12 +00004583 /* Set start position. After vi" another i" must include the ".
4584 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004585 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004586 ++col_start;
4587 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004588 if (VIsual_active)
4589 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004590 /* Set the start of the Visual area when the Visual area was empty, we
4591 * were just inside quotes or the Visual area didn't start at a quote
4592 * and didn't include a quote.
4593 */
4594 if (vis_empty
4595 || (vis_bef_curs
4596 && !selected_quote
4597 && (inside_quotes
4598 || (line[VIsual.col] != quotechar
4599 && (VIsual.col == 0
4600 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004601 {
4602 VIsual = curwin->w_cursor;
4603 redraw_curbuf_later(INVERTED);
4604 }
4605 }
4606 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004607 {
4608 oap->start = curwin->w_cursor;
4609 oap->motion_type = MCHAR;
4610 }
4611
4612 /* Set end position. */
4613 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004614 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004615 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004616 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004617 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004618 if (VIsual_active)
4619 {
4620 if (vis_empty || vis_bef_curs)
4621 {
4622 /* decrement cursor when 'selection' is not exclusive */
4623 if (*p_sel != 'e')
4624 dec_cursor();
4625 }
4626 else
4627 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004628 /* Cursor is at start of Visual area. Set the end of the Visual
4629 * area when it was just inside quotes or it didn't end at a
4630 * quote. */
4631 if (inside_quotes
4632 || (!selected_quote
4633 && line[VIsual.col] != quotechar
4634 && (line[VIsual.col] == NUL
4635 || line[VIsual.col + 1] != quotechar)))
4636 {
4637 dec_cursor();
4638 VIsual = curwin->w_cursor;
4639 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004640 curwin->w_cursor.col = col_start;
4641 }
4642 if (VIsual_mode == 'V')
4643 {
4644 VIsual_mode = 'v';
4645 redraw_cmdline = TRUE; /* show mode later */
4646 }
4647 }
4648 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004649 {
4650 /* Set inclusive and other oap's flags. */
4651 oap->inclusive = inclusive;
4652 }
4653
4654 return OK;
4655}
4656
4657#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004659static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004660
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004661/*
4662 * Find next search match under cursor, cursor at end.
4663 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004664 */
4665 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004666current_search(
4667 long count,
4668 int forward) /* move forward or backwards */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004669{
4670 pos_T start_pos; /* position before the pattern */
4671 pos_T orig_pos; /* position of the cursor at beginning */
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004672 pos_T first_match; /* position of first match */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004673 pos_T pos; /* position after the pattern */
4674 int i;
4675 int dir;
4676 int result; /* result of various function calls */
4677 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004678 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004679 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004680 int one_char;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004681 int direction = forward ? FORWARD : BACKWARD;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004682
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004683 /* wrapping should not occur */
4684 p_ws = FALSE;
4685
4686 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004687 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004688 dec_cursor();
4689
4690 if (VIsual_active)
4691 {
4692 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004693
4694 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004695
4696 /* make sure, searching further will extend the match */
4697 if (VIsual_active)
4698 {
4699 if (forward)
4700 incl(&pos);
4701 else
4702 decl(&pos);
4703 }
4704 }
4705 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004706 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004707
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004708 /* Is the pattern is zero-width?, this time, don't care about the direction
4709 */
4710 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4711 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004712 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004713 {
4714 p_ws = old_p_ws;
4715 return FAIL; /* pattern not found */
4716 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004717
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004718 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004719 * The trick is to first search backwards and then search forward again,
4720 * so that a match at the current cursor position will be correctly
4721 * captured.
4722 */
4723 for (i = 0; i < 2; i++)
4724 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004725 if (forward)
4726 dir = i;
4727 else
4728 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004729
4730 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004731 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004732 flags = SEARCH_END;
4733
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004734 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4735 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004736 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004737
4738 /* First search may fail, but then start searching from the
4739 * beginning of the file (cursor might be on the search match)
4740 * except when Visual mode is active, so that extending the visual
4741 * selection works. */
4742 if (!result && i) /* not found, abort */
4743 {
4744 curwin->w_cursor = orig_pos;
4745 if (VIsual_active)
4746 VIsual = save_VIsual;
4747 p_ws = old_p_ws;
4748 return FAIL;
4749 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004750 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004751 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004752 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004753 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004754 /* try again from start of buffer */
4755 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004756 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004757 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004758 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004759 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004760 /* searching backwards, so set pos to last line and col */
4761 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004762 pos.col = (colnr_T)STRLEN(
4763 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004764 }
4765 }
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004766 if (i == 0)
4767 first_match = pos;
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004768 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004769 }
4770
4771 start_pos = pos;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004772 flags = forward ? SEARCH_END : SEARCH_START;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004773
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004774 /* Check again from the current cursor position,
4775 * since the next match might actually by only one char wide */
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004776 one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004777 if (one_char < 0)
4778 /* search failed, abort */
4779 return FAIL;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004780
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004781 /* move to match, except for zero-width matches, in which case, we are
4782 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004783 if (!one_char)
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004784 {
4785 p_ws = FALSE;
4786 for (i = 0; i < 2; i++)
4787 {
4788 result = searchit(curwin, curbuf, &pos, direction,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004789 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
4790 NULL, NULL);
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004791 /* Search successfull, break out from the loop */
4792 if (result)
4793 break;
4794 /* search failed, try again from the last search position match */
4795 pos = first_match;
4796 }
4797 }
4798
4799 p_ws = old_p_ws;
4800 /* not found */
4801 if (!result)
4802 return FAIL;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004803
4804 if (!VIsual_active)
4805 VIsual = start_pos;
4806
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004807 curwin->w_cursor = pos;
4808 VIsual_active = TRUE;
4809 VIsual_mode = 'v';
4810
4811 if (VIsual_active)
4812 {
4813 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004814 if (*p_sel == 'e')
4815 {
4816 /* Correction for exclusive selection depends on the direction. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004817 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004818 inc_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004819 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004820 inc(&VIsual);
4821 }
4822
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004823 }
4824
4825#ifdef FEAT_FOLDING
4826 if (fdo_flags & FDO_SEARCH && KeyTyped)
4827 foldOpenCursor();
4828#endif
4829
4830 may_start_select('c');
4831#ifdef FEAT_MOUSE
4832 setmouse();
4833#endif
4834#ifdef FEAT_CLIPBOARD
4835 /* Make sure the clipboard gets updated. Needed because start and
4836 * end are still the same, and the selection needs to be owned */
4837 clip_star.vmode = NUL;
4838#endif
4839 redraw_curbuf_later(INVERTED);
4840 showmode();
4841
4842 return OK;
4843}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004844
4845/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004846 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004847 * If move is TRUE, check from the beginning of the buffer, else from position
4848 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004849 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004850 * Returns TRUE, FALSE or -1 for failure.
4851 */
4852 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004853is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004854{
4855 regmmatch_T regmatch;
4856 int nmatched = 0;
4857 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004858 pos_T pos;
4859 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004860 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004861
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004862 if (pattern == NULL)
4863 pattern = spats[last_idx].pat;
4864
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004865 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4866 SEARCH_KEEP, &regmatch) == FAIL)
4867 return -1;
4868
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004869 /* init startcol correctly */
4870 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004871 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004872 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004873 {
4874 CLEAR_POS(&pos);
4875 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004876 else
4877 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004878 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004879 /* accept a match at the cursor position */
4880 flag = SEARCH_START;
4881 }
4882
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004883 if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004884 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004885 {
4886 /* Zero-width pattern should match somewhere, then we can check if
4887 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004888 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004889 do
4890 {
4891 regmatch.startpos[0].col++;
4892 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004893 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004894 if (!nmatched)
4895 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004896 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4897 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004898
4899 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004900 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004901 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004902 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4903 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004904 /* one char width */
4905 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4906 result = TRUE;
4907 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004908 }
4909
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004910 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004911 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004912 return result;
4913}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4916 || defined(PROTO)
4917/*
4918 * return TRUE if line 'lnum' is empty or has white chars only.
4919 */
4920 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004921linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
4923 char_u *p;
4924
4925 p = skipwhite(ml_get(lnum));
4926 return (*p == NUL);
4927}
4928#endif
4929
4930#if defined(FEAT_FIND_ID) || defined(PROTO)
4931/*
4932 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004933 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004936find_pattern_in_path(
4937 char_u *ptr, /* pointer to search pattern */
4938 int dir UNUSED, /* direction of expansion */
4939 int len, /* length of search pattern */
4940 int whole, /* match whole words only */
4941 int skip_comments, /* don't match inside comments */
4942 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004944 long count,
4945 int action, /* What to do when we find it */
4946 linenr_T start_lnum, /* first line to start searching */
4947 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
4949 SearchedFile *files; /* Stack of included files */
4950 SearchedFile *bigger; /* When we need more space */
4951 int max_path_depth = 50;
4952 long match_count = 1;
4953
4954 char_u *pat;
4955 char_u *new_fname;
4956 char_u *curr_fname = curbuf->b_fname;
4957 char_u *prev_fname = NULL;
4958 linenr_T lnum;
4959 int depth;
4960 int depth_displayed; /* For type==CHECK_PATH */
4961 int old_files;
4962 int already_searched;
4963 char_u *file_line;
4964 char_u *line;
4965 char_u *p;
4966 char_u save_char;
4967 int define_matched;
4968 regmatch_T regmatch;
4969 regmatch_T incl_regmatch;
4970 regmatch_T def_regmatch;
4971 int matched = FALSE;
4972 int did_show = FALSE;
4973 int found = FALSE;
4974 int i;
4975 char_u *already = NULL;
4976 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004977 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004978#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 win_T *curwin_save = NULL;
4980#endif
4981
4982 regmatch.regprog = NULL;
4983 incl_regmatch.regprog = NULL;
4984 def_regmatch.regprog = NULL;
4985
4986 file_line = alloc(LSIZE);
4987 if (file_line == NULL)
4988 return;
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 if (type != CHECK_PATH && type != FIND_DEFINE
4991#ifdef FEAT_INS_EXPAND
4992 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4993 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004994 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#endif
4996 )
4997 {
4998 pat = alloc(len + 5);
4999 if (pat == NULL)
5000 goto fpip_end;
5001 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
5002 /* ignore case according to p_ic, p_scs and pat */
5003 regmatch.rm_ic = ignorecase(pat);
5004 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5005 vim_free(pat);
5006 if (regmatch.regprog == NULL)
5007 goto fpip_end;
5008 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005009 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
5010 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005012 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (incl_regmatch.regprog == NULL)
5014 goto fpip_end;
5015 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
5016 }
5017 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
5018 {
5019 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5020 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5021 if (def_regmatch.regprog == NULL)
5022 goto fpip_end;
5023 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5024 }
5025 files = (SearchedFile *)lalloc_clear((long_u)
5026 (max_path_depth * sizeof(SearchedFile)), TRUE);
5027 if (files == NULL)
5028 goto fpip_end;
5029 old_files = max_path_depth;
5030 depth = depth_displayed = -1;
5031
5032 lnum = start_lnum;
5033 if (end_lnum > curbuf->b_ml.ml_line_count)
5034 end_lnum = curbuf->b_ml.ml_line_count;
5035 if (lnum > end_lnum) /* do at least one line */
5036 lnum = end_lnum;
5037 line = ml_get(lnum);
5038
5039 for (;;)
5040 {
5041 if (incl_regmatch.regprog != NULL
5042 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5043 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005044 char_u *p_fname = (curr_fname == curbuf->b_fname)
5045 ? curbuf->b_ffname : curr_fname;
5046
5047 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5048 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5049 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005050 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005051 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5052 else
5053 /* Use text after match with 'include'. */
5054 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005055 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 already_searched = FALSE;
5057 if (new_fname != NULL)
5058 {
5059 /* Check whether we have already searched in this file */
5060 for (i = 0;; i++)
5061 {
5062 if (i == depth + 1)
5063 i = old_files;
5064 if (i == max_path_depth)
5065 break;
5066 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5067 {
5068 if (type != CHECK_PATH &&
5069 action == ACTION_SHOW_ALL && files[i].matched)
5070 {
5071 msg_putchar('\n'); /* cursor below last one */
5072 if (!got_int) /* don't display if 'q'
5073 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005074 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 msg_home_replace_hl(new_fname);
5077 MSG_PUTS(_(" (includes previously listed match)"));
5078 prev_fname = NULL;
5079 }
5080 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01005081 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 already_searched = TRUE;
5083 break;
5084 }
5085 }
5086 }
5087
5088 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5089 || (new_fname == NULL && !already_searched)))
5090 {
5091 if (did_show)
5092 msg_putchar('\n'); /* cursor below last one */
5093 else
5094 {
5095 gotocmdline(TRUE); /* cursor at status line */
5096 MSG_PUTS_TITLE(_("--- Included files "));
5097 if (action != ACTION_SHOW_ALL)
5098 MSG_PUTS_TITLE(_("not found "));
5099 MSG_PUTS_TITLE(_("in path ---\n"));
5100 }
5101 did_show = TRUE;
5102 while (depth_displayed < depth && !got_int)
5103 {
5104 ++depth_displayed;
5105 for (i = 0; i < depth_displayed; i++)
5106 MSG_PUTS(" ");
5107 msg_home_replace(files[depth_displayed].name);
5108 MSG_PUTS(" -->\n");
5109 }
5110 if (!got_int) /* don't display if 'q' typed
5111 for "--more--" message */
5112 {
5113 for (i = 0; i <= depth_displayed; i++)
5114 MSG_PUTS(" ");
5115 if (new_fname != NULL)
5116 {
5117 /* using "new_fname" is more reliable, e.g., when
5118 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005119 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 else
5122 {
5123 /*
5124 * Isolate the file name.
5125 * Include the surrounding "" or <> if present.
5126 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005127 if (inc_opt != NULL
5128 && strstr((char *)inc_opt, "\\zs") != NULL)
5129 {
5130 /* pattern contains \zs, use the match */
5131 p = incl_regmatch.startp[0];
5132 i = (int)(incl_regmatch.endp[0]
5133 - incl_regmatch.startp[0]);
5134 }
5135 else
5136 {
5137 /* find the file name after the end of the match */
5138 for (p = incl_regmatch.endp[0];
5139 *p && !vim_isfilec(*p); p++)
5140 ;
5141 for (i = 0; vim_isfilec(p[i]); i++)
5142 ;
5143 }
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 if (i == 0)
5146 {
5147 /* Nothing found, use the rest of the line. */
5148 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005149 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005151 /* Avoid checking before the start of the line, can
5152 * happen if \zs appears in the regexp. */
5153 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
5155 if (p[-1] == '"' || p[-1] == '<')
5156 {
5157 --p;
5158 ++i;
5159 }
5160 if (p[i] == '"' || p[i] == '>')
5161 ++i;
5162 }
5163 save_char = p[i];
5164 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005165 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 p[i] = save_char;
5167 }
5168
5169 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5170 {
5171 if (already_searched)
5172 MSG_PUTS(_(" (Already listed)"));
5173 else
5174 MSG_PUTS(_(" NOT FOUND"));
5175 }
5176 }
5177 out_flush(); /* output each line directly */
5178 }
5179
5180 if (new_fname != NULL)
5181 {
5182 /* Push the new file onto the file stack */
5183 if (depth + 1 == old_files)
5184 {
5185 bigger = (SearchedFile *)lalloc((long_u)(
5186 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5187 if (bigger != NULL)
5188 {
5189 for (i = 0; i <= depth; i++)
5190 bigger[i] = files[i];
5191 for (i = depth + 1; i < old_files + max_path_depth; i++)
5192 {
5193 bigger[i].fp = NULL;
5194 bigger[i].name = NULL;
5195 bigger[i].lnum = 0;
5196 bigger[i].matched = FALSE;
5197 }
5198 for (i = old_files; i < max_path_depth; i++)
5199 bigger[i + max_path_depth] = files[i];
5200 old_files += max_path_depth;
5201 max_path_depth *= 2;
5202 vim_free(files);
5203 files = bigger;
5204 }
5205 }
5206 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5207 == NULL)
5208 vim_free(new_fname);
5209 else
5210 {
5211 if (++depth == old_files)
5212 {
5213 /*
5214 * lalloc() for 'bigger' must have failed above. We
5215 * will forget one of our already visited files now.
5216 */
5217 vim_free(files[old_files].name);
5218 ++old_files;
5219 }
5220 files[depth].name = curr_fname = new_fname;
5221 files[depth].lnum = 0;
5222 files[depth].matched = FALSE;
5223#ifdef FEAT_INS_EXPAND
5224 if (action == ACTION_EXPAND)
5225 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005226 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005227 vim_snprintf((char*)IObuff, IOSIZE,
5228 _("Scanning included file: %s"),
5229 (char *)new_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005230 msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005232 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005234 if (p_verbose >= 5)
5235 {
5236 verbose_enter();
5237 smsg((char_u *)_("Searching included file %s"),
5238 (char *)new_fname);
5239 verbose_leave();
5240 }
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 }
5244 }
5245 else
5246 {
5247 /*
5248 * Check if the line is a define (type == FIND_DEFINE)
5249 */
5250 p = line;
5251search_line:
5252 define_matched = FALSE;
5253 if (def_regmatch.regprog != NULL
5254 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5255 {
5256 /*
5257 * Pattern must be first identifier after 'define', so skip
5258 * to that position before checking for match of pattern. Also
5259 * don't let it match beyond the end of this identifier.
5260 */
5261 p = def_regmatch.endp[0];
5262 while (*p && !vim_iswordc(*p))
5263 p++;
5264 define_matched = TRUE;
5265 }
5266
5267 /*
5268 * Look for a match. Don't do this if we are looking for a
5269 * define and this line didn't match define_prog above.
5270 */
5271 if (def_regmatch.regprog == NULL || define_matched)
5272 {
5273 if (define_matched
5274#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#endif
5277 )
5278 {
5279 /* compare the first "len" chars from "ptr" */
5280 startp = skipwhite(p);
5281 if (p_ic)
5282 matched = !MB_STRNICMP(startp, ptr, len);
5283 else
5284 matched = !STRNCMP(startp, ptr, len);
5285 if (matched && define_matched && whole
5286 && vim_iswordc(startp[len]))
5287 matched = FALSE;
5288 }
5289 else if (regmatch.regprog != NULL
5290 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5291 {
5292 matched = TRUE;
5293 startp = regmatch.startp[0];
5294 /*
5295 * Check if the line is not a comment line (unless we are
5296 * looking for a define). A line starting with "# define"
5297 * is not considered to be a comment line.
5298 */
5299 if (!define_matched && skip_comments)
5300 {
5301#ifdef FEAT_COMMENTS
5302 if ((*line != '#' ||
5303 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005304 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 matched = FALSE;
5306
5307 /*
5308 * Also check for a "/ *" or "/ /" before the match.
5309 * Skips lines like "int backwards; / * normal index
5310 * * /" when looking for "normal".
5311 * Note: Doesn't skip "/ *" in comments.
5312 */
5313 p = skipwhite(line);
5314 if (matched
5315 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5316#endif
5317 for (p = line; *p && p < startp; ++p)
5318 {
5319 if (matched
5320 && p[0] == '/'
5321 && (p[1] == '*' || p[1] == '/'))
5322 {
5323 matched = FALSE;
5324 /* After "//" all text is comment */
5325 if (p[1] == '/')
5326 break;
5327 ++p;
5328 }
5329 else if (!matched && p[0] == '*' && p[1] == '/')
5330 {
5331 /* Can find match after "* /". */
5332 matched = TRUE;
5333 ++p;
5334 }
5335 }
5336 }
5337 }
5338 }
5339 }
5340 if (matched)
5341 {
5342#ifdef FEAT_INS_EXPAND
5343 if (action == ACTION_EXPAND)
5344 {
5345 int reuse = 0;
5346 int add_r;
5347 char_u *aux;
5348
5349 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5350 break;
5351 found = TRUE;
5352 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 if (vim_iswordp(p))
5357 goto exit_matched;
5358 p = find_word_start(p);
5359 }
5360 p = find_word_end(p);
5361 i = (int)(p - aux);
5362
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005363 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005367
5368 /* Get the next line: when "depth" < 0 from the current
5369 * buffer, otherwise from the included file. Jump to
5370 * exit_matched when past the last line. */
5371 if (depth < 0)
5372 {
5373 if (lnum >= end_lnum)
5374 goto exit_matched;
5375 line = ml_get(++lnum);
5376 }
5377 else if (vim_fgets(line = file_line,
5378 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 goto exit_matched;
5380
5381 /* we read a line, set "already" to check this "line" later
5382 * if depth >= 0 we'll increase files[depth].lnum far
5383 * bellow -- Acevedo */
5384 already = aux = p = skipwhite(line);
5385 p = find_word_start(p);
5386 p = find_word_end(p);
5387 if (p > aux)
5388 {
5389 if (*aux != ')' && IObuff[i-1] != TAB)
5390 {
5391 if (IObuff[i-1] != ' ')
5392 IObuff[i++] = ' ';
5393 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5394 if (p_js
5395 && (IObuff[i-2] == '.'
5396 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5397 && (IObuff[i-2] == '?'
5398 || IObuff[i-2] == '!'))))
5399 IObuff[i++] = ' ';
5400 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005401 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 if (p - aux >= IOSIZE - i)
5403 p = aux + IOSIZE - i - 1;
5404 STRNCPY(IObuff + i, aux, p - aux);
5405 i += (int)(p - aux);
5406 reuse |= CONT_S_IPOS;
5407 }
5408 IObuff[i] = NUL;
5409 aux = IObuff;
5410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005411 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 goto exit_matched;
5413 }
5414
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005415 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5417 dir, reuse);
5418 if (add_r == OK)
5419 /* if dir was BACKWARD then honor it just once */
5420 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005421 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 break;
5423 }
5424 else
5425#endif
5426 if (action == ACTION_SHOW_ALL)
5427 {
5428 found = TRUE;
5429 if (!did_show)
5430 gotocmdline(TRUE); /* cursor at status line */
5431 if (curr_fname != prev_fname)
5432 {
5433 if (did_show)
5434 msg_putchar('\n'); /* cursor below last one */
5435 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005436 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 msg_home_replace_hl(curr_fname);
5438 prev_fname = curr_fname;
5439 }
5440 did_show = TRUE;
5441 if (!got_int)
5442 show_pat_in_path(line, type, TRUE, action,
5443 (depth == -1) ? NULL : files[depth].fp,
5444 (depth == -1) ? &lnum : &files[depth].lnum,
5445 match_count++);
5446
5447 /* Set matched flag for this file and all the ones that
5448 * include it */
5449 for (i = 0; i <= depth; ++i)
5450 files[i].matched = TRUE;
5451 }
5452 else if (--count <= 0)
5453 {
5454 found = TRUE;
5455 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005456#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 && g_do_tagpreview == 0
5458#endif
5459 )
5460 EMSG(_("E387: Match is on current line"));
5461 else if (action == ACTION_SHOW)
5462 {
5463 show_pat_in_path(line, type, did_show, action,
5464 (depth == -1) ? NULL : files[depth].fp,
5465 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5466 did_show = TRUE;
5467 }
5468 else
5469 {
5470#ifdef FEAT_GUI
5471 need_mouse_correct = TRUE;
5472#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005473#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 /* ":psearch" uses the preview window */
5475 if (g_do_tagpreview != 0)
5476 {
5477 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005478 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480#endif
5481 if (action == ACTION_SPLIT)
5482 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005485 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 if (depth == -1)
5488 {
5489 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005490#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (g_do_tagpreview != 0)
5492 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005493 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005494 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005495 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 break; /* failed to jump to file */
5497 }
5498 else
5499#endif
5500 setpcmark();
5501 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005502 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 }
5504 else
5505 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005506 if (!GETFILE_SUCCESS(getfile(
5507 0, files[depth].name, NULL, TRUE,
5508 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 break; /* failed to jump to file */
5510 /* autocommands may have changed the lnum, we don't
5511 * want that here */
5512 curwin->w_cursor.lnum = files[depth].lnum;
5513 }
5514 }
5515 if (action != ACTION_SHOW)
5516 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005517 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 curwin->w_set_curswant = TRUE;
5519 }
5520
Bram Moolenaar4033c552017-09-16 20:54:51 +02005521#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005523 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
5525 /* Return cursor to where we were */
5526 validate_cursor();
5527 redraw_later(VALID);
5528 win_enter(curwin_save, TRUE);
5529 }
5530#endif
5531 break;
5532 }
5533#ifdef FEAT_INS_EXPAND
5534exit_matched:
5535#endif
5536 matched = FALSE;
5537 /* look for other matches in the rest of the line if we
5538 * are not at the end of it already */
5539 if (def_regmatch.regprog == NULL
5540#ifdef FEAT_INS_EXPAND
5541 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005542 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005544 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005545 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 goto search_line;
5547 }
5548 line_breakcheck();
5549#ifdef FEAT_INS_EXPAND
5550 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005551 ins_compl_check_keys(30, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005552 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553#else
5554 if (got_int)
5555#endif
5556 break;
5557
5558 /*
5559 * Read the next line. When reading an included file and encountering
5560 * end-of-file, close the file and continue in the file that included
5561 * it.
5562 */
5563 while (depth >= 0 && !already
5564 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5565 {
5566 fclose(files[depth].fp);
5567 --old_files;
5568 files[old_files].name = files[depth].name;
5569 files[old_files].matched = files[depth].matched;
5570 --depth;
5571 curr_fname = (depth == -1) ? curbuf->b_fname
5572 : files[depth].name;
5573 if (depth < depth_displayed)
5574 depth_displayed = depth;
5575 }
5576 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005579 /* Remove any CR and LF from the line. */
5580 i = (int)STRLEN(line);
5581 if (i > 0 && line[i - 1] == '\n')
5582 line[--i] = NUL;
5583 if (i > 0 && line[i - 1] == '\r')
5584 line[--i] = NUL;
5585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 else if (!already)
5587 {
5588 if (++lnum > end_lnum)
5589 break;
5590 line = ml_get(lnum);
5591 }
5592 already = NULL;
5593 }
5594 /* End of big for (;;) loop. */
5595
5596 /* Close any files that are still open. */
5597 for (i = 0; i <= depth; i++)
5598 {
5599 fclose(files[i].fp);
5600 vim_free(files[i].name);
5601 }
5602 for (i = old_files; i < max_path_depth; i++)
5603 vim_free(files[i].name);
5604 vim_free(files);
5605
5606 if (type == CHECK_PATH)
5607 {
5608 if (!did_show)
5609 {
5610 if (action != ACTION_SHOW_ALL)
5611 MSG(_("All included files were found"));
5612 else
5613 MSG(_("No included files"));
5614 }
5615 }
5616 else if (!found
5617#ifdef FEAT_INS_EXPAND
5618 && action != ACTION_EXPAND
5619#endif
5620 )
5621 {
5622#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005623 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624#else
5625 if (got_int)
5626#endif
5627 EMSG(_(e_interr));
5628 else if (type == FIND_DEFINE)
5629 EMSG(_("E388: Couldn't find definition"));
5630 else
5631 EMSG(_("E389: Couldn't find pattern"));
5632 }
5633 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5634 msg_end();
5635
5636fpip_end:
5637 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005638 vim_regfree(regmatch.regprog);
5639 vim_regfree(incl_regmatch.regprog);
5640 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641}
5642
5643 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005644show_pat_in_path(
5645 char_u *line,
5646 int type,
5647 int did_show,
5648 int action,
5649 FILE *fp,
5650 linenr_T *lnum,
5651 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 char_u *p;
5654
5655 if (did_show)
5656 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005657 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 gotocmdline(TRUE); /* cursor at status line */
5659 if (got_int) /* 'q' typed at "--more--" message */
5660 return;
5661 for (;;)
5662 {
5663 p = line + STRLEN(line) - 1;
5664 if (fp != NULL)
5665 {
5666 /* We used fgets(), so get rid of newline at end */
5667 if (p >= line && *p == '\n')
5668 --p;
5669 if (p >= line && *p == '\r')
5670 --p;
5671 *(p + 1) = NUL;
5672 }
5673 if (action == ACTION_SHOW_ALL)
5674 {
5675 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5676 msg_puts(IObuff);
5677 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5678 /* Highlight line numbers */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005679 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 MSG_PUTS(" ");
5681 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005682 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 out_flush(); /* show one line at a time */
5684
5685 /* Definition continues until line that doesn't end with '\' */
5686 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5687 break;
5688
5689 if (fp != NULL)
5690 {
5691 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5692 break;
5693 ++*lnum;
5694 }
5695 else
5696 {
5697 if (++*lnum > curbuf->b_ml.ml_line_count)
5698 break;
5699 line = ml_get(*lnum);
5700 }
5701 msg_putchar('\n');
5702 }
5703}
5704#endif
5705
5706#ifdef FEAT_VIMINFO
5707 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005708read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 char_u *lp;
5711 int idx = -1;
5712 int magic = FALSE;
5713 int no_scs = FALSE;
5714 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005715 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 long off = 0;
5717 int setlast = FALSE;
5718#ifdef FEAT_SEARCH_EXTRA
5719 static int hlsearch_on = FALSE;
5720#endif
5721 char_u *val;
5722
5723 /*
5724 * Old line types:
5725 * "/pat", "&pat": search/subst. pat
5726 * "~/pat", "~&pat": last used search/subst. pat
5727 * New line types:
5728 * "~h", "~H": hlsearch highlighting off/on
5729 * "~<magic><smartcase><line><end><off><last><which>pat"
5730 * <magic>: 'm' off, 'M' on
5731 * <smartcase>: 's' off, 'S' on
5732 * <line>: 'L' line offset, 'l' char offset
5733 * <end>: 'E' from end, 'e' from start
5734 * <off>: decimal, offset
5735 * <last>: '~' last used pattern
5736 * <which>: '/' search pat, '&' subst. pat
5737 */
5738 lp = virp->vir_line;
5739 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5740 {
5741 if (lp[1] == 'M') /* magic on */
5742 magic = TRUE;
5743 if (lp[2] == 's')
5744 no_scs = TRUE;
5745 if (lp[3] == 'L')
5746 off_line = TRUE;
5747 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005748 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 lp += 5;
5750 off = getdigits(&lp);
5751 }
5752 if (lp[0] == '~') /* use this pattern for last-used pattern */
5753 {
5754 setlast = TRUE;
5755 lp++;
5756 }
5757 if (lp[0] == '/')
5758 idx = RE_SEARCH;
5759 else if (lp[0] == '&')
5760 idx = RE_SUBST;
5761#ifdef FEAT_SEARCH_EXTRA
5762 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5763 hlsearch_on = FALSE;
5764 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5765 hlsearch_on = TRUE;
5766#endif
5767 if (idx >= 0)
5768 {
5769 if (force || spats[idx].pat == NULL)
5770 {
5771 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5772 TRUE);
5773 if (val != NULL)
5774 {
5775 set_last_search_pat(val, idx, magic, setlast);
5776 vim_free(val);
5777 spats[idx].no_scs = no_scs;
5778 spats[idx].off.line = off_line;
5779 spats[idx].off.end = off_end;
5780 spats[idx].off.off = off;
5781#ifdef FEAT_SEARCH_EXTRA
5782 if (setlast)
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02005783 set_no_hlsearch(!hlsearch_on);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784#endif
5785 }
5786 }
5787 }
5788 return viminfo_readline(virp);
5789}
5790
5791 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005792write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 if (get_viminfo_parameter('/') != 0)
5795 {
5796#ifdef FEAT_SEARCH_EXTRA
5797 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5798 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5799#endif
5800 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005801 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
5803}
5804
5805 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005806wvsp_one(
5807 FILE *fp, /* file to write to */
5808 int idx, /* spats[] index */
5809 char *s, /* search pat */
5810 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 if (spats[idx].pat != NULL)
5813 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005814 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 /* off.dir is not stored, it's reset to forward */
5816 fprintf(fp, "%c%c%c%c%ld%s%c",
5817 spats[idx].magic ? 'M' : 'm', /* magic */
5818 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5819 spats[idx].off.line ? 'L' : 'l', /* line offset */
5820 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5821 spats[idx].off.off, /* offset */
5822 last_idx == idx ? "~" : "", /* last used pat */
5823 sc);
5824 viminfo_writestring(fp, spats[idx].pat);
5825 }
5826}
5827#endif /* FEAT_VIMINFO */