blob: 43966511b7f40d95545cd55c554aba0ba354438e [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 Moolenaarbaaa7e92016-01-29 22:47:03 +010019static int check_linecomment(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020#ifdef FEAT_FIND_ID
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010021static void show_pat_in_path(char_u *, int,
22 int, int, FILE *, linenr_T *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020024
25typedef struct searchstat
26{
27 int cur; // current position of found words
28 int cnt; // total count of found words
29 int exact_match; // TRUE if matched exactly on specified position
30 int incomplete; // 0: search was fully completed
31 // 1: recomputing was timed out
32 // 2: max count exceeded
33 int last_maxcount; // the max count of the last search
34} searchstat_T;
35
36static 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);
37static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout);
38
Bram Moolenaarea6561a2020-06-01 21:32:45 +020039#define SEARCH_STAT_DEF_TIMEOUT 40L
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020040#define SEARCH_STAT_DEF_MAX_COUNT 99
41#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar071d4272004-06-13 20:20:40 +000043/*
44 * This file contains various searching-related routines. These fall into
45 * three groups:
46 * 1. string searches (for /, ?, n, and N)
47 * 2. character searches within a single line (for f, F, t, T, etc)
48 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
49 */
50
51/*
52 * String searches
53 *
54 * The string search functions are divided into two levels:
55 * lowest: searchit(); uses an pos_T for starting position and found match.
56 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
57 *
58 * The last search pattern is remembered for repeating the same search.
59 * This pattern is shared between the :g, :s, ? and / commands.
60 * This is in search_regcomp().
61 *
62 * The actual string matching is done using a heavily modified version of
63 * Henry Spencer's regular expression library. See regexp.c.
64 */
65
Bram Moolenaar071d4272004-06-13 20:20:40 +000066/*
67 * Two search patterns are remembered: One for the :substitute command and
68 * one for other searches. last_idx points to the one that was used the last
69 * time.
70 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020071static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
Bram Moolenaar63d9e732019-12-05 21:10:38 +010073 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat
74 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat
Bram Moolenaar071d4272004-06-13 20:20:40 +000075};
76
Bram Moolenaar63d9e732019-12-05 21:10:38 +010077static int last_idx = 0; // index in spats[] for RE_LAST
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar63d9e732019-12-05 21:10:38 +010079static char_u lastc[2] = {NUL, NUL}; // last character searched for
80static int lastcdir = FORWARD; // last direction of character search
81static int last_t_cmd = TRUE; // last search t_cmd
Bram Moolenaardbd24b52015-08-11 14:26:19 +020082static char_u lastc_bytes[MB_MAXBYTES + 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +010083static int lastc_bytelen = 1; // >1 for multi-byte char
Bram Moolenaardbd24b52015-08-11 14:26:19 +020084
Bram Moolenaar63d9e732019-12-05 21:10:38 +010085// copy of spats[], for keeping the search patterns while executing autocmds
Bram Moolenaarc3328162019-07-23 22:15:25 +020086static spat_T saved_spats[2];
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 Moolenaar63d9e732019-12-05 21:10:38 +010092static char_u *mr_pattern = NULL; // pattern used by search_regcomp()
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +010094static int mr_pattern_alloced = FALSE; // mr_pattern was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
96
97#ifdef FEAT_FIND_ID
98/*
99 * Type used by find_pattern_in_path() to remember which included files have
100 * been searched already.
101 */
102typedef struct SearchedFile
103{
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100104 FILE *fp; // File pointer
105 char_u *name; // Full name of file
106 linenr_T lnum; // Line we were up to in file
107 int matched; // Found a match in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108} SearchedFile;
109#endif
110
111/*
112 * translate search pattern for vim_regcomp()
113 *
114 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
115 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
116 * pat_save == RE_BOTH: save pat in both patterns (:global command)
117 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000118 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
120 * options & SEARCH_HIS: put search string in history
121 * options & SEARCH_KEEP: keep previous search pattern
122 *
123 * returns FAIL if failed, OK otherwise.
124 */
125 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100126search_regcomp(
127 char_u *pat,
128 int pat_save,
129 int pat_use,
130 int options,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100131 regmmatch_T *regmatch) // return: pattern and ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 int magic;
134 int i;
135
136 rc_did_emsg = FALSE;
Bram Moolenaarf4e20992020-12-21 19:59:08 +0100137 magic = magic_isset();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139 /*
140 * If no pattern given, use a previously defined pattern.
141 */
142 if (pat == NULL || *pat == NUL)
143 {
144 if (pat_use == RE_LAST)
145 i = last_idx;
146 else
147 i = pat_use;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100148 if (spats[i].pat == NULL) // pattern was never defined
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {
150 if (pat_use == RE_SUBST)
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200151 emsg(_(e_no_previous_substitute_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +0200153 emsg(_(e_no_previous_regular_expression));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 rc_did_emsg = TRUE;
155 return FAIL;
156 }
157 pat = spats[i].pat;
158 magic = spats[i].magic;
159 no_smartcase = spats[i].no_scs;
160 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100161 else if (options & SEARCH_HIS) // put new pattern in history
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163
164#ifdef FEAT_RIGHTLEFT
165 if (mr_pattern_alloced)
166 {
167 vim_free(mr_pattern);
168 mr_pattern_alloced = FALSE;
169 }
170
171 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
172 {
173 char_u *rev_pattern;
174
175 rev_pattern = reverse_text(pat);
176 if (rev_pattern == NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100177 mr_pattern = pat; // out of memory, keep normal pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 else
179 {
180 mr_pattern = rev_pattern;
181 mr_pattern_alloced = TRUE;
182 }
183 }
184 else
185#endif
186 mr_pattern = pat;
187
188 /*
189 * Save the currently used pattern in the appropriate place,
190 * unless the pattern should not be remembered.
191 */
Bram Moolenaare1004402020-10-24 20:49:43 +0200192 if (!(options & SEARCH_KEEP)
193 && (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100195 // search or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
197 save_re_pat(RE_SEARCH, pat, magic);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100198 // substitute or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
200 save_re_pat(RE_SUBST, pat, magic);
201 }
202
203 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000204 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
206 if (regmatch->regprog == NULL)
207 return FAIL;
208 return OK;
209}
210
211/*
212 * Get search pattern used by search_regcomp().
213 */
214 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100215get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216{
217 return mr_pattern;
218}
219
Bram Moolenaarabc97732007-08-08 20:49:37 +0000220#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221/*
222 * Reverse text into allocated memory.
223 * Returns the allocated string, NULL when out of memory.
224 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000225 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100226reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227{
228 unsigned len;
229 unsigned s_i, rev_i;
230 char_u *rev;
231
232 /*
233 * Reverse the pattern.
234 */
235 len = (unsigned)STRLEN(s);
236 rev = alloc(len + 1);
237 if (rev != NULL)
238 {
239 rev_i = len;
240 for (s_i = 0; s_i < len; ++s_i)
241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 if (has_mbyte)
243 {
244 int mb_len;
245
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000246 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 rev_i -= mb_len;
248 mch_memmove(rev + rev_i, s + s_i, mb_len);
249 s_i += mb_len - 1;
250 }
251 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 rev[--rev_i] = s[s_i];
253
254 }
255 rev[len] = NUL;
256 }
257 return rev;
258}
259#endif
260
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100261 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100262save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263{
264 if (spats[idx].pat != pat)
265 {
266 vim_free(spats[idx].pat);
267 spats[idx].pat = vim_strsave(pat);
268 spats[idx].magic = magic;
269 spats[idx].no_scs = no_smartcase;
270 last_idx = idx;
271#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100272 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000274 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200275 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#endif
277 }
278}
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280/*
281 * Save the search patterns, so they can be restored later.
282 * Used before/after executing autocommands and user functions.
283 */
284static int save_level = 0;
285
286 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100287save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288{
289 if (save_level++ == 0)
290 {
291 saved_spats[0] = spats[0];
292 if (spats[0].pat != NULL)
293 saved_spats[0].pat = vim_strsave(spats[0].pat);
294 saved_spats[1] = spats[1];
295 if (spats[1].pat != NULL)
296 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100297#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100298 saved_spats_last_idx = last_idx;
299 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 }
302}
303
304 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100305restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306{
307 if (--save_level == 0)
308 {
309 vim_free(spats[0].pat);
310 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100311#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000312 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 vim_free(spats[1].pat);
315 spats[1] = saved_spats[1];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100316#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100317 last_idx = saved_spats_last_idx;
318 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 }
321}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000323#if defined(EXITFREE) || defined(PROTO)
324 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100325free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000326{
327 vim_free(spats[0].pat);
328 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000329
330# ifdef FEAT_RIGHTLEFT
331 if (mr_pattern_alloced)
332 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200333 vim_free(mr_pattern);
334 mr_pattern_alloced = FALSE;
335 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000336 }
337# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000338}
339#endif
340
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100341#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100342// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
343// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200344static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100345static int did_save_last_search_spat = 0;
346static int saved_last_idx = 0;
347static int saved_no_hlsearch = 0;
348
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100349/*
350 * Save and restore the search pattern for incremental highlight search
351 * feature.
352 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100353 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100354 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100355 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100356 */
357 void
358save_last_search_pattern(void)
359{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200360 if (++did_save_last_search_spat != 1)
361 // nested call, nothing to do
362 return;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100363
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100364 saved_last_search_spat = spats[RE_SEARCH];
365 if (spats[RE_SEARCH].pat != NULL)
366 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
367 saved_last_idx = last_idx;
368 saved_no_hlsearch = no_hlsearch;
369}
370
371 void
372restore_last_search_pattern(void)
373{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200374 if (--did_save_last_search_spat > 0)
375 // nested call, nothing to do
376 return;
377 if (did_save_last_search_spat != 0)
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100378 {
Bram Moolenaar442a8532020-06-04 20:56:09 +0200379 iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100380 return;
381 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100382
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100383 vim_free(spats[RE_SEARCH].pat);
384 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100385 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100386# if defined(FEAT_EVAL)
387 set_vv_searchforward();
388# endif
389 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200390 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100391}
Bram Moolenaard0480092017-11-16 22:20:39 +0100392
393 char_u *
394last_search_pattern(void)
395{
396 return spats[RE_SEARCH].pat;
397}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100398#endif
399
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400/*
401 * Return TRUE when case should be ignored for search pattern "pat".
402 * Uses the 'ignorecase' and 'smartcase' options.
403 */
404 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100405ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200407 return ignorecase_opt(pat, p_ic, p_scs);
408}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200410/*
411 * As ignorecase() put pass the "ic" and "scs" flags.
412 */
413 int
414ignorecase_opt(char_u *pat, int ic_in, int scs)
415{
416 int ic = ic_in;
417
418 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200419 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200420 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 no_smartcase = FALSE;
422
423 return ic;
424}
425
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200426/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200427 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200428 */
429 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100430pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200431{
432 char_u *p = pat;
Christian Brabandt78ba9332021-08-01 12:44:37 +0200433 magic_T magic_val = MAGIC_ON;
434
435 // get the magicness of the pattern
436 (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val);
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200437
438 while (*p != NUL)
439 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200440 int l;
441
442 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
443 {
444 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
445 return TRUE;
446 p += l;
447 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200448 else if (*p == '\\' && magic_val == MAGIC_ON)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200449 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100450 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200451 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100452 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200453 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100454 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200455 p += 2;
456 else
457 p += 1;
458 }
Christian Brabandt78ba9332021-08-01 12:44:37 +0200459 else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL)
460 {
461 if (p[1] != NUL) // skip "_X" and %X
462 p += 2;
463 }
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200464 else if (MB_ISUPPER(*p))
465 return TRUE;
466 else
467 ++p;
468 }
469 return FALSE;
470}
471
Bram Moolenaar113e1072019-01-20 15:30:40 +0100472#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100474last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200475{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200476 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200477}
478
479 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100480last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200481{
482 return lastcdir == FORWARD;
483}
484
485 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100486last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200487{
488 return last_t_cmd == TRUE;
489}
490
491 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100492set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200493{
494 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200495 lastc_bytelen = len;
496 if (len)
497 memcpy(lastc_bytes, s, len);
498 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200499 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200500}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100501#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200502
503 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100504set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200505{
506 lastcdir = cdir;
507}
508
509 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100510set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200511{
512 last_t_cmd = t_cmd;
513}
514
515 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100516last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
518 return spats[last_idx].pat;
519}
520
521/*
522 * Reset search direction to forward. For "gd" and "gD" commands.
523 */
524 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100525reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#if defined(FEAT_EVAL)
529 set_vv_searchforward();
530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531}
532
533#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
534/*
535 * Set the last search pattern. For ":let @/ =" and viminfo.
536 * Also set the saved search pattern, so that this works in an autocommand.
537 */
538 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100539set_last_search_pat(
540 char_u *s,
541 int idx,
542 int magic,
543 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544{
545 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100546 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 if (*s == NUL)
548 spats[idx].pat = NULL;
549 else
550 spats[idx].pat = vim_strsave(s);
551 spats[idx].magic = magic;
552 spats[idx].no_scs = FALSE;
553 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000554#if defined(FEAT_EVAL)
555 set_vv_searchforward();
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 spats[idx].off.line = FALSE;
558 spats[idx].off.end = FALSE;
559 spats[idx].off.off = 0;
560 if (setlast)
561 last_idx = idx;
562 if (save_level)
563 {
564 vim_free(saved_spats[idx].pat);
565 saved_spats[idx] = spats[0];
566 if (spats[idx].pat == NULL)
567 saved_spats[idx].pat = NULL;
568 else
569 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100570# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100571 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100575 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000577 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578# endif
579}
580#endif
581
582#ifdef FEAT_SEARCH_EXTRA
583/*
584 * Get a regexp program for the last used search pattern.
585 * This is used for highlighting all matches in a window.
586 * Values returned in regmatch->regprog and regmatch->rmm_ic.
587 */
588 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100589last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 if (spats[last_idx].pat == NULL)
592 {
593 regmatch->regprog = NULL;
594 return;
595 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100596 ++emsg_off; // So it doesn't beep if bad expr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
598 --emsg_off;
599}
600#endif
601
602/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100603 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100604 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
605 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 *
607 * if (options & SEARCH_MSG) == 0 don't give any messages
608 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
609 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
610 * if (options & SEARCH_HIS) put search pattern in history
611 * if (options & SEARCH_END) return position at end of match
612 * if (options & SEARCH_START) accept match at pos itself
613 * if (options & SEARCH_KEEP) keep previous search pattern
614 * if (options & SEARCH_FOLD) match only once in a closed fold
615 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100616 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 *
618 * Return FAIL (zero) for failure, non-zero for success.
619 * When FEAT_EVAL is defined, returns the index of the first matching
620 * subpattern plus one; one if there was none.
621 */
622 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100623searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200624 win_T *win, // window to search in; can be NULL for a
625 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100626 buf_T *buf,
627 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100628 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100629 int dir,
630 char_u *pat,
631 long count,
632 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200633 int pat_use, // which pattern to use when "pat" is empty
634 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100637 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100638 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 regmmatch_T regmatch;
640 char_u *ptr;
641 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000643 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 int loop;
645 pos_T start_pos;
646 int at_first_line;
647 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200648 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 int match_ok;
650 long nmatched;
651 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100652 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100653 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#ifdef FEAT_SEARCH_EXTRA
655 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200657 linenr_T stop_lnum = 0; // stop after this line number when != 0
658#ifdef FEAT_RELTIME
659 proftime_T *tm = NULL; // timeout limit or NULL
660 int *timed_out = NULL; // set when timed out or NULL
661#endif
662
663 if (extra_arg != NULL)
664 {
665 stop_lnum = extra_arg->sa_stop_lnum;
666#ifdef FEAT_RELTIME
667 tm = extra_arg->sa_tm;
668 timed_out = &extra_arg->sa_timed_out;
669#endif
670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
672 if (search_regcomp(pat, RE_SEARCH, pat_use,
673 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
674 {
675 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100676 semsg(_("E383: Invalid search string: %s"), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 return FAIL;
678 }
679
Bram Moolenaar280f1262006-01-30 00:14:18 +0000680 /*
681 * find the string
682 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100683 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100685 // When not accepting a match at the start position set "extra_col" to
686 // a non-zero value. Don't do that when starting at MAXCOL, since
687 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200688 if (pos->col == MAXCOL)
689 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100690 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200691 else if (has_mbyte
692 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
693 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100694 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100695 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100696 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200697 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100698 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100699 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100700 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100701 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200702 start_char_len = 1;
703 if (dir == FORWARD)
704 {
705 if (options & SEARCH_START)
706 extra_col = 0;
707 else
708 extra_col = start_char_len;
709 }
710 else
711 {
712 if (options & SEARCH_START)
713 extra_col = start_char_len;
714 else
715 extra_col = 0;
716 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100717
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100718 start_pos = *pos; // remember start pos for detecting no match
719 found = 0; // default: not found
720 at_first_line = TRUE; // default: start in first line
721 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 pos->lnum = 1;
724 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100725 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 }
727
728 /*
729 * Start searching in current line, unless searching backwards and
730 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000731 * If we are searching backwards, in column 0, and not including the
732 * current position, gain some efficiency by skipping back a line.
733 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000735 if (dir == BACKWARD && start_pos.col == 0
736 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 {
738 lnum = pos->lnum - 1;
739 at_first_line = FALSE;
740 }
741 else
742 lnum = pos->lnum;
743
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100744 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
747 lnum += dir, at_first_line = FALSE)
748 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100749 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000750 if (stop_lnum != 0 && (dir == FORWARD
751 ? lnum > stop_lnum : lnum < stop_lnum))
752 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000753#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100754 // Stop after passing the "tm" time limit.
Bram Moolenaar76929292008-01-06 19:07:36 +0000755 if (tm != NULL && profile_passed_limit(tm))
756 break;
757#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000758
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000760 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100762 col = at_first_line && (options & SEARCH_COL) ? pos->col
763 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100765 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000766#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200767 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000768#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200769 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000770#endif
771 );
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).
Bram Moolenaar53989552019-12-23 22:59:18 +0100776 if (called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200777#ifdef FEAT_RELTIME
778 || (timed_out != NULL && *timed_out)
779#endif
780 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 break;
782 if (nmatched > 0)
783 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100784 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000785 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000787#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000789#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100790 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000791 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
792 ptr = (char_u *)"";
793 else
794 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /*
797 * Forward search in the first line: match should be after
798 * the start position. If not, continue at the end of the
799 * match (this is vi compatible) or on the next char.
800 */
801 if (dir == FORWARD && at_first_line)
802 {
803 match_ok = TRUE;
804 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000805 * When the match starts in a next line it's certainly
806 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 * When match lands on a NUL the cursor will be put
808 * one back afterwards, compare with that position,
809 * otherwise "/$" will get stuck on end of line.
810 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000811 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100812 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000813 ? (nmatched == 1
814 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000816 : ((int)matchpos.col
817 - (ptr[matchpos.col] == NUL)
818 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 /*
821 * If vi-compatible searching, continue at the end
822 * of the match, otherwise continue one position
823 * forward.
824 */
825 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
826 {
827 if (nmatched > 1)
828 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100829 // end is in next line, thus no match in
830 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 match_ok = FALSE;
832 break;
833 }
834 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100835 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000836 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 && ptr[matchcol] != NUL)
838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 if (has_mbyte)
840 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000841 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 ++matchcol;
844 }
845 }
846 else
847 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000848 matchcol = matchpos.col;
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,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000863 matchcol,
864#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200865 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000866#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200867 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000868#endif
869 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
871 match_ok = FALSE;
872 break;
873 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200874 // vim_regexec_multi() may clear "regprog"
875 if (regmatch.regprog == NULL)
876 break;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000877 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 endpos = regmatch.endpos[0];
879# ifdef FEAT_EVAL
880 submatch = first_submatch(&regmatch);
881# endif
882
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100883 // Need to get the line pointer again, a
884 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000885 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 }
887 if (!match_ok)
888 continue;
889 }
890 if (dir == BACKWARD)
891 {
892 /*
893 * Now, if there are multiple matches on this line,
894 * we have to get the last one. Or the last one before
895 * the cursor, if we're on that line.
896 * When putting the new cursor at the end, compare
897 * relative to the end of the match.
898 */
899 match_ok = FALSE;
900 for (;;)
901 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100902 // Remember a position that is before the start
903 // position, we use it if it's the last match in
904 // the line. Always accept a position after
905 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000906 if (loop
907 || ((options & SEARCH_END)
908 ? (lnum + regmatch.endpos[0].lnum
909 < start_pos.lnum
910 || (lnum + regmatch.endpos[0].lnum
911 == start_pos.lnum
912 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200913 < (int)start_pos.col
914 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000915 : (lnum + regmatch.startpos[0].lnum
916 < start_pos.lnum
917 || (lnum + regmatch.startpos[0].lnum
918 == start_pos.lnum
919 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200920 < (int)start_pos.col
921 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000924 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 endpos = regmatch.endpos[0];
926# ifdef FEAT_EVAL
927 submatch = first_submatch(&regmatch);
928# endif
929 }
930 else
931 break;
932
933 /*
934 * We found a valid match, now check if there is
935 * another one after it.
936 * If vi-compatible searching, continue at the end
937 * of the match, otherwise continue one position
938 * forward.
939 */
940 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
941 {
942 if (nmatched > 1)
943 break;
944 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100945 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000946 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 && ptr[matchcol] != NUL)
948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (has_mbyte)
950 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000951 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 ++matchcol;
954 }
955 }
956 else
957 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100958 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000959 if (matchpos.lnum > 0)
960 break;
961 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (ptr[matchcol] != NUL)
963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 if (has_mbyte)
965 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000966 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 ++matchcol;
969 }
970 }
971 if (ptr[matchcol] == NUL
972 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000973 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000974 matchcol,
975#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200976 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000977#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200978 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000979#endif
980 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100981 {
982#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100983 // If the search timed out, we did find a match
984 // but it might be the wrong one, so that's not
985 // OK.
Bram Moolenaar9d322762018-02-09 16:04:25 +0100986 if (timed_out != NULL && *timed_out)
987 match_ok = FALSE;
988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100990 }
Bram Moolenaar795aaa12020-10-02 20:36:01 +0200991 // vim_regexec_multi() may clear "regprog"
992 if (regmatch.regprog == NULL)
993 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100995 // Need to get the line pointer again, a
996 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000997 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
999
1000 /*
1001 * If there is only a match after the cursor, skip
1002 * this match.
1003 */
1004 if (!match_ok)
1005 continue;
1006 }
1007
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001008 // With the SEARCH_END option move to the last character
1009 // of the match. Don't do it for an empty match, end
1010 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +02001011 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001012 && !(matchpos.lnum == endpos.lnum
1013 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001015 // For a match in the first column, set the position
1016 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001017 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001018 pos->col = endpos.col;
1019 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001020 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001021 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001022 {
1023 --pos->lnum;
1024 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1025 pos->lnum, FALSE));
1026 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001027 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001028 else
1029 {
1030 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001031 if (has_mbyte
1032 && pos->lnum <= buf->b_ml.ml_line_count)
1033 {
1034 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1035 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1036 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001037 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001038 if (end_pos != NULL)
1039 {
1040 end_pos->lnum = lnum + matchpos.lnum;
1041 end_pos->col = matchpos.col;
1042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
1044 else
1045 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001046 pos->lnum = lnum + matchpos.lnum;
1047 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001048 if (end_pos != NULL)
1049 {
1050 end_pos->lnum = lnum + endpos.lnum;
1051 end_pos->col = endpos.col;
1052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001055 if (end_pos != NULL)
1056 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001058 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001060 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001061 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 search_match_endcol = endpos.col;
1063 break;
1064 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001065 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (got_int)
1067 break;
1068
1069#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001070 // Cancel searching if a character was typed. Used for
1071 // 'incsearch'. Don't check too often, that would slowdown
1072 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if ((options & SEARCH_PEEK)
1074 && ((lnum - pos->lnum) & 0x3f) == 0
1075 && char_avail())
1076 {
1077 break_loop = TRUE;
1078 break;
1079 }
1080#endif
1081
1082 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001083 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 at_first_line = FALSE;
1086
Bram Moolenaar795aaa12020-10-02 20:36:01 +02001087 // vim_regexec_multi() may clear "regprog"
1088 if (regmatch.regprog == NULL)
1089 break;
1090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001092 * Stop the search if wrapscan isn't set, "stop_lnum" is
1093 * specified, after an interrupt, after a match and after looping
1094 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001096 if (!p_ws || stop_lnum != 0 || got_int
1097 || called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001098#ifdef FEAT_RELTIME
1099 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001100#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001101#ifdef FEAT_SEARCH_EXTRA
1102 || break_loop
1103#endif
1104 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 break;
1106
1107 /*
1108 * If 'wrapscan' is set we continue at the other end of the file.
1109 * If 'shortmess' does not contain 's', we give a message.
1110 * This message is also remembered in keep_msg for when the screen
1111 * is redrawn. The keep_msg is cleared whenever another message is
1112 * written.
1113 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001114 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001118 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1119 give_warning((char_u *)_(dir == BACKWARD
1120 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001121 if (extra_arg != NULL)
1122 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 }
Bram Moolenaar53989552019-12-23 22:59:18 +01001124 if (got_int || called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001125#ifdef FEAT_RELTIME
1126 || (timed_out != NULL && *timed_out)
1127#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001128#ifdef FEAT_SEARCH_EXTRA
1129 || break_loop
1130#endif
1131 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 break;
1133 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001134 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
Bram Moolenaar473de612013-06-08 18:19:48 +02001136 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001138 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001141 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1143 {
1144 if (p_ws)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001145 semsg(_(e_patnotf2), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 else if (lnum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001147 semsg(_("E384: search hit TOP without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 mr_pattern);
1149 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001150 semsg(_("E385: search hit BOTTOM without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 mr_pattern);
1152 }
1153 return FAIL;
1154 }
1155
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001156 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001157 if (pos->lnum > buf->b_ml.ml_line_count)
1158 {
1159 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001160 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001161 if (pos->col > 0)
1162 --pos->col;
1163 }
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 return submatch + 1;
1166}
1167
1168#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001169 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001170set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001171{
1172 spats[0].off.dir = cdir;
1173}
1174
1175 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001176set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001177{
1178 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1179}
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181/*
1182 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001183 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 */
1185 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001186first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 int submatch;
1189
1190 for (submatch = 1; ; ++submatch)
1191 {
1192 if (rp->startpos[submatch].lnum >= 0)
1193 break;
1194 if (submatch == 9)
1195 {
1196 submatch = 0;
1197 break;
1198 }
1199 }
1200 return submatch;
1201}
1202#endif
1203
1204/*
1205 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001206 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 * If 'dirc' is 0: use previous dir.
1208 * If 'pat' is NULL or empty : use previous string.
1209 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1210 * If 'options & SEARCH_ECHO': echo the search command and handle options
1211 * If 'options & SEARCH_MSG' : may give error message
1212 * If 'options & SEARCH_OPT' : interpret optional flags
1213 * If 'options & SEARCH_HIS' : put search pattern in history
1214 * If 'options & SEARCH_NOOF': don't add offset to position
1215 * If 'options & SEARCH_MARK': set previous context mark
1216 * If 'options & SEARCH_KEEP': keep previous search pattern
1217 * If 'options & SEARCH_START': accept match at curpos itself
1218 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1219 *
1220 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1221 * makes the movement linewise without moving the match position.
1222 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001223 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 */
1225 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001226do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001227 oparg_T *oap, // can be NULL
1228 int dirc, // '/' or '?'
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001229 int search_delim, // the delimiter for the search, e.g. '%' in
1230 // s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001231 char_u *pat,
1232 long count,
1233 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001234 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001236 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001238 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001239 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 char_u *p;
1241 long c;
1242 char_u *dircp;
1243 char_u *strcopy = NULL;
1244 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001245 char_u *msgbuf = NULL;
1246 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001247 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
1249 /*
1250 * A line offset is not remembered, this is vi compatible.
1251 */
1252 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1253 {
1254 spats[0].off.line = FALSE;
1255 spats[0].off.off = 0;
1256 }
1257
1258 /*
1259 * Save the values for when (options & SEARCH_KEEP) is used.
1260 * (there is no "if ()" around this because gcc wants them initialized)
1261 */
1262 old_off = spats[0].off;
1263
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001264 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
1266 /*
1267 * Find out the direction of the search.
1268 */
1269 if (dirc == 0)
1270 dirc = spats[0].off.dir;
1271 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001274#if defined(FEAT_EVAL)
1275 set_vv_searchforward();
1276#endif
1277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 if (options & SEARCH_REV)
1279 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001280#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001281 // There is a bug in the Visual C++ 2.2 compiler which means that
1282 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 dirc = (dirc == '/') ? '?' : '/';
1284#else
1285 if (dirc == '/')
1286 dirc = '?';
1287 else
1288 dirc = '/';
1289#endif
1290 }
1291
1292#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001293 // If the cursor is in a closed fold, don't find another match in the same
1294 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (dirc == '/')
1296 {
1297 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001298 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300 else
1301 {
1302 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1303 pos.col = 0;
1304 }
1305#endif
1306
1307#ifdef FEAT_SEARCH_EXTRA
1308 /*
1309 * Turn 'hlsearch' highlighting back on.
1310 */
1311 if (no_hlsearch && !(options & SEARCH_KEEP))
1312 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001313 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001314 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316#endif
1317
1318 /*
1319 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1320 */
1321 for (;;)
1322 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001323 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001324
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 searchstr = pat;
1326 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001327 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001328 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001330 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001332 searchstr = spats[RE_SUBST].pat;
1333 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001334 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001335 emsg(_(e_no_previous_regular_expression));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001336 retval = 0;
1337 goto end_do_search;
1338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001340 else
1341 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001342 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001343 searchstr = (char_u *)"";
1344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 }
1346
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001347 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 {
1349 /*
1350 * Find end of regular expression.
1351 * If there is a matching '/' or '?', toss it.
1352 */
1353 ps = strcopy;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01001354 p = skip_regexp_ex(pat, search_delim, magic_isset(),
Bram Moolenaard93a7fc2021-01-04 12:42:13 +01001355 &strcopy, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (strcopy != ps)
1357 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001358 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001359 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 pat = strcopy;
1361 searchstr = strcopy;
1362 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001363 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001365 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 *p++ = NUL;
1367 }
1368 spats[0].off.line = FALSE;
1369 spats[0].off.end = FALSE;
1370 spats[0].off.off = 0;
1371 /*
1372 * Check for a line offset or a character offset.
1373 * For get_address (echo off) we don't check for a character
1374 * offset, because it is meaningless and the 's' could be a
1375 * substitute command.
1376 */
1377 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1378 spats[0].off.line = TRUE;
1379 else if ((options & SEARCH_OPT) &&
1380 (*p == 'e' || *p == 's' || *p == 'b'))
1381 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001382 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 spats[0].off.end = SEARCH_END;
1384 ++p;
1385 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001386 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001388 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1390 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001391 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001393 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 spats[0].off.off = 1;
1395 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001396 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ++p;
1398 }
1399
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001400 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 searchcmdlen += (int)(p - pat);
1402
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001403 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001406 if ((options & SEARCH_ECHO) && messaging() &&
1407 !msg_silent &&
1408 (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001411 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001412 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001414 // Compute msg_row early.
1415 msg_start();
1416
Bram Moolenaar984f0312019-05-24 13:11:47 +02001417 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001418 if (!cmd_silent &&
1419 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001420 {
1421 p = off_buf;
1422 *p++ = dirc;
1423 if (spats[0].off.end)
1424 *p++ = 'e';
1425 else if (!spats[0].off.line)
1426 *p++ = 's';
1427 if (spats[0].off.off > 0 || spats[0].off.line)
1428 *p++ = '+';
1429 *p = NUL;
1430 if (spats[0].off.off != 0 || spats[0].off.line)
1431 sprintf((char *)p, "%ld", spats[0].off.off);
1432 off_len = STRLEN(off_buf);
1433 }
1434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001436 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else
1438 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001439
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001440 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001441 {
1442 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001443 // search stat. Use all the space available, so that the
1444 // search state is right aligned. If there is not enough space
1445 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001446 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001447 // Use all the columns.
1448 len = (int)(Rows - msg_row) * Columns - 1;
1449 else
1450 // Use up to 'showcmd' column.
1451 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001452 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1453 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001454 }
1455 else
1456 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001457 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001458
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001459 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001460 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if (msgbuf != NULL)
1462 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001463 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001464 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001465 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1466 // empty for the search_stat feature.
1467 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001468 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001469 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001471 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1472 {
1473 // Use a space to draw the composing char on.
1474 msgbuf[1] = ' ';
1475 mch_memmove(msgbuf + 2, p, STRLEN(p));
1476 }
1477 else
1478 mch_memmove(msgbuf + 1, p, STRLEN(p));
1479 if (off_len > 0)
1480 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001482 trunc = msg_strtrunc(msgbuf, TRUE);
1483 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001485 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001486 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001489#ifdef FEAT_RIGHTLEFT
1490 // The search pattern could be shown on the right in
1491 // rightleft mode, but the 'ruler' and 'showcmd' area use
1492 // it too, thus it would be blanked out again very soon.
1493 // Show it on the left, but do reverse the text.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001494 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1495 {
1496 char_u *r;
1497 size_t pat_len;
1498
1499 r = reverse_text(msgbuf);
1500 if (r != NULL)
1501 {
1502 vim_free(msgbuf);
1503 msgbuf = r;
1504 // move reversed text to beginning of buffer
1505 while (*r != NUL && *r == ' ')
1506 r++;
1507 pat_len = msgbuf + STRLEN(msgbuf) - r;
1508 mch_memmove(msgbuf, r, pat_len);
1509 // overwrite old text
1510 if ((size_t)(r - msgbuf) >= pat_len)
1511 vim_memset(r, ' ', pat_len);
1512 else
1513 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1514 }
1515 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001516#endif
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001517 msg_outtrans(msgbuf);
1518 msg_clr_eos();
1519 msg_check();
1520
1521 gotocmdline(FALSE);
1522 out_flush();
1523 msg_nowait = TRUE; // don't wait for this message
1524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526 }
1527
1528 /*
1529 * If there is a character offset, subtract it from the current
1530 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001531 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 * This is not done for a line offset, because then we would not be vi
1533 * compatible.
1534 */
Bram Moolenaared203462004-06-16 11:19:22 +00001535 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 {
1537 if (spats[0].off.off > 0)
1538 {
1539 for (c = spats[0].off.off; c; --c)
1540 if (decl(&pos) == -1)
1541 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001542 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001544 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 pos.col = MAXCOL;
1546 }
1547 }
1548 else
1549 {
1550 for (c = spats[0].off.off; c; ++c)
1551 if (incl(&pos) == -1)
1552 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001553 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1556 pos.col = 0;
1557 }
1558 }
1559 }
1560
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001561 /*
1562 * The actual search.
1563 */
Bram Moolenaar14184a32019-02-16 15:10:30 +01001564 c = searchit(curwin, curbuf, &pos, NULL,
1565 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 searchstr, count, spats[0].off.end + (options &
1567 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1568 + SEARCH_MSG + SEARCH_START
1569 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001570 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572 if (dircp != NULL)
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001573 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001574
1575 if (!shortmess(SHM_SEARCH)
1576 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1577 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001578 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001579
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 if (c == FAIL)
1581 {
1582 retval = 0;
1583 goto end_do_search;
1584 }
1585 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001586 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001588 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 /*
1591 * Add character and/or line offset
1592 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001593 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001595 pos_T org_pos = pos;
1596
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001597 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 {
1599 c = pos.lnum + spats[0].off.off;
1600 if (c < 1)
1601 pos.lnum = 1;
1602 else if (c > curbuf->b_ml.ml_line_count)
1603 pos.lnum = curbuf->b_ml.ml_line_count;
1604 else
1605 pos.lnum = c;
1606 pos.col = 0;
1607
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001608 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001610 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001612 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001613 c = spats[0].off.off;
1614 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 if (incl(&pos) == -1)
1618 break;
1619 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001620 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 else
1622 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001623 while (c++ < 0)
1624 if (decl(&pos) == -1)
1625 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
1627 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001628 if (!EQUAL_POS(pos, org_pos))
1629 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001632 // Show [1/15] if 'S' is not in 'shortmess'.
1633 if ((options & SEARCH_ECHO)
1634 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001635 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001636 && c != FAIL
1637 && !shortmess(SHM_SEARCHCOUNT)
1638 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001639 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1640 show_top_bot_msg, msgbuf,
1641 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001642#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001643 || (!(fdo_flags & FDO_SEARCH)
1644 && hasFolding(curwin->w_cursor.lnum,
1645 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001646#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001647 ),
1648 SEARCH_STAT_DEF_MAX_COUNT,
1649 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 /*
1652 * The search command can be followed by a ';' to do another search.
1653 * For example: "/pat/;/foo/+3;?bar"
1654 * This is like doing another search command, except:
1655 * - The remembered direction '/' or '?' is from the first search.
1656 * - When an error happens the cursor isn't moved at all.
1657 * Don't do this when called by get_address() (it handles ';' itself).
1658 */
1659 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1660 break;
1661
1662 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001663 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (dirc != '?' && dirc != '/')
1665 {
1666 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001667 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 goto end_do_search;
1669 }
1670 ++pat;
1671 }
1672
1673 if (options & SEARCH_MARK)
1674 setpcmark();
1675 curwin->w_cursor = pos;
1676 curwin->w_set_curswant = TRUE;
1677
1678end_do_search:
Bram Moolenaare1004402020-10-24 20:49:43 +02001679 if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 spats[0].off = old_off;
1681 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001682 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684 return retval;
1685}
1686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687/*
1688 * search_for_exact_line(buf, pos, dir, pat)
1689 *
1690 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001691 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001693 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 * Return OK for success, or FAIL if no line found.
1695 */
1696 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001697search_for_exact_line(
1698 buf_T *buf,
1699 pos_T *pos,
1700 int dir,
1701 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702{
1703 linenr_T start = 0;
1704 char_u *ptr;
1705 char_u *p;
1706
1707 if (buf->b_ml.ml_line_count == 0)
1708 return FAIL;
1709 for (;;)
1710 {
1711 pos->lnum += dir;
1712 if (pos->lnum < 1)
1713 {
1714 if (p_ws)
1715 {
1716 pos->lnum = buf->b_ml.ml_line_count;
1717 if (!shortmess(SHM_SEARCH))
1718 give_warning((char_u *)_(top_bot_msg), TRUE);
1719 }
1720 else
1721 {
1722 pos->lnum = 1;
1723 break;
1724 }
1725 }
1726 else if (pos->lnum > buf->b_ml.ml_line_count)
1727 {
1728 if (p_ws)
1729 {
1730 pos->lnum = 1;
1731 if (!shortmess(SHM_SEARCH))
1732 give_warning((char_u *)_(bot_top_msg), TRUE);
1733 }
1734 else
1735 {
1736 pos->lnum = 1;
1737 break;
1738 }
1739 }
1740 if (pos->lnum == start)
1741 break;
1742 if (start == 0)
1743 start = pos->lnum;
1744 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1745 p = skipwhite(ptr);
1746 pos->col = (colnr_T) (p - ptr);
1747
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001748 // when adding lines the matching line may be empty but it is not
1749 // ignored because we are interested in the next line -- Acevedo
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001750 if ((compl_cont_status & CONT_ADDING)
1751 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 {
1753 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1754 return OK;
1755 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001756 else if (*p != NUL) // ignore empty lines
1757 { // expanding lines or words
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001758 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1759 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 return OK;
1761 }
1762 }
1763 return FAIL;
1764}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
1766/*
1767 * Character Searches
1768 */
1769
1770/*
1771 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1772 * position of the character, otherwise move to just before the char.
1773 * Do this "cap->count1" times.
1774 * Return FAIL or OK.
1775 */
1776 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001777searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001779 int c = cap->nchar; // char to search for
1780 int dir = cap->arg; // TRUE for searching forward
1781 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 int col;
1783 char_u *p;
1784 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001785 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001787 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001789 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001791 *lastc = c;
1792 set_csearch_direction(dir);
1793 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001794 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 if (cap->ncharC1 != 0)
1796 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001797 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1798 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001800 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1801 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 }
1804 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001805 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001807 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001809 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 dir = -lastcdir;
1811 else
1812 dir = lastcdir;
1813 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001814 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001815 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001816
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001817 // Force a move of at least one char, so ";" and "," will move the
1818 // cursor, even if the cursor is right in front of char we are looking
1819 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001820 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001821 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 }
1823
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001824 if (dir == BACKWARD)
1825 cap->oap->inclusive = FALSE;
1826 else
1827 cap->oap->inclusive = TRUE;
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 p = ml_get_curline();
1830 col = curwin->w_cursor.col;
1831 len = (int)STRLEN(p);
1832
1833 while (count--)
1834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (has_mbyte)
1836 {
1837 for (;;)
1838 {
1839 if (dir > 0)
1840 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001841 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 if (col >= len)
1843 return FAIL;
1844 }
1845 else
1846 {
1847 if (col == 0)
1848 return FAIL;
1849 col -= (*mb_head_off)(p, p + col - 1) + 1;
1850 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001851 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001853 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 break;
1855 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001856 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001857 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001858 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001859 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 }
1862 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 {
1864 for (;;)
1865 {
1866 if ((col += dir) < 0 || col >= len)
1867 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001868 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001870 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 }
1872 }
1873 }
1874
1875 if (t_cmd)
1876 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001877 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (has_mbyte)
1880 {
1881 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001882 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001883 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001885 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 col -= (*mb_head_off)(p, p + col);
1887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 }
1889 curwin->w_cursor.col = col;
1890
1891 return OK;
1892}
1893
1894/*
1895 * "Other" Searches
1896 */
1897
1898/*
1899 * findmatch - find the matching paren or brace
1900 *
1901 * Improvement over vi: Braces inside quotes are ignored.
1902 */
1903 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001904findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905{
1906 return findmatchlimit(oap, initc, 0, 0);
1907}
1908
1909/*
1910 * Return TRUE if the character before "linep[col]" equals "ch".
1911 * Return FALSE if "col" is zero.
1912 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1913 * is NULL.
1914 * Handles multibyte string correctly.
1915 */
1916 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001917check_prevcol(
1918 char_u *linep,
1919 int col,
1920 int ch,
1921 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922{
1923 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (col > 0 && has_mbyte)
1925 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (prevcol)
1927 *prevcol = col;
1928 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1929}
1930
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001931/*
1932 * Raw string start is found at linep[startpos.col - 1].
1933 * Return TRUE if the matching end can be found between startpos and endpos.
1934 */
1935 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001936find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001937{
1938 char_u *p;
1939 char_u *delim_copy;
1940 size_t delim_len;
1941 linenr_T lnum;
1942 int found = FALSE;
1943
1944 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1945 ;
1946 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001947 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001948 if (delim_copy == NULL)
1949 return FALSE;
1950 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1951 {
1952 char_u *line = ml_get(lnum);
1953
1954 for (p = line + (lnum == startpos->lnum
1955 ? startpos->col + 1 : 0); *p; ++p)
1956 {
1957 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1958 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02001959 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
1960 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001961 {
1962 found = TRUE;
1963 break;
1964 }
1965 }
1966 if (found)
1967 break;
1968 }
1969 vim_free(delim_copy);
1970 return found;
1971}
1972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001974 * Check matchpairs option for "*initc".
1975 * If there is a match set "*initc" to the matching character and "*findc" to
1976 * the opposite character. Set "*backwards" to the direction.
1977 * When "switchit" is TRUE swap the direction.
1978 */
1979 static void
1980find_mps_values(
1981 int *initc,
1982 int *findc,
1983 int *backwards,
1984 int switchit)
1985{
1986 char_u *ptr;
1987
1988 ptr = curbuf->b_p_mps;
1989 while (*ptr != NUL)
1990 {
1991 if (has_mbyte)
1992 {
1993 char_u *prev;
1994
1995 if (mb_ptr2char(ptr) == *initc)
1996 {
1997 if (switchit)
1998 {
1999 *findc = *initc;
2000 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2001 *backwards = TRUE;
2002 }
2003 else
2004 {
2005 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
2006 *backwards = FALSE;
2007 }
2008 return;
2009 }
2010 prev = ptr;
2011 ptr += mb_ptr2len(ptr) + 1;
2012 if (mb_ptr2char(ptr) == *initc)
2013 {
2014 if (switchit)
2015 {
2016 *findc = *initc;
2017 *initc = mb_ptr2char(prev);
2018 *backwards = FALSE;
2019 }
2020 else
2021 {
2022 *findc = mb_ptr2char(prev);
2023 *backwards = TRUE;
2024 }
2025 return;
2026 }
2027 ptr += mb_ptr2len(ptr);
2028 }
2029 else
2030 {
2031 if (*ptr == *initc)
2032 {
2033 if (switchit)
2034 {
2035 *backwards = TRUE;
2036 *findc = *initc;
2037 *initc = ptr[2];
2038 }
2039 else
2040 {
2041 *backwards = FALSE;
2042 *findc = ptr[2];
2043 }
2044 return;
2045 }
2046 ptr += 2;
2047 if (*ptr == *initc)
2048 {
2049 if (switchit)
2050 {
2051 *backwards = FALSE;
2052 *findc = *initc;
2053 *initc = ptr[-2];
2054 }
2055 else
2056 {
2057 *backwards = TRUE;
2058 *findc = ptr[-2];
2059 }
2060 return;
2061 }
2062 ++ptr;
2063 }
2064 if (*ptr == ',')
2065 ++ptr;
2066 }
2067}
2068
2069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002071 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2072 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 *
2074 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002075 * character at or after the cursor. Special values:
2076 * '*' look for C-style comment / *
2077 * '/' look for C-style comment / *, ignoring comment-end
2078 * '#' look for preprocessor directives
2079 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 *
2081 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2082 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2083 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2084 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002085 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002086 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002087 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 */
2089
2090 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002091findmatchlimit(
2092 oparg_T *oap,
2093 int initc,
2094 int flags,
2095 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002097 static pos_T pos; // current search position
2098 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002100 int count = 0; // cumulative number of braces
2101 int backwards = FALSE; // init for gcc
2102 int raw_string = FALSE; // search for raw string
2103 int inquote = FALSE; // TRUE when inside quotes
2104 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002106 int do_quotes; // check for quotes in current line
2107 int at_start; // do_quotes value at start position
2108 int hash_dir = 0; // Direction searched for # things
2109 int comment_dir = 0; // Direction searched for comments
2110 pos_T match_pos; // Where last slash-star was found
2111 int start_in_quotes; // start position is in quotes
2112 int traveled = 0; // how far we've searched so far
2113 int ignore_cend = FALSE; // ignore comment end
2114 int cpo_match; // vi compatible matching
2115 int cpo_bsl; // don't recognize backslashes
2116 int match_escaped = 0; // search for escaped match
2117 int dir; // Direction to search
2118 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002119#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002120 int lispcomm = FALSE; // inside of Lisp-style comment
2121 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
2124 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002125 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 linep = ml_get(pos.lnum);
2127
2128 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2129 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2130
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002131 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (flags & FM_BACKWARD)
2133 dir = BACKWARD;
2134 else if (flags & FM_FORWARD)
2135 dir = FORWARD;
2136 else
2137 dir = 0;
2138
2139 /*
2140 * if initc given, look in the table for the matching character
2141 * '/' and '*' are special cases: look for start or end of comment.
2142 * When '/' is used, we ignore running backwards into an star-slash, for
2143 * "[*" command, we just want to find any comment.
2144 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002145 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 comment_dir = dir;
2148 if (initc == '/')
2149 ignore_cend = TRUE;
2150 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002151 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 initc = NUL;
2153 }
2154 else if (initc != '#' && initc != NUL)
2155 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002156 find_mps_values(&initc, &findc, &backwards, TRUE);
Connor Lane Smithb9115da2021-07-31 13:31:42 +02002157 if (dir)
2158 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002159 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 return NULL;
2161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else
2163 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002164 /*
2165 * Either initc is '#', or no initc was given and we need to look
2166 * under the cursor.
2167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (initc == '#')
2169 {
2170 hash_dir = dir;
2171 }
2172 else
2173 {
2174 /*
2175 * initc was not given, must look for something to match under
2176 * or near the cursor.
2177 * Only check for special things when 'cpo' doesn't have '%'.
2178 */
2179 if (!cpo_match)
2180 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002181 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 ptr = skipwhite(linep);
2183 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2184 {
2185 ptr = skipwhite(ptr + 1);
2186 if ( STRNCMP(ptr, "if", 2) == 0
2187 || STRNCMP(ptr, "endif", 5) == 0
2188 || STRNCMP(ptr, "el", 2) == 0)
2189 hash_dir = 1;
2190 }
2191
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002192 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else if (linep[pos.col] == '/')
2194 {
2195 if (linep[pos.col + 1] == '*')
2196 {
2197 comment_dir = FORWARD;
2198 backwards = FALSE;
2199 pos.col++;
2200 }
2201 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2202 {
2203 comment_dir = BACKWARD;
2204 backwards = TRUE;
2205 pos.col--;
2206 }
2207 }
2208 else if (linep[pos.col] == '*')
2209 {
2210 if (linep[pos.col + 1] == '/')
2211 {
2212 comment_dir = BACKWARD;
2213 backwards = TRUE;
2214 }
2215 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2216 {
2217 comment_dir = FORWARD;
2218 backwards = FALSE;
2219 }
2220 }
2221 }
2222
2223 /*
2224 * If we are not on a comment or the # at the start of a line, then
2225 * look for brace anywhere on this line after the cursor.
2226 */
2227 if (!hash_dir && !comment_dir)
2228 {
2229 /*
2230 * Find the brace under or after the cursor.
2231 * If beyond the end of the line, use the last character in
2232 * the line.
2233 */
2234 if (linep[pos.col] == NUL && pos.col)
2235 --pos.col;
2236 for (;;)
2237 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002238 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (initc == NUL)
2240 break;
2241
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002242 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (findc)
2244 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002245 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 if (!findc)
2248 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002249 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (!cpo_match && *skipwhite(linep) == '#')
2251 hash_dir = 1;
2252 else
2253 return NULL;
2254 }
2255 else if (!cpo_bsl)
2256 {
2257 int col, bslcnt = 0;
2258
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002259 // Set "match_escaped" if there are an odd number of
2260 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2262 bslcnt++;
2263 match_escaped = (bslcnt & 1);
2264 }
2265 }
2266 }
2267 if (hash_dir)
2268 {
2269 /*
2270 * Look for matching #if, #else, #elif, or #endif
2271 */
2272 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002273 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if (initc != '#')
2275 {
2276 ptr = skipwhite(skipwhite(linep) + 1);
2277 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2278 hash_dir = 1;
2279 else if (STRNCMP(ptr, "endif", 5) == 0)
2280 hash_dir = -1;
2281 else
2282 return NULL;
2283 }
2284 pos.col = 0;
2285 while (!got_int)
2286 {
2287 if (hash_dir > 0)
2288 {
2289 if (pos.lnum == curbuf->b_ml.ml_line_count)
2290 break;
2291 }
2292 else if (pos.lnum == 1)
2293 break;
2294 pos.lnum += hash_dir;
2295 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002296 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ptr = skipwhite(linep);
2298 if (*ptr != '#')
2299 continue;
2300 pos.col = (colnr_T) (ptr - linep);
2301 ptr = skipwhite(ptr + 1);
2302 if (hash_dir > 0)
2303 {
2304 if (STRNCMP(ptr, "if", 2) == 0)
2305 count++;
2306 else if (STRNCMP(ptr, "el", 2) == 0)
2307 {
2308 if (count == 0)
2309 return &pos;
2310 }
2311 else if (STRNCMP(ptr, "endif", 5) == 0)
2312 {
2313 if (count == 0)
2314 return &pos;
2315 count--;
2316 }
2317 }
2318 else
2319 {
2320 if (STRNCMP(ptr, "if", 2) == 0)
2321 {
2322 if (count == 0)
2323 return &pos;
2324 count--;
2325 }
2326 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2327 {
2328 if (count == 0)
2329 return &pos;
2330 }
2331 else if (STRNCMP(ptr, "endif", 5) == 0)
2332 count++;
2333 }
2334 }
2335 return NULL;
2336 }
2337 }
2338
2339#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002340 // This is just guessing: when 'rightleft' is set, search for a matching
2341 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2343 backwards = !backwards;
2344#endif
2345
2346 do_quotes = -1;
2347 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002348 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002349
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002350 // backward search: Check if this line contains a single-line comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002351 if ((backwards && comment_dir)
2352#ifdef FEAT_LISP
2353 || lisp
2354#endif
2355 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002357#ifdef FEAT_LISP
2358 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002359 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 while (!got_int)
2362 {
2363 /*
2364 * Go to the next position, forward or backward. We could use
2365 * inc() and dec() here, but that is much slower
2366 */
2367 if (backwards)
2368 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002369#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002370 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002371 if (lispcomm && pos.col < (colnr_T)comment_col)
2372 break;
2373#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002374 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002376 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 break;
2378 --pos.lnum;
2379
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002380 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 break;
2382
2383 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002384 pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 do_quotes = -1;
2386 line_breakcheck();
2387
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002388 // Check if this line contains a single-line comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002389 if (comment_dir
2390#ifdef FEAT_LISP
2391 || lisp
2392#endif
2393 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002395#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002396 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002397 if (lisp && comment_col != MAXCOL)
2398 pos.col = comment_col;
2399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401 else
2402 {
2403 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 if (has_mbyte)
2405 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002408 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002410 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002411 // at end of line, go to next one
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002412#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002413 // don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002414 || (lisp && comment_col != MAXCOL
2415 && pos.col == (colnr_T)comment_col)
2416#endif
2417 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002419 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002420#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002421 // line is exhausted and comment with it,
2422 // don't search for match in code
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002423 || lispcomm
2424#endif
2425 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 break;
2427 ++pos.lnum;
2428
2429 if (maxtravel && traveled++ > maxtravel)
2430 break;
2431
2432 linep = ml_get(pos.lnum);
2433 pos.col = 0;
2434 do_quotes = -1;
2435 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002436#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002437 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002438 comment_col = check_linecomment(linep);
2439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 }
2441 else
2442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002444 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 ++pos.col;
2447 }
2448 }
2449
2450 /*
2451 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2452 */
2453 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2454 (linep[0] == '{' || linep[0] == '}'))
2455 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002456 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002458 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 }
2460
2461 if (comment_dir)
2462 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002463 // Note: comments do not nest, and we ignore quotes in them
2464 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (comment_dir == FORWARD)
2466 {
2467 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2468 {
2469 pos.col++;
2470 return &pos;
2471 }
2472 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002473 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 /*
2476 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002477 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
2479 if (pos.col == 0)
2480 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002481 else if (raw_string)
2482 {
2483 if (linep[pos.col - 1] == 'R'
2484 && linep[pos.col] == '"'
2485 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2486 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002487 // Possible start of raw string. Now that we have the
2488 // delimiter we can check if it ends before where we
2489 // started searching, or before the previously found
2490 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002491 if (!find_rawstring_end(linep, &pos,
2492 count > 0 ? &match_pos : &curwin->w_cursor))
2493 {
2494 count++;
2495 match_pos = pos;
2496 match_pos.col--;
2497 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002498 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002499 }
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 else if ( linep[pos.col - 1] == '/'
2502 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002503 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 && (int)pos.col < comment_col)
2505 {
2506 count++;
2507 match_pos = pos;
2508 match_pos.col--;
2509 }
2510 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2511 {
2512 if (count > 0)
2513 pos = match_pos;
2514 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2515 && (int)pos.col <= comment_col)
2516 pos.col -= 2;
2517 else if (ignore_cend)
2518 continue;
2519 else
2520 return NULL;
2521 return &pos;
2522 }
2523 }
2524 continue;
2525 }
2526
2527 /*
2528 * If smart matching ('cpoptions' does not contain '%'), braces inside
2529 * of quotes are ignored, but only if there is an even number of
2530 * quotes in the line.
2531 */
2532 if (cpo_match)
2533 do_quotes = 0;
2534 else if (do_quotes == -1)
2535 {
2536 /*
2537 * Count the number of quotes in the line, skipping \" and '"'.
2538 * Watch out for "\\".
2539 */
2540 at_start = do_quotes;
2541 for (ptr = linep; *ptr; ++ptr)
2542 {
2543 if (ptr == linep + pos.col + backwards)
2544 at_start = (do_quotes & 1);
2545 if (*ptr == '"'
2546 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2547 ++do_quotes;
2548 if (*ptr == '\\' && ptr[1] != NUL)
2549 ++ptr;
2550 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002551 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553 /*
2554 * If we find an uneven count, check current line and previous
2555 * one for a '\' at the end.
2556 */
2557 if (!do_quotes)
2558 {
2559 inquote = FALSE;
2560 if (ptr[-1] == '\\')
2561 {
2562 do_quotes = 1;
2563 if (start_in_quotes == MAYBE)
2564 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002565 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 inquote = TRUE;
2567 start_in_quotes = TRUE;
2568 }
2569 else if (backwards)
2570 inquote = TRUE;
2571 }
2572 if (pos.lnum > 1)
2573 {
2574 ptr = ml_get(pos.lnum - 1);
2575 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2576 {
2577 do_quotes = 1;
2578 if (start_in_quotes == MAYBE)
2579 {
2580 inquote = at_start;
2581 if (inquote)
2582 start_in_quotes = TRUE;
2583 }
2584 else if (!backwards)
2585 inquote = TRUE;
2586 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002587
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002588 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002589 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 }
2592 }
2593 if (start_in_quotes == MAYBE)
2594 start_in_quotes = FALSE;
2595
2596 /*
2597 * If 'smartmatch' is set:
2598 * Things inside quotes are ignored by setting 'inquote'. If we
2599 * find a quote without a preceding '\' invert 'inquote'. At the
2600 * end of a line not ending in '\' we reset 'inquote'.
2601 *
2602 * In lines with an uneven number of quotes (without preceding '\')
2603 * we do not know which part to ignore. Therefore we only set
2604 * inquote if the number of quotes in a line is even, unless this
2605 * line or the previous one ends in a '\'. Complicated, isn't it?
2606 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002607 c = PTR2CHAR(linep + pos.col);
2608 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002611 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2613 {
2614 inquote = FALSE;
2615 start_in_quotes = FALSE;
2616 }
2617 break;
2618
2619 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002620 // a quote that is preceded with an odd number of backslashes is
2621 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (do_quotes)
2623 {
2624 int col;
2625
2626 for (col = pos.col - 1; col >= 0; --col)
2627 if (linep[col] != '\\')
2628 break;
2629 if ((((int)pos.col - 1 - col) & 1) == 0)
2630 {
2631 inquote = !inquote;
2632 start_in_quotes = FALSE;
2633 }
2634 }
2635 break;
2636
2637 /*
2638 * If smart matching ('cpoptions' does not contain '%'):
2639 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2640 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2641 * skipped, there is never a brace in them.
2642 * Ignore this when finding matches for `'.
2643 */
2644 case '\'':
2645 if (!cpo_match && initc != '\'' && findc != '\'')
2646 {
2647 if (backwards)
2648 {
2649 if (pos.col > 1)
2650 {
2651 if (linep[pos.col - 2] == '\'')
2652 {
2653 pos.col -= 2;
2654 break;
2655 }
2656 else if (linep[pos.col - 2] == '\\' &&
2657 pos.col > 2 && linep[pos.col - 3] == '\'')
2658 {
2659 pos.col -= 3;
2660 break;
2661 }
2662 }
2663 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002664 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 {
2666 if (linep[pos.col + 1] == '\\' &&
2667 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2668 {
2669 pos.col += 3;
2670 break;
2671 }
2672 else if (linep[pos.col + 2] == '\'')
2673 {
2674 pos.col += 2;
2675 break;
2676 }
2677 }
2678 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002679 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 default:
2682#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002683 /*
2684 * For Lisp skip over backslashed (), {} and [].
2685 * (actually, we skip #\( et al)
2686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 if (curbuf->b_p_lisp
2688 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002689 && pos.col > 1
2690 && check_prevcol(linep, pos.col, '\\', NULL)
2691 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 break;
2693#endif
2694
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002695 // Check for match outside of quotes, and inside of
2696 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 if ((!inquote || start_in_quotes == TRUE)
2698 && (c == initc || c == findc))
2699 {
2700 int col, bslcnt = 0;
2701
2702 if (!cpo_bsl)
2703 {
2704 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2705 bslcnt++;
2706 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002707 // Only accept a match when 'M' is in 'cpo' or when escaping
2708 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2710 {
2711 if (c == initc)
2712 count++;
2713 else
2714 {
2715 if (count == 0)
2716 return &pos;
2717 count--;
2718 }
2719 }
2720 }
2721 }
2722 }
2723
2724 if (comment_dir == BACKWARD && count > 0)
2725 {
2726 pos = match_pos;
2727 return &pos;
2728 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002729 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730}
2731
2732/*
2733 * Check if line[] contains a / / comment.
2734 * Return MAXCOL if not, otherwise return the column.
2735 * TODO: skip strings.
2736 */
2737 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002738check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739{
2740 char_u *p;
2741
2742 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002743#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002744 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002745 if (curbuf->b_p_lisp)
2746 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002747 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002748 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002749 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002750
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002751 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002752 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002753 {
2754 if (*p == '"')
2755 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002756 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002757 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002758 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002759 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002760 }
2761 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002762 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002763 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002764 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002765 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002766 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002767 || (*(p - 1) != '\\' && *(p - 2) != '#')))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002768 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002769 ++p;
2770 }
2771 }
2772 else
2773 p = NULL;
2774 }
2775 else
2776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 while ((p = vim_strchr(p, '/')) != NULL)
2778 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002779 // accept a double /, unless it's preceded with * and followed by *,
2780 // because * / / * is an end and start of a C comment
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002781 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 break;
2783 ++p;
2784 }
2785
2786 if (p == NULL)
2787 return MAXCOL;
2788 return (int)(p - line);
2789}
2790
2791/*
2792 * Move cursor briefly to character matching the one under the cursor.
2793 * Used for Insert mode and "r" command.
2794 * Show the match only if it is visible on the screen.
2795 * If there isn't a match, then beep.
2796 */
2797 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002798showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002799 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
2801 pos_T *lpos, save_cursor;
2802 pos_T mpos;
2803 colnr_T vcol;
2804 long save_so;
2805 long save_siso;
2806#ifdef CURSOR_SHAPE
2807 int save_state;
2808#endif
2809 colnr_T save_dollar_vcol;
2810 char_u *p;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002811 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2812 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813
2814 /*
2815 * Only show match for chars in the 'matchpairs' option.
2816 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002817 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002818 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 {
2820#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002821 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002822 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002823#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002824 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002825 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826#ifdef FEAT_RIGHTLEFT
2827 && !(curwin->w_p_rl ^ p_ri)
2828#endif
2829 )
2830 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002831 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002832 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 return;
2834 }
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +02002835 if (*p == NUL)
2836 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002838 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar165bc692015-07-21 17:53:25 +02002839 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002840 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 {
2842 if (!curwin->w_p_wrap)
2843 getvcol(curwin, lpos, NULL, &vcol, NULL);
2844 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002845 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002847 mpos = *lpos; // save the pos, update_screen() may change it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002849 save_so = *so;
2850 save_siso = *siso;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002851 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2852 // stop displaying the "$".
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002853 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2854 dollar_vcol = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002855 ++curwin->w_virtcol; // do display ')' just before "$"
2856 update_screen(VALID); // show the new char first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857
2858 save_dollar_vcol = dollar_vcol;
2859#ifdef CURSOR_SHAPE
2860 save_state = State;
2861 State = SHOWMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002862 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002864 curwin->w_cursor = mpos; // move to matching char
2865 *so = 0; // don't use 'scrolloff' here
2866 *siso = 0; // don't use 'sidescrolloff' here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 showruler(FALSE);
2868 setcursor();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002869 cursor_on(); // make sure that the cursor is shown
Bram Moolenaara338adc2018-01-31 20:51:47 +01002870 out_flush_cursor(TRUE, FALSE);
2871
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002872 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2873 // which resets it if the matching position is in a previous line
2874 // and has a higher column number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 dollar_vcol = save_dollar_vcol;
2876
2877 /*
2878 * brief pause, unless 'm' is present in 'cpo' and a character is
2879 * available.
2880 */
2881 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002882 ui_delay(p_mat * 100L + 8, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 else if (!char_avail())
Bram Moolenaareda1da02019-11-17 17:06:33 +01002884 ui_delay(p_mat * 100L + 9, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002885 curwin->w_cursor = save_cursor; // restore cursor position
Bram Moolenaar375e3392019-01-31 18:26:10 +01002886 *so = save_so;
2887 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#ifdef CURSOR_SHAPE
2889 State = save_state;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002890 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891#endif
2892 }
2893 }
2894}
2895
2896/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002897 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002898 * If move is TRUE, check from the beginning of the buffer, else from position
2899 * "cur".
2900 * "direction" is FORWARD or BACKWARD.
2901 * Returns TRUE, FALSE or -1 for failure.
2902 */
2903 static int
2904is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2905{
2906 regmmatch_T regmatch;
2907 int nmatched = 0;
2908 int result = -1;
2909 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002910 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002911 int flag = 0;
2912
2913 if (pattern == NULL)
2914 pattern = spats[last_idx].pat;
2915
2916 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
2917 SEARCH_KEEP, &regmatch) == FAIL)
2918 return -1;
2919
2920 // init startcol correctly
2921 regmatch.startpos[0].col = -1;
2922 // move to match
2923 if (move)
2924 {
2925 CLEAR_POS(&pos);
2926 }
2927 else
2928 {
2929 pos = *cur;
2930 // accept a match at the cursor position
2931 flag = SEARCH_START;
2932 }
2933
2934 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2935 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2936 {
2937 // Zero-width pattern should match somewhere, then we can check if
2938 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002939 do
2940 {
2941 regmatch.startpos[0].col++;
2942 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
2943 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
2944 if (nmatched != 0)
2945 break;
Bram Moolenaar795aaa12020-10-02 20:36:01 +02002946 } while (regmatch.regprog != NULL
2947 && direction == FORWARD ? regmatch.startpos[0].col < pos.col
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002948 : regmatch.startpos[0].col > pos.col);
2949
Bram Moolenaar53989552019-12-23 22:59:18 +01002950 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002951 {
2952 result = (nmatched != 0
2953 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2954 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2955 }
2956 }
2957
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002958 vim_regfree(regmatch.regprog);
2959 return result;
2960}
2961
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002962
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002963/*
2964 * Find next search match under cursor, cursor at end.
2965 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002966 */
2967 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002968current_search(
2969 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002970 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002971{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002972 pos_T start_pos; // start position of the pattern match
2973 pos_T end_pos; // end position of the pattern match
2974 pos_T orig_pos; // position of the cursor at beginning
2975 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002976 int i;
2977 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002978 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002979 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002980 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002981 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002982 int zero_width;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002983 int skip_first_backward;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002984
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002985 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002986 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002987 dec_cursor();
2988
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02002989 // When searching forward and the cursor is at the start of the Visual
2990 // area, skip the first search backward, otherwise it doesn't move.
2991 skip_first_backward = forward && VIsual_active
2992 && LT_POS(curwin->w_cursor, VIsual);
2993
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002994 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002995 if (VIsual_active)
2996 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002997 if (forward)
2998 incl(&pos);
2999 else
3000 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003001 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003002
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003003 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003004 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02003005 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003006 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003007 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003008
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003009 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003010 * The trick is to first search backwards and then search forward again,
3011 * so that a match at the current cursor position will be correctly
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003012 * captured. When "forward" is false do it the other way around.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003013 */
3014 for (i = 0; i < 2; i++)
3015 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003016 if (forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003017 {
3018 if (i == 0 && skip_first_backward)
3019 continue;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003020 dir = i;
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003021 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003022 else
3023 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003024
3025 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003026 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003027 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003028 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02003029
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003030 // wrapping should not occur in the first round
3031 if (i == 0)
3032 p_ws = FALSE;
3033
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003034 result = searchit(curwin, curbuf, &pos, &end_pos,
3035 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003036 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003037 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003038
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01003039 p_ws = old_p_ws;
3040
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003041 // First search may fail, but then start searching from the
3042 // beginning of the file (cursor might be on the search match)
3043 // except when Visual mode is active, so that extending the visual
3044 // selection works.
3045 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003046 {
3047 curwin->w_cursor = orig_pos;
3048 if (VIsual_active)
3049 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003050 return FAIL;
3051 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003052 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003053 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003054 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003055 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003056 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003057 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003058 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003059 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003060 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003061 // try again from end of buffer
3062 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003063 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003064 pos.col = (colnr_T)STRLEN(
3065 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003066 }
3067 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003068 }
3069
3070 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003071
3072 if (!VIsual_active)
3073 VIsual = start_pos;
3074
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003075 // put the cursor after the match
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003076 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003077 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaarc07b7f72020-10-11 20:44:15 +02003078 {
3079 if (skip_first_backward)
3080 // put the cursor on the start of the match
3081 curwin->w_cursor = pos;
3082 else
3083 // put the cursor on last character of match
3084 dec_cursor();
3085 }
Bram Moolenaar28f224b2020-10-10 16:45:25 +02003086 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003087 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003088 VIsual_active = TRUE;
3089 VIsual_mode = 'v';
3090
Bram Moolenaarb7633612019-02-10 21:48:25 +01003091 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003092 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003093 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003094 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3095 inc_cursor();
3096 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3097 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003098 }
3099
3100#ifdef FEAT_FOLDING
3101 if (fdo_flags & FDO_SEARCH && KeyTyped)
3102 foldOpenCursor();
3103#endif
3104
3105 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003106 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003107#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003108 // Make sure the clipboard gets updated. Needed because start and
3109 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003110 clip_star.vmode = NUL;
3111#endif
3112 redraw_curbuf_later(INVERTED);
3113 showmode();
3114
3115 return OK;
3116}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
3119 || defined(PROTO)
3120/*
3121 * return TRUE if line 'lnum' is empty or has white chars only.
3122 */
3123 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003124linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125{
3126 char_u *p;
3127
3128 p = skipwhite(ml_get(lnum));
3129 return (*p == NUL);
3130}
3131#endif
3132
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003133/*
3134 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003135 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003136 */
3137 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003138cmdline_search_stat(
3139 int dirc,
3140 pos_T *pos,
3141 pos_T *cursor_pos,
3142 int show_top_bot_msg,
3143 char_u *msgbuf,
3144 int recompute,
3145 int maxcount,
3146 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003147{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003148 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003149
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003150 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3151 timeout);
3152 if (stat.cur > 0)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003153 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003154 char t[SEARCH_STAT_BUF_LEN];
Bram Moolenaare2ad8262019-05-24 13:22:22 +02003155 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003156
3157#ifdef FEAT_RIGHTLEFT
3158 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3159 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003160 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003161 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003162 else if (stat.cnt > maxcount && stat.cur > maxcount)
3163 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3164 maxcount, maxcount);
3165 else if (stat.cnt > maxcount)
3166 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3167 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003168 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003169 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3170 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003171 }
3172 else
3173#endif
3174 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003175 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003176 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003177 else if (stat.cnt > maxcount && stat.cur > maxcount)
3178 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3179 maxcount, maxcount);
3180 else if (stat.cnt > maxcount)
3181 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3182 stat.cur, maxcount);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003183 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003184 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3185 stat.cur, stat.cnt);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003186 }
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003187
3188 len = STRLEN(t);
Bram Moolenaardc6855a2019-05-18 19:26:29 +02003189 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003190 {
Bram Moolenaar16b58ae2019-09-06 20:40:21 +02003191 mch_memmove(t + 2, t, len);
3192 t[0] = 'W';
3193 t[1] = ' ';
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003194 len += 2;
3195 }
3196
3197 mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003198 if (dirc == '?' && stat.cur == maxcount + 1)
3199 stat.cur = -1;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003200
Bram Moolenaar984f0312019-05-24 13:11:47 +02003201 // keep the message even after redraw, but don't put in history
3202 msg_hist_off = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003203 give_warning(msgbuf, FALSE);
Bram Moolenaar984f0312019-05-24 13:11:47 +02003204 msg_hist_off = FALSE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003205 }
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003206}
3207
3208/*
3209 * Add the search count information to "stat".
3210 * "stat" must not be NULL.
3211 * When "recompute" is TRUE always recompute the numbers.
3212 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3213 * dirc == '/': find the next match
3214 * dirc == '?': find the previous match
3215 */
3216 static void
3217update_search_stat(
3218 int dirc,
3219 pos_T *pos,
3220 pos_T *cursor_pos,
3221 searchstat_T *stat,
3222 int recompute,
3223 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003224 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003225{
3226 int save_ws = p_ws;
3227 int wraparound = FALSE;
3228 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003229 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003230 static int cur = 0;
3231 static int cnt = 0;
3232 static int exact_match = FALSE;
3233 static int incomplete = 0;
3234 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3235 static int chgtick = 0;
3236 static char_u *lastpat = NULL;
3237 static buf_T *lbuf = NULL;
3238#ifdef FEAT_RELTIME
3239 proftime_T start;
3240#endif
3241
3242 vim_memset(stat, 0, sizeof(searchstat_T));
3243
3244 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3245 {
3246 stat->cur = cur;
3247 stat->cnt = cnt;
3248 stat->exact_match = exact_match;
3249 stat->incomplete = incomplete;
3250 stat->last_maxcount = last_maxcount;
3251 return;
3252 }
3253 last_maxcount = maxcount;
3254
3255 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3256 || (dirc == '/' && LT_POS(p, lastpos)));
3257
3258 // If anything relevant changed the count has to be recomputed.
3259 // MB_STRNICMP ignores case, but we should not ignore case.
3260 // Unfortunately, there is no MB_STRNICMP function.
3261 // XXX: above comment should be "no MB_STRCMP function" ?
3262 if (!(chgtick == CHANGEDTICK(curbuf)
3263 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3264 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3265 && EQUAL_POS(lastpos, *cursor_pos)
3266 && lbuf == curbuf) || wraparound || cur < 0
3267 || (maxcount > 0 && cur > maxcount) || recompute)
3268 {
3269 cur = 0;
3270 cnt = 0;
3271 exact_match = FALSE;
3272 incomplete = 0;
3273 CLEAR_POS(&lastpos);
3274 lbuf = curbuf;
3275 }
3276
3277 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
3278 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 0))
3279 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3280 else
3281 {
3282 int done_search = FALSE;
3283 pos_T endpos = {0, 0, 0};
3284
3285 p_ws = FALSE;
3286#ifdef FEAT_RELTIME
3287 if (timeout > 0)
3288 profile_setlimit(timeout, &start);
3289#endif
3290 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3291 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3292 {
3293 done_search = TRUE;
3294#ifdef FEAT_RELTIME
3295 // Stop after passing the time limit.
3296 if (timeout > 0 && profile_passed_limit(&start))
3297 {
3298 incomplete = 1;
3299 break;
3300 }
3301#endif
3302 cnt++;
3303 if (LTOREQ_POS(lastpos, p))
3304 {
3305 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003306 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003307 exact_match = TRUE;
3308 }
3309 fast_breakcheck();
3310 if (maxcount > 0 && cnt > maxcount)
3311 {
3312 incomplete = 2; // max count exceeded
3313 break;
3314 }
3315 }
3316 if (got_int)
3317 cur = -1; // abort
3318 if (done_search)
3319 {
3320 vim_free(lastpat);
3321 lastpat = vim_strsave(spats[last_idx].pat);
3322 chgtick = CHANGEDTICK(curbuf);
3323 lbuf = curbuf;
3324 lastpos = p;
3325 }
3326 }
3327 stat->cur = cur;
3328 stat->cnt = cnt;
3329 stat->exact_match = exact_match;
3330 stat->incomplete = incomplete;
3331 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003332 p_ws = save_ws;
3333}
3334
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#if defined(FEAT_FIND_ID) || defined(PROTO)
3336/*
3337 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01003338 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003341find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003342 char_u *ptr, // pointer to search pattern
3343 int dir UNUSED, // direction of expansion
3344 int len, // length of search pattern
3345 int whole, // match whole words only
3346 int skip_comments, // don't match inside comments
3347 int type, // Type of search; are we looking for a type?
3348 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003349 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003350 int action, // What to do when we find it
3351 linenr_T start_lnum, // first line to start searching
3352 linenr_T end_lnum) // last line for searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003354 SearchedFile *files; // Stack of included files
3355 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 int max_path_depth = 50;
3357 long match_count = 1;
3358
3359 char_u *pat;
3360 char_u *new_fname;
3361 char_u *curr_fname = curbuf->b_fname;
3362 char_u *prev_fname = NULL;
3363 linenr_T lnum;
3364 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003365 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 int old_files;
3367 int already_searched;
3368 char_u *file_line;
3369 char_u *line;
3370 char_u *p;
3371 char_u save_char;
3372 int define_matched;
3373 regmatch_T regmatch;
3374 regmatch_T incl_regmatch;
3375 regmatch_T def_regmatch;
3376 int matched = FALSE;
3377 int did_show = FALSE;
3378 int found = FALSE;
3379 int i;
3380 char_u *already = NULL;
3381 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003382 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003383#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 win_T *curwin_save = NULL;
3385#endif
3386
3387 regmatch.regprog = NULL;
3388 incl_regmatch.regprog = NULL;
3389 def_regmatch.regprog = NULL;
3390
3391 file_line = alloc(LSIZE);
3392 if (file_line == NULL)
3393 return;
3394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 if (type != CHECK_PATH && type != FIND_DEFINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003396 // when CONT_SOL is set compare "ptr" with the beginning of the line
3397 // is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003398 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
3400 pat = alloc(len + 5);
3401 if (pat == NULL)
3402 goto fpip_end;
3403 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003404 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003406 regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 vim_free(pat);
3408 if (regmatch.regprog == NULL)
3409 goto fpip_end;
3410 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003411 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3412 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003414 incl_regmatch.regprog = vim_regcomp(inc_opt,
3415 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (incl_regmatch.regprog == NULL)
3417 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003418 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 }
3420 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3421 {
3422 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003423 ? p_def : curbuf->b_p_def,
3424 magic_isset() ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (def_regmatch.regprog == NULL)
3426 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003427 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003429 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (files == NULL)
3431 goto fpip_end;
3432 old_files = max_path_depth;
3433 depth = depth_displayed = -1;
3434
3435 lnum = start_lnum;
3436 if (end_lnum > curbuf->b_ml.ml_line_count)
3437 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003438 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 lnum = end_lnum;
3440 line = ml_get(lnum);
3441
3442 for (;;)
3443 {
3444 if (incl_regmatch.regprog != NULL
3445 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3446 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003447 char_u *p_fname = (curr_fname == curbuf->b_fname)
3448 ? curbuf->b_ffname : curr_fname;
3449
3450 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003451 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003452 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003453 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003454 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3455 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003456 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003457 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003458 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 already_searched = FALSE;
3460 if (new_fname != NULL)
3461 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003462 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 for (i = 0;; i++)
3464 {
3465 if (i == depth + 1)
3466 i = old_files;
3467 if (i == max_path_depth)
3468 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003469 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3470 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
3472 if (type != CHECK_PATH &&
3473 action == ACTION_SHOW_ALL && files[i].matched)
3474 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003475 msg_putchar('\n'); // cursor below last one
3476 if (!got_int) // don't display if 'q'
3477 // typed at "--more--"
3478 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
3480 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003481 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 prev_fname = NULL;
3483 }
3484 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003485 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 already_searched = TRUE;
3487 break;
3488 }
3489 }
3490 }
3491
3492 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3493 || (new_fname == NULL && !already_searched)))
3494 {
3495 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003496 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 else
3498 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003499 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003500 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003502 msg_puts_title(_("not found "));
3503 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 }
3505 did_show = TRUE;
3506 while (depth_displayed < depth && !got_int)
3507 {
3508 ++depth_displayed;
3509 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003510 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003512 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003514 if (!got_int) // don't display if 'q' typed
3515 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003518 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (new_fname != NULL)
3520 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003521 // using "new_fname" is more reliable, e.g., when
3522 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003523 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 }
3525 else
3526 {
3527 /*
3528 * Isolate the file name.
3529 * Include the surrounding "" or <> if present.
3530 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003531 if (inc_opt != NULL
3532 && strstr((char *)inc_opt, "\\zs") != NULL)
3533 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003534 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003535 p = incl_regmatch.startp[0];
3536 i = (int)(incl_regmatch.endp[0]
3537 - incl_regmatch.startp[0]);
3538 }
3539 else
3540 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003541 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003542 for (p = incl_regmatch.endp[0];
3543 *p && !vim_isfilec(*p); p++)
3544 ;
3545 for (i = 0; vim_isfilec(p[i]); i++)
3546 ;
3547 }
3548
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (i == 0)
3550 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003551 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003553 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003555 // Avoid checking before the start of the line, can
3556 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003557 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559 if (p[-1] == '"' || p[-1] == '<')
3560 {
3561 --p;
3562 ++i;
3563 }
3564 if (p[i] == '"' || p[i] == '>')
3565 ++i;
3566 }
3567 save_char = p[i];
3568 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003569 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 p[i] = save_char;
3571 }
3572
3573 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3574 {
3575 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003576 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003578 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003581 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 }
3583
3584 if (new_fname != NULL)
3585 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003586 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (depth + 1 == old_files)
3588 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003589 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (bigger != NULL)
3591 {
3592 for (i = 0; i <= depth; i++)
3593 bigger[i] = files[i];
3594 for (i = depth + 1; i < old_files + max_path_depth; i++)
3595 {
3596 bigger[i].fp = NULL;
3597 bigger[i].name = NULL;
3598 bigger[i].lnum = 0;
3599 bigger[i].matched = FALSE;
3600 }
3601 for (i = old_files; i < max_path_depth; i++)
3602 bigger[i + max_path_depth] = files[i];
3603 old_files += max_path_depth;
3604 max_path_depth *= 2;
3605 vim_free(files);
3606 files = bigger;
3607 }
3608 }
3609 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3610 == NULL)
3611 vim_free(new_fname);
3612 else
3613 {
3614 if (++depth == old_files)
3615 {
3616 /*
3617 * lalloc() for 'bigger' must have failed above. We
3618 * will forget one of our already visited files now.
3619 */
3620 vim_free(files[old_files].name);
3621 ++old_files;
3622 }
3623 files[depth].name = curr_fname = new_fname;
3624 files[depth].lnum = 0;
3625 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (action == ACTION_EXPAND)
3627 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003628 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003629 vim_snprintf((char*)IObuff, IOSIZE,
3630 _("Scanning included file: %s"),
3631 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003632 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003634 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003635 {
3636 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003637 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003638 (char *)new_fname);
3639 verbose_leave();
3640 }
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643 }
3644 }
3645 else
3646 {
3647 /*
3648 * Check if the line is a define (type == FIND_DEFINE)
3649 */
3650 p = line;
3651search_line:
3652 define_matched = FALSE;
3653 if (def_regmatch.regprog != NULL
3654 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3655 {
3656 /*
3657 * Pattern must be first identifier after 'define', so skip
3658 * to that position before checking for match of pattern. Also
3659 * don't let it match beyond the end of this identifier.
3660 */
3661 p = def_regmatch.endp[0];
3662 while (*p && !vim_iswordc(*p))
3663 p++;
3664 define_matched = TRUE;
3665 }
3666
3667 /*
3668 * Look for a match. Don't do this if we are looking for a
3669 * define and this line didn't match define_prog above.
3670 */
3671 if (def_regmatch.regprog == NULL || define_matched)
3672 {
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003673 if (define_matched || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003675 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 startp = skipwhite(p);
3677 if (p_ic)
3678 matched = !MB_STRNICMP(startp, ptr, len);
3679 else
3680 matched = !STRNCMP(startp, ptr, len);
3681 if (matched && define_matched && whole
3682 && vim_iswordc(startp[len]))
3683 matched = FALSE;
3684 }
3685 else if (regmatch.regprog != NULL
3686 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3687 {
3688 matched = TRUE;
3689 startp = regmatch.startp[0];
3690 /*
3691 * Check if the line is not a comment line (unless we are
3692 * looking for a define). A line starting with "# define"
3693 * is not considered to be a comment line.
3694 */
3695 if (!define_matched && skip_comments)
3696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if ((*line != '#' ||
3698 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003699 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 matched = FALSE;
3701
3702 /*
3703 * Also check for a "/ *" or "/ /" before the match.
3704 * Skips lines like "int backwards; / * normal index
3705 * * /" when looking for "normal".
3706 * Note: Doesn't skip "/ *" in comments.
3707 */
3708 p = skipwhite(line);
3709 if (matched
3710 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 for (p = line; *p && p < startp; ++p)
3712 {
3713 if (matched
3714 && p[0] == '/'
3715 && (p[1] == '*' || p[1] == '/'))
3716 {
3717 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003718 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (p[1] == '/')
3720 break;
3721 ++p;
3722 }
3723 else if (!matched && p[0] == '*' && p[1] == '/')
3724 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003725 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 matched = TRUE;
3727 ++p;
3728 }
3729 }
3730 }
3731 }
3732 }
3733 }
3734 if (matched)
3735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (action == ACTION_EXPAND)
3737 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003738 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 int add_r;
3740 char_u *aux;
3741
3742 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3743 break;
3744 found = TRUE;
3745 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003746 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003748 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (vim_iswordp(p))
3750 goto exit_matched;
3751 p = find_word_start(p);
3752 }
3753 p = find_word_end(p);
3754 i = (int)(p - aux);
3755
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003756 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003758 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003760
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003761 // Get the next line: when "depth" < 0 from the current
3762 // buffer, otherwise from the included file. Jump to
3763 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003764 if (depth < 0)
3765 {
3766 if (lnum >= end_lnum)
3767 goto exit_matched;
3768 line = ml_get(++lnum);
3769 }
3770 else if (vim_fgets(line = file_line,
3771 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 goto exit_matched;
3773
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003774 // we read a line, set "already" to check this "line" later
3775 // if depth >= 0 we'll increase files[depth].lnum far
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003776 // below -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 already = aux = p = skipwhite(line);
3778 p = find_word_start(p);
3779 p = find_word_end(p);
3780 if (p > aux)
3781 {
3782 if (*aux != ')' && IObuff[i-1] != TAB)
3783 {
3784 if (IObuff[i-1] != ' ')
3785 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003786 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (p_js
3788 && (IObuff[i-2] == '.'
3789 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3790 && (IObuff[i-2] == '?'
3791 || IObuff[i-2] == '!'))))
3792 IObuff[i++] = ' ';
3793 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003794 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (p - aux >= IOSIZE - i)
3796 p = aux + IOSIZE - i - 1;
3797 STRNCPY(IObuff + i, aux, p - aux);
3798 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003799 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801 IObuff[i] = NUL;
3802 aux = IObuff;
3803
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003804 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 goto exit_matched;
3806 }
3807
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003808 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003810 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003812 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003814 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 break;
3816 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003817 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
3819 found = TRUE;
3820 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003821 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (curr_fname != prev_fname)
3823 {
3824 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003825 msg_putchar('\n'); // cursor below last one
3826 if (!got_int) // don't display if 'q' typed
3827 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 msg_home_replace_hl(curr_fname);
3829 prev_fname = curr_fname;
3830 }
3831 did_show = TRUE;
3832 if (!got_int)
3833 show_pat_in_path(line, type, TRUE, action,
3834 (depth == -1) ? NULL : files[depth].fp,
3835 (depth == -1) ? &lnum : &files[depth].lnum,
3836 match_count++);
3837
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003838 // Set matched flag for this file and all the ones that
3839 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 for (i = 0; i <= depth; ++i)
3841 files[i].matched = TRUE;
3842 }
3843 else if (--count <= 0)
3844 {
3845 found = TRUE;
3846 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003847#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 && g_do_tagpreview == 0
3849#endif
3850 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003851 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 else if (action == ACTION_SHOW)
3853 {
3854 show_pat_in_path(line, type, did_show, action,
3855 (depth == -1) ? NULL : files[depth].fp,
3856 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3857 did_show = TRUE;
3858 }
3859 else
3860 {
3861#ifdef FEAT_GUI
3862 need_mouse_correct = TRUE;
3863#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003864#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003865 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (g_do_tagpreview != 0)
3867 {
3868 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003869 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871#endif
3872 if (action == ACTION_SPLIT)
3873 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003876 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
3878 if (depth == -1)
3879 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003880 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003881#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (g_do_tagpreview != 0)
3883 {
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003884 if (!win_valid(curwin_save))
3885 break;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003886 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003887 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003888 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003889 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
3891 else
3892#endif
3893 setpcmark();
3894 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003895 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 }
3897 else
3898 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003899 if (!GETFILE_SUCCESS(getfile(
3900 0, files[depth].name, NULL, TRUE,
3901 files[depth].lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003902 break; // failed to jump to file
3903 // autocommands may have changed the lnum, we don't
3904 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 curwin->w_cursor.lnum = files[depth].lnum;
3906 }
3907 }
3908 if (action != ACTION_SHOW)
3909 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003910 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 curwin->w_set_curswant = TRUE;
3912 }
3913
Bram Moolenaar4033c552017-09-16 20:54:51 +02003914#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003916 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003918 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 validate_cursor();
3920 redraw_later(VALID);
3921 win_enter(curwin_save, TRUE);
3922 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003923# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003924 else if (WIN_IS_POPUP(curwin))
3925 // can't keep focus in popup window
3926 win_enter(firstwin, TRUE);
3927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928#endif
3929 break;
3930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003933 // look for other matches in the rest of the line if we
3934 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003937 && !(compl_cont_status & CONT_SOL)
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003938 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003939 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 goto search_line;
3941 }
3942 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003944 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003945 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 break;
3947
3948 /*
3949 * Read the next line. When reading an included file and encountering
3950 * end-of-file, close the file and continue in the file that included
3951 * it.
3952 */
3953 while (depth >= 0 && !already
3954 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3955 {
3956 fclose(files[depth].fp);
3957 --old_files;
3958 files[old_files].name = files[depth].name;
3959 files[old_files].matched = files[depth].matched;
3960 --depth;
3961 curr_fname = (depth == -1) ? curbuf->b_fname
3962 : files[depth].name;
3963 if (depth < depth_displayed)
3964 depth_displayed = depth;
3965 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003966 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003969 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003970 i = (int)STRLEN(line);
3971 if (i > 0 && line[i - 1] == '\n')
3972 line[--i] = NUL;
3973 if (i > 0 && line[i - 1] == '\r')
3974 line[--i] = NUL;
3975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 else if (!already)
3977 {
3978 if (++lnum > end_lnum)
3979 break;
3980 line = ml_get(lnum);
3981 }
3982 already = NULL;
3983 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003984 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003986 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 for (i = 0; i <= depth; i++)
3988 {
3989 fclose(files[i].fp);
3990 vim_free(files[i].name);
3991 }
3992 for (i = old_files; i < max_path_depth; i++)
3993 vim_free(files[i].name);
3994 vim_free(files);
3995
3996 if (type == CHECK_PATH)
3997 {
3998 if (!did_show)
3999 {
4000 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01004001 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004003 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02004006 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01004008 if (got_int || ins_compl_interrupted())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004009 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004011 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004013 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4016 msg_end();
4017
4018fpip_end:
4019 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02004020 vim_regfree(regmatch.regprog);
4021 vim_regfree(incl_regmatch.regprog);
4022 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023}
4024
4025 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004026show_pat_in_path(
4027 char_u *line,
4028 int type,
4029 int did_show,
4030 int action,
4031 FILE *fp,
4032 linenr_T *lnum,
4033 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034{
4035 char_u *p;
4036
4037 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004038 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00004039 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004040 gotocmdline(TRUE); // cursor at status line
4041 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 return;
4043 for (;;)
4044 {
4045 p = line + STRLEN(line) - 1;
4046 if (fp != NULL)
4047 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004048 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 if (p >= line && *p == '\n')
4050 --p;
4051 if (p >= line && *p == '\r')
4052 --p;
4053 *(p + 1) = NUL;
4054 }
4055 if (action == ACTION_SHOW_ALL)
4056 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004057 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004058 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004059 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4060 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004061 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4062 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004064 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004065 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004067 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4069 break;
4070
4071 if (fp != NULL)
4072 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004073 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 break;
4075 ++*lnum;
4076 }
4077 else
4078 {
4079 if (++*lnum > curbuf->b_ml.ml_line_count)
4080 break;
4081 line = ml_get(*lnum);
4082 }
4083 msg_putchar('\n');
4084 }
4085}
4086#endif
4087
4088#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004089/*
4090 * Return the last used search pattern at "idx".
4091 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004092 spat_T *
4093get_spat(int idx)
4094{
4095 return &spats[idx];
4096}
4097
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004098/*
4099 * Return the last used search pattern index.
4100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004102get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004104 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004107
4108#ifdef FEAT_EVAL
4109/*
4110 * "searchcount()" function
4111 */
4112 void
4113f_searchcount(typval_T *argvars, typval_T *rettv)
4114{
4115 pos_T pos = curwin->w_cursor;
4116 char_u *pattern = NULL;
4117 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4118 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar4140c4f2020-09-05 23:16:00 +02004119 int recompute = TRUE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004120 searchstat_T stat;
4121
4122 if (rettv_dict_alloc(rettv) == FAIL)
4123 return;
4124
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004125 if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL)
4126 return;
4127
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004128 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4129 recompute = TRUE;
4130
4131 if (argvars[0].v_type != VAR_UNKNOWN)
4132 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004133 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004134 dictitem_T *di;
4135 listitem_T *li;
4136 int error = FALSE;
4137
Bram Moolenaar14681622020-06-03 22:57:39 +02004138 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
4139 {
4140 emsg(_(e_dictreq));
4141 return;
4142 }
4143 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004144 di = dict_find(dict, (char_u *)"timeout", -1);
4145 if (di != NULL)
4146 {
4147 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4148 if (error)
4149 return;
4150 }
4151 di = dict_find(dict, (char_u *)"maxcount", -1);
4152 if (di != NULL)
4153 {
4154 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4155 if (error)
4156 return;
4157 }
Bram Moolenaar597aaac2020-09-05 21:21:16 +02004158 recompute = dict_get_bool(dict, (char_u *)"recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004159 di = dict_find(dict, (char_u *)"pattern", -1);
4160 if (di != NULL)
4161 {
4162 pattern = tv_get_string_chk(&di->di_tv);
4163 if (pattern == NULL)
4164 return;
4165 }
4166 di = dict_find(dict, (char_u *)"pos", -1);
4167 if (di != NULL)
4168 {
4169 if (di->di_tv.v_type != VAR_LIST)
4170 {
4171 semsg(_(e_invarg2), "pos");
4172 return;
4173 }
4174 if (list_len(di->di_tv.vval.v_list) != 3)
4175 {
4176 semsg(_(e_invarg2), "List format should be [lnum, col, off]");
4177 return;
4178 }
4179 li = list_find(di->di_tv.vval.v_list, 0L);
4180 if (li != NULL)
4181 {
4182 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4183 if (error)
4184 return;
4185 }
4186 li = list_find(di->di_tv.vval.v_list, 1L);
4187 if (li != NULL)
4188 {
4189 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
4190 if (error)
4191 return;
4192 }
4193 li = list_find(di->di_tv.vval.v_list, 2L);
4194 if (li != NULL)
4195 {
4196 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
4197 if (error)
4198 return;
4199 }
4200 }
4201 }
4202
4203 save_last_search_pattern();
4204 if (pattern != NULL)
4205 {
4206 if (*pattern == NUL)
4207 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004208 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004209 spats[last_idx].pat = vim_strsave(pattern);
4210 }
4211 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4212 goto the_end; // the previous pattern was never defined
4213
4214 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4215
4216 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4217 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4218 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4219 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4220 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4221
4222the_end:
4223 restore_last_search_pattern();
4224}
Bram Moolenaar635414d2020-09-11 22:25:15 +02004225
4226/*
4227 * Fuzzy string matching
4228 *
4229 * Ported from the lib_fts library authored by Forrest Smith.
4230 * https://github.com/forrestthewoods/lib_fts/tree/master/code
4231 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004232 * The following blog describes the fuzzy matching algorithm:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004233 * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/
4234 *
4235 * Each matching string is assigned a score. The following factors are checked:
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004236 * - Matched letter
4237 * - Unmatched letter
4238 * - Consecutively matched letters
4239 * - Proximity to start
4240 * - Letter following a separator (space, underscore)
4241 * - Uppercase letter following lowercase (aka CamelCase)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004242 *
4243 * Matched letters are good. Unmatched letters are bad. Matching near the start
4244 * is good. Matching the first letter in the middle of a phrase is good.
4245 * Matching the uppercase letters in camel case entries is good.
4246 *
4247 * The score assigned for each factor is explained below.
4248 * File paths are different from file names. File extensions may be ignorable.
4249 * Single words care about consecutive matches but not separators or camel
4250 * case.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004251 * Score starts at 100
Bram Moolenaar635414d2020-09-11 22:25:15 +02004252 * Matched letter: +0 points
4253 * Unmatched letter: -1 point
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004254 * Consecutive match bonus: +15 points
4255 * First letter bonus: +15 points
4256 * Separator bonus: +30 points
4257 * Camel case bonus: +30 points
4258 * Unmatched leading letter: -5 points (max: -15)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004259 *
4260 * There is some nuance to this. Scores don’t have an intrinsic meaning. The
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004261 * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a
Bram Moolenaar635414d2020-09-11 22:25:15 +02004262 * lower minimum score due to unmatched letter penalty. Longer search patterns
4263 * have a higher maximum score due to match bonuses.
4264 *
4265 * Separator and camel case bonus is worth a LOT. Consecutive matches are worth
4266 * quite a bit.
4267 *
4268 * There is a penalty if you DON’T match the first three letters. Which
4269 * effectively rewards matching near the start. However there’s no difference
4270 * in matching between the middle and end.
4271 *
4272 * There is not an explicit bonus for an exact match. Unmatched letters receive
4273 * a penalty. So shorter strings and closer matches are worth more.
4274 */
4275typedef struct
4276{
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004277 int idx; // used for stable sort
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004278 listitem_T *item;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004279 int score;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004280 list_T *lmatchpos;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004281} fuzzyItem_T;
4282
Bram Moolenaare9f9f162020-10-20 19:01:30 +02004283// bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that
4284// matching a whole word is preferred.
4285#define SEQUENTIAL_BONUS 40
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004286// bonus if match occurs after a path separator
4287#define PATH_SEPARATOR_BONUS 30
4288// bonus if match occurs after a word separator
4289#define WORD_SEPARATOR_BONUS 25
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004290// bonus if match is uppercase and prev is lower
4291#define CAMEL_BONUS 30
4292// bonus if the first letter is matched
4293#define FIRST_LETTER_BONUS 15
4294// penalty applied for every letter in str before the first match
4295#define LEADING_LETTER_PENALTY -5
4296// maximum penalty for leading letters
4297#define MAX_LEADING_LETTER_PENALTY -15
4298// penalty for every letter that doesn't match
4299#define UNMATCHED_LETTER_PENALTY -1
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004300// penalty for gap in matching positions (-2 * k)
4301#define GAP_PENALTY -2
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004302// Score for a string that doesn't fuzzy match the pattern
4303#define SCORE_NONE -9999
4304
4305#define FUZZY_MATCH_RECURSION_LIMIT 10
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004306
4307/*
4308 * Compute a score for a fuzzy matched string. The matching character locations
4309 * are in 'matches'.
4310 */
4311 static int
4312fuzzy_match_compute_score(
4313 char_u *str,
4314 int strSz,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004315 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004316 int numMatches)
4317{
4318 int score;
4319 int penalty;
4320 int unmatched;
4321 int i;
4322 char_u *p = str;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004323 int_u sidx = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004324
4325 // Initialize score
4326 score = 100;
4327
4328 // Apply leading letter penalty
4329 penalty = LEADING_LETTER_PENALTY * matches[0];
4330 if (penalty < MAX_LEADING_LETTER_PENALTY)
4331 penalty = MAX_LEADING_LETTER_PENALTY;
4332 score += penalty;
4333
4334 // Apply unmatched penalty
4335 unmatched = strSz - numMatches;
4336 score += UNMATCHED_LETTER_PENALTY * unmatched;
4337
4338 // Apply ordering bonuses
4339 for (i = 0; i < numMatches; ++i)
4340 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004341 int_u currIdx = matches[i];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004342
4343 if (i > 0)
4344 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004345 int_u prevIdx = matches[i - 1];
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004346
4347 // Sequential
4348 if (currIdx == (prevIdx + 1))
4349 score += SEQUENTIAL_BONUS;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004350 else
4351 score += GAP_PENALTY * (currIdx - prevIdx);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004352 }
4353
4354 // Check for bonuses based on neighbor character value
4355 if (currIdx > 0)
4356 {
4357 // Camel case
Bram Moolenaarc53e9c52020-09-22 22:08:32 +02004358 int neighbor = ' ';
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004359 int curr;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004360
4361 if (has_mbyte)
4362 {
4363 while (sidx < currIdx)
4364 {
4365 neighbor = (*mb_ptr2char)(p);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004366 MB_PTR_ADV(p);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004367 sidx++;
4368 }
4369 curr = (*mb_ptr2char)(p);
4370 }
4371 else
4372 {
4373 neighbor = str[currIdx - 1];
4374 curr = str[currIdx];
4375 }
4376
4377 if (vim_islower(neighbor) && vim_isupper(curr))
4378 score += CAMEL_BONUS;
4379
Bram Moolenaardcdd42a2020-10-29 18:58:01 +01004380 // Bonus if the match follows a separator character
4381 if (neighbor == '/' || neighbor == '\\')
4382 score += PATH_SEPARATOR_BONUS;
4383 else if (neighbor == ' ' || neighbor == '_')
4384 score += WORD_SEPARATOR_BONUS;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004385 }
4386 else
4387 {
4388 // First letter
4389 score += FIRST_LETTER_BONUS;
4390 }
4391 }
4392 return score;
4393}
4394
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004395/*
4396 * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'.
4397 * Return the number of matching characters.
4398 */
Bram Moolenaar635414d2020-09-11 22:25:15 +02004399 static int
4400fuzzy_match_recursive(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004401 char_u *fuzpat,
4402 char_u *str,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004403 int_u strIdx,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004404 int *outScore,
4405 char_u *strBegin,
4406 int strLen,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004407 int_u *srcMatches,
4408 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004409 int maxMatches,
4410 int nextMatch,
4411 int *recursionCount)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004412{
4413 // Recursion params
4414 int recursiveMatch = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004415 int_u bestRecursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004416 int bestRecursiveScore = 0;
4417 int first_match;
4418 int matched;
4419
4420 // Count recursions
4421 ++*recursionCount;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004422 if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004423 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004424
4425 // Detect end of strings
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004426 if (*fuzpat == NUL || *str == NUL)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004427 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004428
4429 // Loop through fuzpat and str looking for a match
4430 first_match = TRUE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004431 while (*fuzpat != NUL && *str != NUL)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004432 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004433 int c1;
4434 int c2;
4435
4436 c1 = PTR2CHAR(fuzpat);
4437 c2 = PTR2CHAR(str);
4438
Bram Moolenaar635414d2020-09-11 22:25:15 +02004439 // Found match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004440 if (vim_tolower(c1) == vim_tolower(c2))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004441 {
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004442 int_u recursiveMatches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004443 int recursiveScore = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004444 char_u *next_char;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004445
4446 // Supplied matches buffer was too short
4447 if (nextMatch >= maxMatches)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004448 return 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004449
4450 // "Copy-on-Write" srcMatches into matches
4451 if (first_match && srcMatches)
4452 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004453 memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004454 first_match = FALSE;
4455 }
4456
4457 // Recursive call that "skips" this match
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004458 if (has_mbyte)
4459 next_char = str + (*mb_ptr2len)(str);
4460 else
4461 next_char = str + 1;
4462 if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1,
4463 &recursiveScore, strBegin, strLen, matches,
4464 recursiveMatches,
K.Takataeeec2542021-06-02 13:28:16 +02004465 ARRAY_LENGTH(recursiveMatches),
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004466 nextMatch, recursionCount))
Bram Moolenaar635414d2020-09-11 22:25:15 +02004467 {
4468 // Pick best recursive score
4469 if (!recursiveMatch || recursiveScore > bestRecursiveScore)
4470 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004471 memcpy(bestRecursiveMatches, recursiveMatches,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004472 MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004473 bestRecursiveScore = recursiveScore;
4474 }
4475 recursiveMatch = TRUE;
4476 }
4477
4478 // Advance
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004479 matches[nextMatch++] = strIdx;
4480 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004481 MB_PTR_ADV(fuzpat);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004482 else
4483 ++fuzpat;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004484 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004485 if (has_mbyte)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004486 MB_PTR_ADV(str);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004487 else
4488 ++str;
4489 strIdx++;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004490 }
4491
4492 // Determine if full fuzpat was matched
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004493 matched = *fuzpat == NUL ? TRUE : FALSE;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004494
4495 // Calculate score
4496 if (matched)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004497 *outScore = fuzzy_match_compute_score(strBegin, strLen, matches,
4498 nextMatch);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004499
4500 // Return best result
4501 if (recursiveMatch && (!matched || bestRecursiveScore > *outScore))
4502 {
4503 // Recursive score is better than "this"
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004504 memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0]));
Bram Moolenaar635414d2020-09-11 22:25:15 +02004505 *outScore = bestRecursiveScore;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004506 return nextMatch;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004507 }
4508 else if (matched)
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004509 return nextMatch; // "this" score is better than recursive
Bram Moolenaar635414d2020-09-11 22:25:15 +02004510
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004511 return 0; // no match
Bram Moolenaar635414d2020-09-11 22:25:15 +02004512}
4513
4514/*
4515 * fuzzy_match()
4516 *
4517 * Performs exhaustive search via recursion to find all possible matches and
4518 * match with highest score.
4519 * Scores values have no intrinsic meaning. Possible score range is not
4520 * normalized and varies with pattern.
4521 * Recursion is limited internally (default=10) to prevent degenerate cases
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004522 * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004523 * Uses char_u for match indices. Therefore patterns are limited to
4524 * MAX_FUZZY_MATCHES characters.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004525 *
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004526 * Returns TRUE if 'pat_arg' matches 'str'. Also returns the match score in
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004527 * 'outScore' and the matching character positions in 'matches'.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004528 */
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004529 int
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004530fuzzy_match(
4531 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004532 char_u *pat_arg,
4533 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004534 int *outScore,
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004535 int_u *matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004536 int maxMatches)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004537{
Bram Moolenaar635414d2020-09-11 22:25:15 +02004538 int recursionCount = 0;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004539 int len = MB_CHARLEN(str);
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004540 char_u *save_pat;
4541 char_u *pat;
4542 char_u *p;
4543 int complete = FALSE;
4544 int score = 0;
4545 int numMatches = 0;
4546 int matchCount;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004547
4548 *outScore = 0;
4549
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004550 save_pat = vim_strsave(pat_arg);
4551 if (save_pat == NULL)
4552 return FALSE;
4553 pat = save_pat;
4554 p = pat;
4555
4556 // Try matching each word in 'pat_arg' in 'str'
4557 while (TRUE)
4558 {
4559 if (matchseq)
4560 complete = TRUE;
4561 else
4562 {
4563 // Extract one word from the pattern (separated by space)
4564 p = skipwhite(p);
4565 if (*p == NUL)
4566 break;
4567 pat = p;
4568 while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p)))
4569 {
4570 if (has_mbyte)
4571 MB_PTR_ADV(p);
4572 else
4573 ++p;
4574 }
4575 if (*p == NUL) // processed all the words
4576 complete = TRUE;
4577 *p = NUL;
4578 }
4579
4580 score = 0;
4581 recursionCount = 0;
4582 matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL,
4583 matches + numMatches, maxMatches - numMatches,
4584 0, &recursionCount);
4585 if (matchCount == 0)
4586 {
4587 numMatches = 0;
4588 break;
4589 }
4590
4591 // Accumulate the match score and the number of matches
4592 *outScore += score;
4593 numMatches += matchCount;
4594
4595 if (complete)
4596 break;
4597
4598 // try matching the next word
4599 ++p;
4600 }
4601
4602 vim_free(save_pat);
4603 return numMatches != 0;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004604}
4605
4606/*
4607 * Sort the fuzzy matches in the descending order of the match score.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004608 * For items with same score, retain the order using the index (stable sort)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004609 */
4610 static int
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004611fuzzy_match_item_compare(const void *s1, const void *s2)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004612{
4613 int v1 = ((fuzzyItem_T *)s1)->score;
4614 int v2 = ((fuzzyItem_T *)s2)->score;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004615 int idx1 = ((fuzzyItem_T *)s1)->idx;
4616 int idx2 = ((fuzzyItem_T *)s2)->idx;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004617
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004618 return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004619}
4620
4621/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004622 * Fuzzy search the string 'str' in a list of 'items' and return the matching
4623 * strings in 'fmatchlist'.
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004624 * If 'matchseq' is TRUE, then for multi-word search strings, match all the
4625 * words in sequence.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004626 * If 'items' is a list of strings, then search for 'str' in the list.
4627 * If 'items' is a list of dicts, then either use 'key' to lookup the string
4628 * for each item or use 'item_cb' Funcref function to get the string.
4629 * If 'retmatchpos' is TRUE, then return a list of positions where 'str'
4630 * matches for each item.
Bram Moolenaar635414d2020-09-11 22:25:15 +02004631 */
4632 static void
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004633fuzzy_match_in_list(
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004634 list_T *items,
4635 char_u *str,
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004636 int matchseq,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004637 char_u *key,
4638 callback_T *item_cb,
4639 int retmatchpos,
4640 list_T *fmatchlist)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004641{
4642 long len;
4643 fuzzyItem_T *ptrs;
4644 listitem_T *li;
4645 long i = 0;
4646 int found_match = FALSE;
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004647 int_u matches[MAX_FUZZY_MATCHES];
Bram Moolenaar635414d2020-09-11 22:25:15 +02004648
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004649 len = list_len(items);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004650 if (len == 0)
4651 return;
4652
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004653 ptrs = ALLOC_CLEAR_MULT(fuzzyItem_T, len);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004654 if (ptrs == NULL)
4655 return;
4656
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004657 // For all the string items in items, get the fuzzy matching score
4658 FOR_ALL_LIST_ITEMS(items, li)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004659 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004660 int score;
4661 char_u *itemstr;
4662 typval_T rettv;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004663
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004664 ptrs[i].idx = i;
Bram Moolenaar635414d2020-09-11 22:25:15 +02004665 ptrs[i].item = li;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004666 ptrs[i].score = SCORE_NONE;
4667 itemstr = NULL;
4668 rettv.v_type = VAR_UNKNOWN;
4669 if (li->li_tv.v_type == VAR_STRING) // list of strings
4670 itemstr = li->li_tv.vval.v_string;
4671 else if (li->li_tv.v_type == VAR_DICT &&
4672 (key != NULL || item_cb->cb_name != NULL))
4673 {
4674 // For a dict, either use the specified key to lookup the string or
4675 // use the specified callback function to get the string.
4676 if (key != NULL)
4677 itemstr = dict_get_string(li->li_tv.vval.v_dict, key, FALSE);
4678 else
Bram Moolenaar635414d2020-09-11 22:25:15 +02004679 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004680 typval_T argv[2];
4681
4682 // Invoke the supplied callback (if any) to get the dict item
4683 li->li_tv.vval.v_dict->dv_refcount++;
4684 argv[0].v_type = VAR_DICT;
4685 argv[0].vval.v_dict = li->li_tv.vval.v_dict;
4686 argv[1].v_type = VAR_UNKNOWN;
4687 if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL)
4688 {
4689 if (rettv.v_type == VAR_STRING)
4690 itemstr = rettv.vval.v_string;
4691 }
4692 dict_unref(li->li_tv.vval.v_dict);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004693 }
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004694 }
4695
4696 if (itemstr != NULL
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004697 && fuzzy_match(itemstr, str, matchseq, &score, matches,
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004698 sizeof(matches) / sizeof(matches[0])))
4699 {
4700 // Copy the list of matching positions in itemstr to a list, if
4701 // 'retmatchpos' is set.
4702 if (retmatchpos)
4703 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004704 int j = 0;
4705 char_u *p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004706
4707 ptrs[i].lmatchpos = list_alloc();
4708 if (ptrs[i].lmatchpos == NULL)
4709 goto done;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004710
4711 p = str;
4712 while (*p != NUL)
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004713 {
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004714 if (!VIM_ISWHITE(PTR2CHAR(p)))
4715 {
4716 if (list_append_number(ptrs[i].lmatchpos,
4717 matches[j]) == FAIL)
4718 goto done;
4719 j++;
4720 }
4721 if (has_mbyte)
4722 MB_PTR_ADV(p);
4723 else
4724 ++p;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004725 }
4726 }
4727 ptrs[i].score = score;
4728 found_match = TRUE;
4729 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004730 ++i;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004731 clear_tv(&rettv);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004732 }
4733
4734 if (found_match)
4735 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004736 list_T *l;
4737
Bram Moolenaar635414d2020-09-11 22:25:15 +02004738 // Sort the list by the descending order of the match score
4739 qsort((void *)ptrs, (size_t)len, sizeof(fuzzyItem_T),
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004740 fuzzy_match_item_compare);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004741
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004742 // For matchfuzzy(), return a list of matched strings.
4743 // ['str1', 'str2', 'str3']
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004744 // For matchfuzzypos(), return a list with three items.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004745 // The first item is a list of matched strings. The second item
4746 // is a list of lists where each list item is a list of matched
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004747 // character positions. The third item is a list of matching scores.
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004748 // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]]
4749 if (retmatchpos)
4750 {
4751 li = list_find(fmatchlist, 0);
4752 if (li == NULL || li->li_tv.vval.v_list == NULL)
4753 goto done;
4754 l = li->li_tv.vval.v_list;
4755 }
4756 else
4757 l = fmatchlist;
4758
4759 // Copy the matching strings with a valid score to the return list
Bram Moolenaar635414d2020-09-11 22:25:15 +02004760 for (i = 0; i < len; i++)
4761 {
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004762 if (ptrs[i].score == SCORE_NONE)
Bram Moolenaar635414d2020-09-11 22:25:15 +02004763 break;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004764 list_append_tv(l, &ptrs[i].item->li_tv);
4765 }
4766
4767 // next copy the list of matching positions
4768 if (retmatchpos)
4769 {
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004770 li = list_find(fmatchlist, -2);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004771 if (li == NULL || li->li_tv.vval.v_list == NULL)
4772 goto done;
4773 l = li->li_tv.vval.v_list;
4774
4775 for (i = 0; i < len; i++)
4776 {
4777 if (ptrs[i].score == SCORE_NONE)
4778 break;
4779 if (ptrs[i].lmatchpos != NULL &&
4780 list_append_list(l, ptrs[i].lmatchpos) == FAIL)
4781 goto done;
4782 }
Bram Moolenaar9d19e4f2021-01-02 18:31:32 +01004783
4784 // copy the matching scores
4785 li = list_find(fmatchlist, -1);
4786 if (li == NULL || li->li_tv.vval.v_list == NULL)
4787 goto done;
4788 l = li->li_tv.vval.v_list;
4789 for (i = 0; i < len; i++)
4790 {
4791 if (ptrs[i].score == SCORE_NONE)
4792 break;
4793 if (list_append_number(l, ptrs[i].score) == FAIL)
4794 goto done;
4795 }
Bram Moolenaar635414d2020-09-11 22:25:15 +02004796 }
4797 }
4798
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004799done:
Bram Moolenaar635414d2020-09-11 22:25:15 +02004800 vim_free(ptrs);
4801}
4802
4803/*
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004804 * Do fuzzy matching. Returns the list of matched strings in 'rettv'.
4805 * If 'retmatchpos' is TRUE, also returns the matching character positions.
4806 */
4807 static void
4808do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos)
4809{
4810 callback_T cb;
4811 char_u *key = NULL;
4812 int ret;
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004813 int matchseq = FALSE;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004814
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004815 if (in_vim9script()
4816 && (check_for_list_arg(argvars, 0) == FAIL
4817 || check_for_string_arg(argvars, 1) == FAIL
4818 || check_for_opt_dict_arg(argvars, 2) == FAIL))
4819 return;
4820
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004821 CLEAR_POINTER(&cb);
4822
4823 // validate and get the arguments
4824 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
4825 {
4826 semsg(_(e_listarg), retmatchpos ? "matchfuzzypos()" : "matchfuzzy()");
4827 return;
4828 }
4829 if (argvars[1].v_type != VAR_STRING
4830 || argvars[1].vval.v_string == NULL)
4831 {
4832 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
4833 return;
4834 }
4835
4836 if (argvars[2].v_type != VAR_UNKNOWN)
4837 {
4838 dict_T *d;
4839 dictitem_T *di;
4840
4841 if (argvars[2].v_type != VAR_DICT || argvars[2].vval.v_dict == NULL)
4842 {
4843 emsg(_(e_dictreq));
4844 return;
4845 }
4846
4847 // To search a dict, either a callback function or a key can be
4848 // specified.
4849 d = argvars[2].vval.v_dict;
4850 if ((di = dict_find(d, (char_u *)"key", -1)) != NULL)
4851 {
4852 if (di->di_tv.v_type != VAR_STRING
4853 || di->di_tv.vval.v_string == NULL
4854 || *di->di_tv.vval.v_string == NUL)
4855 {
4856 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
4857 return;
4858 }
4859 key = tv_get_string(&di->di_tv);
4860 }
4861 else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL)
4862 {
4863 cb = get_callback(&di->di_tv);
4864 if (cb.cb_name == NULL)
4865 {
4866 semsg(_(e_invargval), "text_cb");
4867 return;
4868 }
4869 }
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02004870 if (dict_find(d, (char_u *)"matchseq", -1) != NULL)
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);
4876 if (ret != OK)
4877 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)
4890 goto done;
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004891 l = list_alloc();
4892 if (l == NULL)
4893 goto done;
4894 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4895 goto done;
4896 l = list_alloc();
4897 if (l == NULL)
4898 goto done;
4899 if (list_append_list(rettv->vval.v_list, l) == FAIL)
4900 goto done;
4901 }
4902
Bram Moolenaar8ded5b62020-10-23 16:50:30 +02004903 fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]),
4904 matchseq, key, &cb, retmatchpos, rettv->vval.v_list);
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004905
4906done:
4907 free_callback(&cb);
4908}
4909
4910/*
Bram Moolenaar635414d2020-09-11 22:25:15 +02004911 * "matchfuzzy()" function
4912 */
4913 void
4914f_matchfuzzy(typval_T *argvars, typval_T *rettv)
4915{
Bram Moolenaar4f73b8e2020-09-22 20:33:50 +02004916 do_fuzzymatch(argvars, rettv, FALSE);
4917}
4918
4919/*
4920 * "matchfuzzypos()" function
4921 */
4922 void
4923f_matchfuzzypos(typval_T *argvars, typval_T *rettv)
4924{
4925 do_fuzzymatch(argvars, rettv, TRUE);
Bram Moolenaar635414d2020-09-11 22:25:15 +02004926}
4927
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004928#endif