blob: 616331ee1780e368f8a5cc87e510b190a95eb47d [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * search.c: code for normal mode searching commands
11 */
12
13#include "vim.h"
14
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010016static void set_vv_searchforward(void);
17static int first_submatch(regmmatch_T *rp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef FEAT_FIND_ID
John Marriott8c85a2a2024-05-20 19:18:26 +020020static char_u *get_line_and_copy(linenr_T lnum, char_u *buf);
21static void show_pat_in_path(char_u *, int, int, int, FILE *, linenr_T *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020023
24typedef struct searchstat
25{
26 int cur; // current position of found words
27 int cnt; // total count of found words
28 int exact_match; // TRUE if matched exactly on specified position
29 int incomplete; // 0: search was fully completed
30 // 1: recomputing was timed out
31 // 2: max count exceeded
32 int last_maxcount; // the max count of the last search
33} searchstat_T;
34
John Marriott8c85a2a2024-05-20 19:18:26 +020035#ifdef FEAT_SEARCH_EXTRA
36static void save_incsearch_state(void);
37static void restore_incsearch_state(void);
38#endif
39static int check_prevcol(char_u *linep, int col, int ch, int *prevcol);
40static int find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos);
41static void find_mps_values(int *initc, int *findc, int *backwards, int switchit);
42static int is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction);
43static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, size_t msgbuflen, int recompute, int maxcount, long timeout);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020044static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout);
John Marriott8c85a2a2024-05-20 19:18:26 +020045static int fuzzy_match_compute_score(char_u *str, int strSz, int_u *matches, int numMatches);
46static int fuzzy_match_recursive(char_u *fuzpat, char_u *str, int_u strIdx, int *outScore, char_u *strBegin, int strLen, int_u *srcMatches, int_u *matches, int maxMatches, int nextMatch, int *recursionCount);
47#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
48static int fuzzy_match_item_compare(const void *s1, const void *s2);
49static void fuzzy_match_in_list(list_T *l, char_u *str, int matchseq, char_u *key, callback_T *item_cb, int retmatchpos, list_T *fmatchlist, long max_matches);
50static void do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos);
51#endif
52static int fuzzy_match_str_compare(const void *s1, const void *s2);
53static void fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz);
54static int fuzzy_match_func_compare(const void *s1, const void *s2);
55static void fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz);
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;
3272 static buf_T *lbuf = NULL;
3273#ifdef FEAT_RELTIME
3274 proftime_T start;
3275#endif
3276
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003277 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003278
3279 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3280 {
3281 stat->cur = cur;
3282 stat->cnt = cnt;
3283 stat->exact_match = exact_match;
3284 stat->incomplete = incomplete;
3285 stat->last_maxcount = last_maxcount;
3286 return;
3287 }
3288 last_maxcount = maxcount;
3289
3290 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3291 || (dirc == '/' && LT_POS(p, lastpos)));
3292
3293 // If anything relevant changed the count has to be recomputed.
3294 // MB_STRNICMP ignores case, but we should not ignore case.
3295 // Unfortunately, there is no MB_STRNICMP function.
3296 // XXX: above comment should be "no MB_STRCMP function" ?
3297 if (!(chgtick == CHANGEDTICK(curbuf)
3298 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3299 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3300 && EQUAL_POS(lastpos, *cursor_pos)
3301 && lbuf == curbuf) || wraparound || cur < 0
3302 || (maxcount > 0 && cur > maxcount) || recompute)
3303 {
3304 cur = 0;
3305 cnt = 0;
3306 exact_match = FALSE;
3307 incomplete = 0;
3308 CLEAR_POS(&lastpos);
3309 lbuf = curbuf;
3310 }
3311
Christian Brabandt34a6a362023-05-06 19:20:20 +01003312 // when searching backwards and having jumped to the first occurrence,
3313 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003314 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003315 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003316 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3317 else
3318 {
3319 int done_search = FALSE;
3320 pos_T endpos = {0, 0, 0};
3321
3322 p_ws = FALSE;
3323#ifdef FEAT_RELTIME
3324 if (timeout > 0)
3325 profile_setlimit(timeout, &start);
3326#endif
3327 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003328 FORWARD, NULL, 0, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003329 {
3330 done_search = TRUE;
3331#ifdef FEAT_RELTIME
3332 // Stop after passing the time limit.
3333 if (timeout > 0 && profile_passed_limit(&start))
3334 {
3335 incomplete = 1;
3336 break;
3337 }
3338#endif
3339 cnt++;
3340 if (LTOREQ_POS(lastpos, p))
3341 {
3342 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003343 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003344 exact_match = TRUE;
3345 }
3346 fast_breakcheck();
3347 if (maxcount > 0 && cnt > maxcount)
3348 {
3349 incomplete = 2; // max count exceeded
3350 break;
3351 }
3352 }
3353 if (got_int)
3354 cur = -1; // abort
3355 if (done_search)
3356 {
3357 vim_free(lastpat);
3358 lastpat = vim_strsave(spats[last_idx].pat);
3359 chgtick = CHANGEDTICK(curbuf);
3360 lbuf = curbuf;
3361 lastpos = p;
3362 }
3363 }
3364 stat->cur = cur;
3365 stat->cnt = cnt;
3366 stat->exact_match = exact_match;
3367 stat->incomplete = incomplete;
3368 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003369 p_ws = save_ws;
3370}
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003373
3374/*
3375 * Get line "lnum" and copy it into "buf[LSIZE]".
3376 * The copy is made because the regexp may make the line invalid when using a
3377 * mark.
3378 */
3379 static char_u *
3380get_line_and_copy(linenr_T lnum, char_u *buf)
3381{
3382 char_u *line = ml_get(lnum);
3383
3384 vim_strncpy(buf, line, LSIZE - 1);
3385 return buf;
3386}
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388/*
3389 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003390 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003393find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003394 char_u *ptr, // pointer to search pattern
3395 int dir UNUSED, // direction of expansion
3396 int len, // length of search pattern
3397 int whole, // match whole words only
3398 int skip_comments, // don't match inside comments
3399 int type, // Type of search; are we looking for a type?
3400 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003401 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003402 int action, // What to do when we find it
3403 linenr_T start_lnum, // first line to start searching
Colin Kennedy21570352024-03-03 16:16:47 +01003404 linenr_T end_lnum, // last line for searching
3405 int forceit) // If true, always switch to the found path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003407 SearchedFile *files; // Stack of included files
3408 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 int max_path_depth = 50;
3410 long match_count = 1;
3411
3412 char_u *pat;
3413 char_u *new_fname;
3414 char_u *curr_fname = curbuf->b_fname;
3415 char_u *prev_fname = NULL;
3416 linenr_T lnum;
3417 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003418 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 int old_files;
3420 int already_searched;
3421 char_u *file_line;
3422 char_u *line;
3423 char_u *p;
3424 char_u save_char;
3425 int define_matched;
3426 regmatch_T regmatch;
3427 regmatch_T incl_regmatch;
3428 regmatch_T def_regmatch;
3429 int matched = FALSE;
3430 int did_show = FALSE;
3431 int found = FALSE;
3432 int i;
3433 char_u *already = NULL;
3434 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003435 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003436#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 win_T *curwin_save = NULL;
3438#endif
3439
3440 regmatch.regprog = NULL;
3441 incl_regmatch.regprog = NULL;
3442 def_regmatch.regprog = NULL;
3443
3444 file_line = alloc(LSIZE);
3445 if (file_line == NULL)
3446 return;
3447
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003449 // when CONT_SOL is set compare "ptr" with the beginning of the
3450 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3451 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 pat = alloc(len + 5);
3454 if (pat == NULL)
3455 goto fpip_end;
John Marriott8c85a2a2024-05-20 19:18:26 +02003456 vim_snprintf((char *)pat, len + 5, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003457 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003459 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 vim_free(pat);
3461 if (regmatch.regprog == NULL)
3462 goto fpip_end;
3463 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003464 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3465 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003467 incl_regmatch.regprog = vim_regcomp(inc_opt,
3468 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 if (incl_regmatch.regprog == NULL)
3470 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003471 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3474 {
John Marriott8c85a2a2024-05-20 19:18:26 +02003475 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL ? p_def : curbuf->b_p_def,
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003476 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (def_regmatch.regprog == NULL)
3478 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003479 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003481 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (files == NULL)
3483 goto fpip_end;
3484 old_files = max_path_depth;
3485 depth = depth_displayed = -1;
3486
3487 lnum = start_lnum;
3488 if (end_lnum > curbuf->b_ml.ml_line_count)
3489 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003490 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003492 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 for (;;)
3495 {
3496 if (incl_regmatch.regprog != NULL
3497 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3498 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003499 char_u *p_fname = (curr_fname == curbuf->b_fname)
3500 ? curbuf->b_ffname : curr_fname;
3501
3502 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003503 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003504 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003505 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003506 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3507 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003508 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003509 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003510 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 already_searched = FALSE;
3512 if (new_fname != NULL)
3513 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003514 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 for (i = 0;; i++)
3516 {
3517 if (i == depth + 1)
3518 i = old_files;
3519 if (i == max_path_depth)
3520 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003521 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3522 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003524 if (type != CHECK_PATH
3525 && action == ACTION_SHOW_ALL
3526 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003528 msg_putchar('\n'); // cursor below last one
3529 if (!got_int) // don't display if 'q'
3530 // typed at "--more--"
3531 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003534 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 prev_fname = NULL;
3536 }
3537 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003538 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 already_searched = TRUE;
3540 break;
3541 }
3542 }
3543 }
3544
3545 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3546 || (new_fname == NULL && !already_searched)))
3547 {
3548 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003549 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 else
3551 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003552 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003553 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003555 msg_puts_title(_("not found "));
3556 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 did_show = TRUE;
3559 while (depth_displayed < depth && !got_int)
3560 {
3561 ++depth_displayed;
3562 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003563 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003565 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003567 if (!got_int) // don't display if 'q' typed
3568 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 {
3570 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003571 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (new_fname != NULL)
3573 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003574 // using "new_fname" is more reliable, e.g., when
3575 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003576 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578 else
3579 {
3580 /*
3581 * Isolate the file name.
3582 * Include the surrounding "" or <> if present.
3583 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003584 if (inc_opt != NULL
3585 && strstr((char *)inc_opt, "\\zs") != NULL)
3586 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003587 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003588 p = incl_regmatch.startp[0];
3589 i = (int)(incl_regmatch.endp[0]
3590 - incl_regmatch.startp[0]);
3591 }
3592 else
3593 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003594 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003595 for (p = incl_regmatch.endp[0];
3596 *p && !vim_isfilec(*p); p++)
3597 ;
3598 for (i = 0; vim_isfilec(p[i]); i++)
3599 ;
3600 }
3601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 if (i == 0)
3603 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003604 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003606 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003608 // Avoid checking before the start of the line, can
3609 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003610 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 if (p[-1] == '"' || p[-1] == '<')
3613 {
3614 --p;
3615 ++i;
3616 }
3617 if (p[i] == '"' || p[i] == '>')
3618 ++i;
3619 }
3620 save_char = p[i];
3621 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003622 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 p[i] = save_char;
3624 }
3625
3626 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3627 {
3628 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003629 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003631 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003634 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636
3637 if (new_fname != NULL)
3638 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003639 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (depth + 1 == old_files)
3641 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003642 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 if (bigger != NULL)
3644 {
3645 for (i = 0; i <= depth; i++)
3646 bigger[i] = files[i];
3647 for (i = depth + 1; i < old_files + max_path_depth; i++)
3648 {
3649 bigger[i].fp = NULL;
3650 bigger[i].name = NULL;
3651 bigger[i].lnum = 0;
3652 bigger[i].matched = FALSE;
3653 }
3654 for (i = old_files; i < max_path_depth; i++)
3655 bigger[i + max_path_depth] = files[i];
3656 old_files += max_path_depth;
3657 max_path_depth *= 2;
3658 vim_free(files);
3659 files = bigger;
3660 }
3661 }
3662 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3663 == NULL)
3664 vim_free(new_fname);
3665 else
3666 {
3667 if (++depth == old_files)
3668 {
3669 /*
3670 * lalloc() for 'bigger' must have failed above. We
3671 * will forget one of our already visited files now.
3672 */
3673 vim_free(files[old_files].name);
3674 ++old_files;
3675 }
3676 files[depth].name = curr_fname = new_fname;
3677 files[depth].lnum = 0;
3678 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (action == ACTION_EXPAND)
3680 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003681 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003682 vim_snprintf((char*)IObuff, IOSIZE,
3683 _("Scanning included file: %s"),
3684 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003685 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003687 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003688 {
3689 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003690 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003691 (char *)new_fname);
3692 verbose_leave();
3693 }
3694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696 }
3697 }
3698 else
3699 {
3700 /*
3701 * Check if the line is a define (type == FIND_DEFINE)
3702 */
3703 p = line;
3704search_line:
3705 define_matched = FALSE;
3706 if (def_regmatch.regprog != NULL
3707 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3708 {
3709 /*
3710 * Pattern must be first identifier after 'define', so skip
3711 * to that position before checking for match of pattern. Also
3712 * don't let it match beyond the end of this identifier.
3713 */
3714 p = def_regmatch.endp[0];
3715 while (*p && !vim_iswordc(*p))
3716 p++;
3717 define_matched = TRUE;
3718 }
3719
3720 /*
3721 * Look for a match. Don't do this if we are looking for a
3722 * define and this line didn't match define_prog above.
3723 */
3724 if (def_regmatch.regprog == NULL || define_matched)
3725 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003726 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003728 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 startp = skipwhite(p);
3730 if (p_ic)
3731 matched = !MB_STRNICMP(startp, ptr, len);
3732 else
3733 matched = !STRNCMP(startp, ptr, len);
3734 if (matched && define_matched && whole
3735 && vim_iswordc(startp[len]))
3736 matched = FALSE;
3737 }
3738 else if (regmatch.regprog != NULL
3739 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3740 {
3741 matched = TRUE;
3742 startp = regmatch.startp[0];
3743 /*
3744 * Check if the line is not a comment line (unless we are
3745 * looking for a define). A line starting with "# define"
3746 * is not considered to be a comment line.
3747 */
3748 if (!define_matched && skip_comments)
3749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if ((*line != '#' ||
3751 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003752 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 matched = FALSE;
3754
3755 /*
3756 * Also check for a "/ *" or "/ /" before the match.
3757 * Skips lines like "int backwards; / * normal index
3758 * * /" when looking for "normal".
3759 * Note: Doesn't skip "/ *" in comments.
3760 */
3761 p = skipwhite(line);
3762 if (matched
3763 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 for (p = line; *p && p < startp; ++p)
3765 {
3766 if (matched
3767 && p[0] == '/'
3768 && (p[1] == '*' || p[1] == '/'))
3769 {
3770 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003771 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (p[1] == '/')
3773 break;
3774 ++p;
3775 }
3776 else if (!matched && p[0] == '*' && p[1] == '/')
3777 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003778 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 matched = TRUE;
3780 ++p;
3781 }
3782 }
3783 }
3784 }
3785 }
3786 }
3787 if (matched)
3788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 if (action == ACTION_EXPAND)
3790 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003791 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 int add_r;
3793 char_u *aux;
3794
3795 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3796 break;
3797 found = TRUE;
3798 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003799 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003801 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (vim_iswordp(p))
3803 goto exit_matched;
3804 p = find_word_start(p);
3805 }
3806 p = find_word_end(p);
3807 i = (int)(p - aux);
3808
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003809 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003811 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003813
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003814 // Get the next line: when "depth" < 0 from the current
3815 // buffer, otherwise from the included file. Jump to
3816 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003817 if (depth < 0)
3818 {
3819 if (lnum >= end_lnum)
3820 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003821 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003822 }
3823 else if (vim_fgets(line = file_line,
3824 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 goto exit_matched;
3826
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003827 // we read a line, set "already" to check this "line" later
3828 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003829 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 already = aux = p = skipwhite(line);
3831 p = find_word_start(p);
3832 p = find_word_end(p);
3833 if (p > aux)
3834 {
3835 if (*aux != ')' && IObuff[i-1] != TAB)
3836 {
3837 if (IObuff[i-1] != ' ')
3838 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003839 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (p_js
3841 && (IObuff[i-2] == '.'
3842 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3843 && (IObuff[i-2] == '?'
3844 || IObuff[i-2] == '!'))))
3845 IObuff[i++] = ' ';
3846 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003847 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 if (p - aux >= IOSIZE - i)
3849 p = aux + IOSIZE - i - 1;
3850 STRNCPY(IObuff + i, aux, p - aux);
3851 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003852 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854 IObuff[i] = NUL;
3855 aux = IObuff;
3856
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003857 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 goto exit_matched;
3859 }
3860
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003861 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003863 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003865 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003867 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 break;
3869 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003870 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 {
3872 found = TRUE;
3873 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003874 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (curr_fname != prev_fname)
3876 {
3877 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003878 msg_putchar('\n'); // cursor below last one
3879 if (!got_int) // don't display if 'q' typed
3880 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 msg_home_replace_hl(curr_fname);
3882 prev_fname = curr_fname;
3883 }
3884 did_show = TRUE;
3885 if (!got_int)
3886 show_pat_in_path(line, type, TRUE, action,
3887 (depth == -1) ? NULL : files[depth].fp,
3888 (depth == -1) ? &lnum : &files[depth].lnum,
3889 match_count++);
3890
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003891 // Set matched flag for this file and all the ones that
3892 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 for (i = 0; i <= depth; ++i)
3894 files[i].matched = TRUE;
3895 }
3896 else if (--count <= 0)
3897 {
3898 found = TRUE;
3899 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003900#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 && g_do_tagpreview == 0
3902#endif
3903 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003904 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 else if (action == ACTION_SHOW)
3906 {
3907 show_pat_in_path(line, type, did_show, action,
3908 (depth == -1) ? NULL : files[depth].fp,
3909 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3910 did_show = TRUE;
3911 }
3912 else
3913 {
3914#ifdef FEAT_GUI
3915 need_mouse_correct = TRUE;
3916#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003917#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003918 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 if (g_do_tagpreview != 0)
3920 {
3921 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003922 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924#endif
3925 if (action == ACTION_SPLIT)
3926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003929 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 if (depth == -1)
3932 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003933 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003934#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (g_do_tagpreview != 0)
3936 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003937 if (!win_valid(curwin_save))
3938 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003939 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003940 curwin_save->w_buffer->b_fnum, NULL,
Colin Kennedy21570352024-03-03 16:16:47 +01003941 NULL, TRUE, lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003942 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944 else
3945#endif
3946 setpcmark();
3947 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003948 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 else
3951 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003952 if (!GETFILE_SUCCESS(getfile(
3953 0, files[depth].name, NULL, TRUE,
Colin Kennedy21570352024-03-03 16:16:47 +01003954 files[depth].lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003955 break; // failed to jump to file
3956 // autocommands may have changed the lnum, we don't
3957 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 curwin->w_cursor.lnum = files[depth].lnum;
3959 }
3960 }
3961 if (action != ACTION_SHOW)
3962 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003963 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 curwin->w_set_curswant = TRUE;
3965 }
3966
Bram Moolenaar4033c552017-09-16 20:54:51 +02003967#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003969 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003971 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003973 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 win_enter(curwin_save, TRUE);
3975 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003976# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003977 else if (WIN_IS_POPUP(curwin))
3978 // can't keep focus in popup window
3979 win_enter(firstwin, TRUE);
3980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#endif
3982 break;
3983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003986 // look for other matches in the rest of the line if we
3987 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003990 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003991 && *startp != NUL
John Marriott8c85a2a2024-05-20 19:18:26 +02003992 && *(startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 goto search_line;
3994 }
3995 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003997 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003998 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 break;
4000
4001 /*
4002 * Read the next line. When reading an included file and encountering
4003 * end-of-file, close the file and continue in the file that included
4004 * it.
4005 */
4006 while (depth >= 0 && !already
4007 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4008 {
4009 fclose(files[depth].fp);
4010 --old_files;
4011 files[old_files].name = files[depth].name;
4012 files[old_files].matched = files[depth].matched;
4013 --depth;
4014 curr_fname = (depth == -1) ? curbuf->b_fname
4015 : files[depth].name;
4016 if (depth < depth_displayed)
4017 depth_displayed = depth;
4018 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004019 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004020 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004022 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004023 i = (int)STRLEN(line);
4024 if (i > 0 && line[i - 1] == '\n')
4025 line[--i] = NUL;
4026 if (i > 0 && line[i - 1] == '\r')
4027 line[--i] = NUL;
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 else if (!already)
4030 {
4031 if (++lnum > end_lnum)
4032 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01004033 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 already = NULL;
4036 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004037 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004039 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 for (i = 0; i <= depth; i++)
4041 {
4042 fclose(files[i].fp);
4043 vim_free(files[i].name);
4044 }
4045 for (i = old_files; i < max_path_depth; i++)
4046 vim_free(files[i].name);
4047 vim_free(files);
4048
4049 if (type == CHECK_PATH)
4050 {
4051 if (!did_show)
4052 {
4053 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004054 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004056 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
4058 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004059 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004061 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004062 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004064 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004066 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4069 msg_end();
4070
4071fpip_end:
4072 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02004073 vim_regfree(regmatch.regprog);
4074 vim_regfree(incl_regmatch.regprog);
4075 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076}
4077
4078 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004079show_pat_in_path(
4080 char_u *line,
4081 int type,
4082 int did_show,
4083 int action,
4084 FILE *fp,
4085 linenr_T *lnum,
4086 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087{
4088 char_u *p;
John Marriott8c85a2a2024-05-20 19:18:26 +02004089 size_t linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090
4091 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004092 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004093 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004094 gotocmdline(TRUE); // cursor at status line
4095 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 return;
John Marriott8c85a2a2024-05-20 19:18:26 +02004097 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 for (;;)
4099 {
John Marriott8c85a2a2024-05-20 19:18:26 +02004100 p = line + linelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (fp != NULL)
4102 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004103 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 if (p >= line && *p == '\n')
4105 --p;
4106 if (p >= line && *p == '\r')
4107 --p;
4108 *(p + 1) = NUL;
4109 }
4110 if (action == ACTION_SHOW_ALL)
4111 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004112 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004113 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004114 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4115 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004116 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4117 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004119 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004120 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004122 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4124 break;
4125
4126 if (fp != NULL)
4127 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004128 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 break;
John Marriott8c85a2a2024-05-20 19:18:26 +02004130 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 ++*lnum;
4132 }
4133 else
4134 {
4135 if (++*lnum > curbuf->b_ml.ml_line_count)
4136 break;
4137 line = ml_get(*lnum);
John Marriott8c85a2a2024-05-20 19:18:26 +02004138 linelen = ml_get_len(*lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 msg_putchar('\n');
4141 }
4142}
4143#endif
4144
4145#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004146/*
4147 * Return the last used search pattern at "idx".
4148 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004149 spat_T *
4150get_spat(int idx)
4151{
4152 return &spats[idx];
4153}
4154
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004155/*
4156 * Return the last used search pattern index.
4157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004159get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004161 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004164
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004165#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004166/*
4167 * "searchcount()" function
4168 */
4169 void
4170f_searchcount(typval_T *argvars, typval_T *rettv)
4171{
4172 pos_T pos = curwin->w_cursor;
4173 char_u *pattern = NULL;
4174 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4175 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004176 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004177 searchstat_T stat;
4178
4179 if (rettv_dict_alloc(rettv) == FAIL)
4180 return;
4181
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004182 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4183 return;
4184
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004185 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4186 recompute = TRUE;
4187
4188 if (argvars[0].v_type != VAR_UNKNOWN)
4189 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004190 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004191 dictitem_T *di;
4192 listitem_T *li;
4193 int error = FALSE;
4194
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004195 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004196 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004197 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004198 di = dict_find(dict, (char_u *)"timeout", -1);
4199 if (di != NULL)
4200 {
4201 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4202 if (error)
4203 return;
4204 }
4205 di = dict_find(dict, (char_u *)"maxcount", -1);
4206 if (di != NULL)
4207 {
4208 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4209 if (error)
4210 return;
4211 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004212 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004213 di = dict_find(dict, (char_u *)"pattern", -1);
4214 if (di != NULL)
4215 {
4216 pattern = tv_get_string_chk(&di->di_tv);
4217 if (pattern == NULL)
4218 return;
4219 }
4220 di = dict_find(dict, (char_u *)"pos", -1);
4221 if (di != NULL)
4222 {
4223 if (di->di_tv.v_type != VAR_LIST)
4224 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004225 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004226 return;
4227 }
4228 if (list_len(di->di_tv.vval.v_list) != 3)
4229 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004230 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004231 return;
4232 }
4233 li = list_find(di->di_tv.vval.v_list, 0L);
4234 if (li != NULL)
4235 {
4236 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4237 if (error)
4238 return;
4239 }
4240 li = list_find(di->di_tv.vval.v_list, 1L);
4241 if (li != NULL)
4242 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004243 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004244 if (error)
4245 return;
4246 }
4247 li = list_find(di->di_tv.vval.v_list, 2L);
4248 if (li != NULL)
4249 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004250 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004251 if (error)
4252 return;
4253 }
4254 }
4255 }
4256
4257 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004258#ifdef FEAT_SEARCH_EXTRA
4259 save_incsearch_state();
4260#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004261 if (pattern != NULL)
4262 {
4263 if (*pattern == NUL)
4264 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004265 vim_free(spats[last_idx].pat);
John Marriott8c85a2a2024-05-20 19:18:26 +02004266 spats[last_idx].patlen = STRLEN(pattern);
4267 spats[last_idx].pat = vim_strnsave(pattern, spats[last_idx].patlen);
4268 if (spats[last_idx].pat == NULL)
4269 spats[last_idx].patlen = 0;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004270 }
4271 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4272 goto the_end; // the previous pattern was never defined
4273
4274 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4275
4276 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4277 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4278 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4279 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4280 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4281
4282the_end:
4283 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004284#ifdef FEAT_SEARCH_EXTRA
4285 restore_incsearch_state();
4286#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004287}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004288#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004289
4290/*
4291 * Fuzzy string matching
4292 *
4293 * Ported from the lib_fts library authored by Forrest Smith.
4294 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4295 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004296 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004297 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4298 *
4299 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004300 * - Matched letter
4301 * - Unmatched letter
4302 * - Consecutively matched letters
4303 * - Proximity to start
4304 * - Letter following a separator (space, underscore)
4305 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004306 *
4307 * Matched letters are good. Unmatched letters are bad. Matching near the start
4308 * is good. Matching the first letter in the middle of a phrase is good.
4309 * Matching the uppercase letters in camel case entries is good.
4310 *
4311 * The score assigned for each factor is explained below.
4312 * File paths are different from file names. File extensions may be ignorable.
4313 * Single words care about consecutive matches but not separators or camel
4314 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004315 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004316 * Matched letter: +0 points
4317 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004318 * Consecutive match bonus: +15 points
4319 * First letter bonus: +15 points
4320 * Separator bonus: +30 points
4321 * Camel case bonus: +30 points
4322 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004323 *
4324 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004325 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004326 * lower minimum score due to unmatched letter penalty. Longer search patterns
4327 * have a higher maximum score due to match bonuses.
4328 *
4329 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4330 * quite a bit.
4331 *
4332 * There is a penalty if you DON’T match the first three letters. Which
4333 * effectively rewards matching near the start. However there’s no difference
4334 * in matching between the middle and end.
4335 *
4336 * There is not an explicit bonus for an exact match. Unmatched letters receive
4337 * a penalty. So shorter strings and closer matches are worth more.
4338 */
4339typedef struct
4340{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004341 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004342 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004343 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004344 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004345} fuzzyItem_T;
4346
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004347// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4348// matching a whole word is preferred.
4349#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004350// bonus if match occurs after a path separator
4351#define PATH_SEPARATOR_BONUS 30
4352// bonus if match occurs after a word separator
4353#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004354// bonus if match is uppercase and prev is lower
4355#define CAMEL_BONUS 30
4356// bonus if the first letter is matched
4357#define FIRST_LETTER_BONUS 15
4358// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004359#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004360// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004361#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004362// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004363#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004364// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004365#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004366// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004367#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004368
4369#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004370
4371/*
4372 * Compute a score for a fuzzy matched string. The matching character locations
4373 * are in 'matches'.
4374 */
4375 static int
4376fuzzy_match_compute_score(
4377 char_u *str,
4378 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004379 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004380 int numMatches)
4381{
4382 int score;
4383 int penalty;
4384 int unmatched;
4385 int i;
4386 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004387 int_u sidx = 0;
glepnir5a049992024-12-26 15:38:39 +01004388 int is_exact_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004389
4390 // Initialize score
4391 score = 100;
4392
4393 // Apply leading letter penalty
4394 penalty = LEADING_LETTER_PENALTY * matches[0];
4395 if (penalty < MAX_LEADING_LETTER_PENALTY)
4396 penalty = MAX_LEADING_LETTER_PENALTY;
4397 score += penalty;
4398
4399 // Apply unmatched penalty
4400 unmatched = strSz - numMatches;
4401 score += UNMATCHED_LETTER_PENALTY * unmatched;
4402
4403 // Apply ordering bonuses
4404 for (i = 0; i < numMatches; ++i)
4405 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004406 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004407
4408 if (i > 0)
4409 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004410 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004411
4412 // Sequential
4413 if (currIdx == (prevIdx + 1))
4414 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004415 else
4416 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004417 }
4418
4419 // Check for bonuses based on neighbor character value
4420 if (currIdx > 0)
4421 {
4422 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004423 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004424 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004425
4426 if (has_mbyte)
4427 {
4428 while (sidx < currIdx)
4429 {
4430 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004431 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004432 sidx++;
4433 }
4434 curr = (*mb_ptr2char)(p);
4435 }
4436 else
4437 {
4438 neighbor = str[currIdx - 1];
4439 curr = str[currIdx];
4440 }
4441
4442 if (vim_islower(neighbor) && vim_isupper(curr))
4443 score += CAMEL_BONUS;
4444
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004445 // Bonus if the match follows a separator character
4446 if (neighbor == '/' || neighbor == '\\')
4447 score += PATH_SEPARATOR_BONUS;
4448 else if (neighbor == ' ' || neighbor == '_')
4449 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004450 }
4451 else
4452 {
4453 // First letter
4454 score += FIRST_LETTER_BONUS;
4455 }
glepnir5a049992024-12-26 15:38:39 +01004456 // Check exact match condition
4457 if (currIdx != (int_u)i)
4458 is_exact_match = FALSE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004459 }
glepnir5a049992024-12-26 15:38:39 +01004460 // Boost score for exact matches
4461 if (is_exact_match && numMatches == strSz)
4462 score += 100;
4463
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004464 return score;
4465}
4466
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004467/*
4468 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4469 * Return the number of matching characters.
4470 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004471 static int
4472fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004473 char_u *fuzpat,
4474 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004475 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004476 int *outScore,
4477 char_u *strBegin,
4478 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004479 int_u *srcMatches,
4480 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004481 int maxMatches,
4482 int nextMatch,
4483 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004484{
4485 // Recursion params
4486 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004487 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004488 int bestRecursiveScore = 0;
4489 int first_match;
4490 int matched;
4491
4492 // Count recursions
4493 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004494 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004495 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004496
4497 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004498 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004499 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004500
4501 // Loop through fuzpat and str looking for a match
4502 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004503 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004504 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004505 int c1;
4506 int c2;
4507
4508 c1 = PTR2CHAR(fuzpat);
4509 c2 = PTR2CHAR(str);
4510
Bram Moolenaar635414d2020-09-11 22:25:15 +02004511 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004512 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004513 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004514 // Supplied matches buffer was too short
4515 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004516 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004517
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004518 int recursiveScore = 0;
4519 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4520 CLEAR_FIELD(recursiveMatches);
4521
Bram Moolenaar635414d2020-09-11 22:25:15 +02004522 // "Copy-on-Write" srcMatches into matches
4523 if (first_match && srcMatches)
4524 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004525 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004526 first_match = FALSE;
4527 }
4528
4529 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004530 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004531 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4532 &recursiveScore, strBegin, strLen, matches,
4533 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004534 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004535 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004536 {
4537 // Pick best recursive score
4538 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4539 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004540 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004541 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004542 bestRecursiveScore = recursiveScore;
4543 }
4544 recursiveMatch = TRUE;
4545 }
4546
4547 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004548 matches[nextMatch++] = strIdx;
4549 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004550 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004551 else
4552 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004553 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004554 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004555 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004556 else
4557 ++str;
4558 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004559 }
4560
4561 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004562 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004563
4564 // Calculate score
4565 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004566 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4567 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004568
4569 // Return best result
4570 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4571 {
4572 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004573 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004574 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004575 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004576 }
4577 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004578 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004579
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004580 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004581}
4582
4583/*
4584 * fuzzy_match()
4585 *
4586 * Performs exhaustive search via recursion to find all possible matches and
4587 * match with highest score.
4588 * Scores values have no intrinsic meaning. Possible score range is not
4589 * normalized and varies with pattern.
4590 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004591 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004592 * Uses char_u for match indices. Therefore patterns are limited to
4593 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004594 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004595 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4596 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004597 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004598 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004599fuzzy_match(
4600 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004601 char_u *pat_arg,
4602 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004603 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004604 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004605 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004606{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004607 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004608 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004609 char_u *save_pat;
4610 char_u *pat;
4611 char_u *p;
4612 int complete = FALSE;
4613 int score = 0;
4614 int numMatches = 0;
4615 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004616
4617 *outScore = 0;
4618
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004619 save_pat = vim_strsave(pat_arg);
4620 if (save_pat == NULL)
4621 return FALSE;
4622 pat = save_pat;
4623 p = pat;
4624
4625 // Try matching each word in 'pat_arg' in 'str'
4626 while (TRUE)
4627 {
4628 if (matchseq)
4629 complete = TRUE;
4630 else
4631 {
4632 // Extract one word from the pattern (separated by space)
4633 p = skipwhite(p);
4634 if (*p == NUL)
4635 break;
4636 pat = p;
4637 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4638 {
4639 if (has_mbyte)
4640 MB_PTR_ADV(p);
4641 else
4642 ++p;
4643 }
4644 if (*p == NUL) // processed all the words
4645 complete = TRUE;
4646 *p = NUL;
4647 }
4648
4649 score = 0;
4650 recursionCount = 0;
4651 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4652 matches + numMatches, maxMatches - numMatches,
4653 0, &recursionCount);
4654 if (matchCount == 0)
4655 {
4656 numMatches = 0;
4657 break;
4658 }
4659
4660 // Accumulate the match score and the number of matches
4661 *outScore += score;
4662 numMatches += matchCount;
4663
4664 if (complete)
4665 break;
4666
4667 // try matching the next word
4668 ++p;
4669 }
4670
4671 vim_free(save_pat);
4672 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004673}
4674
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004675#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004676/*
4677 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004678 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004679 */
4680 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004681fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004682{
4683 int v1 = ((fuzzyItem_T *)s1)->score;
4684 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004685 int idx1 = ((fuzzyItem_T *)s1)->idx;
4686 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004687
zeertzjq77078272024-02-10 13:24:03 +01004688 if (v1 == v2)
4689 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4690 else
4691 return v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004692}
4693
4694/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004695 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4696 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004697 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4698 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004699 * If 'items' is a list of strings, then search for 'str' in the list.
4700 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4701 * for each item or use 'item_cb' Funcref function to get the string.
4702 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4703 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004704 */
4705 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004706fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004707 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004708 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004709 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004710 char_u *key,
4711 callback_T *item_cb,
4712 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004713 list_T *fmatchlist,
4714 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004715{
4716 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004717 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004718 listitem_T *li;
4719 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004720 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004721 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004722
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004723 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004724 if (len == 0)
4725 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004726 if (max_matches > 0 && len > max_matches)
4727 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004728
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004729 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4730 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004731 return;
4732
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004733 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004734 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004735 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004736 int score;
4737 char_u *itemstr;
4738 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004739
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004740 if (max_matches > 0 && match_count >= max_matches)
4741 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004742
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004743 itemstr = NULL;
4744 rettv.v_type = VAR_UNKNOWN;
4745 if (li->li_tv.v_type == VAR_STRING) // list of strings
4746 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004747 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004748 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004749 {
4750 // For a dict, either use the specified key to lookup the string or
4751 // use the specified callback function to get the string.
4752 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004753 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4754 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004755 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004756 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004757 typval_T argv[2];
4758
4759 // Invoke the supplied callback (if any) to get the dict item
4760 li->li_tv.vval.v_dict->dv_refcount++;
4761 argv[0].v_type = VAR_DICT;
4762 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4763 argv[1].v_type = VAR_UNKNOWN;
4764 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4765 {
4766 if (rettv.v_type == VAR_STRING)
4767 itemstr = rettv.vval.v_string;
4768 }
4769 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004770 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004771 }
4772
4773 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004774 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004775 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004776 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004777 items[match_count].idx = match_count;
4778 items[match_count].item = li;
4779 items[match_count].score = score;
4780
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004781 // Copy the list of matching positions in itemstr to a list, if
4782 // 'retmatchpos' is set.
4783 if (retmatchpos)
4784 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004785 int j = 0;
4786 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004787
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004788 items[match_count].lmatchpos = list_alloc();
4789 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004790 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004791
4792 p = str;
4793 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004794 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004795 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004796 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004797 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004798 matches[j]) == FAIL)
4799 goto done;
4800 j++;
4801 }
4802 if (has_mbyte)
4803 MB_PTR_ADV(p);
4804 else
4805 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004806 }
4807 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004808 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004809 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004810 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004811 }
4812
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004813 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004814 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004815 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004816
Bram Moolenaar635414d2020-09-11 22:25:15 +02004817 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004818 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004819 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004820
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004821 // For matchfuzzy(), return a list of matched strings.
4822 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004823 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004824 // The first item is a list of matched strings. The second item
4825 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004826 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004827 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4828 if (retmatchpos)
4829 {
4830 li = list_find(fmatchlist, 0);
4831 if (li == NULL || li->li_tv.vval.v_list == NULL)
4832 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004833 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004834 }
4835 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004836 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004837
4838 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004839 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004840 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004841 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004842 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004843 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004844 }
4845
4846 // next copy the list of matching positions
4847 if (retmatchpos)
4848 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004849 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004850 if (li == NULL || li->li_tv.vval.v_list == NULL)
4851 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004852 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004853
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004854 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004855 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004856 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004857 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004858 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004859 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004860 goto done;
4861 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004862
4863 // copy the matching scores
4864 li = list_find(fmatchlist, -1);
4865 if (li == NULL || li->li_tv.vval.v_list == NULL)
4866 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004867 retlist = li->li_tv.vval.v_list;
4868 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004869 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004870 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004871 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004872 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004873 goto done;
4874 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004875 }
4876 }
4877
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004878done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004879 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004880}
4881
4882/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004883 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4884 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4885 */
4886 static void
4887do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4888{
4889 callback_T cb;
4890 char_u *key = NULL;
4891 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004892 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004893 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004894
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004895 if (in_vim9script()
4896 && (check_for_list_arg(argvars, 0) == FAIL
4897 || check_for_string_arg(argvars, 1) == FAIL
4898 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4899 return;
4900
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004901 CLEAR_POINTER(&cb);
4902
4903 // validate and get the arguments
4904 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4905 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004906 semsg(_(e_argument_of_str_must_be_list),
4907 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004908 return;
4909 }
4910 if (argvars[1].v_type != VAR_STRING
4911 || argvars[1].vval.v_string == NULL)
4912 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004913 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004914 return;
4915 }
4916
4917 if (argvars[2].v_type != VAR_UNKNOWN)
4918 {
4919 dict_T *d;
4920 dictitem_T *di;
4921
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004922 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004923 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004924
4925 // To search a dict, either a callback function or a key can be
4926 // specified.
4927 d = argvars[2].vval.v_dict;
4928 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4929 {
4930 if (di->di_tv.v_type != VAR_STRING
4931 || di->di_tv.vval.v_string == NULL
4932 || *di->di_tv.vval.v_string == NUL)
4933 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004934 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004935 return;
4936 }
4937 key = tv_get_string(&di->di_tv);
4938 }
4939 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4940 {
4941 cb = get_callback(&di->di_tv);
4942 if (cb.cb_name == NULL)
4943 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004944 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004945 return;
4946 }
4947 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004948
4949 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004950 {
4951 if (di->di_tv.v_type != VAR_NUMBER)
4952 {
4953 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4954 return;
4955 }
4956 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4957 }
4958
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004959 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004960 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004961 }
4962
4963 // get the fuzzy matches
4964 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004965 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004966 goto done;
4967 if (retmatchpos)
4968 {
4969 list_T *l;
4970
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004971 // For matchfuzzypos(), a list with three items are returned. First
4972 // item is a list of matching strings, the second item is a list of
4973 // lists with matching positions within each string and the third item
4974 // is the list of scores of the matches.
4975 l = list_alloc();
4976 if (l == NULL)
4977 goto done;
4978 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004979 {
4980 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004981 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004982 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004983 l = list_alloc();
4984 if (l == NULL)
4985 goto done;
4986 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004987 {
4988 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004989 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004990 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004991 l = list_alloc();
4992 if (l == NULL)
4993 goto done;
4994 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004995 {
4996 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004997 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004998 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004999 }
5000
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02005001 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01005002 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005003
5004done:
5005 free_callback(&cb);
5006}
5007
5008/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02005009 * "matchfuzzy()" function
5010 */
5011 void
5012f_matchfuzzy(typval_T *argvars, typval_T *rettv)
5013{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005014 do_fuzzymatch(argvars, rettv, FALSE);
5015}
5016
5017/*
5018 * "matchfuzzypos()" function
5019 */
5020 void
5021f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
5022{
5023 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02005024}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02005025#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005026
5027/*
5028 * Same as fuzzy_match_item_compare() except for use with a string match
5029 */
5030 static int
5031fuzzy_match_str_compare(const void *s1, const void *s2)
5032{
5033 int v1 = ((fuzmatch_str_T *)s1)->score;
5034 int v2 = ((fuzmatch_str_T *)s2)->score;
5035 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5036 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5037
Christian Brabandte06e4372024-02-09 19:39:14 +01005038 if (v1 == v2)
5039 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5040 else
5041 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005042}
5043
5044/*
5045 * Sort fuzzy matches by score
5046 */
5047 static void
5048fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
5049{
5050 // Sort the list by the descending order of the match score
5051 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5052 fuzzy_match_str_compare);
5053}
5054
5055/*
5056 * Same as fuzzy_match_item_compare() except for use with a function name
5057 * string match. <SNR> functions should be sorted to the end.
5058 */
5059 static int
5060fuzzy_match_func_compare(const void *s1, const void *s2)
5061{
5062 int v1 = ((fuzmatch_str_T *)s1)->score;
5063 int v2 = ((fuzmatch_str_T *)s2)->score;
5064 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5065 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5066 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
5067 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
5068
Christian Brabandte06e4372024-02-09 19:39:14 +01005069 if (*str1 != '<' && *str2 == '<')
5070 return -1;
5071 if (*str1 == '<' && *str2 != '<')
5072 return 1;
5073 if (v1 == v2)
5074 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5075 else
5076 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005077}
5078
5079/*
5080 * Sort fuzzy matches of function names by score.
5081 * <SNR> functions should be sorted to the end.
5082 */
5083 static void
5084fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
5085{
5086 // Sort the list by the descending order of the match score
5087 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5088 fuzzy_match_func_compare);
5089}
5090
5091/*
5092 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
5093 * returns the match score.
5094 */
5095 int
5096fuzzy_match_str(char_u *str, char_u *pat)
5097{
5098 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00005099 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005100
5101 if (str == NULL || pat == NULL)
5102 return 0;
5103
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00005104 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005105 sizeof(matchpos) / sizeof(matchpos[0]));
5106
5107 return score;
5108}
5109
5110/*
glepnir40c1c332024-06-11 19:37:04 +02005111 * Fuzzy match the position of string 'pat' in string 'str'.
5112 * Returns a dynamic array of matching positions. If there is no match,
5113 * returns NULL.
5114 */
5115 garray_T *
5116fuzzy_match_str_with_pos(char_u *str UNUSED, char_u *pat UNUSED)
5117{
5118#ifdef FEAT_SEARCH_EXTRA
5119 int score = 0;
zeertzjq2f95ca92024-06-13 17:14:27 +02005120 garray_T *match_positions = NULL;
5121 int_u matches[MAX_FUZZY_MATCHES];
5122 int j = 0;
glepnir40c1c332024-06-11 19:37:04 +02005123
zeertzjq2f95ca92024-06-13 17:14:27 +02005124 if (str == NULL || pat == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005125 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005126
5127 match_positions = ALLOC_ONE(garray_T);
glepnir40c1c332024-06-11 19:37:04 +02005128 if (match_positions == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005129 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005130 ga_init2(match_positions, sizeof(int_u), 10);
5131
5132 if (!fuzzy_match(str, pat, FALSE, &score, matches, MAX_FUZZY_MATCHES)
5133 || score == 0)
glepnir40c1c332024-06-11 19:37:04 +02005134 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005135 ga_clear(match_positions);
5136 vim_free(match_positions);
5137 return NULL;
glepnir40c1c332024-06-11 19:37:04 +02005138 }
5139
zeertzjq2f95ca92024-06-13 17:14:27 +02005140 for (char_u *p = pat; *p != NUL; MB_PTR_ADV(p))
glepnir40c1c332024-06-11 19:37:04 +02005141 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005142 if (!VIM_ISWHITE(PTR2CHAR(p)))
5143 {
5144 ga_grow(match_positions, 1);
5145 ((int_u *)match_positions->ga_data)[match_positions->ga_len] =
5146 matches[j];
5147 match_positions->ga_len++;
5148 j++;
5149 }
glepnir40c1c332024-06-11 19:37:04 +02005150 }
5151
glepnir40c1c332024-06-11 19:37:04 +02005152 return match_positions;
glepnir40c1c332024-06-11 19:37:04 +02005153#else
5154 return NULL;
5155#endif
5156}
5157
5158/*
glepnir8159fb12024-07-17 20:32:54 +02005159 * This function searches for a fuzzy match of the pattern `pat` within the
5160 * line pointed to by `*ptr`. It splits the line into words, performs fuzzy
5161 * matching on each word, and returns the length and position of the first
5162 * matched word.
5163 */
5164 static int
5165fuzzy_match_str_in_line(char_u **ptr, char_u *pat, int *len, pos_T *current_pos)
5166{
5167 char_u *str = *ptr;
5168 char_u *strBegin = str;
5169 char_u *end = NULL;
5170 char_u *start = NULL;
5171 int found = FALSE;
5172 int result;
5173 char save_end;
5174
5175 if (str == NULL || pat == NULL)
5176 return found;
5177
5178 while (*str != NUL)
5179 {
5180 // Skip non-word characters
5181 start = find_word_start(str);
5182 if (*start == NUL)
5183 break;
5184 end = find_word_end(start);
5185
5186 // Extract the word from start to end
5187 save_end = *end;
5188 *end = NUL;
5189
5190 // Perform fuzzy match
5191 result = fuzzy_match_str(start, pat);
5192 *end = save_end;
5193
5194 if (result > 0)
5195 {
5196 *len = (int)(end - start);
5197 current_pos->col += (int)(end - strBegin);
5198 found = TRUE;
5199 *ptr = start;
5200 break;
5201 }
5202
5203 // Move to the end of the current word for the next iteration
5204 str = end;
5205 // Ensure we continue searching after the current word
5206 while (*str != NUL && !vim_iswordp(str))
5207 MB_PTR_ADV(str);
5208 }
5209
5210 return found;
5211}
5212
5213/*
5214 * Search for the next fuzzy match in the specified buffer.
5215 * This function attempts to find the next occurrence of the given pattern
5216 * in the buffer, starting from the current position. It handles line wrapping
5217 * and direction of search.
5218 *
5219 * Return TRUE if a match is found, otherwise FALSE.
5220 */
5221 int
5222search_for_fuzzy_match(
5223 buf_T *buf,
5224 pos_T *pos,
5225 char_u *pattern,
5226 int dir,
5227 pos_T *start_pos,
5228 int *len,
5229 char_u **ptr,
5230 int whole_line)
5231{
5232 pos_T current_pos = *pos;
5233 pos_T circly_end;
zeertzjq58d70522024-08-31 17:05:39 +02005234 int found_new_match = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02005235 int looped_around = FALSE;
glepnir7cfe6932024-09-15 20:06:28 +02005236 char_u *next_word_end = NULL;
5237 char_u *match_word = NULL;
glepnir8159fb12024-07-17 20:32:54 +02005238
5239 if (whole_line)
5240 current_pos.lnum += dir;
5241
glepnir0be03e12024-07-19 16:45:05 +02005242 if (buf == curbuf)
5243 circly_end = *start_pos;
5244 else
5245 {
5246 circly_end.lnum = buf->b_ml.ml_line_count;
5247 circly_end.col = 0;
5248 circly_end.coladd = 0;
5249 }
5250
glepnir8159fb12024-07-17 20:32:54 +02005251 do {
glepnir8159fb12024-07-17 20:32:54 +02005252
5253 // Check if looped around and back to start position
5254 if (looped_around && EQUAL_POS(current_pos, circly_end))
5255 break;
5256
5257 // Ensure current_pos is valid
5258 if (current_pos.lnum >= 1 && current_pos.lnum <= buf->b_ml.ml_line_count)
5259 {
5260 // Get the current line buffer
5261 *ptr = ml_get_buf(buf, current_pos.lnum, FALSE);
5262 // If ptr is end of line is reached, move to next line
5263 // or previous line based on direction
5264 if (**ptr != NUL)
5265 {
5266 if (!whole_line)
5267 {
5268 *ptr += current_pos.col;
5269 // Try to find a fuzzy match in the current line starting from current position
5270 found_new_match = fuzzy_match_str_in_line(ptr, pattern, len, &current_pos);
5271 if (found_new_match)
5272 {
glepnir7cfe6932024-09-15 20:06:28 +02005273 if (ctrl_x_mode_normal())
5274 {
5275 match_word = vim_strnsave(*ptr, *len);
5276 if (STRCMP(match_word, pattern) == 0)
5277 {
5278 next_word_end = find_word_start(*ptr + *len);
5279 if (*next_word_end != NUL && *next_word_end != NL)
5280 {
5281 // Find end of the word.
5282 if (has_mbyte)
5283 while (*next_word_end != NUL)
5284 {
5285 int l = (*mb_ptr2len)(next_word_end);
5286
5287 if (l < 2 && !vim_iswordc(*next_word_end))
5288 break;
5289 next_word_end += l;
5290 }
5291 else
5292 next_word_end = find_word_end(next_word_end);
5293 }
5294 else if (looped_around)
5295 found_new_match = FALSE;
5296
5297 *len = next_word_end - *ptr;
5298 current_pos.col = *len;
5299 }
5300 vim_free(match_word);
5301 }
glepnir8159fb12024-07-17 20:32:54 +02005302 *pos = current_pos;
5303 break;
5304 }
glepnir0be03e12024-07-19 16:45:05 +02005305 else if (looped_around && current_pos.lnum == circly_end.lnum)
5306 break;
glepnir8159fb12024-07-17 20:32:54 +02005307 }
5308 else
5309 {
5310 if (fuzzy_match_str(*ptr, pattern) > 0)
5311 {
5312 found_new_match = TRUE;
5313 *pos = current_pos;
Ken Takata073cb022024-07-28 17:08:15 +02005314 *len = (int)STRLEN(*ptr);
glepnir8159fb12024-07-17 20:32:54 +02005315 break;
5316 }
5317 }
5318 }
5319 }
5320
5321 // Move to the next line or previous line based on direction
5322 if (dir == FORWARD)
5323 {
5324 if (++current_pos.lnum > buf->b_ml.ml_line_count)
5325 {
5326 if (p_ws)
5327 {
5328 current_pos.lnum = 1;
5329 looped_around = TRUE;
5330 }
5331 else
5332 break;
5333 }
5334 }
5335 else
5336 {
5337 if (--current_pos.lnum < 1)
5338 {
5339 if (p_ws)
5340 {
5341 current_pos.lnum = buf->b_ml.ml_line_count;
5342 looped_around = TRUE;
5343 }
5344 else
5345 break;
5346
5347 }
5348 }
5349 current_pos.col = 0;
5350 } while (TRUE);
5351
5352 return found_new_match;
5353}
5354
5355/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005356 * Free an array of fuzzy string matches "fuzmatch[count]".
5357 */
5358 void
5359fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5360{
5361 int i;
5362
5363 if (fuzmatch == NULL)
5364 return;
5365 for (i = 0; i < count; ++i)
5366 vim_free(fuzmatch[i].str);
5367 vim_free(fuzmatch);
5368}
5369
5370/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005371 * Copy a list of fuzzy matches into a string list after sorting the matches by
5372 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5373 * Returns OK on success and FAIL on memory allocation failure.
5374 */
5375 int
5376fuzzymatches_to_strmatches(
5377 fuzmatch_str_T *fuzmatch,
5378 char_u ***matches,
5379 int count,
5380 int funcsort)
5381{
5382 int i;
5383
5384 if (count <= 0)
5385 return OK;
5386
5387 *matches = ALLOC_MULT(char_u *, count);
5388 if (*matches == NULL)
5389 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005390 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005391 return FAIL;
5392 }
5393
5394 // Sort the list by the descending order of the match score
5395 if (funcsort)
5396 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5397 else
5398 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5399
5400 for (i = 0; i < count; i++)
5401 (*matches)[i] = fuzmatch[i].str;
5402 vim_free(fuzmatch);
5403
5404 return OK;
5405}