blob: 74ca8fefb2c56288da94c62f9f56ffc26538609c [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.
1092 * If 'shortmess' does not contain 's', we give a message.
1093 * This message is also remembered in keep_msg for when the screen
1094 * is redrawn. The keep_msg is cleared whenever another message is
1095 * written.
1096 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001097 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001101 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1102 give_warning((char_u *)_(dir == BACKWARD
1103 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001104 if (extra_arg != NULL)
1105 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
Paul Ollis65745772022-06-05 16:55:54 +01001107 if (got_int || called_emsg > called_emsg_before || *timed_out
Bram Moolenaar78a15312009-05-15 19:33:18 +00001108#ifdef FEAT_SEARCH_EXTRA
1109 || break_loop
1110#endif
1111 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 break;
1113 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001114 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01001116#ifdef FEAT_RELTIME
1117 if (extra_arg != NULL && extra_arg->sa_tm > 0)
1118 disable_regexp_timeout();
1119#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02001120 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001122 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 if (got_int)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001125 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1127 {
1128 if (p_ws)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001129 semsg(_(e_pattern_not_found_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 else if (lnum == 0)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001131 semsg(_(e_search_hit_top_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001133 semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 return FAIL;
1136 }
1137
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001138 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001139 if (pos->lnum > buf->b_ml.ml_line_count)
1140 {
1141 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001142 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001143 if (pos->col > 0)
1144 --pos->col;
1145 }
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 return submatch + 1;
1148}
1149
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00001150#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001151 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001152set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001153{
1154 spats[0].off.dir = cdir;
1155}
1156
1157 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001158set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001159{
1160 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1161}
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163/*
1164 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001165 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 */
1167 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001168first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 int submatch;
1171
1172 for (submatch = 1; ; ++submatch)
1173 {
1174 if (rp->startpos[submatch].lnum >= 0)
1175 break;
1176 if (submatch == 9)
1177 {
1178 submatch = 0;
1179 break;
1180 }
1181 }
1182 return submatch;
1183}
1184#endif
1185
1186/*
1187 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001188 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 * If 'dirc' is 0: use previous dir.
1190 * If 'pat' is NULL or empty : use previous string.
1191 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1192 * If 'options & SEARCH_ECHO': echo the search command and handle options
1193 * If 'options & SEARCH_MSG' : may give error message
1194 * If 'options & SEARCH_OPT' : interpret optional flags
1195 * If 'options & SEARCH_HIS' : put search pattern in history
1196 * If 'options & SEARCH_NOOF': don't add offset to position
1197 * If 'options & SEARCH_MARK': set previous context mark
1198 * If 'options & SEARCH_KEEP': keep previous search pattern
1199 * If 'options & SEARCH_START': accept match at curpos itself
1200 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1201 *
1202 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1203 * makes the movement linewise without moving the match position.
1204 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001205 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
1207 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001208do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001209 oparg_T *oap, // can be NULL
1210 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001211 int search_delim, // the delimiter for the search, e.g. '%' in
1212 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001213 char_u *pat,
1214 long count,
1215 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001216 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001218 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001220 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001221 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 char_u *p;
1223 long c;
1224 char_u *dircp;
1225 char_u *strcopy = NULL;
1226 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001227 char_u *msgbuf = NULL;
1228 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001229 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
1231 /*
1232 * A line offset is not remembered, this is vi compatible.
1233 */
1234 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1235 {
1236 spats[0].off.line = FALSE;
1237 spats[0].off.off = 0;
1238 }
1239
1240 /*
1241 * Save the values for when (options & SEARCH_KEEP) is used.
1242 * (there is no "if ()" around this because gcc wants them initialized)
1243 */
1244 old_off = spats[0].off;
1245
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001246 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 /*
1249 * Find out the direction of the search.
1250 */
1251 if (dirc == 0)
1252 dirc = spats[0].off.dir;
1253 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001256#if defined(FEAT_EVAL)
1257 set_vv_searchforward();
1258#endif
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 if (options & SEARCH_REV)
1261 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001262#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001263 // There is a bug in the Visual C++ 2.2 compiler which means that
1264 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 dirc = (dirc == '/') ? '?' : '/';
1266#else
1267 if (dirc == '/')
1268 dirc = '?';
1269 else
1270 dirc = '/';
1271#endif
1272 }
1273
1274#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001275 // If the cursor is in a closed fold, don't find another match in the same
1276 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 if (dirc == '/')
1278 {
1279 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001280 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 else
1283 {
1284 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1285 pos.col = 0;
1286 }
1287#endif
1288
1289#ifdef FEAT_SEARCH_EXTRA
1290 /*
1291 * Turn 'hlsearch' highlighting back on.
1292 */
1293 if (no_hlsearch && !(options & SEARCH_KEEP))
1294 {
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001295 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001296 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298#endif
1299
1300 /*
1301 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1302 */
1303 for (;;)
1304 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001305 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001306
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 searchstr = pat;
1308 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001309 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001310 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001312 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001314 searchstr = spats[RE_SUBST].pat;
1315 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001316 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001317 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001318 retval = 0;
1319 goto end_do_search;
1320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001322 else
1323 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001324 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001325 searchstr = (char_u *)"";
1326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001329 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
1331 /*
1332 * Find end of regular expression.
1333 * If there is a matching '/' or '?', toss it.
1334 */
1335 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001336 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001337 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 if (strcopy != ps)
1339 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001340 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001341 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 pat = strcopy;
1343 searchstr = strcopy;
1344 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001345 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001347 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 *p++ = NUL;
1349 }
1350 spats[0].off.line = FALSE;
1351 spats[0].off.end = FALSE;
1352 spats[0].off.off = 0;
1353 /*
1354 * Check for a line offset or a character offset.
1355 * For get_address (echo off) we don't check for a character
1356 * offset, because it is meaningless and the 's' could be a
1357 * substitute command.
1358 */
1359 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1360 spats[0].off.line = TRUE;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001361 else if ((options & SEARCH_OPT)
1362 && (*p == 'e' || *p == 's' || *p == 'b'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001364 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 spats[0].off.end = SEARCH_END;
1366 ++p;
1367 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001368 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001370 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1372 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001373 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001375 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 spats[0].off.off = 1;
1377 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001378 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 ++p;
1380 }
1381
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001382 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 searchcmdlen += (int)(p - pat);
1384
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001385 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387
Dominique Pelle7765f5c2022-04-10 11:26:53 +01001388 if ((options & SEARCH_ECHO) && messaging()
1389 && !msg_silent
1390 && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001393 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001394 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001396 // Compute msg_row early.
1397 msg_start();
1398
Bram Moolenaar984f0312019-05-24 13:11:47 +02001399 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001400 if (!cmd_silent &&
1401 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001402 {
1403 p = off_buf;
1404 *p++ = dirc;
1405 if (spats[0].off.end)
1406 *p++ = 'e';
1407 else if (!spats[0].off.line)
1408 *p++ = 's';
1409 if (spats[0].off.off > 0 || spats[0].off.line)
1410 *p++ = '+';
1411 *p = NUL;
1412 if (spats[0].off.off != 0 || spats[0].off.line)
1413 sprintf((char *)p, "%ld", spats[0].off.off);
1414 off_len = STRLEN(off_buf);
1415 }
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001418 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 else
1420 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001421
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001422 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001423 {
1424 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001425 // search stat. Use all the space available, so that the
1426 // search state is right aligned. If there is not enough space
1427 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001428 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001429 // Use all the columns.
1430 len = (int)(Rows - msg_row) * Columns - 1;
1431 else
1432 // Use up to 'showcmd' column.
1433 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001434 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1435 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001436 }
1437 else
1438 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001439 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001440
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001441 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001442 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 if (msgbuf != NULL)
1444 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001445 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001446 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001447 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1448 // empty for the search_stat feature.
1449 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001450 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001451 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001453 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1454 {
1455 // Use a space to draw the composing char on.
1456 msgbuf[1] = ' ';
1457 mch_memmove(msgbuf + 2, p, STRLEN(p));
1458 }
1459 else
1460 mch_memmove(msgbuf + 1, p, STRLEN(p));
1461 if (off_len > 0)
1462 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001464 trunc = msg_strtrunc(msgbuf, TRUE);
1465 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001467 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001468 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001471#ifdef FEAT_RIGHTLEFT
1472 // The search pattern could be shown on the right in
1473 // rightleft mode, but the 'ruler' and 'showcmd' area use
1474 // it too, thus it would be blanked out again very soon.
1475 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001476 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1477 {
1478 char_u *r;
1479 size_t pat_len;
1480
1481 r = reverse_text(msgbuf);
1482 if (r != NULL)
1483 {
1484 vim_free(msgbuf);
1485 msgbuf = r;
1486 // move reversed text to beginning of buffer
1487 while (*r != NUL && *r == ' ')
1488 r++;
1489 pat_len = msgbuf + STRLEN(msgbuf) - r;
1490 mch_memmove(msgbuf, r, pat_len);
1491 // overwrite old text
1492 if ((size_t)(r - msgbuf) >= pat_len)
1493 vim_memset(r, ' ', pat_len);
1494 else
1495 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1496 }
1497 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001498#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001499 msg_outtrans(msgbuf);
1500 msg_clr_eos();
1501 msg_check();
1502
1503 gotocmdline(FALSE);
1504 out_flush();
1505 msg_nowait = TRUE; // don't wait for this message
1506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 }
1508 }
1509
1510 /*
1511 * If there is a character offset, subtract it from the current
1512 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001513 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * This is not done for a line offset, because then we would not be vi
1515 * compatible.
1516 */
Bram Moolenaared203462004-06-16 11:19:22 +00001517 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
1519 if (spats[0].off.off > 0)
1520 {
1521 for (c = spats[0].off.off; c; --c)
1522 if (decl(&pos) == -1)
1523 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001524 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001526 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 pos.col = MAXCOL;
1528 }
1529 }
1530 else
1531 {
1532 for (c = spats[0].off.off; c; ++c)
1533 if (incl(&pos) == -1)
1534 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001535 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1538 pos.col = 0;
1539 }
1540 }
1541 }
1542
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001543 /*
1544 * The actual search.
1545 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001546 c = searchit(curwin, curbuf, &pos, NULL,
1547 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 searchstr, count, spats[0].off.end + (options &
1549 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1550 + SEARCH_MSG + SEARCH_START
1551 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001552 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
1554 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001555 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001556
1557 if (!shortmess(SHM_SEARCH)
1558 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1559 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001560 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (c == FAIL)
1563 {
1564 retval = 0;
1565 goto end_do_search;
1566 }
1567 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001568 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001570 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572 /*
1573 * Add character and/or line offset
1574 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001575 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001577 pos_T org_pos = pos;
1578
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001579 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 {
1581 c = pos.lnum + spats[0].off.off;
1582 if (c < 1)
1583 pos.lnum = 1;
1584 else if (c > curbuf->b_ml.ml_line_count)
1585 pos.lnum = curbuf->b_ml.ml_line_count;
1586 else
1587 pos.lnum = c;
1588 pos.col = 0;
1589
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001590 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001592 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001594 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 c = spats[0].off.off;
1596 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001598 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (incl(&pos) == -1)
1600 break;
1601 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001602 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 else
1604 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001605 while (c++ < 0)
1606 if (decl(&pos) == -1)
1607 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001610 if (!EQUAL_POS(pos, org_pos))
1611 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001614 // Show [1/15] if 'S' is not in 'shortmess'.
1615 if ((options & SEARCH_ECHO)
1616 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001617 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001618 && c != FAIL
1619 && !shortmess(SHM_SEARCHCOUNT)
1620 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001621 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1622 show_top_bot_msg, msgbuf,
1623 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001624#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001625 || (!(fdo_flags & FDO_SEARCH)
1626 && hasFolding(curwin->w_cursor.lnum,
1627 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001628#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001629 ),
1630 SEARCH_STAT_DEF_MAX_COUNT,
1631 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 /*
1634 * The search command can be followed by a ';' to do another search.
1635 * For example: "/pat/;/foo/+3;?bar"
1636 * This is like doing another search command, except:
1637 * - The remembered direction '/' or '?' is from the first search.
1638 * - When an error happens the cursor isn't moved at all.
1639 * Don't do this when called by get_address() (it handles ';' itself).
1640 */
1641 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1642 break;
1643
1644 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001645 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (dirc != '?' && dirc != '/')
1647 {
1648 retval = 0;
Bram Moolenaarac78dd42022-01-02 19:25:26 +00001649 emsg(_(e_expected_question_or_slash_after_semicolon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 goto end_do_search;
1651 }
1652 ++pat;
1653 }
1654
1655 if (options & SEARCH_MARK)
1656 setpcmark();
1657 curwin->w_cursor = pos;
1658 curwin->w_set_curswant = TRUE;
1659
1660end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001661 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 spats[0].off = old_off;
1663 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001664 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
1666 return retval;
1667}
1668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669/*
1670 * search_for_exact_line(buf, pos, dir, pat)
1671 *
1672 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001673 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001675 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 * Return OK for success, or FAIL if no line found.
1677 */
1678 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001679search_for_exact_line(
1680 buf_T *buf,
1681 pos_T *pos,
1682 int dir,
1683 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 linenr_T start = 0;
1686 char_u *ptr;
1687 char_u *p;
1688
1689 if (buf->b_ml.ml_line_count == 0)
1690 return FAIL;
1691 for (;;)
1692 {
1693 pos->lnum += dir;
1694 if (pos->lnum < 1)
1695 {
1696 if (p_ws)
1697 {
1698 pos->lnum = buf->b_ml.ml_line_count;
1699 if (!shortmess(SHM_SEARCH))
1700 give_warning((char_u *)_(top_bot_msg), TRUE);
1701 }
1702 else
1703 {
1704 pos->lnum = 1;
1705 break;
1706 }
1707 }
1708 else if (pos->lnum > buf->b_ml.ml_line_count)
1709 {
1710 if (p_ws)
1711 {
1712 pos->lnum = 1;
1713 if (!shortmess(SHM_SEARCH))
1714 give_warning((char_u *)_(bot_top_msg), TRUE);
1715 }
1716 else
1717 {
1718 pos->lnum = 1;
1719 break;
1720 }
1721 }
1722 if (pos->lnum == start)
1723 break;
1724 if (start == 0)
1725 start = pos->lnum;
1726 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1727 p = skipwhite(ptr);
1728 pos->col = (colnr_T) (p - ptr);
1729
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001730 // when adding lines the matching line may be empty but it is not
1731 // ignored because we are interested in the next line -- Acevedo
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001732 if (compl_status_adding() && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 {
1734 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1735 return OK;
1736 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001737 else if (*p != NUL) // ignore empty lines
1738 { // expanding lines or words
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00001739 if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len())
1740 : STRNCMP(p, pat, ins_compl_len())) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 return OK;
1742 }
1743 }
1744 return FAIL;
1745}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
1747/*
1748 * Character Searches
1749 */
1750
1751/*
1752 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1753 * position of the character, otherwise move to just before the char.
1754 * Do this "cap->count1" times.
1755 * Return FAIL or OK.
1756 */
1757 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001758searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001760 int c = cap->nchar; // char to search for
1761 int dir = cap->arg; // TRUE for searching forward
1762 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 int col;
1764 char_u *p;
1765 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001766 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001768 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001770 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001772 *lastc = c;
1773 set_csearch_direction(dir);
1774 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001775 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (cap->ncharC1 != 0)
1777 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001778 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1779 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001781 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1782 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001786 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001788 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001790 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 dir = -lastcdir;
1792 else
1793 dir = lastcdir;
1794 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001795 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001796 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001797
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001798 // Force a move of at least one char, so ";" and "," will move the
1799 // cursor, even if the cursor is right in front of char we are looking
1800 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001801 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001802 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001805 if (dir == BACKWARD)
1806 cap->oap->inclusive = FALSE;
1807 else
1808 cap->oap->inclusive = TRUE;
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 p = ml_get_curline();
1811 col = curwin->w_cursor.col;
1812 len = (int)STRLEN(p);
1813
1814 while (count--)
1815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 if (has_mbyte)
1817 {
1818 for (;;)
1819 {
1820 if (dir > 0)
1821 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001822 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (col >= len)
1824 return FAIL;
1825 }
1826 else
1827 {
1828 if (col == 0)
1829 return FAIL;
1830 col -= (*mb_head_off)(p, p + col - 1) + 1;
1831 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001832 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001834 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 break;
1836 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001837 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001838 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001839 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001840 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842 }
1843 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 for (;;)
1846 {
1847 if ((col += dir) < 0 || col >= len)
1848 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001849 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001851 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 }
1853 }
1854 }
1855
1856 if (t_cmd)
1857 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001858 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (has_mbyte)
1861 {
1862 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001863 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001864 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001866 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 col -= (*mb_head_off)(p, p + col);
1868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870 curwin->w_cursor.col = col;
1871
1872 return OK;
1873}
1874
1875/*
1876 * "Other" Searches
1877 */
1878
1879/*
1880 * findmatch - find the matching paren or brace
1881 *
1882 * Improvement over vi: Braces inside quotes are ignored.
1883 */
1884 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001885findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886{
1887 return findmatchlimit(oap, initc, 0, 0);
1888}
1889
1890/*
1891 * Return TRUE if the character before "linep[col]" equals "ch".
1892 * Return FALSE if "col" is zero.
1893 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1894 * is NULL.
1895 * Handles multibyte string correctly.
1896 */
1897 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001898check_prevcol(
1899 char_u *linep,
1900 int col,
1901 int ch,
1902 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903{
1904 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (col > 0 && has_mbyte)
1906 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (prevcol)
1908 *prevcol = col;
1909 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1910}
1911
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001912/*
1913 * Raw string start is found at linep[startpos.col - 1].
1914 * Return TRUE if the matching end can be found between startpos and endpos.
1915 */
1916 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001917find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001918{
1919 char_u *p;
1920 char_u *delim_copy;
1921 size_t delim_len;
1922 linenr_T lnum;
1923 int found = FALSE;
1924
1925 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1926 ;
1927 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001928 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001929 if (delim_copy == NULL)
1930 return FALSE;
1931 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1932 {
1933 char_u *line = ml_get(lnum);
1934
1935 for (p = line + (lnum == startpos->lnum
1936 ? startpos->col + 1 : 0); *p; ++p)
1937 {
1938 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1939 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02001940 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
1941 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001942 {
1943 found = TRUE;
1944 break;
1945 }
1946 }
1947 if (found)
1948 break;
1949 }
1950 vim_free(delim_copy);
1951 return found;
1952}
1953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001955 * Check matchpairs option for "*initc".
1956 * If there is a match set "*initc" to the matching character and "*findc" to
1957 * the opposite character. Set "*backwards" to the direction.
1958 * When "switchit" is TRUE swap the direction.
1959 */
1960 static void
1961find_mps_values(
1962 int *initc,
1963 int *findc,
1964 int *backwards,
1965 int switchit)
1966{
1967 char_u *ptr;
1968
1969 ptr = curbuf->b_p_mps;
1970 while (*ptr != NUL)
1971 {
1972 if (has_mbyte)
1973 {
1974 char_u *prev;
1975
1976 if (mb_ptr2char(ptr) == *initc)
1977 {
1978 if (switchit)
1979 {
1980 *findc = *initc;
1981 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1982 *backwards = TRUE;
1983 }
1984 else
1985 {
1986 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1987 *backwards = FALSE;
1988 }
1989 return;
1990 }
1991 prev = ptr;
1992 ptr += mb_ptr2len(ptr) + 1;
1993 if (mb_ptr2char(ptr) == *initc)
1994 {
1995 if (switchit)
1996 {
1997 *findc = *initc;
1998 *initc = mb_ptr2char(prev);
1999 *backwards = FALSE;
2000 }
2001 else
2002 {
2003 *findc = mb_ptr2char(prev);
2004 *backwards = TRUE;
2005 }
2006 return;
2007 }
2008 ptr += mb_ptr2len(ptr);
2009 }
2010 else
2011 {
2012 if (*ptr == *initc)
2013 {
2014 if (switchit)
2015 {
2016 *backwards = TRUE;
2017 *findc = *initc;
2018 *initc = ptr[2];
2019 }
2020 else
2021 {
2022 *backwards = FALSE;
2023 *findc = ptr[2];
2024 }
2025 return;
2026 }
2027 ptr += 2;
2028 if (*ptr == *initc)
2029 {
2030 if (switchit)
2031 {
2032 *backwards = FALSE;
2033 *findc = *initc;
2034 *initc = ptr[-2];
2035 }
2036 else
2037 {
2038 *backwards = TRUE;
2039 *findc = ptr[-2];
2040 }
2041 return;
2042 }
2043 ++ptr;
2044 }
2045 if (*ptr == ',')
2046 ++ptr;
2047 }
2048}
2049
2050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002052 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2053 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 *
2055 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002056 * character at or after the cursor. Special values:
2057 * '*' look for C-style comment / *
2058 * '/' look for C-style comment / *, ignoring comment-end
2059 * '#' look for preprocessor directives
2060 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 *
2062 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2063 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2064 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2065 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002066 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002067 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002068 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002071findmatchlimit(
2072 oparg_T *oap,
2073 int initc,
2074 int flags,
2075 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002077 static pos_T pos; // current search position
2078 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002080 int count = 0; // cumulative number of braces
2081 int backwards = FALSE; // init for gcc
2082 int raw_string = FALSE; // search for raw string
2083 int inquote = FALSE; // TRUE when inside quotes
2084 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002086 int do_quotes; // check for quotes in current line
2087 int at_start; // do_quotes value at start position
2088 int hash_dir = 0; // Direction searched for # things
2089 int comment_dir = 0; // Direction searched for comments
2090 pos_T match_pos; // Where last slash-star was found
2091 int start_in_quotes; // start position is in quotes
2092 int traveled = 0; // how far we've searched so far
2093 int ignore_cend = FALSE; // ignore comment end
2094 int cpo_match; // vi compatible matching
2095 int cpo_bsl; // don't recognize backslashes
2096 int match_escaped = 0; // search for escaped match
2097 int dir; // Direction to search
2098 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002099 int lispcomm = FALSE; // inside of Lisp-style comment
2100 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002103 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 linep = ml_get(pos.lnum);
2105
2106 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2107 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2108
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002109 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (flags & FM_BACKWARD)
2111 dir = BACKWARD;
2112 else if (flags & FM_FORWARD)
2113 dir = FORWARD;
2114 else
2115 dir = 0;
2116
2117 /*
2118 * if initc given, look in the table for the matching character
2119 * '/' and '*' are special cases: look for start or end of comment.
2120 * When '/' is used, we ignore running backwards into an star-slash, for
2121 * "[*" command, we just want to find any comment.
2122 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002123 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
2125 comment_dir = dir;
2126 if (initc == '/')
2127 ignore_cend = TRUE;
2128 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002129 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 initc = NUL;
2131 }
2132 else if (initc != '#' && initc != NUL)
2133 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002134 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002135 if (dir)
2136 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002137 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 return NULL;
2139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 else
2141 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002142 /*
2143 * Either initc is '#', or no initc was given and we need to look
2144 * under the cursor.
2145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (initc == '#')
2147 {
2148 hash_dir = dir;
2149 }
2150 else
2151 {
2152 /*
2153 * initc was not given, must look for something to match under
2154 * or near the cursor.
2155 * Only check for special things when 'cpo' doesn't have '%'.
2156 */
2157 if (!cpo_match)
2158 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002159 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 ptr = skipwhite(linep);
2161 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2162 {
2163 ptr = skipwhite(ptr + 1);
2164 if ( STRNCMP(ptr, "if", 2) == 0
2165 || STRNCMP(ptr, "endif", 5) == 0
2166 || STRNCMP(ptr, "el", 2) == 0)
2167 hash_dir = 1;
2168 }
2169
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002170 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 else if (linep[pos.col] == '/')
2172 {
2173 if (linep[pos.col + 1] == '*')
2174 {
2175 comment_dir = FORWARD;
2176 backwards = FALSE;
2177 pos.col++;
2178 }
2179 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2180 {
2181 comment_dir = BACKWARD;
2182 backwards = TRUE;
2183 pos.col--;
2184 }
2185 }
2186 else if (linep[pos.col] == '*')
2187 {
2188 if (linep[pos.col + 1] == '/')
2189 {
2190 comment_dir = BACKWARD;
2191 backwards = TRUE;
2192 }
2193 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2194 {
2195 comment_dir = FORWARD;
2196 backwards = FALSE;
2197 }
2198 }
2199 }
2200
2201 /*
2202 * If we are not on a comment or the # at the start of a line, then
2203 * look for brace anywhere on this line after the cursor.
2204 */
2205 if (!hash_dir && !comment_dir)
2206 {
2207 /*
2208 * Find the brace under or after the cursor.
2209 * If beyond the end of the line, use the last character in
2210 * the line.
2211 */
2212 if (linep[pos.col] == NUL && pos.col)
2213 --pos.col;
2214 for (;;)
2215 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002216 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (initc == NUL)
2218 break;
2219
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002220 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (findc)
2222 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002223 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 if (!findc)
2226 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002227 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (!cpo_match && *skipwhite(linep) == '#')
2229 hash_dir = 1;
2230 else
2231 return NULL;
2232 }
2233 else if (!cpo_bsl)
2234 {
2235 int col, bslcnt = 0;
2236
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002237 // Set "match_escaped" if there are an odd number of
2238 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2240 bslcnt++;
2241 match_escaped = (bslcnt & 1);
2242 }
2243 }
2244 }
2245 if (hash_dir)
2246 {
2247 /*
2248 * Look for matching #if, #else, #elif, or #endif
2249 */
2250 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002251 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (initc != '#')
2253 {
2254 ptr = skipwhite(skipwhite(linep) + 1);
2255 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2256 hash_dir = 1;
2257 else if (STRNCMP(ptr, "endif", 5) == 0)
2258 hash_dir = -1;
2259 else
2260 return NULL;
2261 }
2262 pos.col = 0;
2263 while (!got_int)
2264 {
2265 if (hash_dir > 0)
2266 {
2267 if (pos.lnum == curbuf->b_ml.ml_line_count)
2268 break;
2269 }
2270 else if (pos.lnum == 1)
2271 break;
2272 pos.lnum += hash_dir;
2273 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002274 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ptr = skipwhite(linep);
2276 if (*ptr != '#')
2277 continue;
2278 pos.col = (colnr_T) (ptr - linep);
2279 ptr = skipwhite(ptr + 1);
2280 if (hash_dir > 0)
2281 {
2282 if (STRNCMP(ptr, "if", 2) == 0)
2283 count++;
2284 else if (STRNCMP(ptr, "el", 2) == 0)
2285 {
2286 if (count == 0)
2287 return &pos;
2288 }
2289 else if (STRNCMP(ptr, "endif", 5) == 0)
2290 {
2291 if (count == 0)
2292 return &pos;
2293 count--;
2294 }
2295 }
2296 else
2297 {
2298 if (STRNCMP(ptr, "if", 2) == 0)
2299 {
2300 if (count == 0)
2301 return &pos;
2302 count--;
2303 }
2304 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2305 {
2306 if (count == 0)
2307 return &pos;
2308 }
2309 else if (STRNCMP(ptr, "endif", 5) == 0)
2310 count++;
2311 }
2312 }
2313 return NULL;
2314 }
2315 }
2316
2317#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002318 // This is just guessing: when 'rightleft' is set, search for a matching
2319 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2321 backwards = !backwards;
2322#endif
2323
2324 do_quotes = -1;
2325 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002326 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002327
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002328 // backward search: Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002329 if ((backwards && comment_dir) || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002331 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002332 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002333
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 while (!got_int)
2335 {
2336 /*
2337 * Go to the next position, forward or backward. We could use
2338 * inc() and dec() here, but that is much slower
2339 */
2340 if (backwards)
2341 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002342 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002343 if (lispcomm && pos.col < (colnr_T)comment_col)
2344 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002345 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002347 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 break;
2349 --pos.lnum;
2350
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002351 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 break;
2353
2354 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002355 pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 do_quotes = -1;
2357 line_breakcheck();
2358
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002359 // Check if this line contains a single-line comment
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002360 if (comment_dir || lisp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 comment_col = check_linecomment(linep);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002362 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002363 if (lisp && comment_col != MAXCOL)
2364 pos.col = comment_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
2366 else
2367 {
2368 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 if (has_mbyte)
2370 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002373 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002375 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 // at end of line, go to next one
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002377 // For lisp don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002378 || (lisp && comment_col != MAXCOL
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002379 && pos.col == (colnr_T)comment_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002381 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002382 // line is exhausted and comment with it,
2383 // don't search for match in code
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002384 || lispcomm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 break;
2386 ++pos.lnum;
2387
2388 if (maxtravel && traveled++ > maxtravel)
2389 break;
2390
2391 linep = ml_get(pos.lnum);
2392 pos.col = 0;
2393 do_quotes = -1;
2394 line_breakcheck();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002395 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002396 comment_col = check_linecomment(linep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398 else
2399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002401 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 ++pos.col;
2404 }
2405 }
2406
2407 /*
2408 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2409 */
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002410 if (pos.col == 0 && (flags & FM_BLOCKSTOP)
2411 && (linep[0] == '{' || linep[0] == '}'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002413 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002415 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417
2418 if (comment_dir)
2419 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002420 // Note: comments do not nest, and we ignore quotes in them
2421 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (comment_dir == FORWARD)
2423 {
2424 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2425 {
2426 pos.col++;
2427 return &pos;
2428 }
2429 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002430 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 /*
2433 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002434 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 if (pos.col == 0)
2437 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002438 else if (raw_string)
2439 {
2440 if (linep[pos.col - 1] == 'R'
2441 && linep[pos.col] == '"'
2442 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2443 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002444 // Possible start of raw string. Now that we have the
2445 // delimiter we can check if it ends before where we
2446 // started searching, or before the previously found
2447 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002448 if (!find_rawstring_end(linep, &pos,
2449 count > 0 ? &match_pos : &curwin->w_cursor))
2450 {
2451 count++;
2452 match_pos = pos;
2453 match_pos.col--;
2454 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002455 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002456 }
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 else if ( linep[pos.col - 1] == '/'
2459 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002460 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 && (int)pos.col < comment_col)
2462 {
2463 count++;
2464 match_pos = pos;
2465 match_pos.col--;
2466 }
2467 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2468 {
2469 if (count > 0)
2470 pos = match_pos;
2471 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2472 && (int)pos.col <= comment_col)
2473 pos.col -= 2;
2474 else if (ignore_cend)
2475 continue;
2476 else
2477 return NULL;
2478 return &pos;
2479 }
2480 }
2481 continue;
2482 }
2483
2484 /*
2485 * If smart matching ('cpoptions' does not contain '%'), braces inside
2486 * of quotes are ignored, but only if there is an even number of
2487 * quotes in the line.
2488 */
2489 if (cpo_match)
2490 do_quotes = 0;
2491 else if (do_quotes == -1)
2492 {
2493 /*
2494 * Count the number of quotes in the line, skipping \" and '"'.
2495 * Watch out for "\\".
2496 */
2497 at_start = do_quotes;
2498 for (ptr = linep; *ptr; ++ptr)
2499 {
2500 if (ptr == linep + pos.col + backwards)
2501 at_start = (do_quotes & 1);
2502 if (*ptr == '"'
2503 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2504 ++do_quotes;
2505 if (*ptr == '\\' && ptr[1] != NUL)
2506 ++ptr;
2507 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002508 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 /*
2511 * If we find an uneven count, check current line and previous
2512 * one for a '\' at the end.
2513 */
2514 if (!do_quotes)
2515 {
2516 inquote = FALSE;
2517 if (ptr[-1] == '\\')
2518 {
2519 do_quotes = 1;
2520 if (start_in_quotes == MAYBE)
2521 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002522 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 inquote = TRUE;
2524 start_in_quotes = TRUE;
2525 }
2526 else if (backwards)
2527 inquote = TRUE;
2528 }
2529 if (pos.lnum > 1)
2530 {
2531 ptr = ml_get(pos.lnum - 1);
2532 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2533 {
2534 do_quotes = 1;
2535 if (start_in_quotes == MAYBE)
2536 {
2537 inquote = at_start;
2538 if (inquote)
2539 start_in_quotes = TRUE;
2540 }
2541 else if (!backwards)
2542 inquote = TRUE;
2543 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002544
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002545 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002546 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548 }
2549 }
2550 if (start_in_quotes == MAYBE)
2551 start_in_quotes = FALSE;
2552
2553 /*
2554 * If 'smartmatch' is set:
2555 * Things inside quotes are ignored by setting 'inquote'. If we
2556 * find a quote without a preceding '\' invert 'inquote'. At the
2557 * end of a line not ending in '\' we reset 'inquote'.
2558 *
2559 * In lines with an uneven number of quotes (without preceding '\')
2560 * we do not know which part to ignore. Therefore we only set
2561 * inquote if the number of quotes in a line is even, unless this
2562 * line or the previous one ends in a '\'. Complicated, isn't it?
2563 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002564 c = PTR2CHAR(linep + pos.col);
2565 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
2567 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002568 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2570 {
2571 inquote = FALSE;
2572 start_in_quotes = FALSE;
2573 }
2574 break;
2575
2576 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002577 // a quote that is preceded with an odd number of backslashes is
2578 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (do_quotes)
2580 {
2581 int col;
2582
2583 for (col = pos.col - 1; col >= 0; --col)
2584 if (linep[col] != '\\')
2585 break;
2586 if ((((int)pos.col - 1 - col) & 1) == 0)
2587 {
2588 inquote = !inquote;
2589 start_in_quotes = FALSE;
2590 }
2591 }
2592 break;
2593
2594 /*
2595 * If smart matching ('cpoptions' does not contain '%'):
2596 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2597 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2598 * skipped, there is never a brace in them.
2599 * Ignore this when finding matches for `'.
2600 */
2601 case '\'':
2602 if (!cpo_match && initc != '\'' && findc != '\'')
2603 {
2604 if (backwards)
2605 {
2606 if (pos.col > 1)
2607 {
2608 if (linep[pos.col - 2] == '\'')
2609 {
2610 pos.col -= 2;
2611 break;
2612 }
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002613 else if (linep[pos.col - 2] == '\\'
2614 && pos.col > 2 && linep[pos.col - 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 {
2616 pos.col -= 3;
2617 break;
2618 }
2619 }
2620 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002621 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01002623 if (linep[pos.col + 1] == '\\'
2624 && linep[pos.col + 2] && linep[pos.col + 3] == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
2626 pos.col += 3;
2627 break;
2628 }
2629 else if (linep[pos.col + 2] == '\'')
2630 {
2631 pos.col += 2;
2632 break;
2633 }
2634 }
2635 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002636 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638 default:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002639 /*
2640 * For Lisp skip over backslashed (), {} and [].
2641 * (actually, we skip #\( et al)
2642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (curbuf->b_p_lisp
Bram Moolenaarebfec1c2023-01-22 21:14:53 +00002644 && vim_strchr((char_u *)"{}()[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002645 && pos.col > 1
2646 && check_prevcol(linep, pos.col, '\\', NULL)
2647 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002650 // Check for match outside of quotes, and inside of
2651 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if ((!inquote || start_in_quotes == TRUE)
2653 && (c == initc || c == findc))
2654 {
2655 int col, bslcnt = 0;
2656
2657 if (!cpo_bsl)
2658 {
2659 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2660 bslcnt++;
2661 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002662 // Only accept a match when 'M' is in 'cpo' or when escaping
2663 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2665 {
2666 if (c == initc)
2667 count++;
2668 else
2669 {
2670 if (count == 0)
2671 return &pos;
2672 count--;
2673 }
2674 }
2675 }
2676 }
2677 }
2678
2679 if (comment_dir == BACKWARD && count > 0)
2680 {
2681 pos = match_pos;
2682 return &pos;
2683 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002684 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685}
2686
2687/*
2688 * Check if line[] contains a / / comment.
2689 * Return MAXCOL if not, otherwise return the column.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 */
Bram Moolenaar6e371ec2021-12-12 14:16:39 +00002691 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002692check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 char_u *p;
2695
2696 p = line;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002697 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002698 if (curbuf->b_p_lisp)
2699 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002700 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002701 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002702 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002703
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002704 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002705 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002706 {
2707 if (*p == '"')
2708 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002709 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002710 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002711 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002712 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002713 }
2714 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002715 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002716 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002717 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002718 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002719 else if (!in_str && ((p - line) < 2
Bram Moolenaarba263672021-12-29 18:09:13 +00002720 || (*(p - 1) != '\\' && *(p - 2) != '#'))
2721 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002722 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002723 ++p;
2724 }
2725 }
2726 else
2727 p = NULL;
2728 }
2729 else
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002730 while ((p = vim_strchr(p, '/')) != NULL)
2731 {
2732 // Accept a double /, unless it's preceded with * and followed by
2733 // *, because * / / * is an end and start of a C comment. Only
2734 // accept the position if it is not inside a string.
2735 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')
Bram Moolenaarba263672021-12-29 18:09:13 +00002736 && !is_pos_in_string(line, (colnr_T)(p - line)))
Bram Moolenaar8e145b82022-05-21 20:17:31 +01002737 break;
2738 ++p;
2739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 if (p == NULL)
2742 return MAXCOL;
2743 return (int)(p - line);
2744}
2745
2746/*
2747 * Move cursor briefly to character matching the one under the cursor.
2748 * Used for Insert mode and "r" command.
2749 * Show the match only if it is visible on the screen.
2750 * If there isn't a match, then beep.
2751 */
2752 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002753showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002754 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755{
2756 pos_T *lpos, save_cursor;
2757 pos_T mpos;
2758 colnr_T vcol;
2759 long save_so;
2760 long save_siso;
2761#ifdef CURSOR_SHAPE
2762 int save_state;
2763#endif
2764 colnr_T save_dollar_vcol;
2765 char_u *p;
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01002766 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2767 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768
2769 /*
2770 * Only show match for chars in the 'matchpairs' option.
2771 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002772 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002773 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002776 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002777 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002778#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002779 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002780 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781#ifdef FEAT_RIGHTLEFT
2782 && !(curwin->w_p_rl ^ p_ri)
2783#endif
2784 )
2785 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002786 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002787 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return;
2789 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002790 if (*p == NUL)
2791 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002793 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 {
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002795 vim_beep(BO_MATCH);
2796 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00002798
2799 if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline)
2800 return;
2801
2802 if (!curwin->w_p_wrap)
2803 getvcol(curwin, lpos, NULL, &vcol, NULL);
2804
2805 int col_visible = (curwin->w_p_wrap
2806 || (vcol >= curwin->w_leftcol
2807 && vcol < curwin->w_leftcol + curwin->w_width));
2808 if (!col_visible)
2809 return;
2810
2811 mpos = *lpos; // save the pos, update_screen() may change it
2812 save_cursor = curwin->w_cursor;
2813 save_so = *so;
2814 save_siso = *siso;
2815 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2816 // stop displaying the "$".
2817 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2818 dollar_vcol = -1;
2819 ++curwin->w_virtcol; // do display ')' just before "$"
2820 update_screen(UPD_VALID); // show the new char first
2821
2822 save_dollar_vcol = dollar_vcol;
2823#ifdef CURSOR_SHAPE
2824 save_state = State;
2825 State = MODE_SHOWMATCH;
2826 ui_cursor_shape(); // may show different cursor shape
2827#endif
2828 curwin->w_cursor = mpos; // move to matching char
2829 *so = 0; // don't use 'scrolloff' here
2830 *siso = 0; // don't use 'sidescrolloff' here
2831 showruler(FALSE);
2832 setcursor();
2833 cursor_on(); // make sure that the cursor is shown
2834 out_flush_cursor(TRUE, FALSE);
2835
2836 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2837 // which resets it if the matching position is in a previous line
2838 // and has a higher column number.
2839 dollar_vcol = save_dollar_vcol;
2840
2841 /*
2842 * brief pause, unless 'm' is present in 'cpo' and a character is
2843 * available.
2844 */
2845 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2846 ui_delay(p_mat * 100L + 8, TRUE);
2847 else if (!char_avail())
2848 ui_delay(p_mat * 100L + 9, FALSE);
2849 curwin->w_cursor = save_cursor; // restore cursor position
2850 *so = save_so;
2851 *siso = save_siso;
2852#ifdef CURSOR_SHAPE
2853 State = save_state;
2854 ui_cursor_shape(); // may show different cursor shape
2855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856}
2857
2858/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002859 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002860 * If move is TRUE, check from the beginning of the buffer, else from position
2861 * "cur".
2862 * "direction" is FORWARD or BACKWARD.
2863 * Returns TRUE, FALSE or -1 for failure.
2864 */
2865 static int
2866is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2867{
2868 regmmatch_T regmatch;
2869 int nmatched = 0;
2870 int result = -1;
2871 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002872 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002873 int flag = 0;
2874
2875 if (pattern == NULL)
2876 pattern = spats[last_idx].pat;
2877
Rob Pillinge86190e2022-12-23 19:06:04 +00002878 if (search_regcomp(pattern, NULL, RE_SEARCH, RE_SEARCH,
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002879 SEARCH_KEEP, &regmatch) == FAIL)
2880 return -1;
2881
2882 // init startcol correctly
2883 regmatch.startpos[0].col = -1;
2884 // move to match
2885 if (move)
2886 {
2887 CLEAR_POS(&pos);
2888 }
2889 else
2890 {
2891 pos = *cur;
2892 // accept a match at the cursor position
2893 flag = SEARCH_START;
2894 }
2895
2896 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2897 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2898 {
2899 // Zero-width pattern should match somewhere, then we can check if
2900 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002901 do
2902 {
2903 regmatch.startpos[0].col++;
2904 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Paul Ollis65745772022-06-05 16:55:54 +01002905 pos.lnum, regmatch.startpos[0].col, NULL);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002906 if (nmatched != 0)
2907 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002908 } while (regmatch.regprog != NULL
2909 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002910 : regmatch.startpos[0].col > pos.col);
2911
Bram Moolenaar53989552019-12-23 22:59:18 +01002912 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002913 {
2914 result = (nmatched != 0
2915 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2916 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2917 }
2918 }
2919
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002920 vim_regfree(regmatch.regprog);
2921 return result;
2922}
2923
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002924
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002925/*
2926 * Find next search match under cursor, cursor at end.
2927 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002928 */
2929 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002930current_search(
2931 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002932 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002933{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002934 pos_T start_pos; // start position of the pattern match
2935 pos_T end_pos; // end position of the pattern match
2936 pos_T orig_pos; // position of the cursor at beginning
2937 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002938 int i;
2939 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002940 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002941 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002942 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002943 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002944 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002945 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002946
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002947 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002948 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002949 dec_cursor();
2950
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002951 // When searching forward and the cursor is at the start of the Visual
2952 // area, skip the first search backward, otherwise it doesn't move.
2953 skip_first_backward = forward && VIsual_active
2954 && LT_POS(curwin->w_cursor, VIsual);
2955
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002956 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002957 if (VIsual_active)
2958 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002959 if (forward)
2960 incl(&pos);
2961 else
2962 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002963 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002964
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002965 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002966 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02002967 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002968 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002969 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002970
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002971 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002972 * The trick is to first search backwards and then search forward again,
2973 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002974 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002975 */
2976 for (i = 0; i < 2; i++)
2977 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002978 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002979 {
2980 if (i == 0 && skip_first_backward)
2981 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002982 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002983 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002984 else
2985 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002986
2987 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002988 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002989 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002990 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002991
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002992 // wrapping should not occur in the first round
2993 if (i == 0)
2994 p_ws = FALSE;
2995
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002996 result = searchit(curwin, curbuf, &pos, &end_pos,
2997 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002998 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002999 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003000
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003001 p_ws = old_p_ws;
3002
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003003 // First search may fail, but then start searching from the
3004 // beginning of the file (cursor might be on the search match)
3005 // except when Visual mode is active, so that extending the visual
3006 // selection works.
3007 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003008 {
3009 curwin->w_cursor = orig_pos;
3010 if (VIsual_active)
3011 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003012 return FAIL;
3013 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003014 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003015 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003016 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003017 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003018 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003019 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003020 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003021 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003022 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003023 // try again from end of buffer
3024 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003025 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003026 pos.col = (colnr_T)STRLEN(
3027 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003028 }
3029 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003030 }
3031
3032 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003033
3034 if (!VIsual_active)
3035 VIsual = start_pos;
3036
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003037 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003038 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003039 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003040 {
3041 if (skip_first_backward)
3042 // put the cursor on the start of the match
3043 curwin->w_cursor = pos;
3044 else
3045 // put the cursor on last character of match
3046 dec_cursor();
3047 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003048 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003049 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003050 VIsual_active = TRUE;
3051 VIsual_mode = 'v';
3052
Bram Moolenaarb7633612019-02-10 21:48:25 +01003053 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003054 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003055 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003056 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3057 inc_cursor();
3058 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3059 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003060 }
3061
3062#ifdef FEAT_FOLDING
3063 if (fdo_flags & FDO_SEARCH && KeyTyped)
3064 foldOpenCursor();
3065#endif
3066
3067 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003068 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003069#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003070 // Make sure the clipboard gets updated. Needed because start and
3071 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003072 clip_star.vmode = NUL;
3073#endif
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003074 redraw_curbuf_later(UPD_INVERTED);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003075 showmode();
3076
3077 return OK;
3078}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003079
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080/*
3081 * return TRUE if line 'lnum' is empty or has white chars only.
3082 */
3083 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003084linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 char_u *p;
3087
3088 p = skipwhite(ml_get(lnum));
3089 return (*p == NUL);
3090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003092/*
3093 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003094 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003095 */
3096 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003097cmdline_search_stat(
3098 int dirc,
3099 pos_T *pos,
3100 pos_T *cursor_pos,
3101 int show_top_bot_msg,
3102 char_u *msgbuf,
3103 int recompute,
3104 int maxcount,
3105 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003106{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003107 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003108
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003109 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3110 timeout);
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003111 if (stat.cur <= 0)
3112 return;
3113
3114 char t[SEARCH_STAT_BUF_LEN];
3115 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003116
3117#ifdef FEAT_RIGHTLEFT
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003118 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3119 {
3120 if (stat.incomplete == 1)
3121 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3122 else if (stat.cnt > maxcount && stat.cur > maxcount)
3123 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3124 maxcount, maxcount);
3125 else if (stat.cnt > maxcount)
3126 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3127 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003128 else
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003129 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3130 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003131 }
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003132 else
3133#endif
3134 {
3135 if (stat.incomplete == 1)
3136 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
3137 else if (stat.cnt > maxcount && stat.cur > maxcount)
3138 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3139 maxcount, maxcount);
3140 else if (stat.cnt > maxcount)
3141 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3142 stat.cur, maxcount);
3143 else
3144 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3145 stat.cur, stat.cnt);
3146 }
3147
3148 len = STRLEN(t);
3149 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
3150 {
3151 mch_memmove(t + 2, t, len);
3152 t[0] = 'W';
3153 t[1] = ' ';
3154 len += 2;
3155 }
3156
zeertzjqa7d36b72023-01-31 21:13:38 +00003157 size_t msgbuf_len = STRLEN(msgbuf);
3158 if (len > msgbuf_len)
3159 len = msgbuf_len;
3160 mch_memmove(msgbuf + msgbuf_len - len, t, len);
3161
Yegappan Lakshmanan6ec66662023-01-23 20:46:21 +00003162 if (dirc == '?' && stat.cur == maxcount + 1)
3163 stat.cur = -1;
3164
3165 // keep the message even after redraw, but don't put in history
3166 msg_hist_off = TRUE;
3167 give_warning(msgbuf, FALSE);
3168 msg_hist_off = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003169}
3170
3171/*
3172 * Add the search count information to "stat".
3173 * "stat" must not be NULL.
3174 * When "recompute" is TRUE always recompute the numbers.
3175 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3176 * dirc == '/': find the next match
3177 * dirc == '?': find the previous match
3178 */
3179 static void
3180update_search_stat(
3181 int dirc,
3182 pos_T *pos,
3183 pos_T *cursor_pos,
3184 searchstat_T *stat,
3185 int recompute,
3186 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003187 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003188{
3189 int save_ws = p_ws;
3190 int wraparound = FALSE;
3191 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003192 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003193 static int cur = 0;
3194 static int cnt = 0;
3195 static int exact_match = FALSE;
3196 static int incomplete = 0;
3197 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3198 static int chgtick = 0;
3199 static char_u *lastpat = NULL;
3200 static buf_T *lbuf = NULL;
3201#ifdef FEAT_RELTIME
3202 proftime_T start;
3203#endif
3204
Yegappan Lakshmanan960dcbd2023-03-07 17:45:11 +00003205 CLEAR_POINTER(stat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003206
3207 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3208 {
3209 stat->cur = cur;
3210 stat->cnt = cnt;
3211 stat->exact_match = exact_match;
3212 stat->incomplete = incomplete;
3213 stat->last_maxcount = last_maxcount;
3214 return;
3215 }
3216 last_maxcount = maxcount;
3217
3218 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3219 || (dirc == '/' && LT_POS(p, lastpos)));
3220
3221 // If anything relevant changed the count has to be recomputed.
3222 // MB_STRNICMP ignores case, but we should not ignore case.
3223 // Unfortunately, there is no MB_STRNICMP function.
3224 // XXX: above comment should be "no MB_STRCMP function" ?
3225 if (!(chgtick == CHANGEDTICK(curbuf)
3226 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3227 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3228 && EQUAL_POS(lastpos, *cursor_pos)
3229 && lbuf == curbuf) || wraparound || cur < 0
3230 || (maxcount > 0 && cur > maxcount) || recompute)
3231 {
3232 cur = 0;
3233 cnt = 0;
3234 exact_match = FALSE;
3235 incomplete = 0;
3236 CLEAR_POS(&lastpos);
3237 lbuf = curbuf;
3238 }
3239
3240 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
3241 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 0))
3242 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3243 else
3244 {
3245 int done_search = FALSE;
3246 pos_T endpos = {0, 0, 0};
3247
3248 p_ws = FALSE;
3249#ifdef FEAT_RELTIME
3250 if (timeout > 0)
3251 profile_setlimit(timeout, &start);
3252#endif
3253 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3254 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3255 {
3256 done_search = TRUE;
3257#ifdef FEAT_RELTIME
3258 // Stop after passing the time limit.
3259 if (timeout > 0 && profile_passed_limit(&start))
3260 {
3261 incomplete = 1;
3262 break;
3263 }
3264#endif
3265 cnt++;
3266 if (LTOREQ_POS(lastpos, p))
3267 {
3268 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003269 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003270 exact_match = TRUE;
3271 }
3272 fast_breakcheck();
3273 if (maxcount > 0 && cnt > maxcount)
3274 {
3275 incomplete = 2; // max count exceeded
3276 break;
3277 }
3278 }
3279 if (got_int)
3280 cur = -1; // abort
3281 if (done_search)
3282 {
3283 vim_free(lastpat);
3284 lastpat = vim_strsave(spats[last_idx].pat);
3285 chgtick = CHANGEDTICK(curbuf);
3286 lbuf = curbuf;
3287 lastpos = p;
3288 }
3289 }
3290 stat->cur = cur;
3291 stat->cnt = cnt;
3292 stat->exact_match = exact_match;
3293 stat->incomplete = incomplete;
3294 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003295 p_ws = save_ws;
3296}
3297
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298#if defined(FEAT_FIND_ID) || defined(PROTO)
Bram Moolenaar409510c2022-06-01 15:23:13 +01003299
3300/*
3301 * Get line "lnum" and copy it into "buf[LSIZE]".
3302 * The copy is made because the regexp may make the line invalid when using a
3303 * mark.
3304 */
3305 static char_u *
3306get_line_and_copy(linenr_T lnum, char_u *buf)
3307{
3308 char_u *line = ml_get(lnum);
3309
3310 vim_strncpy(buf, line, LSIZE - 1);
3311 return buf;
3312}
3313
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314/*
3315 * Find identifiers or defines in included files.
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003316 * If p_ic && compl_status_sol() then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003319find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003320 char_u *ptr, // pointer to search pattern
3321 int dir UNUSED, // direction of expansion
3322 int len, // length of search pattern
3323 int whole, // match whole words only
3324 int skip_comments, // don't match inside comments
3325 int type, // Type of search; are we looking for a type?
3326 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003327 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003328 int action, // What to do when we find it
3329 linenr_T start_lnum, // first line to start searching
3330 linenr_T end_lnum) // last line for searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003332 SearchedFile *files; // Stack of included files
3333 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 int max_path_depth = 50;
3335 long match_count = 1;
3336
3337 char_u *pat;
3338 char_u *new_fname;
3339 char_u *curr_fname = curbuf->b_fname;
3340 char_u *prev_fname = NULL;
3341 linenr_T lnum;
3342 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003343 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 int old_files;
3345 int already_searched;
3346 char_u *file_line;
3347 char_u *line;
3348 char_u *p;
3349 char_u save_char;
3350 int define_matched;
3351 regmatch_T regmatch;
3352 regmatch_T incl_regmatch;
3353 regmatch_T def_regmatch;
3354 int matched = FALSE;
3355 int did_show = FALSE;
3356 int found = FALSE;
3357 int i;
3358 char_u *already = NULL;
3359 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003360 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003361#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 win_T *curwin_save = NULL;
3363#endif
3364
3365 regmatch.regprog = NULL;
3366 incl_regmatch.regprog = NULL;
3367 def_regmatch.regprog = NULL;
3368
3369 file_line = alloc(LSIZE);
3370 if (file_line == NULL)
3371 return;
3372
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 if (type != CHECK_PATH && type != FIND_DEFINE
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003374 // when CONT_SOL is set compare "ptr" with the beginning of the
3375 // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
3376 && !compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
3378 pat = alloc(len + 5);
3379 if (pat == NULL)
3380 goto fpip_end;
3381 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003382 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003384 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 vim_free(pat);
3386 if (regmatch.regprog == NULL)
3387 goto fpip_end;
3388 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003389 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3390 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003392 incl_regmatch.regprog = vim_regcomp(inc_opt,
3393 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (incl_regmatch.regprog == NULL)
3395 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003396 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3399 {
3400 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003401 ? p_def : curbuf->b_p_def,
3402 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 if (def_regmatch.regprog == NULL)
3404 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003405 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003407 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (files == NULL)
3409 goto fpip_end;
3410 old_files = max_path_depth;
3411 depth = depth_displayed = -1;
3412
3413 lnum = start_lnum;
3414 if (end_lnum > curbuf->b_ml.ml_line_count)
3415 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003416 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 lnum = end_lnum;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003418 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 for (;;)
3421 {
3422 if (incl_regmatch.regprog != NULL
3423 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3424 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003425 char_u *p_fname = (curr_fname == curbuf->b_fname)
3426 ? curbuf->b_ffname : curr_fname;
3427
3428 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003429 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003430 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003431 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003432 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3433 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003434 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003435 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003436 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 already_searched = FALSE;
3438 if (new_fname != NULL)
3439 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003440 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 for (i = 0;; i++)
3442 {
3443 if (i == depth + 1)
3444 i = old_files;
3445 if (i == max_path_depth)
3446 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003447 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3448 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 {
Dominique Pelle7765f5c2022-04-10 11:26:53 +01003450 if (type != CHECK_PATH
3451 && action == ACTION_SHOW_ALL
3452 && files[i].matched)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003454 msg_putchar('\n'); // cursor below last one
3455 if (!got_int) // don't display if 'q'
3456 // typed at "--more--"
3457 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
3459 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003460 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 prev_fname = NULL;
3462 }
3463 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003464 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 already_searched = TRUE;
3466 break;
3467 }
3468 }
3469 }
3470
3471 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3472 || (new_fname == NULL && !already_searched)))
3473 {
3474 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003475 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 else
3477 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003478 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003479 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003481 msg_puts_title(_("not found "));
3482 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
3484 did_show = TRUE;
3485 while (depth_displayed < depth && !got_int)
3486 {
3487 ++depth_displayed;
3488 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003489 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003491 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003493 if (!got_int) // don't display if 'q' typed
3494 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003497 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (new_fname != NULL)
3499 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003500 // using "new_fname" is more reliable, e.g., when
3501 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003502 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 else
3505 {
3506 /*
3507 * Isolate the file name.
3508 * Include the surrounding "" or <> if present.
3509 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003510 if (inc_opt != NULL
3511 && strstr((char *)inc_opt, "\\zs") != NULL)
3512 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003513 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003514 p = incl_regmatch.startp[0];
3515 i = (int)(incl_regmatch.endp[0]
3516 - incl_regmatch.startp[0]);
3517 }
3518 else
3519 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003520 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003521 for (p = incl_regmatch.endp[0];
3522 *p && !vim_isfilec(*p); p++)
3523 ;
3524 for (i = 0; vim_isfilec(p[i]); i++)
3525 ;
3526 }
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (i == 0)
3529 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003530 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003532 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003534 // Avoid checking before the start of the line, can
3535 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003536 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 {
3538 if (p[-1] == '"' || p[-1] == '<')
3539 {
3540 --p;
3541 ++i;
3542 }
3543 if (p[i] == '"' || p[i] == '>')
3544 ++i;
3545 }
3546 save_char = p[i];
3547 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003548 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 p[i] = save_char;
3550 }
3551
3552 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3553 {
3554 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003555 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003557 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 }
3559 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003560 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562
3563 if (new_fname != NULL)
3564 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003565 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (depth + 1 == old_files)
3567 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003568 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (bigger != NULL)
3570 {
3571 for (i = 0; i <= depth; i++)
3572 bigger[i] = files[i];
3573 for (i = depth + 1; i < old_files + max_path_depth; i++)
3574 {
3575 bigger[i].fp = NULL;
3576 bigger[i].name = NULL;
3577 bigger[i].lnum = 0;
3578 bigger[i].matched = FALSE;
3579 }
3580 for (i = old_files; i < max_path_depth; i++)
3581 bigger[i + max_path_depth] = files[i];
3582 old_files += max_path_depth;
3583 max_path_depth *= 2;
3584 vim_free(files);
3585 files = bigger;
3586 }
3587 }
3588 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3589 == NULL)
3590 vim_free(new_fname);
3591 else
3592 {
3593 if (++depth == old_files)
3594 {
3595 /*
3596 * lalloc() for 'bigger' must have failed above. We
3597 * will forget one of our already visited files now.
3598 */
3599 vim_free(files[old_files].name);
3600 ++old_files;
3601 }
3602 files[depth].name = curr_fname = new_fname;
3603 files[depth].lnum = 0;
3604 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if (action == ACTION_EXPAND)
3606 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003607 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003608 vim_snprintf((char*)IObuff, IOSIZE,
3609 _("Scanning included file: %s"),
3610 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003611 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003613 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003614 {
3615 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003616 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003617 (char *)new_fname);
3618 verbose_leave();
3619 }
3620
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 }
3623 }
3624 else
3625 {
3626 /*
3627 * Check if the line is a define (type == FIND_DEFINE)
3628 */
3629 p = line;
3630search_line:
3631 define_matched = FALSE;
3632 if (def_regmatch.regprog != NULL
3633 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3634 {
3635 /*
3636 * Pattern must be first identifier after 'define', so skip
3637 * to that position before checking for match of pattern. Also
3638 * don't let it match beyond the end of this identifier.
3639 */
3640 p = def_regmatch.endp[0];
3641 while (*p && !vim_iswordc(*p))
3642 p++;
3643 define_matched = TRUE;
3644 }
3645
3646 /*
3647 * Look for a match. Don't do this if we are looking for a
3648 * define and this line didn't match define_prog above.
3649 */
3650 if (def_regmatch.regprog == NULL || define_matched)
3651 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003652 if (define_matched || compl_status_sol())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003654 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 startp = skipwhite(p);
3656 if (p_ic)
3657 matched = !MB_STRNICMP(startp, ptr, len);
3658 else
3659 matched = !STRNCMP(startp, ptr, len);
3660 if (matched && define_matched && whole
3661 && vim_iswordc(startp[len]))
3662 matched = FALSE;
3663 }
3664 else if (regmatch.regprog != NULL
3665 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3666 {
3667 matched = TRUE;
3668 startp = regmatch.startp[0];
3669 /*
3670 * Check if the line is not a comment line (unless we are
3671 * looking for a define). A line starting with "# define"
3672 * is not considered to be a comment line.
3673 */
3674 if (!define_matched && skip_comments)
3675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 if ((*line != '#' ||
3677 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003678 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 matched = FALSE;
3680
3681 /*
3682 * Also check for a "/ *" or "/ /" before the match.
3683 * Skips lines like "int backwards; / * normal index
3684 * * /" when looking for "normal".
3685 * Note: Doesn't skip "/ *" in comments.
3686 */
3687 p = skipwhite(line);
3688 if (matched
3689 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 for (p = line; *p && p < startp; ++p)
3691 {
3692 if (matched
3693 && p[0] == '/'
3694 && (p[1] == '*' || p[1] == '/'))
3695 {
3696 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003697 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 if (p[1] == '/')
3699 break;
3700 ++p;
3701 }
3702 else if (!matched && p[0] == '*' && p[1] == '/')
3703 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003704 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 matched = TRUE;
3706 ++p;
3707 }
3708 }
3709 }
3710 }
3711 }
3712 }
3713 if (matched)
3714 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 if (action == ACTION_EXPAND)
3716 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003717 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 int add_r;
3719 char_u *aux;
3720
3721 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3722 break;
3723 found = TRUE;
3724 aux = p = startp;
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003725 if (compl_status_adding())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 {
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003727 p += ins_compl_len();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (vim_iswordp(p))
3729 goto exit_matched;
3730 p = find_word_start(p);
3731 }
3732 p = find_word_end(p);
3733 i = (int)(p - aux);
3734
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003735 if (compl_status_adding() && i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003737 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003739
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003740 // Get the next line: when "depth" < 0 from the current
3741 // buffer, otherwise from the included file. Jump to
3742 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003743 if (depth < 0)
3744 {
3745 if (lnum >= end_lnum)
3746 goto exit_matched;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003747 line = get_line_and_copy(++lnum, file_line);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003748 }
3749 else if (vim_fgets(line = file_line,
3750 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 goto exit_matched;
3752
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003753 // we read a line, set "already" to check this "line" later
3754 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003755 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 already = aux = p = skipwhite(line);
3757 p = find_word_start(p);
3758 p = find_word_end(p);
3759 if (p > aux)
3760 {
3761 if (*aux != ')' && IObuff[i-1] != TAB)
3762 {
3763 if (IObuff[i-1] != ' ')
3764 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003765 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (p_js
3767 && (IObuff[i-2] == '.'
3768 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3769 && (IObuff[i-2] == '?'
3770 || IObuff[i-2] == '!'))))
3771 IObuff[i++] = ' ';
3772 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003773 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (p - aux >= IOSIZE - i)
3775 p = aux + IOSIZE - i - 1;
3776 STRNCPY(IObuff + i, aux, p - aux);
3777 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003778 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 IObuff[i] = NUL;
3781 aux = IObuff;
3782
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003783 if (i == ins_compl_len())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 goto exit_matched;
3785 }
3786
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003787 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003789 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003791 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003793 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 break;
3795 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003796 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
3798 found = TRUE;
3799 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003800 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (curr_fname != prev_fname)
3802 {
3803 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003804 msg_putchar('\n'); // cursor below last one
3805 if (!got_int) // don't display if 'q' typed
3806 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 msg_home_replace_hl(curr_fname);
3808 prev_fname = curr_fname;
3809 }
3810 did_show = TRUE;
3811 if (!got_int)
3812 show_pat_in_path(line, type, TRUE, action,
3813 (depth == -1) ? NULL : files[depth].fp,
3814 (depth == -1) ? &lnum : &files[depth].lnum,
3815 match_count++);
3816
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003817 // Set matched flag for this file and all the ones that
3818 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 for (i = 0; i <= depth; ++i)
3820 files[i].matched = TRUE;
3821 }
3822 else if (--count <= 0)
3823 {
3824 found = TRUE;
3825 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003826#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 && g_do_tagpreview == 0
3828#endif
3829 )
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003830 emsg(_(e_match_is_on_current_line));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else if (action == ACTION_SHOW)
3832 {
3833 show_pat_in_path(line, type, did_show, action,
3834 (depth == -1) ? NULL : files[depth].fp,
3835 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3836 did_show = TRUE;
3837 }
3838 else
3839 {
3840#ifdef FEAT_GUI
3841 need_mouse_correct = TRUE;
3842#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003843#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003844 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 if (g_do_tagpreview != 0)
3846 {
3847 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003848 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850#endif
3851 if (action == ACTION_SPLIT)
3852 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003855 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 }
3857 if (depth == -1)
3858 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003859 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003860#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (g_do_tagpreview != 0)
3862 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003863 if (!win_valid(curwin_save))
3864 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003865 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003866 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003867 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003868 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870 else
3871#endif
3872 setpcmark();
3873 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003874 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 else
3877 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003878 if (!GETFILE_SUCCESS(getfile(
3879 0, files[depth].name, NULL, TRUE,
3880 files[depth].lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003881 break; // failed to jump to file
3882 // autocommands may have changed the lnum, we don't
3883 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 curwin->w_cursor.lnum = files[depth].lnum;
3885 }
3886 }
3887 if (action != ACTION_SHOW)
3888 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003889 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 curwin->w_set_curswant = TRUE;
3891 }
3892
Bram Moolenaar4033c552017-09-16 20:54:51 +02003893#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003895 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003897 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 validate_cursor();
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003899 redraw_later(UPD_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 win_enter(curwin_save, TRUE);
3901 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003902# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003903 else if (WIN_IS_POPUP(curwin))
3904 // can't keep focus in popup window
3905 win_enter(firstwin, TRUE);
3906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907#endif
3908 break;
3909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003912 // look for other matches in the rest of the line if we
3913 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 && action == ACTION_EXPAND
Yegappan Lakshmanand94fbfc2022-01-04 17:01:44 +00003916 && !compl_status_sol()
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003917 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003918 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 goto search_line;
3920 }
3921 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003923 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003924 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 break;
3926
3927 /*
3928 * Read the next line. When reading an included file and encountering
3929 * end-of-file, close the file and continue in the file that included
3930 * it.
3931 */
3932 while (depth >= 0 && !already
3933 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3934 {
3935 fclose(files[depth].fp);
3936 --old_files;
3937 files[old_files].name = files[depth].name;
3938 files[old_files].matched = files[depth].matched;
3939 --depth;
3940 curr_fname = (depth == -1) ? curbuf->b_fname
3941 : files[depth].name;
3942 if (depth < depth_displayed)
3943 depth_displayed = depth;
3944 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003945 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003948 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003949 i = (int)STRLEN(line);
3950 if (i > 0 && line[i - 1] == '\n')
3951 line[--i] = NUL;
3952 if (i > 0 && line[i - 1] == '\r')
3953 line[--i] = NUL;
3954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 else if (!already)
3956 {
3957 if (++lnum > end_lnum)
3958 break;
Bram Moolenaar409510c2022-06-01 15:23:13 +01003959 line = get_line_and_copy(lnum, file_line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 already = NULL;
3962 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003963 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003965 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 for (i = 0; i <= depth; i++)
3967 {
3968 fclose(files[i].fp);
3969 vim_free(files[i].name);
3970 }
3971 for (i = old_files; i < max_path_depth; i++)
3972 vim_free(files[i].name);
3973 vim_free(files);
3974
3975 if (type == CHECK_PATH)
3976 {
3977 if (!did_show)
3978 {
3979 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003980 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003982 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 }
3984 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003985 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003987 if (got_int || ins_compl_interrupted())
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00003988 emsg(_(e_interrupted));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 else if (type == FIND_DEFINE)
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003990 emsg(_(e_couldnt_find_definition));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else
Bram Moolenaarac78dd42022-01-02 19:25:26 +00003992 emsg(_(e_couldnt_find_pattern));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
3994 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
3995 msg_end();
3996
3997fpip_end:
3998 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02003999 vim_regfree(regmatch.regprog);
4000 vim_regfree(incl_regmatch.regprog);
4001 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002}
4003
4004 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004005show_pat_in_path(
4006 char_u *line,
4007 int type,
4008 int did_show,
4009 int action,
4010 FILE *fp,
4011 linenr_T *lnum,
4012 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 char_u *p;
4015
4016 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004017 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004018 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004019 gotocmdline(TRUE); // cursor at status line
4020 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return;
4022 for (;;)
4023 {
4024 p = line + STRLEN(line) - 1;
4025 if (fp != NULL)
4026 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004027 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 if (p >= line && *p == '\n')
4029 --p;
4030 if (p >= line && *p == '\r')
4031 --p;
4032 *(p + 1) = NUL;
4033 }
4034 if (action == ACTION_SHOW_ALL)
4035 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004036 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004037 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004038 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4039 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004040 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4041 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004043 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004044 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004046 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4048 break;
4049
4050 if (fp != NULL)
4051 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004052 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 break;
4054 ++*lnum;
4055 }
4056 else
4057 {
4058 if (++*lnum > curbuf->b_ml.ml_line_count)
4059 break;
4060 line = ml_get(*lnum);
4061 }
4062 msg_putchar('\n');
4063 }
4064}
4065#endif
4066
4067#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004068/*
4069 * Return the last used search pattern at "idx".
4070 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004071 spat_T *
4072get_spat(int idx)
4073{
4074 return &spats[idx];
4075}
4076
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004077/*
4078 * Return the last used search pattern index.
4079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004081get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004083 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004086
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004087#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004088/*
4089 * "searchcount()" function
4090 */
4091 void
4092f_searchcount(typval_T *argvars, typval_T *rettv)
4093{
4094 pos_T pos = curwin->w_cursor;
4095 char_u *pattern = NULL;
4096 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4097 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004098 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004099 searchstat_T stat;
4100
4101 if (rettv_dict_alloc(rettv) == FAIL)
4102 return;
4103
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004104 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4105 return;
4106
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004107 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4108 recompute = TRUE;
4109
4110 if (argvars[0].v_type != VAR_UNKNOWN)
4111 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004112 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004113 dictitem_T *di;
4114 listitem_T *li;
4115 int error = FALSE;
4116
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004117 if (check_for_nonnull_dict_arg(argvars, 0) == FAIL)
Bram Moolenaar14681622020-06-03 22:57:39 +02004118 return;
Bram Moolenaar14681622020-06-03 22:57:39 +02004119 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004120 di = dict_find(dict, (char_u *)"timeout", -1);
4121 if (di != NULL)
4122 {
4123 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4124 if (error)
4125 return;
4126 }
4127 di = dict_find(dict, (char_u *)"maxcount", -1);
4128 if (di != NULL)
4129 {
4130 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4131 if (error)
4132 return;
4133 }
Bram Moolenaard61efa52022-07-23 09:52:04 +01004134 recompute = dict_get_bool(dict, "recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004135 di = dict_find(dict, (char_u *)"pattern", -1);
4136 if (di != NULL)
4137 {
4138 pattern = tv_get_string_chk(&di->di_tv);
4139 if (pattern == NULL)
4140 return;
4141 }
4142 di = dict_find(dict, (char_u *)"pos", -1);
4143 if (di != NULL)
4144 {
4145 if (di->di_tv.v_type != VAR_LIST)
4146 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004147 semsg(_(e_invalid_argument_str), "pos");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004148 return;
4149 }
4150 if (list_len(di->di_tv.vval.v_list) != 3)
4151 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004152 semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004153 return;
4154 }
4155 li = list_find(di->di_tv.vval.v_list, 0L);
4156 if (li != NULL)
4157 {
4158 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4159 if (error)
4160 return;
4161 }
4162 li = list_find(di->di_tv.vval.v_list, 1L);
4163 if (li != NULL)
4164 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004165 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004166 if (error)
4167 return;
4168 }
4169 li = list_find(di->di_tv.vval.v_list, 2L);
4170 if (li != NULL)
4171 {
Bram Moolenaar6ed545e2022-05-09 20:09:23 +01004172 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004173 if (error)
4174 return;
4175 }
4176 }
4177 }
4178
4179 save_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004180#ifdef FEAT_SEARCH_EXTRA
4181 save_incsearch_state();
4182#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004183 if (pattern != NULL)
4184 {
4185 if (*pattern == NUL)
4186 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004187 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004188 spats[last_idx].pat = vim_strsave(pattern);
4189 }
4190 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4191 goto the_end; // the previous pattern was never defined
4192
4193 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4194
4195 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4196 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4197 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4198 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4199 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4200
4201the_end:
4202 restore_last_search_pattern();
Christian Brabandt6dd74242022-02-14 12:44:32 +00004203#ifdef FEAT_SEARCH_EXTRA
4204 restore_incsearch_state();
4205#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004206}
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004207#endif
Bram Moolenaar635414d2020-09-11 22:25:15 +02004208
4209/*
4210 * Fuzzy string matching
4211 *
4212 * Ported from the lib_fts library authored by Forrest Smith.
4213 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4214 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004215 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004216 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4217 *
4218 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004219 * - Matched letter
4220 * - Unmatched letter
4221 * - Consecutively matched letters
4222 * - Proximity to start
4223 * - Letter following a separator (space, underscore)
4224 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004225 *
4226 * Matched letters are good. Unmatched letters are bad. Matching near the start
4227 * is good. Matching the first letter in the middle of a phrase is good.
4228 * Matching the uppercase letters in camel case entries is good.
4229 *
4230 * The score assigned for each factor is explained below.
4231 * File paths are different from file names. File extensions may be ignorable.
4232 * Single words care about consecutive matches but not separators or camel
4233 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004234 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004235 * Matched letter: +0 points
4236 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004237 * Consecutive match bonus: +15 points
4238 * First letter bonus: +15 points
4239 * Separator bonus: +30 points
4240 * Camel case bonus: +30 points
4241 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004242 *
4243 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004244 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004245 * lower minimum score due to unmatched letter penalty. Longer search patterns
4246 * have a higher maximum score due to match bonuses.
4247 *
4248 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4249 * quite a bit.
4250 *
4251 * There is a penalty if you DON’T match the first three letters. Which
4252 * effectively rewards matching near the start. However there’s no difference
4253 * in matching between the middle and end.
4254 *
4255 * There is not an explicit bonus for an exact match. Unmatched letters receive
4256 * a penalty. So shorter strings and closer matches are worth more.
4257 */
4258typedef struct
4259{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004260 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004261 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004262 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004263 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004264} fuzzyItem_T;
4265
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004266// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4267// matching a whole word is preferred.
4268#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004269// bonus if match occurs after a path separator
4270#define PATH_SEPARATOR_BONUS 30
4271// bonus if match occurs after a word separator
4272#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004273// bonus if match is uppercase and prev is lower
4274#define CAMEL_BONUS 30
4275// bonus if the first letter is matched
4276#define FIRST_LETTER_BONUS 15
4277// penalty applied for every letter in str before the first match
kylo252ae6f1d82022-02-16 19:24:07 +00004278#define LEADING_LETTER_PENALTY (-5)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004279// maximum penalty for leading letters
kylo252ae6f1d82022-02-16 19:24:07 +00004280#define MAX_LEADING_LETTER_PENALTY (-15)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004281// penalty for every letter that doesn't match
kylo252ae6f1d82022-02-16 19:24:07 +00004282#define UNMATCHED_LETTER_PENALTY (-1)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004283// penalty for gap in matching positions (-2 * k)
kylo252ae6f1d82022-02-16 19:24:07 +00004284#define GAP_PENALTY (-2)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004285// Score for a string that doesn't fuzzy match the pattern
kylo252ae6f1d82022-02-16 19:24:07 +00004286#define SCORE_NONE (-9999)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004287
4288#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004289
4290/*
4291 * Compute a score for a fuzzy matched string. The matching character locations
4292 * are in 'matches'.
4293 */
4294 static int
4295fuzzy_match_compute_score(
4296 char_u *str,
4297 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004298 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004299 int numMatches)
4300{
4301 int score;
4302 int penalty;
4303 int unmatched;
4304 int i;
4305 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004306 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004307
4308 // Initialize score
4309 score = 100;
4310
4311 // Apply leading letter penalty
4312 penalty = LEADING_LETTER_PENALTY * matches[0];
4313 if (penalty < MAX_LEADING_LETTER_PENALTY)
4314 penalty = MAX_LEADING_LETTER_PENALTY;
4315 score += penalty;
4316
4317 // Apply unmatched penalty
4318 unmatched = strSz - numMatches;
4319 score += UNMATCHED_LETTER_PENALTY * unmatched;
4320
4321 // Apply ordering bonuses
4322 for (i = 0; i < numMatches; ++i)
4323 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004324 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004325
4326 if (i > 0)
4327 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004328 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004329
4330 // Sequential
4331 if (currIdx == (prevIdx + 1))
4332 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004333 else
4334 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004335 }
4336
4337 // Check for bonuses based on neighbor character value
4338 if (currIdx > 0)
4339 {
4340 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004341 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004342 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004343
4344 if (has_mbyte)
4345 {
4346 while (sidx < currIdx)
4347 {
4348 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004349 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004350 sidx++;
4351 }
4352 curr = (*mb_ptr2char)(p);
4353 }
4354 else
4355 {
4356 neighbor = str[currIdx - 1];
4357 curr = str[currIdx];
4358 }
4359
4360 if (vim_islower(neighbor) && vim_isupper(curr))
4361 score += CAMEL_BONUS;
4362
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004363 // Bonus if the match follows a separator character
4364 if (neighbor == '/' || neighbor == '\\')
4365 score += PATH_SEPARATOR_BONUS;
4366 else if (neighbor == ' ' || neighbor == '_')
4367 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004368 }
4369 else
4370 {
4371 // First letter
4372 score += FIRST_LETTER_BONUS;
4373 }
4374 }
4375 return score;
4376}
4377
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004378/*
4379 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4380 * Return the number of matching characters.
4381 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004382 static int
4383fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004384 char_u *fuzpat,
4385 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004386 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004387 int *outScore,
4388 char_u *strBegin,
4389 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004390 int_u *srcMatches,
4391 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004392 int maxMatches,
4393 int nextMatch,
4394 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004395{
4396 // Recursion params
4397 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004398 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004399 int bestRecursiveScore = 0;
4400 int first_match;
4401 int matched;
4402
4403 // Count recursions
4404 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004405 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004406 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004407
4408 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004409 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004410 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004411
4412 // Loop through fuzpat and str looking for a match
4413 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004414 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004415 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004416 int c1;
4417 int c2;
4418
4419 c1 = PTR2CHAR(fuzpat);
4420 c2 = PTR2CHAR(str);
4421
Bram Moolenaar635414d2020-09-11 22:25:15 +02004422 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004423 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004424 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004425 int_u recursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004426 int recursiveScore = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004427 char_u *next_char;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004428
4429 // Supplied matches buffer was too short
4430 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004431 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004432
4433 // "Copy-on-Write" srcMatches into matches
4434 if (first_match && srcMatches)
4435 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004436 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004437 first_match = FALSE;
4438 }
4439
4440 // Recursive call that "skips" this match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004441 if (has_mbyte)
4442 next_char = str + (*mb_ptr2len)(str);
4443 else
4444 next_char = str + 1;
4445 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4446 &recursiveScore, strBegin, strLen, matches,
4447 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004448 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004449 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004450 {
4451 // Pick best recursive score
4452 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4453 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004454 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004455 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004456 bestRecursiveScore = recursiveScore;
4457 }
4458 recursiveMatch = TRUE;
4459 }
4460
4461 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004462 matches[nextMatch++] = strIdx;
4463 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004464 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004465 else
4466 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004467 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004468 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004469 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004470 else
4471 ++str;
4472 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004473 }
4474
4475 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004476 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004477
4478 // Calculate score
4479 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004480 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4481 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004482
4483 // Return best result
4484 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4485 {
4486 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004487 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004488 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004489 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004490 }
4491 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004492 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004493
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004494 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004495}
4496
4497/*
4498 * fuzzy_match()
4499 *
4500 * Performs exhaustive search via recursion to find all possible matches and
4501 * match with highest score.
4502 * Scores values have no intrinsic meaning. Possible score range is not
4503 * normalized and varies with pattern.
4504 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004505 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004506 * Uses char_u for match indices. Therefore patterns are limited to
4507 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004508 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004509 * Returns TRUE if 'pat_arg' matches 'str'. Also returns the match score in
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004510 * 'outScore' and the matching character positions in 'matches'.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004511 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004512 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004513fuzzy_match(
4514 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004515 char_u *pat_arg,
4516 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004517 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004518 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004519 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004520{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004521 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004522 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004523 char_u *save_pat;
4524 char_u *pat;
4525 char_u *p;
4526 int complete = FALSE;
4527 int score = 0;
4528 int numMatches = 0;
4529 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004530
4531 *outScore = 0;
4532
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004533 save_pat = vim_strsave(pat_arg);
4534 if (save_pat == NULL)
4535 return FALSE;
4536 pat = save_pat;
4537 p = pat;
4538
4539 // Try matching each word in 'pat_arg' in 'str'
4540 while (TRUE)
4541 {
4542 if (matchseq)
4543 complete = TRUE;
4544 else
4545 {
4546 // Extract one word from the pattern (separated by space)
4547 p = skipwhite(p);
4548 if (*p == NUL)
4549 break;
4550 pat = p;
4551 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4552 {
4553 if (has_mbyte)
4554 MB_PTR_ADV(p);
4555 else
4556 ++p;
4557 }
4558 if (*p == NUL) // processed all the words
4559 complete = TRUE;
4560 *p = NUL;
4561 }
4562
4563 score = 0;
4564 recursionCount = 0;
4565 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4566 matches + numMatches, maxMatches - numMatches,
4567 0, &recursionCount);
4568 if (matchCount == 0)
4569 {
4570 numMatches = 0;
4571 break;
4572 }
4573
4574 // Accumulate the match score and the number of matches
4575 *outScore += score;
4576 numMatches += matchCount;
4577
4578 if (complete)
4579 break;
4580
4581 // try matching the next word
4582 ++p;
4583 }
4584
4585 vim_free(save_pat);
4586 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004587}
4588
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004589#if defined(FEAT_EVAL) || defined(FEAT_PROTO)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004590/*
4591 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004592 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004593 */
4594 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004595fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004596{
4597 int v1 = ((fuzzyItem_T *)s1)->score;
4598 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004599 int idx1 = ((fuzzyItem_T *)s1)->idx;
4600 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004601
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004602 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004603}
4604
4605/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004606 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4607 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004608 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4609 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004610 * If 'items' is a list of strings, then search for 'str' in the list.
4611 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4612 * for each item or use 'item_cb' Funcref function to get the string.
4613 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4614 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004615 */
4616 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004617fuzzy_match_in_list(
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004618 list_T *l,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004619 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004620 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004621 char_u *key,
4622 callback_T *item_cb,
4623 int retmatchpos,
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004624 list_T *fmatchlist,
4625 long max_matches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004626{
4627 long len;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004628 fuzzyItem_T *items;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004629 listitem_T *li;
4630 long i = 0;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004631 long match_count = 0;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004632 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004633
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004634 len = list_len(l);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004635 if (len == 0)
4636 return;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004637 if (max_matches > 0 && len > max_matches)
4638 len = max_matches;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004639
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004640 items = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
4641 if (items == NULL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004642 return;
4643
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004644 // For all the string items in items, get the fuzzy matching score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004645 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004646 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004647 int score;
4648 char_u *itemstr;
4649 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004650
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004651 if (max_matches > 0 && match_count >= max_matches)
4652 break;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004653
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004654 itemstr = NULL;
4655 rettv.v_type = VAR_UNKNOWN;
4656 if (li->li_tv.v_type == VAR_STRING) // list of strings
4657 itemstr = li->li_tv.vval.v_string;
Dominique Pelle7765f5c2022-04-10 11:26:53 +01004658 else if (li->li_tv.v_type == VAR_DICT
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004659 && (key != NULL || item_cb->cb_name != NULL))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004660 {
4661 // For a dict, either use the specified key to lookup the string or
4662 // use the specified callback function to get the string.
4663 if (key != NULL)
Bram Moolenaard61efa52022-07-23 09:52:04 +01004664 itemstr = dict_get_string(li->li_tv.vval.v_dict,
4665 (char *)key, FALSE);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004666 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004667 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004668 typval_T argv[2];
4669
4670 // Invoke the supplied callback (if any) to get the dict item
4671 li->li_tv.vval.v_dict->dv_refcount++;
4672 argv[0].v_type = VAR_DICT;
4673 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4674 argv[1].v_type = VAR_UNKNOWN;
4675 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4676 {
4677 if (rettv.v_type == VAR_STRING)
4678 itemstr = rettv.vval.v_string;
4679 }
4680 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004681 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004682 }
4683
4684 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004685 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004686 MAX_FUZZY_MATCHES))
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004687 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004688 items[match_count].idx = match_count;
4689 items[match_count].item = li;
4690 items[match_count].score = score;
4691
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004692 // Copy the list of matching positions in itemstr to a list, if
4693 // 'retmatchpos' is set.
4694 if (retmatchpos)
4695 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004696 int j = 0;
4697 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004698
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004699 items[match_count].lmatchpos = list_alloc();
4700 if (items[match_count].lmatchpos == NULL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004701 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004702
4703 p = str;
4704 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004705 {
zeertzjq9af2bc02022-05-11 14:15:37 +01004706 if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004707 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004708 if (list_append_number(items[match_count].lmatchpos,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004709 matches[j]) == FAIL)
4710 goto done;
4711 j++;
4712 }
4713 if (has_mbyte)
4714 MB_PTR_ADV(p);
4715 else
4716 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004717 }
4718 }
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004719 ++match_count;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004720 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004721 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004722 }
4723
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004724 if (match_count > 0)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004725 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004726 list_T *retlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004727
Bram Moolenaar635414d2020-09-11 22:25:15 +02004728 // Sort the list by the descending order of the match score
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004729 qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004730 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004731
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004732 // For matchfuzzy(), return a list of matched strings.
4733 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004734 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004735 // The first item is a list of matched strings. The second item
4736 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004737 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004738 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4739 if (retmatchpos)
4740 {
4741 li = list_find(fmatchlist, 0);
4742 if (li == NULL || li->li_tv.vval.v_list == NULL)
4743 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004744 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004745 }
4746 else
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004747 retlist = fmatchlist;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004748
4749 // Copy the matching strings with a valid score to the return list
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004750 for (i = 0; i < match_count; i++)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004751 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004752 if (items[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004753 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004754 list_append_tv(retlist, &items[i].item->li_tv);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004755 }
4756
4757 // next copy the list of matching positions
4758 if (retmatchpos)
4759 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004760 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004761 if (li == NULL || li->li_tv.vval.v_list == NULL)
4762 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004763 retlist = li->li_tv.vval.v_list;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004764
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004765 for (i = 0; i < match_count; i++)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004766 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004767 if (items[i].score == SCORE_NONE)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004768 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004769 if (items[i].lmatchpos != NULL
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004770 && list_append_list(retlist, items[i].lmatchpos) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004771 goto done;
4772 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004773
4774 // copy the matching scores
4775 li = list_find(fmatchlist, -1);
4776 if (li == NULL || li->li_tv.vval.v_list == NULL)
4777 goto done;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004778 retlist = li->li_tv.vval.v_list;
4779 for (i = 0; i < match_count; i++)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004780 {
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004781 if (items[i].score == SCORE_NONE)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004782 break;
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004783 if (list_append_number(retlist, items[i].score) == FAIL)
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004784 goto done;
4785 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004786 }
4787 }
4788
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004789done:
Yegappan Lakshmanan047a7012022-04-16 20:42:40 +01004790 vim_free(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004791}
4792
4793/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004794 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4795 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4796 */
4797 static void
4798do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4799{
4800 callback_T cb;
4801 char_u *key = NULL;
4802 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004803 int matchseq = FALSE;
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004804 long max_matches = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004805
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004806 if (in_vim9script()
4807 && (check_for_list_arg(argvars, 0) == FAIL
4808 || check_for_string_arg(argvars, 1) == FAIL
4809 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4810 return;
4811
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004812 CLEAR_POINTER(&cb);
4813
4814 // validate and get the arguments
4815 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4816 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004817 semsg(_(e_argument_of_str_must_be_list),
4818 retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004819 return;
4820 }
4821 if (argvars[1].v_type != VAR_STRING
4822 || argvars[1].vval.v_string == NULL)
4823 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004824 semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1]));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004825 return;
4826 }
4827
4828 if (argvars[2].v_type != VAR_UNKNOWN)
4829 {
4830 dict_T *d;
4831 dictitem_T *di;
4832
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01004833 if (check_for_nonnull_dict_arg(argvars, 2) == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004834 return;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004835
4836 // To search a dict, either a callback function or a key can be
4837 // specified.
4838 d = argvars[2].vval.v_dict;
4839 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4840 {
4841 if (di->di_tv.v_type != VAR_STRING
4842 || di->di_tv.vval.v_string == NULL
4843 || *di->di_tv.vval.v_string == NUL)
4844 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004845 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004846 return;
4847 }
4848 key = tv_get_string(&di->di_tv);
4849 }
4850 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4851 {
4852 cb = get_callback(&di->di_tv);
4853 if (cb.cb_name == NULL)
4854 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004855 semsg(_(e_invalid_value_for_argument_str), "text_cb");
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004856 return;
4857 }
4858 }
Kazuyuki Miyagi47f1a552022-06-17 18:30:03 +01004859
4860 if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL)
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004861 {
4862 if (di->di_tv.v_type != VAR_NUMBER)
4863 {
4864 semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv));
4865 return;
4866 }
4867 max_matches = (long)tv_get_number_chk(&di->di_tv, NULL);
4868 }
4869
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01004870 if (dict_has_key(d, "matchseq"))
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004871 matchseq = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004872 }
4873
4874 // get the fuzzy matches
4875 ret = rettv_list_alloc(rettv);
Bram Moolenaar5ea38d12022-06-16 21:20:48 +01004876 if (ret == FAIL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004877 goto done;
4878 if (retmatchpos)
4879 {
4880 list_T *l;
4881
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004882 // For matchfuzzypos(), a list with three items are returned. First
4883 // item is a list of matching strings, the second item is a list of
4884 // lists with matching positions within each string and the third item
4885 // is the list of scores of the matches.
4886 l = list_alloc();
4887 if (l == NULL)
4888 goto done;
4889 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004890 {
4891 vim_free(l);
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004892 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004893 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004894 l = list_alloc();
4895 if (l == NULL)
4896 goto done;
4897 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004898 {
4899 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004900 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004901 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004902 l = list_alloc();
4903 if (l == NULL)
4904 goto done;
4905 if (list_append_list(rettv->vval.v_list, l) == FAIL)
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004906 {
4907 vim_free(l);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004908 goto done;
Bram Moolenaar9ba61942022-08-31 11:25:06 +01004909 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004910 }
4911
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004912 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
Yasuhiro Matsumoto9029a6e2022-04-16 12:35:35 +01004913 matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004914
4915done:
4916 free_callback(&cb);
4917}
4918
4919/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004920 * "matchfuzzy()" function
4921 */
4922 void
4923f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4924{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004925 do_fuzzymatch(argvars, rettv, FALSE);
4926}
4927
4928/*
4929 * "matchfuzzypos()" function
4930 */
4931 void
4932f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
4933{
4934 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004935}
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004936#endif
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00004937
4938/*
4939 * Same as fuzzy_match_item_compare() except for use with a string match
4940 */
4941 static int
4942fuzzy_match_str_compare(const void *s1, const void *s2)
4943{
4944 int v1 = ((fuzmatch_str_T *)s1)->score;
4945 int v2 = ((fuzmatch_str_T *)s2)->score;
4946 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4947 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4948
4949 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4950}
4951
4952/*
4953 * Sort fuzzy matches by score
4954 */
4955 static void
4956fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz)
4957{
4958 // Sort the list by the descending order of the match score
4959 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4960 fuzzy_match_str_compare);
4961}
4962
4963/*
4964 * Same as fuzzy_match_item_compare() except for use with a function name
4965 * string match. <SNR> functions should be sorted to the end.
4966 */
4967 static int
4968fuzzy_match_func_compare(const void *s1, const void *s2)
4969{
4970 int v1 = ((fuzmatch_str_T *)s1)->score;
4971 int v2 = ((fuzmatch_str_T *)s2)->score;
4972 int idx1 = ((fuzmatch_str_T *)s1)->idx;
4973 int idx2 = ((fuzmatch_str_T *)s2)->idx;
4974 char_u *str1 = ((fuzmatch_str_T *)s1)->str;
4975 char_u *str2 = ((fuzmatch_str_T *)s2)->str;
4976
4977 if (*str1 != '<' && *str2 == '<') return -1;
4978 if (*str1 == '<' && *str2 != '<') return 1;
4979 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
4980}
4981
4982/*
4983 * Sort fuzzy matches of function names by score.
4984 * <SNR> functions should be sorted to the end.
4985 */
4986 static void
4987fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz)
4988{
4989 // Sort the list by the descending order of the match score
4990 qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T),
4991 fuzzy_match_func_compare);
4992}
4993
4994/*
4995 * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise,
4996 * returns the match score.
4997 */
4998 int
4999fuzzy_match_str(char_u *str, char_u *pat)
5000{
5001 int score = 0;
Yegappan Lakshmanan5ec633b2022-02-25 15:24:24 +00005002 int_u matchpos[MAX_FUZZY_MATCHES];
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005003
5004 if (str == NULL || pat == NULL)
5005 return 0;
5006
Yegappan Lakshmanan6caeda22022-02-27 12:07:30 +00005007 fuzzy_match(str, pat, TRUE, &score, matchpos,
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005008 sizeof(matchpos) / sizeof(matchpos[0]));
5009
5010 return score;
5011}
5012
5013/*
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005014 * Free an array of fuzzy string matches "fuzmatch[count]".
5015 */
5016 void
5017fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count)
5018{
5019 int i;
5020
5021 if (fuzmatch == NULL)
5022 return;
5023 for (i = 0; i < count; ++i)
5024 vim_free(fuzmatch[i].str);
5025 vim_free(fuzmatch);
5026}
5027
5028/*
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005029 * Copy a list of fuzzy matches into a string list after sorting the matches by
5030 * the fuzzy score. Frees the memory allocated for 'fuzmatch'.
5031 * Returns OK on success and FAIL on memory allocation failure.
5032 */
5033 int
5034fuzzymatches_to_strmatches(
5035 fuzmatch_str_T *fuzmatch,
5036 char_u ***matches,
5037 int count,
5038 int funcsort)
5039{
5040 int i;
5041
5042 if (count <= 0)
5043 return OK;
5044
5045 *matches = ALLOC_MULT(char_u *, count);
5046 if (*matches == NULL)
5047 {
Bram Moolenaarc6e0a5e2022-04-10 18:09:06 +01005048 fuzmatch_str_free(fuzmatch, count);
Yegappan Lakshmanan38b85cb2022-02-24 13:28:41 +00005049 return FAIL;
5050 }
5051
5052 // Sort the list by the descending order of the match score
5053 if (funcsort)
5054 fuzzy_match_func_sort((void *)fuzmatch, (size_t)count);
5055 else
5056 fuzzy_match_str_sort((void *)fuzmatch, (size_t)count);
5057
5058 for (i = 0; i < count; i++)
5059 (*matches)[i] = fuzmatch[i].str;
5060 vim_free(fuzmatch);
5061
5062 return OK;
5063}