blob: d4baa9192ccbeed8d402cb5d6ff8cfb37294d1e2 [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
3295 linenr_T end_lnum) // last line for searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003297 SearchedFile *files; // Stack of included files
3298 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 int max_path_depth = 50;
3300 long match_count = 1;
3301
3302 char_u *pat;
3303 char_u *new_fname;
3304 char_u *curr_fname = curbuf->b_fname;
3305 char_u *prev_fname = NULL;
3306 linenr_T lnum;
3307 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003308 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 int old_files;
3310 int already_searched;
3311 char_u *file_line;
3312 char_u *line;
3313 char_u *p;
3314 char_u save_char;
3315 int define_matched;
3316 regmatch_T regmatch;
3317 regmatch_T incl_regmatch;
3318 regmatch_T def_regmatch;
3319 int matched = FALSE;
3320 int did_show = FALSE;
3321 int found = FALSE;
3322 int i;
3323 char_u *already = NULL;
3324 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003325 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003326#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 win_T *curwin_save = NULL;
3328#endif
3329
3330 regmatch.regprog = NULL;
3331 incl_regmatch.regprog = NULL;
3332 def_regmatch.regprog = NULL;
3333
3334 file_line = alloc(LSIZE);
3335 if (file_line == NULL)
3336 return;
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003339 // when CONT_SOL is set compare "ptr" with the beginning of the
3340 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3341 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 {
3343 pat = alloc(len + 5);
3344 if (pat == NULL)
3345 goto fpip_end;
3346 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003347 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003349 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 vim_free(pat);
3351 if (regmatch.regprog == NULL)
3352 goto fpip_end;
3353 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003354 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3355 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003357 incl_regmatch.regprog = vim_regcomp(inc_opt,
3358 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 if (incl_regmatch.regprog == NULL)
3360 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003361 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3364 {
3365 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003366 ? p_def : curbuf->b_p_def,
3367 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (def_regmatch.regprog == NULL)
3369 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003370 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003372 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (files == NULL)
3374 goto fpip_end;
3375 old_files = max_path_depth;
3376 depth = depth_displayed = -1;
3377
3378 lnum = start_lnum;
3379 if (end_lnum > curbuf->b_ml.ml_line_count)
3380 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003381 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003383 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 for (;;)
3386 {
3387 if (incl_regmatch.regprog != NULL
3388 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3389 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003390 char_u *p_fname = (curr_fname == curbuf->b_fname)
3391 ? curbuf->b_ffname : curr_fname;
3392
3393 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003394 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003395 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003396 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003397 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3398 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003399 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003400 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003401 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 already_searched = FALSE;
3403 if (new_fname != NULL)
3404 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003405 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 for (i = 0;; i++)
3407 {
3408 if (i == depth + 1)
3409 i = old_files;
3410 if (i == max_path_depth)
3411 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003412 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3413 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003415 if (type != CHECK_PATH
3416 && action == ACTION_SHOW_ALL
3417 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003419 msg_putchar('\n'); // cursor below last one
3420 if (!got_int) // don't display if 'q'
3421 // typed at "--more--"
3422 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
3424 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003425 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 prev_fname = NULL;
3427 }
3428 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003429 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 already_searched = TRUE;
3431 break;
3432 }
3433 }
3434 }
3435
3436 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3437 || (new_fname == NULL && !already_searched)))
3438 {
3439 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003440 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 else
3442 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003443 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003444 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003446 msg_puts_title(_("not found "));
3447 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449 did_show = TRUE;
3450 while (depth_displayed < depth && !got_int)
3451 {
3452 ++depth_displayed;
3453 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003454 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003456 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003458 if (!got_int) // don't display if 'q' typed
3459 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003462 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (new_fname != NULL)
3464 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003465 // using "new_fname" is more reliable, e.g., when
3466 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003467 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 }
3469 else
3470 {
3471 /*
3472 * Isolate the file name.
3473 * Include the surrounding "" or <> if present.
3474 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003475 if (inc_opt != NULL
3476 && strstr((char *)inc_opt, "\\zs") != NULL)
3477 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003478 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003479 p = incl_regmatch.startp[0];
3480 i = (int)(incl_regmatch.endp[0]
3481 - incl_regmatch.startp[0]);
3482 }
3483 else
3484 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003485 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003486 for (p = incl_regmatch.endp[0];
3487 *p && !vim_isfilec(*p); p++)
3488 ;
3489 for (i = 0; vim_isfilec(p[i]); i++)
3490 ;
3491 }
3492
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 if (i == 0)
3494 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003495 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003497 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003499 // Avoid checking before the start of the line, can
3500 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003501 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 if (p[-1] == '"' || p[-1] == '<')
3504 {
3505 --p;
3506 ++i;
3507 }
3508 if (p[i] == '"' || p[i] == '>')
3509 ++i;
3510 }
3511 save_char = p[i];
3512 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003513 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 p[i] = save_char;
3515 }
3516
3517 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3518 {
3519 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003520 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003522 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003525 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527
3528 if (new_fname != NULL)
3529 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003530 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (depth + 1 == old_files)
3532 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003533 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (bigger != NULL)
3535 {
3536 for (i = 0; i <= depth; i++)
3537 bigger[i] = files[i];
3538 for (i = depth + 1; i < old_files + max_path_depth; i++)
3539 {
3540 bigger[i].fp = NULL;
3541 bigger[i].name = NULL;
3542 bigger[i].lnum = 0;
3543 bigger[i].matched = FALSE;
3544 }
3545 for (i = old_files; i < max_path_depth; i++)
3546 bigger[i + max_path_depth] = files[i];
3547 old_files += max_path_depth;
3548 max_path_depth *= 2;
3549 vim_free(files);
3550 files = bigger;
3551 }
3552 }
3553 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3554 == NULL)
3555 vim_free(new_fname);
3556 else
3557 {
3558 if (++depth == old_files)
3559 {
3560 /*
3561 * lalloc() for 'bigger' must have failed above. We
3562 * will forget one of our already visited files now.
3563 */
3564 vim_free(files[old_files].name);
3565 ++old_files;
3566 }
3567 files[depth].name = curr_fname = new_fname;
3568 files[depth].lnum = 0;
3569 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 if (action == ACTION_EXPAND)
3571 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003572 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003573 vim_snprintf((char*)IObuff, IOSIZE,
3574 _("Scanning included file: %s"),
3575 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003576 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003578 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003579 {
3580 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003581 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003582 (char *)new_fname);
3583 verbose_leave();
3584 }
3585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 }
3587 }
3588 }
3589 else
3590 {
3591 /*
3592 * Check if the line is a define (type == FIND_DEFINE)
3593 */
3594 p = line;
3595search_line:
3596 define_matched = FALSE;
3597 if (def_regmatch.regprog != NULL
3598 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3599 {
3600 /*
3601 * Pattern must be first identifier after 'define', so skip
3602 * to that position before checking for match of pattern. Also
3603 * don't let it match beyond the end of this identifier.
3604 */
3605 p = def_regmatch.endp[0];
3606 while (*p && !vim_iswordc(*p))
3607 p++;
3608 define_matched = TRUE;
3609 }
3610
3611 /*
3612 * Look for a match. Don't do this if we are looking for a
3613 * define and this line didn't match define_prog above.
3614 */
3615 if (def_regmatch.regprog == NULL || define_matched)
3616 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003617 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003619 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 startp = skipwhite(p);
3621 if (p_ic)
3622 matched = !MB_STRNICMP(startp, ptr, len);
3623 else
3624 matched = !STRNCMP(startp, ptr, len);
3625 if (matched && define_matched && whole
3626 && vim_iswordc(startp[len]))
3627 matched = FALSE;
3628 }
3629 else if (regmatch.regprog != NULL
3630 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3631 {
3632 matched = TRUE;
3633 startp = regmatch.startp[0];
3634 /*
3635 * Check if the line is not a comment line (unless we are
3636 * looking for a define). A line starting with "# define"
3637 * is not considered to be a comment line.
3638 */
3639 if (!define_matched && skip_comments)
3640 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 if ((*line != '#' ||
3642 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003643 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 matched = FALSE;
3645
3646 /*
3647 * Also check for a "/ *" or "/ /" before the match.
3648 * Skips lines like "int backwards; / * normal index
3649 * * /" when looking for "normal".
3650 * Note: Doesn't skip "/ *" in comments.
3651 */
3652 p = skipwhite(line);
3653 if (matched
3654 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 for (p = line; *p && p < startp; ++p)
3656 {
3657 if (matched
3658 && p[0] == '/'
3659 && (p[1] == '*' || p[1] == '/'))
3660 {
3661 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003662 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 if (p[1] == '/')
3664 break;
3665 ++p;
3666 }
3667 else if (!matched && p[0] == '*' && p[1] == '/')
3668 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003669 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 matched = TRUE;
3671 ++p;
3672 }
3673 }
3674 }
3675 }
3676 }
3677 }
3678 if (matched)
3679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (action == ACTION_EXPAND)
3681 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003682 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 int add_r;
3684 char_u *aux;
3685
3686 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3687 break;
3688 found = TRUE;
3689 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003690 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003692 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (vim_iswordp(p))
3694 goto exit_matched;
3695 p = find_word_start(p);
3696 }
3697 p = find_word_end(p);
3698 i = (int)(p - aux);
3699
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003700 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003702 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003704
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003705 // Get the next line: when "depth" < 0 from the current
3706 // buffer, otherwise from the included file. Jump to
3707 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003708 if (depth < 0)
3709 {
3710 if (lnum >= end_lnum)
3711 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003712 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003713 }
3714 else if (vim_fgets(line = file_line,
3715 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 goto exit_matched;
3717
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003718 // we read a line, set "already" to check this "line" later
3719 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003720 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 already = aux = p = skipwhite(line);
3722 p = find_word_start(p);
3723 p = find_word_end(p);
3724 if (p > aux)
3725 {
3726 if (*aux != ')' && IObuff[i-1] != TAB)
3727 {
3728 if (IObuff[i-1] != ' ')
3729 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003730 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (p_js
3732 && (IObuff[i-2] == '.'
3733 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3734 && (IObuff[i-2] == '?'
3735 || IObuff[i-2] == '!'))))
3736 IObuff[i++] = ' ';
3737 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003738 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 if (p - aux >= IOSIZE - i)
3740 p = aux + IOSIZE - i - 1;
3741 STRNCPY(IObuff + i, aux, p - aux);
3742 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003743 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 }
3745 IObuff[i] = NUL;
3746 aux = IObuff;
3747
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003748 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 goto exit_matched;
3750 }
3751
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003752 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003754 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003756 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003758 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 break;
3760 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003761 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
3763 found = TRUE;
3764 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003765 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (curr_fname != prev_fname)
3767 {
3768 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003769 msg_putchar('\n'); // cursor below last one
3770 if (!got_int) // don't display if 'q' typed
3771 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 msg_home_replace_hl(curr_fname);
3773 prev_fname = curr_fname;
3774 }
3775 did_show = TRUE;
3776 if (!got_int)
3777 show_pat_in_path(line, type, TRUE, action,
3778 (depth == -1) ? NULL : files[depth].fp,
3779 (depth == -1) ? &lnum : &files[depth].lnum,
3780 match_count++);
3781
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003782 // Set matched flag for this file and all the ones that
3783 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 for (i = 0; i <= depth; ++i)
3785 files[i].matched = TRUE;
3786 }
3787 else if (--count <= 0)
3788 {
3789 found = TRUE;
3790 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003791#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 && g_do_tagpreview == 0
3793#endif
3794 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003795 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 else if (action == ACTION_SHOW)
3797 {
3798 show_pat_in_path(line, type, did_show, action,
3799 (depth == -1) ? NULL : files[depth].fp,
3800 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3801 did_show = TRUE;
3802 }
3803 else
3804 {
3805#ifdef FEAT_GUI
3806 need_mouse_correct = TRUE;
3807#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003808#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003809 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if (g_do_tagpreview != 0)
3811 {
3812 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003813 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815#endif
3816 if (action == ACTION_SPLIT)
3817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003820 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 if (depth == -1)
3823 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003824 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003825#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (g_do_tagpreview != 0)
3827 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003828 if (!win_valid(curwin_save))
3829 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003830 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003831 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003832 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003833 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
3835 else
3836#endif
3837 setpcmark();
3838 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003839 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 else
3842 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003843 if (!GETFILE_SUCCESS(getfile(
3844 0, files[depth].name, NULL, TRUE,
3845 files[depth].lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003846 break; // failed to jump to file
3847 // autocommands may have changed the lnum, we don't
3848 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 curwin->w_cursor.lnum = files[depth].lnum;
3850 }
3851 }
3852 if (action != ACTION_SHOW)
3853 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003854 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 curwin->w_set_curswant = TRUE;
3856 }
3857
Bram Moolenaar4033c552017-09-16 20:54:51 +02003858#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003860 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003862 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003864 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 win_enter(curwin_save, TRUE);
3866 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003867# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003868 else if (WIN_IS_POPUP(curwin))
3869 // can't keep focus in popup window
3870 win_enter(firstwin, TRUE);
3871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872#endif
3873 break;
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003877 // look for other matches in the rest of the line if we
3878 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003881 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003882 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003883 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 goto search_line;
3885 }
3886 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003888 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003889 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 break;
3891
3892 /*
3893 * Read the next line. When reading an included file and encountering
3894 * end-of-file, close the file and continue in the file that included
3895 * it.
3896 */
3897 while (depth >= 0 && !already
3898 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3899 {
3900 fclose(files[depth].fp);
3901 --old_files;
3902 files[old_files].name = files[depth].name;
3903 files[old_files].matched = files[depth].matched;
3904 --depth;
3905 curr_fname = (depth == -1) ? curbuf->b_fname
3906 : files[depth].name;
3907 if (depth < depth_displayed)
3908 depth_displayed = depth;
3909 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003910 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003913 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003914 i = (int)STRLEN(line);
3915 if (i > 0 && line[i - 1] == '\n')
3916 line[--i] = NUL;
3917 if (i > 0 && line[i - 1] == '\r')
3918 line[--i] = NUL;
3919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 else if (!already)
3921 {
3922 if (++lnum > end_lnum)
3923 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003924 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
3926 already = NULL;
3927 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003928 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003930 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 for (i = 0; i <= depth; i++)
3932 {
3933 fclose(files[i].fp);
3934 vim_free(files[i].name);
3935 }
3936 for (i = old_files; i < max_path_depth; i++)
3937 vim_free(files[i].name);
3938 vim_free(files);
3939
3940 if (type == CHECK_PATH)
3941 {
3942 if (!did_show)
3943 {
3944 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003945 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003947 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003950 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003952 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003953 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003955 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003957 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
3960 msg_end();
3961
3962fpip_end:
3963 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02003964 vim_regfree(regmatch.regprog);
3965 vim_regfree(incl_regmatch.regprog);
3966 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967}
3968
3969 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003970show_pat_in_path(
3971 char_u *line,
3972 int type,
3973 int did_show,
3974 int action,
3975 FILE *fp,
3976 linenr_T *lnum,
3977 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 char_u *p;
3980
3981 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003982 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00003983 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003984 gotocmdline(TRUE); // cursor at status line
3985 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 return;
3987 for (;;)
3988 {
3989 p = line + STRLEN(line) - 1;
3990 if (fp != NULL)
3991 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003992 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (p >= line && *p == '\n')
3994 --p;
3995 if (p >= line && *p == '\r')
3996 --p;
3997 *(p + 1) = NUL;
3998 }
3999 if (action == ACTION_SHOW_ALL)
4000 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004001 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004002 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004003 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4004 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004005 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4006 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004008 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004009 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004011 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4013 break;
4014
4015 if (fp != NULL)
4016 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004017 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 break;
4019 ++*lnum;
4020 }
4021 else
4022 {
4023 if (++*lnum > curbuf->b_ml.ml_line_count)
4024 break;
4025 line = ml_get(*lnum);
4026 }
4027 msg_putchar('\n');
4028 }
4029}
4030#endif
4031
4032#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004033/*
4034 * Return the last used search pattern at "idx".
4035 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004036 spat_T *
4037get_spat(int idx)
4038{
4039 return &spats[idx];
4040}
4041
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004042/*
4043 * Return the last used search pattern index.
4044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004046get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004048 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004051
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004052#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004053/*
4054 * "searchcount()" function
4055 */
4056 void
4057f_searchcount(typval_T *argvars, typval_T *rettv)
4058{
4059 pos_T pos = curwin->w_cursor;
4060 char_u *pattern = NULL;
4061 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4062 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004063 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004064 searchstat_T stat;
4065
4066 if (rettv_dict_alloc(rettv) == FAIL)
4067 return;
4068
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004069 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4070 return;
4071
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004072 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4073 recompute = TRUE;
4074
4075 if (argvars[0].v_type != VAR_UNKNOWN)
4076 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004077 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004078 dictitem_T *di;
4079 listitem_T *li;
4080 int error = FALSE;
4081
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004082 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004083 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004084 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004085 di = dict_find(dict, (char_u *)"timeout", -1);
4086 if (di != NULL)
4087 {
4088 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4089 if (error)
4090 return;
4091 }
4092 di = dict_find(dict, (char_u *)"maxcount", -1);
4093 if (di != NULL)
4094 {
4095 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4096 if (error)
4097 return;
4098 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004099 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004100 di = dict_find(dict, (char_u *)"pattern", -1);
4101 if (di != NULL)
4102 {
4103 pattern = tv_get_string_chk(&di->di_tv);
4104 if (pattern == NULL)
4105 return;
4106 }
4107 di = dict_find(dict, (char_u *)"pos", -1);
4108 if (di != NULL)
4109 {
4110 if (di->di_tv.v_type != VAR_LIST)
4111 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004112 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004113 return;
4114 }
4115 if (list_len(di->di_tv.vval.v_list) != 3)
4116 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004117 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004118 return;
4119 }
4120 li = list_find(di->di_tv.vval.v_list, 0L);
4121 if (li != NULL)
4122 {
4123 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4124 if (error)
4125 return;
4126 }
4127 li = list_find(di->di_tv.vval.v_list, 1L);
4128 if (li != NULL)
4129 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004130 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004131 if (error)
4132 return;
4133 }
4134 li = list_find(di->di_tv.vval.v_list, 2L);
4135 if (li != NULL)
4136 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004137 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004138 if (error)
4139 return;
4140 }
4141 }
4142 }
4143
4144 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004145#ifdef FEAT_SEARCH_EXTRA
4146 save_incsearch_state();
4147#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004148 if (pattern != NULL)
4149 {
4150 if (*pattern == NUL)
4151 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004152 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004153 spats[last_idx].pat = vim_strsave(pattern);
4154 }
4155 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4156 goto the_end; // the previous pattern was never defined
4157
4158 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4159
4160 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4161 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4162 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4163 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4164 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4165
4166the_end:
4167 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004168#ifdef FEAT_SEARCH_EXTRA
4169 restore_incsearch_state();
4170#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004171}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004172#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004173
4174/*
4175 * Fuzzy string matching
4176 *
4177 * Ported from the lib_fts library authored by Forrest Smith.
4178 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4179 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004180 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004181 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4182 *
4183 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004184 * - Matched letter
4185 * - Unmatched letter
4186 * - Consecutively matched letters
4187 * - Proximity to start
4188 * - Letter following a separator (space, underscore)
4189 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004190 *
4191 * Matched letters are good. Unmatched letters are bad. Matching near the start
4192 * is good. Matching the first letter in the middle of a phrase is good.
4193 * Matching the uppercase letters in camel case entries is good.
4194 *
4195 * The score assigned for each factor is explained below.
4196 * File paths are different from file names. File extensions may be ignorable.
4197 * Single words care about consecutive matches but not separators or camel
4198 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004199 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004200 * Matched letter: +0 points
4201 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004202 * Consecutive match bonus: +15 points
4203 * First letter bonus: +15 points
4204 * Separator bonus: +30 points
4205 * Camel case bonus: +30 points
4206 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004207 *
4208 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004209 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004210 * lower minimum score due to unmatched letter penalty. Longer search patterns
4211 * have a higher maximum score due to match bonuses.
4212 *
4213 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4214 * quite a bit.
4215 *
4216 * There is a penalty if you DON’T match the first three letters. Which
4217 * effectively rewards matching near the start. However there’s no difference
4218 * in matching between the middle and end.
4219 *
4220 * There is not an explicit bonus for an exact match. Unmatched letters receive
4221 * a penalty. So shorter strings and closer matches are worth more.
4222 */
4223typedef struct
4224{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004225 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004226 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004227 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004228 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004229} fuzzyItem_T;
4230
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004231// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4232// matching a whole word is preferred.
4233#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004234// bonus if match occurs after a path separator
4235#define PATH_SEPARATOR_BONUS 30
4236// bonus if match occurs after a word separator
4237#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004238// bonus if match is uppercase and prev is lower
4239#define CAMEL_BONUS 30
4240// bonus if the first letter is matched
4241#define FIRST_LETTER_BONUS 15
4242// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004243#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004244// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004245#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004246// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004247#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004248// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004249#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004250// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004251#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004252
4253#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004254
4255/*
4256 * Compute a score for a fuzzy matched string. The matching character locations
4257 * are in 'matches'.
4258 */
4259 static int
4260fuzzy_match_compute_score(
4261 char_u *str,
4262 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004263 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004264 int numMatches)
4265{
4266 int score;
4267 int penalty;
4268 int unmatched;
4269 int i;
4270 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004271 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004272
4273 // Initialize score
4274 score = 100;
4275
4276 // Apply leading letter penalty
4277 penalty = LEADING_LETTER_PENALTY * matches[0];
4278 if (penalty < MAX_LEADING_LETTER_PENALTY)
4279 penalty = MAX_LEADING_LETTER_PENALTY;
4280 score += penalty;
4281
4282 // Apply unmatched penalty
4283 unmatched = strSz - numMatches;
4284 score += UNMATCHED_LETTER_PENALTY * unmatched;
4285
4286 // Apply ordering bonuses
4287 for (i = 0; i < numMatches; ++i)
4288 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004289 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004290
4291 if (i > 0)
4292 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004293 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004294
4295 // Sequential
4296 if (currIdx == (prevIdx + 1))
4297 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004298 else
4299 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004300 }
4301
4302 // Check for bonuses based on neighbor character value
4303 if (currIdx > 0)
4304 {
4305 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004306 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004307 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004308
4309 if (has_mbyte)
4310 {
4311 while (sidx < currIdx)
4312 {
4313 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004314 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004315 sidx++;
4316 }
4317 curr = (*mb_ptr2char)(p);
4318 }
4319 else
4320 {
4321 neighbor = str[currIdx - 1];
4322 curr = str[currIdx];
4323 }
4324
4325 if (vim_islower(neighbor) && vim_isupper(curr))
4326 score += CAMEL_BONUS;
4327
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004328 // Bonus if the match follows a separator character
4329 if (neighbor == '/' || neighbor == '\\')
4330 score += PATH_SEPARATOR_BONUS;
4331 else if (neighbor == ' ' || neighbor == '_')
4332 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004333 }
4334 else
4335 {
4336 // First letter
4337 score += FIRST_LETTER_BONUS;
4338 }
4339 }
4340 return score;
4341}
4342
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004343/*
4344 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4345 * Return the number of matching characters.
4346 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004347 static int
4348fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004349 char_u *fuzpat,
4350 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004351 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004352 int *outScore,
4353 char_u *strBegin,
4354 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004355 int_u *srcMatches,
4356 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004357 int maxMatches,
4358 int nextMatch,
4359 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004360{
4361 // Recursion params
4362 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004363 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004364 int bestRecursiveScore = 0;
4365 int first_match;
4366 int matched;
4367
4368 // Count recursions
4369 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004370 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004371 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004372
4373 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004374 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004375 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004376
4377 // Loop through fuzpat and str looking for a match
4378 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004379 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004380 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004381 int c1;
4382 int c2;
4383
4384 c1 = PTR2CHAR(fuzpat);
4385 c2 = PTR2CHAR(str);
4386
Bram Moolenaar635414d2020-09-11 22:25:15 +02004387 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004388 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004389 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004390 // Supplied matches buffer was too short
4391 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004392 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004393
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004394 int recursiveScore = 0;
4395 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4396 CLEAR_FIELD(recursiveMatches);
4397
Bram Moolenaar635414d2020-09-11 22:25:15 +02004398 // "Copy-on-Write" srcMatches into matches
4399 if (first_match && srcMatches)
4400 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004401 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004402 first_match = FALSE;
4403 }
4404
4405 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004406 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004407 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4408 &recursiveScore, strBegin, strLen, matches,
4409 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004410 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004411 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004412 {
4413 // Pick best recursive score
4414 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4415 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004416 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004417 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004418 bestRecursiveScore = recursiveScore;
4419 }
4420 recursiveMatch = TRUE;
4421 }
4422
4423 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004424 matches[nextMatch++] = strIdx;
4425 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004426 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004427 else
4428 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004429 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004430 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004431 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004432 else
4433 ++str;
4434 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004435 }
4436
4437 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004438 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004439
4440 // Calculate score
4441 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004442 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4443 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004444
4445 // Return best result
4446 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4447 {
4448 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004449 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004450 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004451 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004452 }
4453 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004454 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004455
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004456 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004457}
4458
4459/*
4460 * fuzzy_match()
4461 *
4462 * Performs exhaustive search via recursion to find all possible matches and
4463 * match with highest score.
4464 * Scores values have no intrinsic meaning. Possible score range is not
4465 * normalized and varies with pattern.
4466 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004467 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004468 * Uses char_u for match indices. Therefore patterns are limited to
4469 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004470 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004471 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4472 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004473 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004474 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004475fuzzy_match(
4476 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004477 char_u *pat_arg,
4478 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004479 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004480 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004481 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004482{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004483 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004484 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004485 char_u *save_pat;
4486 char_u *pat;
4487 char_u *p;
4488 int complete = FALSE;
4489 int score = 0;
4490 int numMatches = 0;
4491 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004492
4493 *outScore = 0;
4494
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004495 save_pat = vim_strsave(pat_arg);
4496 if (save_pat == NULL)
4497 return FALSE;
4498 pat = save_pat;
4499 p = pat;
4500
4501 // Try matching each word in 'pat_arg' in 'str'
4502 while (TRUE)
4503 {
4504 if (matchseq)
4505 complete = TRUE;
4506 else
4507 {
4508 // Extract one word from the pattern (separated by space)
4509 p = skipwhite(p);
4510 if (*p == NUL)
4511 break;
4512 pat = p;
4513 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4514 {
4515 if (has_mbyte)
4516 MB_PTR_ADV(p);
4517 else
4518 ++p;
4519 }
4520 if (*p == NUL) // processed all the words
4521 complete = TRUE;
4522 *p = NUL;
4523 }
4524
4525 score = 0;
4526 recursionCount = 0;
4527 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4528 matches + numMatches, maxMatches - numMatches,
4529 0, &recursionCount);
4530 if (matchCount == 0)
4531 {
4532 numMatches = 0;
4533 break;
4534 }
4535
4536 // Accumulate the match score and the number of matches
4537 *outScore += score;
4538 numMatches += matchCount;
4539
4540 if (complete)
4541 break;
4542
4543 // try matching the next word
4544 ++p;
4545 }
4546
4547 vim_free(save_pat);
4548 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004549}
4550
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004551#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004552/*
4553 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004554 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004555 */
4556 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004557fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004558{
4559 int v1 = ((fuzzyItem_T *)s1)->score;
4560 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004561 int idx1 = ((fuzzyItem_T *)s1)->idx;
4562 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004563
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004564 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004565}
4566
4567/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004568 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4569 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004570 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4571 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004572 * If 'items' is a list of strings, then search for 'str' in the list.
4573 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4574 * for each item or use 'item_cb' Funcref function to get the string.
4575 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4576 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004577 */
4578 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004579fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004580 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004581 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004582 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004583 char_u *key,
4584 callback_T *item_cb,
4585 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004586 list_T *fmatchlist,
4587 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004588{
4589 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004590 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004591 listitem_T *li;
4592 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004593 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004594 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004595
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004596 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004597 if (len == 0)
4598 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004599 if (max_matches > 0 && len > max_matches)
4600 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004601
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004602 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4603 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004604 return;
4605
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004606 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004607 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004608 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004609 int score;
4610 char_u *itemstr;
4611 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004612
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004613 if (max_matches > 0 && match_count >= max_matches)
4614 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004615
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004616 itemstr = NULL;
4617 rettv.v_type = VAR_UNKNOWN;
4618 if (li->li_tv.v_type == VAR_STRING) // list of strings
4619 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004620 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004621 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004622 {
4623 // For a dict, either use the specified key to lookup the string or
4624 // use the specified callback function to get the string.
4625 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004626 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4627 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004628 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004629 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004630 typval_T argv[2];
4631
4632 // Invoke the supplied callback (if any) to get the dict item
4633 li->li_tv.vval.v_dict->dv_refcount++;
4634 argv[0].v_type = VAR_DICT;
4635 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4636 argv[1].v_type = VAR_UNKNOWN;
4637 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4638 {
4639 if (rettv.v_type == VAR_STRING)
4640 itemstr = rettv.vval.v_string;
4641 }
4642 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004643 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004644 }
4645
4646 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004647 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004648 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004649 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004650 items[match_count].idx = match_count;
4651 items[match_count].item = li;
4652 items[match_count].score = score;
4653
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004654 // Copy the list of matching positions in itemstr to a list, if
4655 // 'retmatchpos' is set.
4656 if (retmatchpos)
4657 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004658 int j = 0;
4659 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004660
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004661 items[match_count].lmatchpos = list_alloc();
4662 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004663 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004664
4665 p = str;
4666 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004667 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004668 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004669 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004670 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004671 matches[j]) == FAIL)
4672 goto done;
4673 j++;
4674 }
4675 if (has_mbyte)
4676 MB_PTR_ADV(p);
4677 else
4678 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004679 }
4680 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004681 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004682 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004683 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004684 }
4685
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004686 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004687 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004688 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004689
Bram Moolenaar635414d2020-09-11 22:25:15 +02004690 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004691 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004692 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004693
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004694 // For matchfuzzy(), return a list of matched strings.
4695 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004696 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004697 // The first item is a list of matched strings. The second item
4698 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004699 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004700 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4701 if (retmatchpos)
4702 {
4703 li = list_find(fmatchlist, 0);
4704 if (li == NULL || li->li_tv.vval.v_list == NULL)
4705 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004706 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004707 }
4708 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004709 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004710
4711 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004712 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004713 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004714 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004715 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004716 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004717 }
4718
4719 // next copy the list of matching positions
4720 if (retmatchpos)
4721 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004722 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004723 if (li == NULL || li->li_tv.vval.v_list == NULL)
4724 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004725 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004726
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004727 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004728 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004729 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004730 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004731 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004732 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004733 goto done;
4734 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004735
4736 // copy the matching scores
4737 li = list_find(fmatchlist, -1);
4738 if (li == NULL || li->li_tv.vval.v_list == NULL)
4739 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004740 retlist = li->li_tv.vval.v_list;
4741 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004742 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004743 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004744 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004745 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004746 goto done;
4747 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004748 }
4749 }
4750
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004751done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004752 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004753}
4754
4755/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004756 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4757 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4758 */
4759 static void
4760do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4761{
4762 callback_T cb;
4763 char_u *key = NULL;
4764 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004765 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004766 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004767
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004768 if (in_vim9script()
4769 && (check_for_list_arg(argvars, 0) == FAIL
4770 || check_for_string_arg(argvars, 1) == FAIL
4771 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4772 return;
4773
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004774 CLEAR_POINTER(&cb);
4775
4776 // validate and get the arguments
4777 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4778 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004779 semsg(_(e_argument_of_str_must_be_list),
4780 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004781 return;
4782 }
4783 if (argvars[1].v_type != VAR_STRING
4784 || argvars[1].vval.v_string == NULL)
4785 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004786 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004787 return;
4788 }
4789
4790 if (argvars[2].v_type != VAR_UNKNOWN)
4791 {
4792 dict_T *d;
4793 dictitem_T *di;
4794
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004795 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004796 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004797
4798 // To search a dict, either a callback function or a key can be
4799 // specified.
4800 d = argvars[2].vval.v_dict;
4801 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4802 {
4803 if (di->di_tv.v_type != VAR_STRING
4804 || di->di_tv.vval.v_string == NULL
4805 || *di->di_tv.vval.v_string == NUL)
4806 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004807 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004808 return;
4809 }
4810 key = tv_get_string(&di->di_tv);
4811 }
4812 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4813 {
4814 cb = get_callback(&di->di_tv);
4815 if (cb.cb_name == NULL)
4816 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004817 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004818 return;
4819 }
4820 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004821
4822 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004823 {
4824 if (di->di_tv.v_type != VAR_NUMBER)
4825 {
4826 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4827 return;
4828 }
4829 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4830 }
4831
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004832 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004833 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004834 }
4835
4836 // get the fuzzy matches
4837 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004838 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004839 goto done;
4840 if (retmatchpos)
4841 {
4842 list_T *l;
4843
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004844 // For matchfuzzypos(), a list with three items are returned. First
4845 // item is a list of matching strings, the second item is a list of
4846 // lists with matching positions within each string and the third item
4847 // is the list of scores of the matches.
4848 l = list_alloc();
4849 if (l == NULL)
4850 goto done;
4851 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004852 {
4853 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004854 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004855 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004856 l = list_alloc();
4857 if (l == NULL)
4858 goto done;
4859 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004860 {
4861 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004862 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004863 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004864 l = list_alloc();
4865 if (l == NULL)
4866 goto done;
4867 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004868 {
4869 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004870 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004871 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004872 }
4873
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004874 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004875 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004876
4877done:
4878 free_callback(&cb);
4879}
4880
4881/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004882 * "matchfuzzy()" function
4883 */
4884 void
4885f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4886{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004887 do_fuzzymatch(argvars, rettv, FALSE);
4888}
4889
4890/*
4891 * "matchfuzzypos()" function
4892 */
4893 void
4894f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
4895{
4896 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004897}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004898#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004899
4900/*
4901 * Same as fuzzy_match_item_compare() except for use with a string match
4902 */
4903 static int
4904fuzzy_match_str_compare(const void *s1, const void *s2)
4905{
4906 int v1 = ((fuzmatch_str_T *)s1)->score;
4907 int v2 = ((fuzmatch_str_T *)s2)->score;
4908 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4909 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4910
4911 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4912}
4913
4914/*
4915 * Sort fuzzy matches by score
4916 */
4917 static void
4918fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
4919{
4920 // Sort the list by the descending order of the match score
4921 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4922 fuzzy_match_str_compare);
4923}
4924
4925/*
4926 * Same as fuzzy_match_item_compare() except for use with a function name
4927 * string match. <SNR> functions should be sorted to the end.
4928 */
4929 static int
4930fuzzy_match_func_compare(const void *s1, const void *s2)
4931{
4932 int v1 = ((fuzmatch_str_T *)s1)->score;
4933 int v2 = ((fuzmatch_str_T *)s2)->score;
4934 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4935 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4936 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
4937 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
4938
4939 if (*str1 != '<' && *str2 == '<') return -1;
4940 if (*str1 == '<' && *str2 != '<') return 1;
4941 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4942}
4943
4944/*
4945 * Sort fuzzy matches of function names by score.
4946 * <SNR> functions should be sorted to the end.
4947 */
4948 static void
4949fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
4950{
4951 // Sort the list by the descending order of the match score
4952 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4953 fuzzy_match_func_compare);
4954}
4955
4956/*
4957 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
4958 * returns the match score.
4959 */
4960 int
4961fuzzy_match_str(char_u *str, char_u *pat)
4962{
4963 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00004964 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004965
4966 if (str == NULL || pat == NULL)
4967 return 0;
4968
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00004969 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004970 sizeof(matchpos) / sizeof(matchpos[0]));
4971
4972 return score;
4973}
4974
4975/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01004976 * Free an array of fuzzy string matches "fuzmatch[count]".
4977 */
4978 void
4979fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
4980{
4981 int i;
4982
4983 if (fuzmatch == NULL)
4984 return;
4985 for (i = 0; i < count; ++i)
4986 vim_free(fuzmatch[i].str);
4987 vim_free(fuzmatch);
4988}
4989
4990/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004991 * Copy a list of fuzzy matches into a string list after sorting the matches by
4992 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
4993 * Returns OK on success and FAIL on memory allocation failure.
4994 */
4995 int
4996fuzzymatches_to_strmatches(
4997 fuzmatch_str_T *fuzmatch,
4998 char_u ***matches,
4999 int count,
5000 int funcsort)
5001{
5002 int i;
5003
5004 if (count <= 0)
5005 return OK;
5006
5007 *matches = ALLOC_MULT(char_u *, count);
5008 if (*matches == NULL)
5009 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005010 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005011 return FAIL;
5012 }
5013
5014 // Sort the list by the descending order of the match score
5015 if (funcsort)
5016 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5017 else
5018 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5019
5020 for (i = 0; i < count; i++)
5021 (*matches)[i] = fuzmatch[i].str;
5022 vim_free(fuzmatch);
5023
5024 return OK;
5025}