blob: 661bcc7a59c3116e371e527408e9bb60231e47af [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}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100424#endif
425
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426/*
427 * Return TRUE when case should be ignored for search pattern "pat".
428 * Uses the 'ignorecase' and 'smartcase' options.
429 */
430 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100431ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200433 return ignorecase_opt(pat, p_ic, p_scs);
434}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200436/*
437 * As ignorecase() put pass the "ic" and "scs" flags.
438 */
439 int
440ignorecase_opt(char_u *pat, int ic_in, int scs)
441{
442 int ic = ic_in;
443
444 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200445 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200446 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 no_smartcase = FALSE;
448
449 return ic;
450}
451
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200452/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200453 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200454 */
455 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100456pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200457{
458 char_u *p = pat;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200459 magic_T magic_val = MAGIC_ON;
460
461 // get the magicness of the pattern
462 (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val);
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200463
464 while (*p != NUL)
465 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200466 int l;
467
468 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
469 {
470 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
471 return TRUE;
472 p += l;
473 }
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200474 else if (*p == '\\' && magic_val <= MAGIC_ON)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200475 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100476 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200477 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100478 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200479 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100480 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200481 p += 2;
482 else
483 p += 1;
484 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200485 else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL)
486 {
487 if (p[1] != NUL) // skip "_X" and %X
488 p += 2;
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200489 else
490 p++;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200491 }
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200492 else if (MB_ISUPPER(*p))
493 return TRUE;
494 else
495 ++p;
496 }
497 return FALSE;
498}
499
Bram Moolenaar113e1072019-01-20 15:30:40 +0100500#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100502last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200503{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200504 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200505}
506
507 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100508last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200509{
510 return lastcdir == FORWARD;
511}
512
513 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100514last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200515{
516 return last_t_cmd == TRUE;
517}
518
519 void
zeertzjqe5d91ba2023-05-14 17:39:18 +0100520set_last_csearch(int c, char_u *s, int len)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200521{
522 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200523 lastc_bytelen = len;
524 if (len)
525 memcpy(lastc_bytes, s, len);
526 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200527 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200528}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100529#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200530
531 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100532set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200533{
534 lastcdir = cdir;
535}
536
537 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100538set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200539{
540 last_t_cmd = t_cmd;
541}
542
543 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100544last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 return spats[last_idx].pat;
547}
548
549/*
550 * Reset search direction to forward. For "gd" and "gD" commands.
551 */
552 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100553reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554{
555 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000556#if defined(FEAT_EVAL)
557 set_vv_searchforward();
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559}
560
561#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
562/*
563 * Set the last search pattern. For ":let @/ =" and viminfo.
564 * Also set the saved search pattern, so that this works in an autocommand.
565 */
566 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100567set_last_search_pat(
568 char_u *s,
569 int idx,
570 int magic,
571 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572{
573 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100574 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 if (*s == NUL)
576 spats[idx].pat = NULL;
577 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200578 {
579 spats[idx].patlen = STRLEN(s);
580 spats[idx].pat = vim_strnsave(s, spats[idx].patlen);
581 }
582 if (spats[idx].pat == NULL)
583 spats[idx].patlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 spats[idx].magic = magic;
585 spats[idx].no_scs = FALSE;
586 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000587#if defined(FEAT_EVAL)
588 set_vv_searchforward();
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 spats[idx].off.line = FALSE;
591 spats[idx].off.end = FALSE;
592 spats[idx].off.off = 0;
593 if (setlast)
594 last_idx = idx;
595 if (save_level)
596 {
597 vim_free(saved_spats[idx].pat);
598 saved_spats[idx] = spats[0];
599 if (spats[idx].pat == NULL)
600 saved_spats[idx].pat = NULL;
601 else
John Marriott8c85a2a2024-05-20 19:18:26 +0200602 saved_spats[idx].pat = vim_strnsave(spats[idx].pat, spats[idx].patlen);
603 if (saved_spats[idx].pat == NULL)
604 saved_spats[idx].patlen = 0;
605 else
606 saved_spats[idx].patlen = spats[idx].patlen;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100607# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100608 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 }
611# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100612 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100614 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615# endif
616}
617#endif
618
619#ifdef FEAT_SEARCH_EXTRA
620/*
621 * Get a regexp program for the last used search pattern.
622 * This is used for highlighting all matches in a window.
623 * Values returned in regmatch->regprog and regmatch->rmm_ic.
624 */
625 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100626last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627{
628 if (spats[last_idx].pat == NULL)
629 {
630 regmatch->regprog = NULL;
631 return;
632 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100633 ++emsg_off; // So it doesn't beep if bad expr
John Marriott8c85a2a2024-05-20 19:18:26 +0200634 (void)search_regcomp((char_u *)"", 0, NULL, 0, last_idx, SEARCH_KEEP, regmatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 --emsg_off;
636}
637#endif
638
639/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100640 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100641 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
642 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 *
644 * if (options & SEARCH_MSG) == 0 don't give any messages
645 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
646 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
647 * if (options & SEARCH_HIS) put search pattern in history
648 * if (options & SEARCH_END) return position at end of match
649 * if (options & SEARCH_START) accept match at pos itself
650 * if (options & SEARCH_KEEP) keep previous search pattern
651 * if (options & SEARCH_FOLD) match only once in a closed fold
652 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100653 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 *
655 * Return FAIL (zero) for failure, non-zero for success.
656 * When FEAT_EVAL is defined, returns the index of the first matching
657 * subpattern plus one; one if there was none.
658 */
659 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100660searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200661 win_T *win, // window to search in; can be NULL for a
662 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100663 buf_T *buf,
664 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100665 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100666 int dir,
667 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +0200668 size_t patlen,
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100669 long count,
670 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200671 int pat_use, // which pattern to use when "pat" is empty
672 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100675 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100676 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 regmmatch_T regmatch;
678 char_u *ptr;
679 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000681 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 int loop;
683 pos_T start_pos;
684 int at_first_line;
685 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200686 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 int match_ok;
688 long nmatched;
689 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100690 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100691 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#ifdef FEAT_SEARCH_EXTRA
693 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200695 linenr_T stop_lnum = 0; // stop after this line number when != 0
Paul Ollis65745772022-06-05 16:55:54 +0100696 int unused_timeout_flag = FALSE;
697 int *timed_out = &unused_timeout_flag; // set when timed out.
John Marriott8c85a2a2024-05-20 19:18:26 +0200698 int search_from_match_end; // vi-compatible search?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
John Marriott8c85a2a2024-05-20 19:18:26 +0200700 if (search_regcomp(pat, patlen, NULL, RE_SEARCH, pat_use,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
702 {
703 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000704 semsg(_(e_invalid_search_string_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 return FAIL;
706 }
707
John Marriott8c85a2a2024-05-20 19:18:26 +0200708 search_from_match_end = vim_strchr(p_cpo, CPO_SEARCH) != NULL;
709
Paul Ollis65745772022-06-05 16:55:54 +0100710 if (extra_arg != NULL)
711 {
712 stop_lnum = extra_arg->sa_stop_lnum;
713#ifdef FEAT_RELTIME
714 if (extra_arg->sa_tm > 0)
Paul Ollis65745772022-06-05 16:55:54 +0100715 init_regexp_timeout(extra_arg->sa_tm);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100716 // Also set the pointer when sa_tm is zero, the caller may have set the
717 // timeout.
718 timed_out = &extra_arg->sa_timed_out;
Paul Ollis65745772022-06-05 16:55:54 +0100719#endif
720 }
721
Bram Moolenaar280f1262006-01-30 00:14:18 +0000722 /*
723 * find the string
724 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100725 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100727 // When not accepting a match at the start position set "extra_col" to
728 // a non-zero value. Don't do that when starting at MAXCOL, since
729 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200730 if (pos->col == MAXCOL)
731 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100732 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200733 else if (has_mbyte
734 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
735 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100736 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100737 ptr = ml_get_buf(buf, pos->lnum, FALSE);
zeertzjq94b7c322024-03-12 21:50:32 +0100738 if (ml_get_buf_len(buf, pos->lnum) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200739 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100740 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100741 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100742 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100743 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200744 start_char_len = 1;
745 if (dir == FORWARD)
746 {
747 if (options & SEARCH_START)
748 extra_col = 0;
749 else
750 extra_col = start_char_len;
751 }
752 else
753 {
754 if (options & SEARCH_START)
755 extra_col = start_char_len;
756 else
757 extra_col = 0;
758 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100759
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100760 start_pos = *pos; // remember start pos for detecting no match
761 found = 0; // default: not found
762 at_first_line = TRUE; // default: start in first line
763 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
765 pos->lnum = 1;
766 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100767 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 }
769
770 /*
771 * Start searching in current line, unless searching backwards and
772 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000773 * If we are searching backwards, in column 0, and not including the
774 * current position, gain some efficiency by skipping back a line.
775 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000777 if (dir == BACKWARD && start_pos.col == 0
778 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
780 lnum = pos->lnum - 1;
781 at_first_line = FALSE;
782 }
783 else
784 lnum = pos->lnum;
785
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100786 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
788 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
789 lnum += dir, at_first_line = FALSE)
790 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100791 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000792 if (stop_lnum != 0 && (dir == FORWARD
793 ? lnum > stop_lnum : lnum < stop_lnum))
794 break;
Paul Ollis65745772022-06-05 16:55:54 +0100795 // Stop after passing the time limit.
796 if (*timed_out)
Bram Moolenaar76929292008-01-06 19:07:36 +0000797 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000800 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100802 col = at_first_line && (options & SEARCH_COL) ? pos->col
803 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 nmatched = vim_regexec_multi(&regmatch, win, buf,
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100805 lnum, col, timed_out);
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200806 // vim_regexec_multi() may clear "regprog"
807 if (regmatch.regprog == NULL)
808 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100809 // Abort searching on an error (e.g., out of stack).
Paul Ollis65745772022-06-05 16:55:54 +0100810 if (called_emsg > called_emsg_before || *timed_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 break;
812 if (nmatched > 0)
813 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100814 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000815 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000817#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000819#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100820 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000821 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
822 ptr = (char_u *)"";
823 else
824 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825
826 /*
827 * Forward search in the first line: match should be after
828 * the start position. If not, continue at the end of the
829 * match (this is vi compatible) or on the next char.
830 */
831 if (dir == FORWARD && at_first_line)
832 {
833 match_ok = TRUE;
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000836 * When the match starts in a next line it's certainly
837 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 * When match lands on a NUL the cursor will be put
839 * one back afterwards, compare with that position,
840 * otherwise "/$" will get stuck on end of line.
841 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000842 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100843 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000844 ? (nmatched == 1
845 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000847 : ((int)matchpos.col
848 - (ptr[matchpos.col] == NUL)
849 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
851 /*
852 * If vi-compatible searching, continue at the end
853 * of the match, otherwise continue one position
854 * forward.
855 */
John Marriott8c85a2a2024-05-20 19:18:26 +0200856 if (search_from_match_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
858 if (nmatched > 1)
859 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100860 // end is in next line, thus no match in
861 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 match_ok = FALSE;
863 break;
864 }
865 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100866 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000867 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 && ptr[matchcol] != NUL)
869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (has_mbyte)
871 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000872 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 ++matchcol;
875 }
876 }
877 else
878 {
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000879 // Advance "matchcol" to the next character.
Bram Moolenaar837ca8f2022-11-26 18:59:19 +0000880 // This uses rmm_matchcol, the actual start of
881 // the match, ignoring "\zs".
882 matchcol = regmatch.rmm_matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (ptr[matchcol] != NUL)
884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000886 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 + matchcol);
888 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 ++matchcol;
890 }
891 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200892 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100893 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (ptr[matchcol] == NUL
895 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000896 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100897 matchcol, timed_out)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 match_ok = FALSE;
900 break;
901 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200902 // vim_regexec_multi() may clear "regprog"
903 if (regmatch.regprog == NULL)
904 break;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000905 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 endpos = regmatch.endpos[0];
907# ifdef FEAT_EVAL
908 submatch = first_submatch(&regmatch);
909# endif
910
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100911 // Need to get the line pointer again, a
912 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000913 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915 if (!match_ok)
916 continue;
917 }
918 if (dir == BACKWARD)
919 {
920 /*
921 * Now, if there are multiple matches on this line,
922 * we have to get the last one. Or the last one before
923 * the cursor, if we're on that line.
924 * When putting the new cursor at the end, compare
925 * relative to the end of the match.
926 */
927 match_ok = FALSE;
928 for (;;)
929 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100930 // Remember a position that is before the start
931 // position, we use it if it's the last match in
932 // the line. Always accept a position after
933 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000934 if (loop
935 || ((options & SEARCH_END)
936 ? (lnum + regmatch.endpos[0].lnum
937 < start_pos.lnum
938 || (lnum + regmatch.endpos[0].lnum
939 == start_pos.lnum
940 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200941 < (int)start_pos.col
942 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000943 : (lnum + regmatch.startpos[0].lnum
944 < start_pos.lnum
945 || (lnum + regmatch.startpos[0].lnum
946 == start_pos.lnum
947 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200948 < (int)start_pos.col
949 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000952 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 endpos = regmatch.endpos[0];
954# ifdef FEAT_EVAL
955 submatch = first_submatch(&regmatch);
956# endif
957 }
958 else
959 break;
960
961 /*
962 * We found a valid match, now check if there is
963 * another one after it.
964 * If vi-compatible searching, continue at the end
965 * of the match, otherwise continue one position
966 * forward.
967 */
John Marriott8c85a2a2024-05-20 19:18:26 +0200968 if (search_from_match_end)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
970 if (nmatched > 1)
971 break;
972 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100973 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000974 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 && ptr[matchcol] != NUL)
976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (has_mbyte)
978 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000979 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 ++matchcol;
982 }
983 }
984 else
985 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100986 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000987 if (matchpos.lnum > 0)
988 break;
989 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 if (ptr[matchcol] != NUL)
991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 if (has_mbyte)
993 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000994 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 ++matchcol;
997 }
998 }
999 if (ptr[matchcol] == NUL
1000 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +00001001 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +01001002 matchcol, timed_out)) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +01001003 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001004 // If the search timed out, we did find a match
1005 // but it might be the wrong one, so that's not
1006 // OK.
Paul Ollis65745772022-06-05 16:55:54 +01001007 if (*timed_out)
Bram Moolenaar9d322762018-02-09 16:04:25 +01001008 match_ok = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +01001010 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001011 // vim_regexec_multi() may clear "regprog"
1012 if (regmatch.regprog == NULL)
1013 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001015 // Need to get the line pointer again, a
1016 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001017 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 }
1019
1020 /*
1021 * If there is only a match after the cursor, skip
1022 * this match.
1023 */
1024 if (!match_ok)
1025 continue;
1026 }
1027
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001028 // With the SEARCH_END option move to the last character
1029 // of the match. Don't do it for an empty match, end
1030 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +02001031 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001032 && !(matchpos.lnum == endpos.lnum
1033 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001035 // For a match in the first column, set the position
1036 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001037 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001038 pos->col = endpos.col;
1039 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001040 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001041 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001042 {
1043 --pos->lnum;
zeertzjq94b7c322024-03-12 21:50:32 +01001044 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001045 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001046 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001047 else
1048 {
1049 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001050 if (has_mbyte
1051 && pos->lnum <= buf->b_ml.ml_line_count)
1052 {
1053 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1054 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1055 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001056 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001057 if (end_pos != NULL)
1058 {
1059 end_pos->lnum = lnum + matchpos.lnum;
1060 end_pos->col = matchpos.col;
1061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
1063 else
1064 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001065 pos->lnum = lnum + matchpos.lnum;
1066 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001067 if (end_pos != NULL)
1068 {
1069 end_pos->lnum = lnum + endpos.lnum;
1070 end_pos->col = endpos.col;
1071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001074 if (end_pos != NULL)
1075 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001077 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001079 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001080 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 search_match_endcol = endpos.col;
1082 break;
1083 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001084 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (got_int)
1086 break;
1087
1088#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001089 // Cancel searching if a character was typed. Used for
1090 // 'incsearch'. Don't check too often, that would slowdown
1091 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if ((options & SEARCH_PEEK)
1093 && ((lnum - pos->lnum) & 0x3f) == 0
1094 && char_avail())
1095 {
1096 break_loop = TRUE;
1097 break;
1098 }
1099#endif
1100
1101 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001102 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
1104 at_first_line = FALSE;
1105
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001106 // vim_regexec_multi() may clear "regprog"
1107 if (regmatch.regprog == NULL)
1108 break;
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001111 * Stop the search if wrapscan isn't set, "stop_lnum" is
1112 * specified, after an interrupt, after a match and after looping
1113 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001115 if (!p_ws || stop_lnum != 0 || got_int
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001116 || called_emsg > called_emsg_before || *timed_out
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001117#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001118 || break_loop
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001119#endif
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001120 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 break;
1122
1123 /*
1124 * If 'wrapscan' is set we continue at the other end of the file.
Christian Brabandt34a6a362023-05-06 19:20:20 +01001125 * If 'shortmess' does not contain 's', we give a message, but
1126 * only, if we won't show the search stat later anyhow,
1127 * (so SEARCH_COUNT must be absent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 * This message is also remembered in keep_msg for when the screen
1129 * is redrawn. The keep_msg is cleared whenever another message is
1130 * written.
1131 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001132 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 lnum = 1;
Christian Brabandt34a6a362023-05-06 19:20:20 +01001136 if (!shortmess(SHM_SEARCH)
1137 && shortmess(SHM_SEARCHCOUNT)
1138 && (options & SEARCH_MSG))
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001139 give_warning((char_u *)_(dir == BACKWARD
1140 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001141 if (extra_arg != NULL)
1142 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
Paul Ollis65745772022-06-05 16:55:54 +01001144 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001145#ifdef FEAT_SEARCH_EXTRA
1146 || break_loop
1147#endif
1148 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 break;
1150 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001151 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001153#ifdef FEAT_RELTIME
1154 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1155 disable_regexp_timeout();
1156#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001157 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001159 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001162 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1164 {
1165 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001166 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001168 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001170 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 }
1172 return FAIL;
1173 }
1174
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001175 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001176 if (pos->lnum > buf->b_ml.ml_line_count)
1177 {
1178 pos->lnum = buf->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01001179 pos->col = ml_get_buf_len(buf, pos->lnum);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001180 if (pos->col > 0)
1181 --pos->col;
1182 }
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 return submatch + 1;
1185}
1186
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001187#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001188 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001189set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001190{
1191 spats[0].off.dir = cdir;
1192}
1193
1194 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001195set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001196{
1197 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1198}
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200/*
1201 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001202 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 */
1204 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001205first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206{
1207 int submatch;
1208
1209 for (submatch = 1; ; ++submatch)
1210 {
1211 if (rp->startpos[submatch].lnum >= 0)
1212 break;
1213 if (submatch == 9)
1214 {
1215 submatch = 0;
1216 break;
1217 }
1218 }
1219 return submatch;
1220}
1221#endif
1222
1223/*
1224 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001225 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 * If 'dirc' is 0: use previous dir.
1227 * If 'pat' is NULL or empty : use previous string.
1228 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1229 * If 'options & SEARCH_ECHO': echo the search command and handle options
1230 * If 'options & SEARCH_MSG' : may give error message
1231 * If 'options & SEARCH_OPT' : interpret optional flags
1232 * If 'options & SEARCH_HIS' : put search pattern in history
1233 * If 'options & SEARCH_NOOF': don't add offset to position
1234 * If 'options & SEARCH_MARK': set previous context mark
1235 * If 'options & SEARCH_KEEP': keep previous search pattern
1236 * If 'options & SEARCH_START': accept match at curpos itself
1237 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1238 *
1239 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1240 * makes the movement linewise without moving the match position.
1241 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001242 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 */
1244 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001245do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001246 oparg_T *oap, // can be NULL
1247 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001248 int search_delim, // the delimiter for the search, e.g. '%' in
1249 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001250 char_u *pat,
John Marriott8c85a2a2024-05-20 19:18:26 +02001251 size_t patlen,
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001252 long count,
1253 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001254 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001256 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 char_u *searchstr;
John Marriott8c85a2a2024-05-20 19:18:26 +02001258 size_t searchstrlen;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001259 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001260 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 char_u *p;
1262 long c;
1263 char_u *dircp;
1264 char_u *strcopy = NULL;
1265 char_u *ps;
John Marriott8c85a2a2024-05-20 19:18:26 +02001266 int show_search_stats;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001267 char_u *msgbuf = NULL;
John Marriott8c85a2a2024-05-20 19:18:26 +02001268 size_t msgbuflen = 0;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001269 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270
John Marriott8c85a2a2024-05-20 19:18:26 +02001271 searchcmdlen = 0;
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 /*
1274 * A line offset is not remembered, this is vi compatible.
1275 */
1276 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1277 {
1278 spats[0].off.line = FALSE;
1279 spats[0].off.off = 0;
1280 }
1281
1282 /*
1283 * Save the values for when (options & SEARCH_KEEP) is used.
1284 * (there is no "if ()" around this because gcc wants them initialized)
1285 */
1286 old_off = spats[0].off;
1287
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001288 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289
1290 /*
1291 * Find out the direction of the search.
1292 */
1293 if (dirc == 0)
1294 dirc = spats[0].off.dir;
1295 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001298#if defined(FEAT_EVAL)
1299 set_vv_searchforward();
1300#endif
1301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 if (options & SEARCH_REV)
1303 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001304#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001305 // There is a bug in the Visual C++ 2.2 compiler which means that
1306 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 dirc = (dirc == '/') ? '?' : '/';
1308#else
1309 if (dirc == '/')
1310 dirc = '?';
1311 else
1312 dirc = '/';
1313#endif
1314 }
1315
1316#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001317 // If the cursor is in a closed fold, don't find another match in the same
1318 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 if (dirc == '/')
1320 {
1321 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001322 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 }
1324 else
1325 {
1326 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1327 pos.col = 0;
1328 }
1329#endif
1330
1331#ifdef FEAT_SEARCH_EXTRA
1332 /*
1333 * Turn 'hlsearch' highlighting back on.
1334 */
1335 if (no_hlsearch && !(options & SEARCH_KEEP))
1336 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001337 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001338 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
1340#endif
1341
1342 /*
1343 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1344 */
1345 for (;;)
1346 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001347 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 searchstr = pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001350 searchstrlen = patlen;
1351
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001353 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001354 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001356 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001358 if (spats[RE_SUBST].pat == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001359 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001360 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001361 retval = 0;
1362 goto end_do_search;
1363 }
John Marriott8c85a2a2024-05-20 19:18:26 +02001364 searchstr = spats[RE_SUBST].pat;
1365 searchstrlen = spats[RE_SUBST].patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001367 else
1368 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001369 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001370 searchstr = (char_u *)"";
John Marriott8c85a2a2024-05-20 19:18:26 +02001371 searchstrlen = 0;
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001375 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 {
1377 /*
1378 * Find end of regular expression.
1379 * If there is a matching '/' or '?', toss it.
1380 */
1381 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001382 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001383 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 if (strcopy != ps)
1385 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001386 size_t len = STRLEN(strcopy);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001387 // made a copy of "pat" to change "\?" to "?"
John Marriott8c85a2a2024-05-20 19:18:26 +02001388 searchcmdlen += (int)(patlen - len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 pat = strcopy;
John Marriott8c85a2a2024-05-20 19:18:26 +02001390 patlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 searchstr = strcopy;
John Marriott8c85a2a2024-05-20 19:18:26 +02001392 searchstrlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001394 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001396 searchstrlen = p - pat;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001397 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 *p++ = NUL;
1399 }
1400 spats[0].off.line = FALSE;
1401 spats[0].off.end = FALSE;
1402 spats[0].off.off = 0;
1403 /*
1404 * Check for a line offset or a character offset.
1405 * For get_address (echo off) we don't check for a character
1406 * offset, because it is meaningless and the 's' could be a
1407 * substitute command.
1408 */
1409 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1410 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001411 else if ((options & SEARCH_OPT)
1412 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001414 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 spats[0].off.end = SEARCH_END;
1416 ++p;
1417 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001418 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001420 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1422 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001423 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001425 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 spats[0].off.off = 1;
1427 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001428 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 ++p;
1430 }
1431
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001432 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 searchcmdlen += (int)(p - pat);
1434
John Marriott8c85a2a2024-05-20 19:18:26 +02001435 patlen -= p - pat;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001436 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
1438
John Marriott8c85a2a2024-05-20 19:18:26 +02001439 show_search_stats = FALSE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001440 if ((options & SEARCH_ECHO) && messaging()
1441 && !msg_silent
1442 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 {
Bram Moolenaar984f0312019-05-24 13:11:47 +02001444 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001445 size_t off_len = 0;
John Marriott8c85a2a2024-05-20 19:18:26 +02001446 size_t plen;
1447 size_t msgbufsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001449 // Compute msg_row early.
1450 msg_start();
1451
Bram Moolenaar984f0312019-05-24 13:11:47 +02001452 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001453 if (!cmd_silent &&
1454 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001455 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001456 off_buf[off_len++] = dirc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001457 if (spats[0].off.end)
John Marriott8c85a2a2024-05-20 19:18:26 +02001458 off_buf[off_len++] = 'e';
Bram Moolenaar984f0312019-05-24 13:11:47 +02001459 else if (!spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001460 off_buf[off_len++] = 's';
Bram Moolenaar984f0312019-05-24 13:11:47 +02001461 if (spats[0].off.off > 0 || spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001462 off_buf[off_len++] = '+';
1463 off_buf[off_len] = NUL;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001464 if (spats[0].off.off != 0 || spats[0].off.line)
John Marriott8c85a2a2024-05-20 19:18:26 +02001465 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 +02001466 }
1467
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 if (*searchstr == NUL)
John Marriott8c85a2a2024-05-20 19:18:26 +02001469 {
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001470 p = spats[0].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001471 plen = spats[0].patlen;
1472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 else
John Marriott8c85a2a2024-05-20 19:18:26 +02001474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 p = searchstr;
John Marriott8c85a2a2024-05-20 19:18:26 +02001476 plen = searchstrlen;
1477 }
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001478
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001479 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001480 {
1481 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001482 // search stat. Use all the space available, so that the
1483 // search state is right aligned. If there is not enough space
1484 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001485 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001486 // Use all the columns.
John Marriott8c85a2a2024-05-20 19:18:26 +02001487 msgbufsize = (int)(Rows - msg_row) * Columns - 1;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001488 else
1489 // Use up to 'showcmd' column.
John Marriott8c85a2a2024-05-20 19:18:26 +02001490 msgbufsize = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
1491 if (msgbufsize < plen + off_len + SEARCH_STAT_BUF_LEN + 3)
1492 msgbufsize = plen + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001493 }
1494 else
1495 // Reserve enough space for the search pattern + offset.
John Marriott8c85a2a2024-05-20 19:18:26 +02001496 msgbufsize = plen + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001497
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001498 vim_free(msgbuf);
John Marriott8c85a2a2024-05-20 19:18:26 +02001499 msgbuf = alloc(msgbufsize);
1500 if (msgbuf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001502 msgbuflen = 0;
1503 }
1504 else
1505 {
1506 vim_memset(msgbuf, ' ', msgbufsize);
1507 msgbuflen = msgbufsize - 1;
1508 msgbuf[msgbuflen] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001509 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1510 // empty for the search_stat feature.
1511 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001512 {
John Marriott8c85a2a2024-05-20 19:18:26 +02001513 char_u *trunc;
1514
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001515 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001517 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1518 {
1519 // Use a space to draw the composing char on.
1520 msgbuf[1] = ' ';
John Marriott8c85a2a2024-05-20 19:18:26 +02001521 mch_memmove(msgbuf + 2, p, plen);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001522 }
1523 else
John Marriott8c85a2a2024-05-20 19:18:26 +02001524 mch_memmove(msgbuf + 1, p, plen);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001525 if (off_len > 0)
John Marriott8c85a2a2024-05-20 19:18:26 +02001526 mch_memmove(msgbuf + plen + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001528 trunc = msg_strtrunc(msgbuf, TRUE);
1529 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001531 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001532 msgbuf = trunc;
John Marriott8c85a2a2024-05-20 19:18:26 +02001533 msgbuflen = STRLEN(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001536#ifdef FEAT_RIGHTLEFT
1537 // The search pattern could be shown on the right in
1538 // rightleft mode, but the 'ruler' and 'showcmd' area use
1539 // it too, thus it would be blanked out again very soon.
1540 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001541 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1542 {
1543 char_u *r;
1544 size_t pat_len;
1545
1546 r = reverse_text(msgbuf);
1547 if (r != NULL)
1548 {
1549 vim_free(msgbuf);
1550 msgbuf = r;
Christian Brabandtcacb6692024-08-22 21:40:14 +02001551 msgbuflen = STRLEN(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001552 // move reversed text to beginning of buffer
1553 while (*r != NUL && *r == ' ')
1554 r++;
John Marriott8c85a2a2024-05-20 19:18:26 +02001555 pat_len = msgbuf + msgbuflen - r;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001556 mch_memmove(msgbuf, r, pat_len);
1557 // overwrite old text
1558 if ((size_t)(r - msgbuf) >= pat_len)
1559 vim_memset(r, ' ', pat_len);
1560 else
1561 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1562 }
1563 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001564#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001565 msg_outtrans(msgbuf);
1566 msg_clr_eos();
1567 msg_check();
1568
1569 gotocmdline(FALSE);
1570 out_flush();
1571 msg_nowait = TRUE; // don't wait for this message
1572 }
John Marriott8c85a2a2024-05-20 19:18:26 +02001573
1574 if (!shortmess(SHM_SEARCHCOUNT))
1575 show_search_stats = TRUE;
1576 } // msgbuf != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578
1579 /*
1580 * If there is a character offset, subtract it from the current
1581 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001582 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 * This is not done for a line offset, because then we would not be vi
1584 * compatible.
1585 */
Bram Moolenaared203462004-06-16 11:19:22 +00001586 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
1588 if (spats[0].off.off > 0)
1589 {
1590 for (c = spats[0].off.off; c; --c)
1591 if (decl(&pos) == -1)
1592 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001593 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001595 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 pos.col = MAXCOL;
1597 }
1598 }
1599 else
1600 {
1601 for (c = spats[0].off.off; c; ++c)
1602 if (incl(&pos) == -1)
1603 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001604 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 {
1606 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1607 pos.col = 0;
1608 }
1609 }
1610 }
1611
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001612 /*
1613 * The actual search.
1614 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001615 c = searchit(curwin, curbuf, &pos, NULL,
1616 dirc == '/' ? FORWARD : BACKWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02001617 searchstr, searchstrlen, count, spats[0].off.end + (options &
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1619 + SEARCH_MSG + SEARCH_START
1620 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001621 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001624 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001625
1626 if (!shortmess(SHM_SEARCH)
1627 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1628 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001629 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 if (c == FAIL)
1632 {
1633 retval = 0;
1634 goto end_do_search;
1635 }
1636 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001637 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001639 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 /*
1642 * Add character and/or line offset
1643 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001644 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001646 pos_T org_pos = pos;
1647
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001648 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 c = pos.lnum + spats[0].off.off;
1651 if (c < 1)
1652 pos.lnum = 1;
1653 else if (c > curbuf->b_ml.ml_line_count)
1654 pos.lnum = curbuf->b_ml.ml_line_count;
1655 else
1656 pos.lnum = c;
1657 pos.col = 0;
1658
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001659 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001661 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001663 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001664 c = spats[0].off.off;
1665 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001667 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (incl(&pos) == -1)
1669 break;
1670 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001671 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 else
1673 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001674 while (c++ < 0)
1675 if (decl(&pos) == -1)
1676 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
1678 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001679 if (!EQUAL_POS(pos, org_pos))
1680 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001683 // Show [1/15] if 'S' is not in 'shortmess'.
John Marriott8c85a2a2024-05-20 19:18:26 +02001684 if (show_search_stats)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001685 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
John Marriott8c85a2a2024-05-20 19:18:26 +02001686 show_top_bot_msg, msgbuf, msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001687 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001688#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001689 || (!(fdo_flags & FDO_SEARCH)
1690 && hasFolding(curwin->w_cursor.lnum,
1691 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001692#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001693 ),
1694 SEARCH_STAT_DEF_MAX_COUNT,
1695 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001696
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 /*
1698 * The search command can be followed by a ';' to do another search.
1699 * For example: "/pat/;/foo/+3;?bar"
1700 * This is like doing another search command, except:
1701 * - The remembered direction '/' or '?' is from the first search.
1702 * - When an error happens the cursor isn't moved at all.
1703 * Don't do this when called by get_address() (it handles ';' itself).
1704 */
1705 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1706 break;
1707
1708 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001709 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 if (dirc != '?' && dirc != '/')
1711 {
1712 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001713 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 goto end_do_search;
1715 }
1716 ++pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001717 --patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719
1720 if (options & SEARCH_MARK)
1721 setpcmark();
1722 curwin->w_cursor = pos;
1723 curwin->w_set_curswant = TRUE;
1724
1725end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001726 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 spats[0].off = old_off;
1728 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001729 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
1731 return retval;
1732}
1733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734/*
1735 * search_for_exact_line(buf, pos, dir, pat)
1736 *
1737 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001738 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001740 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 * Return OK for success, or FAIL if no line found.
1742 */
1743 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001744search_for_exact_line(
1745 buf_T *buf,
1746 pos_T *pos,
1747 int dir,
1748 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
1750 linenr_T start = 0;
1751 char_u *ptr;
1752 char_u *p;
1753
1754 if (buf->b_ml.ml_line_count == 0)
1755 return FAIL;
1756 for (;;)
1757 {
1758 pos->lnum += dir;
1759 if (pos->lnum < 1)
1760 {
1761 if (p_ws)
1762 {
1763 pos->lnum = buf->b_ml.ml_line_count;
1764 if (!shortmess(SHM_SEARCH))
1765 give_warning((char_u *)_(top_bot_msg), TRUE);
1766 }
1767 else
1768 {
1769 pos->lnum = 1;
1770 break;
1771 }
1772 }
1773 else if (pos->lnum > buf->b_ml.ml_line_count)
1774 {
1775 if (p_ws)
1776 {
1777 pos->lnum = 1;
1778 if (!shortmess(SHM_SEARCH))
1779 give_warning((char_u *)_(bot_top_msg), TRUE);
1780 }
1781 else
1782 {
1783 pos->lnum = 1;
1784 break;
1785 }
1786 }
1787 if (pos->lnum == start)
1788 break;
1789 if (start == 0)
1790 start = pos->lnum;
1791 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1792 p = skipwhite(ptr);
1793 pos->col = (colnr_T) (p - ptr);
1794
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001795 // when adding lines the matching line may be empty but it is not
1796 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001797 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 {
1799 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1800 return OK;
1801 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001802 else if (*p != NUL) // ignore empty lines
1803 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001804 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1805 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 return OK;
1807 }
1808 }
1809 return FAIL;
1810}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812/*
1813 * Character Searches
1814 */
1815
1816/*
1817 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1818 * position of the character, otherwise move to just before the char.
1819 * Do this "cap->count1" times.
1820 * Return FAIL or OK.
1821 */
1822 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001823searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001825 int c = cap->nchar; // char to search for
1826 int dir = cap->arg; // TRUE for searching forward
1827 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 int col;
1829 char_u *p;
1830 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001831 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001833 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001835 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001837 *lastc = c;
1838 set_csearch_direction(dir);
1839 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001840 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (cap->ncharC1 != 0)
1842 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001843 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1844 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001846 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1847 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001851 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
zeertzjqe5d91ba2023-05-14 17:39:18 +01001853 if (*lastc == NUL && lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001855 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 dir = -lastcdir;
1857 else
1858 dir = lastcdir;
1859 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001860 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001861 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001862
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001863 // Force a move of at least one char, so ";" and "," will move the
1864 // cursor, even if the cursor is right in front of char we are looking
1865 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001866 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001867 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001870 if (dir == BACKWARD)
1871 cap->oap->inclusive = FALSE;
1872 else
1873 cap->oap->inclusive = TRUE;
1874
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 p = ml_get_curline();
1876 col = curwin->w_cursor.col;
zeertzjq94b7c322024-03-12 21:50:32 +01001877 len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 while (count--)
1880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (has_mbyte)
1882 {
1883 for (;;)
1884 {
1885 if (dir > 0)
1886 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001887 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (col >= len)
1889 return FAIL;
1890 }
1891 else
1892 {
1893 if (col == 0)
1894 return FAIL;
1895 col -= (*mb_head_off)(p, p + col - 1) + 1;
1896 }
zeertzjqe5d91ba2023-05-14 17:39:18 +01001897 if (lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001899 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 break;
1901 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001902 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001903 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001904 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001905 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 }
1908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 for (;;)
1911 {
1912 if ((col += dir) < 0 || col >= len)
1913 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001914 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001916 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
1918 }
1919 }
1920
1921 if (t_cmd)
1922 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001923 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if (has_mbyte)
1926 {
1927 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001928 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001929 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001931 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 col -= (*mb_head_off)(p, p + col);
1933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 }
1935 curwin->w_cursor.col = col;
1936
1937 return OK;
1938}
1939
1940/*
1941 * "Other" Searches
1942 */
1943
1944/*
1945 * findmatch - find the matching paren or brace
1946 *
1947 * Improvement over vi: Braces inside quotes are ignored.
1948 */
1949 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001950findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951{
1952 return findmatchlimit(oap, initc, 0, 0);
1953}
1954
1955/*
1956 * Return TRUE if the character before "linep[col]" equals "ch".
1957 * Return FALSE if "col" is zero.
1958 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1959 * is NULL.
1960 * Handles multibyte string correctly.
1961 */
1962 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001963check_prevcol(
1964 char_u *linep,
1965 int col,
1966 int ch,
1967 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968{
1969 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (col > 0 && has_mbyte)
1971 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (prevcol)
1973 *prevcol = col;
1974 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1975}
1976
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001977/*
1978 * Raw string start is found at linep[startpos.col - 1].
1979 * Return TRUE if the matching end can be found between startpos and endpos.
1980 */
1981 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001982find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001983{
1984 char_u *p;
1985 char_u *delim_copy;
1986 size_t delim_len;
1987 linenr_T lnum;
1988 int found = FALSE;
1989
1990 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1991 ;
1992 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001993 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001994 if (delim_copy == NULL)
1995 return FALSE;
1996 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1997 {
1998 char_u *line = ml_get(lnum);
1999
2000 for (p = line + (lnum == startpos->lnum
2001 ? startpos->col + 1 : 0); *p; ++p)
2002 {
2003 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
2004 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02002005 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
2006 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002007 {
2008 found = TRUE;
2009 break;
2010 }
2011 }
2012 if (found)
2013 break;
2014 }
2015 vim_free(delim_copy);
2016 return found;
2017}
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01002020 * Check matchpairs option for "*initc".
2021 * If there is a match set "*initc" to the matching character and "*findc" to
2022 * the opposite character. Set "*backwards" to the direction.
2023 * When "switchit" is TRUE swap the direction.
2024 */
2025 static void
2026find_mps_values(
2027 int *initc,
2028 int *findc,
2029 int *backwards,
2030 int switchit)
2031{
2032 char_u *ptr;
2033
2034 ptr = curbuf->b_p_mps;
2035 while (*ptr != NUL)
2036 {
2037 if (has_mbyte)
2038 {
2039 char_u *prev;
2040
2041 if (mb_ptr2char(ptr) == *initc)
2042 {
2043 if (switchit)
2044 {
2045 *findc = *initc;
2046 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2047 *backwards = TRUE;
2048 }
2049 else
2050 {
2051 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2052 *backwards = FALSE;
2053 }
2054 return;
2055 }
2056 prev = ptr;
2057 ptr += mb_ptr2len(ptr) + 1;
2058 if (mb_ptr2char(ptr) == *initc)
2059 {
2060 if (switchit)
2061 {
2062 *findc = *initc;
2063 *initc = mb_ptr2char(prev);
2064 *backwards = FALSE;
2065 }
2066 else
2067 {
2068 *findc = mb_ptr2char(prev);
2069 *backwards = TRUE;
2070 }
2071 return;
2072 }
2073 ptr += mb_ptr2len(ptr);
2074 }
2075 else
2076 {
2077 if (*ptr == *initc)
2078 {
2079 if (switchit)
2080 {
2081 *backwards = TRUE;
2082 *findc = *initc;
2083 *initc = ptr[2];
2084 }
2085 else
2086 {
2087 *backwards = FALSE;
2088 *findc = ptr[2];
2089 }
2090 return;
2091 }
2092 ptr += 2;
2093 if (*ptr == *initc)
2094 {
2095 if (switchit)
2096 {
2097 *backwards = FALSE;
2098 *findc = *initc;
2099 *initc = ptr[-2];
2100 }
2101 else
2102 {
2103 *backwards = TRUE;
2104 *findc = ptr[-2];
2105 }
2106 return;
2107 }
2108 ++ptr;
2109 }
2110 if (*ptr == ',')
2111 ++ptr;
2112 }
2113}
2114
2115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002117 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2118 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 *
2120 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002121 * character at or after the cursor. Special values:
2122 * '*' look for C-style comment / *
2123 * '/' look for C-style comment / *, ignoring comment-end
2124 * '#' look for preprocessor directives
2125 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 *
2127 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2128 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2129 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2130 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002131 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002132 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002133 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002136findmatchlimit(
2137 oparg_T *oap,
2138 int initc,
2139 int flags,
2140 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002142 static pos_T pos; // current search position
2143 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002145 int count = 0; // cumulative number of braces
2146 int backwards = FALSE; // init for gcc
2147 int raw_string = FALSE; // search for raw string
2148 int inquote = FALSE; // TRUE when inside quotes
2149 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002151 int do_quotes; // check for quotes in current line
2152 int at_start; // do_quotes value at start position
2153 int hash_dir = 0; // Direction searched for # things
2154 int comment_dir = 0; // Direction searched for comments
2155 pos_T match_pos; // Where last slash-star was found
2156 int start_in_quotes; // start position is in quotes
2157 int traveled = 0; // how far we've searched so far
2158 int ignore_cend = FALSE; // ignore comment end
2159 int cpo_match; // vi compatible matching
2160 int cpo_bsl; // don't recognize backslashes
2161 int match_escaped = 0; // search for escaped match
2162 int dir; // Direction to search
2163 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002164 int lispcomm = FALSE; // inside of Lisp-style comment
2165 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
2167 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002168 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 linep = ml_get(pos.lnum);
2170
2171 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2172 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2173
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002174 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (flags & FM_BACKWARD)
2176 dir = BACKWARD;
2177 else if (flags & FM_FORWARD)
2178 dir = FORWARD;
2179 else
2180 dir = 0;
2181
2182 /*
2183 * if initc given, look in the table for the matching character
2184 * '/' and '*' are special cases: look for start or end of comment.
2185 * When '/' is used, we ignore running backwards into an star-slash, for
2186 * "[*" command, we just want to find any comment.
2187 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002188 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
2190 comment_dir = dir;
2191 if (initc == '/')
2192 ignore_cend = TRUE;
2193 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002194 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 initc = NUL;
2196 }
2197 else if (initc != '#' && initc != NUL)
2198 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002199 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002200 if (dir)
2201 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002202 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 return NULL;
2204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 else
2206 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002207 /*
2208 * Either initc is '#', or no initc was given and we need to look
2209 * under the cursor.
2210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 if (initc == '#')
2212 {
2213 hash_dir = dir;
2214 }
2215 else
2216 {
2217 /*
2218 * initc was not given, must look for something to match under
2219 * or near the cursor.
2220 * Only check for special things when 'cpo' doesn't have '%'.
2221 */
2222 if (!cpo_match)
2223 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002224 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 ptr = skipwhite(linep);
2226 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2227 {
2228 ptr = skipwhite(ptr + 1);
2229 if ( STRNCMP(ptr, "if", 2) == 0
2230 || STRNCMP(ptr, "endif", 5) == 0
2231 || STRNCMP(ptr, "el", 2) == 0)
2232 hash_dir = 1;
2233 }
2234
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002235 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 else if (linep[pos.col] == '/')
2237 {
2238 if (linep[pos.col + 1] == '*')
2239 {
2240 comment_dir = FORWARD;
2241 backwards = FALSE;
2242 pos.col++;
2243 }
2244 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2245 {
2246 comment_dir = BACKWARD;
2247 backwards = TRUE;
2248 pos.col--;
2249 }
2250 }
2251 else if (linep[pos.col] == '*')
2252 {
2253 if (linep[pos.col + 1] == '/')
2254 {
2255 comment_dir = BACKWARD;
2256 backwards = TRUE;
2257 }
2258 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2259 {
2260 comment_dir = FORWARD;
2261 backwards = FALSE;
2262 }
2263 }
2264 }
2265
2266 /*
2267 * If we are not on a comment or the # at the start of a line, then
2268 * look for brace anywhere on this line after the cursor.
2269 */
2270 if (!hash_dir && !comment_dir)
2271 {
2272 /*
2273 * Find the brace under or after the cursor.
2274 * If beyond the end of the line, use the last character in
2275 * the line.
2276 */
2277 if (linep[pos.col] == NUL && pos.col)
2278 --pos.col;
2279 for (;;)
2280 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002281 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (initc == NUL)
2283 break;
2284
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002285 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 if (findc)
2287 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002288 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290 if (!findc)
2291 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002292 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 if (!cpo_match && *skipwhite(linep) == '#')
2294 hash_dir = 1;
2295 else
2296 return NULL;
2297 }
2298 else if (!cpo_bsl)
2299 {
2300 int col, bslcnt = 0;
2301
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002302 // Set "match_escaped" if there are an odd number of
2303 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2305 bslcnt++;
2306 match_escaped = (bslcnt & 1);
2307 }
2308 }
2309 }
2310 if (hash_dir)
2311 {
2312 /*
2313 * Look for matching #if, #else, #elif, or #endif
2314 */
2315 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002316 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (initc != '#')
2318 {
2319 ptr = skipwhite(skipwhite(linep) + 1);
2320 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2321 hash_dir = 1;
2322 else if (STRNCMP(ptr, "endif", 5) == 0)
2323 hash_dir = -1;
2324 else
2325 return NULL;
2326 }
2327 pos.col = 0;
2328 while (!got_int)
2329 {
2330 if (hash_dir > 0)
2331 {
2332 if (pos.lnum == curbuf->b_ml.ml_line_count)
2333 break;
2334 }
2335 else if (pos.lnum == 1)
2336 break;
2337 pos.lnum += hash_dir;
2338 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002339 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 ptr = skipwhite(linep);
2341 if (*ptr != '#')
2342 continue;
2343 pos.col = (colnr_T) (ptr - linep);
2344 ptr = skipwhite(ptr + 1);
2345 if (hash_dir > 0)
2346 {
2347 if (STRNCMP(ptr, "if", 2) == 0)
2348 count++;
2349 else if (STRNCMP(ptr, "el", 2) == 0)
2350 {
2351 if (count == 0)
2352 return &pos;
2353 }
2354 else if (STRNCMP(ptr, "endif", 5) == 0)
2355 {
2356 if (count == 0)
2357 return &pos;
2358 count--;
2359 }
2360 }
2361 else
2362 {
2363 if (STRNCMP(ptr, "if", 2) == 0)
2364 {
2365 if (count == 0)
2366 return &pos;
2367 count--;
2368 }
2369 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2370 {
2371 if (count == 0)
2372 return &pos;
2373 }
2374 else if (STRNCMP(ptr, "endif", 5) == 0)
2375 count++;
2376 }
2377 }
2378 return NULL;
2379 }
2380 }
2381
2382#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002383 // This is just guessing: when 'rightleft' is set, search for a matching
2384 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2386 backwards = !backwards;
2387#endif
2388
2389 do_quotes = -1;
2390 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002391 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002392
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002393 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002394 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002396 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002397 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 while (!got_int)
2400 {
2401 /*
2402 * Go to the next position, forward or backward. We could use
2403 * inc() and dec() here, but that is much slower
2404 */
2405 if (backwards)
2406 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002407 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002408 if (lispcomm && pos.col < (colnr_T)comment_col)
2409 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002410 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002412 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 break;
2414 --pos.lnum;
2415
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002416 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 break;
2418
2419 linep = ml_get(pos.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01002420 pos.col = ml_get_len(pos.lnum); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 do_quotes = -1;
2422 line_breakcheck();
2423
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002424 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002425 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002427 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002428 if (lisp && comment_col != MAXCOL)
2429 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431 else
2432 {
2433 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (has_mbyte)
2435 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
2437 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002438 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002440 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002441 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002442 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002443 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002444 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002446 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002447 // line is exhausted and comment with it,
2448 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002449 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 break;
2451 ++pos.lnum;
2452
2453 if (maxtravel && traveled++ > maxtravel)
2454 break;
2455
2456 linep = ml_get(pos.lnum);
2457 pos.col = 0;
2458 do_quotes = -1;
2459 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002460 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002461 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
2463 else
2464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002466 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 ++pos.col;
2469 }
2470 }
2471
2472 /*
2473 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2474 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002475 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2476 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002478 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002480 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
2482
2483 if (comment_dir)
2484 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002485 // Note: comments do not nest, and we ignore quotes in them
2486 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 if (comment_dir == FORWARD)
2488 {
2489 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2490 {
2491 pos.col++;
2492 return &pos;
2493 }
2494 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002495 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 /*
2498 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002499 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 */
2501 if (pos.col == 0)
2502 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002503 else if (raw_string)
2504 {
2505 if (linep[pos.col - 1] == 'R'
2506 && linep[pos.col] == '"'
2507 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2508 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002509 // Possible start of raw string. Now that we have the
2510 // delimiter we can check if it ends before where we
2511 // started searching, or before the previously found
2512 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002513 if (!find_rawstring_end(linep, &pos,
2514 count > 0 ? &match_pos : &curwin->w_cursor))
2515 {
2516 count++;
2517 match_pos = pos;
2518 match_pos.col--;
2519 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002520 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002521 }
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 else if ( linep[pos.col - 1] == '/'
2524 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002525 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 && (int)pos.col < comment_col)
2527 {
2528 count++;
2529 match_pos = pos;
2530 match_pos.col--;
2531 }
2532 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2533 {
2534 if (count > 0)
2535 pos = match_pos;
2536 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2537 && (int)pos.col <= comment_col)
2538 pos.col -= 2;
2539 else if (ignore_cend)
2540 continue;
2541 else
2542 return NULL;
2543 return &pos;
2544 }
2545 }
2546 continue;
2547 }
2548
2549 /*
2550 * If smart matching ('cpoptions' does not contain '%'), braces inside
2551 * of quotes are ignored, but only if there is an even number of
2552 * quotes in the line.
2553 */
2554 if (cpo_match)
2555 do_quotes = 0;
2556 else if (do_quotes == -1)
2557 {
2558 /*
2559 * Count the number of quotes in the line, skipping \" and '"'.
2560 * Watch out for "\\".
2561 */
2562 at_start = do_quotes;
2563 for (ptr = linep; *ptr; ++ptr)
2564 {
2565 if (ptr == linep + pos.col + backwards)
2566 at_start = (do_quotes & 1);
2567 if (*ptr == '"'
2568 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2569 ++do_quotes;
2570 if (*ptr == '\\' && ptr[1] != NUL)
2571 ++ptr;
2572 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002573 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 /*
2576 * If we find an uneven count, check current line and previous
2577 * one for a '\' at the end.
2578 */
2579 if (!do_quotes)
2580 {
2581 inquote = FALSE;
2582 if (ptr[-1] == '\\')
2583 {
2584 do_quotes = 1;
2585 if (start_in_quotes == MAYBE)
2586 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002587 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 inquote = TRUE;
2589 start_in_quotes = TRUE;
2590 }
2591 else if (backwards)
2592 inquote = TRUE;
2593 }
2594 if (pos.lnum > 1)
2595 {
2596 ptr = ml_get(pos.lnum - 1);
zeertzjq94b7c322024-03-12 21:50:32 +01002597 if (*ptr && *(ptr + ml_get_len(pos.lnum - 1) - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 {
2599 do_quotes = 1;
2600 if (start_in_quotes == MAYBE)
2601 {
2602 inquote = at_start;
2603 if (inquote)
2604 start_in_quotes = TRUE;
2605 }
2606 else if (!backwards)
2607 inquote = TRUE;
2608 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002609
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002610 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002611 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 }
2613 }
2614 }
2615 if (start_in_quotes == MAYBE)
2616 start_in_quotes = FALSE;
2617
2618 /*
2619 * If 'smartmatch' is set:
2620 * Things inside quotes are ignored by setting 'inquote'. If we
2621 * find a quote without a preceding '\' invert 'inquote'. At the
2622 * end of a line not ending in '\' we reset 'inquote'.
2623 *
2624 * In lines with an uneven number of quotes (without preceding '\')
2625 * we do not know which part to ignore. Therefore we only set
2626 * inquote if the number of quotes in a line is even, unless this
2627 * line or the previous one ends in a '\'. Complicated, isn't it?
2628 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002629 c = PTR2CHAR(linep + pos.col);
2630 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002633 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2635 {
2636 inquote = FALSE;
2637 start_in_quotes = FALSE;
2638 }
2639 break;
2640
2641 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002642 // a quote that is preceded with an odd number of backslashes is
2643 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (do_quotes)
2645 {
2646 int col;
2647
2648 for (col = pos.col - 1; col >= 0; --col)
2649 if (linep[col] != '\\')
2650 break;
2651 if ((((int)pos.col - 1 - col) & 1) == 0)
2652 {
2653 inquote = !inquote;
2654 start_in_quotes = FALSE;
2655 }
2656 }
2657 break;
2658
2659 /*
2660 * If smart matching ('cpoptions' does not contain '%'):
2661 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2662 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2663 * skipped, there is never a brace in them.
2664 * Ignore this when finding matches for `'.
2665 */
2666 case '\'':
2667 if (!cpo_match && initc != '\'' && findc != '\'')
2668 {
2669 if (backwards)
2670 {
2671 if (pos.col > 1)
2672 {
2673 if (linep[pos.col - 2] == '\'')
2674 {
2675 pos.col -= 2;
2676 break;
2677 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002678 else if (linep[pos.col - 2] == '\\'
2679 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {
2681 pos.col -= 3;
2682 break;
2683 }
2684 }
2685 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002686 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002688 if (linep[pos.col + 1] == '\\'
2689 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
2691 pos.col += 3;
2692 break;
2693 }
2694 else if (linep[pos.col + 2] == '\'')
2695 {
2696 pos.col += 2;
2697 break;
2698 }
2699 }
2700 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002701 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002704 /*
2705 * For Lisp skip over backslashed (), {} and [].
2706 * (actually, we skip #\( et al)
2707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002709 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002710 && pos.col > 1
2711 && check_prevcol(linep, pos.col, '\\', NULL)
2712 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002715 // Check for match outside of quotes, and inside of
2716 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if ((!inquote || start_in_quotes == TRUE)
2718 && (c == initc || c == findc))
2719 {
2720 int col, bslcnt = 0;
2721
2722 if (!cpo_bsl)
2723 {
2724 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2725 bslcnt++;
2726 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002727 // Only accept a match when 'M' is in 'cpo' or when escaping
2728 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2730 {
2731 if (c == initc)
2732 count++;
2733 else
2734 {
2735 if (count == 0)
2736 return &pos;
2737 count--;
2738 }
2739 }
2740 }
2741 }
2742 }
2743
2744 if (comment_dir == BACKWARD && count > 0)
2745 {
2746 pos = match_pos;
2747 return &pos;
2748 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002749 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750}
2751
2752/*
2753 * Check if line[] contains a / / comment.
2754 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002756 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002757check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758{
2759 char_u *p;
2760
2761 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002762 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002763 if (curbuf->b_p_lisp)
2764 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002765 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002766 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002767 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002768
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002769 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002770 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002771 {
2772 if (*p == '"')
2773 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002774 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002775 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002776 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002777 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002778 }
2779 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002780 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002781 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002782 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002783 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002784 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002785 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2786 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002787 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002788 ++p;
2789 }
2790 }
2791 else
2792 p = NULL;
2793 }
2794 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002795 while ((p = vim_strchr(p, '/')) != NULL)
2796 {
2797 // Accept a double /, unless it's preceded with * and followed by
2798 // *, because * / / * is an end and start of a C comment. Only
2799 // accept the position if it is not inside a string.
2800 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002801 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002802 break;
2803 ++p;
2804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
2806 if (p == NULL)
2807 return MAXCOL;
2808 return (int)(p - line);
2809}
2810
2811/*
2812 * Move cursor briefly to character matching the one under the cursor.
2813 * Used for Insert mode and "r" command.
2814 * Show the match only if it is visible on the screen.
2815 * If there isn't a match, then beep.
2816 */
2817 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002818showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002819 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820{
2821 pos_T *lpos, save_cursor;
2822 pos_T mpos;
2823 colnr_T vcol;
2824 long save_so;
2825 long save_siso;
2826#ifdef CURSOR_SHAPE
2827 int save_state;
2828#endif
2829 colnr_T save_dollar_vcol;
2830 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002831 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2832 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
2834 /*
2835 * Only show match for chars in the 'matchpairs' option.
2836 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002837 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002838 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
2840#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002841 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002842 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002843#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002844 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002845 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846#ifdef FEAT_RIGHTLEFT
2847 && !(curwin->w_p_rl ^ p_ri)
2848#endif
2849 )
2850 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002851 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002852 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 return;
2854 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002855 if (*p == NUL)
2856 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002858 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002860 vim_beep(BO_MATCH);
2861 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002863
2864 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2865 return;
2866
2867 if (!curwin->w_p_wrap)
2868 getvcol(curwin, lpos, NULL, &vcol, NULL);
2869
2870 int col_visible = (curwin->w_p_wrap
2871 || (vcol >= curwin->w_leftcol
2872 && vcol < curwin->w_leftcol + curwin->w_width));
2873 if (!col_visible)
2874 return;
2875
2876 mpos = *lpos; // save the pos, update_screen() may change it
2877 save_cursor = curwin->w_cursor;
2878 save_so = *so;
2879 save_siso = *siso;
2880 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2881 // stop displaying the "$".
2882 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2883 dollar_vcol = -1;
2884 ++curwin->w_virtcol; // do display ')' just before "$"
2885 update_screen(UPD_VALID); // show the new char first
2886
2887 save_dollar_vcol = dollar_vcol;
2888#ifdef CURSOR_SHAPE
2889 save_state = State;
2890 State = MODE_SHOWMATCH;
2891 ui_cursor_shape(); // may show different cursor shape
2892#endif
2893 curwin->w_cursor = mpos; // move to matching char
2894 *so = 0; // don't use 'scrolloff' here
2895 *siso = 0; // don't use 'sidescrolloff' here
2896 showruler(FALSE);
2897 setcursor();
2898 cursor_on(); // make sure that the cursor is shown
2899 out_flush_cursor(TRUE, FALSE);
2900
2901 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2902 // which resets it if the matching position is in a previous line
2903 // and has a higher column number.
2904 dollar_vcol = save_dollar_vcol;
2905
2906 /*
2907 * brief pause, unless 'm' is present in 'cpo' and a character is
2908 * available.
2909 */
2910 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2911 ui_delay(p_mat * 100L + 8, TRUE);
2912 else if (!char_avail())
2913 ui_delay(p_mat * 100L + 9, FALSE);
2914 curwin->w_cursor = save_cursor; // restore cursor position
2915 *so = save_so;
2916 *siso = save_siso;
2917#ifdef CURSOR_SHAPE
2918 State = save_state;
2919 ui_cursor_shape(); // may show different cursor shape
2920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921}
2922
2923/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002924 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002925 * If move is TRUE, check from the beginning of the buffer, else from position
2926 * "cur".
2927 * "direction" is FORWARD or BACKWARD.
2928 * Returns TRUE, FALSE or -1 for failure.
2929 */
2930 static int
John Marriott8c85a2a2024-05-20 19:18:26 +02002931is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002932{
2933 regmmatch_T regmatch;
2934 int nmatched = 0;
2935 int result = -1;
2936 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002937 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002938 int flag = 0;
2939
2940 if (pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02002941 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002942 pattern = spats[last_idx].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02002943 patternlen = spats[last_idx].patlen;
2944 }
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002945
John Marriott8c85a2a2024-05-20 19:18:26 +02002946 if (search_regcomp(pattern, patternlen, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002947 SEARCH_KEEP, &regmatch) == FAIL)
2948 return -1;
2949
2950 // init startcol correctly
2951 regmatch.startpos[0].col = -1;
2952 // move to match
2953 if (move)
2954 {
2955 CLEAR_POS(&pos);
2956 }
2957 else
2958 {
2959 pos = *cur;
2960 // accept a match at the cursor position
2961 flag = SEARCH_START;
2962 }
2963
John Marriott8c85a2a2024-05-20 19:18:26 +02002964 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, patternlen, 1,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002965 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2966 {
2967 // Zero-width pattern should match somewhere, then we can check if
2968 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002969 do
2970 {
2971 regmatch.startpos[0].col++;
2972 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002973 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002974 if (nmatched != 0)
2975 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002976 } while (regmatch.regprog != NULL
2977 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002978 : regmatch.startpos[0].col > pos.col);
2979
Bram Moolenaar53989552019-12-23 22:59:18 +01002980 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002981 {
2982 result = (nmatched != 0
2983 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2984 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2985 }
2986 }
2987
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002988 vim_regfree(regmatch.regprog);
2989 return result;
2990}
2991
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002992
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002993/*
2994 * Find next search match under cursor, cursor at end.
2995 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002996 */
2997 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002998current_search(
2999 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003000 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003001{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003002 pos_T start_pos; // start position of the pattern match
3003 pos_T end_pos; // end position of the pattern match
3004 pos_T orig_pos; // position of the cursor at beginning
3005 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003006 int i;
3007 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003008 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003009 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003010 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02003011 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003012 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003013 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003014
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003015 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003016 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003017 dec_cursor();
3018
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003019 // When searching forward and the cursor is at the start of the Visual
3020 // area, skip the first search backward, otherwise it doesn't move.
3021 skip_first_backward = forward && VIsual_active
3022 && LT_POS(curwin->w_cursor, VIsual);
3023
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003024 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003025 if (VIsual_active)
3026 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003027 if (forward)
3028 incl(&pos);
3029 else
3030 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003031 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003032
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003033 // Is the pattern is zero-width?, this time, don't care about the direction
John Marriott8c85a2a2024-05-20 19:18:26 +02003034 zero_width = is_zero_width(spats[last_idx].pat, spats[last_idx].patlen,
3035 TRUE, &curwin->w_cursor, FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003036 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003037 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003038
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003039 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003040 * The trick is to first search backwards and then search forward again,
3041 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003042 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003043 */
3044 for (i = 0; i < 2; i++)
3045 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003046 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003047 {
3048 if (i == 0 && skip_first_backward)
3049 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003050 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003051 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003052 else
3053 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003054
3055 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003056 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003057 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003058 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003059
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003060 // wrapping should not occur in the first round
3061 if (i == 0)
3062 p_ws = FALSE;
3063
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003064 result = searchit(curwin, curbuf, &pos, &end_pos,
3065 (dir ? FORWARD : BACKWARD),
John Marriott8c85a2a2024-05-20 19:18:26 +02003066 spats[last_idx].pat, spats[last_idx].patlen, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003067 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003068
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003069 p_ws = old_p_ws;
3070
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003071 // First search may fail, but then start searching from the
3072 // beginning of the file (cursor might be on the search match)
3073 // except when Visual mode is active, so that extending the visual
3074 // selection works.
3075 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003076 {
3077 curwin->w_cursor = orig_pos;
3078 if (VIsual_active)
3079 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003080 return FAIL;
3081 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003082 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003083 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003084 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003085 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003086 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003087 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003088 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003089 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003090 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003091 // try again from end of buffer
3092 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003093 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003094 pos.col = ml_get_len(curwin->w_buffer->b_ml.ml_line_count);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003095 }
3096 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003097 }
3098
3099 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003100
3101 if (!VIsual_active)
3102 VIsual = start_pos;
3103
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003104 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003105 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003106 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003107 {
3108 if (skip_first_backward)
3109 // put the cursor on the start of the match
3110 curwin->w_cursor = pos;
3111 else
3112 // put the cursor on last character of match
3113 dec_cursor();
3114 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003115 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003116 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003117 VIsual_active = TRUE;
3118 VIsual_mode = 'v';
3119
Bram Moolenaarb7633612019-02-10 21:48:25 +01003120 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003121 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003122 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003123 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3124 inc_cursor();
3125 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3126 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003127 }
3128
3129#ifdef FEAT_FOLDING
3130 if (fdo_flags & FDO_SEARCH && KeyTyped)
3131 foldOpenCursor();
3132#endif
3133
3134 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003135 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003136#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003137 // Make sure the clipboard gets updated. Needed because start and
3138 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003139 clip_star.vmode = NUL;
3140#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003141 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003142 showmode();
3143
3144 return OK;
3145}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147/*
3148 * return TRUE if line 'lnum' is empty or has white chars only.
3149 */
3150 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003151linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 char_u *p;
3154
3155 p = skipwhite(ml_get(lnum));
3156 return (*p == NUL);
3157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003159/*
3160 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003161 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003162 */
3163 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003164cmdline_search_stat(
3165 int dirc,
3166 pos_T *pos,
3167 pos_T *cursor_pos,
3168 int show_top_bot_msg,
3169 char_u *msgbuf,
John Marriott8c85a2a2024-05-20 19:18:26 +02003170 size_t msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003171 int recompute,
3172 int maxcount,
3173 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003174{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003175 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003176
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003177 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3178 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003179 if (stat.cur <= 0)
3180 return;
3181
3182 char t[SEARCH_STAT_BUF_LEN];
3183 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003184
3185#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003186 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3187 {
3188 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003189 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003190 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003191 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003192 maxcount, maxcount);
3193 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003194 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003195 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003196 else
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 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003199 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003200 else
3201#endif
3202 {
3203 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003204 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003205 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003206 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003207 maxcount, maxcount);
3208 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003209 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003210 stat.cur, maxcount);
3211 else
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 stat.cur, stat.cnt);
3214 }
3215
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003216 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3217 {
3218 mch_memmove(t + 2, t, len);
3219 t[0] = 'W';
3220 t[1] = ' ';
3221 len += 2;
3222 }
3223
John Marriott8c85a2a2024-05-20 19:18:26 +02003224 if (len > msgbuflen)
3225 len = msgbuflen;
3226 mch_memmove(msgbuf + msgbuflen - len, t, len);
zeertzjqa7d36b72023-01-31 21:13:38 +00003227
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003228 if (dirc == '?' && stat.cur == maxcount + 1)
3229 stat.cur = -1;
3230
3231 // keep the message even after redraw, but don't put in history
3232 msg_hist_off = TRUE;
3233 give_warning(msgbuf, FALSE);
3234 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003235}
3236
3237/*
3238 * Add the search count information to "stat".
3239 * "stat" must not be NULL.
3240 * When "recompute" is TRUE always recompute the numbers.
3241 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3242 * dirc == '/': find the next match
3243 * dirc == '?': find the previous match
3244 */
3245 static void
3246update_search_stat(
3247 int dirc,
3248 pos_T *pos,
3249 pos_T *cursor_pos,
3250 searchstat_T *stat,
3251 int recompute,
3252 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003253 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003254{
3255 int save_ws = p_ws;
3256 int wraparound = FALSE;
3257 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003258 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003259 static int cur = 0;
3260 static int cnt = 0;
3261 static int exact_match = FALSE;
3262 static int incomplete = 0;
3263 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3264 static int chgtick = 0;
3265 static char_u *lastpat = NULL;
3266 static buf_T *lbuf = NULL;
3267#ifdef FEAT_RELTIME
3268 proftime_T start;
3269#endif
3270
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003271 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003272
3273 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3274 {
3275 stat->cur = cur;
3276 stat->cnt = cnt;
3277 stat->exact_match = exact_match;
3278 stat->incomplete = incomplete;
3279 stat->last_maxcount = last_maxcount;
3280 return;
3281 }
3282 last_maxcount = maxcount;
3283
3284 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3285 || (dirc == '/' && LT_POS(p, lastpos)));
3286
3287 // If anything relevant changed the count has to be recomputed.
3288 // MB_STRNICMP ignores case, but we should not ignore case.
3289 // Unfortunately, there is no MB_STRNICMP function.
3290 // XXX: above comment should be "no MB_STRCMP function" ?
3291 if (!(chgtick == CHANGEDTICK(curbuf)
3292 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3293 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3294 && EQUAL_POS(lastpos, *cursor_pos)
3295 && lbuf == curbuf) || wraparound || cur < 0
3296 || (maxcount > 0 && cur > maxcount) || recompute)
3297 {
3298 cur = 0;
3299 cnt = 0;
3300 exact_match = FALSE;
3301 incomplete = 0;
3302 CLEAR_POS(&lastpos);
3303 lbuf = curbuf;
3304 }
3305
Christian Brabandt34a6a362023-05-06 19:20:20 +01003306 // when searching backwards and having jumped to the first occurrence,
3307 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003308 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003309 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003310 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3311 else
3312 {
3313 int done_search = FALSE;
3314 pos_T endpos = {0, 0, 0};
3315
3316 p_ws = FALSE;
3317#ifdef FEAT_RELTIME
3318 if (timeout > 0)
3319 profile_setlimit(timeout, &start);
3320#endif
3321 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003322 FORWARD, NULL, 0, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003323 {
3324 done_search = TRUE;
3325#ifdef FEAT_RELTIME
3326 // Stop after passing the time limit.
3327 if (timeout > 0 && profile_passed_limit(&start))
3328 {
3329 incomplete = 1;
3330 break;
3331 }
3332#endif
3333 cnt++;
3334 if (LTOREQ_POS(lastpos, p))
3335 {
3336 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003337 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003338 exact_match = TRUE;
3339 }
3340 fast_breakcheck();
3341 if (maxcount > 0 && cnt > maxcount)
3342 {
3343 incomplete = 2; // max count exceeded
3344 break;
3345 }
3346 }
3347 if (got_int)
3348 cur = -1; // abort
3349 if (done_search)
3350 {
3351 vim_free(lastpat);
3352 lastpat = vim_strsave(spats[last_idx].pat);
3353 chgtick = CHANGEDTICK(curbuf);
3354 lbuf = curbuf;
3355 lastpos = p;
3356 }
3357 }
3358 stat->cur = cur;
3359 stat->cnt = cnt;
3360 stat->exact_match = exact_match;
3361 stat->incomplete = incomplete;
3362 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003363 p_ws = save_ws;
3364}
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003367
3368/*
3369 * Get line "lnum" and copy it into "buf[LSIZE]".
3370 * The copy is made because the regexp may make the line invalid when using a
3371 * mark.
3372 */
3373 static char_u *
3374get_line_and_copy(linenr_T lnum, char_u *buf)
3375{
3376 char_u *line = ml_get(lnum);
3377
3378 vim_strncpy(buf, line, LSIZE - 1);
3379 return buf;
3380}
3381
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382/*
3383 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003384 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003387find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003388 char_u *ptr, // pointer to search pattern
3389 int dir UNUSED, // direction of expansion
3390 int len, // length of search pattern
3391 int whole, // match whole words only
3392 int skip_comments, // don't match inside comments
3393 int type, // Type of search; are we looking for a type?
3394 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003395 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003396 int action, // What to do when we find it
3397 linenr_T start_lnum, // first line to start searching
Colin Kennedy21570352024-03-03 16:16:47 +01003398 linenr_T end_lnum, // last line for searching
3399 int forceit) // If true, always switch to the found path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003401 SearchedFile *files; // Stack of included files
3402 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 int max_path_depth = 50;
3404 long match_count = 1;
3405
3406 char_u *pat;
3407 char_u *new_fname;
3408 char_u *curr_fname = curbuf->b_fname;
3409 char_u *prev_fname = NULL;
3410 linenr_T lnum;
3411 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003412 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 int old_files;
3414 int already_searched;
3415 char_u *file_line;
3416 char_u *line;
3417 char_u *p;
3418 char_u save_char;
3419 int define_matched;
3420 regmatch_T regmatch;
3421 regmatch_T incl_regmatch;
3422 regmatch_T def_regmatch;
3423 int matched = FALSE;
3424 int did_show = FALSE;
3425 int found = FALSE;
3426 int i;
3427 char_u *already = NULL;
3428 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003429 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003430#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 win_T *curwin_save = NULL;
3432#endif
3433
3434 regmatch.regprog = NULL;
3435 incl_regmatch.regprog = NULL;
3436 def_regmatch.regprog = NULL;
3437
3438 file_line = alloc(LSIZE);
3439 if (file_line == NULL)
3440 return;
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003443 // when CONT_SOL is set compare "ptr" with the beginning of the
3444 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3445 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 pat = alloc(len + 5);
3448 if (pat == NULL)
3449 goto fpip_end;
John Marriott8c85a2a2024-05-20 19:18:26 +02003450 vim_snprintf((char *)pat, len + 5, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003451 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003453 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 vim_free(pat);
3455 if (regmatch.regprog == NULL)
3456 goto fpip_end;
3457 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003458 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3459 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003461 incl_regmatch.regprog = vim_regcomp(inc_opt,
3462 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 if (incl_regmatch.regprog == NULL)
3464 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003465 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
3467 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3468 {
John Marriott8c85a2a2024-05-20 19:18:26 +02003469 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL ? p_def : curbuf->b_p_def,
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003470 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 if (def_regmatch.regprog == NULL)
3472 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003473 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003475 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (files == NULL)
3477 goto fpip_end;
3478 old_files = max_path_depth;
3479 depth = depth_displayed = -1;
3480
3481 lnum = start_lnum;
3482 if (end_lnum > curbuf->b_ml.ml_line_count)
3483 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003484 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003486 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 for (;;)
3489 {
3490 if (incl_regmatch.regprog != NULL
3491 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3492 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003493 char_u *p_fname = (curr_fname == curbuf->b_fname)
3494 ? curbuf->b_ffname : curr_fname;
3495
3496 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003497 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003498 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003499 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003500 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3501 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003502 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003503 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003504 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 already_searched = FALSE;
3506 if (new_fname != NULL)
3507 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003508 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 for (i = 0;; i++)
3510 {
3511 if (i == depth + 1)
3512 i = old_files;
3513 if (i == max_path_depth)
3514 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003515 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3516 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003518 if (type != CHECK_PATH
3519 && action == ACTION_SHOW_ALL
3520 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003522 msg_putchar('\n'); // cursor below last one
3523 if (!got_int) // don't display if 'q'
3524 // typed at "--more--"
3525 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003528 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 prev_fname = NULL;
3530 }
3531 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003532 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 already_searched = TRUE;
3534 break;
3535 }
3536 }
3537 }
3538
3539 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3540 || (new_fname == NULL && !already_searched)))
3541 {
3542 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003543 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 else
3545 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003546 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003547 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003549 msg_puts_title(_("not found "));
3550 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 did_show = TRUE;
3553 while (depth_displayed < depth && !got_int)
3554 {
3555 ++depth_displayed;
3556 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003557 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003559 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003561 if (!got_int) // don't display if 'q' typed
3562 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
3564 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003565 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (new_fname != NULL)
3567 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003568 // using "new_fname" is more reliable, e.g., when
3569 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003570 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 else
3573 {
3574 /*
3575 * Isolate the file name.
3576 * Include the surrounding "" or <> if present.
3577 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003578 if (inc_opt != NULL
3579 && strstr((char *)inc_opt, "\\zs") != NULL)
3580 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003581 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003582 p = incl_regmatch.startp[0];
3583 i = (int)(incl_regmatch.endp[0]
3584 - incl_regmatch.startp[0]);
3585 }
3586 else
3587 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003588 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003589 for (p = incl_regmatch.endp[0];
3590 *p && !vim_isfilec(*p); p++)
3591 ;
3592 for (i = 0; vim_isfilec(p[i]); i++)
3593 ;
3594 }
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (i == 0)
3597 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003598 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003600 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003602 // Avoid checking before the start of the line, can
3603 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003604 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 if (p[-1] == '"' || p[-1] == '<')
3607 {
3608 --p;
3609 ++i;
3610 }
3611 if (p[i] == '"' || p[i] == '>')
3612 ++i;
3613 }
3614 save_char = p[i];
3615 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003616 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 p[i] = save_char;
3618 }
3619
3620 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3621 {
3622 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003623 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003625 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003628 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 }
3630
3631 if (new_fname != NULL)
3632 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003633 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 if (depth + 1 == old_files)
3635 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003636 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (bigger != NULL)
3638 {
3639 for (i = 0; i <= depth; i++)
3640 bigger[i] = files[i];
3641 for (i = depth + 1; i < old_files + max_path_depth; i++)
3642 {
3643 bigger[i].fp = NULL;
3644 bigger[i].name = NULL;
3645 bigger[i].lnum = 0;
3646 bigger[i].matched = FALSE;
3647 }
3648 for (i = old_files; i < max_path_depth; i++)
3649 bigger[i + max_path_depth] = files[i];
3650 old_files += max_path_depth;
3651 max_path_depth *= 2;
3652 vim_free(files);
3653 files = bigger;
3654 }
3655 }
3656 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3657 == NULL)
3658 vim_free(new_fname);
3659 else
3660 {
3661 if (++depth == old_files)
3662 {
3663 /*
3664 * lalloc() for 'bigger' must have failed above. We
3665 * will forget one of our already visited files now.
3666 */
3667 vim_free(files[old_files].name);
3668 ++old_files;
3669 }
3670 files[depth].name = curr_fname = new_fname;
3671 files[depth].lnum = 0;
3672 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (action == ACTION_EXPAND)
3674 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003675 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003676 vim_snprintf((char*)IObuff, IOSIZE,
3677 _("Scanning included file: %s"),
3678 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003679 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003681 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003682 {
3683 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003684 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003685 (char *)new_fname);
3686 verbose_leave();
3687 }
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 }
3691 }
3692 else
3693 {
3694 /*
3695 * Check if the line is a define (type == FIND_DEFINE)
3696 */
3697 p = line;
3698search_line:
3699 define_matched = FALSE;
3700 if (def_regmatch.regprog != NULL
3701 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3702 {
3703 /*
3704 * Pattern must be first identifier after 'define', so skip
3705 * to that position before checking for match of pattern. Also
3706 * don't let it match beyond the end of this identifier.
3707 */
3708 p = def_regmatch.endp[0];
3709 while (*p && !vim_iswordc(*p))
3710 p++;
3711 define_matched = TRUE;
3712 }
3713
3714 /*
3715 * Look for a match. Don't do this if we are looking for a
3716 * define and this line didn't match define_prog above.
3717 */
3718 if (def_regmatch.regprog == NULL || define_matched)
3719 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003720 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003722 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 startp = skipwhite(p);
3724 if (p_ic)
3725 matched = !MB_STRNICMP(startp, ptr, len);
3726 else
3727 matched = !STRNCMP(startp, ptr, len);
3728 if (matched && define_matched && whole
3729 && vim_iswordc(startp[len]))
3730 matched = FALSE;
3731 }
3732 else if (regmatch.regprog != NULL
3733 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3734 {
3735 matched = TRUE;
3736 startp = regmatch.startp[0];
3737 /*
3738 * Check if the line is not a comment line (unless we are
3739 * looking for a define). A line starting with "# define"
3740 * is not considered to be a comment line.
3741 */
3742 if (!define_matched && skip_comments)
3743 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if ((*line != '#' ||
3745 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003746 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 matched = FALSE;
3748
3749 /*
3750 * Also check for a "/ *" or "/ /" before the match.
3751 * Skips lines like "int backwards; / * normal index
3752 * * /" when looking for "normal".
3753 * Note: Doesn't skip "/ *" in comments.
3754 */
3755 p = skipwhite(line);
3756 if (matched
3757 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 for (p = line; *p && p < startp; ++p)
3759 {
3760 if (matched
3761 && p[0] == '/'
3762 && (p[1] == '*' || p[1] == '/'))
3763 {
3764 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003765 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (p[1] == '/')
3767 break;
3768 ++p;
3769 }
3770 else if (!matched && p[0] == '*' && p[1] == '/')
3771 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003772 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 matched = TRUE;
3774 ++p;
3775 }
3776 }
3777 }
3778 }
3779 }
3780 }
3781 if (matched)
3782 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 if (action == ACTION_EXPAND)
3784 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003785 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 int add_r;
3787 char_u *aux;
3788
3789 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3790 break;
3791 found = TRUE;
3792 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003793 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003795 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (vim_iswordp(p))
3797 goto exit_matched;
3798 p = find_word_start(p);
3799 }
3800 p = find_word_end(p);
3801 i = (int)(p - aux);
3802
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003803 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003805 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003807
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003808 // Get the next line: when "depth" < 0 from the current
3809 // buffer, otherwise from the included file. Jump to
3810 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003811 if (depth < 0)
3812 {
3813 if (lnum >= end_lnum)
3814 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003815 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003816 }
3817 else if (vim_fgets(line = file_line,
3818 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 goto exit_matched;
3820
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003821 // we read a line, set "already" to check this "line" later
3822 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003823 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 already = aux = p = skipwhite(line);
3825 p = find_word_start(p);
3826 p = find_word_end(p);
3827 if (p > aux)
3828 {
3829 if (*aux != ')' && IObuff[i-1] != TAB)
3830 {
3831 if (IObuff[i-1] != ' ')
3832 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003833 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (p_js
3835 && (IObuff[i-2] == '.'
3836 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3837 && (IObuff[i-2] == '?'
3838 || IObuff[i-2] == '!'))))
3839 IObuff[i++] = ' ';
3840 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003841 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 if (p - aux >= IOSIZE - i)
3843 p = aux + IOSIZE - i - 1;
3844 STRNCPY(IObuff + i, aux, p - aux);
3845 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003846 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 IObuff[i] = NUL;
3849 aux = IObuff;
3850
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003851 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 goto exit_matched;
3853 }
3854
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003855 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003857 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003859 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003861 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 break;
3863 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003864 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 {
3866 found = TRUE;
3867 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003868 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (curr_fname != prev_fname)
3870 {
3871 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003872 msg_putchar('\n'); // cursor below last one
3873 if (!got_int) // don't display if 'q' typed
3874 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 msg_home_replace_hl(curr_fname);
3876 prev_fname = curr_fname;
3877 }
3878 did_show = TRUE;
3879 if (!got_int)
3880 show_pat_in_path(line, type, TRUE, action,
3881 (depth == -1) ? NULL : files[depth].fp,
3882 (depth == -1) ? &lnum : &files[depth].lnum,
3883 match_count++);
3884
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003885 // Set matched flag for this file and all the ones that
3886 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 for (i = 0; i <= depth; ++i)
3888 files[i].matched = TRUE;
3889 }
3890 else if (--count <= 0)
3891 {
3892 found = TRUE;
3893 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003894#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 && g_do_tagpreview == 0
3896#endif
3897 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003898 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 else if (action == ACTION_SHOW)
3900 {
3901 show_pat_in_path(line, type, did_show, action,
3902 (depth == -1) ? NULL : files[depth].fp,
3903 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3904 did_show = TRUE;
3905 }
3906 else
3907 {
3908#ifdef FEAT_GUI
3909 need_mouse_correct = TRUE;
3910#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003911#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003912 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 if (g_do_tagpreview != 0)
3914 {
3915 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003916 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918#endif
3919 if (action == ACTION_SPLIT)
3920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003923 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 }
3925 if (depth == -1)
3926 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003927 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003928#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (g_do_tagpreview != 0)
3930 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003931 if (!win_valid(curwin_save))
3932 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003933 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003934 curwin_save->w_buffer->b_fnum, NULL,
Colin Kennedy21570352024-03-03 16:16:47 +01003935 NULL, TRUE, lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003936 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938 else
3939#endif
3940 setpcmark();
3941 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003942 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944 else
3945 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003946 if (!GETFILE_SUCCESS(getfile(
3947 0, files[depth].name, NULL, TRUE,
Colin Kennedy21570352024-03-03 16:16:47 +01003948 files[depth].lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003949 break; // failed to jump to file
3950 // autocommands may have changed the lnum, we don't
3951 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 curwin->w_cursor.lnum = files[depth].lnum;
3953 }
3954 }
3955 if (action != ACTION_SHOW)
3956 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003957 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 curwin->w_set_curswant = TRUE;
3959 }
3960
Bram Moolenaar4033c552017-09-16 20:54:51 +02003961#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003963 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003965 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003967 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 win_enter(curwin_save, TRUE);
3969 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003970# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003971 else if (WIN_IS_POPUP(curwin))
3972 // can't keep focus in popup window
3973 win_enter(firstwin, TRUE);
3974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#endif
3976 break;
3977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003980 // look for other matches in the rest of the line if we
3981 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003984 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003985 && *startp != NUL
John Marriott8c85a2a2024-05-20 19:18:26 +02003986 && *(startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 goto search_line;
3988 }
3989 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003991 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003992 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 break;
3994
3995 /*
3996 * Read the next line. When reading an included file and encountering
3997 * end-of-file, close the file and continue in the file that included
3998 * it.
3999 */
4000 while (depth >= 0 && !already
4001 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4002 {
4003 fclose(files[depth].fp);
4004 --old_files;
4005 files[old_files].name = files[depth].name;
4006 files[old_files].matched = files[depth].matched;
4007 --depth;
4008 curr_fname = (depth == -1) ? curbuf->b_fname
4009 : files[depth].name;
4010 if (depth < depth_displayed)
4011 depth_displayed = depth;
4012 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004013 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004016 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004017 i = (int)STRLEN(line);
4018 if (i > 0 && line[i - 1] == '\n')
4019 line[--i] = NUL;
4020 if (i > 0 && line[i - 1] == '\r')
4021 line[--i] = NUL;
4022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 else if (!already)
4024 {
4025 if (++lnum > end_lnum)
4026 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01004027 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 }
4029 already = NULL;
4030 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004031 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004033 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 for (i = 0; i <= depth; i++)
4035 {
4036 fclose(files[i].fp);
4037 vim_free(files[i].name);
4038 }
4039 for (i = old_files; i < max_path_depth; i++)
4040 vim_free(files[i].name);
4041 vim_free(files);
4042
4043 if (type == CHECK_PATH)
4044 {
4045 if (!did_show)
4046 {
4047 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004048 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004050 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
4052 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004053 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004055 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004056 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004058 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004060 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
4062 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4063 msg_end();
4064
4065fpip_end:
4066 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02004067 vim_regfree(regmatch.regprog);
4068 vim_regfree(incl_regmatch.regprog);
4069 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070}
4071
4072 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004073show_pat_in_path(
4074 char_u *line,
4075 int type,
4076 int did_show,
4077 int action,
4078 FILE *fp,
4079 linenr_T *lnum,
4080 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081{
4082 char_u *p;
John Marriott8c85a2a2024-05-20 19:18:26 +02004083 size_t linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004086 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004087 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004088 gotocmdline(TRUE); // cursor at status line
4089 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 return;
John Marriott8c85a2a2024-05-20 19:18:26 +02004091 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 for (;;)
4093 {
John Marriott8c85a2a2024-05-20 19:18:26 +02004094 p = line + linelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (fp != NULL)
4096 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004097 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (p >= line && *p == '\n')
4099 --p;
4100 if (p >= line && *p == '\r')
4101 --p;
4102 *(p + 1) = NUL;
4103 }
4104 if (action == ACTION_SHOW_ALL)
4105 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004106 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004107 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004108 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4109 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004110 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4111 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004113 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004114 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004116 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4118 break;
4119
4120 if (fp != NULL)
4121 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004122 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 break;
John Marriott8c85a2a2024-05-20 19:18:26 +02004124 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 ++*lnum;
4126 }
4127 else
4128 {
4129 if (++*lnum > curbuf->b_ml.ml_line_count)
4130 break;
4131 line = ml_get(*lnum);
John Marriott8c85a2a2024-05-20 19:18:26 +02004132 linelen = ml_get_len(*lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 }
4134 msg_putchar('\n');
4135 }
4136}
4137#endif
4138
4139#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004140/*
4141 * Return the last used search pattern at "idx".
4142 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004143 spat_T *
4144get_spat(int idx)
4145{
4146 return &spats[idx];
4147}
4148
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004149/*
4150 * Return the last used search pattern index.
4151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004153get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004155 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004158
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004159#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004160/*
4161 * "searchcount()" function
4162 */
4163 void
4164f_searchcount(typval_T *argvars, typval_T *rettv)
4165{
4166 pos_T pos = curwin->w_cursor;
4167 char_u *pattern = NULL;
4168 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4169 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004170 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004171 searchstat_T stat;
4172
4173 if (rettv_dict_alloc(rettv) == FAIL)
4174 return;
4175
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004176 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4177 return;
4178
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004179 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4180 recompute = TRUE;
4181
4182 if (argvars[0].v_type != VAR_UNKNOWN)
4183 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004184 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004185 dictitem_T *di;
4186 listitem_T *li;
4187 int error = FALSE;
4188
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004189 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004190 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004191 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004192 di = dict_find(dict, (char_u *)"timeout", -1);
4193 if (di != NULL)
4194 {
4195 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4196 if (error)
4197 return;
4198 }
4199 di = dict_find(dict, (char_u *)"maxcount", -1);
4200 if (di != NULL)
4201 {
4202 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4203 if (error)
4204 return;
4205 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004206 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004207 di = dict_find(dict, (char_u *)"pattern", -1);
4208 if (di != NULL)
4209 {
4210 pattern = tv_get_string_chk(&di->di_tv);
4211 if (pattern == NULL)
4212 return;
4213 }
4214 di = dict_find(dict, (char_u *)"pos", -1);
4215 if (di != NULL)
4216 {
4217 if (di->di_tv.v_type != VAR_LIST)
4218 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004219 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004220 return;
4221 }
4222 if (list_len(di->di_tv.vval.v_list) != 3)
4223 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004224 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004225 return;
4226 }
4227 li = list_find(di->di_tv.vval.v_list, 0L);
4228 if (li != NULL)
4229 {
4230 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4231 if (error)
4232 return;
4233 }
4234 li = list_find(di->di_tv.vval.v_list, 1L);
4235 if (li != NULL)
4236 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004237 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004238 if (error)
4239 return;
4240 }
4241 li = list_find(di->di_tv.vval.v_list, 2L);
4242 if (li != NULL)
4243 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004244 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004245 if (error)
4246 return;
4247 }
4248 }
4249 }
4250
4251 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004252#ifdef FEAT_SEARCH_EXTRA
4253 save_incsearch_state();
4254#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004255 if (pattern != NULL)
4256 {
4257 if (*pattern == NUL)
4258 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004259 vim_free(spats[last_idx].pat);
John Marriott8c85a2a2024-05-20 19:18:26 +02004260 spats[last_idx].patlen = STRLEN(pattern);
4261 spats[last_idx].pat = vim_strnsave(pattern, spats[last_idx].patlen);
4262 if (spats[last_idx].pat == NULL)
4263 spats[last_idx].patlen = 0;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004264 }
4265 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4266 goto the_end; // the previous pattern was never defined
4267
4268 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4269
4270 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4271 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4272 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4273 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4274 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4275
4276the_end:
4277 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004278#ifdef FEAT_SEARCH_EXTRA
4279 restore_incsearch_state();
4280#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004281}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004282#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004283
4284/*
4285 * Fuzzy string matching
4286 *
4287 * Ported from the lib_fts library authored by Forrest Smith.
4288 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4289 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004290 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004291 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4292 *
4293 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004294 * - Matched letter
4295 * - Unmatched letter
4296 * - Consecutively matched letters
4297 * - Proximity to start
4298 * - Letter following a separator (space, underscore)
4299 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004300 *
4301 * Matched letters are good. Unmatched letters are bad. Matching near the start
4302 * is good. Matching the first letter in the middle of a phrase is good.
4303 * Matching the uppercase letters in camel case entries is good.
4304 *
4305 * The score assigned for each factor is explained below.
4306 * File paths are different from file names. File extensions may be ignorable.
4307 * Single words care about consecutive matches but not separators or camel
4308 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004309 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004310 * Matched letter: +0 points
4311 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004312 * Consecutive match bonus: +15 points
4313 * First letter bonus: +15 points
4314 * Separator bonus: +30 points
4315 * Camel case bonus: +30 points
4316 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004317 *
4318 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004319 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004320 * lower minimum score due to unmatched letter penalty. Longer search patterns
4321 * have a higher maximum score due to match bonuses.
4322 *
4323 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4324 * quite a bit.
4325 *
4326 * There is a penalty if you DON’T match the first three letters. Which
4327 * effectively rewards matching near the start. However there’s no difference
4328 * in matching between the middle and end.
4329 *
4330 * There is not an explicit bonus for an exact match. Unmatched letters receive
4331 * a penalty. So shorter strings and closer matches are worth more.
4332 */
4333typedef struct
4334{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004335 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004336 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004337 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004338 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004339} fuzzyItem_T;
4340
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004341// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4342// matching a whole word is preferred.
4343#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004344// bonus if match occurs after a path separator
4345#define PATH_SEPARATOR_BONUS 30
4346// bonus if match occurs after a word separator
4347#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004348// bonus if match is uppercase and prev is lower
4349#define CAMEL_BONUS 30
4350// bonus if the first letter is matched
4351#define FIRST_LETTER_BONUS 15
4352// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004353#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004354// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004355#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004356// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004357#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004358// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004359#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004360// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004361#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004362
4363#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004364
4365/*
4366 * Compute a score for a fuzzy matched string. The matching character locations
4367 * are in 'matches'.
4368 */
4369 static int
4370fuzzy_match_compute_score(
4371 char_u *str,
4372 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004373 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004374 int numMatches)
4375{
4376 int score;
4377 int penalty;
4378 int unmatched;
4379 int i;
4380 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004381 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004382
4383 // Initialize score
4384 score = 100;
4385
4386 // Apply leading letter penalty
4387 penalty = LEADING_LETTER_PENALTY * matches[0];
4388 if (penalty < MAX_LEADING_LETTER_PENALTY)
4389 penalty = MAX_LEADING_LETTER_PENALTY;
4390 score += penalty;
4391
4392 // Apply unmatched penalty
4393 unmatched = strSz - numMatches;
4394 score += UNMATCHED_LETTER_PENALTY * unmatched;
4395
4396 // Apply ordering bonuses
4397 for (i = 0; i < numMatches; ++i)
4398 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004399 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004400
4401 if (i > 0)
4402 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004403 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004404
4405 // Sequential
4406 if (currIdx == (prevIdx + 1))
4407 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004408 else
4409 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004410 }
4411
4412 // Check for bonuses based on neighbor character value
4413 if (currIdx > 0)
4414 {
4415 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004416 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004417 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004418
4419 if (has_mbyte)
4420 {
4421 while (sidx < currIdx)
4422 {
4423 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004424 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004425 sidx++;
4426 }
4427 curr = (*mb_ptr2char)(p);
4428 }
4429 else
4430 {
4431 neighbor = str[currIdx - 1];
4432 curr = str[currIdx];
4433 }
4434
4435 if (vim_islower(neighbor) && vim_isupper(curr))
4436 score += CAMEL_BONUS;
4437
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004438 // Bonus if the match follows a separator character
4439 if (neighbor == '/' || neighbor == '\\')
4440 score += PATH_SEPARATOR_BONUS;
4441 else if (neighbor == ' ' || neighbor == '_')
4442 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004443 }
4444 else
4445 {
4446 // First letter
4447 score += FIRST_LETTER_BONUS;
4448 }
4449 }
4450 return score;
4451}
4452
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004453/*
4454 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4455 * Return the number of matching characters.
4456 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004457 static int
4458fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004459 char_u *fuzpat,
4460 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004461 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004462 int *outScore,
4463 char_u *strBegin,
4464 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004465 int_u *srcMatches,
4466 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004467 int maxMatches,
4468 int nextMatch,
4469 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004470{
4471 // Recursion params
4472 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004473 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004474 int bestRecursiveScore = 0;
4475 int first_match;
4476 int matched;
4477
4478 // Count recursions
4479 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004480 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004481 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004482
4483 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004484 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004485 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004486
4487 // Loop through fuzpat and str looking for a match
4488 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004489 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004490 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004491 int c1;
4492 int c2;
4493
4494 c1 = PTR2CHAR(fuzpat);
4495 c2 = PTR2CHAR(str);
4496
Bram Moolenaar635414d2020-09-11 22:25:15 +02004497 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004498 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004499 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004500 // Supplied matches buffer was too short
4501 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004502 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004503
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004504 int recursiveScore = 0;
4505 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4506 CLEAR_FIELD(recursiveMatches);
4507
Bram Moolenaar635414d2020-09-11 22:25:15 +02004508 // "Copy-on-Write" srcMatches into matches
4509 if (first_match && srcMatches)
4510 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004511 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004512 first_match = FALSE;
4513 }
4514
4515 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004516 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004517 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4518 &recursiveScore, strBegin, strLen, matches,
4519 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004520 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004521 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004522 {
4523 // Pick best recursive score
4524 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4525 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004526 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004527 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004528 bestRecursiveScore = recursiveScore;
4529 }
4530 recursiveMatch = TRUE;
4531 }
4532
4533 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004534 matches[nextMatch++] = strIdx;
4535 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004536 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004537 else
4538 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004539 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004540 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004541 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004542 else
4543 ++str;
4544 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004545 }
4546
4547 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004548 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004549
4550 // Calculate score
4551 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004552 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4553 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004554
4555 // Return best result
4556 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4557 {
4558 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004559 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004560 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004561 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004562 }
4563 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004564 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004565
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004566 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004567}
4568
4569/*
4570 * fuzzy_match()
4571 *
4572 * Performs exhaustive search via recursion to find all possible matches and
4573 * match with highest score.
4574 * Scores values have no intrinsic meaning. Possible score range is not
4575 * normalized and varies with pattern.
4576 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004577 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004578 * Uses char_u for match indices. Therefore patterns are limited to
4579 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004580 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004581 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4582 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004583 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004584 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004585fuzzy_match(
4586 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004587 char_u *pat_arg,
4588 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004589 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004590 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004591 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004592{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004593 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004594 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004595 char_u *save_pat;
4596 char_u *pat;
4597 char_u *p;
4598 int complete = FALSE;
4599 int score = 0;
4600 int numMatches = 0;
4601 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004602
4603 *outScore = 0;
4604
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004605 save_pat = vim_strsave(pat_arg);
4606 if (save_pat == NULL)
4607 return FALSE;
4608 pat = save_pat;
4609 p = pat;
4610
4611 // Try matching each word in 'pat_arg' in 'str'
4612 while (TRUE)
4613 {
4614 if (matchseq)
4615 complete = TRUE;
4616 else
4617 {
4618 // Extract one word from the pattern (separated by space)
4619 p = skipwhite(p);
4620 if (*p == NUL)
4621 break;
4622 pat = p;
4623 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4624 {
4625 if (has_mbyte)
4626 MB_PTR_ADV(p);
4627 else
4628 ++p;
4629 }
4630 if (*p == NUL) // processed all the words
4631 complete = TRUE;
4632 *p = NUL;
4633 }
4634
4635 score = 0;
4636 recursionCount = 0;
4637 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4638 matches + numMatches, maxMatches - numMatches,
4639 0, &recursionCount);
4640 if (matchCount == 0)
4641 {
4642 numMatches = 0;
4643 break;
4644 }
4645
4646 // Accumulate the match score and the number of matches
4647 *outScore += score;
4648 numMatches += matchCount;
4649
4650 if (complete)
4651 break;
4652
4653 // try matching the next word
4654 ++p;
4655 }
4656
4657 vim_free(save_pat);
4658 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004659}
4660
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004661#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004662/*
4663 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004664 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004665 */
4666 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004667fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004668{
4669 int v1 = ((fuzzyItem_T *)s1)->score;
4670 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004671 int idx1 = ((fuzzyItem_T *)s1)->idx;
4672 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004673
zeertzjq77078272024-02-10 13:24:03 +01004674 if (v1 == v2)
4675 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4676 else
4677 return v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004678}
4679
4680/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004681 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4682 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004683 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4684 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004685 * If 'items' is a list of strings, then search for 'str' in the list.
4686 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4687 * for each item or use 'item_cb' Funcref function to get the string.
4688 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4689 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004690 */
4691 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004692fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004693 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004694 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004695 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004696 char_u *key,
4697 callback_T *item_cb,
4698 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004699 list_T *fmatchlist,
4700 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004701{
4702 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004703 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004704 listitem_T *li;
4705 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004706 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004707 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004708
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004709 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004710 if (len == 0)
4711 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004712 if (max_matches > 0 && len > max_matches)
4713 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004714
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004715 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4716 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004717 return;
4718
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004719 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004720 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004721 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004722 int score;
4723 char_u *itemstr;
4724 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004725
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004726 if (max_matches > 0 && match_count >= max_matches)
4727 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004728
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004729 itemstr = NULL;
4730 rettv.v_type = VAR_UNKNOWN;
4731 if (li->li_tv.v_type == VAR_STRING) // list of strings
4732 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004733 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004734 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004735 {
4736 // For a dict, either use the specified key to lookup the string or
4737 // use the specified callback function to get the string.
4738 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004739 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4740 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004741 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004742 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004743 typval_T argv[2];
4744
4745 // Invoke the supplied callback (if any) to get the dict item
4746 li->li_tv.vval.v_dict->dv_refcount++;
4747 argv[0].v_type = VAR_DICT;
4748 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4749 argv[1].v_type = VAR_UNKNOWN;
4750 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4751 {
4752 if (rettv.v_type == VAR_STRING)
4753 itemstr = rettv.vval.v_string;
4754 }
4755 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004756 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004757 }
4758
4759 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004760 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004761 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004762 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004763 items[match_count].idx = match_count;
4764 items[match_count].item = li;
4765 items[match_count].score = score;
4766
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004767 // Copy the list of matching positions in itemstr to a list, if
4768 // 'retmatchpos' is set.
4769 if (retmatchpos)
4770 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004771 int j = 0;
4772 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004773
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004774 items[match_count].lmatchpos = list_alloc();
4775 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004776 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004777
4778 p = str;
4779 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004780 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004781 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004782 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004783 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004784 matches[j]) == FAIL)
4785 goto done;
4786 j++;
4787 }
4788 if (has_mbyte)
4789 MB_PTR_ADV(p);
4790 else
4791 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004792 }
4793 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004794 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004795 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004796 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004797 }
4798
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004799 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004800 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004801 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004802
Bram Moolenaar635414d2020-09-11 22:25:15 +02004803 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004804 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004805 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004806
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004807 // For matchfuzzy(), return a list of matched strings.
4808 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004809 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004810 // The first item is a list of matched strings. The second item
4811 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004812 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004813 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4814 if (retmatchpos)
4815 {
4816 li = list_find(fmatchlist, 0);
4817 if (li == NULL || li->li_tv.vval.v_list == NULL)
4818 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004819 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004820 }
4821 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004822 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004823
4824 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004825 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004826 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004827 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004828 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004829 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004830 }
4831
4832 // next copy the list of matching positions
4833 if (retmatchpos)
4834 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004835 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004836 if (li == NULL || li->li_tv.vval.v_list == NULL)
4837 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004838 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004839
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004840 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004841 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004842 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004843 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004844 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004845 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004846 goto done;
4847 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004848
4849 // copy the matching scores
4850 li = list_find(fmatchlist, -1);
4851 if (li == NULL || li->li_tv.vval.v_list == NULL)
4852 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004853 retlist = li->li_tv.vval.v_list;
4854 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004855 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004856 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004857 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004858 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004859 goto done;
4860 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004861 }
4862 }
4863
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004864done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004865 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004866}
4867
4868/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004869 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4870 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4871 */
4872 static void
4873do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4874{
4875 callback_T cb;
4876 char_u *key = NULL;
4877 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004878 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004879 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004880
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004881 if (in_vim9script()
4882 && (check_for_list_arg(argvars, 0) == FAIL
4883 || check_for_string_arg(argvars, 1) == FAIL
4884 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4885 return;
4886
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004887 CLEAR_POINTER(&cb);
4888
4889 // validate and get the arguments
4890 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4891 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004892 semsg(_(e_argument_of_str_must_be_list),
4893 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004894 return;
4895 }
4896 if (argvars[1].v_type != VAR_STRING
4897 || argvars[1].vval.v_string == NULL)
4898 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004899 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004900 return;
4901 }
4902
4903 if (argvars[2].v_type != VAR_UNKNOWN)
4904 {
4905 dict_T *d;
4906 dictitem_T *di;
4907
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004908 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004909 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004910
4911 // To search a dict, either a callback function or a key can be
4912 // specified.
4913 d = argvars[2].vval.v_dict;
4914 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4915 {
4916 if (di->di_tv.v_type != VAR_STRING
4917 || di->di_tv.vval.v_string == NULL
4918 || *di->di_tv.vval.v_string == NUL)
4919 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004920 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004921 return;
4922 }
4923 key = tv_get_string(&di->di_tv);
4924 }
4925 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4926 {
4927 cb = get_callback(&di->di_tv);
4928 if (cb.cb_name == NULL)
4929 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004930 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004931 return;
4932 }
4933 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004934
4935 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004936 {
4937 if (di->di_tv.v_type != VAR_NUMBER)
4938 {
4939 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4940 return;
4941 }
4942 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4943 }
4944
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004945 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004946 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004947 }
4948
4949 // get the fuzzy matches
4950 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004951 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004952 goto done;
4953 if (retmatchpos)
4954 {
4955 list_T *l;
4956
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004957 // For matchfuzzypos(), a list with three items are returned. First
4958 // item is a list of matching strings, the second item is a list of
4959 // lists with matching positions within each string and the third item
4960 // is the list of scores of the matches.
4961 l = list_alloc();
4962 if (l == NULL)
4963 goto done;
4964 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004965 {
4966 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004967 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004968 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004969 l = list_alloc();
4970 if (l == NULL)
4971 goto done;
4972 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004973 {
4974 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004975 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004976 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004977 l = list_alloc();
4978 if (l == NULL)
4979 goto done;
4980 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004981 {
4982 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004983 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004984 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004985 }
4986
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004987 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004988 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004989
4990done:
4991 free_callback(&cb);
4992}
4993
4994/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004995 * "matchfuzzy()" function
4996 */
4997 void
4998f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4999{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02005000 do_fuzzymatch(argvars, rettv, FALSE);
5001}
5002
5003/*
5004 * "matchfuzzypos()" function
5005 */
5006 void
5007f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
5008{
5009 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02005010}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02005011#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005012
5013/*
5014 * Same as fuzzy_match_item_compare() except for use with a string match
5015 */
5016 static int
5017fuzzy_match_str_compare(const void *s1, const void *s2)
5018{
5019 int v1 = ((fuzmatch_str_T *)s1)->score;
5020 int v2 = ((fuzmatch_str_T *)s2)->score;
5021 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5022 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5023
Christian Brabandte06e4372024-02-09 19:39:14 +01005024 if (v1 == v2)
5025 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5026 else
5027 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005028}
5029
5030/*
5031 * Sort fuzzy matches by score
5032 */
5033 static void
5034fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
5035{
5036 // Sort the list by the descending order of the match score
5037 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5038 fuzzy_match_str_compare);
5039}
5040
5041/*
5042 * Same as fuzzy_match_item_compare() except for use with a function name
5043 * string match. <SNR> functions should be sorted to the end.
5044 */
5045 static int
5046fuzzy_match_func_compare(const void *s1, const void *s2)
5047{
5048 int v1 = ((fuzmatch_str_T *)s1)->score;
5049 int v2 = ((fuzmatch_str_T *)s2)->score;
5050 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5051 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5052 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
5053 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
5054
Christian Brabandte06e4372024-02-09 19:39:14 +01005055 if (*str1 != '<' && *str2 == '<')
5056 return -1;
5057 if (*str1 == '<' && *str2 != '<')
5058 return 1;
5059 if (v1 == v2)
5060 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5061 else
5062 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005063}
5064
5065/*
5066 * Sort fuzzy matches of function names by score.
5067 * <SNR> functions should be sorted to the end.
5068 */
5069 static void
5070fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
5071{
5072 // Sort the list by the descending order of the match score
5073 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5074 fuzzy_match_func_compare);
5075}
5076
5077/*
5078 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
5079 * returns the match score.
5080 */
5081 int
5082fuzzy_match_str(char_u *str, char_u *pat)
5083{
5084 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00005085 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005086
5087 if (str == NULL || pat == NULL)
5088 return 0;
5089
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00005090 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005091 sizeof(matchpos) / sizeof(matchpos[0]));
5092
5093 return score;
5094}
5095
5096/*
glepnir40c1c332024-06-11 19:37:04 +02005097 * Fuzzy match the position of string 'pat' in string 'str'.
5098 * Returns a dynamic array of matching positions. If there is no match,
5099 * returns NULL.
5100 */
5101 garray_T *
5102fuzzy_match_str_with_pos(char_u *str UNUSED, char_u *pat UNUSED)
5103{
5104#ifdef FEAT_SEARCH_EXTRA
5105 int score = 0;
zeertzjq2f95ca92024-06-13 17:14:27 +02005106 garray_T *match_positions = NULL;
5107 int_u matches[MAX_FUZZY_MATCHES];
5108 int j = 0;
glepnir40c1c332024-06-11 19:37:04 +02005109
zeertzjq2f95ca92024-06-13 17:14:27 +02005110 if (str == NULL || pat == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005111 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005112
5113 match_positions = ALLOC_ONE(garray_T);
glepnir40c1c332024-06-11 19:37:04 +02005114 if (match_positions == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005115 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005116 ga_init2(match_positions, sizeof(int_u), 10);
5117
5118 if (!fuzzy_match(str, pat, FALSE, &score, matches, MAX_FUZZY_MATCHES)
5119 || score == 0)
glepnir40c1c332024-06-11 19:37:04 +02005120 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005121 ga_clear(match_positions);
5122 vim_free(match_positions);
5123 return NULL;
glepnir40c1c332024-06-11 19:37:04 +02005124 }
5125
zeertzjq2f95ca92024-06-13 17:14:27 +02005126 for (char_u *p = pat; *p != NUL; MB_PTR_ADV(p))
glepnir40c1c332024-06-11 19:37:04 +02005127 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005128 if (!VIM_ISWHITE(PTR2CHAR(p)))
5129 {
5130 ga_grow(match_positions, 1);
5131 ((int_u *)match_positions->ga_data)[match_positions->ga_len] =
5132 matches[j];
5133 match_positions->ga_len++;
5134 j++;
5135 }
glepnir40c1c332024-06-11 19:37:04 +02005136 }
5137
glepnir40c1c332024-06-11 19:37:04 +02005138 return match_positions;
glepnir40c1c332024-06-11 19:37:04 +02005139#else
5140 return NULL;
5141#endif
5142}
5143
5144/*
glepnir8159fb12024-07-17 20:32:54 +02005145 * This function searches for a fuzzy match of the pattern `pat` within the
5146 * line pointed to by `*ptr`. It splits the line into words, performs fuzzy
5147 * matching on each word, and returns the length and position of the first
5148 * matched word.
5149 */
5150 static int
5151fuzzy_match_str_in_line(char_u **ptr, char_u *pat, int *len, pos_T *current_pos)
5152{
5153 char_u *str = *ptr;
5154 char_u *strBegin = str;
5155 char_u *end = NULL;
5156 char_u *start = NULL;
5157 int found = FALSE;
5158 int result;
5159 char save_end;
5160
5161 if (str == NULL || pat == NULL)
5162 return found;
5163
5164 while (*str != NUL)
5165 {
5166 // Skip non-word characters
5167 start = find_word_start(str);
5168 if (*start == NUL)
5169 break;
5170 end = find_word_end(start);
5171
5172 // Extract the word from start to end
5173 save_end = *end;
5174 *end = NUL;
5175
5176 // Perform fuzzy match
5177 result = fuzzy_match_str(start, pat);
5178 *end = save_end;
5179
5180 if (result > 0)
5181 {
5182 *len = (int)(end - start);
5183 current_pos->col += (int)(end - strBegin);
5184 found = TRUE;
5185 *ptr = start;
5186 break;
5187 }
5188
5189 // Move to the end of the current word for the next iteration
5190 str = end;
5191 // Ensure we continue searching after the current word
5192 while (*str != NUL && !vim_iswordp(str))
5193 MB_PTR_ADV(str);
5194 }
5195
5196 return found;
5197}
5198
5199/*
5200 * Search for the next fuzzy match in the specified buffer.
5201 * This function attempts to find the next occurrence of the given pattern
5202 * in the buffer, starting from the current position. It handles line wrapping
5203 * and direction of search.
5204 *
5205 * Return TRUE if a match is found, otherwise FALSE.
5206 */
5207 int
5208search_for_fuzzy_match(
5209 buf_T *buf,
5210 pos_T *pos,
5211 char_u *pattern,
5212 int dir,
5213 pos_T *start_pos,
5214 int *len,
5215 char_u **ptr,
5216 int whole_line)
5217{
5218 pos_T current_pos = *pos;
5219 pos_T circly_end;
zeertzjq58d70522024-08-31 17:05:39 +02005220 int found_new_match = FALSE;
glepnir8159fb12024-07-17 20:32:54 +02005221 int looped_around = FALSE;
glepnir7cfe6932024-09-15 20:06:28 +02005222 char_u *next_word_end = NULL;
5223 char_u *match_word = NULL;
glepnir8159fb12024-07-17 20:32:54 +02005224
5225 if (whole_line)
5226 current_pos.lnum += dir;
5227
glepnir0be03e12024-07-19 16:45:05 +02005228 if (buf == curbuf)
5229 circly_end = *start_pos;
5230 else
5231 {
5232 circly_end.lnum = buf->b_ml.ml_line_count;
5233 circly_end.col = 0;
5234 circly_end.coladd = 0;
5235 }
5236
glepnir8159fb12024-07-17 20:32:54 +02005237 do {
glepnir8159fb12024-07-17 20:32:54 +02005238
5239 // Check if looped around and back to start position
5240 if (looped_around && EQUAL_POS(current_pos, circly_end))
5241 break;
5242
5243 // Ensure current_pos is valid
5244 if (current_pos.lnum >= 1 && current_pos.lnum <= buf->b_ml.ml_line_count)
5245 {
5246 // Get the current line buffer
5247 *ptr = ml_get_buf(buf, current_pos.lnum, FALSE);
5248 // If ptr is end of line is reached, move to next line
5249 // or previous line based on direction
5250 if (**ptr != NUL)
5251 {
5252 if (!whole_line)
5253 {
5254 *ptr += current_pos.col;
5255 // Try to find a fuzzy match in the current line starting from current position
5256 found_new_match = fuzzy_match_str_in_line(ptr, pattern, len, &current_pos);
5257 if (found_new_match)
5258 {
glepnir7cfe6932024-09-15 20:06:28 +02005259 if (ctrl_x_mode_normal())
5260 {
5261 match_word = vim_strnsave(*ptr, *len);
5262 if (STRCMP(match_word, pattern) == 0)
5263 {
5264 next_word_end = find_word_start(*ptr + *len);
5265 if (*next_word_end != NUL && *next_word_end != NL)
5266 {
5267 // Find end of the word.
5268 if (has_mbyte)
5269 while (*next_word_end != NUL)
5270 {
5271 int l = (*mb_ptr2len)(next_word_end);
5272
5273 if (l < 2 && !vim_iswordc(*next_word_end))
5274 break;
5275 next_word_end += l;
5276 }
5277 else
5278 next_word_end = find_word_end(next_word_end);
5279 }
5280 else if (looped_around)
5281 found_new_match = FALSE;
5282
5283 *len = next_word_end - *ptr;
5284 current_pos.col = *len;
5285 }
5286 vim_free(match_word);
5287 }
glepnir8159fb12024-07-17 20:32:54 +02005288 *pos = current_pos;
5289 break;
5290 }
glepnir0be03e12024-07-19 16:45:05 +02005291 else if (looped_around && current_pos.lnum == circly_end.lnum)
5292 break;
glepnir8159fb12024-07-17 20:32:54 +02005293 }
5294 else
5295 {
5296 if (fuzzy_match_str(*ptr, pattern) > 0)
5297 {
5298 found_new_match = TRUE;
5299 *pos = current_pos;
Ken Takata073cb022024-07-28 17:08:15 +02005300 *len = (int)STRLEN(*ptr);
glepnir8159fb12024-07-17 20:32:54 +02005301 break;
5302 }
5303 }
5304 }
5305 }
5306
5307 // Move to the next line or previous line based on direction
5308 if (dir == FORWARD)
5309 {
5310 if (++current_pos.lnum > buf->b_ml.ml_line_count)
5311 {
5312 if (p_ws)
5313 {
5314 current_pos.lnum = 1;
5315 looped_around = TRUE;
5316 }
5317 else
5318 break;
5319 }
5320 }
5321 else
5322 {
5323 if (--current_pos.lnum < 1)
5324 {
5325 if (p_ws)
5326 {
5327 current_pos.lnum = buf->b_ml.ml_line_count;
5328 looped_around = TRUE;
5329 }
5330 else
5331 break;
5332
5333 }
5334 }
5335 current_pos.col = 0;
5336 } while (TRUE);
5337
5338 return found_new_match;
5339}
5340
5341/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005342 * Free an array of fuzzy string matches "fuzmatch[count]".
5343 */
5344 void
5345fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5346{
5347 int i;
5348
5349 if (fuzmatch == NULL)
5350 return;
5351 for (i = 0; i < count; ++i)
5352 vim_free(fuzmatch[i].str);
5353 vim_free(fuzmatch);
5354}
5355
5356/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005357 * Copy a list of fuzzy matches into a string list after sorting the matches by
5358 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5359 * Returns OK on success and FAIL on memory allocation failure.
5360 */
5361 int
5362fuzzymatches_to_strmatches(
5363 fuzmatch_str_T *fuzmatch,
5364 char_u ***matches,
5365 int count,
5366 int funcsort)
5367{
5368 int i;
5369
5370 if (count <= 0)
5371 return OK;
5372
5373 *matches = ALLOC_MULT(char_u *, count);
5374 if (*matches == NULL)
5375 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005376 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005377 return FAIL;
5378 }
5379
5380 // Sort the list by the descending order of the match score
5381 if (funcsort)
5382 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5383 else
5384 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5385
5386 for (i = 0; i < count; i++)
5387 (*matches)[i] = fuzmatch[i].str;
5388 vim_free(fuzmatch);
5389
5390 return OK;
5391}