blob: 166ef4a580c9849f8fe02a9d9fab3711c413dba9 [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)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000164 *used_pat = pat;
Rob Pillinge86190e2022-12-23 19:06:04 +0000165
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 Moolenaarcc2b9d52014-12-13 03:17:11 +0100206 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100207save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000209 if (spats[idx].pat == pat)
210 return;
211
212 vim_free(spats[idx].pat);
213 spats[idx].pat = vim_strsave(pat);
214 spats[idx].magic = magic;
215 spats[idx].no_scs = no_smartcase;
216 last_idx = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000218 // If 'hlsearch' set and search pat changed: need redraw.
219 if (p_hls)
220 redraw_all_later(UPD_SOME_VALID);
221 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223}
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225/*
226 * Save the search patterns, so they can be restored later.
227 * Used before/after executing autocommands and user functions.
228 */
229static int save_level = 0;
230
231 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100232save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000234 if (save_level++ != 0)
235 return;
236
237 saved_spats[0] = spats[0];
238 if (spats[0].pat != NULL)
239 saved_spats[0].pat = vim_strsave(spats[0].pat);
240 saved_spats[1] = spats[1];
241 if (spats[1].pat != NULL)
242 saved_spats[1].pat = vim_strsave(spats[1].pat);
243 if (mr_pattern == NULL)
244 saved_mr_pattern = NULL;
245 else
246 saved_mr_pattern = vim_strsave(mr_pattern);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100247#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000248 saved_spats_last_idx = last_idx;
249 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251}
252
253 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100254restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000256 if (--save_level != 0)
257 return;
258
259 vim_free(spats[0].pat);
260 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100261#if defined(FEAT_EVAL)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000262 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100263#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000264 vim_free(spats[1].pat);
265 spats[1] = saved_spats[1];
266 vim_free(mr_pattern);
267 mr_pattern = saved_mr_pattern;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100268#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000269 last_idx = saved_spats_last_idx;
270 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000274#if defined(EXITFREE) || defined(PROTO)
275 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100276free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000277{
278 vim_free(spats[0].pat);
279 vim_free(spats[1].pat);
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100280 VIM_CLEAR(mr_pattern);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000281}
282#endif
283
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100284#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100285// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
286// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200287static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100288static int did_save_last_search_spat = 0;
289static int saved_last_idx = 0;
290static int saved_no_hlsearch = 0;
Christian Brabandt6dd74242022-02-14 12:44:32 +0000291static int saved_search_match_endcol;
292static int saved_search_match_lines;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100293
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100294/*
295 * Save and restore the search pattern for incremental highlight search
296 * feature.
297 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100298 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100299 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100300 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100301 */
302 void
303save_last_search_pattern(void)
304{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200305 if (++did_save_last_search_spat != 1)
306 // nested call, nothing to do
307 return;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100308
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100309 saved_last_search_spat = spats[RE_SEARCH];
310 if (spats[RE_SEARCH].pat != NULL)
311 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
312 saved_last_idx = last_idx;
313 saved_no_hlsearch = no_hlsearch;
314}
315
316 void
317restore_last_search_pattern(void)
318{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200319 if (--did_save_last_search_spat > 0)
320 // nested call, nothing to do
321 return;
322 if (did_save_last_search_spat != 0)
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100323 {
Bram Moolenaar442a8532020-06-04 20:56:09 +0200324 iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100325 return;
326 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100327
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100328 vim_free(spats[RE_SEARCH].pat);
329 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100330 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100331# if defined(FEAT_EVAL)
332 set_vv_searchforward();
333# endif
334 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200335 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100336}
Bram Moolenaard0480092017-11-16 22:20:39 +0100337
Christian Brabandt6dd74242022-02-14 12:44:32 +0000338/*
339 * Save and restore the incsearch highlighting variables.
340 * This is required so that calling searchcount() at does not invalidate the
341 * incsearch highlighting.
342 */
343 static void
344save_incsearch_state(void)
345{
346 saved_search_match_endcol = search_match_endcol;
347 saved_search_match_lines = search_match_lines;
348}
349
350 static void
351restore_incsearch_state(void)
352{
353 search_match_endcol = saved_search_match_endcol;
354 search_match_lines = saved_search_match_lines;
355}
356
Bram Moolenaard0480092017-11-16 22:20:39 +0100357 char_u *
358last_search_pattern(void)
359{
360 return spats[RE_SEARCH].pat;
361}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100362#endif
363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364/*
365 * Return TRUE when case should be ignored for search pattern "pat".
366 * Uses the 'ignorecase' and 'smartcase' options.
367 */
368 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100369ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200371 return ignorecase_opt(pat, p_ic, p_scs);
372}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200374/*
375 * As ignorecase() put pass the "ic" and "scs" flags.
376 */
377 int
378ignorecase_opt(char_u *pat, int ic_in, int scs)
379{
380 int ic = ic_in;
381
382 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200383 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200384 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 no_smartcase = FALSE;
386
387 return ic;
388}
389
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200390/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200391 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200392 */
393 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100394pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200395{
396 char_u *p = pat;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200397 magic_T magic_val = MAGIC_ON;
398
399 // get the magicness of the pattern
400 (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val);
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200401
402 while (*p != NUL)
403 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200404 int l;
405
406 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
407 {
408 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
409 return TRUE;
410 p += l;
411 }
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200412 else if (*p == '\\' && magic_val <= MAGIC_ON)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200413 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100414 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200415 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100416 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200417 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100418 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200419 p += 2;
420 else
421 p += 1;
422 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200423 else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL)
424 {
425 if (p[1] != NUL) // skip "_X" and %X
426 p += 2;
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200427 else
428 p++;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200429 }
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200430 else if (MB_ISUPPER(*p))
431 return TRUE;
432 else
433 ++p;
434 }
435 return FALSE;
436}
437
Bram Moolenaar113e1072019-01-20 15:30:40 +0100438#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100440last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200441{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200442 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200443}
444
445 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100446last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200447{
448 return lastcdir == FORWARD;
449}
450
451 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100452last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200453{
454 return last_t_cmd == TRUE;
455}
456
457 void
zeertzjqe5d91ba2023-05-14 17:39:18 +0100458set_last_csearch(int c, char_u *s, int len)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200459{
460 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200461 lastc_bytelen = len;
462 if (len)
463 memcpy(lastc_bytes, s, len);
464 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200465 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200466}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100467#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200468
469 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100470set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200471{
472 lastcdir = cdir;
473}
474
475 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100476set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200477{
478 last_t_cmd = t_cmd;
479}
480
481 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100482last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483{
484 return spats[last_idx].pat;
485}
486
487/*
488 * Reset search direction to forward. For "gd" and "gD" commands.
489 */
490 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100491reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#if defined(FEAT_EVAL)
495 set_vv_searchforward();
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497}
498
499#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
500/*
501 * Set the last search pattern. For ":let @/ =" and viminfo.
502 * Also set the saved search pattern, so that this works in an autocommand.
503 */
504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100505set_last_search_pat(
506 char_u *s,
507 int idx,
508 int magic,
509 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510{
511 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100512 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (*s == NUL)
514 spats[idx].pat = NULL;
515 else
516 spats[idx].pat = vim_strsave(s);
517 spats[idx].magic = magic;
518 spats[idx].no_scs = FALSE;
519 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000520#if defined(FEAT_EVAL)
521 set_vv_searchforward();
522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 spats[idx].off.line = FALSE;
524 spats[idx].off.end = FALSE;
525 spats[idx].off.off = 0;
526 if (setlast)
527 last_idx = idx;
528 if (save_level)
529 {
530 vim_free(saved_spats[idx].pat);
531 saved_spats[idx] = spats[0];
532 if (spats[idx].pat == NULL)
533 saved_spats[idx].pat = NULL;
534 else
535 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100536# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100537 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100538# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100541 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100543 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544# endif
545}
546#endif
547
548#ifdef FEAT_SEARCH_EXTRA
549/*
550 * Get a regexp program for the last used search pattern.
551 * This is used for highlighting all matches in a window.
552 * Values returned in regmatch->regprog and regmatch->rmm_ic.
553 */
554 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100555last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556{
557 if (spats[last_idx].pat == NULL)
558 {
559 regmatch->regprog = NULL;
560 return;
561 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100562 ++emsg_off; // So it doesn't beep if bad expr
Rob Pillinge86190e2022-12-23 19:06:04 +0000563 (void)search_regcomp((char_u *)"", NULL, 0, last_idx, SEARCH_KEEP, regmatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 --emsg_off;
565}
566#endif
567
568/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100569 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100570 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
571 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 *
573 * if (options & SEARCH_MSG) == 0 don't give any messages
574 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
575 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
576 * if (options & SEARCH_HIS) put search pattern in history
577 * if (options & SEARCH_END) return position at end of match
578 * if (options & SEARCH_START) accept match at pos itself
579 * if (options & SEARCH_KEEP) keep previous search pattern
580 * if (options & SEARCH_FOLD) match only once in a closed fold
581 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100582 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 *
584 * Return FAIL (zero) for failure, non-zero for success.
585 * When FEAT_EVAL is defined, returns the index of the first matching
586 * subpattern plus one; one if there was none.
587 */
588 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100589searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200590 win_T *win, // window to search in; can be NULL for a
591 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100592 buf_T *buf,
593 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100594 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100595 int dir,
596 char_u *pat,
597 long count,
598 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200599 int pat_use, // which pattern to use when "pat" is empty
600 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100603 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100604 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 regmmatch_T regmatch;
606 char_u *ptr;
607 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000609 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 int loop;
611 pos_T start_pos;
612 int at_first_line;
613 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200614 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 int match_ok;
616 long nmatched;
617 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100618 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100619 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#ifdef FEAT_SEARCH_EXTRA
621 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200623 linenr_T stop_lnum = 0; // stop after this line number when != 0
Paul Ollis65745772022-06-05 16:55:54 +0100624 int unused_timeout_flag = FALSE;
625 int *timed_out = &unused_timeout_flag; // set when timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
Rob Pillinge86190e2022-12-23 19:06:04 +0000627 if (search_regcomp(pat, NULL, RE_SEARCH, pat_use,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
629 {
630 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000631 semsg(_(e_invalid_search_string_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return FAIL;
633 }
634
Paul Ollis65745772022-06-05 16:55:54 +0100635 if (extra_arg != NULL)
636 {
637 stop_lnum = extra_arg->sa_stop_lnum;
638#ifdef FEAT_RELTIME
639 if (extra_arg->sa_tm > 0)
Paul Ollis65745772022-06-05 16:55:54 +0100640 init_regexp_timeout(extra_arg->sa_tm);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100641 // Also set the pointer when sa_tm is zero, the caller may have set the
642 // timeout.
643 timed_out = &extra_arg->sa_timed_out;
Paul Ollis65745772022-06-05 16:55:54 +0100644#endif
645 }
646
Bram Moolenaar280f1262006-01-30 00:14:18 +0000647 /*
648 * find the string
649 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100650 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100652 // When not accepting a match at the start position set "extra_col" to
653 // a non-zero value. Don't do that when starting at MAXCOL, since
654 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200655 if (pos->col == MAXCOL)
656 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100657 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200658 else if (has_mbyte
659 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
660 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100661 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100662 ptr = ml_get_buf(buf, pos->lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100663 if (ml_get_buf_len(buf, pos->lnum) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200664 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100665 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100666 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100667 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100668 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200669 start_char_len = 1;
670 if (dir == FORWARD)
671 {
672 if (options & SEARCH_START)
673 extra_col = 0;
674 else
675 extra_col = start_char_len;
676 }
677 else
678 {
679 if (options & SEARCH_START)
680 extra_col = start_char_len;
681 else
682 extra_col = 0;
683 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100684
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100685 start_pos = *pos; // remember start pos for detecting no match
686 found = 0; // default: not found
687 at_first_line = TRUE; // default: start in first line
688 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
690 pos->lnum = 1;
691 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100692 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 }
694
695 /*
696 * Start searching in current line, unless searching backwards and
697 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000698 * If we are searching backwards, in column 0, and not including the
699 * current position, gain some efficiency by skipping back a line.
700 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000702 if (dir == BACKWARD && start_pos.col == 0
703 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 lnum = pos->lnum - 1;
706 at_first_line = FALSE;
707 }
708 else
709 lnum = pos->lnum;
710
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100711 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
714 lnum += dir, at_first_line = FALSE)
715 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100716 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000717 if (stop_lnum != 0 && (dir == FORWARD
718 ? lnum > stop_lnum : lnum < stop_lnum))
719 break;
Paul Ollis65745772022-06-05 16:55:54 +0100720 // Stop after passing the time limit.
721 if (*timed_out)
Bram Moolenaar76929292008-01-06 19:07:36 +0000722 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000723
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000725 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100727 col = at_first_line && (options & SEARCH_COL) ? pos->col
728 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 nmatched = vim_regexec_multi(&regmatch, win, buf,
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100730 lnum, col, timed_out);
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200731 // vim_regexec_multi() may clear "regprog"
732 if (regmatch.regprog == NULL)
733 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100734 // Abort searching on an error (e.g., out of stack).
Paul Ollis65745772022-06-05 16:55:54 +0100735 if (called_emsg > called_emsg_before || *timed_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 break;
737 if (nmatched > 0)
738 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100739 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000740 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000742#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000744#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100745 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000746 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
747 ptr = (char_u *)"";
748 else
749 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751 /*
752 * Forward search in the first line: match should be after
753 * the start position. If not, continue at the end of the
754 * match (this is vi compatible) or on the next char.
755 */
756 if (dir == FORWARD && at_first_line)
757 {
758 match_ok = TRUE;
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000761 * When the match starts in a next line it's certainly
762 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 * When match lands on a NUL the cursor will be put
764 * one back afterwards, compare with that position,
765 * otherwise "/$" will get stuck on end of line.
766 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000767 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100768 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000769 ? (nmatched == 1
770 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000772 : ((int)matchpos.col
773 - (ptr[matchpos.col] == NUL)
774 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 {
776 /*
777 * If vi-compatible searching, continue at the end
778 * of the match, otherwise continue one position
779 * forward.
780 */
781 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
782 {
783 if (nmatched > 1)
784 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100785 // end is in next line, thus no match in
786 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 match_ok = FALSE;
788 break;
789 }
790 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100791 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000792 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 && ptr[matchcol] != NUL)
794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 if (has_mbyte)
796 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000797 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ++matchcol;
800 }
801 }
802 else
803 {
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000804 // Advance "matchcol" to the next character.
Bram Moolenaar837ca8f2022-11-26 18:59:19 +0000805 // This uses rmm_matchcol, the actual start of
806 // the match, ignoring "\zs".
807 matchcol = regmatch.rmm_matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (ptr[matchcol] != NUL)
809 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000811 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 + matchcol);
813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 ++matchcol;
815 }
816 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200817 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (ptr[matchcol] == NUL
820 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000821 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100822 matchcol, timed_out)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 {
824 match_ok = FALSE;
825 break;
826 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200827 // vim_regexec_multi() may clear "regprog"
828 if (regmatch.regprog == NULL)
829 break;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000830 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 endpos = regmatch.endpos[0];
832# ifdef FEAT_EVAL
833 submatch = first_submatch(&regmatch);
834# endif
835
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100836 // Need to get the line pointer again, a
837 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000838 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 if (!match_ok)
841 continue;
842 }
843 if (dir == BACKWARD)
844 {
845 /*
846 * Now, if there are multiple matches on this line,
847 * we have to get the last one. Or the last one before
848 * the cursor, if we're on that line.
849 * When putting the new cursor at the end, compare
850 * relative to the end of the match.
851 */
852 match_ok = FALSE;
853 for (;;)
854 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100855 // Remember a position that is before the start
856 // position, we use it if it's the last match in
857 // the line. Always accept a position after
858 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000859 if (loop
860 || ((options & SEARCH_END)
861 ? (lnum + regmatch.endpos[0].lnum
862 < start_pos.lnum
863 || (lnum + regmatch.endpos[0].lnum
864 == start_pos.lnum
865 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200866 < (int)start_pos.col
867 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000868 : (lnum + regmatch.startpos[0].lnum
869 < start_pos.lnum
870 || (lnum + regmatch.startpos[0].lnum
871 == start_pos.lnum
872 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200873 < (int)start_pos.col
874 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000877 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 endpos = regmatch.endpos[0];
879# ifdef FEAT_EVAL
880 submatch = first_submatch(&regmatch);
881# endif
882 }
883 else
884 break;
885
886 /*
887 * We found a valid match, now check if there is
888 * another one after it.
889 * If vi-compatible searching, continue at the end
890 * of the match, otherwise continue one position
891 * forward.
892 */
893 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
894 {
895 if (nmatched > 1)
896 break;
897 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100898 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000899 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 && ptr[matchcol] != NUL)
901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (has_mbyte)
903 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000904 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 ++matchcol;
907 }
908 }
909 else
910 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100911 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000912 if (matchpos.lnum > 0)
913 break;
914 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 if (ptr[matchcol] != NUL)
916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if (has_mbyte)
918 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000919 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 ++matchcol;
922 }
923 }
924 if (ptr[matchcol] == NUL
925 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000926 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100927 matchcol, timed_out)) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100928 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100929 // If the search timed out, we did find a match
930 // but it might be the wrong one, so that's not
931 // OK.
Paul Ollis65745772022-06-05 16:55:54 +0100932 if (*timed_out)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100933 match_ok = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100935 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200936 // vim_regexec_multi() may clear "regprog"
937 if (regmatch.regprog == NULL)
938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100940 // Need to get the line pointer again, a
941 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000942 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
944
945 /*
946 * If there is only a match after the cursor, skip
947 * this match.
948 */
949 if (!match_ok)
950 continue;
951 }
952
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100953 // With the SEARCH_END option move to the last character
954 // of the match. Don't do it for an empty match, end
955 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200956 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000957 && !(matchpos.lnum == endpos.lnum
958 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100960 // For a match in the first column, set the position
961 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000962 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000963 pos->col = endpos.col;
964 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000965 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100966 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000967 {
968 --pos->lnum;
zeertzjq94b7c322024-03-12 21:50:32 +0100969 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000970 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000971 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000972 else
973 {
974 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000975 if (has_mbyte
976 && pos->lnum <= buf->b_ml.ml_line_count)
977 {
978 ptr = ml_get_buf(buf, pos->lnum, FALSE);
979 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
980 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000981 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100982 if (end_pos != NULL)
983 {
984 end_pos->lnum = lnum + matchpos.lnum;
985 end_pos->col = matchpos.col;
986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988 else
989 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000990 pos->lnum = lnum + matchpos.lnum;
991 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100992 if (end_pos != NULL)
993 {
994 end_pos->lnum = lnum + endpos.lnum;
995 end_pos->col = endpos.col;
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100999 if (end_pos != NULL)
1000 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001002 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001004 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001005 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 search_match_endcol = endpos.col;
1007 break;
1008 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001009 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (got_int)
1011 break;
1012
1013#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001014 // Cancel searching if a character was typed. Used for
1015 // 'incsearch'. Don't check too often, that would slowdown
1016 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if ((options & SEARCH_PEEK)
1018 && ((lnum - pos->lnum) & 0x3f) == 0
1019 && char_avail())
1020 {
1021 break_loop = TRUE;
1022 break;
1023 }
1024#endif
1025
1026 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001027 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 at_first_line = FALSE;
1030
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001031 // vim_regexec_multi() may clear "regprog"
1032 if (regmatch.regprog == NULL)
1033 break;
1034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001036 * Stop the search if wrapscan isn't set, "stop_lnum" is
1037 * specified, after an interrupt, after a match and after looping
1038 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001040 if (!p_ws || stop_lnum != 0 || got_int
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001041 || called_emsg > called_emsg_before || *timed_out
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001042#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001043 || break_loop
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001044#endif
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001045 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 break;
1047
1048 /*
1049 * If 'wrapscan' is set we continue at the other end of the file.
Christian Brabandt34a6a362023-05-06 19:20:20 +01001050 * If 'shortmess' does not contain 's', we give a message, but
1051 * only, if we won't show the search stat later anyhow,
1052 * (so SEARCH_COUNT must be absent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 * This message is also remembered in keep_msg for when the screen
1054 * is redrawn. The keep_msg is cleared whenever another message is
1055 * written.
1056 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001057 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 lnum = 1;
Christian Brabandt34a6a362023-05-06 19:20:20 +01001061 if (!shortmess(SHM_SEARCH)
1062 && shortmess(SHM_SEARCHCOUNT)
1063 && (options & SEARCH_MSG))
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001064 give_warning((char_u *)_(dir == BACKWARD
1065 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001066 if (extra_arg != NULL)
1067 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
Paul Ollis65745772022-06-05 16:55:54 +01001069 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001070#ifdef FEAT_SEARCH_EXTRA
1071 || break_loop
1072#endif
1073 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 break;
1075 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001076 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001078#ifdef FEAT_RELTIME
1079 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1080 disable_regexp_timeout();
1081#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001082 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001084 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001087 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1089 {
1090 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001091 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001093 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001095 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 return FAIL;
1098 }
1099
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001100 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001101 if (pos->lnum > buf->b_ml.ml_line_count)
1102 {
1103 pos->lnum = buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01001104 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001105 if (pos->col > 0)
1106 --pos->col;
1107 }
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 return submatch + 1;
1110}
1111
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001112#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001113 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001114set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001115{
1116 spats[0].off.dir = cdir;
1117}
1118
1119 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001120set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001121{
1122 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1123}
1124
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125/*
1126 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001127 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
1129 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001130first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
1132 int submatch;
1133
1134 for (submatch = 1; ; ++submatch)
1135 {
1136 if (rp->startpos[submatch].lnum >= 0)
1137 break;
1138 if (submatch == 9)
1139 {
1140 submatch = 0;
1141 break;
1142 }
1143 }
1144 return submatch;
1145}
1146#endif
1147
1148/*
1149 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001150 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 * If 'dirc' is 0: use previous dir.
1152 * If 'pat' is NULL or empty : use previous string.
1153 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1154 * If 'options & SEARCH_ECHO': echo the search command and handle options
1155 * If 'options & SEARCH_MSG' : may give error message
1156 * If 'options & SEARCH_OPT' : interpret optional flags
1157 * If 'options & SEARCH_HIS' : put search pattern in history
1158 * If 'options & SEARCH_NOOF': don't add offset to position
1159 * If 'options & SEARCH_MARK': set previous context mark
1160 * If 'options & SEARCH_KEEP': keep previous search pattern
1161 * If 'options & SEARCH_START': accept match at curpos itself
1162 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1163 *
1164 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1165 * makes the movement linewise without moving the match position.
1166 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001167 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 */
1169 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001170do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001171 oparg_T *oap, // can be NULL
1172 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001173 int search_delim, // the delimiter for the search, e.g. '%' in
1174 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001175 char_u *pat,
1176 long count,
1177 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001178 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001180 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001182 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001183 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 char_u *p;
1185 long c;
1186 char_u *dircp;
1187 char_u *strcopy = NULL;
1188 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001189 char_u *msgbuf = NULL;
1190 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001191 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 /*
1194 * A line offset is not remembered, this is vi compatible.
1195 */
1196 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1197 {
1198 spats[0].off.line = FALSE;
1199 spats[0].off.off = 0;
1200 }
1201
1202 /*
1203 * Save the values for when (options & SEARCH_KEEP) is used.
1204 * (there is no "if ()" around this because gcc wants them initialized)
1205 */
1206 old_off = spats[0].off;
1207
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001208 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
1210 /*
1211 * Find out the direction of the search.
1212 */
1213 if (dirc == 0)
1214 dirc = spats[0].off.dir;
1215 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001218#if defined(FEAT_EVAL)
1219 set_vv_searchforward();
1220#endif
1221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (options & SEARCH_REV)
1223 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001224#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001225 // There is a bug in the Visual C++ 2.2 compiler which means that
1226 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 dirc = (dirc == '/') ? '?' : '/';
1228#else
1229 if (dirc == '/')
1230 dirc = '?';
1231 else
1232 dirc = '/';
1233#endif
1234 }
1235
1236#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001237 // If the cursor is in a closed fold, don't find another match in the same
1238 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 if (dirc == '/')
1240 {
1241 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001242 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244 else
1245 {
1246 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1247 pos.col = 0;
1248 }
1249#endif
1250
1251#ifdef FEAT_SEARCH_EXTRA
1252 /*
1253 * Turn 'hlsearch' highlighting back on.
1254 */
1255 if (no_hlsearch && !(options & SEARCH_KEEP))
1256 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001257 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001258 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260#endif
1261
1262 /*
1263 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1264 */
1265 for (;;)
1266 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001267 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001268
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 searchstr = pat;
1270 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001271 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001272 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001274 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001276 searchstr = spats[RE_SUBST].pat;
1277 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001278 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001279 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001280 retval = 0;
1281 goto end_do_search;
1282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001284 else
1285 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001286 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001287 searchstr = (char_u *)"";
1288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001291 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
1293 /*
1294 * Find end of regular expression.
1295 * If there is a matching '/' or '?', toss it.
1296 */
1297 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001298 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001299 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (strcopy != ps)
1301 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001302 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001303 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 pat = strcopy;
1305 searchstr = strcopy;
1306 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001307 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001309 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 *p++ = NUL;
1311 }
1312 spats[0].off.line = FALSE;
1313 spats[0].off.end = FALSE;
1314 spats[0].off.off = 0;
1315 /*
1316 * Check for a line offset or a character offset.
1317 * For get_address (echo off) we don't check for a character
1318 * offset, because it is meaningless and the 's' could be a
1319 * substitute command.
1320 */
1321 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1322 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001323 else if ((options & SEARCH_OPT)
1324 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001326 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 spats[0].off.end = SEARCH_END;
1328 ++p;
1329 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001330 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001332 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1334 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001335 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001337 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 spats[0].off.off = 1;
1339 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001340 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 ++p;
1342 }
1343
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001344 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 searchcmdlen += (int)(p - pat);
1346
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001347 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 }
1349
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001350 if ((options & SEARCH_ECHO) && messaging()
1351 && !msg_silent
1352 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001355 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001356 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001358 // Compute msg_row early.
1359 msg_start();
1360
Bram Moolenaar984f0312019-05-24 13:11:47 +02001361 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001362 if (!cmd_silent &&
1363 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001364 {
1365 p = off_buf;
1366 *p++ = dirc;
1367 if (spats[0].off.end)
1368 *p++ = 'e';
1369 else if (!spats[0].off.line)
1370 *p++ = 's';
1371 if (spats[0].off.off > 0 || spats[0].off.line)
1372 *p++ = '+';
1373 *p = NUL;
1374 if (spats[0].off.off != 0 || spats[0].off.line)
1375 sprintf((char *)p, "%ld", spats[0].off.off);
1376 off_len = STRLEN(off_buf);
1377 }
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001380 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 else
1382 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001383
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001384 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001385 {
1386 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001387 // search stat. Use all the space available, so that the
1388 // search state is right aligned. If there is not enough space
1389 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001390 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001391 // Use all the columns.
1392 len = (int)(Rows - msg_row) * Columns - 1;
1393 else
1394 // Use up to 'showcmd' column.
1395 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001396 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1397 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001398 }
1399 else
1400 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001401 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001402
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001403 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001404 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 if (msgbuf != NULL)
1406 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001407 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001408 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001409 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1410 // empty for the search_stat feature.
1411 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001412 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001413 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001415 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1416 {
1417 // Use a space to draw the composing char on.
1418 msgbuf[1] = ' ';
1419 mch_memmove(msgbuf + 2, p, STRLEN(p));
1420 }
1421 else
1422 mch_memmove(msgbuf + 1, p, STRLEN(p));
1423 if (off_len > 0)
1424 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001426 trunc = msg_strtrunc(msgbuf, TRUE);
1427 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001429 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001430 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001433#ifdef FEAT_RIGHTLEFT
1434 // The search pattern could be shown on the right in
1435 // rightleft mode, but the 'ruler' and 'showcmd' area use
1436 // it too, thus it would be blanked out again very soon.
1437 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001438 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1439 {
1440 char_u *r;
1441 size_t pat_len;
1442
1443 r = reverse_text(msgbuf);
1444 if (r != NULL)
1445 {
1446 vim_free(msgbuf);
1447 msgbuf = r;
1448 // move reversed text to beginning of buffer
1449 while (*r != NUL && *r == ' ')
1450 r++;
1451 pat_len = msgbuf + STRLEN(msgbuf) - r;
1452 mch_memmove(msgbuf, r, pat_len);
1453 // overwrite old text
1454 if ((size_t)(r - msgbuf) >= pat_len)
1455 vim_memset(r, ' ', pat_len);
1456 else
1457 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1458 }
1459 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001460#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001461 msg_outtrans(msgbuf);
1462 msg_clr_eos();
1463 msg_check();
1464
1465 gotocmdline(FALSE);
1466 out_flush();
1467 msg_nowait = TRUE; // don't wait for this message
1468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
1470 }
1471
1472 /*
1473 * If there is a character offset, subtract it from the current
1474 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001475 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 * This is not done for a line offset, because then we would not be vi
1477 * compatible.
1478 */
Bram Moolenaared203462004-06-16 11:19:22 +00001479 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 if (spats[0].off.off > 0)
1482 {
1483 for (c = spats[0].off.off; c; --c)
1484 if (decl(&pos) == -1)
1485 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001486 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001488 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 pos.col = MAXCOL;
1490 }
1491 }
1492 else
1493 {
1494 for (c = spats[0].off.off; c; ++c)
1495 if (incl(&pos) == -1)
1496 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001497 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
1499 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1500 pos.col = 0;
1501 }
1502 }
1503 }
1504
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001505 /*
1506 * The actual search.
1507 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001508 c = searchit(curwin, curbuf, &pos, NULL,
1509 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 searchstr, count, spats[0].off.end + (options &
1511 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1512 + SEARCH_MSG + SEARCH_START
1513 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001514 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
1516 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001517 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001518
1519 if (!shortmess(SHM_SEARCH)
1520 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1521 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001522 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 if (c == FAIL)
1525 {
1526 retval = 0;
1527 goto end_do_search;
1528 }
1529 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001530 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001532 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
1534 /*
1535 * Add character and/or line offset
1536 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001537 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001539 pos_T org_pos = pos;
1540
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001541 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 {
1543 c = pos.lnum + spats[0].off.off;
1544 if (c < 1)
1545 pos.lnum = 1;
1546 else if (c > curbuf->b_ml.ml_line_count)
1547 pos.lnum = curbuf->b_ml.ml_line_count;
1548 else
1549 pos.lnum = c;
1550 pos.col = 0;
1551
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001552 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001554 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001556 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 c = spats[0].off.off;
1558 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (incl(&pos) == -1)
1562 break;
1563 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001564 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 else
1566 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001567 while (c++ < 0)
1568 if (decl(&pos) == -1)
1569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
1571 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001572 if (!EQUAL_POS(pos, org_pos))
1573 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 }
1575
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001576 // Show [1/15] if 'S' is not in 'shortmess'.
1577 if ((options & SEARCH_ECHO)
1578 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001579 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001580 && c != FAIL
1581 && !shortmess(SHM_SEARCHCOUNT)
1582 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001583 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1584 show_top_bot_msg, msgbuf,
1585 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001586#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001587 || (!(fdo_flags & FDO_SEARCH)
1588 && hasFolding(curwin->w_cursor.lnum,
1589 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001590#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001591 ),
1592 SEARCH_STAT_DEF_MAX_COUNT,
1593 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 /*
1596 * The search command can be followed by a ';' to do another search.
1597 * For example: "/pat/;/foo/+3;?bar"
1598 * This is like doing another search command, except:
1599 * - The remembered direction '/' or '?' is from the first search.
1600 * - When an error happens the cursor isn't moved at all.
1601 * Don't do this when called by get_address() (it handles ';' itself).
1602 */
1603 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1604 break;
1605
1606 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001607 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (dirc != '?' && dirc != '/')
1609 {
1610 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001611 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 goto end_do_search;
1613 }
1614 ++pat;
1615 }
1616
1617 if (options & SEARCH_MARK)
1618 setpcmark();
1619 curwin->w_cursor = pos;
1620 curwin->w_set_curswant = TRUE;
1621
1622end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001623 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 spats[0].off = old_off;
1625 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001626 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 return retval;
1629}
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631/*
1632 * search_for_exact_line(buf, pos, dir, pat)
1633 *
1634 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001635 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001637 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * Return OK for success, or FAIL if no line found.
1639 */
1640 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001641search_for_exact_line(
1642 buf_T *buf,
1643 pos_T *pos,
1644 int dir,
1645 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 linenr_T start = 0;
1648 char_u *ptr;
1649 char_u *p;
1650
1651 if (buf->b_ml.ml_line_count == 0)
1652 return FAIL;
1653 for (;;)
1654 {
1655 pos->lnum += dir;
1656 if (pos->lnum < 1)
1657 {
1658 if (p_ws)
1659 {
1660 pos->lnum = buf->b_ml.ml_line_count;
1661 if (!shortmess(SHM_SEARCH))
1662 give_warning((char_u *)_(top_bot_msg), TRUE);
1663 }
1664 else
1665 {
1666 pos->lnum = 1;
1667 break;
1668 }
1669 }
1670 else if (pos->lnum > buf->b_ml.ml_line_count)
1671 {
1672 if (p_ws)
1673 {
1674 pos->lnum = 1;
1675 if (!shortmess(SHM_SEARCH))
1676 give_warning((char_u *)_(bot_top_msg), TRUE);
1677 }
1678 else
1679 {
1680 pos->lnum = 1;
1681 break;
1682 }
1683 }
1684 if (pos->lnum == start)
1685 break;
1686 if (start == 0)
1687 start = pos->lnum;
1688 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1689 p = skipwhite(ptr);
1690 pos->col = (colnr_T) (p - ptr);
1691
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001692 // when adding lines the matching line may be empty but it is not
1693 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001694 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 {
1696 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1697 return OK;
1698 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001699 else if (*p != NUL) // ignore empty lines
1700 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001701 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1702 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 return OK;
1704 }
1705 }
1706 return FAIL;
1707}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
1709/*
1710 * Character Searches
1711 */
1712
1713/*
1714 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1715 * position of the character, otherwise move to just before the char.
1716 * Do this "cap->count1" times.
1717 * Return FAIL or OK.
1718 */
1719 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001720searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001722 int c = cap->nchar; // char to search for
1723 int dir = cap->arg; // TRUE for searching forward
1724 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 int col;
1726 char_u *p;
1727 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001728 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001730 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001732 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001734 *lastc = c;
1735 set_csearch_direction(dir);
1736 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001737 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (cap->ncharC1 != 0)
1739 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001740 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1741 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001743 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1744 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
1747 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001748 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 {
zeertzjqe5d91ba2023-05-14 17:39:18 +01001750 if (*lastc == NUL && lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001752 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 dir = -lastcdir;
1754 else
1755 dir = lastcdir;
1756 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001757 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001758 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001759
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001760 // Force a move of at least one char, so ";" and "," will move the
1761 // cursor, even if the cursor is right in front of char we are looking
1762 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001763 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001764 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001767 if (dir == BACKWARD)
1768 cap->oap->inclusive = FALSE;
1769 else
1770 cap->oap->inclusive = TRUE;
1771
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 p = ml_get_curline();
1773 col = curwin->w_cursor.col;
zeertzjq94b7c322024-03-12 21:50:32 +01001774 len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775
1776 while (count--)
1777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (has_mbyte)
1779 {
1780 for (;;)
1781 {
1782 if (dir > 0)
1783 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001784 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 if (col >= len)
1786 return FAIL;
1787 }
1788 else
1789 {
1790 if (col == 0)
1791 return FAIL;
1792 col -= (*mb_head_off)(p, p + col - 1) + 1;
1793 }
zeertzjqe5d91ba2023-05-14 17:39:18 +01001794 if (lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001796 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 break;
1798 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001799 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001800 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001801 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001802 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 }
1805 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807 for (;;)
1808 {
1809 if ((col += dir) < 0 || col >= len)
1810 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001811 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001813 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815 }
1816 }
1817
1818 if (t_cmd)
1819 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001820 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (has_mbyte)
1823 {
1824 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001825 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001826 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001828 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 col -= (*mb_head_off)(p, p + col);
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832 curwin->w_cursor.col = col;
1833
1834 return OK;
1835}
1836
1837/*
1838 * "Other" Searches
1839 */
1840
1841/*
1842 * findmatch - find the matching paren or brace
1843 *
1844 * Improvement over vi: Braces inside quotes are ignored.
1845 */
1846 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001847findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 return findmatchlimit(oap, initc, 0, 0);
1850}
1851
1852/*
1853 * Return TRUE if the character before "linep[col]" equals "ch".
1854 * Return FALSE if "col" is zero.
1855 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1856 * is NULL.
1857 * Handles multibyte string correctly.
1858 */
1859 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001860check_prevcol(
1861 char_u *linep,
1862 int col,
1863 int ch,
1864 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865{
1866 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (col > 0 && has_mbyte)
1868 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (prevcol)
1870 *prevcol = col;
1871 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1872}
1873
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001874/*
1875 * Raw string start is found at linep[startpos.col - 1].
1876 * Return TRUE if the matching end can be found between startpos and endpos.
1877 */
1878 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001879find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001880{
1881 char_u *p;
1882 char_u *delim_copy;
1883 size_t delim_len;
1884 linenr_T lnum;
1885 int found = FALSE;
1886
1887 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1888 ;
1889 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001890 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001891 if (delim_copy == NULL)
1892 return FALSE;
1893 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1894 {
1895 char_u *line = ml_get(lnum);
1896
1897 for (p = line + (lnum == startpos->lnum
1898 ? startpos->col + 1 : 0); *p; ++p)
1899 {
1900 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1901 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02001902 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
1903 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001904 {
1905 found = TRUE;
1906 break;
1907 }
1908 }
1909 if (found)
1910 break;
1911 }
1912 vim_free(delim_copy);
1913 return found;
1914}
1915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001917 * Check matchpairs option for "*initc".
1918 * If there is a match set "*initc" to the matching character and "*findc" to
1919 * the opposite character. Set "*backwards" to the direction.
1920 * When "switchit" is TRUE swap the direction.
1921 */
1922 static void
1923find_mps_values(
1924 int *initc,
1925 int *findc,
1926 int *backwards,
1927 int switchit)
1928{
1929 char_u *ptr;
1930
1931 ptr = curbuf->b_p_mps;
1932 while (*ptr != NUL)
1933 {
1934 if (has_mbyte)
1935 {
1936 char_u *prev;
1937
1938 if (mb_ptr2char(ptr) == *initc)
1939 {
1940 if (switchit)
1941 {
1942 *findc = *initc;
1943 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1944 *backwards = TRUE;
1945 }
1946 else
1947 {
1948 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1949 *backwards = FALSE;
1950 }
1951 return;
1952 }
1953 prev = ptr;
1954 ptr += mb_ptr2len(ptr) + 1;
1955 if (mb_ptr2char(ptr) == *initc)
1956 {
1957 if (switchit)
1958 {
1959 *findc = *initc;
1960 *initc = mb_ptr2char(prev);
1961 *backwards = FALSE;
1962 }
1963 else
1964 {
1965 *findc = mb_ptr2char(prev);
1966 *backwards = TRUE;
1967 }
1968 return;
1969 }
1970 ptr += mb_ptr2len(ptr);
1971 }
1972 else
1973 {
1974 if (*ptr == *initc)
1975 {
1976 if (switchit)
1977 {
1978 *backwards = TRUE;
1979 *findc = *initc;
1980 *initc = ptr[2];
1981 }
1982 else
1983 {
1984 *backwards = FALSE;
1985 *findc = ptr[2];
1986 }
1987 return;
1988 }
1989 ptr += 2;
1990 if (*ptr == *initc)
1991 {
1992 if (switchit)
1993 {
1994 *backwards = FALSE;
1995 *findc = *initc;
1996 *initc = ptr[-2];
1997 }
1998 else
1999 {
2000 *backwards = TRUE;
2001 *findc = ptr[-2];
2002 }
2003 return;
2004 }
2005 ++ptr;
2006 }
2007 if (*ptr == ',')
2008 ++ptr;
2009 }
2010}
2011
2012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002014 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2015 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 *
2017 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002018 * character at or after the cursor. Special values:
2019 * '*' look for C-style comment / *
2020 * '/' look for C-style comment / *, ignoring comment-end
2021 * '#' look for preprocessor directives
2022 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 *
2024 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2025 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2026 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2027 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002028 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002029 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002030 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002033findmatchlimit(
2034 oparg_T *oap,
2035 int initc,
2036 int flags,
2037 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002039 static pos_T pos; // current search position
2040 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002042 int count = 0; // cumulative number of braces
2043 int backwards = FALSE; // init for gcc
2044 int raw_string = FALSE; // search for raw string
2045 int inquote = FALSE; // TRUE when inside quotes
2046 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002048 int do_quotes; // check for quotes in current line
2049 int at_start; // do_quotes value at start position
2050 int hash_dir = 0; // Direction searched for # things
2051 int comment_dir = 0; // Direction searched for comments
2052 pos_T match_pos; // Where last slash-star was found
2053 int start_in_quotes; // start position is in quotes
2054 int traveled = 0; // how far we've searched so far
2055 int ignore_cend = FALSE; // ignore comment end
2056 int cpo_match; // vi compatible matching
2057 int cpo_bsl; // don't recognize backslashes
2058 int match_escaped = 0; // search for escaped match
2059 int dir; // Direction to search
2060 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002061 int lispcomm = FALSE; // inside of Lisp-style comment
2062 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002065 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 linep = ml_get(pos.lnum);
2067
2068 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2069 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2070
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002071 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (flags & FM_BACKWARD)
2073 dir = BACKWARD;
2074 else if (flags & FM_FORWARD)
2075 dir = FORWARD;
2076 else
2077 dir = 0;
2078
2079 /*
2080 * if initc given, look in the table for the matching character
2081 * '/' and '*' are special cases: look for start or end of comment.
2082 * When '/' is used, we ignore running backwards into an star-slash, for
2083 * "[*" command, we just want to find any comment.
2084 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002085 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
2087 comment_dir = dir;
2088 if (initc == '/')
2089 ignore_cend = TRUE;
2090 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002091 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 initc = NUL;
2093 }
2094 else if (initc != '#' && initc != NUL)
2095 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002096 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002097 if (dir)
2098 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002099 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 return NULL;
2101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 else
2103 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002104 /*
2105 * Either initc is '#', or no initc was given and we need to look
2106 * under the cursor.
2107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (initc == '#')
2109 {
2110 hash_dir = dir;
2111 }
2112 else
2113 {
2114 /*
2115 * initc was not given, must look for something to match under
2116 * or near the cursor.
2117 * Only check for special things when 'cpo' doesn't have '%'.
2118 */
2119 if (!cpo_match)
2120 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002121 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 ptr = skipwhite(linep);
2123 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2124 {
2125 ptr = skipwhite(ptr + 1);
2126 if ( STRNCMP(ptr, "if", 2) == 0
2127 || STRNCMP(ptr, "endif", 5) == 0
2128 || STRNCMP(ptr, "el", 2) == 0)
2129 hash_dir = 1;
2130 }
2131
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002132 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 else if (linep[pos.col] == '/')
2134 {
2135 if (linep[pos.col + 1] == '*')
2136 {
2137 comment_dir = FORWARD;
2138 backwards = FALSE;
2139 pos.col++;
2140 }
2141 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2142 {
2143 comment_dir = BACKWARD;
2144 backwards = TRUE;
2145 pos.col--;
2146 }
2147 }
2148 else if (linep[pos.col] == '*')
2149 {
2150 if (linep[pos.col + 1] == '/')
2151 {
2152 comment_dir = BACKWARD;
2153 backwards = TRUE;
2154 }
2155 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2156 {
2157 comment_dir = FORWARD;
2158 backwards = FALSE;
2159 }
2160 }
2161 }
2162
2163 /*
2164 * If we are not on a comment or the # at the start of a line, then
2165 * look for brace anywhere on this line after the cursor.
2166 */
2167 if (!hash_dir && !comment_dir)
2168 {
2169 /*
2170 * Find the brace under or after the cursor.
2171 * If beyond the end of the line, use the last character in
2172 * the line.
2173 */
2174 if (linep[pos.col] == NUL && pos.col)
2175 --pos.col;
2176 for (;;)
2177 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002178 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (initc == NUL)
2180 break;
2181
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002182 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (findc)
2184 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002185 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 if (!findc)
2188 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002189 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (!cpo_match && *skipwhite(linep) == '#')
2191 hash_dir = 1;
2192 else
2193 return NULL;
2194 }
2195 else if (!cpo_bsl)
2196 {
2197 int col, bslcnt = 0;
2198
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002199 // Set "match_escaped" if there are an odd number of
2200 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2202 bslcnt++;
2203 match_escaped = (bslcnt & 1);
2204 }
2205 }
2206 }
2207 if (hash_dir)
2208 {
2209 /*
2210 * Look for matching #if, #else, #elif, or #endif
2211 */
2212 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002213 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (initc != '#')
2215 {
2216 ptr = skipwhite(skipwhite(linep) + 1);
2217 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2218 hash_dir = 1;
2219 else if (STRNCMP(ptr, "endif", 5) == 0)
2220 hash_dir = -1;
2221 else
2222 return NULL;
2223 }
2224 pos.col = 0;
2225 while (!got_int)
2226 {
2227 if (hash_dir > 0)
2228 {
2229 if (pos.lnum == curbuf->b_ml.ml_line_count)
2230 break;
2231 }
2232 else if (pos.lnum == 1)
2233 break;
2234 pos.lnum += hash_dir;
2235 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002236 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 ptr = skipwhite(linep);
2238 if (*ptr != '#')
2239 continue;
2240 pos.col = (colnr_T) (ptr - linep);
2241 ptr = skipwhite(ptr + 1);
2242 if (hash_dir > 0)
2243 {
2244 if (STRNCMP(ptr, "if", 2) == 0)
2245 count++;
2246 else if (STRNCMP(ptr, "el", 2) == 0)
2247 {
2248 if (count == 0)
2249 return &pos;
2250 }
2251 else if (STRNCMP(ptr, "endif", 5) == 0)
2252 {
2253 if (count == 0)
2254 return &pos;
2255 count--;
2256 }
2257 }
2258 else
2259 {
2260 if (STRNCMP(ptr, "if", 2) == 0)
2261 {
2262 if (count == 0)
2263 return &pos;
2264 count--;
2265 }
2266 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2267 {
2268 if (count == 0)
2269 return &pos;
2270 }
2271 else if (STRNCMP(ptr, "endif", 5) == 0)
2272 count++;
2273 }
2274 }
2275 return NULL;
2276 }
2277 }
2278
2279#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002280 // This is just guessing: when 'rightleft' is set, search for a matching
2281 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2283 backwards = !backwards;
2284#endif
2285
2286 do_quotes = -1;
2287 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002288 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002289
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002290 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002291 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002293 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002294 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002295
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 while (!got_int)
2297 {
2298 /*
2299 * Go to the next position, forward or backward. We could use
2300 * inc() and dec() here, but that is much slower
2301 */
2302 if (backwards)
2303 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002304 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002305 if (lispcomm && pos.col < (colnr_T)comment_col)
2306 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002307 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002309 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 break;
2311 --pos.lnum;
2312
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002313 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 break;
2315
2316 linep = ml_get(pos.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01002317 pos.col = ml_get_len(pos.lnum); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 do_quotes = -1;
2319 line_breakcheck();
2320
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002321 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002322 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002324 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002325 if (lisp && comment_col != MAXCOL)
2326 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
2328 else
2329 {
2330 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 if (has_mbyte)
2332 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 }
2334 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002335 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002337 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002338 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002339 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002340 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002341 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002343 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002344 // line is exhausted and comment with it,
2345 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002346 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 break;
2348 ++pos.lnum;
2349
2350 if (maxtravel && traveled++ > maxtravel)
2351 break;
2352
2353 linep = ml_get(pos.lnum);
2354 pos.col = 0;
2355 do_quotes = -1;
2356 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002357 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002358 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
2360 else
2361 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002363 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 ++pos.col;
2366 }
2367 }
2368
2369 /*
2370 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2371 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002372 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2373 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002375 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002377 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 }
2379
2380 if (comment_dir)
2381 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002382 // Note: comments do not nest, and we ignore quotes in them
2383 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (comment_dir == FORWARD)
2385 {
2386 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2387 {
2388 pos.col++;
2389 return &pos;
2390 }
2391 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002392 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 {
2394 /*
2395 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002396 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 */
2398 if (pos.col == 0)
2399 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002400 else if (raw_string)
2401 {
2402 if (linep[pos.col - 1] == 'R'
2403 && linep[pos.col] == '"'
2404 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2405 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002406 // Possible start of raw string. Now that we have the
2407 // delimiter we can check if it ends before where we
2408 // started searching, or before the previously found
2409 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002410 if (!find_rawstring_end(linep, &pos,
2411 count > 0 ? &match_pos : &curwin->w_cursor))
2412 {
2413 count++;
2414 match_pos = pos;
2415 match_pos.col--;
2416 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002417 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002418 }
2419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 else if ( linep[pos.col - 1] == '/'
2421 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002422 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 && (int)pos.col < comment_col)
2424 {
2425 count++;
2426 match_pos = pos;
2427 match_pos.col--;
2428 }
2429 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2430 {
2431 if (count > 0)
2432 pos = match_pos;
2433 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2434 && (int)pos.col <= comment_col)
2435 pos.col -= 2;
2436 else if (ignore_cend)
2437 continue;
2438 else
2439 return NULL;
2440 return &pos;
2441 }
2442 }
2443 continue;
2444 }
2445
2446 /*
2447 * If smart matching ('cpoptions' does not contain '%'), braces inside
2448 * of quotes are ignored, but only if there is an even number of
2449 * quotes in the line.
2450 */
2451 if (cpo_match)
2452 do_quotes = 0;
2453 else if (do_quotes == -1)
2454 {
2455 /*
2456 * Count the number of quotes in the line, skipping \" and '"'.
2457 * Watch out for "\\".
2458 */
2459 at_start = do_quotes;
2460 for (ptr = linep; *ptr; ++ptr)
2461 {
2462 if (ptr == linep + pos.col + backwards)
2463 at_start = (do_quotes & 1);
2464 if (*ptr == '"'
2465 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2466 ++do_quotes;
2467 if (*ptr == '\\' && ptr[1] != NUL)
2468 ++ptr;
2469 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002470 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 /*
2473 * If we find an uneven count, check current line and previous
2474 * one for a '\' at the end.
2475 */
2476 if (!do_quotes)
2477 {
2478 inquote = FALSE;
2479 if (ptr[-1] == '\\')
2480 {
2481 do_quotes = 1;
2482 if (start_in_quotes == MAYBE)
2483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002484 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 inquote = TRUE;
2486 start_in_quotes = TRUE;
2487 }
2488 else if (backwards)
2489 inquote = TRUE;
2490 }
2491 if (pos.lnum > 1)
2492 {
2493 ptr = ml_get(pos.lnum - 1);
zeertzjq94b7c322024-03-12 21:50:32 +01002494 if (*ptr && *(ptr + ml_get_len(pos.lnum - 1) - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 do_quotes = 1;
2497 if (start_in_quotes == MAYBE)
2498 {
2499 inquote = at_start;
2500 if (inquote)
2501 start_in_quotes = TRUE;
2502 }
2503 else if (!backwards)
2504 inquote = TRUE;
2505 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002506
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002507 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002508 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510 }
2511 }
2512 if (start_in_quotes == MAYBE)
2513 start_in_quotes = FALSE;
2514
2515 /*
2516 * If 'smartmatch' is set:
2517 * Things inside quotes are ignored by setting 'inquote'. If we
2518 * find a quote without a preceding '\' invert 'inquote'. At the
2519 * end of a line not ending in '\' we reset 'inquote'.
2520 *
2521 * In lines with an uneven number of quotes (without preceding '\')
2522 * we do not know which part to ignore. Therefore we only set
2523 * inquote if the number of quotes in a line is even, unless this
2524 * line or the previous one ends in a '\'. Complicated, isn't it?
2525 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002526 c = PTR2CHAR(linep + pos.col);
2527 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002530 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2532 {
2533 inquote = FALSE;
2534 start_in_quotes = FALSE;
2535 }
2536 break;
2537
2538 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002539 // a quote that is preceded with an odd number of backslashes is
2540 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (do_quotes)
2542 {
2543 int col;
2544
2545 for (col = pos.col - 1; col >= 0; --col)
2546 if (linep[col] != '\\')
2547 break;
2548 if ((((int)pos.col - 1 - col) & 1) == 0)
2549 {
2550 inquote = !inquote;
2551 start_in_quotes = FALSE;
2552 }
2553 }
2554 break;
2555
2556 /*
2557 * If smart matching ('cpoptions' does not contain '%'):
2558 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2559 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2560 * skipped, there is never a brace in them.
2561 * Ignore this when finding matches for `'.
2562 */
2563 case '\'':
2564 if (!cpo_match && initc != '\'' && findc != '\'')
2565 {
2566 if (backwards)
2567 {
2568 if (pos.col > 1)
2569 {
2570 if (linep[pos.col - 2] == '\'')
2571 {
2572 pos.col -= 2;
2573 break;
2574 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002575 else if (linep[pos.col - 2] == '\\'
2576 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
2578 pos.col -= 3;
2579 break;
2580 }
2581 }
2582 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002583 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002585 if (linep[pos.col + 1] == '\\'
2586 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
2588 pos.col += 3;
2589 break;
2590 }
2591 else if (linep[pos.col + 2] == '\'')
2592 {
2593 pos.col += 2;
2594 break;
2595 }
2596 }
2597 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002598 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599
2600 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002601 /*
2602 * For Lisp skip over backslashed (), {} and [].
2603 * (actually, we skip #\( et al)
2604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002606 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002607 && pos.col > 1
2608 && check_prevcol(linep, pos.col, '\\', NULL)
2609 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002612 // Check for match outside of quotes, and inside of
2613 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 if ((!inquote || start_in_quotes == TRUE)
2615 && (c == initc || c == findc))
2616 {
2617 int col, bslcnt = 0;
2618
2619 if (!cpo_bsl)
2620 {
2621 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2622 bslcnt++;
2623 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002624 // Only accept a match when 'M' is in 'cpo' or when escaping
2625 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2627 {
2628 if (c == initc)
2629 count++;
2630 else
2631 {
2632 if (count == 0)
2633 return &pos;
2634 count--;
2635 }
2636 }
2637 }
2638 }
2639 }
2640
2641 if (comment_dir == BACKWARD && count > 0)
2642 {
2643 pos = match_pos;
2644 return &pos;
2645 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002646 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647}
2648
2649/*
2650 * Check if line[] contains a / / comment.
2651 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002653 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002654check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 char_u *p;
2657
2658 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002659 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002660 if (curbuf->b_p_lisp)
2661 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002662 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002663 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002664 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002665
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002666 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002667 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002668 {
2669 if (*p == '"')
2670 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002671 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002672 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002673 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002674 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002675 }
2676 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002677 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002678 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002679 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002680 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002681 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002682 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2683 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002684 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002685 ++p;
2686 }
2687 }
2688 else
2689 p = NULL;
2690 }
2691 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002692 while ((p = vim_strchr(p, '/')) != NULL)
2693 {
2694 // Accept a double /, unless it's preceded with * and followed by
2695 // *, because * / / * is an end and start of a C comment. Only
2696 // accept the position if it is not inside a string.
2697 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002698 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002699 break;
2700 ++p;
2701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 if (p == NULL)
2704 return MAXCOL;
2705 return (int)(p - line);
2706}
2707
2708/*
2709 * Move cursor briefly to character matching the one under the cursor.
2710 * Used for Insert mode and "r" command.
2711 * Show the match only if it is visible on the screen.
2712 * If there isn't a match, then beep.
2713 */
2714 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002715showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002716 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
2718 pos_T *lpos, save_cursor;
2719 pos_T mpos;
2720 colnr_T vcol;
2721 long save_so;
2722 long save_siso;
2723#ifdef CURSOR_SHAPE
2724 int save_state;
2725#endif
2726 colnr_T save_dollar_vcol;
2727 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002728 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2729 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730
2731 /*
2732 * Only show match for chars in the 'matchpairs' option.
2733 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002734 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002735 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {
2737#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002738 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002739 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002740#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002741 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002742 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#ifdef FEAT_RIGHTLEFT
2744 && !(curwin->w_p_rl ^ p_ri)
2745#endif
2746 )
2747 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002748 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002749 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 return;
2751 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002752 if (*p == NUL)
2753 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002755 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002757 vim_beep(BO_MATCH);
2758 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002760
2761 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2762 return;
2763
2764 if (!curwin->w_p_wrap)
2765 getvcol(curwin, lpos, NULL, &vcol, NULL);
2766
2767 int col_visible = (curwin->w_p_wrap
2768 || (vcol >= curwin->w_leftcol
2769 && vcol < curwin->w_leftcol + curwin->w_width));
2770 if (!col_visible)
2771 return;
2772
2773 mpos = *lpos; // save the pos, update_screen() may change it
2774 save_cursor = curwin->w_cursor;
2775 save_so = *so;
2776 save_siso = *siso;
2777 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2778 // stop displaying the "$".
2779 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2780 dollar_vcol = -1;
2781 ++curwin->w_virtcol; // do display ')' just before "$"
2782 update_screen(UPD_VALID); // show the new char first
2783
2784 save_dollar_vcol = dollar_vcol;
2785#ifdef CURSOR_SHAPE
2786 save_state = State;
2787 State = MODE_SHOWMATCH;
2788 ui_cursor_shape(); // may show different cursor shape
2789#endif
2790 curwin->w_cursor = mpos; // move to matching char
2791 *so = 0; // don't use 'scrolloff' here
2792 *siso = 0; // don't use 'sidescrolloff' here
2793 showruler(FALSE);
2794 setcursor();
2795 cursor_on(); // make sure that the cursor is shown
2796 out_flush_cursor(TRUE, FALSE);
2797
2798 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2799 // which resets it if the matching position is in a previous line
2800 // and has a higher column number.
2801 dollar_vcol = save_dollar_vcol;
2802
2803 /*
2804 * brief pause, unless 'm' is present in 'cpo' and a character is
2805 * available.
2806 */
2807 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2808 ui_delay(p_mat * 100L + 8, TRUE);
2809 else if (!char_avail())
2810 ui_delay(p_mat * 100L + 9, FALSE);
2811 curwin->w_cursor = save_cursor; // restore cursor position
2812 *so = save_so;
2813 *siso = save_siso;
2814#ifdef CURSOR_SHAPE
2815 State = save_state;
2816 ui_cursor_shape(); // may show different cursor shape
2817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818}
2819
2820/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002821 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002822 * If move is TRUE, check from the beginning of the buffer, else from position
2823 * "cur".
2824 * "direction" is FORWARD or BACKWARD.
2825 * Returns TRUE, FALSE or -1 for failure.
2826 */
2827 static int
2828is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2829{
2830 regmmatch_T regmatch;
2831 int nmatched = 0;
2832 int result = -1;
2833 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002834 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002835 int flag = 0;
2836
2837 if (pattern == NULL)
2838 pattern = spats[last_idx].pat;
2839
Rob Pillinge86190e2022-12-23 19:06:04 +00002840 if (search_regcomp(pattern, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002841 SEARCH_KEEP, &regmatch) == FAIL)
2842 return -1;
2843
2844 // init startcol correctly
2845 regmatch.startpos[0].col = -1;
2846 // move to match
2847 if (move)
2848 {
2849 CLEAR_POS(&pos);
2850 }
2851 else
2852 {
2853 pos = *cur;
2854 // accept a match at the cursor position
2855 flag = SEARCH_START;
2856 }
2857
2858 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2859 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2860 {
2861 // Zero-width pattern should match somewhere, then we can check if
2862 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002863 do
2864 {
2865 regmatch.startpos[0].col++;
2866 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002867 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002868 if (nmatched != 0)
2869 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002870 } while (regmatch.regprog != NULL
2871 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002872 : regmatch.startpos[0].col > pos.col);
2873
Bram Moolenaar53989552019-12-23 22:59:18 +01002874 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002875 {
2876 result = (nmatched != 0
2877 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2878 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2879 }
2880 }
2881
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002882 vim_regfree(regmatch.regprog);
2883 return result;
2884}
2885
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002886
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002887/*
2888 * Find next search match under cursor, cursor at end.
2889 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002890 */
2891 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002892current_search(
2893 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002894 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002895{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002896 pos_T start_pos; // start position of the pattern match
2897 pos_T end_pos; // end position of the pattern match
2898 pos_T orig_pos; // position of the cursor at beginning
2899 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002900 int i;
2901 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002902 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002903 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002904 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002905 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002906 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002907 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002908
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002909 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002910 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002911 dec_cursor();
2912
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002913 // When searching forward and the cursor is at the start of the Visual
2914 // area, skip the first search backward, otherwise it doesn't move.
2915 skip_first_backward = forward && VIsual_active
2916 && LT_POS(curwin->w_cursor, VIsual);
2917
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002918 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002919 if (VIsual_active)
2920 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002921 if (forward)
2922 incl(&pos);
2923 else
2924 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002925 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002926
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002927 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002928 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02002929 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002930 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002931 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002932
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002933 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002934 * The trick is to first search backwards and then search forward again,
2935 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002936 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002937 */
2938 for (i = 0; i < 2; i++)
2939 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002940 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002941 {
2942 if (i == 0 && skip_first_backward)
2943 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002944 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002945 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002946 else
2947 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002948
2949 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002950 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002951 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002952 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002953
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002954 // wrapping should not occur in the first round
2955 if (i == 0)
2956 p_ws = FALSE;
2957
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002958 result = searchit(curwin, curbuf, &pos, &end_pos,
2959 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002960 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002961 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002962
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002963 p_ws = old_p_ws;
2964
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002965 // First search may fail, but then start searching from the
2966 // beginning of the file (cursor might be on the search match)
2967 // except when Visual mode is active, so that extending the visual
2968 // selection works.
2969 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002970 {
2971 curwin->w_cursor = orig_pos;
2972 if (VIsual_active)
2973 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002974 return FAIL;
2975 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002976 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002977 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002978 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002979 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002980 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002981 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002982 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002983 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002984 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002985 // try again from end of buffer
2986 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002987 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01002988 pos.col = ml_get_len(curwin->w_buffer->b_ml.ml_line_count);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002989 }
2990 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002991 }
2992
2993 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002994
2995 if (!VIsual_active)
2996 VIsual = start_pos;
2997
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002998 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002999 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003000 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003001 {
3002 if (skip_first_backward)
3003 // put the cursor on the start of the match
3004 curwin->w_cursor = pos;
3005 else
3006 // put the cursor on last character of match
3007 dec_cursor();
3008 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003009 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003010 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003011 VIsual_active = TRUE;
3012 VIsual_mode = 'v';
3013
Bram Moolenaarb7633612019-02-10 21:48:25 +01003014 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003015 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003016 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003017 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3018 inc_cursor();
3019 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3020 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003021 }
3022
3023#ifdef FEAT_FOLDING
3024 if (fdo_flags & FDO_SEARCH && KeyTyped)
3025 foldOpenCursor();
3026#endif
3027
3028 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003029 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003030#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003031 // Make sure the clipboard gets updated. Needed because start and
3032 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003033 clip_star.vmode = NUL;
3034#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003035 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003036 showmode();
3037
3038 return OK;
3039}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041/*
3042 * return TRUE if line 'lnum' is empty or has white chars only.
3043 */
3044 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003045linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046{
3047 char_u *p;
3048
3049 p = skipwhite(ml_get(lnum));
3050 return (*p == NUL);
3051}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003053/*
3054 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003055 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003056 */
3057 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003058cmdline_search_stat(
3059 int dirc,
3060 pos_T *pos,
3061 pos_T *cursor_pos,
3062 int show_top_bot_msg,
3063 char_u *msgbuf,
3064 int recompute,
3065 int maxcount,
3066 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003067{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003068 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003069
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003070 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3071 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003072 if (stat.cur <= 0)
3073 return;
3074
3075 char t[SEARCH_STAT_BUF_LEN];
3076 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003077
3078#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003079 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3080 {
3081 if (stat.incomplete == 1)
3082 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3083 else if (stat.cnt > maxcount && stat.cur > maxcount)
3084 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3085 maxcount, maxcount);
3086 else if (stat.cnt > maxcount)
3087 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3088 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003089 else
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003090 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3091 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003092 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003093 else
3094#endif
3095 {
3096 if (stat.incomplete == 1)
3097 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3098 else if (stat.cnt > maxcount && stat.cur > maxcount)
3099 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3100 maxcount, maxcount);
3101 else if (stat.cnt > maxcount)
3102 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3103 stat.cur, maxcount);
3104 else
3105 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3106 stat.cur, stat.cnt);
3107 }
3108
3109 len = STRLEN(t);
3110 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3111 {
3112 mch_memmove(t + 2, t, len);
3113 t[0] = 'W';
3114 t[1] = ' ';
3115 len += 2;
3116 }
3117
zeertzjqa7d36b72023-01-31 21:13:38 +00003118 size_t msgbuf_len = STRLEN(msgbuf);
3119 if (len > msgbuf_len)
3120 len = msgbuf_len;
3121 mch_memmove(msgbuf + msgbuf_len - len, t, len);
3122
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003123 if (dirc == '?' && stat.cur == maxcount + 1)
3124 stat.cur = -1;
3125
3126 // keep the message even after redraw, but don't put in history
3127 msg_hist_off = TRUE;
3128 give_warning(msgbuf, FALSE);
3129 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003130}
3131
3132/*
3133 * Add the search count information to "stat".
3134 * "stat" must not be NULL.
3135 * When "recompute" is TRUE always recompute the numbers.
3136 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3137 * dirc == '/': find the next match
3138 * dirc == '?': find the previous match
3139 */
3140 static void
3141update_search_stat(
3142 int dirc,
3143 pos_T *pos,
3144 pos_T *cursor_pos,
3145 searchstat_T *stat,
3146 int recompute,
3147 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003148 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003149{
3150 int save_ws = p_ws;
3151 int wraparound = FALSE;
3152 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003153 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003154 static int cur = 0;
3155 static int cnt = 0;
3156 static int exact_match = FALSE;
3157 static int incomplete = 0;
3158 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3159 static int chgtick = 0;
3160 static char_u *lastpat = NULL;
3161 static buf_T *lbuf = NULL;
3162#ifdef FEAT_RELTIME
3163 proftime_T start;
3164#endif
3165
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003166 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003167
3168 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3169 {
3170 stat->cur = cur;
3171 stat->cnt = cnt;
3172 stat->exact_match = exact_match;
3173 stat->incomplete = incomplete;
3174 stat->last_maxcount = last_maxcount;
3175 return;
3176 }
3177 last_maxcount = maxcount;
3178
3179 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3180 || (dirc == '/' && LT_POS(p, lastpos)));
3181
3182 // If anything relevant changed the count has to be recomputed.
3183 // MB_STRNICMP ignores case, but we should not ignore case.
3184 // Unfortunately, there is no MB_STRNICMP function.
3185 // XXX: above comment should be "no MB_STRCMP function" ?
3186 if (!(chgtick == CHANGEDTICK(curbuf)
3187 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3188 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3189 && EQUAL_POS(lastpos, *cursor_pos)
3190 && lbuf == curbuf) || wraparound || cur < 0
3191 || (maxcount > 0 && cur > maxcount) || recompute)
3192 {
3193 cur = 0;
3194 cnt = 0;
3195 exact_match = FALSE;
3196 incomplete = 0;
3197 CLEAR_POS(&lastpos);
3198 lbuf = curbuf;
3199 }
3200
Christian Brabandt34a6a362023-05-06 19:20:20 +01003201 // when searching backwards and having jumped to the first occurrence,
3202 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003203 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003204 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003205 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3206 else
3207 {
3208 int done_search = FALSE;
3209 pos_T endpos = {0, 0, 0};
3210
3211 p_ws = FALSE;
3212#ifdef FEAT_RELTIME
3213 if (timeout > 0)
3214 profile_setlimit(timeout, &start);
3215#endif
3216 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3217 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3218 {
3219 done_search = TRUE;
3220#ifdef FEAT_RELTIME
3221 // Stop after passing the time limit.
3222 if (timeout > 0 && profile_passed_limit(&start))
3223 {
3224 incomplete = 1;
3225 break;
3226 }
3227#endif
3228 cnt++;
3229 if (LTOREQ_POS(lastpos, p))
3230 {
3231 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003232 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003233 exact_match = TRUE;
3234 }
3235 fast_breakcheck();
3236 if (maxcount > 0 && cnt > maxcount)
3237 {
3238 incomplete = 2; // max count exceeded
3239 break;
3240 }
3241 }
3242 if (got_int)
3243 cur = -1; // abort
3244 if (done_search)
3245 {
3246 vim_free(lastpat);
3247 lastpat = vim_strsave(spats[last_idx].pat);
3248 chgtick = CHANGEDTICK(curbuf);
3249 lbuf = curbuf;
3250 lastpos = p;
3251 }
3252 }
3253 stat->cur = cur;
3254 stat->cnt = cnt;
3255 stat->exact_match = exact_match;
3256 stat->incomplete = incomplete;
3257 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003258 p_ws = save_ws;
3259}
3260
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003262
3263/*
3264 * Get line "lnum" and copy it into "buf[LSIZE]".
3265 * The copy is made because the regexp may make the line invalid when using a
3266 * mark.
3267 */
3268 static char_u *
3269get_line_and_copy(linenr_T lnum, char_u *buf)
3270{
3271 char_u *line = ml_get(lnum);
3272
3273 vim_strncpy(buf, line, LSIZE - 1);
3274 return buf;
3275}
3276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277/*
3278 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003279 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003282find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003283 char_u *ptr, // pointer to search pattern
3284 int dir UNUSED, // direction of expansion
3285 int len, // length of search pattern
3286 int whole, // match whole words only
3287 int skip_comments, // don't match inside comments
3288 int type, // Type of search; are we looking for a type?
3289 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003290 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003291 int action, // What to do when we find it
3292 linenr_T start_lnum, // first line to start searching
Colin Kennedy21570352024-03-03 16:16:47 +01003293 linenr_T end_lnum, // last line for searching
3294 int forceit) // If true, always switch to the found path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003296 SearchedFile *files; // Stack of included files
3297 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 int max_path_depth = 50;
3299 long match_count = 1;
3300
3301 char_u *pat;
3302 char_u *new_fname;
3303 char_u *curr_fname = curbuf->b_fname;
3304 char_u *prev_fname = NULL;
3305 linenr_T lnum;
3306 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003307 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 int old_files;
3309 int already_searched;
3310 char_u *file_line;
3311 char_u *line;
3312 char_u *p;
3313 char_u save_char;
3314 int define_matched;
3315 regmatch_T regmatch;
3316 regmatch_T incl_regmatch;
3317 regmatch_T def_regmatch;
3318 int matched = FALSE;
3319 int did_show = FALSE;
3320 int found = FALSE;
3321 int i;
3322 char_u *already = NULL;
3323 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003324 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003325#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 win_T *curwin_save = NULL;
3327#endif
3328
3329 regmatch.regprog = NULL;
3330 incl_regmatch.regprog = NULL;
3331 def_regmatch.regprog = NULL;
3332
3333 file_line = alloc(LSIZE);
3334 if (file_line == NULL)
3335 return;
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003338 // when CONT_SOL is set compare "ptr" with the beginning of the
3339 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3340 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 {
3342 pat = alloc(len + 5);
3343 if (pat == NULL)
3344 goto fpip_end;
3345 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003346 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003348 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 vim_free(pat);
3350 if (regmatch.regprog == NULL)
3351 goto fpip_end;
3352 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003353 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3354 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003356 incl_regmatch.regprog = vim_regcomp(inc_opt,
3357 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (incl_regmatch.regprog == NULL)
3359 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003360 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 }
3362 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3363 {
3364 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003365 ? p_def : curbuf->b_p_def,
3366 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 if (def_regmatch.regprog == NULL)
3368 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003369 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003371 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 if (files == NULL)
3373 goto fpip_end;
3374 old_files = max_path_depth;
3375 depth = depth_displayed = -1;
3376
3377 lnum = start_lnum;
3378 if (end_lnum > curbuf->b_ml.ml_line_count)
3379 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003380 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003382 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
3384 for (;;)
3385 {
3386 if (incl_regmatch.regprog != NULL
3387 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3388 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003389 char_u *p_fname = (curr_fname == curbuf->b_fname)
3390 ? curbuf->b_ffname : curr_fname;
3391
3392 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003393 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003394 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003395 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003396 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3397 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003398 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003399 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003400 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 already_searched = FALSE;
3402 if (new_fname != NULL)
3403 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003404 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 for (i = 0;; i++)
3406 {
3407 if (i == depth + 1)
3408 i = old_files;
3409 if (i == max_path_depth)
3410 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003411 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3412 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003414 if (type != CHECK_PATH
3415 && action == ACTION_SHOW_ALL
3416 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003418 msg_putchar('\n'); // cursor below last one
3419 if (!got_int) // don't display if 'q'
3420 // typed at "--more--"
3421 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003424 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 prev_fname = NULL;
3426 }
3427 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003428 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 already_searched = TRUE;
3430 break;
3431 }
3432 }
3433 }
3434
3435 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3436 || (new_fname == NULL && !already_searched)))
3437 {
3438 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003439 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 else
3441 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003442 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003443 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003445 msg_puts_title(_("not found "));
3446 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 }
3448 did_show = TRUE;
3449 while (depth_displayed < depth && !got_int)
3450 {
3451 ++depth_displayed;
3452 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003453 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003455 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003457 if (!got_int) // don't display if 'q' typed
3458 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003461 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (new_fname != NULL)
3463 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003464 // using "new_fname" is more reliable, e.g., when
3465 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003466 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468 else
3469 {
3470 /*
3471 * Isolate the file name.
3472 * Include the surrounding "" or <> if present.
3473 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003474 if (inc_opt != NULL
3475 && strstr((char *)inc_opt, "\\zs") != NULL)
3476 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003477 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003478 p = incl_regmatch.startp[0];
3479 i = (int)(incl_regmatch.endp[0]
3480 - incl_regmatch.startp[0]);
3481 }
3482 else
3483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003484 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003485 for (p = incl_regmatch.endp[0];
3486 *p && !vim_isfilec(*p); p++)
3487 ;
3488 for (i = 0; vim_isfilec(p[i]); i++)
3489 ;
3490 }
3491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (i == 0)
3493 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003494 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003498 // Avoid checking before the start of the line, can
3499 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003500 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 if (p[-1] == '"' || p[-1] == '<')
3503 {
3504 --p;
3505 ++i;
3506 }
3507 if (p[i] == '"' || p[i] == '>')
3508 ++i;
3509 }
3510 save_char = p[i];
3511 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003512 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 p[i] = save_char;
3514 }
3515
3516 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3517 {
3518 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003519 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003521 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003524 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526
3527 if (new_fname != NULL)
3528 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003529 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 if (depth + 1 == old_files)
3531 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003532 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (bigger != NULL)
3534 {
3535 for (i = 0; i <= depth; i++)
3536 bigger[i] = files[i];
3537 for (i = depth + 1; i < old_files + max_path_depth; i++)
3538 {
3539 bigger[i].fp = NULL;
3540 bigger[i].name = NULL;
3541 bigger[i].lnum = 0;
3542 bigger[i].matched = FALSE;
3543 }
3544 for (i = old_files; i < max_path_depth; i++)
3545 bigger[i + max_path_depth] = files[i];
3546 old_files += max_path_depth;
3547 max_path_depth *= 2;
3548 vim_free(files);
3549 files = bigger;
3550 }
3551 }
3552 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3553 == NULL)
3554 vim_free(new_fname);
3555 else
3556 {
3557 if (++depth == old_files)
3558 {
3559 /*
3560 * lalloc() for 'bigger' must have failed above. We
3561 * will forget one of our already visited files now.
3562 */
3563 vim_free(files[old_files].name);
3564 ++old_files;
3565 }
3566 files[depth].name = curr_fname = new_fname;
3567 files[depth].lnum = 0;
3568 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (action == ACTION_EXPAND)
3570 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003571 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003572 vim_snprintf((char*)IObuff, IOSIZE,
3573 _("Scanning included file: %s"),
3574 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003575 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003577 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003578 {
3579 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003580 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003581 (char *)new_fname);
3582 verbose_leave();
3583 }
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586 }
3587 }
3588 else
3589 {
3590 /*
3591 * Check if the line is a define (type == FIND_DEFINE)
3592 */
3593 p = line;
3594search_line:
3595 define_matched = FALSE;
3596 if (def_regmatch.regprog != NULL
3597 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3598 {
3599 /*
3600 * Pattern must be first identifier after 'define', so skip
3601 * to that position before checking for match of pattern. Also
3602 * don't let it match beyond the end of this identifier.
3603 */
3604 p = def_regmatch.endp[0];
3605 while (*p && !vim_iswordc(*p))
3606 p++;
3607 define_matched = TRUE;
3608 }
3609
3610 /*
3611 * Look for a match. Don't do this if we are looking for a
3612 * define and this line didn't match define_prog above.
3613 */
3614 if (def_regmatch.regprog == NULL || define_matched)
3615 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003616 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003618 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 startp = skipwhite(p);
3620 if (p_ic)
3621 matched = !MB_STRNICMP(startp, ptr, len);
3622 else
3623 matched = !STRNCMP(startp, ptr, len);
3624 if (matched && define_matched && whole
3625 && vim_iswordc(startp[len]))
3626 matched = FALSE;
3627 }
3628 else if (regmatch.regprog != NULL
3629 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3630 {
3631 matched = TRUE;
3632 startp = regmatch.startp[0];
3633 /*
3634 * Check if the line is not a comment line (unless we are
3635 * looking for a define). A line starting with "# define"
3636 * is not considered to be a comment line.
3637 */
3638 if (!define_matched && skip_comments)
3639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if ((*line != '#' ||
3641 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003642 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 matched = FALSE;
3644
3645 /*
3646 * Also check for a "/ *" or "/ /" before the match.
3647 * Skips lines like "int backwards; / * normal index
3648 * * /" when looking for "normal".
3649 * Note: Doesn't skip "/ *" in comments.
3650 */
3651 p = skipwhite(line);
3652 if (matched
3653 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 for (p = line; *p && p < startp; ++p)
3655 {
3656 if (matched
3657 && p[0] == '/'
3658 && (p[1] == '*' || p[1] == '/'))
3659 {
3660 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003661 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 if (p[1] == '/')
3663 break;
3664 ++p;
3665 }
3666 else if (!matched && p[0] == '*' && p[1] == '/')
3667 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003668 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 matched = TRUE;
3670 ++p;
3671 }
3672 }
3673 }
3674 }
3675 }
3676 }
3677 if (matched)
3678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (action == ACTION_EXPAND)
3680 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003681 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 int add_r;
3683 char_u *aux;
3684
3685 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3686 break;
3687 found = TRUE;
3688 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003689 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003691 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (vim_iswordp(p))
3693 goto exit_matched;
3694 p = find_word_start(p);
3695 }
3696 p = find_word_end(p);
3697 i = (int)(p - aux);
3698
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003699 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003701 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003704 // Get the next line: when "depth" < 0 from the current
3705 // buffer, otherwise from the included file. Jump to
3706 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003707 if (depth < 0)
3708 {
3709 if (lnum >= end_lnum)
3710 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003711 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003712 }
3713 else if (vim_fgets(line = file_line,
3714 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 goto exit_matched;
3716
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003717 // we read a line, set "already" to check this "line" later
3718 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003719 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 already = aux = p = skipwhite(line);
3721 p = find_word_start(p);
3722 p = find_word_end(p);
3723 if (p > aux)
3724 {
3725 if (*aux != ')' && IObuff[i-1] != TAB)
3726 {
3727 if (IObuff[i-1] != ' ')
3728 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003729 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 if (p_js
3731 && (IObuff[i-2] == '.'
3732 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3733 && (IObuff[i-2] == '?'
3734 || IObuff[i-2] == '!'))))
3735 IObuff[i++] = ' ';
3736 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003737 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 if (p - aux >= IOSIZE - i)
3739 p = aux + IOSIZE - i - 1;
3740 STRNCPY(IObuff + i, aux, p - aux);
3741 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003742 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 }
3744 IObuff[i] = NUL;
3745 aux = IObuff;
3746
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003747 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 goto exit_matched;
3749 }
3750
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003751 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003753 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003755 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003757 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 break;
3759 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003760 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
3762 found = TRUE;
3763 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003764 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (curr_fname != prev_fname)
3766 {
3767 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003768 msg_putchar('\n'); // cursor below last one
3769 if (!got_int) // don't display if 'q' typed
3770 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 msg_home_replace_hl(curr_fname);
3772 prev_fname = curr_fname;
3773 }
3774 did_show = TRUE;
3775 if (!got_int)
3776 show_pat_in_path(line, type, TRUE, action,
3777 (depth == -1) ? NULL : files[depth].fp,
3778 (depth == -1) ? &lnum : &files[depth].lnum,
3779 match_count++);
3780
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003781 // Set matched flag for this file and all the ones that
3782 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 for (i = 0; i <= depth; ++i)
3784 files[i].matched = TRUE;
3785 }
3786 else if (--count <= 0)
3787 {
3788 found = TRUE;
3789 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003790#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 && g_do_tagpreview == 0
3792#endif
3793 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003794 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 else if (action == ACTION_SHOW)
3796 {
3797 show_pat_in_path(line, type, did_show, action,
3798 (depth == -1) ? NULL : files[depth].fp,
3799 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3800 did_show = TRUE;
3801 }
3802 else
3803 {
3804#ifdef FEAT_GUI
3805 need_mouse_correct = TRUE;
3806#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003807#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003808 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (g_do_tagpreview != 0)
3810 {
3811 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003812 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814#endif
3815 if (action == ACTION_SPLIT)
3816 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003819 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 if (depth == -1)
3822 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003823 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003824#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (g_do_tagpreview != 0)
3826 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003827 if (!win_valid(curwin_save))
3828 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003829 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003830 curwin_save->w_buffer->b_fnum, NULL,
Colin Kennedy21570352024-03-03 16:16:47 +01003831 NULL, TRUE, lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003832 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834 else
3835#endif
3836 setpcmark();
3837 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003838 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840 else
3841 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003842 if (!GETFILE_SUCCESS(getfile(
3843 0, files[depth].name, NULL, TRUE,
Colin Kennedy21570352024-03-03 16:16:47 +01003844 files[depth].lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003845 break; // failed to jump to file
3846 // autocommands may have changed the lnum, we don't
3847 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 curwin->w_cursor.lnum = files[depth].lnum;
3849 }
3850 }
3851 if (action != ACTION_SHOW)
3852 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003853 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 curwin->w_set_curswant = TRUE;
3855 }
3856
Bram Moolenaar4033c552017-09-16 20:54:51 +02003857#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003859 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003861 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003863 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 win_enter(curwin_save, TRUE);
3865 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003866# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003867 else if (WIN_IS_POPUP(curwin))
3868 // can't keep focus in popup window
3869 win_enter(firstwin, TRUE);
3870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871#endif
3872 break;
3873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003876 // look for other matches in the rest of the line if we
3877 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003880 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003881 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003882 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 goto search_line;
3884 }
3885 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003887 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003888 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 break;
3890
3891 /*
3892 * Read the next line. When reading an included file and encountering
3893 * end-of-file, close the file and continue in the file that included
3894 * it.
3895 */
3896 while (depth >= 0 && !already
3897 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3898 {
3899 fclose(files[depth].fp);
3900 --old_files;
3901 files[old_files].name = files[depth].name;
3902 files[old_files].matched = files[depth].matched;
3903 --depth;
3904 curr_fname = (depth == -1) ? curbuf->b_fname
3905 : files[depth].name;
3906 if (depth < depth_displayed)
3907 depth_displayed = depth;
3908 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003909 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003912 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003913 i = (int)STRLEN(line);
3914 if (i > 0 && line[i - 1] == '\n')
3915 line[--i] = NUL;
3916 if (i > 0 && line[i - 1] == '\r')
3917 line[--i] = NUL;
3918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 else if (!already)
3920 {
3921 if (++lnum > end_lnum)
3922 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003923 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
3925 already = NULL;
3926 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003927 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003929 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 for (i = 0; i <= depth; i++)
3931 {
3932 fclose(files[i].fp);
3933 vim_free(files[i].name);
3934 }
3935 for (i = old_files; i < max_path_depth; i++)
3936 vim_free(files[i].name);
3937 vim_free(files);
3938
3939 if (type == CHECK_PATH)
3940 {
3941 if (!did_show)
3942 {
3943 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003944 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003946 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 }
3948 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003949 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003951 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003952 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003954 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003956 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
3959 msg_end();
3960
3961fpip_end:
3962 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02003963 vim_regfree(regmatch.regprog);
3964 vim_regfree(incl_regmatch.regprog);
3965 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966}
3967
3968 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003969show_pat_in_path(
3970 char_u *line,
3971 int type,
3972 int did_show,
3973 int action,
3974 FILE *fp,
3975 linenr_T *lnum,
3976 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 char_u *p;
3979
3980 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003981 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00003982 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003983 gotocmdline(TRUE); // cursor at status line
3984 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 return;
3986 for (;;)
3987 {
3988 p = line + STRLEN(line) - 1;
3989 if (fp != NULL)
3990 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003991 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (p >= line && *p == '\n')
3993 --p;
3994 if (p >= line && *p == '\r')
3995 --p;
3996 *(p + 1) = NUL;
3997 }
3998 if (action == ACTION_SHOW_ALL)
3999 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004000 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004001 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004002 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4003 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004004 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4005 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004007 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004008 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004010 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4012 break;
4013
4014 if (fp != NULL)
4015 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004016 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 break;
4018 ++*lnum;
4019 }
4020 else
4021 {
4022 if (++*lnum > curbuf->b_ml.ml_line_count)
4023 break;
4024 line = ml_get(*lnum);
4025 }
4026 msg_putchar('\n');
4027 }
4028}
4029#endif
4030
4031#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004032/*
4033 * Return the last used search pattern at "idx".
4034 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004035 spat_T *
4036get_spat(int idx)
4037{
4038 return &spats[idx];
4039}
4040
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004041/*
4042 * Return the last used search pattern index.
4043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004045get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004047 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004050
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004051#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004052/*
4053 * "searchcount()" function
4054 */
4055 void
4056f_searchcount(typval_T *argvars, typval_T *rettv)
4057{
4058 pos_T pos = curwin->w_cursor;
4059 char_u *pattern = NULL;
4060 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4061 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004062 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004063 searchstat_T stat;
4064
4065 if (rettv_dict_alloc(rettv) == FAIL)
4066 return;
4067
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004068 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4069 return;
4070
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004071 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4072 recompute = TRUE;
4073
4074 if (argvars[0].v_type != VAR_UNKNOWN)
4075 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004076 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004077 dictitem_T *di;
4078 listitem_T *li;
4079 int error = FALSE;
4080
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004081 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004082 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004083 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004084 di = dict_find(dict, (char_u *)"timeout", -1);
4085 if (di != NULL)
4086 {
4087 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4088 if (error)
4089 return;
4090 }
4091 di = dict_find(dict, (char_u *)"maxcount", -1);
4092 if (di != NULL)
4093 {
4094 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4095 if (error)
4096 return;
4097 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004098 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004099 di = dict_find(dict, (char_u *)"pattern", -1);
4100 if (di != NULL)
4101 {
4102 pattern = tv_get_string_chk(&di->di_tv);
4103 if (pattern == NULL)
4104 return;
4105 }
4106 di = dict_find(dict, (char_u *)"pos", -1);
4107 if (di != NULL)
4108 {
4109 if (di->di_tv.v_type != VAR_LIST)
4110 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004111 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004112 return;
4113 }
4114 if (list_len(di->di_tv.vval.v_list) != 3)
4115 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004116 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004117 return;
4118 }
4119 li = list_find(di->di_tv.vval.v_list, 0L);
4120 if (li != NULL)
4121 {
4122 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4123 if (error)
4124 return;
4125 }
4126 li = list_find(di->di_tv.vval.v_list, 1L);
4127 if (li != NULL)
4128 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004129 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004130 if (error)
4131 return;
4132 }
4133 li = list_find(di->di_tv.vval.v_list, 2L);
4134 if (li != NULL)
4135 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004136 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004137 if (error)
4138 return;
4139 }
4140 }
4141 }
4142
4143 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004144#ifdef FEAT_SEARCH_EXTRA
4145 save_incsearch_state();
4146#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004147 if (pattern != NULL)
4148 {
4149 if (*pattern == NUL)
4150 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004151 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004152 spats[last_idx].pat = vim_strsave(pattern);
4153 }
4154 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4155 goto the_end; // the previous pattern was never defined
4156
4157 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4158
4159 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4160 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4161 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4162 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4163 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4164
4165the_end:
4166 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004167#ifdef FEAT_SEARCH_EXTRA
4168 restore_incsearch_state();
4169#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004170}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004171#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004172
4173/*
4174 * Fuzzy string matching
4175 *
4176 * Ported from the lib_fts library authored by Forrest Smith.
4177 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4178 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004179 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004180 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4181 *
4182 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004183 * - Matched letter
4184 * - Unmatched letter
4185 * - Consecutively matched letters
4186 * - Proximity to start
4187 * - Letter following a separator (space, underscore)
4188 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004189 *
4190 * Matched letters are good. Unmatched letters are bad. Matching near the start
4191 * is good. Matching the first letter in the middle of a phrase is good.
4192 * Matching the uppercase letters in camel case entries is good.
4193 *
4194 * The score assigned for each factor is explained below.
4195 * File paths are different from file names. File extensions may be ignorable.
4196 * Single words care about consecutive matches but not separators or camel
4197 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004198 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004199 * Matched letter: +0 points
4200 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004201 * Consecutive match bonus: +15 points
4202 * First letter bonus: +15 points
4203 * Separator bonus: +30 points
4204 * Camel case bonus: +30 points
4205 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004206 *
4207 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004208 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004209 * lower minimum score due to unmatched letter penalty. Longer search patterns
4210 * have a higher maximum score due to match bonuses.
4211 *
4212 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4213 * quite a bit.
4214 *
4215 * There is a penalty if you DON’T match the first three letters. Which
4216 * effectively rewards matching near the start. However there’s no difference
4217 * in matching between the middle and end.
4218 *
4219 * There is not an explicit bonus for an exact match. Unmatched letters receive
4220 * a penalty. So shorter strings and closer matches are worth more.
4221 */
4222typedef struct
4223{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004224 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004225 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004226 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004227 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004228} fuzzyItem_T;
4229
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004230// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4231// matching a whole word is preferred.
4232#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004233// bonus if match occurs after a path separator
4234#define PATH_SEPARATOR_BONUS 30
4235// bonus if match occurs after a word separator
4236#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004237// bonus if match is uppercase and prev is lower
4238#define CAMEL_BONUS 30
4239// bonus if the first letter is matched
4240#define FIRST_LETTER_BONUS 15
4241// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004242#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004243// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004244#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004245// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004246#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004247// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004248#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004249// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004250#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004251
4252#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004253
4254/*
4255 * Compute a score for a fuzzy matched string. The matching character locations
4256 * are in 'matches'.
4257 */
4258 static int
4259fuzzy_match_compute_score(
4260 char_u *str,
4261 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004262 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004263 int numMatches)
4264{
4265 int score;
4266 int penalty;
4267 int unmatched;
4268 int i;
4269 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004270 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004271
4272 // Initialize score
4273 score = 100;
4274
4275 // Apply leading letter penalty
4276 penalty = LEADING_LETTER_PENALTY * matches[0];
4277 if (penalty < MAX_LEADING_LETTER_PENALTY)
4278 penalty = MAX_LEADING_LETTER_PENALTY;
4279 score += penalty;
4280
4281 // Apply unmatched penalty
4282 unmatched = strSz - numMatches;
4283 score += UNMATCHED_LETTER_PENALTY * unmatched;
4284
4285 // Apply ordering bonuses
4286 for (i = 0; i < numMatches; ++i)
4287 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004288 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004289
4290 if (i > 0)
4291 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004292 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004293
4294 // Sequential
4295 if (currIdx == (prevIdx + 1))
4296 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004297 else
4298 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004299 }
4300
4301 // Check for bonuses based on neighbor character value
4302 if (currIdx > 0)
4303 {
4304 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004305 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004306 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004307
4308 if (has_mbyte)
4309 {
4310 while (sidx < currIdx)
4311 {
4312 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004313 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004314 sidx++;
4315 }
4316 curr = (*mb_ptr2char)(p);
4317 }
4318 else
4319 {
4320 neighbor = str[currIdx - 1];
4321 curr = str[currIdx];
4322 }
4323
4324 if (vim_islower(neighbor) && vim_isupper(curr))
4325 score += CAMEL_BONUS;
4326
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004327 // Bonus if the match follows a separator character
4328 if (neighbor == '/' || neighbor == '\\')
4329 score += PATH_SEPARATOR_BONUS;
4330 else if (neighbor == ' ' || neighbor == '_')
4331 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004332 }
4333 else
4334 {
4335 // First letter
4336 score += FIRST_LETTER_BONUS;
4337 }
4338 }
4339 return score;
4340}
4341
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004342/*
4343 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4344 * Return the number of matching characters.
4345 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004346 static int
4347fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004348 char_u *fuzpat,
4349 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004350 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004351 int *outScore,
4352 char_u *strBegin,
4353 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004354 int_u *srcMatches,
4355 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004356 int maxMatches,
4357 int nextMatch,
4358 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004359{
4360 // Recursion params
4361 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004362 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004363 int bestRecursiveScore = 0;
4364 int first_match;
4365 int matched;
4366
4367 // Count recursions
4368 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004369 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004370 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004371
4372 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004373 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004374 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004375
4376 // Loop through fuzpat and str looking for a match
4377 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004378 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004379 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004380 int c1;
4381 int c2;
4382
4383 c1 = PTR2CHAR(fuzpat);
4384 c2 = PTR2CHAR(str);
4385
Bram Moolenaar635414d2020-09-11 22:25:15 +02004386 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004387 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004388 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004389 // Supplied matches buffer was too short
4390 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004391 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004392
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004393 int recursiveScore = 0;
4394 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4395 CLEAR_FIELD(recursiveMatches);
4396
Bram Moolenaar635414d2020-09-11 22:25:15 +02004397 // "Copy-on-Write" srcMatches into matches
4398 if (first_match && srcMatches)
4399 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004400 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004401 first_match = FALSE;
4402 }
4403
4404 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004405 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004406 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4407 &recursiveScore, strBegin, strLen, matches,
4408 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004409 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004410 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004411 {
4412 // Pick best recursive score
4413 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4414 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004415 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004416 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004417 bestRecursiveScore = recursiveScore;
4418 }
4419 recursiveMatch = TRUE;
4420 }
4421
4422 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004423 matches[nextMatch++] = strIdx;
4424 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004425 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004426 else
4427 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004428 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004429 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004430 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004431 else
4432 ++str;
4433 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004434 }
4435
4436 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004437 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004438
4439 // Calculate score
4440 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004441 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4442 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004443
4444 // Return best result
4445 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4446 {
4447 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004448 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004449 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004450 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004451 }
4452 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004453 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004454
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004455 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004456}
4457
4458/*
4459 * fuzzy_match()
4460 *
4461 * Performs exhaustive search via recursion to find all possible matches and
4462 * match with highest score.
4463 * Scores values have no intrinsic meaning. Possible score range is not
4464 * normalized and varies with pattern.
4465 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004466 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004467 * Uses char_u for match indices. Therefore patterns are limited to
4468 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004469 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004470 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4471 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004472 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004473 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004474fuzzy_match(
4475 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004476 char_u *pat_arg,
4477 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004478 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004479 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004480 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004481{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004482 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004483 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004484 char_u *save_pat;
4485 char_u *pat;
4486 char_u *p;
4487 int complete = FALSE;
4488 int score = 0;
4489 int numMatches = 0;
4490 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004491
4492 *outScore = 0;
4493
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004494 save_pat = vim_strsave(pat_arg);
4495 if (save_pat == NULL)
4496 return FALSE;
4497 pat = save_pat;
4498 p = pat;
4499
4500 // Try matching each word in 'pat_arg' in 'str'
4501 while (TRUE)
4502 {
4503 if (matchseq)
4504 complete = TRUE;
4505 else
4506 {
4507 // Extract one word from the pattern (separated by space)
4508 p = skipwhite(p);
4509 if (*p == NUL)
4510 break;
4511 pat = p;
4512 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4513 {
4514 if (has_mbyte)
4515 MB_PTR_ADV(p);
4516 else
4517 ++p;
4518 }
4519 if (*p == NUL) // processed all the words
4520 complete = TRUE;
4521 *p = NUL;
4522 }
4523
4524 score = 0;
4525 recursionCount = 0;
4526 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4527 matches + numMatches, maxMatches - numMatches,
4528 0, &recursionCount);
4529 if (matchCount == 0)
4530 {
4531 numMatches = 0;
4532 break;
4533 }
4534
4535 // Accumulate the match score and the number of matches
4536 *outScore += score;
4537 numMatches += matchCount;
4538
4539 if (complete)
4540 break;
4541
4542 // try matching the next word
4543 ++p;
4544 }
4545
4546 vim_free(save_pat);
4547 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004548}
4549
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004550#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004551/*
4552 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004553 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004554 */
4555 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004556fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004557{
4558 int v1 = ((fuzzyItem_T *)s1)->score;
4559 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004560 int idx1 = ((fuzzyItem_T *)s1)->idx;
4561 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004562
zeertzjq77078272024-02-10 13:24:03 +01004563 if (v1 == v2)
4564 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4565 else
4566 return v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004567}
4568
4569/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004570 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4571 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004572 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4573 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004574 * If 'items' is a list of strings, then search for 'str' in the list.
4575 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4576 * for each item or use 'item_cb' Funcref function to get the string.
4577 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4578 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004579 */
4580 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004581fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004582 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004583 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004584 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004585 char_u *key,
4586 callback_T *item_cb,
4587 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004588 list_T *fmatchlist,
4589 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004590{
4591 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004592 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004593 listitem_T *li;
4594 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004595 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004596 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004597
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004598 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004599 if (len == 0)
4600 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004601 if (max_matches > 0 && len > max_matches)
4602 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004603
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004604 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4605 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004606 return;
4607
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004608 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004609 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004610 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004611 int score;
4612 char_u *itemstr;
4613 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004614
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004615 if (max_matches > 0 && match_count >= max_matches)
4616 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004617
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004618 itemstr = NULL;
4619 rettv.v_type = VAR_UNKNOWN;
4620 if (li->li_tv.v_type == VAR_STRING) // list of strings
4621 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004622 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004623 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004624 {
4625 // For a dict, either use the specified key to lookup the string or
4626 // use the specified callback function to get the string.
4627 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004628 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4629 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004630 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004631 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004632 typval_T argv[2];
4633
4634 // Invoke the supplied callback (if any) to get the dict item
4635 li->li_tv.vval.v_dict->dv_refcount++;
4636 argv[0].v_type = VAR_DICT;
4637 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4638 argv[1].v_type = VAR_UNKNOWN;
4639 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4640 {
4641 if (rettv.v_type == VAR_STRING)
4642 itemstr = rettv.vval.v_string;
4643 }
4644 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004645 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004646 }
4647
4648 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004649 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004650 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004651 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004652 items[match_count].idx = match_count;
4653 items[match_count].item = li;
4654 items[match_count].score = score;
4655
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004656 // Copy the list of matching positions in itemstr to a list, if
4657 // 'retmatchpos' is set.
4658 if (retmatchpos)
4659 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004660 int j = 0;
4661 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004662
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004663 items[match_count].lmatchpos = list_alloc();
4664 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004665 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004666
4667 p = str;
4668 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004669 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004670 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004671 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004672 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004673 matches[j]) == FAIL)
4674 goto done;
4675 j++;
4676 }
4677 if (has_mbyte)
4678 MB_PTR_ADV(p);
4679 else
4680 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004681 }
4682 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004683 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004684 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004685 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004686 }
4687
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004688 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004689 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004690 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004691
Bram Moolenaar635414d2020-09-11 22:25:15 +02004692 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004693 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004694 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004695
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004696 // For matchfuzzy(), return a list of matched strings.
4697 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004698 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004699 // The first item is a list of matched strings. The second item
4700 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004701 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004702 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4703 if (retmatchpos)
4704 {
4705 li = list_find(fmatchlist, 0);
4706 if (li == NULL || li->li_tv.vval.v_list == NULL)
4707 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004708 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004709 }
4710 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004711 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004712
4713 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004714 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004715 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004716 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004717 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004718 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004719 }
4720
4721 // next copy the list of matching positions
4722 if (retmatchpos)
4723 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004724 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004725 if (li == NULL || li->li_tv.vval.v_list == NULL)
4726 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004727 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004728
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004729 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004730 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004731 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004732 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004733 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004734 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004735 goto done;
4736 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004737
4738 // copy the matching scores
4739 li = list_find(fmatchlist, -1);
4740 if (li == NULL || li->li_tv.vval.v_list == NULL)
4741 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004742 retlist = li->li_tv.vval.v_list;
4743 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004744 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004745 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004746 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004747 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004748 goto done;
4749 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004750 }
4751 }
4752
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004753done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004754 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004755}
4756
4757/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004758 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4759 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4760 */
4761 static void
4762do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4763{
4764 callback_T cb;
4765 char_u *key = NULL;
4766 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004767 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004768 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004769
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004770 if (in_vim9script()
4771 && (check_for_list_arg(argvars, 0) == FAIL
4772 || check_for_string_arg(argvars, 1) == FAIL
4773 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4774 return;
4775
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004776 CLEAR_POINTER(&cb);
4777
4778 // validate and get the arguments
4779 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4780 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004781 semsg(_(e_argument_of_str_must_be_list),
4782 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004783 return;
4784 }
4785 if (argvars[1].v_type != VAR_STRING
4786 || argvars[1].vval.v_string == NULL)
4787 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004788 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004789 return;
4790 }
4791
4792 if (argvars[2].v_type != VAR_UNKNOWN)
4793 {
4794 dict_T *d;
4795 dictitem_T *di;
4796
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004797 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004798 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004799
4800 // To search a dict, either a callback function or a key can be
4801 // specified.
4802 d = argvars[2].vval.v_dict;
4803 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4804 {
4805 if (di->di_tv.v_type != VAR_STRING
4806 || di->di_tv.vval.v_string == NULL
4807 || *di->di_tv.vval.v_string == NUL)
4808 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004809 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004810 return;
4811 }
4812 key = tv_get_string(&di->di_tv);
4813 }
4814 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4815 {
4816 cb = get_callback(&di->di_tv);
4817 if (cb.cb_name == NULL)
4818 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004819 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004820 return;
4821 }
4822 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004823
4824 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004825 {
4826 if (di->di_tv.v_type != VAR_NUMBER)
4827 {
4828 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4829 return;
4830 }
4831 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4832 }
4833
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004834 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004835 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004836 }
4837
4838 // get the fuzzy matches
4839 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004840 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004841 goto done;
4842 if (retmatchpos)
4843 {
4844 list_T *l;
4845
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004846 // For matchfuzzypos(), a list with three items are returned. First
4847 // item is a list of matching strings, the second item is a list of
4848 // lists with matching positions within each string and the third item
4849 // is the list of scores of the matches.
4850 l = list_alloc();
4851 if (l == NULL)
4852 goto done;
4853 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004854 {
4855 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004856 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004857 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004858 l = list_alloc();
4859 if (l == NULL)
4860 goto done;
4861 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004862 {
4863 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004864 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004865 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004866 l = list_alloc();
4867 if (l == NULL)
4868 goto done;
4869 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004870 {
4871 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004872 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004873 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004874 }
4875
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004876 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004877 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004878
4879done:
4880 free_callback(&cb);
4881}
4882
4883/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004884 * "matchfuzzy()" function
4885 */
4886 void
4887f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4888{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004889 do_fuzzymatch(argvars, rettv, FALSE);
4890}
4891
4892/*
4893 * "matchfuzzypos()" function
4894 */
4895 void
4896f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
4897{
4898 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004899}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004900#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004901
4902/*
4903 * Same as fuzzy_match_item_compare() except for use with a string match
4904 */
4905 static int
4906fuzzy_match_str_compare(const void *s1, const void *s2)
4907{
4908 int v1 = ((fuzmatch_str_T *)s1)->score;
4909 int v2 = ((fuzmatch_str_T *)s2)->score;
4910 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4911 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4912
Christian Brabandte06e4372024-02-09 19:39:14 +01004913 if (v1 == v2)
4914 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4915 else
4916 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004917}
4918
4919/*
4920 * Sort fuzzy matches by score
4921 */
4922 static void
4923fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
4924{
4925 // Sort the list by the descending order of the match score
4926 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4927 fuzzy_match_str_compare);
4928}
4929
4930/*
4931 * Same as fuzzy_match_item_compare() except for use with a function name
4932 * string match. <SNR> functions should be sorted to the end.
4933 */
4934 static int
4935fuzzy_match_func_compare(const void *s1, const void *s2)
4936{
4937 int v1 = ((fuzmatch_str_T *)s1)->score;
4938 int v2 = ((fuzmatch_str_T *)s2)->score;
4939 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4940 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4941 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
4942 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
4943
Christian Brabandte06e4372024-02-09 19:39:14 +01004944 if (*str1 != '<' && *str2 == '<')
4945 return -1;
4946 if (*str1 == '<' && *str2 != '<')
4947 return 1;
4948 if (v1 == v2)
4949 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4950 else
4951 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004952}
4953
4954/*
4955 * Sort fuzzy matches of function names by score.
4956 * <SNR> functions should be sorted to the end.
4957 */
4958 static void
4959fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
4960{
4961 // Sort the list by the descending order of the match score
4962 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4963 fuzzy_match_func_compare);
4964}
4965
4966/*
4967 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
4968 * returns the match score.
4969 */
4970 int
4971fuzzy_match_str(char_u *str, char_u *pat)
4972{
4973 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00004974 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004975
4976 if (str == NULL || pat == NULL)
4977 return 0;
4978
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00004979 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004980 sizeof(matchpos) / sizeof(matchpos[0]));
4981
4982 return score;
4983}
4984
4985/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01004986 * Free an array of fuzzy string matches "fuzmatch[count]".
4987 */
4988 void
4989fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
4990{
4991 int i;
4992
4993 if (fuzmatch == NULL)
4994 return;
4995 for (i = 0; i < count; ++i)
4996 vim_free(fuzmatch[i].str);
4997 vim_free(fuzmatch);
4998}
4999
5000/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005001 * Copy a list of fuzzy matches into a string list after sorting the matches by
5002 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5003 * Returns OK on success and FAIL on memory allocation failure.
5004 */
5005 int
5006fuzzymatches_to_strmatches(
5007 fuzmatch_str_T *fuzmatch,
5008 char_u ***matches,
5009 int count,
5010 int funcsort)
5011{
5012 int i;
5013
5014 if (count <= 0)
5015 return OK;
5016
5017 *matches = ALLOC_MULT(char_u *, count);
5018 if (*matches == NULL)
5019 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005020 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005021 return FAIL;
5022 }
5023
5024 // Sort the list by the descending order of the match score
5025 if (funcsort)
5026 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5027 else
5028 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5029
5030 for (i = 0; i < count; i++)
5031 (*matches)[i] = fuzmatch[i].str;
5032 vim_free(fuzmatch);
5033
5034 return OK;
5035}