blob: 63171a92c0a214a14e38ee69442252909b652591 [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
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +020026static void search_stat(int dirc, pos_T *pos, int show_top_bot_msg, char_u *msgbuf, int recompute);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
Bram Moolenaar071d4272004-06-13 20:20:40 +000028/*
29 * This file contains various searching-related routines. These fall into
30 * three groups:
31 * 1. string searches (for /, ?, n, and N)
32 * 2. character searches within a single line (for f, F, t, T, etc)
33 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
34 */
35
36/*
37 * String searches
38 *
39 * The string search functions are divided into two levels:
40 * lowest: searchit(); uses an pos_T for starting position and found match.
41 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
42 *
43 * The last search pattern is remembered for repeating the same search.
44 * This pattern is shared between the :g, :s, ? and / commands.
45 * This is in search_regcomp().
46 *
47 * The actual string matching is done using a heavily modified version of
48 * Henry Spencer's regular expression library. See regexp.c.
49 */
50
Bram Moolenaar071d4272004-06-13 20:20:40 +000051/*
52 * Two search patterns are remembered: One for the :substitute command and
53 * one for other searches. last_idx points to the one that was used the last
54 * time.
55 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020056static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000057{
58 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
59 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
60};
61
62static int last_idx = 0; /* index in spats[] for RE_LAST */
63
Bram Moolenaardbd24b52015-08-11 14:26:19 +020064static char_u lastc[2] = {NUL, NUL}; /* last character searched for */
65static int lastcdir = FORWARD; /* last direction of character search */
66static int last_t_cmd = TRUE; /* last search t_cmd */
Bram Moolenaardbd24b52015-08-11 14:26:19 +020067static char_u lastc_bytes[MB_MAXBYTES + 1];
68static int lastc_bytelen = 1; /* >1 for multi-byte char */
Bram Moolenaardbd24b52015-08-11 14:26:19 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/* copy of spats[], for keeping the search patterns while executing autocmds */
Bram Moolenaarc3328162019-07-23 22:15:25 +020071static spat_T saved_spats[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +010073static int saved_spats_last_idx = 0;
74static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
77static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
78#ifdef FEAT_RIGHTLEFT
79static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
81
82#ifdef FEAT_FIND_ID
83/*
84 * Type used by find_pattern_in_path() to remember which included files have
85 * been searched already.
86 */
87typedef struct SearchedFile
88{
89 FILE *fp; /* File pointer */
90 char_u *name; /* Full name of file */
91 linenr_T lnum; /* Line we were up to in file */
92 int matched; /* Found a match in this file */
93} SearchedFile;
94#endif
95
96/*
97 * translate search pattern for vim_regcomp()
98 *
99 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
100 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
101 * pat_save == RE_BOTH: save pat in both patterns (:global command)
102 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000103 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
105 * options & SEARCH_HIS: put search string in history
106 * options & SEARCH_KEEP: keep previous search pattern
107 *
108 * returns FAIL if failed, OK otherwise.
109 */
110 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100111search_regcomp(
112 char_u *pat,
113 int pat_save,
114 int pat_use,
115 int options,
116 regmmatch_T *regmatch) /* return: pattern and ignore-case flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117{
118 int magic;
119 int i;
120
121 rc_did_emsg = FALSE;
122 magic = p_magic;
123
124 /*
125 * If no pattern given, use a previously defined pattern.
126 */
127 if (pat == NULL || *pat == NUL)
128 {
129 if (pat_use == RE_LAST)
130 i = last_idx;
131 else
132 i = pat_use;
133 if (spats[i].pat == NULL) /* pattern was never defined */
134 {
135 if (pat_use == RE_SUBST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100136 emsg(_(e_nopresub));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100138 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 rc_did_emsg = TRUE;
140 return FAIL;
141 }
142 pat = spats[i].pat;
143 magic = spats[i].magic;
144 no_smartcase = spats[i].no_scs;
145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 else if (options & SEARCH_HIS) /* put new pattern in history */
147 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149#ifdef FEAT_RIGHTLEFT
150 if (mr_pattern_alloced)
151 {
152 vim_free(mr_pattern);
153 mr_pattern_alloced = FALSE;
154 }
155
156 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
157 {
158 char_u *rev_pattern;
159
160 rev_pattern = reverse_text(pat);
161 if (rev_pattern == NULL)
162 mr_pattern = pat; /* out of memory, keep normal pattern. */
163 else
164 {
165 mr_pattern = rev_pattern;
166 mr_pattern_alloced = TRUE;
167 }
168 }
169 else
170#endif
171 mr_pattern = pat;
172
173 /*
174 * Save the currently used pattern in the appropriate place,
175 * unless the pattern should not be remembered.
176 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100177 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 {
179 /* search or global command */
180 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
181 save_re_pat(RE_SEARCH, pat, magic);
182 /* substitute or global command */
183 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
184 save_re_pat(RE_SUBST, pat, magic);
185 }
186
187 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000188 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
190 if (regmatch->regprog == NULL)
191 return FAIL;
192 return OK;
193}
194
195/*
196 * Get search pattern used by search_regcomp().
197 */
198 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100199get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
201 return mr_pattern;
202}
203
Bram Moolenaarabc97732007-08-08 20:49:37 +0000204#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205/*
206 * Reverse text into allocated memory.
207 * Returns the allocated string, NULL when out of memory.
208 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000209 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100210reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 unsigned len;
213 unsigned s_i, rev_i;
214 char_u *rev;
215
216 /*
217 * Reverse the pattern.
218 */
219 len = (unsigned)STRLEN(s);
220 rev = alloc(len + 1);
221 if (rev != NULL)
222 {
223 rev_i = len;
224 for (s_i = 0; s_i < len; ++s_i)
225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (has_mbyte)
227 {
228 int mb_len;
229
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000230 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 rev_i -= mb_len;
232 mch_memmove(rev + rev_i, s + s_i, mb_len);
233 s_i += mb_len - 1;
234 }
235 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 rev[--rev_i] = s[s_i];
237
238 }
239 rev[len] = NUL;
240 }
241 return rev;
242}
243#endif
244
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100245 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100246save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247{
248 if (spats[idx].pat != pat)
249 {
250 vim_free(spats[idx].pat);
251 spats[idx].pat = vim_strsave(pat);
252 spats[idx].magic = magic;
253 spats[idx].no_scs = no_smartcase;
254 last_idx = idx;
255#ifdef FEAT_SEARCH_EXTRA
256 /* If 'hlsearch' set and search pat changed: need redraw. */
257 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000258 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200259 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#endif
261 }
262}
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264/*
265 * Save the search patterns, so they can be restored later.
266 * Used before/after executing autocommands and user functions.
267 */
268static int save_level = 0;
269
270 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100271save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272{
273 if (save_level++ == 0)
274 {
275 saved_spats[0] = spats[0];
276 if (spats[0].pat != NULL)
277 saved_spats[0].pat = vim_strsave(spats[0].pat);
278 saved_spats[1] = spats[1];
279 if (spats[1].pat != NULL)
280 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100281#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100282 saved_spats_last_idx = last_idx;
283 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 }
286}
287
288 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100289restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290{
291 if (--save_level == 0)
292 {
293 vim_free(spats[0].pat);
294 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100295#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000296 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 vim_free(spats[1].pat);
299 spats[1] = saved_spats[1];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100300#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100301 last_idx = saved_spats_last_idx;
302 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 }
305}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000307#if defined(EXITFREE) || defined(PROTO)
308 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100309free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000310{
311 vim_free(spats[0].pat);
312 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000313
314# ifdef FEAT_RIGHTLEFT
315 if (mr_pattern_alloced)
316 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200317 vim_free(mr_pattern);
318 mr_pattern_alloced = FALSE;
319 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000320 }
321# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000322}
323#endif
324
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100325#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100326// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
327// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200328static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100329static int did_save_last_search_spat = 0;
330static int saved_last_idx = 0;
331static int saved_no_hlsearch = 0;
332
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100333/*
334 * Save and restore the search pattern for incremental highlight search
335 * feature.
336 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100337 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100338 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100339 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100340 */
341 void
342save_last_search_pattern(void)
343{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100344 if (did_save_last_search_spat != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 iemsg("did_save_last_search_spat is not zero");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100346 else
347 ++did_save_last_search_spat;
348
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100349 saved_last_search_spat = spats[RE_SEARCH];
350 if (spats[RE_SEARCH].pat != NULL)
351 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
352 saved_last_idx = last_idx;
353 saved_no_hlsearch = no_hlsearch;
354}
355
356 void
357restore_last_search_pattern(void)
358{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100359 if (did_save_last_search_spat != 1)
360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361 iemsg("did_save_last_search_spat is not one");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100362 return;
363 }
364 --did_save_last_search_spat;
365
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100366 vim_free(spats[RE_SEARCH].pat);
367 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100368 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100369# if defined(FEAT_EVAL)
370 set_vv_searchforward();
371# endif
372 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200373 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100374}
Bram Moolenaard0480092017-11-16 22:20:39 +0100375
376 char_u *
377last_search_pattern(void)
378{
379 return spats[RE_SEARCH].pat;
380}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100381#endif
382
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383/*
384 * Return TRUE when case should be ignored for search pattern "pat".
385 * Uses the 'ignorecase' and 'smartcase' options.
386 */
387 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100388ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200390 return ignorecase_opt(pat, p_ic, p_scs);
391}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200393/*
394 * As ignorecase() put pass the "ic" and "scs" flags.
395 */
396 int
397ignorecase_opt(char_u *pat, int ic_in, int scs)
398{
399 int ic = ic_in;
400
401 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200402 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200403 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 no_smartcase = FALSE;
405
406 return ic;
407}
408
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200409/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200410 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200411 */
412 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100413pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200414{
415 char_u *p = pat;
416
417 while (*p != NUL)
418 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200419 int l;
420
421 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
422 {
423 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
424 return TRUE;
425 p += l;
426 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100427 else if (*p == '\\')
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200428 {
429 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
430 p += 3;
431 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
432 p += 3;
433 else if (p[1] != NUL) /* skip "\X" */
434 p += 2;
435 else
436 p += 1;
437 }
438 else if (MB_ISUPPER(*p))
439 return TRUE;
440 else
441 ++p;
442 }
443 return FALSE;
444}
445
Bram Moolenaar113e1072019-01-20 15:30:40 +0100446#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100448last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200449{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200450 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200451}
452
453 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100454last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200455{
456 return lastcdir == FORWARD;
457}
458
459 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100460last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200461{
462 return last_t_cmd == TRUE;
463}
464
465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100466set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200467{
468 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200469 lastc_bytelen = len;
470 if (len)
471 memcpy(lastc_bytes, s, len);
472 else
473 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200474}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100475#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200476
477 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100478set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200479{
480 lastcdir = cdir;
481}
482
483 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100484set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200485{
486 last_t_cmd = t_cmd;
487}
488
489 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100490last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 return spats[last_idx].pat;
493}
494
495/*
496 * Reset search direction to forward. For "gd" and "gD" commands.
497 */
498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100499reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#if defined(FEAT_EVAL)
503 set_vv_searchforward();
504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505}
506
507#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
508/*
509 * Set the last search pattern. For ":let @/ =" and viminfo.
510 * Also set the saved search pattern, so that this works in an autocommand.
511 */
512 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100513set_last_search_pat(
514 char_u *s,
515 int idx,
516 int magic,
517 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 vim_free(spats[idx].pat);
520 /* An empty string means that nothing should be matched. */
521 if (*s == NUL)
522 spats[idx].pat = NULL;
523 else
524 spats[idx].pat = vim_strsave(s);
525 spats[idx].magic = magic;
526 spats[idx].no_scs = FALSE;
527 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#if defined(FEAT_EVAL)
529 set_vv_searchforward();
530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 spats[idx].off.line = FALSE;
532 spats[idx].off.end = FALSE;
533 spats[idx].off.off = 0;
534 if (setlast)
535 last_idx = idx;
536 if (save_level)
537 {
538 vim_free(saved_spats[idx].pat);
539 saved_spats[idx] = spats[0];
540 if (spats[idx].pat == NULL)
541 saved_spats[idx].pat = NULL;
542 else
543 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100544# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100545 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
548# ifdef FEAT_SEARCH_EXTRA
549 /* If 'hlsearch' set and search pat changed: need redraw. */
550 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000551 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552# endif
553}
554#endif
555
556#ifdef FEAT_SEARCH_EXTRA
557/*
558 * Get a regexp program for the last used search pattern.
559 * This is used for highlighting all matches in a window.
560 * Values returned in regmatch->regprog and regmatch->rmm_ic.
561 */
562 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100563last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 if (spats[last_idx].pat == NULL)
566 {
567 regmatch->regprog = NULL;
568 return;
569 }
570 ++emsg_off; /* So it doesn't beep if bad expr */
571 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
572 --emsg_off;
573}
574#endif
575
576/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100577 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100578 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
579 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 *
581 * if (options & SEARCH_MSG) == 0 don't give any messages
582 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
583 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
584 * if (options & SEARCH_HIS) put search pattern in history
585 * if (options & SEARCH_END) return position at end of match
586 * if (options & SEARCH_START) accept match at pos itself
587 * if (options & SEARCH_KEEP) keep previous search pattern
588 * if (options & SEARCH_FOLD) match only once in a closed fold
589 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100590 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 *
592 * Return FAIL (zero) for failure, non-zero for success.
593 * When FEAT_EVAL is defined, returns the index of the first matching
594 * subpattern plus one; one if there was none.
595 */
596 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100597searchit(
598 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100600 buf_T *buf,
601 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100602 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100603 int dir,
604 char_u *pat,
605 long count,
606 int options,
607 int pat_use, /* which pattern to use when "pat" is empty */
608 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200609 proftime_T *tm UNUSED, /* timeout limit or NULL */
610 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 int found;
613 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100614 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 regmmatch_T regmatch;
616 char_u *ptr;
617 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000619 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 int loop;
621 pos_T start_pos;
622 int at_first_line;
623 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200624 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 int match_ok;
626 long nmatched;
627 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100628 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000629 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#ifdef FEAT_SEARCH_EXTRA
631 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633
634 if (search_regcomp(pat, RE_SEARCH, pat_use,
635 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
636 {
637 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100638 semsg(_("E383: Invalid search string: %s"), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 return FAIL;
640 }
641
Bram Moolenaar280f1262006-01-30 00:14:18 +0000642 /*
643 * find the string
644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 called_emsg = FALSE;
646 do /* loop for count */
647 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100648 /* When not accepting a match at the start position set "extra_col" to
649 * a non-zero value. Don't do that when starting at MAXCOL, since
650 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200651 if (pos->col == MAXCOL)
652 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100653 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200654 else if (has_mbyte
655 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
656 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100657 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100658 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100659 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200660 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100661 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100662 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100663 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100664 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200665 start_char_len = 1;
666 if (dir == FORWARD)
667 {
668 if (options & SEARCH_START)
669 extra_col = 0;
670 else
671 extra_col = start_char_len;
672 }
673 else
674 {
675 if (options & SEARCH_START)
676 extra_col = start_char_len;
677 else
678 extra_col = 0;
679 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 start_pos = *pos; /* remember start pos for detecting no match */
682 found = 0; /* default: not found */
683 at_first_line = TRUE; /* default: start in first line */
684 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
685 {
686 pos->lnum = 1;
687 pos->col = 0;
688 at_first_line = FALSE; /* not in first line now */
689 }
690
691 /*
692 * Start searching in current line, unless searching backwards and
693 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000694 * If we are searching backwards, in column 0, and not including the
695 * current position, gain some efficiency by skipping back a line.
696 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000698 if (dir == BACKWARD && start_pos.col == 0
699 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 lnum = pos->lnum - 1;
702 at_first_line = FALSE;
703 }
704 else
705 lnum = pos->lnum;
706
707 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
708 {
709 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
710 lnum += dir, at_first_line = FALSE)
711 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000712 /* Stop after checking "stop_lnum", if it's set. */
713 if (stop_lnum != 0 && (dir == FORWARD
714 ? lnum > stop_lnum : lnum < stop_lnum))
715 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000716#ifdef FEAT_RELTIME
717 /* Stop after passing the "tm" time limit. */
718 if (tm != NULL && profile_passed_limit(tm))
719 break;
720#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000723 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100725 col = at_first_line && (options & SEARCH_COL) ? pos->col
726 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100728 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000729#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200730 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000731#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200732 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000733#endif
734 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200736 if (called_emsg
737#ifdef FEAT_RELTIME
738 || (timed_out != NULL && *timed_out)
739#endif
740 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 break;
742 if (nmatched > 0)
743 {
744 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000745 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000747#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000749#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000750 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000751 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
752 ptr = (char_u *)"";
753 else
754 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756 /*
757 * Forward search in the first line: match should be after
758 * the start position. If not, continue at the end of the
759 * match (this is vi compatible) or on the next char.
760 */
761 if (dir == FORWARD && at_first_line)
762 {
763 match_ok = TRUE;
764 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000765 * When the match starts in a next line it's certainly
766 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 * When match lands on a NUL the cursor will be put
768 * one back afterwards, compare with that position,
769 * otherwise "/$" will get stuck on end of line.
770 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000771 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100772 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000773 ? (nmatched == 1
774 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000776 : ((int)matchpos.col
777 - (ptr[matchpos.col] == NUL)
778 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
780 /*
781 * If vi-compatible searching, continue at the end
782 * of the match, otherwise continue one position
783 * forward.
784 */
785 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
786 {
787 if (nmatched > 1)
788 {
789 /* end is in next line, thus no match in
790 * this line */
791 match_ok = FALSE;
792 break;
793 }
794 matchcol = endpos.col;
795 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000796 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 && ptr[matchcol] != NUL)
798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (has_mbyte)
800 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000801 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 ++matchcol;
804 }
805 }
806 else
807 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000808 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 if (ptr[matchcol] != NUL)
810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000812 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 + matchcol);
814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 ++matchcol;
816 }
817 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200818 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (ptr[matchcol] == NUL
821 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000822 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000823 matchcol,
824#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200825 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000826#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200827 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000828#endif
829 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 {
831 match_ok = FALSE;
832 break;
833 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000834 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 endpos = regmatch.endpos[0];
836# ifdef FEAT_EVAL
837 submatch = first_submatch(&regmatch);
838# endif
839
840 /* Need to get the line pointer again, a
841 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000842 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 if (!match_ok)
845 continue;
846 }
847 if (dir == BACKWARD)
848 {
849 /*
850 * Now, if there are multiple matches on this line,
851 * we have to get the last one. Or the last one before
852 * the cursor, if we're on that line.
853 * When putting the new cursor at the end, compare
854 * relative to the end of the match.
855 */
856 match_ok = FALSE;
857 for (;;)
858 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000859 /* Remember a position that is before the start
860 * position, we use it if it's the last match in
861 * the line. Always accept a position after
862 * wrapping around. */
863 if (loop
864 || ((options & SEARCH_END)
865 ? (lnum + regmatch.endpos[0].lnum
866 < start_pos.lnum
867 || (lnum + regmatch.endpos[0].lnum
868 == start_pos.lnum
869 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200870 < (int)start_pos.col
871 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000872 : (lnum + regmatch.startpos[0].lnum
873 < start_pos.lnum
874 || (lnum + regmatch.startpos[0].lnum
875 == start_pos.lnum
876 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200877 < (int)start_pos.col
878 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000881 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 endpos = regmatch.endpos[0];
883# ifdef FEAT_EVAL
884 submatch = first_submatch(&regmatch);
885# endif
886 }
887 else
888 break;
889
890 /*
891 * We found a valid match, now check if there is
892 * another one after it.
893 * If vi-compatible searching, continue at the end
894 * of the match, otherwise continue one position
895 * forward.
896 */
897 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
898 {
899 if (nmatched > 1)
900 break;
901 matchcol = endpos.col;
902 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000903 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 && ptr[matchcol] != NUL)
905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (has_mbyte)
907 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000908 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 ++matchcol;
911 }
912 }
913 else
914 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000915 /* Stop when the match is in a next line. */
916 if (matchpos.lnum > 0)
917 break;
918 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (ptr[matchcol] != NUL)
920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (has_mbyte)
922 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000923 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 ++matchcol;
926 }
927 }
928 if (ptr[matchcol] == NUL
929 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000930 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000931 matchcol,
932#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200933 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000934#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200935 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000936#endif
937 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100938 {
939#ifdef FEAT_RELTIME
940 /* If the search timed out, we did find a match
941 * but it might be the wrong one, so that's not
942 * OK. */
943 if (timed_out != NULL && *timed_out)
944 match_ok = FALSE;
945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 /* Need to get the line pointer again, a
950 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000951 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953
954 /*
955 * If there is only a match after the cursor, skip
956 * this match.
957 */
958 if (!match_ok)
959 continue;
960 }
961
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000962 /* With the SEARCH_END option move to the last character
963 * of the match. Don't do it for an empty match, end
964 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200965 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000966 && !(matchpos.lnum == endpos.lnum
967 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000969 /* For a match in the first column, set the position
970 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000971 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000972 pos->col = endpos.col;
973 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000974 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000975 if (pos->lnum > 1) /* just in case */
976 {
977 --pos->lnum;
978 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
979 pos->lnum, FALSE));
980 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000981 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000982 else
983 {
984 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000985 if (has_mbyte
986 && pos->lnum <= buf->b_ml.ml_line_count)
987 {
988 ptr = ml_get_buf(buf, pos->lnum, FALSE);
989 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
990 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000991 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100992 if (end_pos != NULL)
993 {
994 end_pos->lnum = lnum + matchpos.lnum;
995 end_pos->col = matchpos.col;
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
998 else
999 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001000 pos->lnum = lnum + matchpos.lnum;
1001 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001002 if (end_pos != NULL)
1003 {
1004 end_pos->lnum = lnum + endpos.lnum;
1005 end_pos->col = endpos.col;
1006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001009 if (end_pos != NULL)
1010 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001012 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001015 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 search_match_endcol = endpos.col;
1017 break;
1018 }
1019 line_breakcheck(); /* stop if ctrl-C typed */
1020 if (got_int)
1021 break;
1022
1023#ifdef FEAT_SEARCH_EXTRA
1024 /* Cancel searching if a character was typed. Used for
1025 * 'incsearch'. Don't check too often, that would slowdown
1026 * searching too much. */
1027 if ((options & SEARCH_PEEK)
1028 && ((lnum - pos->lnum) & 0x3f) == 0
1029 && char_avail())
1030 {
1031 break_loop = TRUE;
1032 break;
1033 }
1034#endif
1035
1036 if (loop && lnum == start_pos.lnum)
1037 break; /* if second loop, stop where started */
1038 }
1039 at_first_line = FALSE;
1040
1041 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001042 * Stop the search if wrapscan isn't set, "stop_lnum" is
1043 * specified, after an interrupt, after a match and after looping
1044 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001046 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001047#ifdef FEAT_RELTIME
1048 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001049#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001050#ifdef FEAT_SEARCH_EXTRA
1051 || break_loop
1052#endif
1053 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 break;
1055
1056 /*
1057 * If 'wrapscan' is set we continue at the other end of the file.
1058 * If 'shortmess' does not contain 's', we give a message.
1059 * This message is also remembered in keep_msg for when the screen
1060 * is redrawn. The keep_msg is cleared whenever another message is
1061 * written.
1062 */
1063 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001067 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1068 give_warning((char_u *)_(dir == BACKWARD
1069 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001071 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001072#ifdef FEAT_RELTIME
1073 || (timed_out != NULL && *timed_out)
1074#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001075#ifdef FEAT_SEARCH_EXTRA
1076 || break_loop
1077#endif
1078 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 break;
1080 }
1081 while (--count > 0 && found); /* stop after count matches or no match */
1082
Bram Moolenaar473de612013-06-08 18:19:48 +02001083 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar280f1262006-01-30 00:14:18 +00001085 called_emsg |= save_called_emsg;
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (!found) /* did not find it */
1088 {
1089 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001090 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1092 {
1093 if (p_ws)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001094 semsg(_(e_patnotf2), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 else if (lnum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001096 semsg(_("E384: search hit TOP without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 mr_pattern);
1098 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001099 semsg(_("E385: search hit BOTTOM without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 mr_pattern);
1101 }
1102 return FAIL;
1103 }
1104
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001105 /* A pattern like "\n\zs" may go past the last line. */
1106 if (pos->lnum > buf->b_ml.ml_line_count)
1107 {
1108 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001109 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001110 if (pos->col > 0)
1111 --pos->col;
1112 }
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 return submatch + 1;
1115}
1116
1117#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001118 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001119set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001120{
1121 spats[0].off.dir = cdir;
1122}
1123
1124 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001125set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001126{
1127 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1128}
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130/*
1131 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001132 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 */
1134 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001135first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 int submatch;
1138
1139 for (submatch = 1; ; ++submatch)
1140 {
1141 if (rp->startpos[submatch].lnum >= 0)
1142 break;
1143 if (submatch == 9)
1144 {
1145 submatch = 0;
1146 break;
1147 }
1148 }
1149 return submatch;
1150}
1151#endif
1152
1153/*
1154 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001155 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * If 'dirc' is 0: use previous dir.
1157 * If 'pat' is NULL or empty : use previous string.
1158 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1159 * If 'options & SEARCH_ECHO': echo the search command and handle options
1160 * If 'options & SEARCH_MSG' : may give error message
1161 * If 'options & SEARCH_OPT' : interpret optional flags
1162 * If 'options & SEARCH_HIS' : put search pattern in history
1163 * If 'options & SEARCH_NOOF': don't add offset to position
1164 * If 'options & SEARCH_MARK': set previous context mark
1165 * If 'options & SEARCH_KEEP': keep previous search pattern
1166 * If 'options & SEARCH_START': accept match at curpos itself
1167 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1168 *
1169 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1170 * makes the movement linewise without moving the match position.
1171 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001172 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 */
1174 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001175do_search(
1176 oparg_T *oap, /* can be NULL */
1177 int dirc, /* '/' or '?' */
1178 char_u *pat,
1179 long count,
1180 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001181 proftime_T *tm, /* timeout limit or NULL */
1182 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 pos_T pos; /* position of the last match */
1185 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001186 soffset_T old_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 int retval; /* Return value */
1188 char_u *p;
1189 long c;
1190 char_u *dircp;
1191 char_u *strcopy = NULL;
1192 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001193 char_u *msgbuf = NULL;
1194 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001195 int has_offset = FALSE;
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02001196#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
1198 /*
1199 * A line offset is not remembered, this is vi compatible.
1200 */
1201 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1202 {
1203 spats[0].off.line = FALSE;
1204 spats[0].off.off = 0;
1205 }
1206
1207 /*
1208 * Save the values for when (options & SEARCH_KEEP) is used.
1209 * (there is no "if ()" around this because gcc wants them initialized)
1210 */
1211 old_off = spats[0].off;
1212
1213 pos = curwin->w_cursor; /* start searching at the cursor position */
1214
1215 /*
1216 * Find out the direction of the search.
1217 */
1218 if (dirc == 0)
1219 dirc = spats[0].off.dir;
1220 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001223#if defined(FEAT_EVAL)
1224 set_vv_searchforward();
1225#endif
1226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (options & SEARCH_REV)
1228 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001229#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 /* There is a bug in the Visual C++ 2.2 compiler which means that
1231 * dirc always ends up being '/' */
1232 dirc = (dirc == '/') ? '?' : '/';
1233#else
1234 if (dirc == '/')
1235 dirc = '?';
1236 else
1237 dirc = '/';
1238#endif
1239 }
1240
1241#ifdef FEAT_FOLDING
1242 /* If the cursor is in a closed fold, don't find another match in the same
1243 * fold. */
1244 if (dirc == '/')
1245 {
1246 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1247 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1248 }
1249 else
1250 {
1251 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1252 pos.col = 0;
1253 }
1254#endif
1255
1256#ifdef FEAT_SEARCH_EXTRA
1257 /*
1258 * Turn 'hlsearch' highlighting back on.
1259 */
1260 if (no_hlsearch && !(options & SEARCH_KEEP))
1261 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001262 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001263 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265#endif
1266
1267 /*
1268 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1269 */
1270 for (;;)
1271 {
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001272 int show_top_bot_msg = FALSE;
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 searchstr = pat;
1275 dircp = NULL;
1276 /* use previous pattern */
1277 if (pat == NULL || *pat == NUL || *pat == dirc)
1278 {
1279 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1280 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001281 searchstr = spats[RE_SUBST].pat;
1282 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001284 emsg(_(e_noprevre));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001285 retval = 0;
1286 goto end_do_search;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001289 else
1290 {
1291 /* make search_regcomp() use spats[RE_SEARCH].pat */
1292 searchstr = (char_u *)"";
1293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295
1296 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1297 {
1298 /*
1299 * Find end of regular expression.
1300 * If there is a matching '/' or '?', toss it.
1301 */
1302 ps = strcopy;
1303 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1304 if (strcopy != ps)
1305 {
1306 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001307 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 pat = strcopy;
1309 searchstr = strcopy;
1310 }
1311 if (*p == dirc)
1312 {
1313 dircp = p; /* remember where we put the NUL */
1314 *p++ = NUL;
1315 }
1316 spats[0].off.line = FALSE;
1317 spats[0].off.end = FALSE;
1318 spats[0].off.off = 0;
1319 /*
1320 * Check for a line offset or a character offset.
1321 * For get_address (echo off) we don't check for a character
1322 * offset, because it is meaningless and the 's' could be a
1323 * substitute command.
1324 */
1325 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1326 spats[0].off.line = TRUE;
1327 else if ((options & SEARCH_OPT) &&
1328 (*p == 'e' || *p == 's' || *p == 'b'))
1329 {
1330 if (*p == 'e') /* end */
1331 spats[0].off.end = SEARCH_END;
1332 ++p;
1333 }
1334 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1335 {
1336 /* 'nr' or '+nr' or '-nr' */
1337 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1338 spats[0].off.off = atol((char *)p);
1339 else if (*p == '-') /* single '-' */
1340 spats[0].off.off = -1;
1341 else /* single '+' */
1342 spats[0].off.off = 1;
1343 ++p;
1344 while (VIM_ISDIGIT(*p)) /* skip number */
1345 ++p;
1346 }
1347
1348 /* compute length of search command for get_address() */
1349 searchcmdlen += (int)(p - pat);
1350
1351 pat = p; /* put pat after search command */
1352 }
1353
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001354 if ((options & SEARCH_ECHO) && messaging() &&
1355 !msg_silent &&
1356 (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001359 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001360 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001362 // Compute msg_row early.
1363 msg_start();
1364
Bram Moolenaar984f0312019-05-24 13:11:47 +02001365 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001366 if (!cmd_silent &&
1367 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001368 {
1369 p = off_buf;
1370 *p++ = dirc;
1371 if (spats[0].off.end)
1372 *p++ = 'e';
1373 else if (!spats[0].off.line)
1374 *p++ = 's';
1375 if (spats[0].off.off > 0 || spats[0].off.line)
1376 *p++ = '+';
1377 *p = NUL;
1378 if (spats[0].off.off != 0 || spats[0].off.line)
1379 sprintf((char *)p, "%ld", spats[0].off.off);
1380 off_len = STRLEN(off_buf);
1381 }
1382
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001384 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 else
1386 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001387
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001388 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001389 {
1390 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001391 // search stat. Use all the space available, so that the
1392 // search state is right aligned. If there is not enough space
1393 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001394 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001395 // Use all the columns.
1396 len = (int)(Rows - msg_row) * Columns - 1;
1397 else
1398 // Use up to 'showcmd' column.
1399 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001400 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1401 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001402 }
1403 else
1404 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001405 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001406
Bram Moolenaar51e14382019-05-25 20:21:28 +02001407 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (msgbuf != NULL)
1409 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001410 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001411 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001412 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1413 // empty for the search_stat feature.
1414 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001415 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001416 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001418 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1419 {
1420 // Use a space to draw the composing char on.
1421 msgbuf[1] = ' ';
1422 mch_memmove(msgbuf + 2, p, STRLEN(p));
1423 }
1424 else
1425 mch_memmove(msgbuf + 1, p, STRLEN(p));
1426 if (off_len > 0)
1427 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001429 trunc = msg_strtrunc(msgbuf, TRUE);
1430 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001432 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001433 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001436 #ifdef FEAT_RIGHTLEFT
1437 // The search pattern could be shown on the right in rightleft
1438 // mode, but the 'ruler' and 'showcmd' area use it too, thus
1439 // it would be blanked out again very soon. Show it on the
1440 // left, but do reverse the text.
1441 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1442 {
1443 char_u *r;
1444 size_t pat_len;
1445
1446 r = reverse_text(msgbuf);
1447 if (r != NULL)
1448 {
1449 vim_free(msgbuf);
1450 msgbuf = r;
1451 // move reversed text to beginning of buffer
1452 while (*r != NUL && *r == ' ')
1453 r++;
1454 pat_len = msgbuf + STRLEN(msgbuf) - r;
1455 mch_memmove(msgbuf, r, pat_len);
1456 // overwrite old text
1457 if ((size_t)(r - msgbuf) >= pat_len)
1458 vim_memset(r, ' ', pat_len);
1459 else
1460 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1461 }
1462 }
1463 #endif
1464 msg_outtrans(msgbuf);
1465 msg_clr_eos();
1466 msg_check();
1467
1468 gotocmdline(FALSE);
1469 out_flush();
1470 msg_nowait = TRUE; // don't wait for this message
1471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473 }
1474
1475 /*
1476 * If there is a character offset, subtract it from the current
1477 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001478 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 * This is not done for a line offset, because then we would not be vi
1480 * compatible.
1481 */
Bram Moolenaared203462004-06-16 11:19:22 +00001482 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 if (spats[0].off.off > 0)
1485 {
1486 for (c = spats[0].off.off; c; --c)
1487 if (decl(&pos) == -1)
1488 break;
1489 if (c) /* at start of buffer */
1490 {
1491 pos.lnum = 0; /* allow lnum == 0 here */
1492 pos.col = MAXCOL;
1493 }
1494 }
1495 else
1496 {
1497 for (c = spats[0].off.off; c; ++c)
1498 if (incl(&pos) == -1)
1499 break;
1500 if (c) /* at end of buffer */
1501 {
1502 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1503 pos.col = 0;
1504 }
1505 }
1506 }
1507
Bram Moolenaar14184a32019-02-16 15:10:30 +01001508 c = searchit(curwin, curbuf, &pos, NULL,
1509 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 searchstr, count, spats[0].off.end + (options &
1511 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1512 + SEARCH_MSG + SEARCH_START
1513 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001514 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
1516 if (dircp != NULL)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001517 *dircp = dirc; // restore second '/' or '?' for normal_cmd()
1518
1519 if (!shortmess(SHM_SEARCH)
1520 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1521 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001522 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (c == FAIL)
1525 {
1526 retval = 0;
1527 goto end_do_search;
1528 }
1529 if (spats[0].off.end && oap != NULL)
1530 oap->inclusive = TRUE; /* 'e' includes last character */
1531
1532 retval = 1; /* pattern found */
1533
1534 /*
1535 * Add character and/or line offset
1536 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001537 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001539 pos_T org_pos = pos;
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (spats[0].off.line) /* Add the offset to the line number. */
1542 {
1543 c = pos.lnum + spats[0].off.off;
1544 if (c < 1)
1545 pos.lnum = 1;
1546 else if (c > curbuf->b_ml.ml_line_count)
1547 pos.lnum = curbuf->b_ml.ml_line_count;
1548 else
1549 pos.lnum = c;
1550 pos.col = 0;
1551
1552 retval = 2; /* pattern found, line offset added */
1553 }
Bram Moolenaared203462004-06-16 11:19:22 +00001554 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
1556 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 c = spats[0].off.off;
1558 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (incl(&pos) == -1)
1562 break;
1563 }
1564 /* to the left, check for start of file */
1565 else
1566 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 while (c++ < 0)
1568 if (decl(&pos) == -1)
1569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
1571 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001572 if (!EQUAL_POS(pos, org_pos))
1573 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001576 // Show [1/15] if 'S' is not in 'shortmess'.
1577 if ((options & SEARCH_ECHO)
1578 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001579 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001580 && c != FAIL
1581 && !shortmess(SHM_SEARCHCOUNT)
1582 && msgbuf != NULL)
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001583 search_stat(dirc, &pos, show_top_bot_msg, msgbuf,
1584 (count != 1 || has_offset));
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 /*
1587 * The search command can be followed by a ';' to do another search.
1588 * For example: "/pat/;/foo/+3;?bar"
1589 * This is like doing another search command, except:
1590 * - The remembered direction '/' or '?' is from the first search.
1591 * - When an error happens the cursor isn't moved at all.
1592 * Don't do this when called by get_address() (it handles ';' itself).
1593 */
1594 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1595 break;
1596
1597 dirc = *++pat;
1598 if (dirc != '?' && dirc != '/')
1599 {
1600 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001601 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 goto end_do_search;
1603 }
1604 ++pat;
1605 }
1606
1607 if (options & SEARCH_MARK)
1608 setpcmark();
1609 curwin->w_cursor = pos;
1610 curwin->w_set_curswant = TRUE;
1611
1612end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001613 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 spats[0].off = old_off;
1615 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001616 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 return retval;
1619}
1620
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621/*
1622 * search_for_exact_line(buf, pos, dir, pat)
1623 *
1624 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001625 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001627 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 * Return OK for success, or FAIL if no line found.
1629 */
1630 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001631search_for_exact_line(
1632 buf_T *buf,
1633 pos_T *pos,
1634 int dir,
1635 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 linenr_T start = 0;
1638 char_u *ptr;
1639 char_u *p;
1640
1641 if (buf->b_ml.ml_line_count == 0)
1642 return FAIL;
1643 for (;;)
1644 {
1645 pos->lnum += dir;
1646 if (pos->lnum < 1)
1647 {
1648 if (p_ws)
1649 {
1650 pos->lnum = buf->b_ml.ml_line_count;
1651 if (!shortmess(SHM_SEARCH))
1652 give_warning((char_u *)_(top_bot_msg), TRUE);
1653 }
1654 else
1655 {
1656 pos->lnum = 1;
1657 break;
1658 }
1659 }
1660 else if (pos->lnum > buf->b_ml.ml_line_count)
1661 {
1662 if (p_ws)
1663 {
1664 pos->lnum = 1;
1665 if (!shortmess(SHM_SEARCH))
1666 give_warning((char_u *)_(bot_top_msg), TRUE);
1667 }
1668 else
1669 {
1670 pos->lnum = 1;
1671 break;
1672 }
1673 }
1674 if (pos->lnum == start)
1675 break;
1676 if (start == 0)
1677 start = pos->lnum;
1678 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1679 p = skipwhite(ptr);
1680 pos->col = (colnr_T) (p - ptr);
1681
1682 /* when adding lines the matching line may be empty but it is not
1683 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001684 if ((compl_cont_status & CONT_ADDING)
1685 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 {
1687 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1688 return OK;
1689 }
1690 else if (*p != NUL) /* ignore empty lines */
1691 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001692 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1693 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 return OK;
1695 }
1696 }
1697 return FAIL;
1698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700/*
1701 * Character Searches
1702 */
1703
1704/*
1705 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1706 * position of the character, otherwise move to just before the char.
1707 * Do this "cap->count1" times.
1708 * Return FAIL or OK.
1709 */
1710 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001711searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 int c = cap->nchar; /* char to search for */
1714 int dir = cap->arg; /* TRUE for searching forward */
1715 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 int col;
1717 char_u *p;
1718 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001719 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 if (c != NUL) /* normal search: remember args for repeat */
1722 {
1723 if (!KeyStuffed) /* don't remember when redoing */
1724 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001725 *lastc = c;
1726 set_csearch_direction(dir);
1727 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001728 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 if (cap->ncharC1 != 0)
1730 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001731 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1732 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001734 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1735 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
1738 }
1739 else /* repeat previous search */
1740 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001741 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 return FAIL;
1743 if (dir) /* repeat in opposite direction */
1744 dir = -lastcdir;
1745 else
1746 dir = lastcdir;
1747 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001748 c = *lastc;
1749 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001750
1751 /* Force a move of at least one char, so ";" and "," will move the
1752 * cursor, even if the cursor is right in front of char we are looking
1753 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001754 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001755 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 }
1757
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001758 if (dir == BACKWARD)
1759 cap->oap->inclusive = FALSE;
1760 else
1761 cap->oap->inclusive = TRUE;
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 p = ml_get_curline();
1764 col = curwin->w_cursor.col;
1765 len = (int)STRLEN(p);
1766
1767 while (count--)
1768 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (has_mbyte)
1770 {
1771 for (;;)
1772 {
1773 if (dir > 0)
1774 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001775 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (col >= len)
1777 return FAIL;
1778 }
1779 else
1780 {
1781 if (col == 0)
1782 return FAIL;
1783 col -= (*mb_head_off)(p, p + col - 1) + 1;
1784 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001785 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001787 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 break;
1789 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001790 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001791 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001792 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001793 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
1795 }
1796 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798 for (;;)
1799 {
1800 if ((col += dir) < 0 || col >= len)
1801 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001802 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001804 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 }
1807 }
1808
1809 if (t_cmd)
1810 {
1811 /* backup to before the character (possibly double-byte) */
1812 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (has_mbyte)
1814 {
1815 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001816 /* Landed on the search char which is lastc_bytelen long */
1817 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 else
1819 /* To previous char, which may be multi-byte. */
1820 col -= (*mb_head_off)(p, p + col);
1821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823 curwin->w_cursor.col = col;
1824
1825 return OK;
1826}
1827
1828/*
1829 * "Other" Searches
1830 */
1831
1832/*
1833 * findmatch - find the matching paren or brace
1834 *
1835 * Improvement over vi: Braces inside quotes are ignored.
1836 */
1837 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001838findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 return findmatchlimit(oap, initc, 0, 0);
1841}
1842
1843/*
1844 * Return TRUE if the character before "linep[col]" equals "ch".
1845 * Return FALSE if "col" is zero.
1846 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1847 * is NULL.
1848 * Handles multibyte string correctly.
1849 */
1850 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001851check_prevcol(
1852 char_u *linep,
1853 int col,
1854 int ch,
1855 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
1857 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (col > 0 && has_mbyte)
1859 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (prevcol)
1861 *prevcol = col;
1862 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1863}
1864
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001865/*
1866 * Raw string start is found at linep[startpos.col - 1].
1867 * Return TRUE if the matching end can be found between startpos and endpos.
1868 */
1869 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001870find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001871{
1872 char_u *p;
1873 char_u *delim_copy;
1874 size_t delim_len;
1875 linenr_T lnum;
1876 int found = FALSE;
1877
1878 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1879 ;
1880 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001881 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001882 if (delim_copy == NULL)
1883 return FALSE;
1884 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1885 {
1886 char_u *line = ml_get(lnum);
1887
1888 for (p = line + (lnum == startpos->lnum
1889 ? startpos->col + 1 : 0); *p; ++p)
1890 {
1891 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1892 break;
1893 if (*p == ')' && p[delim_len + 1] == '"'
1894 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1895 {
1896 found = TRUE;
1897 break;
1898 }
1899 }
1900 if (found)
1901 break;
1902 }
1903 vim_free(delim_copy);
1904 return found;
1905}
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
1908 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001909 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1910 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 *
1912 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001913 * character at or after the cursor. Special values:
1914 * '*' look for C-style comment / *
1915 * '/' look for C-style comment / *, ignoring comment-end
1916 * '#' look for preprocessor directives
1917 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 *
1919 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1920 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1921 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1922 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001923 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001924 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001925 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 */
1927
1928 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001929findmatchlimit(
1930 oparg_T *oap,
1931 int initc,
1932 int flags,
1933 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934{
1935 static pos_T pos; /* current search position */
1936 int findc = 0; /* matching brace */
1937 int c;
1938 int count = 0; /* cumulative number of braces */
1939 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001940 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 int inquote = FALSE; /* TRUE when inside quotes */
1942 char_u *linep; /* pointer to current line */
1943 char_u *ptr;
1944 int do_quotes; /* check for quotes in current line */
1945 int at_start; /* do_quotes value at start position */
1946 int hash_dir = 0; /* Direction searched for # things */
1947 int comment_dir = 0; /* Direction searched for comments */
1948 pos_T match_pos; /* Where last slash-star was found */
1949 int start_in_quotes; /* start position is in quotes */
1950 int traveled = 0; /* how far we've searched so far */
1951 int ignore_cend = FALSE; /* ignore comment end */
1952 int cpo_match; /* vi compatible matching */
1953 int cpo_bsl; /* don't recognize backslashes */
1954 int match_escaped = 0; /* search for escaped match */
1955 int dir; /* Direction to search */
1956 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001957#ifdef FEAT_LISP
1958 int lispcomm = FALSE; /* inside of Lisp-style comment */
1959 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961
1962 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001963 pos.coladd = 0;
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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (has_mbyte)
2241 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 }
2244 else /* forward search */
2245 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002246 if (linep[pos.col] == NUL
2247 /* at end of line, go to next one */
2248#ifdef FEAT_LISP
2249 /* don't search for match in comment */
2250 || (lisp && comment_col != MAXCOL
2251 && pos.col == (colnr_T)comment_col)
2252#endif
2253 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002255 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2256#ifdef FEAT_LISP
2257 /* line is exhausted and comment with it,
2258 * don't search for match in code */
2259 || lispcomm
2260#endif
2261 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 break;
2263 ++pos.lnum;
2264
2265 if (maxtravel && traveled++ > maxtravel)
2266 break;
2267
2268 linep = ml_get(pos.lnum);
2269 pos.col = 0;
2270 do_quotes = -1;
2271 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002272#ifdef FEAT_LISP
2273 if (lisp) /* find comment pos in new line */
2274 comment_col = check_linecomment(linep);
2275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277 else
2278 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002280 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 ++pos.col;
2283 }
2284 }
2285
2286 /*
2287 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2288 */
2289 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2290 (linep[0] == '{' || linep[0] == '}'))
2291 {
2292 if (linep[0] == findc && count == 0) /* match! */
2293 return &pos;
2294 break; /* out of scope */
2295 }
2296
2297 if (comment_dir)
2298 {
2299 /* Note: comments do not nest, and we ignore quotes in them */
2300 /* TODO: ignore comment brackets inside strings */
2301 if (comment_dir == FORWARD)
2302 {
2303 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2304 {
2305 pos.col++;
2306 return &pos;
2307 }
2308 }
2309 else /* Searching backwards */
2310 {
2311 /*
2312 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002313 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 */
2315 if (pos.col == 0)
2316 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002317 else if (raw_string)
2318 {
2319 if (linep[pos.col - 1] == 'R'
2320 && linep[pos.col] == '"'
2321 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2322 {
2323 /* Possible start of raw string. Now that we have the
2324 * delimiter we can check if it ends before where we
2325 * started searching, or before the previously found
2326 * raw string start. */
2327 if (!find_rawstring_end(linep, &pos,
2328 count > 0 ? &match_pos : &curwin->w_cursor))
2329 {
2330 count++;
2331 match_pos = pos;
2332 match_pos.col--;
2333 }
2334 linep = ml_get(pos.lnum); /* may have been released */
2335 }
2336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 else if ( linep[pos.col - 1] == '/'
2338 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002339 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 && (int)pos.col < comment_col)
2341 {
2342 count++;
2343 match_pos = pos;
2344 match_pos.col--;
2345 }
2346 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2347 {
2348 if (count > 0)
2349 pos = match_pos;
2350 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2351 && (int)pos.col <= comment_col)
2352 pos.col -= 2;
2353 else if (ignore_cend)
2354 continue;
2355 else
2356 return NULL;
2357 return &pos;
2358 }
2359 }
2360 continue;
2361 }
2362
2363 /*
2364 * If smart matching ('cpoptions' does not contain '%'), braces inside
2365 * of quotes are ignored, but only if there is an even number of
2366 * quotes in the line.
2367 */
2368 if (cpo_match)
2369 do_quotes = 0;
2370 else if (do_quotes == -1)
2371 {
2372 /*
2373 * Count the number of quotes in the line, skipping \" and '"'.
2374 * Watch out for "\\".
2375 */
2376 at_start = do_quotes;
2377 for (ptr = linep; *ptr; ++ptr)
2378 {
2379 if (ptr == linep + pos.col + backwards)
2380 at_start = (do_quotes & 1);
2381 if (*ptr == '"'
2382 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2383 ++do_quotes;
2384 if (*ptr == '\\' && ptr[1] != NUL)
2385 ++ptr;
2386 }
2387 do_quotes &= 1; /* result is 1 with even number of quotes */
2388
2389 /*
2390 * If we find an uneven count, check current line and previous
2391 * one for a '\' at the end.
2392 */
2393 if (!do_quotes)
2394 {
2395 inquote = FALSE;
2396 if (ptr[-1] == '\\')
2397 {
2398 do_quotes = 1;
2399 if (start_in_quotes == MAYBE)
2400 {
2401 /* Do we need to use at_start here? */
2402 inquote = TRUE;
2403 start_in_quotes = TRUE;
2404 }
2405 else if (backwards)
2406 inquote = TRUE;
2407 }
2408 if (pos.lnum > 1)
2409 {
2410 ptr = ml_get(pos.lnum - 1);
2411 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2412 {
2413 do_quotes = 1;
2414 if (start_in_quotes == MAYBE)
2415 {
2416 inquote = at_start;
2417 if (inquote)
2418 start_in_quotes = TRUE;
2419 }
2420 else if (!backwards)
2421 inquote = TRUE;
2422 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002423
2424 /* ml_get() only keeps one line, need to get linep again */
2425 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427 }
2428 }
2429 if (start_in_quotes == MAYBE)
2430 start_in_quotes = FALSE;
2431
2432 /*
2433 * If 'smartmatch' is set:
2434 * Things inside quotes are ignored by setting 'inquote'. If we
2435 * find a quote without a preceding '\' invert 'inquote'. At the
2436 * end of a line not ending in '\' we reset 'inquote'.
2437 *
2438 * In lines with an uneven number of quotes (without preceding '\')
2439 * we do not know which part to ignore. Therefore we only set
2440 * inquote if the number of quotes in a line is even, unless this
2441 * line or the previous one ends in a '\'. Complicated, isn't it?
2442 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002443 c = PTR2CHAR(linep + pos.col);
2444 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
2446 case NUL:
2447 /* at end of line without trailing backslash, reset inquote */
2448 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2449 {
2450 inquote = FALSE;
2451 start_in_quotes = FALSE;
2452 }
2453 break;
2454
2455 case '"':
2456 /* a quote that is preceded with an odd number of backslashes is
2457 * ignored */
2458 if (do_quotes)
2459 {
2460 int col;
2461
2462 for (col = pos.col - 1; col >= 0; --col)
2463 if (linep[col] != '\\')
2464 break;
2465 if ((((int)pos.col - 1 - col) & 1) == 0)
2466 {
2467 inquote = !inquote;
2468 start_in_quotes = FALSE;
2469 }
2470 }
2471 break;
2472
2473 /*
2474 * If smart matching ('cpoptions' does not contain '%'):
2475 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2476 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2477 * skipped, there is never a brace in them.
2478 * Ignore this when finding matches for `'.
2479 */
2480 case '\'':
2481 if (!cpo_match && initc != '\'' && findc != '\'')
2482 {
2483 if (backwards)
2484 {
2485 if (pos.col > 1)
2486 {
2487 if (linep[pos.col - 2] == '\'')
2488 {
2489 pos.col -= 2;
2490 break;
2491 }
2492 else if (linep[pos.col - 2] == '\\' &&
2493 pos.col > 2 && linep[pos.col - 3] == '\'')
2494 {
2495 pos.col -= 3;
2496 break;
2497 }
2498 }
2499 }
2500 else if (linep[pos.col + 1]) /* forward search */
2501 {
2502 if (linep[pos.col + 1] == '\\' &&
2503 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2504 {
2505 pos.col += 3;
2506 break;
2507 }
2508 else if (linep[pos.col + 2] == '\'')
2509 {
2510 pos.col += 2;
2511 break;
2512 }
2513 }
2514 }
2515 /* FALLTHROUGH */
2516
2517 default:
2518#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002519 /*
2520 * For Lisp skip over backslashed (), {} and [].
2521 * (actually, we skip #\( et al)
2522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (curbuf->b_p_lisp
2524 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002525 && pos.col > 1
2526 && check_prevcol(linep, pos.col, '\\', NULL)
2527 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 break;
2529#endif
2530
2531 /* Check for match outside of quotes, and inside of
2532 * quotes when the start is also inside of quotes. */
2533 if ((!inquote || start_in_quotes == TRUE)
2534 && (c == initc || c == findc))
2535 {
2536 int col, bslcnt = 0;
2537
2538 if (!cpo_bsl)
2539 {
2540 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2541 bslcnt++;
2542 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002543 /* Only accept a match when 'M' is in 'cpo' or when escaping
2544 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2546 {
2547 if (c == initc)
2548 count++;
2549 else
2550 {
2551 if (count == 0)
2552 return &pos;
2553 count--;
2554 }
2555 }
2556 }
2557 }
2558 }
2559
2560 if (comment_dir == BACKWARD && count > 0)
2561 {
2562 pos = match_pos;
2563 return &pos;
2564 }
2565 return (pos_T *)NULL; /* never found it */
2566}
2567
2568/*
2569 * Check if line[] contains a / / comment.
2570 * Return MAXCOL if not, otherwise return the column.
2571 * TODO: skip strings.
2572 */
2573 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002574check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 char_u *p;
2577
2578 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002579#ifdef FEAT_LISP
2580 /* skip Lispish one-line comments */
2581 if (curbuf->b_p_lisp)
2582 {
2583 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2584 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002585 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586
2587 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002588 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002589 {
2590 if (*p == '"')
2591 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002592 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002593 {
2594 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002595 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002596 }
2597 else if (p == line || ((p - line) >= 2
2598 /* skip #\" form */
2599 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002600 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002601 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002602 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002603 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2604 break; /* found! */
2605 ++p;
2606 }
2607 }
2608 else
2609 p = NULL;
2610 }
2611 else
2612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 while ((p = vim_strchr(p, '/')) != NULL)
2614 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002615 /* accept a double /, unless it's preceded with * and followed by *,
2616 * because * / / * is an end and start of a C comment */
2617 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 break;
2619 ++p;
2620 }
2621
2622 if (p == NULL)
2623 return MAXCOL;
2624 return (int)(p - line);
2625}
2626
2627/*
2628 * Move cursor briefly to character matching the one under the cursor.
2629 * Used for Insert mode and "r" command.
2630 * Show the match only if it is visible on the screen.
2631 * If there isn't a match, then beep.
2632 */
2633 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002634showmatch(
2635 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636{
2637 pos_T *lpos, save_cursor;
2638 pos_T mpos;
2639 colnr_T vcol;
2640 long save_so;
2641 long save_siso;
2642#ifdef CURSOR_SHAPE
2643 int save_state;
2644#endif
2645 colnr_T save_dollar_vcol;
2646 char_u *p;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002647 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2648 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 /*
2651 * Only show match for chars in the 'matchpairs' option.
2652 */
2653 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002654 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002657 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002658 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002659#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002660 p += MB_PTR2LEN(p) + 1;
2661 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662#ifdef FEAT_RIGHTLEFT
2663 && !(curwin->w_p_rl ^ p_ri)
2664#endif
2665 )
2666 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002667 p += MB_PTR2LEN(p);
2668 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 return;
2670 }
2671
2672 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002673 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002674 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
2676 if (!curwin->w_p_wrap)
2677 getvcol(curwin, lpos, NULL, &vcol, NULL);
2678 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002679 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
2681 mpos = *lpos; /* save the pos, update_screen() may change it */
2682 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002683 save_so = *so;
2684 save_siso = *siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2686 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002687 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2688 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 ++curwin->w_virtcol; /* do display ')' just before "$" */
2690 update_screen(VALID); /* show the new char first */
2691
2692 save_dollar_vcol = dollar_vcol;
2693#ifdef CURSOR_SHAPE
2694 save_state = State;
2695 State = SHOWMATCH;
2696 ui_cursor_shape(); /* may show different cursor shape */
2697#endif
2698 curwin->w_cursor = mpos; /* move to matching char */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002699 *so = 0; /* don't use 'scrolloff' here */
2700 *siso = 0; /* don't use 'sidescrolloff' here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 showruler(FALSE);
2702 setcursor();
2703 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002704 out_flush_cursor(TRUE, FALSE);
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2707 * which resets it if the matching position is in a previous line
2708 * and has a higher column number. */
2709 dollar_vcol = save_dollar_vcol;
2710
2711 /*
2712 * brief pause, unless 'm' is present in 'cpo' and a character is
2713 * available.
2714 */
2715 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2716 ui_delay(p_mat * 100L, TRUE);
2717 else if (!char_avail())
2718 ui_delay(p_mat * 100L, FALSE);
2719 curwin->w_cursor = save_cursor; /* restore cursor position */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002720 *so = save_so;
2721 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#ifdef CURSOR_SHAPE
2723 State = save_state;
2724 ui_cursor_shape(); /* may show different cursor shape */
2725#endif
2726 }
2727 }
2728}
2729
2730/*
Bram Moolenaar85160712018-06-19 18:27:41 +02002731 * Find the start of the next sentence, searching in the direction specified
2732 * by the "dir" argument. The cursor is positioned on the start of the next
2733 * sentence when found. If the next sentence is found, return OK. Return FAIL
2734 * otherwise. See ":h sentence" for the precise definition of a "sentence"
2735 * text object.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 */
2737 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002738findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 pos_T pos, tpos;
2741 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002742 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 int startlnum;
2744 int noskip = FALSE; /* do not skip blanks */
2745 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002746 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 pos = curwin->w_cursor;
2749 if (dir == FORWARD)
2750 func = incl;
2751 else
2752 func = decl;
2753
2754 while (count--)
2755 {
2756 /*
2757 * if on an empty line, skip upto a non-empty line
2758 */
2759 if (gchar_pos(&pos) == NUL)
2760 {
2761 do
2762 if ((*func)(&pos) == -1)
2763 break;
2764 while (gchar_pos(&pos) == NUL);
2765 if (dir == FORWARD)
2766 goto found;
2767 }
2768 /*
2769 * if on the start of a paragraph or a section and searching forward,
2770 * go to the next line
2771 */
2772 else if (dir == FORWARD && pos.col == 0 &&
2773 startPS(pos.lnum, NUL, FALSE))
2774 {
2775 if (pos.lnum == curbuf->b_ml.ml_line_count)
2776 return FAIL;
2777 ++pos.lnum;
2778 goto found;
2779 }
2780 else if (dir == BACKWARD)
2781 decl(&pos);
2782
Bram Moolenaar85160712018-06-19 18:27:41 +02002783 // go back to the previous non-white non-punctuation character
Bram Moolenaardef9e822004-12-31 20:58:58 +00002784 found_dot = FALSE;
Bram Moolenaar85160712018-06-19 18:27:41 +02002785 while (c = gchar_pos(&pos), VIM_ISWHITE(c)
2786 || vim_strchr((char_u *)".!?)]\"'", c) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 {
Bram Moolenaar85160712018-06-19 18:27:41 +02002788 tpos = pos;
2789 if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 break;
Bram Moolenaar85160712018-06-19 18:27:41 +02002791
2792 if (found_dot)
2793 break;
2794 if (vim_strchr((char_u *) ".!?", c) != NULL)
2795 found_dot = TRUE;
2796
2797 if (vim_strchr((char_u *) ")]\"'", c) != NULL
2798 && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
2799 break;
2800
2801 decl(&pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 }
2803
2804 /* remember the line where the search started */
2805 startlnum = pos.lnum;
2806 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2807
2808 for (;;) /* find end of sentence */
2809 {
2810 c = gchar_pos(&pos);
2811 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2812 {
2813 if (dir == BACKWARD && pos.lnum != startlnum)
2814 ++pos.lnum;
2815 break;
2816 }
2817 if (c == '.' || c == '!' || c == '?')
2818 {
2819 tpos = pos;
2820 do
2821 if ((c = inc(&tpos)) == -1)
2822 break;
2823 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2824 != NULL);
2825 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2826 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2827 && gchar_pos(&tpos) == ' ')))
2828 {
2829 pos = tpos;
2830 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2831 inc(&pos);
2832 break;
2833 }
2834 }
2835 if ((*func)(&pos) == -1)
2836 {
2837 if (count)
2838 return FAIL;
2839 noskip = TRUE;
2840 break;
2841 }
2842 }
2843found:
2844 /* skip white space */
2845 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2846 if (incl(&pos) == -1)
2847 break;
2848 }
2849
2850 setpcmark();
2851 curwin->w_cursor = pos;
2852 return OK;
2853}
2854
2855/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002856 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002858 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 * If 'what' is '{' or '}' we go to the next section.
2860 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002861 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 */
2863 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002864findpar(
2865 int *pincl, /* Return: TRUE if last char is to be included */
2866 int dir,
2867 long count,
2868 int what,
2869 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
2871 linenr_T curr;
2872 int did_skip; /* TRUE after separating lines have been skipped */
2873 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002874 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875#ifdef FEAT_FOLDING
2876 linenr_T fold_first; /* first line of a closed fold */
2877 linenr_T fold_last; /* last line of a closed fold */
2878 int fold_skipped; /* TRUE if a closed fold was skipped this
2879 iteration */
2880#endif
2881
2882 curr = curwin->w_cursor.lnum;
2883
2884 while (count--)
2885 {
2886 did_skip = FALSE;
2887 for (first = TRUE; ; first = FALSE)
2888 {
2889 if (*ml_get(curr) != NUL)
2890 did_skip = TRUE;
2891
2892#ifdef FEAT_FOLDING
2893 /* skip folded lines */
2894 fold_skipped = FALSE;
2895 if (first && hasFolding(curr, &fold_first, &fold_last))
2896 {
2897 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2898 fold_skipped = TRUE;
2899 }
2900#endif
2901
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002902 /* POSIX has its own ideas of what a paragraph boundary is and it
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002903 * doesn't match historical Vi: It also stops at a "{" in the
2904 * first column and at an empty line. */
2905 if (!first && did_skip && (startPS(curr, what, both)
2906 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 break;
2908
2909#ifdef FEAT_FOLDING
2910 if (fold_skipped)
2911 curr -= dir;
2912#endif
2913 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2914 {
2915 if (count)
2916 return FALSE;
2917 curr -= dir;
2918 break;
2919 }
2920 }
2921 }
2922 setpcmark();
2923 if (both && *ml_get(curr) == '}') /* include line with '}' */
2924 ++curr;
2925 curwin->w_cursor.lnum = curr;
2926 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2927 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002928 char_u *line = ml_get(curr);
2929
2930 /* Put the cursor on the last character in the last line and make the
2931 * motion inclusive. */
2932 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
2934 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002935 curwin->w_cursor.col -=
2936 (*mb_head_off)(line, line + curwin->w_cursor.col);
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002937 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 }
2939 }
2940 else
2941 curwin->w_cursor.col = 0;
2942 return TRUE;
2943}
2944
2945/*
2946 * check if the string 's' is a nroff macro that is in option 'opt'
2947 */
2948 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002949inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 char_u *macro;
2952
2953 for (macro = opt; macro[0]; ++macro)
2954 {
2955 /* Accept two characters in the option being equal to two characters
2956 * in the line. A space in the option matches with a space in the
2957 * line or the line having ended. */
2958 if ( (macro[0] == s[0]
2959 || (macro[0] == ' '
2960 && (s[0] == NUL || s[0] == ' ')))
2961 && (macro[1] == s[1]
2962 || ((macro[1] == NUL || macro[1] == ' ')
2963 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2964 break;
2965 ++macro;
2966 if (macro[0] == NUL)
2967 break;
2968 }
2969 return (macro[0] != NUL);
2970}
2971
2972/*
2973 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2974 * If 'para' is '{' or '}' only check for sections.
2975 * If 'both' is TRUE also stop at '}'
2976 */
2977 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002978startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 char_u *s;
2981
2982 s = ml_get(lnum);
2983 if (*s == para || *s == '\f' || (both && *s == '}'))
2984 return TRUE;
2985 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2986 (!para && inmacro(p_para, s + 1))))
2987 return TRUE;
2988 return FALSE;
2989}
2990
2991/*
2992 * The following routines do the word searches performed by the 'w', 'W',
2993 * 'b', 'B', 'e', and 'E' commands.
2994 */
2995
2996/*
2997 * To perform these searches, characters are placed into one of three
2998 * classes, and transitions between classes determine word boundaries.
2999 *
3000 * The classes are:
3001 *
3002 * 0 - white space
3003 * 1 - punctuation
3004 * 2 or higher - keyword characters (letters, digits and underscore)
3005 */
3006
3007static int cls_bigword; /* TRUE for "W", "B" or "E" */
3008
3009/*
3010 * cls() - returns the class of character at curwin->w_cursor
3011 *
3012 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
3013 * from class 2 and higher are reported as class 1 since only white space
3014 * boundaries are of interest.
3015 */
3016 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003017cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 int c;
3020
3021 c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 if (c == ' ' || c == '\t' || c == NUL)
3023 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (enc_dbcs != 0 && c > 0xFF)
3025 {
3026 /* If cls_bigword, report multi-byte chars as class 1. */
3027 if (enc_dbcs == DBCS_KOR && cls_bigword)
3028 return 1;
3029
3030 /* process code leading/trailing bytes */
3031 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3032 }
3033 if (enc_utf8)
3034 {
3035 c = utf_class(c);
3036 if (c != 0 && cls_bigword)
3037 return 1;
3038 return c;
3039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
3041 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3042 if (cls_bigword)
3043 return 1;
3044
3045 if (vim_iswordc(c))
3046 return 2;
3047 return 1;
3048}
3049
3050
3051/*
3052 * fwd_word(count, type, eol) - move forward one word
3053 *
3054 * Returns FAIL if the cursor was already at the end of the file.
3055 * If eol is TRUE, last word stops at end of line (for operators).
3056 */
3057 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003058fwd_word(
3059 long count,
3060 int bigword, /* "W", "E" or "B" */
3061 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063 int sclass; /* starting class */
3064 int i;
3065 int last_line;
3066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 cls_bigword = bigword;
3069 while (--count >= 0)
3070 {
3071#ifdef FEAT_FOLDING
3072 /* When inside a range of folded lines, move to the last char of the
3073 * last line. */
3074 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3075 coladvance((colnr_T)MAXCOL);
3076#endif
3077 sclass = cls();
3078
3079 /*
3080 * We always move at least one character, unless on the last
3081 * character in the buffer.
3082 */
3083 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3084 i = inc_cursor();
3085 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3086 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003087 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 return OK;
3089
3090 /*
3091 * Go one char past end of current word (if any)
3092 */
3093 if (sclass != 0)
3094 while (cls() == sclass)
3095 {
3096 i = inc_cursor();
3097 if (i == -1 || (i >= 1 && eol && count == 0))
3098 return OK;
3099 }
3100
3101 /*
3102 * go to next non-white
3103 */
3104 while (cls() == 0)
3105 {
3106 /*
3107 * We'll stop if we land on a blank line
3108 */
3109 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3110 break;
3111
3112 i = inc_cursor();
3113 if (i == -1 || (i >= 1 && eol && count == 0))
3114 return OK;
3115 }
3116 }
3117 return OK;
3118}
3119
3120/*
3121 * bck_word() - move backward 'count' words
3122 *
3123 * If stop is TRUE and we are already on the start of a word, move one less.
3124 *
3125 * Returns FAIL if top of the file was reached.
3126 */
3127 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003128bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 int sclass; /* starting class */
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 cls_bigword = bigword;
3134 while (--count >= 0)
3135 {
3136#ifdef FEAT_FOLDING
3137 /* When inside a range of folded lines, move to the first char of the
3138 * first line. */
3139 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3140 curwin->w_cursor.col = 0;
3141#endif
3142 sclass = cls();
3143 if (dec_cursor() == -1) /* started at start of file */
3144 return FAIL;
3145
3146 if (!stop || sclass == cls() || sclass == 0)
3147 {
3148 /*
3149 * Skip white space before the word.
3150 * Stop on an empty line.
3151 */
3152 while (cls() == 0)
3153 {
3154 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003155 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 goto finished;
3157 if (dec_cursor() == -1) /* hit start of file, stop here */
3158 return OK;
3159 }
3160
3161 /*
3162 * Move backward to start of this word.
3163 */
3164 if (skip_chars(cls(), BACKWARD))
3165 return OK;
3166 }
3167
3168 inc_cursor(); /* overshot - forward one */
3169finished:
3170 stop = FALSE;
3171 }
3172 return OK;
3173}
3174
3175/*
3176 * end_word() - move to the end of the word
3177 *
3178 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3179 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3180 * motion crosses blank lines. When the real vi crosses a blank line in an
3181 * 'e' motion, the cursor is placed on the FIRST character of the next
3182 * non-blank line. The 'E' command, however, works correctly. Since this
3183 * appears to be a bug, I have not duplicated it here.
3184 *
3185 * Returns FAIL if end of the file was reached.
3186 *
3187 * If stop is TRUE and we are already on the end of a word, move one less.
3188 * If empty is TRUE stop on an empty line.
3189 */
3190 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003191end_word(
3192 long count,
3193 int bigword,
3194 int stop,
3195 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 int sclass; /* starting class */
3198
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 cls_bigword = bigword;
3201 while (--count >= 0)
3202 {
3203#ifdef FEAT_FOLDING
3204 /* When inside a range of folded lines, move to the last char of the
3205 * last line. */
3206 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3207 coladvance((colnr_T)MAXCOL);
3208#endif
3209 sclass = cls();
3210 if (inc_cursor() == -1)
3211 return FAIL;
3212
3213 /*
3214 * If we're in the middle of a word, we just have to move to the end
3215 * of it.
3216 */
3217 if (cls() == sclass && sclass != 0)
3218 {
3219 /*
3220 * Move forward to end of the current word
3221 */
3222 if (skip_chars(sclass, FORWARD))
3223 return FAIL;
3224 }
3225 else if (!stop || sclass == 0)
3226 {
3227 /*
3228 * We were at the end of a word. Go to the end of the next word.
3229 * First skip white space, if 'empty' is TRUE, stop at empty line.
3230 */
3231 while (cls() == 0)
3232 {
3233 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003234 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 goto finished;
3236 if (inc_cursor() == -1) /* hit end of file, stop here */
3237 return FAIL;
3238 }
3239
3240 /*
3241 * Move forward to the end of this word.
3242 */
3243 if (skip_chars(cls(), FORWARD))
3244 return FAIL;
3245 }
3246 dec_cursor(); /* overshot - one char backward */
3247finished:
3248 stop = FALSE; /* we move only one word less */
3249 }
3250 return OK;
3251}
3252
3253/*
3254 * Move back to the end of the word.
3255 *
3256 * Returns FAIL if start of the file was reached.
3257 */
3258 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003259bckend_word(
3260 long count,
3261 int bigword, /* TRUE for "B" */
3262 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 int sclass; /* starting class */
3265 int i;
3266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 cls_bigword = bigword;
3269 while (--count >= 0)
3270 {
3271 sclass = cls();
3272 if ((i = dec_cursor()) == -1)
3273 return FAIL;
3274 if (eol && i == 1)
3275 return OK;
3276
3277 /*
3278 * Move backward to before the start of this word.
3279 */
3280 if (sclass != 0)
3281 {
3282 while (cls() == sclass)
3283 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3284 return OK;
3285 }
3286
3287 /*
3288 * Move backward to end of the previous word
3289 */
3290 while (cls() == 0)
3291 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003292 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 break;
3294 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3295 return OK;
3296 }
3297 }
3298 return OK;
3299}
3300
3301/*
3302 * Skip a row of characters of the same class.
3303 * Return TRUE when end-of-file reached, FALSE otherwise.
3304 */
3305 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003306skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 while (cls() == cclass)
3309 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3310 return TRUE;
3311 return FALSE;
3312}
3313
3314#ifdef FEAT_TEXTOBJ
3315/*
3316 * Go back to the start of the word or the start of white space
3317 */
3318 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003319back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 int sclass; /* starting class */
3322
3323 sclass = cls();
3324 for (;;)
3325 {
3326 if (curwin->w_cursor.col == 0) /* stop at start of line */
3327 break;
3328 dec_cursor();
3329 if (cls() != sclass) /* stop at start of word */
3330 {
3331 inc_cursor();
3332 break;
3333 }
3334 }
3335}
3336
3337 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003338find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 int c;
3341
3342 while (decl(posp) != -1)
3343 {
3344 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003345 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 {
3347 incl(posp);
3348 break;
3349 }
3350 }
3351}
3352
3353/*
3354 * Skip count/2 sentences and count/2 separating white spaces.
3355 */
3356 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003357findsent_forward(
3358 long count,
3359 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 while (count--)
3362 {
3363 findsent(FORWARD, 1L);
3364 if (at_start_sent)
3365 find_first_blank(&curwin->w_cursor);
3366 if (count == 0 || at_start_sent)
3367 decl(&curwin->w_cursor);
3368 at_start_sent = !at_start_sent;
3369 }
3370}
3371
3372/*
3373 * Find word under cursor, cursor at end.
3374 * Used while an operator is pending, and in Visual mode.
3375 */
3376 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003377current_word(
3378 oparg_T *oap,
3379 long count,
3380 int include, /* TRUE: include word and white space */
3381 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 pos_T start_pos;
3384 pos_T pos;
3385 int inclusive = TRUE;
3386 int include_white = FALSE;
3387
3388 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003389 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003392 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 dec_cursor();
3394
3395 /*
3396 * When Visual mode is not active, or when the VIsual area is only one
3397 * character, select the word and/or white space under the cursor.
3398 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003399 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 /*
3402 * Go to start of current word or white space.
3403 */
3404 back_in_line();
3405 start_pos = curwin->w_cursor;
3406
3407 /*
3408 * If the start is on white space, and white space should be included
3409 * (" word"), or start is not on white space, and white space should
3410 * not be included ("word"), find end of word.
3411 */
3412 if ((cls() == 0) == include)
3413 {
3414 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3415 return FAIL;
3416 }
3417 else
3418 {
3419 /*
3420 * If the start is not on white space, and white space should be
3421 * included ("word "), or start is on white space and white
3422 * space should not be included (" "), find start of word.
3423 * If we end up in the first column of the next line (single char
3424 * word) back up to end of the line.
3425 */
3426 fwd_word(1L, bigword, TRUE);
3427 if (curwin->w_cursor.col == 0)
3428 decl(&curwin->w_cursor);
3429 else
3430 oneleft();
3431
3432 if (include)
3433 include_white = TRUE;
3434 }
3435
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (VIsual_active)
3437 {
3438 /* should do something when inclusive == FALSE ! */
3439 VIsual = start_pos;
3440 redraw_curbuf_later(INVERTED); /* update the inversion */
3441 }
3442 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 oap->start = start_pos;
3445 oap->motion_type = MCHAR;
3446 }
3447 --count;
3448 }
3449
3450 /*
3451 * When count is still > 0, extend with more objects.
3452 */
3453 while (count > 0)
3454 {
3455 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003456 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 /*
3459 * In Visual mode, with cursor at start: move cursor back.
3460 */
3461 if (decl(&curwin->w_cursor) == -1)
3462 return FAIL;
3463 if (include != (cls() != 0))
3464 {
3465 if (bck_word(1L, bigword, TRUE) == FAIL)
3466 return FAIL;
3467 }
3468 else
3469 {
3470 if (bckend_word(1L, bigword, TRUE) == FAIL)
3471 return FAIL;
3472 (void)incl(&curwin->w_cursor);
3473 }
3474 }
3475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 /*
3478 * Move cursor forward one word and/or white area.
3479 */
3480 if (incl(&curwin->w_cursor) == -1)
3481 return FAIL;
3482 if (include != (cls() == 0))
3483 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003484 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 return FAIL;
3486 /*
3487 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003488 * the first character on the line.
3489 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003491 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 inclusive = FALSE;
3493 }
3494 else
3495 {
3496 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3497 return FAIL;
3498 }
3499 }
3500 --count;
3501 }
3502
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003503 if (include_white && (cls() != 0
3504 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 /*
3507 * If we don't include white space at the end, move the start
3508 * to include some white space there. This makes "daw" work
3509 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003510 * word). Also when "2daw" deletes "word." at the end of the line
3511 * (cursor is at start of next line).
3512 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 */
3514 pos = curwin->w_cursor; /* save cursor position */
3515 curwin->w_cursor = start_pos;
3516 if (oneleft() == OK)
3517 {
3518 back_in_line();
3519 if (cls() == 0 && curwin->w_cursor.col > 0)
3520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (VIsual_active)
3522 VIsual = curwin->w_cursor;
3523 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 oap->start = curwin->w_cursor;
3525 }
3526 }
3527 curwin->w_cursor = pos; /* put cursor back at end */
3528 }
3529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 if (VIsual_active)
3531 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003532 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 inc_cursor();
3534 if (VIsual_mode == 'V')
3535 {
3536 VIsual_mode = 'v';
3537 redraw_cmdline = TRUE; /* show mode later */
3538 }
3539 }
3540 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 oap->inclusive = inclusive;
3542
3543 return OK;
3544}
3545
3546/*
3547 * Find sentence(s) under the cursor, cursor at end.
3548 * When Visual active, extend it by one or more sentences.
3549 */
3550 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003551current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
3553 pos_T start_pos;
3554 pos_T pos;
3555 int start_blank;
3556 int c;
3557 int at_start_sent;
3558 long ncount;
3559
3560 start_pos = curwin->w_cursor;
3561 pos = start_pos;
3562 findsent(FORWARD, 1L); /* Find start of next sentence. */
3563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003565 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003567 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003570 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 /*
3573 * Cursor at start of Visual area.
3574 * Find out where we are:
3575 * - in the white space before a sentence
3576 * - in a sentence or just after it
3577 * - at the start of a sentence
3578 */
3579 at_start_sent = TRUE;
3580 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003581 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003584 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 at_start_sent = FALSE;
3587 break;
3588 }
3589 incl(&pos);
3590 }
3591 if (!at_start_sent)
3592 {
3593 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003594 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 at_start_sent = TRUE; /* exactly at start of sentence */
3596 else
3597 /* inside a sentence, go to its end (start of next) */
3598 findsent(FORWARD, 1L);
3599 }
3600 if (include) /* "as" gets twice as much as "is" */
3601 count *= 2;
3602 while (count--)
3603 {
3604 if (at_start_sent)
3605 find_first_blank(&curwin->w_cursor);
3606 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003607 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 findsent(BACKWARD, 1L);
3609 at_start_sent = !at_start_sent;
3610 }
3611 }
3612 else
3613 {
3614 /*
3615 * Cursor at end of Visual area.
3616 * Find out where we are:
3617 * - just before a sentence
3618 * - just before or in the white space before a sentence
3619 * - in a sentence
3620 */
3621 incl(&pos);
3622 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003623 /* not just before a sentence */
3624 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
3626 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003627 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003630 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
3632 at_start_sent = TRUE;
3633 break;
3634 }
3635 incl(&pos);
3636 }
3637 if (at_start_sent) /* in the sentence */
3638 findsent(BACKWARD, 1L);
3639 else /* in/before white before a sentence */
3640 curwin->w_cursor = start_pos;
3641 }
3642
3643 if (include) /* "as" gets twice as much as "is" */
3644 count *= 2;
3645 findsent_forward(count, at_start_sent);
3646 if (*p_sel == 'e')
3647 ++curwin->w_cursor.col;
3648 }
3649 return OK;
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
3652 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003653 * If the cursor started on a blank, check if it is just before the start
3654 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003656 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003658 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 start_blank = TRUE;
3661 find_first_blank(&start_pos); /* go back to first blank */
3662 }
3663 else
3664 {
3665 start_blank = FALSE;
3666 findsent(BACKWARD, 1L);
3667 start_pos = curwin->w_cursor;
3668 }
3669 if (include)
3670 ncount = count * 2;
3671 else
3672 {
3673 ncount = count;
3674 if (start_blank)
3675 --ncount;
3676 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003677 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 findsent_forward(ncount, TRUE);
3679 else
3680 decl(&curwin->w_cursor);
3681
3682 if (include)
3683 {
3684 /*
3685 * If the blank in front of the sentence is included, exclude the
3686 * blanks at the end of the sentence, go back to the first blank.
3687 * If there are no trailing blanks, try to include leading blanks.
3688 */
3689 if (start_blank)
3690 {
3691 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003692 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3693 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 decl(&curwin->w_cursor);
3695 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003696 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 find_first_blank(&start_pos);
3698 }
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 if (VIsual_active)
3701 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003702 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003703 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 goto extend;
3705 if (*p_sel == 'e')
3706 ++curwin->w_cursor.col;
3707 VIsual = start_pos;
3708 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003709 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 redraw_curbuf_later(INVERTED); /* update the inversion */
3711 }
3712 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
3714 /* include a newline after the sentence, if there is one */
3715 if (incl(&curwin->w_cursor) == -1)
3716 oap->inclusive = TRUE;
3717 else
3718 oap->inclusive = FALSE;
3719 oap->start = start_pos;
3720 oap->motion_type = MCHAR;
3721 }
3722 return OK;
3723}
3724
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003725/*
3726 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003727 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003730current_block(
3731 oparg_T *oap,
3732 long count,
3733 int include, /* TRUE == include white space */
3734 int what, /* '(', '{', etc. */
3735 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 pos_T old_pos;
3738 pos_T *pos = NULL;
3739 pos_T start_pos;
3740 pos_T *end_pos;
3741 pos_T old_start, old_end;
3742 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003743 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003746 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 old_start = old_end;
3748
3749 /*
3750 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3751 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003752 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
3754 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003755 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 while (inindent(1))
3757 if (inc_cursor() != 0)
3758 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003759 if (gchar_cursor() == what)
3760 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 ++curwin->w_cursor.col;
3762 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003763 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
3765 old_start = VIsual;
3766 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3767 }
3768 else
3769 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771 /*
3772 * Search backwards for unclosed '(', '{', etc..
3773 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003774 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3775 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003778 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 while (count-- > 0)
3780 {
3781 if ((pos = findmatch(NULL, what)) == NULL)
3782 break;
3783 curwin->w_cursor = *pos;
3784 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3785 }
3786 p_cpo = save_cpo;
3787
3788 /*
3789 * Search for matching ')', '}', etc.
3790 * Put this position in curwin->w_cursor.
3791 */
3792 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3793 {
3794 curwin->w_cursor = old_pos;
3795 return FAIL;
3796 }
3797 curwin->w_cursor = *end_pos;
3798
3799 /*
3800 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003801 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3802 * indent. But only if the resulting area is not smaller than what we
3803 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 */
3805 while (!include)
3806 {
3807 incl(&start_pos);
3808 sol = (curwin->w_cursor.col == 0);
3809 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003810 while (inindent(1))
3811 {
3812 sol = TRUE;
3813 if (decl(&curwin->w_cursor) != 0)
3814 break;
3815 }
3816
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 /*
3818 * In Visual mode, when the resulting area is not bigger than what we
3819 * started with, extend it to the next block, and then exclude again.
3820 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003821 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 && VIsual_active)
3823 {
3824 curwin->w_cursor = old_start;
3825 decl(&curwin->w_cursor);
3826 if ((pos = findmatch(NULL, what)) == NULL)
3827 {
3828 curwin->w_cursor = old_pos;
3829 return FAIL;
3830 }
3831 start_pos = *pos;
3832 curwin->w_cursor = *pos;
3833 if ((end_pos = findmatch(NULL, other)) == NULL)
3834 {
3835 curwin->w_cursor = old_pos;
3836 return FAIL;
3837 }
3838 curwin->w_cursor = *end_pos;
3839 }
3840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 break;
3842 }
3843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 if (VIsual_active)
3845 {
3846 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003847 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003848 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 inc(&curwin->w_cursor); /* include the line break */
3850 VIsual = start_pos;
3851 VIsual_mode = 'v';
3852 redraw_curbuf_later(INVERTED); /* update the inversion */
3853 showmode();
3854 }
3855 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 {
3857 oap->start = start_pos;
3858 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003859 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003862 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003863 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003865 else
3866 /* End is before the start (no text in between <>, [], etc.): don't
3867 * operate on any text. */
3868 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870
3871 return OK;
3872}
3873
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003874/*
3875 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3876 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3877 */
3878 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003879in_html_tag(
3880 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003881{
3882 char_u *line = ml_get_curline();
3883 char_u *p;
3884 int c;
3885 int lc = NUL;
3886 pos_T pos;
3887
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003888 if (enc_dbcs)
3889 {
3890 char_u *lp = NULL;
3891
3892 /* We search forward until the cursor, because searching backwards is
3893 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003894 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003895 if (*p == '>' || *p == '<')
3896 {
3897 lc = *p;
3898 lp = p;
3899 }
3900 if (*p != '<') /* check for '<' under cursor */
3901 {
3902 if (lc != '<')
3903 return FALSE;
3904 p = lp;
3905 }
3906 }
3907 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003908 {
3909 for (p = line + curwin->w_cursor.col; p > line; )
3910 {
3911 if (*p == '<') /* find '<' under/before cursor */
3912 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003913 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003914 if (*p == '>') /* find '>' before cursor */
3915 break;
3916 }
3917 if (*p != '<')
3918 return FALSE;
3919 }
3920
3921 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003922 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003923
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003924 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003925 if (end_tag)
3926 /* check that there is a '/' after the '<' */
3927 return *p == '/';
3928
3929 /* check that there is no '/' after the '<' */
3930 if (*p == '/')
3931 return FALSE;
3932
3933 /* check that the matching '>' is not preceded by '/' */
3934 for (;;)
3935 {
3936 if (inc(&pos) < 0)
3937 return FALSE;
3938 c = *ml_get_pos(&pos);
3939 if (c == '>')
3940 break;
3941 lc = c;
3942 }
3943 return lc != '/';
3944}
3945
3946/*
3947 * Find tag block under the cursor, cursor at end.
3948 */
3949 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003950current_tagblock(
3951 oparg_T *oap,
3952 long count_arg,
3953 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003954{
3955 long count = count_arg;
3956 long n;
3957 pos_T old_pos;
3958 pos_T start_pos;
3959 pos_T end_pos;
3960 pos_T old_start, old_end;
3961 char_u *spat, *epat;
3962 char_u *p;
3963 char_u *cp;
3964 int len;
3965 int r;
3966 int do_include = include;
3967 int save_p_ws = p_ws;
3968 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003969 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003970
3971 p_ws = FALSE;
3972
3973 old_pos = curwin->w_cursor;
3974 old_end = curwin->w_cursor; /* remember where we started */
3975 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003976 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003977 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003978
3979 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003980 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003981 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003982 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003983 {
3984 setpcmark();
3985
3986 /* ignore indent */
3987 while (inindent(1))
3988 if (inc_cursor() != 0)
3989 break;
3990
3991 if (in_html_tag(FALSE))
3992 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003993 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003994 while (*ml_get_cursor() != '>')
3995 if (inc_cursor() < 0)
3996 break;
3997 }
3998 else if (in_html_tag(TRUE))
3999 {
4000 /* cursor on end tag, move to just before it */
4001 while (*ml_get_cursor() != '<')
4002 if (dec_cursor() < 0)
4003 break;
4004 dec_cursor();
4005 old_end = curwin->w_cursor;
4006 }
4007 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004008 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004009 {
4010 old_start = VIsual;
4011 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4012 }
4013 else
4014 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004015
4016again:
4017 /*
4018 * Search backwards for unclosed "<aaa>".
4019 * Put this position in start_pos.
4020 */
4021 for (n = 0; n < count; ++n)
4022 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004023 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004024 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004025 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004026 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004027 {
4028 curwin->w_cursor = old_pos;
4029 goto theend;
4030 }
4031 }
4032 start_pos = curwin->w_cursor;
4033
4034 /*
4035 * Search for matching "</aaa>". First isolate the "aaa".
4036 */
4037 inc_cursor();
4038 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004039 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004040 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004041 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004042 if (len == 0)
4043 {
4044 curwin->w_cursor = old_pos;
4045 goto theend;
4046 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004047 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004048 epat = alloc(len + 9);
4049 if (spat == NULL || epat == NULL)
4050 {
4051 vim_free(spat);
4052 vim_free(epat);
4053 curwin->w_cursor = old_pos;
4054 goto theend;
4055 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004056 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004057 sprintf((char *)epat, "</%.*s>\\c", len, p);
4058
Bram Moolenaar48570482017-10-30 21:48:41 +01004059 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004060 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004061
4062 vim_free(spat);
4063 vim_free(epat);
4064
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004065 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004066 {
4067 /* Can't find other end or it's before the previous end. Could be a
4068 * HTML tag that doesn't have a matching end. Search backwards for
4069 * another starting tag. */
4070 count = 1;
4071 curwin->w_cursor = start_pos;
4072 goto again;
4073 }
4074
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02004075 if (do_include)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004076 {
4077 /* Include up to the '>'. */
4078 while (*ml_get_cursor() != '>')
4079 if (inc_cursor() < 0)
4080 break;
4081 }
4082 else
4083 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004084 char_u *c = ml_get_cursor();
4085
4086 /* Exclude the '<' of the end tag.
4087 * If the closing tag is on new line, do not decrement cursor, but
4088 * make operation exclusive, so that the linefeed will be selected */
4089 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4090 /* do not decrement cursor */
4091 is_inclusive = FALSE;
4092 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004093 dec_cursor();
4094 }
4095 end_pos = curwin->w_cursor;
4096
4097 if (!do_include)
4098 {
4099 /* Exclude the start tag. */
4100 curwin->w_cursor = start_pos;
4101 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004102 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004103 {
4104 inc_cursor();
4105 start_pos = curwin->w_cursor;
4106 break;
4107 }
4108 curwin->w_cursor = end_pos;
4109
Bram Moolenaarb476cb72018-08-16 21:37:50 +02004110 // If we are in Visual mode and now have the same text as before set
4111 // "do_include" and try again.
4112 if (VIsual_active && EQUAL_POS(start_pos, old_start)
4113 && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004114 {
4115 do_include = TRUE;
4116 curwin->w_cursor = old_start;
4117 count = count_arg;
4118 goto again;
4119 }
4120 }
4121
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004122 if (VIsual_active)
4123 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004124 /* If the end is before the start there is no text between tags, select
4125 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004126 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004127 curwin->w_cursor = start_pos;
4128 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004129 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004130 VIsual = start_pos;
4131 VIsual_mode = 'v';
4132 redraw_curbuf_later(INVERTED); /* update the inversion */
4133 showmode();
4134 }
4135 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004136 {
4137 oap->start = start_pos;
4138 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004139 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004140 {
4141 /* End is before the start: there is no text between tags; operate
4142 * on an empty area. */
4143 curwin->w_cursor = start_pos;
4144 oap->inclusive = FALSE;
4145 }
4146 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004147 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004148 }
4149 retval = OK;
4150
4151theend:
4152 p_ws = save_p_ws;
4153 return retval;
4154}
4155
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004157current_par(
4158 oparg_T *oap,
4159 long count,
4160 int include, /* TRUE == include white space */
4161 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162{
4163 linenr_T start_lnum;
4164 linenr_T end_lnum;
4165 int white_in_front;
4166 int dir;
4167 int start_is_white;
4168 int prev_start_is_white;
4169 int retval = OK;
4170 int do_white = FALSE;
4171 int t;
4172 int i;
4173
4174 if (type == 'S') /* not implemented yet */
4175 return FAIL;
4176
4177 start_lnum = curwin->w_cursor.lnum;
4178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 /*
4180 * When visual area is more than one line: extend it.
4181 */
4182 if (VIsual_active && start_lnum != VIsual.lnum)
4183 {
4184extend:
4185 if (start_lnum < VIsual.lnum)
4186 dir = BACKWARD;
4187 else
4188 dir = FORWARD;
4189 for (i = count; --i >= 0; )
4190 {
4191 if (start_lnum ==
4192 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4193 {
4194 retval = FAIL;
4195 break;
4196 }
4197
4198 prev_start_is_white = -1;
4199 for (t = 0; t < 2; ++t)
4200 {
4201 start_lnum += dir;
4202 start_is_white = linewhite(start_lnum);
4203 if (prev_start_is_white == start_is_white)
4204 {
4205 start_lnum -= dir;
4206 break;
4207 }
4208 for (;;)
4209 {
4210 if (start_lnum == (dir == BACKWARD
4211 ? 1 : curbuf->b_ml.ml_line_count))
4212 break;
4213 if (start_is_white != linewhite(start_lnum + dir)
4214 || (!start_is_white
4215 && startPS(start_lnum + (dir > 0
4216 ? 1 : 0), 0, 0)))
4217 break;
4218 start_lnum += dir;
4219 }
4220 if (!include)
4221 break;
4222 if (start_lnum == (dir == BACKWARD
4223 ? 1 : curbuf->b_ml.ml_line_count))
4224 break;
4225 prev_start_is_white = start_is_white;
4226 }
4227 }
4228 curwin->w_cursor.lnum = start_lnum;
4229 curwin->w_cursor.col = 0;
4230 return retval;
4231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233 /*
4234 * First move back to the start_lnum of the paragraph or white lines
4235 */
4236 white_in_front = linewhite(start_lnum);
4237 while (start_lnum > 1)
4238 {
4239 if (white_in_front) /* stop at first white line */
4240 {
4241 if (!linewhite(start_lnum - 1))
4242 break;
4243 }
4244 else /* stop at first non-white line of start of paragraph */
4245 {
4246 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4247 break;
4248 }
4249 --start_lnum;
4250 }
4251
4252 /*
4253 * Move past the end of any white lines.
4254 */
4255 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004256 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4257 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 --end_lnum;
4260 i = count;
4261 if (!include && white_in_front)
4262 --i;
4263 while (i--)
4264 {
4265 if (end_lnum == curbuf->b_ml.ml_line_count)
4266 return FAIL;
4267
4268 if (!include)
4269 do_white = linewhite(end_lnum + 1);
4270
4271 if (include || !do_white)
4272 {
4273 ++end_lnum;
4274 /*
4275 * skip to end of paragraph
4276 */
4277 while (end_lnum < curbuf->b_ml.ml_line_count
4278 && !linewhite(end_lnum + 1)
4279 && !startPS(end_lnum + 1, 0, 0))
4280 ++end_lnum;
4281 }
4282
4283 if (i == 0 && white_in_front && include)
4284 break;
4285
4286 /*
4287 * skip to end of white lines after paragraph
4288 */
4289 if (include || do_white)
4290 while (end_lnum < curbuf->b_ml.ml_line_count
4291 && linewhite(end_lnum + 1))
4292 ++end_lnum;
4293 }
4294
4295 /*
4296 * If there are no empty lines at the end, try to find some empty lines at
4297 * the start (unless that has been done already).
4298 */
4299 if (!white_in_front && !linewhite(end_lnum) && include)
4300 while (start_lnum > 1 && linewhite(start_lnum - 1))
4301 --start_lnum;
4302
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (VIsual_active)
4304 {
4305 /* Problem: when doing "Vipipip" nothing happens in a single white
4306 * line, we get stuck there. Trap this here. */
4307 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4308 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004309 if (VIsual.lnum != start_lnum)
4310 {
4311 VIsual.lnum = start_lnum;
4312 VIsual.col = 0;
4313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 VIsual_mode = 'V';
4315 redraw_curbuf_later(INVERTED); /* update the inversion */
4316 showmode();
4317 }
4318 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 oap->start.lnum = start_lnum;
4321 oap->start.col = 0;
4322 oap->motion_type = MLINE;
4323 }
4324 curwin->w_cursor.lnum = end_lnum;
4325 curwin->w_cursor.col = 0;
4326
4327 return OK;
4328}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004329
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004330/*
4331 * Search quote char from string line[col].
4332 * Quote character escaped by one of the characters in "escape" is not counted
4333 * as a quote.
4334 * Returns column number of "quotechar" or -1 when not found.
4335 */
4336 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004337find_next_quote(
4338 char_u *line,
4339 int col,
4340 int quotechar,
4341 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004342{
4343 int c;
4344
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004345 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004346 {
4347 c = line[col];
4348 if (c == NUL)
4349 return -1;
4350 else if (escape != NULL && vim_strchr(escape, c))
4351 ++col;
4352 else if (c == quotechar)
4353 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004354 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004355 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004356 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004357 ++col;
4358 }
4359 return col;
4360}
4361
4362/*
4363 * Search backwards in "line" from column "col_start" to find "quotechar".
4364 * Quote character escaped by one of the characters in "escape" is not counted
4365 * as a quote.
4366 * Return the found column or zero.
4367 */
4368 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004369find_prev_quote(
4370 char_u *line,
4371 int col_start,
4372 int quotechar,
4373 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004374{
4375 int n;
4376
4377 while (col_start > 0)
4378 {
4379 --col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004380 col_start -= (*mb_head_off)(line, line + col_start);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004381 n = 0;
4382 if (escape != NULL)
4383 while (col_start - n > 0 && vim_strchr(escape,
4384 line[col_start - n - 1]) != NULL)
4385 ++n;
4386 if (n & 1)
4387 col_start -= n; /* uneven number of escape chars, skip it */
4388 else if (line[col_start] == quotechar)
4389 break;
4390 }
4391 return col_start;
4392}
4393
4394/*
4395 * Find quote under the cursor, cursor at end.
4396 * Returns TRUE if found, else FALSE.
4397 */
4398 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004399current_quote(
4400 oparg_T *oap,
4401 long count,
4402 int include, /* TRUE == include quote char */
4403 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004404{
4405 char_u *line = ml_get_curline();
4406 int col_end;
4407 int col_start = curwin->w_cursor.col;
4408 int inclusive = FALSE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004409 int vis_empty = TRUE; // Visual selection <= 1 char
4410 int vis_bef_curs = FALSE; // Visual starts before cursor
4411 int inside_quotes = FALSE; // Looks like "i'" done before
4412 int selected_quote = FALSE; // Has quote inside selection
Bram Moolenaarab194812005-09-14 21:40:12 +00004413 int i;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004414 int restore_vis_bef = FALSE; // restore VIsual on abort
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004415
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004416 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004417 if (VIsual_active)
4418 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004419 /* this only works within one line */
4420 if (VIsual.lnum != curwin->w_cursor.lnum)
4421 return FALSE;
4422
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004423 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004424 if (*p_sel == 'e')
4425 {
4426 if (!vis_bef_curs)
4427 {
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004428 // VIsual needs to be the start of Visual selection.
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004429 pos_T t = curwin->w_cursor;
4430
4431 curwin->w_cursor = VIsual;
4432 VIsual = t;
4433 vis_bef_curs = TRUE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004434 restore_vis_bef = TRUE;
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004435 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004436 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004437 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004438 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004439 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004440
4441 if (!vis_empty)
4442 {
4443 /* Check if the existing selection exactly spans the text inside
4444 * quotes. */
4445 if (vis_bef_curs)
4446 {
4447 inside_quotes = VIsual.col > 0
4448 && line[VIsual.col - 1] == quotechar
4449 && line[curwin->w_cursor.col] != NUL
4450 && line[curwin->w_cursor.col + 1] == quotechar;
4451 i = VIsual.col;
4452 col_end = curwin->w_cursor.col;
4453 }
4454 else
4455 {
4456 inside_quotes = curwin->w_cursor.col > 0
4457 && line[curwin->w_cursor.col - 1] == quotechar
4458 && line[VIsual.col] != NUL
4459 && line[VIsual.col + 1] == quotechar;
4460 i = curwin->w_cursor.col;
4461 col_end = VIsual.col;
4462 }
4463
4464 /* Find out if we have a quote in the selection. */
4465 while (i <= col_end)
4466 if (line[i++] == quotechar)
4467 {
4468 selected_quote = TRUE;
4469 break;
4470 }
4471 }
4472
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004473 if (!vis_empty && line[col_start] == quotechar)
4474 {
4475 /* Already selecting something and on a quote character. Find the
4476 * next quoted string. */
4477 if (vis_bef_curs)
4478 {
4479 /* Assume we are on a closing quote: move to after the next
4480 * opening quote. */
4481 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4482 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004483 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004484 col_end = find_next_quote(line, col_start + 1, quotechar,
4485 curbuf->b_p_qe);
4486 if (col_end < 0)
4487 {
4488 /* We were on a starting quote perhaps? */
4489 col_end = col_start;
4490 col_start = curwin->w_cursor.col;
4491 }
4492 }
4493 else
4494 {
4495 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4496 if (line[col_end] != quotechar)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004497 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004498 col_start = find_prev_quote(line, col_end, quotechar,
4499 curbuf->b_p_qe);
4500 if (line[col_start] != quotechar)
4501 {
4502 /* We were on an ending quote perhaps? */
4503 col_start = col_end;
4504 col_end = curwin->w_cursor.col;
4505 }
4506 }
4507 }
4508 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004509
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004510 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004511 {
4512 int first_col = col_start;
4513
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004514 if (!vis_empty)
4515 {
4516 if (vis_bef_curs)
4517 first_col = find_next_quote(line, col_start, quotechar, NULL);
4518 else
4519 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4520 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004521
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004522 /* The cursor is on a quote, we don't know if it's the opening or
4523 * closing quote. Search from the start of the line to find out.
4524 * Also do this when there is a Visual area, a' may leave the cursor
4525 * in between two strings. */
4526 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004527 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004528 {
4529 /* Find open quote character. */
4530 col_start = find_next_quote(line, col_start, quotechar, NULL);
4531 if (col_start < 0 || col_start > first_col)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004532 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004533 /* Find close quote character. */
4534 col_end = find_next_quote(line, col_start + 1, quotechar,
4535 curbuf->b_p_qe);
4536 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004537 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004538 /* If is cursor between start and end quote character, it is
4539 * target text object. */
4540 if (col_start <= first_col && first_col <= col_end)
4541 break;
4542 col_start = col_end + 1;
4543 }
4544 }
4545 else
4546 {
4547 /* Search backward for a starting quote. */
4548 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4549 if (line[col_start] != quotechar)
4550 {
4551 /* No quote before the cursor, look after the cursor. */
4552 col_start = find_next_quote(line, col_start, quotechar, NULL);
4553 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004554 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004555 }
4556
4557 /* Find close quote character. */
4558 col_end = find_next_quote(line, col_start + 1, quotechar,
4559 curbuf->b_p_qe);
4560 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004561 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004562 }
4563
4564 /* When "include" is TRUE, include spaces after closing quote or before
4565 * the starting quote. */
4566 if (include)
4567 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004568 if (VIM_ISWHITE(line[col_end + 1]))
4569 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004570 ++col_end;
4571 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004572 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004573 --col_start;
4574 }
4575
Bram Moolenaarab194812005-09-14 21:40:12 +00004576 /* Set start position. After vi" another i" must include the ".
4577 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004578 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004579 ++col_start;
4580 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004581 if (VIsual_active)
4582 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004583 /* Set the start of the Visual area when the Visual area was empty, we
4584 * were just inside quotes or the Visual area didn't start at a quote
4585 * and didn't include a quote.
4586 */
4587 if (vis_empty
4588 || (vis_bef_curs
4589 && !selected_quote
4590 && (inside_quotes
4591 || (line[VIsual.col] != quotechar
4592 && (VIsual.col == 0
4593 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004594 {
4595 VIsual = curwin->w_cursor;
4596 redraw_curbuf_later(INVERTED);
4597 }
4598 }
4599 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004600 {
4601 oap->start = curwin->w_cursor;
4602 oap->motion_type = MCHAR;
4603 }
4604
4605 /* Set end position. */
4606 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004607 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004608 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004609 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004610 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004611 if (VIsual_active)
4612 {
4613 if (vis_empty || vis_bef_curs)
4614 {
4615 /* decrement cursor when 'selection' is not exclusive */
4616 if (*p_sel != 'e')
4617 dec_cursor();
4618 }
4619 else
4620 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004621 /* Cursor is at start of Visual area. Set the end of the Visual
4622 * area when it was just inside quotes or it didn't end at a
4623 * quote. */
4624 if (inside_quotes
4625 || (!selected_quote
4626 && line[VIsual.col] != quotechar
4627 && (line[VIsual.col] == NUL
4628 || line[VIsual.col + 1] != quotechar)))
4629 {
4630 dec_cursor();
4631 VIsual = curwin->w_cursor;
4632 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004633 curwin->w_cursor.col = col_start;
4634 }
4635 if (VIsual_mode == 'V')
4636 {
4637 VIsual_mode = 'v';
4638 redraw_cmdline = TRUE; /* show mode later */
4639 }
4640 }
4641 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004642 {
4643 /* Set inclusive and other oap's flags. */
4644 oap->inclusive = inclusive;
4645 }
4646
4647 return OK;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004648
4649abort_search:
4650 if (VIsual_active && *p_sel == 'e')
4651 {
4652 inc_cursor();
4653 if (restore_vis_bef)
4654 {
4655 pos_T t = curwin->w_cursor;
4656
4657 curwin->w_cursor = VIsual;
4658 VIsual = t;
4659 }
4660 }
4661 return FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004662}
4663
4664#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004666static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004667
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004668/*
4669 * Find next search match under cursor, cursor at end.
4670 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004671 */
4672 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004673current_search(
4674 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004675 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004676{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004677 pos_T start_pos; // start position of the pattern match
4678 pos_T end_pos; // end position of the pattern match
4679 pos_T orig_pos; // position of the cursor at beginning
4680 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004681 int i;
4682 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004683 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004684 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004685 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004686 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004687 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004688
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004689 /* wrapping should not occur */
4690 p_ws = FALSE;
4691
4692 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004693 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004694 dec_cursor();
4695
4696 if (VIsual_active)
4697 {
4698 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004699
4700 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004701
4702 /* make sure, searching further will extend the match */
4703 if (VIsual_active)
4704 {
4705 if (forward)
4706 incl(&pos);
4707 else
4708 decl(&pos);
4709 }
4710 }
4711 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004712 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004713
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004714 /* Is the pattern is zero-width?, this time, don't care about the direction
4715 */
4716 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4717 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004718 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004719 {
4720 p_ws = old_p_ws;
4721 return FAIL; /* pattern not found */
4722 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004723
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004724 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004725 * The trick is to first search backwards and then search forward again,
4726 * so that a match at the current cursor position will be correctly
4727 * captured.
4728 */
4729 for (i = 0; i < 2; i++)
4730 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004731 if (forward)
4732 dir = i;
4733 else
4734 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004735
4736 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004737 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004738 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004739 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004740
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004741 result = searchit(curwin, curbuf, &pos, &end_pos,
4742 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004743 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004744 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004745
4746 /* First search may fail, but then start searching from the
4747 * beginning of the file (cursor might be on the search match)
4748 * except when Visual mode is active, so that extending the visual
4749 * selection works. */
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004750 if (i == 1 && !result) /* not found, abort */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004751 {
4752 curwin->w_cursor = orig_pos;
4753 if (VIsual_active)
4754 VIsual = save_VIsual;
4755 p_ws = old_p_ws;
4756 return FAIL;
4757 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004758 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004759 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004760 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004761 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004762 /* try again from start of buffer */
4763 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004764 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004765 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004766 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004767 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004768 /* searching backwards, so set pos to last line and col */
4769 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004770 pos.col = (colnr_T)STRLEN(
4771 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004772 }
4773 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004774 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004775 }
4776
4777 start_pos = pos;
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004778 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004779
4780 if (!VIsual_active)
4781 VIsual = start_pos;
4782
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004783 // put cursor on last character of match
4784 curwin->w_cursor = end_pos;
4785 if (LT_POS(VIsual, end_pos))
4786 dec_cursor();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004787 VIsual_active = TRUE;
4788 VIsual_mode = 'v';
4789
Bram Moolenaarb7633612019-02-10 21:48:25 +01004790 redraw_curbuf_later(INVERTED); /* update the inversion */
4791 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004792 {
Bram Moolenaarb7633612019-02-10 21:48:25 +01004793 /* Correction for exclusive selection depends on the direction. */
4794 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
4795 inc_cursor();
4796 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
4797 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004798 }
4799
4800#ifdef FEAT_FOLDING
4801 if (fdo_flags & FDO_SEARCH && KeyTyped)
4802 foldOpenCursor();
4803#endif
4804
4805 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004806 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004807#ifdef FEAT_CLIPBOARD
4808 /* Make sure the clipboard gets updated. Needed because start and
4809 * end are still the same, and the selection needs to be owned */
4810 clip_star.vmode = NUL;
4811#endif
4812 redraw_curbuf_later(INVERTED);
4813 showmode();
4814
4815 return OK;
4816}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004817
4818/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004819 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004820 * If move is TRUE, check from the beginning of the buffer, else from position
4821 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004822 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004823 * Returns TRUE, FALSE or -1 for failure.
4824 */
4825 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004826is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004827{
4828 regmmatch_T regmatch;
4829 int nmatched = 0;
4830 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004831 pos_T pos;
4832 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004833 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004834
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004835 if (pattern == NULL)
4836 pattern = spats[last_idx].pat;
4837
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004838 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4839 SEARCH_KEEP, &regmatch) == FAIL)
4840 return -1;
4841
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004842 /* init startcol correctly */
4843 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004844 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004845 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004846 {
4847 CLEAR_POS(&pos);
4848 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004849 else
4850 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004851 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004852 /* accept a match at the cursor position */
4853 flag = SEARCH_START;
4854 }
4855
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004856 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004857 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004858 {
4859 /* Zero-width pattern should match somewhere, then we can check if
4860 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004861 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004862 do
4863 {
4864 regmatch.startpos[0].col++;
4865 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004866 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004867 if (!nmatched)
4868 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004869 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4870 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004871
4872 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004873 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004874 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004875 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4876 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004877 /* one char width */
4878 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4879 result = TRUE;
4880 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004881 }
4882
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004883 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004884 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004885 return result;
4886}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4889 || defined(PROTO)
4890/*
4891 * return TRUE if line 'lnum' is empty or has white chars only.
4892 */
4893 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004894linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896 char_u *p;
4897
4898 p = skipwhite(ml_get(lnum));
4899 return (*p == NUL);
4900}
4901#endif
4902
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004903/*
4904 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02004905 * When "recompute" is TRUE always recompute the numbers.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004906 */
4907 static void
4908search_stat(
4909 int dirc,
4910 pos_T *pos,
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02004911 int show_top_bot_msg,
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02004912 char_u *msgbuf,
4913 int recompute)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004914{
4915 int save_ws = p_ws;
4916 int wraparound = FALSE;
4917 pos_T p = (*pos);
4918 static pos_T lastpos = {0, 0, 0};
4919 static int cur = 0;
4920 static int cnt = 0;
4921 static int chgtick = 0;
4922 static char_u *lastpat = NULL;
4923 static buf_T *lbuf = NULL;
4924#ifdef FEAT_RELTIME
4925 proftime_T start;
4926#endif
4927#define OUT_OF_TIME 999
4928
4929 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
4930 || (dirc == '/' && LT_POS(p, lastpos)));
4931
4932 // If anything relevant changed the count has to be recomputed.
4933 // MB_STRNICMP ignores case, but we should not ignore case.
4934 // Unfortunately, there is no MB_STRNICMP function.
4935 if (!(chgtick == CHANGEDTICK(curbuf)
4936 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
4937 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
4938 && EQUAL_POS(lastpos, curwin->w_cursor)
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02004939 && lbuf == curbuf) || wraparound || cur < 0 || cur > 99 || recompute)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004940 {
4941 cur = 0;
4942 cnt = 0;
4943 CLEAR_POS(&lastpos);
4944 lbuf = curbuf;
4945 }
4946
4947 if (EQUAL_POS(lastpos, curwin->w_cursor) && !wraparound
4948 && (dirc == '/' ? cur < cnt : cur > 0))
4949 cur += dirc == '/' ? 1 : -1;
4950 else
4951 {
4952 p_ws = FALSE;
4953#ifdef FEAT_RELTIME
4954 profile_setlimit(20L, &start);
4955#endif
4956 while (!got_int && searchit(curwin, curbuf, &lastpos, NULL,
Bram Moolenaar9ce3fa82019-05-07 21:29:11 +02004957 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST,
4958 (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004959 {
4960#ifdef FEAT_RELTIME
4961 // Stop after passing the time limit.
4962 if (profile_passed_limit(&start))
4963 {
4964 cnt = OUT_OF_TIME;
4965 cur = OUT_OF_TIME;
4966 break;
4967 }
4968#endif
4969 cnt++;
4970 if (LTOREQ_POS(lastpos, p))
4971 cur++;
4972 fast_breakcheck();
4973 if (cnt > 99)
4974 break;
4975 }
4976 if (got_int)
4977 cur = -1; // abort
4978 }
4979 if (cur > 0)
4980 {
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004981 char t[SEARCH_STAT_BUF_LEN] = "";
Bram Moolenaare2ad8262019-05-24 13:22:22 +02004982 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004983
4984#ifdef FEAT_RIGHTLEFT
4985 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
4986 {
4987 if (cur == OUT_OF_TIME)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004988 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004989 else if (cnt > 99 && cur > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004990 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004991 else if (cnt > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004992 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/%d]", cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004993 else
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004994 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cnt, cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004995 }
4996 else
4997#endif
4998 {
4999 if (cur == OUT_OF_TIME)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005000 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005001 else if (cnt > 99 && cur > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005002 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005003 else if (cnt > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005004 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>99]", cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005005 else
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005006 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cur, cnt);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005007 }
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005008
5009 len = STRLEN(t);
Bram Moolenaardc6855a2019-05-18 19:26:29 +02005010 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005011 {
Bram Moolenaar16b58ae2019-09-06 20:40:21 +02005012 mch_memmove(t + 2, t, len);
5013 t[0] = 'W';
5014 t[1] = ' ';
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005015 len += 2;
5016 }
5017
5018 mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005019 if (dirc == '?' && cur == 100)
5020 cur = -1;
5021
5022 vim_free(lastpat);
5023 lastpat = vim_strsave(spats[last_idx].pat);
5024 chgtick = CHANGEDTICK(curbuf);
5025 lbuf = curbuf;
5026 lastpos = p;
5027
Bram Moolenaar984f0312019-05-24 13:11:47 +02005028 // keep the message even after redraw, but don't put in history
5029 msg_hist_off = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005030 give_warning(msgbuf, FALSE);
Bram Moolenaar984f0312019-05-24 13:11:47 +02005031 msg_hist_off = FALSE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005032 }
5033 p_ws = save_ws;
5034}
5035
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036#if defined(FEAT_FIND_ID) || defined(PROTO)
5037/*
5038 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01005039 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005042find_pattern_in_path(
5043 char_u *ptr, /* pointer to search pattern */
5044 int dir UNUSED, /* direction of expansion */
5045 int len, /* length of search pattern */
5046 int whole, /* match whole words only */
5047 int skip_comments, /* don't match inside comments */
5048 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005050 long count,
5051 int action, /* What to do when we find it */
5052 linenr_T start_lnum, /* first line to start searching */
5053 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 SearchedFile *files; /* Stack of included files */
5056 SearchedFile *bigger; /* When we need more space */
5057 int max_path_depth = 50;
5058 long match_count = 1;
5059
5060 char_u *pat;
5061 char_u *new_fname;
5062 char_u *curr_fname = curbuf->b_fname;
5063 char_u *prev_fname = NULL;
5064 linenr_T lnum;
5065 int depth;
5066 int depth_displayed; /* For type==CHECK_PATH */
5067 int old_files;
5068 int already_searched;
5069 char_u *file_line;
5070 char_u *line;
5071 char_u *p;
5072 char_u save_char;
5073 int define_matched;
5074 regmatch_T regmatch;
5075 regmatch_T incl_regmatch;
5076 regmatch_T def_regmatch;
5077 int matched = FALSE;
5078 int did_show = FALSE;
5079 int found = FALSE;
5080 int i;
5081 char_u *already = NULL;
5082 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005083 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005084#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 win_T *curwin_save = NULL;
5086#endif
5087
5088 regmatch.regprog = NULL;
5089 incl_regmatch.regprog = NULL;
5090 def_regmatch.regprog = NULL;
5091
5092 file_line = alloc(LSIZE);
5093 if (file_line == NULL)
5094 return;
5095
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 if (type != CHECK_PATH && type != FIND_DEFINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 /* when CONT_SOL is set compare "ptr" with the beginning of the line
5098 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005099 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 pat = alloc(len + 5);
5102 if (pat == NULL)
5103 goto fpip_end;
5104 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
5105 /* ignore case according to p_ic, p_scs and pat */
5106 regmatch.rm_ic = ignorecase(pat);
5107 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5108 vim_free(pat);
5109 if (regmatch.regprog == NULL)
5110 goto fpip_end;
5111 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005112 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
5113 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005115 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (incl_regmatch.regprog == NULL)
5117 goto fpip_end;
5118 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
5119 }
5120 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
5121 {
5122 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5123 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5124 if (def_regmatch.regprog == NULL)
5125 goto fpip_end;
5126 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5127 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005128 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (files == NULL)
5130 goto fpip_end;
5131 old_files = max_path_depth;
5132 depth = depth_displayed = -1;
5133
5134 lnum = start_lnum;
5135 if (end_lnum > curbuf->b_ml.ml_line_count)
5136 end_lnum = curbuf->b_ml.ml_line_count;
5137 if (lnum > end_lnum) /* do at least one line */
5138 lnum = end_lnum;
5139 line = ml_get(lnum);
5140
5141 for (;;)
5142 {
5143 if (incl_regmatch.regprog != NULL
5144 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5145 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005146 char_u *p_fname = (curr_fname == curbuf->b_fname)
5147 ? curbuf->b_ffname : curr_fname;
5148
5149 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5150 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5151 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005152 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005153 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5154 else
5155 /* Use text after match with 'include'. */
5156 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005157 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 already_searched = FALSE;
5159 if (new_fname != NULL)
5160 {
5161 /* Check whether we have already searched in this file */
5162 for (i = 0;; i++)
5163 {
5164 if (i == depth + 1)
5165 i = old_files;
5166 if (i == max_path_depth)
5167 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02005168 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
5169 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
5171 if (type != CHECK_PATH &&
5172 action == ACTION_SHOW_ALL && files[i].matched)
5173 {
5174 msg_putchar('\n'); /* cursor below last one */
5175 if (!got_int) /* don't display if 'q'
5176 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005177 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
5179 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005180 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 prev_fname = NULL;
5182 }
5183 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01005184 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 already_searched = TRUE;
5186 break;
5187 }
5188 }
5189 }
5190
5191 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5192 || (new_fname == NULL && !already_searched)))
5193 {
5194 if (did_show)
5195 msg_putchar('\n'); /* cursor below last one */
5196 else
5197 {
5198 gotocmdline(TRUE); /* cursor at status line */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005199 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005201 msg_puts_title(_("not found "));
5202 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
5204 did_show = TRUE;
5205 while (depth_displayed < depth && !got_int)
5206 {
5207 ++depth_displayed;
5208 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005209 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005211 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213 if (!got_int) /* don't display if 'q' typed
5214 for "--more--" message */
5215 {
5216 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005217 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (new_fname != NULL)
5219 {
5220 /* using "new_fname" is more reliable, e.g., when
5221 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005222 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224 else
5225 {
5226 /*
5227 * Isolate the file name.
5228 * Include the surrounding "" or <> if present.
5229 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005230 if (inc_opt != NULL
5231 && strstr((char *)inc_opt, "\\zs") != NULL)
5232 {
5233 /* pattern contains \zs, use the match */
5234 p = incl_regmatch.startp[0];
5235 i = (int)(incl_regmatch.endp[0]
5236 - incl_regmatch.startp[0]);
5237 }
5238 else
5239 {
5240 /* find the file name after the end of the match */
5241 for (p = incl_regmatch.endp[0];
5242 *p && !vim_isfilec(*p); p++)
5243 ;
5244 for (i = 0; vim_isfilec(p[i]); i++)
5245 ;
5246 }
5247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 if (i == 0)
5249 {
5250 /* Nothing found, use the rest of the line. */
5251 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005252 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005254 /* Avoid checking before the start of the line, can
5255 * happen if \zs appears in the regexp. */
5256 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 if (p[-1] == '"' || p[-1] == '<')
5259 {
5260 --p;
5261 ++i;
5262 }
5263 if (p[i] == '"' || p[i] == '>')
5264 ++i;
5265 }
5266 save_char = p[i];
5267 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005268 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 p[i] = save_char;
5270 }
5271
5272 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5273 {
5274 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005275 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005277 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 }
5280 out_flush(); /* output each line directly */
5281 }
5282
5283 if (new_fname != NULL)
5284 {
5285 /* Push the new file onto the file stack */
5286 if (depth + 1 == old_files)
5287 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005288 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (bigger != NULL)
5290 {
5291 for (i = 0; i <= depth; i++)
5292 bigger[i] = files[i];
5293 for (i = depth + 1; i < old_files + max_path_depth; i++)
5294 {
5295 bigger[i].fp = NULL;
5296 bigger[i].name = NULL;
5297 bigger[i].lnum = 0;
5298 bigger[i].matched = FALSE;
5299 }
5300 for (i = old_files; i < max_path_depth; i++)
5301 bigger[i + max_path_depth] = files[i];
5302 old_files += max_path_depth;
5303 max_path_depth *= 2;
5304 vim_free(files);
5305 files = bigger;
5306 }
5307 }
5308 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5309 == NULL)
5310 vim_free(new_fname);
5311 else
5312 {
5313 if (++depth == old_files)
5314 {
5315 /*
5316 * lalloc() for 'bigger' must have failed above. We
5317 * will forget one of our already visited files now.
5318 */
5319 vim_free(files[old_files].name);
5320 ++old_files;
5321 }
5322 files[depth].name = curr_fname = new_fname;
5323 files[depth].lnum = 0;
5324 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 if (action == ACTION_EXPAND)
5326 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005327 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005328 vim_snprintf((char*)IObuff, IOSIZE,
5329 _("Scanning included file: %s"),
5330 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005331 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005333 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005334 {
5335 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005336 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005337 (char *)new_fname);
5338 verbose_leave();
5339 }
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 }
5343 }
5344 else
5345 {
5346 /*
5347 * Check if the line is a define (type == FIND_DEFINE)
5348 */
5349 p = line;
5350search_line:
5351 define_matched = FALSE;
5352 if (def_regmatch.regprog != NULL
5353 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5354 {
5355 /*
5356 * Pattern must be first identifier after 'define', so skip
5357 * to that position before checking for match of pattern. Also
5358 * don't let it match beyond the end of this identifier.
5359 */
5360 p = def_regmatch.endp[0];
5361 while (*p && !vim_iswordc(*p))
5362 p++;
5363 define_matched = TRUE;
5364 }
5365
5366 /*
5367 * Look for a match. Don't do this if we are looking for a
5368 * define and this line didn't match define_prog above.
5369 */
5370 if (def_regmatch.regprog == NULL || define_matched)
5371 {
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005372 if (define_matched || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 {
5374 /* compare the first "len" chars from "ptr" */
5375 startp = skipwhite(p);
5376 if (p_ic)
5377 matched = !MB_STRNICMP(startp, ptr, len);
5378 else
5379 matched = !STRNCMP(startp, ptr, len);
5380 if (matched && define_matched && whole
5381 && vim_iswordc(startp[len]))
5382 matched = FALSE;
5383 }
5384 else if (regmatch.regprog != NULL
5385 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5386 {
5387 matched = TRUE;
5388 startp = regmatch.startp[0];
5389 /*
5390 * Check if the line is not a comment line (unless we are
5391 * looking for a define). A line starting with "# define"
5392 * is not considered to be a comment line.
5393 */
5394 if (!define_matched && skip_comments)
5395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if ((*line != '#' ||
5397 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005398 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 matched = FALSE;
5400
5401 /*
5402 * Also check for a "/ *" or "/ /" before the match.
5403 * Skips lines like "int backwards; / * normal index
5404 * * /" when looking for "normal".
5405 * Note: Doesn't skip "/ *" in comments.
5406 */
5407 p = skipwhite(line);
5408 if (matched
5409 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 for (p = line; *p && p < startp; ++p)
5411 {
5412 if (matched
5413 && p[0] == '/'
5414 && (p[1] == '*' || p[1] == '/'))
5415 {
5416 matched = FALSE;
5417 /* After "//" all text is comment */
5418 if (p[1] == '/')
5419 break;
5420 ++p;
5421 }
5422 else if (!matched && p[0] == '*' && p[1] == '/')
5423 {
5424 /* Can find match after "* /". */
5425 matched = TRUE;
5426 ++p;
5427 }
5428 }
5429 }
5430 }
5431 }
5432 }
5433 if (matched)
5434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 if (action == ACTION_EXPAND)
5436 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005437 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 int add_r;
5439 char_u *aux;
5440
5441 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5442 break;
5443 found = TRUE;
5444 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005445 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 if (vim_iswordp(p))
5449 goto exit_matched;
5450 p = find_word_start(p);
5451 }
5452 p = find_word_end(p);
5453 i = (int)(p - aux);
5454
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005455 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005459
5460 /* Get the next line: when "depth" < 0 from the current
5461 * buffer, otherwise from the included file. Jump to
5462 * exit_matched when past the last line. */
5463 if (depth < 0)
5464 {
5465 if (lnum >= end_lnum)
5466 goto exit_matched;
5467 line = ml_get(++lnum);
5468 }
5469 else if (vim_fgets(line = file_line,
5470 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 goto exit_matched;
5472
5473 /* we read a line, set "already" to check this "line" later
5474 * if depth >= 0 we'll increase files[depth].lnum far
5475 * bellow -- Acevedo */
5476 already = aux = p = skipwhite(line);
5477 p = find_word_start(p);
5478 p = find_word_end(p);
5479 if (p > aux)
5480 {
5481 if (*aux != ')' && IObuff[i-1] != TAB)
5482 {
5483 if (IObuff[i-1] != ' ')
5484 IObuff[i++] = ' ';
5485 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5486 if (p_js
5487 && (IObuff[i-2] == '.'
5488 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5489 && (IObuff[i-2] == '?'
5490 || IObuff[i-2] == '!'))))
5491 IObuff[i++] = ' ';
5492 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005493 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (p - aux >= IOSIZE - i)
5495 p = aux + IOSIZE - i - 1;
5496 STRNCPY(IObuff + i, aux, p - aux);
5497 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005498 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500 IObuff[i] = NUL;
5501 aux = IObuff;
5502
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 goto exit_matched;
5505 }
5506
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005507 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005509 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 if (add_r == OK)
5511 /* if dir was BACKWARD then honor it just once */
5512 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005513 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 break;
5515 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005516 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 {
5518 found = TRUE;
5519 if (!did_show)
5520 gotocmdline(TRUE); /* cursor at status line */
5521 if (curr_fname != prev_fname)
5522 {
5523 if (did_show)
5524 msg_putchar('\n'); /* cursor below last one */
5525 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005526 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 msg_home_replace_hl(curr_fname);
5528 prev_fname = curr_fname;
5529 }
5530 did_show = TRUE;
5531 if (!got_int)
5532 show_pat_in_path(line, type, TRUE, action,
5533 (depth == -1) ? NULL : files[depth].fp,
5534 (depth == -1) ? &lnum : &files[depth].lnum,
5535 match_count++);
5536
5537 /* Set matched flag for this file and all the ones that
5538 * include it */
5539 for (i = 0; i <= depth; ++i)
5540 files[i].matched = TRUE;
5541 }
5542 else if (--count <= 0)
5543 {
5544 found = TRUE;
5545 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005546#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 && g_do_tagpreview == 0
5548#endif
5549 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005550 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 else if (action == ACTION_SHOW)
5552 {
5553 show_pat_in_path(line, type, did_show, action,
5554 (depth == -1) ? NULL : files[depth].fp,
5555 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5556 did_show = TRUE;
5557 }
5558 else
5559 {
5560#ifdef FEAT_GUI
5561 need_mouse_correct = TRUE;
5562#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005563#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 /* ":psearch" uses the preview window */
5565 if (g_do_tagpreview != 0)
5566 {
5567 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02005568 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570#endif
5571 if (action == ACTION_SPLIT)
5572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005575 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577 if (depth == -1)
5578 {
5579 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005580#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 if (g_do_tagpreview != 0)
5582 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005583 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005584 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005585 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 break; /* failed to jump to file */
5587 }
5588 else
5589#endif
5590 setpcmark();
5591 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005592 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594 else
5595 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005596 if (!GETFILE_SUCCESS(getfile(
5597 0, files[depth].name, NULL, TRUE,
5598 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 break; /* failed to jump to file */
5600 /* autocommands may have changed the lnum, we don't
5601 * want that here */
5602 curwin->w_cursor.lnum = files[depth].lnum;
5603 }
5604 }
5605 if (action != ACTION_SHOW)
5606 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005607 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 curwin->w_set_curswant = TRUE;
5609 }
5610
Bram Moolenaar4033c552017-09-16 20:54:51 +02005611#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005613 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 /* Return cursor to where we were */
5616 validate_cursor();
5617 redraw_later(VALID);
5618 win_enter(curwin_save, TRUE);
5619 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02005620# ifdef FEAT_TEXT_PROP
5621 else if (WIN_IS_POPUP(curwin))
5622 // can't keep focus in popup window
5623 win_enter(firstwin, TRUE);
5624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625#endif
5626 break;
5627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 matched = FALSE;
5630 /* look for other matches in the rest of the line if we
5631 * are not at the end of it already */
5632 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005634 && !(compl_cont_status & CONT_SOL)
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005635 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005636 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 goto search_line;
5638 }
5639 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005641 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005642 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 break;
5644
5645 /*
5646 * Read the next line. When reading an included file and encountering
5647 * end-of-file, close the file and continue in the file that included
5648 * it.
5649 */
5650 while (depth >= 0 && !already
5651 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5652 {
5653 fclose(files[depth].fp);
5654 --old_files;
5655 files[old_files].name = files[depth].name;
5656 files[old_files].matched = files[depth].matched;
5657 --depth;
5658 curr_fname = (depth == -1) ? curbuf->b_fname
5659 : files[depth].name;
5660 if (depth < depth_displayed)
5661 depth_displayed = depth;
5662 }
5663 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005666 /* Remove any CR and LF from the line. */
5667 i = (int)STRLEN(line);
5668 if (i > 0 && line[i - 1] == '\n')
5669 line[--i] = NUL;
5670 if (i > 0 && line[i - 1] == '\r')
5671 line[--i] = NUL;
5672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 else if (!already)
5674 {
5675 if (++lnum > end_lnum)
5676 break;
5677 line = ml_get(lnum);
5678 }
5679 already = NULL;
5680 }
5681 /* End of big for (;;) loop. */
5682
5683 /* Close any files that are still open. */
5684 for (i = 0; i <= depth; i++)
5685 {
5686 fclose(files[i].fp);
5687 vim_free(files[i].name);
5688 }
5689 for (i = old_files; i < max_path_depth; i++)
5690 vim_free(files[i].name);
5691 vim_free(files);
5692
5693 if (type == CHECK_PATH)
5694 {
5695 if (!did_show)
5696 {
5697 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005698 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005700 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005703 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005705 if (got_int || ins_compl_interrupted())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005706 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005708 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005710 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5713 msg_end();
5714
5715fpip_end:
5716 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005717 vim_regfree(regmatch.regprog);
5718 vim_regfree(incl_regmatch.regprog);
5719 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720}
5721
5722 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005723show_pat_in_path(
5724 char_u *line,
5725 int type,
5726 int did_show,
5727 int action,
5728 FILE *fp,
5729 linenr_T *lnum,
5730 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 char_u *p;
5733
5734 if (did_show)
5735 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005736 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 gotocmdline(TRUE); /* cursor at status line */
5738 if (got_int) /* 'q' typed at "--more--" message */
5739 return;
5740 for (;;)
5741 {
5742 p = line + STRLEN(line) - 1;
5743 if (fp != NULL)
5744 {
5745 /* We used fgets(), so get rid of newline at end */
5746 if (p >= line && *p == '\n')
5747 --p;
5748 if (p >= line && *p == '\r')
5749 --p;
5750 *(p + 1) = NUL;
5751 }
5752 if (action == ACTION_SHOW_ALL)
5753 {
5754 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005755 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5757 /* Highlight line numbers */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005758 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
5759 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005761 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 out_flush(); /* show one line at a time */
5763
5764 /* Definition continues until line that doesn't end with '\' */
5765 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5766 break;
5767
5768 if (fp != NULL)
5769 {
5770 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5771 break;
5772 ++*lnum;
5773 }
5774 else
5775 {
5776 if (++*lnum > curbuf->b_ml.ml_line_count)
5777 break;
5778 line = ml_get(*lnum);
5779 }
5780 msg_putchar('\n');
5781 }
5782}
5783#endif
5784
5785#ifdef FEAT_VIMINFO
Bram Moolenaarc3328162019-07-23 22:15:25 +02005786 spat_T *
5787get_spat(int idx)
5788{
5789 return &spats[idx];
5790}
5791
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02005793get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
Bram Moolenaarc3328162019-07-23 22:15:25 +02005795 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797#endif