blob: c8f8736768ca5294c91071231804173136a76f2f [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;
1551 // move reversed text to beginning of buffer
1552 while (*r != NUL && *r == ' ')
1553 r++;
John Marriott8c85a2a2024-05-20 19:18:26 +02001554 pat_len = msgbuf + msgbuflen - r;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001555 mch_memmove(msgbuf, r, pat_len);
1556 // overwrite old text
1557 if ((size_t)(r - msgbuf) >= pat_len)
1558 vim_memset(r, ' ', pat_len);
1559 else
1560 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1561 }
1562 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001563#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001564 msg_outtrans(msgbuf);
1565 msg_clr_eos();
1566 msg_check();
1567
1568 gotocmdline(FALSE);
1569 out_flush();
1570 msg_nowait = TRUE; // don't wait for this message
1571 }
John Marriott8c85a2a2024-05-20 19:18:26 +02001572
1573 if (!shortmess(SHM_SEARCHCOUNT))
1574 show_search_stats = TRUE;
1575 } // msgbuf != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
1577
1578 /*
1579 * If there is a character offset, subtract it from the current
1580 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001581 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 * This is not done for a line offset, because then we would not be vi
1583 * compatible.
1584 */
Bram Moolenaared203462004-06-16 11:19:22 +00001585 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
1587 if (spats[0].off.off > 0)
1588 {
1589 for (c = spats[0].off.off; c; --c)
1590 if (decl(&pos) == -1)
1591 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001592 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001594 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 pos.col = MAXCOL;
1596 }
1597 }
1598 else
1599 {
1600 for (c = spats[0].off.off; c; ++c)
1601 if (incl(&pos) == -1)
1602 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001603 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 {
1605 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1606 pos.col = 0;
1607 }
1608 }
1609 }
1610
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001611 /*
1612 * The actual search.
1613 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001614 c = searchit(curwin, curbuf, &pos, NULL,
1615 dirc == '/' ? FORWARD : BACKWARD,
John Marriott8c85a2a2024-05-20 19:18:26 +02001616 searchstr, searchstrlen, count, spats[0].off.end + (options &
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1618 + SEARCH_MSG + SEARCH_START
1619 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001620 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621
1622 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001623 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001624
1625 if (!shortmess(SHM_SEARCH)
1626 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1627 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001628 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001629
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (c == FAIL)
1631 {
1632 retval = 0;
1633 goto end_do_search;
1634 }
1635 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001636 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001638 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
1640 /*
1641 * Add character and/or line offset
1642 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001643 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001645 pos_T org_pos = pos;
1646
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001647 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 {
1649 c = pos.lnum + spats[0].off.off;
1650 if (c < 1)
1651 pos.lnum = 1;
1652 else if (c > curbuf->b_ml.ml_line_count)
1653 pos.lnum = curbuf->b_ml.ml_line_count;
1654 else
1655 pos.lnum = c;
1656 pos.col = 0;
1657
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001658 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001660 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001662 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001663 c = spats[0].off.off;
1664 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001666 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 if (incl(&pos) == -1)
1668 break;
1669 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001670 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 else
1672 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001673 while (c++ < 0)
1674 if (decl(&pos) == -1)
1675 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 }
1677 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001678 if (!EQUAL_POS(pos, org_pos))
1679 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001682 // Show [1/15] if 'S' is not in 'shortmess'.
John Marriott8c85a2a2024-05-20 19:18:26 +02001683 if (show_search_stats)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001684 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
John Marriott8c85a2a2024-05-20 19:18:26 +02001685 show_top_bot_msg, msgbuf, msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001686 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001687#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001688 || (!(fdo_flags & FDO_SEARCH)
1689 && hasFolding(curwin->w_cursor.lnum,
1690 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001691#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001692 ),
1693 SEARCH_STAT_DEF_MAX_COUNT,
1694 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 /*
1697 * The search command can be followed by a ';' to do another search.
1698 * For example: "/pat/;/foo/+3;?bar"
1699 * This is like doing another search command, except:
1700 * - The remembered direction '/' or '?' is from the first search.
1701 * - When an error happens the cursor isn't moved at all.
1702 * Don't do this when called by get_address() (it handles ';' itself).
1703 */
1704 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1705 break;
1706
1707 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001708 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if (dirc != '?' && dirc != '/')
1710 {
1711 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001712 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 goto end_do_search;
1714 }
1715 ++pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02001716 --patlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
1718
1719 if (options & SEARCH_MARK)
1720 setpcmark();
1721 curwin->w_cursor = pos;
1722 curwin->w_set_curswant = TRUE;
1723
1724end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001725 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 spats[0].off = old_off;
1727 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001728 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729
1730 return retval;
1731}
1732
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733/*
1734 * search_for_exact_line(buf, pos, dir, pat)
1735 *
1736 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001737 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001739 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 * Return OK for success, or FAIL if no line found.
1741 */
1742 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001743search_for_exact_line(
1744 buf_T *buf,
1745 pos_T *pos,
1746 int dir,
1747 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
1749 linenr_T start = 0;
1750 char_u *ptr;
1751 char_u *p;
1752
1753 if (buf->b_ml.ml_line_count == 0)
1754 return FAIL;
1755 for (;;)
1756 {
1757 pos->lnum += dir;
1758 if (pos->lnum < 1)
1759 {
1760 if (p_ws)
1761 {
1762 pos->lnum = buf->b_ml.ml_line_count;
1763 if (!shortmess(SHM_SEARCH))
1764 give_warning((char_u *)_(top_bot_msg), TRUE);
1765 }
1766 else
1767 {
1768 pos->lnum = 1;
1769 break;
1770 }
1771 }
1772 else if (pos->lnum > buf->b_ml.ml_line_count)
1773 {
1774 if (p_ws)
1775 {
1776 pos->lnum = 1;
1777 if (!shortmess(SHM_SEARCH))
1778 give_warning((char_u *)_(bot_top_msg), TRUE);
1779 }
1780 else
1781 {
1782 pos->lnum = 1;
1783 break;
1784 }
1785 }
1786 if (pos->lnum == start)
1787 break;
1788 if (start == 0)
1789 start = pos->lnum;
1790 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1791 p = skipwhite(ptr);
1792 pos->col = (colnr_T) (p - ptr);
1793
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001794 // when adding lines the matching line may be empty but it is not
1795 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001796 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
1798 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1799 return OK;
1800 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001801 else if (*p != NUL) // ignore empty lines
1802 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001803 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1804 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 return OK;
1806 }
1807 }
1808 return FAIL;
1809}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
1811/*
1812 * Character Searches
1813 */
1814
1815/*
1816 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1817 * position of the character, otherwise move to just before the char.
1818 * Do this "cap->count1" times.
1819 * Return FAIL or OK.
1820 */
1821 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001822searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001824 int c = cap->nchar; // char to search for
1825 int dir = cap->arg; // TRUE for searching forward
1826 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 int col;
1828 char_u *p;
1829 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001830 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001832 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001834 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001836 *lastc = c;
1837 set_csearch_direction(dir);
1838 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001839 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 if (cap->ncharC1 != 0)
1841 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001842 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1843 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001845 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1846 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 }
1849 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001850 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
zeertzjqe5d91ba2023-05-14 17:39:18 +01001852 if (*lastc == NUL && lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001854 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 dir = -lastcdir;
1856 else
1857 dir = lastcdir;
1858 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001859 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001860 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001861
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001862 // Force a move of at least one char, so ";" and "," will move the
1863 // cursor, even if the cursor is right in front of char we are looking
1864 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001865 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001866 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 }
1868
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001869 if (dir == BACKWARD)
1870 cap->oap->inclusive = FALSE;
1871 else
1872 cap->oap->inclusive = TRUE;
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 p = ml_get_curline();
1875 col = curwin->w_cursor.col;
zeertzjq94b7c322024-03-12 21:50:32 +01001876 len = ml_get_curline_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 while (count--)
1879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (has_mbyte)
1881 {
1882 for (;;)
1883 {
1884 if (dir > 0)
1885 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001886 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (col >= len)
1888 return FAIL;
1889 }
1890 else
1891 {
1892 if (col == 0)
1893 return FAIL;
1894 col -= (*mb_head_off)(p, p + col - 1) + 1;
1895 }
zeertzjqe5d91ba2023-05-14 17:39:18 +01001896 if (lastc_bytelen <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001898 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 break;
1900 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001901 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001902 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001903 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001904 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 }
1907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 for (;;)
1910 {
1911 if ((col += dir) < 0 || col >= len)
1912 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001913 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001915 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 }
1918 }
1919
1920 if (t_cmd)
1921 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001922 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (has_mbyte)
1925 {
1926 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001927 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001928 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001930 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 col -= (*mb_head_off)(p, p + col);
1932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 curwin->w_cursor.col = col;
1935
1936 return OK;
1937}
1938
1939/*
1940 * "Other" Searches
1941 */
1942
1943/*
1944 * findmatch - find the matching paren or brace
1945 *
1946 * Improvement over vi: Braces inside quotes are ignored.
1947 */
1948 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001949findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
1951 return findmatchlimit(oap, initc, 0, 0);
1952}
1953
1954/*
1955 * Return TRUE if the character before "linep[col]" equals "ch".
1956 * Return FALSE if "col" is zero.
1957 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1958 * is NULL.
1959 * Handles multibyte string correctly.
1960 */
1961 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001962check_prevcol(
1963 char_u *linep,
1964 int col,
1965 int ch,
1966 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967{
1968 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (col > 0 && has_mbyte)
1970 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 if (prevcol)
1972 *prevcol = col;
1973 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1974}
1975
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001976/*
1977 * Raw string start is found at linep[startpos.col - 1].
1978 * Return TRUE if the matching end can be found between startpos and endpos.
1979 */
1980 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001981find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001982{
1983 char_u *p;
1984 char_u *delim_copy;
1985 size_t delim_len;
1986 linenr_T lnum;
1987 int found = FALSE;
1988
1989 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1990 ;
1991 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001992 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001993 if (delim_copy == NULL)
1994 return FALSE;
1995 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1996 {
1997 char_u *line = ml_get(lnum);
1998
1999 for (p = line + (lnum == startpos->lnum
2000 ? startpos->col + 1 : 0); *p; ++p)
2001 {
2002 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
2003 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02002004 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
2005 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002006 {
2007 found = TRUE;
2008 break;
2009 }
2010 }
2011 if (found)
2012 break;
2013 }
2014 vim_free(delim_copy);
2015 return found;
2016}
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01002019 * Check matchpairs option for "*initc".
2020 * If there is a match set "*initc" to the matching character and "*findc" to
2021 * the opposite character. Set "*backwards" to the direction.
2022 * When "switchit" is TRUE swap the direction.
2023 */
2024 static void
2025find_mps_values(
2026 int *initc,
2027 int *findc,
2028 int *backwards,
2029 int switchit)
2030{
2031 char_u *ptr;
2032
2033 ptr = curbuf->b_p_mps;
2034 while (*ptr != NUL)
2035 {
2036 if (has_mbyte)
2037 {
2038 char_u *prev;
2039
2040 if (mb_ptr2char(ptr) == *initc)
2041 {
2042 if (switchit)
2043 {
2044 *findc = *initc;
2045 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2046 *backwards = TRUE;
2047 }
2048 else
2049 {
2050 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2051 *backwards = FALSE;
2052 }
2053 return;
2054 }
2055 prev = ptr;
2056 ptr += mb_ptr2len(ptr) + 1;
2057 if (mb_ptr2char(ptr) == *initc)
2058 {
2059 if (switchit)
2060 {
2061 *findc = *initc;
2062 *initc = mb_ptr2char(prev);
2063 *backwards = FALSE;
2064 }
2065 else
2066 {
2067 *findc = mb_ptr2char(prev);
2068 *backwards = TRUE;
2069 }
2070 return;
2071 }
2072 ptr += mb_ptr2len(ptr);
2073 }
2074 else
2075 {
2076 if (*ptr == *initc)
2077 {
2078 if (switchit)
2079 {
2080 *backwards = TRUE;
2081 *findc = *initc;
2082 *initc = ptr[2];
2083 }
2084 else
2085 {
2086 *backwards = FALSE;
2087 *findc = ptr[2];
2088 }
2089 return;
2090 }
2091 ptr += 2;
2092 if (*ptr == *initc)
2093 {
2094 if (switchit)
2095 {
2096 *backwards = FALSE;
2097 *findc = *initc;
2098 *initc = ptr[-2];
2099 }
2100 else
2101 {
2102 *backwards = TRUE;
2103 *findc = ptr[-2];
2104 }
2105 return;
2106 }
2107 ++ptr;
2108 }
2109 if (*ptr == ',')
2110 ++ptr;
2111 }
2112}
2113
2114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002116 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2117 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 *
2119 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002120 * character at or after the cursor. Special values:
2121 * '*' look for C-style comment / *
2122 * '/' look for C-style comment / *, ignoring comment-end
2123 * '#' look for preprocessor directives
2124 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 *
2126 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2127 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2128 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2129 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002130 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002131 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002132 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002135findmatchlimit(
2136 oparg_T *oap,
2137 int initc,
2138 int flags,
2139 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002141 static pos_T pos; // current search position
2142 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002144 int count = 0; // cumulative number of braces
2145 int backwards = FALSE; // init for gcc
2146 int raw_string = FALSE; // search for raw string
2147 int inquote = FALSE; // TRUE when inside quotes
2148 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002150 int do_quotes; // check for quotes in current line
2151 int at_start; // do_quotes value at start position
2152 int hash_dir = 0; // Direction searched for # things
2153 int comment_dir = 0; // Direction searched for comments
2154 pos_T match_pos; // Where last slash-star was found
2155 int start_in_quotes; // start position is in quotes
2156 int traveled = 0; // how far we've searched so far
2157 int ignore_cend = FALSE; // ignore comment end
2158 int cpo_match; // vi compatible matching
2159 int cpo_bsl; // don't recognize backslashes
2160 int match_escaped = 0; // search for escaped match
2161 int dir; // Direction to search
2162 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002163 int lispcomm = FALSE; // inside of Lisp-style comment
2164 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
2166 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002167 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 linep = ml_get(pos.lnum);
2169
2170 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2171 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2172
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002173 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 if (flags & FM_BACKWARD)
2175 dir = BACKWARD;
2176 else if (flags & FM_FORWARD)
2177 dir = FORWARD;
2178 else
2179 dir = 0;
2180
2181 /*
2182 * if initc given, look in the table for the matching character
2183 * '/' and '*' are special cases: look for start or end of comment.
2184 * When '/' is used, we ignore running backwards into an star-slash, for
2185 * "[*" command, we just want to find any comment.
2186 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002187 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
2189 comment_dir = dir;
2190 if (initc == '/')
2191 ignore_cend = TRUE;
2192 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002193 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 initc = NUL;
2195 }
2196 else if (initc != '#' && initc != NUL)
2197 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002198 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002199 if (dir)
2200 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002201 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 return NULL;
2203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 else
2205 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002206 /*
2207 * Either initc is '#', or no initc was given and we need to look
2208 * under the cursor.
2209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (initc == '#')
2211 {
2212 hash_dir = dir;
2213 }
2214 else
2215 {
2216 /*
2217 * initc was not given, must look for something to match under
2218 * or near the cursor.
2219 * Only check for special things when 'cpo' doesn't have '%'.
2220 */
2221 if (!cpo_match)
2222 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002223 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 ptr = skipwhite(linep);
2225 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2226 {
2227 ptr = skipwhite(ptr + 1);
2228 if ( STRNCMP(ptr, "if", 2) == 0
2229 || STRNCMP(ptr, "endif", 5) == 0
2230 || STRNCMP(ptr, "el", 2) == 0)
2231 hash_dir = 1;
2232 }
2233
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002234 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 else if (linep[pos.col] == '/')
2236 {
2237 if (linep[pos.col + 1] == '*')
2238 {
2239 comment_dir = FORWARD;
2240 backwards = FALSE;
2241 pos.col++;
2242 }
2243 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2244 {
2245 comment_dir = BACKWARD;
2246 backwards = TRUE;
2247 pos.col--;
2248 }
2249 }
2250 else if (linep[pos.col] == '*')
2251 {
2252 if (linep[pos.col + 1] == '/')
2253 {
2254 comment_dir = BACKWARD;
2255 backwards = TRUE;
2256 }
2257 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2258 {
2259 comment_dir = FORWARD;
2260 backwards = FALSE;
2261 }
2262 }
2263 }
2264
2265 /*
2266 * If we are not on a comment or the # at the start of a line, then
2267 * look for brace anywhere on this line after the cursor.
2268 */
2269 if (!hash_dir && !comment_dir)
2270 {
2271 /*
2272 * Find the brace under or after the cursor.
2273 * If beyond the end of the line, use the last character in
2274 * the line.
2275 */
2276 if (linep[pos.col] == NUL && pos.col)
2277 --pos.col;
2278 for (;;)
2279 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002280 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (initc == NUL)
2282 break;
2283
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002284 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (findc)
2286 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002287 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 if (!findc)
2290 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002291 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (!cpo_match && *skipwhite(linep) == '#')
2293 hash_dir = 1;
2294 else
2295 return NULL;
2296 }
2297 else if (!cpo_bsl)
2298 {
2299 int col, bslcnt = 0;
2300
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002301 // Set "match_escaped" if there are an odd number of
2302 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2304 bslcnt++;
2305 match_escaped = (bslcnt & 1);
2306 }
2307 }
2308 }
2309 if (hash_dir)
2310 {
2311 /*
2312 * Look for matching #if, #else, #elif, or #endif
2313 */
2314 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002315 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (initc != '#')
2317 {
2318 ptr = skipwhite(skipwhite(linep) + 1);
2319 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2320 hash_dir = 1;
2321 else if (STRNCMP(ptr, "endif", 5) == 0)
2322 hash_dir = -1;
2323 else
2324 return NULL;
2325 }
2326 pos.col = 0;
2327 while (!got_int)
2328 {
2329 if (hash_dir > 0)
2330 {
2331 if (pos.lnum == curbuf->b_ml.ml_line_count)
2332 break;
2333 }
2334 else if (pos.lnum == 1)
2335 break;
2336 pos.lnum += hash_dir;
2337 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002338 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 ptr = skipwhite(linep);
2340 if (*ptr != '#')
2341 continue;
2342 pos.col = (colnr_T) (ptr - linep);
2343 ptr = skipwhite(ptr + 1);
2344 if (hash_dir > 0)
2345 {
2346 if (STRNCMP(ptr, "if", 2) == 0)
2347 count++;
2348 else if (STRNCMP(ptr, "el", 2) == 0)
2349 {
2350 if (count == 0)
2351 return &pos;
2352 }
2353 else if (STRNCMP(ptr, "endif", 5) == 0)
2354 {
2355 if (count == 0)
2356 return &pos;
2357 count--;
2358 }
2359 }
2360 else
2361 {
2362 if (STRNCMP(ptr, "if", 2) == 0)
2363 {
2364 if (count == 0)
2365 return &pos;
2366 count--;
2367 }
2368 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2369 {
2370 if (count == 0)
2371 return &pos;
2372 }
2373 else if (STRNCMP(ptr, "endif", 5) == 0)
2374 count++;
2375 }
2376 }
2377 return NULL;
2378 }
2379 }
2380
2381#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002382 // This is just guessing: when 'rightleft' is set, search for a matching
2383 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2385 backwards = !backwards;
2386#endif
2387
2388 do_quotes = -1;
2389 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002390 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002391
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002392 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002393 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002395 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002396 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002397
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 while (!got_int)
2399 {
2400 /*
2401 * Go to the next position, forward or backward. We could use
2402 * inc() and dec() here, but that is much slower
2403 */
2404 if (backwards)
2405 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002406 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002407 if (lispcomm && pos.col < (colnr_T)comment_col)
2408 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002409 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002411 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 break;
2413 --pos.lnum;
2414
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002415 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 break;
2417
2418 linep = ml_get(pos.lnum);
zeertzjq94b7c322024-03-12 21:50:32 +01002419 pos.col = ml_get_len(pos.lnum); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 do_quotes = -1;
2421 line_breakcheck();
2422
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002423 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002424 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002426 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002427 if (lisp && comment_col != MAXCOL)
2428 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430 else
2431 {
2432 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (has_mbyte)
2434 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
2436 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002437 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002439 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002440 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002441 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002442 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002443 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002445 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002446 // line is exhausted and comment with it,
2447 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002448 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 break;
2450 ++pos.lnum;
2451
2452 if (maxtravel && traveled++ > maxtravel)
2453 break;
2454
2455 linep = ml_get(pos.lnum);
2456 pos.col = 0;
2457 do_quotes = -1;
2458 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002459 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002460 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 else
2463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002465 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 ++pos.col;
2468 }
2469 }
2470
2471 /*
2472 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2473 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002474 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2475 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002477 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002479 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481
2482 if (comment_dir)
2483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002484 // Note: comments do not nest, and we ignore quotes in them
2485 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (comment_dir == FORWARD)
2487 {
2488 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2489 {
2490 pos.col++;
2491 return &pos;
2492 }
2493 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002494 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 /*
2497 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002498 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 */
2500 if (pos.col == 0)
2501 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002502 else if (raw_string)
2503 {
2504 if (linep[pos.col - 1] == 'R'
2505 && linep[pos.col] == '"'
2506 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2507 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002508 // Possible start of raw string. Now that we have the
2509 // delimiter we can check if it ends before where we
2510 // started searching, or before the previously found
2511 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002512 if (!find_rawstring_end(linep, &pos,
2513 count > 0 ? &match_pos : &curwin->w_cursor))
2514 {
2515 count++;
2516 match_pos = pos;
2517 match_pos.col--;
2518 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002519 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002520 }
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 else if ( linep[pos.col - 1] == '/'
2523 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002524 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 && (int)pos.col < comment_col)
2526 {
2527 count++;
2528 match_pos = pos;
2529 match_pos.col--;
2530 }
2531 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2532 {
2533 if (count > 0)
2534 pos = match_pos;
2535 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2536 && (int)pos.col <= comment_col)
2537 pos.col -= 2;
2538 else if (ignore_cend)
2539 continue;
2540 else
2541 return NULL;
2542 return &pos;
2543 }
2544 }
2545 continue;
2546 }
2547
2548 /*
2549 * If smart matching ('cpoptions' does not contain '%'), braces inside
2550 * of quotes are ignored, but only if there is an even number of
2551 * quotes in the line.
2552 */
2553 if (cpo_match)
2554 do_quotes = 0;
2555 else if (do_quotes == -1)
2556 {
2557 /*
2558 * Count the number of quotes in the line, skipping \" and '"'.
2559 * Watch out for "\\".
2560 */
2561 at_start = do_quotes;
2562 for (ptr = linep; *ptr; ++ptr)
2563 {
2564 if (ptr == linep + pos.col + backwards)
2565 at_start = (do_quotes & 1);
2566 if (*ptr == '"'
2567 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2568 ++do_quotes;
2569 if (*ptr == '\\' && ptr[1] != NUL)
2570 ++ptr;
2571 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002572 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
2574 /*
2575 * If we find an uneven count, check current line and previous
2576 * one for a '\' at the end.
2577 */
2578 if (!do_quotes)
2579 {
2580 inquote = FALSE;
2581 if (ptr[-1] == '\\')
2582 {
2583 do_quotes = 1;
2584 if (start_in_quotes == MAYBE)
2585 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002586 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 inquote = TRUE;
2588 start_in_quotes = TRUE;
2589 }
2590 else if (backwards)
2591 inquote = TRUE;
2592 }
2593 if (pos.lnum > 1)
2594 {
2595 ptr = ml_get(pos.lnum - 1);
zeertzjq94b7c322024-03-12 21:50:32 +01002596 if (*ptr && *(ptr + ml_get_len(pos.lnum - 1) - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 {
2598 do_quotes = 1;
2599 if (start_in_quotes == MAYBE)
2600 {
2601 inquote = at_start;
2602 if (inquote)
2603 start_in_quotes = TRUE;
2604 }
2605 else if (!backwards)
2606 inquote = TRUE;
2607 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002608
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002609 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002610 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 }
2612 }
2613 }
2614 if (start_in_quotes == MAYBE)
2615 start_in_quotes = FALSE;
2616
2617 /*
2618 * If 'smartmatch' is set:
2619 * Things inside quotes are ignored by setting 'inquote'. If we
2620 * find a quote without a preceding '\' invert 'inquote'. At the
2621 * end of a line not ending in '\' we reset 'inquote'.
2622 *
2623 * In lines with an uneven number of quotes (without preceding '\')
2624 * we do not know which part to ignore. Therefore we only set
2625 * inquote if the number of quotes in a line is even, unless this
2626 * line or the previous one ends in a '\'. Complicated, isn't it?
2627 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002628 c = PTR2CHAR(linep + pos.col);
2629 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002632 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2634 {
2635 inquote = FALSE;
2636 start_in_quotes = FALSE;
2637 }
2638 break;
2639
2640 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002641 // a quote that is preceded with an odd number of backslashes is
2642 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (do_quotes)
2644 {
2645 int col;
2646
2647 for (col = pos.col - 1; col >= 0; --col)
2648 if (linep[col] != '\\')
2649 break;
2650 if ((((int)pos.col - 1 - col) & 1) == 0)
2651 {
2652 inquote = !inquote;
2653 start_in_quotes = FALSE;
2654 }
2655 }
2656 break;
2657
2658 /*
2659 * If smart matching ('cpoptions' does not contain '%'):
2660 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2661 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2662 * skipped, there is never a brace in them.
2663 * Ignore this when finding matches for `'.
2664 */
2665 case '\'':
2666 if (!cpo_match && initc != '\'' && findc != '\'')
2667 {
2668 if (backwards)
2669 {
2670 if (pos.col > 1)
2671 {
2672 if (linep[pos.col - 2] == '\'')
2673 {
2674 pos.col -= 2;
2675 break;
2676 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002677 else if (linep[pos.col - 2] == '\\'
2678 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
2680 pos.col -= 3;
2681 break;
2682 }
2683 }
2684 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002685 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002687 if (linep[pos.col + 1] == '\\'
2688 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 {
2690 pos.col += 3;
2691 break;
2692 }
2693 else if (linep[pos.col + 2] == '\'')
2694 {
2695 pos.col += 2;
2696 break;
2697 }
2698 }
2699 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002700 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002703 /*
2704 * For Lisp skip over backslashed (), {} and [].
2705 * (actually, we skip #\( et al)
2706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002708 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002709 && pos.col > 1
2710 && check_prevcol(linep, pos.col, '\\', NULL)
2711 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002714 // Check for match outside of quotes, and inside of
2715 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 if ((!inquote || start_in_quotes == TRUE)
2717 && (c == initc || c == findc))
2718 {
2719 int col, bslcnt = 0;
2720
2721 if (!cpo_bsl)
2722 {
2723 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2724 bslcnt++;
2725 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002726 // Only accept a match when 'M' is in 'cpo' or when escaping
2727 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2729 {
2730 if (c == initc)
2731 count++;
2732 else
2733 {
2734 if (count == 0)
2735 return &pos;
2736 count--;
2737 }
2738 }
2739 }
2740 }
2741 }
2742
2743 if (comment_dir == BACKWARD && count > 0)
2744 {
2745 pos = match_pos;
2746 return &pos;
2747 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002748 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749}
2750
2751/*
2752 * Check if line[] contains a / / comment.
2753 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002755 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002756check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 char_u *p;
2759
2760 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002761 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002762 if (curbuf->b_p_lisp)
2763 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002764 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002765 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002766 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002767
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002768 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002769 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002770 {
2771 if (*p == '"')
2772 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002773 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002774 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002775 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002776 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002777 }
2778 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002779 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002780 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002781 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002782 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002783 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002784 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2785 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002786 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002787 ++p;
2788 }
2789 }
2790 else
2791 p = NULL;
2792 }
2793 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002794 while ((p = vim_strchr(p, '/')) != NULL)
2795 {
2796 // Accept a double /, unless it's preceded with * and followed by
2797 // *, because * / / * is an end and start of a C comment. Only
2798 // accept the position if it is not inside a string.
2799 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002800 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002801 break;
2802 ++p;
2803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
2805 if (p == NULL)
2806 return MAXCOL;
2807 return (int)(p - line);
2808}
2809
2810/*
2811 * Move cursor briefly to character matching the one under the cursor.
2812 * Used for Insert mode and "r" command.
2813 * Show the match only if it is visible on the screen.
2814 * If there isn't a match, then beep.
2815 */
2816 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002817showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002818 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 pos_T *lpos, save_cursor;
2821 pos_T mpos;
2822 colnr_T vcol;
2823 long save_so;
2824 long save_siso;
2825#ifdef CURSOR_SHAPE
2826 int save_state;
2827#endif
2828 colnr_T save_dollar_vcol;
2829 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002830 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2831 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
2833 /*
2834 * Only show match for chars in the 'matchpairs' option.
2835 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002836 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002837 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
2839#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002840 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002841 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002842#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002843 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002844 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef FEAT_RIGHTLEFT
2846 && !(curwin->w_p_rl ^ p_ri)
2847#endif
2848 )
2849 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002850 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002851 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 return;
2853 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002854 if (*p == NUL)
2855 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002857 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002859 vim_beep(BO_MATCH);
2860 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002862
2863 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2864 return;
2865
2866 if (!curwin->w_p_wrap)
2867 getvcol(curwin, lpos, NULL, &vcol, NULL);
2868
2869 int col_visible = (curwin->w_p_wrap
2870 || (vcol >= curwin->w_leftcol
2871 && vcol < curwin->w_leftcol + curwin->w_width));
2872 if (!col_visible)
2873 return;
2874
2875 mpos = *lpos; // save the pos, update_screen() may change it
2876 save_cursor = curwin->w_cursor;
2877 save_so = *so;
2878 save_siso = *siso;
2879 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2880 // stop displaying the "$".
2881 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2882 dollar_vcol = -1;
2883 ++curwin->w_virtcol; // do display ')' just before "$"
2884 update_screen(UPD_VALID); // show the new char first
2885
2886 save_dollar_vcol = dollar_vcol;
2887#ifdef CURSOR_SHAPE
2888 save_state = State;
2889 State = MODE_SHOWMATCH;
2890 ui_cursor_shape(); // may show different cursor shape
2891#endif
2892 curwin->w_cursor = mpos; // move to matching char
2893 *so = 0; // don't use 'scrolloff' here
2894 *siso = 0; // don't use 'sidescrolloff' here
2895 showruler(FALSE);
2896 setcursor();
2897 cursor_on(); // make sure that the cursor is shown
2898 out_flush_cursor(TRUE, FALSE);
2899
2900 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2901 // which resets it if the matching position is in a previous line
2902 // and has a higher column number.
2903 dollar_vcol = save_dollar_vcol;
2904
2905 /*
2906 * brief pause, unless 'm' is present in 'cpo' and a character is
2907 * available.
2908 */
2909 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2910 ui_delay(p_mat * 100L + 8, TRUE);
2911 else if (!char_avail())
2912 ui_delay(p_mat * 100L + 9, FALSE);
2913 curwin->w_cursor = save_cursor; // restore cursor position
2914 *so = save_so;
2915 *siso = save_siso;
2916#ifdef CURSOR_SHAPE
2917 State = save_state;
2918 ui_cursor_shape(); // may show different cursor shape
2919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
2922/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002923 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002924 * If move is TRUE, check from the beginning of the buffer, else from position
2925 * "cur".
2926 * "direction" is FORWARD or BACKWARD.
2927 * Returns TRUE, FALSE or -1 for failure.
2928 */
2929 static int
John Marriott8c85a2a2024-05-20 19:18:26 +02002930is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002931{
2932 regmmatch_T regmatch;
2933 int nmatched = 0;
2934 int result = -1;
2935 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002936 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002937 int flag = 0;
2938
2939 if (pattern == NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +02002940 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002941 pattern = spats[last_idx].pat;
John Marriott8c85a2a2024-05-20 19:18:26 +02002942 patternlen = spats[last_idx].patlen;
2943 }
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002944
John Marriott8c85a2a2024-05-20 19:18:26 +02002945 if (search_regcomp(pattern, patternlen, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002946 SEARCH_KEEP, &regmatch) == FAIL)
2947 return -1;
2948
2949 // init startcol correctly
2950 regmatch.startpos[0].col = -1;
2951 // move to match
2952 if (move)
2953 {
2954 CLEAR_POS(&pos);
2955 }
2956 else
2957 {
2958 pos = *cur;
2959 // accept a match at the cursor position
2960 flag = SEARCH_START;
2961 }
2962
John Marriott8c85a2a2024-05-20 19:18:26 +02002963 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, patternlen, 1,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002964 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2965 {
2966 // Zero-width pattern should match somewhere, then we can check if
2967 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002968 do
2969 {
2970 regmatch.startpos[0].col++;
2971 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002972 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002973 if (nmatched != 0)
2974 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002975 } while (regmatch.regprog != NULL
2976 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002977 : regmatch.startpos[0].col > pos.col);
2978
Bram Moolenaar53989552019-12-23 22:59:18 +01002979 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002980 {
2981 result = (nmatched != 0
2982 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2983 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2984 }
2985 }
2986
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002987 vim_regfree(regmatch.regprog);
2988 return result;
2989}
2990
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002991
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002992/*
2993 * Find next search match under cursor, cursor at end.
2994 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002995 */
2996 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002997current_search(
2998 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002999 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003000{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003001 pos_T start_pos; // start position of the pattern match
3002 pos_T end_pos; // end position of the pattern match
3003 pos_T orig_pos; // position of the cursor at beginning
3004 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003005 int i;
3006 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003007 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003008 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003009 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02003010 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003011 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003012 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003013
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003014 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003015 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003016 dec_cursor();
3017
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003018 // When searching forward and the cursor is at the start of the Visual
3019 // area, skip the first search backward, otherwise it doesn't move.
3020 skip_first_backward = forward && VIsual_active
3021 && LT_POS(curwin->w_cursor, VIsual);
3022
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003023 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003024 if (VIsual_active)
3025 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003026 if (forward)
3027 incl(&pos);
3028 else
3029 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003030 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003031
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003032 // Is the pattern is zero-width?, this time, don't care about the direction
John Marriott8c85a2a2024-05-20 19:18:26 +02003033 zero_width = is_zero_width(spats[last_idx].pat, spats[last_idx].patlen,
3034 TRUE, &curwin->w_cursor, FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003035 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003036 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003037
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003038 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003039 * The trick is to first search backwards and then search forward again,
3040 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003041 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003042 */
3043 for (i = 0; i < 2; i++)
3044 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003045 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003046 {
3047 if (i == 0 && skip_first_backward)
3048 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003049 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003050 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003051 else
3052 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003053
3054 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003055 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003056 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003057 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003058
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003059 // wrapping should not occur in the first round
3060 if (i == 0)
3061 p_ws = FALSE;
3062
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003063 result = searchit(curwin, curbuf, &pos, &end_pos,
3064 (dir ? FORWARD : BACKWARD),
John Marriott8c85a2a2024-05-20 19:18:26 +02003065 spats[last_idx].pat, spats[last_idx].patlen, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003066 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003067
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003068 p_ws = old_p_ws;
3069
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003070 // First search may fail, but then start searching from the
3071 // beginning of the file (cursor might be on the search match)
3072 // except when Visual mode is active, so that extending the visual
3073 // selection works.
3074 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003075 {
3076 curwin->w_cursor = orig_pos;
3077 if (VIsual_active)
3078 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003079 return FAIL;
3080 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003081 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003082 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003083 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003084 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003085 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003086 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003087 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003088 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003089 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003090 // try again from end of buffer
3091 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003092 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
zeertzjq94b7c322024-03-12 21:50:32 +01003093 pos.col = ml_get_len(curwin->w_buffer->b_ml.ml_line_count);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003094 }
3095 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003096 }
3097
3098 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003099
3100 if (!VIsual_active)
3101 VIsual = start_pos;
3102
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003103 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003104 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003105 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003106 {
3107 if (skip_first_backward)
3108 // put the cursor on the start of the match
3109 curwin->w_cursor = pos;
3110 else
3111 // put the cursor on last character of match
3112 dec_cursor();
3113 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003114 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003115 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003116 VIsual_active = TRUE;
3117 VIsual_mode = 'v';
3118
Bram Moolenaarb7633612019-02-10 21:48:25 +01003119 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003120 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003121 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003122 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3123 inc_cursor();
3124 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3125 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003126 }
3127
3128#ifdef FEAT_FOLDING
3129 if (fdo_flags & FDO_SEARCH && KeyTyped)
3130 foldOpenCursor();
3131#endif
3132
3133 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003134 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003135#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003136 // Make sure the clipboard gets updated. Needed because start and
3137 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003138 clip_star.vmode = NUL;
3139#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003140 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003141 showmode();
3142
3143 return OK;
3144}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146/*
3147 * return TRUE if line 'lnum' is empty or has white chars only.
3148 */
3149 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003150linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 char_u *p;
3153
3154 p = skipwhite(ml_get(lnum));
3155 return (*p == NUL);
3156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003158/*
3159 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003160 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003161 */
3162 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003163cmdline_search_stat(
3164 int dirc,
3165 pos_T *pos,
3166 pos_T *cursor_pos,
3167 int show_top_bot_msg,
3168 char_u *msgbuf,
John Marriott8c85a2a2024-05-20 19:18:26 +02003169 size_t msgbuflen,
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003170 int recompute,
3171 int maxcount,
3172 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003173{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003174 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003175
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003176 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3177 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003178 if (stat.cur <= 0)
3179 return;
3180
3181 char t[SEARCH_STAT_BUF_LEN];
3182 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003183
3184#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003185 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3186 {
3187 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003188 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003189 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003190 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003191 maxcount, maxcount);
3192 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003193 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003194 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003195 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003196 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003197 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003198 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003199 else
3200#endif
3201 {
3202 if (stat.incomplete == 1)
John Marriott8c85a2a2024-05-20 19:18:26 +02003203 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003204 else if (stat.cnt > maxcount && stat.cur > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003205 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003206 maxcount, maxcount);
3207 else if (stat.cnt > maxcount)
John Marriott8c85a2a2024-05-20 19:18:26 +02003208 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003209 stat.cur, maxcount);
3210 else
John Marriott8c85a2a2024-05-20 19:18:26 +02003211 len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003212 stat.cur, stat.cnt);
3213 }
3214
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003215 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3216 {
3217 mch_memmove(t + 2, t, len);
3218 t[0] = 'W';
3219 t[1] = ' ';
3220 len += 2;
3221 }
3222
John Marriott8c85a2a2024-05-20 19:18:26 +02003223 if (len > msgbuflen)
3224 len = msgbuflen;
3225 mch_memmove(msgbuf + msgbuflen - len, t, len);
zeertzjqa7d36b72023-01-31 21:13:38 +00003226
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003227 if (dirc == '?' && stat.cur == maxcount + 1)
3228 stat.cur = -1;
3229
3230 // keep the message even after redraw, but don't put in history
3231 msg_hist_off = TRUE;
3232 give_warning(msgbuf, FALSE);
3233 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003234}
3235
3236/*
3237 * Add the search count information to "stat".
3238 * "stat" must not be NULL.
3239 * When "recompute" is TRUE always recompute the numbers.
3240 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3241 * dirc == '/': find the next match
3242 * dirc == '?': find the previous match
3243 */
3244 static void
3245update_search_stat(
3246 int dirc,
3247 pos_T *pos,
3248 pos_T *cursor_pos,
3249 searchstat_T *stat,
3250 int recompute,
3251 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003252 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003253{
3254 int save_ws = p_ws;
3255 int wraparound = FALSE;
3256 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003257 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003258 static int cur = 0;
3259 static int cnt = 0;
3260 static int exact_match = FALSE;
3261 static int incomplete = 0;
3262 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3263 static int chgtick = 0;
3264 static char_u *lastpat = NULL;
3265 static buf_T *lbuf = NULL;
3266#ifdef FEAT_RELTIME
3267 proftime_T start;
3268#endif
3269
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003270 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003271
3272 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3273 {
3274 stat->cur = cur;
3275 stat->cnt = cnt;
3276 stat->exact_match = exact_match;
3277 stat->incomplete = incomplete;
3278 stat->last_maxcount = last_maxcount;
3279 return;
3280 }
3281 last_maxcount = maxcount;
3282
3283 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3284 || (dirc == '/' && LT_POS(p, lastpos)));
3285
3286 // If anything relevant changed the count has to be recomputed.
3287 // MB_STRNICMP ignores case, but we should not ignore case.
3288 // Unfortunately, there is no MB_STRNICMP function.
3289 // XXX: above comment should be "no MB_STRCMP function" ?
3290 if (!(chgtick == CHANGEDTICK(curbuf)
3291 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3292 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3293 && EQUAL_POS(lastpos, *cursor_pos)
3294 && lbuf == curbuf) || wraparound || cur < 0
3295 || (maxcount > 0 && cur > maxcount) || recompute)
3296 {
3297 cur = 0;
3298 cnt = 0;
3299 exact_match = FALSE;
3300 incomplete = 0;
3301 CLEAR_POS(&lastpos);
3302 lbuf = curbuf;
3303 }
3304
Christian Brabandt34a6a362023-05-06 19:20:20 +01003305 // when searching backwards and having jumped to the first occurrence,
3306 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003307 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003308 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003309 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3310 else
3311 {
3312 int done_search = FALSE;
3313 pos_T endpos = {0, 0, 0};
3314
3315 p_ws = FALSE;
3316#ifdef FEAT_RELTIME
3317 if (timeout > 0)
3318 profile_setlimit(timeout, &start);
3319#endif
3320 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
John Marriott8c85a2a2024-05-20 19:18:26 +02003321 FORWARD, NULL, 0, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003322 {
3323 done_search = TRUE;
3324#ifdef FEAT_RELTIME
3325 // Stop after passing the time limit.
3326 if (timeout > 0 && profile_passed_limit(&start))
3327 {
3328 incomplete = 1;
3329 break;
3330 }
3331#endif
3332 cnt++;
3333 if (LTOREQ_POS(lastpos, p))
3334 {
3335 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003336 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003337 exact_match = TRUE;
3338 }
3339 fast_breakcheck();
3340 if (maxcount > 0 && cnt > maxcount)
3341 {
3342 incomplete = 2; // max count exceeded
3343 break;
3344 }
3345 }
3346 if (got_int)
3347 cur = -1; // abort
3348 if (done_search)
3349 {
3350 vim_free(lastpat);
3351 lastpat = vim_strsave(spats[last_idx].pat);
3352 chgtick = CHANGEDTICK(curbuf);
3353 lbuf = curbuf;
3354 lastpos = p;
3355 }
3356 }
3357 stat->cur = cur;
3358 stat->cnt = cnt;
3359 stat->exact_match = exact_match;
3360 stat->incomplete = incomplete;
3361 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003362 p_ws = save_ws;
3363}
3364
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003366
3367/*
3368 * Get line "lnum" and copy it into "buf[LSIZE]".
3369 * The copy is made because the regexp may make the line invalid when using a
3370 * mark.
3371 */
3372 static char_u *
3373get_line_and_copy(linenr_T lnum, char_u *buf)
3374{
3375 char_u *line = ml_get(lnum);
3376
3377 vim_strncpy(buf, line, LSIZE - 1);
3378 return buf;
3379}
3380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381/*
3382 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003383 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003386find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003387 char_u *ptr, // pointer to search pattern
3388 int dir UNUSED, // direction of expansion
3389 int len, // length of search pattern
3390 int whole, // match whole words only
3391 int skip_comments, // don't match inside comments
3392 int type, // Type of search; are we looking for a type?
3393 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003394 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003395 int action, // What to do when we find it
3396 linenr_T start_lnum, // first line to start searching
Colin Kennedy21570352024-03-03 16:16:47 +01003397 linenr_T end_lnum, // last line for searching
3398 int forceit) // If true, always switch to the found path
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003400 SearchedFile *files; // Stack of included files
3401 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 int max_path_depth = 50;
3403 long match_count = 1;
3404
3405 char_u *pat;
3406 char_u *new_fname;
3407 char_u *curr_fname = curbuf->b_fname;
3408 char_u *prev_fname = NULL;
3409 linenr_T lnum;
3410 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003411 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 int old_files;
3413 int already_searched;
3414 char_u *file_line;
3415 char_u *line;
3416 char_u *p;
3417 char_u save_char;
3418 int define_matched;
3419 regmatch_T regmatch;
3420 regmatch_T incl_regmatch;
3421 regmatch_T def_regmatch;
3422 int matched = FALSE;
3423 int did_show = FALSE;
3424 int found = FALSE;
3425 int i;
3426 char_u *already = NULL;
3427 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003428 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003429#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 win_T *curwin_save = NULL;
3431#endif
3432
3433 regmatch.regprog = NULL;
3434 incl_regmatch.regprog = NULL;
3435 def_regmatch.regprog = NULL;
3436
3437 file_line = alloc(LSIZE);
3438 if (file_line == NULL)
3439 return;
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003442 // when CONT_SOL is set compare "ptr" with the beginning of the
3443 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3444 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 pat = alloc(len + 5);
3447 if (pat == NULL)
3448 goto fpip_end;
John Marriott8c85a2a2024-05-20 19:18:26 +02003449 vim_snprintf((char *)pat, len + 5, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003450 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003452 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 vim_free(pat);
3454 if (regmatch.regprog == NULL)
3455 goto fpip_end;
3456 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003457 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3458 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003460 incl_regmatch.regprog = vim_regcomp(inc_opt,
3461 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (incl_regmatch.regprog == NULL)
3463 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003464 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
3466 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3467 {
John Marriott8c85a2a2024-05-20 19:18:26 +02003468 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL ? p_def : curbuf->b_p_def,
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003469 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 if (def_regmatch.regprog == NULL)
3471 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003472 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003474 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (files == NULL)
3476 goto fpip_end;
3477 old_files = max_path_depth;
3478 depth = depth_displayed = -1;
3479
3480 lnum = start_lnum;
3481 if (end_lnum > curbuf->b_ml.ml_line_count)
3482 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003483 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003485 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
3487 for (;;)
3488 {
3489 if (incl_regmatch.regprog != NULL
3490 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3491 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003492 char_u *p_fname = (curr_fname == curbuf->b_fname)
3493 ? curbuf->b_ffname : curr_fname;
3494
3495 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003496 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003497 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003498 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003499 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3500 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003501 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003502 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003503 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 already_searched = FALSE;
3505 if (new_fname != NULL)
3506 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003507 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 for (i = 0;; i++)
3509 {
3510 if (i == depth + 1)
3511 i = old_files;
3512 if (i == max_path_depth)
3513 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003514 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3515 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003517 if (type != CHECK_PATH
3518 && action == ACTION_SHOW_ALL
3519 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003521 msg_putchar('\n'); // cursor below last one
3522 if (!got_int) // don't display if 'q'
3523 // typed at "--more--"
3524 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003527 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 prev_fname = NULL;
3529 }
3530 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003531 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 already_searched = TRUE;
3533 break;
3534 }
3535 }
3536 }
3537
3538 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3539 || (new_fname == NULL && !already_searched)))
3540 {
3541 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003542 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 else
3544 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003545 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003546 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003548 msg_puts_title(_("not found "));
3549 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551 did_show = TRUE;
3552 while (depth_displayed < depth && !got_int)
3553 {
3554 ++depth_displayed;
3555 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003556 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003558 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003560 if (!got_int) // don't display if 'q' typed
3561 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
3563 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003564 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 if (new_fname != NULL)
3566 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003567 // using "new_fname" is more reliable, e.g., when
3568 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003569 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571 else
3572 {
3573 /*
3574 * Isolate the file name.
3575 * Include the surrounding "" or <> if present.
3576 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003577 if (inc_opt != NULL
3578 && strstr((char *)inc_opt, "\\zs") != NULL)
3579 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003580 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003581 p = incl_regmatch.startp[0];
3582 i = (int)(incl_regmatch.endp[0]
3583 - incl_regmatch.startp[0]);
3584 }
3585 else
3586 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003587 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003588 for (p = incl_regmatch.endp[0];
3589 *p && !vim_isfilec(*p); p++)
3590 ;
3591 for (i = 0; vim_isfilec(p[i]); i++)
3592 ;
3593 }
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (i == 0)
3596 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003597 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003599 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003601 // Avoid checking before the start of the line, can
3602 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003603 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
3605 if (p[-1] == '"' || p[-1] == '<')
3606 {
3607 --p;
3608 ++i;
3609 }
3610 if (p[i] == '"' || p[i] == '>')
3611 ++i;
3612 }
3613 save_char = p[i];
3614 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003615 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 p[i] = save_char;
3617 }
3618
3619 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3620 {
3621 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003622 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003624 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003627 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629
3630 if (new_fname != NULL)
3631 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003632 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 if (depth + 1 == old_files)
3634 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003635 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (bigger != NULL)
3637 {
3638 for (i = 0; i <= depth; i++)
3639 bigger[i] = files[i];
3640 for (i = depth + 1; i < old_files + max_path_depth; i++)
3641 {
3642 bigger[i].fp = NULL;
3643 bigger[i].name = NULL;
3644 bigger[i].lnum = 0;
3645 bigger[i].matched = FALSE;
3646 }
3647 for (i = old_files; i < max_path_depth; i++)
3648 bigger[i + max_path_depth] = files[i];
3649 old_files += max_path_depth;
3650 max_path_depth *= 2;
3651 vim_free(files);
3652 files = bigger;
3653 }
3654 }
3655 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3656 == NULL)
3657 vim_free(new_fname);
3658 else
3659 {
3660 if (++depth == old_files)
3661 {
3662 /*
3663 * lalloc() for 'bigger' must have failed above. We
3664 * will forget one of our already visited files now.
3665 */
3666 vim_free(files[old_files].name);
3667 ++old_files;
3668 }
3669 files[depth].name = curr_fname = new_fname;
3670 files[depth].lnum = 0;
3671 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 if (action == ACTION_EXPAND)
3673 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003674 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003675 vim_snprintf((char*)IObuff, IOSIZE,
3676 _("Scanning included file: %s"),
3677 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003678 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003680 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003681 {
3682 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003683 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003684 (char *)new_fname);
3685 verbose_leave();
3686 }
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 }
3690 }
3691 else
3692 {
3693 /*
3694 * Check if the line is a define (type == FIND_DEFINE)
3695 */
3696 p = line;
3697search_line:
3698 define_matched = FALSE;
3699 if (def_regmatch.regprog != NULL
3700 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3701 {
3702 /*
3703 * Pattern must be first identifier after 'define', so skip
3704 * to that position before checking for match of pattern. Also
3705 * don't let it match beyond the end of this identifier.
3706 */
3707 p = def_regmatch.endp[0];
3708 while (*p && !vim_iswordc(*p))
3709 p++;
3710 define_matched = TRUE;
3711 }
3712
3713 /*
3714 * Look for a match. Don't do this if we are looking for a
3715 * define and this line didn't match define_prog above.
3716 */
3717 if (def_regmatch.regprog == NULL || define_matched)
3718 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003719 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003721 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 startp = skipwhite(p);
3723 if (p_ic)
3724 matched = !MB_STRNICMP(startp, ptr, len);
3725 else
3726 matched = !STRNCMP(startp, ptr, len);
3727 if (matched && define_matched && whole
3728 && vim_iswordc(startp[len]))
3729 matched = FALSE;
3730 }
3731 else if (regmatch.regprog != NULL
3732 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3733 {
3734 matched = TRUE;
3735 startp = regmatch.startp[0];
3736 /*
3737 * Check if the line is not a comment line (unless we are
3738 * looking for a define). A line starting with "# define"
3739 * is not considered to be a comment line.
3740 */
3741 if (!define_matched && skip_comments)
3742 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if ((*line != '#' ||
3744 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003745 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 matched = FALSE;
3747
3748 /*
3749 * Also check for a "/ *" or "/ /" before the match.
3750 * Skips lines like "int backwards; / * normal index
3751 * * /" when looking for "normal".
3752 * Note: Doesn't skip "/ *" in comments.
3753 */
3754 p = skipwhite(line);
3755 if (matched
3756 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 for (p = line; *p && p < startp; ++p)
3758 {
3759 if (matched
3760 && p[0] == '/'
3761 && (p[1] == '*' || p[1] == '/'))
3762 {
3763 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003764 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (p[1] == '/')
3766 break;
3767 ++p;
3768 }
3769 else if (!matched && p[0] == '*' && p[1] == '/')
3770 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003771 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 matched = TRUE;
3773 ++p;
3774 }
3775 }
3776 }
3777 }
3778 }
3779 }
3780 if (matched)
3781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 if (action == ACTION_EXPAND)
3783 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003784 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 int add_r;
3786 char_u *aux;
3787
3788 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3789 break;
3790 found = TRUE;
3791 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003792 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003794 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (vim_iswordp(p))
3796 goto exit_matched;
3797 p = find_word_start(p);
3798 }
3799 p = find_word_end(p);
3800 i = (int)(p - aux);
3801
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003802 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003804 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003806
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003807 // Get the next line: when "depth" < 0 from the current
3808 // buffer, otherwise from the included file. Jump to
3809 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003810 if (depth < 0)
3811 {
3812 if (lnum >= end_lnum)
3813 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003814 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003815 }
3816 else if (vim_fgets(line = file_line,
3817 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 goto exit_matched;
3819
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003820 // we read a line, set "already" to check this "line" later
3821 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003822 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 already = aux = p = skipwhite(line);
3824 p = find_word_start(p);
3825 p = find_word_end(p);
3826 if (p > aux)
3827 {
3828 if (*aux != ')' && IObuff[i-1] != TAB)
3829 {
3830 if (IObuff[i-1] != ' ')
3831 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003832 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (p_js
3834 && (IObuff[i-2] == '.'
3835 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3836 && (IObuff[i-2] == '?'
3837 || IObuff[i-2] == '!'))))
3838 IObuff[i++] = ' ';
3839 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003840 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (p - aux >= IOSIZE - i)
3842 p = aux + IOSIZE - i - 1;
3843 STRNCPY(IObuff + i, aux, p - aux);
3844 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003845 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 }
3847 IObuff[i] = NUL;
3848 aux = IObuff;
3849
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003850 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 goto exit_matched;
3852 }
3853
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003854 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003856 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003858 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003860 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 break;
3862 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003863 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
3865 found = TRUE;
3866 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003867 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 if (curr_fname != prev_fname)
3869 {
3870 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003871 msg_putchar('\n'); // cursor below last one
3872 if (!got_int) // don't display if 'q' typed
3873 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 msg_home_replace_hl(curr_fname);
3875 prev_fname = curr_fname;
3876 }
3877 did_show = TRUE;
3878 if (!got_int)
3879 show_pat_in_path(line, type, TRUE, action,
3880 (depth == -1) ? NULL : files[depth].fp,
3881 (depth == -1) ? &lnum : &files[depth].lnum,
3882 match_count++);
3883
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003884 // Set matched flag for this file and all the ones that
3885 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 for (i = 0; i <= depth; ++i)
3887 files[i].matched = TRUE;
3888 }
3889 else if (--count <= 0)
3890 {
3891 found = TRUE;
3892 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003893#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 && g_do_tagpreview == 0
3895#endif
3896 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003897 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 else if (action == ACTION_SHOW)
3899 {
3900 show_pat_in_path(line, type, did_show, action,
3901 (depth == -1) ? NULL : files[depth].fp,
3902 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3903 did_show = TRUE;
3904 }
3905 else
3906 {
3907#ifdef FEAT_GUI
3908 need_mouse_correct = TRUE;
3909#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003910#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003911 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (g_do_tagpreview != 0)
3913 {
3914 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003915 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917#endif
3918 if (action == ACTION_SPLIT)
3919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003922 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
3924 if (depth == -1)
3925 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003926 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003927#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if (g_do_tagpreview != 0)
3929 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003930 if (!win_valid(curwin_save))
3931 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003932 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003933 curwin_save->w_buffer->b_fnum, NULL,
Colin Kennedy21570352024-03-03 16:16:47 +01003934 NULL, TRUE, lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003935 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937 else
3938#endif
3939 setpcmark();
3940 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003941 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 }
3943 else
3944 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003945 if (!GETFILE_SUCCESS(getfile(
3946 0, files[depth].name, NULL, TRUE,
Colin Kennedy21570352024-03-03 16:16:47 +01003947 files[depth].lnum, forceit)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003948 break; // failed to jump to file
3949 // autocommands may have changed the lnum, we don't
3950 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 curwin->w_cursor.lnum = files[depth].lnum;
3952 }
3953 }
3954 if (action != ACTION_SHOW)
3955 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003956 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 curwin->w_set_curswant = TRUE;
3958 }
3959
Bram Moolenaar4033c552017-09-16 20:54:51 +02003960#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003962 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003964 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003966 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 win_enter(curwin_save, TRUE);
3968 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003969# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003970 else if (WIN_IS_POPUP(curwin))
3971 // can't keep focus in popup window
3972 win_enter(firstwin, TRUE);
3973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974#endif
3975 break;
3976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003979 // look for other matches in the rest of the line if we
3980 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003983 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003984 && *startp != NUL
John Marriott8c85a2a2024-05-20 19:18:26 +02003985 && *(startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 goto search_line;
3987 }
3988 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003990 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003991 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 break;
3993
3994 /*
3995 * Read the next line. When reading an included file and encountering
3996 * end-of-file, close the file and continue in the file that included
3997 * it.
3998 */
3999 while (depth >= 0 && !already
4000 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4001 {
4002 fclose(files[depth].fp);
4003 --old_files;
4004 files[old_files].name = files[depth].name;
4005 files[old_files].matched = files[depth].matched;
4006 --depth;
4007 curr_fname = (depth == -1) ? curbuf->b_fname
4008 : files[depth].name;
4009 if (depth < depth_displayed)
4010 depth_displayed = depth;
4011 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004012 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004015 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004016 i = (int)STRLEN(line);
4017 if (i > 0 && line[i - 1] == '\n')
4018 line[--i] = NUL;
4019 if (i > 0 && line[i - 1] == '\r')
4020 line[--i] = NUL;
4021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 else if (!already)
4023 {
4024 if (++lnum > end_lnum)
4025 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01004026 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028 already = NULL;
4029 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004030 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004032 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 for (i = 0; i <= depth; i++)
4034 {
4035 fclose(files[i].fp);
4036 vim_free(files[i].name);
4037 }
4038 for (i = old_files; i < max_path_depth; i++)
4039 vim_free(files[i].name);
4040 vim_free(files);
4041
4042 if (type == CHECK_PATH)
4043 {
4044 if (!did_show)
4045 {
4046 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004047 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004049 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
4051 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004052 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004054 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004055 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004057 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00004059 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 }
4061 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4062 msg_end();
4063
4064fpip_end:
4065 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02004066 vim_regfree(regmatch.regprog);
4067 vim_regfree(incl_regmatch.regprog);
4068 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069}
4070
4071 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004072show_pat_in_path(
4073 char_u *line,
4074 int type,
4075 int did_show,
4076 int action,
4077 FILE *fp,
4078 linenr_T *lnum,
4079 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
4081 char_u *p;
John Marriott8c85a2a2024-05-20 19:18:26 +02004082 size_t linelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004085 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004086 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004087 gotocmdline(TRUE); // cursor at status line
4088 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return;
John Marriott8c85a2a2024-05-20 19:18:26 +02004090 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 for (;;)
4092 {
John Marriott8c85a2a2024-05-20 19:18:26 +02004093 p = line + linelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (fp != NULL)
4095 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004096 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (p >= line && *p == '\n')
4098 --p;
4099 if (p >= line && *p == '\r')
4100 --p;
4101 *(p + 1) = NUL;
4102 }
4103 if (action == ACTION_SHOW_ALL)
4104 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004105 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004106 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004107 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4108 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004109 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4110 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004112 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004113 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004115 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4117 break;
4118
4119 if (fp != NULL)
4120 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004121 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 break;
John Marriott8c85a2a2024-05-20 19:18:26 +02004123 linelen = STRLEN(line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 ++*lnum;
4125 }
4126 else
4127 {
4128 if (++*lnum > curbuf->b_ml.ml_line_count)
4129 break;
4130 line = ml_get(*lnum);
John Marriott8c85a2a2024-05-20 19:18:26 +02004131 linelen = ml_get_len(*lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 msg_putchar('\n');
4134 }
4135}
4136#endif
4137
4138#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004139/*
4140 * Return the last used search pattern at "idx".
4141 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004142 spat_T *
4143get_spat(int idx)
4144{
4145 return &spats[idx];
4146}
4147
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004148/*
4149 * Return the last used search pattern index.
4150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004152get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004154 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004157
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004158#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004159/*
4160 * "searchcount()" function
4161 */
4162 void
4163f_searchcount(typval_T *argvars, typval_T *rettv)
4164{
4165 pos_T pos = curwin->w_cursor;
4166 char_u *pattern = NULL;
4167 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4168 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004169 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004170 searchstat_T stat;
4171
4172 if (rettv_dict_alloc(rettv) == FAIL)
4173 return;
4174
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004175 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4176 return;
4177
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004178 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4179 recompute = TRUE;
4180
4181 if (argvars[0].v_type != VAR_UNKNOWN)
4182 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004183 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004184 dictitem_T *di;
4185 listitem_T *li;
4186 int error = FALSE;
4187
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004188 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004189 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004190 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004191 di = dict_find(dict, (char_u *)"timeout", -1);
4192 if (di != NULL)
4193 {
4194 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4195 if (error)
4196 return;
4197 }
4198 di = dict_find(dict, (char_u *)"maxcount", -1);
4199 if (di != NULL)
4200 {
4201 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4202 if (error)
4203 return;
4204 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004205 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004206 di = dict_find(dict, (char_u *)"pattern", -1);
4207 if (di != NULL)
4208 {
4209 pattern = tv_get_string_chk(&di->di_tv);
4210 if (pattern == NULL)
4211 return;
4212 }
4213 di = dict_find(dict, (char_u *)"pos", -1);
4214 if (di != NULL)
4215 {
4216 if (di->di_tv.v_type != VAR_LIST)
4217 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004218 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004219 return;
4220 }
4221 if (list_len(di->di_tv.vval.v_list) != 3)
4222 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004223 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004224 return;
4225 }
4226 li = list_find(di->di_tv.vval.v_list, 0L);
4227 if (li != NULL)
4228 {
4229 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4230 if (error)
4231 return;
4232 }
4233 li = list_find(di->di_tv.vval.v_list, 1L);
4234 if (li != NULL)
4235 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004236 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004237 if (error)
4238 return;
4239 }
4240 li = list_find(di->di_tv.vval.v_list, 2L);
4241 if (li != NULL)
4242 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004243 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004244 if (error)
4245 return;
4246 }
4247 }
4248 }
4249
4250 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004251#ifdef FEAT_SEARCH_EXTRA
4252 save_incsearch_state();
4253#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004254 if (pattern != NULL)
4255 {
4256 if (*pattern == NUL)
4257 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004258 vim_free(spats[last_idx].pat);
John Marriott8c85a2a2024-05-20 19:18:26 +02004259 spats[last_idx].patlen = STRLEN(pattern);
4260 spats[last_idx].pat = vim_strnsave(pattern, spats[last_idx].patlen);
4261 if (spats[last_idx].pat == NULL)
4262 spats[last_idx].patlen = 0;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004263 }
4264 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4265 goto the_end; // the previous pattern was never defined
4266
4267 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4268
4269 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4270 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4271 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4272 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4273 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4274
4275the_end:
4276 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004277#ifdef FEAT_SEARCH_EXTRA
4278 restore_incsearch_state();
4279#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004280}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004281#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004282
4283/*
4284 * Fuzzy string matching
4285 *
4286 * Ported from the lib_fts library authored by Forrest Smith.
4287 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4288 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004289 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004290 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4291 *
4292 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004293 * - Matched letter
4294 * - Unmatched letter
4295 * - Consecutively matched letters
4296 * - Proximity to start
4297 * - Letter following a separator (space, underscore)
4298 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004299 *
4300 * Matched letters are good. Unmatched letters are bad. Matching near the start
4301 * is good. Matching the first letter in the middle of a phrase is good.
4302 * Matching the uppercase letters in camel case entries is good.
4303 *
4304 * The score assigned for each factor is explained below.
4305 * File paths are different from file names. File extensions may be ignorable.
4306 * Single words care about consecutive matches but not separators or camel
4307 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004308 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004309 * Matched letter: +0 points
4310 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004311 * Consecutive match bonus: +15 points
4312 * First letter bonus: +15 points
4313 * Separator bonus: +30 points
4314 * Camel case bonus: +30 points
4315 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004316 *
4317 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004318 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004319 * lower minimum score due to unmatched letter penalty. Longer search patterns
4320 * have a higher maximum score due to match bonuses.
4321 *
4322 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4323 * quite a bit.
4324 *
4325 * There is a penalty if you DON’T match the first three letters. Which
4326 * effectively rewards matching near the start. However there’s no difference
4327 * in matching between the middle and end.
4328 *
4329 * There is not an explicit bonus for an exact match. Unmatched letters receive
4330 * a penalty. So shorter strings and closer matches are worth more.
4331 */
4332typedef struct
4333{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004334 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004335 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004336 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004337 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004338} fuzzyItem_T;
4339
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004340// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4341// matching a whole word is preferred.
4342#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004343// bonus if match occurs after a path separator
4344#define PATH_SEPARATOR_BONUS 30
4345// bonus if match occurs after a word separator
4346#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004347// bonus if match is uppercase and prev is lower
4348#define CAMEL_BONUS 30
4349// bonus if the first letter is matched
4350#define FIRST_LETTER_BONUS 15
4351// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004352#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004353// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004354#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004355// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004356#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004357// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004358#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004359// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004360#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004361
4362#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004363
4364/*
4365 * Compute a score for a fuzzy matched string. The matching character locations
4366 * are in 'matches'.
4367 */
4368 static int
4369fuzzy_match_compute_score(
4370 char_u *str,
4371 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004372 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004373 int numMatches)
4374{
4375 int score;
4376 int penalty;
4377 int unmatched;
4378 int i;
4379 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004380 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004381
4382 // Initialize score
4383 score = 100;
4384
4385 // Apply leading letter penalty
4386 penalty = LEADING_LETTER_PENALTY * matches[0];
4387 if (penalty < MAX_LEADING_LETTER_PENALTY)
4388 penalty = MAX_LEADING_LETTER_PENALTY;
4389 score += penalty;
4390
4391 // Apply unmatched penalty
4392 unmatched = strSz - numMatches;
4393 score += UNMATCHED_LETTER_PENALTY * unmatched;
4394
4395 // Apply ordering bonuses
4396 for (i = 0; i < numMatches; ++i)
4397 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004398 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004399
4400 if (i > 0)
4401 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004402 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004403
4404 // Sequential
4405 if (currIdx == (prevIdx + 1))
4406 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004407 else
4408 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004409 }
4410
4411 // Check for bonuses based on neighbor character value
4412 if (currIdx > 0)
4413 {
4414 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004415 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004416 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004417
4418 if (has_mbyte)
4419 {
4420 while (sidx < currIdx)
4421 {
4422 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004423 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004424 sidx++;
4425 }
4426 curr = (*mb_ptr2char)(p);
4427 }
4428 else
4429 {
4430 neighbor = str[currIdx - 1];
4431 curr = str[currIdx];
4432 }
4433
4434 if (vim_islower(neighbor) && vim_isupper(curr))
4435 score += CAMEL_BONUS;
4436
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004437 // Bonus if the match follows a separator character
4438 if (neighbor == '/' || neighbor == '\\')
4439 score += PATH_SEPARATOR_BONUS;
4440 else if (neighbor == ' ' || neighbor == '_')
4441 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004442 }
4443 else
4444 {
4445 // First letter
4446 score += FIRST_LETTER_BONUS;
4447 }
4448 }
4449 return score;
4450}
4451
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004452/*
4453 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4454 * Return the number of matching characters.
4455 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004456 static int
4457fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004458 char_u *fuzpat,
4459 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004460 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004461 int *outScore,
4462 char_u *strBegin,
4463 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004464 int_u *srcMatches,
4465 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004466 int maxMatches,
4467 int nextMatch,
4468 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004469{
4470 // Recursion params
4471 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004472 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004473 int bestRecursiveScore = 0;
4474 int first_match;
4475 int matched;
4476
4477 // Count recursions
4478 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004479 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004480 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004481
4482 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004483 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004484 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004485
4486 // Loop through fuzpat and str looking for a match
4487 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004488 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004489 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004490 int c1;
4491 int c2;
4492
4493 c1 = PTR2CHAR(fuzpat);
4494 c2 = PTR2CHAR(str);
4495
Bram Moolenaar635414d2020-09-11 22:25:15 +02004496 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004497 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004498 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004499 // Supplied matches buffer was too short
4500 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004501 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004502
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004503 int recursiveScore = 0;
4504 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4505 CLEAR_FIELD(recursiveMatches);
4506
Bram Moolenaar635414d2020-09-11 22:25:15 +02004507 // "Copy-on-Write" srcMatches into matches
4508 if (first_match && srcMatches)
4509 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004510 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004511 first_match = FALSE;
4512 }
4513
4514 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004515 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004516 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4517 &recursiveScore, strBegin, strLen, matches,
4518 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004519 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004520 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004521 {
4522 // Pick best recursive score
4523 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4524 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004525 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004526 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004527 bestRecursiveScore = recursiveScore;
4528 }
4529 recursiveMatch = TRUE;
4530 }
4531
4532 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004533 matches[nextMatch++] = strIdx;
4534 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004535 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004536 else
4537 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004538 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004539 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004540 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004541 else
4542 ++str;
4543 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004544 }
4545
4546 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004547 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004548
4549 // Calculate score
4550 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004551 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4552 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004553
4554 // Return best result
4555 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4556 {
4557 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004558 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004559 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004560 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004561 }
4562 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004563 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004564
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004565 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004566}
4567
4568/*
4569 * fuzzy_match()
4570 *
4571 * Performs exhaustive search via recursion to find all possible matches and
4572 * match with highest score.
4573 * Scores values have no intrinsic meaning. Possible score range is not
4574 * normalized and varies with pattern.
4575 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004576 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004577 * Uses char_u for match indices. Therefore patterns are limited to
4578 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004579 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004580 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4581 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004582 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004583 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004584fuzzy_match(
4585 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004586 char_u *pat_arg,
4587 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004588 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004589 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004590 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004591{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004592 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004593 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004594 char_u *save_pat;
4595 char_u *pat;
4596 char_u *p;
4597 int complete = FALSE;
4598 int score = 0;
4599 int numMatches = 0;
4600 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004601
4602 *outScore = 0;
4603
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004604 save_pat = vim_strsave(pat_arg);
4605 if (save_pat == NULL)
4606 return FALSE;
4607 pat = save_pat;
4608 p = pat;
4609
4610 // Try matching each word in 'pat_arg' in 'str'
4611 while (TRUE)
4612 {
4613 if (matchseq)
4614 complete = TRUE;
4615 else
4616 {
4617 // Extract one word from the pattern (separated by space)
4618 p = skipwhite(p);
4619 if (*p == NUL)
4620 break;
4621 pat = p;
4622 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4623 {
4624 if (has_mbyte)
4625 MB_PTR_ADV(p);
4626 else
4627 ++p;
4628 }
4629 if (*p == NUL) // processed all the words
4630 complete = TRUE;
4631 *p = NUL;
4632 }
4633
4634 score = 0;
4635 recursionCount = 0;
4636 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4637 matches + numMatches, maxMatches - numMatches,
4638 0, &recursionCount);
4639 if (matchCount == 0)
4640 {
4641 numMatches = 0;
4642 break;
4643 }
4644
4645 // Accumulate the match score and the number of matches
4646 *outScore += score;
4647 numMatches += matchCount;
4648
4649 if (complete)
4650 break;
4651
4652 // try matching the next word
4653 ++p;
4654 }
4655
4656 vim_free(save_pat);
4657 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004658}
4659
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004660#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004661/*
4662 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004663 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004664 */
4665 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004666fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004667{
4668 int v1 = ((fuzzyItem_T *)s1)->score;
4669 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004670 int idx1 = ((fuzzyItem_T *)s1)->idx;
4671 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004672
zeertzjq77078272024-02-10 13:24:03 +01004673 if (v1 == v2)
4674 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
4675 else
4676 return v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004677}
4678
4679/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004680 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4681 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004682 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4683 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004684 * If 'items' is a list of strings, then search for 'str' in the list.
4685 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4686 * for each item or use 'item_cb' Funcref function to get the string.
4687 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4688 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004689 */
4690 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004691fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004692 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004693 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004694 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004695 char_u *key,
4696 callback_T *item_cb,
4697 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004698 list_T *fmatchlist,
4699 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004700{
4701 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004702 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004703 listitem_T *li;
4704 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004705 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004706 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004707
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004708 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004709 if (len == 0)
4710 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004711 if (max_matches > 0 && len > max_matches)
4712 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004713
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004714 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4715 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004716 return;
4717
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004718 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004719 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004720 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004721 int score;
4722 char_u *itemstr;
4723 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004724
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004725 if (max_matches > 0 && match_count >= max_matches)
4726 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004727
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004728 itemstr = NULL;
4729 rettv.v_type = VAR_UNKNOWN;
4730 if (li->li_tv.v_type == VAR_STRING) // list of strings
4731 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004732 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004733 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004734 {
4735 // For a dict, either use the specified key to lookup the string or
4736 // use the specified callback function to get the string.
4737 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004738 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4739 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004740 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004741 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004742 typval_T argv[2];
4743
4744 // Invoke the supplied callback (if any) to get the dict item
4745 li->li_tv.vval.v_dict->dv_refcount++;
4746 argv[0].v_type = VAR_DICT;
4747 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4748 argv[1].v_type = VAR_UNKNOWN;
4749 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4750 {
4751 if (rettv.v_type == VAR_STRING)
4752 itemstr = rettv.vval.v_string;
4753 }
4754 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004755 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004756 }
4757
4758 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004759 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004760 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004761 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004762 items[match_count].idx = match_count;
4763 items[match_count].item = li;
4764 items[match_count].score = score;
4765
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004766 // Copy the list of matching positions in itemstr to a list, if
4767 // 'retmatchpos' is set.
4768 if (retmatchpos)
4769 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004770 int j = 0;
4771 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004772
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004773 items[match_count].lmatchpos = list_alloc();
4774 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004775 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004776
4777 p = str;
4778 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004779 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004780 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004781 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004782 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004783 matches[j]) == FAIL)
4784 goto done;
4785 j++;
4786 }
4787 if (has_mbyte)
4788 MB_PTR_ADV(p);
4789 else
4790 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004791 }
4792 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004793 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004794 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004795 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004796 }
4797
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004798 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004799 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004800 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004801
Bram Moolenaar635414d2020-09-11 22:25:15 +02004802 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004803 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004804 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004805
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004806 // For matchfuzzy(), return a list of matched strings.
4807 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004808 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004809 // The first item is a list of matched strings. The second item
4810 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004811 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004812 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4813 if (retmatchpos)
4814 {
4815 li = list_find(fmatchlist, 0);
4816 if (li == NULL || li->li_tv.vval.v_list == NULL)
4817 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004818 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004819 }
4820 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004821 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004822
4823 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004824 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004825 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004826 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004827 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004828 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004829 }
4830
4831 // next copy the list of matching positions
4832 if (retmatchpos)
4833 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004834 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004835 if (li == NULL || li->li_tv.vval.v_list == NULL)
4836 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004837 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004838
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004839 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004840 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004841 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004842 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004843 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004844 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004845 goto done;
4846 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004847
4848 // copy the matching scores
4849 li = list_find(fmatchlist, -1);
4850 if (li == NULL || li->li_tv.vval.v_list == NULL)
4851 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004852 retlist = li->li_tv.vval.v_list;
4853 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004854 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004855 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004856 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004857 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004858 goto done;
4859 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004860 }
4861 }
4862
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004863done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004864 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004865}
4866
4867/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004868 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4869 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4870 */
4871 static void
4872do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4873{
4874 callback_T cb;
4875 char_u *key = NULL;
4876 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004877 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004878 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004879
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004880 if (in_vim9script()
4881 && (check_for_list_arg(argvars, 0) == FAIL
4882 || check_for_string_arg(argvars, 1) == FAIL
4883 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4884 return;
4885
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004886 CLEAR_POINTER(&cb);
4887
4888 // validate and get the arguments
4889 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4890 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004891 semsg(_(e_argument_of_str_must_be_list),
4892 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004893 return;
4894 }
4895 if (argvars[1].v_type != VAR_STRING
4896 || argvars[1].vval.v_string == NULL)
4897 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004898 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004899 return;
4900 }
4901
4902 if (argvars[2].v_type != VAR_UNKNOWN)
4903 {
4904 dict_T *d;
4905 dictitem_T *di;
4906
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004907 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004908 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004909
4910 // To search a dict, either a callback function or a key can be
4911 // specified.
4912 d = argvars[2].vval.v_dict;
4913 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4914 {
4915 if (di->di_tv.v_type != VAR_STRING
4916 || di->di_tv.vval.v_string == NULL
4917 || *di->di_tv.vval.v_string == NUL)
4918 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004919 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004920 return;
4921 }
4922 key = tv_get_string(&di->di_tv);
4923 }
4924 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4925 {
4926 cb = get_callback(&di->di_tv);
4927 if (cb.cb_name == NULL)
4928 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004929 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004930 return;
4931 }
4932 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004933
4934 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004935 {
4936 if (di->di_tv.v_type != VAR_NUMBER)
4937 {
4938 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4939 return;
4940 }
4941 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4942 }
4943
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004944 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004945 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004946 }
4947
4948 // get the fuzzy matches
4949 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004950 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004951 goto done;
4952 if (retmatchpos)
4953 {
4954 list_T *l;
4955
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004956 // For matchfuzzypos(), a list with three items are returned. First
4957 // item is a list of matching strings, the second item is a list of
4958 // lists with matching positions within each string and the third item
4959 // is the list of scores of the matches.
4960 l = list_alloc();
4961 if (l == NULL)
4962 goto done;
4963 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004964 {
4965 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004966 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004967 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004968 l = list_alloc();
4969 if (l == NULL)
4970 goto done;
4971 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004972 {
4973 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004974 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004975 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004976 l = list_alloc();
4977 if (l == NULL)
4978 goto done;
4979 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004980 {
4981 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004982 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004983 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004984 }
4985
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004986 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004987 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004988
4989done:
4990 free_callback(&cb);
4991}
4992
4993/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004994 * "matchfuzzy()" function
4995 */
4996 void
4997f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4998{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004999 do_fuzzymatch(argvars, rettv, FALSE);
5000}
5001
5002/*
5003 * "matchfuzzypos()" function
5004 */
5005 void
5006f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
5007{
5008 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02005009}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02005010#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005011
5012/*
5013 * Same as fuzzy_match_item_compare() except for use with a string match
5014 */
5015 static int
5016fuzzy_match_str_compare(const void *s1, const void *s2)
5017{
5018 int v1 = ((fuzmatch_str_T *)s1)->score;
5019 int v2 = ((fuzmatch_str_T *)s2)->score;
5020 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5021 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5022
Christian Brabandte06e4372024-02-09 19:39:14 +01005023 if (v1 == v2)
5024 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5025 else
5026 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005027}
5028
5029/*
5030 * Sort fuzzy matches by score
5031 */
5032 static void
5033fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
5034{
5035 // Sort the list by the descending order of the match score
5036 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5037 fuzzy_match_str_compare);
5038}
5039
5040/*
5041 * Same as fuzzy_match_item_compare() except for use with a function name
5042 * string match. <SNR> functions should be sorted to the end.
5043 */
5044 static int
5045fuzzy_match_func_compare(const void *s1, const void *s2)
5046{
5047 int v1 = ((fuzmatch_str_T *)s1)->score;
5048 int v2 = ((fuzmatch_str_T *)s2)->score;
5049 int idx1 = ((fuzmatch_str_T *)s1)->idx;
5050 int idx2 = ((fuzmatch_str_T *)s2)->idx;
5051 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
5052 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
5053
Christian Brabandte06e4372024-02-09 19:39:14 +01005054 if (*str1 != '<' && *str2 == '<')
5055 return -1;
5056 if (*str1 == '<' && *str2 != '<')
5057 return 1;
5058 if (v1 == v2)
5059 return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1;
5060 else
5061 return v1 > v2 ? -1 : 1;
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005062}
5063
5064/*
5065 * Sort fuzzy matches of function names by score.
5066 * <SNR> functions should be sorted to the end.
5067 */
5068 static void
5069fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
5070{
5071 // Sort the list by the descending order of the match score
5072 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
5073 fuzzy_match_func_compare);
5074}
5075
5076/*
5077 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
5078 * returns the match score.
5079 */
5080 int
5081fuzzy_match_str(char_u *str, char_u *pat)
5082{
5083 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00005084 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005085
5086 if (str == NULL || pat == NULL)
5087 return 0;
5088
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00005089 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005090 sizeof(matchpos) / sizeof(matchpos[0]));
5091
5092 return score;
5093}
5094
5095/*
glepnir40c1c332024-06-11 19:37:04 +02005096 * Fuzzy match the position of string 'pat' in string 'str'.
5097 * Returns a dynamic array of matching positions. If there is no match,
5098 * returns NULL.
5099 */
5100 garray_T *
5101fuzzy_match_str_with_pos(char_u *str UNUSED, char_u *pat UNUSED)
5102{
5103#ifdef FEAT_SEARCH_EXTRA
5104 int score = 0;
zeertzjq2f95ca92024-06-13 17:14:27 +02005105 garray_T *match_positions = NULL;
5106 int_u matches[MAX_FUZZY_MATCHES];
5107 int j = 0;
glepnir40c1c332024-06-11 19:37:04 +02005108
zeertzjq2f95ca92024-06-13 17:14:27 +02005109 if (str == NULL || pat == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005110 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005111
5112 match_positions = ALLOC_ONE(garray_T);
glepnir40c1c332024-06-11 19:37:04 +02005113 if (match_positions == NULL)
zeertzjqd9be94c2024-07-14 10:20:20 +02005114 return NULL;
zeertzjq2f95ca92024-06-13 17:14:27 +02005115 ga_init2(match_positions, sizeof(int_u), 10);
5116
5117 if (!fuzzy_match(str, pat, FALSE, &score, matches, MAX_FUZZY_MATCHES)
5118 || score == 0)
glepnir40c1c332024-06-11 19:37:04 +02005119 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005120 ga_clear(match_positions);
5121 vim_free(match_positions);
5122 return NULL;
glepnir40c1c332024-06-11 19:37:04 +02005123 }
5124
zeertzjq2f95ca92024-06-13 17:14:27 +02005125 for (char_u *p = pat; *p != NUL; MB_PTR_ADV(p))
glepnir40c1c332024-06-11 19:37:04 +02005126 {
zeertzjq2f95ca92024-06-13 17:14:27 +02005127 if (!VIM_ISWHITE(PTR2CHAR(p)))
5128 {
5129 ga_grow(match_positions, 1);
5130 ((int_u *)match_positions->ga_data)[match_positions->ga_len] =
5131 matches[j];
5132 match_positions->ga_len++;
5133 j++;
5134 }
glepnir40c1c332024-06-11 19:37:04 +02005135 }
5136
glepnir40c1c332024-06-11 19:37:04 +02005137 return match_positions;
glepnir40c1c332024-06-11 19:37:04 +02005138#else
5139 return NULL;
5140#endif
5141}
5142
5143/*
glepnir8159fb12024-07-17 20:32:54 +02005144 * This function searches for a fuzzy match of the pattern `pat` within the
5145 * line pointed to by `*ptr`. It splits the line into words, performs fuzzy
5146 * matching on each word, and returns the length and position of the first
5147 * matched word.
5148 */
5149 static int
5150fuzzy_match_str_in_line(char_u **ptr, char_u *pat, int *len, pos_T *current_pos)
5151{
5152 char_u *str = *ptr;
5153 char_u *strBegin = str;
5154 char_u *end = NULL;
5155 char_u *start = NULL;
5156 int found = FALSE;
5157 int result;
5158 char save_end;
5159
5160 if (str == NULL || pat == NULL)
5161 return found;
5162
5163 while (*str != NUL)
5164 {
5165 // Skip non-word characters
5166 start = find_word_start(str);
5167 if (*start == NUL)
5168 break;
5169 end = find_word_end(start);
5170
5171 // Extract the word from start to end
5172 save_end = *end;
5173 *end = NUL;
5174
5175 // Perform fuzzy match
5176 result = fuzzy_match_str(start, pat);
5177 *end = save_end;
5178
5179 if (result > 0)
5180 {
5181 *len = (int)(end - start);
5182 current_pos->col += (int)(end - strBegin);
5183 found = TRUE;
5184 *ptr = start;
5185 break;
5186 }
5187
5188 // Move to the end of the current word for the next iteration
5189 str = end;
5190 // Ensure we continue searching after the current word
5191 while (*str != NUL && !vim_iswordp(str))
5192 MB_PTR_ADV(str);
5193 }
5194
5195 return found;
5196}
5197
5198/*
5199 * Search for the next fuzzy match in the specified buffer.
5200 * This function attempts to find the next occurrence of the given pattern
5201 * in the buffer, starting from the current position. It handles line wrapping
5202 * and direction of search.
5203 *
5204 * Return TRUE if a match is found, otherwise FALSE.
5205 */
5206 int
5207search_for_fuzzy_match(
5208 buf_T *buf,
5209 pos_T *pos,
5210 char_u *pattern,
5211 int dir,
5212 pos_T *start_pos,
5213 int *len,
5214 char_u **ptr,
5215 int whole_line)
5216{
5217 pos_T current_pos = *pos;
5218 pos_T circly_end;
5219 int found_new_match = FAIL;
5220 int looped_around = FALSE;
5221
5222 if (whole_line)
5223 current_pos.lnum += dir;
5224
glepnir0be03e12024-07-19 16:45:05 +02005225 if (buf == curbuf)
5226 circly_end = *start_pos;
5227 else
5228 {
5229 circly_end.lnum = buf->b_ml.ml_line_count;
5230 circly_end.col = 0;
5231 circly_end.coladd = 0;
5232 }
5233
glepnir8159fb12024-07-17 20:32:54 +02005234 do {
glepnir8159fb12024-07-17 20:32:54 +02005235
5236 // Check if looped around and back to start position
5237 if (looped_around && EQUAL_POS(current_pos, circly_end))
5238 break;
5239
5240 // Ensure current_pos is valid
5241 if (current_pos.lnum >= 1 && current_pos.lnum <= buf->b_ml.ml_line_count)
5242 {
5243 // Get the current line buffer
5244 *ptr = ml_get_buf(buf, current_pos.lnum, FALSE);
5245 // If ptr is end of line is reached, move to next line
5246 // or previous line based on direction
5247 if (**ptr != NUL)
5248 {
5249 if (!whole_line)
5250 {
5251 *ptr += current_pos.col;
5252 // Try to find a fuzzy match in the current line starting from current position
5253 found_new_match = fuzzy_match_str_in_line(ptr, pattern, len, &current_pos);
5254 if (found_new_match)
5255 {
5256 *pos = current_pos;
5257 break;
5258 }
glepnir0be03e12024-07-19 16:45:05 +02005259 else if (looped_around && current_pos.lnum == circly_end.lnum)
5260 break;
glepnir8159fb12024-07-17 20:32:54 +02005261 }
5262 else
5263 {
5264 if (fuzzy_match_str(*ptr, pattern) > 0)
5265 {
5266 found_new_match = TRUE;
5267 *pos = current_pos;
5268 *len = STRLEN(*ptr);
5269 break;
5270 }
5271 }
5272 }
5273 }
5274
5275 // Move to the next line or previous line based on direction
5276 if (dir == FORWARD)
5277 {
5278 if (++current_pos.lnum > buf->b_ml.ml_line_count)
5279 {
5280 if (p_ws)
5281 {
5282 current_pos.lnum = 1;
5283 looped_around = TRUE;
5284 }
5285 else
5286 break;
5287 }
5288 }
5289 else
5290 {
5291 if (--current_pos.lnum < 1)
5292 {
5293 if (p_ws)
5294 {
5295 current_pos.lnum = buf->b_ml.ml_line_count;
5296 looped_around = TRUE;
5297 }
5298 else
5299 break;
5300
5301 }
5302 }
5303 current_pos.col = 0;
5304 } while (TRUE);
5305
5306 return found_new_match;
5307}
5308
5309/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005310 * Free an array of fuzzy string matches "fuzmatch[count]".
5311 */
5312 void
5313fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5314{
5315 int i;
5316
5317 if (fuzmatch == NULL)
5318 return;
5319 for (i = 0; i < count; ++i)
5320 vim_free(fuzmatch[i].str);
5321 vim_free(fuzmatch);
5322}
5323
5324/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005325 * Copy a list of fuzzy matches into a string list after sorting the matches by
5326 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5327 * Returns OK on success and FAIL on memory allocation failure.
5328 */
5329 int
5330fuzzymatches_to_strmatches(
5331 fuzmatch_str_T *fuzmatch,
5332 char_u ***matches,
5333 int count,
5334 int funcsort)
5335{
5336 int i;
5337
5338 if (count <= 0)
5339 return OK;
5340
5341 *matches = ALLOC_MULT(char_u *, count);
5342 if (*matches == NULL)
5343 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005344 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005345 return FAIL;
5346 }
5347
5348 // Sort the list by the descending order of the match score
5349 if (funcsort)
5350 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5351 else
5352 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5353
5354 for (i = 0; i < count; i++)
5355 (*matches)[i] = fuzmatch[i].str;
5356 vim_free(fuzmatch);
5357
5358 return OK;
5359}