blob: a6b60f63cba7062a9a74dfd95bffcf1c212a0d35 [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
39#define SEARCH_STAT_DEF_TIMEOUT 20L
40#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;
137 magic = p_magic;
138
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 Moolenaarf9e3e092019-01-13 23:38:42 +0100151 emsg(_(e_nopresub));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100153 emsg(_(e_noprevre));
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 Moolenaar14177b72014-01-14 15:53:51 +0100192 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100194 // search or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
196 save_re_pat(RE_SEARCH, pat, magic);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100197 // substitute or global command
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
199 save_re_pat(RE_SUBST, pat, magic);
200 }
201
202 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000203 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
205 if (regmatch->regprog == NULL)
206 return FAIL;
207 return OK;
208}
209
210/*
211 * Get search pattern used by search_regcomp().
212 */
213 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100214get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215{
216 return mr_pattern;
217}
218
Bram Moolenaarabc97732007-08-08 20:49:37 +0000219#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220/*
221 * Reverse text into allocated memory.
222 * Returns the allocated string, NULL when out of memory.
223 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000224 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100225reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226{
227 unsigned len;
228 unsigned s_i, rev_i;
229 char_u *rev;
230
231 /*
232 * Reverse the pattern.
233 */
234 len = (unsigned)STRLEN(s);
235 rev = alloc(len + 1);
236 if (rev != NULL)
237 {
238 rev_i = len;
239 for (s_i = 0; s_i < len; ++s_i)
240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 if (has_mbyte)
242 {
243 int mb_len;
244
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000245 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 rev_i -= mb_len;
247 mch_memmove(rev + rev_i, s + s_i, mb_len);
248 s_i += mb_len - 1;
249 }
250 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 rev[--rev_i] = s[s_i];
252
253 }
254 rev[len] = NUL;
255 }
256 return rev;
257}
258#endif
259
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100260 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100261save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262{
263 if (spats[idx].pat != pat)
264 {
265 vim_free(spats[idx].pat);
266 spats[idx].pat = vim_strsave(pat);
267 spats[idx].magic = magic;
268 spats[idx].no_scs = no_smartcase;
269 last_idx = idx;
270#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100271 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000273 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200274 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275#endif
276 }
277}
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279/*
280 * Save the search patterns, so they can be restored later.
281 * Used before/after executing autocommands and user functions.
282 */
283static int save_level = 0;
284
285 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100286save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287{
288 if (save_level++ == 0)
289 {
290 saved_spats[0] = spats[0];
291 if (spats[0].pat != NULL)
292 saved_spats[0].pat = vim_strsave(spats[0].pat);
293 saved_spats[1] = spats[1];
294 if (spats[1].pat != NULL)
295 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100296#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100297 saved_spats_last_idx = last_idx;
298 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 }
301}
302
303 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100304restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305{
306 if (--save_level == 0)
307 {
308 vim_free(spats[0].pat);
309 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100310#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000311 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 vim_free(spats[1].pat);
314 spats[1] = saved_spats[1];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100315#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100316 last_idx = saved_spats_last_idx;
317 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 }
320}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000322#if defined(EXITFREE) || defined(PROTO)
323 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100324free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000325{
326 vim_free(spats[0].pat);
327 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000328
329# ifdef FEAT_RIGHTLEFT
330 if (mr_pattern_alloced)
331 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200332 vim_free(mr_pattern);
333 mr_pattern_alloced = FALSE;
334 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000335 }
336# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000337}
338#endif
339
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100340#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100341// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
342// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200343static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100344static int did_save_last_search_spat = 0;
345static int saved_last_idx = 0;
346static int saved_no_hlsearch = 0;
347
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100348/*
349 * Save and restore the search pattern for incremental highlight search
350 * feature.
351 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100352 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100353 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100354 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100355 */
356 void
357save_last_search_pattern(void)
358{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100359 if (did_save_last_search_spat != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100360 iemsg("did_save_last_search_spat is not zero");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100361 else
362 ++did_save_last_search_spat;
363
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 Moolenaar01a060d2018-11-30 21:57:55 +0100374 if (did_save_last_search_spat != 1)
375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100376 iemsg("did_save_last_search_spat is not one");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100377 return;
378 }
379 --did_save_last_search_spat;
380
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100381 vim_free(spats[RE_SEARCH].pat);
382 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100383 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100384# if defined(FEAT_EVAL)
385 set_vv_searchforward();
386# endif
387 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200388 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100389}
Bram Moolenaard0480092017-11-16 22:20:39 +0100390
391 char_u *
392last_search_pattern(void)
393{
394 return spats[RE_SEARCH].pat;
395}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100396#endif
397
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398/*
399 * Return TRUE when case should be ignored for search pattern "pat".
400 * Uses the 'ignorecase' and 'smartcase' options.
401 */
402 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100403ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200405 return ignorecase_opt(pat, p_ic, p_scs);
406}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200408/*
409 * As ignorecase() put pass the "ic" and "scs" flags.
410 */
411 int
412ignorecase_opt(char_u *pat, int ic_in, int scs)
413{
414 int ic = ic_in;
415
416 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200417 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200418 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 no_smartcase = FALSE;
420
421 return ic;
422}
423
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200424/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200425 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200426 */
427 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100428pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200429{
430 char_u *p = pat;
431
432 while (*p != NUL)
433 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200434 int l;
435
436 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
437 {
438 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
439 return TRUE;
440 p += l;
441 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100442 else if (*p == '\\')
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200443 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100444 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200445 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100446 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200447 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100448 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200449 p += 2;
450 else
451 p += 1;
452 }
453 else if (MB_ISUPPER(*p))
454 return TRUE;
455 else
456 ++p;
457 }
458 return FALSE;
459}
460
Bram Moolenaar113e1072019-01-20 15:30:40 +0100461#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100463last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200464{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200465 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200466}
467
468 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100469last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200470{
471 return lastcdir == FORWARD;
472}
473
474 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100475last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200476{
477 return last_t_cmd == TRUE;
478}
479
480 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100481set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200482{
483 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200484 lastc_bytelen = len;
485 if (len)
486 memcpy(lastc_bytes, s, len);
487 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200488 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200489}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100490#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200491
492 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100493set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200494{
495 lastcdir = cdir;
496}
497
498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100499set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200500{
501 last_t_cmd = t_cmd;
502}
503
504 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100505last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506{
507 return spats[last_idx].pat;
508}
509
510/*
511 * Reset search direction to forward. For "gd" and "gD" commands.
512 */
513 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100514reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
516 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000517#if defined(FEAT_EVAL)
518 set_vv_searchforward();
519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520}
521
522#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
523/*
524 * Set the last search pattern. For ":let @/ =" and viminfo.
525 * Also set the saved search pattern, so that this works in an autocommand.
526 */
527 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100528set_last_search_pat(
529 char_u *s,
530 int idx,
531 int magic,
532 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533{
534 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100535 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 if (*s == NUL)
537 spats[idx].pat = NULL;
538 else
539 spats[idx].pat = vim_strsave(s);
540 spats[idx].magic = magic;
541 spats[idx].no_scs = FALSE;
542 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#if defined(FEAT_EVAL)
544 set_vv_searchforward();
545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 spats[idx].off.line = FALSE;
547 spats[idx].off.end = FALSE;
548 spats[idx].off.off = 0;
549 if (setlast)
550 last_idx = idx;
551 if (save_level)
552 {
553 vim_free(saved_spats[idx].pat);
554 saved_spats[idx] = spats[0];
555 if (spats[idx].pat == NULL)
556 saved_spats[idx].pat = NULL;
557 else
558 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100559# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100560 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100561# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 }
563# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100564 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000566 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567# endif
568}
569#endif
570
571#ifdef FEAT_SEARCH_EXTRA
572/*
573 * Get a regexp program for the last used search pattern.
574 * This is used for highlighting all matches in a window.
575 * Values returned in regmatch->regprog and regmatch->rmm_ic.
576 */
577 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100578last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 if (spats[last_idx].pat == NULL)
581 {
582 regmatch->regprog = NULL;
583 return;
584 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100585 ++emsg_off; // So it doesn't beep if bad expr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
587 --emsg_off;
588}
589#endif
590
591/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100592 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100593 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
594 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 *
596 * if (options & SEARCH_MSG) == 0 don't give any messages
597 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
598 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
599 * if (options & SEARCH_HIS) put search pattern in history
600 * if (options & SEARCH_END) return position at end of match
601 * if (options & SEARCH_START) accept match at pos itself
602 * if (options & SEARCH_KEEP) keep previous search pattern
603 * if (options & SEARCH_FOLD) match only once in a closed fold
604 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100605 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 *
607 * Return FAIL (zero) for failure, non-zero for success.
608 * When FEAT_EVAL is defined, returns the index of the first matching
609 * subpattern plus one; one if there was none.
610 */
611 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100612searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200613 win_T *win, // window to search in; can be NULL for a
614 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100615 buf_T *buf,
616 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100617 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100618 int dir,
619 char_u *pat,
620 long count,
621 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200622 int pat_use, // which pattern to use when "pat" is empty
623 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100626 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100627 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 regmmatch_T regmatch;
629 char_u *ptr;
630 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000632 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 int loop;
634 pos_T start_pos;
635 int at_first_line;
636 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200637 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int match_ok;
639 long nmatched;
640 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100641 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100642 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_SEARCH_EXTRA
644 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200646 linenr_T stop_lnum = 0; // stop after this line number when != 0
647#ifdef FEAT_RELTIME
648 proftime_T *tm = NULL; // timeout limit or NULL
649 int *timed_out = NULL; // set when timed out or NULL
650#endif
651
652 if (extra_arg != NULL)
653 {
654 stop_lnum = extra_arg->sa_stop_lnum;
655#ifdef FEAT_RELTIME
656 tm = extra_arg->sa_tm;
657 timed_out = &extra_arg->sa_timed_out;
658#endif
659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 if (search_regcomp(pat, RE_SEARCH, pat_use,
662 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
663 {
664 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100665 semsg(_("E383: Invalid search string: %s"), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return FAIL;
667 }
668
Bram Moolenaar280f1262006-01-30 00:14:18 +0000669 /*
670 * find the string
671 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100672 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100674 // When not accepting a match at the start position set "extra_col" to
675 // a non-zero value. Don't do that when starting at MAXCOL, since
676 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200677 if (pos->col == MAXCOL)
678 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100679 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200680 else if (has_mbyte
681 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
682 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100683 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100684 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100685 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200686 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100687 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100688 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100689 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100690 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200691 start_char_len = 1;
692 if (dir == FORWARD)
693 {
694 if (options & SEARCH_START)
695 extra_col = 0;
696 else
697 extra_col = start_char_len;
698 }
699 else
700 {
701 if (options & SEARCH_START)
702 extra_col = start_char_len;
703 else
704 extra_col = 0;
705 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100706
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100707 start_pos = *pos; // remember start pos for detecting no match
708 found = 0; // default: not found
709 at_first_line = TRUE; // default: start in first line
710 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
712 pos->lnum = 1;
713 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100714 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
716
717 /*
718 * Start searching in current line, unless searching backwards and
719 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000720 * If we are searching backwards, in column 0, and not including the
721 * current position, gain some efficiency by skipping back a line.
722 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000724 if (dir == BACKWARD && start_pos.col == 0
725 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 lnum = pos->lnum - 1;
728 at_first_line = FALSE;
729 }
730 else
731 lnum = pos->lnum;
732
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100733 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
735 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
736 lnum += dir, at_first_line = FALSE)
737 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100738 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000739 if (stop_lnum != 0 && (dir == FORWARD
740 ? lnum > stop_lnum : lnum < stop_lnum))
741 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000742#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100743 // Stop after passing the "tm" time limit.
Bram Moolenaar76929292008-01-06 19:07:36 +0000744 if (tm != NULL && profile_passed_limit(tm))
745 break;
746#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000749 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100751 col = at_first_line && (options & SEARCH_COL) ? pos->col
752 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100754 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000755#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200756 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000757#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200758 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000759#endif
760 );
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100761 // Abort searching on an error (e.g., out of stack).
Bram Moolenaar53989552019-12-23 22:59:18 +0100762 if (called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200763#ifdef FEAT_RELTIME
764 || (timed_out != NULL && *timed_out)
765#endif
766 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 break;
768 if (nmatched > 0)
769 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100770 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000771 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000773#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000775#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100776 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000777 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
778 ptr = (char_u *)"";
779 else
780 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782 /*
783 * Forward search in the first line: match should be after
784 * the start position. If not, continue at the end of the
785 * match (this is vi compatible) or on the next char.
786 */
787 if (dir == FORWARD && at_first_line)
788 {
789 match_ok = TRUE;
790 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000791 * When the match starts in a next line it's certainly
792 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 * When match lands on a NUL the cursor will be put
794 * one back afterwards, compare with that position,
795 * otherwise "/$" will get stuck on end of line.
796 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000797 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100798 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000799 ? (nmatched == 1
800 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000802 : ((int)matchpos.col
803 - (ptr[matchpos.col] == NUL)
804 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
806 /*
807 * If vi-compatible searching, continue at the end
808 * of the match, otherwise continue one position
809 * forward.
810 */
811 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
812 {
813 if (nmatched > 1)
814 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100815 // end is in next line, thus no match in
816 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 match_ok = FALSE;
818 break;
819 }
820 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100821 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000822 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 && ptr[matchcol] != NUL)
824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 if (has_mbyte)
826 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000827 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 ++matchcol;
830 }
831 }
832 else
833 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000834 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (ptr[matchcol] != NUL)
836 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000838 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 + matchcol);
840 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 ++matchcol;
842 }
843 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200844 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if (ptr[matchcol] == NUL
847 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000848 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000849 matchcol,
850#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200851 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000852#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200853 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000854#endif
855 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
857 match_ok = FALSE;
858 break;
859 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000860 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 endpos = regmatch.endpos[0];
862# ifdef FEAT_EVAL
863 submatch = first_submatch(&regmatch);
864# endif
865
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100866 // Need to get the line pointer again, a
867 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000868 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 if (!match_ok)
871 continue;
872 }
873 if (dir == BACKWARD)
874 {
875 /*
876 * Now, if there are multiple matches on this line,
877 * we have to get the last one. Or the last one before
878 * the cursor, if we're on that line.
879 * When putting the new cursor at the end, compare
880 * relative to the end of the match.
881 */
882 match_ok = FALSE;
883 for (;;)
884 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100885 // Remember a position that is before the start
886 // position, we use it if it's the last match in
887 // the line. Always accept a position after
888 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000889 if (loop
890 || ((options & SEARCH_END)
891 ? (lnum + regmatch.endpos[0].lnum
892 < start_pos.lnum
893 || (lnum + regmatch.endpos[0].lnum
894 == start_pos.lnum
895 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200896 < (int)start_pos.col
897 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000898 : (lnum + regmatch.startpos[0].lnum
899 < start_pos.lnum
900 || (lnum + regmatch.startpos[0].lnum
901 == start_pos.lnum
902 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200903 < (int)start_pos.col
904 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000907 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 endpos = regmatch.endpos[0];
909# ifdef FEAT_EVAL
910 submatch = first_submatch(&regmatch);
911# endif
912 }
913 else
914 break;
915
916 /*
917 * We found a valid match, now check if there is
918 * another one after it.
919 * If vi-compatible searching, continue at the end
920 * of the match, otherwise continue one position
921 * forward.
922 */
923 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
924 {
925 if (nmatched > 1)
926 break;
927 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100928 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000929 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 && ptr[matchcol] != NUL)
931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 if (has_mbyte)
933 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000934 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 ++matchcol;
937 }
938 }
939 else
940 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100941 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000942 if (matchpos.lnum > 0)
943 break;
944 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (ptr[matchcol] != NUL)
946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (has_mbyte)
948 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000949 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 ++matchcol;
952 }
953 }
954 if (ptr[matchcol] == NUL
955 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000956 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000957 matchcol,
958#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200959 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000960#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200961 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000962#endif
963 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100964 {
965#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100966 // If the search timed out, we did find a match
967 // but it might be the wrong one, so that's not
968 // OK.
Bram Moolenaar9d322762018-02-09 16:04:25 +0100969 if (timed_out != NULL && *timed_out)
970 match_ok = FALSE;
971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100975 // Need to get the line pointer again, a
976 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000977 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979
980 /*
981 * If there is only a match after the cursor, skip
982 * this match.
983 */
984 if (!match_ok)
985 continue;
986 }
987
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100988 // With the SEARCH_END option move to the last character
989 // of the match. Don't do it for an empty match, end
990 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200991 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000992 && !(matchpos.lnum == endpos.lnum
993 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100995 // For a match in the first column, set the position
996 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000997 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000998 pos->col = endpos.col;
999 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001000 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001001 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001002 {
1003 --pos->lnum;
1004 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1005 pos->lnum, FALSE));
1006 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001007 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001008 else
1009 {
1010 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001011 if (has_mbyte
1012 && pos->lnum <= buf->b_ml.ml_line_count)
1013 {
1014 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1015 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1016 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001017 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001018 if (end_pos != NULL)
1019 {
1020 end_pos->lnum = lnum + matchpos.lnum;
1021 end_pos->col = matchpos.col;
1022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 else
1025 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001026 pos->lnum = lnum + matchpos.lnum;
1027 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001028 if (end_pos != NULL)
1029 {
1030 end_pos->lnum = lnum + endpos.lnum;
1031 end_pos->col = endpos.col;
1032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001035 if (end_pos != NULL)
1036 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001038 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001040 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001041 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 search_match_endcol = endpos.col;
1043 break;
1044 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001045 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (got_int)
1047 break;
1048
1049#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001050 // Cancel searching if a character was typed. Used for
1051 // 'incsearch'. Don't check too often, that would slowdown
1052 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if ((options & SEARCH_PEEK)
1054 && ((lnum - pos->lnum) & 0x3f) == 0
1055 && char_avail())
1056 {
1057 break_loop = TRUE;
1058 break;
1059 }
1060#endif
1061
1062 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001063 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065 at_first_line = FALSE;
1066
1067 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001068 * Stop the search if wrapscan isn't set, "stop_lnum" is
1069 * specified, after an interrupt, after a match and after looping
1070 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001072 if (!p_ws || stop_lnum != 0 || got_int
1073 || called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001074#ifdef FEAT_RELTIME
1075 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001076#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001077#ifdef FEAT_SEARCH_EXTRA
1078 || break_loop
1079#endif
1080 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 break;
1082
1083 /*
1084 * If 'wrapscan' is set we continue at the other end of the file.
1085 * If 'shortmess' does not contain 's', we give a message.
1086 * This message is also remembered in keep_msg for when the screen
1087 * is redrawn. The keep_msg is cleared whenever another message is
1088 * written.
1089 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001090 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001094 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1095 give_warning((char_u *)_(dir == BACKWARD
1096 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001097 if (extra_arg != NULL)
1098 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
Bram Moolenaar53989552019-12-23 22:59:18 +01001100 if (got_int || called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001101#ifdef FEAT_RELTIME
1102 || (timed_out != NULL && *timed_out)
1103#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001104#ifdef FEAT_SEARCH_EXTRA
1105 || break_loop
1106#endif
1107 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 break;
1109 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001110 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
Bram Moolenaar473de612013-06-08 18:19:48 +02001112 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001114 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001117 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1119 {
1120 if (p_ws)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001121 semsg(_(e_patnotf2), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 else if (lnum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001123 semsg(_("E384: search hit TOP without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 mr_pattern);
1125 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001126 semsg(_("E385: search hit BOTTOM without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 mr_pattern);
1128 }
1129 return FAIL;
1130 }
1131
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001132 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001133 if (pos->lnum > buf->b_ml.ml_line_count)
1134 {
1135 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001136 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001137 if (pos->col > 0)
1138 --pos->col;
1139 }
1140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 return submatch + 1;
1142}
1143
1144#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001145 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001146set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001147{
1148 spats[0].off.dir = cdir;
1149}
1150
1151 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001152set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001153{
1154 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1155}
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157/*
1158 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001159 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 */
1161 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001162first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163{
1164 int submatch;
1165
1166 for (submatch = 1; ; ++submatch)
1167 {
1168 if (rp->startpos[submatch].lnum >= 0)
1169 break;
1170 if (submatch == 9)
1171 {
1172 submatch = 0;
1173 break;
1174 }
1175 }
1176 return submatch;
1177}
1178#endif
1179
1180/*
1181 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001182 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 * If 'dirc' is 0: use previous dir.
1184 * If 'pat' is NULL or empty : use previous string.
1185 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1186 * If 'options & SEARCH_ECHO': echo the search command and handle options
1187 * If 'options & SEARCH_MSG' : may give error message
1188 * If 'options & SEARCH_OPT' : interpret optional flags
1189 * If 'options & SEARCH_HIS' : put search pattern in history
1190 * If 'options & SEARCH_NOOF': don't add offset to position
1191 * If 'options & SEARCH_MARK': set previous context mark
1192 * If 'options & SEARCH_KEEP': keep previous search pattern
1193 * If 'options & SEARCH_START': accept match at curpos itself
1194 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1195 *
1196 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1197 * makes the movement linewise without moving the match position.
1198 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001199 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
1201 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001202do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001203 oparg_T *oap, // can be NULL
1204 int dirc, // '/' or '?'
Bram Moolenaarc036e872020-02-21 21:30:52 +01001205 int search_delim, // the delimiter for the search, e.g. '%' in s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001206 char_u *pat,
1207 long count,
1208 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001209 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001211 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001213 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001214 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 char_u *p;
1216 long c;
1217 char_u *dircp;
1218 char_u *strcopy = NULL;
1219 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001220 char_u *msgbuf = NULL;
1221 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001222 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224 /*
1225 * A line offset is not remembered, this is vi compatible.
1226 */
1227 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1228 {
1229 spats[0].off.line = FALSE;
1230 spats[0].off.off = 0;
1231 }
1232
1233 /*
1234 * Save the values for when (options & SEARCH_KEEP) is used.
1235 * (there is no "if ()" around this because gcc wants them initialized)
1236 */
1237 old_off = spats[0].off;
1238
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001239 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
1241 /*
1242 * Find out the direction of the search.
1243 */
1244 if (dirc == 0)
1245 dirc = spats[0].off.dir;
1246 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001249#if defined(FEAT_EVAL)
1250 set_vv_searchforward();
1251#endif
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if (options & SEARCH_REV)
1254 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001255#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001256 // There is a bug in the Visual C++ 2.2 compiler which means that
1257 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 dirc = (dirc == '/') ? '?' : '/';
1259#else
1260 if (dirc == '/')
1261 dirc = '?';
1262 else
1263 dirc = '/';
1264#endif
1265 }
1266
1267#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001268 // If the cursor is in a closed fold, don't find another match in the same
1269 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (dirc == '/')
1271 {
1272 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001273 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275 else
1276 {
1277 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1278 pos.col = 0;
1279 }
1280#endif
1281
1282#ifdef FEAT_SEARCH_EXTRA
1283 /*
1284 * Turn 'hlsearch' highlighting back on.
1285 */
1286 if (no_hlsearch && !(options & SEARCH_KEEP))
1287 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001288 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001289 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291#endif
1292
1293 /*
1294 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1295 */
1296 for (;;)
1297 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001298 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 searchstr = pat;
1301 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001302 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001303 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001305 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001307 searchstr = spats[RE_SUBST].pat;
1308 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 emsg(_(e_noprevre));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001311 retval = 0;
1312 goto end_do_search;
1313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001315 else
1316 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001317 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001318 searchstr = (char_u *)"";
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001322 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 {
1324 /*
1325 * Find end of regular expression.
1326 * If there is a matching '/' or '?', toss it.
1327 */
1328 ps = strcopy;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001329 p = skip_regexp_ex(pat, search_delim, (int)p_magic, &strcopy, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 if (strcopy != ps)
1331 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001332 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001333 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 pat = strcopy;
1335 searchstr = strcopy;
1336 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001337 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001339 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 *p++ = NUL;
1341 }
1342 spats[0].off.line = FALSE;
1343 spats[0].off.end = FALSE;
1344 spats[0].off.off = 0;
1345 /*
1346 * Check for a line offset or a character offset.
1347 * For get_address (echo off) we don't check for a character
1348 * offset, because it is meaningless and the 's' could be a
1349 * substitute command.
1350 */
1351 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1352 spats[0].off.line = TRUE;
1353 else if ((options & SEARCH_OPT) &&
1354 (*p == 'e' || *p == 's' || *p == 'b'))
1355 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001356 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 spats[0].off.end = SEARCH_END;
1358 ++p;
1359 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001360 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001362 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1364 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001365 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001367 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 spats[0].off.off = 1;
1369 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001370 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 ++p;
1372 }
1373
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001374 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 searchcmdlen += (int)(p - pat);
1376
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001377 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001380 if ((options & SEARCH_ECHO) && messaging() &&
1381 !msg_silent &&
1382 (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001385 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001386 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001388 // Compute msg_row early.
1389 msg_start();
1390
Bram Moolenaar984f0312019-05-24 13:11:47 +02001391 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001392 if (!cmd_silent &&
1393 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001394 {
1395 p = off_buf;
1396 *p++ = dirc;
1397 if (spats[0].off.end)
1398 *p++ = 'e';
1399 else if (!spats[0].off.line)
1400 *p++ = 's';
1401 if (spats[0].off.off > 0 || spats[0].off.line)
1402 *p++ = '+';
1403 *p = NUL;
1404 if (spats[0].off.off != 0 || spats[0].off.line)
1405 sprintf((char *)p, "%ld", spats[0].off.off);
1406 off_len = STRLEN(off_buf);
1407 }
1408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001410 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 else
1412 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001413
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001414 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001415 {
1416 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001417 // search stat. Use all the space available, so that the
1418 // search state is right aligned. If there is not enough space
1419 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001420 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001421 // Use all the columns.
1422 len = (int)(Rows - msg_row) * Columns - 1;
1423 else
1424 // Use up to 'showcmd' column.
1425 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001426 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1427 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001428 }
1429 else
1430 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001431 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001432
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001433 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001434 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (msgbuf != NULL)
1436 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001437 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001438 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001439 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1440 // empty for the search_stat feature.
1441 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001442 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001443 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001445 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1446 {
1447 // Use a space to draw the composing char on.
1448 msgbuf[1] = ' ';
1449 mch_memmove(msgbuf + 2, p, STRLEN(p));
1450 }
1451 else
1452 mch_memmove(msgbuf + 1, p, STRLEN(p));
1453 if (off_len > 0)
1454 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001456 trunc = msg_strtrunc(msgbuf, TRUE);
1457 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001459 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001460 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001463 #ifdef FEAT_RIGHTLEFT
1464 // The search pattern could be shown on the right in rightleft
1465 // mode, but the 'ruler' and 'showcmd' area use it too, thus
1466 // it would be blanked out again very soon. Show it on the
1467 // left, but do reverse the text.
1468 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1469 {
1470 char_u *r;
1471 size_t pat_len;
1472
1473 r = reverse_text(msgbuf);
1474 if (r != NULL)
1475 {
1476 vim_free(msgbuf);
1477 msgbuf = r;
1478 // move reversed text to beginning of buffer
1479 while (*r != NUL && *r == ' ')
1480 r++;
1481 pat_len = msgbuf + STRLEN(msgbuf) - r;
1482 mch_memmove(msgbuf, r, pat_len);
1483 // overwrite old text
1484 if ((size_t)(r - msgbuf) >= pat_len)
1485 vim_memset(r, ' ', pat_len);
1486 else
1487 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1488 }
1489 }
1490 #endif
1491 msg_outtrans(msgbuf);
1492 msg_clr_eos();
1493 msg_check();
1494
1495 gotocmdline(FALSE);
1496 out_flush();
1497 msg_nowait = TRUE; // don't wait for this message
1498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 }
1501
1502 /*
1503 * If there is a character offset, subtract it from the current
1504 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001505 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 * This is not done for a line offset, because then we would not be vi
1507 * compatible.
1508 */
Bram Moolenaared203462004-06-16 11:19:22 +00001509 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
1511 if (spats[0].off.off > 0)
1512 {
1513 for (c = spats[0].off.off; c; --c)
1514 if (decl(&pos) == -1)
1515 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001516 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001518 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 pos.col = MAXCOL;
1520 }
1521 }
1522 else
1523 {
1524 for (c = spats[0].off.off; c; ++c)
1525 if (incl(&pos) == -1)
1526 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001527 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1530 pos.col = 0;
1531 }
1532 }
1533 }
1534
Bram Moolenaar14184a32019-02-16 15:10:30 +01001535 c = searchit(curwin, curbuf, &pos, NULL,
1536 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 searchstr, count, spats[0].off.end + (options &
1538 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1539 + SEARCH_MSG + SEARCH_START
1540 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001541 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
1543 if (dircp != NULL)
Bram Moolenaarc036e872020-02-21 21:30:52 +01001544 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001545
1546 if (!shortmess(SHM_SEARCH)
1547 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1548 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001549 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (c == FAIL)
1552 {
1553 retval = 0;
1554 goto end_do_search;
1555 }
1556 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001557 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001559 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
1561 /*
1562 * Add character and/or line offset
1563 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001564 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001566 pos_T org_pos = pos;
1567
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001568 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
1570 c = pos.lnum + spats[0].off.off;
1571 if (c < 1)
1572 pos.lnum = 1;
1573 else if (c > curbuf->b_ml.ml_line_count)
1574 pos.lnum = curbuf->b_ml.ml_line_count;
1575 else
1576 pos.lnum = c;
1577 pos.col = 0;
1578
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001579 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001581 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001583 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001584 c = spats[0].off.off;
1585 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001587 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 if (incl(&pos) == -1)
1589 break;
1590 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001591 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 else
1593 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001594 while (c++ < 0)
1595 if (decl(&pos) == -1)
1596 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001599 if (!EQUAL_POS(pos, org_pos))
1600 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001603 // Show [1/15] if 'S' is not in 'shortmess'.
1604 if ((options & SEARCH_ECHO)
1605 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001606 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001607 && c != FAIL
1608 && !shortmess(SHM_SEARCHCOUNT)
1609 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001610 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1611 show_top_bot_msg, msgbuf,
1612 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001613#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001614 || (!(fdo_flags & FDO_SEARCH)
1615 && hasFolding(curwin->w_cursor.lnum,
1616 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001617#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001618 ),
1619 SEARCH_STAT_DEF_MAX_COUNT,
1620 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 /*
1623 * The search command can be followed by a ';' to do another search.
1624 * For example: "/pat/;/foo/+3;?bar"
1625 * This is like doing another search command, except:
1626 * - The remembered direction '/' or '?' is from the first search.
1627 * - When an error happens the cursor isn't moved at all.
1628 * Don't do this when called by get_address() (it handles ';' itself).
1629 */
1630 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1631 break;
1632
1633 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001634 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (dirc != '?' && dirc != '/')
1636 {
1637 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001638 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 goto end_do_search;
1640 }
1641 ++pat;
1642 }
1643
1644 if (options & SEARCH_MARK)
1645 setpcmark();
1646 curwin->w_cursor = pos;
1647 curwin->w_set_curswant = TRUE;
1648
1649end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001650 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 spats[0].off = old_off;
1652 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001653 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654
1655 return retval;
1656}
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658/*
1659 * search_for_exact_line(buf, pos, dir, pat)
1660 *
1661 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001662 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001664 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 * Return OK for success, or FAIL if no line found.
1666 */
1667 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001668search_for_exact_line(
1669 buf_T *buf,
1670 pos_T *pos,
1671 int dir,
1672 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673{
1674 linenr_T start = 0;
1675 char_u *ptr;
1676 char_u *p;
1677
1678 if (buf->b_ml.ml_line_count == 0)
1679 return FAIL;
1680 for (;;)
1681 {
1682 pos->lnum += dir;
1683 if (pos->lnum < 1)
1684 {
1685 if (p_ws)
1686 {
1687 pos->lnum = buf->b_ml.ml_line_count;
1688 if (!shortmess(SHM_SEARCH))
1689 give_warning((char_u *)_(top_bot_msg), TRUE);
1690 }
1691 else
1692 {
1693 pos->lnum = 1;
1694 break;
1695 }
1696 }
1697 else if (pos->lnum > buf->b_ml.ml_line_count)
1698 {
1699 if (p_ws)
1700 {
1701 pos->lnum = 1;
1702 if (!shortmess(SHM_SEARCH))
1703 give_warning((char_u *)_(bot_top_msg), TRUE);
1704 }
1705 else
1706 {
1707 pos->lnum = 1;
1708 break;
1709 }
1710 }
1711 if (pos->lnum == start)
1712 break;
1713 if (start == 0)
1714 start = pos->lnum;
1715 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1716 p = skipwhite(ptr);
1717 pos->col = (colnr_T) (p - ptr);
1718
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001719 // when adding lines the matching line may be empty but it is not
1720 // ignored because we are interested in the next line -- Acevedo
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001721 if ((compl_cont_status & CONT_ADDING)
1722 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 {
1724 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1725 return OK;
1726 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001727 else if (*p != NUL) // ignore empty lines
1728 { // expanding lines or words
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001729 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1730 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 return OK;
1732 }
1733 }
1734 return FAIL;
1735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
1737/*
1738 * Character Searches
1739 */
1740
1741/*
1742 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1743 * position of the character, otherwise move to just before the char.
1744 * Do this "cap->count1" times.
1745 * Return FAIL or OK.
1746 */
1747 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001748searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001750 int c = cap->nchar; // char to search for
1751 int dir = cap->arg; // TRUE for searching forward
1752 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 int col;
1754 char_u *p;
1755 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001756 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001758 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001760 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001762 *lastc = c;
1763 set_csearch_direction(dir);
1764 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001765 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 if (cap->ncharC1 != 0)
1767 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001768 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1769 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001771 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1772 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
1775 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001776 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001778 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001780 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 dir = -lastcdir;
1782 else
1783 dir = lastcdir;
1784 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001785 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001786 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001787
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001788 // Force a move of at least one char, so ";" and "," will move the
1789 // cursor, even if the cursor is right in front of char we are looking
1790 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001791 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001792 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 }
1794
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001795 if (dir == BACKWARD)
1796 cap->oap->inclusive = FALSE;
1797 else
1798 cap->oap->inclusive = TRUE;
1799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 p = ml_get_curline();
1801 col = curwin->w_cursor.col;
1802 len = (int)STRLEN(p);
1803
1804 while (count--)
1805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (has_mbyte)
1807 {
1808 for (;;)
1809 {
1810 if (dir > 0)
1811 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001812 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 if (col >= len)
1814 return FAIL;
1815 }
1816 else
1817 {
1818 if (col == 0)
1819 return FAIL;
1820 col -= (*mb_head_off)(p, p + col - 1) + 1;
1821 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001822 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001824 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 break;
1826 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001827 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001828 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001829 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001830 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832 }
1833 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
1835 for (;;)
1836 {
1837 if ((col += dir) < 0 || col >= len)
1838 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001839 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001841 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 }
1843 }
1844 }
1845
1846 if (t_cmd)
1847 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001848 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (has_mbyte)
1851 {
1852 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001853 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001854 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001856 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 col -= (*mb_head_off)(p, p + col);
1858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860 curwin->w_cursor.col = col;
1861
1862 return OK;
1863}
1864
1865/*
1866 * "Other" Searches
1867 */
1868
1869/*
1870 * findmatch - find the matching paren or brace
1871 *
1872 * Improvement over vi: Braces inside quotes are ignored.
1873 */
1874 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001875findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
1877 return findmatchlimit(oap, initc, 0, 0);
1878}
1879
1880/*
1881 * Return TRUE if the character before "linep[col]" equals "ch".
1882 * Return FALSE if "col" is zero.
1883 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1884 * is NULL.
1885 * Handles multibyte string correctly.
1886 */
1887 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001888check_prevcol(
1889 char_u *linep,
1890 int col,
1891 int ch,
1892 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (col > 0 && has_mbyte)
1896 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (prevcol)
1898 *prevcol = col;
1899 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1900}
1901
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001902/*
1903 * Raw string start is found at linep[startpos.col - 1].
1904 * Return TRUE if the matching end can be found between startpos and endpos.
1905 */
1906 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001907find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001908{
1909 char_u *p;
1910 char_u *delim_copy;
1911 size_t delim_len;
1912 linenr_T lnum;
1913 int found = FALSE;
1914
1915 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1916 ;
1917 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001918 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001919 if (delim_copy == NULL)
1920 return FALSE;
1921 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1922 {
1923 char_u *line = ml_get(lnum);
1924
1925 for (p = line + (lnum == startpos->lnum
1926 ? startpos->col + 1 : 0); *p; ++p)
1927 {
1928 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1929 break;
1930 if (*p == ')' && p[delim_len + 1] == '"'
1931 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1932 {
1933 found = TRUE;
1934 break;
1935 }
1936 }
1937 if (found)
1938 break;
1939 }
1940 vim_free(delim_copy);
1941 return found;
1942}
1943
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001945 * Check matchpairs option for "*initc".
1946 * If there is a match set "*initc" to the matching character and "*findc" to
1947 * the opposite character. Set "*backwards" to the direction.
1948 * When "switchit" is TRUE swap the direction.
1949 */
1950 static void
1951find_mps_values(
1952 int *initc,
1953 int *findc,
1954 int *backwards,
1955 int switchit)
1956{
1957 char_u *ptr;
1958
1959 ptr = curbuf->b_p_mps;
1960 while (*ptr != NUL)
1961 {
1962 if (has_mbyte)
1963 {
1964 char_u *prev;
1965
1966 if (mb_ptr2char(ptr) == *initc)
1967 {
1968 if (switchit)
1969 {
1970 *findc = *initc;
1971 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1972 *backwards = TRUE;
1973 }
1974 else
1975 {
1976 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1977 *backwards = FALSE;
1978 }
1979 return;
1980 }
1981 prev = ptr;
1982 ptr += mb_ptr2len(ptr) + 1;
1983 if (mb_ptr2char(ptr) == *initc)
1984 {
1985 if (switchit)
1986 {
1987 *findc = *initc;
1988 *initc = mb_ptr2char(prev);
1989 *backwards = FALSE;
1990 }
1991 else
1992 {
1993 *findc = mb_ptr2char(prev);
1994 *backwards = TRUE;
1995 }
1996 return;
1997 }
1998 ptr += mb_ptr2len(ptr);
1999 }
2000 else
2001 {
2002 if (*ptr == *initc)
2003 {
2004 if (switchit)
2005 {
2006 *backwards = TRUE;
2007 *findc = *initc;
2008 *initc = ptr[2];
2009 }
2010 else
2011 {
2012 *backwards = FALSE;
2013 *findc = ptr[2];
2014 }
2015 return;
2016 }
2017 ptr += 2;
2018 if (*ptr == *initc)
2019 {
2020 if (switchit)
2021 {
2022 *backwards = FALSE;
2023 *findc = *initc;
2024 *initc = ptr[-2];
2025 }
2026 else
2027 {
2028 *backwards = TRUE;
2029 *findc = ptr[-2];
2030 }
2031 return;
2032 }
2033 ++ptr;
2034 }
2035 if (*ptr == ',')
2036 ++ptr;
2037 }
2038}
2039
2040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002042 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2043 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 *
2045 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002046 * character at or after the cursor. Special values:
2047 * '*' look for C-style comment / *
2048 * '/' look for C-style comment / *, ignoring comment-end
2049 * '#' look for preprocessor directives
2050 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 *
2052 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2053 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2054 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2055 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002056 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002057 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002058 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 */
2060
2061 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002062findmatchlimit(
2063 oparg_T *oap,
2064 int initc,
2065 int flags,
2066 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002068 static pos_T pos; // current search position
2069 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002071 int count = 0; // cumulative number of braces
2072 int backwards = FALSE; // init for gcc
2073 int raw_string = FALSE; // search for raw string
2074 int inquote = FALSE; // TRUE when inside quotes
2075 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002077 int do_quotes; // check for quotes in current line
2078 int at_start; // do_quotes value at start position
2079 int hash_dir = 0; // Direction searched for # things
2080 int comment_dir = 0; // Direction searched for comments
2081 pos_T match_pos; // Where last slash-star was found
2082 int start_in_quotes; // start position is in quotes
2083 int traveled = 0; // how far we've searched so far
2084 int ignore_cend = FALSE; // ignore comment end
2085 int cpo_match; // vi compatible matching
2086 int cpo_bsl; // don't recognize backslashes
2087 int match_escaped = 0; // search for escaped match
2088 int dir; // Direction to search
2089 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002090#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002091 int lispcomm = FALSE; // inside of Lisp-style comment
2092 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
2095 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002096 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 linep = ml_get(pos.lnum);
2098
2099 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2100 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2101
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002102 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (flags & FM_BACKWARD)
2104 dir = BACKWARD;
2105 else if (flags & FM_FORWARD)
2106 dir = FORWARD;
2107 else
2108 dir = 0;
2109
2110 /*
2111 * if initc given, look in the table for the matching character
2112 * '/' and '*' are special cases: look for start or end of comment.
2113 * When '/' is used, we ignore running backwards into an star-slash, for
2114 * "[*" command, we just want to find any comment.
2115 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002116 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 comment_dir = dir;
2119 if (initc == '/')
2120 ignore_cend = TRUE;
2121 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002122 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 initc = NUL;
2124 }
2125 else if (initc != '#' && initc != NUL)
2126 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002127 find_mps_values(&initc, &findc, &backwards, TRUE);
2128 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 return NULL;
2130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 else
2132 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002133 /*
2134 * Either initc is '#', or no initc was given and we need to look
2135 * under the cursor.
2136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 if (initc == '#')
2138 {
2139 hash_dir = dir;
2140 }
2141 else
2142 {
2143 /*
2144 * initc was not given, must look for something to match under
2145 * or near the cursor.
2146 * Only check for special things when 'cpo' doesn't have '%'.
2147 */
2148 if (!cpo_match)
2149 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002150 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 ptr = skipwhite(linep);
2152 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2153 {
2154 ptr = skipwhite(ptr + 1);
2155 if ( STRNCMP(ptr, "if", 2) == 0
2156 || STRNCMP(ptr, "endif", 5) == 0
2157 || STRNCMP(ptr, "el", 2) == 0)
2158 hash_dir = 1;
2159 }
2160
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002161 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 else if (linep[pos.col] == '/')
2163 {
2164 if (linep[pos.col + 1] == '*')
2165 {
2166 comment_dir = FORWARD;
2167 backwards = FALSE;
2168 pos.col++;
2169 }
2170 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2171 {
2172 comment_dir = BACKWARD;
2173 backwards = TRUE;
2174 pos.col--;
2175 }
2176 }
2177 else if (linep[pos.col] == '*')
2178 {
2179 if (linep[pos.col + 1] == '/')
2180 {
2181 comment_dir = BACKWARD;
2182 backwards = TRUE;
2183 }
2184 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2185 {
2186 comment_dir = FORWARD;
2187 backwards = FALSE;
2188 }
2189 }
2190 }
2191
2192 /*
2193 * If we are not on a comment or the # at the start of a line, then
2194 * look for brace anywhere on this line after the cursor.
2195 */
2196 if (!hash_dir && !comment_dir)
2197 {
2198 /*
2199 * Find the brace under or after the cursor.
2200 * If beyond the end of the line, use the last character in
2201 * the line.
2202 */
2203 if (linep[pos.col] == NUL && pos.col)
2204 --pos.col;
2205 for (;;)
2206 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002207 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 if (initc == NUL)
2209 break;
2210
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002211 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (findc)
2213 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002214 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 }
2216 if (!findc)
2217 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002218 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 if (!cpo_match && *skipwhite(linep) == '#')
2220 hash_dir = 1;
2221 else
2222 return NULL;
2223 }
2224 else if (!cpo_bsl)
2225 {
2226 int col, bslcnt = 0;
2227
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002228 // Set "match_escaped" if there are an odd number of
2229 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2231 bslcnt++;
2232 match_escaped = (bslcnt & 1);
2233 }
2234 }
2235 }
2236 if (hash_dir)
2237 {
2238 /*
2239 * Look for matching #if, #else, #elif, or #endif
2240 */
2241 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002242 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (initc != '#')
2244 {
2245 ptr = skipwhite(skipwhite(linep) + 1);
2246 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2247 hash_dir = 1;
2248 else if (STRNCMP(ptr, "endif", 5) == 0)
2249 hash_dir = -1;
2250 else
2251 return NULL;
2252 }
2253 pos.col = 0;
2254 while (!got_int)
2255 {
2256 if (hash_dir > 0)
2257 {
2258 if (pos.lnum == curbuf->b_ml.ml_line_count)
2259 break;
2260 }
2261 else if (pos.lnum == 1)
2262 break;
2263 pos.lnum += hash_dir;
2264 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002265 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 ptr = skipwhite(linep);
2267 if (*ptr != '#')
2268 continue;
2269 pos.col = (colnr_T) (ptr - linep);
2270 ptr = skipwhite(ptr + 1);
2271 if (hash_dir > 0)
2272 {
2273 if (STRNCMP(ptr, "if", 2) == 0)
2274 count++;
2275 else if (STRNCMP(ptr, "el", 2) == 0)
2276 {
2277 if (count == 0)
2278 return &pos;
2279 }
2280 else if (STRNCMP(ptr, "endif", 5) == 0)
2281 {
2282 if (count == 0)
2283 return &pos;
2284 count--;
2285 }
2286 }
2287 else
2288 {
2289 if (STRNCMP(ptr, "if", 2) == 0)
2290 {
2291 if (count == 0)
2292 return &pos;
2293 count--;
2294 }
2295 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2296 {
2297 if (count == 0)
2298 return &pos;
2299 }
2300 else if (STRNCMP(ptr, "endif", 5) == 0)
2301 count++;
2302 }
2303 }
2304 return NULL;
2305 }
2306 }
2307
2308#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002309 // This is just guessing: when 'rightleft' is set, search for a matching
2310 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2312 backwards = !backwards;
2313#endif
2314
2315 do_quotes = -1;
2316 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002317 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002318
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002319 // backward search: Check if this line contains a single-line comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002320 if ((backwards && comment_dir)
2321#ifdef FEAT_LISP
2322 || lisp
2323#endif
2324 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002326#ifdef FEAT_LISP
2327 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002328 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 while (!got_int)
2331 {
2332 /*
2333 * Go to the next position, forward or backward. We could use
2334 * inc() and dec() here, but that is much slower
2335 */
2336 if (backwards)
2337 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002338#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002339 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002340 if (lispcomm && pos.col < (colnr_T)comment_col)
2341 break;
2342#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002343 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002345 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 break;
2347 --pos.lnum;
2348
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002349 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 break;
2351
2352 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002353 pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 do_quotes = -1;
2355 line_breakcheck();
2356
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002357 // Check if this line contains a single-line comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002358 if (comment_dir
2359#ifdef FEAT_LISP
2360 || lisp
2361#endif
2362 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002364#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002365 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002366 if (lisp && comment_col != MAXCOL)
2367 pos.col = comment_col;
2368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370 else
2371 {
2372 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 if (has_mbyte)
2374 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 }
2376 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002377 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002379 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002380 // at end of line, go to next one
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002381#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002382 // don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002383 || (lisp && comment_col != MAXCOL
2384 && pos.col == (colnr_T)comment_col)
2385#endif
2386 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002388 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002389#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002390 // line is exhausted and comment with it,
2391 // don't search for match in code
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002392 || lispcomm
2393#endif
2394 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 break;
2396 ++pos.lnum;
2397
2398 if (maxtravel && traveled++ > maxtravel)
2399 break;
2400
2401 linep = ml_get(pos.lnum);
2402 pos.col = 0;
2403 do_quotes = -1;
2404 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002405#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002406 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002407 comment_col = check_linecomment(linep);
2408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 else
2411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002413 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 ++pos.col;
2416 }
2417 }
2418
2419 /*
2420 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2421 */
2422 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2423 (linep[0] == '{' || linep[0] == '}'))
2424 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002425 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002427 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429
2430 if (comment_dir)
2431 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002432 // Note: comments do not nest, and we ignore quotes in them
2433 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (comment_dir == FORWARD)
2435 {
2436 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2437 {
2438 pos.col++;
2439 return &pos;
2440 }
2441 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002442 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 /*
2445 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002446 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 */
2448 if (pos.col == 0)
2449 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002450 else if (raw_string)
2451 {
2452 if (linep[pos.col - 1] == 'R'
2453 && linep[pos.col] == '"'
2454 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2455 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002456 // Possible start of raw string. Now that we have the
2457 // delimiter we can check if it ends before where we
2458 // started searching, or before the previously found
2459 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002460 if (!find_rawstring_end(linep, &pos,
2461 count > 0 ? &match_pos : &curwin->w_cursor))
2462 {
2463 count++;
2464 match_pos = pos;
2465 match_pos.col--;
2466 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002467 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002468 }
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 else if ( linep[pos.col - 1] == '/'
2471 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002472 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 && (int)pos.col < comment_col)
2474 {
2475 count++;
2476 match_pos = pos;
2477 match_pos.col--;
2478 }
2479 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2480 {
2481 if (count > 0)
2482 pos = match_pos;
2483 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2484 && (int)pos.col <= comment_col)
2485 pos.col -= 2;
2486 else if (ignore_cend)
2487 continue;
2488 else
2489 return NULL;
2490 return &pos;
2491 }
2492 }
2493 continue;
2494 }
2495
2496 /*
2497 * If smart matching ('cpoptions' does not contain '%'), braces inside
2498 * of quotes are ignored, but only if there is an even number of
2499 * quotes in the line.
2500 */
2501 if (cpo_match)
2502 do_quotes = 0;
2503 else if (do_quotes == -1)
2504 {
2505 /*
2506 * Count the number of quotes in the line, skipping \" and '"'.
2507 * Watch out for "\\".
2508 */
2509 at_start = do_quotes;
2510 for (ptr = linep; *ptr; ++ptr)
2511 {
2512 if (ptr == linep + pos.col + backwards)
2513 at_start = (do_quotes & 1);
2514 if (*ptr == '"'
2515 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2516 ++do_quotes;
2517 if (*ptr == '\\' && ptr[1] != NUL)
2518 ++ptr;
2519 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002520 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
2522 /*
2523 * If we find an uneven count, check current line and previous
2524 * one for a '\' at the end.
2525 */
2526 if (!do_quotes)
2527 {
2528 inquote = FALSE;
2529 if (ptr[-1] == '\\')
2530 {
2531 do_quotes = 1;
2532 if (start_in_quotes == MAYBE)
2533 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002534 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 inquote = TRUE;
2536 start_in_quotes = TRUE;
2537 }
2538 else if (backwards)
2539 inquote = TRUE;
2540 }
2541 if (pos.lnum > 1)
2542 {
2543 ptr = ml_get(pos.lnum - 1);
2544 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2545 {
2546 do_quotes = 1;
2547 if (start_in_quotes == MAYBE)
2548 {
2549 inquote = at_start;
2550 if (inquote)
2551 start_in_quotes = TRUE;
2552 }
2553 else if (!backwards)
2554 inquote = TRUE;
2555 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002556
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002557 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002558 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560 }
2561 }
2562 if (start_in_quotes == MAYBE)
2563 start_in_quotes = FALSE;
2564
2565 /*
2566 * If 'smartmatch' is set:
2567 * Things inside quotes are ignored by setting 'inquote'. If we
2568 * find a quote without a preceding '\' invert 'inquote'. At the
2569 * end of a line not ending in '\' we reset 'inquote'.
2570 *
2571 * In lines with an uneven number of quotes (without preceding '\')
2572 * we do not know which part to ignore. Therefore we only set
2573 * inquote if the number of quotes in a line is even, unless this
2574 * line or the previous one ends in a '\'. Complicated, isn't it?
2575 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002576 c = PTR2CHAR(linep + pos.col);
2577 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002580 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2582 {
2583 inquote = FALSE;
2584 start_in_quotes = FALSE;
2585 }
2586 break;
2587
2588 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002589 // a quote that is preceded with an odd number of backslashes is
2590 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (do_quotes)
2592 {
2593 int col;
2594
2595 for (col = pos.col - 1; col >= 0; --col)
2596 if (linep[col] != '\\')
2597 break;
2598 if ((((int)pos.col - 1 - col) & 1) == 0)
2599 {
2600 inquote = !inquote;
2601 start_in_quotes = FALSE;
2602 }
2603 }
2604 break;
2605
2606 /*
2607 * If smart matching ('cpoptions' does not contain '%'):
2608 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2609 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2610 * skipped, there is never a brace in them.
2611 * Ignore this when finding matches for `'.
2612 */
2613 case '\'':
2614 if (!cpo_match && initc != '\'' && findc != '\'')
2615 {
2616 if (backwards)
2617 {
2618 if (pos.col > 1)
2619 {
2620 if (linep[pos.col - 2] == '\'')
2621 {
2622 pos.col -= 2;
2623 break;
2624 }
2625 else if (linep[pos.col - 2] == '\\' &&
2626 pos.col > 2 && linep[pos.col - 3] == '\'')
2627 {
2628 pos.col -= 3;
2629 break;
2630 }
2631 }
2632 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002633 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
2635 if (linep[pos.col + 1] == '\\' &&
2636 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2637 {
2638 pos.col += 3;
2639 break;
2640 }
2641 else if (linep[pos.col + 2] == '\'')
2642 {
2643 pos.col += 2;
2644 break;
2645 }
2646 }
2647 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002648 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649
2650 default:
2651#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002652 /*
2653 * For Lisp skip over backslashed (), {} and [].
2654 * (actually, we skip #\( et al)
2655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 if (curbuf->b_p_lisp
2657 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002658 && pos.col > 1
2659 && check_prevcol(linep, pos.col, '\\', NULL)
2660 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 break;
2662#endif
2663
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002664 // Check for match outside of quotes, and inside of
2665 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 if ((!inquote || start_in_quotes == TRUE)
2667 && (c == initc || c == findc))
2668 {
2669 int col, bslcnt = 0;
2670
2671 if (!cpo_bsl)
2672 {
2673 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2674 bslcnt++;
2675 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002676 // Only accept a match when 'M' is in 'cpo' or when escaping
2677 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2679 {
2680 if (c == initc)
2681 count++;
2682 else
2683 {
2684 if (count == 0)
2685 return &pos;
2686 count--;
2687 }
2688 }
2689 }
2690 }
2691 }
2692
2693 if (comment_dir == BACKWARD && count > 0)
2694 {
2695 pos = match_pos;
2696 return &pos;
2697 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002698 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699}
2700
2701/*
2702 * Check if line[] contains a / / comment.
2703 * Return MAXCOL if not, otherwise return the column.
2704 * TODO: skip strings.
2705 */
2706 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002707check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
2709 char_u *p;
2710
2711 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002712#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002713 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002714 if (curbuf->b_p_lisp)
2715 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002716 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002717 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002718 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002719
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002720 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002721 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002722 {
2723 if (*p == '"')
2724 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002725 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002726 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002727 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002728 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002729 }
2730 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002731 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002732 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002733 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002734 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002735 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002736 || (*(p - 1) != '\\' && *(p - 2) != '#')))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002737 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002738 ++p;
2739 }
2740 }
2741 else
2742 p = NULL;
2743 }
2744 else
2745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 while ((p = vim_strchr(p, '/')) != NULL)
2747 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002748 // accept a double /, unless it's preceded with * and followed by *,
2749 // because * / / * is an end and start of a C comment
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002750 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 break;
2752 ++p;
2753 }
2754
2755 if (p == NULL)
2756 return MAXCOL;
2757 return (int)(p - line);
2758}
2759
2760/*
2761 * Move cursor briefly to character matching the one under the cursor.
2762 * Used for Insert mode and "r" command.
2763 * Show the match only if it is visible on the screen.
2764 * If there isn't a match, then beep.
2765 */
2766 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002767showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002768 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 pos_T *lpos, save_cursor;
2771 pos_T mpos;
2772 colnr_T vcol;
2773 long save_so;
2774 long save_siso;
2775#ifdef CURSOR_SHAPE
2776 int save_state;
2777#endif
2778 colnr_T save_dollar_vcol;
2779 char_u *p;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002780 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2781 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782
2783 /*
2784 * Only show match for chars in the 'matchpairs' option.
2785 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002786 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002787 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 {
2789#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002790 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002791 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002792#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002793 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002794 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795#ifdef FEAT_RIGHTLEFT
2796 && !(curwin->w_p_rl ^ p_ri)
2797#endif
2798 )
2799 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002800 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002801 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return;
2803 }
2804
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002805 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar165bc692015-07-21 17:53:25 +02002806 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002807 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
2809 if (!curwin->w_p_wrap)
2810 getvcol(curwin, lpos, NULL, &vcol, NULL);
2811 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002812 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002814 mpos = *lpos; // save the pos, update_screen() may change it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002816 save_so = *so;
2817 save_siso = *siso;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002818 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2819 // stop displaying the "$".
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002820 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2821 dollar_vcol = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002822 ++curwin->w_virtcol; // do display ')' just before "$"
2823 update_screen(VALID); // show the new char first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824
2825 save_dollar_vcol = dollar_vcol;
2826#ifdef CURSOR_SHAPE
2827 save_state = State;
2828 State = SHOWMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002829 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002831 curwin->w_cursor = mpos; // move to matching char
2832 *so = 0; // don't use 'scrolloff' here
2833 *siso = 0; // don't use 'sidescrolloff' here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 showruler(FALSE);
2835 setcursor();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002836 cursor_on(); // make sure that the cursor is shown
Bram Moolenaara338adc2018-01-31 20:51:47 +01002837 out_flush_cursor(TRUE, FALSE);
2838
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002839 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2840 // which resets it if the matching position is in a previous line
2841 // and has a higher column number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 dollar_vcol = save_dollar_vcol;
2843
2844 /*
2845 * brief pause, unless 'm' is present in 'cpo' and a character is
2846 * available.
2847 */
2848 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002849 ui_delay(p_mat * 100L + 8, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 else if (!char_avail())
Bram Moolenaareda1da02019-11-17 17:06:33 +01002851 ui_delay(p_mat * 100L + 9, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002852 curwin->w_cursor = save_cursor; // restore cursor position
Bram Moolenaar375e3392019-01-31 18:26:10 +01002853 *so = save_so;
2854 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855#ifdef CURSOR_SHAPE
2856 State = save_state;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002857 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858#endif
2859 }
2860 }
2861}
2862
2863/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002864 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002865 * If move is TRUE, check from the beginning of the buffer, else from position
2866 * "cur".
2867 * "direction" is FORWARD or BACKWARD.
2868 * Returns TRUE, FALSE or -1 for failure.
2869 */
2870 static int
2871is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2872{
2873 regmmatch_T regmatch;
2874 int nmatched = 0;
2875 int result = -1;
2876 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002877 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002878 int flag = 0;
2879
2880 if (pattern == NULL)
2881 pattern = spats[last_idx].pat;
2882
2883 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
2884 SEARCH_KEEP, &regmatch) == FAIL)
2885 return -1;
2886
2887 // init startcol correctly
2888 regmatch.startpos[0].col = -1;
2889 // move to match
2890 if (move)
2891 {
2892 CLEAR_POS(&pos);
2893 }
2894 else
2895 {
2896 pos = *cur;
2897 // accept a match at the cursor position
2898 flag = SEARCH_START;
2899 }
2900
2901 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2902 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2903 {
2904 // Zero-width pattern should match somewhere, then we can check if
2905 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002906 do
2907 {
2908 regmatch.startpos[0].col++;
2909 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
2910 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
2911 if (nmatched != 0)
2912 break;
2913 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
2914 : regmatch.startpos[0].col > pos.col);
2915
Bram Moolenaar53989552019-12-23 22:59:18 +01002916 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002917 {
2918 result = (nmatched != 0
2919 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2920 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2921 }
2922 }
2923
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002924 vim_regfree(regmatch.regprog);
2925 return result;
2926}
2927
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002928
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002929/*
2930 * Find next search match under cursor, cursor at end.
2931 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002932 */
2933 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002934current_search(
2935 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002936 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002937{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002938 pos_T start_pos; // start position of the pattern match
2939 pos_T end_pos; // end position of the pattern match
2940 pos_T orig_pos; // position of the cursor at beginning
2941 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002942 int i;
2943 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002944 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002945 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002946 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002947 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002948 int zero_width;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002949
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002950 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002951 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002952 dec_cursor();
2953
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002954 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002955 if (VIsual_active)
2956 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002957 if (forward)
2958 incl(&pos);
2959 else
2960 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002961 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002962
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002963 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002964 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02002965 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002966 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002967 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002968
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002969 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002970 * The trick is to first search backwards and then search forward again,
2971 * so that a match at the current cursor position will be correctly
2972 * captured.
2973 */
2974 for (i = 0; i < 2; i++)
2975 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002976 if (forward)
2977 dir = i;
2978 else
2979 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002980
2981 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002982 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002983 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002984 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002985
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002986 // wrapping should not occur in the first round
2987 if (i == 0)
2988 p_ws = FALSE;
2989
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002990 result = searchit(curwin, curbuf, &pos, &end_pos,
2991 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002992 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002993 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002994
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002995 p_ws = old_p_ws;
2996
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002997 // First search may fail, but then start searching from the
2998 // beginning of the file (cursor might be on the search match)
2999 // except when Visual mode is active, so that extending the visual
3000 // selection works.
3001 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003002 {
3003 curwin->w_cursor = orig_pos;
3004 if (VIsual_active)
3005 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003006 return FAIL;
3007 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003008 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003009 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003010 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003011 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003012 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003013 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003014 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003015 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003016 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003017 // try again from end of buffer
3018 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003019 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003020 pos.col = (colnr_T)STRLEN(
3021 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003022 }
3023 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003024 }
3025
3026 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003027
3028 if (!VIsual_active)
3029 VIsual = start_pos;
3030
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003031 // put cursor on last character of match
3032 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003033 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003034 dec_cursor();
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003035 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
3036 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003037 VIsual_active = TRUE;
3038 VIsual_mode = 'v';
3039
Bram Moolenaarb7633612019-02-10 21:48:25 +01003040 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003041 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003042 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003043 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3044 inc_cursor();
3045 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3046 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003047 }
3048
3049#ifdef FEAT_FOLDING
3050 if (fdo_flags & FDO_SEARCH && KeyTyped)
3051 foldOpenCursor();
3052#endif
3053
3054 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003055 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003056#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003057 // Make sure the clipboard gets updated. Needed because start and
3058 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003059 clip_star.vmode = NUL;
3060#endif
3061 redraw_curbuf_later(INVERTED);
3062 showmode();
3063
3064 return OK;
3065}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
3068 || defined(PROTO)
3069/*
3070 * return TRUE if line 'lnum' is empty or has white chars only.
3071 */
3072 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003073linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
3075 char_u *p;
3076
3077 p = skipwhite(ml_get(lnum));
3078 return (*p == NUL);
3079}
3080#endif
3081
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003082/*
3083 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003084 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003085 */
3086 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003087cmdline_search_stat(
3088 int dirc,
3089 pos_T *pos,
3090 pos_T *cursor_pos,
3091 int show_top_bot_msg,
3092 char_u *msgbuf,
3093 int recompute,
3094 int maxcount,
3095 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003096{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003097 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003098
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003099 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3100 timeout);
3101 if (stat.cur > 0)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003102 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003103 char t[SEARCH_STAT_BUF_LEN];
Bram Moolenaare2ad8262019-05-24 13:22:22 +02003104 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003105
3106#ifdef FEAT_RIGHTLEFT
3107 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3108 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003109 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003110 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003111 else if (stat.cnt > maxcount && stat.cur > maxcount)
3112 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3113 maxcount, maxcount);
3114 else if (stat.cnt > maxcount)
3115 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3116 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003117 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003118 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3119 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003120 }
3121 else
3122#endif
3123 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003124 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003125 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003126 else if (stat.cnt > maxcount && stat.cur > maxcount)
3127 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3128 maxcount, maxcount);
3129 else if (stat.cnt > maxcount)
3130 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3131 stat.cur, maxcount);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003132 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003133 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3134 stat.cur, stat.cnt);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003135 }
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003136
3137 len = STRLEN(t);
Bram Moolenaardc6855a2019-05-18 19:26:29 +02003138 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003139 {
Bram Moolenaar16b58ae2019-09-06 20:40:21 +02003140 mch_memmove(t + 2, t, len);
3141 t[0] = 'W';
3142 t[1] = ' ';
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003143 len += 2;
3144 }
3145
3146 mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003147 if (dirc == '?' && stat.cur == maxcount + 1)
3148 stat.cur = -1;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003149
Bram Moolenaar984f0312019-05-24 13:11:47 +02003150 // keep the message even after redraw, but don't put in history
3151 msg_hist_off = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003152 give_warning(msgbuf, FALSE);
Bram Moolenaar984f0312019-05-24 13:11:47 +02003153 msg_hist_off = FALSE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003154 }
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003155}
3156
3157/*
3158 * Add the search count information to "stat".
3159 * "stat" must not be NULL.
3160 * When "recompute" is TRUE always recompute the numbers.
3161 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3162 * dirc == '/': find the next match
3163 * dirc == '?': find the previous match
3164 */
3165 static void
3166update_search_stat(
3167 int dirc,
3168 pos_T *pos,
3169 pos_T *cursor_pos,
3170 searchstat_T *stat,
3171 int recompute,
3172 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003173 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003174{
3175 int save_ws = p_ws;
3176 int wraparound = FALSE;
3177 pos_T p = (*pos);
3178 static pos_T lastpos = {0, 0, 0};
3179 static int cur = 0;
3180 static int cnt = 0;
3181 static int exact_match = FALSE;
3182 static int incomplete = 0;
3183 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3184 static int chgtick = 0;
3185 static char_u *lastpat = NULL;
3186 static buf_T *lbuf = NULL;
3187#ifdef FEAT_RELTIME
3188 proftime_T start;
3189#endif
3190
3191 vim_memset(stat, 0, sizeof(searchstat_T));
3192
3193 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3194 {
3195 stat->cur = cur;
3196 stat->cnt = cnt;
3197 stat->exact_match = exact_match;
3198 stat->incomplete = incomplete;
3199 stat->last_maxcount = last_maxcount;
3200 return;
3201 }
3202 last_maxcount = maxcount;
3203
3204 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3205 || (dirc == '/' && LT_POS(p, lastpos)));
3206
3207 // If anything relevant changed the count has to be recomputed.
3208 // MB_STRNICMP ignores case, but we should not ignore case.
3209 // Unfortunately, there is no MB_STRNICMP function.
3210 // XXX: above comment should be "no MB_STRCMP function" ?
3211 if (!(chgtick == CHANGEDTICK(curbuf)
3212 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3213 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3214 && EQUAL_POS(lastpos, *cursor_pos)
3215 && lbuf == curbuf) || wraparound || cur < 0
3216 || (maxcount > 0 && cur > maxcount) || recompute)
3217 {
3218 cur = 0;
3219 cnt = 0;
3220 exact_match = FALSE;
3221 incomplete = 0;
3222 CLEAR_POS(&lastpos);
3223 lbuf = curbuf;
3224 }
3225
3226 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
3227 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 0))
3228 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3229 else
3230 {
3231 int done_search = FALSE;
3232 pos_T endpos = {0, 0, 0};
3233
3234 p_ws = FALSE;
3235#ifdef FEAT_RELTIME
3236 if (timeout > 0)
3237 profile_setlimit(timeout, &start);
3238#endif
3239 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3240 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3241 {
3242 done_search = TRUE;
3243#ifdef FEAT_RELTIME
3244 // Stop after passing the time limit.
3245 if (timeout > 0 && profile_passed_limit(&start))
3246 {
3247 incomplete = 1;
3248 break;
3249 }
3250#endif
3251 cnt++;
3252 if (LTOREQ_POS(lastpos, p))
3253 {
3254 cur = cnt;
3255 if (LTOREQ_POS(p, endpos))
3256 exact_match = TRUE;
3257 }
3258 fast_breakcheck();
3259 if (maxcount > 0 && cnt > maxcount)
3260 {
3261 incomplete = 2; // max count exceeded
3262 break;
3263 }
3264 }
3265 if (got_int)
3266 cur = -1; // abort
3267 if (done_search)
3268 {
3269 vim_free(lastpat);
3270 lastpat = vim_strsave(spats[last_idx].pat);
3271 chgtick = CHANGEDTICK(curbuf);
3272 lbuf = curbuf;
3273 lastpos = p;
3274 }
3275 }
3276 stat->cur = cur;
3277 stat->cnt = cnt;
3278 stat->exact_match = exact_match;
3279 stat->incomplete = incomplete;
3280 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003281 p_ws = save_ws;
3282}
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284#if defined(FEAT_FIND_ID) || defined(PROTO)
3285/*
3286 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01003287 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003290find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003291 char_u *ptr, // pointer to search pattern
3292 int dir UNUSED, // direction of expansion
3293 int len, // length of search pattern
3294 int whole, // match whole words only
3295 int skip_comments, // don't match inside comments
3296 int type, // Type of search; are we looking for a type?
3297 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003298 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003299 int action, // What to do when we find it
3300 linenr_T start_lnum, // first line to start searching
3301 linenr_T end_lnum) // last line for searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003303 SearchedFile *files; // Stack of included files
3304 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 int max_path_depth = 50;
3306 long match_count = 1;
3307
3308 char_u *pat;
3309 char_u *new_fname;
3310 char_u *curr_fname = curbuf->b_fname;
3311 char_u *prev_fname = NULL;
3312 linenr_T lnum;
3313 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003314 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 int old_files;
3316 int already_searched;
3317 char_u *file_line;
3318 char_u *line;
3319 char_u *p;
3320 char_u save_char;
3321 int define_matched;
3322 regmatch_T regmatch;
3323 regmatch_T incl_regmatch;
3324 regmatch_T def_regmatch;
3325 int matched = FALSE;
3326 int did_show = FALSE;
3327 int found = FALSE;
3328 int i;
3329 char_u *already = NULL;
3330 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003331 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003332#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 win_T *curwin_save = NULL;
3334#endif
3335
3336 regmatch.regprog = NULL;
3337 incl_regmatch.regprog = NULL;
3338 def_regmatch.regprog = NULL;
3339
3340 file_line = alloc(LSIZE);
3341 if (file_line == NULL)
3342 return;
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (type != CHECK_PATH && type != FIND_DEFINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003345 // when CONT_SOL is set compare "ptr" with the beginning of the line
3346 // is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003347 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
3349 pat = alloc(len + 5);
3350 if (pat == NULL)
3351 goto fpip_end;
3352 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003353 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 regmatch.rm_ic = ignorecase(pat);
3355 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
3356 vim_free(pat);
3357 if (regmatch.regprog == NULL)
3358 goto fpip_end;
3359 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003360 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3361 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003363 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (incl_regmatch.regprog == NULL)
3365 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003366 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 }
3368 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3369 {
3370 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
3371 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
3372 if (def_regmatch.regprog == NULL)
3373 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003374 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003376 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 if (files == NULL)
3378 goto fpip_end;
3379 old_files = max_path_depth;
3380 depth = depth_displayed = -1;
3381
3382 lnum = start_lnum;
3383 if (end_lnum > curbuf->b_ml.ml_line_count)
3384 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003385 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 lnum = end_lnum;
3387 line = ml_get(lnum);
3388
3389 for (;;)
3390 {
3391 if (incl_regmatch.regprog != NULL
3392 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3393 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003394 char_u *p_fname = (curr_fname == curbuf->b_fname)
3395 ? curbuf->b_ffname : curr_fname;
3396
3397 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003398 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003399 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003400 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003401 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3402 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003403 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003404 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003405 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 already_searched = FALSE;
3407 if (new_fname != NULL)
3408 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003409 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 for (i = 0;; i++)
3411 {
3412 if (i == depth + 1)
3413 i = old_files;
3414 if (i == max_path_depth)
3415 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003416 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3417 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
3419 if (type != CHECK_PATH &&
3420 action == ACTION_SHOW_ALL && files[i].matched)
3421 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003422 msg_putchar('\n'); // cursor below last one
3423 if (!got_int) // don't display if 'q'
3424 // typed at "--more--"
3425 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003428 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 prev_fname = NULL;
3430 }
3431 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003432 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 already_searched = TRUE;
3434 break;
3435 }
3436 }
3437 }
3438
3439 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3440 || (new_fname == NULL && !already_searched)))
3441 {
3442 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003443 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 else
3445 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003446 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003447 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003449 msg_puts_title(_("not found "));
3450 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452 did_show = TRUE;
3453 while (depth_displayed < depth && !got_int)
3454 {
3455 ++depth_displayed;
3456 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003457 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003459 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003461 if (!got_int) // don't display if 'q' typed
3462 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003465 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (new_fname != NULL)
3467 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003468 // using "new_fname" is more reliable, e.g., when
3469 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003470 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
3472 else
3473 {
3474 /*
3475 * Isolate the file name.
3476 * Include the surrounding "" or <> if present.
3477 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003478 if (inc_opt != NULL
3479 && strstr((char *)inc_opt, "\\zs") != NULL)
3480 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003481 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003482 p = incl_regmatch.startp[0];
3483 i = (int)(incl_regmatch.endp[0]
3484 - incl_regmatch.startp[0]);
3485 }
3486 else
3487 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003488 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003489 for (p = incl_regmatch.endp[0];
3490 *p && !vim_isfilec(*p); p++)
3491 ;
3492 for (i = 0; vim_isfilec(p[i]); i++)
3493 ;
3494 }
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 if (i == 0)
3497 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003498 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003500 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003502 // Avoid checking before the start of the line, can
3503 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003504 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 if (p[-1] == '"' || p[-1] == '<')
3507 {
3508 --p;
3509 ++i;
3510 }
3511 if (p[i] == '"' || p[i] == '>')
3512 ++i;
3513 }
3514 save_char = p[i];
3515 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003516 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 p[i] = save_char;
3518 }
3519
3520 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3521 {
3522 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003523 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003525 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003528 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
3530
3531 if (new_fname != NULL)
3532 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003533 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 if (depth + 1 == old_files)
3535 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003536 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (bigger != NULL)
3538 {
3539 for (i = 0; i <= depth; i++)
3540 bigger[i] = files[i];
3541 for (i = depth + 1; i < old_files + max_path_depth; i++)
3542 {
3543 bigger[i].fp = NULL;
3544 bigger[i].name = NULL;
3545 bigger[i].lnum = 0;
3546 bigger[i].matched = FALSE;
3547 }
3548 for (i = old_files; i < max_path_depth; i++)
3549 bigger[i + max_path_depth] = files[i];
3550 old_files += max_path_depth;
3551 max_path_depth *= 2;
3552 vim_free(files);
3553 files = bigger;
3554 }
3555 }
3556 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3557 == NULL)
3558 vim_free(new_fname);
3559 else
3560 {
3561 if (++depth == old_files)
3562 {
3563 /*
3564 * lalloc() for 'bigger' must have failed above. We
3565 * will forget one of our already visited files now.
3566 */
3567 vim_free(files[old_files].name);
3568 ++old_files;
3569 }
3570 files[depth].name = curr_fname = new_fname;
3571 files[depth].lnum = 0;
3572 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (action == ACTION_EXPAND)
3574 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003575 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003576 vim_snprintf((char*)IObuff, IOSIZE,
3577 _("Scanning included file: %s"),
3578 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003579 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003581 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003582 {
3583 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003584 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003585 (char *)new_fname);
3586 verbose_leave();
3587 }
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590 }
3591 }
3592 else
3593 {
3594 /*
3595 * Check if the line is a define (type == FIND_DEFINE)
3596 */
3597 p = line;
3598search_line:
3599 define_matched = FALSE;
3600 if (def_regmatch.regprog != NULL
3601 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3602 {
3603 /*
3604 * Pattern must be first identifier after 'define', so skip
3605 * to that position before checking for match of pattern. Also
3606 * don't let it match beyond the end of this identifier.
3607 */
3608 p = def_regmatch.endp[0];
3609 while (*p && !vim_iswordc(*p))
3610 p++;
3611 define_matched = TRUE;
3612 }
3613
3614 /*
3615 * Look for a match. Don't do this if we are looking for a
3616 * define and this line didn't match define_prog above.
3617 */
3618 if (def_regmatch.regprog == NULL || define_matched)
3619 {
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003620 if (define_matched || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003622 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 startp = skipwhite(p);
3624 if (p_ic)
3625 matched = !MB_STRNICMP(startp, ptr, len);
3626 else
3627 matched = !STRNCMP(startp, ptr, len);
3628 if (matched && define_matched && whole
3629 && vim_iswordc(startp[len]))
3630 matched = FALSE;
3631 }
3632 else if (regmatch.regprog != NULL
3633 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3634 {
3635 matched = TRUE;
3636 startp = regmatch.startp[0];
3637 /*
3638 * Check if the line is not a comment line (unless we are
3639 * looking for a define). A line starting with "# define"
3640 * is not considered to be a comment line.
3641 */
3642 if (!define_matched && skip_comments)
3643 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if ((*line != '#' ||
3645 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003646 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 matched = FALSE;
3648
3649 /*
3650 * Also check for a "/ *" or "/ /" before the match.
3651 * Skips lines like "int backwards; / * normal index
3652 * * /" when looking for "normal".
3653 * Note: Doesn't skip "/ *" in comments.
3654 */
3655 p = skipwhite(line);
3656 if (matched
3657 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 for (p = line; *p && p < startp; ++p)
3659 {
3660 if (matched
3661 && p[0] == '/'
3662 && (p[1] == '*' || p[1] == '/'))
3663 {
3664 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003665 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (p[1] == '/')
3667 break;
3668 ++p;
3669 }
3670 else if (!matched && p[0] == '*' && p[1] == '/')
3671 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003672 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 matched = TRUE;
3674 ++p;
3675 }
3676 }
3677 }
3678 }
3679 }
3680 }
3681 if (matched)
3682 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 if (action == ACTION_EXPAND)
3684 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003685 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 int add_r;
3687 char_u *aux;
3688
3689 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3690 break;
3691 found = TRUE;
3692 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003693 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003695 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (vim_iswordp(p))
3697 goto exit_matched;
3698 p = find_word_start(p);
3699 }
3700 p = find_word_end(p);
3701 i = (int)(p - aux);
3702
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003703 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003705 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003707
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003708 // Get the next line: when "depth" < 0 from the current
3709 // buffer, otherwise from the included file. Jump to
3710 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003711 if (depth < 0)
3712 {
3713 if (lnum >= end_lnum)
3714 goto exit_matched;
3715 line = ml_get(++lnum);
3716 }
3717 else if (vim_fgets(line = file_line,
3718 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 goto exit_matched;
3720
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003721 // we read a line, set "already" to check this "line" later
3722 // if depth >= 0 we'll increase files[depth].lnum far
3723 // bellow -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 already = aux = p = skipwhite(line);
3725 p = find_word_start(p);
3726 p = find_word_end(p);
3727 if (p > aux)
3728 {
3729 if (*aux != ')' && IObuff[i-1] != TAB)
3730 {
3731 if (IObuff[i-1] != ' ')
3732 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003733 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (p_js
3735 && (IObuff[i-2] == '.'
3736 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3737 && (IObuff[i-2] == '?'
3738 || IObuff[i-2] == '!'))))
3739 IObuff[i++] = ' ';
3740 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003741 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 if (p - aux >= IOSIZE - i)
3743 p = aux + IOSIZE - i - 1;
3744 STRNCPY(IObuff + i, aux, p - aux);
3745 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003746 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748 IObuff[i] = NUL;
3749 aux = IObuff;
3750
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003751 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 goto exit_matched;
3753 }
3754
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003755 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003757 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003759 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003761 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 break;
3763 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003764 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
3766 found = TRUE;
3767 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003768 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 if (curr_fname != prev_fname)
3770 {
3771 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003772 msg_putchar('\n'); // cursor below last one
3773 if (!got_int) // don't display if 'q' typed
3774 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 msg_home_replace_hl(curr_fname);
3776 prev_fname = curr_fname;
3777 }
3778 did_show = TRUE;
3779 if (!got_int)
3780 show_pat_in_path(line, type, TRUE, action,
3781 (depth == -1) ? NULL : files[depth].fp,
3782 (depth == -1) ? &lnum : &files[depth].lnum,
3783 match_count++);
3784
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003785 // Set matched flag for this file and all the ones that
3786 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 for (i = 0; i <= depth; ++i)
3788 files[i].matched = TRUE;
3789 }
3790 else if (--count <= 0)
3791 {
3792 found = TRUE;
3793 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003794#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 && g_do_tagpreview == 0
3796#endif
3797 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003798 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 else if (action == ACTION_SHOW)
3800 {
3801 show_pat_in_path(line, type, did_show, action,
3802 (depth == -1) ? NULL : files[depth].fp,
3803 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3804 did_show = TRUE;
3805 }
3806 else
3807 {
3808#ifdef FEAT_GUI
3809 need_mouse_correct = TRUE;
3810#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003811#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003812 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (g_do_tagpreview != 0)
3814 {
3815 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003816 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818#endif
3819 if (action == ACTION_SPLIT)
3820 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003823 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 }
3825 if (depth == -1)
3826 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003827 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003828#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (g_do_tagpreview != 0)
3830 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003831 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003832 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003833 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003834 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836 else
3837#endif
3838 setpcmark();
3839 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003840 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 else
3843 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003844 if (!GETFILE_SUCCESS(getfile(
3845 0, files[depth].name, NULL, TRUE,
3846 files[depth].lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003847 break; // failed to jump to file
3848 // autocommands may have changed the lnum, we don't
3849 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 curwin->w_cursor.lnum = files[depth].lnum;
3851 }
3852 }
3853 if (action != ACTION_SHOW)
3854 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003855 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 curwin->w_set_curswant = TRUE;
3857 }
3858
Bram Moolenaar4033c552017-09-16 20:54:51 +02003859#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003861 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003863 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 validate_cursor();
3865 redraw_later(VALID);
3866 win_enter(curwin_save, TRUE);
3867 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003868# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003869 else if (WIN_IS_POPUP(curwin))
3870 // can't keep focus in popup window
3871 win_enter(firstwin, TRUE);
3872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873#endif
3874 break;
3875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003878 // look for other matches in the rest of the line if we
3879 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882 && !(compl_cont_status & CONT_SOL)
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003883 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003884 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 goto search_line;
3886 }
3887 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003889 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003890 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 break;
3892
3893 /*
3894 * Read the next line. When reading an included file and encountering
3895 * end-of-file, close the file and continue in the file that included
3896 * it.
3897 */
3898 while (depth >= 0 && !already
3899 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3900 {
3901 fclose(files[depth].fp);
3902 --old_files;
3903 files[old_files].name = files[depth].name;
3904 files[old_files].matched = files[depth].matched;
3905 --depth;
3906 curr_fname = (depth == -1) ? curbuf->b_fname
3907 : files[depth].name;
3908 if (depth < depth_displayed)
3909 depth_displayed = depth;
3910 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003911 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003914 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003915 i = (int)STRLEN(line);
3916 if (i > 0 && line[i - 1] == '\n')
3917 line[--i] = NUL;
3918 if (i > 0 && line[i - 1] == '\r')
3919 line[--i] = NUL;
3920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 else if (!already)
3922 {
3923 if (++lnum > end_lnum)
3924 break;
3925 line = ml_get(lnum);
3926 }
3927 already = NULL;
3928 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003929 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003931 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 for (i = 0; i <= depth; i++)
3933 {
3934 fclose(files[i].fp);
3935 vim_free(files[i].name);
3936 }
3937 for (i = old_files; i < max_path_depth; i++)
3938 vim_free(files[i].name);
3939 vim_free(files);
3940
3941 if (type == CHECK_PATH)
3942 {
3943 if (!did_show)
3944 {
3945 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003946 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003948 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
3950 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003951 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003953 if (got_int || ins_compl_interrupted())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003954 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003958 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
3961 msg_end();
3962
3963fpip_end:
3964 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02003965 vim_regfree(regmatch.regprog);
3966 vim_regfree(incl_regmatch.regprog);
3967 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968}
3969
3970 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003971show_pat_in_path(
3972 char_u *line,
3973 int type,
3974 int did_show,
3975 int action,
3976 FILE *fp,
3977 linenr_T *lnum,
3978 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979{
3980 char_u *p;
3981
3982 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003983 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00003984 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003985 gotocmdline(TRUE); // cursor at status line
3986 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 return;
3988 for (;;)
3989 {
3990 p = line + STRLEN(line) - 1;
3991 if (fp != NULL)
3992 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003993 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (p >= line && *p == '\n')
3995 --p;
3996 if (p >= line && *p == '\r')
3997 --p;
3998 *(p + 1) = NUL;
3999 }
4000 if (action == ACTION_SHOW_ALL)
4001 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004002 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004003 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004004 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4005 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004006 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4007 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004009 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004010 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004012 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4014 break;
4015
4016 if (fp != NULL)
4017 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004018 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 break;
4020 ++*lnum;
4021 }
4022 else
4023 {
4024 if (++*lnum > curbuf->b_ml.ml_line_count)
4025 break;
4026 line = ml_get(*lnum);
4027 }
4028 msg_putchar('\n');
4029 }
4030}
4031#endif
4032
4033#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004034/*
4035 * Return the last used search pattern at "idx".
4036 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004037 spat_T *
4038get_spat(int idx)
4039{
4040 return &spats[idx];
4041}
4042
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004043/*
4044 * Return the last used search pattern index.
4045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004047get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004049 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004052
4053#ifdef FEAT_EVAL
4054/*
4055 * "searchcount()" function
4056 */
4057 void
4058f_searchcount(typval_T *argvars, typval_T *rettv)
4059{
4060 pos_T pos = curwin->w_cursor;
4061 char_u *pattern = NULL;
4062 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4063 long timeout = SEARCH_STAT_DEF_TIMEOUT;
4064 int recompute = TRUE;
4065 searchstat_T stat;
4066
4067 if (rettv_dict_alloc(rettv) == FAIL)
4068 return;
4069
4070 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4071 recompute = TRUE;
4072
4073 if (argvars[0].v_type != VAR_UNKNOWN)
4074 {
4075 dict_T *dict = argvars[0].vval.v_dict;
4076 dictitem_T *di;
4077 listitem_T *li;
4078 int error = FALSE;
4079
4080 di = dict_find(dict, (char_u *)"timeout", -1);
4081 if (di != NULL)
4082 {
4083 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4084 if (error)
4085 return;
4086 }
4087 di = dict_find(dict, (char_u *)"maxcount", -1);
4088 if (di != NULL)
4089 {
4090 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4091 if (error)
4092 return;
4093 }
4094 di = dict_find(dict, (char_u *)"recompute", -1);
4095 if (di != NULL)
4096 {
4097 recompute = tv_get_number_chk(&di->di_tv, &error);
4098 if (error)
4099 return;
4100 }
4101 di = dict_find(dict, (char_u *)"pattern", -1);
4102 if (di != NULL)
4103 {
4104 pattern = tv_get_string_chk(&di->di_tv);
4105 if (pattern == NULL)
4106 return;
4107 }
4108 di = dict_find(dict, (char_u *)"pos", -1);
4109 if (di != NULL)
4110 {
4111 if (di->di_tv.v_type != VAR_LIST)
4112 {
4113 semsg(_(e_invarg2), "pos");
4114 return;
4115 }
4116 if (list_len(di->di_tv.vval.v_list) != 3)
4117 {
4118 semsg(_(e_invarg2), "List format should be [lnum, col, off]");
4119 return;
4120 }
4121 li = list_find(di->di_tv.vval.v_list, 0L);
4122 if (li != NULL)
4123 {
4124 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4125 if (error)
4126 return;
4127 }
4128 li = list_find(di->di_tv.vval.v_list, 1L);
4129 if (li != NULL)
4130 {
4131 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
4132 if (error)
4133 return;
4134 }
4135 li = list_find(di->di_tv.vval.v_list, 2L);
4136 if (li != NULL)
4137 {
4138 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
4139 if (error)
4140 return;
4141 }
4142 }
4143 }
4144
4145 save_last_search_pattern();
4146 if (pattern != NULL)
4147 {
4148 if (*pattern == NUL)
4149 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004150 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004151 spats[last_idx].pat = vim_strsave(pattern);
4152 }
4153 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4154 goto the_end; // the previous pattern was never defined
4155
4156 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4157
4158 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4159 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4160 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4161 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4162 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4163
4164the_end:
4165 restore_last_search_pattern();
4166}
4167#endif