blob: 43c40e03d5444dc268b66cf376e3554898292478 [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
John Marriott8c85a2a2024-05-20 19:18:26 +020020static char_u *get_line_and_copy(linenr_T lnum, char_u *buf);
21static void show_pat_in_path(char_u *, int, 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
John Marriott8c85a2a2024-05-20 19:18:26 +020035#ifdef FEAT_SEARCH_EXTRA
36static void save_incsearch_state(void);
37static void restore_incsearch_state(void);
38#endif
39static int check_prevcol(char_u *linep, int col, int ch, int *prevcol);
40static int find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos);
41static void find_mps_values(int *initc, int *findc, int *backwards, int switchit);
42static int is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction);
43static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, size_t msgbuflen, int recompute, int maxcount, long timeout);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020044static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout);
John Marriott8c85a2a2024-05-20 19:18:26 +020045static int fuzzy_match_compute_score(char_u *str, int strSz, int_u *matches, int numMatches);
46static int fuzzy_match_recursive(char_u *fuzpat, char_u *str, int_u strIdx, int *outScore, char_u *strBegin, int strLen, int_u *srcMatches, int_u *matches, int maxMatches, int nextMatch, int *recursionCount);
47#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
48static int fuzzy_match_item_compare(const void *s1, const void *s2);
49static void fuzzy_match_in_list(list_T *l, char_u *str, int matchseq, char_u *key, callback_T *item_cb, int retmatchpos, list_T *fmatchlist, long max_matches);
50static void do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos);
51#endif
52static int fuzzy_match_str_compare(const void *s1, const void *s2);
53static void fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz);
54static int fuzzy_match_func_compare(const void *s1, const void *s2);
55static void fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020056
Bram Moolenaarea6561a2020-06-01 21:32:45 +020057#define SEARCH_STAT_DEF_TIMEOUT 40L
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020058#define SEARCH_STAT_DEF_MAX_COUNT 99
59#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaar071d4272004-06-13 20:20:40 +000061/*
62 * This file contains various searching-related routines. These fall into
63 * three groups:
64 * 1. string searches (for /, ?, n, and N)
65 * 2. character searches within a single line (for f, F, t, T, etc)
66 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
67 */
68
69/*
70 * String searches
71 *
72 * The string search functions are divided into two levels:
73 * lowest: searchit(); uses an pos_T for starting position and found match.
74 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
75 *
76 * The last search pattern is remembered for repeating the same search.
77 * This pattern is shared between the :g, :s, ? and / commands.
78 * This is in search_regcomp().
79 *
80 * The actual string matching is done using a heavily modified version of
81 * Henry Spencer's regular expression library. See regexp.c.
82 */
83
Bram Moolenaar071d4272004-06-13 20:20:40 +000084/*
85 * Two search patterns are remembered: One for the :substitute command and
86 * one for other searches. last_idx points to the one that was used the last
87 * time.
88 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020089static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000090{
John Marriott8c85a2a2024-05-20 19:18:26 +020091 {NULL, 0, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat
92 {NULL, 0, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat
Bram Moolenaar071d4272004-06-13 20:20:40 +000093};
94
Bram Moolenaar63d9e732019-12-05 21:10:38 +010095static int last_idx = 0; // index in spats[] for RE_LAST
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar63d9e732019-12-05 21:10:38 +010097static char_u lastc[2] = {NUL, NUL}; // last character searched for
98static int lastcdir = FORWARD; // last direction of character search
99static int last_t_cmd = TRUE; // last search t_cmd
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200100static char_u lastc_bytes[MB_MAXBYTES + 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100101static int lastc_bytelen = 1; // >1 for multi-byte char
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200102
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100103// copy of spats[], for keeping the search patterns while executing autocmds
John Marriott8c85a2a2024-05-20 19:18:26 +0200104static spat_T saved_spats[ARRAY_LENGTH(spats)];
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100105static char_u *saved_mr_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200106static size_t saved_mr_patternlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100108static int saved_spats_last_idx = 0;
109static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100112// allocated copy of pattern used by search_regcomp()
113static char_u *mr_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200114static size_t mr_patternlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115
116#ifdef FEAT_FIND_ID
117/*
118 * Type used by find_pattern_in_path() to remember which included files have
119 * been searched already.
120 */
121typedef struct SearchedFile
122{
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100123 FILE *fp; // File pointer
124 char_u *name; // Full name of file
125 linenr_T lnum; // Line we were up to in file
126 int matched; // Found a match in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127} SearchedFile;
128#endif
129
130/*
131 * translate search pattern for vim_regcomp()
132 *
133 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
134 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
135 * pat_save == RE_BOTH: save pat in both patterns (:global command)
136 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000137 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
139 * options & SEARCH_HIS: put search string in history
140 * options & SEARCH_KEEP: keep previous search pattern
141 *
142 * returns FAIL if failed, OK otherwise.
143 */
144 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100145search_regcomp(
146 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +0200147 size_t patlen,
Rob Pillinge86190e2022-12-23 19:06:04 +0000148 char_u **used_pat,
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100149 int pat_save,
150 int pat_use,
151 int options,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100152 regmmatch_T *regmatch) // return: pattern and ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153{
154 int magic;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155
156 rc_did_emsg = FALSE;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100157 magic = magic_isset();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
159 /*
160 * If no pattern given, use a previously defined pattern.
161 */
162 if (pat == NULL || *pat == NUL)
163 {
John Marriott8c85a2a2024-05-20 19:18:26 +0200164 int i;
165
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 if (pat_use == RE_LAST)
167 i = last_idx;
168 else
169 i = pat_use;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100170 if (spats[i].pat == NULL) // pattern was never defined
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 {
172 if (pat_use == RE_SUBST)
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200173 emsg(_(e_no_previous_substitute_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200175 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 rc_did_emsg = TRUE;
177 return FAIL;
178 }
179 pat = spats[i].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +0200180 patlen = spats[i].patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 magic = spats[i].magic;
182 no_smartcase = spats[i].no_scs;
183 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100184 else if (options & SEARCH_HIS) // put new pattern in history
John Marriott8c85a2a2024-05-20 19:18:26 +0200185 add_to_history(HIST_SEARCH, pat, patlen, TRUE, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186
Rob Pillinge86190e2022-12-23 19:06:04 +0000187 if (used_pat)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000188 *used_pat = pat;
Rob Pillinge86190e2022-12-23 19:06:04 +0000189
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100190 vim_free(mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100193 mr_pattern = reverse_text(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 else
195#endif
John Marriott8c85a2a2024-05-20 19:18:26 +0200196 mr_pattern = vim_strnsave(pat, patlen);
197 if (mr_pattern == NULL)
198 mr_patternlen = 0;
199 else
200 mr_patternlen = patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202 /*
203 * Save the currently used pattern in the appropriate place,
204 * unless the pattern should not be remembered.
205 */
Bram Moolenaare1004402020-10-24 20:49:43 +0200206 if (!(options & SEARCH_KEEP)
207 && (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100209 // search or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
John Marriott8c85a2a2024-05-20 19:18:26 +0200211 save_re_pat(RE_SEARCH, pat, patlen, magic);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100212 // substitute or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
John Marriott8c85a2a2024-05-20 19:18:26 +0200214 save_re_pat(RE_SUBST, pat, patlen, magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 }
216
217 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000218 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
220 if (regmatch->regprog == NULL)
221 return FAIL;
222 return OK;
223}
224
225/*
226 * Get search pattern used by search_regcomp().
227 */
228 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100229get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230{
231 return mr_pattern;
232}
233
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100234 void
John Marriott8c85a2a2024-05-20 19:18:26 +0200235save_re_pat(int idx, char_u *pat, size_t patlen, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000237 if (spats[idx].pat == pat)
238 return;
239
240 vim_free(spats[idx].pat);
John Marriott8c85a2a2024-05-20 19:18:26 +0200241 spats[idx].pat = vim_strnsave(pat, patlen);
242 if (spats[idx].pat == NULL)
243 spats[idx].patlen = 0;
244 else
245 spats[idx].patlen = patlen;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000246 spats[idx].magic = magic;
247 spats[idx].no_scs = no_smartcase;
248 last_idx = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000250 // If 'hlsearch' set and search pat changed: need redraw.
251 if (p_hls)
252 redraw_all_later(UPD_SOME_VALID);
253 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255}
256
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257/*
258 * Save the search patterns, so they can be restored later.
259 * Used before/after executing autocommands and user functions.
260 */
261static int save_level = 0;
262
263 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100264save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265{
John Marriott8c85a2a2024-05-20 19:18:26 +0200266 int i;
267
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000268 if (save_level++ != 0)
269 return;
270
John Marriott8c85a2a2024-05-20 19:18:26 +0200271 for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i)
272 {
273 saved_spats[i] = spats[i];
274 if (spats[i].pat != NULL)
275 {
276 saved_spats[i].pat = vim_strnsave(spats[i].pat, spats[i].patlen);
277 if (saved_spats[i].pat == NULL)
278 saved_spats[i].patlen = 0;
279 else
280 saved_spats[i].patlen = spats[i].patlen;
281 }
282 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000283 if (mr_pattern == NULL)
284 saved_mr_pattern = NULL;
285 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200286 saved_mr_pattern = vim_strnsave(mr_pattern, mr_patternlen);
287 if (saved_mr_pattern == NULL)
288 saved_mr_patternlen = 0;
289 else
290 saved_mr_patternlen = mr_patternlen;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100291#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000292 saved_spats_last_idx = last_idx;
293 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295}
296
297 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100298restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299{
John Marriott8c85a2a2024-05-20 19:18:26 +0200300 int i;
301
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000302 if (--save_level != 0)
303 return;
304
John Marriott8c85a2a2024-05-20 19:18:26 +0200305 for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i)
306 {
307 vim_free(spats[i].pat);
308 spats[i] = saved_spats[i];
309 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100310#if defined(FEAT_EVAL)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000311 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100312#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000313 vim_free(mr_pattern);
314 mr_pattern = saved_mr_pattern;
John Marriott8c85a2a2024-05-20 19:18:26 +0200315 mr_patternlen = saved_mr_patternlen;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100316#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000317 last_idx = saved_spats_last_idx;
318 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000322#if defined(EXITFREE) || defined(PROTO)
323 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100324free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000325{
John Marriott8c85a2a2024-05-20 19:18:26 +0200326 int i;
327
328 for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i)
329 {
330 VIM_CLEAR(spats[i].pat);
331 spats[i].patlen = 0;
332 }
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100333 VIM_CLEAR(mr_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +0200334 mr_patternlen = 0;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000335}
336#endif
337
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100338#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100339// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
340// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200341static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100342static int did_save_last_search_spat = 0;
343static int saved_last_idx = 0;
344static int saved_no_hlsearch = 0;
Christian Brabandt6dd74242022-02-14 12:44:32 +0000345static int saved_search_match_endcol;
346static int saved_search_match_lines;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100347
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100348/*
349 * Save and restore the search pattern for incremental highlight search
350 * feature.
351 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100352 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100353 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100354 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100355 */
356 void
357save_last_search_pattern(void)
358{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200359 if (++did_save_last_search_spat != 1)
360 // nested call, nothing to do
361 return;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100362
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100363 saved_last_search_spat = spats[RE_SEARCH];
364 if (spats[RE_SEARCH].pat != NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +0200365 {
366 saved_last_search_spat.pat = vim_strnsave(spats[RE_SEARCH].pat, spats[RE_SEARCH].patlen);
367 if (saved_last_search_spat.pat == NULL)
368 saved_last_search_spat.patlen = 0;
369 else
370 saved_last_search_spat.patlen = spats[RE_SEARCH].patlen;
371 }
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100372 saved_last_idx = last_idx;
373 saved_no_hlsearch = no_hlsearch;
374}
375
376 void
377restore_last_search_pattern(void)
378{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200379 if (--did_save_last_search_spat > 0)
380 // nested call, nothing to do
381 return;
382 if (did_save_last_search_spat != 0)
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100383 {
Bram Moolenaar442a8532020-06-04 20:56:09 +0200384 iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100385 return;
386 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100387
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100388 vim_free(spats[RE_SEARCH].pat);
389 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100390 saved_last_search_spat.pat = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200391 saved_last_search_spat.patlen = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100392# if defined(FEAT_EVAL)
393 set_vv_searchforward();
394# endif
395 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200396 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100397}
Bram Moolenaard0480092017-11-16 22:20:39 +0100398
Christian Brabandt6dd74242022-02-14 12:44:32 +0000399/*
400 * Save and restore the incsearch highlighting variables.
401 * This is required so that calling searchcount() at does not invalidate the
402 * incsearch highlighting.
403 */
404 static void
405save_incsearch_state(void)
406{
407 saved_search_match_endcol = search_match_endcol;
408 saved_search_match_lines = search_match_lines;
409}
410
411 static void
412restore_incsearch_state(void)
413{
414 search_match_endcol = saved_search_match_endcol;
415 search_match_lines = saved_search_match_lines;
416}
417
Bram Moolenaard0480092017-11-16 22:20:39 +0100418 char_u *
419last_search_pattern(void)
420{
421 return spats[RE_SEARCH].pat;
422}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100423#endif
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425/*
426 * Return TRUE when case should be ignored for search pattern "pat".
427 * Uses the 'ignorecase' and 'smartcase' options.
428 */
429 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100430ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200432 return ignorecase_opt(pat, p_ic, p_scs);
433}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200435/*
436 * As ignorecase() put pass the "ic" and "scs" flags.
437 */
438 int
439ignorecase_opt(char_u *pat, int ic_in, int scs)
440{
441 int ic = ic_in;
442
443 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200444 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200445 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 no_smartcase = FALSE;
447
448 return ic;
449}
450
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200451/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200452 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200453 */
454 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100455pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200456{
457 char_u *p = pat;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200458 magic_T magic_val = MAGIC_ON;
459
460 // get the magicness of the pattern
461 (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val);
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200462
463 while (*p != NUL)
464 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200465 int l;
466
467 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
468 {
469 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
470 return TRUE;
471 p += l;
472 }
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200473 else if (*p == '\\' && magic_val <= MAGIC_ON)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200474 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100475 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200476 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100477 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200478 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100479 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200480 p += 2;
481 else
482 p += 1;
483 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200484 else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL)
485 {
486 if (p[1] != NUL) // skip "_X" and %X
487 p += 2;
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200488 else
489 p++;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200490 }
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200491 else if (MB_ISUPPER(*p))
492 return TRUE;
493 else
494 ++p;
495 }
496 return FALSE;
497}
498
Bram Moolenaar113e1072019-01-20 15:30:40 +0100499#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100501last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200502{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200503 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200504}
505
506 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100507last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200508{
509 return lastcdir == FORWARD;
510}
511
512 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100513last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200514{
515 return last_t_cmd == TRUE;
516}
517
518 void
zeertzjqe5d91ba2023-05-14 17:39:18 +0100519set_last_csearch(int c, char_u *s, int len)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200520{
521 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200522 lastc_bytelen = len;
523 if (len)
524 memcpy(lastc_bytes, s, len);
525 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200526 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200527}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100528#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200529
530 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100531set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200532{
533 lastcdir = cdir;
534}
535
536 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100537set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200538{
539 last_t_cmd = t_cmd;
540}
541
542 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100543last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 return spats[last_idx].pat;
546}
547
548/*
549 * Reset search direction to forward. For "gd" and "gD" commands.
550 */
551 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100552reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553{
554 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000555#if defined(FEAT_EVAL)
556 set_vv_searchforward();
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558}
559
560#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
561/*
562 * Set the last search pattern. For ":let @/ =" and viminfo.
563 * Also set the saved search pattern, so that this works in an autocommand.
564 */
565 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100566set_last_search_pat(
567 char_u *s,
568 int idx,
569 int magic,
570 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
572 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100573 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 if (*s == NUL)
575 spats[idx].pat = NULL;
576 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200577 {
578 spats[idx].patlen = STRLEN(s);
579 spats[idx].pat = vim_strnsave(s, spats[idx].patlen);
580 }
581 if (spats[idx].pat == NULL)
582 spats[idx].patlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 spats[idx].magic = magic;
584 spats[idx].no_scs = FALSE;
585 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000586#if defined(FEAT_EVAL)
587 set_vv_searchforward();
588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 spats[idx].off.line = FALSE;
590 spats[idx].off.end = FALSE;
591 spats[idx].off.off = 0;
592 if (setlast)
593 last_idx = idx;
594 if (save_level)
595 {
596 vim_free(saved_spats[idx].pat);
597 saved_spats[idx] = spats[0];
598 if (spats[idx].pat == NULL)
599 saved_spats[idx].pat = NULL;
600 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200601 saved_spats[idx].pat = vim_strnsave(spats[idx].pat, spats[idx].patlen);
602 if (saved_spats[idx].pat == NULL)
603 saved_spats[idx].patlen = 0;
604 else
605 saved_spats[idx].patlen = spats[idx].patlen;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100606# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100607 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 }
610# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100611 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100613 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614# endif
615}
616#endif
617
618#ifdef FEAT_SEARCH_EXTRA
619/*
620 * Get a regexp program for the last used search pattern.
621 * This is used for highlighting all matches in a window.
622 * Values returned in regmatch->regprog and regmatch->rmm_ic.
623 */
624 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100625last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 if (spats[last_idx].pat == NULL)
628 {
629 regmatch->regprog = NULL;
630 return;
631 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100632 ++emsg_off; // So it doesn't beep if bad expr
John Marriott8c85a2a2024-05-20 19:18:26 +0200633 (void)search_regcomp((char_u *)"", 0, NULL, 0, last_idx, SEARCH_KEEP, regmatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 --emsg_off;
635}
636#endif
637
638/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100639 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100640 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
641 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 *
643 * if (options & SEARCH_MSG) == 0 don't give any messages
644 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
645 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
646 * if (options & SEARCH_HIS) put search pattern in history
647 * if (options & SEARCH_END) return position at end of match
648 * if (options & SEARCH_START) accept match at pos itself
649 * if (options & SEARCH_KEEP) keep previous search pattern
650 * if (options & SEARCH_FOLD) match only once in a closed fold
651 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100652 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 *
654 * Return FAIL (zero) for failure, non-zero for success.
655 * When FEAT_EVAL is defined, returns the index of the first matching
656 * subpattern plus one; one if there was none.
657 */
658 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100659searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200660 win_T *win, // window to search in; can be NULL for a
661 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100662 buf_T *buf,
663 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100664 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100665 int dir,
666 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +0200667 size_t patlen,
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100668 long count,
669 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200670 int pat_use, // which pattern to use when "pat" is empty
671 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
673 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100674 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100675 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 regmmatch_T regmatch;
677 char_u *ptr;
678 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000680 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 int loop;
682 pos_T start_pos;
683 int at_first_line;
684 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200685 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 int match_ok;
687 long nmatched;
688 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100689 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100690 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691#ifdef FEAT_SEARCH_EXTRA
692 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200694 linenr_T stop_lnum = 0; // stop after this line number when != 0
Paul Ollis65745772022-06-05 16:55:54 +0100695 int unused_timeout_flag = FALSE;
696 int *timed_out = &unused_timeout_flag; // set when timed out.
John Marriott8c85a2a2024-05-20 19:18:26 +0200697 int search_from_match_end; // vi-compatible search?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
John Marriott8c85a2a2024-05-20 19:18:26 +0200699 if (search_regcomp(pat, patlen, NULL, RE_SEARCH, pat_use,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
701 {
702 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000703 semsg(_(e_invalid_search_string_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 return FAIL;
705 }
706
John Marriott8c85a2a2024-05-20 19:18:26 +0200707 search_from_match_end = vim_strchr(p_cpo, CPO_SEARCH) != NULL;
708
Paul Ollis65745772022-06-05 16:55:54 +0100709 if (extra_arg != NULL)
710 {
711 stop_lnum = extra_arg->sa_stop_lnum;
712#ifdef FEAT_RELTIME
713 if (extra_arg->sa_tm > 0)
Paul Ollis65745772022-06-05 16:55:54 +0100714 init_regexp_timeout(extra_arg->sa_tm);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100715 // Also set the pointer when sa_tm is zero, the caller may have set the
716 // timeout.
717 timed_out = &extra_arg->sa_timed_out;
Paul Ollis65745772022-06-05 16:55:54 +0100718#endif
719 }
720
Bram Moolenaar280f1262006-01-30 00:14:18 +0000721 /*
722 * find the string
723 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100724 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100726 // When not accepting a match at the start position set "extra_col" to
727 // a non-zero value. Don't do that when starting at MAXCOL, since
728 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200729 if (pos->col == MAXCOL)
730 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100731 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200732 else if (has_mbyte
733 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
734 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100735 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100736 ptr = ml_get_buf(buf, pos->lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100737 if (ml_get_buf_len(buf, pos->lnum) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200738 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100739 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100740 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100741 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100742 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200743 start_char_len = 1;
744 if (dir == FORWARD)
745 {
746 if (options & SEARCH_START)
747 extra_col = 0;
748 else
749 extra_col = start_char_len;
750 }
751 else
752 {
753 if (options & SEARCH_START)
754 extra_col = start_char_len;
755 else
756 extra_col = 0;
757 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100758
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100759 start_pos = *pos; // remember start pos for detecting no match
760 found = 0; // default: not found
761 at_first_line = TRUE; // default: start in first line
762 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {
764 pos->lnum = 1;
765 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100766 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768
769 /*
770 * Start searching in current line, unless searching backwards and
771 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000772 * If we are searching backwards, in column 0, and not including the
773 * current position, gain some efficiency by skipping back a line.
774 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000776 if (dir == BACKWARD && start_pos.col == 0
777 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
779 lnum = pos->lnum - 1;
780 at_first_line = FALSE;
781 }
782 else
783 lnum = pos->lnum;
784
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100785 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
787 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
788 lnum += dir, at_first_line = FALSE)
789 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100790 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000791 if (stop_lnum != 0 && (dir == FORWARD
792 ? lnum > stop_lnum : lnum < stop_lnum))
793 break;
Paul Ollis65745772022-06-05 16:55:54 +0100794 // Stop after passing the time limit.
795 if (*timed_out)
Bram Moolenaar76929292008-01-06 19:07:36 +0000796 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000799 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100801 col = at_first_line && (options & SEARCH_COL) ? pos->col
802 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 nmatched = vim_regexec_multi(&regmatch, win, buf,
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100804 lnum, col, timed_out);
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200805 // vim_regexec_multi() may clear "regprog"
806 if (regmatch.regprog == NULL)
807 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100808 // Abort searching on an error (e.g., out of stack).
Paul Ollis65745772022-06-05 16:55:54 +0100809 if (called_emsg > called_emsg_before || *timed_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 break;
811 if (nmatched > 0)
812 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100813 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000814 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000816#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000818#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100819 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000820 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
821 ptr = (char_u *)"";
822 else
823 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
825 /*
826 * Forward search in the first line: match should be after
827 * the start position. If not, continue at the end of the
828 * match (this is vi compatible) or on the next char.
829 */
830 if (dir == FORWARD && at_first_line)
831 {
832 match_ok = TRUE;
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000835 * When the match starts in a next line it's certainly
836 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 * When match lands on a NUL the cursor will be put
838 * one back afterwards, compare with that position,
839 * otherwise "/$" will get stuck on end of line.
840 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000841 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100842 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000843 ? (nmatched == 1
844 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000846 : ((int)matchpos.col
847 - (ptr[matchpos.col] == NUL)
848 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
850 /*
851 * If vi-compatible searching, continue at the end
852 * of the match, otherwise continue one position
853 * forward.
854 */
John Marriott8c85a2a2024-05-20 19:18:26 +0200855 if (search_from_match_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
857 if (nmatched > 1)
858 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100859 // end is in next line, thus no match in
860 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 match_ok = FALSE;
862 break;
863 }
864 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100865 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000866 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 && ptr[matchcol] != NUL)
868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 if (has_mbyte)
870 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000871 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 ++matchcol;
874 }
875 }
876 else
877 {
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000878 // Advance "matchcol" to the next character.
Bram Moolenaar837ca8f2022-11-26 18:59:19 +0000879 // This uses rmm_matchcol, the actual start of
880 // the match, ignoring "\zs".
881 matchcol = regmatch.rmm_matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (ptr[matchcol] != NUL)
883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000885 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 + matchcol);
887 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 ++matchcol;
889 }
890 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200891 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (ptr[matchcol] == NUL
894 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000895 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100896 matchcol, timed_out)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898 match_ok = FALSE;
899 break;
900 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200901 // vim_regexec_multi() may clear "regprog"
902 if (regmatch.regprog == NULL)
903 break;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000904 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 endpos = regmatch.endpos[0];
906# ifdef FEAT_EVAL
907 submatch = first_submatch(&regmatch);
908# endif
909
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100910 // Need to get the line pointer again, a
911 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000912 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914 if (!match_ok)
915 continue;
916 }
917 if (dir == BACKWARD)
918 {
919 /*
920 * Now, if there are multiple matches on this line,
921 * we have to get the last one. Or the last one before
922 * the cursor, if we're on that line.
923 * When putting the new cursor at the end, compare
924 * relative to the end of the match.
925 */
926 match_ok = FALSE;
927 for (;;)
928 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100929 // Remember a position that is before the start
930 // position, we use it if it's the last match in
931 // the line. Always accept a position after
932 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000933 if (loop
934 || ((options & SEARCH_END)
935 ? (lnum + regmatch.endpos[0].lnum
936 < start_pos.lnum
937 || (lnum + regmatch.endpos[0].lnum
938 == start_pos.lnum
939 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200940 < (int)start_pos.col
941 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000942 : (lnum + regmatch.startpos[0].lnum
943 < start_pos.lnum
944 || (lnum + regmatch.startpos[0].lnum
945 == start_pos.lnum
946 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200947 < (int)start_pos.col
948 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000951 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 endpos = regmatch.endpos[0];
953# ifdef FEAT_EVAL
954 submatch = first_submatch(&regmatch);
955# endif
956 }
957 else
958 break;
959
960 /*
961 * We found a valid match, now check if there is
962 * another one after it.
963 * If vi-compatible searching, continue at the end
964 * of the match, otherwise continue one position
965 * forward.
966 */
John Marriott8c85a2a2024-05-20 19:18:26 +0200967 if (search_from_match_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
969 if (nmatched > 1)
970 break;
971 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100972 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000973 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 && ptr[matchcol] != NUL)
975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 if (has_mbyte)
977 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000978 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 ++matchcol;
981 }
982 }
983 else
984 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100985 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000986 if (matchpos.lnum > 0)
987 break;
988 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (ptr[matchcol] != NUL)
990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 if (has_mbyte)
992 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000993 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 ++matchcol;
996 }
997 }
998 if (ptr[matchcol] == NUL
999 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +00001000 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +01001001 matchcol, timed_out)) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +01001002 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001003 // If the search timed out, we did find a match
1004 // but it might be the wrong one, so that's not
1005 // OK.
Paul Ollis65745772022-06-05 16:55:54 +01001006 if (*timed_out)
Bram Moolenaar9d322762018-02-09 16:04:25 +01001007 match_ok = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +01001009 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001010 // vim_regexec_multi() may clear "regprog"
1011 if (regmatch.regprog == NULL)
1012 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001014 // Need to get the line pointer again, a
1015 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001016 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018
1019 /*
1020 * If there is only a match after the cursor, skip
1021 * this match.
1022 */
1023 if (!match_ok)
1024 continue;
1025 }
1026
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001027 // With the SEARCH_END option move to the last character
1028 // of the match. Don't do it for an empty match, end
1029 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +02001030 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001031 && !(matchpos.lnum == endpos.lnum
1032 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001034 // For a match in the first column, set the position
1035 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001036 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001037 pos->col = endpos.col;
1038 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001039 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001040 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001041 {
1042 --pos->lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01001043 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001044 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001045 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001046 else
1047 {
1048 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001049 if (has_mbyte
1050 && pos->lnum <= buf->b_ml.ml_line_count)
1051 {
1052 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1053 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1054 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001055 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001056 if (end_pos != NULL)
1057 {
1058 end_pos->lnum = lnum + matchpos.lnum;
1059 end_pos->col = matchpos.col;
1060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062 else
1063 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001064 pos->lnum = lnum + matchpos.lnum;
1065 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001066 if (end_pos != NULL)
1067 {
1068 end_pos->lnum = lnum + endpos.lnum;
1069 end_pos->col = endpos.col;
1070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001073 if (end_pos != NULL)
1074 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001076 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001078 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001079 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 search_match_endcol = endpos.col;
1081 break;
1082 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001083 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (got_int)
1085 break;
1086
1087#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001088 // Cancel searching if a character was typed. Used for
1089 // 'incsearch'. Don't check too often, that would slowdown
1090 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if ((options & SEARCH_PEEK)
1092 && ((lnum - pos->lnum) & 0x3f) == 0
1093 && char_avail())
1094 {
1095 break_loop = TRUE;
1096 break;
1097 }
1098#endif
1099
1100 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001101 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103 at_first_line = FALSE;
1104
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001105 // vim_regexec_multi() may clear "regprog"
1106 if (regmatch.regprog == NULL)
1107 break;
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001110 * Stop the search if wrapscan isn't set, "stop_lnum" is
1111 * specified, after an interrupt, after a match and after looping
1112 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001114 if (!p_ws || stop_lnum != 0 || got_int
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001115 || called_emsg > called_emsg_before || *timed_out
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001116#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001117 || break_loop
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001118#endif
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001119 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
1122 /*
1123 * If 'wrapscan' is set we continue at the other end of the file.
Christian Brabandt34a6a362023-05-06 19:20:20 +01001124 * If 'shortmess' does not contain 's', we give a message, but
1125 * only, if we won't show the search stat later anyhow,
1126 * (so SEARCH_COUNT must be absent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 * This message is also remembered in keep_msg for when the screen
1128 * is redrawn. The keep_msg is cleared whenever another message is
1129 * written.
1130 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001131 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 lnum = 1;
Christian Brabandt34a6a362023-05-06 19:20:20 +01001135 if (!shortmess(SHM_SEARCH)
1136 && shortmess(SHM_SEARCHCOUNT)
1137 && (options & SEARCH_MSG))
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001138 give_warning((char_u *)_(dir == BACKWARD
1139 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001140 if (extra_arg != NULL)
1141 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
Paul Ollis65745772022-06-05 16:55:54 +01001143 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001144#ifdef FEAT_SEARCH_EXTRA
1145 || break_loop
1146#endif
1147 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 break;
1149 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001150 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001152#ifdef FEAT_RELTIME
1153 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1154 disable_regexp_timeout();
1155#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001156 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001158 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001161 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1163 {
1164 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001165 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001167 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001169 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 return FAIL;
1172 }
1173
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001174 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001175 if (pos->lnum > buf->b_ml.ml_line_count)
1176 {
1177 pos->lnum = buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01001178 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001179 if (pos->col > 0)
1180 --pos->col;
1181 }
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 return submatch + 1;
1184}
1185
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001186#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001187 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001188set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001189{
1190 spats[0].off.dir = cdir;
1191}
1192
1193 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001194set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001195{
1196 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1197}
1198
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199/*
1200 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001201 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 */
1203 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001204first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205{
1206 int submatch;
1207
1208 for (submatch = 1; ; ++submatch)
1209 {
1210 if (rp->startpos[submatch].lnum >= 0)
1211 break;
1212 if (submatch == 9)
1213 {
1214 submatch = 0;
1215 break;
1216 }
1217 }
1218 return submatch;
1219}
1220#endif
1221
1222/*
1223 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001224 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 * If 'dirc' is 0: use previous dir.
1226 * If 'pat' is NULL or empty : use previous string.
1227 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1228 * If 'options & SEARCH_ECHO': echo the search command and handle options
1229 * If 'options & SEARCH_MSG' : may give error message
1230 * If 'options & SEARCH_OPT' : interpret optional flags
1231 * If 'options & SEARCH_HIS' : put search pattern in history
1232 * If 'options & SEARCH_NOOF': don't add offset to position
1233 * If 'options & SEARCH_MARK': set previous context mark
1234 * If 'options & SEARCH_KEEP': keep previous search pattern
1235 * If 'options & SEARCH_START': accept match at curpos itself
1236 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1237 *
1238 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1239 * makes the movement linewise without moving the match position.
1240 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001241 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 */
1243 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001244do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001245 oparg_T *oap, // can be NULL
1246 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001247 int search_delim, // the delimiter for the search, e.g. '%' in
1248 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001249 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +02001250 size_t patlen,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001251 long count,
1252 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001253 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001255 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 char_u *searchstr;
John Marriott8c85a2a2024-05-20 19:18:26 +02001257 size_t searchstrlen;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001258 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001259 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 char_u *p;
1261 long c;
1262 char_u *dircp;
1263 char_u *strcopy = NULL;
1264 char_u *ps;
John Marriott8c85a2a2024-05-20 19:18:26 +02001265 int show_search_stats;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001266 char_u *msgbuf = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +02001267 size_t msgbuflen = 0;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001268 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
John Marriott8c85a2a2024-05-20 19:18:26 +02001270 searchcmdlen = 0;
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 /*
1273 * A line offset is not remembered, this is vi compatible.
1274 */
1275 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1276 {
1277 spats[0].off.line = FALSE;
1278 spats[0].off.off = 0;
1279 }
1280
1281 /*
1282 * Save the values for when (options & SEARCH_KEEP) is used.
1283 * (there is no "if ()" around this because gcc wants them initialized)
1284 */
1285 old_off = spats[0].off;
1286
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001287 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288
1289 /*
1290 * Find out the direction of the search.
1291 */
1292 if (dirc == 0)
1293 dirc = spats[0].off.dir;
1294 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001297#if defined(FEAT_EVAL)
1298 set_vv_searchforward();
1299#endif
1300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 if (options & SEARCH_REV)
1302 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001303#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001304 // There is a bug in the Visual C++ 2.2 compiler which means that
1305 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 dirc = (dirc == '/') ? '?' : '/';
1307#else
1308 if (dirc == '/')
1309 dirc = '?';
1310 else
1311 dirc = '/';
1312#endif
1313 }
1314
1315#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001316 // If the cursor is in a closed fold, don't find another match in the same
1317 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 if (dirc == '/')
1319 {
1320 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001321 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 else
1324 {
1325 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1326 pos.col = 0;
1327 }
1328#endif
1329
1330#ifdef FEAT_SEARCH_EXTRA
1331 /*
1332 * Turn 'hlsearch' highlighting back on.
1333 */
1334 if (no_hlsearch && !(options & SEARCH_KEEP))
1335 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001336 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001337 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339#endif
1340
1341 /*
1342 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1343 */
1344 for (;;)
1345 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001346 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 searchstr = pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001349 searchstrlen = patlen;
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001352 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001353 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001355 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001357 if (spats[RE_SUBST].pat == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001358 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001359 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001360 retval = 0;
1361 goto end_do_search;
1362 }
John Marriott8c85a2a2024-05-20 19:18:26 +02001363 searchstr = spats[RE_SUBST].pat;
1364 searchstrlen = spats[RE_SUBST].patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001366 else
1367 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001368 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001369 searchstr = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02001370 searchstrlen = 0;
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001374 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 /*
1377 * Find end of regular expression.
1378 * If there is a matching '/' or '?', toss it.
1379 */
1380 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001381 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001382 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (strcopy != ps)
1384 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001385 size_t len = STRLEN(strcopy);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001386 // made a copy of "pat" to change "\?" to "?"
John Marriott8c85a2a2024-05-20 19:18:26 +02001387 searchcmdlen += (int)(patlen - len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 pat = strcopy;
John Marriott8c85a2a2024-05-20 19:18:26 +02001389 patlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 searchstr = strcopy;
John Marriott8c85a2a2024-05-20 19:18:26 +02001391 searchstrlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001393 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001395 searchstrlen = p - pat;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001396 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 *p++ = NUL;
1398 }
1399 spats[0].off.line = FALSE;
1400 spats[0].off.end = FALSE;
1401 spats[0].off.off = 0;
1402 /*
1403 * Check for a line offset or a character offset.
1404 * For get_address (echo off) we don't check for a character
1405 * offset, because it is meaningless and the 's' could be a
1406 * substitute command.
1407 */
1408 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1409 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001410 else if ((options & SEARCH_OPT)
1411 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001413 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 spats[0].off.end = SEARCH_END;
1415 ++p;
1416 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001417 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001419 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1421 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001422 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001424 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 spats[0].off.off = 1;
1426 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001427 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 ++p;
1429 }
1430
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001431 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 searchcmdlen += (int)(p - pat);
1433
John Marriott8c85a2a2024-05-20 19:18:26 +02001434 patlen -= p - pat;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001435 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
1437
John Marriott8c85a2a2024-05-20 19:18:26 +02001438 show_search_stats = FALSE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001439 if ((options & SEARCH_ECHO) && messaging()
1440 && !msg_silent
1441 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
Bram Moolenaar984f0312019-05-24 13:11:47 +02001443 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001444 size_t off_len = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02001445 size_t plen;
1446 size_t msgbufsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001448 // Compute msg_row early.
1449 msg_start();
1450
Bram Moolenaar984f0312019-05-24 13:11:47 +02001451 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001452 if (!cmd_silent &&
1453 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001454 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001455 off_buf[off_len++] = dirc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001456 if (spats[0].off.end)
John Marriott8c85a2a2024-05-20 19:18:26 +02001457 off_buf[off_len++] = 'e';
Bram Moolenaar984f0312019-05-24 13:11:47 +02001458 else if (!spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001459 off_buf[off_len++] = 's';
Bram Moolenaar984f0312019-05-24 13:11:47 +02001460 if (spats[0].off.off > 0 || spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001461 off_buf[off_len++] = '+';
1462 off_buf[off_len] = NUL;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001463 if (spats[0].off.off != 0 || spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001464 off_len += vim_snprintf((char *)off_buf + off_len, sizeof(off_buf) - off_len, "%ld", spats[0].off.off);
Bram Moolenaar984f0312019-05-24 13:11:47 +02001465 }
1466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (*searchstr == NUL)
John Marriott8c85a2a2024-05-20 19:18:26 +02001468 {
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001469 p = spats[0].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001470 plen = spats[0].patlen;
1471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 else
John Marriott8c85a2a2024-05-20 19:18:26 +02001473 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 p = searchstr;
John Marriott8c85a2a2024-05-20 19:18:26 +02001475 plen = searchstrlen;
1476 }
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001477
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001478 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001479 {
1480 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001481 // search stat. Use all the space available, so that the
1482 // search state is right aligned. If there is not enough space
1483 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001484 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001485 // Use all the columns.
John Marriott8c85a2a2024-05-20 19:18:26 +02001486 msgbufsize = (int)(Rows - msg_row) * Columns - 1;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001487 else
1488 // Use up to 'showcmd' column.
John Marriott8c85a2a2024-05-20 19:18:26 +02001489 msgbufsize = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
1490 if (msgbufsize < plen + off_len + SEARCH_STAT_BUF_LEN + 3)
1491 msgbufsize = plen + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001492 }
1493 else
1494 // Reserve enough space for the search pattern + offset.
John Marriott8c85a2a2024-05-20 19:18:26 +02001495 msgbufsize = plen + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001496
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001497 vim_free(msgbuf);
John Marriott8c85a2a2024-05-20 19:18:26 +02001498 msgbuf = alloc(msgbufsize);
1499 if (msgbuf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001501 msgbuflen = 0;
1502 }
1503 else
1504 {
1505 vim_memset(msgbuf, ' ', msgbufsize);
1506 msgbuflen = msgbufsize - 1;
1507 msgbuf[msgbuflen] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001508 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1509 // empty for the search_stat feature.
1510 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001511 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001512 char_u *trunc;
1513
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001514 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001516 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1517 {
1518 // Use a space to draw the composing char on.
1519 msgbuf[1] = ' ';
John Marriott8c85a2a2024-05-20 19:18:26 +02001520 mch_memmove(msgbuf + 2, p, plen);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001521 }
1522 else
John Marriott8c85a2a2024-05-20 19:18:26 +02001523 mch_memmove(msgbuf + 1, p, plen);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001524 if (off_len > 0)
John Marriott8c85a2a2024-05-20 19:18:26 +02001525 mch_memmove(msgbuf + plen + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001527 trunc = msg_strtrunc(msgbuf, TRUE);
1528 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001530 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001531 msgbuf = trunc;
John Marriott8c85a2a2024-05-20 19:18:26 +02001532 msgbuflen = STRLEN(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001535#ifdef FEAT_RIGHTLEFT
1536 // The search pattern could be shown on the right in
1537 // rightleft mode, but the 'ruler' and 'showcmd' area use
1538 // it too, thus it would be blanked out again very soon.
1539 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001540 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1541 {
1542 char_u *r;
1543 size_t pat_len;
1544
1545 r = reverse_text(msgbuf);
1546 if (r != NULL)
1547 {
1548 vim_free(msgbuf);
1549 msgbuf = r;
1550 // move reversed text to beginning of buffer
1551 while (*r != NUL && *r == ' ')
1552 r++;
John Marriott8c85a2a2024-05-20 19:18:26 +02001553 pat_len = msgbuf + msgbuflen - r;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001554 mch_memmove(msgbuf, r, pat_len);
1555 // overwrite old text
1556 if ((size_t)(r - msgbuf) >= pat_len)
1557 vim_memset(r, ' ', pat_len);
1558 else
1559 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1560 }
1561 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001562#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001563 msg_outtrans(msgbuf);
1564 msg_clr_eos();
1565 msg_check();
1566
1567 gotocmdline(FALSE);
1568 out_flush();
1569 msg_nowait = TRUE; // don't wait for this message
1570 }
John Marriott8c85a2a2024-05-20 19:18:26 +02001571
1572 if (!shortmess(SHM_SEARCHCOUNT))
1573 show_search_stats = TRUE;
1574 } // msgbuf != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576
1577 /*
1578 * If there is a character offset, subtract it from the current
1579 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001580 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 * This is not done for a line offset, because then we would not be vi
1582 * compatible.
1583 */
Bram Moolenaared203462004-06-16 11:19:22 +00001584 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
1586 if (spats[0].off.off > 0)
1587 {
1588 for (c = spats[0].off.off; c; --c)
1589 if (decl(&pos) == -1)
1590 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001591 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001593 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 pos.col = MAXCOL;
1595 }
1596 }
1597 else
1598 {
1599 for (c = spats[0].off.off; c; ++c)
1600 if (incl(&pos) == -1)
1601 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001602 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1605 pos.col = 0;
1606 }
1607 }
1608 }
1609
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001610 /*
1611 * The actual search.
1612 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001613 c = searchit(curwin, curbuf, &pos, NULL,
1614 dirc == '/' ? FORWARD : BACKWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02001615 searchstr, searchstrlen, count, spats[0].off.end + (options &
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1617 + SEARCH_MSG + SEARCH_START
1618 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001619 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001622 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001623
1624 if (!shortmess(SHM_SEARCH)
1625 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1626 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001627 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 if (c == FAIL)
1630 {
1631 retval = 0;
1632 goto end_do_search;
1633 }
1634 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001635 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001637 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
1639 /*
1640 * Add character and/or line offset
1641 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001642 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001644 pos_T org_pos = pos;
1645
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001646 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 {
1648 c = pos.lnum + spats[0].off.off;
1649 if (c < 1)
1650 pos.lnum = 1;
1651 else if (c > curbuf->b_ml.ml_line_count)
1652 pos.lnum = curbuf->b_ml.ml_line_count;
1653 else
1654 pos.lnum = c;
1655 pos.col = 0;
1656
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001657 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001659 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001661 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001662 c = spats[0].off.off;
1663 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001665 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (incl(&pos) == -1)
1667 break;
1668 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001669 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 else
1671 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001672 while (c++ < 0)
1673 if (decl(&pos) == -1)
1674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001677 if (!EQUAL_POS(pos, org_pos))
1678 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 }
1680
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001681 // Show [1/15] if 'S' is not in 'shortmess'.
John Marriott8c85a2a2024-05-20 19:18:26 +02001682 if (show_search_stats)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001683 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
John Marriott8c85a2a2024-05-20 19:18:26 +02001684 show_top_bot_msg, msgbuf, msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001685 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001686#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001687 || (!(fdo_flags & FDO_SEARCH)
1688 && hasFolding(curwin->w_cursor.lnum,
1689 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001690#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001691 ),
1692 SEARCH_STAT_DEF_MAX_COUNT,
1693 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 /*
1696 * The search command can be followed by a ';' to do another search.
1697 * For example: "/pat/;/foo/+3;?bar"
1698 * This is like doing another search command, except:
1699 * - The remembered direction '/' or '?' is from the first search.
1700 * - When an error happens the cursor isn't moved at all.
1701 * Don't do this when called by get_address() (it handles ';' itself).
1702 */
1703 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1704 break;
1705
1706 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001707 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 if (dirc != '?' && dirc != '/')
1709 {
1710 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001711 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 goto end_do_search;
1713 }
1714 ++pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001715 --patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717
1718 if (options & SEARCH_MARK)
1719 setpcmark();
1720 curwin->w_cursor = pos;
1721 curwin->w_set_curswant = TRUE;
1722
1723end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001724 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 spats[0].off = old_off;
1726 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001727 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
1729 return retval;
1730}
1731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732/*
1733 * search_for_exact_line(buf, pos, dir, pat)
1734 *
1735 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001736 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001738 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 * Return OK for success, or FAIL if no line found.
1740 */
1741 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001742search_for_exact_line(
1743 buf_T *buf,
1744 pos_T *pos,
1745 int dir,
1746 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747{
1748 linenr_T start = 0;
1749 char_u *ptr;
1750 char_u *p;
1751
1752 if (buf->b_ml.ml_line_count == 0)
1753 return FAIL;
1754 for (;;)
1755 {
1756 pos->lnum += dir;
1757 if (pos->lnum < 1)
1758 {
1759 if (p_ws)
1760 {
1761 pos->lnum = buf->b_ml.ml_line_count;
1762 if (!shortmess(SHM_SEARCH))
1763 give_warning((char_u *)_(top_bot_msg), TRUE);
1764 }
1765 else
1766 {
1767 pos->lnum = 1;
1768 break;
1769 }
1770 }
1771 else if (pos->lnum > buf->b_ml.ml_line_count)
1772 {
1773 if (p_ws)
1774 {
1775 pos->lnum = 1;
1776 if (!shortmess(SHM_SEARCH))
1777 give_warning((char_u *)_(bot_top_msg), TRUE);
1778 }
1779 else
1780 {
1781 pos->lnum = 1;
1782 break;
1783 }
1784 }
1785 if (pos->lnum == start)
1786 break;
1787 if (start == 0)
1788 start = pos->lnum;
1789 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1790 p = skipwhite(ptr);
1791 pos->col = (colnr_T) (p - ptr);
1792
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001793 // when adding lines the matching line may be empty but it is not
1794 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001795 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 {
1797 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1798 return OK;
1799 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001800 else if (*p != NUL) // ignore empty lines
1801 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001802 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1803 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 return OK;
1805 }
1806 }
1807 return FAIL;
1808}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809
1810/*
1811 * Character Searches
1812 */
1813
1814/*
1815 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1816 * position of the character, otherwise move to just before the char.
1817 * Do this "cap->count1" times.
1818 * Return FAIL or OK.
1819 */
1820 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001821searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001823 int c = cap->nchar; // char to search for
1824 int dir = cap->arg; // TRUE for searching forward
1825 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 int col;
1827 char_u *p;
1828 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001829 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001831 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001833 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001835 *lastc = c;
1836 set_csearch_direction(dir);
1837 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001838 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if (cap->ncharC1 != 0)
1840 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001841 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1842 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001844 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1845 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
1848 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001849 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
zeertzjqe5d91ba2023-05-14 17:39:18 +01001851 if (*lastc == NUL && lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001853 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 dir = -lastcdir;
1855 else
1856 dir = lastcdir;
1857 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001858 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001859 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001860
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001861 // Force a move of at least one char, so ";" and "," will move the
1862 // cursor, even if the cursor is right in front of char we are looking
1863 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001864 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001865 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 }
1867
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001868 if (dir == BACKWARD)
1869 cap->oap->inclusive = FALSE;
1870 else
1871 cap->oap->inclusive = TRUE;
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 p = ml_get_curline();
1874 col = curwin->w_cursor.col;
zeertzjq94b7c322024-03-12 21:50:32 +01001875 len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876
1877 while (count--)
1878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (has_mbyte)
1880 {
1881 for (;;)
1882 {
1883 if (dir > 0)
1884 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001885 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (col >= len)
1887 return FAIL;
1888 }
1889 else
1890 {
1891 if (col == 0)
1892 return FAIL;
1893 col -= (*mb_head_off)(p, p + col - 1) + 1;
1894 }
zeertzjqe5d91ba2023-05-14 17:39:18 +01001895 if (lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001897 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 break;
1899 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001900 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001901 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001902 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001903 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 }
1906 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 for (;;)
1909 {
1910 if ((col += dir) < 0 || col >= len)
1911 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001912 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001914 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
1917 }
1918
1919 if (t_cmd)
1920 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001921 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (has_mbyte)
1924 {
1925 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001926 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001927 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001929 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 col -= (*mb_head_off)(p, p + col);
1931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
1933 curwin->w_cursor.col = col;
1934
1935 return OK;
1936}
1937
1938/*
1939 * "Other" Searches
1940 */
1941
1942/*
1943 * findmatch - find the matching paren or brace
1944 *
1945 * Improvement over vi: Braces inside quotes are ignored.
1946 */
1947 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001948findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
1950 return findmatchlimit(oap, initc, 0, 0);
1951}
1952
1953/*
1954 * Return TRUE if the character before "linep[col]" equals "ch".
1955 * Return FALSE if "col" is zero.
1956 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1957 * is NULL.
1958 * Handles multibyte string correctly.
1959 */
1960 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001961check_prevcol(
1962 char_u *linep,
1963 int col,
1964 int ch,
1965 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966{
1967 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 if (col > 0 && has_mbyte)
1969 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (prevcol)
1971 *prevcol = col;
1972 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1973}
1974
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001975/*
1976 * Raw string start is found at linep[startpos.col - 1].
1977 * Return TRUE if the matching end can be found between startpos and endpos.
1978 */
1979 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001980find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001981{
1982 char_u *p;
1983 char_u *delim_copy;
1984 size_t delim_len;
1985 linenr_T lnum;
1986 int found = FALSE;
1987
1988 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1989 ;
1990 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001991 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001992 if (delim_copy == NULL)
1993 return FALSE;
1994 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1995 {
1996 char_u *line = ml_get(lnum);
1997
1998 for (p = line + (lnum == startpos->lnum
1999 ? startpos->col + 1 : 0); *p; ++p)
2000 {
2001 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
2002 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02002003 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
2004 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002005 {
2006 found = TRUE;
2007 break;
2008 }
2009 }
2010 if (found)
2011 break;
2012 }
2013 vim_free(delim_copy);
2014 return found;
2015}
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01002018 * Check matchpairs option for "*initc".
2019 * If there is a match set "*initc" to the matching character and "*findc" to
2020 * the opposite character. Set "*backwards" to the direction.
2021 * When "switchit" is TRUE swap the direction.
2022 */
2023 static void
2024find_mps_values(
2025 int *initc,
2026 int *findc,
2027 int *backwards,
2028 int switchit)
2029{
2030 char_u *ptr;
2031
2032 ptr = curbuf->b_p_mps;
2033 while (*ptr != NUL)
2034 {
2035 if (has_mbyte)
2036 {
2037 char_u *prev;
2038
2039 if (mb_ptr2char(ptr) == *initc)
2040 {
2041 if (switchit)
2042 {
2043 *findc = *initc;
2044 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2045 *backwards = TRUE;
2046 }
2047 else
2048 {
2049 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2050 *backwards = FALSE;
2051 }
2052 return;
2053 }
2054 prev = ptr;
2055 ptr += mb_ptr2len(ptr) + 1;
2056 if (mb_ptr2char(ptr) == *initc)
2057 {
2058 if (switchit)
2059 {
2060 *findc = *initc;
2061 *initc = mb_ptr2char(prev);
2062 *backwards = FALSE;
2063 }
2064 else
2065 {
2066 *findc = mb_ptr2char(prev);
2067 *backwards = TRUE;
2068 }
2069 return;
2070 }
2071 ptr += mb_ptr2len(ptr);
2072 }
2073 else
2074 {
2075 if (*ptr == *initc)
2076 {
2077 if (switchit)
2078 {
2079 *backwards = TRUE;
2080 *findc = *initc;
2081 *initc = ptr[2];
2082 }
2083 else
2084 {
2085 *backwards = FALSE;
2086 *findc = ptr[2];
2087 }
2088 return;
2089 }
2090 ptr += 2;
2091 if (*ptr == *initc)
2092 {
2093 if (switchit)
2094 {
2095 *backwards = FALSE;
2096 *findc = *initc;
2097 *initc = ptr[-2];
2098 }
2099 else
2100 {
2101 *backwards = TRUE;
2102 *findc = ptr[-2];
2103 }
2104 return;
2105 }
2106 ++ptr;
2107 }
2108 if (*ptr == ',')
2109 ++ptr;
2110 }
2111}
2112
2113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002115 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2116 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 *
2118 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002119 * character at or after the cursor. Special values:
2120 * '*' look for C-style comment / *
2121 * '/' look for C-style comment / *, ignoring comment-end
2122 * '#' look for preprocessor directives
2123 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 *
2125 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2126 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2127 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2128 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002129 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002130 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002131 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002134findmatchlimit(
2135 oparg_T *oap,
2136 int initc,
2137 int flags,
2138 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002140 static pos_T pos; // current search position
2141 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002143 int count = 0; // cumulative number of braces
2144 int backwards = FALSE; // init for gcc
2145 int raw_string = FALSE; // search for raw string
2146 int inquote = FALSE; // TRUE when inside quotes
2147 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002149 int do_quotes; // check for quotes in current line
2150 int at_start; // do_quotes value at start position
2151 int hash_dir = 0; // Direction searched for # things
2152 int comment_dir = 0; // Direction searched for comments
2153 pos_T match_pos; // Where last slash-star was found
2154 int start_in_quotes; // start position is in quotes
2155 int traveled = 0; // how far we've searched so far
2156 int ignore_cend = FALSE; // ignore comment end
2157 int cpo_match; // vi compatible matching
2158 int cpo_bsl; // don't recognize backslashes
2159 int match_escaped = 0; // search for escaped match
2160 int dir; // Direction to search
2161 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002162 int lispcomm = FALSE; // inside of Lisp-style comment
2163 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002166 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 linep = ml_get(pos.lnum);
2168
2169 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2170 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2171
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002172 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (flags & FM_BACKWARD)
2174 dir = BACKWARD;
2175 else if (flags & FM_FORWARD)
2176 dir = FORWARD;
2177 else
2178 dir = 0;
2179
2180 /*
2181 * if initc given, look in the table for the matching character
2182 * '/' and '*' are special cases: look for start or end of comment.
2183 * When '/' is used, we ignore running backwards into an star-slash, for
2184 * "[*" command, we just want to find any comment.
2185 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002186 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
2188 comment_dir = dir;
2189 if (initc == '/')
2190 ignore_cend = TRUE;
2191 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002192 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 initc = NUL;
2194 }
2195 else if (initc != '#' && initc != NUL)
2196 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002197 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002198 if (dir)
2199 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002200 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 return NULL;
2202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 else
2204 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002205 /*
2206 * Either initc is '#', or no initc was given and we need to look
2207 * under the cursor.
2208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (initc == '#')
2210 {
2211 hash_dir = dir;
2212 }
2213 else
2214 {
2215 /*
2216 * initc was not given, must look for something to match under
2217 * or near the cursor.
2218 * Only check for special things when 'cpo' doesn't have '%'.
2219 */
2220 if (!cpo_match)
2221 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002222 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 ptr = skipwhite(linep);
2224 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2225 {
2226 ptr = skipwhite(ptr + 1);
2227 if ( STRNCMP(ptr, "if", 2) == 0
2228 || STRNCMP(ptr, "endif", 5) == 0
2229 || STRNCMP(ptr, "el", 2) == 0)
2230 hash_dir = 1;
2231 }
2232
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002233 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 else if (linep[pos.col] == '/')
2235 {
2236 if (linep[pos.col + 1] == '*')
2237 {
2238 comment_dir = FORWARD;
2239 backwards = FALSE;
2240 pos.col++;
2241 }
2242 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2243 {
2244 comment_dir = BACKWARD;
2245 backwards = TRUE;
2246 pos.col--;
2247 }
2248 }
2249 else if (linep[pos.col] == '*')
2250 {
2251 if (linep[pos.col + 1] == '/')
2252 {
2253 comment_dir = BACKWARD;
2254 backwards = TRUE;
2255 }
2256 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2257 {
2258 comment_dir = FORWARD;
2259 backwards = FALSE;
2260 }
2261 }
2262 }
2263
2264 /*
2265 * If we are not on a comment or the # at the start of a line, then
2266 * look for brace anywhere on this line after the cursor.
2267 */
2268 if (!hash_dir && !comment_dir)
2269 {
2270 /*
2271 * Find the brace under or after the cursor.
2272 * If beyond the end of the line, use the last character in
2273 * the line.
2274 */
2275 if (linep[pos.col] == NUL && pos.col)
2276 --pos.col;
2277 for (;;)
2278 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002279 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (initc == NUL)
2281 break;
2282
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002283 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 if (findc)
2285 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002286 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288 if (!findc)
2289 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002290 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 if (!cpo_match && *skipwhite(linep) == '#')
2292 hash_dir = 1;
2293 else
2294 return NULL;
2295 }
2296 else if (!cpo_bsl)
2297 {
2298 int col, bslcnt = 0;
2299
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002300 // Set "match_escaped" if there are an odd number of
2301 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2303 bslcnt++;
2304 match_escaped = (bslcnt & 1);
2305 }
2306 }
2307 }
2308 if (hash_dir)
2309 {
2310 /*
2311 * Look for matching #if, #else, #elif, or #endif
2312 */
2313 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002314 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 if (initc != '#')
2316 {
2317 ptr = skipwhite(skipwhite(linep) + 1);
2318 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2319 hash_dir = 1;
2320 else if (STRNCMP(ptr, "endif", 5) == 0)
2321 hash_dir = -1;
2322 else
2323 return NULL;
2324 }
2325 pos.col = 0;
2326 while (!got_int)
2327 {
2328 if (hash_dir > 0)
2329 {
2330 if (pos.lnum == curbuf->b_ml.ml_line_count)
2331 break;
2332 }
2333 else if (pos.lnum == 1)
2334 break;
2335 pos.lnum += hash_dir;
2336 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002337 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 ptr = skipwhite(linep);
2339 if (*ptr != '#')
2340 continue;
2341 pos.col = (colnr_T) (ptr - linep);
2342 ptr = skipwhite(ptr + 1);
2343 if (hash_dir > 0)
2344 {
2345 if (STRNCMP(ptr, "if", 2) == 0)
2346 count++;
2347 else if (STRNCMP(ptr, "el", 2) == 0)
2348 {
2349 if (count == 0)
2350 return &pos;
2351 }
2352 else if (STRNCMP(ptr, "endif", 5) == 0)
2353 {
2354 if (count == 0)
2355 return &pos;
2356 count--;
2357 }
2358 }
2359 else
2360 {
2361 if (STRNCMP(ptr, "if", 2) == 0)
2362 {
2363 if (count == 0)
2364 return &pos;
2365 count--;
2366 }
2367 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2368 {
2369 if (count == 0)
2370 return &pos;
2371 }
2372 else if (STRNCMP(ptr, "endif", 5) == 0)
2373 count++;
2374 }
2375 }
2376 return NULL;
2377 }
2378 }
2379
2380#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002381 // This is just guessing: when 'rightleft' is set, search for a matching
2382 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2384 backwards = !backwards;
2385#endif
2386
2387 do_quotes = -1;
2388 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002389 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002390
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002391 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002392 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002394 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002395 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 while (!got_int)
2398 {
2399 /*
2400 * Go to the next position, forward or backward. We could use
2401 * inc() and dec() here, but that is much slower
2402 */
2403 if (backwards)
2404 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002405 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002406 if (lispcomm && pos.col < (colnr_T)comment_col)
2407 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002408 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002410 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 break;
2412 --pos.lnum;
2413
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002414 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 break;
2416
2417 linep = ml_get(pos.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01002418 pos.col = ml_get_len(pos.lnum); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 do_quotes = -1;
2420 line_breakcheck();
2421
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002422 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002423 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002425 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002426 if (lisp && comment_col != MAXCOL)
2427 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 else
2430 {
2431 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (has_mbyte)
2433 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 }
2435 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002436 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002438 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002439 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002440 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002441 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002442 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002444 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002445 // line is exhausted and comment with it,
2446 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002447 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 break;
2449 ++pos.lnum;
2450
2451 if (maxtravel && traveled++ > maxtravel)
2452 break;
2453
2454 linep = ml_get(pos.lnum);
2455 pos.col = 0;
2456 do_quotes = -1;
2457 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002458 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002459 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461 else
2462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002464 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 ++pos.col;
2467 }
2468 }
2469
2470 /*
2471 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2472 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002473 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2474 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002476 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002478 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
2480
2481 if (comment_dir)
2482 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002483 // Note: comments do not nest, and we ignore quotes in them
2484 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (comment_dir == FORWARD)
2486 {
2487 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2488 {
2489 pos.col++;
2490 return &pos;
2491 }
2492 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002493 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 /*
2496 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002497 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
2499 if (pos.col == 0)
2500 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002501 else if (raw_string)
2502 {
2503 if (linep[pos.col - 1] == 'R'
2504 && linep[pos.col] == '"'
2505 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2506 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002507 // Possible start of raw string. Now that we have the
2508 // delimiter we can check if it ends before where we
2509 // started searching, or before the previously found
2510 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002511 if (!find_rawstring_end(linep, &pos,
2512 count > 0 ? &match_pos : &curwin->w_cursor))
2513 {
2514 count++;
2515 match_pos = pos;
2516 match_pos.col--;
2517 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002518 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002519 }
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 else if ( linep[pos.col - 1] == '/'
2522 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002523 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 && (int)pos.col < comment_col)
2525 {
2526 count++;
2527 match_pos = pos;
2528 match_pos.col--;
2529 }
2530 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2531 {
2532 if (count > 0)
2533 pos = match_pos;
2534 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2535 && (int)pos.col <= comment_col)
2536 pos.col -= 2;
2537 else if (ignore_cend)
2538 continue;
2539 else
2540 return NULL;
2541 return &pos;
2542 }
2543 }
2544 continue;
2545 }
2546
2547 /*
2548 * If smart matching ('cpoptions' does not contain '%'), braces inside
2549 * of quotes are ignored, but only if there is an even number of
2550 * quotes in the line.
2551 */
2552 if (cpo_match)
2553 do_quotes = 0;
2554 else if (do_quotes == -1)
2555 {
2556 /*
2557 * Count the number of quotes in the line, skipping \" and '"'.
2558 * Watch out for "\\".
2559 */
2560 at_start = do_quotes;
2561 for (ptr = linep; *ptr; ++ptr)
2562 {
2563 if (ptr == linep + pos.col + backwards)
2564 at_start = (do_quotes & 1);
2565 if (*ptr == '"'
2566 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2567 ++do_quotes;
2568 if (*ptr == '\\' && ptr[1] != NUL)
2569 ++ptr;
2570 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002571 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 /*
2574 * If we find an uneven count, check current line and previous
2575 * one for a '\' at the end.
2576 */
2577 if (!do_quotes)
2578 {
2579 inquote = FALSE;
2580 if (ptr[-1] == '\\')
2581 {
2582 do_quotes = 1;
2583 if (start_in_quotes == MAYBE)
2584 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002585 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 inquote = TRUE;
2587 start_in_quotes = TRUE;
2588 }
2589 else if (backwards)
2590 inquote = TRUE;
2591 }
2592 if (pos.lnum > 1)
2593 {
2594 ptr = ml_get(pos.lnum - 1);
zeertzjq94b7c322024-03-12 21:50:32 +01002595 if (*ptr && *(ptr + ml_get_len(pos.lnum - 1) - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 do_quotes = 1;
2598 if (start_in_quotes == MAYBE)
2599 {
2600 inquote = at_start;
2601 if (inquote)
2602 start_in_quotes = TRUE;
2603 }
2604 else if (!backwards)
2605 inquote = TRUE;
2606 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002607
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002608 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002609 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 }
2611 }
2612 }
2613 if (start_in_quotes == MAYBE)
2614 start_in_quotes = FALSE;
2615
2616 /*
2617 * If 'smartmatch' is set:
2618 * Things inside quotes are ignored by setting 'inquote'. If we
2619 * find a quote without a preceding '\' invert 'inquote'. At the
2620 * end of a line not ending in '\' we reset 'inquote'.
2621 *
2622 * In lines with an uneven number of quotes (without preceding '\')
2623 * we do not know which part to ignore. Therefore we only set
2624 * inquote if the number of quotes in a line is even, unless this
2625 * line or the previous one ends in a '\'. Complicated, isn't it?
2626 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002627 c = PTR2CHAR(linep + pos.col);
2628 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002631 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2633 {
2634 inquote = FALSE;
2635 start_in_quotes = FALSE;
2636 }
2637 break;
2638
2639 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002640 // a quote that is preceded with an odd number of backslashes is
2641 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 if (do_quotes)
2643 {
2644 int col;
2645
2646 for (col = pos.col - 1; col >= 0; --col)
2647 if (linep[col] != '\\')
2648 break;
2649 if ((((int)pos.col - 1 - col) & 1) == 0)
2650 {
2651 inquote = !inquote;
2652 start_in_quotes = FALSE;
2653 }
2654 }
2655 break;
2656
2657 /*
2658 * If smart matching ('cpoptions' does not contain '%'):
2659 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2660 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2661 * skipped, there is never a brace in them.
2662 * Ignore this when finding matches for `'.
2663 */
2664 case '\'':
2665 if (!cpo_match && initc != '\'' && findc != '\'')
2666 {
2667 if (backwards)
2668 {
2669 if (pos.col > 1)
2670 {
2671 if (linep[pos.col - 2] == '\'')
2672 {
2673 pos.col -= 2;
2674 break;
2675 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002676 else if (linep[pos.col - 2] == '\\'
2677 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
2679 pos.col -= 3;
2680 break;
2681 }
2682 }
2683 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002684 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002686 if (linep[pos.col + 1] == '\\'
2687 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {
2689 pos.col += 3;
2690 break;
2691 }
2692 else if (linep[pos.col + 2] == '\'')
2693 {
2694 pos.col += 2;
2695 break;
2696 }
2697 }
2698 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002699 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002702 /*
2703 * For Lisp skip over backslashed (), {} and [].
2704 * (actually, we skip #\( et al)
2705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002707 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002708 && pos.col > 1
2709 && check_prevcol(linep, pos.col, '\\', NULL)
2710 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002713 // Check for match outside of quotes, and inside of
2714 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 if ((!inquote || start_in_quotes == TRUE)
2716 && (c == initc || c == findc))
2717 {
2718 int col, bslcnt = 0;
2719
2720 if (!cpo_bsl)
2721 {
2722 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2723 bslcnt++;
2724 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002725 // Only accept a match when 'M' is in 'cpo' or when escaping
2726 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2728 {
2729 if (c == initc)
2730 count++;
2731 else
2732 {
2733 if (count == 0)
2734 return &pos;
2735 count--;
2736 }
2737 }
2738 }
2739 }
2740 }
2741
2742 if (comment_dir == BACKWARD && count > 0)
2743 {
2744 pos = match_pos;
2745 return &pos;
2746 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002747 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748}
2749
2750/*
2751 * Check if line[] contains a / / comment.
2752 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002754 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002755check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 char_u *p;
2758
2759 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002760 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002761 if (curbuf->b_p_lisp)
2762 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002763 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002764 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002765 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002766
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002767 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002768 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002769 {
2770 if (*p == '"')
2771 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002772 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002773 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002774 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002775 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002776 }
2777 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002778 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002779 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002780 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002781 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002782 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002783 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2784 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002785 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002786 ++p;
2787 }
2788 }
2789 else
2790 p = NULL;
2791 }
2792 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002793 while ((p = vim_strchr(p, '/')) != NULL)
2794 {
2795 // Accept a double /, unless it's preceded with * and followed by
2796 // *, because * / / * is an end and start of a C comment. Only
2797 // accept the position if it is not inside a string.
2798 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002799 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002800 break;
2801 ++p;
2802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803
2804 if (p == NULL)
2805 return MAXCOL;
2806 return (int)(p - line);
2807}
2808
2809/*
2810 * Move cursor briefly to character matching the one under the cursor.
2811 * Used for Insert mode and "r" command.
2812 * Show the match only if it is visible on the screen.
2813 * If there isn't a match, then beep.
2814 */
2815 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002816showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002817 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 pos_T *lpos, save_cursor;
2820 pos_T mpos;
2821 colnr_T vcol;
2822 long save_so;
2823 long save_siso;
2824#ifdef CURSOR_SHAPE
2825 int save_state;
2826#endif
2827 colnr_T save_dollar_vcol;
2828 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002829 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2830 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831
2832 /*
2833 * Only show match for chars in the 'matchpairs' option.
2834 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002835 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002836 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
2838#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002839 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002840 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002841#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002842 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002843 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844#ifdef FEAT_RIGHTLEFT
2845 && !(curwin->w_p_rl ^ p_ri)
2846#endif
2847 )
2848 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002849 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002850 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 return;
2852 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002853 if (*p == NUL)
2854 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002856 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002858 vim_beep(BO_MATCH);
2859 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002861
2862 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2863 return;
2864
2865 if (!curwin->w_p_wrap)
2866 getvcol(curwin, lpos, NULL, &vcol, NULL);
2867
2868 int col_visible = (curwin->w_p_wrap
2869 || (vcol >= curwin->w_leftcol
2870 && vcol < curwin->w_leftcol + curwin->w_width));
2871 if (!col_visible)
2872 return;
2873
2874 mpos = *lpos; // save the pos, update_screen() may change it
2875 save_cursor = curwin->w_cursor;
2876 save_so = *so;
2877 save_siso = *siso;
2878 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2879 // stop displaying the "$".
2880 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2881 dollar_vcol = -1;
2882 ++curwin->w_virtcol; // do display ')' just before "$"
2883 update_screen(UPD_VALID); // show the new char first
2884
2885 save_dollar_vcol = dollar_vcol;
2886#ifdef CURSOR_SHAPE
2887 save_state = State;
2888 State = MODE_SHOWMATCH;
2889 ui_cursor_shape(); // may show different cursor shape
2890#endif
2891 curwin->w_cursor = mpos; // move to matching char
2892 *so = 0; // don't use 'scrolloff' here
2893 *siso = 0; // don't use 'sidescrolloff' here
2894 showruler(FALSE);
2895 setcursor();
2896 cursor_on(); // make sure that the cursor is shown
2897 out_flush_cursor(TRUE, FALSE);
2898
2899 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2900 // which resets it if the matching position is in a previous line
2901 // and has a higher column number.
2902 dollar_vcol = save_dollar_vcol;
2903
2904 /*
2905 * brief pause, unless 'm' is present in 'cpo' and a character is
2906 * available.
2907 */
2908 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2909 ui_delay(p_mat * 100L + 8, TRUE);
2910 else if (!char_avail())
2911 ui_delay(p_mat * 100L + 9, FALSE);
2912 curwin->w_cursor = save_cursor; // restore cursor position
2913 *so = save_so;
2914 *siso = save_siso;
2915#ifdef CURSOR_SHAPE
2916 State = save_state;
2917 ui_cursor_shape(); // may show different cursor shape
2918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919}
2920
2921/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002922 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002923 * If move is TRUE, check from the beginning of the buffer, else from position
2924 * "cur".
2925 * "direction" is FORWARD or BACKWARD.
2926 * Returns TRUE, FALSE or -1 for failure.
2927 */
2928 static int
John Marriott8c85a2a2024-05-20 19:18:26 +02002929is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002930{
2931 regmmatch_T regmatch;
2932 int nmatched = 0;
2933 int result = -1;
2934 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002935 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002936 int flag = 0;
2937
2938 if (pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02002939 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002940 pattern = spats[last_idx].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02002941 patternlen = spats[last_idx].patlen;
2942 }
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002943
John Marriott8c85a2a2024-05-20 19:18:26 +02002944 if (search_regcomp(pattern, patternlen, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002945 SEARCH_KEEP, &regmatch) == FAIL)
2946 return -1;
2947
2948 // init startcol correctly
2949 regmatch.startpos[0].col = -1;
2950 // move to match
2951 if (move)
2952 {
2953 CLEAR_POS(&pos);
2954 }
2955 else
2956 {
2957 pos = *cur;
2958 // accept a match at the cursor position
2959 flag = SEARCH_START;
2960 }
2961
John Marriott8c85a2a2024-05-20 19:18:26 +02002962 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, patternlen, 1,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002963 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2964 {
2965 // Zero-width pattern should match somewhere, then we can check if
2966 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002967 do
2968 {
2969 regmatch.startpos[0].col++;
2970 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002971 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002972 if (nmatched != 0)
2973 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002974 } while (regmatch.regprog != NULL
2975 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002976 : regmatch.startpos[0].col > pos.col);
2977
Bram Moolenaar53989552019-12-23 22:59:18 +01002978 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002979 {
2980 result = (nmatched != 0
2981 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2982 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2983 }
2984 }
2985
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002986 vim_regfree(regmatch.regprog);
2987 return result;
2988}
2989
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002990
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002991/*
2992 * Find next search match under cursor, cursor at end.
2993 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002994 */
2995 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002996current_search(
2997 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002998 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002999{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003000 pos_T start_pos; // start position of the pattern match
3001 pos_T end_pos; // end position of the pattern match
3002 pos_T orig_pos; // position of the cursor at beginning
3003 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003004 int i;
3005 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003006 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003007 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003008 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02003009 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003010 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003011 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003012
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003013 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003014 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003015 dec_cursor();
3016
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003017 // When searching forward and the cursor is at the start of the Visual
3018 // area, skip the first search backward, otherwise it doesn't move.
3019 skip_first_backward = forward && VIsual_active
3020 && LT_POS(curwin->w_cursor, VIsual);
3021
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003022 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003023 if (VIsual_active)
3024 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003025 if (forward)
3026 incl(&pos);
3027 else
3028 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003029 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003030
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003031 // Is the pattern is zero-width?, this time, don't care about the direction
John Marriott8c85a2a2024-05-20 19:18:26 +02003032 zero_width = is_zero_width(spats[last_idx].pat, spats[last_idx].patlen,
3033 TRUE, &curwin->w_cursor, FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003034 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003035 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003036
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003037 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003038 * The trick is to first search backwards and then search forward again,
3039 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003040 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003041 */
3042 for (i = 0; i < 2; i++)
3043 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003044 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003045 {
3046 if (i == 0 && skip_first_backward)
3047 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003048 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003049 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003050 else
3051 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003052
3053 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003054 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003055 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003056 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003057
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003058 // wrapping should not occur in the first round
3059 if (i == 0)
3060 p_ws = FALSE;
3061
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003062 result = searchit(curwin, curbuf, &pos, &end_pos,
3063 (dir ? FORWARD : BACKWARD),
John Marriott8c85a2a2024-05-20 19:18:26 +02003064 spats[last_idx].pat, spats[last_idx].patlen, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003065 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003066
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003067 p_ws = old_p_ws;
3068
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003069 // First search may fail, but then start searching from the
3070 // beginning of the file (cursor might be on the search match)
3071 // except when Visual mode is active, so that extending the visual
3072 // selection works.
3073 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003074 {
3075 curwin->w_cursor = orig_pos;
3076 if (VIsual_active)
3077 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003078 return FAIL;
3079 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003080 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003081 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003082 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003083 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003084 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003085 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003086 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003087 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003088 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003089 // try again from end of buffer
3090 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003091 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003092 pos.col = ml_get_len(curwin->w_buffer->b_ml.ml_line_count);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003093 }
3094 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003095 }
3096
3097 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003098
3099 if (!VIsual_active)
3100 VIsual = start_pos;
3101
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003102 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003103 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003104 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003105 {
3106 if (skip_first_backward)
3107 // put the cursor on the start of the match
3108 curwin->w_cursor = pos;
3109 else
3110 // put the cursor on last character of match
3111 dec_cursor();
3112 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003113 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003114 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003115 VIsual_active = TRUE;
3116 VIsual_mode = 'v';
3117
Bram Moolenaarb7633612019-02-10 21:48:25 +01003118 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003119 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003120 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003121 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3122 inc_cursor();
3123 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3124 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003125 }
3126
3127#ifdef FEAT_FOLDING
3128 if (fdo_flags & FDO_SEARCH && KeyTyped)
3129 foldOpenCursor();
3130#endif
3131
3132 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003133 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003134#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003135 // Make sure the clipboard gets updated. Needed because start and
3136 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003137 clip_star.vmode = NUL;
3138#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003139 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003140 showmode();
3141
3142 return OK;
3143}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145/*
3146 * return TRUE if line 'lnum' is empty or has white chars only.
3147 */
3148 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003149linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150{
3151 char_u *p;
3152
3153 p = skipwhite(ml_get(lnum));
3154 return (*p == NUL);
3155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003157/*
3158 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003159 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003160 */
3161 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003162cmdline_search_stat(
3163 int dirc,
3164 pos_T *pos,
3165 pos_T *cursor_pos,
3166 int show_top_bot_msg,
3167 char_u *msgbuf,
John Marriott8c85a2a2024-05-20 19:18:26 +02003168 size_t msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003169 int recompute,
3170 int maxcount,
3171 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003172{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003173 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003174
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003175 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3176 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003177 if (stat.cur <= 0)
3178 return;
3179
3180 char t[SEARCH_STAT_BUF_LEN];
3181 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003182
3183#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003184 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3185 {
3186 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003187 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003188 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003189 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003190 maxcount, maxcount);
3191 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003192 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003193 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003194 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003195 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003196 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003197 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003198 else
3199#endif
3200 {
3201 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003202 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003203 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003204 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003205 maxcount, maxcount);
3206 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003207 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003208 stat.cur, maxcount);
3209 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003210 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003211 stat.cur, stat.cnt);
3212 }
3213
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003214 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3215 {
3216 mch_memmove(t + 2, t, len);
3217 t[0] = 'W';
3218 t[1] = ' ';
3219 len += 2;
3220 }
3221
John Marriott8c85a2a2024-05-20 19:18:26 +02003222 if (len > msgbuflen)
3223 len = msgbuflen;
3224 mch_memmove(msgbuf + msgbuflen - len, t, len);
zeertzjqa7d36b72023-01-31 21:13:38 +00003225
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003226 if (dirc == '?' && stat.cur == maxcount + 1)
3227 stat.cur = -1;
3228
3229 // keep the message even after redraw, but don't put in history
3230 msg_hist_off = TRUE;
3231 give_warning(msgbuf, FALSE);
3232 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003233}
3234
3235/*
3236 * Add the search count information to "stat".
3237 * "stat" must not be NULL.
3238 * When "recompute" is TRUE always recompute the numbers.
3239 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3240 * dirc == '/': find the next match
3241 * dirc == '?': find the previous match
3242 */
3243 static void
3244update_search_stat(
3245 int dirc,
3246 pos_T *pos,
3247 pos_T *cursor_pos,
3248 searchstat_T *stat,
3249 int recompute,
3250 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003251 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003252{
3253 int save_ws = p_ws;
3254 int wraparound = FALSE;
3255 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003256 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003257 static int cur = 0;
3258 static int cnt = 0;
3259 static int exact_match = FALSE;
3260 static int incomplete = 0;
3261 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3262 static int chgtick = 0;
3263 static char_u *lastpat = NULL;
3264 static buf_T *lbuf = NULL;
3265#ifdef FEAT_RELTIME
3266 proftime_T start;
3267#endif
3268
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003269 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003270
3271 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3272 {
3273 stat->cur = cur;
3274 stat->cnt = cnt;
3275 stat->exact_match = exact_match;
3276 stat->incomplete = incomplete;
3277 stat->last_maxcount = last_maxcount;
3278 return;
3279 }
3280 last_maxcount = maxcount;
3281
3282 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3283 || (dirc == '/' && LT_POS(p, lastpos)));
3284
3285 // If anything relevant changed the count has to be recomputed.
3286 // MB_STRNICMP ignores case, but we should not ignore case.
3287 // Unfortunately, there is no MB_STRNICMP function.
3288 // XXX: above comment should be "no MB_STRCMP function" ?
3289 if (!(chgtick == CHANGEDTICK(curbuf)
3290 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3291 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3292 && EQUAL_POS(lastpos, *cursor_pos)
3293 && lbuf == curbuf) || wraparound || cur < 0
3294 || (maxcount > 0 && cur > maxcount) || recompute)
3295 {
3296 cur = 0;
3297 cnt = 0;
3298 exact_match = FALSE;
3299 incomplete = 0;
3300 CLEAR_POS(&lastpos);
3301 lbuf = curbuf;
3302 }
3303
Christian Brabandt34a6a362023-05-06 19:20:20 +01003304 // when searching backwards and having jumped to the first occurrence,
3305 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003306 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003307 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003308 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3309 else
3310 {
3311 int done_search = FALSE;
3312 pos_T endpos = {0, 0, 0};
3313
3314 p_ws = FALSE;
3315#ifdef FEAT_RELTIME
3316 if (timeout > 0)
3317 profile_setlimit(timeout, &start);
3318#endif
3319 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003320 FORWARD, NULL, 0, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003321 {
3322 done_search = TRUE;
3323#ifdef FEAT_RELTIME
3324 // Stop after passing the time limit.
3325 if (timeout > 0 && profile_passed_limit(&start))
3326 {
3327 incomplete = 1;
3328 break;
3329 }
3330#endif
3331 cnt++;
3332 if (LTOREQ_POS(lastpos, p))
3333 {
3334 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003335 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003336 exact_match = TRUE;
3337 }
3338 fast_breakcheck();
3339 if (maxcount > 0 && cnt > maxcount)
3340 {
3341 incomplete = 2; // max count exceeded
3342 break;
3343 }
3344 }
3345 if (got_int)
3346 cur = -1; // abort
3347 if (done_search)
3348 {
3349 vim_free(lastpat);
3350 lastpat = vim_strsave(spats[last_idx].pat);
3351 chgtick = CHANGEDTICK(curbuf);
3352 lbuf = curbuf;
3353 lastpos = p;
3354 }
3355 }
3356 stat->cur = cur;
3357 stat->cnt = cnt;
3358 stat->exact_match = exact_match;
3359 stat->incomplete = incomplete;
3360 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003361 p_ws = save_ws;
3362}
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003365
3366/*
3367 * Get line "lnum" and copy it into "buf[LSIZE]".
3368 * The copy is made because the regexp may make the line invalid when using a
3369 * mark.
3370 */
3371 static char_u *
3372get_line_and_copy(linenr_T lnum, char_u *buf)
3373{
3374 char_u *line = ml_get(lnum);
3375
3376 vim_strncpy(buf, line, LSIZE - 1);
3377 return buf;
3378}
3379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380/*
3381 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003382 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003385find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003386 char_u *ptr, // pointer to search pattern
3387 int dir UNUSED, // direction of expansion
3388 int len, // length of search pattern
3389 int whole, // match whole words only
3390 int skip_comments, // don't match inside comments
3391 int type, // Type of search; are we looking for a type?
3392 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003393 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003394 int action, // What to do when we find it
3395 linenr_T start_lnum, // first line to start searching
Colin Kennedy21570352024-03-03 16:16:47 +01003396 linenr_T end_lnum, // last line for searching
3397 int forceit) // If true, always switch to the found path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003399 SearchedFile *files; // Stack of included files
3400 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 int max_path_depth = 50;
3402 long match_count = 1;
3403
3404 char_u *pat;
3405 char_u *new_fname;
3406 char_u *curr_fname = curbuf->b_fname;
3407 char_u *prev_fname = NULL;
3408 linenr_T lnum;
3409 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003410 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 int old_files;
3412 int already_searched;
3413 char_u *file_line;
3414 char_u *line;
3415 char_u *p;
3416 char_u save_char;
3417 int define_matched;
3418 regmatch_T regmatch;
3419 regmatch_T incl_regmatch;
3420 regmatch_T def_regmatch;
3421 int matched = FALSE;
3422 int did_show = FALSE;
3423 int found = FALSE;
3424 int i;
3425 char_u *already = NULL;
3426 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003427 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003428#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 win_T *curwin_save = NULL;
3430#endif
3431
3432 regmatch.regprog = NULL;
3433 incl_regmatch.regprog = NULL;
3434 def_regmatch.regprog = NULL;
3435
3436 file_line = alloc(LSIZE);
3437 if (file_line == NULL)
3438 return;
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003441 // when CONT_SOL is set compare "ptr" with the beginning of the
3442 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3443 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 {
3445 pat = alloc(len + 5);
3446 if (pat == NULL)
3447 goto fpip_end;
John Marriott8c85a2a2024-05-20 19:18:26 +02003448 vim_snprintf((char *)pat, len + 5, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003449 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003451 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 vim_free(pat);
3453 if (regmatch.regprog == NULL)
3454 goto fpip_end;
3455 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003456 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3457 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003459 incl_regmatch.regprog = vim_regcomp(inc_opt,
3460 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (incl_regmatch.regprog == NULL)
3462 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003463 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3466 {
John Marriott8c85a2a2024-05-20 19:18:26 +02003467 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL ? p_def : curbuf->b_p_def,
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003468 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (def_regmatch.regprog == NULL)
3470 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003471 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003473 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 if (files == NULL)
3475 goto fpip_end;
3476 old_files = max_path_depth;
3477 depth = depth_displayed = -1;
3478
3479 lnum = start_lnum;
3480 if (end_lnum > curbuf->b_ml.ml_line_count)
3481 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003482 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003484 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 for (;;)
3487 {
3488 if (incl_regmatch.regprog != NULL
3489 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3490 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003491 char_u *p_fname = (curr_fname == curbuf->b_fname)
3492 ? curbuf->b_ffname : curr_fname;
3493
3494 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003495 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003496 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003497 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003498 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3499 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003500 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003501 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003502 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 already_searched = FALSE;
3504 if (new_fname != NULL)
3505 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003506 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 for (i = 0;; i++)
3508 {
3509 if (i == depth + 1)
3510 i = old_files;
3511 if (i == max_path_depth)
3512 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003513 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3514 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003516 if (type != CHECK_PATH
3517 && action == ACTION_SHOW_ALL
3518 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003520 msg_putchar('\n'); // cursor below last one
3521 if (!got_int) // don't display if 'q'
3522 // typed at "--more--"
3523 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003526 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 prev_fname = NULL;
3528 }
3529 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003530 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 already_searched = TRUE;
3532 break;
3533 }
3534 }
3535 }
3536
3537 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3538 || (new_fname == NULL && !already_searched)))
3539 {
3540 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003541 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 else
3543 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003544 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003545 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003547 msg_puts_title(_("not found "));
3548 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 did_show = TRUE;
3551 while (depth_displayed < depth && !got_int)
3552 {
3553 ++depth_displayed;
3554 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003555 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003557 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003559 if (!got_int) // don't display if 'q' typed
3560 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003563 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (new_fname != NULL)
3565 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003566 // using "new_fname" is more reliable, e.g., when
3567 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003568 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570 else
3571 {
3572 /*
3573 * Isolate the file name.
3574 * Include the surrounding "" or <> if present.
3575 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003576 if (inc_opt != NULL
3577 && strstr((char *)inc_opt, "\\zs") != NULL)
3578 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003579 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003580 p = incl_regmatch.startp[0];
3581 i = (int)(incl_regmatch.endp[0]
3582 - incl_regmatch.startp[0]);
3583 }
3584 else
3585 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003586 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003587 for (p = incl_regmatch.endp[0];
3588 *p && !vim_isfilec(*p); p++)
3589 ;
3590 for (i = 0; vim_isfilec(p[i]); i++)
3591 ;
3592 }
3593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 if (i == 0)
3595 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003596 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003598 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003600 // Avoid checking before the start of the line, can
3601 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003602 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
3604 if (p[-1] == '"' || p[-1] == '<')
3605 {
3606 --p;
3607 ++i;
3608 }
3609 if (p[i] == '"' || p[i] == '>')
3610 ++i;
3611 }
3612 save_char = p[i];
3613 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003614 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 p[i] = save_char;
3616 }
3617
3618 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3619 {
3620 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003621 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003623 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 }
3625 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003626 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628
3629 if (new_fname != NULL)
3630 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003631 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 if (depth + 1 == old_files)
3633 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003634 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (bigger != NULL)
3636 {
3637 for (i = 0; i <= depth; i++)
3638 bigger[i] = files[i];
3639 for (i = depth + 1; i < old_files + max_path_depth; i++)
3640 {
3641 bigger[i].fp = NULL;
3642 bigger[i].name = NULL;
3643 bigger[i].lnum = 0;
3644 bigger[i].matched = FALSE;
3645 }
3646 for (i = old_files; i < max_path_depth; i++)
3647 bigger[i + max_path_depth] = files[i];
3648 old_files += max_path_depth;
3649 max_path_depth *= 2;
3650 vim_free(files);
3651 files = bigger;
3652 }
3653 }
3654 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3655 == NULL)
3656 vim_free(new_fname);
3657 else
3658 {
3659 if (++depth == old_files)
3660 {
3661 /*
3662 * lalloc() for 'bigger' must have failed above. We
3663 * will forget one of our already visited files now.
3664 */
3665 vim_free(files[old_files].name);
3666 ++old_files;
3667 }
3668 files[depth].name = curr_fname = new_fname;
3669 files[depth].lnum = 0;
3670 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (action == ACTION_EXPAND)
3672 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003673 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003674 vim_snprintf((char*)IObuff, IOSIZE,
3675 _("Scanning included file: %s"),
3676 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003677 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003679 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003680 {
3681 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003682 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003683 (char *)new_fname);
3684 verbose_leave();
3685 }
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 }
3688 }
3689 }
3690 else
3691 {
3692 /*
3693 * Check if the line is a define (type == FIND_DEFINE)
3694 */
3695 p = line;
3696search_line:
3697 define_matched = FALSE;
3698 if (def_regmatch.regprog != NULL
3699 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3700 {
3701 /*
3702 * Pattern must be first identifier after 'define', so skip
3703 * to that position before checking for match of pattern. Also
3704 * don't let it match beyond the end of this identifier.
3705 */
3706 p = def_regmatch.endp[0];
3707 while (*p && !vim_iswordc(*p))
3708 p++;
3709 define_matched = TRUE;
3710 }
3711
3712 /*
3713 * Look for a match. Don't do this if we are looking for a
3714 * define and this line didn't match define_prog above.
3715 */
3716 if (def_regmatch.regprog == NULL || define_matched)
3717 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003718 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003720 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 startp = skipwhite(p);
3722 if (p_ic)
3723 matched = !MB_STRNICMP(startp, ptr, len);
3724 else
3725 matched = !STRNCMP(startp, ptr, len);
3726 if (matched && define_matched && whole
3727 && vim_iswordc(startp[len]))
3728 matched = FALSE;
3729 }
3730 else if (regmatch.regprog != NULL
3731 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3732 {
3733 matched = TRUE;
3734 startp = regmatch.startp[0];
3735 /*
3736 * Check if the line is not a comment line (unless we are
3737 * looking for a define). A line starting with "# define"
3738 * is not considered to be a comment line.
3739 */
3740 if (!define_matched && skip_comments)
3741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if ((*line != '#' ||
3743 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003744 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 matched = FALSE;
3746
3747 /*
3748 * Also check for a "/ *" or "/ /" before the match.
3749 * Skips lines like "int backwards; / * normal index
3750 * * /" when looking for "normal".
3751 * Note: Doesn't skip "/ *" in comments.
3752 */
3753 p = skipwhite(line);
3754 if (matched
3755 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 for (p = line; *p && p < startp; ++p)
3757 {
3758 if (matched
3759 && p[0] == '/'
3760 && (p[1] == '*' || p[1] == '/'))
3761 {
3762 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003763 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 if (p[1] == '/')
3765 break;
3766 ++p;
3767 }
3768 else if (!matched && p[0] == '*' && p[1] == '/')
3769 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003770 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 matched = TRUE;
3772 ++p;
3773 }
3774 }
3775 }
3776 }
3777 }
3778 }
3779 if (matched)
3780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (action == ACTION_EXPAND)
3782 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003783 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 int add_r;
3785 char_u *aux;
3786
3787 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3788 break;
3789 found = TRUE;
3790 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003791 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003793 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 if (vim_iswordp(p))
3795 goto exit_matched;
3796 p = find_word_start(p);
3797 }
3798 p = find_word_end(p);
3799 i = (int)(p - aux);
3800
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003801 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003803 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003805
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003806 // Get the next line: when "depth" < 0 from the current
3807 // buffer, otherwise from the included file. Jump to
3808 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003809 if (depth < 0)
3810 {
3811 if (lnum >= end_lnum)
3812 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003813 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003814 }
3815 else if (vim_fgets(line = file_line,
3816 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 goto exit_matched;
3818
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003819 // we read a line, set "already" to check this "line" later
3820 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003821 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 already = aux = p = skipwhite(line);
3823 p = find_word_start(p);
3824 p = find_word_end(p);
3825 if (p > aux)
3826 {
3827 if (*aux != ')' && IObuff[i-1] != TAB)
3828 {
3829 if (IObuff[i-1] != ' ')
3830 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003831 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (p_js
3833 && (IObuff[i-2] == '.'
3834 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3835 && (IObuff[i-2] == '?'
3836 || IObuff[i-2] == '!'))))
3837 IObuff[i++] = ' ';
3838 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003839 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (p - aux >= IOSIZE - i)
3841 p = aux + IOSIZE - i - 1;
3842 STRNCPY(IObuff + i, aux, p - aux);
3843 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003844 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 }
3846 IObuff[i] = NUL;
3847 aux = IObuff;
3848
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003849 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 goto exit_matched;
3851 }
3852
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003853 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003855 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003857 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003859 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 break;
3861 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003862 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
3864 found = TRUE;
3865 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003866 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (curr_fname != prev_fname)
3868 {
3869 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003870 msg_putchar('\n'); // cursor below last one
3871 if (!got_int) // don't display if 'q' typed
3872 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 msg_home_replace_hl(curr_fname);
3874 prev_fname = curr_fname;
3875 }
3876 did_show = TRUE;
3877 if (!got_int)
3878 show_pat_in_path(line, type, TRUE, action,
3879 (depth == -1) ? NULL : files[depth].fp,
3880 (depth == -1) ? &lnum : &files[depth].lnum,
3881 match_count++);
3882
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003883 // Set matched flag for this file and all the ones that
3884 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 for (i = 0; i <= depth; ++i)
3886 files[i].matched = TRUE;
3887 }
3888 else if (--count <= 0)
3889 {
3890 found = TRUE;
3891 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003892#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 && g_do_tagpreview == 0
3894#endif
3895 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003896 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 else if (action == ACTION_SHOW)
3898 {
3899 show_pat_in_path(line, type, did_show, action,
3900 (depth == -1) ? NULL : files[depth].fp,
3901 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3902 did_show = TRUE;
3903 }
3904 else
3905 {
3906#ifdef FEAT_GUI
3907 need_mouse_correct = TRUE;
3908#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003909#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003910 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (g_do_tagpreview != 0)
3912 {
3913 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003914 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 }
3916#endif
3917 if (action == ACTION_SPLIT)
3918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003921 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 }
3923 if (depth == -1)
3924 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003925 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003926#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if (g_do_tagpreview != 0)
3928 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003929 if (!win_valid(curwin_save))
3930 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003931 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003932 curwin_save->w_buffer->b_fnum, NULL,
Colin Kennedy21570352024-03-03 16:16:47 +01003933 NULL, TRUE, lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003934 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 }
3936 else
3937#endif
3938 setpcmark();
3939 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003940 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 }
3942 else
3943 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003944 if (!GETFILE_SUCCESS(getfile(
3945 0, files[depth].name, NULL, TRUE,
Colin Kennedy21570352024-03-03 16:16:47 +01003946 files[depth].lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003947 break; // failed to jump to file
3948 // autocommands may have changed the lnum, we don't
3949 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 curwin->w_cursor.lnum = files[depth].lnum;
3951 }
3952 }
3953 if (action != ACTION_SHOW)
3954 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003955 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 curwin->w_set_curswant = TRUE;
3957 }
3958
Bram Moolenaar4033c552017-09-16 20:54:51 +02003959#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003961 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003963 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003965 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 win_enter(curwin_save, TRUE);
3967 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003968# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003969 else if (WIN_IS_POPUP(curwin))
3970 // can't keep focus in popup window
3971 win_enter(firstwin, TRUE);
3972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973#endif
3974 break;
3975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003978 // look for other matches in the rest of the line if we
3979 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003982 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003983 && *startp != NUL
John Marriott8c85a2a2024-05-20 19:18:26 +02003984 && *(startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 goto search_line;
3986 }
3987 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003989 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003990 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 break;
3992
3993 /*
3994 * Read the next line. When reading an included file and encountering
3995 * end-of-file, close the file and continue in the file that included
3996 * it.
3997 */
3998 while (depth >= 0 && !already
3999 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4000 {
4001 fclose(files[depth].fp);
4002 --old_files;
4003 files[old_files].name = files[depth].name;
4004 files[old_files].matched = files[depth].matched;
4005 --depth;
4006 curr_fname = (depth == -1) ? curbuf->b_fname
4007 : files[depth].name;
4008 if (depth < depth_displayed)
4009 depth_displayed = depth;
4010 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004011 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004014 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004015 i = (int)STRLEN(line);
4016 if (i > 0 && line[i - 1] == '\n')
4017 line[--i] = NUL;
4018 if (i > 0 && line[i - 1] == '\r')
4019 line[--i] = NUL;
4020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 else if (!already)
4022 {
4023 if (++lnum > end_lnum)
4024 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01004025 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027 already = NULL;
4028 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004029 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004031 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 for (i = 0; i <= depth; i++)
4033 {
4034 fclose(files[i].fp);
4035 vim_free(files[i].name);
4036 }
4037 for (i = old_files; i < max_path_depth; i++)
4038 vim_free(files[i].name);
4039 vim_free(files);
4040
4041 if (type == CHECK_PATH)
4042 {
4043 if (!did_show)
4044 {
4045 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004046 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004048 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004051 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004053 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004054 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004056 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004058 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4061 msg_end();
4062
4063fpip_end:
4064 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02004065 vim_regfree(regmatch.regprog);
4066 vim_regfree(incl_regmatch.regprog);
4067 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068}
4069
4070 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004071show_pat_in_path(
4072 char_u *line,
4073 int type,
4074 int did_show,
4075 int action,
4076 FILE *fp,
4077 linenr_T *lnum,
4078 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079{
4080 char_u *p;
John Marriott8c85a2a2024-05-20 19:18:26 +02004081 size_t linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004084 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004085 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004086 gotocmdline(TRUE); // cursor at status line
4087 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return;
John Marriott8c85a2a2024-05-20 19:18:26 +02004089 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 for (;;)
4091 {
John Marriott8c85a2a2024-05-20 19:18:26 +02004092 p = line + linelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (fp != NULL)
4094 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004095 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (p >= line && *p == '\n')
4097 --p;
4098 if (p >= line && *p == '\r')
4099 --p;
4100 *(p + 1) = NUL;
4101 }
4102 if (action == ACTION_SHOW_ALL)
4103 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004104 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004105 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004106 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4107 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004108 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4109 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004111 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004112 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004114 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4116 break;
4117
4118 if (fp != NULL)
4119 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004120 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 break;
John Marriott8c85a2a2024-05-20 19:18:26 +02004122 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 ++*lnum;
4124 }
4125 else
4126 {
4127 if (++*lnum > curbuf->b_ml.ml_line_count)
4128 break;
4129 line = ml_get(*lnum);
John Marriott8c85a2a2024-05-20 19:18:26 +02004130 linelen = ml_get_len(*lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132 msg_putchar('\n');
4133 }
4134}
4135#endif
4136
4137#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004138/*
4139 * Return the last used search pattern at "idx".
4140 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004141 spat_T *
4142get_spat(int idx)
4143{
4144 return &spats[idx];
4145}
4146
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004147/*
4148 * Return the last used search pattern index.
4149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004151get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004153 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004156
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004157#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004158/*
4159 * "searchcount()" function
4160 */
4161 void
4162f_searchcount(typval_T *argvars, typval_T *rettv)
4163{
4164 pos_T pos = curwin->w_cursor;
4165 char_u *pattern = NULL;
4166 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4167 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004168 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004169 searchstat_T stat;
4170
4171 if (rettv_dict_alloc(rettv) == FAIL)
4172 return;
4173
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004174 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4175 return;
4176
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004177 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4178 recompute = TRUE;
4179
4180 if (argvars[0].v_type != VAR_UNKNOWN)
4181 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004182 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004183 dictitem_T *di;
4184 listitem_T *li;
4185 int error = FALSE;
4186
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004187 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004188 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004189 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004190 di = dict_find(dict, (char_u *)"timeout", -1);
4191 if (di != NULL)
4192 {
4193 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4194 if (error)
4195 return;
4196 }
4197 di = dict_find(dict, (char_u *)"maxcount", -1);
4198 if (di != NULL)
4199 {
4200 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4201 if (error)
4202 return;
4203 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004204 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004205 di = dict_find(dict, (char_u *)"pattern", -1);
4206 if (di != NULL)
4207 {
4208 pattern = tv_get_string_chk(&di->di_tv);
4209 if (pattern == NULL)
4210 return;
4211 }
4212 di = dict_find(dict, (char_u *)"pos", -1);
4213 if (di != NULL)
4214 {
4215 if (di->di_tv.v_type != VAR_LIST)
4216 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004217 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004218 return;
4219 }
4220 if (list_len(di->di_tv.vval.v_list) != 3)
4221 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004222 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004223 return;
4224 }
4225 li = list_find(di->di_tv.vval.v_list, 0L);
4226 if (li != NULL)
4227 {
4228 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4229 if (error)
4230 return;
4231 }
4232 li = list_find(di->di_tv.vval.v_list, 1L);
4233 if (li != NULL)
4234 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004235 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004236 if (error)
4237 return;
4238 }
4239 li = list_find(di->di_tv.vval.v_list, 2L);
4240 if (li != NULL)
4241 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004242 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004243 if (error)
4244 return;
4245 }
4246 }
4247 }
4248
4249 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004250#ifdef FEAT_SEARCH_EXTRA
4251 save_incsearch_state();
4252#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004253 if (pattern != NULL)
4254 {
4255 if (*pattern == NUL)
4256 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004257 vim_free(spats[last_idx].pat);
John Marriott8c85a2a2024-05-20 19:18:26 +02004258 spats[last_idx].patlen = STRLEN(pattern);
4259 spats[last_idx].pat = vim_strnsave(pattern, spats[last_idx].patlen);
4260 if (spats[last_idx].pat == NULL)
4261 spats[last_idx].patlen = 0;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004262 }
4263 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4264 goto the_end; // the previous pattern was never defined
4265
4266 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4267
4268 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4269 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4270 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4271 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4272 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4273
4274the_end:
4275 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004276#ifdef FEAT_SEARCH_EXTRA
4277 restore_incsearch_state();
4278#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004279}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004280#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004281
4282/*
4283 * Fuzzy string matching
4284 *
4285 * Ported from the lib_fts library authored by Forrest Smith.
4286 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4287 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004288 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004289 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4290 *
4291 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004292 * - Matched letter
4293 * - Unmatched letter
4294 * - Consecutively matched letters
4295 * - Proximity to start
4296 * - Letter following a separator (space, underscore)
4297 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004298 *
4299 * Matched letters are good. Unmatched letters are bad. Matching near the start
4300 * is good. Matching the first letter in the middle of a phrase is good.
4301 * Matching the uppercase letters in camel case entries is good.
4302 *
4303 * The score assigned for each factor is explained below.
4304 * File paths are different from file names. File extensions may be ignorable.
4305 * Single words care about consecutive matches but not separators or camel
4306 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004307 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004308 * Matched letter: +0 points
4309 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004310 * Consecutive match bonus: +15 points
4311 * First letter bonus: +15 points
4312 * Separator bonus: +30 points
4313 * Camel case bonus: +30 points
4314 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004315 *
4316 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004317 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004318 * lower minimum score due to unmatched letter penalty. Longer search patterns
4319 * have a higher maximum score due to match bonuses.
4320 *
4321 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4322 * quite a bit.
4323 *
4324 * There is a penalty if you DON’T match the first three letters. Which
4325 * effectively rewards matching near the start. However there’s no difference
4326 * in matching between the middle and end.
4327 *
4328 * There is not an explicit bonus for an exact match. Unmatched letters receive
4329 * a penalty. So shorter strings and closer matches are worth more.
4330 */
4331typedef struct
4332{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004333 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004334 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004335 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004336 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004337} fuzzyItem_T;
4338
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004339// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4340// matching a whole word is preferred.
4341#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004342// bonus if match occurs after a path separator
4343#define PATH_SEPARATOR_BONUS 30
4344// bonus if match occurs after a word separator
4345#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004346// bonus if match is uppercase and prev is lower
4347#define CAMEL_BONUS 30
4348// bonus if the first letter is matched
4349#define FIRST_LETTER_BONUS 15
4350// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004351#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004352// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004353#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004354// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004355#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004356// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004357#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004358// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004359#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004360
4361#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004362
4363/*
4364 * Compute a score for a fuzzy matched string. The matching character locations
4365 * are in 'matches'.
4366 */
4367 static int
4368fuzzy_match_compute_score(
4369 char_u *str,
4370 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004371 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004372 int numMatches)
4373{
4374 int score;
4375 int penalty;
4376 int unmatched;
4377 int i;
4378 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004379 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004380
4381 // Initialize score
4382 score = 100;
4383
4384 // Apply leading letter penalty
4385 penalty = LEADING_LETTER_PENALTY * matches[0];
4386 if (penalty < MAX_LEADING_LETTER_PENALTY)
4387 penalty = MAX_LEADING_LETTER_PENALTY;
4388 score += penalty;
4389
4390 // Apply unmatched penalty
4391 unmatched = strSz - numMatches;
4392 score += UNMATCHED_LETTER_PENALTY * unmatched;
4393
4394 // Apply ordering bonuses
4395 for (i = 0; i < numMatches; ++i)
4396 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004397 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004398
4399 if (i > 0)
4400 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004401 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004402
4403 // Sequential
4404 if (currIdx == (prevIdx + 1))
4405 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004406 else
4407 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004408 }
4409
4410 // Check for bonuses based on neighbor character value
4411 if (currIdx > 0)
4412 {
4413 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004414 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004415 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004416
4417 if (has_mbyte)
4418 {
4419 while (sidx < currIdx)
4420 {
4421 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004422 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004423 sidx++;
4424 }
4425 curr = (*mb_ptr2char)(p);
4426 }
4427 else
4428 {
4429 neighbor = str[currIdx - 1];
4430 curr = str[currIdx];
4431 }
4432
4433 if (vim_islower(neighbor) && vim_isupper(curr))
4434 score += CAMEL_BONUS;
4435
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004436 // Bonus if the match follows a separator character
4437 if (neighbor == '/' || neighbor == '\\')
4438 score += PATH_SEPARATOR_BONUS;
4439 else if (neighbor == ' ' || neighbor == '_')
4440 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004441 }
4442 else
4443 {
4444 // First letter
4445 score += FIRST_LETTER_BONUS;
4446 }
4447 }
4448 return score;
4449}
4450
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004451/*
4452 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4453 * Return the number of matching characters.
4454 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004455 static int
4456fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004457 char_u *fuzpat,
4458 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004459 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004460 int *outScore,
4461 char_u *strBegin,
4462 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004463 int_u *srcMatches,
4464 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004465 int maxMatches,
4466 int nextMatch,
4467 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004468{
4469 // Recursion params
4470 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004471 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004472 int bestRecursiveScore = 0;
4473 int first_match;
4474 int matched;
4475
4476 // Count recursions
4477 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004478 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004479 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004480
4481 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004482 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004483 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004484
4485 // Loop through fuzpat and str looking for a match
4486 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004487 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004488 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004489 int c1;
4490 int c2;
4491
4492 c1 = PTR2CHAR(fuzpat);
4493 c2 = PTR2CHAR(str);
4494
Bram Moolenaar635414d2020-09-11 22:25:15 +02004495 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004496 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004497 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004498 // Supplied matches buffer was too short
4499 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004500 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004501
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004502 int recursiveScore = 0;
4503 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4504 CLEAR_FIELD(recursiveMatches);
4505
Bram Moolenaar635414d2020-09-11 22:25:15 +02004506 // "Copy-on-Write" srcMatches into matches
4507 if (first_match && srcMatches)
4508 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004509 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004510 first_match = FALSE;
4511 }
4512
4513 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004514 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004515 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4516 &recursiveScore, strBegin, strLen, matches,
4517 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004518 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004519 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004520 {
4521 // Pick best recursive score
4522 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4523 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004524 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004525 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004526 bestRecursiveScore = recursiveScore;
4527 }
4528 recursiveMatch = TRUE;
4529 }
4530
4531 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004532 matches[nextMatch++] = strIdx;
4533 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004534 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004535 else
4536 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004537 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004538 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004539 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004540 else
4541 ++str;
4542 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004543 }
4544
4545 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004546 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004547
4548 // Calculate score
4549 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004550 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4551 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004552
4553 // Return best result
4554 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4555 {
4556 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004557 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004558 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004559 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004560 }
4561 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004562 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004563
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004564 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004565}
4566
4567/*
4568 * fuzzy_match()
4569 *
4570 * Performs exhaustive search via recursion to find all possible matches and
4571 * match with highest score.
4572 * Scores values have no intrinsic meaning. Possible score range is not
4573 * normalized and varies with pattern.
4574 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004575 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004576 * Uses char_u for match indices. Therefore patterns are limited to
4577 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004578 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004579 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4580 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004581 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004582 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004583fuzzy_match(
4584 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004585 char_u *pat_arg,
4586 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004587 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004588 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004589 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004590{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004591 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004592 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004593 char_u *save_pat;
4594 char_u *pat;
4595 char_u *p;
4596 int complete = FALSE;
4597 int score = 0;
4598 int numMatches = 0;
4599 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004600
4601 *outScore = 0;
4602
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004603 save_pat = vim_strsave(pat_arg);
4604 if (save_pat == NULL)
4605 return FALSE;
4606 pat = save_pat;
4607 p = pat;
4608
4609 // Try matching each word in 'pat_arg' in 'str'
4610 while (TRUE)
4611 {
4612 if (matchseq)
4613 complete = TRUE;
4614 else
4615 {
4616 // Extract one word from the pattern (separated by space)
4617 p = skipwhite(p);
4618 if (*p == NUL)
4619 break;
4620 pat = p;
4621 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4622 {
4623 if (has_mbyte)
4624 MB_PTR_ADV(p);
4625 else
4626 ++p;
4627 }
4628 if (*p == NUL) // processed all the words
4629 complete = TRUE;
4630 *p = NUL;
4631 }
4632
4633 score = 0;
4634 recursionCount = 0;
4635 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4636 matches + numMatches, maxMatches - numMatches,
4637 0, &recursionCount);
4638 if (matchCount == 0)
4639 {
4640 numMatches = 0;
4641 break;
4642 }
4643
4644 // Accumulate the match score and the number of matches
4645 *outScore += score;
4646 numMatches += matchCount;
4647
4648 if (complete)
4649 break;
4650
4651 // try matching the next word
4652 ++p;
4653 }
4654
4655 vim_free(save_pat);
4656 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004657}
4658
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004659#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004660/*
4661 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004662 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004663 */
4664 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004665fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004666{
4667 int v1 = ((fuzzyItem_T *)s1)->score;
4668 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004669 int idx1 = ((fuzzyItem_T *)s1)->idx;
4670 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004671
zeertzjq77078272024-02-10 13:24:03 +01004672 if (v1 == v2)
4673 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4674 else
4675 return v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004676}
4677
4678/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004679 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4680 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004681 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4682 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004683 * If 'items' is a list of strings, then search for 'str' in the list.
4684 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4685 * for each item or use 'item_cb' Funcref function to get the string.
4686 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4687 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004688 */
4689 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004690fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004691 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004692 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004693 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004694 char_u *key,
4695 callback_T *item_cb,
4696 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004697 list_T *fmatchlist,
4698 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004699{
4700 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004701 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004702 listitem_T *li;
4703 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004704 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004705 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004706
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004707 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004708 if (len == 0)
4709 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004710 if (max_matches > 0 && len > max_matches)
4711 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004712
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004713 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4714 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004715 return;
4716
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004717 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004718 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004719 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004720 int score;
4721 char_u *itemstr;
4722 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004723
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004724 if (max_matches > 0 && match_count >= max_matches)
4725 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004726
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004727 itemstr = NULL;
4728 rettv.v_type = VAR_UNKNOWN;
4729 if (li->li_tv.v_type == VAR_STRING) // list of strings
4730 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004731 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004732 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004733 {
4734 // For a dict, either use the specified key to lookup the string or
4735 // use the specified callback function to get the string.
4736 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004737 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4738 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004739 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004740 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004741 typval_T argv[2];
4742
4743 // Invoke the supplied callback (if any) to get the dict item
4744 li->li_tv.vval.v_dict->dv_refcount++;
4745 argv[0].v_type = VAR_DICT;
4746 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4747 argv[1].v_type = VAR_UNKNOWN;
4748 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4749 {
4750 if (rettv.v_type == VAR_STRING)
4751 itemstr = rettv.vval.v_string;
4752 }
4753 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004754 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004755 }
4756
4757 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004758 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004759 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004760 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004761 items[match_count].idx = match_count;
4762 items[match_count].item = li;
4763 items[match_count].score = score;
4764
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004765 // Copy the list of matching positions in itemstr to a list, if
4766 // 'retmatchpos' is set.
4767 if (retmatchpos)
4768 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004769 int j = 0;
4770 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004771
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004772 items[match_count].lmatchpos = list_alloc();
4773 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004774 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004775
4776 p = str;
4777 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004778 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004779 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004780 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004781 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004782 matches[j]) == FAIL)
4783 goto done;
4784 j++;
4785 }
4786 if (has_mbyte)
4787 MB_PTR_ADV(p);
4788 else
4789 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004790 }
4791 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004792 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004793 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004794 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004795 }
4796
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004797 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004798 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004799 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004800
Bram Moolenaar635414d2020-09-11 22:25:15 +02004801 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004802 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004803 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004804
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004805 // For matchfuzzy(), return a list of matched strings.
4806 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004807 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004808 // The first item is a list of matched strings. The second item
4809 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004810 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004811 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4812 if (retmatchpos)
4813 {
4814 li = list_find(fmatchlist, 0);
4815 if (li == NULL || li->li_tv.vval.v_list == NULL)
4816 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004817 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004818 }
4819 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004820 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004821
4822 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004823 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004824 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004825 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004826 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004827 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004828 }
4829
4830 // next copy the list of matching positions
4831 if (retmatchpos)
4832 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004833 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004834 if (li == NULL || li->li_tv.vval.v_list == NULL)
4835 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004836 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004837
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004838 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004839 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004840 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004841 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004842 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004843 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004844 goto done;
4845 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004846
4847 // copy the matching scores
4848 li = list_find(fmatchlist, -1);
4849 if (li == NULL || li->li_tv.vval.v_list == NULL)
4850 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004851 retlist = li->li_tv.vval.v_list;
4852 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004853 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004854 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004855 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004856 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004857 goto done;
4858 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004859 }
4860 }
4861
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004862done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004863 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004864}
4865
4866/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004867 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4868 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4869 */
4870 static void
4871do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4872{
4873 callback_T cb;
4874 char_u *key = NULL;
4875 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004876 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004877 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004878
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004879 if (in_vim9script()
4880 && (check_for_list_arg(argvars, 0) == FAIL
4881 || check_for_string_arg(argvars, 1) == FAIL
4882 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4883 return;
4884
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004885 CLEAR_POINTER(&cb);
4886
4887 // validate and get the arguments
4888 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4889 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004890 semsg(_(e_argument_of_str_must_be_list),
4891 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004892 return;
4893 }
4894 if (argvars[1].v_type != VAR_STRING
4895 || argvars[1].vval.v_string == NULL)
4896 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004897 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004898 return;
4899 }
4900
4901 if (argvars[2].v_type != VAR_UNKNOWN)
4902 {
4903 dict_T *d;
4904 dictitem_T *di;
4905
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004906 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004907 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004908
4909 // To search a dict, either a callback function or a key can be
4910 // specified.
4911 d = argvars[2].vval.v_dict;
4912 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4913 {
4914 if (di->di_tv.v_type != VAR_STRING
4915 || di->di_tv.vval.v_string == NULL
4916 || *di->di_tv.vval.v_string == NUL)
4917 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004918 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004919 return;
4920 }
4921 key = tv_get_string(&di->di_tv);
4922 }
4923 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4924 {
4925 cb = get_callback(&di->di_tv);
4926 if (cb.cb_name == NULL)
4927 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004928 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004929 return;
4930 }
4931 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004932
4933 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004934 {
4935 if (di->di_tv.v_type != VAR_NUMBER)
4936 {
4937 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4938 return;
4939 }
4940 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4941 }
4942
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004943 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004944 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004945 }
4946
4947 // get the fuzzy matches
4948 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004949 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004950 goto done;
4951 if (retmatchpos)
4952 {
4953 list_T *l;
4954
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004955 // For matchfuzzypos(), a list with three items are returned. First
4956 // item is a list of matching strings, the second item is a list of
4957 // lists with matching positions within each string and the third item
4958 // is the list of scores of the matches.
4959 l = list_alloc();
4960 if (l == NULL)
4961 goto done;
4962 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004963 {
4964 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004965 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004966 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004967 l = list_alloc();
4968 if (l == NULL)
4969 goto done;
4970 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004971 {
4972 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004973 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004974 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004975 l = list_alloc();
4976 if (l == NULL)
4977 goto done;
4978 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004979 {
4980 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004981 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004982 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004983 }
4984
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004985 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004986 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004987
4988done:
4989 free_callback(&cb);
4990}
4991
4992/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004993 * "matchfuzzy()" function
4994 */
4995 void
4996f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4997{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004998 do_fuzzymatch(argvars, rettv, FALSE);
4999}
5000
5001/*
5002 * "matchfuzzypos()" function
5003 */
5004 void
5005f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
5006{
5007 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02005008}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02005009#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005010
5011/*
5012 * Same as fuzzy_match_item_compare() except for use with a string match
5013 */
5014 static int
5015fuzzy_match_str_compare(const void *s1, const void *s2)
5016{
5017 int v1 = ((fuzmatch_str_T *)s1)->score;
5018 int v2 = ((fuzmatch_str_T *)s2)->score;
5019 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5020 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5021
Christian Brabandte06e4372024-02-09 19:39:14 +01005022 if (v1 == v2)
5023 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5024 else
5025 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005026}
5027
5028/*
5029 * Sort fuzzy matches by score
5030 */
5031 static void
5032fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
5033{
5034 // Sort the list by the descending order of the match score
5035 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5036 fuzzy_match_str_compare);
5037}
5038
5039/*
5040 * Same as fuzzy_match_item_compare() except for use with a function name
5041 * string match. <SNR> functions should be sorted to the end.
5042 */
5043 static int
5044fuzzy_match_func_compare(const void *s1, const void *s2)
5045{
5046 int v1 = ((fuzmatch_str_T *)s1)->score;
5047 int v2 = ((fuzmatch_str_T *)s2)->score;
5048 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5049 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5050 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
5051 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
5052
Christian Brabandte06e4372024-02-09 19:39:14 +01005053 if (*str1 != '<' && *str2 == '<')
5054 return -1;
5055 if (*str1 == '<' && *str2 != '<')
5056 return 1;
5057 if (v1 == v2)
5058 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5059 else
5060 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005061}
5062
5063/*
5064 * Sort fuzzy matches of function names by score.
5065 * <SNR> functions should be sorted to the end.
5066 */
5067 static void
5068fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
5069{
5070 // Sort the list by the descending order of the match score
5071 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5072 fuzzy_match_func_compare);
5073}
5074
5075/*
5076 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
5077 * returns the match score.
5078 */
5079 int
5080fuzzy_match_str(char_u *str, char_u *pat)
5081{
5082 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00005083 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005084
5085 if (str == NULL || pat == NULL)
5086 return 0;
5087
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00005088 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005089 sizeof(matchpos) / sizeof(matchpos[0]));
5090
5091 return score;
5092}
5093
5094/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005095 * Free an array of fuzzy string matches "fuzmatch[count]".
5096 */
5097 void
5098fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5099{
5100 int i;
5101
5102 if (fuzmatch == NULL)
5103 return;
5104 for (i = 0; i < count; ++i)
5105 vim_free(fuzmatch[i].str);
5106 vim_free(fuzmatch);
5107}
5108
5109/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005110 * Copy a list of fuzzy matches into a string list after sorting the matches by
5111 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5112 * Returns OK on success and FAIL on memory allocation failure.
5113 */
5114 int
5115fuzzymatches_to_strmatches(
5116 fuzmatch_str_T *fuzmatch,
5117 char_u ***matches,
5118 int count,
5119 int funcsort)
5120{
5121 int i;
5122
5123 if (count <= 0)
5124 return OK;
5125
5126 *matches = ALLOC_MULT(char_u *, count);
5127 if (*matches == NULL)
5128 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005129 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005130 return FAIL;
5131 }
5132
5133 // Sort the list by the descending order of the match score
5134 if (funcsort)
5135 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5136 else
5137 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5138
5139 for (i = 0; i < count; i++)
5140 (*matches)[i] = fuzmatch[i].str;
5141 vim_free(fuzmatch);
5142
5143 return OK;
5144}