blob: 94b1ef1e6bc89b712dadcfc3d4553553060a4377 [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)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100159 emsg(_(e_nopresub));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100161 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 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)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100372 iemsg("did_save_last_search_spat is not zero");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100373 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 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100388 iemsg("did_save_last_search_spat is not one");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100389 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 Moolenaar113e1072019-01-20 15:30:40 +0100479#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100481last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200482{
483#ifdef FEAT_MBYTE
484 return lastc_bytes;
485#else
486 return lastc;
487#endif
488}
489
490 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100491last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200492{
493 return lastcdir == FORWARD;
494}
495
496 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100497last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200498{
499 return last_t_cmd == TRUE;
500}
501
502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100503set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200504{
505 *lastc = c;
506#ifdef FEAT_MBYTE
507 lastc_bytelen = len;
508 if (len)
509 memcpy(lastc_bytes, s, len);
510 else
511 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
512#endif
513}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100514#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200515
516 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100517set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200518{
519 lastcdir = cdir;
520}
521
522 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100523set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200524{
525 last_t_cmd = t_cmd;
526}
527
528 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100529last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530{
531 return spats[last_idx].pat;
532}
533
534/*
535 * Reset search direction to forward. For "gd" and "gD" commands.
536 */
537 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100538reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000541#if defined(FEAT_EVAL)
542 set_vv_searchforward();
543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544}
545
546#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
547/*
548 * Set the last search pattern. For ":let @/ =" and viminfo.
549 * Also set the saved search pattern, so that this works in an autocommand.
550 */
551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100552set_last_search_pat(
553 char_u *s,
554 int idx,
555 int magic,
556 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 vim_free(spats[idx].pat);
559 /* An empty string means that nothing should be matched. */
560 if (*s == NUL)
561 spats[idx].pat = NULL;
562 else
563 spats[idx].pat = vim_strsave(s);
564 spats[idx].magic = magic;
565 spats[idx].no_scs = FALSE;
566 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000567#if defined(FEAT_EVAL)
568 set_vv_searchforward();
569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 spats[idx].off.line = FALSE;
571 spats[idx].off.end = FALSE;
572 spats[idx].off.off = 0;
573 if (setlast)
574 last_idx = idx;
575 if (save_level)
576 {
577 vim_free(saved_spats[idx].pat);
578 saved_spats[idx] = spats[0];
579 if (spats[idx].pat == NULL)
580 saved_spats[idx].pat = NULL;
581 else
582 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaared8bc782018-12-01 21:08:21 +0100583 saved_spats_last_idx = last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 }
585# ifdef FEAT_SEARCH_EXTRA
586 /* If 'hlsearch' set and search pat changed: need redraw. */
587 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000588 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589# endif
590}
591#endif
592
593#ifdef FEAT_SEARCH_EXTRA
594/*
595 * Get a regexp program for the last used search pattern.
596 * This is used for highlighting all matches in a window.
597 * Values returned in regmatch->regprog and regmatch->rmm_ic.
598 */
599 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100600last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 if (spats[last_idx].pat == NULL)
603 {
604 regmatch->regprog = NULL;
605 return;
606 }
607 ++emsg_off; /* So it doesn't beep if bad expr */
608 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
609 --emsg_off;
610}
611#endif
612
613/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100614 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100615 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
616 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 *
618 * if (options & SEARCH_MSG) == 0 don't give any messages
619 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
620 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
621 * if (options & SEARCH_HIS) put search pattern in history
622 * if (options & SEARCH_END) return position at end of match
623 * if (options & SEARCH_START) accept match at pos itself
624 * if (options & SEARCH_KEEP) keep previous search pattern
625 * if (options & SEARCH_FOLD) match only once in a closed fold
626 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100627 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 *
629 * Return FAIL (zero) for failure, non-zero for success.
630 * When FEAT_EVAL is defined, returns the index of the first matching
631 * subpattern plus one; one if there was none.
632 */
633 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100634searchit(
635 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100637 buf_T *buf,
638 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100639 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100640 int dir,
641 char_u *pat,
642 long count,
643 int options,
644 int pat_use, /* which pattern to use when "pat" is empty */
645 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200646 proftime_T *tm UNUSED, /* timeout limit or NULL */
647 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648{
649 int found;
650 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100651 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 regmmatch_T regmatch;
653 char_u *ptr;
654 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000656 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 int loop;
658 pos_T start_pos;
659 int at_first_line;
660 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200661 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 int match_ok;
663 long nmatched;
664 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100665 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000666 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667#ifdef FEAT_SEARCH_EXTRA
668 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#endif
670
671 if (search_regcomp(pat, RE_SEARCH, pat_use,
672 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
673 {
674 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 semsg(_("E383: Invalid search string: %s"), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 return FAIL;
677 }
678
Bram Moolenaar280f1262006-01-30 00:14:18 +0000679 /*
680 * find the string
681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 called_emsg = FALSE;
683 do /* loop for count */
684 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100685 /* When not accepting a match at the start position set "extra_col" to
686 * a non-zero value. Don't do that when starting at MAXCOL, since
687 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200688 if (pos->col == MAXCOL)
689 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100690#ifdef FEAT_MBYTE
691 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200692 else if (has_mbyte
693 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
694 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100695 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100696 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100697 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200698 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100699 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100700 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100701 }
702#endif
703 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200704 start_char_len = 1;
705 if (dir == FORWARD)
706 {
707 if (options & SEARCH_START)
708 extra_col = 0;
709 else
710 extra_col = start_char_len;
711 }
712 else
713 {
714 if (options & SEARCH_START)
715 extra_col = start_char_len;
716 else
717 extra_col = 0;
718 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100719
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 start_pos = *pos; /* remember start pos for detecting no match */
721 found = 0; /* default: not found */
722 at_first_line = TRUE; /* default: start in first line */
723 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
724 {
725 pos->lnum = 1;
726 pos->col = 0;
727 at_first_line = FALSE; /* not in first line now */
728 }
729
730 /*
731 * Start searching in current line, unless searching backwards and
732 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000733 * If we are searching backwards, in column 0, and not including the
734 * current position, gain some efficiency by skipping back a line.
735 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000737 if (dir == BACKWARD && start_pos.col == 0
738 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
740 lnum = pos->lnum - 1;
741 at_first_line = FALSE;
742 }
743 else
744 lnum = pos->lnum;
745
746 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
747 {
748 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
749 lnum += dir, at_first_line = FALSE)
750 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000751 /* Stop after checking "stop_lnum", if it's set. */
752 if (stop_lnum != 0 && (dir == FORWARD
753 ? lnum > stop_lnum : lnum < stop_lnum))
754 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000755#ifdef FEAT_RELTIME
756 /* Stop after passing the "tm" time limit. */
757 if (tm != NULL && profile_passed_limit(tm))
758 break;
759#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000762 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100764 col = at_first_line && (options & SEARCH_COL) ? pos->col
765 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100767 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000768#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200769 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000770#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200771 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000772#endif
773 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200775 if (called_emsg
776#ifdef FEAT_RELTIME
777 || (timed_out != NULL && *timed_out)
778#endif
779 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 break;
781 if (nmatched > 0)
782 {
783 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000784 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000786#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000788#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000789 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000790 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
791 ptr = (char_u *)"";
792 else
793 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795 /*
796 * Forward search in the first line: match should be after
797 * the start position. If not, continue at the end of the
798 * match (this is vi compatible) or on the next char.
799 */
800 if (dir == FORWARD && at_first_line)
801 {
802 match_ok = TRUE;
803 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000804 * When the match starts in a next line it's certainly
805 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * When match lands on a NUL the cursor will be put
807 * one back afterwards, compare with that position,
808 * otherwise "/$" will get stuck on end of line.
809 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000810 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100811 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000812 ? (nmatched == 1
813 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000815 : ((int)matchpos.col
816 - (ptr[matchpos.col] == NUL)
817 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
819 /*
820 * If vi-compatible searching, continue at the end
821 * of the match, otherwise continue one position
822 * forward.
823 */
824 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
825 {
826 if (nmatched > 1)
827 {
828 /* end is in next line, thus no match in
829 * this line */
830 match_ok = FALSE;
831 break;
832 }
833 matchcol = endpos.col;
834 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000835 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 && ptr[matchcol] != NUL)
837 {
838#ifdef FEAT_MBYTE
839 if (has_mbyte)
840 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000841 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else
843#endif
844 ++matchcol;
845 }
846 }
847 else
848 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000849 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (ptr[matchcol] != NUL)
851 {
852#ifdef FEAT_MBYTE
853 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000854 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 + matchcol);
856 else
857#endif
858 ++matchcol;
859 }
860 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200861 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100862 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 if (ptr[matchcol] == NUL
864 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000865 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000866 matchcol,
867#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200868 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000869#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200870 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000871#endif
872 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
874 match_ok = FALSE;
875 break;
876 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000877 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 endpos = regmatch.endpos[0];
879# ifdef FEAT_EVAL
880 submatch = first_submatch(&regmatch);
881# endif
882
883 /* Need to get the line pointer again, a
884 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000885 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 if (!match_ok)
888 continue;
889 }
890 if (dir == BACKWARD)
891 {
892 /*
893 * Now, if there are multiple matches on this line,
894 * we have to get the last one. Or the last one before
895 * the cursor, if we're on that line.
896 * When putting the new cursor at the end, compare
897 * relative to the end of the match.
898 */
899 match_ok = FALSE;
900 for (;;)
901 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000902 /* Remember a position that is before the start
903 * position, we use it if it's the last match in
904 * the line. Always accept a position after
905 * wrapping around. */
906 if (loop
907 || ((options & SEARCH_END)
908 ? (lnum + regmatch.endpos[0].lnum
909 < start_pos.lnum
910 || (lnum + regmatch.endpos[0].lnum
911 == start_pos.lnum
912 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200913 < (int)start_pos.col
914 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000915 : (lnum + regmatch.startpos[0].lnum
916 < start_pos.lnum
917 || (lnum + regmatch.startpos[0].lnum
918 == start_pos.lnum
919 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200920 < (int)start_pos.col
921 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000924 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 endpos = regmatch.endpos[0];
926# ifdef FEAT_EVAL
927 submatch = first_submatch(&regmatch);
928# endif
929 }
930 else
931 break;
932
933 /*
934 * We found a valid match, now check if there is
935 * another one after it.
936 * If vi-compatible searching, continue at the end
937 * of the match, otherwise continue one position
938 * forward.
939 */
940 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
941 {
942 if (nmatched > 1)
943 break;
944 matchcol = endpos.col;
945 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000946 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 && ptr[matchcol] != NUL)
948 {
949#ifdef FEAT_MBYTE
950 if (has_mbyte)
951 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000952 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 else
954#endif
955 ++matchcol;
956 }
957 }
958 else
959 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000960 /* Stop when the match is in a next line. */
961 if (matchpos.lnum > 0)
962 break;
963 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (ptr[matchcol] != NUL)
965 {
966#ifdef FEAT_MBYTE
967 if (has_mbyte)
968 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000969 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 else
971#endif
972 ++matchcol;
973 }
974 }
975 if (ptr[matchcol] == NUL
976 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000977 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000978 matchcol,
979#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200980 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000981#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200982 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000983#endif
984 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100985 {
986#ifdef FEAT_RELTIME
987 /* If the search timed out, we did find a match
988 * but it might be the wrong one, so that's not
989 * OK. */
990 if (timed_out != NULL && *timed_out)
991 match_ok = FALSE;
992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 /* Need to get the line pointer again, a
997 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000998 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 }
1000
1001 /*
1002 * If there is only a match after the cursor, skip
1003 * this match.
1004 */
1005 if (!match_ok)
1006 continue;
1007 }
1008
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001009 /* With the SEARCH_END option move to the last character
1010 * of the match. Don't do it for an empty match, end
1011 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +02001012 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001013 && !(matchpos.lnum == endpos.lnum
1014 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001016 /* For a match in the first column, set the position
1017 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001018 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001019 pos->col = endpos.col;
1020 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001021 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001022 if (pos->lnum > 1) /* just in case */
1023 {
1024 --pos->lnum;
1025 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1026 pos->lnum, FALSE));
1027 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001028 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001029 else
1030 {
1031 --pos->col;
1032#ifdef FEAT_MBYTE
1033 if (has_mbyte
1034 && pos->lnum <= buf->b_ml.ml_line_count)
1035 {
1036 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1037 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1038 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001039#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001040 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001041 if (end_pos != NULL)
1042 {
1043 end_pos->lnum = lnum + matchpos.lnum;
1044 end_pos->col = matchpos.col;
1045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 }
1047 else
1048 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001049 pos->lnum = lnum + matchpos.lnum;
1050 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001051 if (end_pos != NULL)
1052 {
1053 end_pos->lnum = lnum + endpos.lnum;
1054 end_pos->col = endpos.col;
1055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057#ifdef FEAT_VIRTUALEDIT
1058 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001059 if (end_pos != NULL)
1060 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001063 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001066 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 search_match_endcol = endpos.col;
1068 break;
1069 }
1070 line_breakcheck(); /* stop if ctrl-C typed */
1071 if (got_int)
1072 break;
1073
1074#ifdef FEAT_SEARCH_EXTRA
1075 /* Cancel searching if a character was typed. Used for
1076 * 'incsearch'. Don't check too often, that would slowdown
1077 * searching too much. */
1078 if ((options & SEARCH_PEEK)
1079 && ((lnum - pos->lnum) & 0x3f) == 0
1080 && char_avail())
1081 {
1082 break_loop = TRUE;
1083 break;
1084 }
1085#endif
1086
1087 if (loop && lnum == start_pos.lnum)
1088 break; /* if second loop, stop where started */
1089 }
1090 at_first_line = FALSE;
1091
1092 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001093 * Stop the search if wrapscan isn't set, "stop_lnum" is
1094 * specified, after an interrupt, after a match and after looping
1095 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001097 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001098#ifdef FEAT_RELTIME
1099 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001100#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001101#ifdef FEAT_SEARCH_EXTRA
1102 || break_loop
1103#endif
1104 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 break;
1106
1107 /*
1108 * If 'wrapscan' is set we continue at the other end of the file.
1109 * If 'shortmess' does not contain 's', we give a message.
1110 * This message is also remembered in keep_msg for when the screen
1111 * is redrawn. The keep_msg is cleared whenever another message is
1112 * written.
1113 */
1114 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001118 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1119 give_warning((char_u *)_(dir == BACKWARD
1120 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001122 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001123#ifdef FEAT_RELTIME
1124 || (timed_out != NULL && *timed_out)
1125#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001126#ifdef FEAT_SEARCH_EXTRA
1127 || break_loop
1128#endif
1129 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 break;
1131 }
1132 while (--count > 0 && found); /* stop after count matches or no match */
1133
Bram Moolenaar473de612013-06-08 18:19:48 +02001134 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
Bram Moolenaar280f1262006-01-30 00:14:18 +00001136 called_emsg |= save_called_emsg;
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 if (!found) /* did not find it */
1139 {
1140 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001141 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1143 {
1144 if (p_ws)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001145 semsg(_(e_patnotf2), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 else if (lnum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001147 semsg(_("E384: search hit TOP without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 mr_pattern);
1149 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 semsg(_("E385: search hit BOTTOM without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 mr_pattern);
1152 }
1153 return FAIL;
1154 }
1155
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001156 /* A pattern like "\n\zs" may go past the last line. */
1157 if (pos->lnum > buf->b_ml.ml_line_count)
1158 {
1159 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001160 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001161 if (pos->col > 0)
1162 --pos->col;
1163 }
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 return submatch + 1;
1166}
1167
1168#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001169 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001170set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001171{
1172 spats[0].off.dir = cdir;
1173}
1174
1175 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001176set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001177{
1178 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1179}
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181/*
1182 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001183 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 */
1185 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001186first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 int submatch;
1189
1190 for (submatch = 1; ; ++submatch)
1191 {
1192 if (rp->startpos[submatch].lnum >= 0)
1193 break;
1194 if (submatch == 9)
1195 {
1196 submatch = 0;
1197 break;
1198 }
1199 }
1200 return submatch;
1201}
1202#endif
1203
1204/*
1205 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001206 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 * If 'dirc' is 0: use previous dir.
1208 * If 'pat' is NULL or empty : use previous string.
1209 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1210 * If 'options & SEARCH_ECHO': echo the search command and handle options
1211 * If 'options & SEARCH_MSG' : may give error message
1212 * If 'options & SEARCH_OPT' : interpret optional flags
1213 * If 'options & SEARCH_HIS' : put search pattern in history
1214 * If 'options & SEARCH_NOOF': don't add offset to position
1215 * If 'options & SEARCH_MARK': set previous context mark
1216 * If 'options & SEARCH_KEEP': keep previous search pattern
1217 * If 'options & SEARCH_START': accept match at curpos itself
1218 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1219 *
1220 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1221 * makes the movement linewise without moving the match position.
1222 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001223 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 */
1225 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001226do_search(
1227 oparg_T *oap, /* can be NULL */
1228 int dirc, /* '/' or '?' */
1229 char_u *pat,
1230 long count,
1231 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001232 proftime_T *tm, /* timeout limit or NULL */
1233 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234{
1235 pos_T pos; /* position of the last match */
1236 char_u *searchstr;
1237 struct soffset old_off;
1238 int retval; /* Return value */
1239 char_u *p;
1240 long c;
1241 char_u *dircp;
1242 char_u *strcopy = NULL;
1243 char_u *ps;
1244
1245 /*
1246 * A line offset is not remembered, this is vi compatible.
1247 */
1248 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1249 {
1250 spats[0].off.line = FALSE;
1251 spats[0].off.off = 0;
1252 }
1253
1254 /*
1255 * Save the values for when (options & SEARCH_KEEP) is used.
1256 * (there is no "if ()" around this because gcc wants them initialized)
1257 */
1258 old_off = spats[0].off;
1259
1260 pos = curwin->w_cursor; /* start searching at the cursor position */
1261
1262 /*
1263 * Find out the direction of the search.
1264 */
1265 if (dirc == 0)
1266 dirc = spats[0].off.dir;
1267 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001270#if defined(FEAT_EVAL)
1271 set_vv_searchforward();
1272#endif
1273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 if (options & SEARCH_REV)
1275 {
1276#ifdef WIN32
1277 /* There is a bug in the Visual C++ 2.2 compiler which means that
1278 * dirc always ends up being '/' */
1279 dirc = (dirc == '/') ? '?' : '/';
1280#else
1281 if (dirc == '/')
1282 dirc = '?';
1283 else
1284 dirc = '/';
1285#endif
1286 }
1287
1288#ifdef FEAT_FOLDING
1289 /* If the cursor is in a closed fold, don't find another match in the same
1290 * fold. */
1291 if (dirc == '/')
1292 {
1293 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1294 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1295 }
1296 else
1297 {
1298 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1299 pos.col = 0;
1300 }
1301#endif
1302
1303#ifdef FEAT_SEARCH_EXTRA
1304 /*
1305 * Turn 'hlsearch' highlighting back on.
1306 */
1307 if (no_hlsearch && !(options & SEARCH_KEEP))
1308 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001309 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001310 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312#endif
1313
1314 /*
1315 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1316 */
1317 for (;;)
1318 {
1319 searchstr = pat;
1320 dircp = NULL;
1321 /* use previous pattern */
1322 if (pat == NULL || *pat == NUL || *pat == dirc)
1323 {
1324 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1325 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001326 searchstr = spats[RE_SUBST].pat;
1327 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001329 emsg(_(e_noprevre));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001330 retval = 0;
1331 goto end_do_search;
1332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001334 else
1335 {
1336 /* make search_regcomp() use spats[RE_SEARCH].pat */
1337 searchstr = (char_u *)"";
1338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340
1341 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1342 {
1343 /*
1344 * Find end of regular expression.
1345 * If there is a matching '/' or '?', toss it.
1346 */
1347 ps = strcopy;
1348 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1349 if (strcopy != ps)
1350 {
1351 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001352 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 pat = strcopy;
1354 searchstr = strcopy;
1355 }
1356 if (*p == dirc)
1357 {
1358 dircp = p; /* remember where we put the NUL */
1359 *p++ = NUL;
1360 }
1361 spats[0].off.line = FALSE;
1362 spats[0].off.end = FALSE;
1363 spats[0].off.off = 0;
1364 /*
1365 * Check for a line offset or a character offset.
1366 * For get_address (echo off) we don't check for a character
1367 * offset, because it is meaningless and the 's' could be a
1368 * substitute command.
1369 */
1370 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1371 spats[0].off.line = TRUE;
1372 else if ((options & SEARCH_OPT) &&
1373 (*p == 'e' || *p == 's' || *p == 'b'))
1374 {
1375 if (*p == 'e') /* end */
1376 spats[0].off.end = SEARCH_END;
1377 ++p;
1378 }
1379 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1380 {
1381 /* 'nr' or '+nr' or '-nr' */
1382 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1383 spats[0].off.off = atol((char *)p);
1384 else if (*p == '-') /* single '-' */
1385 spats[0].off.off = -1;
1386 else /* single '+' */
1387 spats[0].off.off = 1;
1388 ++p;
1389 while (VIM_ISDIGIT(*p)) /* skip number */
1390 ++p;
1391 }
1392
1393 /* compute length of search command for get_address() */
1394 searchcmdlen += (int)(p - pat);
1395
1396 pat = p; /* put pat after search command */
1397 }
1398
1399 if ((options & SEARCH_ECHO) && messaging()
1400 && !cmd_silent && msg_silent == 0)
1401 {
1402 char_u *msgbuf;
1403 char_u *trunc;
1404
1405 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001406 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 else
1408 p = searchstr;
1409 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1410 if (msgbuf != NULL)
1411 {
1412 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001413#ifdef FEAT_MBYTE
1414 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1415 {
1416 /* Use a space to draw the composing char on. */
1417 msgbuf[1] = ' ';
1418 STRCPY(msgbuf + 2, p);
1419 }
1420 else
1421#endif
1422 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1424 {
1425 p = msgbuf + STRLEN(msgbuf);
1426 *p++ = dirc;
1427 if (spats[0].off.end)
1428 *p++ = 'e';
1429 else if (!spats[0].off.line)
1430 *p++ = 's';
1431 if (spats[0].off.off > 0 || spats[0].off.line)
1432 *p++ = '+';
1433 if (spats[0].off.off != 0 || spats[0].off.line)
1434 sprintf((char *)p, "%ld", spats[0].off.off);
1435 else
1436 *p = NUL;
1437 }
1438
1439 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001440 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442#ifdef FEAT_RIGHTLEFT
1443 /* The search pattern could be shown on the right in rightleft
1444 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1445 * it would be blanked out again very soon. Show it on the
1446 * left, but do reverse the text. */
1447 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1448 {
1449 char_u *r;
1450
1451 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1452 if (r != NULL)
1453 {
1454 vim_free(trunc);
1455 trunc = r;
1456 }
1457 }
1458#endif
1459 if (trunc != NULL)
1460 {
1461 msg_outtrans(trunc);
1462 vim_free(trunc);
1463 }
1464 else
1465 msg_outtrans(msgbuf);
1466 msg_clr_eos();
1467 msg_check();
1468 vim_free(msgbuf);
1469
1470 gotocmdline(FALSE);
1471 out_flush();
1472 msg_nowait = TRUE; /* don't wait for this message */
1473 }
1474 }
1475
1476 /*
1477 * If there is a character offset, subtract it from the current
1478 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001479 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 * This is not done for a line offset, because then we would not be vi
1481 * compatible.
1482 */
Bram Moolenaared203462004-06-16 11:19:22 +00001483 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 if (spats[0].off.off > 0)
1486 {
1487 for (c = spats[0].off.off; c; --c)
1488 if (decl(&pos) == -1)
1489 break;
1490 if (c) /* at start of buffer */
1491 {
1492 pos.lnum = 0; /* allow lnum == 0 here */
1493 pos.col = MAXCOL;
1494 }
1495 }
1496 else
1497 {
1498 for (c = spats[0].off.off; c; ++c)
1499 if (incl(&pos) == -1)
1500 break;
1501 if (c) /* at end of buffer */
1502 {
1503 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1504 pos.col = 0;
1505 }
1506 }
1507 }
1508
1509#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1510 if (p_altkeymap && curwin->w_p_rl)
1511 lrFswap(searchstr,0);
1512#endif
1513
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001514 c = searchit(curwin, curbuf, &pos, NULL, dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 searchstr, count, spats[0].off.end + (options &
1516 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1517 + SEARCH_MSG + SEARCH_START
1518 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001519 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
1521 if (dircp != NULL)
1522 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1523 if (c == FAIL)
1524 {
1525 retval = 0;
1526 goto end_do_search;
1527 }
1528 if (spats[0].off.end && oap != NULL)
1529 oap->inclusive = TRUE; /* 'e' includes last character */
1530
1531 retval = 1; /* pattern found */
1532
1533 /*
1534 * Add character and/or line offset
1535 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001536 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 {
1538 if (spats[0].off.line) /* Add the offset to the line number. */
1539 {
1540 c = pos.lnum + spats[0].off.off;
1541 if (c < 1)
1542 pos.lnum = 1;
1543 else if (c > curbuf->b_ml.ml_line_count)
1544 pos.lnum = curbuf->b_ml.ml_line_count;
1545 else
1546 pos.lnum = c;
1547 pos.col = 0;
1548
1549 retval = 2; /* pattern found, line offset added */
1550 }
Bram Moolenaared203462004-06-16 11:19:22 +00001551 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
1553 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554 c = spats[0].off.off;
1555 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (incl(&pos) == -1)
1559 break;
1560 }
1561 /* to the left, check for start of file */
1562 else
1563 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564 while (c++ < 0)
1565 if (decl(&pos) == -1)
1566 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568 }
1569 }
1570
1571 /*
1572 * The search command can be followed by a ';' to do another search.
1573 * For example: "/pat/;/foo/+3;?bar"
1574 * This is like doing another search command, except:
1575 * - The remembered direction '/' or '?' is from the first search.
1576 * - When an error happens the cursor isn't moved at all.
1577 * Don't do this when called by get_address() (it handles ';' itself).
1578 */
1579 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1580 break;
1581
1582 dirc = *++pat;
1583 if (dirc != '?' && dirc != '/')
1584 {
1585 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001586 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 goto end_do_search;
1588 }
1589 ++pat;
1590 }
1591
1592 if (options & SEARCH_MARK)
1593 setpcmark();
1594 curwin->w_cursor = pos;
1595 curwin->w_set_curswant = TRUE;
1596
1597end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001598 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 spats[0].off = old_off;
1600 vim_free(strcopy);
1601
1602 return retval;
1603}
1604
1605#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1606/*
1607 * search_for_exact_line(buf, pos, dir, pat)
1608 *
1609 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001610 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001612 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 * Return OK for success, or FAIL if no line found.
1614 */
1615 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001616search_for_exact_line(
1617 buf_T *buf,
1618 pos_T *pos,
1619 int dir,
1620 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621{
1622 linenr_T start = 0;
1623 char_u *ptr;
1624 char_u *p;
1625
1626 if (buf->b_ml.ml_line_count == 0)
1627 return FAIL;
1628 for (;;)
1629 {
1630 pos->lnum += dir;
1631 if (pos->lnum < 1)
1632 {
1633 if (p_ws)
1634 {
1635 pos->lnum = buf->b_ml.ml_line_count;
1636 if (!shortmess(SHM_SEARCH))
1637 give_warning((char_u *)_(top_bot_msg), TRUE);
1638 }
1639 else
1640 {
1641 pos->lnum = 1;
1642 break;
1643 }
1644 }
1645 else if (pos->lnum > buf->b_ml.ml_line_count)
1646 {
1647 if (p_ws)
1648 {
1649 pos->lnum = 1;
1650 if (!shortmess(SHM_SEARCH))
1651 give_warning((char_u *)_(bot_top_msg), TRUE);
1652 }
1653 else
1654 {
1655 pos->lnum = 1;
1656 break;
1657 }
1658 }
1659 if (pos->lnum == start)
1660 break;
1661 if (start == 0)
1662 start = pos->lnum;
1663 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1664 p = skipwhite(ptr);
1665 pos->col = (colnr_T) (p - ptr);
1666
1667 /* when adding lines the matching line may be empty but it is not
1668 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001669 if ((compl_cont_status & CONT_ADDING)
1670 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 {
1672 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1673 return OK;
1674 }
1675 else if (*p != NUL) /* ignore empty lines */
1676 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001677 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1678 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 return OK;
1680 }
1681 }
1682 return FAIL;
1683}
1684#endif /* FEAT_INS_EXPAND */
1685
1686/*
1687 * Character Searches
1688 */
1689
1690/*
1691 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1692 * position of the character, otherwise move to just before the char.
1693 * Do this "cap->count1" times.
1694 * Return FAIL or OK.
1695 */
1696 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001697searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 int c = cap->nchar; /* char to search for */
1700 int dir = cap->arg; /* TRUE for searching forward */
1701 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 int col;
1703 char_u *p;
1704 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001705 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
1707 if (c != NUL) /* normal search: remember args for repeat */
1708 {
1709 if (!KeyStuffed) /* don't remember when redoing */
1710 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001711 *lastc = c;
1712 set_csearch_direction(dir);
1713 set_csearch_until(t_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001715 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (cap->ncharC1 != 0)
1717 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001718 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1719 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001721 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1722 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 }
1724#endif
1725 }
1726 }
1727 else /* repeat previous search */
1728 {
Bram Moolenaar454709b2017-03-12 16:37:14 +01001729 if (*lastc == NUL
1730#ifdef FEAT_MBYTE
1731 && lastc_bytelen == 1
1732#endif
1733 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 return FAIL;
1735 if (dir) /* repeat in opposite direction */
1736 dir = -lastcdir;
1737 else
1738 dir = lastcdir;
1739 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001740 c = *lastc;
1741 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001742
1743 /* Force a move of at least one char, so ";" and "," will move the
1744 * cursor, even if the cursor is right in front of char we are looking
1745 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001746 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001747 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001750 if (dir == BACKWARD)
1751 cap->oap->inclusive = FALSE;
1752 else
1753 cap->oap->inclusive = TRUE;
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 p = ml_get_curline();
1756 col = curwin->w_cursor.col;
1757 len = (int)STRLEN(p);
1758
1759 while (count--)
1760 {
1761#ifdef FEAT_MBYTE
1762 if (has_mbyte)
1763 {
1764 for (;;)
1765 {
1766 if (dir > 0)
1767 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001768 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (col >= len)
1770 return FAIL;
1771 }
1772 else
1773 {
1774 if (col == 0)
1775 return FAIL;
1776 col -= (*mb_head_off)(p, p + col - 1) + 1;
1777 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001778 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001780 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 break;
1782 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001783 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001784 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001785 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001786 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 }
1789 else
1790#endif
1791 {
1792 for (;;)
1793 {
1794 if ((col += dir) < 0 || col >= len)
1795 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001796 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001798 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 }
1800 }
1801 }
1802
1803 if (t_cmd)
1804 {
1805 /* backup to before the character (possibly double-byte) */
1806 col -= dir;
1807#ifdef FEAT_MBYTE
1808 if (has_mbyte)
1809 {
1810 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001811 /* Landed on the search char which is lastc_bytelen long */
1812 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
1814 /* To previous char, which may be multi-byte. */
1815 col -= (*mb_head_off)(p, p + col);
1816 }
1817#endif
1818 }
1819 curwin->w_cursor.col = col;
1820
1821 return OK;
1822}
1823
1824/*
1825 * "Other" Searches
1826 */
1827
1828/*
1829 * findmatch - find the matching paren or brace
1830 *
1831 * Improvement over vi: Braces inside quotes are ignored.
1832 */
1833 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001834findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 return findmatchlimit(oap, initc, 0, 0);
1837}
1838
1839/*
1840 * Return TRUE if the character before "linep[col]" equals "ch".
1841 * Return FALSE if "col" is zero.
1842 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1843 * is NULL.
1844 * Handles multibyte string correctly.
1845 */
1846 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001847check_prevcol(
1848 char_u *linep,
1849 int col,
1850 int ch,
1851 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852{
1853 --col;
1854#ifdef FEAT_MBYTE
1855 if (col > 0 && has_mbyte)
1856 col -= (*mb_head_off)(linep, linep + col);
1857#endif
1858 if (prevcol)
1859 *prevcol = col;
1860 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1861}
1862
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001863/*
1864 * Raw string start is found at linep[startpos.col - 1].
1865 * Return TRUE if the matching end can be found between startpos and endpos.
1866 */
1867 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001868find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001869{
1870 char_u *p;
1871 char_u *delim_copy;
1872 size_t delim_len;
1873 linenr_T lnum;
1874 int found = FALSE;
1875
1876 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1877 ;
1878 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001879 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001880 if (delim_copy == NULL)
1881 return FALSE;
1882 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1883 {
1884 char_u *line = ml_get(lnum);
1885
1886 for (p = line + (lnum == startpos->lnum
1887 ? startpos->col + 1 : 0); *p; ++p)
1888 {
1889 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1890 break;
1891 if (*p == ')' && p[delim_len + 1] == '"'
1892 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1893 {
1894 found = TRUE;
1895 break;
1896 }
1897 }
1898 if (found)
1899 break;
1900 }
1901 vim_free(delim_copy);
1902 return found;
1903}
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905/*
1906 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001907 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1908 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 *
1910 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001911 * character at or after the cursor. Special values:
1912 * '*' look for C-style comment / *
1913 * '/' look for C-style comment / *, ignoring comment-end
1914 * '#' look for preprocessor directives
1915 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 *
1917 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1918 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1919 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1920 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001921 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001922 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001923 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 */
1925
1926 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001927findmatchlimit(
1928 oparg_T *oap,
1929 int initc,
1930 int flags,
1931 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
1933 static pos_T pos; /* current search position */
1934 int findc = 0; /* matching brace */
1935 int c;
1936 int count = 0; /* cumulative number of braces */
1937 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001938 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 int inquote = FALSE; /* TRUE when inside quotes */
1940 char_u *linep; /* pointer to current line */
1941 char_u *ptr;
1942 int do_quotes; /* check for quotes in current line */
1943 int at_start; /* do_quotes value at start position */
1944 int hash_dir = 0; /* Direction searched for # things */
1945 int comment_dir = 0; /* Direction searched for comments */
1946 pos_T match_pos; /* Where last slash-star was found */
1947 int start_in_quotes; /* start position is in quotes */
1948 int traveled = 0; /* how far we've searched so far */
1949 int ignore_cend = FALSE; /* ignore comment end */
1950 int cpo_match; /* vi compatible matching */
1951 int cpo_bsl; /* don't recognize backslashes */
1952 int match_escaped = 0; /* search for escaped match */
1953 int dir; /* Direction to search */
1954 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001955#ifdef FEAT_LISP
1956 int lispcomm = FALSE; /* inside of Lisp-style comment */
1957 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1958#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001961#ifdef FEAT_VIRTUALEDIT
1962 pos.coladd = 0;
1963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 linep = ml_get(pos.lnum);
1965
1966 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1967 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1968
1969 /* Direction to search when initc is '/', '*' or '#' */
1970 if (flags & FM_BACKWARD)
1971 dir = BACKWARD;
1972 else if (flags & FM_FORWARD)
1973 dir = FORWARD;
1974 else
1975 dir = 0;
1976
1977 /*
1978 * if initc given, look in the table for the matching character
1979 * '/' and '*' are special cases: look for start or end of comment.
1980 * When '/' is used, we ignore running backwards into an star-slash, for
1981 * "[*" command, we just want to find any comment.
1982 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001983 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 comment_dir = dir;
1986 if (initc == '/')
1987 ignore_cend = TRUE;
1988 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001989 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 initc = NUL;
1991 }
1992 else if (initc != '#' && initc != NUL)
1993 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001994 find_mps_values(&initc, &findc, &backwards, TRUE);
1995 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 return NULL;
1997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 else
1999 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002000 /*
2001 * Either initc is '#', or no initc was given and we need to look
2002 * under the cursor.
2003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 if (initc == '#')
2005 {
2006 hash_dir = dir;
2007 }
2008 else
2009 {
2010 /*
2011 * initc was not given, must look for something to match under
2012 * or near the cursor.
2013 * Only check for special things when 'cpo' doesn't have '%'.
2014 */
2015 if (!cpo_match)
2016 {
2017 /* Are we before or at #if, #else etc.? */
2018 ptr = skipwhite(linep);
2019 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2020 {
2021 ptr = skipwhite(ptr + 1);
2022 if ( STRNCMP(ptr, "if", 2) == 0
2023 || STRNCMP(ptr, "endif", 5) == 0
2024 || STRNCMP(ptr, "el", 2) == 0)
2025 hash_dir = 1;
2026 }
2027
2028 /* Are we on a comment? */
2029 else if (linep[pos.col] == '/')
2030 {
2031 if (linep[pos.col + 1] == '*')
2032 {
2033 comment_dir = FORWARD;
2034 backwards = FALSE;
2035 pos.col++;
2036 }
2037 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2038 {
2039 comment_dir = BACKWARD;
2040 backwards = TRUE;
2041 pos.col--;
2042 }
2043 }
2044 else if (linep[pos.col] == '*')
2045 {
2046 if (linep[pos.col + 1] == '/')
2047 {
2048 comment_dir = BACKWARD;
2049 backwards = TRUE;
2050 }
2051 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2052 {
2053 comment_dir = FORWARD;
2054 backwards = FALSE;
2055 }
2056 }
2057 }
2058
2059 /*
2060 * If we are not on a comment or the # at the start of a line, then
2061 * look for brace anywhere on this line after the cursor.
2062 */
2063 if (!hash_dir && !comment_dir)
2064 {
2065 /*
2066 * Find the brace under or after the cursor.
2067 * If beyond the end of the line, use the last character in
2068 * the line.
2069 */
2070 if (linep[pos.col] == NUL && pos.col)
2071 --pos.col;
2072 for (;;)
2073 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002074 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 if (initc == NUL)
2076 break;
2077
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002078 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (findc)
2080 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002081 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 if (!findc)
2084 {
2085 /* no brace in the line, maybe use " #if" then */
2086 if (!cpo_match && *skipwhite(linep) == '#')
2087 hash_dir = 1;
2088 else
2089 return NULL;
2090 }
2091 else if (!cpo_bsl)
2092 {
2093 int col, bslcnt = 0;
2094
2095 /* Set "match_escaped" if there are an odd number of
2096 * backslashes. */
2097 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2098 bslcnt++;
2099 match_escaped = (bslcnt & 1);
2100 }
2101 }
2102 }
2103 if (hash_dir)
2104 {
2105 /*
2106 * Look for matching #if, #else, #elif, or #endif
2107 */
2108 if (oap != NULL)
2109 oap->motion_type = MLINE; /* Linewise for this case only */
2110 if (initc != '#')
2111 {
2112 ptr = skipwhite(skipwhite(linep) + 1);
2113 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2114 hash_dir = 1;
2115 else if (STRNCMP(ptr, "endif", 5) == 0)
2116 hash_dir = -1;
2117 else
2118 return NULL;
2119 }
2120 pos.col = 0;
2121 while (!got_int)
2122 {
2123 if (hash_dir > 0)
2124 {
2125 if (pos.lnum == curbuf->b_ml.ml_line_count)
2126 break;
2127 }
2128 else if (pos.lnum == 1)
2129 break;
2130 pos.lnum += hash_dir;
2131 linep = ml_get(pos.lnum);
2132 line_breakcheck(); /* check for CTRL-C typed */
2133 ptr = skipwhite(linep);
2134 if (*ptr != '#')
2135 continue;
2136 pos.col = (colnr_T) (ptr - linep);
2137 ptr = skipwhite(ptr + 1);
2138 if (hash_dir > 0)
2139 {
2140 if (STRNCMP(ptr, "if", 2) == 0)
2141 count++;
2142 else if (STRNCMP(ptr, "el", 2) == 0)
2143 {
2144 if (count == 0)
2145 return &pos;
2146 }
2147 else if (STRNCMP(ptr, "endif", 5) == 0)
2148 {
2149 if (count == 0)
2150 return &pos;
2151 count--;
2152 }
2153 }
2154 else
2155 {
2156 if (STRNCMP(ptr, "if", 2) == 0)
2157 {
2158 if (count == 0)
2159 return &pos;
2160 count--;
2161 }
2162 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2163 {
2164 if (count == 0)
2165 return &pos;
2166 }
2167 else if (STRNCMP(ptr, "endif", 5) == 0)
2168 count++;
2169 }
2170 }
2171 return NULL;
2172 }
2173 }
2174
2175#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002176 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 * paren/brace in the other direction. */
2178 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2179 backwards = !backwards;
2180#endif
2181
2182 do_quotes = -1;
2183 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002184 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002187 if ((backwards && comment_dir)
2188#ifdef FEAT_LISP
2189 || lisp
2190#endif
2191 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002193#ifdef FEAT_LISP
2194 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2195 lispcomm = TRUE; /* find match inside this comment */
2196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 while (!got_int)
2198 {
2199 /*
2200 * Go to the next position, forward or backward. We could use
2201 * inc() and dec() here, but that is much slower
2202 */
2203 if (backwards)
2204 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002205#ifdef FEAT_LISP
2206 /* char to match is inside of comment, don't search outside */
2207 if (lispcomm && pos.col < (colnr_T)comment_col)
2208 break;
2209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (pos.col == 0) /* at start of line, go to prev. one */
2211 {
2212 if (pos.lnum == 1) /* start of file */
2213 break;
2214 --pos.lnum;
2215
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002216 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 break;
2218
2219 linep = ml_get(pos.lnum);
2220 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2221 do_quotes = -1;
2222 line_breakcheck();
2223
2224 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002225 if (comment_dir
2226#ifdef FEAT_LISP
2227 || lisp
2228#endif
2229 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002231#ifdef FEAT_LISP
2232 /* skip comment */
2233 if (lisp && comment_col != MAXCOL)
2234 pos.col = comment_col;
2235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237 else
2238 {
2239 --pos.col;
2240#ifdef FEAT_MBYTE
2241 if (has_mbyte)
2242 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2243#endif
2244 }
2245 }
2246 else /* forward search */
2247 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002248 if (linep[pos.col] == NUL
2249 /* at end of line, go to next one */
2250#ifdef FEAT_LISP
2251 /* don't search for match in comment */
2252 || (lisp && comment_col != MAXCOL
2253 && pos.col == (colnr_T)comment_col)
2254#endif
2255 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002257 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2258#ifdef FEAT_LISP
2259 /* line is exhausted and comment with it,
2260 * don't search for match in code */
2261 || lispcomm
2262#endif
2263 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 break;
2265 ++pos.lnum;
2266
2267 if (maxtravel && traveled++ > maxtravel)
2268 break;
2269
2270 linep = ml_get(pos.lnum);
2271 pos.col = 0;
2272 do_quotes = -1;
2273 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002274#ifdef FEAT_LISP
2275 if (lisp) /* find comment pos in new line */
2276 comment_col = check_linecomment(linep);
2277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279 else
2280 {
2281#ifdef FEAT_MBYTE
2282 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002283 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 else
2285#endif
2286 ++pos.col;
2287 }
2288 }
2289
2290 /*
2291 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2292 */
2293 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2294 (linep[0] == '{' || linep[0] == '}'))
2295 {
2296 if (linep[0] == findc && count == 0) /* match! */
2297 return &pos;
2298 break; /* out of scope */
2299 }
2300
2301 if (comment_dir)
2302 {
2303 /* Note: comments do not nest, and we ignore quotes in them */
2304 /* TODO: ignore comment brackets inside strings */
2305 if (comment_dir == FORWARD)
2306 {
2307 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2308 {
2309 pos.col++;
2310 return &pos;
2311 }
2312 }
2313 else /* Searching backwards */
2314 {
2315 /*
2316 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002317 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
2319 if (pos.col == 0)
2320 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002321 else if (raw_string)
2322 {
2323 if (linep[pos.col - 1] == 'R'
2324 && linep[pos.col] == '"'
2325 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2326 {
2327 /* Possible start of raw string. Now that we have the
2328 * delimiter we can check if it ends before where we
2329 * started searching, or before the previously found
2330 * raw string start. */
2331 if (!find_rawstring_end(linep, &pos,
2332 count > 0 ? &match_pos : &curwin->w_cursor))
2333 {
2334 count++;
2335 match_pos = pos;
2336 match_pos.col--;
2337 }
2338 linep = ml_get(pos.lnum); /* may have been released */
2339 }
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 else if ( linep[pos.col - 1] == '/'
2342 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002343 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 && (int)pos.col < comment_col)
2345 {
2346 count++;
2347 match_pos = pos;
2348 match_pos.col--;
2349 }
2350 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2351 {
2352 if (count > 0)
2353 pos = match_pos;
2354 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2355 && (int)pos.col <= comment_col)
2356 pos.col -= 2;
2357 else if (ignore_cend)
2358 continue;
2359 else
2360 return NULL;
2361 return &pos;
2362 }
2363 }
2364 continue;
2365 }
2366
2367 /*
2368 * If smart matching ('cpoptions' does not contain '%'), braces inside
2369 * of quotes are ignored, but only if there is an even number of
2370 * quotes in the line.
2371 */
2372 if (cpo_match)
2373 do_quotes = 0;
2374 else if (do_quotes == -1)
2375 {
2376 /*
2377 * Count the number of quotes in the line, skipping \" and '"'.
2378 * Watch out for "\\".
2379 */
2380 at_start = do_quotes;
2381 for (ptr = linep; *ptr; ++ptr)
2382 {
2383 if (ptr == linep + pos.col + backwards)
2384 at_start = (do_quotes & 1);
2385 if (*ptr == '"'
2386 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2387 ++do_quotes;
2388 if (*ptr == '\\' && ptr[1] != NUL)
2389 ++ptr;
2390 }
2391 do_quotes &= 1; /* result is 1 with even number of quotes */
2392
2393 /*
2394 * If we find an uneven count, check current line and previous
2395 * one for a '\' at the end.
2396 */
2397 if (!do_quotes)
2398 {
2399 inquote = FALSE;
2400 if (ptr[-1] == '\\')
2401 {
2402 do_quotes = 1;
2403 if (start_in_quotes == MAYBE)
2404 {
2405 /* Do we need to use at_start here? */
2406 inquote = TRUE;
2407 start_in_quotes = TRUE;
2408 }
2409 else if (backwards)
2410 inquote = TRUE;
2411 }
2412 if (pos.lnum > 1)
2413 {
2414 ptr = ml_get(pos.lnum - 1);
2415 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2416 {
2417 do_quotes = 1;
2418 if (start_in_quotes == MAYBE)
2419 {
2420 inquote = at_start;
2421 if (inquote)
2422 start_in_quotes = TRUE;
2423 }
2424 else if (!backwards)
2425 inquote = TRUE;
2426 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002427
2428 /* ml_get() only keeps one line, need to get linep again */
2429 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 }
2432 }
2433 if (start_in_quotes == MAYBE)
2434 start_in_quotes = FALSE;
2435
2436 /*
2437 * If 'smartmatch' is set:
2438 * Things inside quotes are ignored by setting 'inquote'. If we
2439 * find a quote without a preceding '\' invert 'inquote'. At the
2440 * end of a line not ending in '\' we reset 'inquote'.
2441 *
2442 * In lines with an uneven number of quotes (without preceding '\')
2443 * we do not know which part to ignore. Therefore we only set
2444 * inquote if the number of quotes in a line is even, unless this
2445 * line or the previous one ends in a '\'. Complicated, isn't it?
2446 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002447 c = PTR2CHAR(linep + pos.col);
2448 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 case NUL:
2451 /* at end of line without trailing backslash, reset inquote */
2452 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2453 {
2454 inquote = FALSE;
2455 start_in_quotes = FALSE;
2456 }
2457 break;
2458
2459 case '"':
2460 /* a quote that is preceded with an odd number of backslashes is
2461 * ignored */
2462 if (do_quotes)
2463 {
2464 int col;
2465
2466 for (col = pos.col - 1; col >= 0; --col)
2467 if (linep[col] != '\\')
2468 break;
2469 if ((((int)pos.col - 1 - col) & 1) == 0)
2470 {
2471 inquote = !inquote;
2472 start_in_quotes = FALSE;
2473 }
2474 }
2475 break;
2476
2477 /*
2478 * If smart matching ('cpoptions' does not contain '%'):
2479 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2480 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2481 * skipped, there is never a brace in them.
2482 * Ignore this when finding matches for `'.
2483 */
2484 case '\'':
2485 if (!cpo_match && initc != '\'' && findc != '\'')
2486 {
2487 if (backwards)
2488 {
2489 if (pos.col > 1)
2490 {
2491 if (linep[pos.col - 2] == '\'')
2492 {
2493 pos.col -= 2;
2494 break;
2495 }
2496 else if (linep[pos.col - 2] == '\\' &&
2497 pos.col > 2 && linep[pos.col - 3] == '\'')
2498 {
2499 pos.col -= 3;
2500 break;
2501 }
2502 }
2503 }
2504 else if (linep[pos.col + 1]) /* forward search */
2505 {
2506 if (linep[pos.col + 1] == '\\' &&
2507 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2508 {
2509 pos.col += 3;
2510 break;
2511 }
2512 else if (linep[pos.col + 2] == '\'')
2513 {
2514 pos.col += 2;
2515 break;
2516 }
2517 }
2518 }
2519 /* FALLTHROUGH */
2520
2521 default:
2522#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002523 /*
2524 * For Lisp skip over backslashed (), {} and [].
2525 * (actually, we skip #\( et al)
2526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (curbuf->b_p_lisp
2528 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002529 && pos.col > 1
2530 && check_prevcol(linep, pos.col, '\\', NULL)
2531 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 break;
2533#endif
2534
2535 /* Check for match outside of quotes, and inside of
2536 * quotes when the start is also inside of quotes. */
2537 if ((!inquote || start_in_quotes == TRUE)
2538 && (c == initc || c == findc))
2539 {
2540 int col, bslcnt = 0;
2541
2542 if (!cpo_bsl)
2543 {
2544 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2545 bslcnt++;
2546 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002547 /* Only accept a match when 'M' is in 'cpo' or when escaping
2548 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2550 {
2551 if (c == initc)
2552 count++;
2553 else
2554 {
2555 if (count == 0)
2556 return &pos;
2557 count--;
2558 }
2559 }
2560 }
2561 }
2562 }
2563
2564 if (comment_dir == BACKWARD && count > 0)
2565 {
2566 pos = match_pos;
2567 return &pos;
2568 }
2569 return (pos_T *)NULL; /* never found it */
2570}
2571
2572/*
2573 * Check if line[] contains a / / comment.
2574 * Return MAXCOL if not, otherwise return the column.
2575 * TODO: skip strings.
2576 */
2577 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002578check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
2580 char_u *p;
2581
2582 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002583#ifdef FEAT_LISP
2584 /* skip Lispish one-line comments */
2585 if (curbuf->b_p_lisp)
2586 {
2587 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2588 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002589 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002590
2591 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002592 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002593 {
2594 if (*p == '"')
2595 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002596 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002597 {
2598 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002599 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002600 }
2601 else if (p == line || ((p - line) >= 2
2602 /* skip #\" form */
2603 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002604 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002605 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002606 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002607 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2608 break; /* found! */
2609 ++p;
2610 }
2611 }
2612 else
2613 p = NULL;
2614 }
2615 else
2616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 while ((p = vim_strchr(p, '/')) != NULL)
2618 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002619 /* accept a double /, unless it's preceded with * and followed by *,
2620 * because * / / * is an end and start of a C comment */
2621 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 break;
2623 ++p;
2624 }
2625
2626 if (p == NULL)
2627 return MAXCOL;
2628 return (int)(p - line);
2629}
2630
2631/*
2632 * Move cursor briefly to character matching the one under the cursor.
2633 * Used for Insert mode and "r" command.
2634 * Show the match only if it is visible on the screen.
2635 * If there isn't a match, then beep.
2636 */
2637 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002638showmatch(
2639 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 pos_T *lpos, save_cursor;
2642 pos_T mpos;
2643 colnr_T vcol;
2644 long save_so;
2645 long save_siso;
2646#ifdef CURSOR_SHAPE
2647 int save_state;
2648#endif
2649 colnr_T save_dollar_vcol;
2650 char_u *p;
2651
2652 /*
2653 * Only show match for chars in the 'matchpairs' option.
2654 */
2655 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002656 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002659 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002660 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002661#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002662 p += MB_PTR2LEN(p) + 1;
2663 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#ifdef FEAT_RIGHTLEFT
2665 && !(curwin->w_p_rl ^ p_ri)
2666#endif
2667 )
2668 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002669 p += MB_PTR2LEN(p);
2670 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return;
2672 }
2673
2674 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002675 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002676 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
2678 if (!curwin->w_p_wrap)
2679 getvcol(curwin, lpos, NULL, &vcol, NULL);
2680 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002681 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
2683 mpos = *lpos; /* save the pos, update_screen() may change it */
2684 save_cursor = curwin->w_cursor;
2685 save_so = p_so;
2686 save_siso = p_siso;
2687 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2688 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002689 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2690 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 ++curwin->w_virtcol; /* do display ')' just before "$" */
2692 update_screen(VALID); /* show the new char first */
2693
2694 save_dollar_vcol = dollar_vcol;
2695#ifdef CURSOR_SHAPE
2696 save_state = State;
2697 State = SHOWMATCH;
2698 ui_cursor_shape(); /* may show different cursor shape */
2699#endif
2700 curwin->w_cursor = mpos; /* move to matching char */
2701 p_so = 0; /* don't use 'scrolloff' here */
2702 p_siso = 0; /* don't use 'sidescrolloff' here */
2703 showruler(FALSE);
2704 setcursor();
2705 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002706 out_flush_cursor(TRUE, FALSE);
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2709 * which resets it if the matching position is in a previous line
2710 * and has a higher column number. */
2711 dollar_vcol = save_dollar_vcol;
2712
2713 /*
2714 * brief pause, unless 'm' is present in 'cpo' and a character is
2715 * available.
2716 */
2717 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2718 ui_delay(p_mat * 100L, TRUE);
2719 else if (!char_avail())
2720 ui_delay(p_mat * 100L, FALSE);
2721 curwin->w_cursor = save_cursor; /* restore cursor position */
2722 p_so = save_so;
2723 p_siso = save_siso;
2724#ifdef CURSOR_SHAPE
2725 State = save_state;
2726 ui_cursor_shape(); /* may show different cursor shape */
2727#endif
2728 }
2729 }
2730}
2731
2732/*
Bram Moolenaar85160712018-06-19 18:27:41 +02002733 * Find the start of the next sentence, searching in the direction specified
2734 * by the "dir" argument. The cursor is positioned on the start of the next
2735 * sentence when found. If the next sentence is found, return OK. Return FAIL
2736 * otherwise. See ":h sentence" for the precise definition of a "sentence"
2737 * text object.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 */
2739 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002740findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 pos_T pos, tpos;
2743 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002744 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 int startlnum;
2746 int noskip = FALSE; /* do not skip blanks */
2747 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002748 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 pos = curwin->w_cursor;
2751 if (dir == FORWARD)
2752 func = incl;
2753 else
2754 func = decl;
2755
2756 while (count--)
2757 {
2758 /*
2759 * if on an empty line, skip upto a non-empty line
2760 */
2761 if (gchar_pos(&pos) == NUL)
2762 {
2763 do
2764 if ((*func)(&pos) == -1)
2765 break;
2766 while (gchar_pos(&pos) == NUL);
2767 if (dir == FORWARD)
2768 goto found;
2769 }
2770 /*
2771 * if on the start of a paragraph or a section and searching forward,
2772 * go to the next line
2773 */
2774 else if (dir == FORWARD && pos.col == 0 &&
2775 startPS(pos.lnum, NUL, FALSE))
2776 {
2777 if (pos.lnum == curbuf->b_ml.ml_line_count)
2778 return FAIL;
2779 ++pos.lnum;
2780 goto found;
2781 }
2782 else if (dir == BACKWARD)
2783 decl(&pos);
2784
Bram Moolenaar85160712018-06-19 18:27:41 +02002785 // go back to the previous non-white non-punctuation character
Bram Moolenaardef9e822004-12-31 20:58:58 +00002786 found_dot = FALSE;
Bram Moolenaar85160712018-06-19 18:27:41 +02002787 while (c = gchar_pos(&pos), VIM_ISWHITE(c)
2788 || vim_strchr((char_u *)".!?)]\"'", c) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
Bram Moolenaar85160712018-06-19 18:27:41 +02002790 tpos = pos;
2791 if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 break;
Bram Moolenaar85160712018-06-19 18:27:41 +02002793
2794 if (found_dot)
2795 break;
2796 if (vim_strchr((char_u *) ".!?", c) != NULL)
2797 found_dot = TRUE;
2798
2799 if (vim_strchr((char_u *) ")]\"'", c) != NULL
2800 && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
2801 break;
2802
2803 decl(&pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805
2806 /* remember the line where the search started */
2807 startlnum = pos.lnum;
2808 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2809
2810 for (;;) /* find end of sentence */
2811 {
2812 c = gchar_pos(&pos);
2813 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2814 {
2815 if (dir == BACKWARD && pos.lnum != startlnum)
2816 ++pos.lnum;
2817 break;
2818 }
2819 if (c == '.' || c == '!' || c == '?')
2820 {
2821 tpos = pos;
2822 do
2823 if ((c = inc(&tpos)) == -1)
2824 break;
2825 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2826 != NULL);
2827 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2828 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2829 && gchar_pos(&tpos) == ' ')))
2830 {
2831 pos = tpos;
2832 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2833 inc(&pos);
2834 break;
2835 }
2836 }
2837 if ((*func)(&pos) == -1)
2838 {
2839 if (count)
2840 return FAIL;
2841 noskip = TRUE;
2842 break;
2843 }
2844 }
2845found:
2846 /* skip white space */
2847 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2848 if (incl(&pos) == -1)
2849 break;
2850 }
2851
2852 setpcmark();
2853 curwin->w_cursor = pos;
2854 return OK;
2855}
2856
2857/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002858 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002860 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 * If 'what' is '{' or '}' we go to the next section.
2862 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002863 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 */
2865 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002866findpar(
2867 int *pincl, /* Return: TRUE if last char is to be included */
2868 int dir,
2869 long count,
2870 int what,
2871 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 linenr_T curr;
2874 int did_skip; /* TRUE after separating lines have been skipped */
2875 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002876 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877#ifdef FEAT_FOLDING
2878 linenr_T fold_first; /* first line of a closed fold */
2879 linenr_T fold_last; /* last line of a closed fold */
2880 int fold_skipped; /* TRUE if a closed fold was skipped this
2881 iteration */
2882#endif
2883
2884 curr = curwin->w_cursor.lnum;
2885
2886 while (count--)
2887 {
2888 did_skip = FALSE;
2889 for (first = TRUE; ; first = FALSE)
2890 {
2891 if (*ml_get(curr) != NUL)
2892 did_skip = TRUE;
2893
2894#ifdef FEAT_FOLDING
2895 /* skip folded lines */
2896 fold_skipped = FALSE;
2897 if (first && hasFolding(curr, &fold_first, &fold_last))
2898 {
2899 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2900 fold_skipped = TRUE;
2901 }
2902#endif
2903
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002904 /* POSIX has its own ideas of what a paragraph boundary is and it
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002905 * doesn't match historical Vi: It also stops at a "{" in the
2906 * first column and at an empty line. */
2907 if (!first && did_skip && (startPS(curr, what, both)
2908 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 break;
2910
2911#ifdef FEAT_FOLDING
2912 if (fold_skipped)
2913 curr -= dir;
2914#endif
2915 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2916 {
2917 if (count)
2918 return FALSE;
2919 curr -= dir;
2920 break;
2921 }
2922 }
2923 }
2924 setpcmark();
2925 if (both && *ml_get(curr) == '}') /* include line with '}' */
2926 ++curr;
2927 curwin->w_cursor.lnum = curr;
2928 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2929 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002930 char_u *line = ml_get(curr);
2931
2932 /* Put the cursor on the last character in the last line and make the
2933 * motion inclusive. */
2934 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
2936 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002937#ifdef FEAT_MBYTE
2938 curwin->w_cursor.col -=
2939 (*mb_head_off)(line, line + curwin->w_cursor.col);
2940#endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002941 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
2943 }
2944 else
2945 curwin->w_cursor.col = 0;
2946 return TRUE;
2947}
2948
2949/*
2950 * check if the string 's' is a nroff macro that is in option 'opt'
2951 */
2952 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002953inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 char_u *macro;
2956
2957 for (macro = opt; macro[0]; ++macro)
2958 {
2959 /* Accept two characters in the option being equal to two characters
2960 * in the line. A space in the option matches with a space in the
2961 * line or the line having ended. */
2962 if ( (macro[0] == s[0]
2963 || (macro[0] == ' '
2964 && (s[0] == NUL || s[0] == ' ')))
2965 && (macro[1] == s[1]
2966 || ((macro[1] == NUL || macro[1] == ' ')
2967 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2968 break;
2969 ++macro;
2970 if (macro[0] == NUL)
2971 break;
2972 }
2973 return (macro[0] != NUL);
2974}
2975
2976/*
2977 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2978 * If 'para' is '{' or '}' only check for sections.
2979 * If 'both' is TRUE also stop at '}'
2980 */
2981 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002982startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 char_u *s;
2985
2986 s = ml_get(lnum);
2987 if (*s == para || *s == '\f' || (both && *s == '}'))
2988 return TRUE;
2989 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2990 (!para && inmacro(p_para, s + 1))))
2991 return TRUE;
2992 return FALSE;
2993}
2994
2995/*
2996 * The following routines do the word searches performed by the 'w', 'W',
2997 * 'b', 'B', 'e', and 'E' commands.
2998 */
2999
3000/*
3001 * To perform these searches, characters are placed into one of three
3002 * classes, and transitions between classes determine word boundaries.
3003 *
3004 * The classes are:
3005 *
3006 * 0 - white space
3007 * 1 - punctuation
3008 * 2 or higher - keyword characters (letters, digits and underscore)
3009 */
3010
3011static int cls_bigword; /* TRUE for "W", "B" or "E" */
3012
3013/*
3014 * cls() - returns the class of character at curwin->w_cursor
3015 *
3016 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
3017 * from class 2 and higher are reported as class 1 since only white space
3018 * boundaries are of interest.
3019 */
3020 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003021cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 int c;
3024
3025 c = gchar_cursor();
3026#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
3027 if (p_altkeymap && c == F_BLANK)
3028 return 0;
3029#endif
3030 if (c == ' ' || c == '\t' || c == NUL)
3031 return 0;
3032#ifdef FEAT_MBYTE
3033 if (enc_dbcs != 0 && c > 0xFF)
3034 {
3035 /* If cls_bigword, report multi-byte chars as class 1. */
3036 if (enc_dbcs == DBCS_KOR && cls_bigword)
3037 return 1;
3038
3039 /* process code leading/trailing bytes */
3040 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3041 }
3042 if (enc_utf8)
3043 {
3044 c = utf_class(c);
3045 if (c != 0 && cls_bigword)
3046 return 1;
3047 return c;
3048 }
3049#endif
3050
3051 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3052 if (cls_bigword)
3053 return 1;
3054
3055 if (vim_iswordc(c))
3056 return 2;
3057 return 1;
3058}
3059
3060
3061/*
3062 * fwd_word(count, type, eol) - move forward one word
3063 *
3064 * Returns FAIL if the cursor was already at the end of the file.
3065 * If eol is TRUE, last word stops at end of line (for operators).
3066 */
3067 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003068fwd_word(
3069 long count,
3070 int bigword, /* "W", "E" or "B" */
3071 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 int sclass; /* starting class */
3074 int i;
3075 int last_line;
3076
3077#ifdef FEAT_VIRTUALEDIT
3078 curwin->w_cursor.coladd = 0;
3079#endif
3080 cls_bigword = bigword;
3081 while (--count >= 0)
3082 {
3083#ifdef FEAT_FOLDING
3084 /* When inside a range of folded lines, move to the last char of the
3085 * last line. */
3086 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3087 coladvance((colnr_T)MAXCOL);
3088#endif
3089 sclass = cls();
3090
3091 /*
3092 * We always move at least one character, unless on the last
3093 * character in the buffer.
3094 */
3095 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3096 i = inc_cursor();
3097 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3098 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003099 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 return OK;
3101
3102 /*
3103 * Go one char past end of current word (if any)
3104 */
3105 if (sclass != 0)
3106 while (cls() == sclass)
3107 {
3108 i = inc_cursor();
3109 if (i == -1 || (i >= 1 && eol && count == 0))
3110 return OK;
3111 }
3112
3113 /*
3114 * go to next non-white
3115 */
3116 while (cls() == 0)
3117 {
3118 /*
3119 * We'll stop if we land on a blank line
3120 */
3121 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3122 break;
3123
3124 i = inc_cursor();
3125 if (i == -1 || (i >= 1 && eol && count == 0))
3126 return OK;
3127 }
3128 }
3129 return OK;
3130}
3131
3132/*
3133 * bck_word() - move backward 'count' words
3134 *
3135 * If stop is TRUE and we are already on the start of a word, move one less.
3136 *
3137 * Returns FAIL if top of the file was reached.
3138 */
3139 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003140bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141{
3142 int sclass; /* starting class */
3143
3144#ifdef FEAT_VIRTUALEDIT
3145 curwin->w_cursor.coladd = 0;
3146#endif
3147 cls_bigword = bigword;
3148 while (--count >= 0)
3149 {
3150#ifdef FEAT_FOLDING
3151 /* When inside a range of folded lines, move to the first char of the
3152 * first line. */
3153 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3154 curwin->w_cursor.col = 0;
3155#endif
3156 sclass = cls();
3157 if (dec_cursor() == -1) /* started at start of file */
3158 return FAIL;
3159
3160 if (!stop || sclass == cls() || sclass == 0)
3161 {
3162 /*
3163 * Skip white space before the word.
3164 * Stop on an empty line.
3165 */
3166 while (cls() == 0)
3167 {
3168 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003169 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 goto finished;
3171 if (dec_cursor() == -1) /* hit start of file, stop here */
3172 return OK;
3173 }
3174
3175 /*
3176 * Move backward to start of this word.
3177 */
3178 if (skip_chars(cls(), BACKWARD))
3179 return OK;
3180 }
3181
3182 inc_cursor(); /* overshot - forward one */
3183finished:
3184 stop = FALSE;
3185 }
3186 return OK;
3187}
3188
3189/*
3190 * end_word() - move to the end of the word
3191 *
3192 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3193 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3194 * motion crosses blank lines. When the real vi crosses a blank line in an
3195 * 'e' motion, the cursor is placed on the FIRST character of the next
3196 * non-blank line. The 'E' command, however, works correctly. Since this
3197 * appears to be a bug, I have not duplicated it here.
3198 *
3199 * Returns FAIL if end of the file was reached.
3200 *
3201 * If stop is TRUE and we are already on the end of a word, move one less.
3202 * If empty is TRUE stop on an empty line.
3203 */
3204 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003205end_word(
3206 long count,
3207 int bigword,
3208 int stop,
3209 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 int sclass; /* starting class */
3212
3213#ifdef FEAT_VIRTUALEDIT
3214 curwin->w_cursor.coladd = 0;
3215#endif
3216 cls_bigword = bigword;
3217 while (--count >= 0)
3218 {
3219#ifdef FEAT_FOLDING
3220 /* When inside a range of folded lines, move to the last char of the
3221 * last line. */
3222 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3223 coladvance((colnr_T)MAXCOL);
3224#endif
3225 sclass = cls();
3226 if (inc_cursor() == -1)
3227 return FAIL;
3228
3229 /*
3230 * If we're in the middle of a word, we just have to move to the end
3231 * of it.
3232 */
3233 if (cls() == sclass && sclass != 0)
3234 {
3235 /*
3236 * Move forward to end of the current word
3237 */
3238 if (skip_chars(sclass, FORWARD))
3239 return FAIL;
3240 }
3241 else if (!stop || sclass == 0)
3242 {
3243 /*
3244 * We were at the end of a word. Go to the end of the next word.
3245 * First skip white space, if 'empty' is TRUE, stop at empty line.
3246 */
3247 while (cls() == 0)
3248 {
3249 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003250 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 goto finished;
3252 if (inc_cursor() == -1) /* hit end of file, stop here */
3253 return FAIL;
3254 }
3255
3256 /*
3257 * Move forward to the end of this word.
3258 */
3259 if (skip_chars(cls(), FORWARD))
3260 return FAIL;
3261 }
3262 dec_cursor(); /* overshot - one char backward */
3263finished:
3264 stop = FALSE; /* we move only one word less */
3265 }
3266 return OK;
3267}
3268
3269/*
3270 * Move back to the end of the word.
3271 *
3272 * Returns FAIL if start of the file was reached.
3273 */
3274 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003275bckend_word(
3276 long count,
3277 int bigword, /* TRUE for "B" */
3278 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 int sclass; /* starting class */
3281 int i;
3282
3283#ifdef FEAT_VIRTUALEDIT
3284 curwin->w_cursor.coladd = 0;
3285#endif
3286 cls_bigword = bigword;
3287 while (--count >= 0)
3288 {
3289 sclass = cls();
3290 if ((i = dec_cursor()) == -1)
3291 return FAIL;
3292 if (eol && i == 1)
3293 return OK;
3294
3295 /*
3296 * Move backward to before the start of this word.
3297 */
3298 if (sclass != 0)
3299 {
3300 while (cls() == sclass)
3301 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3302 return OK;
3303 }
3304
3305 /*
3306 * Move backward to end of the previous word
3307 */
3308 while (cls() == 0)
3309 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003310 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 break;
3312 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3313 return OK;
3314 }
3315 }
3316 return OK;
3317}
3318
3319/*
3320 * Skip a row of characters of the same class.
3321 * Return TRUE when end-of-file reached, FALSE otherwise.
3322 */
3323 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003324skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 while (cls() == cclass)
3327 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3328 return TRUE;
3329 return FALSE;
3330}
3331
3332#ifdef FEAT_TEXTOBJ
3333/*
3334 * Go back to the start of the word or the start of white space
3335 */
3336 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003337back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 int sclass; /* starting class */
3340
3341 sclass = cls();
3342 for (;;)
3343 {
3344 if (curwin->w_cursor.col == 0) /* stop at start of line */
3345 break;
3346 dec_cursor();
3347 if (cls() != sclass) /* stop at start of word */
3348 {
3349 inc_cursor();
3350 break;
3351 }
3352 }
3353}
3354
3355 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003356find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 int c;
3359
3360 while (decl(posp) != -1)
3361 {
3362 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003363 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 incl(posp);
3366 break;
3367 }
3368 }
3369}
3370
3371/*
3372 * Skip count/2 sentences and count/2 separating white spaces.
3373 */
3374 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003375findsent_forward(
3376 long count,
3377 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 while (count--)
3380 {
3381 findsent(FORWARD, 1L);
3382 if (at_start_sent)
3383 find_first_blank(&curwin->w_cursor);
3384 if (count == 0 || at_start_sent)
3385 decl(&curwin->w_cursor);
3386 at_start_sent = !at_start_sent;
3387 }
3388}
3389
3390/*
3391 * Find word under cursor, cursor at end.
3392 * Used while an operator is pending, and in Visual mode.
3393 */
3394 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003395current_word(
3396 oparg_T *oap,
3397 long count,
3398 int include, /* TRUE: include word and white space */
3399 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 pos_T start_pos;
3402 pos_T pos;
3403 int inclusive = TRUE;
3404 int include_white = FALSE;
3405
3406 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003407 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003410 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 dec_cursor();
3412
3413 /*
3414 * When Visual mode is not active, or when the VIsual area is only one
3415 * character, select the word and/or white space under the cursor.
3416 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003417 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
3419 /*
3420 * Go to start of current word or white space.
3421 */
3422 back_in_line();
3423 start_pos = curwin->w_cursor;
3424
3425 /*
3426 * If the start is on white space, and white space should be included
3427 * (" word"), or start is not on white space, and white space should
3428 * not be included ("word"), find end of word.
3429 */
3430 if ((cls() == 0) == include)
3431 {
3432 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3433 return FAIL;
3434 }
3435 else
3436 {
3437 /*
3438 * If the start is not on white space, and white space should be
3439 * included ("word "), or start is on white space and white
3440 * space should not be included (" "), find start of word.
3441 * If we end up in the first column of the next line (single char
3442 * word) back up to end of the line.
3443 */
3444 fwd_word(1L, bigword, TRUE);
3445 if (curwin->w_cursor.col == 0)
3446 decl(&curwin->w_cursor);
3447 else
3448 oneleft();
3449
3450 if (include)
3451 include_white = TRUE;
3452 }
3453
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 if (VIsual_active)
3455 {
3456 /* should do something when inclusive == FALSE ! */
3457 VIsual = start_pos;
3458 redraw_curbuf_later(INVERTED); /* update the inversion */
3459 }
3460 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 oap->start = start_pos;
3463 oap->motion_type = MCHAR;
3464 }
3465 --count;
3466 }
3467
3468 /*
3469 * When count is still > 0, extend with more objects.
3470 */
3471 while (count > 0)
3472 {
3473 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003474 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 /*
3477 * In Visual mode, with cursor at start: move cursor back.
3478 */
3479 if (decl(&curwin->w_cursor) == -1)
3480 return FAIL;
3481 if (include != (cls() != 0))
3482 {
3483 if (bck_word(1L, bigword, TRUE) == FAIL)
3484 return FAIL;
3485 }
3486 else
3487 {
3488 if (bckend_word(1L, bigword, TRUE) == FAIL)
3489 return FAIL;
3490 (void)incl(&curwin->w_cursor);
3491 }
3492 }
3493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 /*
3496 * Move cursor forward one word and/or white area.
3497 */
3498 if (incl(&curwin->w_cursor) == -1)
3499 return FAIL;
3500 if (include != (cls() == 0))
3501 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003502 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return FAIL;
3504 /*
3505 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003506 * the first character on the line.
3507 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003509 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 inclusive = FALSE;
3511 }
3512 else
3513 {
3514 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3515 return FAIL;
3516 }
3517 }
3518 --count;
3519 }
3520
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003521 if (include_white && (cls() != 0
3522 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 /*
3525 * If we don't include white space at the end, move the start
3526 * to include some white space there. This makes "daw" work
3527 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003528 * word). Also when "2daw" deletes "word." at the end of the line
3529 * (cursor is at start of next line).
3530 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 */
3532 pos = curwin->w_cursor; /* save cursor position */
3533 curwin->w_cursor = start_pos;
3534 if (oneleft() == OK)
3535 {
3536 back_in_line();
3537 if (cls() == 0 && curwin->w_cursor.col > 0)
3538 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (VIsual_active)
3540 VIsual = curwin->w_cursor;
3541 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 oap->start = curwin->w_cursor;
3543 }
3544 }
3545 curwin->w_cursor = pos; /* put cursor back at end */
3546 }
3547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (VIsual_active)
3549 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003550 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 inc_cursor();
3552 if (VIsual_mode == 'V')
3553 {
3554 VIsual_mode = 'v';
3555 redraw_cmdline = TRUE; /* show mode later */
3556 }
3557 }
3558 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 oap->inclusive = inclusive;
3560
3561 return OK;
3562}
3563
3564/*
3565 * Find sentence(s) under the cursor, cursor at end.
3566 * When Visual active, extend it by one or more sentences.
3567 */
3568 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003569current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570{
3571 pos_T start_pos;
3572 pos_T pos;
3573 int start_blank;
3574 int c;
3575 int at_start_sent;
3576 long ncount;
3577
3578 start_pos = curwin->w_cursor;
3579 pos = start_pos;
3580 findsent(FORWARD, 1L); /* Find start of next sentence. */
3581
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003583 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003585 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003588 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
3590 /*
3591 * Cursor at start of Visual area.
3592 * Find out where we are:
3593 * - in the white space before a sentence
3594 * - in a sentence or just after it
3595 * - at the start of a sentence
3596 */
3597 at_start_sent = TRUE;
3598 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003599 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
3601 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003602 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 at_start_sent = FALSE;
3605 break;
3606 }
3607 incl(&pos);
3608 }
3609 if (!at_start_sent)
3610 {
3611 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003612 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 at_start_sent = TRUE; /* exactly at start of sentence */
3614 else
3615 /* inside a sentence, go to its end (start of next) */
3616 findsent(FORWARD, 1L);
3617 }
3618 if (include) /* "as" gets twice as much as "is" */
3619 count *= 2;
3620 while (count--)
3621 {
3622 if (at_start_sent)
3623 find_first_blank(&curwin->w_cursor);
3624 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003625 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 findsent(BACKWARD, 1L);
3627 at_start_sent = !at_start_sent;
3628 }
3629 }
3630 else
3631 {
3632 /*
3633 * Cursor at end of Visual area.
3634 * Find out where we are:
3635 * - just before a sentence
3636 * - just before or in the white space before a sentence
3637 * - in a sentence
3638 */
3639 incl(&pos);
3640 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003641 /* not just before a sentence */
3642 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
3644 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003645 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 {
3647 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003648 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
3650 at_start_sent = TRUE;
3651 break;
3652 }
3653 incl(&pos);
3654 }
3655 if (at_start_sent) /* in the sentence */
3656 findsent(BACKWARD, 1L);
3657 else /* in/before white before a sentence */
3658 curwin->w_cursor = start_pos;
3659 }
3660
3661 if (include) /* "as" gets twice as much as "is" */
3662 count *= 2;
3663 findsent_forward(count, at_start_sent);
3664 if (*p_sel == 'e')
3665 ++curwin->w_cursor.col;
3666 }
3667 return OK;
3668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003671 * If the cursor started on a blank, check if it is just before the start
3672 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003674 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003676 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
3678 start_blank = TRUE;
3679 find_first_blank(&start_pos); /* go back to first blank */
3680 }
3681 else
3682 {
3683 start_blank = FALSE;
3684 findsent(BACKWARD, 1L);
3685 start_pos = curwin->w_cursor;
3686 }
3687 if (include)
3688 ncount = count * 2;
3689 else
3690 {
3691 ncount = count;
3692 if (start_blank)
3693 --ncount;
3694 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003695 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 findsent_forward(ncount, TRUE);
3697 else
3698 decl(&curwin->w_cursor);
3699
3700 if (include)
3701 {
3702 /*
3703 * If the blank in front of the sentence is included, exclude the
3704 * blanks at the end of the sentence, go back to the first blank.
3705 * If there are no trailing blanks, try to include leading blanks.
3706 */
3707 if (start_blank)
3708 {
3709 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003710 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3711 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 decl(&curwin->w_cursor);
3713 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003714 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 find_first_blank(&start_pos);
3716 }
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (VIsual_active)
3719 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003720 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003721 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 goto extend;
3723 if (*p_sel == 'e')
3724 ++curwin->w_cursor.col;
3725 VIsual = start_pos;
3726 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003727 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 redraw_curbuf_later(INVERTED); /* update the inversion */
3729 }
3730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 {
3732 /* include a newline after the sentence, if there is one */
3733 if (incl(&curwin->w_cursor) == -1)
3734 oap->inclusive = TRUE;
3735 else
3736 oap->inclusive = FALSE;
3737 oap->start = start_pos;
3738 oap->motion_type = MCHAR;
3739 }
3740 return OK;
3741}
3742
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003743/*
3744 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003745 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003748current_block(
3749 oparg_T *oap,
3750 long count,
3751 int include, /* TRUE == include white space */
3752 int what, /* '(', '{', etc. */
3753 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754{
3755 pos_T old_pos;
3756 pos_T *pos = NULL;
3757 pos_T start_pos;
3758 pos_T *end_pos;
3759 pos_T old_start, old_end;
3760 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003761 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003764 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 old_start = old_end;
3766
3767 /*
3768 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3769 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003770 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 {
3772 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003773 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 while (inindent(1))
3775 if (inc_cursor() != 0)
3776 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003777 if (gchar_cursor() == what)
3778 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 ++curwin->w_cursor.col;
3780 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003781 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783 old_start = VIsual;
3784 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3785 }
3786 else
3787 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 /*
3790 * Search backwards for unclosed '(', '{', etc..
3791 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003792 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3793 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003796 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 while (count-- > 0)
3798 {
3799 if ((pos = findmatch(NULL, what)) == NULL)
3800 break;
3801 curwin->w_cursor = *pos;
3802 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3803 }
3804 p_cpo = save_cpo;
3805
3806 /*
3807 * Search for matching ')', '}', etc.
3808 * Put this position in curwin->w_cursor.
3809 */
3810 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3811 {
3812 curwin->w_cursor = old_pos;
3813 return FAIL;
3814 }
3815 curwin->w_cursor = *end_pos;
3816
3817 /*
3818 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003819 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3820 * indent. But only if the resulting area is not smaller than what we
3821 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 */
3823 while (!include)
3824 {
3825 incl(&start_pos);
3826 sol = (curwin->w_cursor.col == 0);
3827 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003828 while (inindent(1))
3829 {
3830 sol = TRUE;
3831 if (decl(&curwin->w_cursor) != 0)
3832 break;
3833 }
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 /*
3836 * In Visual mode, when the resulting area is not bigger than what we
3837 * started with, extend it to the next block, and then exclude again.
3838 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003839 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 && VIsual_active)
3841 {
3842 curwin->w_cursor = old_start;
3843 decl(&curwin->w_cursor);
3844 if ((pos = findmatch(NULL, what)) == NULL)
3845 {
3846 curwin->w_cursor = old_pos;
3847 return FAIL;
3848 }
3849 start_pos = *pos;
3850 curwin->w_cursor = *pos;
3851 if ((end_pos = findmatch(NULL, other)) == NULL)
3852 {
3853 curwin->w_cursor = old_pos;
3854 return FAIL;
3855 }
3856 curwin->w_cursor = *end_pos;
3857 }
3858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 break;
3860 }
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (VIsual_active)
3863 {
3864 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003865 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003866 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 inc(&curwin->w_cursor); /* include the line break */
3868 VIsual = start_pos;
3869 VIsual_mode = 'v';
3870 redraw_curbuf_later(INVERTED); /* update the inversion */
3871 showmode();
3872 }
3873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
3875 oap->start = start_pos;
3876 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003877 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003880 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003881 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003883 else
3884 /* End is before the start (no text in between <>, [], etc.): don't
3885 * operate on any text. */
3886 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888
3889 return OK;
3890}
3891
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003892/*
3893 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3894 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3895 */
3896 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003897in_html_tag(
3898 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003899{
3900 char_u *line = ml_get_curline();
3901 char_u *p;
3902 int c;
3903 int lc = NUL;
3904 pos_T pos;
3905
3906#ifdef FEAT_MBYTE
3907 if (enc_dbcs)
3908 {
3909 char_u *lp = NULL;
3910
3911 /* We search forward until the cursor, because searching backwards is
3912 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003913 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003914 if (*p == '>' || *p == '<')
3915 {
3916 lc = *p;
3917 lp = p;
3918 }
3919 if (*p != '<') /* check for '<' under cursor */
3920 {
3921 if (lc != '<')
3922 return FALSE;
3923 p = lp;
3924 }
3925 }
3926 else
3927#endif
3928 {
3929 for (p = line + curwin->w_cursor.col; p > line; )
3930 {
3931 if (*p == '<') /* find '<' under/before cursor */
3932 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003933 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003934 if (*p == '>') /* find '>' before cursor */
3935 break;
3936 }
3937 if (*p != '<')
3938 return FALSE;
3939 }
3940
3941 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003943
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003944 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003945 if (end_tag)
3946 /* check that there is a '/' after the '<' */
3947 return *p == '/';
3948
3949 /* check that there is no '/' after the '<' */
3950 if (*p == '/')
3951 return FALSE;
3952
3953 /* check that the matching '>' is not preceded by '/' */
3954 for (;;)
3955 {
3956 if (inc(&pos) < 0)
3957 return FALSE;
3958 c = *ml_get_pos(&pos);
3959 if (c == '>')
3960 break;
3961 lc = c;
3962 }
3963 return lc != '/';
3964}
3965
3966/*
3967 * Find tag block under the cursor, cursor at end.
3968 */
3969 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003970current_tagblock(
3971 oparg_T *oap,
3972 long count_arg,
3973 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003974{
3975 long count = count_arg;
3976 long n;
3977 pos_T old_pos;
3978 pos_T start_pos;
3979 pos_T end_pos;
3980 pos_T old_start, old_end;
3981 char_u *spat, *epat;
3982 char_u *p;
3983 char_u *cp;
3984 int len;
3985 int r;
3986 int do_include = include;
3987 int save_p_ws = p_ws;
3988 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003989 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003990
3991 p_ws = FALSE;
3992
3993 old_pos = curwin->w_cursor;
3994 old_end = curwin->w_cursor; /* remember where we started */
3995 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003996 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003997 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003998
3999 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00004000 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004001 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004002 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004003 {
4004 setpcmark();
4005
4006 /* ignore indent */
4007 while (inindent(1))
4008 if (inc_cursor() != 0)
4009 break;
4010
4011 if (in_html_tag(FALSE))
4012 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004013 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004014 while (*ml_get_cursor() != '>')
4015 if (inc_cursor() < 0)
4016 break;
4017 }
4018 else if (in_html_tag(TRUE))
4019 {
4020 /* cursor on end tag, move to just before it */
4021 while (*ml_get_cursor() != '<')
4022 if (dec_cursor() < 0)
4023 break;
4024 dec_cursor();
4025 old_end = curwin->w_cursor;
4026 }
4027 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004028 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004029 {
4030 old_start = VIsual;
4031 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4032 }
4033 else
4034 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004035
4036again:
4037 /*
4038 * Search backwards for unclosed "<aaa>".
4039 * Put this position in start_pos.
4040 */
4041 for (n = 0; n < count; ++n)
4042 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004043 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004044 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004045 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004046 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004047 {
4048 curwin->w_cursor = old_pos;
4049 goto theend;
4050 }
4051 }
4052 start_pos = curwin->w_cursor;
4053
4054 /*
4055 * Search for matching "</aaa>". First isolate the "aaa".
4056 */
4057 inc_cursor();
4058 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004059 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004060 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004061 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004062 if (len == 0)
4063 {
4064 curwin->w_cursor = old_pos;
4065 goto theend;
4066 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004067 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004068 epat = alloc(len + 9);
4069 if (spat == NULL || epat == NULL)
4070 {
4071 vim_free(spat);
4072 vim_free(epat);
4073 curwin->w_cursor = old_pos;
4074 goto theend;
4075 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004076 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004077 sprintf((char *)epat, "</%.*s>\\c", len, p);
4078
Bram Moolenaar48570482017-10-30 21:48:41 +01004079 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004080 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004081
4082 vim_free(spat);
4083 vim_free(epat);
4084
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004085 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004086 {
4087 /* Can't find other end or it's before the previous end. Could be a
4088 * HTML tag that doesn't have a matching end. Search backwards for
4089 * another starting tag. */
4090 count = 1;
4091 curwin->w_cursor = start_pos;
4092 goto again;
4093 }
4094
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02004095 if (do_include)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004096 {
4097 /* Include up to the '>'. */
4098 while (*ml_get_cursor() != '>')
4099 if (inc_cursor() < 0)
4100 break;
4101 }
4102 else
4103 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004104 char_u *c = ml_get_cursor();
4105
4106 /* Exclude the '<' of the end tag.
4107 * If the closing tag is on new line, do not decrement cursor, but
4108 * make operation exclusive, so that the linefeed will be selected */
4109 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4110 /* do not decrement cursor */
4111 is_inclusive = FALSE;
4112 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004113 dec_cursor();
4114 }
4115 end_pos = curwin->w_cursor;
4116
4117 if (!do_include)
4118 {
4119 /* Exclude the start tag. */
4120 curwin->w_cursor = start_pos;
4121 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004122 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004123 {
4124 inc_cursor();
4125 start_pos = curwin->w_cursor;
4126 break;
4127 }
4128 curwin->w_cursor = end_pos;
4129
Bram Moolenaarb476cb72018-08-16 21:37:50 +02004130 // If we are in Visual mode and now have the same text as before set
4131 // "do_include" and try again.
4132 if (VIsual_active && EQUAL_POS(start_pos, old_start)
4133 && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004134 {
4135 do_include = TRUE;
4136 curwin->w_cursor = old_start;
4137 count = count_arg;
4138 goto again;
4139 }
4140 }
4141
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004142 if (VIsual_active)
4143 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004144 /* If the end is before the start there is no text between tags, select
4145 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004146 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004147 curwin->w_cursor = start_pos;
4148 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004149 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004150 VIsual = start_pos;
4151 VIsual_mode = 'v';
4152 redraw_curbuf_later(INVERTED); /* update the inversion */
4153 showmode();
4154 }
4155 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004156 {
4157 oap->start = start_pos;
4158 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004159 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004160 {
4161 /* End is before the start: there is no text between tags; operate
4162 * on an empty area. */
4163 curwin->w_cursor = start_pos;
4164 oap->inclusive = FALSE;
4165 }
4166 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004167 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004168 }
4169 retval = OK;
4170
4171theend:
4172 p_ws = save_p_ws;
4173 return retval;
4174}
4175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004177current_par(
4178 oparg_T *oap,
4179 long count,
4180 int include, /* TRUE == include white space */
4181 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
4183 linenr_T start_lnum;
4184 linenr_T end_lnum;
4185 int white_in_front;
4186 int dir;
4187 int start_is_white;
4188 int prev_start_is_white;
4189 int retval = OK;
4190 int do_white = FALSE;
4191 int t;
4192 int i;
4193
4194 if (type == 'S') /* not implemented yet */
4195 return FAIL;
4196
4197 start_lnum = curwin->w_cursor.lnum;
4198
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 /*
4200 * When visual area is more than one line: extend it.
4201 */
4202 if (VIsual_active && start_lnum != VIsual.lnum)
4203 {
4204extend:
4205 if (start_lnum < VIsual.lnum)
4206 dir = BACKWARD;
4207 else
4208 dir = FORWARD;
4209 for (i = count; --i >= 0; )
4210 {
4211 if (start_lnum ==
4212 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4213 {
4214 retval = FAIL;
4215 break;
4216 }
4217
4218 prev_start_is_white = -1;
4219 for (t = 0; t < 2; ++t)
4220 {
4221 start_lnum += dir;
4222 start_is_white = linewhite(start_lnum);
4223 if (prev_start_is_white == start_is_white)
4224 {
4225 start_lnum -= dir;
4226 break;
4227 }
4228 for (;;)
4229 {
4230 if (start_lnum == (dir == BACKWARD
4231 ? 1 : curbuf->b_ml.ml_line_count))
4232 break;
4233 if (start_is_white != linewhite(start_lnum + dir)
4234 || (!start_is_white
4235 && startPS(start_lnum + (dir > 0
4236 ? 1 : 0), 0, 0)))
4237 break;
4238 start_lnum += dir;
4239 }
4240 if (!include)
4241 break;
4242 if (start_lnum == (dir == BACKWARD
4243 ? 1 : curbuf->b_ml.ml_line_count))
4244 break;
4245 prev_start_is_white = start_is_white;
4246 }
4247 }
4248 curwin->w_cursor.lnum = start_lnum;
4249 curwin->w_cursor.col = 0;
4250 return retval;
4251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 /*
4254 * First move back to the start_lnum of the paragraph or white lines
4255 */
4256 white_in_front = linewhite(start_lnum);
4257 while (start_lnum > 1)
4258 {
4259 if (white_in_front) /* stop at first white line */
4260 {
4261 if (!linewhite(start_lnum - 1))
4262 break;
4263 }
4264 else /* stop at first non-white line of start of paragraph */
4265 {
4266 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4267 break;
4268 }
4269 --start_lnum;
4270 }
4271
4272 /*
4273 * Move past the end of any white lines.
4274 */
4275 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004276 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4277 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 --end_lnum;
4280 i = count;
4281 if (!include && white_in_front)
4282 --i;
4283 while (i--)
4284 {
4285 if (end_lnum == curbuf->b_ml.ml_line_count)
4286 return FAIL;
4287
4288 if (!include)
4289 do_white = linewhite(end_lnum + 1);
4290
4291 if (include || !do_white)
4292 {
4293 ++end_lnum;
4294 /*
4295 * skip to end of paragraph
4296 */
4297 while (end_lnum < curbuf->b_ml.ml_line_count
4298 && !linewhite(end_lnum + 1)
4299 && !startPS(end_lnum + 1, 0, 0))
4300 ++end_lnum;
4301 }
4302
4303 if (i == 0 && white_in_front && include)
4304 break;
4305
4306 /*
4307 * skip to end of white lines after paragraph
4308 */
4309 if (include || do_white)
4310 while (end_lnum < curbuf->b_ml.ml_line_count
4311 && linewhite(end_lnum + 1))
4312 ++end_lnum;
4313 }
4314
4315 /*
4316 * If there are no empty lines at the end, try to find some empty lines at
4317 * the start (unless that has been done already).
4318 */
4319 if (!white_in_front && !linewhite(end_lnum) && include)
4320 while (start_lnum > 1 && linewhite(start_lnum - 1))
4321 --start_lnum;
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (VIsual_active)
4324 {
4325 /* Problem: when doing "Vipipip" nothing happens in a single white
4326 * line, we get stuck there. Trap this here. */
4327 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4328 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004329 if (VIsual.lnum != start_lnum)
4330 {
4331 VIsual.lnum = start_lnum;
4332 VIsual.col = 0;
4333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 VIsual_mode = 'V';
4335 redraw_curbuf_later(INVERTED); /* update the inversion */
4336 showmode();
4337 }
4338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {
4340 oap->start.lnum = start_lnum;
4341 oap->start.col = 0;
4342 oap->motion_type = MLINE;
4343 }
4344 curwin->w_cursor.lnum = end_lnum;
4345 curwin->w_cursor.col = 0;
4346
4347 return OK;
4348}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004349
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004350/*
4351 * Search quote char from string line[col].
4352 * Quote character escaped by one of the characters in "escape" is not counted
4353 * as a quote.
4354 * Returns column number of "quotechar" or -1 when not found.
4355 */
4356 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004357find_next_quote(
4358 char_u *line,
4359 int col,
4360 int quotechar,
4361 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004362{
4363 int c;
4364
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004365 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004366 {
4367 c = line[col];
4368 if (c == NUL)
4369 return -1;
4370 else if (escape != NULL && vim_strchr(escape, c))
4371 ++col;
4372 else if (c == quotechar)
4373 break;
4374#ifdef FEAT_MBYTE
4375 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004376 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004377 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004379 ++col;
4380 }
4381 return col;
4382}
4383
4384/*
4385 * Search backwards in "line" from column "col_start" to find "quotechar".
4386 * Quote character escaped by one of the characters in "escape" is not counted
4387 * as a quote.
4388 * Return the found column or zero.
4389 */
4390 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004391find_prev_quote(
4392 char_u *line,
4393 int col_start,
4394 int quotechar,
4395 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004396{
4397 int n;
4398
4399 while (col_start > 0)
4400 {
4401 --col_start;
4402#ifdef FEAT_MBYTE
4403 col_start -= (*mb_head_off)(line, line + col_start);
4404#endif
4405 n = 0;
4406 if (escape != NULL)
4407 while (col_start - n > 0 && vim_strchr(escape,
4408 line[col_start - n - 1]) != NULL)
4409 ++n;
4410 if (n & 1)
4411 col_start -= n; /* uneven number of escape chars, skip it */
4412 else if (line[col_start] == quotechar)
4413 break;
4414 }
4415 return col_start;
4416}
4417
4418/*
4419 * Find quote under the cursor, cursor at end.
4420 * Returns TRUE if found, else FALSE.
4421 */
4422 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004423current_quote(
4424 oparg_T *oap,
4425 long count,
4426 int include, /* TRUE == include quote char */
4427 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004428{
4429 char_u *line = ml_get_curline();
4430 int col_end;
4431 int col_start = curwin->w_cursor.col;
4432 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004433 int vis_empty = TRUE; /* Visual selection <= 1 char */
4434 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004435 int inside_quotes = FALSE; /* Looks like "i'" done before */
4436 int selected_quote = FALSE; /* Has quote inside selection */
4437 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004438
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004439 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004440 if (VIsual_active)
4441 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004442 /* this only works within one line */
4443 if (VIsual.lnum != curwin->w_cursor.lnum)
4444 return FALSE;
4445
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004446 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004447 if (*p_sel == 'e')
4448 {
4449 if (!vis_bef_curs)
4450 {
4451 /* VIsual needs to be start of Visual selection. */
4452 pos_T t = curwin->w_cursor;
4453
4454 curwin->w_cursor = VIsual;
4455 VIsual = t;
4456 vis_bef_curs = TRUE;
4457 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004458 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004459 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004460 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004461 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004462
4463 if (!vis_empty)
4464 {
4465 /* Check if the existing selection exactly spans the text inside
4466 * quotes. */
4467 if (vis_bef_curs)
4468 {
4469 inside_quotes = VIsual.col > 0
4470 && line[VIsual.col - 1] == quotechar
4471 && line[curwin->w_cursor.col] != NUL
4472 && line[curwin->w_cursor.col + 1] == quotechar;
4473 i = VIsual.col;
4474 col_end = curwin->w_cursor.col;
4475 }
4476 else
4477 {
4478 inside_quotes = curwin->w_cursor.col > 0
4479 && line[curwin->w_cursor.col - 1] == quotechar
4480 && line[VIsual.col] != NUL
4481 && line[VIsual.col + 1] == quotechar;
4482 i = curwin->w_cursor.col;
4483 col_end = VIsual.col;
4484 }
4485
4486 /* Find out if we have a quote in the selection. */
4487 while (i <= col_end)
4488 if (line[i++] == quotechar)
4489 {
4490 selected_quote = TRUE;
4491 break;
4492 }
4493 }
4494
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004495 if (!vis_empty && line[col_start] == quotechar)
4496 {
4497 /* Already selecting something and on a quote character. Find the
4498 * next quoted string. */
4499 if (vis_bef_curs)
4500 {
4501 /* Assume we are on a closing quote: move to after the next
4502 * opening quote. */
4503 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4504 if (col_start < 0)
4505 return FALSE;
4506 col_end = find_next_quote(line, col_start + 1, quotechar,
4507 curbuf->b_p_qe);
4508 if (col_end < 0)
4509 {
4510 /* We were on a starting quote perhaps? */
4511 col_end = col_start;
4512 col_start = curwin->w_cursor.col;
4513 }
4514 }
4515 else
4516 {
4517 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4518 if (line[col_end] != quotechar)
4519 return FALSE;
4520 col_start = find_prev_quote(line, col_end, quotechar,
4521 curbuf->b_p_qe);
4522 if (line[col_start] != quotechar)
4523 {
4524 /* We were on an ending quote perhaps? */
4525 col_start = col_end;
4526 col_end = curwin->w_cursor.col;
4527 }
4528 }
4529 }
4530 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004531
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004532 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004533 {
4534 int first_col = col_start;
4535
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004536 if (!vis_empty)
4537 {
4538 if (vis_bef_curs)
4539 first_col = find_next_quote(line, col_start, quotechar, NULL);
4540 else
4541 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4542 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004543
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004544 /* The cursor is on a quote, we don't know if it's the opening or
4545 * closing quote. Search from the start of the line to find out.
4546 * Also do this when there is a Visual area, a' may leave the cursor
4547 * in between two strings. */
4548 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004549 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004550 {
4551 /* Find open quote character. */
4552 col_start = find_next_quote(line, col_start, quotechar, NULL);
4553 if (col_start < 0 || col_start > first_col)
4554 return FALSE;
4555 /* Find close quote character. */
4556 col_end = find_next_quote(line, col_start + 1, quotechar,
4557 curbuf->b_p_qe);
4558 if (col_end < 0)
4559 return FALSE;
4560 /* If is cursor between start and end quote character, it is
4561 * target text object. */
4562 if (col_start <= first_col && first_col <= col_end)
4563 break;
4564 col_start = col_end + 1;
4565 }
4566 }
4567 else
4568 {
4569 /* Search backward for a starting quote. */
4570 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4571 if (line[col_start] != quotechar)
4572 {
4573 /* No quote before the cursor, look after the cursor. */
4574 col_start = find_next_quote(line, col_start, quotechar, NULL);
4575 if (col_start < 0)
4576 return FALSE;
4577 }
4578
4579 /* Find close quote character. */
4580 col_end = find_next_quote(line, col_start + 1, quotechar,
4581 curbuf->b_p_qe);
4582 if (col_end < 0)
4583 return FALSE;
4584 }
4585
4586 /* When "include" is TRUE, include spaces after closing quote or before
4587 * the starting quote. */
4588 if (include)
4589 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004590 if (VIM_ISWHITE(line[col_end + 1]))
4591 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004592 ++col_end;
4593 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004594 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004595 --col_start;
4596 }
4597
Bram Moolenaarab194812005-09-14 21:40:12 +00004598 /* Set start position. After vi" another i" must include the ".
4599 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004600 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004601 ++col_start;
4602 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004603 if (VIsual_active)
4604 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004605 /* Set the start of the Visual area when the Visual area was empty, we
4606 * were just inside quotes or the Visual area didn't start at a quote
4607 * and didn't include a quote.
4608 */
4609 if (vis_empty
4610 || (vis_bef_curs
4611 && !selected_quote
4612 && (inside_quotes
4613 || (line[VIsual.col] != quotechar
4614 && (VIsual.col == 0
4615 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004616 {
4617 VIsual = curwin->w_cursor;
4618 redraw_curbuf_later(INVERTED);
4619 }
4620 }
4621 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004622 {
4623 oap->start = curwin->w_cursor;
4624 oap->motion_type = MCHAR;
4625 }
4626
4627 /* Set end position. */
4628 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004629 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004630 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004631 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004632 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004633 if (VIsual_active)
4634 {
4635 if (vis_empty || vis_bef_curs)
4636 {
4637 /* decrement cursor when 'selection' is not exclusive */
4638 if (*p_sel != 'e')
4639 dec_cursor();
4640 }
4641 else
4642 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004643 /* Cursor is at start of Visual area. Set the end of the Visual
4644 * area when it was just inside quotes or it didn't end at a
4645 * quote. */
4646 if (inside_quotes
4647 || (!selected_quote
4648 && line[VIsual.col] != quotechar
4649 && (line[VIsual.col] == NUL
4650 || line[VIsual.col + 1] != quotechar)))
4651 {
4652 dec_cursor();
4653 VIsual = curwin->w_cursor;
4654 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004655 curwin->w_cursor.col = col_start;
4656 }
4657 if (VIsual_mode == 'V')
4658 {
4659 VIsual_mode = 'v';
4660 redraw_cmdline = TRUE; /* show mode later */
4661 }
4662 }
4663 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004664 {
4665 /* Set inclusive and other oap's flags. */
4666 oap->inclusive = inclusive;
4667 }
4668
4669 return OK;
4670}
4671
4672#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004674static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004675
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004676/*
4677 * Find next search match under cursor, cursor at end.
4678 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004679 */
4680 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004681current_search(
4682 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004683 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004684{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004685 pos_T start_pos; // start position of the pattern match
4686 pos_T end_pos; // end position of the pattern match
4687 pos_T orig_pos; // position of the cursor at beginning
4688 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004689 int i;
4690 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004691 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004692 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004693 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004694 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004695 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004696
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004697 /* wrapping should not occur */
4698 p_ws = FALSE;
4699
4700 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004701 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004702 dec_cursor();
4703
4704 if (VIsual_active)
4705 {
4706 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004707
4708 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004709
4710 /* make sure, searching further will extend the match */
4711 if (VIsual_active)
4712 {
4713 if (forward)
4714 incl(&pos);
4715 else
4716 decl(&pos);
4717 }
4718 }
4719 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004720 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004721
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004722 /* Is the pattern is zero-width?, this time, don't care about the direction
4723 */
4724 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4725 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004726 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004727 {
4728 p_ws = old_p_ws;
4729 return FAIL; /* pattern not found */
4730 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004731
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004732 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004733 * The trick is to first search backwards and then search forward again,
4734 * so that a match at the current cursor position will be correctly
4735 * captured.
4736 */
4737 for (i = 0; i < 2; i++)
4738 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004739 if (forward)
4740 dir = i;
4741 else
4742 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004743
4744 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004745 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004746 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004747 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004748
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004749 result = searchit(curwin, curbuf, &pos, &end_pos,
4750 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004751 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004752 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004753
4754 /* First search may fail, but then start searching from the
4755 * beginning of the file (cursor might be on the search match)
4756 * except when Visual mode is active, so that extending the visual
4757 * selection works. */
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004758 if (i == 1 && !result) /* not found, abort */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004759 {
4760 curwin->w_cursor = orig_pos;
4761 if (VIsual_active)
4762 VIsual = save_VIsual;
4763 p_ws = old_p_ws;
4764 return FAIL;
4765 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004766 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004767 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004768 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004769 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004770 /* try again from start of buffer */
4771 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004772 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004773 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004774 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004775 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004776 /* searching backwards, so set pos to last line and col */
4777 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004778 pos.col = (colnr_T)STRLEN(
4779 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004780 }
4781 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004782 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004783 }
4784
4785 start_pos = pos;
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004786 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004787
4788 if (!VIsual_active)
4789 VIsual = start_pos;
4790
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004791 // put cursor on last character of match
4792 curwin->w_cursor = end_pos;
4793 if (LT_POS(VIsual, end_pos))
4794 dec_cursor();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004795 VIsual_active = TRUE;
4796 VIsual_mode = 'v';
4797
4798 if (VIsual_active)
4799 {
4800 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004801 if (*p_sel == 'e')
4802 {
4803 /* Correction for exclusive selection depends on the direction. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004804 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004805 inc_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004806 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004807 inc(&VIsual);
4808 }
4809
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004810 }
4811
4812#ifdef FEAT_FOLDING
4813 if (fdo_flags & FDO_SEARCH && KeyTyped)
4814 foldOpenCursor();
4815#endif
4816
4817 may_start_select('c');
4818#ifdef FEAT_MOUSE
4819 setmouse();
4820#endif
4821#ifdef FEAT_CLIPBOARD
4822 /* Make sure the clipboard gets updated. Needed because start and
4823 * end are still the same, and the selection needs to be owned */
4824 clip_star.vmode = NUL;
4825#endif
4826 redraw_curbuf_later(INVERTED);
4827 showmode();
4828
4829 return OK;
4830}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004831
4832/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004833 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004834 * If move is TRUE, check from the beginning of the buffer, else from position
4835 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004836 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004837 * Returns TRUE, FALSE or -1 for failure.
4838 */
4839 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004840is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004841{
4842 regmmatch_T regmatch;
4843 int nmatched = 0;
4844 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004845 pos_T pos;
4846 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004847 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004848
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004849 if (pattern == NULL)
4850 pattern = spats[last_idx].pat;
4851
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004852 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4853 SEARCH_KEEP, &regmatch) == FAIL)
4854 return -1;
4855
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004856 /* init startcol correctly */
4857 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004858 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004859 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004860 {
4861 CLEAR_POS(&pos);
4862 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004863 else
4864 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004865 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004866 /* accept a match at the cursor position */
4867 flag = SEARCH_START;
4868 }
4869
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004870 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004871 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004872 {
4873 /* Zero-width pattern should match somewhere, then we can check if
4874 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004875 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004876 do
4877 {
4878 regmatch.startpos[0].col++;
4879 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004880 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004881 if (!nmatched)
4882 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004883 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4884 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004885
4886 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004887 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004888 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004889 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4890 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004891 /* one char width */
4892 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4893 result = TRUE;
4894 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004895 }
4896
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004897 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004898 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004899 return result;
4900}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4903 || defined(PROTO)
4904/*
4905 * return TRUE if line 'lnum' is empty or has white chars only.
4906 */
4907 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004908linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
4910 char_u *p;
4911
4912 p = skipwhite(ml_get(lnum));
4913 return (*p == NUL);
4914}
4915#endif
4916
4917#if defined(FEAT_FIND_ID) || defined(PROTO)
4918/*
4919 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004920 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004923find_pattern_in_path(
4924 char_u *ptr, /* pointer to search pattern */
4925 int dir UNUSED, /* direction of expansion */
4926 int len, /* length of search pattern */
4927 int whole, /* match whole words only */
4928 int skip_comments, /* don't match inside comments */
4929 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004931 long count,
4932 int action, /* What to do when we find it */
4933 linenr_T start_lnum, /* first line to start searching */
4934 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936 SearchedFile *files; /* Stack of included files */
4937 SearchedFile *bigger; /* When we need more space */
4938 int max_path_depth = 50;
4939 long match_count = 1;
4940
4941 char_u *pat;
4942 char_u *new_fname;
4943 char_u *curr_fname = curbuf->b_fname;
4944 char_u *prev_fname = NULL;
4945 linenr_T lnum;
4946 int depth;
4947 int depth_displayed; /* For type==CHECK_PATH */
4948 int old_files;
4949 int already_searched;
4950 char_u *file_line;
4951 char_u *line;
4952 char_u *p;
4953 char_u save_char;
4954 int define_matched;
4955 regmatch_T regmatch;
4956 regmatch_T incl_regmatch;
4957 regmatch_T def_regmatch;
4958 int matched = FALSE;
4959 int did_show = FALSE;
4960 int found = FALSE;
4961 int i;
4962 char_u *already = NULL;
4963 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004964 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004965#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 win_T *curwin_save = NULL;
4967#endif
4968
4969 regmatch.regprog = NULL;
4970 incl_regmatch.regprog = NULL;
4971 def_regmatch.regprog = NULL;
4972
4973 file_line = alloc(LSIZE);
4974 if (file_line == NULL)
4975 return;
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (type != CHECK_PATH && type != FIND_DEFINE
4978#ifdef FEAT_INS_EXPAND
4979 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4980 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004981 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
4983 )
4984 {
4985 pat = alloc(len + 5);
4986 if (pat == NULL)
4987 goto fpip_end;
4988 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4989 /* ignore case according to p_ic, p_scs and pat */
4990 regmatch.rm_ic = ignorecase(pat);
4991 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4992 vim_free(pat);
4993 if (regmatch.regprog == NULL)
4994 goto fpip_end;
4995 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004996 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4997 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004999 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (incl_regmatch.regprog == NULL)
5001 goto fpip_end;
5002 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
5003 }
5004 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
5005 {
5006 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5007 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5008 if (def_regmatch.regprog == NULL)
5009 goto fpip_end;
5010 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5011 }
5012 files = (SearchedFile *)lalloc_clear((long_u)
5013 (max_path_depth * sizeof(SearchedFile)), TRUE);
5014 if (files == NULL)
5015 goto fpip_end;
5016 old_files = max_path_depth;
5017 depth = depth_displayed = -1;
5018
5019 lnum = start_lnum;
5020 if (end_lnum > curbuf->b_ml.ml_line_count)
5021 end_lnum = curbuf->b_ml.ml_line_count;
5022 if (lnum > end_lnum) /* do at least one line */
5023 lnum = end_lnum;
5024 line = ml_get(lnum);
5025
5026 for (;;)
5027 {
5028 if (incl_regmatch.regprog != NULL
5029 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5030 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005031 char_u *p_fname = (curr_fname == curbuf->b_fname)
5032 ? curbuf->b_ffname : curr_fname;
5033
5034 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5035 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5036 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005037 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005038 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5039 else
5040 /* Use text after match with 'include'. */
5041 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005042 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 already_searched = FALSE;
5044 if (new_fname != NULL)
5045 {
5046 /* Check whether we have already searched in this file */
5047 for (i = 0;; i++)
5048 {
5049 if (i == depth + 1)
5050 i = old_files;
5051 if (i == max_path_depth)
5052 break;
5053 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5054 {
5055 if (type != CHECK_PATH &&
5056 action == ACTION_SHOW_ALL && files[i].matched)
5057 {
5058 msg_putchar('\n'); /* cursor below last one */
5059 if (!got_int) /* don't display if 'q'
5060 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005061 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005064 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 prev_fname = NULL;
5066 }
5067 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01005068 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 already_searched = TRUE;
5070 break;
5071 }
5072 }
5073 }
5074
5075 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5076 || (new_fname == NULL && !already_searched)))
5077 {
5078 if (did_show)
5079 msg_putchar('\n'); /* cursor below last one */
5080 else
5081 {
5082 gotocmdline(TRUE); /* cursor at status line */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005083 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005085 msg_puts_title(_("not found "));
5086 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 did_show = TRUE;
5089 while (depth_displayed < depth && !got_int)
5090 {
5091 ++depth_displayed;
5092 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005093 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005095 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 if (!got_int) /* don't display if 'q' typed
5098 for "--more--" message */
5099 {
5100 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005101 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (new_fname != NULL)
5103 {
5104 /* using "new_fname" is more reliable, e.g., when
5105 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005106 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 else
5109 {
5110 /*
5111 * Isolate the file name.
5112 * Include the surrounding "" or <> if present.
5113 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005114 if (inc_opt != NULL
5115 && strstr((char *)inc_opt, "\\zs") != NULL)
5116 {
5117 /* pattern contains \zs, use the match */
5118 p = incl_regmatch.startp[0];
5119 i = (int)(incl_regmatch.endp[0]
5120 - incl_regmatch.startp[0]);
5121 }
5122 else
5123 {
5124 /* find the file name after the end of the match */
5125 for (p = incl_regmatch.endp[0];
5126 *p && !vim_isfilec(*p); p++)
5127 ;
5128 for (i = 0; vim_isfilec(p[i]); i++)
5129 ;
5130 }
5131
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (i == 0)
5133 {
5134 /* Nothing found, use the rest of the line. */
5135 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005136 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005138 /* Avoid checking before the start of the line, can
5139 * happen if \zs appears in the regexp. */
5140 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 if (p[-1] == '"' || p[-1] == '<')
5143 {
5144 --p;
5145 ++i;
5146 }
5147 if (p[i] == '"' || p[i] == '>')
5148 ++i;
5149 }
5150 save_char = p[i];
5151 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005152 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 p[i] = save_char;
5154 }
5155
5156 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5157 {
5158 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005159 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005161 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
5163 }
5164 out_flush(); /* output each line directly */
5165 }
5166
5167 if (new_fname != NULL)
5168 {
5169 /* Push the new file onto the file stack */
5170 if (depth + 1 == old_files)
5171 {
5172 bigger = (SearchedFile *)lalloc((long_u)(
5173 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5174 if (bigger != NULL)
5175 {
5176 for (i = 0; i <= depth; i++)
5177 bigger[i] = files[i];
5178 for (i = depth + 1; i < old_files + max_path_depth; i++)
5179 {
5180 bigger[i].fp = NULL;
5181 bigger[i].name = NULL;
5182 bigger[i].lnum = 0;
5183 bigger[i].matched = FALSE;
5184 }
5185 for (i = old_files; i < max_path_depth; i++)
5186 bigger[i + max_path_depth] = files[i];
5187 old_files += max_path_depth;
5188 max_path_depth *= 2;
5189 vim_free(files);
5190 files = bigger;
5191 }
5192 }
5193 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5194 == NULL)
5195 vim_free(new_fname);
5196 else
5197 {
5198 if (++depth == old_files)
5199 {
5200 /*
5201 * lalloc() for 'bigger' must have failed above. We
5202 * will forget one of our already visited files now.
5203 */
5204 vim_free(files[old_files].name);
5205 ++old_files;
5206 }
5207 files[depth].name = curr_fname = new_fname;
5208 files[depth].lnum = 0;
5209 files[depth].matched = FALSE;
5210#ifdef FEAT_INS_EXPAND
5211 if (action == ACTION_EXPAND)
5212 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005213 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005214 vim_snprintf((char*)IObuff, IOSIZE,
5215 _("Scanning included file: %s"),
5216 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005217 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005219 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005221 if (p_verbose >= 5)
5222 {
5223 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005224 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005225 (char *)new_fname);
5226 verbose_leave();
5227 }
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 }
5231 }
5232 else
5233 {
5234 /*
5235 * Check if the line is a define (type == FIND_DEFINE)
5236 */
5237 p = line;
5238search_line:
5239 define_matched = FALSE;
5240 if (def_regmatch.regprog != NULL
5241 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5242 {
5243 /*
5244 * Pattern must be first identifier after 'define', so skip
5245 * to that position before checking for match of pattern. Also
5246 * don't let it match beyond the end of this identifier.
5247 */
5248 p = def_regmatch.endp[0];
5249 while (*p && !vim_iswordc(*p))
5250 p++;
5251 define_matched = TRUE;
5252 }
5253
5254 /*
5255 * Look for a match. Don't do this if we are looking for a
5256 * define and this line didn't match define_prog above.
5257 */
5258 if (def_regmatch.regprog == NULL || define_matched)
5259 {
5260 if (define_matched
5261#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263#endif
5264 )
5265 {
5266 /* compare the first "len" chars from "ptr" */
5267 startp = skipwhite(p);
5268 if (p_ic)
5269 matched = !MB_STRNICMP(startp, ptr, len);
5270 else
5271 matched = !STRNCMP(startp, ptr, len);
5272 if (matched && define_matched && whole
5273 && vim_iswordc(startp[len]))
5274 matched = FALSE;
5275 }
5276 else if (regmatch.regprog != NULL
5277 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5278 {
5279 matched = TRUE;
5280 startp = regmatch.startp[0];
5281 /*
5282 * Check if the line is not a comment line (unless we are
5283 * looking for a define). A line starting with "# define"
5284 * is not considered to be a comment line.
5285 */
5286 if (!define_matched && skip_comments)
5287 {
5288#ifdef FEAT_COMMENTS
5289 if ((*line != '#' ||
5290 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005291 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 matched = FALSE;
5293
5294 /*
5295 * Also check for a "/ *" or "/ /" before the match.
5296 * Skips lines like "int backwards; / * normal index
5297 * * /" when looking for "normal".
5298 * Note: Doesn't skip "/ *" in comments.
5299 */
5300 p = skipwhite(line);
5301 if (matched
5302 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5303#endif
5304 for (p = line; *p && p < startp; ++p)
5305 {
5306 if (matched
5307 && p[0] == '/'
5308 && (p[1] == '*' || p[1] == '/'))
5309 {
5310 matched = FALSE;
5311 /* After "//" all text is comment */
5312 if (p[1] == '/')
5313 break;
5314 ++p;
5315 }
5316 else if (!matched && p[0] == '*' && p[1] == '/')
5317 {
5318 /* Can find match after "* /". */
5319 matched = TRUE;
5320 ++p;
5321 }
5322 }
5323 }
5324 }
5325 }
5326 }
5327 if (matched)
5328 {
5329#ifdef FEAT_INS_EXPAND
5330 if (action == ACTION_EXPAND)
5331 {
5332 int reuse = 0;
5333 int add_r;
5334 char_u *aux;
5335
5336 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5337 break;
5338 found = TRUE;
5339 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005342 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (vim_iswordp(p))
5344 goto exit_matched;
5345 p = find_word_start(p);
5346 }
5347 p = find_word_end(p);
5348 i = (int)(p - aux);
5349
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005350 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005354
5355 /* Get the next line: when "depth" < 0 from the current
5356 * buffer, otherwise from the included file. Jump to
5357 * exit_matched when past the last line. */
5358 if (depth < 0)
5359 {
5360 if (lnum >= end_lnum)
5361 goto exit_matched;
5362 line = ml_get(++lnum);
5363 }
5364 else if (vim_fgets(line = file_line,
5365 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 goto exit_matched;
5367
5368 /* we read a line, set "already" to check this "line" later
5369 * if depth >= 0 we'll increase files[depth].lnum far
5370 * bellow -- Acevedo */
5371 already = aux = p = skipwhite(line);
5372 p = find_word_start(p);
5373 p = find_word_end(p);
5374 if (p > aux)
5375 {
5376 if (*aux != ')' && IObuff[i-1] != TAB)
5377 {
5378 if (IObuff[i-1] != ' ')
5379 IObuff[i++] = ' ';
5380 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5381 if (p_js
5382 && (IObuff[i-2] == '.'
5383 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5384 && (IObuff[i-2] == '?'
5385 || IObuff[i-2] == '!'))))
5386 IObuff[i++] = ' ';
5387 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005388 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 if (p - aux >= IOSIZE - i)
5390 p = aux + IOSIZE - i - 1;
5391 STRNCPY(IObuff + i, aux, p - aux);
5392 i += (int)(p - aux);
5393 reuse |= CONT_S_IPOS;
5394 }
5395 IObuff[i] = NUL;
5396 aux = IObuff;
5397
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005398 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 goto exit_matched;
5400 }
5401
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005402 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5404 dir, reuse);
5405 if (add_r == OK)
5406 /* if dir was BACKWARD then honor it just once */
5407 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005408 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 break;
5410 }
5411 else
5412#endif
5413 if (action == ACTION_SHOW_ALL)
5414 {
5415 found = TRUE;
5416 if (!did_show)
5417 gotocmdline(TRUE); /* cursor at status line */
5418 if (curr_fname != prev_fname)
5419 {
5420 if (did_show)
5421 msg_putchar('\n'); /* cursor below last one */
5422 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005423 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 msg_home_replace_hl(curr_fname);
5425 prev_fname = curr_fname;
5426 }
5427 did_show = TRUE;
5428 if (!got_int)
5429 show_pat_in_path(line, type, TRUE, action,
5430 (depth == -1) ? NULL : files[depth].fp,
5431 (depth == -1) ? &lnum : &files[depth].lnum,
5432 match_count++);
5433
5434 /* Set matched flag for this file and all the ones that
5435 * include it */
5436 for (i = 0; i <= depth; ++i)
5437 files[i].matched = TRUE;
5438 }
5439 else if (--count <= 0)
5440 {
5441 found = TRUE;
5442 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005443#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 && g_do_tagpreview == 0
5445#endif
5446 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005447 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 else if (action == ACTION_SHOW)
5449 {
5450 show_pat_in_path(line, type, did_show, action,
5451 (depth == -1) ? NULL : files[depth].fp,
5452 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5453 did_show = TRUE;
5454 }
5455 else
5456 {
5457#ifdef FEAT_GUI
5458 need_mouse_correct = TRUE;
5459#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005460#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 /* ":psearch" uses the preview window */
5462 if (g_do_tagpreview != 0)
5463 {
5464 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005465 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 }
5467#endif
5468 if (action == ACTION_SPLIT)
5469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005472 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 if (depth == -1)
5475 {
5476 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005477#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 if (g_do_tagpreview != 0)
5479 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005480 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005481 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005482 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 break; /* failed to jump to file */
5484 }
5485 else
5486#endif
5487 setpcmark();
5488 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005489 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
5491 else
5492 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005493 if (!GETFILE_SUCCESS(getfile(
5494 0, files[depth].name, NULL, TRUE,
5495 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 break; /* failed to jump to file */
5497 /* autocommands may have changed the lnum, we don't
5498 * want that here */
5499 curwin->w_cursor.lnum = files[depth].lnum;
5500 }
5501 }
5502 if (action != ACTION_SHOW)
5503 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005504 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 curwin->w_set_curswant = TRUE;
5506 }
5507
Bram Moolenaar4033c552017-09-16 20:54:51 +02005508#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005510 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
5512 /* Return cursor to where we were */
5513 validate_cursor();
5514 redraw_later(VALID);
5515 win_enter(curwin_save, TRUE);
5516 }
5517#endif
5518 break;
5519 }
5520#ifdef FEAT_INS_EXPAND
5521exit_matched:
5522#endif
5523 matched = FALSE;
5524 /* look for other matches in the rest of the line if we
5525 * are not at the end of it already */
5526 if (def_regmatch.regprog == NULL
5527#ifdef FEAT_INS_EXPAND
5528 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005529 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005531 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005532 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 goto search_line;
5534 }
5535 line_breakcheck();
5536#ifdef FEAT_INS_EXPAND
5537 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005538 ins_compl_check_keys(30, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005539 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#else
5541 if (got_int)
5542#endif
5543 break;
5544
5545 /*
5546 * Read the next line. When reading an included file and encountering
5547 * end-of-file, close the file and continue in the file that included
5548 * it.
5549 */
5550 while (depth >= 0 && !already
5551 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5552 {
5553 fclose(files[depth].fp);
5554 --old_files;
5555 files[old_files].name = files[depth].name;
5556 files[old_files].matched = files[depth].matched;
5557 --depth;
5558 curr_fname = (depth == -1) ? curbuf->b_fname
5559 : files[depth].name;
5560 if (depth < depth_displayed)
5561 depth_displayed = depth;
5562 }
5563 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005564 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005566 /* Remove any CR and LF from the line. */
5567 i = (int)STRLEN(line);
5568 if (i > 0 && line[i - 1] == '\n')
5569 line[--i] = NUL;
5570 if (i > 0 && line[i - 1] == '\r')
5571 line[--i] = NUL;
5572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 else if (!already)
5574 {
5575 if (++lnum > end_lnum)
5576 break;
5577 line = ml_get(lnum);
5578 }
5579 already = NULL;
5580 }
5581 /* End of big for (;;) loop. */
5582
5583 /* Close any files that are still open. */
5584 for (i = 0; i <= depth; i++)
5585 {
5586 fclose(files[i].fp);
5587 vim_free(files[i].name);
5588 }
5589 for (i = old_files; i < max_path_depth; i++)
5590 vim_free(files[i].name);
5591 vim_free(files);
5592
5593 if (type == CHECK_PATH)
5594 {
5595 if (!did_show)
5596 {
5597 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005598 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005600 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602 }
5603 else if (!found
5604#ifdef FEAT_INS_EXPAND
5605 && action != ACTION_EXPAND
5606#endif
5607 )
5608 {
5609#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005610 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611#else
5612 if (got_int)
5613#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005616 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005618 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5621 msg_end();
5622
5623fpip_end:
5624 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005625 vim_regfree(regmatch.regprog);
5626 vim_regfree(incl_regmatch.regprog);
5627 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628}
5629
5630 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005631show_pat_in_path(
5632 char_u *line,
5633 int type,
5634 int did_show,
5635 int action,
5636 FILE *fp,
5637 linenr_T *lnum,
5638 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
5640 char_u *p;
5641
5642 if (did_show)
5643 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005644 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 gotocmdline(TRUE); /* cursor at status line */
5646 if (got_int) /* 'q' typed at "--more--" message */
5647 return;
5648 for (;;)
5649 {
5650 p = line + STRLEN(line) - 1;
5651 if (fp != NULL)
5652 {
5653 /* We used fgets(), so get rid of newline at end */
5654 if (p >= line && *p == '\n')
5655 --p;
5656 if (p >= line && *p == '\r')
5657 --p;
5658 *(p + 1) = NUL;
5659 }
5660 if (action == ACTION_SHOW_ALL)
5661 {
5662 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005663 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5665 /* Highlight line numbers */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005666 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
5667 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005669 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 out_flush(); /* show one line at a time */
5671
5672 /* Definition continues until line that doesn't end with '\' */
5673 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5674 break;
5675
5676 if (fp != NULL)
5677 {
5678 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5679 break;
5680 ++*lnum;
5681 }
5682 else
5683 {
5684 if (++*lnum > curbuf->b_ml.ml_line_count)
5685 break;
5686 line = ml_get(*lnum);
5687 }
5688 msg_putchar('\n');
5689 }
5690}
5691#endif
5692
5693#ifdef FEAT_VIMINFO
5694 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005695read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 char_u *lp;
5698 int idx = -1;
5699 int magic = FALSE;
5700 int no_scs = FALSE;
5701 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005702 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 long off = 0;
5704 int setlast = FALSE;
5705#ifdef FEAT_SEARCH_EXTRA
5706 static int hlsearch_on = FALSE;
5707#endif
5708 char_u *val;
5709
5710 /*
5711 * Old line types:
5712 * "/pat", "&pat": search/subst. pat
5713 * "~/pat", "~&pat": last used search/subst. pat
5714 * New line types:
5715 * "~h", "~H": hlsearch highlighting off/on
5716 * "~<magic><smartcase><line><end><off><last><which>pat"
5717 * <magic>: 'm' off, 'M' on
5718 * <smartcase>: 's' off, 'S' on
5719 * <line>: 'L' line offset, 'l' char offset
5720 * <end>: 'E' from end, 'e' from start
5721 * <off>: decimal, offset
5722 * <last>: '~' last used pattern
5723 * <which>: '/' search pat, '&' subst. pat
5724 */
5725 lp = virp->vir_line;
5726 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5727 {
5728 if (lp[1] == 'M') /* magic on */
5729 magic = TRUE;
5730 if (lp[2] == 's')
5731 no_scs = TRUE;
5732 if (lp[3] == 'L')
5733 off_line = TRUE;
5734 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005735 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 lp += 5;
5737 off = getdigits(&lp);
5738 }
5739 if (lp[0] == '~') /* use this pattern for last-used pattern */
5740 {
5741 setlast = TRUE;
5742 lp++;
5743 }
5744 if (lp[0] == '/')
5745 idx = RE_SEARCH;
5746 else if (lp[0] == '&')
5747 idx = RE_SUBST;
5748#ifdef FEAT_SEARCH_EXTRA
5749 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5750 hlsearch_on = FALSE;
5751 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5752 hlsearch_on = TRUE;
5753#endif
5754 if (idx >= 0)
5755 {
5756 if (force || spats[idx].pat == NULL)
5757 {
5758 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5759 TRUE);
5760 if (val != NULL)
5761 {
5762 set_last_search_pat(val, idx, magic, setlast);
5763 vim_free(val);
5764 spats[idx].no_scs = no_scs;
5765 spats[idx].off.line = off_line;
5766 spats[idx].off.end = off_end;
5767 spats[idx].off.off = off;
5768#ifdef FEAT_SEARCH_EXTRA
5769 if (setlast)
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02005770 set_no_hlsearch(!hlsearch_on);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#endif
5772 }
5773 }
5774 }
5775 return viminfo_readline(virp);
5776}
5777
5778 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005779write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 if (get_viminfo_parameter('/') != 0)
5782 {
5783#ifdef FEAT_SEARCH_EXTRA
5784 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5785 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5786#endif
5787 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005788 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790}
5791
5792 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005793wvsp_one(
5794 FILE *fp, /* file to write to */
5795 int idx, /* spats[] index */
5796 char *s, /* search pat */
5797 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 if (spats[idx].pat != NULL)
5800 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005801 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 /* off.dir is not stored, it's reset to forward */
5803 fprintf(fp, "%c%c%c%c%ld%s%c",
5804 spats[idx].magic ? 'M' : 'm', /* magic */
5805 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5806 spats[idx].off.line ? 'L' : 'l', /* line offset */
5807 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5808 spats[idx].off.off, /* offset */
5809 last_idx == idx ? "~" : "", /* last used pat */
5810 sc);
5811 viminfo_writestring(fp, spats[idx].pat);
5812 }
5813}
5814#endif /* FEAT_VIMINFO */