blob: 83aaf0ac313f8569850acd49e74bf812d4842226 [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);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100663 if ((int)STRLEN(ptr) <= 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;
969 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
970 pos->lnum, FALSE));
971 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000972 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000973 else
974 {
975 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000976 if (has_mbyte
977 && pos->lnum <= buf->b_ml.ml_line_count)
978 {
979 ptr = ml_get_buf(buf, pos->lnum, FALSE);
980 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
981 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000982 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100983 if (end_pos != NULL)
984 {
985 end_pos->lnum = lnum + matchpos.lnum;
986 end_pos->col = matchpos.col;
987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989 else
990 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000991 pos->lnum = lnum + matchpos.lnum;
992 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100993 if (end_pos != NULL)
994 {
995 end_pos->lnum = lnum + endpos.lnum;
996 end_pos->col = endpos.col;
997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001000 if (end_pos != NULL)
1001 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001003 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001005 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001006 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 search_match_endcol = endpos.col;
1008 break;
1009 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001010 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 if (got_int)
1012 break;
1013
1014#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001015 // Cancel searching if a character was typed. Used for
1016 // 'incsearch'. Don't check too often, that would slowdown
1017 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 if ((options & SEARCH_PEEK)
1019 && ((lnum - pos->lnum) & 0x3f) == 0
1020 && char_avail())
1021 {
1022 break_loop = TRUE;
1023 break;
1024 }
1025#endif
1026
1027 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001028 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 at_first_line = FALSE;
1031
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001032 // vim_regexec_multi() may clear "regprog"
1033 if (regmatch.regprog == NULL)
1034 break;
1035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001037 * Stop the search if wrapscan isn't set, "stop_lnum" is
1038 * specified, after an interrupt, after a match and after looping
1039 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001041 if (!p_ws || stop_lnum != 0 || got_int
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001042 || called_emsg > called_emsg_before || *timed_out
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001043#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001044 || break_loop
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001045#endif
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001046 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 break;
1048
1049 /*
1050 * If 'wrapscan' is set we continue at the other end of the file.
Christian Brabandt34a6a362023-05-06 19:20:20 +01001051 * If 'shortmess' does not contain 's', we give a message, but
1052 * only, if we won't show the search stat later anyhow,
1053 * (so SEARCH_COUNT must be absent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 * This message is also remembered in keep_msg for when the screen
1055 * is redrawn. The keep_msg is cleared whenever another message is
1056 * written.
1057 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001058 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 lnum = 1;
Christian Brabandt34a6a362023-05-06 19:20:20 +01001062 if (!shortmess(SHM_SEARCH)
1063 && shortmess(SHM_SEARCHCOUNT)
1064 && (options & SEARCH_MSG))
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001065 give_warning((char_u *)_(dir == BACKWARD
1066 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001067 if (extra_arg != NULL)
1068 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
Paul Ollis65745772022-06-05 16:55:54 +01001070 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001071#ifdef FEAT_SEARCH_EXTRA
1072 || break_loop
1073#endif
1074 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 break;
1076 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001077 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001079#ifdef FEAT_RELTIME
1080 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1081 disable_regexp_timeout();
1082#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001083 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001085 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
1087 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001088 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1090 {
1091 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001092 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001094 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001096 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 return FAIL;
1099 }
1100
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001101 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001102 if (pos->lnum > buf->b_ml.ml_line_count)
1103 {
1104 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001105 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001106 if (pos->col > 0)
1107 --pos->col;
1108 }
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 return submatch + 1;
1111}
1112
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001113#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001114 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001115set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001116{
1117 spats[0].off.dir = cdir;
1118}
1119
1120 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001121set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001122{
1123 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1124}
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126/*
1127 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001128 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
1130 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001131first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
1133 int submatch;
1134
1135 for (submatch = 1; ; ++submatch)
1136 {
1137 if (rp->startpos[submatch].lnum >= 0)
1138 break;
1139 if (submatch == 9)
1140 {
1141 submatch = 0;
1142 break;
1143 }
1144 }
1145 return submatch;
1146}
1147#endif
1148
1149/*
1150 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001151 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * If 'dirc' is 0: use previous dir.
1153 * If 'pat' is NULL or empty : use previous string.
1154 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1155 * If 'options & SEARCH_ECHO': echo the search command and handle options
1156 * If 'options & SEARCH_MSG' : may give error message
1157 * If 'options & SEARCH_OPT' : interpret optional flags
1158 * If 'options & SEARCH_HIS' : put search pattern in history
1159 * If 'options & SEARCH_NOOF': don't add offset to position
1160 * If 'options & SEARCH_MARK': set previous context mark
1161 * If 'options & SEARCH_KEEP': keep previous search pattern
1162 * If 'options & SEARCH_START': accept match at curpos itself
1163 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1164 *
1165 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1166 * makes the movement linewise without moving the match position.
1167 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001168 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 */
1170 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001171do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001172 oparg_T *oap, // can be NULL
1173 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001174 int search_delim, // the delimiter for the search, e.g. '%' in
1175 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001176 char_u *pat,
1177 long count,
1178 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001179 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001181 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001183 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001184 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 char_u *p;
1186 long c;
1187 char_u *dircp;
1188 char_u *strcopy = NULL;
1189 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001190 char_u *msgbuf = NULL;
1191 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001192 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
1194 /*
1195 * A line offset is not remembered, this is vi compatible.
1196 */
1197 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1198 {
1199 spats[0].off.line = FALSE;
1200 spats[0].off.off = 0;
1201 }
1202
1203 /*
1204 * Save the values for when (options & SEARCH_KEEP) is used.
1205 * (there is no "if ()" around this because gcc wants them initialized)
1206 */
1207 old_off = spats[0].off;
1208
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001209 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210
1211 /*
1212 * Find out the direction of the search.
1213 */
1214 if (dirc == 0)
1215 dirc = spats[0].off.dir;
1216 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001219#if defined(FEAT_EVAL)
1220 set_vv_searchforward();
1221#endif
1222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (options & SEARCH_REV)
1224 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001225#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001226 // There is a bug in the Visual C++ 2.2 compiler which means that
1227 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 dirc = (dirc == '/') ? '?' : '/';
1229#else
1230 if (dirc == '/')
1231 dirc = '?';
1232 else
1233 dirc = '/';
1234#endif
1235 }
1236
1237#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001238 // If the cursor is in a closed fold, don't find another match in the same
1239 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 if (dirc == '/')
1241 {
1242 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001243 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 }
1245 else
1246 {
1247 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1248 pos.col = 0;
1249 }
1250#endif
1251
1252#ifdef FEAT_SEARCH_EXTRA
1253 /*
1254 * Turn 'hlsearch' highlighting back on.
1255 */
1256 if (no_hlsearch && !(options & SEARCH_KEEP))
1257 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001258 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001259 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261#endif
1262
1263 /*
1264 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1265 */
1266 for (;;)
1267 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001268 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 searchstr = pat;
1271 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001272 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001273 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001275 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001277 searchstr = spats[RE_SUBST].pat;
1278 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001279 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001280 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001281 retval = 0;
1282 goto end_do_search;
1283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001285 else
1286 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001287 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001288 searchstr = (char_u *)"";
1289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001292 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 {
1294 /*
1295 * Find end of regular expression.
1296 * If there is a matching '/' or '?', toss it.
1297 */
1298 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001299 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001300 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (strcopy != ps)
1302 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001303 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001304 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 pat = strcopy;
1306 searchstr = strcopy;
1307 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001308 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001310 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 *p++ = NUL;
1312 }
1313 spats[0].off.line = FALSE;
1314 spats[0].off.end = FALSE;
1315 spats[0].off.off = 0;
1316 /*
1317 * Check for a line offset or a character offset.
1318 * For get_address (echo off) we don't check for a character
1319 * offset, because it is meaningless and the 's' could be a
1320 * substitute command.
1321 */
1322 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1323 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001324 else if ((options & SEARCH_OPT)
1325 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001327 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 spats[0].off.end = SEARCH_END;
1329 ++p;
1330 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001331 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001333 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1335 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001336 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001338 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 spats[0].off.off = 1;
1340 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001341 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 ++p;
1343 }
1344
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001345 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 searchcmdlen += (int)(p - pat);
1347
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001348 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 }
1350
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001351 if ((options & SEARCH_ECHO) && messaging()
1352 && !msg_silent
1353 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001356 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001357 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001359 // Compute msg_row early.
1360 msg_start();
1361
Bram Moolenaar984f0312019-05-24 13:11:47 +02001362 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001363 if (!cmd_silent &&
1364 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001365 {
1366 p = off_buf;
1367 *p++ = dirc;
1368 if (spats[0].off.end)
1369 *p++ = 'e';
1370 else if (!spats[0].off.line)
1371 *p++ = 's';
1372 if (spats[0].off.off > 0 || spats[0].off.line)
1373 *p++ = '+';
1374 *p = NUL;
1375 if (spats[0].off.off != 0 || spats[0].off.line)
1376 sprintf((char *)p, "%ld", spats[0].off.off);
1377 off_len = STRLEN(off_buf);
1378 }
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001381 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 else
1383 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001384
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001385 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001386 {
1387 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001388 // search stat. Use all the space available, so that the
1389 // search state is right aligned. If there is not enough space
1390 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001391 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001392 // Use all the columns.
1393 len = (int)(Rows - msg_row) * Columns - 1;
1394 else
1395 // Use up to 'showcmd' column.
1396 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001397 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1398 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001399 }
1400 else
1401 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001402 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001403
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001404 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001405 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (msgbuf != NULL)
1407 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001408 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001409 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001410 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1411 // empty for the search_stat feature.
1412 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001413 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001414 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001416 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1417 {
1418 // Use a space to draw the composing char on.
1419 msgbuf[1] = ' ';
1420 mch_memmove(msgbuf + 2, p, STRLEN(p));
1421 }
1422 else
1423 mch_memmove(msgbuf + 1, p, STRLEN(p));
1424 if (off_len > 0)
1425 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001427 trunc = msg_strtrunc(msgbuf, TRUE);
1428 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001430 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001431 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001434#ifdef FEAT_RIGHTLEFT
1435 // The search pattern could be shown on the right in
1436 // rightleft mode, but the 'ruler' and 'showcmd' area use
1437 // it too, thus it would be blanked out again very soon.
1438 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001439 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1440 {
1441 char_u *r;
1442 size_t pat_len;
1443
1444 r = reverse_text(msgbuf);
1445 if (r != NULL)
1446 {
1447 vim_free(msgbuf);
1448 msgbuf = r;
1449 // move reversed text to beginning of buffer
1450 while (*r != NUL && *r == ' ')
1451 r++;
1452 pat_len = msgbuf + STRLEN(msgbuf) - r;
1453 mch_memmove(msgbuf, r, pat_len);
1454 // overwrite old text
1455 if ((size_t)(r - msgbuf) >= pat_len)
1456 vim_memset(r, ' ', pat_len);
1457 else
1458 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1459 }
1460 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001461#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001462 msg_outtrans(msgbuf);
1463 msg_clr_eos();
1464 msg_check();
1465
1466 gotocmdline(FALSE);
1467 out_flush();
1468 msg_nowait = TRUE; // don't wait for this message
1469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471 }
1472
1473 /*
1474 * If there is a character offset, subtract it from the current
1475 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001476 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 * This is not done for a line offset, because then we would not be vi
1478 * compatible.
1479 */
Bram Moolenaared203462004-06-16 11:19:22 +00001480 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 if (spats[0].off.off > 0)
1483 {
1484 for (c = spats[0].off.off; c; --c)
1485 if (decl(&pos) == -1)
1486 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001487 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001489 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 pos.col = MAXCOL;
1491 }
1492 }
1493 else
1494 {
1495 for (c = spats[0].off.off; c; ++c)
1496 if (incl(&pos) == -1)
1497 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001498 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1501 pos.col = 0;
1502 }
1503 }
1504 }
1505
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001506 /*
1507 * The actual search.
1508 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001509 c = searchit(curwin, curbuf, &pos, NULL,
1510 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 searchstr, count, spats[0].off.end + (options &
1512 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1513 + SEARCH_MSG + SEARCH_START
1514 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001515 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
1517 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001518 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001519
1520 if (!shortmess(SHM_SEARCH)
1521 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1522 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001523 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (c == FAIL)
1526 {
1527 retval = 0;
1528 goto end_do_search;
1529 }
1530 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001531 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001533 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
1535 /*
1536 * Add character and/or line offset
1537 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001538 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001540 pos_T org_pos = pos;
1541
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001542 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 c = pos.lnum + spats[0].off.off;
1545 if (c < 1)
1546 pos.lnum = 1;
1547 else if (c > curbuf->b_ml.ml_line_count)
1548 pos.lnum = curbuf->b_ml.ml_line_count;
1549 else
1550 pos.lnum = c;
1551 pos.col = 0;
1552
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001553 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001555 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001557 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558 c = spats[0].off.off;
1559 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001561 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (incl(&pos) == -1)
1563 break;
1564 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001565 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 else
1567 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568 while (c++ < 0)
1569 if (decl(&pos) == -1)
1570 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001573 if (!EQUAL_POS(pos, org_pos))
1574 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001577 // Show [1/15] if 'S' is not in 'shortmess'.
1578 if ((options & SEARCH_ECHO)
1579 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001580 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001581 && c != FAIL
1582 && !shortmess(SHM_SEARCHCOUNT)
1583 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001584 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1585 show_top_bot_msg, msgbuf,
1586 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001587#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001588 || (!(fdo_flags & FDO_SEARCH)
1589 && hasFolding(curwin->w_cursor.lnum,
1590 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001591#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001592 ),
1593 SEARCH_STAT_DEF_MAX_COUNT,
1594 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 /*
1597 * The search command can be followed by a ';' to do another search.
1598 * For example: "/pat/;/foo/+3;?bar"
1599 * This is like doing another search command, except:
1600 * - The remembered direction '/' or '?' is from the first search.
1601 * - When an error happens the cursor isn't moved at all.
1602 * Don't do this when called by get_address() (it handles ';' itself).
1603 */
1604 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1605 break;
1606
1607 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001608 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (dirc != '?' && dirc != '/')
1610 {
1611 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001612 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 goto end_do_search;
1614 }
1615 ++pat;
1616 }
1617
1618 if (options & SEARCH_MARK)
1619 setpcmark();
1620 curwin->w_cursor = pos;
1621 curwin->w_set_curswant = TRUE;
1622
1623end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001624 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 spats[0].off = old_off;
1626 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001627 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 return retval;
1630}
1631
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632/*
1633 * search_for_exact_line(buf, pos, dir, pat)
1634 *
1635 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001636 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001638 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 * Return OK for success, or FAIL if no line found.
1640 */
1641 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001642search_for_exact_line(
1643 buf_T *buf,
1644 pos_T *pos,
1645 int dir,
1646 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 linenr_T start = 0;
1649 char_u *ptr;
1650 char_u *p;
1651
1652 if (buf->b_ml.ml_line_count == 0)
1653 return FAIL;
1654 for (;;)
1655 {
1656 pos->lnum += dir;
1657 if (pos->lnum < 1)
1658 {
1659 if (p_ws)
1660 {
1661 pos->lnum = buf->b_ml.ml_line_count;
1662 if (!shortmess(SHM_SEARCH))
1663 give_warning((char_u *)_(top_bot_msg), TRUE);
1664 }
1665 else
1666 {
1667 pos->lnum = 1;
1668 break;
1669 }
1670 }
1671 else if (pos->lnum > buf->b_ml.ml_line_count)
1672 {
1673 if (p_ws)
1674 {
1675 pos->lnum = 1;
1676 if (!shortmess(SHM_SEARCH))
1677 give_warning((char_u *)_(bot_top_msg), TRUE);
1678 }
1679 else
1680 {
1681 pos->lnum = 1;
1682 break;
1683 }
1684 }
1685 if (pos->lnum == start)
1686 break;
1687 if (start == 0)
1688 start = pos->lnum;
1689 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1690 p = skipwhite(ptr);
1691 pos->col = (colnr_T) (p - ptr);
1692
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001693 // when adding lines the matching line may be empty but it is not
1694 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001695 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1698 return OK;
1699 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001700 else if (*p != NUL) // ignore empty lines
1701 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001702 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1703 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 return OK;
1705 }
1706 }
1707 return FAIL;
1708}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
1710/*
1711 * Character Searches
1712 */
1713
1714/*
1715 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1716 * position of the character, otherwise move to just before the char.
1717 * Do this "cap->count1" times.
1718 * Return FAIL or OK.
1719 */
1720 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001721searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001723 int c = cap->nchar; // char to search for
1724 int dir = cap->arg; // TRUE for searching forward
1725 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 int col;
1727 char_u *p;
1728 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001729 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001731 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001733 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001735 *lastc = c;
1736 set_csearch_direction(dir);
1737 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001738 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (cap->ncharC1 != 0)
1740 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001741 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1742 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001744 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1745 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
1748 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001749 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 {
zeertzjqe5d91ba2023-05-14 17:39:18 +01001751 if (*lastc == NUL && lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001753 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 dir = -lastcdir;
1755 else
1756 dir = lastcdir;
1757 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001758 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001759 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001760
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001761 // Force a move of at least one char, so ";" and "," will move the
1762 // cursor, even if the cursor is right in front of char we are looking
1763 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001764 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001765 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001768 if (dir == BACKWARD)
1769 cap->oap->inclusive = FALSE;
1770 else
1771 cap->oap->inclusive = TRUE;
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 p = ml_get_curline();
1774 col = curwin->w_cursor.col;
1775 len = (int)STRLEN(p);
1776
1777 while (count--)
1778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (has_mbyte)
1780 {
1781 for (;;)
1782 {
1783 if (dir > 0)
1784 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001785 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 if (col >= len)
1787 return FAIL;
1788 }
1789 else
1790 {
1791 if (col == 0)
1792 return FAIL;
1793 col -= (*mb_head_off)(p, p + col - 1) + 1;
1794 }
zeertzjqe5d91ba2023-05-14 17:39:18 +01001795 if (lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001797 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 break;
1799 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001800 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001801 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001802 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001803 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 }
1805 }
1806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 {
1808 for (;;)
1809 {
1810 if ((col += dir) < 0 || col >= len)
1811 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001812 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001814 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816 }
1817 }
1818
1819 if (t_cmd)
1820 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001821 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (has_mbyte)
1824 {
1825 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001826 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001827 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001829 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 col -= (*mb_head_off)(p, p + col);
1831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 curwin->w_cursor.col = col;
1834
1835 return OK;
1836}
1837
1838/*
1839 * "Other" Searches
1840 */
1841
1842/*
1843 * findmatch - find the matching paren or brace
1844 *
1845 * Improvement over vi: Braces inside quotes are ignored.
1846 */
1847 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001848findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 return findmatchlimit(oap, initc, 0, 0);
1851}
1852
1853/*
1854 * Return TRUE if the character before "linep[col]" equals "ch".
1855 * Return FALSE if "col" is zero.
1856 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1857 * is NULL.
1858 * Handles multibyte string correctly.
1859 */
1860 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001861check_prevcol(
1862 char_u *linep,
1863 int col,
1864 int ch,
1865 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (col > 0 && has_mbyte)
1869 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (prevcol)
1871 *prevcol = col;
1872 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1873}
1874
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001875/*
1876 * Raw string start is found at linep[startpos.col - 1].
1877 * Return TRUE if the matching end can be found between startpos and endpos.
1878 */
1879 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001880find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001881{
1882 char_u *p;
1883 char_u *delim_copy;
1884 size_t delim_len;
1885 linenr_T lnum;
1886 int found = FALSE;
1887
1888 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1889 ;
1890 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001891 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001892 if (delim_copy == NULL)
1893 return FALSE;
1894 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1895 {
1896 char_u *line = ml_get(lnum);
1897
1898 for (p = line + (lnum == startpos->lnum
1899 ? startpos->col + 1 : 0); *p; ++p)
1900 {
1901 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1902 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02001903 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
1904 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001905 {
1906 found = TRUE;
1907 break;
1908 }
1909 }
1910 if (found)
1911 break;
1912 }
1913 vim_free(delim_copy);
1914 return found;
1915}
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001918 * Check matchpairs option for "*initc".
1919 * If there is a match set "*initc" to the matching character and "*findc" to
1920 * the opposite character. Set "*backwards" to the direction.
1921 * When "switchit" is TRUE swap the direction.
1922 */
1923 static void
1924find_mps_values(
1925 int *initc,
1926 int *findc,
1927 int *backwards,
1928 int switchit)
1929{
1930 char_u *ptr;
1931
1932 ptr = curbuf->b_p_mps;
1933 while (*ptr != NUL)
1934 {
1935 if (has_mbyte)
1936 {
1937 char_u *prev;
1938
1939 if (mb_ptr2char(ptr) == *initc)
1940 {
1941 if (switchit)
1942 {
1943 *findc = *initc;
1944 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1945 *backwards = TRUE;
1946 }
1947 else
1948 {
1949 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1950 *backwards = FALSE;
1951 }
1952 return;
1953 }
1954 prev = ptr;
1955 ptr += mb_ptr2len(ptr) + 1;
1956 if (mb_ptr2char(ptr) == *initc)
1957 {
1958 if (switchit)
1959 {
1960 *findc = *initc;
1961 *initc = mb_ptr2char(prev);
1962 *backwards = FALSE;
1963 }
1964 else
1965 {
1966 *findc = mb_ptr2char(prev);
1967 *backwards = TRUE;
1968 }
1969 return;
1970 }
1971 ptr += mb_ptr2len(ptr);
1972 }
1973 else
1974 {
1975 if (*ptr == *initc)
1976 {
1977 if (switchit)
1978 {
1979 *backwards = TRUE;
1980 *findc = *initc;
1981 *initc = ptr[2];
1982 }
1983 else
1984 {
1985 *backwards = FALSE;
1986 *findc = ptr[2];
1987 }
1988 return;
1989 }
1990 ptr += 2;
1991 if (*ptr == *initc)
1992 {
1993 if (switchit)
1994 {
1995 *backwards = FALSE;
1996 *findc = *initc;
1997 *initc = ptr[-2];
1998 }
1999 else
2000 {
2001 *backwards = TRUE;
2002 *findc = ptr[-2];
2003 }
2004 return;
2005 }
2006 ++ptr;
2007 }
2008 if (*ptr == ',')
2009 ++ptr;
2010 }
2011}
2012
2013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002015 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2016 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 *
2018 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002019 * character at or after the cursor. Special values:
2020 * '*' look for C-style comment / *
2021 * '/' look for C-style comment / *, ignoring comment-end
2022 * '#' look for preprocessor directives
2023 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 *
2025 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2026 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2027 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2028 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002029 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002030 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002031 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002034findmatchlimit(
2035 oparg_T *oap,
2036 int initc,
2037 int flags,
2038 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002040 static pos_T pos; // current search position
2041 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002043 int count = 0; // cumulative number of braces
2044 int backwards = FALSE; // init for gcc
2045 int raw_string = FALSE; // search for raw string
2046 int inquote = FALSE; // TRUE when inside quotes
2047 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002049 int do_quotes; // check for quotes in current line
2050 int at_start; // do_quotes value at start position
2051 int hash_dir = 0; // Direction searched for # things
2052 int comment_dir = 0; // Direction searched for comments
2053 pos_T match_pos; // Where last slash-star was found
2054 int start_in_quotes; // start position is in quotes
2055 int traveled = 0; // how far we've searched so far
2056 int ignore_cend = FALSE; // ignore comment end
2057 int cpo_match; // vi compatible matching
2058 int cpo_bsl; // don't recognize backslashes
2059 int match_escaped = 0; // search for escaped match
2060 int dir; // Direction to search
2061 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002062 int lispcomm = FALSE; // inside of Lisp-style comment
2063 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002066 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 linep = ml_get(pos.lnum);
2068
2069 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2070 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2071
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002072 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (flags & FM_BACKWARD)
2074 dir = BACKWARD;
2075 else if (flags & FM_FORWARD)
2076 dir = FORWARD;
2077 else
2078 dir = 0;
2079
2080 /*
2081 * if initc given, look in the table for the matching character
2082 * '/' and '*' are special cases: look for start or end of comment.
2083 * When '/' is used, we ignore running backwards into an star-slash, for
2084 * "[*" command, we just want to find any comment.
2085 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002086 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
2088 comment_dir = dir;
2089 if (initc == '/')
2090 ignore_cend = TRUE;
2091 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002092 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 initc = NUL;
2094 }
2095 else if (initc != '#' && initc != NUL)
2096 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002097 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002098 if (dir)
2099 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002100 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 return NULL;
2102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 else
2104 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002105 /*
2106 * Either initc is '#', or no initc was given and we need to look
2107 * under the cursor.
2108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (initc == '#')
2110 {
2111 hash_dir = dir;
2112 }
2113 else
2114 {
2115 /*
2116 * initc was not given, must look for something to match under
2117 * or near the cursor.
2118 * Only check for special things when 'cpo' doesn't have '%'.
2119 */
2120 if (!cpo_match)
2121 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002122 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 ptr = skipwhite(linep);
2124 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2125 {
2126 ptr = skipwhite(ptr + 1);
2127 if ( STRNCMP(ptr, "if", 2) == 0
2128 || STRNCMP(ptr, "endif", 5) == 0
2129 || STRNCMP(ptr, "el", 2) == 0)
2130 hash_dir = 1;
2131 }
2132
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002133 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 else if (linep[pos.col] == '/')
2135 {
2136 if (linep[pos.col + 1] == '*')
2137 {
2138 comment_dir = FORWARD;
2139 backwards = FALSE;
2140 pos.col++;
2141 }
2142 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2143 {
2144 comment_dir = BACKWARD;
2145 backwards = TRUE;
2146 pos.col--;
2147 }
2148 }
2149 else if (linep[pos.col] == '*')
2150 {
2151 if (linep[pos.col + 1] == '/')
2152 {
2153 comment_dir = BACKWARD;
2154 backwards = TRUE;
2155 }
2156 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2157 {
2158 comment_dir = FORWARD;
2159 backwards = FALSE;
2160 }
2161 }
2162 }
2163
2164 /*
2165 * If we are not on a comment or the # at the start of a line, then
2166 * look for brace anywhere on this line after the cursor.
2167 */
2168 if (!hash_dir && !comment_dir)
2169 {
2170 /*
2171 * Find the brace under or after the cursor.
2172 * If beyond the end of the line, use the last character in
2173 * the line.
2174 */
2175 if (linep[pos.col] == NUL && pos.col)
2176 --pos.col;
2177 for (;;)
2178 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002179 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (initc == NUL)
2181 break;
2182
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002183 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (findc)
2185 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002186 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 }
2188 if (!findc)
2189 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002190 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (!cpo_match && *skipwhite(linep) == '#')
2192 hash_dir = 1;
2193 else
2194 return NULL;
2195 }
2196 else if (!cpo_bsl)
2197 {
2198 int col, bslcnt = 0;
2199
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002200 // Set "match_escaped" if there are an odd number of
2201 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2203 bslcnt++;
2204 match_escaped = (bslcnt & 1);
2205 }
2206 }
2207 }
2208 if (hash_dir)
2209 {
2210 /*
2211 * Look for matching #if, #else, #elif, or #endif
2212 */
2213 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002214 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 if (initc != '#')
2216 {
2217 ptr = skipwhite(skipwhite(linep) + 1);
2218 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2219 hash_dir = 1;
2220 else if (STRNCMP(ptr, "endif", 5) == 0)
2221 hash_dir = -1;
2222 else
2223 return NULL;
2224 }
2225 pos.col = 0;
2226 while (!got_int)
2227 {
2228 if (hash_dir > 0)
2229 {
2230 if (pos.lnum == curbuf->b_ml.ml_line_count)
2231 break;
2232 }
2233 else if (pos.lnum == 1)
2234 break;
2235 pos.lnum += hash_dir;
2236 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002237 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 ptr = skipwhite(linep);
2239 if (*ptr != '#')
2240 continue;
2241 pos.col = (colnr_T) (ptr - linep);
2242 ptr = skipwhite(ptr + 1);
2243 if (hash_dir > 0)
2244 {
2245 if (STRNCMP(ptr, "if", 2) == 0)
2246 count++;
2247 else if (STRNCMP(ptr, "el", 2) == 0)
2248 {
2249 if (count == 0)
2250 return &pos;
2251 }
2252 else if (STRNCMP(ptr, "endif", 5) == 0)
2253 {
2254 if (count == 0)
2255 return &pos;
2256 count--;
2257 }
2258 }
2259 else
2260 {
2261 if (STRNCMP(ptr, "if", 2) == 0)
2262 {
2263 if (count == 0)
2264 return &pos;
2265 count--;
2266 }
2267 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2268 {
2269 if (count == 0)
2270 return &pos;
2271 }
2272 else if (STRNCMP(ptr, "endif", 5) == 0)
2273 count++;
2274 }
2275 }
2276 return NULL;
2277 }
2278 }
2279
2280#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002281 // This is just guessing: when 'rightleft' is set, search for a matching
2282 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2284 backwards = !backwards;
2285#endif
2286
2287 do_quotes = -1;
2288 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002289 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002290
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002291 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002292 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002294 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002295 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 while (!got_int)
2298 {
2299 /*
2300 * Go to the next position, forward or backward. We could use
2301 * inc() and dec() here, but that is much slower
2302 */
2303 if (backwards)
2304 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002305 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002306 if (lispcomm && pos.col < (colnr_T)comment_col)
2307 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002308 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002310 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 break;
2312 --pos.lnum;
2313
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002314 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 break;
2316
2317 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002318 pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 do_quotes = -1;
2320 line_breakcheck();
2321
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002322 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002323 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002325 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002326 if (lisp && comment_col != MAXCOL)
2327 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329 else
2330 {
2331 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 if (has_mbyte)
2333 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002336 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002338 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002339 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002340 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002341 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002342 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002344 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002345 // line is exhausted and comment with it,
2346 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002347 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 break;
2349 ++pos.lnum;
2350
2351 if (maxtravel && traveled++ > maxtravel)
2352 break;
2353
2354 linep = ml_get(pos.lnum);
2355 pos.col = 0;
2356 do_quotes = -1;
2357 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002358 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002359 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361 else
2362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002364 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 ++pos.col;
2367 }
2368 }
2369
2370 /*
2371 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2372 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002373 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2374 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002378 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
2380
2381 if (comment_dir)
2382 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002383 // Note: comments do not nest, and we ignore quotes in them
2384 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (comment_dir == FORWARD)
2386 {
2387 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2388 {
2389 pos.col++;
2390 return &pos;
2391 }
2392 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002393 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
2395 /*
2396 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002397 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 */
2399 if (pos.col == 0)
2400 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002401 else if (raw_string)
2402 {
2403 if (linep[pos.col - 1] == 'R'
2404 && linep[pos.col] == '"'
2405 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2406 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002407 // Possible start of raw string. Now that we have the
2408 // delimiter we can check if it ends before where we
2409 // started searching, or before the previously found
2410 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002411 if (!find_rawstring_end(linep, &pos,
2412 count > 0 ? &match_pos : &curwin->w_cursor))
2413 {
2414 count++;
2415 match_pos = pos;
2416 match_pos.col--;
2417 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002418 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002419 }
2420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 else if ( linep[pos.col - 1] == '/'
2422 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002423 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 && (int)pos.col < comment_col)
2425 {
2426 count++;
2427 match_pos = pos;
2428 match_pos.col--;
2429 }
2430 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2431 {
2432 if (count > 0)
2433 pos = match_pos;
2434 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2435 && (int)pos.col <= comment_col)
2436 pos.col -= 2;
2437 else if (ignore_cend)
2438 continue;
2439 else
2440 return NULL;
2441 return &pos;
2442 }
2443 }
2444 continue;
2445 }
2446
2447 /*
2448 * If smart matching ('cpoptions' does not contain '%'), braces inside
2449 * of quotes are ignored, but only if there is an even number of
2450 * quotes in the line.
2451 */
2452 if (cpo_match)
2453 do_quotes = 0;
2454 else if (do_quotes == -1)
2455 {
2456 /*
2457 * Count the number of quotes in the line, skipping \" and '"'.
2458 * Watch out for "\\".
2459 */
2460 at_start = do_quotes;
2461 for (ptr = linep; *ptr; ++ptr)
2462 {
2463 if (ptr == linep + pos.col + backwards)
2464 at_start = (do_quotes & 1);
2465 if (*ptr == '"'
2466 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2467 ++do_quotes;
2468 if (*ptr == '\\' && ptr[1] != NUL)
2469 ++ptr;
2470 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002471 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473 /*
2474 * If we find an uneven count, check current line and previous
2475 * one for a '\' at the end.
2476 */
2477 if (!do_quotes)
2478 {
2479 inquote = FALSE;
2480 if (ptr[-1] == '\\')
2481 {
2482 do_quotes = 1;
2483 if (start_in_quotes == MAYBE)
2484 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002485 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 inquote = TRUE;
2487 start_in_quotes = TRUE;
2488 }
2489 else if (backwards)
2490 inquote = TRUE;
2491 }
2492 if (pos.lnum > 1)
2493 {
2494 ptr = ml_get(pos.lnum - 1);
2495 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2496 {
2497 do_quotes = 1;
2498 if (start_in_quotes == MAYBE)
2499 {
2500 inquote = at_start;
2501 if (inquote)
2502 start_in_quotes = TRUE;
2503 }
2504 else if (!backwards)
2505 inquote = TRUE;
2506 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002507
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002508 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002509 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 }
2511 }
2512 }
2513 if (start_in_quotes == MAYBE)
2514 start_in_quotes = FALSE;
2515
2516 /*
2517 * If 'smartmatch' is set:
2518 * Things inside quotes are ignored by setting 'inquote'. If we
2519 * find a quote without a preceding '\' invert 'inquote'. At the
2520 * end of a line not ending in '\' we reset 'inquote'.
2521 *
2522 * In lines with an uneven number of quotes (without preceding '\')
2523 * we do not know which part to ignore. Therefore we only set
2524 * inquote if the number of quotes in a line is even, unless this
2525 * line or the previous one ends in a '\'. Complicated, isn't it?
2526 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002527 c = PTR2CHAR(linep + pos.col);
2528 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002531 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2533 {
2534 inquote = FALSE;
2535 start_in_quotes = FALSE;
2536 }
2537 break;
2538
2539 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002540 // a quote that is preceded with an odd number of backslashes is
2541 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (do_quotes)
2543 {
2544 int col;
2545
2546 for (col = pos.col - 1; col >= 0; --col)
2547 if (linep[col] != '\\')
2548 break;
2549 if ((((int)pos.col - 1 - col) & 1) == 0)
2550 {
2551 inquote = !inquote;
2552 start_in_quotes = FALSE;
2553 }
2554 }
2555 break;
2556
2557 /*
2558 * If smart matching ('cpoptions' does not contain '%'):
2559 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2560 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2561 * skipped, there is never a brace in them.
2562 * Ignore this when finding matches for `'.
2563 */
2564 case '\'':
2565 if (!cpo_match && initc != '\'' && findc != '\'')
2566 {
2567 if (backwards)
2568 {
2569 if (pos.col > 1)
2570 {
2571 if (linep[pos.col - 2] == '\'')
2572 {
2573 pos.col -= 2;
2574 break;
2575 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002576 else if (linep[pos.col - 2] == '\\'
2577 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 pos.col -= 3;
2580 break;
2581 }
2582 }
2583 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002584 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002586 if (linep[pos.col + 1] == '\\'
2587 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
2589 pos.col += 3;
2590 break;
2591 }
2592 else if (linep[pos.col + 2] == '\'')
2593 {
2594 pos.col += 2;
2595 break;
2596 }
2597 }
2598 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002599 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
2601 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002602 /*
2603 * For Lisp skip over backslashed (), {} and [].
2604 * (actually, we skip #\( et al)
2605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002607 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002608 && pos.col > 1
2609 && check_prevcol(linep, pos.col, '\\', NULL)
2610 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002613 // Check for match outside of quotes, and inside of
2614 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if ((!inquote || start_in_quotes == TRUE)
2616 && (c == initc || c == findc))
2617 {
2618 int col, bslcnt = 0;
2619
2620 if (!cpo_bsl)
2621 {
2622 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2623 bslcnt++;
2624 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002625 // Only accept a match when 'M' is in 'cpo' or when escaping
2626 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2628 {
2629 if (c == initc)
2630 count++;
2631 else
2632 {
2633 if (count == 0)
2634 return &pos;
2635 count--;
2636 }
2637 }
2638 }
2639 }
2640 }
2641
2642 if (comment_dir == BACKWARD && count > 0)
2643 {
2644 pos = match_pos;
2645 return &pos;
2646 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002647 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648}
2649
2650/*
2651 * Check if line[] contains a / / comment.
2652 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002654 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002655check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 char_u *p;
2658
2659 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002660 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002661 if (curbuf->b_p_lisp)
2662 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002663 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002664 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002665 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002666
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002667 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002668 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002669 {
2670 if (*p == '"')
2671 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002672 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002673 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002674 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002675 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002676 }
2677 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002678 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002679 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002680 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002681 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002682 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002683 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2684 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002685 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002686 ++p;
2687 }
2688 }
2689 else
2690 p = NULL;
2691 }
2692 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002693 while ((p = vim_strchr(p, '/')) != NULL)
2694 {
2695 // Accept a double /, unless it's preceded with * and followed by
2696 // *, because * / / * is an end and start of a C comment. Only
2697 // accept the position if it is not inside a string.
2698 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002699 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002700 break;
2701 ++p;
2702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703
2704 if (p == NULL)
2705 return MAXCOL;
2706 return (int)(p - line);
2707}
2708
2709/*
2710 * Move cursor briefly to character matching the one under the cursor.
2711 * Used for Insert mode and "r" command.
2712 * Show the match only if it is visible on the screen.
2713 * If there isn't a match, then beep.
2714 */
2715 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002716showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002717 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 pos_T *lpos, save_cursor;
2720 pos_T mpos;
2721 colnr_T vcol;
2722 long save_so;
2723 long save_siso;
2724#ifdef CURSOR_SHAPE
2725 int save_state;
2726#endif
2727 colnr_T save_dollar_vcol;
2728 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002729 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2730 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
2733 * Only show match for chars in the 'matchpairs' option.
2734 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002735 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002736 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
2738#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002739 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002740 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002741#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002742 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002743 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744#ifdef FEAT_RIGHTLEFT
2745 && !(curwin->w_p_rl ^ p_ri)
2746#endif
2747 )
2748 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002749 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002750 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 return;
2752 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002753 if (*p == NUL)
2754 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002756 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002758 vim_beep(BO_MATCH);
2759 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002761
2762 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2763 return;
2764
2765 if (!curwin->w_p_wrap)
2766 getvcol(curwin, lpos, NULL, &vcol, NULL);
2767
2768 int col_visible = (curwin->w_p_wrap
2769 || (vcol >= curwin->w_leftcol
2770 && vcol < curwin->w_leftcol + curwin->w_width));
2771 if (!col_visible)
2772 return;
2773
2774 mpos = *lpos; // save the pos, update_screen() may change it
2775 save_cursor = curwin->w_cursor;
2776 save_so = *so;
2777 save_siso = *siso;
2778 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2779 // stop displaying the "$".
2780 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2781 dollar_vcol = -1;
2782 ++curwin->w_virtcol; // do display ')' just before "$"
2783 update_screen(UPD_VALID); // show the new char first
2784
2785 save_dollar_vcol = dollar_vcol;
2786#ifdef CURSOR_SHAPE
2787 save_state = State;
2788 State = MODE_SHOWMATCH;
2789 ui_cursor_shape(); // may show different cursor shape
2790#endif
2791 curwin->w_cursor = mpos; // move to matching char
2792 *so = 0; // don't use 'scrolloff' here
2793 *siso = 0; // don't use 'sidescrolloff' here
2794 showruler(FALSE);
2795 setcursor();
2796 cursor_on(); // make sure that the cursor is shown
2797 out_flush_cursor(TRUE, FALSE);
2798
2799 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2800 // which resets it if the matching position is in a previous line
2801 // and has a higher column number.
2802 dollar_vcol = save_dollar_vcol;
2803
2804 /*
2805 * brief pause, unless 'm' is present in 'cpo' and a character is
2806 * available.
2807 */
2808 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2809 ui_delay(p_mat * 100L + 8, TRUE);
2810 else if (!char_avail())
2811 ui_delay(p_mat * 100L + 9, FALSE);
2812 curwin->w_cursor = save_cursor; // restore cursor position
2813 *so = save_so;
2814 *siso = save_siso;
2815#ifdef CURSOR_SHAPE
2816 State = save_state;
2817 ui_cursor_shape(); // may show different cursor shape
2818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819}
2820
2821/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002822 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002823 * If move is TRUE, check from the beginning of the buffer, else from position
2824 * "cur".
2825 * "direction" is FORWARD or BACKWARD.
2826 * Returns TRUE, FALSE or -1 for failure.
2827 */
2828 static int
2829is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2830{
2831 regmmatch_T regmatch;
2832 int nmatched = 0;
2833 int result = -1;
2834 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002835 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002836 int flag = 0;
2837
2838 if (pattern == NULL)
2839 pattern = spats[last_idx].pat;
2840
Rob Pillinge86190e2022-12-23 19:06:04 +00002841 if (search_regcomp(pattern, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002842 SEARCH_KEEP, &regmatch) == FAIL)
2843 return -1;
2844
2845 // init startcol correctly
2846 regmatch.startpos[0].col = -1;
2847 // move to match
2848 if (move)
2849 {
2850 CLEAR_POS(&pos);
2851 }
2852 else
2853 {
2854 pos = *cur;
2855 // accept a match at the cursor position
2856 flag = SEARCH_START;
2857 }
2858
2859 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2860 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2861 {
2862 // Zero-width pattern should match somewhere, then we can check if
2863 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002864 do
2865 {
2866 regmatch.startpos[0].col++;
2867 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002868 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002869 if (nmatched != 0)
2870 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002871 } while (regmatch.regprog != NULL
2872 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002873 : regmatch.startpos[0].col > pos.col);
2874
Bram Moolenaar53989552019-12-23 22:59:18 +01002875 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002876 {
2877 result = (nmatched != 0
2878 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2879 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2880 }
2881 }
2882
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002883 vim_regfree(regmatch.regprog);
2884 return result;
2885}
2886
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002887
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002888/*
2889 * Find next search match under cursor, cursor at end.
2890 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002891 */
2892 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002893current_search(
2894 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002895 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002896{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002897 pos_T start_pos; // start position of the pattern match
2898 pos_T end_pos; // end position of the pattern match
2899 pos_T orig_pos; // position of the cursor at beginning
2900 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002901 int i;
2902 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002903 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002904 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002905 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002906 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002907 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002908 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002909
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002910 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002911 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002912 dec_cursor();
2913
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002914 // When searching forward and the cursor is at the start of the Visual
2915 // area, skip the first search backward, otherwise it doesn't move.
2916 skip_first_backward = forward && VIsual_active
2917 && LT_POS(curwin->w_cursor, VIsual);
2918
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002919 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002920 if (VIsual_active)
2921 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002922 if (forward)
2923 incl(&pos);
2924 else
2925 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002926 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002927
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002928 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002929 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02002930 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002931 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002932 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002933
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002934 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002935 * The trick is to first search backwards and then search forward again,
2936 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002937 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002938 */
2939 for (i = 0; i < 2; i++)
2940 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002941 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002942 {
2943 if (i == 0 && skip_first_backward)
2944 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002945 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002946 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002947 else
2948 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002949
2950 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002951 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002952 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002953 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002954
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002955 // wrapping should not occur in the first round
2956 if (i == 0)
2957 p_ws = FALSE;
2958
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002959 result = searchit(curwin, curbuf, &pos, &end_pos,
2960 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002961 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002962 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002963
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002964 p_ws = old_p_ws;
2965
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002966 // First search may fail, but then start searching from the
2967 // beginning of the file (cursor might be on the search match)
2968 // except when Visual mode is active, so that extending the visual
2969 // selection works.
2970 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002971 {
2972 curwin->w_cursor = orig_pos;
2973 if (VIsual_active)
2974 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002975 return FAIL;
2976 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002977 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002978 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002979 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002980 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002981 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002982 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002983 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002984 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002985 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002986 // try again from end of buffer
2987 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002988 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02002989 pos.col = (colnr_T)STRLEN(
2990 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002991 }
2992 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002993 }
2994
2995 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002996
2997 if (!VIsual_active)
2998 VIsual = start_pos;
2999
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003000 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003001 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003002 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003003 {
3004 if (skip_first_backward)
3005 // put the cursor on the start of the match
3006 curwin->w_cursor = pos;
3007 else
3008 // put the cursor on last character of match
3009 dec_cursor();
3010 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003011 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003012 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003013 VIsual_active = TRUE;
3014 VIsual_mode = 'v';
3015
Bram Moolenaarb7633612019-02-10 21:48:25 +01003016 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003017 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003018 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003019 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3020 inc_cursor();
3021 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3022 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003023 }
3024
3025#ifdef FEAT_FOLDING
3026 if (fdo_flags & FDO_SEARCH && KeyTyped)
3027 foldOpenCursor();
3028#endif
3029
3030 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003031 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003032#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003033 // Make sure the clipboard gets updated. Needed because start and
3034 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003035 clip_star.vmode = NUL;
3036#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003037 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003038 showmode();
3039
3040 return OK;
3041}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
3044 * return TRUE if line 'lnum' is empty or has white chars only.
3045 */
3046 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003047linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048{
3049 char_u *p;
3050
3051 p = skipwhite(ml_get(lnum));
3052 return (*p == NUL);
3053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003055/*
3056 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003057 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003058 */
3059 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003060cmdline_search_stat(
3061 int dirc,
3062 pos_T *pos,
3063 pos_T *cursor_pos,
3064 int show_top_bot_msg,
3065 char_u *msgbuf,
3066 int recompute,
3067 int maxcount,
3068 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003069{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003070 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003071
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003072 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3073 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003074 if (stat.cur <= 0)
3075 return;
3076
3077 char t[SEARCH_STAT_BUF_LEN];
3078 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003079
3080#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003081 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3082 {
3083 if (stat.incomplete == 1)
3084 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3085 else if (stat.cnt > maxcount && stat.cur > maxcount)
3086 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3087 maxcount, maxcount);
3088 else if (stat.cnt > maxcount)
3089 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3090 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003091 else
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003092 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3093 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003094 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003095 else
3096#endif
3097 {
3098 if (stat.incomplete == 1)
3099 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3100 else if (stat.cnt > maxcount && stat.cur > maxcount)
3101 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3102 maxcount, maxcount);
3103 else if (stat.cnt > maxcount)
3104 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3105 stat.cur, maxcount);
3106 else
3107 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3108 stat.cur, stat.cnt);
3109 }
3110
3111 len = STRLEN(t);
3112 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3113 {
3114 mch_memmove(t + 2, t, len);
3115 t[0] = 'W';
3116 t[1] = ' ';
3117 len += 2;
3118 }
3119
zeertzjqa7d36b72023-01-31 21:13:38 +00003120 size_t msgbuf_len = STRLEN(msgbuf);
3121 if (len > msgbuf_len)
3122 len = msgbuf_len;
3123 mch_memmove(msgbuf + msgbuf_len - len, t, len);
3124
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003125 if (dirc == '?' && stat.cur == maxcount + 1)
3126 stat.cur = -1;
3127
3128 // keep the message even after redraw, but don't put in history
3129 msg_hist_off = TRUE;
3130 give_warning(msgbuf, FALSE);
3131 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003132}
3133
3134/*
3135 * Add the search count information to "stat".
3136 * "stat" must not be NULL.
3137 * When "recompute" is TRUE always recompute the numbers.
3138 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3139 * dirc == '/': find the next match
3140 * dirc == '?': find the previous match
3141 */
3142 static void
3143update_search_stat(
3144 int dirc,
3145 pos_T *pos,
3146 pos_T *cursor_pos,
3147 searchstat_T *stat,
3148 int recompute,
3149 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003150 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003151{
3152 int save_ws = p_ws;
3153 int wraparound = FALSE;
3154 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003155 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003156 static int cur = 0;
3157 static int cnt = 0;
3158 static int exact_match = FALSE;
3159 static int incomplete = 0;
3160 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3161 static int chgtick = 0;
3162 static char_u *lastpat = NULL;
3163 static buf_T *lbuf = NULL;
3164#ifdef FEAT_RELTIME
3165 proftime_T start;
3166#endif
3167
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003168 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003169
3170 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3171 {
3172 stat->cur = cur;
3173 stat->cnt = cnt;
3174 stat->exact_match = exact_match;
3175 stat->incomplete = incomplete;
3176 stat->last_maxcount = last_maxcount;
3177 return;
3178 }
3179 last_maxcount = maxcount;
3180
3181 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3182 || (dirc == '/' && LT_POS(p, lastpos)));
3183
3184 // If anything relevant changed the count has to be recomputed.
3185 // MB_STRNICMP ignores case, but we should not ignore case.
3186 // Unfortunately, there is no MB_STRNICMP function.
3187 // XXX: above comment should be "no MB_STRCMP function" ?
3188 if (!(chgtick == CHANGEDTICK(curbuf)
3189 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3190 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3191 && EQUAL_POS(lastpos, *cursor_pos)
3192 && lbuf == curbuf) || wraparound || cur < 0
3193 || (maxcount > 0 && cur > maxcount) || recompute)
3194 {
3195 cur = 0;
3196 cnt = 0;
3197 exact_match = FALSE;
3198 incomplete = 0;
3199 CLEAR_POS(&lastpos);
3200 lbuf = curbuf;
3201 }
3202
Christian Brabandt34a6a362023-05-06 19:20:20 +01003203 // when searching backwards and having jumped to the first occurrence,
3204 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003205 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003206 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003207 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3208 else
3209 {
3210 int done_search = FALSE;
3211 pos_T endpos = {0, 0, 0};
3212
3213 p_ws = FALSE;
3214#ifdef FEAT_RELTIME
3215 if (timeout > 0)
3216 profile_setlimit(timeout, &start);
3217#endif
3218 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3219 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3220 {
3221 done_search = TRUE;
3222#ifdef FEAT_RELTIME
3223 // Stop after passing the time limit.
3224 if (timeout > 0 && profile_passed_limit(&start))
3225 {
3226 incomplete = 1;
3227 break;
3228 }
3229#endif
3230 cnt++;
3231 if (LTOREQ_POS(lastpos, p))
3232 {
3233 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003234 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003235 exact_match = TRUE;
3236 }
3237 fast_breakcheck();
3238 if (maxcount > 0 && cnt > maxcount)
3239 {
3240 incomplete = 2; // max count exceeded
3241 break;
3242 }
3243 }
3244 if (got_int)
3245 cur = -1; // abort
3246 if (done_search)
3247 {
3248 vim_free(lastpat);
3249 lastpat = vim_strsave(spats[last_idx].pat);
3250 chgtick = CHANGEDTICK(curbuf);
3251 lbuf = curbuf;
3252 lastpos = p;
3253 }
3254 }
3255 stat->cur = cur;
3256 stat->cnt = cnt;
3257 stat->exact_match = exact_match;
3258 stat->incomplete = incomplete;
3259 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003260 p_ws = save_ws;
3261}
3262
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003264
3265/*
3266 * Get line "lnum" and copy it into "buf[LSIZE]".
3267 * The copy is made because the regexp may make the line invalid when using a
3268 * mark.
3269 */
3270 static char_u *
3271get_line_and_copy(linenr_T lnum, char_u *buf)
3272{
3273 char_u *line = ml_get(lnum);
3274
3275 vim_strncpy(buf, line, LSIZE - 1);
3276 return buf;
3277}
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279/*
3280 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003281 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003284find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003285 char_u *ptr, // pointer to search pattern
3286 int dir UNUSED, // direction of expansion
3287 int len, // length of search pattern
3288 int whole, // match whole words only
3289 int skip_comments, // don't match inside comments
3290 int type, // Type of search; are we looking for a type?
3291 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003292 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003293 int action, // What to do when we find it
3294 linenr_T start_lnum, // first line to start searching
Colin Kennedy21570352024-03-03 16:16:47 +01003295 linenr_T end_lnum, // last line for searching
3296 int forceit) // If true, always switch to the found path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003298 SearchedFile *files; // Stack of included files
3299 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 int max_path_depth = 50;
3301 long match_count = 1;
3302
3303 char_u *pat;
3304 char_u *new_fname;
3305 char_u *curr_fname = curbuf->b_fname;
3306 char_u *prev_fname = NULL;
3307 linenr_T lnum;
3308 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003309 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 int old_files;
3311 int already_searched;
3312 char_u *file_line;
3313 char_u *line;
3314 char_u *p;
3315 char_u save_char;
3316 int define_matched;
3317 regmatch_T regmatch;
3318 regmatch_T incl_regmatch;
3319 regmatch_T def_regmatch;
3320 int matched = FALSE;
3321 int did_show = FALSE;
3322 int found = FALSE;
3323 int i;
3324 char_u *already = NULL;
3325 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003326 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003327#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 win_T *curwin_save = NULL;
3329#endif
3330
3331 regmatch.regprog = NULL;
3332 incl_regmatch.regprog = NULL;
3333 def_regmatch.regprog = NULL;
3334
3335 file_line = alloc(LSIZE);
3336 if (file_line == NULL)
3337 return;
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003340 // when CONT_SOL is set compare "ptr" with the beginning of the
3341 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3342 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344 pat = alloc(len + 5);
3345 if (pat == NULL)
3346 goto fpip_end;
3347 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003348 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003350 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 vim_free(pat);
3352 if (regmatch.regprog == NULL)
3353 goto fpip_end;
3354 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003355 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3356 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003358 incl_regmatch.regprog = vim_regcomp(inc_opt,
3359 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (incl_regmatch.regprog == NULL)
3361 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003362 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 }
3364 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3365 {
3366 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003367 ? p_def : curbuf->b_p_def,
3368 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (def_regmatch.regprog == NULL)
3370 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003371 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003373 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (files == NULL)
3375 goto fpip_end;
3376 old_files = max_path_depth;
3377 depth = depth_displayed = -1;
3378
3379 lnum = start_lnum;
3380 if (end_lnum > curbuf->b_ml.ml_line_count)
3381 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003382 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003384 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385
3386 for (;;)
3387 {
3388 if (incl_regmatch.regprog != NULL
3389 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3390 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003391 char_u *p_fname = (curr_fname == curbuf->b_fname)
3392 ? curbuf->b_ffname : curr_fname;
3393
3394 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003395 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003396 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003397 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003398 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3399 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003400 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003401 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003402 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 already_searched = FALSE;
3404 if (new_fname != NULL)
3405 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003406 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 for (i = 0;; i++)
3408 {
3409 if (i == depth + 1)
3410 i = old_files;
3411 if (i == max_path_depth)
3412 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003413 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3414 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003416 if (type != CHECK_PATH
3417 && action == ACTION_SHOW_ALL
3418 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003420 msg_putchar('\n'); // cursor below last one
3421 if (!got_int) // don't display if 'q'
3422 // typed at "--more--"
3423 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003426 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 prev_fname = NULL;
3428 }
3429 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003430 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 already_searched = TRUE;
3432 break;
3433 }
3434 }
3435 }
3436
3437 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3438 || (new_fname == NULL && !already_searched)))
3439 {
3440 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003441 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 else
3443 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003444 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003445 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003447 msg_puts_title(_("not found "));
3448 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450 did_show = TRUE;
3451 while (depth_displayed < depth && !got_int)
3452 {
3453 ++depth_displayed;
3454 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003455 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003457 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003459 if (!got_int) // don't display if 'q' typed
3460 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003463 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 if (new_fname != NULL)
3465 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003466 // using "new_fname" is more reliable, e.g., when
3467 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003468 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470 else
3471 {
3472 /*
3473 * Isolate the file name.
3474 * Include the surrounding "" or <> if present.
3475 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003476 if (inc_opt != NULL
3477 && strstr((char *)inc_opt, "\\zs") != NULL)
3478 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003479 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003480 p = incl_regmatch.startp[0];
3481 i = (int)(incl_regmatch.endp[0]
3482 - incl_regmatch.startp[0]);
3483 }
3484 else
3485 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003486 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003487 for (p = incl_regmatch.endp[0];
3488 *p && !vim_isfilec(*p); p++)
3489 ;
3490 for (i = 0; vim_isfilec(p[i]); i++)
3491 ;
3492 }
3493
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 if (i == 0)
3495 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003496 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003498 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003500 // Avoid checking before the start of the line, can
3501 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003502 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 if (p[-1] == '"' || p[-1] == '<')
3505 {
3506 --p;
3507 ++i;
3508 }
3509 if (p[i] == '"' || p[i] == '>')
3510 ++i;
3511 }
3512 save_char = p[i];
3513 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003514 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 p[i] = save_char;
3516 }
3517
3518 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3519 {
3520 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003521 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003523 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003526 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528
3529 if (new_fname != NULL)
3530 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003531 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (depth + 1 == old_files)
3533 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003534 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (bigger != NULL)
3536 {
3537 for (i = 0; i <= depth; i++)
3538 bigger[i] = files[i];
3539 for (i = depth + 1; i < old_files + max_path_depth; i++)
3540 {
3541 bigger[i].fp = NULL;
3542 bigger[i].name = NULL;
3543 bigger[i].lnum = 0;
3544 bigger[i].matched = FALSE;
3545 }
3546 for (i = old_files; i < max_path_depth; i++)
3547 bigger[i + max_path_depth] = files[i];
3548 old_files += max_path_depth;
3549 max_path_depth *= 2;
3550 vim_free(files);
3551 files = bigger;
3552 }
3553 }
3554 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3555 == NULL)
3556 vim_free(new_fname);
3557 else
3558 {
3559 if (++depth == old_files)
3560 {
3561 /*
3562 * lalloc() for 'bigger' must have failed above. We
3563 * will forget one of our already visited files now.
3564 */
3565 vim_free(files[old_files].name);
3566 ++old_files;
3567 }
3568 files[depth].name = curr_fname = new_fname;
3569 files[depth].lnum = 0;
3570 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 if (action == ACTION_EXPAND)
3572 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003573 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003574 vim_snprintf((char*)IObuff, IOSIZE,
3575 _("Scanning included file: %s"),
3576 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003577 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003579 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003580 {
3581 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003582 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003583 (char *)new_fname);
3584 verbose_leave();
3585 }
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 }
3588 }
3589 }
3590 else
3591 {
3592 /*
3593 * Check if the line is a define (type == FIND_DEFINE)
3594 */
3595 p = line;
3596search_line:
3597 define_matched = FALSE;
3598 if (def_regmatch.regprog != NULL
3599 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3600 {
3601 /*
3602 * Pattern must be first identifier after 'define', so skip
3603 * to that position before checking for match of pattern. Also
3604 * don't let it match beyond the end of this identifier.
3605 */
3606 p = def_regmatch.endp[0];
3607 while (*p && !vim_iswordc(*p))
3608 p++;
3609 define_matched = TRUE;
3610 }
3611
3612 /*
3613 * Look for a match. Don't do this if we are looking for a
3614 * define and this line didn't match define_prog above.
3615 */
3616 if (def_regmatch.regprog == NULL || define_matched)
3617 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003618 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003620 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 startp = skipwhite(p);
3622 if (p_ic)
3623 matched = !MB_STRNICMP(startp, ptr, len);
3624 else
3625 matched = !STRNCMP(startp, ptr, len);
3626 if (matched && define_matched && whole
3627 && vim_iswordc(startp[len]))
3628 matched = FALSE;
3629 }
3630 else if (regmatch.regprog != NULL
3631 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3632 {
3633 matched = TRUE;
3634 startp = regmatch.startp[0];
3635 /*
3636 * Check if the line is not a comment line (unless we are
3637 * looking for a define). A line starting with "# define"
3638 * is not considered to be a comment line.
3639 */
3640 if (!define_matched && skip_comments)
3641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 if ((*line != '#' ||
3643 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003644 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 matched = FALSE;
3646
3647 /*
3648 * Also check for a "/ *" or "/ /" before the match.
3649 * Skips lines like "int backwards; / * normal index
3650 * * /" when looking for "normal".
3651 * Note: Doesn't skip "/ *" in comments.
3652 */
3653 p = skipwhite(line);
3654 if (matched
3655 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 for (p = line; *p && p < startp; ++p)
3657 {
3658 if (matched
3659 && p[0] == '/'
3660 && (p[1] == '*' || p[1] == '/'))
3661 {
3662 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003663 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 if (p[1] == '/')
3665 break;
3666 ++p;
3667 }
3668 else if (!matched && p[0] == '*' && p[1] == '/')
3669 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003670 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 matched = TRUE;
3672 ++p;
3673 }
3674 }
3675 }
3676 }
3677 }
3678 }
3679 if (matched)
3680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 if (action == ACTION_EXPAND)
3682 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003683 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 int add_r;
3685 char_u *aux;
3686
3687 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3688 break;
3689 found = TRUE;
3690 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003691 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003693 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 if (vim_iswordp(p))
3695 goto exit_matched;
3696 p = find_word_start(p);
3697 }
3698 p = find_word_end(p);
3699 i = (int)(p - aux);
3700
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003701 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003703 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003705
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003706 // Get the next line: when "depth" < 0 from the current
3707 // buffer, otherwise from the included file. Jump to
3708 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003709 if (depth < 0)
3710 {
3711 if (lnum >= end_lnum)
3712 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003713 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003714 }
3715 else if (vim_fgets(line = file_line,
3716 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 goto exit_matched;
3718
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003719 // we read a line, set "already" to check this "line" later
3720 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003721 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 already = aux = p = skipwhite(line);
3723 p = find_word_start(p);
3724 p = find_word_end(p);
3725 if (p > aux)
3726 {
3727 if (*aux != ')' && IObuff[i-1] != TAB)
3728 {
3729 if (IObuff[i-1] != ' ')
3730 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003731 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 if (p_js
3733 && (IObuff[i-2] == '.'
3734 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3735 && (IObuff[i-2] == '?'
3736 || IObuff[i-2] == '!'))))
3737 IObuff[i++] = ' ';
3738 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003739 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (p - aux >= IOSIZE - i)
3741 p = aux + IOSIZE - i - 1;
3742 STRNCPY(IObuff + i, aux, p - aux);
3743 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003744 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 }
3746 IObuff[i] = NUL;
3747 aux = IObuff;
3748
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003749 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 goto exit_matched;
3751 }
3752
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003753 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003755 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003757 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003759 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 break;
3761 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003762 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 {
3764 found = TRUE;
3765 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003766 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (curr_fname != prev_fname)
3768 {
3769 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003770 msg_putchar('\n'); // cursor below last one
3771 if (!got_int) // don't display if 'q' typed
3772 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 msg_home_replace_hl(curr_fname);
3774 prev_fname = curr_fname;
3775 }
3776 did_show = TRUE;
3777 if (!got_int)
3778 show_pat_in_path(line, type, TRUE, action,
3779 (depth == -1) ? NULL : files[depth].fp,
3780 (depth == -1) ? &lnum : &files[depth].lnum,
3781 match_count++);
3782
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003783 // Set matched flag for this file and all the ones that
3784 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 for (i = 0; i <= depth; ++i)
3786 files[i].matched = TRUE;
3787 }
3788 else if (--count <= 0)
3789 {
3790 found = TRUE;
3791 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003792#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 && g_do_tagpreview == 0
3794#endif
3795 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003796 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 else if (action == ACTION_SHOW)
3798 {
3799 show_pat_in_path(line, type, did_show, action,
3800 (depth == -1) ? NULL : files[depth].fp,
3801 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3802 did_show = TRUE;
3803 }
3804 else
3805 {
3806#ifdef FEAT_GUI
3807 need_mouse_correct = TRUE;
3808#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003809#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003810 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (g_do_tagpreview != 0)
3812 {
3813 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003814 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816#endif
3817 if (action == ACTION_SPLIT)
3818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003821 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 if (depth == -1)
3824 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003825 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003826#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (g_do_tagpreview != 0)
3828 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003829 if (!win_valid(curwin_save))
3830 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003831 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003832 curwin_save->w_buffer->b_fnum, NULL,
Colin Kennedy21570352024-03-03 16:16:47 +01003833 NULL, TRUE, lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003834 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 else
3837#endif
3838 setpcmark();
3839 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003840 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 else
3843 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003844 if (!GETFILE_SUCCESS(getfile(
3845 0, files[depth].name, NULL, TRUE,
Colin Kennedy21570352024-03-03 16:16:47 +01003846 files[depth].lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003847 break; // failed to jump to file
3848 // autocommands may have changed the lnum, we don't
3849 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 curwin->w_cursor.lnum = files[depth].lnum;
3851 }
3852 }
3853 if (action != ACTION_SHOW)
3854 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003855 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 curwin->w_set_curswant = TRUE;
3857 }
3858
Bram Moolenaar4033c552017-09-16 20:54:51 +02003859#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003861 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003863 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003865 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 win_enter(curwin_save, TRUE);
3867 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003868# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003869 else if (WIN_IS_POPUP(curwin))
3870 // can't keep focus in popup window
3871 win_enter(firstwin, TRUE);
3872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873#endif
3874 break;
3875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003878 // look for other matches in the rest of the line if we
3879 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003882 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003883 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003884 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 goto search_line;
3886 }
3887 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003889 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003890 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 break;
3892
3893 /*
3894 * Read the next line. When reading an included file and encountering
3895 * end-of-file, close the file and continue in the file that included
3896 * it.
3897 */
3898 while (depth >= 0 && !already
3899 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3900 {
3901 fclose(files[depth].fp);
3902 --old_files;
3903 files[old_files].name = files[depth].name;
3904 files[old_files].matched = files[depth].matched;
3905 --depth;
3906 curr_fname = (depth == -1) ? curbuf->b_fname
3907 : files[depth].name;
3908 if (depth < depth_displayed)
3909 depth_displayed = depth;
3910 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003911 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003914 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003915 i = (int)STRLEN(line);
3916 if (i > 0 && line[i - 1] == '\n')
3917 line[--i] = NUL;
3918 if (i > 0 && line[i - 1] == '\r')
3919 line[--i] = NUL;
3920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 else if (!already)
3922 {
3923 if (++lnum > end_lnum)
3924 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003925 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927 already = NULL;
3928 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003929 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003931 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 for (i = 0; i <= depth; i++)
3933 {
3934 fclose(files[i].fp);
3935 vim_free(files[i].name);
3936 }
3937 for (i = old_files; i < max_path_depth; i++)
3938 vim_free(files[i].name);
3939 vim_free(files);
3940
3941 if (type == CHECK_PATH)
3942 {
3943 if (!did_show)
3944 {
3945 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003946 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003948 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003951 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003953 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003954 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003956 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003958 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
3961 msg_end();
3962
3963fpip_end:
3964 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02003965 vim_regfree(regmatch.regprog);
3966 vim_regfree(incl_regmatch.regprog);
3967 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968}
3969
3970 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003971show_pat_in_path(
3972 char_u *line,
3973 int type,
3974 int did_show,
3975 int action,
3976 FILE *fp,
3977 linenr_T *lnum,
3978 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
3980 char_u *p;
3981
3982 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003983 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00003984 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003985 gotocmdline(TRUE); // cursor at status line
3986 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return;
3988 for (;;)
3989 {
3990 p = line + STRLEN(line) - 1;
3991 if (fp != NULL)
3992 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003993 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (p >= line && *p == '\n')
3995 --p;
3996 if (p >= line && *p == '\r')
3997 --p;
3998 *(p + 1) = NUL;
3999 }
4000 if (action == ACTION_SHOW_ALL)
4001 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004002 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004003 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004004 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4005 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004006 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4007 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004009 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004010 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004012 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4014 break;
4015
4016 if (fp != NULL)
4017 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004018 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 break;
4020 ++*lnum;
4021 }
4022 else
4023 {
4024 if (++*lnum > curbuf->b_ml.ml_line_count)
4025 break;
4026 line = ml_get(*lnum);
4027 }
4028 msg_putchar('\n');
4029 }
4030}
4031#endif
4032
4033#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004034/*
4035 * Return the last used search pattern at "idx".
4036 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004037 spat_T *
4038get_spat(int idx)
4039{
4040 return &spats[idx];
4041}
4042
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004043/*
4044 * Return the last used search pattern index.
4045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004047get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004049 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004052
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004053#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004054/*
4055 * "searchcount()" function
4056 */
4057 void
4058f_searchcount(typval_T *argvars, typval_T *rettv)
4059{
4060 pos_T pos = curwin->w_cursor;
4061 char_u *pattern = NULL;
4062 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4063 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004064 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004065 searchstat_T stat;
4066
4067 if (rettv_dict_alloc(rettv) == FAIL)
4068 return;
4069
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004070 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4071 return;
4072
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004073 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4074 recompute = TRUE;
4075
4076 if (argvars[0].v_type != VAR_UNKNOWN)
4077 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004078 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004079 dictitem_T *di;
4080 listitem_T *li;
4081 int error = FALSE;
4082
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004083 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004084 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004085 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004086 di = dict_find(dict, (char_u *)"timeout", -1);
4087 if (di != NULL)
4088 {
4089 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4090 if (error)
4091 return;
4092 }
4093 di = dict_find(dict, (char_u *)"maxcount", -1);
4094 if (di != NULL)
4095 {
4096 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4097 if (error)
4098 return;
4099 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004100 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004101 di = dict_find(dict, (char_u *)"pattern", -1);
4102 if (di != NULL)
4103 {
4104 pattern = tv_get_string_chk(&di->di_tv);
4105 if (pattern == NULL)
4106 return;
4107 }
4108 di = dict_find(dict, (char_u *)"pos", -1);
4109 if (di != NULL)
4110 {
4111 if (di->di_tv.v_type != VAR_LIST)
4112 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004113 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004114 return;
4115 }
4116 if (list_len(di->di_tv.vval.v_list) != 3)
4117 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004118 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004119 return;
4120 }
4121 li = list_find(di->di_tv.vval.v_list, 0L);
4122 if (li != NULL)
4123 {
4124 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4125 if (error)
4126 return;
4127 }
4128 li = list_find(di->di_tv.vval.v_list, 1L);
4129 if (li != NULL)
4130 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004131 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004132 if (error)
4133 return;
4134 }
4135 li = list_find(di->di_tv.vval.v_list, 2L);
4136 if (li != NULL)
4137 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004138 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004139 if (error)
4140 return;
4141 }
4142 }
4143 }
4144
4145 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004146#ifdef FEAT_SEARCH_EXTRA
4147 save_incsearch_state();
4148#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004149 if (pattern != NULL)
4150 {
4151 if (*pattern == NUL)
4152 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004153 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004154 spats[last_idx].pat = vim_strsave(pattern);
4155 }
4156 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4157 goto the_end; // the previous pattern was never defined
4158
4159 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4160
4161 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4162 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4163 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4164 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4165 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4166
4167the_end:
4168 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004169#ifdef FEAT_SEARCH_EXTRA
4170 restore_incsearch_state();
4171#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004172}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004173#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004174
4175/*
4176 * Fuzzy string matching
4177 *
4178 * Ported from the lib_fts library authored by Forrest Smith.
4179 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4180 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004181 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004182 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4183 *
4184 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004185 * - Matched letter
4186 * - Unmatched letter
4187 * - Consecutively matched letters
4188 * - Proximity to start
4189 * - Letter following a separator (space, underscore)
4190 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004191 *
4192 * Matched letters are good. Unmatched letters are bad. Matching near the start
4193 * is good. Matching the first letter in the middle of a phrase is good.
4194 * Matching the uppercase letters in camel case entries is good.
4195 *
4196 * The score assigned for each factor is explained below.
4197 * File paths are different from file names. File extensions may be ignorable.
4198 * Single words care about consecutive matches but not separators or camel
4199 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004200 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004201 * Matched letter: +0 points
4202 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004203 * Consecutive match bonus: +15 points
4204 * First letter bonus: +15 points
4205 * Separator bonus: +30 points
4206 * Camel case bonus: +30 points
4207 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004208 *
4209 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004210 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004211 * lower minimum score due to unmatched letter penalty. Longer search patterns
4212 * have a higher maximum score due to match bonuses.
4213 *
4214 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4215 * quite a bit.
4216 *
4217 * There is a penalty if you DON’T match the first three letters. Which
4218 * effectively rewards matching near the start. However there’s no difference
4219 * in matching between the middle and end.
4220 *
4221 * There is not an explicit bonus for an exact match. Unmatched letters receive
4222 * a penalty. So shorter strings and closer matches are worth more.
4223 */
4224typedef struct
4225{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004226 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004227 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004228 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004229 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004230} fuzzyItem_T;
4231
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004232// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4233// matching a whole word is preferred.
4234#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004235// bonus if match occurs after a path separator
4236#define PATH_SEPARATOR_BONUS 30
4237// bonus if match occurs after a word separator
4238#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004239// bonus if match is uppercase and prev is lower
4240#define CAMEL_BONUS 30
4241// bonus if the first letter is matched
4242#define FIRST_LETTER_BONUS 15
4243// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004244#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004245// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004246#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004247// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004248#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004249// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004250#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004251// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004252#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004253
4254#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004255
4256/*
4257 * Compute a score for a fuzzy matched string. The matching character locations
4258 * are in 'matches'.
4259 */
4260 static int
4261fuzzy_match_compute_score(
4262 char_u *str,
4263 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004264 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004265 int numMatches)
4266{
4267 int score;
4268 int penalty;
4269 int unmatched;
4270 int i;
4271 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004272 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004273
4274 // Initialize score
4275 score = 100;
4276
4277 // Apply leading letter penalty
4278 penalty = LEADING_LETTER_PENALTY * matches[0];
4279 if (penalty < MAX_LEADING_LETTER_PENALTY)
4280 penalty = MAX_LEADING_LETTER_PENALTY;
4281 score += penalty;
4282
4283 // Apply unmatched penalty
4284 unmatched = strSz - numMatches;
4285 score += UNMATCHED_LETTER_PENALTY * unmatched;
4286
4287 // Apply ordering bonuses
4288 for (i = 0; i < numMatches; ++i)
4289 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004290 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004291
4292 if (i > 0)
4293 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004294 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004295
4296 // Sequential
4297 if (currIdx == (prevIdx + 1))
4298 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004299 else
4300 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004301 }
4302
4303 // Check for bonuses based on neighbor character value
4304 if (currIdx > 0)
4305 {
4306 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004307 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004308 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004309
4310 if (has_mbyte)
4311 {
4312 while (sidx < currIdx)
4313 {
4314 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004315 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004316 sidx++;
4317 }
4318 curr = (*mb_ptr2char)(p);
4319 }
4320 else
4321 {
4322 neighbor = str[currIdx - 1];
4323 curr = str[currIdx];
4324 }
4325
4326 if (vim_islower(neighbor) && vim_isupper(curr))
4327 score += CAMEL_BONUS;
4328
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004329 // Bonus if the match follows a separator character
4330 if (neighbor == '/' || neighbor == '\\')
4331 score += PATH_SEPARATOR_BONUS;
4332 else if (neighbor == ' ' || neighbor == '_')
4333 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004334 }
4335 else
4336 {
4337 // First letter
4338 score += FIRST_LETTER_BONUS;
4339 }
4340 }
4341 return score;
4342}
4343
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004344/*
4345 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4346 * Return the number of matching characters.
4347 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004348 static int
4349fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004350 char_u *fuzpat,
4351 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004352 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004353 int *outScore,
4354 char_u *strBegin,
4355 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004356 int_u *srcMatches,
4357 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004358 int maxMatches,
4359 int nextMatch,
4360 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004361{
4362 // Recursion params
4363 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004364 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004365 int bestRecursiveScore = 0;
4366 int first_match;
4367 int matched;
4368
4369 // Count recursions
4370 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004371 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004372 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004373
4374 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004375 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004376 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004377
4378 // Loop through fuzpat and str looking for a match
4379 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004380 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004381 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004382 int c1;
4383 int c2;
4384
4385 c1 = PTR2CHAR(fuzpat);
4386 c2 = PTR2CHAR(str);
4387
Bram Moolenaar635414d2020-09-11 22:25:15 +02004388 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004389 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004390 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004391 // Supplied matches buffer was too short
4392 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004393 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004394
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004395 int recursiveScore = 0;
4396 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4397 CLEAR_FIELD(recursiveMatches);
4398
Bram Moolenaar635414d2020-09-11 22:25:15 +02004399 // "Copy-on-Write" srcMatches into matches
4400 if (first_match && srcMatches)
4401 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004402 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004403 first_match = FALSE;
4404 }
4405
4406 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004407 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004408 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4409 &recursiveScore, strBegin, strLen, matches,
4410 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004411 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004412 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004413 {
4414 // Pick best recursive score
4415 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4416 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004417 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004418 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004419 bestRecursiveScore = recursiveScore;
4420 }
4421 recursiveMatch = TRUE;
4422 }
4423
4424 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004425 matches[nextMatch++] = strIdx;
4426 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004427 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004428 else
4429 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004430 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004431 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004432 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004433 else
4434 ++str;
4435 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004436 }
4437
4438 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004439 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004440
4441 // Calculate score
4442 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004443 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4444 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004445
4446 // Return best result
4447 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4448 {
4449 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004450 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004451 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004452 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004453 }
4454 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004455 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004456
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004457 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004458}
4459
4460/*
4461 * fuzzy_match()
4462 *
4463 * Performs exhaustive search via recursion to find all possible matches and
4464 * match with highest score.
4465 * Scores values have no intrinsic meaning. Possible score range is not
4466 * normalized and varies with pattern.
4467 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004468 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004469 * Uses char_u for match indices. Therefore patterns are limited to
4470 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004471 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004472 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4473 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004474 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004475 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004476fuzzy_match(
4477 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004478 char_u *pat_arg,
4479 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004480 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004481 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004482 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004483{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004484 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004485 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004486 char_u *save_pat;
4487 char_u *pat;
4488 char_u *p;
4489 int complete = FALSE;
4490 int score = 0;
4491 int numMatches = 0;
4492 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004493
4494 *outScore = 0;
4495
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004496 save_pat = vim_strsave(pat_arg);
4497 if (save_pat == NULL)
4498 return FALSE;
4499 pat = save_pat;
4500 p = pat;
4501
4502 // Try matching each word in 'pat_arg' in 'str'
4503 while (TRUE)
4504 {
4505 if (matchseq)
4506 complete = TRUE;
4507 else
4508 {
4509 // Extract one word from the pattern (separated by space)
4510 p = skipwhite(p);
4511 if (*p == NUL)
4512 break;
4513 pat = p;
4514 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4515 {
4516 if (has_mbyte)
4517 MB_PTR_ADV(p);
4518 else
4519 ++p;
4520 }
4521 if (*p == NUL) // processed all the words
4522 complete = TRUE;
4523 *p = NUL;
4524 }
4525
4526 score = 0;
4527 recursionCount = 0;
4528 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4529 matches + numMatches, maxMatches - numMatches,
4530 0, &recursionCount);
4531 if (matchCount == 0)
4532 {
4533 numMatches = 0;
4534 break;
4535 }
4536
4537 // Accumulate the match score and the number of matches
4538 *outScore += score;
4539 numMatches += matchCount;
4540
4541 if (complete)
4542 break;
4543
4544 // try matching the next word
4545 ++p;
4546 }
4547
4548 vim_free(save_pat);
4549 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004550}
4551
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004552#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004553/*
4554 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004555 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004556 */
4557 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004558fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004559{
4560 int v1 = ((fuzzyItem_T *)s1)->score;
4561 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004562 int idx1 = ((fuzzyItem_T *)s1)->idx;
4563 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004564
zeertzjq77078272024-02-10 13:24:03 +01004565 if (v1 == v2)
4566 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4567 else
4568 return v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004569}
4570
4571/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004572 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4573 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004574 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4575 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004576 * If 'items' is a list of strings, then search for 'str' in the list.
4577 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4578 * for each item or use 'item_cb' Funcref function to get the string.
4579 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4580 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004581 */
4582 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004583fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004584 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004585 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004586 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004587 char_u *key,
4588 callback_T *item_cb,
4589 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004590 list_T *fmatchlist,
4591 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004592{
4593 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004594 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004595 listitem_T *li;
4596 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004597 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004598 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004599
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004600 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004601 if (len == 0)
4602 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004603 if (max_matches > 0 && len > max_matches)
4604 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004605
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004606 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4607 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004608 return;
4609
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004610 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004611 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004612 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004613 int score;
4614 char_u *itemstr;
4615 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004616
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004617 if (max_matches > 0 && match_count >= max_matches)
4618 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004619
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004620 itemstr = NULL;
4621 rettv.v_type = VAR_UNKNOWN;
4622 if (li->li_tv.v_type == VAR_STRING) // list of strings
4623 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004624 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004625 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004626 {
4627 // For a dict, either use the specified key to lookup the string or
4628 // use the specified callback function to get the string.
4629 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004630 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4631 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004632 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004633 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004634 typval_T argv[2];
4635
4636 // Invoke the supplied callback (if any) to get the dict item
4637 li->li_tv.vval.v_dict->dv_refcount++;
4638 argv[0].v_type = VAR_DICT;
4639 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4640 argv[1].v_type = VAR_UNKNOWN;
4641 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4642 {
4643 if (rettv.v_type == VAR_STRING)
4644 itemstr = rettv.vval.v_string;
4645 }
4646 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004647 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004648 }
4649
4650 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004651 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004652 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004653 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004654 items[match_count].idx = match_count;
4655 items[match_count].item = li;
4656 items[match_count].score = score;
4657
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004658 // Copy the list of matching positions in itemstr to a list, if
4659 // 'retmatchpos' is set.
4660 if (retmatchpos)
4661 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004662 int j = 0;
4663 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004664
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004665 items[match_count].lmatchpos = list_alloc();
4666 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004667 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004668
4669 p = str;
4670 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004671 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004672 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004673 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004674 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004675 matches[j]) == FAIL)
4676 goto done;
4677 j++;
4678 }
4679 if (has_mbyte)
4680 MB_PTR_ADV(p);
4681 else
4682 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004683 }
4684 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004685 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004686 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004687 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004688 }
4689
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004690 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004691 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004692 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004693
Bram Moolenaar635414d2020-09-11 22:25:15 +02004694 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004695 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004696 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004697
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004698 // For matchfuzzy(), return a list of matched strings.
4699 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004700 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004701 // The first item is a list of matched strings. The second item
4702 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004703 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004704 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4705 if (retmatchpos)
4706 {
4707 li = list_find(fmatchlist, 0);
4708 if (li == NULL || li->li_tv.vval.v_list == NULL)
4709 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004710 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004711 }
4712 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004713 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004714
4715 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004716 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004717 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004718 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004719 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004720 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004721 }
4722
4723 // next copy the list of matching positions
4724 if (retmatchpos)
4725 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004726 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004727 if (li == NULL || li->li_tv.vval.v_list == NULL)
4728 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004729 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004730
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004731 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004732 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004733 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004734 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004735 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004736 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004737 goto done;
4738 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004739
4740 // copy the matching scores
4741 li = list_find(fmatchlist, -1);
4742 if (li == NULL || li->li_tv.vval.v_list == NULL)
4743 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004744 retlist = li->li_tv.vval.v_list;
4745 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004746 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004747 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004748 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004749 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004750 goto done;
4751 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004752 }
4753 }
4754
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004755done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004756 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004757}
4758
4759/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004760 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4761 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4762 */
4763 static void
4764do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4765{
4766 callback_T cb;
4767 char_u *key = NULL;
4768 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004769 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004770 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004771
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004772 if (in_vim9script()
4773 && (check_for_list_arg(argvars, 0) == FAIL
4774 || check_for_string_arg(argvars, 1) == FAIL
4775 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4776 return;
4777
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004778 CLEAR_POINTER(&cb);
4779
4780 // validate and get the arguments
4781 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4782 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004783 semsg(_(e_argument_of_str_must_be_list),
4784 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004785 return;
4786 }
4787 if (argvars[1].v_type != VAR_STRING
4788 || argvars[1].vval.v_string == NULL)
4789 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004790 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004791 return;
4792 }
4793
4794 if (argvars[2].v_type != VAR_UNKNOWN)
4795 {
4796 dict_T *d;
4797 dictitem_T *di;
4798
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004799 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004800 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004801
4802 // To search a dict, either a callback function or a key can be
4803 // specified.
4804 d = argvars[2].vval.v_dict;
4805 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4806 {
4807 if (di->di_tv.v_type != VAR_STRING
4808 || di->di_tv.vval.v_string == NULL
4809 || *di->di_tv.vval.v_string == NUL)
4810 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004811 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004812 return;
4813 }
4814 key = tv_get_string(&di->di_tv);
4815 }
4816 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4817 {
4818 cb = get_callback(&di->di_tv);
4819 if (cb.cb_name == NULL)
4820 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004821 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004822 return;
4823 }
4824 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004825
4826 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004827 {
4828 if (di->di_tv.v_type != VAR_NUMBER)
4829 {
4830 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4831 return;
4832 }
4833 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4834 }
4835
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004836 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004837 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004838 }
4839
4840 // get the fuzzy matches
4841 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004842 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004843 goto done;
4844 if (retmatchpos)
4845 {
4846 list_T *l;
4847
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004848 // For matchfuzzypos(), a list with three items are returned. First
4849 // item is a list of matching strings, the second item is a list of
4850 // lists with matching positions within each string and the third item
4851 // is the list of scores of the matches.
4852 l = list_alloc();
4853 if (l == NULL)
4854 goto done;
4855 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004856 {
4857 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004858 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004859 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004860 l = list_alloc();
4861 if (l == NULL)
4862 goto done;
4863 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004864 {
4865 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004866 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004867 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004868 l = list_alloc();
4869 if (l == NULL)
4870 goto done;
4871 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004872 {
4873 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004874 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004875 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004876 }
4877
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004878 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004879 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004880
4881done:
4882 free_callback(&cb);
4883}
4884
4885/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004886 * "matchfuzzy()" function
4887 */
4888 void
4889f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4890{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004891 do_fuzzymatch(argvars, rettv, FALSE);
4892}
4893
4894/*
4895 * "matchfuzzypos()" function
4896 */
4897 void
4898f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
4899{
4900 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004901}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004902#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004903
4904/*
4905 * Same as fuzzy_match_item_compare() except for use with a string match
4906 */
4907 static int
4908fuzzy_match_str_compare(const void *s1, const void *s2)
4909{
4910 int v1 = ((fuzmatch_str_T *)s1)->score;
4911 int v2 = ((fuzmatch_str_T *)s2)->score;
4912 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4913 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4914
Christian Brabandte06e4372024-02-09 19:39:14 +01004915 if (v1 == v2)
4916 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4917 else
4918 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004919}
4920
4921/*
4922 * Sort fuzzy matches by score
4923 */
4924 static void
4925fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
4926{
4927 // Sort the list by the descending order of the match score
4928 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4929 fuzzy_match_str_compare);
4930}
4931
4932/*
4933 * Same as fuzzy_match_item_compare() except for use with a function name
4934 * string match. <SNR> functions should be sorted to the end.
4935 */
4936 static int
4937fuzzy_match_func_compare(const void *s1, const void *s2)
4938{
4939 int v1 = ((fuzmatch_str_T *)s1)->score;
4940 int v2 = ((fuzmatch_str_T *)s2)->score;
4941 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4942 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4943 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
4944 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
4945
Christian Brabandte06e4372024-02-09 19:39:14 +01004946 if (*str1 != '<' && *str2 == '<')
4947 return -1;
4948 if (*str1 == '<' && *str2 != '<')
4949 return 1;
4950 if (v1 == v2)
4951 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4952 else
4953 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004954}
4955
4956/*
4957 * Sort fuzzy matches of function names by score.
4958 * <SNR> functions should be sorted to the end.
4959 */
4960 static void
4961fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
4962{
4963 // Sort the list by the descending order of the match score
4964 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4965 fuzzy_match_func_compare);
4966}
4967
4968/*
4969 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
4970 * returns the match score.
4971 */
4972 int
4973fuzzy_match_str(char_u *str, char_u *pat)
4974{
4975 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00004976 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004977
4978 if (str == NULL || pat == NULL)
4979 return 0;
4980
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00004981 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004982 sizeof(matchpos) / sizeof(matchpos[0]));
4983
4984 return score;
4985}
4986
4987/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01004988 * Free an array of fuzzy string matches "fuzmatch[count]".
4989 */
4990 void
4991fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
4992{
4993 int i;
4994
4995 if (fuzmatch == NULL)
4996 return;
4997 for (i = 0; i < count; ++i)
4998 vim_free(fuzmatch[i].str);
4999 vim_free(fuzmatch);
5000}
5001
5002/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005003 * Copy a list of fuzzy matches into a string list after sorting the matches by
5004 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5005 * Returns OK on success and FAIL on memory allocation failure.
5006 */
5007 int
5008fuzzymatches_to_strmatches(
5009 fuzmatch_str_T *fuzmatch,
5010 char_u ***matches,
5011 int count,
5012 int funcsort)
5013{
5014 int i;
5015
5016 if (count <= 0)
5017 return OK;
5018
5019 *matches = ALLOC_MULT(char_u *, count);
5020 if (*matches == NULL)
5021 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005022 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005023 return FAIL;
5024 }
5025
5026 // Sort the list by the descending order of the match score
5027 if (funcsort)
5028 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5029 else
5030 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5031
5032 for (i = 0; i < count; i++)
5033 (*matches)[i] = fuzmatch[i].str;
5034 vim_free(fuzmatch);
5035
5036 return OK;
5037}