blob: 219afd5755c075df7afac94e4a9a0f007a846acf [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 Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef FEAT_FIND_ID
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010020static void show_pat_in_path(char_u *, int,
21 int, int, FILE *, linenr_T *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020023
24typedef struct searchstat
25{
26 int cur; // current position of found words
27 int cnt; // total count of found words
28 int exact_match; // TRUE if matched exactly on specified position
29 int incomplete; // 0: search was fully completed
30 // 1: recomputing was timed out
31 // 2: max count exceeded
32 int last_maxcount; // the max count of the last search
33} searchstat_T;
34
35static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, int recompute, int maxcount, long timeout);
36static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout);
37
Bram Moolenaarea6561a2020-06-01 21:32:45 +020038#define SEARCH_STAT_DEF_TIMEOUT 40L
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020039#define SEARCH_STAT_DEF_MAX_COUNT 99
40#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar071d4272004-06-13 20:20:40 +000042/*
43 * This file contains various searching-related routines. These fall into
44 * three groups:
45 * 1. string searches (for /, ?, n, and N)
46 * 2. character searches within a single line (for f, F, t, T, etc)
47 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
48 */
49
50/*
51 * String searches
52 *
53 * The string search functions are divided into two levels:
54 * lowest: searchit(); uses an pos_T for starting position and found match.
55 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
56 *
57 * The last search pattern is remembered for repeating the same search.
58 * This pattern is shared between the :g, :s, ? and / commands.
59 * This is in search_regcomp().
60 *
61 * The actual string matching is done using a heavily modified version of
62 * Henry Spencer's regular expression library. See regexp.c.
63 */
64
Bram Moolenaar071d4272004-06-13 20:20:40 +000065/*
66 * Two search patterns are remembered: One for the :substitute command and
67 * one for other searches. last_idx points to the one that was used the last
68 * time.
69 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020070static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar63d9e732019-12-05 21:10:38 +010072 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat
73 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat
Bram Moolenaar071d4272004-06-13 20:20:40 +000074};
75
Bram Moolenaar63d9e732019-12-05 21:10:38 +010076static int last_idx = 0; // index in spats[] for RE_LAST
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
Bram Moolenaar63d9e732019-12-05 21:10:38 +010078static char_u lastc[2] = {NUL, NUL}; // last character searched for
79static int lastcdir = FORWARD; // last direction of character search
80static int last_t_cmd = TRUE; // last search t_cmd
Bram Moolenaardbd24b52015-08-11 14:26:19 +020081static char_u lastc_bytes[MB_MAXBYTES + 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +010082static int lastc_bytelen = 1; // >1 for multi-byte char
Bram Moolenaardbd24b52015-08-11 14:26:19 +020083
Bram Moolenaar63d9e732019-12-05 21:10:38 +010084// copy of spats[], for keeping the search patterns while executing autocmds
Bram Moolenaarc3328162019-07-23 22:15:25 +020085static spat_T saved_spats[2];
Bram Moolenaara2cff1d2021-10-15 12:51:29 +010086static char_u *saved_mr_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000087# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +010088static int saved_spats_last_idx = 0;
89static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
Bram Moolenaara2cff1d2021-10-15 12:51:29 +010092// allocated copy of pattern used by search_regcomp()
93static char_u *mr_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95#ifdef FEAT_FIND_ID
96/*
97 * Type used by find_pattern_in_path() to remember which included files have
98 * been searched already.
99 */
100typedef struct SearchedFile
101{
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100102 FILE *fp; // File pointer
103 char_u *name; // Full name of file
104 linenr_T lnum; // Line we were up to in file
105 int matched; // Found a match in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106} SearchedFile;
107#endif
108
109/*
110 * translate search pattern for vim_regcomp()
111 *
112 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
113 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
114 * pat_save == RE_BOTH: save pat in both patterns (:global command)
115 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000116 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
118 * options & SEARCH_HIS: put search string in history
119 * options & SEARCH_KEEP: keep previous search pattern
120 *
121 * returns FAIL if failed, OK otherwise.
122 */
123 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100124search_regcomp(
125 char_u *pat,
Rob Pillinge86190e2022-12-23 19:06:04 +0000126 char_u **used_pat,
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100127 int pat_save,
128 int pat_use,
129 int options,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100130 regmmatch_T *regmatch) // return: pattern and ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 int magic;
133 int i;
134
135 rc_did_emsg = FALSE;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100136 magic = magic_isset();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
138 /*
139 * If no pattern given, use a previously defined pattern.
140 */
141 if (pat == NULL || *pat == NUL)
142 {
143 if (pat_use == RE_LAST)
144 i = last_idx;
145 else
146 i = pat_use;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100147 if (spats[i].pat == NULL) // pattern was never defined
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 {
149 if (pat_use == RE_SUBST)
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200150 emsg(_(e_no_previous_substitute_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200152 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 rc_did_emsg = TRUE;
154 return FAIL;
155 }
156 pat = spats[i].pat;
157 magic = spats[i].magic;
158 no_smartcase = spats[i].no_scs;
159 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100160 else if (options & SEARCH_HIS) // put new pattern in history
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Rob Pillinge86190e2022-12-23 19:06:04 +0000163 if (used_pat)
164 *used_pat = pat;
165
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100166 vim_free(mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100169 mr_pattern = reverse_text(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 else
171#endif
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100172 mr_pattern = vim_strsave(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 /*
175 * Save the currently used pattern in the appropriate place,
176 * unless the pattern should not be remembered.
177 */
Bram Moolenaare1004402020-10-24 20:49:43 +0200178 if (!(options & SEARCH_KEEP)
179 && (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100181 // search or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
183 save_re_pat(RE_SEARCH, pat, magic);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100184 // substitute or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
186 save_re_pat(RE_SUBST, pat, magic);
187 }
188
189 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000190 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
192 if (regmatch->regprog == NULL)
193 return FAIL;
194 return OK;
195}
196
197/*
198 * Get search pattern used by search_regcomp().
199 */
200 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100201get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
203 return mr_pattern;
204}
205
Bram Moolenaarabc97732007-08-08 20:49:37 +0000206#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207/*
208 * Reverse text into allocated memory.
209 * Returns the allocated string, NULL when out of memory.
210 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000211 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100212reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 unsigned len;
215 unsigned s_i, rev_i;
216 char_u *rev;
217
218 /*
219 * Reverse the pattern.
220 */
221 len = (unsigned)STRLEN(s);
222 rev = alloc(len + 1);
223 if (rev != NULL)
224 {
225 rev_i = len;
226 for (s_i = 0; s_i < len; ++s_i)
227 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 if (has_mbyte)
229 {
230 int mb_len;
231
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000232 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 rev_i -= mb_len;
234 mch_memmove(rev + rev_i, s + s_i, mb_len);
235 s_i += mb_len - 1;
236 }
237 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 rev[--rev_i] = s[s_i];
239
240 }
241 rev[len] = NUL;
242 }
243 return rev;
244}
245#endif
246
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100247 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100248save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
250 if (spats[idx].pat != pat)
251 {
252 vim_free(spats[idx].pat);
253 spats[idx].pat = vim_strsave(pat);
254 spats[idx].magic = magic;
255 spats[idx].no_scs = no_smartcase;
256 last_idx = idx;
257#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100258 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (p_hls)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100260 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200261 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#endif
263 }
264}
265
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266/*
267 * Save the search patterns, so they can be restored later.
268 * Used before/after executing autocommands and user functions.
269 */
270static int save_level = 0;
271
272 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100273save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274{
275 if (save_level++ == 0)
276 {
277 saved_spats[0] = spats[0];
278 if (spats[0].pat != NULL)
279 saved_spats[0].pat = vim_strsave(spats[0].pat);
280 saved_spats[1] = spats[1];
281 if (spats[1].pat != NULL)
282 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100283 if (mr_pattern == NULL)
284 saved_mr_pattern = NULL;
285 else
286 saved_mr_pattern = vim_strsave(mr_pattern);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100287#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100288 saved_spats_last_idx = last_idx;
289 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 }
292}
293
294 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100295restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296{
297 if (--save_level == 0)
298 {
299 vim_free(spats[0].pat);
300 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100301#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000302 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 vim_free(spats[1].pat);
305 spats[1] = saved_spats[1];
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100306 vim_free(mr_pattern);
307 mr_pattern = saved_mr_pattern;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100308#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100309 last_idx = saved_spats_last_idx;
310 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 }
313}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000315#if defined(EXITFREE) || defined(PROTO)
316 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100317free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000318{
319 vim_free(spats[0].pat);
320 vim_free(spats[1].pat);
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100321 VIM_CLEAR(mr_pattern);
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;
Christian Brabandt6dd74242022-02-14 12:44:32 +0000332static int saved_search_match_endcol;
333static int saved_search_match_lines;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100334
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100335/*
336 * Save and restore the search pattern for incremental highlight search
337 * feature.
338 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100339 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100340 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100341 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100342 */
343 void
344save_last_search_pattern(void)
345{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200346 if (++did_save_last_search_spat != 1)
347 // nested call, nothing to do
348 return;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100349
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100350 saved_last_search_spat = spats[RE_SEARCH];
351 if (spats[RE_SEARCH].pat != NULL)
352 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
353 saved_last_idx = last_idx;
354 saved_no_hlsearch = no_hlsearch;
355}
356
357 void
358restore_last_search_pattern(void)
359{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200360 if (--did_save_last_search_spat > 0)
361 // nested call, nothing to do
362 return;
363 if (did_save_last_search_spat != 0)
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100364 {
Bram Moolenaar442a8532020-06-04 20:56:09 +0200365 iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100366 return;
367 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100368
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100369 vim_free(spats[RE_SEARCH].pat);
370 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100371 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100372# if defined(FEAT_EVAL)
373 set_vv_searchforward();
374# endif
375 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200376 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100377}
Bram Moolenaard0480092017-11-16 22:20:39 +0100378
Christian Brabandt6dd74242022-02-14 12:44:32 +0000379/*
380 * Save and restore the incsearch highlighting variables.
381 * This is required so that calling searchcount() at does not invalidate the
382 * incsearch highlighting.
383 */
384 static void
385save_incsearch_state(void)
386{
387 saved_search_match_endcol = search_match_endcol;
388 saved_search_match_lines = search_match_lines;
389}
390
391 static void
392restore_incsearch_state(void)
393{
394 search_match_endcol = saved_search_match_endcol;
395 search_match_lines = saved_search_match_lines;
396}
397
Bram Moolenaard0480092017-11-16 22:20:39 +0100398 char_u *
399last_search_pattern(void)
400{
401 return spats[RE_SEARCH].pat;
402}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100403#endif
404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
406 * Return TRUE when case should be ignored for search pattern "pat".
407 * Uses the 'ignorecase' and 'smartcase' options.
408 */
409 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100410ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200412 return ignorecase_opt(pat, p_ic, p_scs);
413}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200415/*
416 * As ignorecase() put pass the "ic" and "scs" flags.
417 */
418 int
419ignorecase_opt(char_u *pat, int ic_in, int scs)
420{
421 int ic = ic_in;
422
423 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200424 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200425 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 no_smartcase = FALSE;
427
428 return ic;
429}
430
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200431/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200432 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200433 */
434 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100435pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200436{
437 char_u *p = pat;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200438 magic_T magic_val = MAGIC_ON;
439
440 // get the magicness of the pattern
441 (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val);
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200442
443 while (*p != NUL)
444 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200445 int l;
446
447 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
448 {
449 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
450 return TRUE;
451 p += l;
452 }
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200453 else if (*p == '\\' && magic_val <= MAGIC_ON)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200454 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100455 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200456 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100457 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200458 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100459 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200460 p += 2;
461 else
462 p += 1;
463 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200464 else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL)
465 {
466 if (p[1] != NUL) // skip "_X" and %X
467 p += 2;
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200468 else
469 p++;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200470 }
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200471 else if (MB_ISUPPER(*p))
472 return TRUE;
473 else
474 ++p;
475 }
476 return FALSE;
477}
478
Bram Moolenaar113e1072019-01-20 15:30:40 +0100479#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100481last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200482{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200483 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200484}
485
486 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100487last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200488{
489 return lastcdir == FORWARD;
490}
491
492 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100493last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200494{
495 return last_t_cmd == TRUE;
496}
497
498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100499set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200500{
501 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200502 lastc_bytelen = len;
503 if (len)
504 memcpy(lastc_bytes, s, len);
505 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200506 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200507}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100508#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200509
510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100511set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200512{
513 lastcdir = cdir;
514}
515
516 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100517set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200518{
519 last_t_cmd = t_cmd;
520}
521
522 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100523last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 return spats[last_idx].pat;
526}
527
528/*
529 * Reset search direction to forward. For "gd" and "gD" commands.
530 */
531 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100532reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#if defined(FEAT_EVAL)
536 set_vv_searchforward();
537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538}
539
540#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
541/*
542 * Set the last search pattern. For ":let @/ =" and viminfo.
543 * Also set the saved search pattern, so that this works in an autocommand.
544 */
545 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100546set_last_search_pat(
547 char_u *s,
548 int idx,
549 int magic,
550 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
552 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100553 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (*s == NUL)
555 spats[idx].pat = NULL;
556 else
557 spats[idx].pat = vim_strsave(s);
558 spats[idx].magic = magic;
559 spats[idx].no_scs = FALSE;
560 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#if defined(FEAT_EVAL)
562 set_vv_searchforward();
563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 spats[idx].off.line = FALSE;
565 spats[idx].off.end = FALSE;
566 spats[idx].off.off = 0;
567 if (setlast)
568 last_idx = idx;
569 if (save_level)
570 {
571 vim_free(saved_spats[idx].pat);
572 saved_spats[idx] = spats[0];
573 if (spats[idx].pat == NULL)
574 saved_spats[idx].pat = NULL;
575 else
576 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100577# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100578 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100582 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100584 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585# endif
586}
587#endif
588
589#ifdef FEAT_SEARCH_EXTRA
590/*
591 * Get a regexp program for the last used search pattern.
592 * This is used for highlighting all matches in a window.
593 * Values returned in regmatch->regprog and regmatch->rmm_ic.
594 */
595 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100596last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 if (spats[last_idx].pat == NULL)
599 {
600 regmatch->regprog = NULL;
601 return;
602 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100603 ++emsg_off; // So it doesn't beep if bad expr
Rob Pillinge86190e2022-12-23 19:06:04 +0000604 (void)search_regcomp((char_u *)"", NULL, 0, last_idx, SEARCH_KEEP, regmatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 --emsg_off;
606}
607#endif
608
609/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100610 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100611 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
612 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 *
614 * if (options & SEARCH_MSG) == 0 don't give any messages
615 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
616 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
617 * if (options & SEARCH_HIS) put search pattern in history
618 * if (options & SEARCH_END) return position at end of match
619 * if (options & SEARCH_START) accept match at pos itself
620 * if (options & SEARCH_KEEP) keep previous search pattern
621 * if (options & SEARCH_FOLD) match only once in a closed fold
622 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100623 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 *
625 * Return FAIL (zero) for failure, non-zero for success.
626 * When FEAT_EVAL is defined, returns the index of the first matching
627 * subpattern plus one; one if there was none.
628 */
629 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100630searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200631 win_T *win, // window to search in; can be NULL for a
632 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100633 buf_T *buf,
634 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100635 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100636 int dir,
637 char_u *pat,
638 long count,
639 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200640 int pat_use, // which pattern to use when "pat" is empty
641 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100644 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100645 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 regmmatch_T regmatch;
647 char_u *ptr;
648 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000650 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 int loop;
652 pos_T start_pos;
653 int at_first_line;
654 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200655 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 int match_ok;
657 long nmatched;
658 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100659 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100660 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661#ifdef FEAT_SEARCH_EXTRA
662 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200664 linenr_T stop_lnum = 0; // stop after this line number when != 0
Paul Ollis65745772022-06-05 16:55:54 +0100665 int unused_timeout_flag = FALSE;
666 int *timed_out = &unused_timeout_flag; // set when timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Rob Pillinge86190e2022-12-23 19:06:04 +0000668 if (search_regcomp(pat, NULL, RE_SEARCH, pat_use,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
670 {
671 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000672 semsg(_(e_invalid_search_string_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 return FAIL;
674 }
675
Paul Ollis65745772022-06-05 16:55:54 +0100676 if (extra_arg != NULL)
677 {
678 stop_lnum = extra_arg->sa_stop_lnum;
679#ifdef FEAT_RELTIME
680 if (extra_arg->sa_tm > 0)
Paul Ollis65745772022-06-05 16:55:54 +0100681 init_regexp_timeout(extra_arg->sa_tm);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100682 // Also set the pointer when sa_tm is zero, the caller may have set the
683 // timeout.
684 timed_out = &extra_arg->sa_timed_out;
Paul Ollis65745772022-06-05 16:55:54 +0100685#endif
686 }
687
Bram Moolenaar280f1262006-01-30 00:14:18 +0000688 /*
689 * find the string
690 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100691 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100693 // When not accepting a match at the start position set "extra_col" to
694 // a non-zero value. Don't do that when starting at MAXCOL, since
695 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200696 if (pos->col == MAXCOL)
697 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100698 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200699 else if (has_mbyte
700 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
701 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100702 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100703 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100704 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200705 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100706 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100707 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100708 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100709 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200710 start_char_len = 1;
711 if (dir == FORWARD)
712 {
713 if (options & SEARCH_START)
714 extra_col = 0;
715 else
716 extra_col = start_char_len;
717 }
718 else
719 {
720 if (options & SEARCH_START)
721 extra_col = start_char_len;
722 else
723 extra_col = 0;
724 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100725
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100726 start_pos = *pos; // remember start pos for detecting no match
727 found = 0; // default: not found
728 at_first_line = TRUE; // default: start in first line
729 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
731 pos->lnum = 1;
732 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100733 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735
736 /*
737 * Start searching in current line, unless searching backwards and
738 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000739 * If we are searching backwards, in column 0, and not including the
740 * current position, gain some efficiency by skipping back a line.
741 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000743 if (dir == BACKWARD && start_pos.col == 0
744 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 lnum = pos->lnum - 1;
747 at_first_line = FALSE;
748 }
749 else
750 lnum = pos->lnum;
751
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100752 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
755 lnum += dir, at_first_line = FALSE)
756 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100757 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000758 if (stop_lnum != 0 && (dir == FORWARD
759 ? lnum > stop_lnum : lnum < stop_lnum))
760 break;
Paul Ollis65745772022-06-05 16:55:54 +0100761 // Stop after passing the time limit.
762 if (*timed_out)
Bram Moolenaar76929292008-01-06 19:07:36 +0000763 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000766 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100768 col = at_first_line && (options & SEARCH_COL) ? pos->col
769 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 nmatched = vim_regexec_multi(&regmatch, win, buf,
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100771 lnum, col, timed_out);
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200772 // vim_regexec_multi() may clear "regprog"
773 if (regmatch.regprog == NULL)
774 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100775 // Abort searching on an error (e.g., out of stack).
Paul Ollis65745772022-06-05 16:55:54 +0100776 if (called_emsg > called_emsg_before || *timed_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 break;
778 if (nmatched > 0)
779 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100780 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000781 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000783#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000785#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100786 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000787 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
788 ptr = (char_u *)"";
789 else
790 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 /*
793 * Forward search in the first line: match should be after
794 * the start position. If not, continue at the end of the
795 * match (this is vi compatible) or on the next char.
796 */
797 if (dir == FORWARD && at_first_line)
798 {
799 match_ok = TRUE;
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000802 * When the match starts in a next line it's certainly
803 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 * When match lands on a NUL the cursor will be put
805 * one back afterwards, compare with that position,
806 * otherwise "/$" will get stuck on end of line.
807 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000808 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100809 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000810 ? (nmatched == 1
811 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000813 : ((int)matchpos.col
814 - (ptr[matchpos.col] == NUL)
815 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
817 /*
818 * If vi-compatible searching, continue at the end
819 * of the match, otherwise continue one position
820 * forward.
821 */
822 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
823 {
824 if (nmatched > 1)
825 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100826 // end is in next line, thus no match in
827 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 match_ok = FALSE;
829 break;
830 }
831 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100832 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000833 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 && ptr[matchcol] != NUL)
835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (has_mbyte)
837 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000838 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 ++matchcol;
841 }
842 }
843 else
844 {
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000845 // Advance "matchcol" to the next character.
Bram Moolenaar837ca8f2022-11-26 18:59:19 +0000846 // This uses rmm_matchcol, the actual start of
847 // the match, ignoring "\zs".
848 matchcol = regmatch.rmm_matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (ptr[matchcol] != NUL)
850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000852 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 + matchcol);
854 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 ++matchcol;
856 }
857 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200858 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (ptr[matchcol] == NUL
861 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000862 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100863 matchcol, timed_out)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {
865 match_ok = FALSE;
866 break;
867 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200868 // vim_regexec_multi() may clear "regprog"
869 if (regmatch.regprog == NULL)
870 break;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000871 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 endpos = regmatch.endpos[0];
873# ifdef FEAT_EVAL
874 submatch = first_submatch(&regmatch);
875# endif
876
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100877 // Need to get the line pointer again, a
878 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000879 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 if (!match_ok)
882 continue;
883 }
884 if (dir == BACKWARD)
885 {
886 /*
887 * Now, if there are multiple matches on this line,
888 * we have to get the last one. Or the last one before
889 * the cursor, if we're on that line.
890 * When putting the new cursor at the end, compare
891 * relative to the end of the match.
892 */
893 match_ok = FALSE;
894 for (;;)
895 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100896 // Remember a position that is before the start
897 // position, we use it if it's the last match in
898 // the line. Always accept a position after
899 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000900 if (loop
901 || ((options & SEARCH_END)
902 ? (lnum + regmatch.endpos[0].lnum
903 < start_pos.lnum
904 || (lnum + regmatch.endpos[0].lnum
905 == start_pos.lnum
906 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200907 < (int)start_pos.col
908 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000909 : (lnum + regmatch.startpos[0].lnum
910 < start_pos.lnum
911 || (lnum + regmatch.startpos[0].lnum
912 == start_pos.lnum
913 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200914 < (int)start_pos.col
915 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000918 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 endpos = regmatch.endpos[0];
920# ifdef FEAT_EVAL
921 submatch = first_submatch(&regmatch);
922# endif
923 }
924 else
925 break;
926
927 /*
928 * We found a valid match, now check if there is
929 * another one after it.
930 * If vi-compatible searching, continue at the end
931 * of the match, otherwise continue one position
932 * forward.
933 */
934 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
935 {
936 if (nmatched > 1)
937 break;
938 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100939 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000940 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 && ptr[matchcol] != NUL)
942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (has_mbyte)
944 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000945 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 ++matchcol;
948 }
949 }
950 else
951 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100952 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000953 if (matchpos.lnum > 0)
954 break;
955 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (ptr[matchcol] != NUL)
957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (has_mbyte)
959 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000960 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++matchcol;
963 }
964 }
965 if (ptr[matchcol] == NUL
966 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000967 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100968 matchcol, timed_out)) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100969 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100970 // If the search timed out, we did find a match
971 // but it might be the wrong one, so that's not
972 // OK.
Paul Ollis65745772022-06-05 16:55:54 +0100973 if (*timed_out)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100974 match_ok = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100976 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200977 // vim_regexec_multi() may clear "regprog"
978 if (regmatch.regprog == NULL)
979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100981 // Need to get the line pointer again, a
982 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000983 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
985
986 /*
987 * If there is only a match after the cursor, skip
988 * this match.
989 */
990 if (!match_ok)
991 continue;
992 }
993
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100994 // With the SEARCH_END option move to the last character
995 // of the match. Don't do it for an empty match, end
996 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200997 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000998 && !(matchpos.lnum == endpos.lnum
999 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001001 // For a match in the first column, set the position
1002 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001003 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001004 pos->col = endpos.col;
1005 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001006 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001007 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001008 {
1009 --pos->lnum;
1010 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1011 pos->lnum, FALSE));
1012 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001013 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001014 else
1015 {
1016 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001017 if (has_mbyte
1018 && pos->lnum <= buf->b_ml.ml_line_count)
1019 {
1020 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1021 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1022 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001023 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001024 if (end_pos != NULL)
1025 {
1026 end_pos->lnum = lnum + matchpos.lnum;
1027 end_pos->col = matchpos.col;
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
1031 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001032 pos->lnum = lnum + matchpos.lnum;
1033 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001034 if (end_pos != NULL)
1035 {
1036 end_pos->lnum = lnum + endpos.lnum;
1037 end_pos->col = endpos.col;
1038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001041 if (end_pos != NULL)
1042 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001044 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001046 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001047 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 search_match_endcol = endpos.col;
1049 break;
1050 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001051 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (got_int)
1053 break;
1054
1055#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001056 // Cancel searching if a character was typed. Used for
1057 // 'incsearch'. Don't check too often, that would slowdown
1058 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if ((options & SEARCH_PEEK)
1060 && ((lnum - pos->lnum) & 0x3f) == 0
1061 && char_avail())
1062 {
1063 break_loop = TRUE;
1064 break;
1065 }
1066#endif
1067
1068 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001069 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
1071 at_first_line = FALSE;
1072
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001073 // vim_regexec_multi() may clear "regprog"
1074 if (regmatch.regprog == NULL)
1075 break;
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001078 * Stop the search if wrapscan isn't set, "stop_lnum" is
1079 * specified, after an interrupt, after a match and after looping
1080 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001082 if (!p_ws || stop_lnum != 0 || got_int
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001083 || called_emsg > called_emsg_before || *timed_out
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001084#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001085 || break_loop
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001086#endif
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001087 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 break;
1089
1090 /*
1091 * If 'wrapscan' is set we continue at the other end of the file.
1092 * If 'shortmess' does not contain 's', we give a message.
1093 * This message is also remembered in keep_msg for when the screen
1094 * is redrawn. The keep_msg is cleared whenever another message is
1095 * written.
1096 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001097 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001101 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1102 give_warning((char_u *)_(dir == BACKWARD
1103 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001104 if (extra_arg != NULL)
1105 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
Paul Ollis65745772022-06-05 16:55:54 +01001107 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001108#ifdef FEAT_SEARCH_EXTRA
1109 || break_loop
1110#endif
1111 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 break;
1113 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001114 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001116#ifdef FEAT_RELTIME
1117 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1118 disable_regexp_timeout();
1119#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001120 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001122 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001125 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1127 {
1128 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001129 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001131 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001133 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 return FAIL;
1136 }
1137
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001138 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001139 if (pos->lnum > buf->b_ml.ml_line_count)
1140 {
1141 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001142 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001143 if (pos->col > 0)
1144 --pos->col;
1145 }
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 return submatch + 1;
1148}
1149
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001150#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001151 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001152set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001153{
1154 spats[0].off.dir = cdir;
1155}
1156
1157 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001158set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001159{
1160 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1161}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163/*
1164 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001165 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 */
1167 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001168first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 int submatch;
1171
1172 for (submatch = 1; ; ++submatch)
1173 {
1174 if (rp->startpos[submatch].lnum >= 0)
1175 break;
1176 if (submatch == 9)
1177 {
1178 submatch = 0;
1179 break;
1180 }
1181 }
1182 return submatch;
1183}
1184#endif
1185
1186/*
1187 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001188 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 * If 'dirc' is 0: use previous dir.
1190 * If 'pat' is NULL or empty : use previous string.
1191 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1192 * If 'options & SEARCH_ECHO': echo the search command and handle options
1193 * If 'options & SEARCH_MSG' : may give error message
1194 * If 'options & SEARCH_OPT' : interpret optional flags
1195 * If 'options & SEARCH_HIS' : put search pattern in history
1196 * If 'options & SEARCH_NOOF': don't add offset to position
1197 * If 'options & SEARCH_MARK': set previous context mark
1198 * If 'options & SEARCH_KEEP': keep previous search pattern
1199 * If 'options & SEARCH_START': accept match at curpos itself
1200 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1201 *
1202 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1203 * makes the movement linewise without moving the match position.
1204 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001205 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
1207 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001208do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001209 oparg_T *oap, // can be NULL
1210 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001211 int search_delim, // the delimiter for the search, e.g. '%' in
1212 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001213 char_u *pat,
1214 long count,
1215 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001216 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001218 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001220 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001221 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 char_u *p;
1223 long c;
1224 char_u *dircp;
1225 char_u *strcopy = NULL;
1226 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001227 char_u *msgbuf = NULL;
1228 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001229 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231 /*
1232 * A line offset is not remembered, this is vi compatible.
1233 */
1234 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1235 {
1236 spats[0].off.line = FALSE;
1237 spats[0].off.off = 0;
1238 }
1239
1240 /*
1241 * Save the values for when (options & SEARCH_KEEP) is used.
1242 * (there is no "if ()" around this because gcc wants them initialized)
1243 */
1244 old_off = spats[0].off;
1245
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001246 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 /*
1249 * Find out the direction of the search.
1250 */
1251 if (dirc == 0)
1252 dirc = spats[0].off.dir;
1253 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001256#if defined(FEAT_EVAL)
1257 set_vv_searchforward();
1258#endif
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (options & SEARCH_REV)
1261 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001262#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001263 // There is a bug in the Visual C++ 2.2 compiler which means that
1264 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 dirc = (dirc == '/') ? '?' : '/';
1266#else
1267 if (dirc == '/')
1268 dirc = '?';
1269 else
1270 dirc = '/';
1271#endif
1272 }
1273
1274#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001275 // If the cursor is in a closed fold, don't find another match in the same
1276 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 if (dirc == '/')
1278 {
1279 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001280 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 else
1283 {
1284 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1285 pos.col = 0;
1286 }
1287#endif
1288
1289#ifdef FEAT_SEARCH_EXTRA
1290 /*
1291 * Turn 'hlsearch' highlighting back on.
1292 */
1293 if (no_hlsearch && !(options & SEARCH_KEEP))
1294 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001295 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001296 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298#endif
1299
1300 /*
1301 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1302 */
1303 for (;;)
1304 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001305 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001306
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 searchstr = pat;
1308 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001309 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001310 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001312 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001314 searchstr = spats[RE_SUBST].pat;
1315 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001316 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001317 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001318 retval = 0;
1319 goto end_do_search;
1320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001322 else
1323 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001324 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001325 searchstr = (char_u *)"";
1326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001329 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
1331 /*
1332 * Find end of regular expression.
1333 * If there is a matching '/' or '?', toss it.
1334 */
1335 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001336 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001337 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (strcopy != ps)
1339 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001340 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001341 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 pat = strcopy;
1343 searchstr = strcopy;
1344 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001345 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001347 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 *p++ = NUL;
1349 }
1350 spats[0].off.line = FALSE;
1351 spats[0].off.end = FALSE;
1352 spats[0].off.off = 0;
1353 /*
1354 * Check for a line offset or a character offset.
1355 * For get_address (echo off) we don't check for a character
1356 * offset, because it is meaningless and the 's' could be a
1357 * substitute command.
1358 */
1359 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1360 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001361 else if ((options & SEARCH_OPT)
1362 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001364 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 spats[0].off.end = SEARCH_END;
1366 ++p;
1367 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001368 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001370 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1372 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001373 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001375 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 spats[0].off.off = 1;
1377 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001378 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 ++p;
1380 }
1381
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001382 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 searchcmdlen += (int)(p - pat);
1384
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001385 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001388 if ((options & SEARCH_ECHO) && messaging()
1389 && !msg_silent
1390 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001393 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001394 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001396 // Compute msg_row early.
1397 msg_start();
1398
Bram Moolenaar984f0312019-05-24 13:11:47 +02001399 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001400 if (!cmd_silent &&
1401 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001402 {
1403 p = off_buf;
1404 *p++ = dirc;
1405 if (spats[0].off.end)
1406 *p++ = 'e';
1407 else if (!spats[0].off.line)
1408 *p++ = 's';
1409 if (spats[0].off.off > 0 || spats[0].off.line)
1410 *p++ = '+';
1411 *p = NUL;
1412 if (spats[0].off.off != 0 || spats[0].off.line)
1413 sprintf((char *)p, "%ld", spats[0].off.off);
1414 off_len = STRLEN(off_buf);
1415 }
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001418 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 else
1420 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001421
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001422 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001423 {
1424 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001425 // search stat. Use all the space available, so that the
1426 // search state is right aligned. If there is not enough space
1427 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001428 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001429 // Use all the columns.
1430 len = (int)(Rows - msg_row) * Columns - 1;
1431 else
1432 // Use up to 'showcmd' column.
1433 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001434 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1435 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001436 }
1437 else
1438 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001439 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001440
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001441 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001442 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (msgbuf != NULL)
1444 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001445 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001446 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001447 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1448 // empty for the search_stat feature.
1449 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001450 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001451 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001453 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1454 {
1455 // Use a space to draw the composing char on.
1456 msgbuf[1] = ' ';
1457 mch_memmove(msgbuf + 2, p, STRLEN(p));
1458 }
1459 else
1460 mch_memmove(msgbuf + 1, p, STRLEN(p));
1461 if (off_len > 0)
1462 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001464 trunc = msg_strtrunc(msgbuf, TRUE);
1465 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001467 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001468 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001471#ifdef FEAT_RIGHTLEFT
1472 // The search pattern could be shown on the right in
1473 // rightleft mode, but the 'ruler' and 'showcmd' area use
1474 // it too, thus it would be blanked out again very soon.
1475 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001476 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1477 {
1478 char_u *r;
1479 size_t pat_len;
1480
1481 r = reverse_text(msgbuf);
1482 if (r != NULL)
1483 {
1484 vim_free(msgbuf);
1485 msgbuf = r;
1486 // move reversed text to beginning of buffer
1487 while (*r != NUL && *r == ' ')
1488 r++;
1489 pat_len = msgbuf + STRLEN(msgbuf) - r;
1490 mch_memmove(msgbuf, r, pat_len);
1491 // overwrite old text
1492 if ((size_t)(r - msgbuf) >= pat_len)
1493 vim_memset(r, ' ', pat_len);
1494 else
1495 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1496 }
1497 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001498#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001499 msg_outtrans(msgbuf);
1500 msg_clr_eos();
1501 msg_check();
1502
1503 gotocmdline(FALSE);
1504 out_flush();
1505 msg_nowait = TRUE; // don't wait for this message
1506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508 }
1509
1510 /*
1511 * If there is a character offset, subtract it from the current
1512 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001513 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * This is not done for a line offset, because then we would not be vi
1515 * compatible.
1516 */
Bram Moolenaared203462004-06-16 11:19:22 +00001517 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 if (spats[0].off.off > 0)
1520 {
1521 for (c = spats[0].off.off; c; --c)
1522 if (decl(&pos) == -1)
1523 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001524 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001526 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 pos.col = MAXCOL;
1528 }
1529 }
1530 else
1531 {
1532 for (c = spats[0].off.off; c; ++c)
1533 if (incl(&pos) == -1)
1534 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001535 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1538 pos.col = 0;
1539 }
1540 }
1541 }
1542
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001543 /*
1544 * The actual search.
1545 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001546 c = searchit(curwin, curbuf, &pos, NULL,
1547 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 searchstr, count, spats[0].off.end + (options &
1549 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1550 + SEARCH_MSG + SEARCH_START
1551 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001552 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001555 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001556
1557 if (!shortmess(SHM_SEARCH)
1558 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1559 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001560 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (c == FAIL)
1563 {
1564 retval = 0;
1565 goto end_do_search;
1566 }
1567 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001568 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001570 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572 /*
1573 * Add character and/or line offset
1574 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001575 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001577 pos_T org_pos = pos;
1578
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001579 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
1581 c = pos.lnum + spats[0].off.off;
1582 if (c < 1)
1583 pos.lnum = 1;
1584 else if (c > curbuf->b_ml.ml_line_count)
1585 pos.lnum = curbuf->b_ml.ml_line_count;
1586 else
1587 pos.lnum = c;
1588 pos.col = 0;
1589
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001590 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001592 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001594 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 c = spats[0].off.off;
1596 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001598 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (incl(&pos) == -1)
1600 break;
1601 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001602 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 else
1604 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605 while (c++ < 0)
1606 if (decl(&pos) == -1)
1607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001610 if (!EQUAL_POS(pos, org_pos))
1611 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001614 // Show [1/15] if 'S' is not in 'shortmess'.
1615 if ((options & SEARCH_ECHO)
1616 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001617 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001618 && c != FAIL
1619 && !shortmess(SHM_SEARCHCOUNT)
1620 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001621 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1622 show_top_bot_msg, msgbuf,
1623 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001624#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001625 || (!(fdo_flags & FDO_SEARCH)
1626 && hasFolding(curwin->w_cursor.lnum,
1627 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001628#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001629 ),
1630 SEARCH_STAT_DEF_MAX_COUNT,
1631 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 /*
1634 * The search command can be followed by a ';' to do another search.
1635 * For example: "/pat/;/foo/+3;?bar"
1636 * This is like doing another search command, except:
1637 * - The remembered direction '/' or '?' is from the first search.
1638 * - When an error happens the cursor isn't moved at all.
1639 * Don't do this when called by get_address() (it handles ';' itself).
1640 */
1641 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1642 break;
1643
1644 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001645 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (dirc != '?' && dirc != '/')
1647 {
1648 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001649 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 goto end_do_search;
1651 }
1652 ++pat;
1653 }
1654
1655 if (options & SEARCH_MARK)
1656 setpcmark();
1657 curwin->w_cursor = pos;
1658 curwin->w_set_curswant = TRUE;
1659
1660end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001661 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 spats[0].off = old_off;
1663 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001664 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 return retval;
1667}
1668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669/*
1670 * search_for_exact_line(buf, pos, dir, pat)
1671 *
1672 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001673 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001675 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 * Return OK for success, or FAIL if no line found.
1677 */
1678 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001679search_for_exact_line(
1680 buf_T *buf,
1681 pos_T *pos,
1682 int dir,
1683 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 linenr_T start = 0;
1686 char_u *ptr;
1687 char_u *p;
1688
1689 if (buf->b_ml.ml_line_count == 0)
1690 return FAIL;
1691 for (;;)
1692 {
1693 pos->lnum += dir;
1694 if (pos->lnum < 1)
1695 {
1696 if (p_ws)
1697 {
1698 pos->lnum = buf->b_ml.ml_line_count;
1699 if (!shortmess(SHM_SEARCH))
1700 give_warning((char_u *)_(top_bot_msg), TRUE);
1701 }
1702 else
1703 {
1704 pos->lnum = 1;
1705 break;
1706 }
1707 }
1708 else if (pos->lnum > buf->b_ml.ml_line_count)
1709 {
1710 if (p_ws)
1711 {
1712 pos->lnum = 1;
1713 if (!shortmess(SHM_SEARCH))
1714 give_warning((char_u *)_(bot_top_msg), TRUE);
1715 }
1716 else
1717 {
1718 pos->lnum = 1;
1719 break;
1720 }
1721 }
1722 if (pos->lnum == start)
1723 break;
1724 if (start == 0)
1725 start = pos->lnum;
1726 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1727 p = skipwhite(ptr);
1728 pos->col = (colnr_T) (p - ptr);
1729
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001730 // when adding lines the matching line may be empty but it is not
1731 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001732 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
1734 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1735 return OK;
1736 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001737 else if (*p != NUL) // ignore empty lines
1738 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001739 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1740 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 return OK;
1742 }
1743 }
1744 return FAIL;
1745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
1747/*
1748 * Character Searches
1749 */
1750
1751/*
1752 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1753 * position of the character, otherwise move to just before the char.
1754 * Do this "cap->count1" times.
1755 * Return FAIL or OK.
1756 */
1757 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001758searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001760 int c = cap->nchar; // char to search for
1761 int dir = cap->arg; // TRUE for searching forward
1762 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 int col;
1764 char_u *p;
1765 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001766 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001768 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001770 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001772 *lastc = c;
1773 set_csearch_direction(dir);
1774 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001775 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (cap->ncharC1 != 0)
1777 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001778 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1779 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001781 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1782 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001786 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001788 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001790 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 dir = -lastcdir;
1792 else
1793 dir = lastcdir;
1794 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001795 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001796 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001797
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001798 // Force a move of at least one char, so ";" and "," will move the
1799 // cursor, even if the cursor is right in front of char we are looking
1800 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001801 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001802 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001805 if (dir == BACKWARD)
1806 cap->oap->inclusive = FALSE;
1807 else
1808 cap->oap->inclusive = TRUE;
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 p = ml_get_curline();
1811 col = curwin->w_cursor.col;
1812 len = (int)STRLEN(p);
1813
1814 while (count--)
1815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (has_mbyte)
1817 {
1818 for (;;)
1819 {
1820 if (dir > 0)
1821 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001822 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (col >= len)
1824 return FAIL;
1825 }
1826 else
1827 {
1828 if (col == 0)
1829 return FAIL;
1830 col -= (*mb_head_off)(p, p + col - 1) + 1;
1831 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001832 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001834 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 break;
1836 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001837 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001838 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001839 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001840 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 }
1843 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 for (;;)
1846 {
1847 if ((col += dir) < 0 || col >= len)
1848 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001849 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001851 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853 }
1854 }
1855
1856 if (t_cmd)
1857 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001858 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (has_mbyte)
1861 {
1862 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001863 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001864 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001866 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 col -= (*mb_head_off)(p, p + col);
1868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 curwin->w_cursor.col = col;
1871
1872 return OK;
1873}
1874
1875/*
1876 * "Other" Searches
1877 */
1878
1879/*
1880 * findmatch - find the matching paren or brace
1881 *
1882 * Improvement over vi: Braces inside quotes are ignored.
1883 */
1884 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001885findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887 return findmatchlimit(oap, initc, 0, 0);
1888}
1889
1890/*
1891 * Return TRUE if the character before "linep[col]" equals "ch".
1892 * Return FALSE if "col" is zero.
1893 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1894 * is NULL.
1895 * Handles multibyte string correctly.
1896 */
1897 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001898check_prevcol(
1899 char_u *linep,
1900 int col,
1901 int ch,
1902 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (col > 0 && has_mbyte)
1906 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (prevcol)
1908 *prevcol = col;
1909 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1910}
1911
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001912/*
1913 * Raw string start is found at linep[startpos.col - 1].
1914 * Return TRUE if the matching end can be found between startpos and endpos.
1915 */
1916 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001917find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001918{
1919 char_u *p;
1920 char_u *delim_copy;
1921 size_t delim_len;
1922 linenr_T lnum;
1923 int found = FALSE;
1924
1925 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1926 ;
1927 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001928 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001929 if (delim_copy == NULL)
1930 return FALSE;
1931 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1932 {
1933 char_u *line = ml_get(lnum);
1934
1935 for (p = line + (lnum == startpos->lnum
1936 ? startpos->col + 1 : 0); *p; ++p)
1937 {
1938 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1939 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02001940 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
1941 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001942 {
1943 found = TRUE;
1944 break;
1945 }
1946 }
1947 if (found)
1948 break;
1949 }
1950 vim_free(delim_copy);
1951 return found;
1952}
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001955 * Check matchpairs option for "*initc".
1956 * If there is a match set "*initc" to the matching character and "*findc" to
1957 * the opposite character. Set "*backwards" to the direction.
1958 * When "switchit" is TRUE swap the direction.
1959 */
1960 static void
1961find_mps_values(
1962 int *initc,
1963 int *findc,
1964 int *backwards,
1965 int switchit)
1966{
1967 char_u *ptr;
1968
1969 ptr = curbuf->b_p_mps;
1970 while (*ptr != NUL)
1971 {
1972 if (has_mbyte)
1973 {
1974 char_u *prev;
1975
1976 if (mb_ptr2char(ptr) == *initc)
1977 {
1978 if (switchit)
1979 {
1980 *findc = *initc;
1981 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1982 *backwards = TRUE;
1983 }
1984 else
1985 {
1986 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1987 *backwards = FALSE;
1988 }
1989 return;
1990 }
1991 prev = ptr;
1992 ptr += mb_ptr2len(ptr) + 1;
1993 if (mb_ptr2char(ptr) == *initc)
1994 {
1995 if (switchit)
1996 {
1997 *findc = *initc;
1998 *initc = mb_ptr2char(prev);
1999 *backwards = FALSE;
2000 }
2001 else
2002 {
2003 *findc = mb_ptr2char(prev);
2004 *backwards = TRUE;
2005 }
2006 return;
2007 }
2008 ptr += mb_ptr2len(ptr);
2009 }
2010 else
2011 {
2012 if (*ptr == *initc)
2013 {
2014 if (switchit)
2015 {
2016 *backwards = TRUE;
2017 *findc = *initc;
2018 *initc = ptr[2];
2019 }
2020 else
2021 {
2022 *backwards = FALSE;
2023 *findc = ptr[2];
2024 }
2025 return;
2026 }
2027 ptr += 2;
2028 if (*ptr == *initc)
2029 {
2030 if (switchit)
2031 {
2032 *backwards = FALSE;
2033 *findc = *initc;
2034 *initc = ptr[-2];
2035 }
2036 else
2037 {
2038 *backwards = TRUE;
2039 *findc = ptr[-2];
2040 }
2041 return;
2042 }
2043 ++ptr;
2044 }
2045 if (*ptr == ',')
2046 ++ptr;
2047 }
2048}
2049
2050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002052 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2053 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 *
2055 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002056 * character at or after the cursor. Special values:
2057 * '*' look for C-style comment / *
2058 * '/' look for C-style comment / *, ignoring comment-end
2059 * '#' look for preprocessor directives
2060 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 *
2062 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2063 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2064 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2065 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002066 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002067 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002068 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002071findmatchlimit(
2072 oparg_T *oap,
2073 int initc,
2074 int flags,
2075 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002077 static pos_T pos; // current search position
2078 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002080 int count = 0; // cumulative number of braces
2081 int backwards = FALSE; // init for gcc
2082 int raw_string = FALSE; // search for raw string
2083 int inquote = FALSE; // TRUE when inside quotes
2084 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002086 int do_quotes; // check for quotes in current line
2087 int at_start; // do_quotes value at start position
2088 int hash_dir = 0; // Direction searched for # things
2089 int comment_dir = 0; // Direction searched for comments
2090 pos_T match_pos; // Where last slash-star was found
2091 int start_in_quotes; // start position is in quotes
2092 int traveled = 0; // how far we've searched so far
2093 int ignore_cend = FALSE; // ignore comment end
2094 int cpo_match; // vi compatible matching
2095 int cpo_bsl; // don't recognize backslashes
2096 int match_escaped = 0; // search for escaped match
2097 int dir; // Direction to search
2098 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002099 int lispcomm = FALSE; // inside of Lisp-style comment
2100 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002103 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 linep = ml_get(pos.lnum);
2105
2106 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2107 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2108
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002109 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (flags & FM_BACKWARD)
2111 dir = BACKWARD;
2112 else if (flags & FM_FORWARD)
2113 dir = FORWARD;
2114 else
2115 dir = 0;
2116
2117 /*
2118 * if initc given, look in the table for the matching character
2119 * '/' and '*' are special cases: look for start or end of comment.
2120 * When '/' is used, we ignore running backwards into an star-slash, for
2121 * "[*" command, we just want to find any comment.
2122 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002123 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
2125 comment_dir = dir;
2126 if (initc == '/')
2127 ignore_cend = TRUE;
2128 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002129 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 initc = NUL;
2131 }
2132 else if (initc != '#' && initc != NUL)
2133 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002134 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002135 if (dir)
2136 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002137 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 return NULL;
2139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 else
2141 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002142 /*
2143 * Either initc is '#', or no initc was given and we need to look
2144 * under the cursor.
2145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (initc == '#')
2147 {
2148 hash_dir = dir;
2149 }
2150 else
2151 {
2152 /*
2153 * initc was not given, must look for something to match under
2154 * or near the cursor.
2155 * Only check for special things when 'cpo' doesn't have '%'.
2156 */
2157 if (!cpo_match)
2158 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002159 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 ptr = skipwhite(linep);
2161 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2162 {
2163 ptr = skipwhite(ptr + 1);
2164 if ( STRNCMP(ptr, "if", 2) == 0
2165 || STRNCMP(ptr, "endif", 5) == 0
2166 || STRNCMP(ptr, "el", 2) == 0)
2167 hash_dir = 1;
2168 }
2169
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002170 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 else if (linep[pos.col] == '/')
2172 {
2173 if (linep[pos.col + 1] == '*')
2174 {
2175 comment_dir = FORWARD;
2176 backwards = FALSE;
2177 pos.col++;
2178 }
2179 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2180 {
2181 comment_dir = BACKWARD;
2182 backwards = TRUE;
2183 pos.col--;
2184 }
2185 }
2186 else if (linep[pos.col] == '*')
2187 {
2188 if (linep[pos.col + 1] == '/')
2189 {
2190 comment_dir = BACKWARD;
2191 backwards = TRUE;
2192 }
2193 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2194 {
2195 comment_dir = FORWARD;
2196 backwards = FALSE;
2197 }
2198 }
2199 }
2200
2201 /*
2202 * If we are not on a comment or the # at the start of a line, then
2203 * look for brace anywhere on this line after the cursor.
2204 */
2205 if (!hash_dir && !comment_dir)
2206 {
2207 /*
2208 * Find the brace under or after the cursor.
2209 * If beyond the end of the line, use the last character in
2210 * the line.
2211 */
2212 if (linep[pos.col] == NUL && pos.col)
2213 --pos.col;
2214 for (;;)
2215 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002216 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (initc == NUL)
2218 break;
2219
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002220 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (findc)
2222 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002223 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 if (!findc)
2226 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002227 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (!cpo_match && *skipwhite(linep) == '#')
2229 hash_dir = 1;
2230 else
2231 return NULL;
2232 }
2233 else if (!cpo_bsl)
2234 {
2235 int col, bslcnt = 0;
2236
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002237 // Set "match_escaped" if there are an odd number of
2238 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2240 bslcnt++;
2241 match_escaped = (bslcnt & 1);
2242 }
2243 }
2244 }
2245 if (hash_dir)
2246 {
2247 /*
2248 * Look for matching #if, #else, #elif, or #endif
2249 */
2250 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002251 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (initc != '#')
2253 {
2254 ptr = skipwhite(skipwhite(linep) + 1);
2255 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2256 hash_dir = 1;
2257 else if (STRNCMP(ptr, "endif", 5) == 0)
2258 hash_dir = -1;
2259 else
2260 return NULL;
2261 }
2262 pos.col = 0;
2263 while (!got_int)
2264 {
2265 if (hash_dir > 0)
2266 {
2267 if (pos.lnum == curbuf->b_ml.ml_line_count)
2268 break;
2269 }
2270 else if (pos.lnum == 1)
2271 break;
2272 pos.lnum += hash_dir;
2273 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002274 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ptr = skipwhite(linep);
2276 if (*ptr != '#')
2277 continue;
2278 pos.col = (colnr_T) (ptr - linep);
2279 ptr = skipwhite(ptr + 1);
2280 if (hash_dir > 0)
2281 {
2282 if (STRNCMP(ptr, "if", 2) == 0)
2283 count++;
2284 else if (STRNCMP(ptr, "el", 2) == 0)
2285 {
2286 if (count == 0)
2287 return &pos;
2288 }
2289 else if (STRNCMP(ptr, "endif", 5) == 0)
2290 {
2291 if (count == 0)
2292 return &pos;
2293 count--;
2294 }
2295 }
2296 else
2297 {
2298 if (STRNCMP(ptr, "if", 2) == 0)
2299 {
2300 if (count == 0)
2301 return &pos;
2302 count--;
2303 }
2304 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2305 {
2306 if (count == 0)
2307 return &pos;
2308 }
2309 else if (STRNCMP(ptr, "endif", 5) == 0)
2310 count++;
2311 }
2312 }
2313 return NULL;
2314 }
2315 }
2316
2317#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002318 // This is just guessing: when 'rightleft' is set, search for a matching
2319 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2321 backwards = !backwards;
2322#endif
2323
2324 do_quotes = -1;
2325 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002326 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002327
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002328 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002329 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002331 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002332 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 while (!got_int)
2335 {
2336 /*
2337 * Go to the next position, forward or backward. We could use
2338 * inc() and dec() here, but that is much slower
2339 */
2340 if (backwards)
2341 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002342 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002343 if (lispcomm && pos.col < (colnr_T)comment_col)
2344 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002345 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002347 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 break;
2349 --pos.lnum;
2350
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002351 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 break;
2353
2354 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002355 pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 do_quotes = -1;
2357 line_breakcheck();
2358
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002359 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002360 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002362 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002363 if (lisp && comment_col != MAXCOL)
2364 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366 else
2367 {
2368 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 if (has_mbyte)
2370 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002373 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002375 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002377 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002378 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002379 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002381 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002382 // line is exhausted and comment with it,
2383 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002384 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 break;
2386 ++pos.lnum;
2387
2388 if (maxtravel && traveled++ > maxtravel)
2389 break;
2390
2391 linep = ml_get(pos.lnum);
2392 pos.col = 0;
2393 do_quotes = -1;
2394 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002395 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002396 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398 else
2399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002401 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 ++pos.col;
2404 }
2405 }
2406
2407 /*
2408 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2409 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002410 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2411 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002413 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002415 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417
2418 if (comment_dir)
2419 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002420 // Note: comments do not nest, and we ignore quotes in them
2421 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (comment_dir == FORWARD)
2423 {
2424 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2425 {
2426 pos.col++;
2427 return &pos;
2428 }
2429 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002430 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 /*
2433 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002434 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 if (pos.col == 0)
2437 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002438 else if (raw_string)
2439 {
2440 if (linep[pos.col - 1] == 'R'
2441 && linep[pos.col] == '"'
2442 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2443 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002444 // Possible start of raw string. Now that we have the
2445 // delimiter we can check if it ends before where we
2446 // started searching, or before the previously found
2447 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002448 if (!find_rawstring_end(linep, &pos,
2449 count > 0 ? &match_pos : &curwin->w_cursor))
2450 {
2451 count++;
2452 match_pos = pos;
2453 match_pos.col--;
2454 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002455 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002456 }
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 else if ( linep[pos.col - 1] == '/'
2459 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002460 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 && (int)pos.col < comment_col)
2462 {
2463 count++;
2464 match_pos = pos;
2465 match_pos.col--;
2466 }
2467 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2468 {
2469 if (count > 0)
2470 pos = match_pos;
2471 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2472 && (int)pos.col <= comment_col)
2473 pos.col -= 2;
2474 else if (ignore_cend)
2475 continue;
2476 else
2477 return NULL;
2478 return &pos;
2479 }
2480 }
2481 continue;
2482 }
2483
2484 /*
2485 * If smart matching ('cpoptions' does not contain '%'), braces inside
2486 * of quotes are ignored, but only if there is an even number of
2487 * quotes in the line.
2488 */
2489 if (cpo_match)
2490 do_quotes = 0;
2491 else if (do_quotes == -1)
2492 {
2493 /*
2494 * Count the number of quotes in the line, skipping \" and '"'.
2495 * Watch out for "\\".
2496 */
2497 at_start = do_quotes;
2498 for (ptr = linep; *ptr; ++ptr)
2499 {
2500 if (ptr == linep + pos.col + backwards)
2501 at_start = (do_quotes & 1);
2502 if (*ptr == '"'
2503 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2504 ++do_quotes;
2505 if (*ptr == '\\' && ptr[1] != NUL)
2506 ++ptr;
2507 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002508 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 /*
2511 * If we find an uneven count, check current line and previous
2512 * one for a '\' at the end.
2513 */
2514 if (!do_quotes)
2515 {
2516 inquote = FALSE;
2517 if (ptr[-1] == '\\')
2518 {
2519 do_quotes = 1;
2520 if (start_in_quotes == MAYBE)
2521 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002522 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 inquote = TRUE;
2524 start_in_quotes = TRUE;
2525 }
2526 else if (backwards)
2527 inquote = TRUE;
2528 }
2529 if (pos.lnum > 1)
2530 {
2531 ptr = ml_get(pos.lnum - 1);
2532 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2533 {
2534 do_quotes = 1;
2535 if (start_in_quotes == MAYBE)
2536 {
2537 inquote = at_start;
2538 if (inquote)
2539 start_in_quotes = TRUE;
2540 }
2541 else if (!backwards)
2542 inquote = TRUE;
2543 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002544
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002545 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002546 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548 }
2549 }
2550 if (start_in_quotes == MAYBE)
2551 start_in_quotes = FALSE;
2552
2553 /*
2554 * If 'smartmatch' is set:
2555 * Things inside quotes are ignored by setting 'inquote'. If we
2556 * find a quote without a preceding '\' invert 'inquote'. At the
2557 * end of a line not ending in '\' we reset 'inquote'.
2558 *
2559 * In lines with an uneven number of quotes (without preceding '\')
2560 * we do not know which part to ignore. Therefore we only set
2561 * inquote if the number of quotes in a line is even, unless this
2562 * line or the previous one ends in a '\'. Complicated, isn't it?
2563 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002564 c = PTR2CHAR(linep + pos.col);
2565 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
2567 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002568 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2570 {
2571 inquote = FALSE;
2572 start_in_quotes = FALSE;
2573 }
2574 break;
2575
2576 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002577 // a quote that is preceded with an odd number of backslashes is
2578 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (do_quotes)
2580 {
2581 int col;
2582
2583 for (col = pos.col - 1; col >= 0; --col)
2584 if (linep[col] != '\\')
2585 break;
2586 if ((((int)pos.col - 1 - col) & 1) == 0)
2587 {
2588 inquote = !inquote;
2589 start_in_quotes = FALSE;
2590 }
2591 }
2592 break;
2593
2594 /*
2595 * If smart matching ('cpoptions' does not contain '%'):
2596 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2597 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2598 * skipped, there is never a brace in them.
2599 * Ignore this when finding matches for `'.
2600 */
2601 case '\'':
2602 if (!cpo_match && initc != '\'' && findc != '\'')
2603 {
2604 if (backwards)
2605 {
2606 if (pos.col > 1)
2607 {
2608 if (linep[pos.col - 2] == '\'')
2609 {
2610 pos.col -= 2;
2611 break;
2612 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002613 else if (linep[pos.col - 2] == '\\'
2614 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 pos.col -= 3;
2617 break;
2618 }
2619 }
2620 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002621 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002623 if (linep[pos.col + 1] == '\\'
2624 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
2626 pos.col += 3;
2627 break;
2628 }
2629 else if (linep[pos.col + 2] == '\'')
2630 {
2631 pos.col += 2;
2632 break;
2633 }
2634 }
2635 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002636 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002639 /*
2640 * For Lisp skip over backslashed (), {} and [].
2641 * (actually, we skip #\( et al)
2642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (curbuf->b_p_lisp
2644 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002645 && pos.col > 1
2646 && check_prevcol(linep, pos.col, '\\', NULL)
2647 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002650 // Check for match outside of quotes, and inside of
2651 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if ((!inquote || start_in_quotes == TRUE)
2653 && (c == initc || c == findc))
2654 {
2655 int col, bslcnt = 0;
2656
2657 if (!cpo_bsl)
2658 {
2659 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2660 bslcnt++;
2661 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002662 // Only accept a match when 'M' is in 'cpo' or when escaping
2663 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2665 {
2666 if (c == initc)
2667 count++;
2668 else
2669 {
2670 if (count == 0)
2671 return &pos;
2672 count--;
2673 }
2674 }
2675 }
2676 }
2677 }
2678
2679 if (comment_dir == BACKWARD && count > 0)
2680 {
2681 pos = match_pos;
2682 return &pos;
2683 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002684 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685}
2686
2687/*
2688 * Check if line[] contains a / / comment.
2689 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002691 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 char_u *p;
2695
2696 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002697 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002698 if (curbuf->b_p_lisp)
2699 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002700 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002701 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002702 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002704 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002705 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002706 {
2707 if (*p == '"')
2708 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002709 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002710 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002711 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002712 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002713 }
2714 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002715 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002716 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002717 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002718 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002719 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002720 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2721 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002722 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002723 ++p;
2724 }
2725 }
2726 else
2727 p = NULL;
2728 }
2729 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002730 while ((p = vim_strchr(p, '/')) != NULL)
2731 {
2732 // Accept a double /, unless it's preceded with * and followed by
2733 // *, because * / / * is an end and start of a C comment. Only
2734 // accept the position if it is not inside a string.
2735 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002736 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002737 break;
2738 ++p;
2739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 if (p == NULL)
2742 return MAXCOL;
2743 return (int)(p - line);
2744}
2745
2746/*
2747 * Move cursor briefly to character matching the one under the cursor.
2748 * Used for Insert mode and "r" command.
2749 * Show the match only if it is visible on the screen.
2750 * If there isn't a match, then beep.
2751 */
2752 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002753showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002754 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 pos_T *lpos, save_cursor;
2757 pos_T mpos;
2758 colnr_T vcol;
2759 long save_so;
2760 long save_siso;
2761#ifdef CURSOR_SHAPE
2762 int save_state;
2763#endif
2764 colnr_T save_dollar_vcol;
2765 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002766 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2767 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 /*
2770 * Only show match for chars in the 'matchpairs' option.
2771 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002772 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002773 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002776 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002777 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002778#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002779 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002780 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781#ifdef FEAT_RIGHTLEFT
2782 && !(curwin->w_p_rl ^ p_ri)
2783#endif
2784 )
2785 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002786 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002787 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return;
2789 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002790 if (*p == NUL)
2791 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002793 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar165bc692015-07-21 17:53:25 +02002794 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002795 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
2797 if (!curwin->w_p_wrap)
2798 getvcol(curwin, lpos, NULL, &vcol, NULL);
2799 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002800 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002802 mpos = *lpos; // save the pos, update_screen() may change it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002804 save_so = *so;
2805 save_siso = *siso;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002806 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2807 // stop displaying the "$".
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002808 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2809 dollar_vcol = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002810 ++curwin->w_virtcol; // do display ')' just before "$"
Bram Moolenaara4d158b2022-08-14 14:17:45 +01002811 update_screen(UPD_VALID); // show the new char first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812
2813 save_dollar_vcol = dollar_vcol;
2814#ifdef CURSOR_SHAPE
2815 save_state = State;
Bram Moolenaar24959102022-05-07 20:01:16 +01002816 State = MODE_SHOWMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002817 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002819 curwin->w_cursor = mpos; // move to matching char
2820 *so = 0; // don't use 'scrolloff' here
2821 *siso = 0; // don't use 'sidescrolloff' here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 showruler(FALSE);
2823 setcursor();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002824 cursor_on(); // make sure that the cursor is shown
Bram Moolenaara338adc2018-01-31 20:51:47 +01002825 out_flush_cursor(TRUE, FALSE);
2826
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002827 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2828 // which resets it if the matching position is in a previous line
2829 // and has a higher column number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 dollar_vcol = save_dollar_vcol;
2831
2832 /*
2833 * brief pause, unless 'm' is present in 'cpo' and a character is
2834 * available.
2835 */
2836 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002837 ui_delay(p_mat * 100L + 8, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 else if (!char_avail())
Bram Moolenaareda1da02019-11-17 17:06:33 +01002839 ui_delay(p_mat * 100L + 9, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002840 curwin->w_cursor = save_cursor; // restore cursor position
Bram Moolenaar375e3392019-01-31 18:26:10 +01002841 *so = save_so;
2842 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#ifdef CURSOR_SHAPE
2844 State = save_state;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002845 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846#endif
2847 }
2848 }
2849}
2850
2851/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002852 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002853 * If move is TRUE, check from the beginning of the buffer, else from position
2854 * "cur".
2855 * "direction" is FORWARD or BACKWARD.
2856 * Returns TRUE, FALSE or -1 for failure.
2857 */
2858 static int
2859is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2860{
2861 regmmatch_T regmatch;
2862 int nmatched = 0;
2863 int result = -1;
2864 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002865 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002866 int flag = 0;
2867
2868 if (pattern == NULL)
2869 pattern = spats[last_idx].pat;
2870
Rob Pillinge86190e2022-12-23 19:06:04 +00002871 if (search_regcomp(pattern, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002872 SEARCH_KEEP, &regmatch) == FAIL)
2873 return -1;
2874
2875 // init startcol correctly
2876 regmatch.startpos[0].col = -1;
2877 // move to match
2878 if (move)
2879 {
2880 CLEAR_POS(&pos);
2881 }
2882 else
2883 {
2884 pos = *cur;
2885 // accept a match at the cursor position
2886 flag = SEARCH_START;
2887 }
2888
2889 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2890 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2891 {
2892 // Zero-width pattern should match somewhere, then we can check if
2893 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002894 do
2895 {
2896 regmatch.startpos[0].col++;
2897 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002898 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002899 if (nmatched != 0)
2900 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002901 } while (regmatch.regprog != NULL
2902 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002903 : regmatch.startpos[0].col > pos.col);
2904
Bram Moolenaar53989552019-12-23 22:59:18 +01002905 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002906 {
2907 result = (nmatched != 0
2908 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2909 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2910 }
2911 }
2912
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002913 vim_regfree(regmatch.regprog);
2914 return result;
2915}
2916
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002917
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002918/*
2919 * Find next search match under cursor, cursor at end.
2920 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002921 */
2922 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002923current_search(
2924 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002925 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002926{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002927 pos_T start_pos; // start position of the pattern match
2928 pos_T end_pos; // end position of the pattern match
2929 pos_T orig_pos; // position of the cursor at beginning
2930 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002931 int i;
2932 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002933 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002934 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002935 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002936 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002937 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002938 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002939
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002940 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002941 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002942 dec_cursor();
2943
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002944 // When searching forward and the cursor is at the start of the Visual
2945 // area, skip the first search backward, otherwise it doesn't move.
2946 skip_first_backward = forward && VIsual_active
2947 && LT_POS(curwin->w_cursor, VIsual);
2948
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002949 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002950 if (VIsual_active)
2951 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002952 if (forward)
2953 incl(&pos);
2954 else
2955 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002956 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002957
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002958 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002959 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02002960 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002961 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002962 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002963
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002964 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002965 * The trick is to first search backwards and then search forward again,
2966 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002967 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002968 */
2969 for (i = 0; i < 2; i++)
2970 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002971 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002972 {
2973 if (i == 0 && skip_first_backward)
2974 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002975 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002976 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002977 else
2978 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002979
2980 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002981 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002982 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002983 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002984
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002985 // wrapping should not occur in the first round
2986 if (i == 0)
2987 p_ws = FALSE;
2988
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002989 result = searchit(curwin, curbuf, &pos, &end_pos,
2990 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002991 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002992 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002993
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002994 p_ws = old_p_ws;
2995
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002996 // First search may fail, but then start searching from the
2997 // beginning of the file (cursor might be on the search match)
2998 // except when Visual mode is active, so that extending the visual
2999 // selection works.
3000 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003001 {
3002 curwin->w_cursor = orig_pos;
3003 if (VIsual_active)
3004 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003005 return FAIL;
3006 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003007 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003008 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003009 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003010 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003011 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003012 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003013 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003014 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003015 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003016 // try again from end of buffer
3017 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003018 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003019 pos.col = (colnr_T)STRLEN(
3020 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003021 }
3022 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003023 }
3024
3025 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003026
3027 if (!VIsual_active)
3028 VIsual = start_pos;
3029
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003030 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003031 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003032 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003033 {
3034 if (skip_first_backward)
3035 // put the cursor on the start of the match
3036 curwin->w_cursor = pos;
3037 else
3038 // put the cursor on last character of match
3039 dec_cursor();
3040 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003041 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003042 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003043 VIsual_active = TRUE;
3044 VIsual_mode = 'v';
3045
Bram Moolenaarb7633612019-02-10 21:48:25 +01003046 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003047 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003048 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003049 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3050 inc_cursor();
3051 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3052 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003053 }
3054
3055#ifdef FEAT_FOLDING
3056 if (fdo_flags & FDO_SEARCH && KeyTyped)
3057 foldOpenCursor();
3058#endif
3059
3060 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003061 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003062#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003063 // Make sure the clipboard gets updated. Needed because start and
3064 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003065 clip_star.vmode = NUL;
3066#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003067 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003068 showmode();
3069
3070 return OK;
3071}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003072
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073/*
3074 * return TRUE if line 'lnum' is empty or has white chars only.
3075 */
3076 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003077linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 char_u *p;
3080
3081 p = skipwhite(ml_get(lnum));
3082 return (*p == NUL);
3083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003085/*
3086 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003087 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003088 */
3089 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003090cmdline_search_stat(
3091 int dirc,
3092 pos_T *pos,
3093 pos_T *cursor_pos,
3094 int show_top_bot_msg,
3095 char_u *msgbuf,
3096 int recompute,
3097 int maxcount,
3098 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003099{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003100 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003101
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003102 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3103 timeout);
3104 if (stat.cur > 0)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003105 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003106 char t[SEARCH_STAT_BUF_LEN];
Bram Moolenaare2ad8262019-05-24 13:22:22 +02003107 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003108
3109#ifdef FEAT_RIGHTLEFT
3110 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3111 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003112 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003113 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003114 else if (stat.cnt > maxcount && stat.cur > maxcount)
3115 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3116 maxcount, maxcount);
3117 else if (stat.cnt > maxcount)
3118 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3119 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003120 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003121 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3122 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003123 }
3124 else
3125#endif
3126 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003127 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003128 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003129 else if (stat.cnt > maxcount && stat.cur > maxcount)
3130 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3131 maxcount, maxcount);
3132 else if (stat.cnt > maxcount)
3133 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3134 stat.cur, maxcount);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003135 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003136 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3137 stat.cur, stat.cnt);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003138 }
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003139
3140 len = STRLEN(t);
Bram Moolenaardc6855a2019-05-18 19:26:29 +02003141 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003142 {
Bram Moolenaar16b58ae2019-09-06 20:40:21 +02003143 mch_memmove(t + 2, t, len);
3144 t[0] = 'W';
3145 t[1] = ' ';
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003146 len += 2;
3147 }
3148
3149 mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003150 if (dirc == '?' && stat.cur == maxcount + 1)
3151 stat.cur = -1;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003152
Bram Moolenaar984f0312019-05-24 13:11:47 +02003153 // keep the message even after redraw, but don't put in history
3154 msg_hist_off = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003155 give_warning(msgbuf, FALSE);
Bram Moolenaar984f0312019-05-24 13:11:47 +02003156 msg_hist_off = FALSE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003157 }
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003158}
3159
3160/*
3161 * Add the search count information to "stat".
3162 * "stat" must not be NULL.
3163 * When "recompute" is TRUE always recompute the numbers.
3164 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3165 * dirc == '/': find the next match
3166 * dirc == '?': find the previous match
3167 */
3168 static void
3169update_search_stat(
3170 int dirc,
3171 pos_T *pos,
3172 pos_T *cursor_pos,
3173 searchstat_T *stat,
3174 int recompute,
3175 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003176 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003177{
3178 int save_ws = p_ws;
3179 int wraparound = FALSE;
3180 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003181 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003182 static int cur = 0;
3183 static int cnt = 0;
3184 static int exact_match = FALSE;
3185 static int incomplete = 0;
3186 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3187 static int chgtick = 0;
3188 static char_u *lastpat = NULL;
3189 static buf_T *lbuf = NULL;
3190#ifdef FEAT_RELTIME
3191 proftime_T start;
3192#endif
3193
3194 vim_memset(stat, 0, sizeof(searchstat_T));
3195
3196 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3197 {
3198 stat->cur = cur;
3199 stat->cnt = cnt;
3200 stat->exact_match = exact_match;
3201 stat->incomplete = incomplete;
3202 stat->last_maxcount = last_maxcount;
3203 return;
3204 }
3205 last_maxcount = maxcount;
3206
3207 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3208 || (dirc == '/' && LT_POS(p, lastpos)));
3209
3210 // If anything relevant changed the count has to be recomputed.
3211 // MB_STRNICMP ignores case, but we should not ignore case.
3212 // Unfortunately, there is no MB_STRNICMP function.
3213 // XXX: above comment should be "no MB_STRCMP function" ?
3214 if (!(chgtick == CHANGEDTICK(curbuf)
3215 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3216 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3217 && EQUAL_POS(lastpos, *cursor_pos)
3218 && lbuf == curbuf) || wraparound || cur < 0
3219 || (maxcount > 0 && cur > maxcount) || recompute)
3220 {
3221 cur = 0;
3222 cnt = 0;
3223 exact_match = FALSE;
3224 incomplete = 0;
3225 CLEAR_POS(&lastpos);
3226 lbuf = curbuf;
3227 }
3228
3229 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
3230 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 0))
3231 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3232 else
3233 {
3234 int done_search = FALSE;
3235 pos_T endpos = {0, 0, 0};
3236
3237 p_ws = FALSE;
3238#ifdef FEAT_RELTIME
3239 if (timeout > 0)
3240 profile_setlimit(timeout, &start);
3241#endif
3242 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3243 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3244 {
3245 done_search = TRUE;
3246#ifdef FEAT_RELTIME
3247 // Stop after passing the time limit.
3248 if (timeout > 0 && profile_passed_limit(&start))
3249 {
3250 incomplete = 1;
3251 break;
3252 }
3253#endif
3254 cnt++;
3255 if (LTOREQ_POS(lastpos, p))
3256 {
3257 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003258 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003259 exact_match = TRUE;
3260 }
3261 fast_breakcheck();
3262 if (maxcount > 0 && cnt > maxcount)
3263 {
3264 incomplete = 2; // max count exceeded
3265 break;
3266 }
3267 }
3268 if (got_int)
3269 cur = -1; // abort
3270 if (done_search)
3271 {
3272 vim_free(lastpat);
3273 lastpat = vim_strsave(spats[last_idx].pat);
3274 chgtick = CHANGEDTICK(curbuf);
3275 lbuf = curbuf;
3276 lastpos = p;
3277 }
3278 }
3279 stat->cur = cur;
3280 stat->cnt = cnt;
3281 stat->exact_match = exact_match;
3282 stat->incomplete = incomplete;
3283 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003284 p_ws = save_ws;
3285}
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003288
3289/*
3290 * Get line "lnum" and copy it into "buf[LSIZE]".
3291 * The copy is made because the regexp may make the line invalid when using a
3292 * mark.
3293 */
3294 static char_u *
3295get_line_and_copy(linenr_T lnum, char_u *buf)
3296{
3297 char_u *line = ml_get(lnum);
3298
3299 vim_strncpy(buf, line, LSIZE - 1);
3300 return buf;
3301}
3302
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303/*
3304 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003305 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003308find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003309 char_u *ptr, // pointer to search pattern
3310 int dir UNUSED, // direction of expansion
3311 int len, // length of search pattern
3312 int whole, // match whole words only
3313 int skip_comments, // don't match inside comments
3314 int type, // Type of search; are we looking for a type?
3315 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003316 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003317 int action, // What to do when we find it
3318 linenr_T start_lnum, // first line to start searching
3319 linenr_T end_lnum) // last line for searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003321 SearchedFile *files; // Stack of included files
3322 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 int max_path_depth = 50;
3324 long match_count = 1;
3325
3326 char_u *pat;
3327 char_u *new_fname;
3328 char_u *curr_fname = curbuf->b_fname;
3329 char_u *prev_fname = NULL;
3330 linenr_T lnum;
3331 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003332 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 int old_files;
3334 int already_searched;
3335 char_u *file_line;
3336 char_u *line;
3337 char_u *p;
3338 char_u save_char;
3339 int define_matched;
3340 regmatch_T regmatch;
3341 regmatch_T incl_regmatch;
3342 regmatch_T def_regmatch;
3343 int matched = FALSE;
3344 int did_show = FALSE;
3345 int found = FALSE;
3346 int i;
3347 char_u *already = NULL;
3348 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003349 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003350#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 win_T *curwin_save = NULL;
3352#endif
3353
3354 regmatch.regprog = NULL;
3355 incl_regmatch.regprog = NULL;
3356 def_regmatch.regprog = NULL;
3357
3358 file_line = alloc(LSIZE);
3359 if (file_line == NULL)
3360 return;
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003363 // when CONT_SOL is set compare "ptr" with the beginning of the
3364 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3365 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 pat = alloc(len + 5);
3368 if (pat == NULL)
3369 goto fpip_end;
3370 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003371 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003373 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 vim_free(pat);
3375 if (regmatch.regprog == NULL)
3376 goto fpip_end;
3377 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003378 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3379 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003381 incl_regmatch.regprog = vim_regcomp(inc_opt,
3382 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 if (incl_regmatch.regprog == NULL)
3384 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003385 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3388 {
3389 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003390 ? p_def : curbuf->b_p_def,
3391 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 if (def_regmatch.regprog == NULL)
3393 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003394 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003396 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 if (files == NULL)
3398 goto fpip_end;
3399 old_files = max_path_depth;
3400 depth = depth_displayed = -1;
3401
3402 lnum = start_lnum;
3403 if (end_lnum > curbuf->b_ml.ml_line_count)
3404 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003405 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003407 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
3409 for (;;)
3410 {
3411 if (incl_regmatch.regprog != NULL
3412 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3413 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003414 char_u *p_fname = (curr_fname == curbuf->b_fname)
3415 ? curbuf->b_ffname : curr_fname;
3416
3417 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003418 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003419 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003420 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003421 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3422 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003423 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003424 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003425 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 already_searched = FALSE;
3427 if (new_fname != NULL)
3428 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003429 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 for (i = 0;; i++)
3431 {
3432 if (i == depth + 1)
3433 i = old_files;
3434 if (i == max_path_depth)
3435 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003436 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3437 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003439 if (type != CHECK_PATH
3440 && action == ACTION_SHOW_ALL
3441 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003443 msg_putchar('\n'); // cursor below last one
3444 if (!got_int) // don't display if 'q'
3445 // typed at "--more--"
3446 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003449 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 prev_fname = NULL;
3451 }
3452 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003453 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 already_searched = TRUE;
3455 break;
3456 }
3457 }
3458 }
3459
3460 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3461 || (new_fname == NULL && !already_searched)))
3462 {
3463 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003464 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 else
3466 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003467 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003468 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003470 msg_puts_title(_("not found "));
3471 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 did_show = TRUE;
3474 while (depth_displayed < depth && !got_int)
3475 {
3476 ++depth_displayed;
3477 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003478 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003480 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003482 if (!got_int) // don't display if 'q' typed
3483 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003486 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 if (new_fname != NULL)
3488 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003489 // using "new_fname" is more reliable, e.g., when
3490 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003491 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493 else
3494 {
3495 /*
3496 * Isolate the file name.
3497 * Include the surrounding "" or <> if present.
3498 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003499 if (inc_opt != NULL
3500 && strstr((char *)inc_opt, "\\zs") != NULL)
3501 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003502 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003503 p = incl_regmatch.startp[0];
3504 i = (int)(incl_regmatch.endp[0]
3505 - incl_regmatch.startp[0]);
3506 }
3507 else
3508 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003509 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003510 for (p = incl_regmatch.endp[0];
3511 *p && !vim_isfilec(*p); p++)
3512 ;
3513 for (i = 0; vim_isfilec(p[i]); i++)
3514 ;
3515 }
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (i == 0)
3518 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003519 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003521 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003523 // Avoid checking before the start of the line, can
3524 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003525 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 if (p[-1] == '"' || p[-1] == '<')
3528 {
3529 --p;
3530 ++i;
3531 }
3532 if (p[i] == '"' || p[i] == '>')
3533 ++i;
3534 }
3535 save_char = p[i];
3536 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003537 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 p[i] = save_char;
3539 }
3540
3541 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3542 {
3543 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003544 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003546 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 }
3548 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003549 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551
3552 if (new_fname != NULL)
3553 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003554 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (depth + 1 == old_files)
3556 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003557 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (bigger != NULL)
3559 {
3560 for (i = 0; i <= depth; i++)
3561 bigger[i] = files[i];
3562 for (i = depth + 1; i < old_files + max_path_depth; i++)
3563 {
3564 bigger[i].fp = NULL;
3565 bigger[i].name = NULL;
3566 bigger[i].lnum = 0;
3567 bigger[i].matched = FALSE;
3568 }
3569 for (i = old_files; i < max_path_depth; i++)
3570 bigger[i + max_path_depth] = files[i];
3571 old_files += max_path_depth;
3572 max_path_depth *= 2;
3573 vim_free(files);
3574 files = bigger;
3575 }
3576 }
3577 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3578 == NULL)
3579 vim_free(new_fname);
3580 else
3581 {
3582 if (++depth == old_files)
3583 {
3584 /*
3585 * lalloc() for 'bigger' must have failed above. We
3586 * will forget one of our already visited files now.
3587 */
3588 vim_free(files[old_files].name);
3589 ++old_files;
3590 }
3591 files[depth].name = curr_fname = new_fname;
3592 files[depth].lnum = 0;
3593 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (action == ACTION_EXPAND)
3595 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003596 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003597 vim_snprintf((char*)IObuff, IOSIZE,
3598 _("Scanning included file: %s"),
3599 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003600 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003602 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003603 {
3604 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003605 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003606 (char *)new_fname);
3607 verbose_leave();
3608 }
3609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611 }
3612 }
3613 else
3614 {
3615 /*
3616 * Check if the line is a define (type == FIND_DEFINE)
3617 */
3618 p = line;
3619search_line:
3620 define_matched = FALSE;
3621 if (def_regmatch.regprog != NULL
3622 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3623 {
3624 /*
3625 * Pattern must be first identifier after 'define', so skip
3626 * to that position before checking for match of pattern. Also
3627 * don't let it match beyond the end of this identifier.
3628 */
3629 p = def_regmatch.endp[0];
3630 while (*p && !vim_iswordc(*p))
3631 p++;
3632 define_matched = TRUE;
3633 }
3634
3635 /*
3636 * Look for a match. Don't do this if we are looking for a
3637 * define and this line didn't match define_prog above.
3638 */
3639 if (def_regmatch.regprog == NULL || define_matched)
3640 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003641 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003643 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 startp = skipwhite(p);
3645 if (p_ic)
3646 matched = !MB_STRNICMP(startp, ptr, len);
3647 else
3648 matched = !STRNCMP(startp, ptr, len);
3649 if (matched && define_matched && whole
3650 && vim_iswordc(startp[len]))
3651 matched = FALSE;
3652 }
3653 else if (regmatch.regprog != NULL
3654 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3655 {
3656 matched = TRUE;
3657 startp = regmatch.startp[0];
3658 /*
3659 * Check if the line is not a comment line (unless we are
3660 * looking for a define). A line starting with "# define"
3661 * is not considered to be a comment line.
3662 */
3663 if (!define_matched && skip_comments)
3664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 if ((*line != '#' ||
3666 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003667 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 matched = FALSE;
3669
3670 /*
3671 * Also check for a "/ *" or "/ /" before the match.
3672 * Skips lines like "int backwards; / * normal index
3673 * * /" when looking for "normal".
3674 * Note: Doesn't skip "/ *" in comments.
3675 */
3676 p = skipwhite(line);
3677 if (matched
3678 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 for (p = line; *p && p < startp; ++p)
3680 {
3681 if (matched
3682 && p[0] == '/'
3683 && (p[1] == '*' || p[1] == '/'))
3684 {
3685 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003686 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (p[1] == '/')
3688 break;
3689 ++p;
3690 }
3691 else if (!matched && p[0] == '*' && p[1] == '/')
3692 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003693 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 matched = TRUE;
3695 ++p;
3696 }
3697 }
3698 }
3699 }
3700 }
3701 }
3702 if (matched)
3703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (action == ACTION_EXPAND)
3705 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003706 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 int add_r;
3708 char_u *aux;
3709
3710 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3711 break;
3712 found = TRUE;
3713 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003714 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003716 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (vim_iswordp(p))
3718 goto exit_matched;
3719 p = find_word_start(p);
3720 }
3721 p = find_word_end(p);
3722 i = (int)(p - aux);
3723
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003724 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003726 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003728
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003729 // Get the next line: when "depth" < 0 from the current
3730 // buffer, otherwise from the included file. Jump to
3731 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003732 if (depth < 0)
3733 {
3734 if (lnum >= end_lnum)
3735 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003736 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003737 }
3738 else if (vim_fgets(line = file_line,
3739 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 goto exit_matched;
3741
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003742 // we read a line, set "already" to check this "line" later
3743 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003744 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 already = aux = p = skipwhite(line);
3746 p = find_word_start(p);
3747 p = find_word_end(p);
3748 if (p > aux)
3749 {
3750 if (*aux != ')' && IObuff[i-1] != TAB)
3751 {
3752 if (IObuff[i-1] != ' ')
3753 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003754 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 if (p_js
3756 && (IObuff[i-2] == '.'
3757 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3758 && (IObuff[i-2] == '?'
3759 || IObuff[i-2] == '!'))))
3760 IObuff[i++] = ' ';
3761 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003762 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 if (p - aux >= IOSIZE - i)
3764 p = aux + IOSIZE - i - 1;
3765 STRNCPY(IObuff + i, aux, p - aux);
3766 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003767 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 IObuff[i] = NUL;
3770 aux = IObuff;
3771
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003772 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 goto exit_matched;
3774 }
3775
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003776 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003778 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003780 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003782 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 break;
3784 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003785 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
3787 found = TRUE;
3788 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003789 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (curr_fname != prev_fname)
3791 {
3792 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003793 msg_putchar('\n'); // cursor below last one
3794 if (!got_int) // don't display if 'q' typed
3795 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 msg_home_replace_hl(curr_fname);
3797 prev_fname = curr_fname;
3798 }
3799 did_show = TRUE;
3800 if (!got_int)
3801 show_pat_in_path(line, type, TRUE, action,
3802 (depth == -1) ? NULL : files[depth].fp,
3803 (depth == -1) ? &lnum : &files[depth].lnum,
3804 match_count++);
3805
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003806 // Set matched flag for this file and all the ones that
3807 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 for (i = 0; i <= depth; ++i)
3809 files[i].matched = TRUE;
3810 }
3811 else if (--count <= 0)
3812 {
3813 found = TRUE;
3814 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003815#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 && g_do_tagpreview == 0
3817#endif
3818 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003819 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 else if (action == ACTION_SHOW)
3821 {
3822 show_pat_in_path(line, type, did_show, action,
3823 (depth == -1) ? NULL : files[depth].fp,
3824 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3825 did_show = TRUE;
3826 }
3827 else
3828 {
3829#ifdef FEAT_GUI
3830 need_mouse_correct = TRUE;
3831#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003832#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003833 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (g_do_tagpreview != 0)
3835 {
3836 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003837 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839#endif
3840 if (action == ACTION_SPLIT)
3841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003844 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 if (depth == -1)
3847 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003848 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003849#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (g_do_tagpreview != 0)
3851 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003852 if (!win_valid(curwin_save))
3853 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003854 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003855 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003856 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003857 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859 else
3860#endif
3861 setpcmark();
3862 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003863 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
3865 else
3866 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003867 if (!GETFILE_SUCCESS(getfile(
3868 0, files[depth].name, NULL, TRUE,
3869 files[depth].lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003870 break; // failed to jump to file
3871 // autocommands may have changed the lnum, we don't
3872 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 curwin->w_cursor.lnum = files[depth].lnum;
3874 }
3875 }
3876 if (action != ACTION_SHOW)
3877 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003878 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 curwin->w_set_curswant = TRUE;
3880 }
3881
Bram Moolenaar4033c552017-09-16 20:54:51 +02003882#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003884 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003886 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003888 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 win_enter(curwin_save, TRUE);
3890 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003891# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003892 else if (WIN_IS_POPUP(curwin))
3893 // can't keep focus in popup window
3894 win_enter(firstwin, TRUE);
3895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896#endif
3897 break;
3898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003901 // look for other matches in the rest of the line if we
3902 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003905 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003906 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003907 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 goto search_line;
3909 }
3910 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003912 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003913 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915
3916 /*
3917 * Read the next line. When reading an included file and encountering
3918 * end-of-file, close the file and continue in the file that included
3919 * it.
3920 */
3921 while (depth >= 0 && !already
3922 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3923 {
3924 fclose(files[depth].fp);
3925 --old_files;
3926 files[old_files].name = files[depth].name;
3927 files[old_files].matched = files[depth].matched;
3928 --depth;
3929 curr_fname = (depth == -1) ? curbuf->b_fname
3930 : files[depth].name;
3931 if (depth < depth_displayed)
3932 depth_displayed = depth;
3933 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003934 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003935 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003937 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003938 i = (int)STRLEN(line);
3939 if (i > 0 && line[i - 1] == '\n')
3940 line[--i] = NUL;
3941 if (i > 0 && line[i - 1] == '\r')
3942 line[--i] = NUL;
3943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 else if (!already)
3945 {
3946 if (++lnum > end_lnum)
3947 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003948 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 already = NULL;
3951 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003952 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003954 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 for (i = 0; i <= depth; i++)
3956 {
3957 fclose(files[i].fp);
3958 vim_free(files[i].name);
3959 }
3960 for (i = old_files; i < max_path_depth; i++)
3961 vim_free(files[i].name);
3962 vim_free(files);
3963
3964 if (type == CHECK_PATH)
3965 {
3966 if (!did_show)
3967 {
3968 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003969 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003971 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003974 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003976 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003977 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003979 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003981 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
3983 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
3984 msg_end();
3985
3986fpip_end:
3987 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02003988 vim_regfree(regmatch.regprog);
3989 vim_regfree(incl_regmatch.regprog);
3990 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991}
3992
3993 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003994show_pat_in_path(
3995 char_u *line,
3996 int type,
3997 int did_show,
3998 int action,
3999 FILE *fp,
4000 linenr_T *lnum,
4001 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 char_u *p;
4004
4005 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004006 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004007 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004008 gotocmdline(TRUE); // cursor at status line
4009 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return;
4011 for (;;)
4012 {
4013 p = line + STRLEN(line) - 1;
4014 if (fp != NULL)
4015 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004016 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 if (p >= line && *p == '\n')
4018 --p;
4019 if (p >= line && *p == '\r')
4020 --p;
4021 *(p + 1) = NUL;
4022 }
4023 if (action == ACTION_SHOW_ALL)
4024 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004025 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004026 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004027 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4028 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004029 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4030 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004032 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004033 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004035 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4037 break;
4038
4039 if (fp != NULL)
4040 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004041 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 break;
4043 ++*lnum;
4044 }
4045 else
4046 {
4047 if (++*lnum > curbuf->b_ml.ml_line_count)
4048 break;
4049 line = ml_get(*lnum);
4050 }
4051 msg_putchar('\n');
4052 }
4053}
4054#endif
4055
4056#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004057/*
4058 * Return the last used search pattern at "idx".
4059 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004060 spat_T *
4061get_spat(int idx)
4062{
4063 return &spats[idx];
4064}
4065
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004066/*
4067 * Return the last used search pattern index.
4068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004070get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004072 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004075
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004076#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004077/*
4078 * "searchcount()" function
4079 */
4080 void
4081f_searchcount(typval_T *argvars, typval_T *rettv)
4082{
4083 pos_T pos = curwin->w_cursor;
4084 char_u *pattern = NULL;
4085 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4086 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004087 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004088 searchstat_T stat;
4089
4090 if (rettv_dict_alloc(rettv) == FAIL)
4091 return;
4092
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004093 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4094 return;
4095
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004096 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4097 recompute = TRUE;
4098
4099 if (argvars[0].v_type != VAR_UNKNOWN)
4100 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004101 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004102 dictitem_T *di;
4103 listitem_T *li;
4104 int error = FALSE;
4105
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004106 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004107 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004108 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004109 di = dict_find(dict, (char_u *)"timeout", -1);
4110 if (di != NULL)
4111 {
4112 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4113 if (error)
4114 return;
4115 }
4116 di = dict_find(dict, (char_u *)"maxcount", -1);
4117 if (di != NULL)
4118 {
4119 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4120 if (error)
4121 return;
4122 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004123 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004124 di = dict_find(dict, (char_u *)"pattern", -1);
4125 if (di != NULL)
4126 {
4127 pattern = tv_get_string_chk(&di->di_tv);
4128 if (pattern == NULL)
4129 return;
4130 }
4131 di = dict_find(dict, (char_u *)"pos", -1);
4132 if (di != NULL)
4133 {
4134 if (di->di_tv.v_type != VAR_LIST)
4135 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004136 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004137 return;
4138 }
4139 if (list_len(di->di_tv.vval.v_list) != 3)
4140 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004141 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004142 return;
4143 }
4144 li = list_find(di->di_tv.vval.v_list, 0L);
4145 if (li != NULL)
4146 {
4147 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4148 if (error)
4149 return;
4150 }
4151 li = list_find(di->di_tv.vval.v_list, 1L);
4152 if (li != NULL)
4153 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004154 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004155 if (error)
4156 return;
4157 }
4158 li = list_find(di->di_tv.vval.v_list, 2L);
4159 if (li != NULL)
4160 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004161 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004162 if (error)
4163 return;
4164 }
4165 }
4166 }
4167
4168 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004169#ifdef FEAT_SEARCH_EXTRA
4170 save_incsearch_state();
4171#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004172 if (pattern != NULL)
4173 {
4174 if (*pattern == NUL)
4175 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004176 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004177 spats[last_idx].pat = vim_strsave(pattern);
4178 }
4179 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4180 goto the_end; // the previous pattern was never defined
4181
4182 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4183
4184 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4185 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4186 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4187 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4188 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4189
4190the_end:
4191 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004192#ifdef FEAT_SEARCH_EXTRA
4193 restore_incsearch_state();
4194#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004195}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004196#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004197
4198/*
4199 * Fuzzy string matching
4200 *
4201 * Ported from the lib_fts library authored by Forrest Smith.
4202 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4203 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004204 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004205 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4206 *
4207 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004208 * - Matched letter
4209 * - Unmatched letter
4210 * - Consecutively matched letters
4211 * - Proximity to start
4212 * - Letter following a separator (space, underscore)
4213 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004214 *
4215 * Matched letters are good. Unmatched letters are bad. Matching near the start
4216 * is good. Matching the first letter in the middle of a phrase is good.
4217 * Matching the uppercase letters in camel case entries is good.
4218 *
4219 * The score assigned for each factor is explained below.
4220 * File paths are different from file names. File extensions may be ignorable.
4221 * Single words care about consecutive matches but not separators or camel
4222 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004223 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004224 * Matched letter: +0 points
4225 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004226 * Consecutive match bonus: +15 points
4227 * First letter bonus: +15 points
4228 * Separator bonus: +30 points
4229 * Camel case bonus: +30 points
4230 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004231 *
4232 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004233 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004234 * lower minimum score due to unmatched letter penalty. Longer search patterns
4235 * have a higher maximum score due to match bonuses.
4236 *
4237 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4238 * quite a bit.
4239 *
4240 * There is a penalty if you DON’T match the first three letters. Which
4241 * effectively rewards matching near the start. However there’s no difference
4242 * in matching between the middle and end.
4243 *
4244 * There is not an explicit bonus for an exact match. Unmatched letters receive
4245 * a penalty. So shorter strings and closer matches are worth more.
4246 */
4247typedef struct
4248{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004249 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004250 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004251 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004252 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004253} fuzzyItem_T;
4254
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004255// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4256// matching a whole word is preferred.
4257#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004258// bonus if match occurs after a path separator
4259#define PATH_SEPARATOR_BONUS 30
4260// bonus if match occurs after a word separator
4261#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004262// bonus if match is uppercase and prev is lower
4263#define CAMEL_BONUS 30
4264// bonus if the first letter is matched
4265#define FIRST_LETTER_BONUS 15
4266// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004267#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004268// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004269#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004270// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004271#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004272// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004273#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004274// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004275#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004276
4277#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004278
4279/*
4280 * Compute a score for a fuzzy matched string. The matching character locations
4281 * are in 'matches'.
4282 */
4283 static int
4284fuzzy_match_compute_score(
4285 char_u *str,
4286 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004287 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004288 int numMatches)
4289{
4290 int score;
4291 int penalty;
4292 int unmatched;
4293 int i;
4294 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004295 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004296
4297 // Initialize score
4298 score = 100;
4299
4300 // Apply leading letter penalty
4301 penalty = LEADING_LETTER_PENALTY * matches[0];
4302 if (penalty < MAX_LEADING_LETTER_PENALTY)
4303 penalty = MAX_LEADING_LETTER_PENALTY;
4304 score += penalty;
4305
4306 // Apply unmatched penalty
4307 unmatched = strSz - numMatches;
4308 score += UNMATCHED_LETTER_PENALTY * unmatched;
4309
4310 // Apply ordering bonuses
4311 for (i = 0; i < numMatches; ++i)
4312 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004313 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004314
4315 if (i > 0)
4316 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004317 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004318
4319 // Sequential
4320 if (currIdx == (prevIdx + 1))
4321 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004322 else
4323 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004324 }
4325
4326 // Check for bonuses based on neighbor character value
4327 if (currIdx > 0)
4328 {
4329 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004330 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004331 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004332
4333 if (has_mbyte)
4334 {
4335 while (sidx < currIdx)
4336 {
4337 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004338 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004339 sidx++;
4340 }
4341 curr = (*mb_ptr2char)(p);
4342 }
4343 else
4344 {
4345 neighbor = str[currIdx - 1];
4346 curr = str[currIdx];
4347 }
4348
4349 if (vim_islower(neighbor) && vim_isupper(curr))
4350 score += CAMEL_BONUS;
4351
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004352 // Bonus if the match follows a separator character
4353 if (neighbor == '/' || neighbor == '\\')
4354 score += PATH_SEPARATOR_BONUS;
4355 else if (neighbor == ' ' || neighbor == '_')
4356 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004357 }
4358 else
4359 {
4360 // First letter
4361 score += FIRST_LETTER_BONUS;
4362 }
4363 }
4364 return score;
4365}
4366
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004367/*
4368 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4369 * Return the number of matching characters.
4370 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004371 static int
4372fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004373 char_u *fuzpat,
4374 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004375 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004376 int *outScore,
4377 char_u *strBegin,
4378 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004379 int_u *srcMatches,
4380 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004381 int maxMatches,
4382 int nextMatch,
4383 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004384{
4385 // Recursion params
4386 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004387 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004388 int bestRecursiveScore = 0;
4389 int first_match;
4390 int matched;
4391
4392 // Count recursions
4393 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004394 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004395 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004396
4397 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004398 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004399 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004400
4401 // Loop through fuzpat and str looking for a match
4402 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004403 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004404 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004405 int c1;
4406 int c2;
4407
4408 c1 = PTR2CHAR(fuzpat);
4409 c2 = PTR2CHAR(str);
4410
Bram Moolenaar635414d2020-09-11 22:25:15 +02004411 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004412 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004413 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004414 int_u recursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004415 int recursiveScore = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004416 char_u *next_char;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004417
4418 // Supplied matches buffer was too short
4419 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004420 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004421
4422 // "Copy-on-Write" srcMatches into matches
4423 if (first_match && srcMatches)
4424 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004425 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004426 first_match = FALSE;
4427 }
4428
4429 // Recursive call that "skips" this match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004430 if (has_mbyte)
4431 next_char = str + (*mb_ptr2len)(str);
4432 else
4433 next_char = str + 1;
4434 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4435 &recursiveScore, strBegin, strLen, matches,
4436 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004437 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004438 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004439 {
4440 // Pick best recursive score
4441 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4442 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004443 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004444 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004445 bestRecursiveScore = recursiveScore;
4446 }
4447 recursiveMatch = TRUE;
4448 }
4449
4450 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004451 matches[nextMatch++] = strIdx;
4452 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004453 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004454 else
4455 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004456 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004457 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004458 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004459 else
4460 ++str;
4461 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004462 }
4463
4464 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004465 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004466
4467 // Calculate score
4468 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004469 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4470 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004471
4472 // Return best result
4473 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4474 {
4475 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004476 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004477 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004478 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004479 }
4480 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004481 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004482
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004483 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004484}
4485
4486/*
4487 * fuzzy_match()
4488 *
4489 * Performs exhaustive search via recursion to find all possible matches and
4490 * match with highest score.
4491 * Scores values have no intrinsic meaning. Possible score range is not
4492 * normalized and varies with pattern.
4493 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004494 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004495 * Uses char_u for match indices. Therefore patterns are limited to
4496 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004497 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004498 * Returns TRUE if 'pat_arg' matches 'str'. Also returns the match score in
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004499 * 'outScore' and the matching character positions in 'matches'.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004500 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004501 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004502fuzzy_match(
4503 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004504 char_u *pat_arg,
4505 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004506 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004507 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004508 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004509{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004510 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004511 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004512 char_u *save_pat;
4513 char_u *pat;
4514 char_u *p;
4515 int complete = FALSE;
4516 int score = 0;
4517 int numMatches = 0;
4518 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004519
4520 *outScore = 0;
4521
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004522 save_pat = vim_strsave(pat_arg);
4523 if (save_pat == NULL)
4524 return FALSE;
4525 pat = save_pat;
4526 p = pat;
4527
4528 // Try matching each word in 'pat_arg' in 'str'
4529 while (TRUE)
4530 {
4531 if (matchseq)
4532 complete = TRUE;
4533 else
4534 {
4535 // Extract one word from the pattern (separated by space)
4536 p = skipwhite(p);
4537 if (*p == NUL)
4538 break;
4539 pat = p;
4540 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4541 {
4542 if (has_mbyte)
4543 MB_PTR_ADV(p);
4544 else
4545 ++p;
4546 }
4547 if (*p == NUL) // processed all the words
4548 complete = TRUE;
4549 *p = NUL;
4550 }
4551
4552 score = 0;
4553 recursionCount = 0;
4554 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4555 matches + numMatches, maxMatches - numMatches,
4556 0, &recursionCount);
4557 if (matchCount == 0)
4558 {
4559 numMatches = 0;
4560 break;
4561 }
4562
4563 // Accumulate the match score and the number of matches
4564 *outScore += score;
4565 numMatches += matchCount;
4566
4567 if (complete)
4568 break;
4569
4570 // try matching the next word
4571 ++p;
4572 }
4573
4574 vim_free(save_pat);
4575 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004576}
4577
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004578#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004579/*
4580 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004581 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004582 */
4583 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004584fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004585{
4586 int v1 = ((fuzzyItem_T *)s1)->score;
4587 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004588 int idx1 = ((fuzzyItem_T *)s1)->idx;
4589 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004590
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004591 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004592}
4593
4594/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004595 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4596 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004597 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4598 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004599 * If 'items' is a list of strings, then search for 'str' in the list.
4600 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4601 * for each item or use 'item_cb' Funcref function to get the string.
4602 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4603 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004604 */
4605 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004606fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004607 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004608 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004609 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004610 char_u *key,
4611 callback_T *item_cb,
4612 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004613 list_T *fmatchlist,
4614 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004615{
4616 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004617 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004618 listitem_T *li;
4619 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004620 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004621 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004622
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004623 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004624 if (len == 0)
4625 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004626 if (max_matches > 0 && len > max_matches)
4627 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004628
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004629 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4630 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004631 return;
4632
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004633 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004634 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004635 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004636 int score;
4637 char_u *itemstr;
4638 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004639
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004640 if (max_matches > 0 && match_count >= max_matches)
4641 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004642
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004643 itemstr = NULL;
4644 rettv.v_type = VAR_UNKNOWN;
4645 if (li->li_tv.v_type == VAR_STRING) // list of strings
4646 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004647 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004648 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004649 {
4650 // For a dict, either use the specified key to lookup the string or
4651 // use the specified callback function to get the string.
4652 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004653 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4654 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004655 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004656 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004657 typval_T argv[2];
4658
4659 // Invoke the supplied callback (if any) to get the dict item
4660 li->li_tv.vval.v_dict->dv_refcount++;
4661 argv[0].v_type = VAR_DICT;
4662 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4663 argv[1].v_type = VAR_UNKNOWN;
4664 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4665 {
4666 if (rettv.v_type == VAR_STRING)
4667 itemstr = rettv.vval.v_string;
4668 }
4669 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004670 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004671 }
4672
4673 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004674 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004675 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004676 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004677 items[match_count].idx = match_count;
4678 items[match_count].item = li;
4679 items[match_count].score = score;
4680
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004681 // Copy the list of matching positions in itemstr to a list, if
4682 // 'retmatchpos' is set.
4683 if (retmatchpos)
4684 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004685 int j = 0;
4686 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004687
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004688 items[match_count].lmatchpos = list_alloc();
4689 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004690 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004691
4692 p = str;
4693 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004694 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004695 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004696 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004697 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004698 matches[j]) == FAIL)
4699 goto done;
4700 j++;
4701 }
4702 if (has_mbyte)
4703 MB_PTR_ADV(p);
4704 else
4705 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004706 }
4707 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004708 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004709 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004710 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004711 }
4712
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004713 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004714 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004715 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004716
Bram Moolenaar635414d2020-09-11 22:25:15 +02004717 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004718 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004719 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004720
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004721 // For matchfuzzy(), return a list of matched strings.
4722 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004723 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004724 // The first item is a list of matched strings. The second item
4725 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004726 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004727 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4728 if (retmatchpos)
4729 {
4730 li = list_find(fmatchlist, 0);
4731 if (li == NULL || li->li_tv.vval.v_list == NULL)
4732 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004733 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004734 }
4735 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004736 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004737
4738 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004739 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004740 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004741 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004742 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004743 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004744 }
4745
4746 // next copy the list of matching positions
4747 if (retmatchpos)
4748 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004749 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004750 if (li == NULL || li->li_tv.vval.v_list == NULL)
4751 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004752 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004753
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004754 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004755 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004756 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004757 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004758 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004759 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004760 goto done;
4761 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004762
4763 // copy the matching scores
4764 li = list_find(fmatchlist, -1);
4765 if (li == NULL || li->li_tv.vval.v_list == NULL)
4766 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004767 retlist = li->li_tv.vval.v_list;
4768 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004769 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004770 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004771 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004772 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004773 goto done;
4774 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004775 }
4776 }
4777
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004778done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004779 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004780}
4781
4782/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004783 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4784 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4785 */
4786 static void
4787do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4788{
4789 callback_T cb;
4790 char_u *key = NULL;
4791 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004792 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004793 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004794
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004795 if (in_vim9script()
4796 && (check_for_list_arg(argvars, 0) == FAIL
4797 || check_for_string_arg(argvars, 1) == FAIL
4798 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4799 return;
4800
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004801 CLEAR_POINTER(&cb);
4802
4803 // validate and get the arguments
4804 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4805 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004806 semsg(_(e_argument_of_str_must_be_list),
4807 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004808 return;
4809 }
4810 if (argvars[1].v_type != VAR_STRING
4811 || argvars[1].vval.v_string == NULL)
4812 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004813 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004814 return;
4815 }
4816
4817 if (argvars[2].v_type != VAR_UNKNOWN)
4818 {
4819 dict_T *d;
4820 dictitem_T *di;
4821
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004822 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004823 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004824
4825 // To search a dict, either a callback function or a key can be
4826 // specified.
4827 d = argvars[2].vval.v_dict;
4828 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4829 {
4830 if (di->di_tv.v_type != VAR_STRING
4831 || di->di_tv.vval.v_string == NULL
4832 || *di->di_tv.vval.v_string == NUL)
4833 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004834 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004835 return;
4836 }
4837 key = tv_get_string(&di->di_tv);
4838 }
4839 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4840 {
4841 cb = get_callback(&di->di_tv);
4842 if (cb.cb_name == NULL)
4843 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004844 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004845 return;
4846 }
4847 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004848
4849 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004850 {
4851 if (di->di_tv.v_type != VAR_NUMBER)
4852 {
4853 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4854 return;
4855 }
4856 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4857 }
4858
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004859 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004860 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004861 }
4862
4863 // get the fuzzy matches
4864 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004865 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004866 goto done;
4867 if (retmatchpos)
4868 {
4869 list_T *l;
4870
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004871 // For matchfuzzypos(), a list with three items are returned. First
4872 // item is a list of matching strings, the second item is a list of
4873 // lists with matching positions within each string and the third item
4874 // is the list of scores of the matches.
4875 l = list_alloc();
4876 if (l == NULL)
4877 goto done;
4878 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004879 {
4880 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004881 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004882 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004883 l = list_alloc();
4884 if (l == NULL)
4885 goto done;
4886 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004887 {
4888 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004889 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004890 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004891 l = list_alloc();
4892 if (l == NULL)
4893 goto done;
4894 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004895 {
4896 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004897 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004898 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004899 }
4900
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004901 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004902 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004903
4904done:
4905 free_callback(&cb);
4906}
4907
4908/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004909 * "matchfuzzy()" function
4910 */
4911 void
4912f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4913{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004914 do_fuzzymatch(argvars, rettv, FALSE);
4915}
4916
4917/*
4918 * "matchfuzzypos()" function
4919 */
4920 void
4921f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
4922{
4923 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004924}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004925#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004926
4927/*
4928 * Same as fuzzy_match_item_compare() except for use with a string match
4929 */
4930 static int
4931fuzzy_match_str_compare(const void *s1, const void *s2)
4932{
4933 int v1 = ((fuzmatch_str_T *)s1)->score;
4934 int v2 = ((fuzmatch_str_T *)s2)->score;
4935 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4936 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4937
4938 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4939}
4940
4941/*
4942 * Sort fuzzy matches by score
4943 */
4944 static void
4945fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
4946{
4947 // Sort the list by the descending order of the match score
4948 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4949 fuzzy_match_str_compare);
4950}
4951
4952/*
4953 * Same as fuzzy_match_item_compare() except for use with a function name
4954 * string match. <SNR> functions should be sorted to the end.
4955 */
4956 static int
4957fuzzy_match_func_compare(const void *s1, const void *s2)
4958{
4959 int v1 = ((fuzmatch_str_T *)s1)->score;
4960 int v2 = ((fuzmatch_str_T *)s2)->score;
4961 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4962 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4963 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
4964 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
4965
4966 if (*str1 != '<' && *str2 == '<') return -1;
4967 if (*str1 == '<' && *str2 != '<') return 1;
4968 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4969}
4970
4971/*
4972 * Sort fuzzy matches of function names by score.
4973 * <SNR> functions should be sorted to the end.
4974 */
4975 static void
4976fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
4977{
4978 // Sort the list by the descending order of the match score
4979 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4980 fuzzy_match_func_compare);
4981}
4982
4983/*
4984 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
4985 * returns the match score.
4986 */
4987 int
4988fuzzy_match_str(char_u *str, char_u *pat)
4989{
4990 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00004991 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004992
4993 if (str == NULL || pat == NULL)
4994 return 0;
4995
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00004996 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004997 sizeof(matchpos) / sizeof(matchpos[0]));
4998
4999 return score;
5000}
5001
5002/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005003 * Free an array of fuzzy string matches "fuzmatch[count]".
5004 */
5005 void
5006fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5007{
5008 int i;
5009
5010 if (fuzmatch == NULL)
5011 return;
5012 for (i = 0; i < count; ++i)
5013 vim_free(fuzmatch[i].str);
5014 vim_free(fuzmatch);
5015}
5016
5017/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005018 * Copy a list of fuzzy matches into a string list after sorting the matches by
5019 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5020 * Returns OK on success and FAIL on memory allocation failure.
5021 */
5022 int
5023fuzzymatches_to_strmatches(
5024 fuzmatch_str_T *fuzmatch,
5025 char_u ***matches,
5026 int count,
5027 int funcsort)
5028{
5029 int i;
5030
5031 if (count <= 0)
5032 return OK;
5033
5034 *matches = ALLOC_MULT(char_u *, count);
5035 if (*matches == NULL)
5036 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005037 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005038 return FAIL;
5039 }
5040
5041 // Sort the list by the descending order of the match score
5042 if (funcsort)
5043 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5044 else
5045 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5046
5047 for (i = 0; i < count; i++)
5048 (*matches)[i] = fuzmatch[i].str;
5049 vim_free(fuzmatch);
5050
5051 return OK;
5052}