blob: 793e042341d69ff3d9396197a8ed592e391c7b8f [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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010020static void show_pat_in_path(char_u *, int,
21 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
35static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, int recompute, int maxcount, long timeout);
36static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout);
37
Bram Moolenaarea6561a2020-06-01 21:32:45 +020038#define SEARCH_STAT_DEF_TIMEOUT 40L
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020039#define SEARCH_STAT_DEF_MAX_COUNT 99
40#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar071d4272004-06-13 20:20:40 +000042/*
43 * This file contains various searching-related routines. These fall into
44 * three groups:
45 * 1. string searches (for /, ?, n, and N)
46 * 2. character searches within a single line (for f, F, t, T, etc)
47 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
48 */
49
50/*
51 * String searches
52 *
53 * The string search functions are divided into two levels:
54 * lowest: searchit(); uses an pos_T for starting position and found match.
55 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
56 *
57 * The last search pattern is remembered for repeating the same search.
58 * This pattern is shared between the :g, :s, ? and / commands.
59 * This is in search_regcomp().
60 *
61 * The actual string matching is done using a heavily modified version of
62 * Henry Spencer's regular expression library. See regexp.c.
63 */
64
Bram Moolenaar071d4272004-06-13 20:20:40 +000065/*
66 * Two search patterns are remembered: One for the :substitute command and
67 * one for other searches. last_idx points to the one that was used the last
68 * time.
69 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020070static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar63d9e732019-12-05 21:10:38 +010072 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat
73 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat
Bram Moolenaar071d4272004-06-13 20:20:40 +000074};
75
Bram Moolenaar63d9e732019-12-05 21:10:38 +010076static int last_idx = 0; // index in spats[] for RE_LAST
Bram Moolenaar071d4272004-06-13 20:20:40 +000077
Bram Moolenaar63d9e732019-12-05 21:10:38 +010078static char_u lastc[2] = {NUL, NUL}; // last character searched for
79static int lastcdir = FORWARD; // last direction of character search
80static int last_t_cmd = TRUE; // last search t_cmd
Bram Moolenaardbd24b52015-08-11 14:26:19 +020081static char_u lastc_bytes[MB_MAXBYTES + 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +010082static int lastc_bytelen = 1; // >1 for multi-byte char
Bram Moolenaardbd24b52015-08-11 14:26:19 +020083
Bram Moolenaar63d9e732019-12-05 21:10:38 +010084// copy of spats[], for keeping the search patterns while executing autocmds
Bram Moolenaarc3328162019-07-23 22:15:25 +020085static spat_T saved_spats[2];
Bram Moolenaara2cff1d2021-10-15 12:51:29 +010086static char_u *saved_mr_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000087# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +010088static int saved_spats_last_idx = 0;
89static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
Bram Moolenaara2cff1d2021-10-15 12:51:29 +010092// allocated copy of pattern used by search_regcomp()
93static char_u *mr_pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000094
95#ifdef FEAT_FIND_ID
96/*
97 * Type used by find_pattern_in_path() to remember which included files have
98 * been searched already.
99 */
100typedef struct SearchedFile
101{
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100102 FILE *fp; // File pointer
103 char_u *name; // Full name of file
104 linenr_T lnum; // Line we were up to in file
105 int matched; // Found a match in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106} SearchedFile;
107#endif
108
109/*
110 * translate search pattern for vim_regcomp()
111 *
112 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
113 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
114 * pat_save == RE_BOTH: save pat in both patterns (:global command)
115 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000116 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
118 * options & SEARCH_HIS: put search string in history
119 * options & SEARCH_KEEP: keep previous search pattern
120 *
121 * returns FAIL if failed, OK otherwise.
122 */
123 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100124search_regcomp(
125 char_u *pat,
Rob Pillinge86190e2022-12-23 19:06:04 +0000126 char_u **used_pat,
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100127 int pat_save,
128 int pat_use,
129 int options,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100130 regmmatch_T *regmatch) // return: pattern and ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131{
132 int magic;
133 int i;
134
135 rc_did_emsg = FALSE;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100136 magic = magic_isset();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
138 /*
139 * If no pattern given, use a previously defined pattern.
140 */
141 if (pat == NULL || *pat == NUL)
142 {
143 if (pat_use == RE_LAST)
144 i = last_idx;
145 else
146 i = pat_use;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100147 if (spats[i].pat == NULL) // pattern was never defined
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 {
149 if (pat_use == RE_SUBST)
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200150 emsg(_(e_no_previous_substitute_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200152 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 rc_did_emsg = TRUE;
154 return FAIL;
155 }
156 pat = spats[i].pat;
157 magic = spats[i].magic;
158 no_smartcase = spats[i].no_scs;
159 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100160 else if (options & SEARCH_HIS) // put new pattern in history
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
Rob Pillinge86190e2022-12-23 19:06:04 +0000163 if (used_pat)
Bram Moolenaarebfec1c2023-01-22 21:14:53 +0000164 *used_pat = pat;
Rob Pillinge86190e2022-12-23 19:06:04 +0000165
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100166 vim_free(mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167#ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100169 mr_pattern = reverse_text(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 else
171#endif
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100172 mr_pattern = vim_strsave(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 /*
175 * Save the currently used pattern in the appropriate place,
176 * unless the pattern should not be remembered.
177 */
Bram Moolenaare1004402020-10-24 20:49:43 +0200178 if (!(options & SEARCH_KEEP)
179 && (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100181 // search or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
183 save_re_pat(RE_SEARCH, pat, magic);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100184 // substitute or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
186 save_re_pat(RE_SUBST, pat, magic);
187 }
188
189 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000190 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
192 if (regmatch->regprog == NULL)
193 return FAIL;
194 return OK;
195}
196
197/*
198 * Get search pattern used by search_regcomp().
199 */
200 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100201get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
203 return mr_pattern;
204}
205
Bram Moolenaarabc97732007-08-08 20:49:37 +0000206#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207/*
208 * Reverse text into allocated memory.
209 * Returns the allocated string, NULL when out of memory.
210 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000211 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100212reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213{
214 unsigned len;
215 unsigned s_i, rev_i;
216 char_u *rev;
217
218 /*
219 * Reverse the pattern.
220 */
221 len = (unsigned)STRLEN(s);
222 rev = alloc(len + 1);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000223 if (rev == NULL)
224 return NULL;
225
226 rev_i = len;
227 for (s_i = 0; s_i < len; ++s_i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000229 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000231 int mb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000233 mb_len = (*mb_ptr2len)(s + s_i);
234 rev_i -= mb_len;
235 mch_memmove(rev + rev_i, s + s_i, mb_len);
236 s_i += mb_len - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000238 else
239 rev[--rev_i] = s[s_i];
240
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000242 rev[len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 return rev;
244}
245#endif
246
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100247 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100248save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000250 if (spats[idx].pat == pat)
251 return;
252
253 vim_free(spats[idx].pat);
254 spats[idx].pat = vim_strsave(pat);
255 spats[idx].magic = magic;
256 spats[idx].no_scs = no_smartcase;
257 last_idx = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000259 // If 'hlsearch' set and search pat changed: need redraw.
260 if (p_hls)
261 redraw_all_later(UPD_SOME_VALID);
262 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264}
265
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266/*
267 * Save the search patterns, so they can be restored later.
268 * Used before/after executing autocommands and user functions.
269 */
270static int save_level = 0;
271
272 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100273save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000275 if (save_level++ != 0)
276 return;
277
278 saved_spats[0] = spats[0];
279 if (spats[0].pat != NULL)
280 saved_spats[0].pat = vim_strsave(spats[0].pat);
281 saved_spats[1] = spats[1];
282 if (spats[1].pat != NULL)
283 saved_spats[1].pat = vim_strsave(spats[1].pat);
284 if (mr_pattern == NULL)
285 saved_mr_pattern = NULL;
286 else
287 saved_mr_pattern = vim_strsave(mr_pattern);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100288#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000289 saved_spats_last_idx = last_idx;
290 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292}
293
294 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100295restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296{
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000297 if (--save_level != 0)
298 return;
299
300 vim_free(spats[0].pat);
301 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100302#if defined(FEAT_EVAL)
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000303 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100304#endif
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000305 vim_free(spats[1].pat);
306 spats[1] = saved_spats[1];
307 vim_free(mr_pattern);
308 mr_pattern = saved_mr_pattern;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100309#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +0000310 last_idx = saved_spats_last_idx;
311 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000315#if defined(EXITFREE) || defined(PROTO)
316 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100317free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000318{
319 vim_free(spats[0].pat);
320 vim_free(spats[1].pat);
Bram Moolenaara2cff1d2021-10-15 12:51:29 +0100321 VIM_CLEAR(mr_pattern);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000322}
323#endif
324
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100325#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100326// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
327// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200328static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100329static int did_save_last_search_spat = 0;
330static int saved_last_idx = 0;
331static int saved_no_hlsearch = 0;
Christian Brabandt6dd74242022-02-14 12:44:32 +0000332static int saved_search_match_endcol;
333static int saved_search_match_lines;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100334
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100335/*
336 * Save and restore the search pattern for incremental highlight search
337 * feature.
338 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100339 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100340 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100341 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100342 */
343 void
344save_last_search_pattern(void)
345{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200346 if (++did_save_last_search_spat != 1)
347 // nested call, nothing to do
348 return;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100349
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100350 saved_last_search_spat = spats[RE_SEARCH];
351 if (spats[RE_SEARCH].pat != NULL)
352 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
353 saved_last_idx = last_idx;
354 saved_no_hlsearch = no_hlsearch;
355}
356
357 void
358restore_last_search_pattern(void)
359{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200360 if (--did_save_last_search_spat > 0)
361 // nested call, nothing to do
362 return;
363 if (did_save_last_search_spat != 0)
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100364 {
Bram Moolenaar442a8532020-06-04 20:56:09 +0200365 iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100366 return;
367 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100368
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100369 vim_free(spats[RE_SEARCH].pat);
370 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100371 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100372# if defined(FEAT_EVAL)
373 set_vv_searchforward();
374# endif
375 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200376 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100377}
Bram Moolenaard0480092017-11-16 22:20:39 +0100378
Christian Brabandt6dd74242022-02-14 12:44:32 +0000379/*
380 * Save and restore the incsearch highlighting variables.
381 * This is required so that calling searchcount() at does not invalidate the
382 * incsearch highlighting.
383 */
384 static void
385save_incsearch_state(void)
386{
387 saved_search_match_endcol = search_match_endcol;
388 saved_search_match_lines = search_match_lines;
389}
390
391 static void
392restore_incsearch_state(void)
393{
394 search_match_endcol = saved_search_match_endcol;
395 search_match_lines = saved_search_match_lines;
396}
397
Bram Moolenaard0480092017-11-16 22:20:39 +0100398 char_u *
399last_search_pattern(void)
400{
401 return spats[RE_SEARCH].pat;
402}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100403#endif
404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
406 * Return TRUE when case should be ignored for search pattern "pat".
407 * Uses the 'ignorecase' and 'smartcase' options.
408 */
409 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100410ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200412 return ignorecase_opt(pat, p_ic, p_scs);
413}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200415/*
416 * As ignorecase() put pass the "ic" and "scs" flags.
417 */
418 int
419ignorecase_opt(char_u *pat, int ic_in, int scs)
420{
421 int ic = ic_in;
422
423 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200424 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200425 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 no_smartcase = FALSE;
427
428 return ic;
429}
430
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200431/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200432 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200433 */
434 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100435pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200436{
437 char_u *p = pat;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200438 magic_T magic_val = MAGIC_ON;
439
440 // get the magicness of the pattern
441 (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val);
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200442
443 while (*p != NUL)
444 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200445 int l;
446
447 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
448 {
449 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
450 return TRUE;
451 p += l;
452 }
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200453 else if (*p == '\\' && magic_val <= MAGIC_ON)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200454 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100455 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200456 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100457 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200458 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100459 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200460 p += 2;
461 else
462 p += 1;
463 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200464 else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL)
465 {
466 if (p[1] != NUL) // skip "_X" and %X
467 p += 2;
Christian Brabandtbc67e5a2021-08-05 15:24:59 +0200468 else
469 p++;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200470 }
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200471 else if (MB_ISUPPER(*p))
472 return TRUE;
473 else
474 ++p;
475 }
476 return FALSE;
477}
478
Bram Moolenaar113e1072019-01-20 15:30:40 +0100479#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100481last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200482{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200483 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200484}
485
486 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100487last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200488{
489 return lastcdir == FORWARD;
490}
491
492 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100493last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200494{
495 return last_t_cmd == TRUE;
496}
497
498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100499set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200500{
501 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200502 lastc_bytelen = len;
503 if (len)
504 memcpy(lastc_bytes, s, len);
505 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200506 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200507}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100508#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200509
510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100511set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200512{
513 lastcdir = cdir;
514}
515
516 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100517set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200518{
519 last_t_cmd = t_cmd;
520}
521
522 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100523last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524{
525 return spats[last_idx].pat;
526}
527
528/*
529 * Reset search direction to forward. For "gd" and "gD" commands.
530 */
531 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100532reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#if defined(FEAT_EVAL)
536 set_vv_searchforward();
537#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538}
539
540#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
541/*
542 * Set the last search pattern. For ":let @/ =" and viminfo.
543 * Also set the saved search pattern, so that this works in an autocommand.
544 */
545 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100546set_last_search_pat(
547 char_u *s,
548 int idx,
549 int magic,
550 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551{
552 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100553 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (*s == NUL)
555 spats[idx].pat = NULL;
556 else
557 spats[idx].pat = vim_strsave(s);
558 spats[idx].magic = magic;
559 spats[idx].no_scs = FALSE;
560 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#if defined(FEAT_EVAL)
562 set_vv_searchforward();
563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 spats[idx].off.line = FALSE;
565 spats[idx].off.end = FALSE;
566 spats[idx].off.off = 0;
567 if (setlast)
568 last_idx = idx;
569 if (save_level)
570 {
571 vim_free(saved_spats[idx].pat);
572 saved_spats[idx] = spats[0];
573 if (spats[idx].pat == NULL)
574 saved_spats[idx].pat = NULL;
575 else
576 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100577# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100578 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 }
581# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100582 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100584 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585# endif
586}
587#endif
588
589#ifdef FEAT_SEARCH_EXTRA
590/*
591 * Get a regexp program for the last used search pattern.
592 * This is used for highlighting all matches in a window.
593 * Values returned in regmatch->regprog and regmatch->rmm_ic.
594 */
595 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100596last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 if (spats[last_idx].pat == NULL)
599 {
600 regmatch->regprog = NULL;
601 return;
602 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100603 ++emsg_off; // So it doesn't beep if bad expr
Rob Pillinge86190e2022-12-23 19:06:04 +0000604 (void)search_regcomp((char_u *)"", NULL, 0, last_idx, SEARCH_KEEP, regmatch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 --emsg_off;
606}
607#endif
608
609/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100610 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100611 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
612 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 *
614 * if (options & SEARCH_MSG) == 0 don't give any messages
615 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
616 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
617 * if (options & SEARCH_HIS) put search pattern in history
618 * if (options & SEARCH_END) return position at end of match
619 * if (options & SEARCH_START) accept match at pos itself
620 * if (options & SEARCH_KEEP) keep previous search pattern
621 * if (options & SEARCH_FOLD) match only once in a closed fold
622 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100623 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 *
625 * Return FAIL (zero) for failure, non-zero for success.
626 * When FEAT_EVAL is defined, returns the index of the first matching
627 * subpattern plus one; one if there was none.
628 */
629 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100630searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200631 win_T *win, // window to search in; can be NULL for a
632 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100633 buf_T *buf,
634 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100635 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100636 int dir,
637 char_u *pat,
638 long count,
639 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200640 int pat_use, // which pattern to use when "pat" is empty
641 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100644 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100645 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 regmmatch_T regmatch;
647 char_u *ptr;
648 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000650 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 int loop;
652 pos_T start_pos;
653 int at_first_line;
654 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200655 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 int match_ok;
657 long nmatched;
658 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100659 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100660 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661#ifdef FEAT_SEARCH_EXTRA
662 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200664 linenr_T stop_lnum = 0; // stop after this line number when != 0
Paul Ollis65745772022-06-05 16:55:54 +0100665 int unused_timeout_flag = FALSE;
666 int *timed_out = &unused_timeout_flag; // set when timed out.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Rob Pillinge86190e2022-12-23 19:06:04 +0000668 if (search_regcomp(pat, NULL, RE_SEARCH, pat_use,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
670 {
671 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarac78dd42022-01-02 19:25:26 +0000672 semsg(_(e_invalid_search_string_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 return FAIL;
674 }
675
Paul Ollis65745772022-06-05 16:55:54 +0100676 if (extra_arg != NULL)
677 {
678 stop_lnum = extra_arg->sa_stop_lnum;
679#ifdef FEAT_RELTIME
680 if (extra_arg->sa_tm > 0)
Paul Ollis65745772022-06-05 16:55:54 +0100681 init_regexp_timeout(extra_arg->sa_tm);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +0100682 // Also set the pointer when sa_tm is zero, the caller may have set the
683 // timeout.
684 timed_out = &extra_arg->sa_timed_out;
Paul Ollis65745772022-06-05 16:55:54 +0100685#endif
686 }
687
Bram Moolenaar280f1262006-01-30 00:14:18 +0000688 /*
689 * find the string
690 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100691 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100693 // When not accepting a match at the start position set "extra_col" to
694 // a non-zero value. Don't do that when starting at MAXCOL, since
695 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200696 if (pos->col == MAXCOL)
697 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100698 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200699 else if (has_mbyte
700 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
701 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100702 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100703 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100704 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200705 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100706 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100707 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100708 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100709 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200710 start_char_len = 1;
711 if (dir == FORWARD)
712 {
713 if (options & SEARCH_START)
714 extra_col = 0;
715 else
716 extra_col = start_char_len;
717 }
718 else
719 {
720 if (options & SEARCH_START)
721 extra_col = start_char_len;
722 else
723 extra_col = 0;
724 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100725
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100726 start_pos = *pos; // remember start pos for detecting no match
727 found = 0; // default: not found
728 at_first_line = TRUE; // default: start in first line
729 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
731 pos->lnum = 1;
732 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100733 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735
736 /*
737 * Start searching in current line, unless searching backwards and
738 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000739 * If we are searching backwards, in column 0, and not including the
740 * current position, gain some efficiency by skipping back a line.
741 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000743 if (dir == BACKWARD && start_pos.col == 0
744 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 lnum = pos->lnum - 1;
747 at_first_line = FALSE;
748 }
749 else
750 lnum = pos->lnum;
751
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100752 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 {
754 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
755 lnum += dir, at_first_line = FALSE)
756 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100757 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000758 if (stop_lnum != 0 && (dir == FORWARD
759 ? lnum > stop_lnum : lnum < stop_lnum))
760 break;
Paul Ollis65745772022-06-05 16:55:54 +0100761 // Stop after passing the time limit.
762 if (*timed_out)
Bram Moolenaar76929292008-01-06 19:07:36 +0000763 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000766 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100768 col = at_first_line && (options & SEARCH_COL) ? pos->col
769 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 nmatched = vim_regexec_multi(&regmatch, win, buf,
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100771 lnum, col, timed_out);
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200772 // vim_regexec_multi() may clear "regprog"
773 if (regmatch.regprog == NULL)
774 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100775 // Abort searching on an error (e.g., out of stack).
Paul Ollis65745772022-06-05 16:55:54 +0100776 if (called_emsg > called_emsg_before || *timed_out)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 break;
778 if (nmatched > 0)
779 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100780 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000781 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000783#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000785#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100786 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000787 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
788 ptr = (char_u *)"";
789 else
790 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792 /*
793 * Forward search in the first line: match should be after
794 * the start position. If not, continue at the end of the
795 * match (this is vi compatible) or on the next char.
796 */
797 if (dir == FORWARD && at_first_line)
798 {
799 match_ok = TRUE;
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000802 * When the match starts in a next line it's certainly
803 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 * When match lands on a NUL the cursor will be put
805 * one back afterwards, compare with that position,
806 * otherwise "/$" will get stuck on end of line.
807 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000808 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100809 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000810 ? (nmatched == 1
811 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000813 : ((int)matchpos.col
814 - (ptr[matchpos.col] == NUL)
815 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
817 /*
818 * If vi-compatible searching, continue at the end
819 * of the match, otherwise continue one position
820 * forward.
821 */
822 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
823 {
824 if (nmatched > 1)
825 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100826 // end is in next line, thus no match in
827 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 match_ok = FALSE;
829 break;
830 }
831 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100832 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000833 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 && ptr[matchcol] != NUL)
835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (has_mbyte)
837 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000838 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 ++matchcol;
841 }
842 }
843 else
844 {
Bram Moolenaarc96311b2022-11-25 21:13:47 +0000845 // Advance "matchcol" to the next character.
Bram Moolenaar837ca8f2022-11-26 18:59:19 +0000846 // This uses rmm_matchcol, the actual start of
847 // the match, ignoring "\zs".
848 matchcol = regmatch.rmm_matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (ptr[matchcol] != NUL)
850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000852 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 + matchcol);
854 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 ++matchcol;
856 }
857 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200858 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (ptr[matchcol] == NUL
861 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000862 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100863 matchcol, timed_out)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {
865 match_ok = FALSE;
866 break;
867 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200868 // vim_regexec_multi() may clear "regprog"
869 if (regmatch.regprog == NULL)
870 break;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000871 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 endpos = regmatch.endpos[0];
873# ifdef FEAT_EVAL
874 submatch = first_submatch(&regmatch);
875# endif
876
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100877 // Need to get the line pointer again, a
878 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000879 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 if (!match_ok)
882 continue;
883 }
884 if (dir == BACKWARD)
885 {
886 /*
887 * Now, if there are multiple matches on this line,
888 * we have to get the last one. Or the last one before
889 * the cursor, if we're on that line.
890 * When putting the new cursor at the end, compare
891 * relative to the end of the match.
892 */
893 match_ok = FALSE;
894 for (;;)
895 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100896 // Remember a position that is before the start
897 // position, we use it if it's the last match in
898 // the line. Always accept a position after
899 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000900 if (loop
901 || ((options & SEARCH_END)
902 ? (lnum + regmatch.endpos[0].lnum
903 < start_pos.lnum
904 || (lnum + regmatch.endpos[0].lnum
905 == start_pos.lnum
906 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200907 < (int)start_pos.col
908 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000909 : (lnum + regmatch.startpos[0].lnum
910 < start_pos.lnum
911 || (lnum + regmatch.startpos[0].lnum
912 == start_pos.lnum
913 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200914 < (int)start_pos.col
915 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000918 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 endpos = regmatch.endpos[0];
920# ifdef FEAT_EVAL
921 submatch = first_submatch(&regmatch);
922# endif
923 }
924 else
925 break;
926
927 /*
928 * We found a valid match, now check if there is
929 * another one after it.
930 * If vi-compatible searching, continue at the end
931 * of the match, otherwise continue one position
932 * forward.
933 */
934 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
935 {
936 if (nmatched > 1)
937 break;
938 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100939 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000940 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 && ptr[matchcol] != NUL)
942 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (has_mbyte)
944 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000945 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 ++matchcol;
948 }
949 }
950 else
951 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100952 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000953 if (matchpos.lnum > 0)
954 break;
955 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (ptr[matchcol] != NUL)
957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (has_mbyte)
959 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000960 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++matchcol;
963 }
964 }
965 if (ptr[matchcol] == NUL
966 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000967 win, buf, lnum + matchpos.lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100968 matchcol, timed_out)) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100969 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100970 // If the search timed out, we did find a match
971 // but it might be the wrong one, so that's not
972 // OK.
Paul Ollis65745772022-06-05 16:55:54 +0100973 if (*timed_out)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100974 match_ok = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100976 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200977 // vim_regexec_multi() may clear "regprog"
978 if (regmatch.regprog == NULL)
979 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100981 // Need to get the line pointer again, a
982 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000983 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
985
986 /*
987 * If there is only a match after the cursor, skip
988 * this match.
989 */
990 if (!match_ok)
991 continue;
992 }
993
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100994 // With the SEARCH_END option move to the last character
995 // of the match. Don't do it for an empty match, end
996 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200997 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000998 && !(matchpos.lnum == endpos.lnum
999 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001001 // For a match in the first column, set the position
1002 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001003 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001004 pos->col = endpos.col;
1005 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001006 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001007 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001008 {
1009 --pos->lnum;
1010 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1011 pos->lnum, FALSE));
1012 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001013 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001014 else
1015 {
1016 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001017 if (has_mbyte
1018 && pos->lnum <= buf->b_ml.ml_line_count)
1019 {
1020 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1021 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1022 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001023 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001024 if (end_pos != NULL)
1025 {
1026 end_pos->lnum = lnum + matchpos.lnum;
1027 end_pos->col = matchpos.col;
1028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 }
1030 else
1031 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001032 pos->lnum = lnum + matchpos.lnum;
1033 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001034 if (end_pos != NULL)
1035 {
1036 end_pos->lnum = lnum + endpos.lnum;
1037 end_pos->col = endpos.col;
1038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001041 if (end_pos != NULL)
1042 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001044 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001046 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001047 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 search_match_endcol = endpos.col;
1049 break;
1050 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001051 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (got_int)
1053 break;
1054
1055#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001056 // Cancel searching if a character was typed. Used for
1057 // 'incsearch'. Don't check too often, that would slowdown
1058 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if ((options & SEARCH_PEEK)
1060 && ((lnum - pos->lnum) & 0x3f) == 0
1061 && char_avail())
1062 {
1063 break_loop = TRUE;
1064 break;
1065 }
1066#endif
1067
1068 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001069 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
1071 at_first_line = FALSE;
1072
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001073 // vim_regexec_multi() may clear "regprog"
1074 if (regmatch.regprog == NULL)
1075 break;
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001078 * Stop the search if wrapscan isn't set, "stop_lnum" is
1079 * specified, after an interrupt, after a match and after looping
1080 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001082 if (!p_ws || stop_lnum != 0 || got_int
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001083 || called_emsg > called_emsg_before || *timed_out
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001084#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001085 || break_loop
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001086#endif
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001087 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 break;
1089
1090 /*
1091 * If 'wrapscan' is set we continue at the other end of the file.
Christian Brabandt34a6a362023-05-06 19:20:20 +01001092 * If 'shortmess' does not contain 's', we give a message, but
1093 * only, if we won't show the search stat later anyhow,
1094 * (so SEARCH_COUNT must be absent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 * This message is also remembered in keep_msg for when the screen
1096 * is redrawn. The keep_msg is cleared whenever another message is
1097 * written.
1098 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001099 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 lnum = 1;
Christian Brabandt34a6a362023-05-06 19:20:20 +01001103 if (!shortmess(SHM_SEARCH)
1104 && shortmess(SHM_SEARCHCOUNT)
1105 && (options & SEARCH_MSG))
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001106 give_warning((char_u *)_(dir == BACKWARD
1107 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001108 if (extra_arg != NULL)
1109 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
Paul Ollis65745772022-06-05 16:55:54 +01001111 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001112#ifdef FEAT_SEARCH_EXTRA
1113 || break_loop
1114#endif
1115 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 break;
1117 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001118 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001120#ifdef FEAT_RELTIME
1121 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1122 disable_regexp_timeout();
1123#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001124 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001126 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
1128 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001129 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1131 {
1132 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001133 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001135 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001137 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 return FAIL;
1140 }
1141
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001142 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001143 if (pos->lnum > buf->b_ml.ml_line_count)
1144 {
1145 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001146 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001147 if (pos->col > 0)
1148 --pos->col;
1149 }
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 return submatch + 1;
1152}
1153
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001154#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001155 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001156set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001157{
1158 spats[0].off.dir = cdir;
1159}
1160
1161 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001162set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001163{
1164 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1165}
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167/*
1168 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001169 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 */
1171 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001172first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173{
1174 int submatch;
1175
1176 for (submatch = 1; ; ++submatch)
1177 {
1178 if (rp->startpos[submatch].lnum >= 0)
1179 break;
1180 if (submatch == 9)
1181 {
1182 submatch = 0;
1183 break;
1184 }
1185 }
1186 return submatch;
1187}
1188#endif
1189
1190/*
1191 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001192 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 * If 'dirc' is 0: use previous dir.
1194 * If 'pat' is NULL or empty : use previous string.
1195 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1196 * If 'options & SEARCH_ECHO': echo the search command and handle options
1197 * If 'options & SEARCH_MSG' : may give error message
1198 * If 'options & SEARCH_OPT' : interpret optional flags
1199 * If 'options & SEARCH_HIS' : put search pattern in history
1200 * If 'options & SEARCH_NOOF': don't add offset to position
1201 * If 'options & SEARCH_MARK': set previous context mark
1202 * If 'options & SEARCH_KEEP': keep previous search pattern
1203 * If 'options & SEARCH_START': accept match at curpos itself
1204 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1205 *
1206 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1207 * makes the movement linewise without moving the match position.
1208 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001209 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
1211 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001212do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001213 oparg_T *oap, // can be NULL
1214 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001215 int search_delim, // the delimiter for the search, e.g. '%' in
1216 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001217 char_u *pat,
1218 long count,
1219 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001220 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001222 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001224 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001225 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 char_u *p;
1227 long c;
1228 char_u *dircp;
1229 char_u *strcopy = NULL;
1230 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001231 char_u *msgbuf = NULL;
1232 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001233 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
1235 /*
1236 * A line offset is not remembered, this is vi compatible.
1237 */
1238 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1239 {
1240 spats[0].off.line = FALSE;
1241 spats[0].off.off = 0;
1242 }
1243
1244 /*
1245 * Save the values for when (options & SEARCH_KEEP) is used.
1246 * (there is no "if ()" around this because gcc wants them initialized)
1247 */
1248 old_off = spats[0].off;
1249
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001250 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 /*
1253 * Find out the direction of the search.
1254 */
1255 if (dirc == 0)
1256 dirc = spats[0].off.dir;
1257 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001260#if defined(FEAT_EVAL)
1261 set_vv_searchforward();
1262#endif
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 if (options & SEARCH_REV)
1265 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001266#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001267 // There is a bug in the Visual C++ 2.2 compiler which means that
1268 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 dirc = (dirc == '/') ? '?' : '/';
1270#else
1271 if (dirc == '/')
1272 dirc = '?';
1273 else
1274 dirc = '/';
1275#endif
1276 }
1277
1278#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001279 // If the cursor is in a closed fold, don't find another match in the same
1280 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (dirc == '/')
1282 {
1283 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001284 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 else
1287 {
1288 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1289 pos.col = 0;
1290 }
1291#endif
1292
1293#ifdef FEAT_SEARCH_EXTRA
1294 /*
1295 * Turn 'hlsearch' highlighting back on.
1296 */
1297 if (no_hlsearch && !(options & SEARCH_KEEP))
1298 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001299 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001300 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
1302#endif
1303
1304 /*
1305 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1306 */
1307 for (;;)
1308 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001309 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 searchstr = pat;
1312 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001313 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001314 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001316 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001318 searchstr = spats[RE_SUBST].pat;
1319 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001320 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001321 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001322 retval = 0;
1323 goto end_do_search;
1324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001326 else
1327 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001328 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001329 searchstr = (char_u *)"";
1330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001333 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
1335 /*
1336 * Find end of regular expression.
1337 * If there is a matching '/' or '?', toss it.
1338 */
1339 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001340 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001341 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 if (strcopy != ps)
1343 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001344 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001345 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 pat = strcopy;
1347 searchstr = strcopy;
1348 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001349 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001351 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 *p++ = NUL;
1353 }
1354 spats[0].off.line = FALSE;
1355 spats[0].off.end = FALSE;
1356 spats[0].off.off = 0;
1357 /*
1358 * Check for a line offset or a character offset.
1359 * For get_address (echo off) we don't check for a character
1360 * offset, because it is meaningless and the 's' could be a
1361 * substitute command.
1362 */
1363 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1364 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001365 else if ((options & SEARCH_OPT)
1366 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001368 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 spats[0].off.end = SEARCH_END;
1370 ++p;
1371 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001372 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001374 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1376 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001377 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001379 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 spats[0].off.off = 1;
1381 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001382 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 ++p;
1384 }
1385
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001386 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 searchcmdlen += (int)(p - pat);
1388
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001389 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001392 if ((options & SEARCH_ECHO) && messaging()
1393 && !msg_silent
1394 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001397 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001398 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001400 // Compute msg_row early.
1401 msg_start();
1402
Bram Moolenaar984f0312019-05-24 13:11:47 +02001403 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001404 if (!cmd_silent &&
1405 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001406 {
1407 p = off_buf;
1408 *p++ = dirc;
1409 if (spats[0].off.end)
1410 *p++ = 'e';
1411 else if (!spats[0].off.line)
1412 *p++ = 's';
1413 if (spats[0].off.off > 0 || spats[0].off.line)
1414 *p++ = '+';
1415 *p = NUL;
1416 if (spats[0].off.off != 0 || spats[0].off.line)
1417 sprintf((char *)p, "%ld", spats[0].off.off);
1418 off_len = STRLEN(off_buf);
1419 }
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001422 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 else
1424 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001425
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001426 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001427 {
1428 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001429 // search stat. Use all the space available, so that the
1430 // search state is right aligned. If there is not enough space
1431 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001432 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001433 // Use all the columns.
1434 len = (int)(Rows - msg_row) * Columns - 1;
1435 else
1436 // Use up to 'showcmd' column.
1437 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001438 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1439 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001440 }
1441 else
1442 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001443 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001444
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001445 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001446 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (msgbuf != NULL)
1448 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001449 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001450 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001451 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1452 // empty for the search_stat feature.
1453 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001454 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001455 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001457 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1458 {
1459 // Use a space to draw the composing char on.
1460 msgbuf[1] = ' ';
1461 mch_memmove(msgbuf + 2, p, STRLEN(p));
1462 }
1463 else
1464 mch_memmove(msgbuf + 1, p, STRLEN(p));
1465 if (off_len > 0)
1466 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001468 trunc = msg_strtrunc(msgbuf, TRUE);
1469 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001471 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001472 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001475#ifdef FEAT_RIGHTLEFT
1476 // The search pattern could be shown on the right in
1477 // rightleft mode, but the 'ruler' and 'showcmd' area use
1478 // it too, thus it would be blanked out again very soon.
1479 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001480 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1481 {
1482 char_u *r;
1483 size_t pat_len;
1484
1485 r = reverse_text(msgbuf);
1486 if (r != NULL)
1487 {
1488 vim_free(msgbuf);
1489 msgbuf = r;
1490 // move reversed text to beginning of buffer
1491 while (*r != NUL && *r == ' ')
1492 r++;
1493 pat_len = msgbuf + STRLEN(msgbuf) - r;
1494 mch_memmove(msgbuf, r, pat_len);
1495 // overwrite old text
1496 if ((size_t)(r - msgbuf) >= pat_len)
1497 vim_memset(r, ' ', pat_len);
1498 else
1499 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1500 }
1501 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001502#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001503 msg_outtrans(msgbuf);
1504 msg_clr_eos();
1505 msg_check();
1506
1507 gotocmdline(FALSE);
1508 out_flush();
1509 msg_nowait = TRUE; // don't wait for this message
1510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 }
1513
1514 /*
1515 * If there is a character offset, subtract it from the current
1516 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001517 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 * This is not done for a line offset, because then we would not be vi
1519 * compatible.
1520 */
Bram Moolenaared203462004-06-16 11:19:22 +00001521 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 if (spats[0].off.off > 0)
1524 {
1525 for (c = spats[0].off.off; c; --c)
1526 if (decl(&pos) == -1)
1527 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001528 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001530 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 pos.col = MAXCOL;
1532 }
1533 }
1534 else
1535 {
1536 for (c = spats[0].off.off; c; ++c)
1537 if (incl(&pos) == -1)
1538 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001539 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 {
1541 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1542 pos.col = 0;
1543 }
1544 }
1545 }
1546
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001547 /*
1548 * The actual search.
1549 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001550 c = searchit(curwin, curbuf, &pos, NULL,
1551 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 searchstr, count, spats[0].off.end + (options &
1553 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1554 + SEARCH_MSG + SEARCH_START
1555 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001556 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
1558 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001559 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001560
1561 if (!shortmess(SHM_SEARCH)
1562 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1563 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001564 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001565
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 if (c == FAIL)
1567 {
1568 retval = 0;
1569 goto end_do_search;
1570 }
1571 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001572 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001574 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576 /*
1577 * Add character and/or line offset
1578 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001579 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001581 pos_T org_pos = pos;
1582
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001583 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 c = pos.lnum + spats[0].off.off;
1586 if (c < 1)
1587 pos.lnum = 1;
1588 else if (c > curbuf->b_ml.ml_line_count)
1589 pos.lnum = curbuf->b_ml.ml_line_count;
1590 else
1591 pos.lnum = c;
1592 pos.col = 0;
1593
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001594 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001596 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001598 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001599 c = spats[0].off.off;
1600 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (incl(&pos) == -1)
1604 break;
1605 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001606 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 else
1608 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001609 while (c++ < 0)
1610 if (decl(&pos) == -1)
1611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001614 if (!EQUAL_POS(pos, org_pos))
1615 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001618 // Show [1/15] if 'S' is not in 'shortmess'.
1619 if ((options & SEARCH_ECHO)
1620 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001621 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001622 && c != FAIL
1623 && !shortmess(SHM_SEARCHCOUNT)
1624 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001625 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1626 show_top_bot_msg, msgbuf,
1627 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001628#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001629 || (!(fdo_flags & FDO_SEARCH)
1630 && hasFolding(curwin->w_cursor.lnum,
1631 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001632#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001633 ),
1634 SEARCH_STAT_DEF_MAX_COUNT,
1635 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001636
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 /*
1638 * The search command can be followed by a ';' to do another search.
1639 * For example: "/pat/;/foo/+3;?bar"
1640 * This is like doing another search command, except:
1641 * - The remembered direction '/' or '?' is from the first search.
1642 * - When an error happens the cursor isn't moved at all.
1643 * Don't do this when called by get_address() (it handles ';' itself).
1644 */
1645 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1646 break;
1647
1648 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001649 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (dirc != '?' && dirc != '/')
1651 {
1652 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001653 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 goto end_do_search;
1655 }
1656 ++pat;
1657 }
1658
1659 if (options & SEARCH_MARK)
1660 setpcmark();
1661 curwin->w_cursor = pos;
1662 curwin->w_set_curswant = TRUE;
1663
1664end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001665 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 spats[0].off = old_off;
1667 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001668 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 return retval;
1671}
1672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673/*
1674 * search_for_exact_line(buf, pos, dir, pat)
1675 *
1676 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001677 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001679 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 * Return OK for success, or FAIL if no line found.
1681 */
1682 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001683search_for_exact_line(
1684 buf_T *buf,
1685 pos_T *pos,
1686 int dir,
1687 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 linenr_T start = 0;
1690 char_u *ptr;
1691 char_u *p;
1692
1693 if (buf->b_ml.ml_line_count == 0)
1694 return FAIL;
1695 for (;;)
1696 {
1697 pos->lnum += dir;
1698 if (pos->lnum < 1)
1699 {
1700 if (p_ws)
1701 {
1702 pos->lnum = buf->b_ml.ml_line_count;
1703 if (!shortmess(SHM_SEARCH))
1704 give_warning((char_u *)_(top_bot_msg), TRUE);
1705 }
1706 else
1707 {
1708 pos->lnum = 1;
1709 break;
1710 }
1711 }
1712 else if (pos->lnum > buf->b_ml.ml_line_count)
1713 {
1714 if (p_ws)
1715 {
1716 pos->lnum = 1;
1717 if (!shortmess(SHM_SEARCH))
1718 give_warning((char_u *)_(bot_top_msg), TRUE);
1719 }
1720 else
1721 {
1722 pos->lnum = 1;
1723 break;
1724 }
1725 }
1726 if (pos->lnum == start)
1727 break;
1728 if (start == 0)
1729 start = pos->lnum;
1730 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1731 p = skipwhite(ptr);
1732 pos->col = (colnr_T) (p - ptr);
1733
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001734 // when adding lines the matching line may be empty but it is not
1735 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001736 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 {
1738 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1739 return OK;
1740 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001741 else if (*p != NUL) // ignore empty lines
1742 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001743 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1744 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 return OK;
1746 }
1747 }
1748 return FAIL;
1749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751/*
1752 * Character Searches
1753 */
1754
1755/*
1756 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1757 * position of the character, otherwise move to just before the char.
1758 * Do this "cap->count1" times.
1759 * Return FAIL or OK.
1760 */
1761 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001762searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001764 int c = cap->nchar; // char to search for
1765 int dir = cap->arg; // TRUE for searching forward
1766 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 int col;
1768 char_u *p;
1769 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001770 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001772 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001774 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001776 *lastc = c;
1777 set_csearch_direction(dir);
1778 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001779 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (cap->ncharC1 != 0)
1781 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001782 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1783 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001785 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1786 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 }
1789 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001790 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001792 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001794 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 dir = -lastcdir;
1796 else
1797 dir = lastcdir;
1798 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001799 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001800 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001801
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001802 // Force a move of at least one char, so ";" and "," will move the
1803 // cursor, even if the cursor is right in front of char we are looking
1804 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001805 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001806 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001809 if (dir == BACKWARD)
1810 cap->oap->inclusive = FALSE;
1811 else
1812 cap->oap->inclusive = TRUE;
1813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 p = ml_get_curline();
1815 col = curwin->w_cursor.col;
1816 len = (int)STRLEN(p);
1817
1818 while (count--)
1819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (has_mbyte)
1821 {
1822 for (;;)
1823 {
1824 if (dir > 0)
1825 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001826 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (col >= len)
1828 return FAIL;
1829 }
1830 else
1831 {
1832 if (col == 0)
1833 return FAIL;
1834 col -= (*mb_head_off)(p, p + col - 1) + 1;
1835 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001836 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001838 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 break;
1840 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001841 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001842 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001843 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001844 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 }
1846 }
1847 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 {
1849 for (;;)
1850 {
1851 if ((col += dir) < 0 || col >= len)
1852 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001853 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001855 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 }
1857 }
1858 }
1859
1860 if (t_cmd)
1861 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001862 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (has_mbyte)
1865 {
1866 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001867 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001868 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001870 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 col -= (*mb_head_off)(p, p + col);
1872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 curwin->w_cursor.col = col;
1875
1876 return OK;
1877}
1878
1879/*
1880 * "Other" Searches
1881 */
1882
1883/*
1884 * findmatch - find the matching paren or brace
1885 *
1886 * Improvement over vi: Braces inside quotes are ignored.
1887 */
1888 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001889findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890{
1891 return findmatchlimit(oap, initc, 0, 0);
1892}
1893
1894/*
1895 * Return TRUE if the character before "linep[col]" equals "ch".
1896 * Return FALSE if "col" is zero.
1897 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1898 * is NULL.
1899 * Handles multibyte string correctly.
1900 */
1901 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001902check_prevcol(
1903 char_u *linep,
1904 int col,
1905 int ch,
1906 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907{
1908 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (col > 0 && has_mbyte)
1910 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (prevcol)
1912 *prevcol = col;
1913 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1914}
1915
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001916/*
1917 * Raw string start is found at linep[startpos.col - 1].
1918 * Return TRUE if the matching end can be found between startpos and endpos.
1919 */
1920 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001921find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001922{
1923 char_u *p;
1924 char_u *delim_copy;
1925 size_t delim_len;
1926 linenr_T lnum;
1927 int found = FALSE;
1928
1929 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1930 ;
1931 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001932 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001933 if (delim_copy == NULL)
1934 return FALSE;
1935 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1936 {
1937 char_u *line = ml_get(lnum);
1938
1939 for (p = line + (lnum == startpos->lnum
1940 ? startpos->col + 1 : 0); *p; ++p)
1941 {
1942 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1943 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02001944 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
1945 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001946 {
1947 found = TRUE;
1948 break;
1949 }
1950 }
1951 if (found)
1952 break;
1953 }
1954 vim_free(delim_copy);
1955 return found;
1956}
1957
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001959 * Check matchpairs option for "*initc".
1960 * If there is a match set "*initc" to the matching character and "*findc" to
1961 * the opposite character. Set "*backwards" to the direction.
1962 * When "switchit" is TRUE swap the direction.
1963 */
1964 static void
1965find_mps_values(
1966 int *initc,
1967 int *findc,
1968 int *backwards,
1969 int switchit)
1970{
1971 char_u *ptr;
1972
1973 ptr = curbuf->b_p_mps;
1974 while (*ptr != NUL)
1975 {
1976 if (has_mbyte)
1977 {
1978 char_u *prev;
1979
1980 if (mb_ptr2char(ptr) == *initc)
1981 {
1982 if (switchit)
1983 {
1984 *findc = *initc;
1985 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1986 *backwards = TRUE;
1987 }
1988 else
1989 {
1990 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1991 *backwards = FALSE;
1992 }
1993 return;
1994 }
1995 prev = ptr;
1996 ptr += mb_ptr2len(ptr) + 1;
1997 if (mb_ptr2char(ptr) == *initc)
1998 {
1999 if (switchit)
2000 {
2001 *findc = *initc;
2002 *initc = mb_ptr2char(prev);
2003 *backwards = FALSE;
2004 }
2005 else
2006 {
2007 *findc = mb_ptr2char(prev);
2008 *backwards = TRUE;
2009 }
2010 return;
2011 }
2012 ptr += mb_ptr2len(ptr);
2013 }
2014 else
2015 {
2016 if (*ptr == *initc)
2017 {
2018 if (switchit)
2019 {
2020 *backwards = TRUE;
2021 *findc = *initc;
2022 *initc = ptr[2];
2023 }
2024 else
2025 {
2026 *backwards = FALSE;
2027 *findc = ptr[2];
2028 }
2029 return;
2030 }
2031 ptr += 2;
2032 if (*ptr == *initc)
2033 {
2034 if (switchit)
2035 {
2036 *backwards = FALSE;
2037 *findc = *initc;
2038 *initc = ptr[-2];
2039 }
2040 else
2041 {
2042 *backwards = TRUE;
2043 *findc = ptr[-2];
2044 }
2045 return;
2046 }
2047 ++ptr;
2048 }
2049 if (*ptr == ',')
2050 ++ptr;
2051 }
2052}
2053
2054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002056 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2057 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 *
2059 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002060 * character at or after the cursor. Special values:
2061 * '*' look for C-style comment / *
2062 * '/' look for C-style comment / *, ignoring comment-end
2063 * '#' look for preprocessor directives
2064 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 *
2066 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2067 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2068 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2069 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002070 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002071 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002072 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002075findmatchlimit(
2076 oparg_T *oap,
2077 int initc,
2078 int flags,
2079 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002081 static pos_T pos; // current search position
2082 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002084 int count = 0; // cumulative number of braces
2085 int backwards = FALSE; // init for gcc
2086 int raw_string = FALSE; // search for raw string
2087 int inquote = FALSE; // TRUE when inside quotes
2088 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002090 int do_quotes; // check for quotes in current line
2091 int at_start; // do_quotes value at start position
2092 int hash_dir = 0; // Direction searched for # things
2093 int comment_dir = 0; // Direction searched for comments
2094 pos_T match_pos; // Where last slash-star was found
2095 int start_in_quotes; // start position is in quotes
2096 int traveled = 0; // how far we've searched so far
2097 int ignore_cend = FALSE; // ignore comment end
2098 int cpo_match; // vi compatible matching
2099 int cpo_bsl; // don't recognize backslashes
2100 int match_escaped = 0; // search for escaped match
2101 int dir; // Direction to search
2102 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002103 int lispcomm = FALSE; // inside of Lisp-style comment
2104 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002107 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 linep = ml_get(pos.lnum);
2109
2110 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2111 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2112
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002113 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if (flags & FM_BACKWARD)
2115 dir = BACKWARD;
2116 else if (flags & FM_FORWARD)
2117 dir = FORWARD;
2118 else
2119 dir = 0;
2120
2121 /*
2122 * if initc given, look in the table for the matching character
2123 * '/' and '*' are special cases: look for start or end of comment.
2124 * When '/' is used, we ignore running backwards into an star-slash, for
2125 * "[*" command, we just want to find any comment.
2126 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002127 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
2129 comment_dir = dir;
2130 if (initc == '/')
2131 ignore_cend = TRUE;
2132 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002133 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 initc = NUL;
2135 }
2136 else if (initc != '#' && initc != NUL)
2137 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002138 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002139 if (dir)
2140 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002141 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 return NULL;
2143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 else
2145 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002146 /*
2147 * Either initc is '#', or no initc was given and we need to look
2148 * under the cursor.
2149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if (initc == '#')
2151 {
2152 hash_dir = dir;
2153 }
2154 else
2155 {
2156 /*
2157 * initc was not given, must look for something to match under
2158 * or near the cursor.
2159 * Only check for special things when 'cpo' doesn't have '%'.
2160 */
2161 if (!cpo_match)
2162 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002163 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 ptr = skipwhite(linep);
2165 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2166 {
2167 ptr = skipwhite(ptr + 1);
2168 if ( STRNCMP(ptr, "if", 2) == 0
2169 || STRNCMP(ptr, "endif", 5) == 0
2170 || STRNCMP(ptr, "el", 2) == 0)
2171 hash_dir = 1;
2172 }
2173
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002174 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 else if (linep[pos.col] == '/')
2176 {
2177 if (linep[pos.col + 1] == '*')
2178 {
2179 comment_dir = FORWARD;
2180 backwards = FALSE;
2181 pos.col++;
2182 }
2183 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2184 {
2185 comment_dir = BACKWARD;
2186 backwards = TRUE;
2187 pos.col--;
2188 }
2189 }
2190 else if (linep[pos.col] == '*')
2191 {
2192 if (linep[pos.col + 1] == '/')
2193 {
2194 comment_dir = BACKWARD;
2195 backwards = TRUE;
2196 }
2197 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2198 {
2199 comment_dir = FORWARD;
2200 backwards = FALSE;
2201 }
2202 }
2203 }
2204
2205 /*
2206 * If we are not on a comment or the # at the start of a line, then
2207 * look for brace anywhere on this line after the cursor.
2208 */
2209 if (!hash_dir && !comment_dir)
2210 {
2211 /*
2212 * Find the brace under or after the cursor.
2213 * If beyond the end of the line, use the last character in
2214 * the line.
2215 */
2216 if (linep[pos.col] == NUL && pos.col)
2217 --pos.col;
2218 for (;;)
2219 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002220 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (initc == NUL)
2222 break;
2223
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002224 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 if (findc)
2226 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002227 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229 if (!findc)
2230 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002231 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 if (!cpo_match && *skipwhite(linep) == '#')
2233 hash_dir = 1;
2234 else
2235 return NULL;
2236 }
2237 else if (!cpo_bsl)
2238 {
2239 int col, bslcnt = 0;
2240
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002241 // Set "match_escaped" if there are an odd number of
2242 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2244 bslcnt++;
2245 match_escaped = (bslcnt & 1);
2246 }
2247 }
2248 }
2249 if (hash_dir)
2250 {
2251 /*
2252 * Look for matching #if, #else, #elif, or #endif
2253 */
2254 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002255 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (initc != '#')
2257 {
2258 ptr = skipwhite(skipwhite(linep) + 1);
2259 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2260 hash_dir = 1;
2261 else if (STRNCMP(ptr, "endif", 5) == 0)
2262 hash_dir = -1;
2263 else
2264 return NULL;
2265 }
2266 pos.col = 0;
2267 while (!got_int)
2268 {
2269 if (hash_dir > 0)
2270 {
2271 if (pos.lnum == curbuf->b_ml.ml_line_count)
2272 break;
2273 }
2274 else if (pos.lnum == 1)
2275 break;
2276 pos.lnum += hash_dir;
2277 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002278 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 ptr = skipwhite(linep);
2280 if (*ptr != '#')
2281 continue;
2282 pos.col = (colnr_T) (ptr - linep);
2283 ptr = skipwhite(ptr + 1);
2284 if (hash_dir > 0)
2285 {
2286 if (STRNCMP(ptr, "if", 2) == 0)
2287 count++;
2288 else if (STRNCMP(ptr, "el", 2) == 0)
2289 {
2290 if (count == 0)
2291 return &pos;
2292 }
2293 else if (STRNCMP(ptr, "endif", 5) == 0)
2294 {
2295 if (count == 0)
2296 return &pos;
2297 count--;
2298 }
2299 }
2300 else
2301 {
2302 if (STRNCMP(ptr, "if", 2) == 0)
2303 {
2304 if (count == 0)
2305 return &pos;
2306 count--;
2307 }
2308 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2309 {
2310 if (count == 0)
2311 return &pos;
2312 }
2313 else if (STRNCMP(ptr, "endif", 5) == 0)
2314 count++;
2315 }
2316 }
2317 return NULL;
2318 }
2319 }
2320
2321#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002322 // This is just guessing: when 'rightleft' is set, search for a matching
2323 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2325 backwards = !backwards;
2326#endif
2327
2328 do_quotes = -1;
2329 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002330 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002331
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002332 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002333 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002335 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002336 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002337
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 while (!got_int)
2339 {
2340 /*
2341 * Go to the next position, forward or backward. We could use
2342 * inc() and dec() here, but that is much slower
2343 */
2344 if (backwards)
2345 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002346 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002347 if (lispcomm && pos.col < (colnr_T)comment_col)
2348 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002349 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002351 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 break;
2353 --pos.lnum;
2354
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002355 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 break;
2357
2358 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002359 pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 do_quotes = -1;
2361 line_breakcheck();
2362
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002363 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002364 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002366 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002367 if (lisp && comment_col != MAXCOL)
2368 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 else
2371 {
2372 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (has_mbyte)
2374 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002377 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002379 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002380 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002381 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002382 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002383 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002385 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002386 // line is exhausted and comment with it,
2387 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002388 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 break;
2390 ++pos.lnum;
2391
2392 if (maxtravel && traveled++ > maxtravel)
2393 break;
2394
2395 linep = ml_get(pos.lnum);
2396 pos.col = 0;
2397 do_quotes = -1;
2398 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002399 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002400 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 else
2403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002405 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 ++pos.col;
2408 }
2409 }
2410
2411 /*
2412 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2413 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002414 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2415 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002417 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002419 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
2421
2422 if (comment_dir)
2423 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002424 // Note: comments do not nest, and we ignore quotes in them
2425 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (comment_dir == FORWARD)
2427 {
2428 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2429 {
2430 pos.col++;
2431 return &pos;
2432 }
2433 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002434 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 /*
2437 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002438 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
2440 if (pos.col == 0)
2441 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002442 else if (raw_string)
2443 {
2444 if (linep[pos.col - 1] == 'R'
2445 && linep[pos.col] == '"'
2446 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2447 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002448 // Possible start of raw string. Now that we have the
2449 // delimiter we can check if it ends before where we
2450 // started searching, or before the previously found
2451 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002452 if (!find_rawstring_end(linep, &pos,
2453 count > 0 ? &match_pos : &curwin->w_cursor))
2454 {
2455 count++;
2456 match_pos = pos;
2457 match_pos.col--;
2458 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002459 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002460 }
2461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 else if ( linep[pos.col - 1] == '/'
2463 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002464 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 && (int)pos.col < comment_col)
2466 {
2467 count++;
2468 match_pos = pos;
2469 match_pos.col--;
2470 }
2471 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2472 {
2473 if (count > 0)
2474 pos = match_pos;
2475 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2476 && (int)pos.col <= comment_col)
2477 pos.col -= 2;
2478 else if (ignore_cend)
2479 continue;
2480 else
2481 return NULL;
2482 return &pos;
2483 }
2484 }
2485 continue;
2486 }
2487
2488 /*
2489 * If smart matching ('cpoptions' does not contain '%'), braces inside
2490 * of quotes are ignored, but only if there is an even number of
2491 * quotes in the line.
2492 */
2493 if (cpo_match)
2494 do_quotes = 0;
2495 else if (do_quotes == -1)
2496 {
2497 /*
2498 * Count the number of quotes in the line, skipping \" and '"'.
2499 * Watch out for "\\".
2500 */
2501 at_start = do_quotes;
2502 for (ptr = linep; *ptr; ++ptr)
2503 {
2504 if (ptr == linep + pos.col + backwards)
2505 at_start = (do_quotes & 1);
2506 if (*ptr == '"'
2507 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2508 ++do_quotes;
2509 if (*ptr == '\\' && ptr[1] != NUL)
2510 ++ptr;
2511 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002512 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
2514 /*
2515 * If we find an uneven count, check current line and previous
2516 * one for a '\' at the end.
2517 */
2518 if (!do_quotes)
2519 {
2520 inquote = FALSE;
2521 if (ptr[-1] == '\\')
2522 {
2523 do_quotes = 1;
2524 if (start_in_quotes == MAYBE)
2525 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002526 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 inquote = TRUE;
2528 start_in_quotes = TRUE;
2529 }
2530 else if (backwards)
2531 inquote = TRUE;
2532 }
2533 if (pos.lnum > 1)
2534 {
2535 ptr = ml_get(pos.lnum - 1);
2536 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2537 {
2538 do_quotes = 1;
2539 if (start_in_quotes == MAYBE)
2540 {
2541 inquote = at_start;
2542 if (inquote)
2543 start_in_quotes = TRUE;
2544 }
2545 else if (!backwards)
2546 inquote = TRUE;
2547 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002548
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002549 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002550 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 }
2552 }
2553 }
2554 if (start_in_quotes == MAYBE)
2555 start_in_quotes = FALSE;
2556
2557 /*
2558 * If 'smartmatch' is set:
2559 * Things inside quotes are ignored by setting 'inquote'. If we
2560 * find a quote without a preceding '\' invert 'inquote'. At the
2561 * end of a line not ending in '\' we reset 'inquote'.
2562 *
2563 * In lines with an uneven number of quotes (without preceding '\')
2564 * we do not know which part to ignore. Therefore we only set
2565 * inquote if the number of quotes in a line is even, unless this
2566 * line or the previous one ends in a '\'. Complicated, isn't it?
2567 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002568 c = PTR2CHAR(linep + pos.col);
2569 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
2571 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002572 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2574 {
2575 inquote = FALSE;
2576 start_in_quotes = FALSE;
2577 }
2578 break;
2579
2580 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002581 // a quote that is preceded with an odd number of backslashes is
2582 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (do_quotes)
2584 {
2585 int col;
2586
2587 for (col = pos.col - 1; col >= 0; --col)
2588 if (linep[col] != '\\')
2589 break;
2590 if ((((int)pos.col - 1 - col) & 1) == 0)
2591 {
2592 inquote = !inquote;
2593 start_in_quotes = FALSE;
2594 }
2595 }
2596 break;
2597
2598 /*
2599 * If smart matching ('cpoptions' does not contain '%'):
2600 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2601 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2602 * skipped, there is never a brace in them.
2603 * Ignore this when finding matches for `'.
2604 */
2605 case '\'':
2606 if (!cpo_match && initc != '\'' && findc != '\'')
2607 {
2608 if (backwards)
2609 {
2610 if (pos.col > 1)
2611 {
2612 if (linep[pos.col - 2] == '\'')
2613 {
2614 pos.col -= 2;
2615 break;
2616 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002617 else if (linep[pos.col - 2] == '\\'
2618 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 pos.col -= 3;
2621 break;
2622 }
2623 }
2624 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002625 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002627 if (linep[pos.col + 1] == '\\'
2628 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 pos.col += 3;
2631 break;
2632 }
2633 else if (linep[pos.col + 2] == '\'')
2634 {
2635 pos.col += 2;
2636 break;
2637 }
2638 }
2639 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002640 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002643 /*
2644 * For Lisp skip over backslashed (), {} and [].
2645 * (actually, we skip #\( et al)
2646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002648 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002649 && pos.col > 1
2650 && check_prevcol(linep, pos.col, '\\', NULL)
2651 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002654 // Check for match outside of quotes, and inside of
2655 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if ((!inquote || start_in_quotes == TRUE)
2657 && (c == initc || c == findc))
2658 {
2659 int col, bslcnt = 0;
2660
2661 if (!cpo_bsl)
2662 {
2663 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2664 bslcnt++;
2665 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002666 // Only accept a match when 'M' is in 'cpo' or when escaping
2667 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2669 {
2670 if (c == initc)
2671 count++;
2672 else
2673 {
2674 if (count == 0)
2675 return &pos;
2676 count--;
2677 }
2678 }
2679 }
2680 }
2681 }
2682
2683 if (comment_dir == BACKWARD && count > 0)
2684 {
2685 pos = match_pos;
2686 return &pos;
2687 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002688 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689}
2690
2691/*
2692 * Check if line[] contains a / / comment.
2693 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002695 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002696check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
2698 char_u *p;
2699
2700 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002701 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002702 if (curbuf->b_p_lisp)
2703 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002704 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002705 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002706 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002707
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002708 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002709 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002710 {
2711 if (*p == '"')
2712 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002713 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002714 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002715 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002716 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002717 }
2718 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002719 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002720 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002721 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002722 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002723 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002724 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2725 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002726 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002727 ++p;
2728 }
2729 }
2730 else
2731 p = NULL;
2732 }
2733 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002734 while ((p = vim_strchr(p, '/')) != NULL)
2735 {
2736 // Accept a double /, unless it's preceded with * and followed by
2737 // *, because * / / * is an end and start of a C comment. Only
2738 // accept the position if it is not inside a string.
2739 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002740 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002741 break;
2742 ++p;
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744
2745 if (p == NULL)
2746 return MAXCOL;
2747 return (int)(p - line);
2748}
2749
2750/*
2751 * Move cursor briefly to character matching the one under the cursor.
2752 * Used for Insert mode and "r" command.
2753 * Show the match only if it is visible on the screen.
2754 * If there isn't a match, then beep.
2755 */
2756 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002757showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002758 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759{
2760 pos_T *lpos, save_cursor;
2761 pos_T mpos;
2762 colnr_T vcol;
2763 long save_so;
2764 long save_siso;
2765#ifdef CURSOR_SHAPE
2766 int save_state;
2767#endif
2768 colnr_T save_dollar_vcol;
2769 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002770 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2771 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772
2773 /*
2774 * Only show match for chars in the 'matchpairs' option.
2775 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002776 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002777 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
2779#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002780 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002781 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002782#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002783 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002784 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785#ifdef FEAT_RIGHTLEFT
2786 && !(curwin->w_p_rl ^ p_ri)
2787#endif
2788 )
2789 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002790 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002791 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 return;
2793 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002794 if (*p == NUL)
2795 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002797 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002799 vim_beep(BO_MATCH);
2800 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002802
2803 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2804 return;
2805
2806 if (!curwin->w_p_wrap)
2807 getvcol(curwin, lpos, NULL, &vcol, NULL);
2808
2809 int col_visible = (curwin->w_p_wrap
2810 || (vcol >= curwin->w_leftcol
2811 && vcol < curwin->w_leftcol + curwin->w_width));
2812 if (!col_visible)
2813 return;
2814
2815 mpos = *lpos; // save the pos, update_screen() may change it
2816 save_cursor = curwin->w_cursor;
2817 save_so = *so;
2818 save_siso = *siso;
2819 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2820 // stop displaying the "$".
2821 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2822 dollar_vcol = -1;
2823 ++curwin->w_virtcol; // do display ')' just before "$"
2824 update_screen(UPD_VALID); // show the new char first
2825
2826 save_dollar_vcol = dollar_vcol;
2827#ifdef CURSOR_SHAPE
2828 save_state = State;
2829 State = MODE_SHOWMATCH;
2830 ui_cursor_shape(); // may show different cursor shape
2831#endif
2832 curwin->w_cursor = mpos; // move to matching char
2833 *so = 0; // don't use 'scrolloff' here
2834 *siso = 0; // don't use 'sidescrolloff' here
2835 showruler(FALSE);
2836 setcursor();
2837 cursor_on(); // make sure that the cursor is shown
2838 out_flush_cursor(TRUE, FALSE);
2839
2840 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2841 // which resets it if the matching position is in a previous line
2842 // and has a higher column number.
2843 dollar_vcol = save_dollar_vcol;
2844
2845 /*
2846 * brief pause, unless 'm' is present in 'cpo' and a character is
2847 * available.
2848 */
2849 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2850 ui_delay(p_mat * 100L + 8, TRUE);
2851 else if (!char_avail())
2852 ui_delay(p_mat * 100L + 9, FALSE);
2853 curwin->w_cursor = save_cursor; // restore cursor position
2854 *so = save_so;
2855 *siso = save_siso;
2856#ifdef CURSOR_SHAPE
2857 State = save_state;
2858 ui_cursor_shape(); // may show different cursor shape
2859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860}
2861
2862/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002863 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002864 * If move is TRUE, check from the beginning of the buffer, else from position
2865 * "cur".
2866 * "direction" is FORWARD or BACKWARD.
2867 * Returns TRUE, FALSE or -1 for failure.
2868 */
2869 static int
2870is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2871{
2872 regmmatch_T regmatch;
2873 int nmatched = 0;
2874 int result = -1;
2875 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002876 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002877 int flag = 0;
2878
2879 if (pattern == NULL)
2880 pattern = spats[last_idx].pat;
2881
Rob Pillinge86190e2022-12-23 19:06:04 +00002882 if (search_regcomp(pattern, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002883 SEARCH_KEEP, &regmatch) == FAIL)
2884 return -1;
2885
2886 // init startcol correctly
2887 regmatch.startpos[0].col = -1;
2888 // move to match
2889 if (move)
2890 {
2891 CLEAR_POS(&pos);
2892 }
2893 else
2894 {
2895 pos = *cur;
2896 // accept a match at the cursor position
2897 flag = SEARCH_START;
2898 }
2899
2900 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2901 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2902 {
2903 // Zero-width pattern should match somewhere, then we can check if
2904 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002905 do
2906 {
2907 regmatch.startpos[0].col++;
2908 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002909 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002910 if (nmatched != 0)
2911 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002912 } while (regmatch.regprog != NULL
2913 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002914 : regmatch.startpos[0].col > pos.col);
2915
Bram Moolenaar53989552019-12-23 22:59:18 +01002916 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002917 {
2918 result = (nmatched != 0
2919 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2920 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2921 }
2922 }
2923
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002924 vim_regfree(regmatch.regprog);
2925 return result;
2926}
2927
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002928
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002929/*
2930 * Find next search match under cursor, cursor at end.
2931 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002932 */
2933 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002934current_search(
2935 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002936 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002937{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002938 pos_T start_pos; // start position of the pattern match
2939 pos_T end_pos; // end position of the pattern match
2940 pos_T orig_pos; // position of the cursor at beginning
2941 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002942 int i;
2943 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002944 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002945 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002946 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002947 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002948 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002949 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002950
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002951 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002952 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002953 dec_cursor();
2954
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002955 // When searching forward and the cursor is at the start of the Visual
2956 // area, skip the first search backward, otherwise it doesn't move.
2957 skip_first_backward = forward && VIsual_active
2958 && LT_POS(curwin->w_cursor, VIsual);
2959
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002960 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002961 if (VIsual_active)
2962 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002963 if (forward)
2964 incl(&pos);
2965 else
2966 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002967 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002968
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002969 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002970 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02002971 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002972 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002973 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002974
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002975 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002976 * The trick is to first search backwards and then search forward again,
2977 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002978 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002979 */
2980 for (i = 0; i < 2; i++)
2981 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002982 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002983 {
2984 if (i == 0 && skip_first_backward)
2985 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002986 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002987 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002988 else
2989 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002990
2991 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002992 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002993 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002994 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002995
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002996 // wrapping should not occur in the first round
2997 if (i == 0)
2998 p_ws = FALSE;
2999
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003000 result = searchit(curwin, curbuf, &pos, &end_pos,
3001 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003002 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003003 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003004
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003005 p_ws = old_p_ws;
3006
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003007 // First search may fail, but then start searching from the
3008 // beginning of the file (cursor might be on the search match)
3009 // except when Visual mode is active, so that extending the visual
3010 // selection works.
3011 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003012 {
3013 curwin->w_cursor = orig_pos;
3014 if (VIsual_active)
3015 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003016 return FAIL;
3017 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003018 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003019 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003020 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003021 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003022 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003023 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003024 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003025 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003026 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003027 // try again from end of buffer
3028 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003029 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003030 pos.col = (colnr_T)STRLEN(
3031 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003032 }
3033 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003034 }
3035
3036 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003037
3038 if (!VIsual_active)
3039 VIsual = start_pos;
3040
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003041 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003042 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003043 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003044 {
3045 if (skip_first_backward)
3046 // put the cursor on the start of the match
3047 curwin->w_cursor = pos;
3048 else
3049 // put the cursor on last character of match
3050 dec_cursor();
3051 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003052 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003053 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003054 VIsual_active = TRUE;
3055 VIsual_mode = 'v';
3056
Bram Moolenaarb7633612019-02-10 21:48:25 +01003057 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003058 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003059 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003060 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3061 inc_cursor();
3062 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3063 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003064 }
3065
3066#ifdef FEAT_FOLDING
3067 if (fdo_flags & FDO_SEARCH && KeyTyped)
3068 foldOpenCursor();
3069#endif
3070
3071 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003072 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003073#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003074 // Make sure the clipboard gets updated. Needed because start and
3075 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003076 clip_star.vmode = NUL;
3077#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003078 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003079 showmode();
3080
3081 return OK;
3082}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084/*
3085 * return TRUE if line 'lnum' is empty or has white chars only.
3086 */
3087 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003088linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 char_u *p;
3091
3092 p = skipwhite(ml_get(lnum));
3093 return (*p == NUL);
3094}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003096/*
3097 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003098 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003099 */
3100 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003101cmdline_search_stat(
3102 int dirc,
3103 pos_T *pos,
3104 pos_T *cursor_pos,
3105 int show_top_bot_msg,
3106 char_u *msgbuf,
3107 int recompute,
3108 int maxcount,
3109 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003110{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003111 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003112
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003113 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3114 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003115 if (stat.cur <= 0)
3116 return;
3117
3118 char t[SEARCH_STAT_BUF_LEN];
3119 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003120
3121#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003122 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3123 {
3124 if (stat.incomplete == 1)
3125 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3126 else if (stat.cnt > maxcount && stat.cur > maxcount)
3127 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3128 maxcount, maxcount);
3129 else if (stat.cnt > maxcount)
3130 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3131 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003132 else
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003133 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3134 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003135 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003136 else
3137#endif
3138 {
3139 if (stat.incomplete == 1)
3140 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3141 else if (stat.cnt > maxcount && stat.cur > maxcount)
3142 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3143 maxcount, maxcount);
3144 else if (stat.cnt > maxcount)
3145 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3146 stat.cur, maxcount);
3147 else
3148 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3149 stat.cur, stat.cnt);
3150 }
3151
3152 len = STRLEN(t);
3153 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3154 {
3155 mch_memmove(t + 2, t, len);
3156 t[0] = 'W';
3157 t[1] = ' ';
3158 len += 2;
3159 }
3160
zeertzjqa7d36b72023-01-31 21:13:38 +00003161 size_t msgbuf_len = STRLEN(msgbuf);
3162 if (len > msgbuf_len)
3163 len = msgbuf_len;
3164 mch_memmove(msgbuf + msgbuf_len - len, t, len);
3165
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003166 if (dirc == '?' && stat.cur == maxcount + 1)
3167 stat.cur = -1;
3168
3169 // keep the message even after redraw, but don't put in history
3170 msg_hist_off = TRUE;
3171 give_warning(msgbuf, FALSE);
3172 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003173}
3174
3175/*
3176 * Add the search count information to "stat".
3177 * "stat" must not be NULL.
3178 * When "recompute" is TRUE always recompute the numbers.
3179 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3180 * dirc == '/': find the next match
3181 * dirc == '?': find the previous match
3182 */
3183 static void
3184update_search_stat(
3185 int dirc,
3186 pos_T *pos,
3187 pos_T *cursor_pos,
3188 searchstat_T *stat,
3189 int recompute,
3190 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003191 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003192{
3193 int save_ws = p_ws;
3194 int wraparound = FALSE;
3195 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003196 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003197 static int cur = 0;
3198 static int cnt = 0;
3199 static int exact_match = FALSE;
3200 static int incomplete = 0;
3201 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3202 static int chgtick = 0;
3203 static char_u *lastpat = NULL;
3204 static buf_T *lbuf = NULL;
3205#ifdef FEAT_RELTIME
3206 proftime_T start;
3207#endif
3208
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003209 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003210
3211 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3212 {
3213 stat->cur = cur;
3214 stat->cnt = cnt;
3215 stat->exact_match = exact_match;
3216 stat->incomplete = incomplete;
3217 stat->last_maxcount = last_maxcount;
3218 return;
3219 }
3220 last_maxcount = maxcount;
3221
3222 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3223 || (dirc == '/' && LT_POS(p, lastpos)));
3224
3225 // If anything relevant changed the count has to be recomputed.
3226 // MB_STRNICMP ignores case, but we should not ignore case.
3227 // Unfortunately, there is no MB_STRNICMP function.
3228 // XXX: above comment should be "no MB_STRCMP function" ?
3229 if (!(chgtick == CHANGEDTICK(curbuf)
3230 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3231 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3232 && EQUAL_POS(lastpos, *cursor_pos)
3233 && lbuf == curbuf) || wraparound || cur < 0
3234 || (maxcount > 0 && cur > maxcount) || recompute)
3235 {
3236 cur = 0;
3237 cnt = 0;
3238 exact_match = FALSE;
3239 incomplete = 0;
3240 CLEAR_POS(&lastpos);
3241 lbuf = curbuf;
3242 }
3243
Christian Brabandt34a6a362023-05-06 19:20:20 +01003244 // when searching backwards and having jumped to the first occurrence,
3245 // cur must remain greater than 1
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003246 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
Christian Brabandt34a6a362023-05-06 19:20:20 +01003247 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003248 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3249 else
3250 {
3251 int done_search = FALSE;
3252 pos_T endpos = {0, 0, 0};
3253
3254 p_ws = FALSE;
3255#ifdef FEAT_RELTIME
3256 if (timeout > 0)
3257 profile_setlimit(timeout, &start);
3258#endif
3259 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3260 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3261 {
3262 done_search = TRUE;
3263#ifdef FEAT_RELTIME
3264 // Stop after passing the time limit.
3265 if (timeout > 0 && profile_passed_limit(&start))
3266 {
3267 incomplete = 1;
3268 break;
3269 }
3270#endif
3271 cnt++;
3272 if (LTOREQ_POS(lastpos, p))
3273 {
3274 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003275 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003276 exact_match = TRUE;
3277 }
3278 fast_breakcheck();
3279 if (maxcount > 0 && cnt > maxcount)
3280 {
3281 incomplete = 2; // max count exceeded
3282 break;
3283 }
3284 }
3285 if (got_int)
3286 cur = -1; // abort
3287 if (done_search)
3288 {
3289 vim_free(lastpat);
3290 lastpat = vim_strsave(spats[last_idx].pat);
3291 chgtick = CHANGEDTICK(curbuf);
3292 lbuf = curbuf;
3293 lastpos = p;
3294 }
3295 }
3296 stat->cur = cur;
3297 stat->cnt = cnt;
3298 stat->exact_match = exact_match;
3299 stat->incomplete = incomplete;
3300 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003301 p_ws = save_ws;
3302}
3303
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003305
3306/*
3307 * Get line "lnum" and copy it into "buf[LSIZE]".
3308 * The copy is made because the regexp may make the line invalid when using a
3309 * mark.
3310 */
3311 static char_u *
3312get_line_and_copy(linenr_T lnum, char_u *buf)
3313{
3314 char_u *line = ml_get(lnum);
3315
3316 vim_strncpy(buf, line, LSIZE - 1);
3317 return buf;
3318}
3319
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320/*
3321 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003322 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003325find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003326 char_u *ptr, // pointer to search pattern
3327 int dir UNUSED, // direction of expansion
3328 int len, // length of search pattern
3329 int whole, // match whole words only
3330 int skip_comments, // don't match inside comments
3331 int type, // Type of search; are we looking for a type?
3332 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003333 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003334 int action, // What to do when we find it
3335 linenr_T start_lnum, // first line to start searching
3336 linenr_T end_lnum) // last line for searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003338 SearchedFile *files; // Stack of included files
3339 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 int max_path_depth = 50;
3341 long match_count = 1;
3342
3343 char_u *pat;
3344 char_u *new_fname;
3345 char_u *curr_fname = curbuf->b_fname;
3346 char_u *prev_fname = NULL;
3347 linenr_T lnum;
3348 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003349 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 int old_files;
3351 int already_searched;
3352 char_u *file_line;
3353 char_u *line;
3354 char_u *p;
3355 char_u save_char;
3356 int define_matched;
3357 regmatch_T regmatch;
3358 regmatch_T incl_regmatch;
3359 regmatch_T def_regmatch;
3360 int matched = FALSE;
3361 int did_show = FALSE;
3362 int found = FALSE;
3363 int i;
3364 char_u *already = NULL;
3365 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003366 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003367#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 win_T *curwin_save = NULL;
3369#endif
3370
3371 regmatch.regprog = NULL;
3372 incl_regmatch.regprog = NULL;
3373 def_regmatch.regprog = NULL;
3374
3375 file_line = alloc(LSIZE);
3376 if (file_line == NULL)
3377 return;
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003380 // when CONT_SOL is set compare "ptr" with the beginning of the
3381 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3382 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 pat = alloc(len + 5);
3385 if (pat == NULL)
3386 goto fpip_end;
3387 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003388 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003390 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 vim_free(pat);
3392 if (regmatch.regprog == NULL)
3393 goto fpip_end;
3394 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003395 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3396 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003398 incl_regmatch.regprog = vim_regcomp(inc_opt,
3399 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 if (incl_regmatch.regprog == NULL)
3401 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003402 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
3404 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3405 {
3406 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003407 ? p_def : curbuf->b_p_def,
3408 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (def_regmatch.regprog == NULL)
3410 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003411 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003413 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (files == NULL)
3415 goto fpip_end;
3416 old_files = max_path_depth;
3417 depth = depth_displayed = -1;
3418
3419 lnum = start_lnum;
3420 if (end_lnum > curbuf->b_ml.ml_line_count)
3421 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003422 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003424 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 for (;;)
3427 {
3428 if (incl_regmatch.regprog != NULL
3429 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3430 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003431 char_u *p_fname = (curr_fname == curbuf->b_fname)
3432 ? curbuf->b_ffname : curr_fname;
3433
3434 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003435 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003436 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003437 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003438 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3439 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003440 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003441 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003442 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 already_searched = FALSE;
3444 if (new_fname != NULL)
3445 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003446 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 for (i = 0;; i++)
3448 {
3449 if (i == depth + 1)
3450 i = old_files;
3451 if (i == max_path_depth)
3452 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003453 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3454 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003456 if (type != CHECK_PATH
3457 && action == ACTION_SHOW_ALL
3458 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003460 msg_putchar('\n'); // cursor below last one
3461 if (!got_int) // don't display if 'q'
3462 // typed at "--more--"
3463 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003466 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 prev_fname = NULL;
3468 }
3469 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003470 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 already_searched = TRUE;
3472 break;
3473 }
3474 }
3475 }
3476
3477 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3478 || (new_fname == NULL && !already_searched)))
3479 {
3480 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003481 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 else
3483 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003484 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003485 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003487 msg_puts_title(_("not found "));
3488 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490 did_show = TRUE;
3491 while (depth_displayed < depth && !got_int)
3492 {
3493 ++depth_displayed;
3494 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003495 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003497 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003499 if (!got_int) // don't display if 'q' typed
3500 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003503 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 if (new_fname != NULL)
3505 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003506 // using "new_fname" is more reliable, e.g., when
3507 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003508 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 }
3510 else
3511 {
3512 /*
3513 * Isolate the file name.
3514 * Include the surrounding "" or <> if present.
3515 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003516 if (inc_opt != NULL
3517 && strstr((char *)inc_opt, "\\zs") != NULL)
3518 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003519 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003520 p = incl_regmatch.startp[0];
3521 i = (int)(incl_regmatch.endp[0]
3522 - incl_regmatch.startp[0]);
3523 }
3524 else
3525 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003526 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003527 for (p = incl_regmatch.endp[0];
3528 *p && !vim_isfilec(*p); p++)
3529 ;
3530 for (i = 0; vim_isfilec(p[i]); i++)
3531 ;
3532 }
3533
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (i == 0)
3535 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003536 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003538 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003540 // Avoid checking before the start of the line, can
3541 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003542 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 if (p[-1] == '"' || p[-1] == '<')
3545 {
3546 --p;
3547 ++i;
3548 }
3549 if (p[i] == '"' || p[i] == '>')
3550 ++i;
3551 }
3552 save_char = p[i];
3553 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003554 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 p[i] = save_char;
3556 }
3557
3558 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3559 {
3560 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003561 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003563 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003566 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
3569 if (new_fname != NULL)
3570 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003571 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (depth + 1 == old_files)
3573 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003574 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 if (bigger != NULL)
3576 {
3577 for (i = 0; i <= depth; i++)
3578 bigger[i] = files[i];
3579 for (i = depth + 1; i < old_files + max_path_depth; i++)
3580 {
3581 bigger[i].fp = NULL;
3582 bigger[i].name = NULL;
3583 bigger[i].lnum = 0;
3584 bigger[i].matched = FALSE;
3585 }
3586 for (i = old_files; i < max_path_depth; i++)
3587 bigger[i + max_path_depth] = files[i];
3588 old_files += max_path_depth;
3589 max_path_depth *= 2;
3590 vim_free(files);
3591 files = bigger;
3592 }
3593 }
3594 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3595 == NULL)
3596 vim_free(new_fname);
3597 else
3598 {
3599 if (++depth == old_files)
3600 {
3601 /*
3602 * lalloc() for 'bigger' must have failed above. We
3603 * will forget one of our already visited files now.
3604 */
3605 vim_free(files[old_files].name);
3606 ++old_files;
3607 }
3608 files[depth].name = curr_fname = new_fname;
3609 files[depth].lnum = 0;
3610 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (action == ACTION_EXPAND)
3612 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003613 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003614 vim_snprintf((char*)IObuff, IOSIZE,
3615 _("Scanning included file: %s"),
3616 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003617 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003619 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003620 {
3621 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003622 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003623 (char *)new_fname);
3624 verbose_leave();
3625 }
3626
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 }
3628 }
3629 }
3630 else
3631 {
3632 /*
3633 * Check if the line is a define (type == FIND_DEFINE)
3634 */
3635 p = line;
3636search_line:
3637 define_matched = FALSE;
3638 if (def_regmatch.regprog != NULL
3639 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3640 {
3641 /*
3642 * Pattern must be first identifier after 'define', so skip
3643 * to that position before checking for match of pattern. Also
3644 * don't let it match beyond the end of this identifier.
3645 */
3646 p = def_regmatch.endp[0];
3647 while (*p && !vim_iswordc(*p))
3648 p++;
3649 define_matched = TRUE;
3650 }
3651
3652 /*
3653 * Look for a match. Don't do this if we are looking for a
3654 * define and this line didn't match define_prog above.
3655 */
3656 if (def_regmatch.regprog == NULL || define_matched)
3657 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003658 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003660 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 startp = skipwhite(p);
3662 if (p_ic)
3663 matched = !MB_STRNICMP(startp, ptr, len);
3664 else
3665 matched = !STRNCMP(startp, ptr, len);
3666 if (matched && define_matched && whole
3667 && vim_iswordc(startp[len]))
3668 matched = FALSE;
3669 }
3670 else if (regmatch.regprog != NULL
3671 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3672 {
3673 matched = TRUE;
3674 startp = regmatch.startp[0];
3675 /*
3676 * Check if the line is not a comment line (unless we are
3677 * looking for a define). A line starting with "# define"
3678 * is not considered to be a comment line.
3679 */
3680 if (!define_matched && skip_comments)
3681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if ((*line != '#' ||
3683 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003684 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 matched = FALSE;
3686
3687 /*
3688 * Also check for a "/ *" or "/ /" before the match.
3689 * Skips lines like "int backwards; / * normal index
3690 * * /" when looking for "normal".
3691 * Note: Doesn't skip "/ *" in comments.
3692 */
3693 p = skipwhite(line);
3694 if (matched
3695 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 for (p = line; *p && p < startp; ++p)
3697 {
3698 if (matched
3699 && p[0] == '/'
3700 && (p[1] == '*' || p[1] == '/'))
3701 {
3702 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003703 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (p[1] == '/')
3705 break;
3706 ++p;
3707 }
3708 else if (!matched && p[0] == '*' && p[1] == '/')
3709 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003710 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 matched = TRUE;
3712 ++p;
3713 }
3714 }
3715 }
3716 }
3717 }
3718 }
3719 if (matched)
3720 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (action == ACTION_EXPAND)
3722 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003723 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 int add_r;
3725 char_u *aux;
3726
3727 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3728 break;
3729 found = TRUE;
3730 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003731 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003733 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (vim_iswordp(p))
3735 goto exit_matched;
3736 p = find_word_start(p);
3737 }
3738 p = find_word_end(p);
3739 i = (int)(p - aux);
3740
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003741 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003743 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003745
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003746 // Get the next line: when "depth" < 0 from the current
3747 // buffer, otherwise from the included file. Jump to
3748 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003749 if (depth < 0)
3750 {
3751 if (lnum >= end_lnum)
3752 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003753 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003754 }
3755 else if (vim_fgets(line = file_line,
3756 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 goto exit_matched;
3758
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003759 // we read a line, set "already" to check this "line" later
3760 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003761 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 already = aux = p = skipwhite(line);
3763 p = find_word_start(p);
3764 p = find_word_end(p);
3765 if (p > aux)
3766 {
3767 if (*aux != ')' && IObuff[i-1] != TAB)
3768 {
3769 if (IObuff[i-1] != ' ')
3770 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003771 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (p_js
3773 && (IObuff[i-2] == '.'
3774 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3775 && (IObuff[i-2] == '?'
3776 || IObuff[i-2] == '!'))))
3777 IObuff[i++] = ' ';
3778 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003779 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 if (p - aux >= IOSIZE - i)
3781 p = aux + IOSIZE - i - 1;
3782 STRNCPY(IObuff + i, aux, p - aux);
3783 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003784 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 IObuff[i] = NUL;
3787 aux = IObuff;
3788
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003789 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 goto exit_matched;
3791 }
3792
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003793 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003795 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003797 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003799 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 break;
3801 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003802 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 {
3804 found = TRUE;
3805 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003806 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (curr_fname != prev_fname)
3808 {
3809 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003810 msg_putchar('\n'); // cursor below last one
3811 if (!got_int) // don't display if 'q' typed
3812 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 msg_home_replace_hl(curr_fname);
3814 prev_fname = curr_fname;
3815 }
3816 did_show = TRUE;
3817 if (!got_int)
3818 show_pat_in_path(line, type, TRUE, action,
3819 (depth == -1) ? NULL : files[depth].fp,
3820 (depth == -1) ? &lnum : &files[depth].lnum,
3821 match_count++);
3822
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003823 // Set matched flag for this file and all the ones that
3824 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 for (i = 0; i <= depth; ++i)
3826 files[i].matched = TRUE;
3827 }
3828 else if (--count <= 0)
3829 {
3830 found = TRUE;
3831 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003832#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 && g_do_tagpreview == 0
3834#endif
3835 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003836 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 else if (action == ACTION_SHOW)
3838 {
3839 show_pat_in_path(line, type, did_show, action,
3840 (depth == -1) ? NULL : files[depth].fp,
3841 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3842 did_show = TRUE;
3843 }
3844 else
3845 {
3846#ifdef FEAT_GUI
3847 need_mouse_correct = TRUE;
3848#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003849#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003850 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 if (g_do_tagpreview != 0)
3852 {
3853 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003854 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856#endif
3857 if (action == ACTION_SPLIT)
3858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003861 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 if (depth == -1)
3864 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003865 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003866#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (g_do_tagpreview != 0)
3868 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003869 if (!win_valid(curwin_save))
3870 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003871 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003872 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003873 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003874 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 else
3877#endif
3878 setpcmark();
3879 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003880 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
3882 else
3883 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003884 if (!GETFILE_SUCCESS(getfile(
3885 0, files[depth].name, NULL, TRUE,
3886 files[depth].lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003887 break; // failed to jump to file
3888 // autocommands may have changed the lnum, we don't
3889 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 curwin->w_cursor.lnum = files[depth].lnum;
3891 }
3892 }
3893 if (action != ACTION_SHOW)
3894 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003895 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 curwin->w_set_curswant = TRUE;
3897 }
3898
Bram Moolenaar4033c552017-09-16 20:54:51 +02003899#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003901 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003903 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003905 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 win_enter(curwin_save, TRUE);
3907 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003908# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003909 else if (WIN_IS_POPUP(curwin))
3910 // can't keep focus in popup window
3911 win_enter(firstwin, TRUE);
3912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#endif
3914 break;
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003918 // look for other matches in the rest of the line if we
3919 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003922 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003923 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003924 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 goto search_line;
3926 }
3927 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003929 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003930 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932
3933 /*
3934 * Read the next line. When reading an included file and encountering
3935 * end-of-file, close the file and continue in the file that included
3936 * it.
3937 */
3938 while (depth >= 0 && !already
3939 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3940 {
3941 fclose(files[depth].fp);
3942 --old_files;
3943 files[old_files].name = files[depth].name;
3944 files[old_files].matched = files[depth].matched;
3945 --depth;
3946 curr_fname = (depth == -1) ? curbuf->b_fname
3947 : files[depth].name;
3948 if (depth < depth_displayed)
3949 depth_displayed = depth;
3950 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003951 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003954 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003955 i = (int)STRLEN(line);
3956 if (i > 0 && line[i - 1] == '\n')
3957 line[--i] = NUL;
3958 if (i > 0 && line[i - 1] == '\r')
3959 line[--i] = NUL;
3960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else if (!already)
3962 {
3963 if (++lnum > end_lnum)
3964 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003965 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 already = NULL;
3968 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003969 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003971 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 for (i = 0; i <= depth; i++)
3973 {
3974 fclose(files[i].fp);
3975 vim_free(files[i].name);
3976 }
3977 for (i = old_files; i < max_path_depth; i++)
3978 vim_free(files[i].name);
3979 vim_free(files);
3980
3981 if (type == CHECK_PATH)
3982 {
3983 if (!did_show)
3984 {
3985 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003986 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003988 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003991 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003993 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003994 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003996 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003998 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4001 msg_end();
4002
4003fpip_end:
4004 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02004005 vim_regfree(regmatch.regprog);
4006 vim_regfree(incl_regmatch.regprog);
4007 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008}
4009
4010 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004011show_pat_in_path(
4012 char_u *line,
4013 int type,
4014 int did_show,
4015 int action,
4016 FILE *fp,
4017 linenr_T *lnum,
4018 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
4020 char_u *p;
4021
4022 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004023 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004024 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004025 gotocmdline(TRUE); // cursor at status line
4026 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return;
4028 for (;;)
4029 {
4030 p = line + STRLEN(line) - 1;
4031 if (fp != NULL)
4032 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004033 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 if (p >= line && *p == '\n')
4035 --p;
4036 if (p >= line && *p == '\r')
4037 --p;
4038 *(p + 1) = NUL;
4039 }
4040 if (action == ACTION_SHOW_ALL)
4041 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004042 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004043 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004044 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4045 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004046 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4047 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004049 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004050 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004052 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4054 break;
4055
4056 if (fp != NULL)
4057 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004058 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 break;
4060 ++*lnum;
4061 }
4062 else
4063 {
4064 if (++*lnum > curbuf->b_ml.ml_line_count)
4065 break;
4066 line = ml_get(*lnum);
4067 }
4068 msg_putchar('\n');
4069 }
4070}
4071#endif
4072
4073#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004074/*
4075 * Return the last used search pattern at "idx".
4076 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004077 spat_T *
4078get_spat(int idx)
4079{
4080 return &spats[idx];
4081}
4082
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004083/*
4084 * Return the last used search pattern index.
4085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004087get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004089 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004092
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004093#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004094/*
4095 * "searchcount()" function
4096 */
4097 void
4098f_searchcount(typval_T *argvars, typval_T *rettv)
4099{
4100 pos_T pos = curwin->w_cursor;
4101 char_u *pattern = NULL;
4102 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4103 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004104 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004105 searchstat_T stat;
4106
4107 if (rettv_dict_alloc(rettv) == FAIL)
4108 return;
4109
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004110 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4111 return;
4112
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004113 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4114 recompute = TRUE;
4115
4116 if (argvars[0].v_type != VAR_UNKNOWN)
4117 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004118 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004119 dictitem_T *di;
4120 listitem_T *li;
4121 int error = FALSE;
4122
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004123 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004124 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004125 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004126 di = dict_find(dict, (char_u *)"timeout", -1);
4127 if (di != NULL)
4128 {
4129 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4130 if (error)
4131 return;
4132 }
4133 di = dict_find(dict, (char_u *)"maxcount", -1);
4134 if (di != NULL)
4135 {
4136 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4137 if (error)
4138 return;
4139 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004140 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004141 di = dict_find(dict, (char_u *)"pattern", -1);
4142 if (di != NULL)
4143 {
4144 pattern = tv_get_string_chk(&di->di_tv);
4145 if (pattern == NULL)
4146 return;
4147 }
4148 di = dict_find(dict, (char_u *)"pos", -1);
4149 if (di != NULL)
4150 {
4151 if (di->di_tv.v_type != VAR_LIST)
4152 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004153 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004154 return;
4155 }
4156 if (list_len(di->di_tv.vval.v_list) != 3)
4157 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004158 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004159 return;
4160 }
4161 li = list_find(di->di_tv.vval.v_list, 0L);
4162 if (li != NULL)
4163 {
4164 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4165 if (error)
4166 return;
4167 }
4168 li = list_find(di->di_tv.vval.v_list, 1L);
4169 if (li != NULL)
4170 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004171 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004172 if (error)
4173 return;
4174 }
4175 li = list_find(di->di_tv.vval.v_list, 2L);
4176 if (li != NULL)
4177 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004178 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004179 if (error)
4180 return;
4181 }
4182 }
4183 }
4184
4185 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004186#ifdef FEAT_SEARCH_EXTRA
4187 save_incsearch_state();
4188#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004189 if (pattern != NULL)
4190 {
4191 if (*pattern == NUL)
4192 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004193 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004194 spats[last_idx].pat = vim_strsave(pattern);
4195 }
4196 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4197 goto the_end; // the previous pattern was never defined
4198
4199 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4200
4201 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4202 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4203 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4204 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4205 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4206
4207the_end:
4208 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004209#ifdef FEAT_SEARCH_EXTRA
4210 restore_incsearch_state();
4211#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004212}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004213#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004214
4215/*
4216 * Fuzzy string matching
4217 *
4218 * Ported from the lib_fts library authored by Forrest Smith.
4219 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4220 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004221 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004222 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4223 *
4224 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004225 * - Matched letter
4226 * - Unmatched letter
4227 * - Consecutively matched letters
4228 * - Proximity to start
4229 * - Letter following a separator (space, underscore)
4230 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004231 *
4232 * Matched letters are good. Unmatched letters are bad. Matching near the start
4233 * is good. Matching the first letter in the middle of a phrase is good.
4234 * Matching the uppercase letters in camel case entries is good.
4235 *
4236 * The score assigned for each factor is explained below.
4237 * File paths are different from file names. File extensions may be ignorable.
4238 * Single words care about consecutive matches but not separators or camel
4239 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004240 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004241 * Matched letter: +0 points
4242 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004243 * Consecutive match bonus: +15 points
4244 * First letter bonus: +15 points
4245 * Separator bonus: +30 points
4246 * Camel case bonus: +30 points
4247 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004248 *
4249 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004250 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004251 * lower minimum score due to unmatched letter penalty. Longer search patterns
4252 * have a higher maximum score due to match bonuses.
4253 *
4254 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4255 * quite a bit.
4256 *
4257 * There is a penalty if you DON’T match the first three letters. Which
4258 * effectively rewards matching near the start. However there’s no difference
4259 * in matching between the middle and end.
4260 *
4261 * There is not an explicit bonus for an exact match. Unmatched letters receive
4262 * a penalty. So shorter strings and closer matches are worth more.
4263 */
4264typedef struct
4265{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004266 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004267 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004268 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004269 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004270} fuzzyItem_T;
4271
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004272// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4273// matching a whole word is preferred.
4274#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004275// bonus if match occurs after a path separator
4276#define PATH_SEPARATOR_BONUS 30
4277// bonus if match occurs after a word separator
4278#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004279// bonus if match is uppercase and prev is lower
4280#define CAMEL_BONUS 30
4281// bonus if the first letter is matched
4282#define FIRST_LETTER_BONUS 15
4283// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004284#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004285// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004286#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004287// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004288#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004289// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004290#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004291// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004292#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004293
4294#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004295
4296/*
4297 * Compute a score for a fuzzy matched string. The matching character locations
4298 * are in 'matches'.
4299 */
4300 static int
4301fuzzy_match_compute_score(
4302 char_u *str,
4303 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004304 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004305 int numMatches)
4306{
4307 int score;
4308 int penalty;
4309 int unmatched;
4310 int i;
4311 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004312 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004313
4314 // Initialize score
4315 score = 100;
4316
4317 // Apply leading letter penalty
4318 penalty = LEADING_LETTER_PENALTY * matches[0];
4319 if (penalty < MAX_LEADING_LETTER_PENALTY)
4320 penalty = MAX_LEADING_LETTER_PENALTY;
4321 score += penalty;
4322
4323 // Apply unmatched penalty
4324 unmatched = strSz - numMatches;
4325 score += UNMATCHED_LETTER_PENALTY * unmatched;
4326
4327 // Apply ordering bonuses
4328 for (i = 0; i < numMatches; ++i)
4329 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004330 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004331
4332 if (i > 0)
4333 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004334 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004335
4336 // Sequential
4337 if (currIdx == (prevIdx + 1))
4338 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004339 else
4340 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004341 }
4342
4343 // Check for bonuses based on neighbor character value
4344 if (currIdx > 0)
4345 {
4346 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004347 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004348 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004349
4350 if (has_mbyte)
4351 {
4352 while (sidx < currIdx)
4353 {
4354 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004355 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004356 sidx++;
4357 }
4358 curr = (*mb_ptr2char)(p);
4359 }
4360 else
4361 {
4362 neighbor = str[currIdx - 1];
4363 curr = str[currIdx];
4364 }
4365
4366 if (vim_islower(neighbor) && vim_isupper(curr))
4367 score += CAMEL_BONUS;
4368
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004369 // Bonus if the match follows a separator character
4370 if (neighbor == '/' || neighbor == '\\')
4371 score += PATH_SEPARATOR_BONUS;
4372 else if (neighbor == ' ' || neighbor == '_')
4373 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004374 }
4375 else
4376 {
4377 // First letter
4378 score += FIRST_LETTER_BONUS;
4379 }
4380 }
4381 return score;
4382}
4383
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004384/*
4385 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4386 * Return the number of matching characters.
4387 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004388 static int
4389fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004390 char_u *fuzpat,
4391 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004392 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004393 int *outScore,
4394 char_u *strBegin,
4395 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004396 int_u *srcMatches,
4397 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004398 int maxMatches,
4399 int nextMatch,
4400 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004401{
4402 // Recursion params
4403 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004404 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004405 int bestRecursiveScore = 0;
4406 int first_match;
4407 int matched;
4408
4409 // Count recursions
4410 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004411 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004412 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004413
4414 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004415 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004416 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004417
4418 // Loop through fuzpat and str looking for a match
4419 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004420 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004421 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004422 int c1;
4423 int c2;
4424
4425 c1 = PTR2CHAR(fuzpat);
4426 c2 = PTR2CHAR(str);
4427
Bram Moolenaar635414d2020-09-11 22:25:15 +02004428 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004429 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004430 {
Bram Moolenaar635414d2020-09-11 22:25:15 +02004431 // Supplied matches buffer was too short
4432 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004433 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004434
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004435 int recursiveScore = 0;
4436 int_u recursiveMatches[MAX_FUZZY_MATCHES];
4437 CLEAR_FIELD(recursiveMatches);
4438
Bram Moolenaar635414d2020-09-11 22:25:15 +02004439 // "Copy-on-Write" srcMatches into matches
4440 if (first_match && srcMatches)
4441 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004442 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004443 first_match = FALSE;
4444 }
4445
4446 // Recursive call that "skips" this match
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004447 char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004448 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4449 &recursiveScore, strBegin, strLen, matches,
4450 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004451 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004452 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004453 {
4454 // Pick best recursive score
4455 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4456 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004457 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004458 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004459 bestRecursiveScore = recursiveScore;
4460 }
4461 recursiveMatch = TRUE;
4462 }
4463
4464 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004465 matches[nextMatch++] = strIdx;
4466 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004467 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004468 else
4469 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004470 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004471 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004472 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004473 else
4474 ++str;
4475 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004476 }
4477
4478 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004479 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004480
4481 // Calculate score
4482 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004483 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4484 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004485
4486 // Return best result
4487 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4488 {
4489 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004490 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004491 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004492 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004493 }
4494 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004495 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004496
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004497 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004498}
4499
4500/*
4501 * fuzzy_match()
4502 *
4503 * Performs exhaustive search via recursion to find all possible matches and
4504 * match with highest score.
4505 * Scores values have no intrinsic meaning. Possible score range is not
4506 * normalized and varies with pattern.
4507 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004508 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004509 * Uses char_u for match indices. Therefore patterns are limited to
4510 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004511 *
Bram Moolenaarcaf642c2023-04-29 21:38:04 +01004512 * Returns TRUE if "pat_arg" matches "str". Also returns the match score in
4513 * "outScore" and the matching character positions in "matches".
Bram Moolenaar635414d2020-09-11 22:25:15 +02004514 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004515 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004516fuzzy_match(
4517 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004518 char_u *pat_arg,
4519 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004520 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004521 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004522 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004523{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004524 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004525 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004526 char_u *save_pat;
4527 char_u *pat;
4528 char_u *p;
4529 int complete = FALSE;
4530 int score = 0;
4531 int numMatches = 0;
4532 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004533
4534 *outScore = 0;
4535
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004536 save_pat = vim_strsave(pat_arg);
4537 if (save_pat == NULL)
4538 return FALSE;
4539 pat = save_pat;
4540 p = pat;
4541
4542 // Try matching each word in 'pat_arg' in 'str'
4543 while (TRUE)
4544 {
4545 if (matchseq)
4546 complete = TRUE;
4547 else
4548 {
4549 // Extract one word from the pattern (separated by space)
4550 p = skipwhite(p);
4551 if (*p == NUL)
4552 break;
4553 pat = p;
4554 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4555 {
4556 if (has_mbyte)
4557 MB_PTR_ADV(p);
4558 else
4559 ++p;
4560 }
4561 if (*p == NUL) // processed all the words
4562 complete = TRUE;
4563 *p = NUL;
4564 }
4565
4566 score = 0;
4567 recursionCount = 0;
4568 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4569 matches + numMatches, maxMatches - numMatches,
4570 0, &recursionCount);
4571 if (matchCount == 0)
4572 {
4573 numMatches = 0;
4574 break;
4575 }
4576
4577 // Accumulate the match score and the number of matches
4578 *outScore += score;
4579 numMatches += matchCount;
4580
4581 if (complete)
4582 break;
4583
4584 // try matching the next word
4585 ++p;
4586 }
4587
4588 vim_free(save_pat);
4589 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004590}
4591
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004592#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004593/*
4594 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004595 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004596 */
4597 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004598fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004599{
4600 int v1 = ((fuzzyItem_T *)s1)->score;
4601 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004602 int idx1 = ((fuzzyItem_T *)s1)->idx;
4603 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004604
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004605 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004606}
4607
4608/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004609 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4610 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004611 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4612 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004613 * If 'items' is a list of strings, then search for 'str' in the list.
4614 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4615 * for each item or use 'item_cb' Funcref function to get the string.
4616 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4617 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004618 */
4619 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004620fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004621 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004622 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004623 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004624 char_u *key,
4625 callback_T *item_cb,
4626 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004627 list_T *fmatchlist,
4628 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004629{
4630 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004631 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004632 listitem_T *li;
4633 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004634 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004635 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004636
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004637 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004638 if (len == 0)
4639 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004640 if (max_matches > 0 && len > max_matches)
4641 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004642
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004643 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4644 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004645 return;
4646
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004647 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004648 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004649 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004650 int score;
4651 char_u *itemstr;
4652 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004653
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004654 if (max_matches > 0 && match_count >= max_matches)
4655 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004656
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004657 itemstr = NULL;
4658 rettv.v_type = VAR_UNKNOWN;
4659 if (li->li_tv.v_type == VAR_STRING) // list of strings
4660 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004661 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004662 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004663 {
4664 // For a dict, either use the specified key to lookup the string or
4665 // use the specified callback function to get the string.
4666 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004667 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4668 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004669 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004670 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004671 typval_T argv[2];
4672
4673 // Invoke the supplied callback (if any) to get the dict item
4674 li->li_tv.vval.v_dict->dv_refcount++;
4675 argv[0].v_type = VAR_DICT;
4676 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4677 argv[1].v_type = VAR_UNKNOWN;
4678 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4679 {
4680 if (rettv.v_type == VAR_STRING)
4681 itemstr = rettv.vval.v_string;
4682 }
4683 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004684 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004685 }
4686
4687 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004688 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004689 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004690 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004691 items[match_count].idx = match_count;
4692 items[match_count].item = li;
4693 items[match_count].score = score;
4694
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004695 // Copy the list of matching positions in itemstr to a list, if
4696 // 'retmatchpos' is set.
4697 if (retmatchpos)
4698 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004699 int j = 0;
4700 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004701
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004702 items[match_count].lmatchpos = list_alloc();
4703 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004704 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004705
4706 p = str;
4707 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004708 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004709 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004710 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004711 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004712 matches[j]) == FAIL)
4713 goto done;
4714 j++;
4715 }
4716 if (has_mbyte)
4717 MB_PTR_ADV(p);
4718 else
4719 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004720 }
4721 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004722 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004723 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004724 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004725 }
4726
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004727 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004728 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004729 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004730
Bram Moolenaar635414d2020-09-11 22:25:15 +02004731 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004732 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004733 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004734
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004735 // For matchfuzzy(), return a list of matched strings.
4736 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004737 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004738 // The first item is a list of matched strings. The second item
4739 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004740 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004741 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4742 if (retmatchpos)
4743 {
4744 li = list_find(fmatchlist, 0);
4745 if (li == NULL || li->li_tv.vval.v_list == NULL)
4746 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004747 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004748 }
4749 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004750 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004751
4752 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004753 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004754 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004755 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004756 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004757 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004758 }
4759
4760 // next copy the list of matching positions
4761 if (retmatchpos)
4762 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004763 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004764 if (li == NULL || li->li_tv.vval.v_list == NULL)
4765 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004766 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004767
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004768 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004769 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004770 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004771 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004772 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004773 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004774 goto done;
4775 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004776
4777 // copy the matching scores
4778 li = list_find(fmatchlist, -1);
4779 if (li == NULL || li->li_tv.vval.v_list == NULL)
4780 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004781 retlist = li->li_tv.vval.v_list;
4782 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004783 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004784 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004785 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004786 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004787 goto done;
4788 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004789 }
4790 }
4791
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004792done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004793 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004794}
4795
4796/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004797 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4798 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4799 */
4800 static void
4801do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4802{
4803 callback_T cb;
4804 char_u *key = NULL;
4805 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004806 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004807 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004808
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004809 if (in_vim9script()
4810 && (check_for_list_arg(argvars, 0) == FAIL
4811 || check_for_string_arg(argvars, 1) == FAIL
4812 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4813 return;
4814
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004815 CLEAR_POINTER(&cb);
4816
4817 // validate and get the arguments
4818 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4819 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004820 semsg(_(e_argument_of_str_must_be_list),
4821 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004822 return;
4823 }
4824 if (argvars[1].v_type != VAR_STRING
4825 || argvars[1].vval.v_string == NULL)
4826 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004827 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004828 return;
4829 }
4830
4831 if (argvars[2].v_type != VAR_UNKNOWN)
4832 {
4833 dict_T *d;
4834 dictitem_T *di;
4835
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004836 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004837 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004838
4839 // To search a dict, either a callback function or a key can be
4840 // specified.
4841 d = argvars[2].vval.v_dict;
4842 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4843 {
4844 if (di->di_tv.v_type != VAR_STRING
4845 || di->di_tv.vval.v_string == NULL
4846 || *di->di_tv.vval.v_string == NUL)
4847 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004848 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004849 return;
4850 }
4851 key = tv_get_string(&di->di_tv);
4852 }
4853 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4854 {
4855 cb = get_callback(&di->di_tv);
4856 if (cb.cb_name == NULL)
4857 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004858 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004859 return;
4860 }
4861 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004862
4863 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004864 {
4865 if (di->di_tv.v_type != VAR_NUMBER)
4866 {
4867 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4868 return;
4869 }
4870 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4871 }
4872
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004873 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004874 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004875 }
4876
4877 // get the fuzzy matches
4878 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004879 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004880 goto done;
4881 if (retmatchpos)
4882 {
4883 list_T *l;
4884
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004885 // For matchfuzzypos(), a list with three items are returned. First
4886 // item is a list of matching strings, the second item is a list of
4887 // lists with matching positions within each string and the third item
4888 // is the list of scores of the matches.
4889 l = list_alloc();
4890 if (l == NULL)
4891 goto done;
4892 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004893 {
4894 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004895 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004896 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004897 l = list_alloc();
4898 if (l == NULL)
4899 goto done;
4900 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004901 {
4902 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004903 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004904 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004905 l = list_alloc();
4906 if (l == NULL)
4907 goto done;
4908 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004909 {
4910 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004911 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004912 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004913 }
4914
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004915 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004916 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004917
4918done:
4919 free_callback(&cb);
4920}
4921
4922/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004923 * "matchfuzzy()" function
4924 */
4925 void
4926f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4927{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004928 do_fuzzymatch(argvars, rettv, FALSE);
4929}
4930
4931/*
4932 * "matchfuzzypos()" function
4933 */
4934 void
4935f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
4936{
4937 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004938}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004939#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004940
4941/*
4942 * Same as fuzzy_match_item_compare() except for use with a string match
4943 */
4944 static int
4945fuzzy_match_str_compare(const void *s1, const void *s2)
4946{
4947 int v1 = ((fuzmatch_str_T *)s1)->score;
4948 int v2 = ((fuzmatch_str_T *)s2)->score;
4949 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4950 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4951
4952 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4953}
4954
4955/*
4956 * Sort fuzzy matches by score
4957 */
4958 static void
4959fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
4960{
4961 // Sort the list by the descending order of the match score
4962 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4963 fuzzy_match_str_compare);
4964}
4965
4966/*
4967 * Same as fuzzy_match_item_compare() except for use with a function name
4968 * string match. <SNR> functions should be sorted to the end.
4969 */
4970 static int
4971fuzzy_match_func_compare(const void *s1, const void *s2)
4972{
4973 int v1 = ((fuzmatch_str_T *)s1)->score;
4974 int v2 = ((fuzmatch_str_T *)s2)->score;
4975 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4976 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4977 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
4978 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
4979
4980 if (*str1 != '<' && *str2 == '<') return -1;
4981 if (*str1 == '<' && *str2 != '<') return 1;
4982 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4983}
4984
4985/*
4986 * Sort fuzzy matches of function names by score.
4987 * <SNR> functions should be sorted to the end.
4988 */
4989 static void
4990fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
4991{
4992 // Sort the list by the descending order of the match score
4993 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4994 fuzzy_match_func_compare);
4995}
4996
4997/*
4998 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
4999 * returns the match score.
5000 */
5001 int
5002fuzzy_match_str(char_u *str, char_u *pat)
5003{
5004 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00005005 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005006
5007 if (str == NULL || pat == NULL)
5008 return 0;
5009
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00005010 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005011 sizeof(matchpos) / sizeof(matchpos[0]));
5012
5013 return score;
5014}
5015
5016/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005017 * Free an array of fuzzy string matches "fuzmatch[count]".
5018 */
5019 void
5020fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5021{
5022 int i;
5023
5024 if (fuzmatch == NULL)
5025 return;
5026 for (i = 0; i < count; ++i)
5027 vim_free(fuzmatch[i].str);
5028 vim_free(fuzmatch);
5029}
5030
5031/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005032 * Copy a list of fuzzy matches into a string list after sorting the matches by
5033 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5034 * Returns OK on success and FAIL on memory allocation failure.
5035 */
5036 int
5037fuzzymatches_to_strmatches(
5038 fuzmatch_str_T *fuzmatch,
5039 char_u ***matches,
5040 int count,
5041 int funcsort)
5042{
5043 int i;
5044
5045 if (count <= 0)
5046 return OK;
5047
5048 *matches = ALLOC_MULT(char_u *, count);
5049 if (*matches == NULL)
5050 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005051 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005052 return FAIL;
5053 }
5054
5055 // Sort the list by the descending order of the match score
5056 if (funcsort)
5057 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5058 else
5059 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5060
5061 for (i = 0; i < count; i++)
5062 (*matches)[i] = fuzmatch[i].str;
5063 vim_free(fuzmatch);
5064
5065 return OK;
5066}