blob: 3519c32cb6cad4c585f1cd27e7ad367a3ddd3ddb [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);
glepnir9dfc7e52025-01-21 22:33:13 +010045static int fuzzy_match_compute_score(char_u *fuzpat, char_u *str, int strSz, int_u *matches, int numMatches);
John Marriott8c85a2a2024-05-20 19:18:26 +020046static 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);
glepnir8159fb12024-07-17 20:32:54 +020056static int fuzzy_match_str_in_line(char_u **ptr, char_u *pat, int *len, pos_T *current_pos);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020057
Bram Moolenaarea6561a2020-06-01 21:32:45 +020058#define SEARCH_STAT_DEF_TIMEOUT 40L
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020059#define SEARCH_STAT_DEF_MAX_COUNT 99
60#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
Bram Moolenaar071d4272004-06-13 20:20:40 +000062/*
63 * This file contains various searching-related routines. These fall into
64 * three groups:
65 * 1. string searches (for /, ?, n, and N)
66 * 2. character searches within a single line (for f, F, t, T, etc)
67 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
68 */
69
70/*
71 * String searches
72 *
73 * The string search functions are divided into two levels:
74 * lowest: searchit(); uses an pos_T for starting position and found match.
75 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
76 *
77 * The last search pattern is remembered for repeating the same search.
78 * This pattern is shared between the :g, :s, ? and / commands.
79 * This is in search_regcomp().
80 *
81 * The actual string matching is done using a heavily modified version of
82 * Henry Spencer's regular expression library. See regexp.c.
83 */
84
Bram Moolenaar071d4272004-06-13 20:20:40 +000085/*
86 * Two search patterns are remembered: One for the :substitute command and
87 * one for other searches. last_idx points to the one that was used the last
88 * time.
89 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020090static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000091{
John Marriott8c85a2a2024-05-20 19:18:26 +020092 {NULL, 0, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat
93 {NULL, 0, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat
Bram Moolenaar071d4272004-06-13 20:20:40 +000094};
95
Bram Moolenaar63d9e732019-12-05 21:10:38 +010096static int last_idx = 0; // index in spats[] for RE_LAST
Bram Moolenaar071d4272004-06-13 20:20:40 +000097
Bram Moolenaar63d9e732019-12-05 21:10:38 +010098static char_u lastc[2] = {NUL, NUL}; // last character searched for
99static int lastcdir = FORWARD; // last direction of character search
100static int last_t_cmd = TRUE; // last search t_cmd
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200101static char_u lastc_bytes[MB_MAXBYTES + 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100102static int lastc_bytelen = 1; // >1 for multi-byte char
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200103
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100104// copy of spats[], for keeping the search patterns while executing autocmds
John Marriott8c85a2a2024-05-20 19:18:26 +0200105static spat_T saved_spats[ARRAY_LENGTH(spats)];
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100106static char_u *saved_mr_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200107static size_t saved_mr_patternlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100109static int saved_spats_last_idx = 0;
110static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100113// allocated copy of pattern used by search_regcomp()
114static char_u *mr_pattern = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200115static size_t mr_patternlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
117#ifdef FEAT_FIND_ID
118/*
119 * Type used by find_pattern_in_path() to remember which included files have
120 * been searched already.
121 */
122typedef struct SearchedFile
123{
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100124 FILE *fp; // File pointer
125 char_u *name; // Full name of file
126 linenr_T lnum; // Line we were up to in file
127 int matched; // Found a match in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128} SearchedFile;
129#endif
130
131/*
132 * translate search pattern for vim_regcomp()
133 *
134 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
135 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
136 * pat_save == RE_BOTH: save pat in both patterns (:global command)
137 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000138 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
140 * options & SEARCH_HIS: put search string in history
141 * options & SEARCH_KEEP: keep previous search pattern
142 *
143 * returns FAIL if failed, OK otherwise.
144 */
145 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100146search_regcomp(
147 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +0200148 size_t patlen,
Rob Pillinge86190e2022-12-23 19:06:04 +0000149 char_u **used_pat,
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100150 int pat_save,
151 int pat_use,
152 int options,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100153 regmmatch_T *regmatch) // return: pattern and ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
155 int magic;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156
157 rc_did_emsg = FALSE;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100158 magic = magic_isset();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160 /*
161 * If no pattern given, use a previously defined pattern.
162 */
163 if (pat == NULL || *pat == NUL)
164 {
John Marriott8c85a2a2024-05-20 19:18:26 +0200165 int i;
166
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 if (pat_use == RE_LAST)
168 i = last_idx;
169 else
170 i = pat_use;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100171 if (spats[i].pat == NULL) // pattern was never defined
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 {
173 if (pat_use == RE_SUBST)
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200174 emsg(_(e_no_previous_substitute_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200176 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 rc_did_emsg = TRUE;
178 return FAIL;
179 }
180 pat = spats[i].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +0200181 patlen = spats[i].patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 magic = spats[i].magic;
183 no_smartcase = spats[i].no_scs;
184 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100185 else if (options & SEARCH_HIS) // put new pattern in history
John Marriott8c85a2a2024-05-20 19:18:26 +0200186 add_to_history(HIST_SEARCH, pat, patlen, TRUE, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
Rob Pillinge86190e2022-12-23 19:06:04 +0000188 if (used_pat)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000189 *used_pat = pat;
Rob Pillinge86190e2022-12-23 19:06:04 +0000190
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100191 vim_free(mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100194 mr_pattern = reverse_text(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 else
196#endif
John Marriott8c85a2a2024-05-20 19:18:26 +0200197 mr_pattern = vim_strnsave(pat, patlen);
198 if (mr_pattern == NULL)
199 mr_patternlen = 0;
200 else
201 mr_patternlen = patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202
203 /*
204 * Save the currently used pattern in the appropriate place,
205 * unless the pattern should not be remembered.
206 */
Bram Moolenaare1004402020-10-24 20:49:43 +0200207 if (!(options & SEARCH_KEEP)
208 && (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100210 // search or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
John Marriott8c85a2a2024-05-20 19:18:26 +0200212 save_re_pat(RE_SEARCH, pat, patlen, magic);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100213 // substitute or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
John Marriott8c85a2a2024-05-20 19:18:26 +0200215 save_re_pat(RE_SUBST, pat, patlen, magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 }
217
218 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000219 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
221 if (regmatch->regprog == NULL)
222 return FAIL;
223 return OK;
224}
225
226/*
227 * Get search pattern used by search_regcomp().
228 */
229 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100230get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 return mr_pattern;
233}
234
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100235 void
John Marriott8c85a2a2024-05-20 19:18:26 +0200236save_re_pat(int idx, char_u *pat, size_t patlen, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000238 if (spats[idx].pat == pat)
239 return;
240
241 vim_free(spats[idx].pat);
John Marriott8c85a2a2024-05-20 19:18:26 +0200242 spats[idx].pat = vim_strnsave(pat, patlen);
243 if (spats[idx].pat == NULL)
244 spats[idx].patlen = 0;
245 else
246 spats[idx].patlen = patlen;
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000247 spats[idx].magic = magic;
248 spats[idx].no_scs = no_smartcase;
249 last_idx = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000251 // If 'hlsearch' set and search pat changed: need redraw.
252 if (p_hls)
253 redraw_all_later(UPD_SOME_VALID);
254 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256}
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258/*
259 * Save the search patterns, so they can be restored later.
260 * Used before/after executing autocommands and user functions.
261 */
262static int save_level = 0;
263
264 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100265save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266{
John Marriott8c85a2a2024-05-20 19:18:26 +0200267 int i;
268
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000269 if (save_level++ != 0)
270 return;
271
John Marriott8c85a2a2024-05-20 19:18:26 +0200272 for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i)
273 {
274 saved_spats[i] = spats[i];
275 if (spats[i].pat != NULL)
276 {
277 saved_spats[i].pat = vim_strnsave(spats[i].pat, spats[i].patlen);
278 if (saved_spats[i].pat == NULL)
279 saved_spats[i].patlen = 0;
280 else
281 saved_spats[i].patlen = spats[i].patlen;
282 }
283 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000284 if (mr_pattern == NULL)
285 saved_mr_pattern = NULL;
286 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200287 saved_mr_pattern = vim_strnsave(mr_pattern, mr_patternlen);
288 if (saved_mr_pattern == NULL)
289 saved_mr_patternlen = 0;
290 else
291 saved_mr_patternlen = mr_patternlen;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100292#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000293 saved_spats_last_idx = last_idx;
294 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296}
297
298 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100299restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300{
John Marriott8c85a2a2024-05-20 19:18:26 +0200301 int i;
302
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000303 if (--save_level != 0)
304 return;
305
John Marriott8c85a2a2024-05-20 19:18:26 +0200306 for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i)
307 {
308 vim_free(spats[i].pat);
309 spats[i] = saved_spats[i];
310 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100311#if defined(FEAT_EVAL)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000312 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100313#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000314 vim_free(mr_pattern);
315 mr_pattern = saved_mr_pattern;
John Marriott8c85a2a2024-05-20 19:18:26 +0200316 mr_patternlen = saved_mr_patternlen;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100317#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000318 last_idx = saved_spats_last_idx;
319 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000323#if defined(EXITFREE) || defined(PROTO)
324 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100325free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000326{
John Marriott8c85a2a2024-05-20 19:18:26 +0200327 int i;
328
329 for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i)
330 {
331 VIM_CLEAR(spats[i].pat);
332 spats[i].patlen = 0;
333 }
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100334 VIM_CLEAR(mr_pattern);
John Marriott8c85a2a2024-05-20 19:18:26 +0200335 mr_patternlen = 0;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000336}
337#endif
338
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100339#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100340// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
341// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200342static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100343static int did_save_last_search_spat = 0;
344static int saved_last_idx = 0;
345static int saved_no_hlsearch = 0;
Christian Brabandt6dd74242022-02-14 12:44:32 +0000346static int saved_search_match_endcol;
347static int saved_search_match_lines;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100348
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100349/*
350 * Save and restore the search pattern for incremental highlight search
351 * feature.
352 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100353 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100354 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100355 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100356 */
357 void
358save_last_search_pattern(void)
359{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200360 if (++did_save_last_search_spat != 1)
361 // nested call, nothing to do
362 return;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100363
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100364 saved_last_search_spat = spats[RE_SEARCH];
365 if (spats[RE_SEARCH].pat != NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +0200366 {
367 saved_last_search_spat.pat = vim_strnsave(spats[RE_SEARCH].pat, spats[RE_SEARCH].patlen);
368 if (saved_last_search_spat.pat == NULL)
369 saved_last_search_spat.patlen = 0;
370 else
371 saved_last_search_spat.patlen = spats[RE_SEARCH].patlen;
372 }
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100373 saved_last_idx = last_idx;
374 saved_no_hlsearch = no_hlsearch;
375}
376
377 void
378restore_last_search_pattern(void)
379{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200380 if (--did_save_last_search_spat > 0)
381 // nested call, nothing to do
382 return;
383 if (did_save_last_search_spat != 0)
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100384 {
Bram Moolenaar442a8532020-06-04 20:56:09 +0200385 iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100386 return;
387 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100388
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100389 vim_free(spats[RE_SEARCH].pat);
390 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100391 saved_last_search_spat.pat = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +0200392 saved_last_search_spat.patlen = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100393# if defined(FEAT_EVAL)
394 set_vv_searchforward();
395# endif
396 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200397 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100398}
Bram Moolenaard0480092017-11-16 22:20:39 +0100399
Christian Brabandt6dd74242022-02-14 12:44:32 +0000400/*
401 * Save and restore the incsearch highlighting variables.
402 * This is required so that calling searchcount() at does not invalidate the
403 * incsearch highlighting.
404 */
405 static void
406save_incsearch_state(void)
407{
408 saved_search_match_endcol = search_match_endcol;
409 saved_search_match_lines = search_match_lines;
410}
411
412 static void
413restore_incsearch_state(void)
414{
415 search_match_endcol = saved_search_match_endcol;
416 search_match_lines = saved_search_match_lines;
417}
418
Bram Moolenaard0480092017-11-16 22:20:39 +0100419 char_u *
420last_search_pattern(void)
421{
422 return spats[RE_SEARCH].pat;
423}
John Marriottccf89072024-10-07 21:40:39 +0200424
425 size_t
426last_search_pattern_len(void)
427{
428 return spats[RE_SEARCH].patlen;
429}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100430#endif
431
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432/*
433 * Return TRUE when case should be ignored for search pattern "pat".
434 * Uses the 'ignorecase' and 'smartcase' options.
435 */
436 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100437ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200439 return ignorecase_opt(pat, p_ic, p_scs);
440}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200442/*
443 * As ignorecase() put pass the "ic" and "scs" flags.
444 */
445 int
446ignorecase_opt(char_u *pat, int ic_in, int scs)
447{
448 int ic = ic_in;
449
450 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200451 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200452 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 no_smartcase = FALSE;
454
455 return ic;
456}
457
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200458/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200459 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200460 */
461 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100462pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200463{
464 char_u *p = pat;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200465 magic_T magic_val = MAGIC_ON;
466
467 // get the magicness of the pattern
468 (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val);
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200469
470 while (*p != NUL)
471 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200472 int l;
473
474 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
475 {
476 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
477 return TRUE;
478 p += l;
479 }
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200480 else if (*p == '\\' && magic_val <= MAGIC_ON)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200481 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100482 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200483 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100484 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200485 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100486 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200487 p += 2;
488 else
489 p += 1;
490 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200491 else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL)
492 {
493 if (p[1] != NUL) // skip "_X" and %X
494 p += 2;
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200495 else
496 p++;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200497 }
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200498 else if (MB_ISUPPER(*p))
499 return TRUE;
500 else
501 ++p;
502 }
503 return FALSE;
504}
505
Bram Moolenaar113e1072019-01-20 15:30:40 +0100506#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100508last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200509{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200510 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200511}
512
513 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100514last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200515{
516 return lastcdir == FORWARD;
517}
518
519 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100520last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200521{
522 return last_t_cmd == TRUE;
523}
524
525 void
zeertzjqe5d91ba2023-05-14 17:39:18 +0100526set_last_csearch(int c, char_u *s, int len)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200527{
528 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200529 lastc_bytelen = len;
530 if (len)
531 memcpy(lastc_bytes, s, len);
532 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200533 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200534}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100535#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200536
537 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100538set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200539{
540 lastcdir = cdir;
541}
542
543 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100544set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200545{
546 last_t_cmd = t_cmd;
547}
548
549 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100550last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
552 return spats[last_idx].pat;
553}
554
555/*
556 * Reset search direction to forward. For "gd" and "gD" commands.
557 */
558 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100559reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000562#if defined(FEAT_EVAL)
563 set_vv_searchforward();
564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565}
566
567#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
568/*
569 * Set the last search pattern. For ":let @/ =" and viminfo.
570 * Also set the saved search pattern, so that this works in an autocommand.
571 */
572 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100573set_last_search_pat(
574 char_u *s,
575 int idx,
576 int magic,
577 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578{
579 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100580 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 if (*s == NUL)
582 spats[idx].pat = NULL;
583 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200584 {
585 spats[idx].patlen = STRLEN(s);
586 spats[idx].pat = vim_strnsave(s, spats[idx].patlen);
587 }
588 if (spats[idx].pat == NULL)
589 spats[idx].patlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 spats[idx].magic = magic;
591 spats[idx].no_scs = FALSE;
592 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000593#if defined(FEAT_EVAL)
594 set_vv_searchforward();
595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 spats[idx].off.line = FALSE;
597 spats[idx].off.end = FALSE;
598 spats[idx].off.off = 0;
599 if (setlast)
600 last_idx = idx;
601 if (save_level)
602 {
603 vim_free(saved_spats[idx].pat);
604 saved_spats[idx] = spats[0];
605 if (spats[idx].pat == NULL)
606 saved_spats[idx].pat = NULL;
607 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200608 saved_spats[idx].pat = vim_strnsave(spats[idx].pat, spats[idx].patlen);
609 if (saved_spats[idx].pat == NULL)
610 saved_spats[idx].patlen = 0;
611 else
612 saved_spats[idx].patlen = spats[idx].patlen;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100613# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100614 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100615# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 }
617# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100618 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100620 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621# endif
622}
623#endif
624
625#ifdef FEAT_SEARCH_EXTRA
626/*
627 * Get a regexp program for the last used search pattern.
628 * This is used for highlighting all matches in a window.
629 * Values returned in regmatch->regprog and regmatch->rmm_ic.
630 */
631 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100632last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 if (spats[last_idx].pat == NULL)
635 {
636 regmatch->regprog = NULL;
637 return;
638 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100639 ++emsg_off; // So it doesn't beep if bad expr
John Marriott8c85a2a2024-05-20 19:18:26 +0200640 (void)search_regcomp((char_u *)"", 0, NULL, 0, last_idx, SEARCH_KEEP, regmatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 --emsg_off;
642}
643#endif
644
645/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100646 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100647 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
648 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 *
650 * if (options & SEARCH_MSG) == 0 don't give any messages
651 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
652 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
653 * if (options & SEARCH_HIS) put search pattern in history
654 * if (options & SEARCH_END) return position at end of match
655 * if (options & SEARCH_START) accept match at pos itself
656 * if (options & SEARCH_KEEP) keep previous search pattern
657 * if (options & SEARCH_FOLD) match only once in a closed fold
658 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100659 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 *
661 * Return FAIL (zero) for failure, non-zero for success.
662 * When FEAT_EVAL is defined, returns the index of the first matching
663 * subpattern plus one; one if there was none.
664 */
665 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100666searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200667 win_T *win, // window to search in; can be NULL for a
668 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100669 buf_T *buf,
670 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100671 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100672 int dir,
673 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +0200674 size_t patlen,
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100675 long count,
676 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200677 int pat_use, // which pattern to use when "pat" is empty
678 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
680 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100681 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100682 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 regmmatch_T regmatch;
684 char_u *ptr;
685 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000687 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 int loop;
689 pos_T start_pos;
690 int at_first_line;
691 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200692 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 int match_ok;
694 long nmatched;
695 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100696 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100697 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#ifdef FEAT_SEARCH_EXTRA
699 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200701 linenr_T stop_lnum = 0; // stop after this line number when != 0
Paul Ollis65745772022-06-05 16:55:54 +0100702 int unused_timeout_flag = FALSE;
703 int *timed_out = &unused_timeout_flag; // set when timed out.
John Marriott8c85a2a2024-05-20 19:18:26 +0200704 int search_from_match_end; // vi-compatible search?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
John Marriott8c85a2a2024-05-20 19:18:26 +0200706 if (search_regcomp(pat, patlen, NULL, RE_SEARCH, pat_use,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
708 {
709 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000710 semsg(_(e_invalid_search_string_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 return FAIL;
712 }
713
John Marriott8c85a2a2024-05-20 19:18:26 +0200714 search_from_match_end = vim_strchr(p_cpo, CPO_SEARCH) != NULL;
715
Paul Ollis65745772022-06-05 16:55:54 +0100716 if (extra_arg != NULL)
717 {
718 stop_lnum = extra_arg->sa_stop_lnum;
719#ifdef FEAT_RELTIME
720 if (extra_arg->sa_tm > 0)
Paul Ollis65745772022-06-05 16:55:54 +0100721 init_regexp_timeout(extra_arg->sa_tm);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100722 // Also set the pointer when sa_tm is zero, the caller may have set the
723 // timeout.
724 timed_out = &extra_arg->sa_timed_out;
Paul Ollis65745772022-06-05 16:55:54 +0100725#endif
726 }
727
Bram Moolenaar280f1262006-01-30 00:14:18 +0000728 /*
729 * find the string
730 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100731 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100733 // When not accepting a match at the start position set "extra_col" to
734 // a non-zero value. Don't do that when starting at MAXCOL, since
735 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200736 if (pos->col == MAXCOL)
737 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100738 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200739 else if (has_mbyte
740 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
741 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100742 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100743 ptr = ml_get_buf(buf, pos->lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100744 if (ml_get_buf_len(buf, pos->lnum) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200745 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100746 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100747 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100748 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100749 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200750 start_char_len = 1;
751 if (dir == FORWARD)
752 {
753 if (options & SEARCH_START)
754 extra_col = 0;
755 else
756 extra_col = start_char_len;
757 }
758 else
759 {
760 if (options & SEARCH_START)
761 extra_col = start_char_len;
762 else
763 extra_col = 0;
764 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100765
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100766 start_pos = *pos; // remember start pos for detecting no match
767 found = 0; // default: not found
768 at_first_line = TRUE; // default: start in first line
769 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
771 pos->lnum = 1;
772 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100773 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 }
775
776 /*
777 * Start searching in current line, unless searching backwards and
778 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000779 * If we are searching backwards, in column 0, and not including the
780 * current position, gain some efficiency by skipping back a line.
781 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000783 if (dir == BACKWARD && start_pos.col == 0
784 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
786 lnum = pos->lnum - 1;
787 at_first_line = FALSE;
788 }
789 else
790 lnum = pos->lnum;
791
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100792 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
795 lnum += dir, at_first_line = FALSE)
796 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100797 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000798 if (stop_lnum != 0 && (dir == FORWARD
799 ? lnum > stop_lnum : lnum < stop_lnum))
800 break;
Paul Ollis65745772022-06-05 16:55:54 +0100801 // Stop after passing the time limit.
802 if (*timed_out)
Bram Moolenaar76929292008-01-06 19:07:36 +0000803 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000806 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100808 col = at_first_line && (options & SEARCH_COL) ? pos->col
809 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 nmatched = vim_regexec_multi(&regmatch, win, buf,
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100811 lnum, col, timed_out);
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200812 // vim_regexec_multi() may clear "regprog"
813 if (regmatch.regprog == NULL)
814 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100815 // Abort searching on an error (e.g., out of stack).
Paul Ollis65745772022-06-05 16:55:54 +0100816 if (called_emsg > called_emsg_before || *timed_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 break;
818 if (nmatched > 0)
819 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100820 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000821 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000823#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000825#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100826 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000827 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
828 ptr = (char_u *)"";
829 else
830 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
832 /*
833 * Forward search in the first line: match should be after
834 * the start position. If not, continue at the end of the
835 * match (this is vi compatible) or on the next char.
836 */
837 if (dir == FORWARD && at_first_line)
838 {
839 match_ok = TRUE;
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000840
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000842 * When the match starts in a next line it's certainly
843 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 * When match lands on a NUL the cursor will be put
845 * one back afterwards, compare with that position,
846 * otherwise "/$" will get stuck on end of line.
847 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000848 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100849 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000850 ? (nmatched == 1
851 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000853 : ((int)matchpos.col
854 - (ptr[matchpos.col] == NUL)
855 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
857 /*
858 * If vi-compatible searching, continue at the end
859 * of the match, otherwise continue one position
860 * forward.
861 */
John Marriott8c85a2a2024-05-20 19:18:26 +0200862 if (search_from_match_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 {
864 if (nmatched > 1)
865 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100866 // end is in next line, thus no match in
867 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 match_ok = FALSE;
869 break;
870 }
871 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100872 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000873 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 && ptr[matchcol] != NUL)
875 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (has_mbyte)
877 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000878 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 ++matchcol;
881 }
882 }
883 else
884 {
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000885 // Advance "matchcol" to the next character.
Bram Moolenaar837ca8f2022-11-26 18:59:19 +0000886 // This uses rmm_matchcol, the actual start of
887 // the match, ignoring "\zs".
888 matchcol = regmatch.rmm_matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 if (ptr[matchcol] != NUL)
890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000892 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 + matchcol);
894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 ++matchcol;
896 }
897 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200898 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100899 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (ptr[matchcol] == NUL
901 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000902 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100903 matchcol, timed_out)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 {
905 match_ok = FALSE;
906 break;
907 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200908 // vim_regexec_multi() may clear "regprog"
909 if (regmatch.regprog == NULL)
910 break;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000911 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 endpos = regmatch.endpos[0];
913# ifdef FEAT_EVAL
914 submatch = first_submatch(&regmatch);
915# endif
916
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100917 // Need to get the line pointer again, a
918 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000919 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 if (!match_ok)
922 continue;
923 }
924 if (dir == BACKWARD)
925 {
926 /*
927 * Now, if there are multiple matches on this line,
928 * we have to get the last one. Or the last one before
929 * the cursor, if we're on that line.
930 * When putting the new cursor at the end, compare
931 * relative to the end of the match.
932 */
933 match_ok = FALSE;
934 for (;;)
935 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100936 // Remember a position that is before the start
937 // position, we use it if it's the last match in
938 // the line. Always accept a position after
939 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000940 if (loop
941 || ((options & SEARCH_END)
942 ? (lnum + regmatch.endpos[0].lnum
943 < start_pos.lnum
944 || (lnum + regmatch.endpos[0].lnum
945 == start_pos.lnum
946 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200947 < (int)start_pos.col
948 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000949 : (lnum + regmatch.startpos[0].lnum
950 < start_pos.lnum
951 || (lnum + regmatch.startpos[0].lnum
952 == start_pos.lnum
953 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200954 < (int)start_pos.col
955 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000958 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 endpos = regmatch.endpos[0];
960# ifdef FEAT_EVAL
961 submatch = first_submatch(&regmatch);
962# endif
963 }
964 else
965 break;
966
967 /*
968 * We found a valid match, now check if there is
969 * another one after it.
970 * If vi-compatible searching, continue at the end
971 * of the match, otherwise continue one position
972 * forward.
973 */
John Marriott8c85a2a2024-05-20 19:18:26 +0200974 if (search_from_match_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
976 if (nmatched > 1)
977 break;
978 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100979 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000980 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 && ptr[matchcol] != NUL)
982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 if (has_mbyte)
984 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000985 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 ++matchcol;
988 }
989 }
990 else
991 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100992 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000993 if (matchpos.lnum > 0)
994 break;
995 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 if (ptr[matchcol] != NUL)
997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (has_mbyte)
999 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001000 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 ++matchcol;
1003 }
1004 }
1005 if (ptr[matchcol] == NUL
1006 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +00001007 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +01001008 matchcol, timed_out)) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +01001009 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001010 // If the search timed out, we did find a match
1011 // but it might be the wrong one, so that's not
1012 // OK.
Paul Ollis65745772022-06-05 16:55:54 +01001013 if (*timed_out)
Bram Moolenaar9d322762018-02-09 16:04:25 +01001014 match_ok = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +01001016 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001017 // vim_regexec_multi() may clear "regprog"
1018 if (regmatch.regprog == NULL)
1019 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001021 // Need to get the line pointer again, a
1022 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001023 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025
1026 /*
1027 * If there is only a match after the cursor, skip
1028 * this match.
1029 */
1030 if (!match_ok)
1031 continue;
1032 }
1033
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001034 // With the SEARCH_END option move to the last character
1035 // of the match. Don't do it for an empty match, end
1036 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +02001037 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001038 && !(matchpos.lnum == endpos.lnum
1039 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001041 // For a match in the first column, set the position
1042 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001043 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001044 pos->col = endpos.col;
1045 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001046 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001047 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001048 {
1049 --pos->lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01001050 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001051 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001052 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001053 else
1054 {
1055 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001056 if (has_mbyte
1057 && pos->lnum <= buf->b_ml.ml_line_count)
1058 {
1059 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1060 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1061 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001062 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001063 if (end_pos != NULL)
1064 {
1065 end_pos->lnum = lnum + matchpos.lnum;
1066 end_pos->col = matchpos.col;
1067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 else
1070 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001071 pos->lnum = lnum + matchpos.lnum;
1072 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001073 if (end_pos != NULL)
1074 {
1075 end_pos->lnum = lnum + endpos.lnum;
1076 end_pos->col = endpos.col;
1077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001080 if (end_pos != NULL)
1081 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001083 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001085 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001086 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 search_match_endcol = endpos.col;
1088 break;
1089 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001090 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (got_int)
1092 break;
1093
1094#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001095 // Cancel searching if a character was typed. Used for
1096 // 'incsearch'. Don't check too often, that would slowdown
1097 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if ((options & SEARCH_PEEK)
1099 && ((lnum - pos->lnum) & 0x3f) == 0
1100 && char_avail())
1101 {
1102 break_loop = TRUE;
1103 break;
1104 }
1105#endif
1106
1107 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001108 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110 at_first_line = FALSE;
1111
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001112 // vim_regexec_multi() may clear "regprog"
1113 if (regmatch.regprog == NULL)
1114 break;
1115
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001117 * Stop the search if wrapscan isn't set, "stop_lnum" is
1118 * specified, after an interrupt, after a match and after looping
1119 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001121 if (!p_ws || stop_lnum != 0 || got_int
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001122 || called_emsg > called_emsg_before || *timed_out
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001123#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001124 || break_loop
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001125#endif
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001126 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 break;
1128
1129 /*
1130 * If 'wrapscan' is set we continue at the other end of the file.
Christian Brabandt34a6a362023-05-06 19:20:20 +01001131 * If 'shortmess' does not contain 's', we give a message, but
1132 * only, if we won't show the search stat later anyhow,
1133 * (so SEARCH_COUNT must be absent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * This message is also remembered in keep_msg for when the screen
1135 * is redrawn. The keep_msg is cleared whenever another message is
1136 * written.
1137 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001138 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 lnum = 1;
Christian Brabandt34a6a362023-05-06 19:20:20 +01001142 if (!shortmess(SHM_SEARCH)
1143 && shortmess(SHM_SEARCHCOUNT)
1144 && (options & SEARCH_MSG))
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001145 give_warning((char_u *)_(dir == BACKWARD
1146 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001147 if (extra_arg != NULL)
1148 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 }
Paul Ollis65745772022-06-05 16:55:54 +01001150 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001151#ifdef FEAT_SEARCH_EXTRA
1152 || break_loop
1153#endif
1154 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 break;
1156 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001157 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001159#ifdef FEAT_RELTIME
1160 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1161 disable_regexp_timeout();
1162#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001163 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001165 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 {
1167 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001168 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1170 {
1171 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001172 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001174 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001176 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 }
1178 return FAIL;
1179 }
1180
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001181 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001182 if (pos->lnum > buf->b_ml.ml_line_count)
1183 {
1184 pos->lnum = buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01001185 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001186 if (pos->col > 0)
1187 --pos->col;
1188 }
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 return submatch + 1;
1191}
1192
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001193#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001194 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001195set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001196{
1197 spats[0].off.dir = cdir;
1198}
1199
1200 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001201set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001202{
1203 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1204}
1205
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206/*
1207 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001208 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
1210 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001211first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212{
1213 int submatch;
1214
1215 for (submatch = 1; ; ++submatch)
1216 {
1217 if (rp->startpos[submatch].lnum >= 0)
1218 break;
1219 if (submatch == 9)
1220 {
1221 submatch = 0;
1222 break;
1223 }
1224 }
1225 return submatch;
1226}
1227#endif
1228
1229/*
1230 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001231 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 * If 'dirc' is 0: use previous dir.
1233 * If 'pat' is NULL or empty : use previous string.
1234 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1235 * If 'options & SEARCH_ECHO': echo the search command and handle options
1236 * If 'options & SEARCH_MSG' : may give error message
1237 * If 'options & SEARCH_OPT' : interpret optional flags
1238 * If 'options & SEARCH_HIS' : put search pattern in history
1239 * If 'options & SEARCH_NOOF': don't add offset to position
1240 * If 'options & SEARCH_MARK': set previous context mark
1241 * If 'options & SEARCH_KEEP': keep previous search pattern
1242 * If 'options & SEARCH_START': accept match at curpos itself
1243 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1244 *
1245 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1246 * makes the movement linewise without moving the match position.
1247 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001248 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 */
1250 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001251do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001252 oparg_T *oap, // can be NULL
1253 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001254 int search_delim, // the delimiter for the search, e.g. '%' in
1255 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001256 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +02001257 size_t patlen,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001258 long count,
1259 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001260 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001262 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 char_u *searchstr;
John Marriott8c85a2a2024-05-20 19:18:26 +02001264 size_t searchstrlen;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001265 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001266 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 char_u *p;
1268 long c;
1269 char_u *dircp;
1270 char_u *strcopy = NULL;
1271 char_u *ps;
John Marriott8c85a2a2024-05-20 19:18:26 +02001272 int show_search_stats;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001273 char_u *msgbuf = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +02001274 size_t msgbuflen = 0;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001275 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276
John Marriott8c85a2a2024-05-20 19:18:26 +02001277 searchcmdlen = 0;
1278
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 /*
1280 * A line offset is not remembered, this is vi compatible.
1281 */
1282 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1283 {
1284 spats[0].off.line = FALSE;
1285 spats[0].off.off = 0;
1286 }
1287
1288 /*
1289 * Save the values for when (options & SEARCH_KEEP) is used.
1290 * (there is no "if ()" around this because gcc wants them initialized)
1291 */
1292 old_off = spats[0].off;
1293
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001294 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295
1296 /*
1297 * Find out the direction of the search.
1298 */
1299 if (dirc == 0)
1300 dirc = spats[0].off.dir;
1301 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001304#if defined(FEAT_EVAL)
1305 set_vv_searchforward();
1306#endif
1307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (options & SEARCH_REV)
1309 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001310#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001311 // There is a bug in the Visual C++ 2.2 compiler which means that
1312 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 dirc = (dirc == '/') ? '?' : '/';
1314#else
1315 if (dirc == '/')
1316 dirc = '?';
1317 else
1318 dirc = '/';
1319#endif
1320 }
1321
1322#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001323 // If the cursor is in a closed fold, don't find another match in the same
1324 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (dirc == '/')
1326 {
1327 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001328 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330 else
1331 {
1332 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1333 pos.col = 0;
1334 }
1335#endif
1336
1337#ifdef FEAT_SEARCH_EXTRA
1338 /*
1339 * Turn 'hlsearch' highlighting back on.
1340 */
1341 if (no_hlsearch && !(options & SEARCH_KEEP))
1342 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001343 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001344 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346#endif
1347
1348 /*
1349 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1350 */
1351 for (;;)
1352 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001353 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 searchstr = pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001356 searchstrlen = patlen;
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001359 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001360 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001362 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001364 if (spats[RE_SUBST].pat == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001365 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001366 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001367 retval = 0;
1368 goto end_do_search;
1369 }
John Marriott8c85a2a2024-05-20 19:18:26 +02001370 searchstr = spats[RE_SUBST].pat;
1371 searchstrlen = spats[RE_SUBST].patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001373 else
1374 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001375 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001376 searchstr = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02001377 searchstrlen = 0;
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001381 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
1383 /*
1384 * Find end of regular expression.
1385 * If there is a matching '/' or '?', toss it.
1386 */
1387 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001388 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001389 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 if (strcopy != ps)
1391 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001392 size_t len = STRLEN(strcopy);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001393 // made a copy of "pat" to change "\?" to "?"
John Marriott8c85a2a2024-05-20 19:18:26 +02001394 searchcmdlen += (int)(patlen - len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 pat = strcopy;
John Marriott8c85a2a2024-05-20 19:18:26 +02001396 patlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 searchstr = strcopy;
John Marriott8c85a2a2024-05-20 19:18:26 +02001398 searchstrlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001400 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001402 searchstrlen = p - pat;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001403 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 *p++ = NUL;
1405 }
1406 spats[0].off.line = FALSE;
1407 spats[0].off.end = FALSE;
1408 spats[0].off.off = 0;
1409 /*
1410 * Check for a line offset or a character offset.
1411 * For get_address (echo off) we don't check for a character
1412 * offset, because it is meaningless and the 's' could be a
1413 * substitute command.
1414 */
1415 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1416 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001417 else if ((options & SEARCH_OPT)
1418 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001420 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 spats[0].off.end = SEARCH_END;
1422 ++p;
1423 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001424 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001426 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1428 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001429 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001431 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 spats[0].off.off = 1;
1433 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001434 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 ++p;
1436 }
1437
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001438 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 searchcmdlen += (int)(p - pat);
1440
John Marriott8c85a2a2024-05-20 19:18:26 +02001441 patlen -= p - pat;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001442 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 }
1444
John Marriott8c85a2a2024-05-20 19:18:26 +02001445 show_search_stats = FALSE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001446 if ((options & SEARCH_ECHO) && messaging()
1447 && !msg_silent
1448 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 {
Bram Moolenaar984f0312019-05-24 13:11:47 +02001450 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001451 size_t off_len = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02001452 size_t plen;
1453 size_t msgbufsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001455 // Compute msg_row early.
1456 msg_start();
1457
Bram Moolenaar984f0312019-05-24 13:11:47 +02001458 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001459 if (!cmd_silent &&
1460 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001461 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001462 off_buf[off_len++] = dirc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001463 if (spats[0].off.end)
John Marriott8c85a2a2024-05-20 19:18:26 +02001464 off_buf[off_len++] = 'e';
Bram Moolenaar984f0312019-05-24 13:11:47 +02001465 else if (!spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001466 off_buf[off_len++] = 's';
Bram Moolenaar984f0312019-05-24 13:11:47 +02001467 if (spats[0].off.off > 0 || spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001468 off_buf[off_len++] = '+';
1469 off_buf[off_len] = NUL;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001470 if (spats[0].off.off != 0 || spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001471 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 +02001472 }
1473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 if (*searchstr == NUL)
John Marriott8c85a2a2024-05-20 19:18:26 +02001475 {
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001476 p = spats[0].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001477 plen = spats[0].patlen;
1478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 else
John Marriott8c85a2a2024-05-20 19:18:26 +02001480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 p = searchstr;
John Marriott8c85a2a2024-05-20 19:18:26 +02001482 plen = searchstrlen;
1483 }
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001484
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001485 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001486 {
1487 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001488 // search stat. Use all the space available, so that the
1489 // search state is right aligned. If there is not enough space
1490 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001491 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001492 // Use all the columns.
John Marriott8c85a2a2024-05-20 19:18:26 +02001493 msgbufsize = (int)(Rows - msg_row) * Columns - 1;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001494 else
1495 // Use up to 'showcmd' column.
John Marriott8c85a2a2024-05-20 19:18:26 +02001496 msgbufsize = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
1497 if (msgbufsize < plen + off_len + SEARCH_STAT_BUF_LEN + 3)
1498 msgbufsize = plen + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001499 }
1500 else
1501 // Reserve enough space for the search pattern + offset.
John Marriott8c85a2a2024-05-20 19:18:26 +02001502 msgbufsize = plen + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001503
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001504 vim_free(msgbuf);
John Marriott8c85a2a2024-05-20 19:18:26 +02001505 msgbuf = alloc(msgbufsize);
1506 if (msgbuf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001508 msgbuflen = 0;
1509 }
1510 else
1511 {
1512 vim_memset(msgbuf, ' ', msgbufsize);
1513 msgbuflen = msgbufsize - 1;
1514 msgbuf[msgbuflen] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001515 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1516 // empty for the search_stat feature.
1517 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001518 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001519 char_u *trunc;
1520
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001521 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001523 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1524 {
1525 // Use a space to draw the composing char on.
1526 msgbuf[1] = ' ';
John Marriott8c85a2a2024-05-20 19:18:26 +02001527 mch_memmove(msgbuf + 2, p, plen);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001528 }
1529 else
John Marriott8c85a2a2024-05-20 19:18:26 +02001530 mch_memmove(msgbuf + 1, p, plen);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001531 if (off_len > 0)
John Marriott8c85a2a2024-05-20 19:18:26 +02001532 mch_memmove(msgbuf + plen + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001534 trunc = msg_strtrunc(msgbuf, TRUE);
1535 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001537 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001538 msgbuf = trunc;
John Marriott8c85a2a2024-05-20 19:18:26 +02001539 msgbuflen = STRLEN(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001542#ifdef FEAT_RIGHTLEFT
1543 // The search pattern could be shown on the right in
1544 // rightleft mode, but the 'ruler' and 'showcmd' area use
1545 // it too, thus it would be blanked out again very soon.
1546 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001547 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1548 {
1549 char_u *r;
1550 size_t pat_len;
1551
1552 r = reverse_text(msgbuf);
1553 if (r != NULL)
1554 {
1555 vim_free(msgbuf);
1556 msgbuf = r;
Christian Brabandtcacb6692024-08-22 21:40:14 +02001557 msgbuflen = STRLEN(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001558 // move reversed text to beginning of buffer
1559 while (*r != NUL && *r == ' ')
1560 r++;
John Marriott8c85a2a2024-05-20 19:18:26 +02001561 pat_len = msgbuf + msgbuflen - r;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001562 mch_memmove(msgbuf, r, pat_len);
1563 // overwrite old text
1564 if ((size_t)(r - msgbuf) >= pat_len)
1565 vim_memset(r, ' ', pat_len);
1566 else
1567 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1568 }
1569 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001570#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001571 msg_outtrans(msgbuf);
1572 msg_clr_eos();
1573 msg_check();
1574
1575 gotocmdline(FALSE);
1576 out_flush();
1577 msg_nowait = TRUE; // don't wait for this message
1578 }
John Marriott8c85a2a2024-05-20 19:18:26 +02001579
1580 if (!shortmess(SHM_SEARCHCOUNT))
1581 show_search_stats = TRUE;
1582 } // msgbuf != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 }
1584
1585 /*
1586 * If there is a character offset, subtract it from the current
1587 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001588 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 * This is not done for a line offset, because then we would not be vi
1590 * compatible.
1591 */
Bram Moolenaared203462004-06-16 11:19:22 +00001592 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 if (spats[0].off.off > 0)
1595 {
1596 for (c = spats[0].off.off; c; --c)
1597 if (decl(&pos) == -1)
1598 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001599 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001601 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 pos.col = MAXCOL;
1603 }
1604 }
1605 else
1606 {
1607 for (c = spats[0].off.off; c; ++c)
1608 if (incl(&pos) == -1)
1609 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001610 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
1612 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1613 pos.col = 0;
1614 }
1615 }
1616 }
1617
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001618 /*
1619 * The actual search.
1620 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001621 c = searchit(curwin, curbuf, &pos, NULL,
1622 dirc == '/' ? FORWARD : BACKWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02001623 searchstr, searchstrlen, count, spats[0].off.end + (options &
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1625 + SEARCH_MSG + SEARCH_START
1626 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001627 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001630 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001631
1632 if (!shortmess(SHM_SEARCH)
1633 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1634 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001635 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 if (c == FAIL)
1638 {
1639 retval = 0;
1640 goto end_do_search;
1641 }
1642 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001643 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001645 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 /*
1648 * Add character and/or line offset
1649 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001650 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001652 pos_T org_pos = pos;
1653
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001654 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 {
1656 c = pos.lnum + spats[0].off.off;
1657 if (c < 1)
1658 pos.lnum = 1;
1659 else if (c > curbuf->b_ml.ml_line_count)
1660 pos.lnum = curbuf->b_ml.ml_line_count;
1661 else
1662 pos.lnum = c;
1663 pos.col = 0;
1664
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001665 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001667 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001669 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001670 c = spats[0].off.off;
1671 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001673 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (incl(&pos) == -1)
1675 break;
1676 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001677 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 else
1679 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001680 while (c++ < 0)
1681 if (decl(&pos) == -1)
1682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001685 if (!EQUAL_POS(pos, org_pos))
1686 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001689 // Show [1/15] if 'S' is not in 'shortmess'.
John Marriott8c85a2a2024-05-20 19:18:26 +02001690 if (show_search_stats)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001691 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
John Marriott8c85a2a2024-05-20 19:18:26 +02001692 show_top_bot_msg, msgbuf, msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001693 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001694#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001695 || (!(fdo_flags & FDO_SEARCH)
1696 && hasFolding(curwin->w_cursor.lnum,
1697 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001698#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001699 ),
1700 SEARCH_STAT_DEF_MAX_COUNT,
1701 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 /*
1704 * The search command can be followed by a ';' to do another search.
1705 * For example: "/pat/;/foo/+3;?bar"
1706 * This is like doing another search command, except:
1707 * - The remembered direction '/' or '?' is from the first search.
1708 * - When an error happens the cursor isn't moved at all.
1709 * Don't do this when called by get_address() (it handles ';' itself).
1710 */
1711 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1712 break;
1713
1714 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001715 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (dirc != '?' && dirc != '/')
1717 {
1718 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001719 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 goto end_do_search;
1721 }
1722 ++pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001723 --patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
1725
1726 if (options & SEARCH_MARK)
1727 setpcmark();
1728 curwin->w_cursor = pos;
1729 curwin->w_set_curswant = TRUE;
1730
1731end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001732 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 spats[0].off = old_off;
1734 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001735 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
1737 return retval;
1738}
1739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740/*
1741 * search_for_exact_line(buf, pos, dir, pat)
1742 *
1743 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001744 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001746 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 * Return OK for success, or FAIL if no line found.
1748 */
1749 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001750search_for_exact_line(
1751 buf_T *buf,
1752 pos_T *pos,
1753 int dir,
1754 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755{
1756 linenr_T start = 0;
1757 char_u *ptr;
1758 char_u *p;
1759
1760 if (buf->b_ml.ml_line_count == 0)
1761 return FAIL;
1762 for (;;)
1763 {
1764 pos->lnum += dir;
1765 if (pos->lnum < 1)
1766 {
1767 if (p_ws)
1768 {
1769 pos->lnum = buf->b_ml.ml_line_count;
1770 if (!shortmess(SHM_SEARCH))
1771 give_warning((char_u *)_(top_bot_msg), TRUE);
1772 }
1773 else
1774 {
1775 pos->lnum = 1;
1776 break;
1777 }
1778 }
1779 else if (pos->lnum > buf->b_ml.ml_line_count)
1780 {
1781 if (p_ws)
1782 {
1783 pos->lnum = 1;
1784 if (!shortmess(SHM_SEARCH))
1785 give_warning((char_u *)_(bot_top_msg), TRUE);
1786 }
1787 else
1788 {
1789 pos->lnum = 1;
1790 break;
1791 }
1792 }
1793 if (pos->lnum == start)
1794 break;
1795 if (start == 0)
1796 start = pos->lnum;
1797 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1798 p = skipwhite(ptr);
1799 pos->col = (colnr_T) (p - ptr);
1800
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001801 // when adding lines the matching line may be empty but it is not
1802 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001803 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
1805 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1806 return OK;
1807 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001808 else if (*p != NUL) // ignore empty lines
1809 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001810 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1811 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 return OK;
1813 }
1814 }
1815 return FAIL;
1816}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818/*
1819 * Character Searches
1820 */
1821
1822/*
1823 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1824 * position of the character, otherwise move to just before the char.
1825 * Do this "cap->count1" times.
1826 * Return FAIL or OK.
1827 */
1828 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001829searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001831 int c = cap->nchar; // char to search for
1832 int dir = cap->arg; // TRUE for searching forward
1833 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int col;
1835 char_u *p;
1836 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001837 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001839 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001841 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001843 *lastc = c;
1844 set_csearch_direction(dir);
1845 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001846 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 if (cap->ncharC1 != 0)
1848 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001849 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1850 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001852 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1853 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 }
1856 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001857 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
zeertzjqe5d91ba2023-05-14 17:39:18 +01001859 if (*lastc == NUL && lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001861 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 dir = -lastcdir;
1863 else
1864 dir = lastcdir;
1865 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001866 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001867 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001868
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001869 // Force a move of at least one char, so ";" and "," will move the
1870 // cursor, even if the cursor is right in front of char we are looking
1871 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001872 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001873 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 }
1875
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001876 if (dir == BACKWARD)
1877 cap->oap->inclusive = FALSE;
1878 else
1879 cap->oap->inclusive = TRUE;
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 p = ml_get_curline();
1882 col = curwin->w_cursor.col;
zeertzjq94b7c322024-03-12 21:50:32 +01001883 len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 while (count--)
1886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (has_mbyte)
1888 {
1889 for (;;)
1890 {
1891 if (dir > 0)
1892 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001893 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (col >= len)
1895 return FAIL;
1896 }
1897 else
1898 {
1899 if (col == 0)
1900 return FAIL;
1901 col -= (*mb_head_off)(p, p + col - 1) + 1;
1902 }
zeertzjqe5d91ba2023-05-14 17:39:18 +01001903 if (lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001905 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 break;
1907 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001908 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001909 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001910 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001911 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 }
1914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
1916 for (;;)
1917 {
1918 if ((col += dir) < 0 || col >= len)
1919 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001920 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001922 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924 }
1925 }
1926
1927 if (t_cmd)
1928 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001929 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (has_mbyte)
1932 {
1933 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001934 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001935 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001937 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 col -= (*mb_head_off)(p, p + col);
1939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941 curwin->w_cursor.col = col;
1942
1943 return OK;
1944}
1945
1946/*
1947 * "Other" Searches
1948 */
1949
1950/*
1951 * findmatch - find the matching paren or brace
1952 *
1953 * Improvement over vi: Braces inside quotes are ignored.
1954 */
1955 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001956findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958 return findmatchlimit(oap, initc, 0, 0);
1959}
1960
1961/*
1962 * Return TRUE if the character before "linep[col]" equals "ch".
1963 * Return FALSE if "col" is zero.
1964 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1965 * is NULL.
1966 * Handles multibyte string correctly.
1967 */
1968 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001969check_prevcol(
1970 char_u *linep,
1971 int col,
1972 int ch,
1973 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (col > 0 && has_mbyte)
1977 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (prevcol)
1979 *prevcol = col;
1980 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1981}
1982
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001983/*
1984 * Raw string start is found at linep[startpos.col - 1].
1985 * Return TRUE if the matching end can be found between startpos and endpos.
1986 */
1987 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001988find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001989{
1990 char_u *p;
1991 char_u *delim_copy;
1992 size_t delim_len;
1993 linenr_T lnum;
1994 int found = FALSE;
1995
1996 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1997 ;
1998 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001999 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002000 if (delim_copy == NULL)
2001 return FALSE;
2002 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
2003 {
2004 char_u *line = ml_get(lnum);
2005
2006 for (p = line + (lnum == startpos->lnum
2007 ? startpos->col + 1 : 0); *p; ++p)
2008 {
2009 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
2010 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02002011 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
2012 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002013 {
2014 found = TRUE;
2015 break;
2016 }
2017 }
2018 if (found)
2019 break;
2020 }
2021 vim_free(delim_copy);
2022 return found;
2023}
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01002026 * Check matchpairs option for "*initc".
2027 * If there is a match set "*initc" to the matching character and "*findc" to
2028 * the opposite character. Set "*backwards" to the direction.
2029 * When "switchit" is TRUE swap the direction.
2030 */
2031 static void
2032find_mps_values(
2033 int *initc,
2034 int *findc,
2035 int *backwards,
2036 int switchit)
2037{
2038 char_u *ptr;
2039
2040 ptr = curbuf->b_p_mps;
2041 while (*ptr != NUL)
2042 {
2043 if (has_mbyte)
2044 {
2045 char_u *prev;
2046
2047 if (mb_ptr2char(ptr) == *initc)
2048 {
2049 if (switchit)
2050 {
2051 *findc = *initc;
2052 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2053 *backwards = TRUE;
2054 }
2055 else
2056 {
2057 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2058 *backwards = FALSE;
2059 }
2060 return;
2061 }
2062 prev = ptr;
2063 ptr += mb_ptr2len(ptr) + 1;
2064 if (mb_ptr2char(ptr) == *initc)
2065 {
2066 if (switchit)
2067 {
2068 *findc = *initc;
2069 *initc = mb_ptr2char(prev);
2070 *backwards = FALSE;
2071 }
2072 else
2073 {
2074 *findc = mb_ptr2char(prev);
2075 *backwards = TRUE;
2076 }
2077 return;
2078 }
2079 ptr += mb_ptr2len(ptr);
2080 }
2081 else
2082 {
2083 if (*ptr == *initc)
2084 {
2085 if (switchit)
2086 {
2087 *backwards = TRUE;
2088 *findc = *initc;
2089 *initc = ptr[2];
2090 }
2091 else
2092 {
2093 *backwards = FALSE;
2094 *findc = ptr[2];
2095 }
2096 return;
2097 }
2098 ptr += 2;
2099 if (*ptr == *initc)
2100 {
2101 if (switchit)
2102 {
2103 *backwards = FALSE;
2104 *findc = *initc;
2105 *initc = ptr[-2];
2106 }
2107 else
2108 {
2109 *backwards = TRUE;
2110 *findc = ptr[-2];
2111 }
2112 return;
2113 }
2114 ++ptr;
2115 }
2116 if (*ptr == ',')
2117 ++ptr;
2118 }
2119}
2120
2121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002123 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2124 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 *
2126 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002127 * character at or after the cursor. Special values:
2128 * '*' look for C-style comment / *
2129 * '/' look for C-style comment / *, ignoring comment-end
2130 * '#' look for preprocessor directives
2131 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 *
2133 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2134 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2135 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2136 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002137 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002138 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002139 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002142findmatchlimit(
2143 oparg_T *oap,
2144 int initc,
2145 int flags,
2146 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002148 static pos_T pos; // current search position
2149 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002151 int count = 0; // cumulative number of braces
2152 int backwards = FALSE; // init for gcc
2153 int raw_string = FALSE; // search for raw string
2154 int inquote = FALSE; // TRUE when inside quotes
2155 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002157 int do_quotes; // check for quotes in current line
2158 int at_start; // do_quotes value at start position
2159 int hash_dir = 0; // Direction searched for # things
2160 int comment_dir = 0; // Direction searched for comments
2161 pos_T match_pos; // Where last slash-star was found
2162 int start_in_quotes; // start position is in quotes
2163 int traveled = 0; // how far we've searched so far
2164 int ignore_cend = FALSE; // ignore comment end
2165 int cpo_match; // vi compatible matching
2166 int cpo_bsl; // don't recognize backslashes
2167 int match_escaped = 0; // search for escaped match
2168 int dir; // Direction to search
2169 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002170 int lispcomm = FALSE; // inside of Lisp-style comment
2171 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
2173 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002174 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 linep = ml_get(pos.lnum);
2176
2177 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2178 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2179
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002180 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (flags & FM_BACKWARD)
2182 dir = BACKWARD;
2183 else if (flags & FM_FORWARD)
2184 dir = FORWARD;
2185 else
2186 dir = 0;
2187
2188 /*
2189 * if initc given, look in the table for the matching character
2190 * '/' and '*' are special cases: look for start or end of comment.
2191 * When '/' is used, we ignore running backwards into an star-slash, for
2192 * "[*" command, we just want to find any comment.
2193 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002194 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
2196 comment_dir = dir;
2197 if (initc == '/')
2198 ignore_cend = TRUE;
2199 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002200 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 initc = NUL;
2202 }
2203 else if (initc != '#' && initc != NUL)
2204 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002205 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002206 if (dir)
2207 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002208 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 return NULL;
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 else
2212 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002213 /*
2214 * Either initc is '#', or no initc was given and we need to look
2215 * under the cursor.
2216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (initc == '#')
2218 {
2219 hash_dir = dir;
2220 }
2221 else
2222 {
2223 /*
2224 * initc was not given, must look for something to match under
2225 * or near the cursor.
2226 * Only check for special things when 'cpo' doesn't have '%'.
2227 */
2228 if (!cpo_match)
2229 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002230 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 ptr = skipwhite(linep);
2232 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2233 {
2234 ptr = skipwhite(ptr + 1);
2235 if ( STRNCMP(ptr, "if", 2) == 0
2236 || STRNCMP(ptr, "endif", 5) == 0
2237 || STRNCMP(ptr, "el", 2) == 0)
2238 hash_dir = 1;
2239 }
2240
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002241 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 else if (linep[pos.col] == '/')
2243 {
2244 if (linep[pos.col + 1] == '*')
2245 {
2246 comment_dir = FORWARD;
2247 backwards = FALSE;
2248 pos.col++;
2249 }
2250 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2251 {
2252 comment_dir = BACKWARD;
2253 backwards = TRUE;
2254 pos.col--;
2255 }
2256 }
2257 else if (linep[pos.col] == '*')
2258 {
2259 if (linep[pos.col + 1] == '/')
2260 {
2261 comment_dir = BACKWARD;
2262 backwards = TRUE;
2263 }
2264 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2265 {
2266 comment_dir = FORWARD;
2267 backwards = FALSE;
2268 }
2269 }
2270 }
2271
2272 /*
2273 * If we are not on a comment or the # at the start of a line, then
2274 * look for brace anywhere on this line after the cursor.
2275 */
2276 if (!hash_dir && !comment_dir)
2277 {
2278 /*
2279 * Find the brace under or after the cursor.
2280 * If beyond the end of the line, use the last character in
2281 * the line.
2282 */
2283 if (linep[pos.col] == NUL && pos.col)
2284 --pos.col;
2285 for (;;)
2286 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002287 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 if (initc == NUL)
2289 break;
2290
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002291 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (findc)
2293 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002294 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296 if (!findc)
2297 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002298 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (!cpo_match && *skipwhite(linep) == '#')
2300 hash_dir = 1;
2301 else
2302 return NULL;
2303 }
2304 else if (!cpo_bsl)
2305 {
2306 int col, bslcnt = 0;
2307
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002308 // Set "match_escaped" if there are an odd number of
2309 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2311 bslcnt++;
2312 match_escaped = (bslcnt & 1);
2313 }
2314 }
2315 }
2316 if (hash_dir)
2317 {
2318 /*
2319 * Look for matching #if, #else, #elif, or #endif
2320 */
2321 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002322 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (initc != '#')
2324 {
2325 ptr = skipwhite(skipwhite(linep) + 1);
2326 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2327 hash_dir = 1;
2328 else if (STRNCMP(ptr, "endif", 5) == 0)
2329 hash_dir = -1;
2330 else
2331 return NULL;
2332 }
2333 pos.col = 0;
2334 while (!got_int)
2335 {
2336 if (hash_dir > 0)
2337 {
2338 if (pos.lnum == curbuf->b_ml.ml_line_count)
2339 break;
2340 }
2341 else if (pos.lnum == 1)
2342 break;
2343 pos.lnum += hash_dir;
2344 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002345 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 ptr = skipwhite(linep);
2347 if (*ptr != '#')
2348 continue;
2349 pos.col = (colnr_T) (ptr - linep);
2350 ptr = skipwhite(ptr + 1);
2351 if (hash_dir > 0)
2352 {
2353 if (STRNCMP(ptr, "if", 2) == 0)
2354 count++;
2355 else if (STRNCMP(ptr, "el", 2) == 0)
2356 {
2357 if (count == 0)
2358 return &pos;
2359 }
2360 else if (STRNCMP(ptr, "endif", 5) == 0)
2361 {
2362 if (count == 0)
2363 return &pos;
2364 count--;
2365 }
2366 }
2367 else
2368 {
2369 if (STRNCMP(ptr, "if", 2) == 0)
2370 {
2371 if (count == 0)
2372 return &pos;
2373 count--;
2374 }
2375 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2376 {
2377 if (count == 0)
2378 return &pos;
2379 }
2380 else if (STRNCMP(ptr, "endif", 5) == 0)
2381 count++;
2382 }
2383 }
2384 return NULL;
2385 }
2386 }
2387
2388#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002389 // This is just guessing: when 'rightleft' is set, search for a matching
2390 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2392 backwards = !backwards;
2393#endif
2394
2395 do_quotes = -1;
2396 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002397 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002398
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002399 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002400 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002402 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002403 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 while (!got_int)
2406 {
2407 /*
2408 * Go to the next position, forward or backward. We could use
2409 * inc() and dec() here, but that is much slower
2410 */
2411 if (backwards)
2412 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002413 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002414 if (lispcomm && pos.col < (colnr_T)comment_col)
2415 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002416 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002418 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 break;
2420 --pos.lnum;
2421
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002422 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 break;
2424
2425 linep = ml_get(pos.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01002426 pos.col = ml_get_len(pos.lnum); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 do_quotes = -1;
2428 line_breakcheck();
2429
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002430 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002431 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002433 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002434 if (lisp && comment_col != MAXCOL)
2435 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 else
2438 {
2439 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (has_mbyte)
2441 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002444 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002446 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002447 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002448 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002449 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002450 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002452 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002453 // line is exhausted and comment with it,
2454 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002455 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 break;
2457 ++pos.lnum;
2458
2459 if (maxtravel && traveled++ > maxtravel)
2460 break;
2461
2462 linep = ml_get(pos.lnum);
2463 pos.col = 0;
2464 do_quotes = -1;
2465 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002466 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002467 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469 else
2470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002472 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 ++pos.col;
2475 }
2476 }
2477
2478 /*
2479 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2480 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002481 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2482 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002484 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002486 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
2488
2489 if (comment_dir)
2490 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002491 // Note: comments do not nest, and we ignore quotes in them
2492 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (comment_dir == FORWARD)
2494 {
2495 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2496 {
2497 pos.col++;
2498 return &pos;
2499 }
2500 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002501 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 /*
2504 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002505 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
2507 if (pos.col == 0)
2508 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002509 else if (raw_string)
2510 {
2511 if (linep[pos.col - 1] == 'R'
2512 && linep[pos.col] == '"'
2513 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2514 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002515 // Possible start of raw string. Now that we have the
2516 // delimiter we can check if it ends before where we
2517 // started searching, or before the previously found
2518 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002519 if (!find_rawstring_end(linep, &pos,
2520 count > 0 ? &match_pos : &curwin->w_cursor))
2521 {
2522 count++;
2523 match_pos = pos;
2524 match_pos.col--;
2525 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002526 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002527 }
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 else if ( linep[pos.col - 1] == '/'
2530 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002531 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 && (int)pos.col < comment_col)
2533 {
2534 count++;
2535 match_pos = pos;
2536 match_pos.col--;
2537 }
2538 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2539 {
2540 if (count > 0)
2541 pos = match_pos;
2542 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2543 && (int)pos.col <= comment_col)
2544 pos.col -= 2;
2545 else if (ignore_cend)
2546 continue;
2547 else
2548 return NULL;
2549 return &pos;
2550 }
2551 }
2552 continue;
2553 }
2554
2555 /*
2556 * If smart matching ('cpoptions' does not contain '%'), braces inside
2557 * of quotes are ignored, but only if there is an even number of
2558 * quotes in the line.
2559 */
2560 if (cpo_match)
2561 do_quotes = 0;
2562 else if (do_quotes == -1)
2563 {
2564 /*
2565 * Count the number of quotes in the line, skipping \" and '"'.
2566 * Watch out for "\\".
2567 */
2568 at_start = do_quotes;
2569 for (ptr = linep; *ptr; ++ptr)
2570 {
2571 if (ptr == linep + pos.col + backwards)
2572 at_start = (do_quotes & 1);
2573 if (*ptr == '"'
2574 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2575 ++do_quotes;
2576 if (*ptr == '\\' && ptr[1] != NUL)
2577 ++ptr;
2578 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002579 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
2581 /*
2582 * If we find an uneven count, check current line and previous
2583 * one for a '\' at the end.
2584 */
2585 if (!do_quotes)
2586 {
2587 inquote = FALSE;
2588 if (ptr[-1] == '\\')
2589 {
2590 do_quotes = 1;
2591 if (start_in_quotes == MAYBE)
2592 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002593 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 inquote = TRUE;
2595 start_in_quotes = TRUE;
2596 }
2597 else if (backwards)
2598 inquote = TRUE;
2599 }
2600 if (pos.lnum > 1)
2601 {
2602 ptr = ml_get(pos.lnum - 1);
zeertzjq94b7c322024-03-12 21:50:32 +01002603 if (*ptr && *(ptr + ml_get_len(pos.lnum - 1) - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
2605 do_quotes = 1;
2606 if (start_in_quotes == MAYBE)
2607 {
2608 inquote = at_start;
2609 if (inquote)
2610 start_in_quotes = TRUE;
2611 }
2612 else if (!backwards)
2613 inquote = TRUE;
2614 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002615
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002616 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002617 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
2619 }
2620 }
2621 if (start_in_quotes == MAYBE)
2622 start_in_quotes = FALSE;
2623
2624 /*
2625 * If 'smartmatch' is set:
2626 * Things inside quotes are ignored by setting 'inquote'. If we
2627 * find a quote without a preceding '\' invert 'inquote'. At the
2628 * end of a line not ending in '\' we reset 'inquote'.
2629 *
2630 * In lines with an uneven number of quotes (without preceding '\')
2631 * we do not know which part to ignore. Therefore we only set
2632 * inquote if the number of quotes in a line is even, unless this
2633 * line or the previous one ends in a '\'. Complicated, isn't it?
2634 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002635 c = PTR2CHAR(linep + pos.col);
2636 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002639 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2641 {
2642 inquote = FALSE;
2643 start_in_quotes = FALSE;
2644 }
2645 break;
2646
2647 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002648 // a quote that is preceded with an odd number of backslashes is
2649 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (do_quotes)
2651 {
2652 int col;
2653
2654 for (col = pos.col - 1; col >= 0; --col)
2655 if (linep[col] != '\\')
2656 break;
2657 if ((((int)pos.col - 1 - col) & 1) == 0)
2658 {
2659 inquote = !inquote;
2660 start_in_quotes = FALSE;
2661 }
2662 }
2663 break;
2664
2665 /*
2666 * If smart matching ('cpoptions' does not contain '%'):
2667 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2668 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2669 * skipped, there is never a brace in them.
2670 * Ignore this when finding matches for `'.
2671 */
2672 case '\'':
2673 if (!cpo_match && initc != '\'' && findc != '\'')
2674 {
2675 if (backwards)
2676 {
2677 if (pos.col > 1)
2678 {
2679 if (linep[pos.col - 2] == '\'')
2680 {
2681 pos.col -= 2;
2682 break;
2683 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002684 else if (linep[pos.col - 2] == '\\'
2685 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 pos.col -= 3;
2688 break;
2689 }
2690 }
2691 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002692 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002694 if (linep[pos.col + 1] == '\\'
2695 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 pos.col += 3;
2698 break;
2699 }
2700 else if (linep[pos.col + 2] == '\'')
2701 {
2702 pos.col += 2;
2703 break;
2704 }
2705 }
2706 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002707 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
2709 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002710 /*
2711 * For Lisp skip over backslashed (), {} and [].
2712 * (actually, we skip #\( et al)
2713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002715 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002716 && pos.col > 1
2717 && check_prevcol(linep, pos.col, '\\', NULL)
2718 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002721 // Check for match outside of quotes, and inside of
2722 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if ((!inquote || start_in_quotes == TRUE)
2724 && (c == initc || c == findc))
2725 {
2726 int col, bslcnt = 0;
2727
2728 if (!cpo_bsl)
2729 {
2730 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2731 bslcnt++;
2732 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002733 // Only accept a match when 'M' is in 'cpo' or when escaping
2734 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2736 {
2737 if (c == initc)
2738 count++;
2739 else
2740 {
2741 if (count == 0)
2742 return &pos;
2743 count--;
2744 }
2745 }
2746 }
2747 }
2748 }
2749
2750 if (comment_dir == BACKWARD && count > 0)
2751 {
2752 pos = match_pos;
2753 return &pos;
2754 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002755 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756}
2757
2758/*
2759 * Check if line[] contains a / / comment.
2760 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002762 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002763check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 char_u *p;
2766
2767 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002768 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002769 if (curbuf->b_p_lisp)
2770 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002771 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002772 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002773 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002774
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002775 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002776 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002777 {
2778 if (*p == '"')
2779 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002780 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002781 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002782 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002783 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002784 }
2785 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002786 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002787 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002788 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002789 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002790 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002791 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2792 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002793 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002794 ++p;
2795 }
2796 }
2797 else
2798 p = NULL;
2799 }
2800 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002801 while ((p = vim_strchr(p, '/')) != NULL)
2802 {
2803 // Accept a double /, unless it's preceded with * and followed by
2804 // *, because * / / * is an end and start of a C comment. Only
2805 // accept the position if it is not inside a string.
2806 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002807 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002808 break;
2809 ++p;
2810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811
2812 if (p == NULL)
2813 return MAXCOL;
2814 return (int)(p - line);
2815}
2816
2817/*
2818 * Move cursor briefly to character matching the one under the cursor.
2819 * Used for Insert mode and "r" command.
2820 * Show the match only if it is visible on the screen.
2821 * If there isn't a match, then beep.
2822 */
2823 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002824showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002825 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 pos_T *lpos, save_cursor;
2828 pos_T mpos;
2829 colnr_T vcol;
2830 long save_so;
2831 long save_siso;
2832#ifdef CURSOR_SHAPE
2833 int save_state;
2834#endif
2835 colnr_T save_dollar_vcol;
2836 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002837 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2838 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
2840 /*
2841 * Only show match for chars in the 'matchpairs' option.
2842 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002843 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002844 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 {
2846#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002847 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002848 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002849#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002850 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002851 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852#ifdef FEAT_RIGHTLEFT
2853 && !(curwin->w_p_rl ^ p_ri)
2854#endif
2855 )
2856 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002857 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002858 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 return;
2860 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002861 if (*p == NUL)
2862 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002864 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002866 vim_beep(BO_MATCH);
2867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002869
2870 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2871 return;
2872
2873 if (!curwin->w_p_wrap)
2874 getvcol(curwin, lpos, NULL, &vcol, NULL);
2875
2876 int col_visible = (curwin->w_p_wrap
2877 || (vcol >= curwin->w_leftcol
2878 && vcol < curwin->w_leftcol + curwin->w_width));
2879 if (!col_visible)
2880 return;
2881
2882 mpos = *lpos; // save the pos, update_screen() may change it
2883 save_cursor = curwin->w_cursor;
2884 save_so = *so;
2885 save_siso = *siso;
2886 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2887 // stop displaying the "$".
2888 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2889 dollar_vcol = -1;
2890 ++curwin->w_virtcol; // do display ')' just before "$"
2891 update_screen(UPD_VALID); // show the new char first
2892
2893 save_dollar_vcol = dollar_vcol;
2894#ifdef CURSOR_SHAPE
2895 save_state = State;
2896 State = MODE_SHOWMATCH;
2897 ui_cursor_shape(); // may show different cursor shape
2898#endif
2899 curwin->w_cursor = mpos; // move to matching char
2900 *so = 0; // don't use 'scrolloff' here
2901 *siso = 0; // don't use 'sidescrolloff' here
2902 showruler(FALSE);
2903 setcursor();
2904 cursor_on(); // make sure that the cursor is shown
2905 out_flush_cursor(TRUE, FALSE);
2906
2907 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2908 // which resets it if the matching position is in a previous line
2909 // and has a higher column number.
2910 dollar_vcol = save_dollar_vcol;
2911
2912 /*
2913 * brief pause, unless 'm' is present in 'cpo' and a character is
2914 * available.
2915 */
2916 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2917 ui_delay(p_mat * 100L + 8, TRUE);
2918 else if (!char_avail())
2919 ui_delay(p_mat * 100L + 9, FALSE);
2920 curwin->w_cursor = save_cursor; // restore cursor position
2921 *so = save_so;
2922 *siso = save_siso;
2923#ifdef CURSOR_SHAPE
2924 State = save_state;
2925 ui_cursor_shape(); // may show different cursor shape
2926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927}
2928
2929/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002930 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002931 * If move is TRUE, check from the beginning of the buffer, else from position
2932 * "cur".
2933 * "direction" is FORWARD or BACKWARD.
2934 * Returns TRUE, FALSE or -1 for failure.
2935 */
2936 static int
John Marriott8c85a2a2024-05-20 19:18:26 +02002937is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002938{
2939 regmmatch_T regmatch;
2940 int nmatched = 0;
2941 int result = -1;
2942 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002943 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002944 int flag = 0;
2945
2946 if (pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02002947 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002948 pattern = spats[last_idx].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02002949 patternlen = spats[last_idx].patlen;
2950 }
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002951
John Marriott8c85a2a2024-05-20 19:18:26 +02002952 if (search_regcomp(pattern, patternlen, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002953 SEARCH_KEEP, &regmatch) == FAIL)
2954 return -1;
2955
2956 // init startcol correctly
2957 regmatch.startpos[0].col = -1;
2958 // move to match
2959 if (move)
2960 {
2961 CLEAR_POS(&pos);
2962 }
2963 else
2964 {
2965 pos = *cur;
2966 // accept a match at the cursor position
2967 flag = SEARCH_START;
2968 }
2969
John Marriott8c85a2a2024-05-20 19:18:26 +02002970 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, patternlen, 1,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002971 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2972 {
2973 // Zero-width pattern should match somewhere, then we can check if
2974 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002975 do
2976 {
2977 regmatch.startpos[0].col++;
2978 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002979 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002980 if (nmatched != 0)
2981 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002982 } while (regmatch.regprog != NULL
2983 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002984 : regmatch.startpos[0].col > pos.col);
2985
Bram Moolenaar53989552019-12-23 22:59:18 +01002986 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002987 {
2988 result = (nmatched != 0
2989 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2990 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2991 }
2992 }
2993
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002994 vim_regfree(regmatch.regprog);
2995 return result;
2996}
2997
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002998
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002999/*
3000 * Find next search match under cursor, cursor at end.
3001 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003002 */
3003 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003004current_search(
3005 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003006 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003007{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003008 pos_T start_pos; // start position of the pattern match
3009 pos_T end_pos; // end position of the pattern match
3010 pos_T orig_pos; // position of the cursor at beginning
3011 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003012 int i;
3013 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003014 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003015 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003016 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02003017 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003018 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003019 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003020
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003021 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003022 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003023 dec_cursor();
3024
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003025 // When searching forward and the cursor is at the start of the Visual
3026 // area, skip the first search backward, otherwise it doesn't move.
3027 skip_first_backward = forward && VIsual_active
3028 && LT_POS(curwin->w_cursor, VIsual);
3029
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003030 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003031 if (VIsual_active)
3032 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003033 if (forward)
3034 incl(&pos);
3035 else
3036 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003037 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003038
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003039 // Is the pattern is zero-width?, this time, don't care about the direction
John Marriott8c85a2a2024-05-20 19:18:26 +02003040 zero_width = is_zero_width(spats[last_idx].pat, spats[last_idx].patlen,
3041 TRUE, &curwin->w_cursor, FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003042 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003043 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003044
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003045 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003046 * The trick is to first search backwards and then search forward again,
3047 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003048 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003049 */
3050 for (i = 0; i < 2; i++)
3051 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003052 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003053 {
3054 if (i == 0 && skip_first_backward)
3055 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003056 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003057 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003058 else
3059 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003060
3061 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003062 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003063 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003064 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003065
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003066 // wrapping should not occur in the first round
3067 if (i == 0)
3068 p_ws = FALSE;
3069
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003070 result = searchit(curwin, curbuf, &pos, &end_pos,
3071 (dir ? FORWARD : BACKWARD),
John Marriott8c85a2a2024-05-20 19:18:26 +02003072 spats[last_idx].pat, spats[last_idx].patlen, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003073 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003074
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003075 p_ws = old_p_ws;
3076
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003077 // First search may fail, but then start searching from the
3078 // beginning of the file (cursor might be on the search match)
3079 // except when Visual mode is active, so that extending the visual
3080 // selection works.
3081 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003082 {
3083 curwin->w_cursor = orig_pos;
3084 if (VIsual_active)
3085 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003086 return FAIL;
3087 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003088 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003089 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003090 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003091 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003092 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003093 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003094 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003095 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003096 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003097 // try again from end of buffer
3098 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003099 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003100 pos.col = ml_get_len(curwin->w_buffer->b_ml.ml_line_count);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003101 }
3102 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003103 }
3104
3105 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003106
3107 if (!VIsual_active)
3108 VIsual = start_pos;
3109
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003110 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003111 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003112 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003113 {
3114 if (skip_first_backward)
3115 // put the cursor on the start of the match
3116 curwin->w_cursor = pos;
3117 else
3118 // put the cursor on last character of match
3119 dec_cursor();
3120 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003121 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003122 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003123 VIsual_active = TRUE;
3124 VIsual_mode = 'v';
3125
Bram Moolenaarb7633612019-02-10 21:48:25 +01003126 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003127 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003128 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003129 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3130 inc_cursor();
3131 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3132 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003133 }
3134
3135#ifdef FEAT_FOLDING
3136 if (fdo_flags & FDO_SEARCH && KeyTyped)
3137 foldOpenCursor();
3138#endif
3139
3140 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003141 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003142#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003143 // Make sure the clipboard gets updated. Needed because start and
3144 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003145 clip_star.vmode = NUL;
3146#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003147 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003148 showmode();
3149
3150 return OK;
3151}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003152
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153/*
3154 * return TRUE if line 'lnum' is empty or has white chars only.
3155 */
3156 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003157linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158{
3159 char_u *p;
3160
3161 p = skipwhite(ml_get(lnum));
3162 return (*p == NUL);
3163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003165/*
3166 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003167 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003168 */
3169 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003170cmdline_search_stat(
3171 int dirc,
3172 pos_T *pos,
3173 pos_T *cursor_pos,
3174 int show_top_bot_msg,
3175 char_u *msgbuf,
John Marriott8c85a2a2024-05-20 19:18:26 +02003176 size_t msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003177 int recompute,
3178 int maxcount,
3179 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003180{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003181 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003182
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003183 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3184 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003185 if (stat.cur <= 0)
3186 return;
3187
3188 char t[SEARCH_STAT_BUF_LEN];
3189 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003190
3191#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003192 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3193 {
3194 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003195 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003196 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003197 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003198 maxcount, maxcount);
3199 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003200 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003201 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003202 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003203 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003204 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003205 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003206 else
3207#endif
3208 {
3209 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003210 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003211 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003212 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003213 maxcount, maxcount);
3214 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003215 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003216 stat.cur, maxcount);
3217 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003218 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003219 stat.cur, stat.cnt);
3220 }
3221
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003222 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3223 {
3224 mch_memmove(t + 2, t, len);
3225 t[0] = 'W';
3226 t[1] = ' ';
3227 len += 2;
3228 }
3229
John Marriott8c85a2a2024-05-20 19:18:26 +02003230 if (len > msgbuflen)
3231 len = msgbuflen;
3232 mch_memmove(msgbuf + msgbuflen - len, t, len);
zeertzjqa7d36b72023-01-31 21:13:38 +00003233
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003234 if (dirc == '?' && stat.cur == maxcount + 1)
3235 stat.cur = -1;
3236
3237 // keep the message even after redraw, but don't put in history
3238 msg_hist_off = TRUE;
3239 give_warning(msgbuf, FALSE);
3240 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003241}
3242
3243/*
3244 * Add the search count information to "stat".
3245 * "stat" must not be NULL.
3246 * When "recompute" is TRUE always recompute the numbers.
3247 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3248 * dirc == '/': find the next match
3249 * dirc == '?': find the previous match
3250 */
3251 static void
3252update_search_stat(
3253 int dirc,
3254 pos_T *pos,
3255 pos_T *cursor_pos,
3256 searchstat_T *stat,
3257 int recompute,
3258 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003259 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003260{
3261 int save_ws = p_ws;
3262 int wraparound = FALSE;
3263 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003264 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003265 static int cur = 0;
3266 static int cnt = 0;
3267 static int exact_match = FALSE;
3268 static int incomplete = 0;
3269 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3270 static int chgtick = 0;
3271 static char_u *lastpat = NULL;
John Marriottb79fa3d2025-02-21 19:59:56 +01003272 static size_t lastpatlen = 0;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003273 static buf_T *lbuf = NULL;
3274#ifdef FEAT_RELTIME
3275 proftime_T start;
3276#endif
3277
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003278 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003279
3280 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3281 {
3282 stat->cur = cur;
3283 stat->cnt = cnt;
3284 stat->exact_match = exact_match;
3285 stat->incomplete = incomplete;
3286 stat->last_maxcount = last_maxcount;
3287 return;
3288 }
3289 last_maxcount = maxcount;
3290
3291 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3292 || (dirc == '/' && LT_POS(p, lastpos)));
3293
3294 // If anything relevant changed the count has to be recomputed.
3295 // MB_STRNICMP ignores case, but we should not ignore case.
3296 // Unfortunately, there is no MB_STRNICMP function.
3297 // XXX: above comment should be "no MB_STRCMP function" ?
3298 if (!(chgtick == CHANGEDTICK(curbuf)
John Marriottb79fa3d2025-02-21 19:59:56 +01003299 && (lastpat != NULL
3300 && MB_STRNICMP(lastpat, spats[last_idx].pat, lastpatlen) == 0
3301 && lastpatlen == spats[last_idx].patlen
3302 )
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003303 && EQUAL_POS(lastpos, *cursor_pos)
3304 && lbuf == curbuf) || wraparound || cur < 0
3305 || (maxcount > 0 && cur > maxcount) || recompute)
3306 {
3307 cur = 0;
3308 cnt = 0;
3309 exact_match = FALSE;
3310 incomplete = 0;
3311 CLEAR_POS(&lastpos);
3312 lbuf = curbuf;
3313 }
3314
Christian Brabandt34a6a362023-05-06 19:20:20 +01003315 // when searching backwards and having jumped to the first occurrence,
3316 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003317 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003318 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003319 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3320 else
3321 {
3322 int done_search = FALSE;
3323 pos_T endpos = {0, 0, 0};
3324
3325 p_ws = FALSE;
3326#ifdef FEAT_RELTIME
3327 if (timeout > 0)
3328 profile_setlimit(timeout, &start);
3329#endif
3330 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003331 FORWARD, NULL, 0, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003332 {
3333 done_search = TRUE;
3334#ifdef FEAT_RELTIME
3335 // Stop after passing the time limit.
3336 if (timeout > 0 && profile_passed_limit(&start))
3337 {
3338 incomplete = 1;
3339 break;
3340 }
3341#endif
3342 cnt++;
3343 if (LTOREQ_POS(lastpos, p))
3344 {
3345 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003346 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003347 exact_match = TRUE;
3348 }
3349 fast_breakcheck();
3350 if (maxcount > 0 && cnt > maxcount)
3351 {
3352 incomplete = 2; // max count exceeded
3353 break;
3354 }
3355 }
3356 if (got_int)
3357 cur = -1; // abort
3358 if (done_search)
3359 {
3360 vim_free(lastpat);
John Marriottb79fa3d2025-02-21 19:59:56 +01003361 lastpat = vim_strnsave(spats[last_idx].pat, spats[last_idx].patlen);
3362 if (lastpat == NULL)
3363 lastpatlen = 0;
3364 else
3365 lastpatlen = spats[last_idx].patlen;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003366 chgtick = CHANGEDTICK(curbuf);
3367 lbuf = curbuf;
3368 lastpos = p;
3369 }
3370 }
3371 stat->cur = cur;
3372 stat->cnt = cnt;
3373 stat->exact_match = exact_match;
3374 stat->incomplete = incomplete;
3375 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003376 p_ws = save_ws;
3377}
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003380
3381/*
3382 * Get line "lnum" and copy it into "buf[LSIZE]".
3383 * The copy is made because the regexp may make the line invalid when using a
3384 * mark.
3385 */
3386 static char_u *
3387get_line_and_copy(linenr_T lnum, char_u *buf)
3388{
3389 char_u *line = ml_get(lnum);
3390
3391 vim_strncpy(buf, line, LSIZE - 1);
3392 return buf;
3393}
3394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395/*
3396 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003397 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003400find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003401 char_u *ptr, // pointer to search pattern
3402 int dir UNUSED, // direction of expansion
3403 int len, // length of search pattern
3404 int whole, // match whole words only
3405 int skip_comments, // don't match inside comments
3406 int type, // Type of search; are we looking for a type?
3407 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003408 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003409 int action, // What to do when we find it
3410 linenr_T start_lnum, // first line to start searching
Colin Kennedy21570352024-03-03 16:16:47 +01003411 linenr_T end_lnum, // last line for searching
3412 int forceit) // If true, always switch to the found path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003414 SearchedFile *files; // Stack of included files
3415 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 int max_path_depth = 50;
3417 long match_count = 1;
3418
3419 char_u *pat;
3420 char_u *new_fname;
3421 char_u *curr_fname = curbuf->b_fname;
3422 char_u *prev_fname = NULL;
3423 linenr_T lnum;
3424 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003425 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 int old_files;
3427 int already_searched;
3428 char_u *file_line;
3429 char_u *line;
3430 char_u *p;
3431 char_u save_char;
3432 int define_matched;
3433 regmatch_T regmatch;
3434 regmatch_T incl_regmatch;
3435 regmatch_T def_regmatch;
3436 int matched = FALSE;
3437 int did_show = FALSE;
3438 int found = FALSE;
3439 int i;
3440 char_u *already = NULL;
3441 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003442 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003443#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 win_T *curwin_save = NULL;
3445#endif
3446
3447 regmatch.regprog = NULL;
3448 incl_regmatch.regprog = NULL;
3449 def_regmatch.regprog = NULL;
3450
3451 file_line = alloc(LSIZE);
3452 if (file_line == NULL)
3453 return;
3454
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003456 // when CONT_SOL is set compare "ptr" with the beginning of the
3457 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3458 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 pat = alloc(len + 5);
3461 if (pat == NULL)
3462 goto fpip_end;
John Marriott8c85a2a2024-05-20 19:18:26 +02003463 vim_snprintf((char *)pat, len + 5, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003464 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003466 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 vim_free(pat);
3468 if (regmatch.regprog == NULL)
3469 goto fpip_end;
3470 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003471 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3472 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003474 incl_regmatch.regprog = vim_regcomp(inc_opt,
3475 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (incl_regmatch.regprog == NULL)
3477 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003478 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 }
3480 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3481 {
John Marriott8c85a2a2024-05-20 19:18:26 +02003482 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL ? p_def : curbuf->b_p_def,
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003483 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (def_regmatch.regprog == NULL)
3485 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003486 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003488 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (files == NULL)
3490 goto fpip_end;
3491 old_files = max_path_depth;
3492 depth = depth_displayed = -1;
3493
3494 lnum = start_lnum;
3495 if (end_lnum > curbuf->b_ml.ml_line_count)
3496 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003497 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003499 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
3501 for (;;)
3502 {
3503 if (incl_regmatch.regprog != NULL
3504 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3505 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003506 char_u *p_fname = (curr_fname == curbuf->b_fname)
3507 ? curbuf->b_ffname : curr_fname;
3508
3509 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003510 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003511 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003512 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003513 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3514 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003515 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003516 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003517 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 already_searched = FALSE;
3519 if (new_fname != NULL)
3520 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003521 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 for (i = 0;; i++)
3523 {
3524 if (i == depth + 1)
3525 i = old_files;
3526 if (i == max_path_depth)
3527 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003528 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3529 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003531 if (type != CHECK_PATH
3532 && action == ACTION_SHOW_ALL
3533 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003535 msg_putchar('\n'); // cursor below last one
3536 if (!got_int) // don't display if 'q'
3537 // typed at "--more--"
3538 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003541 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 prev_fname = NULL;
3543 }
3544 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003545 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 already_searched = TRUE;
3547 break;
3548 }
3549 }
3550 }
3551
3552 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3553 || (new_fname == NULL && !already_searched)))
3554 {
3555 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003556 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 else
3558 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003559 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003560 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003562 msg_puts_title(_("not found "));
3563 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 did_show = TRUE;
3566 while (depth_displayed < depth && !got_int)
3567 {
3568 ++depth_displayed;
3569 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003570 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003572 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003574 if (!got_int) // don't display if 'q' typed
3575 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
3577 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003578 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (new_fname != NULL)
3580 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003581 // using "new_fname" is more reliable, e.g., when
3582 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003583 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585 else
3586 {
3587 /*
3588 * Isolate the file name.
3589 * Include the surrounding "" or <> if present.
3590 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003591 if (inc_opt != NULL
3592 && strstr((char *)inc_opt, "\\zs") != NULL)
3593 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003594 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003595 p = incl_regmatch.startp[0];
3596 i = (int)(incl_regmatch.endp[0]
3597 - incl_regmatch.startp[0]);
3598 }
3599 else
3600 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003601 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003602 for (p = incl_regmatch.endp[0];
3603 *p && !vim_isfilec(*p); p++)
3604 ;
3605 for (i = 0; vim_isfilec(p[i]); i++)
3606 ;
3607 }
3608
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 if (i == 0)
3610 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003611 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003613 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003615 // Avoid checking before the start of the line, can
3616 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003617 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 if (p[-1] == '"' || p[-1] == '<')
3620 {
3621 --p;
3622 ++i;
3623 }
3624 if (p[i] == '"' || p[i] == '>')
3625 ++i;
3626 }
3627 save_char = p[i];
3628 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003629 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 p[i] = save_char;
3631 }
3632
3633 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3634 {
3635 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003636 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003638 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003641 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643
3644 if (new_fname != NULL)
3645 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003646 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 if (depth + 1 == old_files)
3648 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003649 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (bigger != NULL)
3651 {
3652 for (i = 0; i <= depth; i++)
3653 bigger[i] = files[i];
3654 for (i = depth + 1; i < old_files + max_path_depth; i++)
3655 {
3656 bigger[i].fp = NULL;
3657 bigger[i].name = NULL;
3658 bigger[i].lnum = 0;
3659 bigger[i].matched = FALSE;
3660 }
3661 for (i = old_files; i < max_path_depth; i++)
3662 bigger[i + max_path_depth] = files[i];
3663 old_files += max_path_depth;
3664 max_path_depth *= 2;
3665 vim_free(files);
3666 files = bigger;
3667 }
3668 }
3669 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3670 == NULL)
3671 vim_free(new_fname);
3672 else
3673 {
3674 if (++depth == old_files)
3675 {
3676 /*
3677 * lalloc() for 'bigger' must have failed above. We
3678 * will forget one of our already visited files now.
3679 */
3680 vim_free(files[old_files].name);
3681 ++old_files;
3682 }
3683 files[depth].name = curr_fname = new_fname;
3684 files[depth].lnum = 0;
3685 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (action == ACTION_EXPAND)
3687 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003688 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003689 vim_snprintf((char*)IObuff, IOSIZE,
3690 _("Scanning included file: %s"),
3691 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003692 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003694 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003695 {
3696 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003697 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003698 (char *)new_fname);
3699 verbose_leave();
3700 }
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 }
3704 }
3705 else
3706 {
3707 /*
3708 * Check if the line is a define (type == FIND_DEFINE)
3709 */
3710 p = line;
3711search_line:
3712 define_matched = FALSE;
3713 if (def_regmatch.regprog != NULL
3714 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3715 {
3716 /*
3717 * Pattern must be first identifier after 'define', so skip
3718 * to that position before checking for match of pattern. Also
3719 * don't let it match beyond the end of this identifier.
3720 */
3721 p = def_regmatch.endp[0];
3722 while (*p && !vim_iswordc(*p))
3723 p++;
3724 define_matched = TRUE;
3725 }
3726
3727 /*
3728 * Look for a match. Don't do this if we are looking for a
3729 * define and this line didn't match define_prog above.
3730 */
3731 if (def_regmatch.regprog == NULL || define_matched)
3732 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003733 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003735 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 startp = skipwhite(p);
3737 if (p_ic)
3738 matched = !MB_STRNICMP(startp, ptr, len);
3739 else
3740 matched = !STRNCMP(startp, ptr, len);
3741 if (matched && define_matched && whole
3742 && vim_iswordc(startp[len]))
3743 matched = FALSE;
3744 }
3745 else if (regmatch.regprog != NULL
3746 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3747 {
3748 matched = TRUE;
3749 startp = regmatch.startp[0];
3750 /*
3751 * Check if the line is not a comment line (unless we are
3752 * looking for a define). A line starting with "# define"
3753 * is not considered to be a comment line.
3754 */
3755 if (!define_matched && skip_comments)
3756 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 if ((*line != '#' ||
3758 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003759 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 matched = FALSE;
3761
3762 /*
3763 * Also check for a "/ *" or "/ /" before the match.
3764 * Skips lines like "int backwards; / * normal index
3765 * * /" when looking for "normal".
3766 * Note: Doesn't skip "/ *" in comments.
3767 */
3768 p = skipwhite(line);
3769 if (matched
3770 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 for (p = line; *p && p < startp; ++p)
3772 {
3773 if (matched
3774 && p[0] == '/'
3775 && (p[1] == '*' || p[1] == '/'))
3776 {
3777 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003778 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (p[1] == '/')
3780 break;
3781 ++p;
3782 }
3783 else if (!matched && p[0] == '*' && p[1] == '/')
3784 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003785 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 matched = TRUE;
3787 ++p;
3788 }
3789 }
3790 }
3791 }
3792 }
3793 }
3794 if (matched)
3795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (action == ACTION_EXPAND)
3797 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003798 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 int add_r;
3800 char_u *aux;
3801
3802 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3803 break;
3804 found = TRUE;
3805 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003806 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003808 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 if (vim_iswordp(p))
3810 goto exit_matched;
3811 p = find_word_start(p);
3812 }
3813 p = find_word_end(p);
3814 i = (int)(p - aux);
3815
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003816 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003818 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003820
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003821 // Get the next line: when "depth" < 0 from the current
3822 // buffer, otherwise from the included file. Jump to
3823 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003824 if (depth < 0)
3825 {
3826 if (lnum >= end_lnum)
3827 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003828 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003829 }
3830 else if (vim_fgets(line = file_line,
3831 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 goto exit_matched;
3833
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003834 // we read a line, set "already" to check this "line" later
3835 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003836 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 already = aux = p = skipwhite(line);
3838 p = find_word_start(p);
3839 p = find_word_end(p);
3840 if (p > aux)
3841 {
3842 if (*aux != ')' && IObuff[i-1] != TAB)
3843 {
3844 if (IObuff[i-1] != ' ')
3845 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003846 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (p_js
3848 && (IObuff[i-2] == '.'
3849 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3850 && (IObuff[i-2] == '?'
3851 || IObuff[i-2] == '!'))))
3852 IObuff[i++] = ' ';
3853 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003854 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (p - aux >= IOSIZE - i)
3856 p = aux + IOSIZE - i - 1;
3857 STRNCPY(IObuff + i, aux, p - aux);
3858 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003859 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861 IObuff[i] = NUL;
3862 aux = IObuff;
3863
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003864 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 goto exit_matched;
3866 }
3867
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003868 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003870 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003872 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003874 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 break;
3876 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003877 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
3879 found = TRUE;
3880 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003881 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (curr_fname != prev_fname)
3883 {
3884 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003885 msg_putchar('\n'); // cursor below last one
3886 if (!got_int) // don't display if 'q' typed
3887 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 msg_home_replace_hl(curr_fname);
3889 prev_fname = curr_fname;
3890 }
3891 did_show = TRUE;
3892 if (!got_int)
3893 show_pat_in_path(line, type, TRUE, action,
3894 (depth == -1) ? NULL : files[depth].fp,
3895 (depth == -1) ? &lnum : &files[depth].lnum,
3896 match_count++);
3897
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003898 // Set matched flag for this file and all the ones that
3899 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 for (i = 0; i <= depth; ++i)
3901 files[i].matched = TRUE;
3902 }
3903 else if (--count <= 0)
3904 {
3905 found = TRUE;
3906 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003907#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 && g_do_tagpreview == 0
3909#endif
3910 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003911 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 else if (action == ACTION_SHOW)
3913 {
3914 show_pat_in_path(line, type, did_show, action,
3915 (depth == -1) ? NULL : files[depth].fp,
3916 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3917 did_show = TRUE;
3918 }
3919 else
3920 {
3921#ifdef FEAT_GUI
3922 need_mouse_correct = TRUE;
3923#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003924#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003925 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (g_do_tagpreview != 0)
3927 {
3928 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003929 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931#endif
3932 if (action == ACTION_SPLIT)
3933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003936 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 if (depth == -1)
3939 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003940 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003941#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (g_do_tagpreview != 0)
3943 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003944 if (!win_valid(curwin_save))
3945 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003946 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003947 curwin_save->w_buffer->b_fnum, NULL,
Colin Kennedy21570352024-03-03 16:16:47 +01003948 NULL, TRUE, lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003949 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 else
3952#endif
3953 setpcmark();
3954 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003955 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957 else
3958 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003959 if (!GETFILE_SUCCESS(getfile(
3960 0, files[depth].name, NULL, TRUE,
Colin Kennedy21570352024-03-03 16:16:47 +01003961 files[depth].lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003962 break; // failed to jump to file
3963 // autocommands may have changed the lnum, we don't
3964 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 curwin->w_cursor.lnum = files[depth].lnum;
3966 }
3967 }
3968 if (action != ACTION_SHOW)
3969 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003970 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 curwin->w_set_curswant = TRUE;
3972 }
3973
Bram Moolenaar4033c552017-09-16 20:54:51 +02003974#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003976 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003978 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003980 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 win_enter(curwin_save, TRUE);
3982 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003983# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003984 else if (WIN_IS_POPUP(curwin))
3985 // can't keep focus in popup window
3986 win_enter(firstwin, TRUE);
3987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988#endif
3989 break;
3990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003993 // look for other matches in the rest of the line if we
3994 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003997 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003998 && *startp != NUL
John Marriott8c85a2a2024-05-20 19:18:26 +02003999 && *(startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 goto search_line;
4001 }
4002 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004004 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004005 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 break;
4007
4008 /*
4009 * Read the next line. When reading an included file and encountering
4010 * end-of-file, close the file and continue in the file that included
4011 * it.
4012 */
4013 while (depth >= 0 && !already
4014 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4015 {
4016 fclose(files[depth].fp);
4017 --old_files;
4018 files[old_files].name = files[depth].name;
4019 files[old_files].matched = files[depth].matched;
4020 --depth;
4021 curr_fname = (depth == -1) ? curbuf->b_fname
4022 : files[depth].name;
4023 if (depth < depth_displayed)
4024 depth_displayed = depth;
4025 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004026 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004029 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004030 i = (int)STRLEN(line);
4031 if (i > 0 && line[i - 1] == '\n')
4032 line[--i] = NUL;
4033 if (i > 0 && line[i - 1] == '\r')
4034 line[--i] = NUL;
4035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 else if (!already)
4037 {
4038 if (++lnum > end_lnum)
4039 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01004040 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 }
4042 already = NULL;
4043 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004044 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004046 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 for (i = 0; i <= depth; i++)
4048 {
4049 fclose(files[i].fp);
4050 vim_free(files[i].name);
4051 }
4052 for (i = old_files; i < max_path_depth; i++)
4053 vim_free(files[i].name);
4054 vim_free(files);
4055
4056 if (type == CHECK_PATH)
4057 {
4058 if (!did_show)
4059 {
4060 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004061 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004063 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
4065 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004066 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004068 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004069 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004071 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004073 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4076 msg_end();
4077
4078fpip_end:
4079 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02004080 vim_regfree(regmatch.regprog);
4081 vim_regfree(incl_regmatch.regprog);
4082 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083}
4084
4085 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004086show_pat_in_path(
4087 char_u *line,
4088 int type,
4089 int did_show,
4090 int action,
4091 FILE *fp,
4092 linenr_T *lnum,
4093 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094{
4095 char_u *p;
John Marriott8c85a2a2024-05-20 19:18:26 +02004096 size_t linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004099 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004100 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004101 gotocmdline(TRUE); // cursor at status line
4102 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return;
John Marriott8c85a2a2024-05-20 19:18:26 +02004104 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 for (;;)
4106 {
John Marriott8c85a2a2024-05-20 19:18:26 +02004107 p = line + linelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (fp != NULL)
4109 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004110 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 if (p >= line && *p == '\n')
4112 --p;
4113 if (p >= line && *p == '\r')
4114 --p;
4115 *(p + 1) = NUL;
4116 }
4117 if (action == ACTION_SHOW_ALL)
4118 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004119 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004120 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004121 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4122 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004123 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4124 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004126 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004127 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004129 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4131 break;
4132
4133 if (fp != NULL)
4134 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004135 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 break;
John Marriott8c85a2a2024-05-20 19:18:26 +02004137 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 ++*lnum;
4139 }
4140 else
4141 {
4142 if (++*lnum > curbuf->b_ml.ml_line_count)
4143 break;
4144 line = ml_get(*lnum);
John Marriott8c85a2a2024-05-20 19:18:26 +02004145 linelen = ml_get_len(*lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
4147 msg_putchar('\n');
4148 }
4149}
4150#endif
4151
4152#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004153/*
4154 * Return the last used search pattern at "idx".
4155 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004156 spat_T *
4157get_spat(int idx)
4158{
4159 return &spats[idx];
4160}
4161
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004162/*
4163 * Return the last used search pattern index.
4164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004166get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004168 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004171
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004172#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004173/*
4174 * "searchcount()" function
4175 */
4176 void
4177f_searchcount(typval_T *argvars, typval_T *rettv)
4178{
4179 pos_T pos = curwin->w_cursor;
4180 char_u *pattern = NULL;
4181 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4182 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004183 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004184 searchstat_T stat;
4185
4186 if (rettv_dict_alloc(rettv) == FAIL)
4187 return;
4188
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004189 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4190 return;
4191
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004192 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4193 recompute = TRUE;
4194
4195 if (argvars[0].v_type != VAR_UNKNOWN)
4196 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004197 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004198 dictitem_T *di;
4199 listitem_T *li;
4200 int error = FALSE;
4201
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004202 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004203 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004204 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004205 di = dict_find(dict, (char_u *)"timeout", -1);
4206 if (di != NULL)
4207 {
4208 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4209 if (error)
4210 return;
4211 }
4212 di = dict_find(dict, (char_u *)"maxcount", -1);
4213 if (di != NULL)
4214 {
4215 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4216 if (error)
4217 return;
4218 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004219 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004220 di = dict_find(dict, (char_u *)"pattern", -1);
4221 if (di != NULL)
4222 {
4223 pattern = tv_get_string_chk(&di->di_tv);
4224 if (pattern == NULL)
4225 return;
4226 }
4227 di = dict_find(dict, (char_u *)"pos", -1);
4228 if (di != NULL)
4229 {
4230 if (di->di_tv.v_type != VAR_LIST)
4231 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004232 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004233 return;
4234 }
4235 if (list_len(di->di_tv.vval.v_list) != 3)
4236 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004237 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004238 return;
4239 }
4240 li = list_find(di->di_tv.vval.v_list, 0L);
4241 if (li != NULL)
4242 {
4243 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4244 if (error)
4245 return;
4246 }
4247 li = list_find(di->di_tv.vval.v_list, 1L);
4248 if (li != NULL)
4249 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004250 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004251 if (error)
4252 return;
4253 }
4254 li = list_find(di->di_tv.vval.v_list, 2L);
4255 if (li != NULL)
4256 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004257 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004258 if (error)
4259 return;
4260 }
4261 }
4262 }
4263
4264 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004265#ifdef FEAT_SEARCH_EXTRA
4266 save_incsearch_state();
4267#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004268 if (pattern != NULL)
4269 {
4270 if (*pattern == NUL)
4271 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004272 vim_free(spats[last_idx].pat);
John Marriott8c85a2a2024-05-20 19:18:26 +02004273 spats[last_idx].patlen = STRLEN(pattern);
4274 spats[last_idx].pat = vim_strnsave(pattern, spats[last_idx].patlen);
4275 if (spats[last_idx].pat == NULL)
4276 spats[last_idx].patlen = 0;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004277 }
4278 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4279 goto the_end; // the previous pattern was never defined
4280
4281 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4282
4283 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4284 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4285 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4286 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4287 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4288
4289the_end:
4290 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004291#ifdef FEAT_SEARCH_EXTRA
4292 restore_incsearch_state();
4293#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004294}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004295#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004296
4297/*
4298 * Fuzzy string matching
4299 *
4300 * Ported from the lib_fts library authored by Forrest Smith.
4301 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4302 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004303 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004304 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4305 *
4306 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004307 * - Matched letter
4308 * - Unmatched letter
4309 * - Consecutively matched letters
4310 * - Proximity to start
4311 * - Letter following a separator (space, underscore)
4312 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004313 *
4314 * Matched letters are good. Unmatched letters are bad. Matching near the start
4315 * is good. Matching the first letter in the middle of a phrase is good.
4316 * Matching the uppercase letters in camel case entries is good.
4317 *
4318 * The score assigned for each factor is explained below.
4319 * File paths are different from file names. File extensions may be ignorable.
4320 * Single words care about consecutive matches but not separators or camel
4321 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004322 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004323 * Matched letter: +0 points
4324 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004325 * Consecutive match bonus: +15 points
4326 * First letter bonus: +15 points
4327 * Separator bonus: +30 points
4328 * Camel case bonus: +30 points
4329 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004330 *
4331 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004332 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004333 * lower minimum score due to unmatched letter penalty. Longer search patterns
4334 * have a higher maximum score due to match bonuses.
4335 *
4336 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4337 * quite a bit.
4338 *
4339 * There is a penalty if you DON’T match the first three letters. Which
4340 * effectively rewards matching near the start. However there’s no difference
4341 * in matching between the middle and end.
4342 *
4343 * There is not an explicit bonus for an exact match. Unmatched letters receive
4344 * a penalty. So shorter strings and closer matches are worth more.
4345 */
4346typedef struct
4347{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004348 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004349 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004350 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004351 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004352} fuzzyItem_T;
4353
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004354// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4355// matching a whole word is preferred.
4356#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004357// bonus if match occurs after a path separator
4358#define PATH_SEPARATOR_BONUS 30
4359// bonus if match occurs after a word separator
4360#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004361// bonus if match is uppercase and prev is lower
4362#define CAMEL_BONUS 30
4363// bonus if the first letter is matched
4364#define FIRST_LETTER_BONUS 15
glepnir9dfc7e52025-01-21 22:33:13 +01004365// bonus if exact match
4366#define EXACT_MATCH_BONUS 100
4367// bonus if case match when no ignorecase
4368#define CASE_MATCH_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004369// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004370#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004371// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004372#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004373// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004374#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004375// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004376#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004377// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004378#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004379
4380#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004381
4382/*
4383 * Compute a score for a fuzzy matched string. The matching character locations
4384 * are in 'matches'.
4385 */
4386 static int
4387fuzzy_match_compute_score(
glepnir9dfc7e52025-01-21 22:33:13 +01004388 char_u *fuzpat,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004389 char_u *str,
4390 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004391 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004392 int numMatches)
4393{
4394 int score;
4395 int penalty;
4396 int unmatched;
4397 int i;
4398 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004399 int_u sidx = 0;
glepnir5a049992024-12-26 15:38:39 +01004400 int is_exact_match = TRUE;
glepnir9dfc7e52025-01-21 22:33:13 +01004401 char_u *orig_fuzpat = fuzpat - numMatches;
4402 char_u *curpat = orig_fuzpat;
4403 int pat_idx = 0;
4404 // Track consecutive camel case matches
4405 int consecutive_camel = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004406
4407 // Initialize score
4408 score = 100;
4409
4410 // Apply leading letter penalty
4411 penalty = LEADING_LETTER_PENALTY * matches[0];
4412 if (penalty < MAX_LEADING_LETTER_PENALTY)
4413 penalty = MAX_LEADING_LETTER_PENALTY;
4414 score += penalty;
4415
4416 // Apply unmatched penalty
4417 unmatched = strSz - numMatches;
4418 score += UNMATCHED_LETTER_PENALTY * unmatched;
4419
4420 // Apply ordering bonuses
4421 for (i = 0; i < numMatches; ++i)
4422 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004423 int_u currIdx = matches[i];
glepnir9dfc7e52025-01-21 22:33:13 +01004424 int curr;
4425 int is_camel = FALSE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004426
4427 if (i > 0)
4428 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004429 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004430
4431 // Sequential
4432 if (currIdx == (prevIdx + 1))
4433 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004434 else
glepnir9dfc7e52025-01-21 22:33:13 +01004435 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004436 score += GAP_PENALTY * (currIdx - prevIdx);
glepnir9dfc7e52025-01-21 22:33:13 +01004437 // Reset consecutive camel count on gap
4438 consecutive_camel = 0;
4439 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004440 }
4441
4442 // Check for bonuses based on neighbor character value
4443 if (currIdx > 0)
4444 {
4445 // Camel case
glepnir9dfc7e52025-01-21 22:33:13 +01004446 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004447
4448 if (has_mbyte)
4449 {
4450 while (sidx < currIdx)
4451 {
4452 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004453 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004454 sidx++;
4455 }
4456 curr = (*mb_ptr2char)(p);
4457 }
4458 else
4459 {
4460 neighbor = str[currIdx - 1];
4461 curr = str[currIdx];
4462 }
4463
glepnir9dfc7e52025-01-21 22:33:13 +01004464 // Enhanced camel case scoring
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004465 if (vim_islower(neighbor) && vim_isupper(curr))
glepnir9dfc7e52025-01-21 22:33:13 +01004466 {
4467 score += CAMEL_BONUS * 2; // Double the camel case bonus
4468 is_camel = TRUE;
4469 consecutive_camel++;
4470 // Additional bonus for consecutive camel
4471 if (consecutive_camel > 1)
4472 score += CAMEL_BONUS;
4473 }
4474 else
4475 consecutive_camel = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004476
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004477 // Bonus if the match follows a separator character
4478 if (neighbor == '/' || neighbor == '\\')
4479 score += PATH_SEPARATOR_BONUS;
4480 else if (neighbor == ' ' || neighbor == '_')
4481 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004482 }
4483 else
4484 {
4485 // First letter
4486 score += FIRST_LETTER_BONUS;
glepnir9dfc7e52025-01-21 22:33:13 +01004487 curr = has_mbyte ? (*mb_ptr2char)(p) : str[currIdx];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004488 }
glepnir9dfc7e52025-01-21 22:33:13 +01004489
4490 // Case matching bonus
4491 if (vim_isalpha(curr))
4492 {
4493 while (pat_idx < i && *curpat)
4494 {
4495 if (has_mbyte)
4496 MB_PTR_ADV(curpat);
4497 else
4498 curpat++;
4499 pat_idx++;
4500 }
4501
4502 if (has_mbyte)
4503 {
4504 if (curr == (*mb_ptr2char)(curpat))
4505 {
4506 score += CASE_MATCH_BONUS;
4507 // Extra bonus for exact case match in camel
4508 if (is_camel)
4509 score += CASE_MATCH_BONUS / 2;
4510 }
4511 }
4512 else if (curr == *curpat)
4513 {
4514 score += CASE_MATCH_BONUS;
4515 if (is_camel)
4516 score += CASE_MATCH_BONUS / 2;
4517 }
4518 }
4519
glepnir5a049992024-12-26 15:38:39 +01004520 // Check exact match condition
4521 if (currIdx != (int_u)i)
4522 is_exact_match = FALSE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004523 }
glepnir9dfc7e52025-01-21 22:33:13 +01004524
glepnir5a049992024-12-26 15:38:39 +01004525 // Boost score for exact matches
4526 if (is_exact_match && numMatches == strSz)
glepnir9dfc7e52025-01-21 22:33:13 +01004527 score += EXACT_MATCH_BONUS;
glepnir5a049992024-12-26 15:38:39 +01004528
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004529 return score;
4530}
4531
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004532/*
4533 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4534 * Return the number of matching characters.
4535 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004536 static int
4537fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004538 char_u *fuzpat,
4539 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004540 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004541 int *outScore,
4542 char_u *strBegin,
4543 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004544 int_u *srcMatches,
4545 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004546 int maxMatches,
4547 int nextMatch,
4548 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004549{
4550 // Recursion params
4551 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004552 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004553 int bestRecursiveScore = 0;
4554 int first_match;
4555 int matched;
4556
4557 // Count recursions
4558 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004559 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004560 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004561
4562 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004563 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004564 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004565
4566 // Loop through fuzpat and str looking for a match
4567 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004568 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004569 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004570 int c1;
4571 int c2;
4572
4573 c1 = PTR2CHAR(fuzpat);
4574 c2 = PTR2CHAR(str);
4575
Bram Moolenaar635414d2020-09-11 22:25:15 +02004576 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004577 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004578 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004579 // Supplied matches buffer was too short
4580 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004581 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004582
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004583 int recursiveScore = 0;
4584 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4585 CLEAR_FIELD(recursiveMatches);
4586
Bram Moolenaar635414d2020-09-11 22:25:15 +02004587 // "Copy-on-Write" srcMatches into matches
4588 if (first_match && srcMatches)
4589 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004590 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004591 first_match = FALSE;
4592 }
4593
4594 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004595 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004596 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4597 &recursiveScore, strBegin, strLen, matches,
4598 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004599 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004600 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004601 {
4602 // Pick best recursive score
4603 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4604 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004605 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004606 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004607 bestRecursiveScore = recursiveScore;
4608 }
4609 recursiveMatch = TRUE;
4610 }
4611
4612 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004613 matches[nextMatch++] = strIdx;
4614 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004615 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004616 else
4617 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004618 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004619 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004620 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004621 else
4622 ++str;
4623 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004624 }
4625
4626 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004627 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004628
4629 // Calculate score
4630 if (matched)
glepnir9dfc7e52025-01-21 22:33:13 +01004631 *outScore = fuzzy_match_compute_score(fuzpat, strBegin, strLen, matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004632 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004633
4634 // Return best result
4635 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4636 {
4637 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004638 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004639 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004640 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004641 }
4642 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004643 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004644
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004645 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004646}
4647
4648/*
4649 * fuzzy_match()
4650 *
4651 * Performs exhaustive search via recursion to find all possible matches and
4652 * match with highest score.
4653 * Scores values have no intrinsic meaning. Possible score range is not
4654 * normalized and varies with pattern.
4655 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004656 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004657 * Uses char_u for match indices. Therefore patterns are limited to
4658 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004659 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004660 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4661 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004662 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004663 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004664fuzzy_match(
4665 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004666 char_u *pat_arg,
4667 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004668 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004669 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004670 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004671{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004672 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004673 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004674 char_u *save_pat;
4675 char_u *pat;
4676 char_u *p;
4677 int complete = FALSE;
4678 int score = 0;
4679 int numMatches = 0;
4680 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004681
4682 *outScore = 0;
4683
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004684 save_pat = vim_strsave(pat_arg);
4685 if (save_pat == NULL)
4686 return FALSE;
4687 pat = save_pat;
4688 p = pat;
4689
4690 // Try matching each word in 'pat_arg' in 'str'
4691 while (TRUE)
4692 {
4693 if (matchseq)
4694 complete = TRUE;
4695 else
4696 {
4697 // Extract one word from the pattern (separated by space)
4698 p = skipwhite(p);
4699 if (*p == NUL)
4700 break;
4701 pat = p;
4702 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4703 {
4704 if (has_mbyte)
4705 MB_PTR_ADV(p);
4706 else
4707 ++p;
4708 }
4709 if (*p == NUL) // processed all the words
4710 complete = TRUE;
4711 *p = NUL;
4712 }
4713
4714 score = 0;
4715 recursionCount = 0;
4716 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4717 matches + numMatches, maxMatches - numMatches,
4718 0, &recursionCount);
4719 if (matchCount == 0)
4720 {
4721 numMatches = 0;
4722 break;
4723 }
4724
4725 // Accumulate the match score and the number of matches
4726 *outScore += score;
4727 numMatches += matchCount;
4728
4729 if (complete)
4730 break;
4731
4732 // try matching the next word
4733 ++p;
4734 }
4735
4736 vim_free(save_pat);
4737 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004738}
4739
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004740#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004741/*
4742 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004743 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004744 */
4745 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004746fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004747{
4748 int v1 = ((fuzzyItem_T *)s1)->score;
4749 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004750 int idx1 = ((fuzzyItem_T *)s1)->idx;
4751 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004752
zeertzjq77078272024-02-10 13:24:03 +01004753 if (v1 == v2)
4754 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4755 else
4756 return v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004757}
4758
4759/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004760 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4761 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004762 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4763 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004764 * If 'items' is a list of strings, then search for 'str' in the list.
4765 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4766 * for each item or use 'item_cb' Funcref function to get the string.
4767 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4768 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004769 */
4770 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004771fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004772 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004773 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004774 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004775 char_u *key,
4776 callback_T *item_cb,
4777 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004778 list_T *fmatchlist,
4779 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004780{
4781 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004782 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004783 listitem_T *li;
4784 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004785 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004786 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004787
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004788 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004789 if (len == 0)
4790 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004791 if (max_matches > 0 && len > max_matches)
4792 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004793
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004794 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4795 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004796 return;
4797
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004798 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004799 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004800 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004801 int score;
4802 char_u *itemstr;
4803 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004804
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004805 if (max_matches > 0 && match_count >= max_matches)
4806 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004807
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004808 itemstr = NULL;
4809 rettv.v_type = VAR_UNKNOWN;
4810 if (li->li_tv.v_type == VAR_STRING) // list of strings
4811 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004812 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004813 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004814 {
4815 // For a dict, either use the specified key to lookup the string or
4816 // use the specified callback function to get the string.
4817 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004818 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4819 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004820 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004821 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004822 typval_T argv[2];
4823
4824 // Invoke the supplied callback (if any) to get the dict item
4825 li->li_tv.vval.v_dict->dv_refcount++;
4826 argv[0].v_type = VAR_DICT;
4827 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4828 argv[1].v_type = VAR_UNKNOWN;
4829 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4830 {
4831 if (rettv.v_type == VAR_STRING)
4832 itemstr = rettv.vval.v_string;
4833 }
4834 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004835 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004836 }
4837
4838 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004839 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004840 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004841 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004842 items[match_count].idx = match_count;
4843 items[match_count].item = li;
4844 items[match_count].score = score;
4845
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004846 // Copy the list of matching positions in itemstr to a list, if
4847 // 'retmatchpos' is set.
4848 if (retmatchpos)
4849 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004850 int j = 0;
4851 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004852
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004853 items[match_count].lmatchpos = list_alloc();
4854 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004855 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004856
4857 p = str;
4858 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004859 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004860 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004861 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004862 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004863 matches[j]) == FAIL)
4864 goto done;
4865 j++;
4866 }
4867 if (has_mbyte)
4868 MB_PTR_ADV(p);
4869 else
4870 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004871 }
4872 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004873 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004874 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004875 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004876 }
4877
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004878 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004879 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004880 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004881
Bram Moolenaar635414d2020-09-11 22:25:15 +02004882 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004883 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004884 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004885
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004886 // For matchfuzzy(), return a list of matched strings.
4887 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004888 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004889 // The first item is a list of matched strings. The second item
4890 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004891 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004892 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4893 if (retmatchpos)
4894 {
4895 li = list_find(fmatchlist, 0);
4896 if (li == NULL || li->li_tv.vval.v_list == NULL)
4897 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004898 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004899 }
4900 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004901 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004902
4903 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004904 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004905 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004906 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004907 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004908 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004909 }
4910
4911 // next copy the list of matching positions
4912 if (retmatchpos)
4913 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004914 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004915 if (li == NULL || li->li_tv.vval.v_list == NULL)
4916 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004917 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004918
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004919 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004920 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004921 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004922 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004923 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004924 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004925 goto done;
4926 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004927
4928 // copy the matching scores
4929 li = list_find(fmatchlist, -1);
4930 if (li == NULL || li->li_tv.vval.v_list == NULL)
4931 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004932 retlist = li->li_tv.vval.v_list;
4933 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004934 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004935 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004936 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004937 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004938 goto done;
4939 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004940 }
4941 }
4942
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004943done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004944 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004945}
4946
4947/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004948 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4949 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4950 */
4951 static void
4952do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4953{
4954 callback_T cb;
4955 char_u *key = NULL;
4956 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004957 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004958 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004959
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004960 if (in_vim9script()
4961 && (check_for_list_arg(argvars, 0) == FAIL
4962 || check_for_string_arg(argvars, 1) == FAIL
4963 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4964 return;
4965
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004966 CLEAR_POINTER(&cb);
4967
4968 // validate and get the arguments
4969 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4970 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004971 semsg(_(e_argument_of_str_must_be_list),
4972 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004973 return;
4974 }
4975 if (argvars[1].v_type != VAR_STRING
4976 || argvars[1].vval.v_string == NULL)
4977 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004978 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004979 return;
4980 }
4981
4982 if (argvars[2].v_type != VAR_UNKNOWN)
4983 {
4984 dict_T *d;
4985 dictitem_T *di;
4986
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004987 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004988 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004989
4990 // To search a dict, either a callback function or a key can be
4991 // specified.
4992 d = argvars[2].vval.v_dict;
4993 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4994 {
4995 if (di->di_tv.v_type != VAR_STRING
4996 || di->di_tv.vval.v_string == NULL
4997 || *di->di_tv.vval.v_string == NUL)
4998 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004999 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005000 return;
5001 }
5002 key = tv_get_string(&di->di_tv);
5003 }
5004 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
5005 {
5006 cb = get_callback(&di->di_tv);
5007 if (cb.cb_name == NULL)
5008 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00005009 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005010 return;
5011 }
5012 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01005013
5014 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01005015 {
5016 if (di->di_tv.v_type != VAR_NUMBER)
5017 {
5018 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
5019 return;
5020 }
5021 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
5022 }
5023
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01005024 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02005025 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005026 }
5027
5028 // get the fuzzy matches
5029 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01005030 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005031 goto done;
5032 if (retmatchpos)
5033 {
5034 list_T *l;
5035
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01005036 // For matchfuzzypos(), a list with three items are returned. First
5037 // item is a list of matching strings, the second item is a list of
5038 // lists with matching positions within each string and the third item
5039 // is the list of scores of the matches.
5040 l = list_alloc();
5041 if (l == NULL)
5042 goto done;
5043 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01005044 {
5045 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01005046 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01005047 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005048 l = list_alloc();
5049 if (l == NULL)
5050 goto done;
5051 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01005052 {
5053 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005054 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01005055 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005056 l = list_alloc();
5057 if (l == NULL)
5058 goto done;
5059 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01005060 {
5061 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005062 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01005063 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005064 }
5065
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02005066 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01005067 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005068
5069done:
5070 free_callback(&cb);
5071}
5072
5073/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02005074 * "matchfuzzy()" function
5075 */
5076 void
5077f_matchfuzzy(typval_T *argvars, typval_T *rettv)
5078{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005079 do_fuzzymatch(argvars, rettv, FALSE);
5080}
5081
5082/*
5083 * "matchfuzzypos()" function
5084 */
5085 void
5086f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
5087{
5088 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02005089}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02005090#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005091
5092/*
5093 * Same as fuzzy_match_item_compare() except for use with a string match
5094 */
5095 static int
5096fuzzy_match_str_compare(const void *s1, const void *s2)
5097{
5098 int v1 = ((fuzmatch_str_T *)s1)->score;
5099 int v2 = ((fuzmatch_str_T *)s2)->score;
5100 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5101 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5102
Christian Brabandte06e4372024-02-09 19:39:14 +01005103 if (v1 == v2)
5104 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5105 else
5106 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005107}
5108
5109/*
5110 * Sort fuzzy matches by score
5111 */
5112 static void
5113fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
5114{
5115 // Sort the list by the descending order of the match score
5116 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5117 fuzzy_match_str_compare);
5118}
5119
5120/*
5121 * Same as fuzzy_match_item_compare() except for use with a function name
5122 * string match. <SNR> functions should be sorted to the end.
5123 */
5124 static int
5125fuzzy_match_func_compare(const void *s1, const void *s2)
5126{
5127 int v1 = ((fuzmatch_str_T *)s1)->score;
5128 int v2 = ((fuzmatch_str_T *)s2)->score;
5129 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5130 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5131 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
5132 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
5133
Christian Brabandte06e4372024-02-09 19:39:14 +01005134 if (*str1 != '<' && *str2 == '<')
5135 return -1;
5136 if (*str1 == '<' && *str2 != '<')
5137 return 1;
5138 if (v1 == v2)
5139 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5140 else
5141 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005142}
5143
5144/*
5145 * Sort fuzzy matches of function names by score.
5146 * <SNR> functions should be sorted to the end.
5147 */
5148 static void
5149fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
5150{
5151 // Sort the list by the descending order of the match score
5152 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5153 fuzzy_match_func_compare);
5154}
5155
5156/*
5157 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
5158 * returns the match score.
5159 */
5160 int
5161fuzzy_match_str(char_u *str, char_u *pat)
5162{
5163 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00005164 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005165
5166 if (str == NULL || pat == NULL)
5167 return 0;
5168
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00005169 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005170 sizeof(matchpos) / sizeof(matchpos[0]));
5171
5172 return score;
5173}
5174
5175/*
glepnir40c1c332024-06-11 19:37:04 +02005176 * Fuzzy match the position of string 'pat' in string 'str'.
5177 * Returns a dynamic array of matching positions. If there is no match,
5178 * returns NULL.
5179 */
5180 garray_T *
5181fuzzy_match_str_with_pos(char_u *str UNUSED, char_u *pat UNUSED)
5182{
5183#ifdef FEAT_SEARCH_EXTRA
5184 int score = 0;
zeertzjq2f95ca92024-06-13 17:14:27 +02005185 garray_T *match_positions = NULL;
5186 int_u matches[MAX_FUZZY_MATCHES];
5187 int j = 0;
glepnir40c1c332024-06-11 19:37:04 +02005188
zeertzjq2f95ca92024-06-13 17:14:27 +02005189 if (str == NULL || pat == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005190 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005191
5192 match_positions = ALLOC_ONE(garray_T);
glepnir40c1c332024-06-11 19:37:04 +02005193 if (match_positions == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005194 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005195 ga_init2(match_positions, sizeof(int_u), 10);
5196
5197 if (!fuzzy_match(str, pat, FALSE, &score, matches, MAX_FUZZY_MATCHES)
5198 || score == 0)
glepnir40c1c332024-06-11 19:37:04 +02005199 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005200 ga_clear(match_positions);
5201 vim_free(match_positions);
5202 return NULL;
glepnir40c1c332024-06-11 19:37:04 +02005203 }
5204
zeertzjq2f95ca92024-06-13 17:14:27 +02005205 for (char_u *p = pat; *p != NUL; MB_PTR_ADV(p))
glepnir40c1c332024-06-11 19:37:04 +02005206 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005207 if (!VIM_ISWHITE(PTR2CHAR(p)))
5208 {
5209 ga_grow(match_positions, 1);
5210 ((int_u *)match_positions->ga_data)[match_positions->ga_len] =
5211 matches[j];
5212 match_positions->ga_len++;
5213 j++;
5214 }
glepnir40c1c332024-06-11 19:37:04 +02005215 }
5216
glepnir40c1c332024-06-11 19:37:04 +02005217 return match_positions;
glepnir40c1c332024-06-11 19:37:04 +02005218#else
5219 return NULL;
5220#endif
5221}
5222
5223/*
glepnir8159fb12024-07-17 20:32:54 +02005224 * This function searches for a fuzzy match of the pattern `pat` within the
5225 * line pointed to by `*ptr`. It splits the line into words, performs fuzzy
5226 * matching on each word, and returns the length and position of the first
5227 * matched word.
5228 */
5229 static int
5230fuzzy_match_str_in_line(char_u **ptr, char_u *pat, int *len, pos_T *current_pos)
5231{
5232 char_u *str = *ptr;
5233 char_u *strBegin = str;
5234 char_u *end = NULL;
5235 char_u *start = NULL;
5236 int found = FALSE;
5237 int result;
5238 char save_end;
5239
5240 if (str == NULL || pat == NULL)
5241 return found;
5242
5243 while (*str != NUL)
5244 {
5245 // Skip non-word characters
5246 start = find_word_start(str);
5247 if (*start == NUL)
5248 break;
5249 end = find_word_end(start);
5250
5251 // Extract the word from start to end
5252 save_end = *end;
5253 *end = NUL;
5254
5255 // Perform fuzzy match
5256 result = fuzzy_match_str(start, pat);
5257 *end = save_end;
5258
5259 if (result > 0)
5260 {
5261 *len = (int)(end - start);
5262 current_pos->col += (int)(end - strBegin);
5263 found = TRUE;
5264 *ptr = start;
5265 break;
5266 }
5267
5268 // Move to the end of the current word for the next iteration
5269 str = end;
5270 // Ensure we continue searching after the current word
5271 while (*str != NUL && !vim_iswordp(str))
5272 MB_PTR_ADV(str);
5273 }
5274
5275 return found;
5276}
5277
5278/*
5279 * Search for the next fuzzy match in the specified buffer.
5280 * This function attempts to find the next occurrence of the given pattern
5281 * in the buffer, starting from the current position. It handles line wrapping
5282 * and direction of search.
5283 *
5284 * Return TRUE if a match is found, otherwise FALSE.
5285 */
5286 int
5287search_for_fuzzy_match(
5288 buf_T *buf,
5289 pos_T *pos,
5290 char_u *pattern,
5291 int dir,
5292 pos_T *start_pos,
5293 int *len,
5294 char_u **ptr,
5295 int whole_line)
5296{
5297 pos_T current_pos = *pos;
5298 pos_T circly_end;
zeertzjq58d70522024-08-31 17:05:39 +02005299 int found_new_match = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02005300 int looped_around = FALSE;
5301
5302 if (whole_line)
5303 current_pos.lnum += dir;
5304
glepnir0be03e12024-07-19 16:45:05 +02005305 if (buf == curbuf)
5306 circly_end = *start_pos;
5307 else
5308 {
5309 circly_end.lnum = buf->b_ml.ml_line_count;
5310 circly_end.col = 0;
5311 circly_end.coladd = 0;
5312 }
5313
glepnir8159fb12024-07-17 20:32:54 +02005314 do {
glepnir8159fb12024-07-17 20:32:54 +02005315
5316 // Check if looped around and back to start position
5317 if (looped_around && EQUAL_POS(current_pos, circly_end))
5318 break;
5319
5320 // Ensure current_pos is valid
5321 if (current_pos.lnum >= 1 && current_pos.lnum <= buf->b_ml.ml_line_count)
5322 {
5323 // Get the current line buffer
5324 *ptr = ml_get_buf(buf, current_pos.lnum, FALSE);
5325 // If ptr is end of line is reached, move to next line
5326 // or previous line based on direction
5327 if (**ptr != NUL)
5328 {
5329 if (!whole_line)
5330 {
5331 *ptr += current_pos.col;
5332 // Try to find a fuzzy match in the current line starting from current position
5333 found_new_match = fuzzy_match_str_in_line(ptr, pattern, len, &current_pos);
5334 if (found_new_match)
5335 {
glepnir7cfe6932024-09-15 20:06:28 +02005336 if (ctrl_x_mode_normal())
5337 {
John Marriottb79fa3d2025-02-21 19:59:56 +01005338 if (STRNCMP(*ptr, pattern, *len) == 0 && pattern[*len] == NUL)
glepnir7cfe6932024-09-15 20:06:28 +02005339 {
John Marriottb79fa3d2025-02-21 19:59:56 +01005340 char_u *next_word_end = find_word_start(*ptr + *len);
glepnir7cfe6932024-09-15 20:06:28 +02005341 if (*next_word_end != NUL && *next_word_end != NL)
5342 {
5343 // Find end of the word.
5344 if (has_mbyte)
5345 while (*next_word_end != NUL)
5346 {
5347 int l = (*mb_ptr2len)(next_word_end);
5348
5349 if (l < 2 && !vim_iswordc(*next_word_end))
5350 break;
5351 next_word_end += l;
5352 }
5353 else
5354 next_word_end = find_word_end(next_word_end);
5355 }
5356 else if (looped_around)
5357 found_new_match = FALSE;
5358
5359 *len = next_word_end - *ptr;
5360 current_pos.col = *len;
5361 }
glepnir7cfe6932024-09-15 20:06:28 +02005362 }
glepnir8159fb12024-07-17 20:32:54 +02005363 *pos = current_pos;
5364 break;
5365 }
glepnir0be03e12024-07-19 16:45:05 +02005366 else if (looped_around && current_pos.lnum == circly_end.lnum)
5367 break;
glepnir8159fb12024-07-17 20:32:54 +02005368 }
5369 else
5370 {
5371 if (fuzzy_match_str(*ptr, pattern) > 0)
5372 {
5373 found_new_match = TRUE;
5374 *pos = current_pos;
John Marriottb79fa3d2025-02-21 19:59:56 +01005375 *len = (int)ml_get_buf_len(buf, current_pos.lnum);
glepnir8159fb12024-07-17 20:32:54 +02005376 break;
5377 }
5378 }
5379 }
5380 }
5381
5382 // Move to the next line or previous line based on direction
5383 if (dir == FORWARD)
5384 {
5385 if (++current_pos.lnum > buf->b_ml.ml_line_count)
5386 {
5387 if (p_ws)
5388 {
5389 current_pos.lnum = 1;
5390 looped_around = TRUE;
5391 }
5392 else
5393 break;
5394 }
5395 }
5396 else
5397 {
5398 if (--current_pos.lnum < 1)
5399 {
5400 if (p_ws)
5401 {
5402 current_pos.lnum = buf->b_ml.ml_line_count;
5403 looped_around = TRUE;
5404 }
5405 else
5406 break;
5407
5408 }
5409 }
5410 current_pos.col = 0;
5411 } while (TRUE);
5412
5413 return found_new_match;
5414}
5415
5416/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005417 * Free an array of fuzzy string matches "fuzmatch[count]".
5418 */
5419 void
5420fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5421{
5422 int i;
5423
5424 if (fuzmatch == NULL)
5425 return;
5426 for (i = 0; i < count; ++i)
5427 vim_free(fuzmatch[i].str);
5428 vim_free(fuzmatch);
5429}
5430
5431/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005432 * Copy a list of fuzzy matches into a string list after sorting the matches by
5433 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5434 * Returns OK on success and FAIL on memory allocation failure.
5435 */
5436 int
5437fuzzymatches_to_strmatches(
5438 fuzmatch_str_T *fuzmatch,
5439 char_u ***matches,
5440 int count,
5441 int funcsort)
5442{
5443 int i;
5444
5445 if (count <= 0)
5446 return OK;
5447
5448 *matches = ALLOC_MULT(char_u *, count);
5449 if (*matches == NULL)
5450 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005451 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005452 return FAIL;
5453 }
5454
5455 // Sort the list by the descending order of the match score
5456 if (funcsort)
5457 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5458 else
5459 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5460
5461 for (i = 0; i < count; i++)
5462 (*matches)[i] = fuzmatch[i].str;
5463 vim_free(fuzmatch);
5464
5465 return OK;
5466}