blob: c1da58b461ab641ae40a8c7619da4dce5c516fa1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9/*
10 * search.c: code for normal mode searching commands
11 */
12
13#include "vim.h"
14
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010016static void set_vv_searchforward(void);
17static int first_submatch(regmmatch_T *rp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static int check_linecomment(char_u *line);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020#ifdef FEAT_FIND_ID
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010021static void show_pat_in_path(char_u *, int,
22 int, int, FILE *, linenr_T *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020024
25typedef struct searchstat
26{
27 int cur; // current position of found words
28 int cnt; // total count of found words
29 int exact_match; // TRUE if matched exactly on specified position
30 int incomplete; // 0: search was fully completed
31 // 1: recomputing was timed out
32 // 2: max count exceeded
33 int last_maxcount; // the max count of the last search
34} searchstat_T;
35
36static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, int recompute, int maxcount, long timeout);
37static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout);
38
Bram Moolenaarea6561a2020-06-01 21:32:45 +020039#define SEARCH_STAT_DEF_TIMEOUT 40L
Bram Moolenaare8f5ec02020-06-01 17:28:35 +020040#define SEARCH_STAT_DEF_MAX_COUNT 99
41#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +000042
Bram Moolenaar071d4272004-06-13 20:20:40 +000043/*
44 * This file contains various searching-related routines. These fall into
45 * three groups:
46 * 1. string searches (for /, ?, n, and N)
47 * 2. character searches within a single line (for f, F, t, T, etc)
48 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
49 */
50
51/*
52 * String searches
53 *
54 * The string search functions are divided into two levels:
55 * lowest: searchit(); uses an pos_T for starting position and found match.
56 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
57 *
58 * The last search pattern is remembered for repeating the same search.
59 * This pattern is shared between the :g, :s, ? and / commands.
60 * This is in search_regcomp().
61 *
62 * The actual string matching is done using a heavily modified version of
63 * Henry Spencer's regular expression library. See regexp.c.
64 */
65
Bram Moolenaar071d4272004-06-13 20:20:40 +000066/*
67 * Two search patterns are remembered: One for the :substitute command and
68 * one for other searches. last_idx points to the one that was used the last
69 * time.
70 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020071static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000072{
Bram Moolenaar63d9e732019-12-05 21:10:38 +010073 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat
74 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat
Bram Moolenaar071d4272004-06-13 20:20:40 +000075};
76
Bram Moolenaar63d9e732019-12-05 21:10:38 +010077static int last_idx = 0; // index in spats[] for RE_LAST
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar63d9e732019-12-05 21:10:38 +010079static char_u lastc[2] = {NUL, NUL}; // last character searched for
80static int lastcdir = FORWARD; // last direction of character search
81static int last_t_cmd = TRUE; // last search t_cmd
Bram Moolenaardbd24b52015-08-11 14:26:19 +020082static char_u lastc_bytes[MB_MAXBYTES + 1];
Bram Moolenaar63d9e732019-12-05 21:10:38 +010083static int lastc_bytelen = 1; // >1 for multi-byte char
Bram Moolenaardbd24b52015-08-11 14:26:19 +020084
Bram Moolenaar63d9e732019-12-05 21:10:38 +010085// copy of spats[], for keeping the search patterns while executing autocmds
Bram Moolenaarc3328162019-07-23 22:15:25 +020086static spat_T saved_spats[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000087# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +010088static int saved_spats_last_idx = 0;
89static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
Bram Moolenaar63d9e732019-12-05 21:10:38 +010092static char_u *mr_pattern = NULL; // pattern used by search_regcomp()
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +010094static int mr_pattern_alloced = FALSE; // mr_pattern was allocated
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
96
97#ifdef FEAT_FIND_ID
98/*
99 * Type used by find_pattern_in_path() to remember which included files have
100 * been searched already.
101 */
102typedef struct SearchedFile
103{
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100104 FILE *fp; // File pointer
105 char_u *name; // Full name of file
106 linenr_T lnum; // Line we were up to in file
107 int matched; // Found a match in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108} SearchedFile;
109#endif
110
111/*
112 * translate search pattern for vim_regcomp()
113 *
114 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
115 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
116 * pat_save == RE_BOTH: save pat in both patterns (:global command)
117 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000118 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
120 * options & SEARCH_HIS: put search string in history
121 * options & SEARCH_KEEP: keep previous search pattern
122 *
123 * returns FAIL if failed, OK otherwise.
124 */
125 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100126search_regcomp(
127 char_u *pat,
128 int pat_save,
129 int pat_use,
130 int options,
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100131 regmmatch_T *regmatch) // return: pattern and ignore-case flag
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132{
133 int magic;
134 int i;
135
136 rc_did_emsg = FALSE;
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 Moolenaar442a8532020-06-04 20:56:09 +0200359 if (++did_save_last_search_spat != 1)
360 // nested call, nothing to do
361 return;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100362
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100363 saved_last_search_spat = spats[RE_SEARCH];
364 if (spats[RE_SEARCH].pat != NULL)
365 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
366 saved_last_idx = last_idx;
367 saved_no_hlsearch = no_hlsearch;
368}
369
370 void
371restore_last_search_pattern(void)
372{
Bram Moolenaar442a8532020-06-04 20:56:09 +0200373 if (--did_save_last_search_spat > 0)
374 // nested call, nothing to do
375 return;
376 if (did_save_last_search_spat != 0)
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100377 {
Bram Moolenaar442a8532020-06-04 20:56:09 +0200378 iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100379 return;
380 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100381
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100382 vim_free(spats[RE_SEARCH].pat);
383 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100384 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100385# if defined(FEAT_EVAL)
386 set_vv_searchforward();
387# endif
388 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200389 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100390}
Bram Moolenaard0480092017-11-16 22:20:39 +0100391
392 char_u *
393last_search_pattern(void)
394{
395 return spats[RE_SEARCH].pat;
396}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100397#endif
398
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399/*
400 * Return TRUE when case should be ignored for search pattern "pat".
401 * Uses the 'ignorecase' and 'smartcase' options.
402 */
403 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100404ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200406 return ignorecase_opt(pat, p_ic, p_scs);
407}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200409/*
410 * As ignorecase() put pass the "ic" and "scs" flags.
411 */
412 int
413ignorecase_opt(char_u *pat, int ic_in, int scs)
414{
415 int ic = ic_in;
416
417 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200418 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200419 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 no_smartcase = FALSE;
421
422 return ic;
423}
424
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200425/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200426 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200427 */
428 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100429pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200430{
431 char_u *p = pat;
432
433 while (*p != NUL)
434 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200435 int l;
436
437 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
438 {
439 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
440 return TRUE;
441 p += l;
442 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100443 else if (*p == '\\')
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200444 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100445 if (p[1] == '_' && p[2] != NUL) // skip "\_X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200446 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100447 else if (p[1] == '%' && p[2] != NUL) // skip "\%X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200448 p += 3;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100449 else if (p[1] != NUL) // skip "\X"
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200450 p += 2;
451 else
452 p += 1;
453 }
454 else if (MB_ISUPPER(*p))
455 return TRUE;
456 else
457 ++p;
458 }
459 return FALSE;
460}
461
Bram Moolenaar113e1072019-01-20 15:30:40 +0100462#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100464last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200465{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200466 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200467}
468
469 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100470last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200471{
472 return lastcdir == FORWARD;
473}
474
475 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100476last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200477{
478 return last_t_cmd == TRUE;
479}
480
481 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100482set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200483{
484 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200485 lastc_bytelen = len;
486 if (len)
487 memcpy(lastc_bytes, s, len);
488 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200489 CLEAR_FIELD(lastc_bytes);
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200490}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100491#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200492
493 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100494set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200495{
496 lastcdir = cdir;
497}
498
499 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100500set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200501{
502 last_t_cmd = t_cmd;
503}
504
505 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100506last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
508 return spats[last_idx].pat;
509}
510
511/*
512 * Reset search direction to forward. For "gd" and "gD" commands.
513 */
514 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100515reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000518#if defined(FEAT_EVAL)
519 set_vv_searchforward();
520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521}
522
523#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
524/*
525 * Set the last search pattern. For ":let @/ =" and viminfo.
526 * Also set the saved search pattern, so that this works in an autocommand.
527 */
528 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100529set_last_search_pat(
530 char_u *s,
531 int idx,
532 int magic,
533 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535 vim_free(spats[idx].pat);
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100536 // An empty string means that nothing should be matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 if (*s == NUL)
538 spats[idx].pat = NULL;
539 else
540 spats[idx].pat = vim_strsave(s);
541 spats[idx].magic = magic;
542 spats[idx].no_scs = FALSE;
543 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000544#if defined(FEAT_EVAL)
545 set_vv_searchforward();
546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 spats[idx].off.line = FALSE;
548 spats[idx].off.end = FALSE;
549 spats[idx].off.off = 0;
550 if (setlast)
551 last_idx = idx;
552 if (save_level)
553 {
554 vim_free(saved_spats[idx].pat);
555 saved_spats[idx] = spats[0];
556 if (spats[idx].pat == NULL)
557 saved_spats[idx].pat = NULL;
558 else
559 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100560# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100561 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100562# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
564# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100565 // If 'hlsearch' set and search pat changed: need redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000567 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568# endif
569}
570#endif
571
572#ifdef FEAT_SEARCH_EXTRA
573/*
574 * Get a regexp program for the last used search pattern.
575 * This is used for highlighting all matches in a window.
576 * Values returned in regmatch->regprog and regmatch->rmm_ic.
577 */
578 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100579last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
581 if (spats[last_idx].pat == NULL)
582 {
583 regmatch->regprog = NULL;
584 return;
585 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100586 ++emsg_off; // So it doesn't beep if bad expr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
588 --emsg_off;
589}
590#endif
591
592/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100593 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100594 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
595 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 *
597 * if (options & SEARCH_MSG) == 0 don't give any messages
598 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
599 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
600 * if (options & SEARCH_HIS) put search pattern in history
601 * if (options & SEARCH_END) return position at end of match
602 * if (options & SEARCH_START) accept match at pos itself
603 * if (options & SEARCH_KEEP) keep previous search pattern
604 * if (options & SEARCH_FOLD) match only once in a closed fold
605 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100606 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 *
608 * Return FAIL (zero) for failure, non-zero for success.
609 * When FEAT_EVAL is defined, returns the index of the first matching
610 * subpattern plus one; one if there was none.
611 */
612 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100613searchit(
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200614 win_T *win, // window to search in; can be NULL for a
615 // buffer without a window!
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100616 buf_T *buf,
617 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100618 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100619 int dir,
620 char_u *pat,
621 long count,
622 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200623 int pat_use, // which pattern to use when "pat" is empty
624 searchit_arg_T *extra_arg) // optional extra arguments, can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 int found;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100627 linenr_T lnum; // no init to shut up Apollo cc
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100628 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 regmmatch_T regmatch;
630 char_u *ptr;
631 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000633 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int loop;
635 pos_T start_pos;
636 int at_first_line;
637 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200638 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 int match_ok;
640 long nmatched;
641 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100642 int first_match = TRUE;
Bram Moolenaar53989552019-12-23 22:59:18 +0100643 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_SEARCH_EXTRA
645 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#endif
Bram Moolenaar92ea26b2019-10-18 20:53:34 +0200647 linenr_T stop_lnum = 0; // stop after this line number when != 0
648#ifdef FEAT_RELTIME
649 proftime_T *tm = NULL; // timeout limit or NULL
650 int *timed_out = NULL; // set when timed out or NULL
651#endif
652
653 if (extra_arg != NULL)
654 {
655 stop_lnum = extra_arg->sa_stop_lnum;
656#ifdef FEAT_RELTIME
657 tm = extra_arg->sa_tm;
658 timed_out = &extra_arg->sa_timed_out;
659#endif
660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662 if (search_regcomp(pat, RE_SEARCH, pat_use,
663 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
664 {
665 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100666 semsg(_("E383: Invalid search string: %s"), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 return FAIL;
668 }
669
Bram Moolenaar280f1262006-01-30 00:14:18 +0000670 /*
671 * find the string
672 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100673 do // loop for count
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100675 // When not accepting a match at the start position set "extra_col" to
676 // a non-zero value. Don't do that when starting at MAXCOL, since
677 // MAXCOL + 1 is zero.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200678 if (pos->col == MAXCOL)
679 start_char_len = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100680 // Watch out for the "col" being MAXCOL - 2, used in a closed fold.
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200681 else if (has_mbyte
682 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
683 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100684 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100685 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100686 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200687 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100688 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100689 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100690 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100691 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200692 start_char_len = 1;
693 if (dir == FORWARD)
694 {
695 if (options & SEARCH_START)
696 extra_col = 0;
697 else
698 extra_col = start_char_len;
699 }
700 else
701 {
702 if (options & SEARCH_START)
703 extra_col = start_char_len;
704 else
705 extra_col = 0;
706 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100707
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100708 start_pos = *pos; // remember start pos for detecting no match
709 found = 0; // default: not found
710 at_first_line = TRUE; // default: start in first line
711 if (pos->lnum == 0) // correct lnum for when starting in line 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 pos->lnum = 1;
714 pos->col = 0;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100715 at_first_line = FALSE; // not in first line now
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717
718 /*
719 * Start searching in current line, unless searching backwards and
720 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000721 * If we are searching backwards, in column 0, and not including the
722 * current position, gain some efficiency by skipping back a line.
723 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000725 if (dir == BACKWARD && start_pos.col == 0
726 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
728 lnum = pos->lnum - 1;
729 at_first_line = FALSE;
730 }
731 else
732 lnum = pos->lnum;
733
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100734 for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
736 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
737 lnum += dir, at_first_line = FALSE)
738 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100739 // Stop after checking "stop_lnum", if it's set.
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000740 if (stop_lnum != 0 && (dir == FORWARD
741 ? lnum > stop_lnum : lnum < stop_lnum))
742 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000743#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100744 // Stop after passing the "tm" time limit.
Bram Moolenaar76929292008-01-06 19:07:36 +0000745 if (tm != NULL && profile_passed_limit(tm))
746 break;
747#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000750 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100752 col = at_first_line && (options & SEARCH_COL) ? pos->col
753 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100755 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000756#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200757 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000758#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200759 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000760#endif
761 );
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100762 // Abort searching on an error (e.g., out of stack).
Bram Moolenaar53989552019-12-23 22:59:18 +0100763 if (called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200764#ifdef FEAT_RELTIME
765 || (timed_out != NULL && *timed_out)
766#endif
767 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 break;
769 if (nmatched > 0)
770 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100771 // match may actually be in another line when using \zs
Bram Moolenaar677ee682005-01-27 14:41:15 +0000772 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000774#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000776#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100777 // "lnum" may be past end of buffer for "\n\zs".
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000778 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
779 ptr = (char_u *)"";
780 else
781 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
783 /*
784 * Forward search in the first line: match should be after
785 * the start position. If not, continue at the end of the
786 * match (this is vi compatible) or on the next char.
787 */
788 if (dir == FORWARD && at_first_line)
789 {
790 match_ok = TRUE;
791 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000792 * When the match starts in a next line it's certainly
793 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * When match lands on a NUL the cursor will be put
795 * one back afterwards, compare with that position,
796 * otherwise "/$" will get stuck on end of line.
797 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000798 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100799 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000800 ? (nmatched == 1
801 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000803 : ((int)matchpos.col
804 - (ptr[matchpos.col] == NUL)
805 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
807 /*
808 * If vi-compatible searching, continue at the end
809 * of the match, otherwise continue one position
810 * forward.
811 */
812 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
813 {
814 if (nmatched > 1)
815 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100816 // end is in next line, thus no match in
817 // this line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 match_ok = FALSE;
819 break;
820 }
821 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100822 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000823 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 && ptr[matchcol] != NUL)
825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (has_mbyte)
827 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000828 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++matchcol;
831 }
832 }
833 else
834 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000835 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (ptr[matchcol] != NUL)
837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000839 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 + matchcol);
841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 ++matchcol;
843 }
844 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200845 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (ptr[matchcol] == NUL
848 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000849 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000850 matchcol,
851#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200852 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000853#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200854 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000855#endif
856 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
858 match_ok = FALSE;
859 break;
860 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000861 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 endpos = regmatch.endpos[0];
863# ifdef FEAT_EVAL
864 submatch = first_submatch(&regmatch);
865# endif
866
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100867 // Need to get the line pointer again, a
868 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000869 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 if (!match_ok)
872 continue;
873 }
874 if (dir == BACKWARD)
875 {
876 /*
877 * Now, if there are multiple matches on this line,
878 * we have to get the last one. Or the last one before
879 * the cursor, if we're on that line.
880 * When putting the new cursor at the end, compare
881 * relative to the end of the match.
882 */
883 match_ok = FALSE;
884 for (;;)
885 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100886 // Remember a position that is before the start
887 // position, we use it if it's the last match in
888 // the line. Always accept a position after
889 // wrapping around.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000890 if (loop
891 || ((options & SEARCH_END)
892 ? (lnum + regmatch.endpos[0].lnum
893 < start_pos.lnum
894 || (lnum + regmatch.endpos[0].lnum
895 == start_pos.lnum
896 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200897 < (int)start_pos.col
898 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000899 : (lnum + regmatch.startpos[0].lnum
900 < start_pos.lnum
901 || (lnum + regmatch.startpos[0].lnum
902 == start_pos.lnum
903 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200904 < (int)start_pos.col
905 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000908 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 endpos = regmatch.endpos[0];
910# ifdef FEAT_EVAL
911 submatch = first_submatch(&regmatch);
912# endif
913 }
914 else
915 break;
916
917 /*
918 * We found a valid match, now check if there is
919 * another one after it.
920 * If vi-compatible searching, continue at the end
921 * of the match, otherwise continue one position
922 * forward.
923 */
924 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
925 {
926 if (nmatched > 1)
927 break;
928 matchcol = endpos.col;
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100929 // for empty match: advance one char
Bram Moolenaar677ee682005-01-27 14:41:15 +0000930 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 && ptr[matchcol] != NUL)
932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (has_mbyte)
934 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000935 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 ++matchcol;
938 }
939 }
940 else
941 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100942 // Stop when the match is in a next line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000943 if (matchpos.lnum > 0)
944 break;
945 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (ptr[matchcol] != NUL)
947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (has_mbyte)
949 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000950 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 ++matchcol;
953 }
954 }
955 if (ptr[matchcol] == NUL
956 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000957 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000958 matchcol,
959#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200960 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000961#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200962 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000963#endif
964 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100965 {
966#ifdef FEAT_RELTIME
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100967 // If the search timed out, we did find a match
968 // but it might be the wrong one, so that's not
969 // OK.
Bram Moolenaar9d322762018-02-09 16:04:25 +0100970 if (timed_out != NULL && *timed_out)
971 match_ok = FALSE;
972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100976 // Need to get the line pointer again, a
977 // multi-line search may have made it invalid.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000978 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980
981 /*
982 * If there is only a match after the cursor, skip
983 * this match.
984 */
985 if (!match_ok)
986 continue;
987 }
988
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100989 // With the SEARCH_END option move to the last character
990 // of the match. Don't do it for an empty match, end
991 // should be same as start then.
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200992 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000993 && !(matchpos.lnum == endpos.lnum
994 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +0100996 // For a match in the first column, set the position
997 // on the NUL in the previous line.
Bram Moolenaar677ee682005-01-27 14:41:15 +0000998 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000999 pos->col = endpos.col;
1000 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001001 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001002 if (pos->lnum > 1) // just in case
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001003 {
1004 --pos->lnum;
1005 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1006 pos->lnum, FALSE));
1007 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001008 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001009 else
1010 {
1011 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001012 if (has_mbyte
1013 && pos->lnum <= buf->b_ml.ml_line_count)
1014 {
1015 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1016 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1017 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001018 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001019 if (end_pos != NULL)
1020 {
1021 end_pos->lnum = lnum + matchpos.lnum;
1022 end_pos->col = matchpos.col;
1023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025 else
1026 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001027 pos->lnum = lnum + matchpos.lnum;
1028 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001029 if (end_pos != NULL)
1030 {
1031 end_pos->lnum = lnum + endpos.lnum;
1032 end_pos->col = endpos.col;
1033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001036 if (end_pos != NULL)
1037 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001039 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001041 // Set variables used for 'incsearch' highlighting.
Bram Moolenaar677ee682005-01-27 14:41:15 +00001042 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 search_match_endcol = endpos.col;
1044 break;
1045 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001046 line_breakcheck(); // stop if ctrl-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (got_int)
1048 break;
1049
1050#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001051 // Cancel searching if a character was typed. Used for
1052 // 'incsearch'. Don't check too often, that would slowdown
1053 // searching too much.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if ((options & SEARCH_PEEK)
1055 && ((lnum - pos->lnum) & 0x3f) == 0
1056 && char_avail())
1057 {
1058 break_loop = TRUE;
1059 break;
1060 }
1061#endif
1062
1063 if (loop && lnum == start_pos.lnum)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001064 break; // if second loop, stop where started
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 at_first_line = FALSE;
1067
1068 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001069 * Stop the search if wrapscan isn't set, "stop_lnum" is
1070 * specified, after an interrupt, after a match and after looping
1071 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 */
Bram Moolenaar53989552019-12-23 22:59:18 +01001073 if (!p_ws || stop_lnum != 0 || got_int
1074 || called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001075#ifdef FEAT_RELTIME
1076 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001077#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001078#ifdef FEAT_SEARCH_EXTRA
1079 || break_loop
1080#endif
1081 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 break;
1083
1084 /*
1085 * If 'wrapscan' is set we continue at the other end of the file.
1086 * If 'shortmess' does not contain 's', we give a message.
1087 * This message is also remembered in keep_msg for when the screen
1088 * is redrawn. The keep_msg is cleared whenever another message is
1089 * written.
1090 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001091 if (dir == BACKWARD) // start second loop at the other end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001095 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1096 give_warning((char_u *)_(dir == BACKWARD
1097 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001098 if (extra_arg != NULL)
1099 extra_arg->sa_wrapped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
Bram Moolenaar53989552019-12-23 22:59:18 +01001101 if (got_int || called_emsg > called_emsg_before
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001102#ifdef FEAT_RELTIME
1103 || (timed_out != NULL && *timed_out)
1104#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001105#ifdef FEAT_SEARCH_EXTRA
1106 || break_loop
1107#endif
1108 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 break;
1110 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001111 while (--count > 0 && found); // stop after count matches or no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
Bram Moolenaar473de612013-06-08 18:19:48 +02001113 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001115 if (!found) // did not find it
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001118 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1120 {
1121 if (p_ws)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001122 semsg(_(e_patnotf2), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else if (lnum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001124 semsg(_("E384: search hit TOP without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 mr_pattern);
1126 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001127 semsg(_("E385: search hit BOTTOM without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 mr_pattern);
1129 }
1130 return FAIL;
1131 }
1132
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001133 // A pattern like "\n\zs" may go past the last line.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001134 if (pos->lnum > buf->b_ml.ml_line_count)
1135 {
1136 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001137 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001138 if (pos->col > 0)
1139 --pos->col;
1140 }
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 return submatch + 1;
1143}
1144
1145#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001146 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001147set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001148{
1149 spats[0].off.dir = cdir;
1150}
1151
1152 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001153set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001154{
1155 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1156}
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158/*
1159 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001160 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 */
1162 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001163first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
1165 int submatch;
1166
1167 for (submatch = 1; ; ++submatch)
1168 {
1169 if (rp->startpos[submatch].lnum >= 0)
1170 break;
1171 if (submatch == 9)
1172 {
1173 submatch = 0;
1174 break;
1175 }
1176 }
1177 return submatch;
1178}
1179#endif
1180
1181/*
1182 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001183 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 * If 'dirc' is 0: use previous dir.
1185 * If 'pat' is NULL or empty : use previous string.
1186 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1187 * If 'options & SEARCH_ECHO': echo the search command and handle options
1188 * If 'options & SEARCH_MSG' : may give error message
1189 * If 'options & SEARCH_OPT' : interpret optional flags
1190 * If 'options & SEARCH_HIS' : put search pattern in history
1191 * If 'options & SEARCH_NOOF': don't add offset to position
1192 * If 'options & SEARCH_MARK': set previous context mark
1193 * If 'options & SEARCH_KEEP': keep previous search pattern
1194 * If 'options & SEARCH_START': accept match at curpos itself
1195 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1196 *
1197 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1198 * makes the movement linewise without moving the match position.
1199 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001200 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 */
1202 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001203do_search(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001204 oparg_T *oap, // can be NULL
1205 int dirc, // '/' or '?'
Bram Moolenaarc036e872020-02-21 21:30:52 +01001206 int search_delim, // the delimiter for the search, e.g. '%' in s%regex%replacement%
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001207 char_u *pat,
1208 long count,
1209 int options,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001210 searchit_arg_T *sia) // optional arguments or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001212 pos_T pos; // position of the last match
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001214 soffset_T old_off;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001215 int retval; // Return value
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 char_u *p;
1217 long c;
1218 char_u *dircp;
1219 char_u *strcopy = NULL;
1220 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001221 char_u *msgbuf = NULL;
1222 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001223 int has_offset = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
1225 /*
1226 * A line offset is not remembered, this is vi compatible.
1227 */
1228 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1229 {
1230 spats[0].off.line = FALSE;
1231 spats[0].off.off = 0;
1232 }
1233
1234 /*
1235 * Save the values for when (options & SEARCH_KEEP) is used.
1236 * (there is no "if ()" around this because gcc wants them initialized)
1237 */
1238 old_off = spats[0].off;
1239
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001240 pos = curwin->w_cursor; // start searching at the cursor position
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242 /*
1243 * Find out the direction of the search.
1244 */
1245 if (dirc == 0)
1246 dirc = spats[0].off.dir;
1247 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001250#if defined(FEAT_EVAL)
1251 set_vv_searchforward();
1252#endif
1253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (options & SEARCH_REV)
1255 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001256#ifdef MSWIN
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001257 // There is a bug in the Visual C++ 2.2 compiler which means that
1258 // dirc always ends up being '/'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 dirc = (dirc == '/') ? '?' : '/';
1260#else
1261 if (dirc == '/')
1262 dirc = '?';
1263 else
1264 dirc = '/';
1265#endif
1266 }
1267
1268#ifdef FEAT_FOLDING
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001269 // If the cursor is in a closed fold, don't find another match in the same
1270 // fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (dirc == '/')
1272 {
1273 if (hasFolding(pos.lnum, NULL, &pos.lnum))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001274 pos.col = MAXCOL - 2; // avoid overflow when adding 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276 else
1277 {
1278 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1279 pos.col = 0;
1280 }
1281#endif
1282
1283#ifdef FEAT_SEARCH_EXTRA
1284 /*
1285 * Turn 'hlsearch' highlighting back on.
1286 */
1287 if (no_hlsearch && !(options & SEARCH_KEEP))
1288 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001289 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001290 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292#endif
1293
1294 /*
1295 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1296 */
1297 for (;;)
1298 {
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001299 int show_top_bot_msg = FALSE;
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001300
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 searchstr = pat;
1302 dircp = NULL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001303 // use previous pattern
Bram Moolenaarc036e872020-02-21 21:30:52 +01001304 if (pat == NULL || *pat == NUL || *pat == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001306 if (spats[RE_SEARCH].pat == NULL) // no previous pattern
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001308 searchstr = spats[RE_SUBST].pat;
1309 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001311 emsg(_(e_noprevre));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001312 retval = 0;
1313 goto end_do_search;
1314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001316 else
1317 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001318 // make search_regcomp() use spats[RE_SEARCH].pat
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001319 searchstr = (char_u *)"";
1320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
1322
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001323 if (pat != NULL && *pat != NUL) // look for (new) offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 {
1325 /*
1326 * Find end of regular expression.
1327 * If there is a matching '/' or '?', toss it.
1328 */
1329 ps = strcopy;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02001330 p = skip_regexp_ex(pat, search_delim, (int)p_magic, &strcopy, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 if (strcopy != ps)
1332 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001333 // made a copy of "pat" to change "\?" to "?"
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001334 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 pat = strcopy;
1336 searchstr = strcopy;
1337 }
Bram Moolenaarc036e872020-02-21 21:30:52 +01001338 if (*p == search_delim)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001340 dircp = p; // remember where we put the NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 *p++ = NUL;
1342 }
1343 spats[0].off.line = FALSE;
1344 spats[0].off.end = FALSE;
1345 spats[0].off.off = 0;
1346 /*
1347 * Check for a line offset or a character offset.
1348 * For get_address (echo off) we don't check for a character
1349 * offset, because it is meaningless and the 's' could be a
1350 * substitute command.
1351 */
1352 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1353 spats[0].off.line = TRUE;
1354 else if ((options & SEARCH_OPT) &&
1355 (*p == 'e' || *p == 's' || *p == 'b'))
1356 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001357 if (*p == 'e') // end
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 spats[0].off.end = SEARCH_END;
1359 ++p;
1360 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001361 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001363 // 'nr' or '+nr' or '-nr'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1365 spats[0].off.off = atol((char *)p);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001366 else if (*p == '-') // single '-'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 spats[0].off.off = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001368 else // single '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 spats[0].off.off = 1;
1370 ++p;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001371 while (VIM_ISDIGIT(*p)) // skip number
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 ++p;
1373 }
1374
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001375 // compute length of search command for get_address()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 searchcmdlen += (int)(p - pat);
1377
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001378 pat = p; // put pat after search command
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 }
1380
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001381 if ((options & SEARCH_ECHO) && messaging() &&
1382 !msg_silent &&
1383 (!cmd_silent || !shortmess(SHM_SEARCHCOUNT)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001386 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001387 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001389 // Compute msg_row early.
1390 msg_start();
1391
Bram Moolenaar984f0312019-05-24 13:11:47 +02001392 // Get the offset, so we know how long it is.
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001393 if (!cmd_silent &&
1394 (spats[0].off.line || spats[0].off.end || spats[0].off.off))
Bram Moolenaar984f0312019-05-24 13:11:47 +02001395 {
1396 p = off_buf;
1397 *p++ = dirc;
1398 if (spats[0].off.end)
1399 *p++ = 'e';
1400 else if (!spats[0].off.line)
1401 *p++ = 's';
1402 if (spats[0].off.off > 0 || spats[0].off.line)
1403 *p++ = '+';
1404 *p = NUL;
1405 if (spats[0].off.off != 0 || spats[0].off.line)
1406 sprintf((char *)p, "%ld", spats[0].off.off);
1407 off_len = STRLEN(off_buf);
1408 }
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001411 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 else
1413 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001414
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001415 if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001416 {
1417 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001418 // search stat. Use all the space available, so that the
1419 // search state is right aligned. If there is not enough space
1420 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar19e8ac72019-09-03 22:23:38 +02001421 if (msg_scrolled != 0 && !cmd_silent)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001422 // Use all the columns.
1423 len = (int)(Rows - msg_row) * Columns - 1;
1424 else
1425 // Use up to 'showcmd' column.
1426 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001427 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1428 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001429 }
1430 else
1431 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001432 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001433
Bram Moolenaar880e4d92020-04-11 21:31:28 +02001434 vim_free(msgbuf);
Bram Moolenaar51e14382019-05-25 20:21:28 +02001435 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (msgbuf != NULL)
1437 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001438 vim_memset(msgbuf, ' ', len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001439 msgbuf[len - 1] = NUL;
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001440 // do not fill the msgbuf buffer, if cmd_silent is set, leave it
1441 // empty for the search_stat feature.
1442 if (!cmd_silent)
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001443 {
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001444 msgbuf[0] = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001446 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1447 {
1448 // Use a space to draw the composing char on.
1449 msgbuf[1] = ' ';
1450 mch_memmove(msgbuf + 2, p, STRLEN(p));
1451 }
1452 else
1453 mch_memmove(msgbuf + 1, p, STRLEN(p));
1454 if (off_len > 0)
1455 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001457 trunc = msg_strtrunc(msgbuf, TRUE);
1458 if (trunc != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001460 vim_free(msgbuf);
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001461 msgbuf = trunc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001464 #ifdef FEAT_RIGHTLEFT
1465 // The search pattern could be shown on the right in rightleft
1466 // mode, but the 'ruler' and 'showcmd' area use it too, thus
1467 // it would be blanked out again very soon. Show it on the
1468 // left, but do reverse the text.
1469 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1470 {
1471 char_u *r;
1472 size_t pat_len;
1473
1474 r = reverse_text(msgbuf);
1475 if (r != NULL)
1476 {
1477 vim_free(msgbuf);
1478 msgbuf = r;
1479 // move reversed text to beginning of buffer
1480 while (*r != NUL && *r == ' ')
1481 r++;
1482 pat_len = msgbuf + STRLEN(msgbuf) - r;
1483 mch_memmove(msgbuf, r, pat_len);
1484 // overwrite old text
1485 if ((size_t)(r - msgbuf) >= pat_len)
1486 vim_memset(r, ' ', pat_len);
1487 else
1488 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
1489 }
1490 }
1491 #endif
1492 msg_outtrans(msgbuf);
1493 msg_clr_eos();
1494 msg_check();
1495
1496 gotocmdline(FALSE);
1497 out_flush();
1498 msg_nowait = TRUE; // don't wait for this message
1499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 }
1502
1503 /*
1504 * If there is a character offset, subtract it from the current
1505 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001506 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * This is not done for a line offset, because then we would not be vi
1508 * compatible.
1509 */
Bram Moolenaared203462004-06-16 11:19:22 +00001510 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
1512 if (spats[0].off.off > 0)
1513 {
1514 for (c = spats[0].off.off; c; --c)
1515 if (decl(&pos) == -1)
1516 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001517 if (c) // at start of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001519 pos.lnum = 0; // allow lnum == 0 here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 pos.col = MAXCOL;
1521 }
1522 }
1523 else
1524 {
1525 for (c = spats[0].off.off; c; ++c)
1526 if (incl(&pos) == -1)
1527 break;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001528 if (c) // at end of buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1531 pos.col = 0;
1532 }
1533 }
1534 }
1535
Bram Moolenaar14184a32019-02-16 15:10:30 +01001536 c = searchit(curwin, curbuf, &pos, NULL,
1537 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 searchstr, count, spats[0].off.end + (options &
1539 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1540 + SEARCH_MSG + SEARCH_START
1541 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02001542 RE_LAST, sia);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 if (dircp != NULL)
Bram Moolenaarc036e872020-02-21 21:30:52 +01001545 *dircp = search_delim; // restore second '/' or '?' for normal_cmd()
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001546
1547 if (!shortmess(SHM_SEARCH)
1548 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1549 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001550 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 if (c == FAIL)
1553 {
1554 retval = 0;
1555 goto end_do_search;
1556 }
1557 if (spats[0].off.end && oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001558 oap->inclusive = TRUE; // 'e' includes last character
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001560 retval = 1; // pattern found
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
1562 /*
1563 * Add character and/or line offset
1564 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001565 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001567 pos_T org_pos = pos;
1568
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001569 if (spats[0].off.line) // Add the offset to the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 {
1571 c = pos.lnum + spats[0].off.off;
1572 if (c < 1)
1573 pos.lnum = 1;
1574 else if (c > curbuf->b_ml.ml_line_count)
1575 pos.lnum = curbuf->b_ml.ml_line_count;
1576 else
1577 pos.lnum = c;
1578 pos.col = 0;
1579
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001580 retval = 2; // pattern found, line offset added
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001582 else if (pos.col < MAXCOL - 2) // just in case
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001584 // to the right, check for end of file
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001585 c = spats[0].off.off;
1586 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (incl(&pos) == -1)
1590 break;
1591 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001592 // to the left, check for start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 else
1594 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001595 while (c++ < 0)
1596 if (decl(&pos) == -1)
1597 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001600 if (!EQUAL_POS(pos, org_pos))
1601 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001604 // Show [1/15] if 'S' is not in 'shortmess'.
1605 if ((options & SEARCH_ECHO)
1606 && messaging()
Bram Moolenaar359ad1a2019-09-02 21:44:59 +02001607 && !msg_silent
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001608 && c != FAIL
1609 && !shortmess(SHM_SEARCHCOUNT)
1610 && msgbuf != NULL)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001611 cmdline_search_stat(dirc, &pos, &curwin->w_cursor,
1612 show_top_bot_msg, msgbuf,
1613 (count != 1 || has_offset
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001614#ifdef FEAT_FOLDING
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001615 || (!(fdo_flags & FDO_SEARCH)
1616 && hasFolding(curwin->w_cursor.lnum,
1617 NULL, NULL))
Bram Moolenaar6cb07262020-05-29 22:49:43 +02001618#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02001619 ),
1620 SEARCH_STAT_DEF_MAX_COUNT,
1621 SEARCH_STAT_DEF_TIMEOUT);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 /*
1624 * The search command can be followed by a ';' to do another search.
1625 * For example: "/pat/;/foo/+3;?bar"
1626 * This is like doing another search command, except:
1627 * - The remembered direction '/' or '?' is from the first search.
1628 * - When an error happens the cursor isn't moved at all.
1629 * Don't do this when called by get_address() (it handles ';' itself).
1630 */
1631 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1632 break;
1633
1634 dirc = *++pat;
Bram Moolenaarc036e872020-02-21 21:30:52 +01001635 search_delim = dirc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 if (dirc != '?' && dirc != '/')
1637 {
1638 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 goto end_do_search;
1641 }
1642 ++pat;
1643 }
1644
1645 if (options & SEARCH_MARK)
1646 setpcmark();
1647 curwin->w_cursor = pos;
1648 curwin->w_set_curswant = TRUE;
1649
1650end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001651 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 spats[0].off = old_off;
1653 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001654 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
1656 return retval;
1657}
1658
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659/*
1660 * search_for_exact_line(buf, pos, dir, pat)
1661 *
1662 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001663 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001665 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 * Return OK for success, or FAIL if no line found.
1667 */
1668 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001669search_for_exact_line(
1670 buf_T *buf,
1671 pos_T *pos,
1672 int dir,
1673 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 linenr_T start = 0;
1676 char_u *ptr;
1677 char_u *p;
1678
1679 if (buf->b_ml.ml_line_count == 0)
1680 return FAIL;
1681 for (;;)
1682 {
1683 pos->lnum += dir;
1684 if (pos->lnum < 1)
1685 {
1686 if (p_ws)
1687 {
1688 pos->lnum = buf->b_ml.ml_line_count;
1689 if (!shortmess(SHM_SEARCH))
1690 give_warning((char_u *)_(top_bot_msg), TRUE);
1691 }
1692 else
1693 {
1694 pos->lnum = 1;
1695 break;
1696 }
1697 }
1698 else if (pos->lnum > buf->b_ml.ml_line_count)
1699 {
1700 if (p_ws)
1701 {
1702 pos->lnum = 1;
1703 if (!shortmess(SHM_SEARCH))
1704 give_warning((char_u *)_(bot_top_msg), TRUE);
1705 }
1706 else
1707 {
1708 pos->lnum = 1;
1709 break;
1710 }
1711 }
1712 if (pos->lnum == start)
1713 break;
1714 if (start == 0)
1715 start = pos->lnum;
1716 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1717 p = skipwhite(ptr);
1718 pos->col = (colnr_T) (p - ptr);
1719
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001720 // when adding lines the matching line may be empty but it is not
1721 // ignored because we are interested in the next line -- Acevedo
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001722 if ((compl_cont_status & CONT_ADDING)
1723 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 {
1725 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1726 return OK;
1727 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001728 else if (*p != NUL) // ignore empty lines
1729 { // expanding lines or words
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001730 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1731 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 return OK;
1733 }
1734 }
1735 return FAIL;
1736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
1738/*
1739 * Character Searches
1740 */
1741
1742/*
1743 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1744 * position of the character, otherwise move to just before the char.
1745 * Do this "cap->count1" times.
1746 * Return FAIL or OK.
1747 */
1748 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001749searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001751 int c = cap->nchar; // char to search for
1752 int dir = cap->arg; // TRUE for searching forward
1753 long count = cap->count1; // repeat count
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 int col;
1755 char_u *p;
1756 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001757 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001759 if (c != NUL) // normal search: remember args for repeat
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001761 if (!KeyStuffed) // don't remember when redoing
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001763 *lastc = c;
1764 set_csearch_direction(dir);
1765 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001766 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (cap->ncharC1 != 0)
1768 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001769 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1770 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001772 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1773 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 }
1776 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001777 else // repeat previous search
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001779 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 return FAIL;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001781 if (dir) // repeat in opposite direction
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 dir = -lastcdir;
1783 else
1784 dir = lastcdir;
1785 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001786 c = *lastc;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001787 // For multi-byte re-use last lastc_bytes[] and lastc_bytelen.
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001788
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001789 // Force a move of at least one char, so ";" and "," will move the
1790 // cursor, even if the cursor is right in front of char we are looking
1791 // at.
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001792 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001793 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 }
1795
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001796 if (dir == BACKWARD)
1797 cap->oap->inclusive = FALSE;
1798 else
1799 cap->oap->inclusive = TRUE;
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 p = ml_get_curline();
1802 col = curwin->w_cursor.col;
1803 len = (int)STRLEN(p);
1804
1805 while (count--)
1806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (has_mbyte)
1808 {
1809 for (;;)
1810 {
1811 if (dir > 0)
1812 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001813 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (col >= len)
1815 return FAIL;
1816 }
1817 else
1818 {
1819 if (col == 0)
1820 return FAIL;
1821 col -= (*mb_head_off)(p, p + col - 1) + 1;
1822 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001823 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001825 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 break;
1827 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001828 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001829 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001830 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001831 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833 }
1834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 for (;;)
1837 {
1838 if ((col += dir) < 0 || col >= len)
1839 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001840 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001842 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
1844 }
1845 }
1846
1847 if (t_cmd)
1848 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001849 // backup to before the character (possibly double-byte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (has_mbyte)
1852 {
1853 if (dir < 0)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001854 // Landed on the search char which is lastc_bytelen long
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001855 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01001857 // To previous char, which may be multi-byte.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 col -= (*mb_head_off)(p, p + col);
1859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 }
1861 curwin->w_cursor.col = col;
1862
1863 return OK;
1864}
1865
1866/*
1867 * "Other" Searches
1868 */
1869
1870/*
1871 * findmatch - find the matching paren or brace
1872 *
1873 * Improvement over vi: Braces inside quotes are ignored.
1874 */
1875 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001876findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
1878 return findmatchlimit(oap, initc, 0, 0);
1879}
1880
1881/*
1882 * Return TRUE if the character before "linep[col]" equals "ch".
1883 * Return FALSE if "col" is zero.
1884 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1885 * is NULL.
1886 * Handles multibyte string correctly.
1887 */
1888 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001889check_prevcol(
1890 char_u *linep,
1891 int col,
1892 int ch,
1893 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (col > 0 && has_mbyte)
1897 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (prevcol)
1899 *prevcol = col;
1900 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1901}
1902
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001903/*
1904 * Raw string start is found at linep[startpos.col - 1].
1905 * Return TRUE if the matching end can be found between startpos and endpos.
1906 */
1907 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001908find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001909{
1910 char_u *p;
1911 char_u *delim_copy;
1912 size_t delim_len;
1913 linenr_T lnum;
1914 int found = FALSE;
1915
1916 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1917 ;
1918 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar71ccd032020-06-12 22:59:11 +02001919 delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001920 if (delim_copy == NULL)
1921 return FALSE;
1922 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1923 {
1924 char_u *line = ml_get(lnum);
1925
1926 for (p = line + (lnum == startpos->lnum
1927 ? startpos->col + 1 : 0); *p; ++p)
1928 {
1929 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1930 break;
Bram Moolenaar282f9c62020-08-04 21:46:18 +02001931 if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0
1932 && p[delim_len + 1] == '"')
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001933 {
1934 found = TRUE;
1935 break;
1936 }
1937 }
1938 if (found)
1939 break;
1940 }
1941 vim_free(delim_copy);
1942 return found;
1943}
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945/*
Bram Moolenaar556ae8e2019-11-21 22:27:22 +01001946 * Check matchpairs option for "*initc".
1947 * If there is a match set "*initc" to the matching character and "*findc" to
1948 * the opposite character. Set "*backwards" to the direction.
1949 * When "switchit" is TRUE swap the direction.
1950 */
1951 static void
1952find_mps_values(
1953 int *initc,
1954 int *findc,
1955 int *backwards,
1956 int switchit)
1957{
1958 char_u *ptr;
1959
1960 ptr = curbuf->b_p_mps;
1961 while (*ptr != NUL)
1962 {
1963 if (has_mbyte)
1964 {
1965 char_u *prev;
1966
1967 if (mb_ptr2char(ptr) == *initc)
1968 {
1969 if (switchit)
1970 {
1971 *findc = *initc;
1972 *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1973 *backwards = TRUE;
1974 }
1975 else
1976 {
1977 *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1);
1978 *backwards = FALSE;
1979 }
1980 return;
1981 }
1982 prev = ptr;
1983 ptr += mb_ptr2len(ptr) + 1;
1984 if (mb_ptr2char(ptr) == *initc)
1985 {
1986 if (switchit)
1987 {
1988 *findc = *initc;
1989 *initc = mb_ptr2char(prev);
1990 *backwards = FALSE;
1991 }
1992 else
1993 {
1994 *findc = mb_ptr2char(prev);
1995 *backwards = TRUE;
1996 }
1997 return;
1998 }
1999 ptr += mb_ptr2len(ptr);
2000 }
2001 else
2002 {
2003 if (*ptr == *initc)
2004 {
2005 if (switchit)
2006 {
2007 *backwards = TRUE;
2008 *findc = *initc;
2009 *initc = ptr[2];
2010 }
2011 else
2012 {
2013 *backwards = FALSE;
2014 *findc = ptr[2];
2015 }
2016 return;
2017 }
2018 ptr += 2;
2019 if (*ptr == *initc)
2020 {
2021 if (switchit)
2022 {
2023 *backwards = FALSE;
2024 *findc = *initc;
2025 *initc = ptr[-2];
2026 }
2027 else
2028 {
2029 *backwards = TRUE;
2030 *findc = ptr[-2];
2031 }
2032 return;
2033 }
2034 ++ptr;
2035 }
2036 if (*ptr == ',')
2037 ++ptr;
2038 }
2039}
2040
2041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002043 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
2044 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 *
2046 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002047 * character at or after the cursor. Special values:
2048 * '*' look for C-style comment / *
2049 * '/' look for C-style comment / *, ignoring comment-end
2050 * '#' look for preprocessor directives
2051 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 *
2053 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
2054 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
2055 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
2056 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002057 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002058 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002059 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 */
2061
2062 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002063findmatchlimit(
2064 oparg_T *oap,
2065 int initc,
2066 int flags,
2067 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002069 static pos_T pos; // current search position
2070 int findc = 0; // matching brace
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 int c;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002072 int count = 0; // cumulative number of braces
2073 int backwards = FALSE; // init for gcc
2074 int raw_string = FALSE; // search for raw string
2075 int inquote = FALSE; // TRUE when inside quotes
2076 char_u *linep; // pointer to current line
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 char_u *ptr;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002078 int do_quotes; // check for quotes in current line
2079 int at_start; // do_quotes value at start position
2080 int hash_dir = 0; // Direction searched for # things
2081 int comment_dir = 0; // Direction searched for comments
2082 pos_T match_pos; // Where last slash-star was found
2083 int start_in_quotes; // start position is in quotes
2084 int traveled = 0; // how far we've searched so far
2085 int ignore_cend = FALSE; // ignore comment end
2086 int cpo_match; // vi compatible matching
2087 int cpo_bsl; // don't recognize backslashes
2088 int match_escaped = 0; // search for escaped match
2089 int dir; // Direction to search
2090 int comment_col = MAXCOL; // start of / / comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002091#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002092 int lispcomm = FALSE; // inside of Lisp-style comment
2093 int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
2096 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02002097 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 linep = ml_get(pos.lnum);
2099
2100 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
2101 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
2102
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002103 // Direction to search when initc is '/', '*' or '#'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if (flags & FM_BACKWARD)
2105 dir = BACKWARD;
2106 else if (flags & FM_FORWARD)
2107 dir = FORWARD;
2108 else
2109 dir = 0;
2110
2111 /*
2112 * if initc given, look in the table for the matching character
2113 * '/' and '*' are special cases: look for start or end of comment.
2114 * When '/' is used, we ignore running backwards into an star-slash, for
2115 * "[*" command, we just want to find any comment.
2116 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002117 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 comment_dir = dir;
2120 if (initc == '/')
2121 ignore_cend = TRUE;
2122 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002123 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 initc = NUL;
2125 }
2126 else if (initc != '#' && initc != NUL)
2127 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002128 find_mps_values(&initc, &findc, &backwards, TRUE);
2129 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 return NULL;
2131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 else
2133 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002134 /*
2135 * Either initc is '#', or no initc was given and we need to look
2136 * under the cursor.
2137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 if (initc == '#')
2139 {
2140 hash_dir = dir;
2141 }
2142 else
2143 {
2144 /*
2145 * initc was not given, must look for something to match under
2146 * or near the cursor.
2147 * Only check for special things when 'cpo' doesn't have '%'.
2148 */
2149 if (!cpo_match)
2150 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002151 // Are we before or at #if, #else etc.?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 ptr = skipwhite(linep);
2153 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2154 {
2155 ptr = skipwhite(ptr + 1);
2156 if ( STRNCMP(ptr, "if", 2) == 0
2157 || STRNCMP(ptr, "endif", 5) == 0
2158 || STRNCMP(ptr, "el", 2) == 0)
2159 hash_dir = 1;
2160 }
2161
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002162 // Are we on a comment?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 else if (linep[pos.col] == '/')
2164 {
2165 if (linep[pos.col + 1] == '*')
2166 {
2167 comment_dir = FORWARD;
2168 backwards = FALSE;
2169 pos.col++;
2170 }
2171 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2172 {
2173 comment_dir = BACKWARD;
2174 backwards = TRUE;
2175 pos.col--;
2176 }
2177 }
2178 else if (linep[pos.col] == '*')
2179 {
2180 if (linep[pos.col + 1] == '/')
2181 {
2182 comment_dir = BACKWARD;
2183 backwards = TRUE;
2184 }
2185 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2186 {
2187 comment_dir = FORWARD;
2188 backwards = FALSE;
2189 }
2190 }
2191 }
2192
2193 /*
2194 * If we are not on a comment or the # at the start of a line, then
2195 * look for brace anywhere on this line after the cursor.
2196 */
2197 if (!hash_dir && !comment_dir)
2198 {
2199 /*
2200 * Find the brace under or after the cursor.
2201 * If beyond the end of the line, use the last character in
2202 * the line.
2203 */
2204 if (linep[pos.col] == NUL && pos.col)
2205 --pos.col;
2206 for (;;)
2207 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002208 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (initc == NUL)
2210 break;
2211
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002212 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 if (findc)
2214 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002215 pos.col += mb_ptr2len(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217 if (!findc)
2218 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002219 // no brace in the line, maybe use " #if" then
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 if (!cpo_match && *skipwhite(linep) == '#')
2221 hash_dir = 1;
2222 else
2223 return NULL;
2224 }
2225 else if (!cpo_bsl)
2226 {
2227 int col, bslcnt = 0;
2228
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002229 // Set "match_escaped" if there are an odd number of
2230 // backslashes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2232 bslcnt++;
2233 match_escaped = (bslcnt & 1);
2234 }
2235 }
2236 }
2237 if (hash_dir)
2238 {
2239 /*
2240 * Look for matching #if, #else, #elif, or #endif
2241 */
2242 if (oap != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002243 oap->motion_type = MLINE; // Linewise for this case only
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (initc != '#')
2245 {
2246 ptr = skipwhite(skipwhite(linep) + 1);
2247 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2248 hash_dir = 1;
2249 else if (STRNCMP(ptr, "endif", 5) == 0)
2250 hash_dir = -1;
2251 else
2252 return NULL;
2253 }
2254 pos.col = 0;
2255 while (!got_int)
2256 {
2257 if (hash_dir > 0)
2258 {
2259 if (pos.lnum == curbuf->b_ml.ml_line_count)
2260 break;
2261 }
2262 else if (pos.lnum == 1)
2263 break;
2264 pos.lnum += hash_dir;
2265 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002266 line_breakcheck(); // check for CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 ptr = skipwhite(linep);
2268 if (*ptr != '#')
2269 continue;
2270 pos.col = (colnr_T) (ptr - linep);
2271 ptr = skipwhite(ptr + 1);
2272 if (hash_dir > 0)
2273 {
2274 if (STRNCMP(ptr, "if", 2) == 0)
2275 count++;
2276 else if (STRNCMP(ptr, "el", 2) == 0)
2277 {
2278 if (count == 0)
2279 return &pos;
2280 }
2281 else if (STRNCMP(ptr, "endif", 5) == 0)
2282 {
2283 if (count == 0)
2284 return &pos;
2285 count--;
2286 }
2287 }
2288 else
2289 {
2290 if (STRNCMP(ptr, "if", 2) == 0)
2291 {
2292 if (count == 0)
2293 return &pos;
2294 count--;
2295 }
2296 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2297 {
2298 if (count == 0)
2299 return &pos;
2300 }
2301 else if (STRNCMP(ptr, "endif", 5) == 0)
2302 count++;
2303 }
2304 }
2305 return NULL;
2306 }
2307 }
2308
2309#ifdef FEAT_RIGHTLEFT
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002310 // This is just guessing: when 'rightleft' is set, search for a matching
2311 // paren/brace in the other direction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2313 backwards = !backwards;
2314#endif
2315
2316 do_quotes = -1;
2317 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002318 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002319
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002320 // backward search: Check if this line contains a single-line comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002321 if ((backwards && comment_dir)
2322#ifdef FEAT_LISP
2323 || lisp
2324#endif
2325 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002327#ifdef FEAT_LISP
2328 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002329 lispcomm = TRUE; // find match inside this comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 while (!got_int)
2332 {
2333 /*
2334 * Go to the next position, forward or backward. We could use
2335 * inc() and dec() here, but that is much slower
2336 */
2337 if (backwards)
2338 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002339#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002340 // char to match is inside of comment, don't search outside
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002341 if (lispcomm && pos.col < (colnr_T)comment_col)
2342 break;
2343#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002344 if (pos.col == 0) // at start of line, go to prev. one
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002346 if (pos.lnum == 1) // start of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 break;
2348 --pos.lnum;
2349
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002350 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 break;
2352
2353 linep = ml_get(pos.lnum);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002354 pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 do_quotes = -1;
2356 line_breakcheck();
2357
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002358 // Check if this line contains a single-line comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002359 if (comment_dir
2360#ifdef FEAT_LISP
2361 || lisp
2362#endif
2363 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002365#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002366 // skip comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002367 if (lisp && comment_col != MAXCOL)
2368 pos.col = comment_col;
2369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371 else
2372 {
2373 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 if (has_mbyte)
2375 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002378 else // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002380 if (linep[pos.col] == NUL
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002381 // at end of line, go to next one
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002382#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002383 // don't search for match in comment
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002384 || (lisp && comment_col != MAXCOL
2385 && pos.col == (colnr_T)comment_col)
2386#endif
2387 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002389 if (pos.lnum == curbuf->b_ml.ml_line_count // end of file
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002390#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002391 // line is exhausted and comment with it,
2392 // don't search for match in code
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002393 || lispcomm
2394#endif
2395 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 break;
2397 ++pos.lnum;
2398
2399 if (maxtravel && traveled++ > maxtravel)
2400 break;
2401
2402 linep = ml_get(pos.lnum);
2403 pos.col = 0;
2404 do_quotes = -1;
2405 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002406#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002407 if (lisp) // find comment pos in new line
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002408 comment_col = check_linecomment(linep);
2409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411 else
2412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002414 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 ++pos.col;
2417 }
2418 }
2419
2420 /*
2421 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2422 */
2423 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2424 (linep[0] == '{' || linep[0] == '}'))
2425 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002426 if (linep[0] == findc && count == 0) // match!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 return &pos;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002428 break; // out of scope
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430
2431 if (comment_dir)
2432 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002433 // Note: comments do not nest, and we ignore quotes in them
2434 // TODO: ignore comment brackets inside strings
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 if (comment_dir == FORWARD)
2436 {
2437 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2438 {
2439 pos.col++;
2440 return &pos;
2441 }
2442 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002443 else // Searching backwards
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
2445 /*
2446 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002447 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 */
2449 if (pos.col == 0)
2450 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002451 else if (raw_string)
2452 {
2453 if (linep[pos.col - 1] == 'R'
2454 && linep[pos.col] == '"'
2455 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2456 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002457 // Possible start of raw string. Now that we have the
2458 // delimiter we can check if it ends before where we
2459 // started searching, or before the previously found
2460 // raw string start.
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002461 if (!find_rawstring_end(linep, &pos,
2462 count > 0 ? &match_pos : &curwin->w_cursor))
2463 {
2464 count++;
2465 match_pos = pos;
2466 match_pos.col--;
2467 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002468 linep = ml_get(pos.lnum); // may have been released
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002469 }
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 else if ( linep[pos.col - 1] == '/'
2472 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002473 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 && (int)pos.col < comment_col)
2475 {
2476 count++;
2477 match_pos = pos;
2478 match_pos.col--;
2479 }
2480 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2481 {
2482 if (count > 0)
2483 pos = match_pos;
2484 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2485 && (int)pos.col <= comment_col)
2486 pos.col -= 2;
2487 else if (ignore_cend)
2488 continue;
2489 else
2490 return NULL;
2491 return &pos;
2492 }
2493 }
2494 continue;
2495 }
2496
2497 /*
2498 * If smart matching ('cpoptions' does not contain '%'), braces inside
2499 * of quotes are ignored, but only if there is an even number of
2500 * quotes in the line.
2501 */
2502 if (cpo_match)
2503 do_quotes = 0;
2504 else if (do_quotes == -1)
2505 {
2506 /*
2507 * Count the number of quotes in the line, skipping \" and '"'.
2508 * Watch out for "\\".
2509 */
2510 at_start = do_quotes;
2511 for (ptr = linep; *ptr; ++ptr)
2512 {
2513 if (ptr == linep + pos.col + backwards)
2514 at_start = (do_quotes & 1);
2515 if (*ptr == '"'
2516 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2517 ++do_quotes;
2518 if (*ptr == '\\' && ptr[1] != NUL)
2519 ++ptr;
2520 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002521 do_quotes &= 1; // result is 1 with even number of quotes
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 /*
2524 * If we find an uneven count, check current line and previous
2525 * one for a '\' at the end.
2526 */
2527 if (!do_quotes)
2528 {
2529 inquote = FALSE;
2530 if (ptr[-1] == '\\')
2531 {
2532 do_quotes = 1;
2533 if (start_in_quotes == MAYBE)
2534 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002535 // Do we need to use at_start here?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 inquote = TRUE;
2537 start_in_quotes = TRUE;
2538 }
2539 else if (backwards)
2540 inquote = TRUE;
2541 }
2542 if (pos.lnum > 1)
2543 {
2544 ptr = ml_get(pos.lnum - 1);
2545 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2546 {
2547 do_quotes = 1;
2548 if (start_in_quotes == MAYBE)
2549 {
2550 inquote = at_start;
2551 if (inquote)
2552 start_in_quotes = TRUE;
2553 }
2554 else if (!backwards)
2555 inquote = TRUE;
2556 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002557
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002558 // ml_get() only keeps one line, need to get linep again
Bram Moolenaaraec11792007-07-10 11:09:36 +00002559 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561 }
2562 }
2563 if (start_in_quotes == MAYBE)
2564 start_in_quotes = FALSE;
2565
2566 /*
2567 * If 'smartmatch' is set:
2568 * Things inside quotes are ignored by setting 'inquote'. If we
2569 * find a quote without a preceding '\' invert 'inquote'. At the
2570 * end of a line not ending in '\' we reset 'inquote'.
2571 *
2572 * In lines with an uneven number of quotes (without preceding '\')
2573 * we do not know which part to ignore. Therefore we only set
2574 * inquote if the number of quotes in a line is even, unless this
2575 * line or the previous one ends in a '\'. Complicated, isn't it?
2576 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002577 c = PTR2CHAR(linep + pos.col);
2578 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
2580 case NUL:
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002581 // at end of line without trailing backslash, reset inquote
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2583 {
2584 inquote = FALSE;
2585 start_in_quotes = FALSE;
2586 }
2587 break;
2588
2589 case '"':
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002590 // a quote that is preceded with an odd number of backslashes is
2591 // ignored
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (do_quotes)
2593 {
2594 int col;
2595
2596 for (col = pos.col - 1; col >= 0; --col)
2597 if (linep[col] != '\\')
2598 break;
2599 if ((((int)pos.col - 1 - col) & 1) == 0)
2600 {
2601 inquote = !inquote;
2602 start_in_quotes = FALSE;
2603 }
2604 }
2605 break;
2606
2607 /*
2608 * If smart matching ('cpoptions' does not contain '%'):
2609 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2610 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2611 * skipped, there is never a brace in them.
2612 * Ignore this when finding matches for `'.
2613 */
2614 case '\'':
2615 if (!cpo_match && initc != '\'' && findc != '\'')
2616 {
2617 if (backwards)
2618 {
2619 if (pos.col > 1)
2620 {
2621 if (linep[pos.col - 2] == '\'')
2622 {
2623 pos.col -= 2;
2624 break;
2625 }
2626 else if (linep[pos.col - 2] == '\\' &&
2627 pos.col > 2 && linep[pos.col - 3] == '\'')
2628 {
2629 pos.col -= 3;
2630 break;
2631 }
2632 }
2633 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002634 else if (linep[pos.col + 1]) // forward search
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {
2636 if (linep[pos.col + 1] == '\\' &&
2637 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2638 {
2639 pos.col += 3;
2640 break;
2641 }
2642 else if (linep[pos.col + 2] == '\'')
2643 {
2644 pos.col += 2;
2645 break;
2646 }
2647 }
2648 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002649 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 default:
2652#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002653 /*
2654 * For Lisp skip over backslashed (), {} and [].
2655 * (actually, we skip #\( et al)
2656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (curbuf->b_p_lisp
2658 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002659 && pos.col > 1
2660 && check_prevcol(linep, pos.col, '\\', NULL)
2661 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 break;
2663#endif
2664
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002665 // Check for match outside of quotes, and inside of
2666 // quotes when the start is also inside of quotes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if ((!inquote || start_in_quotes == TRUE)
2668 && (c == initc || c == findc))
2669 {
2670 int col, bslcnt = 0;
2671
2672 if (!cpo_bsl)
2673 {
2674 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2675 bslcnt++;
2676 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002677 // Only accept a match when 'M' is in 'cpo' or when escaping
2678 // is what we expect.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2680 {
2681 if (c == initc)
2682 count++;
2683 else
2684 {
2685 if (count == 0)
2686 return &pos;
2687 count--;
2688 }
2689 }
2690 }
2691 }
2692 }
2693
2694 if (comment_dir == BACKWARD && count > 0)
2695 {
2696 pos = match_pos;
2697 return &pos;
2698 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002699 return (pos_T *)NULL; // never found it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700}
2701
2702/*
2703 * Check if line[] contains a / / comment.
2704 * Return MAXCOL if not, otherwise return the column.
2705 * TODO: skip strings.
2706 */
2707 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002708check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 char_u *p;
2711
2712 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002713#ifdef FEAT_LISP
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002714 // skip Lispish one-line comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002715 if (curbuf->b_p_lisp)
2716 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002717 if (vim_strchr(p, ';') != NULL) // there may be comments
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002718 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002719 int in_str = FALSE; // inside of string
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002720
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002721 p = line; // scan from start
Bram Moolenaar520470a2005-06-16 21:59:56 +00002722 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002723 {
2724 if (*p == '"')
2725 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002726 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002727 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002728 if (*(p - 1) != '\\') // skip escaped quote
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002729 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002730 }
2731 else if (p == line || ((p - line) >= 2
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002732 // skip #\" form
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002733 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002734 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002735 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002736 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002737 || (*(p - 1) != '\\' && *(p - 2) != '#')))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002738 break; // found!
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002739 ++p;
2740 }
2741 }
2742 else
2743 p = NULL;
2744 }
2745 else
2746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 while ((p = vim_strchr(p, '/')) != NULL)
2748 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002749 // accept a double /, unless it's preceded with * and followed by *,
2750 // because * / / * is an end and start of a C comment
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002751 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 break;
2753 ++p;
2754 }
2755
2756 if (p == NULL)
2757 return MAXCOL;
2758 return (int)(p - line);
2759}
2760
2761/*
2762 * Move cursor briefly to character matching the one under the cursor.
2763 * Used for Insert mode and "r" command.
2764 * Show the match only if it is visible on the screen.
2765 * If there isn't a match, then beep.
2766 */
2767 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002768showmatch(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002769 int c) // char to show match for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 pos_T *lpos, save_cursor;
2772 pos_T mpos;
2773 colnr_T vcol;
2774 long save_so;
2775 long save_siso;
2776#ifdef CURSOR_SHAPE
2777 int save_state;
2778#endif
2779 colnr_T save_dollar_vcol;
2780 char_u *p;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002781 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2782 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 /*
2785 * Only show match for chars in the 'matchpairs' option.
2786 */
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002787 // 'matchpairs' is "x:y,x:y"
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002788 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
2790#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002791 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002792 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002793#endif
Bram Moolenaar1614a142019-10-06 22:00:13 +02002794 p += mb_ptr2len(p) + 1;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002795 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796#ifdef FEAT_RIGHTLEFT
2797 && !(curwin->w_p_rl ^ p_ri)
2798#endif
2799 )
2800 break;
Bram Moolenaar1614a142019-10-06 22:00:13 +02002801 p += mb_ptr2len(p);
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002802 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 return;
2804 }
2805
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002806 if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep
Bram Moolenaar165bc692015-07-21 17:53:25 +02002807 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002808 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
2810 if (!curwin->w_p_wrap)
2811 getvcol(curwin, lpos, NULL, &vcol, NULL);
2812 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002813 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002815 mpos = *lpos; // save the pos, update_screen() may change it
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002817 save_so = *so;
2818 save_siso = *siso;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002819 // Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2820 // stop displaying the "$".
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002821 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2822 dollar_vcol = -1;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002823 ++curwin->w_virtcol; // do display ')' just before "$"
2824 update_screen(VALID); // show the new char first
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
2826 save_dollar_vcol = dollar_vcol;
2827#ifdef CURSOR_SHAPE
2828 save_state = State;
2829 State = SHOWMATCH;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002830 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831#endif
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002832 curwin->w_cursor = mpos; // move to matching char
2833 *so = 0; // don't use 'scrolloff' here
2834 *siso = 0; // don't use 'sidescrolloff' here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 showruler(FALSE);
2836 setcursor();
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002837 cursor_on(); // make sure that the cursor is shown
Bram Moolenaara338adc2018-01-31 20:51:47 +01002838 out_flush_cursor(TRUE, FALSE);
2839
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002840 // Restore dollar_vcol(), because setcursor() may call curs_rows()
2841 // which resets it if the matching position is in a previous line
2842 // and has a higher column number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 dollar_vcol = save_dollar_vcol;
2844
2845 /*
2846 * brief pause, unless 'm' is present in 'cpo' and a character is
2847 * available.
2848 */
2849 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002850 ui_delay(p_mat * 100L + 8, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 else if (!char_avail())
Bram Moolenaareda1da02019-11-17 17:06:33 +01002852 ui_delay(p_mat * 100L + 9, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002853 curwin->w_cursor = save_cursor; // restore cursor position
Bram Moolenaar375e3392019-01-31 18:26:10 +01002854 *so = save_so;
2855 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856#ifdef CURSOR_SHAPE
2857 State = save_state;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002858 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859#endif
2860 }
2861 }
2862}
2863
2864/*
Bram Moolenaar453c1922019-10-26 14:42:09 +02002865 * Check if the pattern is zero-width.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002866 * If move is TRUE, check from the beginning of the buffer, else from position
2867 * "cur".
2868 * "direction" is FORWARD or BACKWARD.
2869 * Returns TRUE, FALSE or -1 for failure.
2870 */
2871 static int
2872is_zero_width(char_u *pattern, int move, pos_T *cur, int direction)
2873{
2874 regmmatch_T regmatch;
2875 int nmatched = 0;
2876 int result = -1;
2877 pos_T pos;
Bram Moolenaar53989552019-12-23 22:59:18 +01002878 int called_emsg_before = called_emsg;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002879 int flag = 0;
2880
2881 if (pattern == NULL)
2882 pattern = spats[last_idx].pat;
2883
2884 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
2885 SEARCH_KEEP, &regmatch) == FAIL)
2886 return -1;
2887
2888 // init startcol correctly
2889 regmatch.startpos[0].col = -1;
2890 // move to match
2891 if (move)
2892 {
2893 CLEAR_POS(&pos);
2894 }
2895 else
2896 {
2897 pos = *cur;
2898 // accept a match at the cursor position
2899 flag = SEARCH_START;
2900 }
2901
2902 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
2903 SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL)
2904 {
2905 // Zero-width pattern should match somewhere, then we can check if
2906 // start and end are in the same position.
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002907 do
2908 {
2909 regmatch.startpos[0].col++;
2910 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
2911 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
2912 if (nmatched != 0)
2913 break;
2914 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
2915 : regmatch.startpos[0].col > pos.col);
2916
Bram Moolenaar53989552019-12-23 22:59:18 +01002917 if (called_emsg == called_emsg_before)
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002918 {
2919 result = (nmatched != 0
2920 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
2921 && regmatch.startpos[0].col == regmatch.endpos[0].col);
2922 }
2923 }
2924
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002925 vim_regfree(regmatch.regprog);
2926 return result;
2927}
2928
Bram Moolenaardde0efe2012-08-23 15:53:05 +02002929
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002930/*
2931 * Find next search match under cursor, cursor at end.
2932 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002933 */
2934 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002935current_search(
2936 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002937 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002938{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002939 pos_T start_pos; // start position of the pattern match
2940 pos_T end_pos; // end position of the pattern match
2941 pos_T orig_pos; // position of the cursor at beginning
2942 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002943 int i;
2944 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002945 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002946 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002947 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02002948 pos_T save_VIsual = VIsual;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002949 int zero_width;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002950
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002951 // Correct cursor when 'selection' is exclusive
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002952 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002953 dec_cursor();
2954
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002955 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002956 if (VIsual_active)
2957 {
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002958 if (forward)
2959 incl(&pos);
2960 else
2961 decl(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002962 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002963
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002964 // Is the pattern is zero-width?, this time, don't care about the direction
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002965 zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor,
Bram Moolenaar22ab5472017-09-26 12:28:45 +02002966 FORWARD);
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002967 if (zero_width == -1)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002968 return FAIL; // pattern not found
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002969
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002970 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002971 * The trick is to first search backwards and then search forward again,
2972 * so that a match at the current cursor position will be correctly
2973 * captured.
2974 */
2975 for (i = 0; i < 2; i++)
2976 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002977 if (forward)
2978 dir = i;
2979 else
2980 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002981
2982 flags = 0;
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02002983 if (!dir && !zero_width)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002984 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002985 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02002986
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002987 // wrapping should not occur in the first round
2988 if (i == 0)
2989 p_ws = FALSE;
2990
Bram Moolenaar5d24a222018-12-23 19:10:09 +01002991 result = searchit(curwin, curbuf, &pos, &end_pos,
2992 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002993 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02002994 SEARCH_KEEP | flags, RE_SEARCH, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02002995
Bram Moolenaar82cf7f62019-11-02 23:22:47 +01002996 p_ws = old_p_ws;
2997
Bram Moolenaar63d9e732019-12-05 21:10:38 +01002998 // First search may fail, but then start searching from the
2999 // beginning of the file (cursor might be on the search match)
3000 // except when Visual mode is active, so that extending the visual
3001 // selection works.
3002 if (i == 1 && !result) // not found, abort
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003003 {
3004 curwin->w_cursor = orig_pos;
3005 if (VIsual_active)
3006 VIsual = save_VIsual;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003007 return FAIL;
3008 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003009 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003010 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003011 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003012 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003013 // try again from start of buffer
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003014 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003015 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003016 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003017 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003018 // try again from end of buffer
3019 // searching backwards, so set pos to last line and col
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003020 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003021 pos.col = (colnr_T)STRLEN(
3022 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003023 }
3024 }
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003025 }
3026
3027 start_pos = pos;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003028
3029 if (!VIsual_active)
3030 VIsual = start_pos;
3031
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003032 // put cursor on last character of match
3033 curwin->w_cursor = end_pos;
Bram Moolenaar453c1922019-10-26 14:42:09 +02003034 if (LT_POS(VIsual, end_pos) && forward)
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003035 dec_cursor();
Bram Moolenaaredaad6e2019-10-24 15:23:37 +02003036 else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
3037 curwin->w_cursor = pos; // put the cursor on the start of the match
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003038 VIsual_active = TRUE;
3039 VIsual_mode = 'v';
3040
Bram Moolenaarb7633612019-02-10 21:48:25 +01003041 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003042 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003043 // Correction for exclusive selection depends on the direction.
Bram Moolenaarb7633612019-02-10 21:48:25 +01003044 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
3045 inc_cursor();
3046 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
3047 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003048 }
3049
3050#ifdef FEAT_FOLDING
3051 if (fdo_flags & FDO_SEARCH && KeyTyped)
3052 foldOpenCursor();
3053#endif
3054
3055 may_start_select('c');
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003056 setmouse();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003057#ifdef FEAT_CLIPBOARD
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003058 // Make sure the clipboard gets updated. Needed because start and
3059 // end are still the same, and the selection needs to be owned
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02003060 clip_star.vmode = NUL;
3061#endif
3062 redraw_curbuf_later(INVERTED);
3063 showmode();
3064
3065 return OK;
3066}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02003067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
3069 || defined(PROTO)
3070/*
3071 * return TRUE if line 'lnum' is empty or has white chars only.
3072 */
3073 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003074linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075{
3076 char_u *p;
3077
3078 p = skipwhite(ml_get(lnum));
3079 return (*p == NUL);
3080}
3081#endif
3082
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003083/*
3084 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003085 * See update_search_stat() for other arguments.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003086 */
3087 static void
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003088cmdline_search_stat(
3089 int dirc,
3090 pos_T *pos,
3091 pos_T *cursor_pos,
3092 int show_top_bot_msg,
3093 char_u *msgbuf,
3094 int recompute,
3095 int maxcount,
3096 long timeout)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003097{
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003098 searchstat_T stat;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003099
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003100 update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount,
3101 timeout);
3102 if (stat.cur > 0)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003103 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003104 char t[SEARCH_STAT_BUF_LEN];
Bram Moolenaare2ad8262019-05-24 13:22:22 +02003105 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003106
3107#ifdef FEAT_RIGHTLEFT
3108 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
3109 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003110 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003111 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003112 else if (stat.cnt > maxcount && stat.cur > maxcount)
3113 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3114 maxcount, maxcount);
3115 else if (stat.cnt > maxcount)
3116 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]",
3117 maxcount, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003118 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003119 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3120 stat.cnt, stat.cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003121 }
3122 else
3123#endif
3124 {
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003125 if (stat.incomplete == 1)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02003126 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003127 else if (stat.cnt > maxcount && stat.cur > maxcount)
3128 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]",
3129 maxcount, maxcount);
3130 else if (stat.cnt > maxcount)
3131 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]",
3132 stat.cur, maxcount);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003133 else
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003134 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]",
3135 stat.cur, stat.cnt);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003136 }
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003137
3138 len = STRLEN(t);
Bram Moolenaardc6855a2019-05-18 19:26:29 +02003139 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003140 {
Bram Moolenaar16b58ae2019-09-06 20:40:21 +02003141 mch_memmove(t + 2, t, len);
3142 t[0] = 'W';
3143 t[1] = ' ';
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02003144 len += 2;
3145 }
3146
3147 mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003148 if (dirc == '?' && stat.cur == maxcount + 1)
3149 stat.cur = -1;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003150
Bram Moolenaar984f0312019-05-24 13:11:47 +02003151 // keep the message even after redraw, but don't put in history
3152 msg_hist_off = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003153 give_warning(msgbuf, FALSE);
Bram Moolenaar984f0312019-05-24 13:11:47 +02003154 msg_hist_off = FALSE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003155 }
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003156}
3157
3158/*
3159 * Add the search count information to "stat".
3160 * "stat" must not be NULL.
3161 * When "recompute" is TRUE always recompute the numbers.
3162 * dirc == 0: don't find the next/previous match (only set the result to "stat")
3163 * dirc == '/': find the next match
3164 * dirc == '?': find the previous match
3165 */
3166 static void
3167update_search_stat(
3168 int dirc,
3169 pos_T *pos,
3170 pos_T *cursor_pos,
3171 searchstat_T *stat,
3172 int recompute,
3173 int maxcount,
Bram Moolenaarf9ca08e2020-06-01 18:56:03 +02003174 long timeout UNUSED)
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003175{
3176 int save_ws = p_ws;
3177 int wraparound = FALSE;
3178 pos_T p = (*pos);
Bram Moolenaar14681622020-06-03 22:57:39 +02003179 static pos_T lastpos = {0, 0, 0};
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003180 static int cur = 0;
3181 static int cnt = 0;
3182 static int exact_match = FALSE;
3183 static int incomplete = 0;
3184 static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT;
3185 static int chgtick = 0;
3186 static char_u *lastpat = NULL;
3187 static buf_T *lbuf = NULL;
3188#ifdef FEAT_RELTIME
3189 proftime_T start;
3190#endif
3191
3192 vim_memset(stat, 0, sizeof(searchstat_T));
3193
3194 if (dirc == 0 && !recompute && !EMPTY_POS(lastpos))
3195 {
3196 stat->cur = cur;
3197 stat->cnt = cnt;
3198 stat->exact_match = exact_match;
3199 stat->incomplete = incomplete;
3200 stat->last_maxcount = last_maxcount;
3201 return;
3202 }
3203 last_maxcount = maxcount;
3204
3205 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
3206 || (dirc == '/' && LT_POS(p, lastpos)));
3207
3208 // If anything relevant changed the count has to be recomputed.
3209 // MB_STRNICMP ignores case, but we should not ignore case.
3210 // Unfortunately, there is no MB_STRNICMP function.
3211 // XXX: above comment should be "no MB_STRCMP function" ?
3212 if (!(chgtick == CHANGEDTICK(curbuf)
3213 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
3214 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
3215 && EQUAL_POS(lastpos, *cursor_pos)
3216 && lbuf == curbuf) || wraparound || cur < 0
3217 || (maxcount > 0 && cur > maxcount) || recompute)
3218 {
3219 cur = 0;
3220 cnt = 0;
3221 exact_match = FALSE;
3222 incomplete = 0;
3223 CLEAR_POS(&lastpos);
3224 lbuf = curbuf;
3225 }
3226
3227 if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound
3228 && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 0))
3229 cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1;
3230 else
3231 {
3232 int done_search = FALSE;
3233 pos_T endpos = {0, 0, 0};
3234
3235 p_ws = FALSE;
3236#ifdef FEAT_RELTIME
3237 if (timeout > 0)
3238 profile_setlimit(timeout, &start);
3239#endif
3240 while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos,
3241 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL)
3242 {
3243 done_search = TRUE;
3244#ifdef FEAT_RELTIME
3245 // Stop after passing the time limit.
3246 if (timeout > 0 && profile_passed_limit(&start))
3247 {
3248 incomplete = 1;
3249 break;
3250 }
3251#endif
3252 cnt++;
3253 if (LTOREQ_POS(lastpos, p))
3254 {
3255 cur = cnt;
Bram Moolenaar57f75a52020-06-02 22:06:21 +02003256 if (LT_POS(p, endpos))
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02003257 exact_match = TRUE;
3258 }
3259 fast_breakcheck();
3260 if (maxcount > 0 && cnt > maxcount)
3261 {
3262 incomplete = 2; // max count exceeded
3263 break;
3264 }
3265 }
3266 if (got_int)
3267 cur = -1; // abort
3268 if (done_search)
3269 {
3270 vim_free(lastpat);
3271 lastpat = vim_strsave(spats[last_idx].pat);
3272 chgtick = CHANGEDTICK(curbuf);
3273 lbuf = curbuf;
3274 lastpos = p;
3275 }
3276 }
3277 stat->cur = cur;
3278 stat->cnt = cnt;
3279 stat->exact_match = exact_match;
3280 stat->incomplete = incomplete;
3281 stat->last_maxcount = last_maxcount;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02003282 p_ws = save_ws;
3283}
3284
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285#if defined(FEAT_FIND_ID) || defined(PROTO)
3286/*
3287 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01003288 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003291find_pattern_in_path(
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003292 char_u *ptr, // pointer to search pattern
3293 int dir UNUSED, // direction of expansion
3294 int len, // length of search pattern
3295 int whole, // match whole words only
3296 int skip_comments, // don't match inside comments
3297 int type, // Type of search; are we looking for a type?
3298 // a macro?
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003299 long count,
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003300 int action, // What to do when we find it
3301 linenr_T start_lnum, // first line to start searching
3302 linenr_T end_lnum) // last line for searching
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003304 SearchedFile *files; // Stack of included files
3305 SearchedFile *bigger; // When we need more space
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 int max_path_depth = 50;
3307 long match_count = 1;
3308
3309 char_u *pat;
3310 char_u *new_fname;
3311 char_u *curr_fname = curbuf->b_fname;
3312 char_u *prev_fname = NULL;
3313 linenr_T lnum;
3314 int depth;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003315 int depth_displayed; // For type==CHECK_PATH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 int old_files;
3317 int already_searched;
3318 char_u *file_line;
3319 char_u *line;
3320 char_u *p;
3321 char_u save_char;
3322 int define_matched;
3323 regmatch_T regmatch;
3324 regmatch_T incl_regmatch;
3325 regmatch_T def_regmatch;
3326 int matched = FALSE;
3327 int did_show = FALSE;
3328 int found = FALSE;
3329 int i;
3330 char_u *already = NULL;
3331 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003332 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003333#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 win_T *curwin_save = NULL;
3335#endif
3336
3337 regmatch.regprog = NULL;
3338 incl_regmatch.regprog = NULL;
3339 def_regmatch.regprog = NULL;
3340
3341 file_line = alloc(LSIZE);
3342 if (file_line == NULL)
3343 return;
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (type != CHECK_PATH && type != FIND_DEFINE
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003346 // when CONT_SOL is set compare "ptr" with the beginning of the line
3347 // is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003348 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 {
3350 pat = alloc(len + 5);
3351 if (pat == NULL)
3352 goto fpip_end;
3353 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003354 // ignore case according to p_ic, p_scs and pat
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 regmatch.rm_ic = ignorecase(pat);
3356 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
3357 vim_free(pat);
3358 if (regmatch.regprog == NULL)
3359 goto fpip_end;
3360 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003361 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
3362 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003364 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (incl_regmatch.regprog == NULL)
3366 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003367 incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 }
3369 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
3370 {
3371 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
3372 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
3373 if (def_regmatch.regprog == NULL)
3374 goto fpip_end;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003375 def_regmatch.rm_ic = FALSE; // don't ignore case in define pat.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003377 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (files == NULL)
3379 goto fpip_end;
3380 old_files = max_path_depth;
3381 depth = depth_displayed = -1;
3382
3383 lnum = start_lnum;
3384 if (end_lnum > curbuf->b_ml.ml_line_count)
3385 end_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003386 if (lnum > end_lnum) // do at least one line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 lnum = end_lnum;
3388 line = ml_get(lnum);
3389
3390 for (;;)
3391 {
3392 if (incl_regmatch.regprog != NULL
3393 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
3394 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003395 char_u *p_fname = (curr_fname == curbuf->b_fname)
3396 ? curbuf->b_ffname : curr_fname;
3397
3398 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003399 // Use text from '\zs' to '\ze' (or end) of 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003400 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003401 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003402 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
3403 else
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003404 // Use text after match with 'include'.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003405 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003406 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 already_searched = FALSE;
3408 if (new_fname != NULL)
3409 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003410 // Check whether we have already searched in this file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 for (i = 0;; i++)
3412 {
3413 if (i == depth + 1)
3414 i = old_files;
3415 if (i == max_path_depth)
3416 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02003417 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
3418 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 {
3420 if (type != CHECK_PATH &&
3421 action == ACTION_SHOW_ALL && files[i].matched)
3422 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003423 msg_putchar('\n'); // cursor below last one
3424 if (!got_int) // don't display if 'q'
3425 // typed at "--more--"
3426 // message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 {
3428 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003429 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 prev_fname = NULL;
3431 }
3432 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01003433 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 already_searched = TRUE;
3435 break;
3436 }
3437 }
3438 }
3439
3440 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
3441 || (new_fname == NULL && !already_searched)))
3442 {
3443 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003444 msg_putchar('\n'); // cursor below last one
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else
3446 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003447 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar32526b32019-01-19 17:43:09 +01003448 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003450 msg_puts_title(_("not found "));
3451 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 did_show = TRUE;
3454 while (depth_displayed < depth && !got_int)
3455 {
3456 ++depth_displayed;
3457 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003458 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003460 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003462 if (!got_int) // don't display if 'q' typed
3463 // for "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003466 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (new_fname != NULL)
3468 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003469 // using "new_fname" is more reliable, e.g., when
3470 // 'includeexpr' is set.
Bram Moolenaar8820b482017-03-16 17:23:31 +01003471 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
3473 else
3474 {
3475 /*
3476 * Isolate the file name.
3477 * Include the surrounding "" or <> if present.
3478 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003479 if (inc_opt != NULL
3480 && strstr((char *)inc_opt, "\\zs") != NULL)
3481 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003482 // pattern contains \zs, use the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003483 p = incl_regmatch.startp[0];
3484 i = (int)(incl_regmatch.endp[0]
3485 - incl_regmatch.startp[0]);
3486 }
3487 else
3488 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003489 // find the file name after the end of the match
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003490 for (p = incl_regmatch.endp[0];
3491 *p && !vim_isfilec(*p); p++)
3492 ;
3493 for (i = 0; vim_isfilec(p[i]); i++)
3494 ;
3495 }
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 if (i == 0)
3498 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003499 // Nothing found, use the rest of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003501 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003503 // Avoid checking before the start of the line, can
3504 // happen if \zs appears in the regexp.
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02003505 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 if (p[-1] == '"' || p[-1] == '<')
3508 {
3509 --p;
3510 ++i;
3511 }
3512 if (p[i] == '"' || p[i] == '>')
3513 ++i;
3514 }
3515 save_char = p[i];
3516 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01003517 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 p[i] = save_char;
3519 }
3520
3521 if (new_fname == NULL && action == ACTION_SHOW_ALL)
3522 {
3523 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003524 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003526 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 }
3528 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003529 out_flush(); // output each line directly
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 }
3531
3532 if (new_fname != NULL)
3533 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003534 // Push the new file onto the file stack
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 if (depth + 1 == old_files)
3536 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003537 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (bigger != NULL)
3539 {
3540 for (i = 0; i <= depth; i++)
3541 bigger[i] = files[i];
3542 for (i = depth + 1; i < old_files + max_path_depth; i++)
3543 {
3544 bigger[i].fp = NULL;
3545 bigger[i].name = NULL;
3546 bigger[i].lnum = 0;
3547 bigger[i].matched = FALSE;
3548 }
3549 for (i = old_files; i < max_path_depth; i++)
3550 bigger[i + max_path_depth] = files[i];
3551 old_files += max_path_depth;
3552 max_path_depth *= 2;
3553 vim_free(files);
3554 files = bigger;
3555 }
3556 }
3557 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
3558 == NULL)
3559 vim_free(new_fname);
3560 else
3561 {
3562 if (++depth == old_files)
3563 {
3564 /*
3565 * lalloc() for 'bigger' must have failed above. We
3566 * will forget one of our already visited files now.
3567 */
3568 vim_free(files[old_files].name);
3569 ++old_files;
3570 }
3571 files[depth].name = curr_fname = new_fname;
3572 files[depth].lnum = 0;
3573 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (action == ACTION_EXPAND)
3575 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003576 msg_hist_off = TRUE; // reset in msg_trunc_attr()
Bram Moolenaar555b2802005-05-19 21:08:39 +00003577 vim_snprintf((char*)IObuff, IOSIZE,
3578 _("Scanning included file: %s"),
3579 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01003580 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003582 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003583 {
3584 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003585 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003586 (char *)new_fname);
3587 verbose_leave();
3588 }
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591 }
3592 }
3593 else
3594 {
3595 /*
3596 * Check if the line is a define (type == FIND_DEFINE)
3597 */
3598 p = line;
3599search_line:
3600 define_matched = FALSE;
3601 if (def_regmatch.regprog != NULL
3602 && vim_regexec(&def_regmatch, line, (colnr_T)0))
3603 {
3604 /*
3605 * Pattern must be first identifier after 'define', so skip
3606 * to that position before checking for match of pattern. Also
3607 * don't let it match beyond the end of this identifier.
3608 */
3609 p = def_regmatch.endp[0];
3610 while (*p && !vim_iswordc(*p))
3611 p++;
3612 define_matched = TRUE;
3613 }
3614
3615 /*
3616 * Look for a match. Don't do this if we are looking for a
3617 * define and this line didn't match define_prog above.
3618 */
3619 if (def_regmatch.regprog == NULL || define_matched)
3620 {
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003621 if (define_matched || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003623 // compare the first "len" chars from "ptr"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 startp = skipwhite(p);
3625 if (p_ic)
3626 matched = !MB_STRNICMP(startp, ptr, len);
3627 else
3628 matched = !STRNCMP(startp, ptr, len);
3629 if (matched && define_matched && whole
3630 && vim_iswordc(startp[len]))
3631 matched = FALSE;
3632 }
3633 else if (regmatch.regprog != NULL
3634 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
3635 {
3636 matched = TRUE;
3637 startp = regmatch.startp[0];
3638 /*
3639 * Check if the line is not a comment line (unless we are
3640 * looking for a define). A line starting with "# define"
3641 * is not considered to be a comment line.
3642 */
3643 if (!define_matched && skip_comments)
3644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 if ((*line != '#' ||
3646 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02003647 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 matched = FALSE;
3649
3650 /*
3651 * Also check for a "/ *" or "/ /" before the match.
3652 * Skips lines like "int backwards; / * normal index
3653 * * /" when looking for "normal".
3654 * Note: Doesn't skip "/ *" in comments.
3655 */
3656 p = skipwhite(line);
3657 if (matched
3658 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 for (p = line; *p && p < startp; ++p)
3660 {
3661 if (matched
3662 && p[0] == '/'
3663 && (p[1] == '*' || p[1] == '/'))
3664 {
3665 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003666 // After "//" all text is comment
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (p[1] == '/')
3668 break;
3669 ++p;
3670 }
3671 else if (!matched && p[0] == '*' && p[1] == '/')
3672 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003673 // Can find match after "* /".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 matched = TRUE;
3675 ++p;
3676 }
3677 }
3678 }
3679 }
3680 }
3681 }
3682 if (matched)
3683 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 if (action == ACTION_EXPAND)
3685 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003686 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 int add_r;
3688 char_u *aux;
3689
3690 if (depth == -1 && lnum == curwin->w_cursor.lnum)
3691 break;
3692 found = TRUE;
3693 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003694 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003696 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (vim_iswordp(p))
3698 goto exit_matched;
3699 p = find_word_start(p);
3700 }
3701 p = find_word_end(p);
3702 i = (int)(p - aux);
3703
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003704 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003706 // IOSIZE > compl_length, so the STRNCPY works
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00003708
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003709 // Get the next line: when "depth" < 0 from the current
3710 // buffer, otherwise from the included file. Jump to
3711 // exit_matched when past the last line.
Bram Moolenaar89d40322006-08-29 15:30:07 +00003712 if (depth < 0)
3713 {
3714 if (lnum >= end_lnum)
3715 goto exit_matched;
3716 line = ml_get(++lnum);
3717 }
3718 else if (vim_fgets(line = file_line,
3719 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 goto exit_matched;
3721
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003722 // we read a line, set "already" to check this "line" later
3723 // if depth >= 0 we'll increase files[depth].lnum far
3724 // bellow -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 already = aux = p = skipwhite(line);
3726 p = find_word_start(p);
3727 p = find_word_end(p);
3728 if (p > aux)
3729 {
3730 if (*aux != ')' && IObuff[i-1] != TAB)
3731 {
3732 if (IObuff[i-1] != ' ')
3733 IObuff[i++] = ' ';
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003734 // IObuf =~ "\(\k\|\i\).* ", thus i >= 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 if (p_js
3736 && (IObuff[i-2] == '.'
3737 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
3738 && (IObuff[i-2] == '?'
3739 || IObuff[i-2] == '!'))))
3740 IObuff[i++] = ' ';
3741 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003742 // copy as much as possible of the new word
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (p - aux >= IOSIZE - i)
3744 p = aux + IOSIZE - i - 1;
3745 STRNCPY(IObuff + i, aux, p - aux);
3746 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003747 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 }
3749 IObuff[i] = NUL;
3750 aux = IObuff;
3751
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003752 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 goto exit_matched;
3754 }
3755
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003756 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02003758 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 if (add_r == OK)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003760 // if dir was BACKWARD then honor it just once
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003762 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 break;
3764 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003765 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
3767 found = TRUE;
3768 if (!did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003769 gotocmdline(TRUE); // cursor at status line
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 if (curr_fname != prev_fname)
3771 {
3772 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003773 msg_putchar('\n'); // cursor below last one
3774 if (!got_int) // don't display if 'q' typed
3775 // at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 msg_home_replace_hl(curr_fname);
3777 prev_fname = curr_fname;
3778 }
3779 did_show = TRUE;
3780 if (!got_int)
3781 show_pat_in_path(line, type, TRUE, action,
3782 (depth == -1) ? NULL : files[depth].fp,
3783 (depth == -1) ? &lnum : &files[depth].lnum,
3784 match_count++);
3785
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003786 // Set matched flag for this file and all the ones that
3787 // include it
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 for (i = 0; i <= depth; ++i)
3789 files[i].matched = TRUE;
3790 }
3791 else if (--count <= 0)
3792 {
3793 found = TRUE;
3794 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02003795#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 && g_do_tagpreview == 0
3797#endif
3798 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003799 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 else if (action == ACTION_SHOW)
3801 {
3802 show_pat_in_path(line, type, did_show, action,
3803 (depth == -1) ? NULL : files[depth].fp,
3804 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
3805 did_show = TRUE;
3806 }
3807 else
3808 {
3809#ifdef FEAT_GUI
3810 need_mouse_correct = TRUE;
3811#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02003812#if defined(FEAT_QUICKFIX)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003813 // ":psearch" uses the preview window
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (g_do_tagpreview != 0)
3815 {
3816 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003817 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819#endif
3820 if (action == ACTION_SPLIT)
3821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003824 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
3826 if (depth == -1)
3827 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003828 // match in current file
Bram Moolenaar4033c552017-09-16 20:54:51 +02003829#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (g_do_tagpreview != 0)
3831 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003832 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003833 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003834 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003835 break; // failed to jump to file
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 else
3838#endif
3839 setpcmark();
3840 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02003841 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 else
3844 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02003845 if (!GETFILE_SUCCESS(getfile(
3846 0, files[depth].name, NULL, TRUE,
3847 files[depth].lnum, FALSE)))
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003848 break; // failed to jump to file
3849 // autocommands may have changed the lnum, we don't
3850 // want that here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 curwin->w_cursor.lnum = files[depth].lnum;
3852 }
3853 }
3854 if (action != ACTION_SHOW)
3855 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003856 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 curwin->w_set_curswant = TRUE;
3858 }
3859
Bram Moolenaar4033c552017-09-16 20:54:51 +02003860#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003862 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003864 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 validate_cursor();
3866 redraw_later(VALID);
3867 win_enter(curwin_save, TRUE);
3868 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003869# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003870 else if (WIN_IS_POPUP(curwin))
3871 // can't keep focus in popup window
3872 win_enter(firstwin, TRUE);
3873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#endif
3875 break;
3876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 matched = FALSE;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003879 // look for other matches in the rest of the line if we
3880 // are not at the end of it already
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003883 && !(compl_cont_status & CONT_SOL)
Bram Moolenaarfe81d452009-04-22 14:44:41 +00003884 && *startp != NUL
Bram Moolenaar1614a142019-10-06 22:00:13 +02003885 && *(p = startp + mb_ptr2len(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 goto search_line;
3887 }
3888 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02003890 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003891 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 break;
3893
3894 /*
3895 * Read the next line. When reading an included file and encountering
3896 * end-of-file, close the file and continue in the file that included
3897 * it.
3898 */
3899 while (depth >= 0 && !already
3900 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
3901 {
3902 fclose(files[depth].fp);
3903 --old_files;
3904 files[old_files].name = files[depth].name;
3905 files[old_files].matched = files[depth].matched;
3906 --depth;
3907 curr_fname = (depth == -1) ? curbuf->b_fname
3908 : files[depth].name;
3909 if (depth < depth_displayed)
3910 depth_displayed = depth;
3911 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003912 if (depth >= 0) // we could read the line
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 files[depth].lnum++;
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003915 // Remove any CR and LF from the line.
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02003916 i = (int)STRLEN(line);
3917 if (i > 0 && line[i - 1] == '\n')
3918 line[--i] = NUL;
3919 if (i > 0 && line[i - 1] == '\r')
3920 line[--i] = NUL;
3921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 else if (!already)
3923 {
3924 if (++lnum > end_lnum)
3925 break;
3926 line = ml_get(lnum);
3927 }
3928 already = NULL;
3929 }
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003930 // End of big for (;;) loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003932 // Close any files that are still open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 for (i = 0; i <= depth; i++)
3934 {
3935 fclose(files[i].fp);
3936 vim_free(files[i].name);
3937 }
3938 for (i = old_files; i < max_path_depth; i++)
3939 vim_free(files[i].name);
3940 vim_free(files);
3941
3942 if (type == CHECK_PATH)
3943 {
3944 if (!did_show)
3945 {
3946 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003947 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01003949 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02003952 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01003954 if (got_int || ins_compl_interrupted())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003955 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003957 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003959 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
3961 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
3962 msg_end();
3963
3964fpip_end:
3965 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02003966 vim_regfree(regmatch.regprog);
3967 vim_regfree(incl_regmatch.regprog);
3968 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969}
3970
3971 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003972show_pat_in_path(
3973 char_u *line,
3974 int type,
3975 int did_show,
3976 int action,
3977 FILE *fp,
3978 linenr_T *lnum,
3979 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980{
3981 char_u *p;
3982
3983 if (did_show)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003984 msg_putchar('\n'); // cursor below last one
Bram Moolenaar91170f82006-05-05 21:15:17 +00003985 else if (!msg_silent)
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003986 gotocmdline(TRUE); // cursor at status line
3987 if (got_int) // 'q' typed at "--more--" message
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 return;
3989 for (;;)
3990 {
3991 p = line + STRLEN(line) - 1;
3992 if (fp != NULL)
3993 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01003994 // We used fgets(), so get rid of newline at end
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (p >= line && *p == '\n')
3996 --p;
3997 if (p >= line && *p == '\r')
3998 --p;
3999 *(p + 1) = NUL;
4000 }
4001 if (action == ACTION_SHOW_ALL)
4002 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004003 sprintf((char *)IObuff, "%3ld: ", count); // show match nr
Bram Moolenaar32526b32019-01-19 17:43:09 +01004004 msg_puts((char *)IObuff);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004005 sprintf((char *)IObuff, "%4ld", *lnum); // show line nr
4006 // Highlight line numbers
Bram Moolenaar32526b32019-01-19 17:43:09 +01004007 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
4008 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004010 msg_prt_line(line, FALSE);
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004011 out_flush(); // show one line at a time
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004013 // Definition continues until line that doesn't end with '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4015 break;
4016
4017 if (fp != NULL)
4018 {
Bram Moolenaar63d9e732019-12-05 21:10:38 +01004019 if (vim_fgets(line, LSIZE, fp)) // end of file
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 break;
4021 ++*lnum;
4022 }
4023 else
4024 {
4025 if (++*lnum > curbuf->b_ml.ml_line_count)
4026 break;
4027 line = ml_get(*lnum);
4028 }
4029 msg_putchar('\n');
4030 }
4031}
4032#endif
4033
4034#ifdef FEAT_VIMINFO
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004035/*
4036 * Return the last used search pattern at "idx".
4037 */
Bram Moolenaarc3328162019-07-23 22:15:25 +02004038 spat_T *
4039get_spat(int idx)
4040{
4041 return &spats[idx];
4042}
4043
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02004044/*
4045 * Return the last used search pattern index.
4046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02004048get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
Bram Moolenaarc3328162019-07-23 22:15:25 +02004050 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052#endif
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004053
4054#ifdef FEAT_EVAL
4055/*
4056 * "searchcount()" function
4057 */
4058 void
4059f_searchcount(typval_T *argvars, typval_T *rettv)
4060{
4061 pos_T pos = curwin->w_cursor;
4062 char_u *pattern = NULL;
4063 int maxcount = SEARCH_STAT_DEF_MAX_COUNT;
4064 long timeout = SEARCH_STAT_DEF_TIMEOUT;
Bram Moolenaar597aaac2020-09-05 21:21:16 +02004065 int recompute = FALSE;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004066 searchstat_T stat;
4067
4068 if (rettv_dict_alloc(rettv) == FAIL)
4069 return;
4070
4071 if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag
4072 recompute = TRUE;
4073
4074 if (argvars[0].v_type != VAR_UNKNOWN)
4075 {
Bram Moolenaar14681622020-06-03 22:57:39 +02004076 dict_T *dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004077 dictitem_T *di;
4078 listitem_T *li;
4079 int error = FALSE;
4080
Bram Moolenaar14681622020-06-03 22:57:39 +02004081 if (argvars[0].v_type != VAR_DICT || argvars[0].vval.v_dict == NULL)
4082 {
4083 emsg(_(e_dictreq));
4084 return;
4085 }
4086 dict = argvars[0].vval.v_dict;
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004087 di = dict_find(dict, (char_u *)"timeout", -1);
4088 if (di != NULL)
4089 {
4090 timeout = (long)tv_get_number_chk(&di->di_tv, &error);
4091 if (error)
4092 return;
4093 }
4094 di = dict_find(dict, (char_u *)"maxcount", -1);
4095 if (di != NULL)
4096 {
4097 maxcount = (int)tv_get_number_chk(&di->di_tv, &error);
4098 if (error)
4099 return;
4100 }
Bram Moolenaar597aaac2020-09-05 21:21:16 +02004101 recompute = dict_get_bool(dict, (char_u *)"recompute", recompute);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004102 di = dict_find(dict, (char_u *)"pattern", -1);
4103 if (di != NULL)
4104 {
4105 pattern = tv_get_string_chk(&di->di_tv);
4106 if (pattern == NULL)
4107 return;
4108 }
4109 di = dict_find(dict, (char_u *)"pos", -1);
4110 if (di != NULL)
4111 {
4112 if (di->di_tv.v_type != VAR_LIST)
4113 {
4114 semsg(_(e_invarg2), "pos");
4115 return;
4116 }
4117 if (list_len(di->di_tv.vval.v_list) != 3)
4118 {
4119 semsg(_(e_invarg2), "List format should be [lnum, col, off]");
4120 return;
4121 }
4122 li = list_find(di->di_tv.vval.v_list, 0L);
4123 if (li != NULL)
4124 {
4125 pos.lnum = tv_get_number_chk(&li->li_tv, &error);
4126 if (error)
4127 return;
4128 }
4129 li = list_find(di->di_tv.vval.v_list, 1L);
4130 if (li != NULL)
4131 {
4132 pos.col = tv_get_number_chk(&li->li_tv, &error) - 1;
4133 if (error)
4134 return;
4135 }
4136 li = list_find(di->di_tv.vval.v_list, 2L);
4137 if (li != NULL)
4138 {
4139 pos.coladd = tv_get_number_chk(&li->li_tv, &error);
4140 if (error)
4141 return;
4142 }
4143 }
4144 }
4145
4146 save_last_search_pattern();
4147 if (pattern != NULL)
4148 {
4149 if (*pattern == NUL)
4150 goto the_end;
Bram Moolenaar109aece2020-06-01 19:08:54 +02004151 vim_free(spats[last_idx].pat);
Bram Moolenaare8f5ec02020-06-01 17:28:35 +02004152 spats[last_idx].pat = vim_strsave(pattern);
4153 }
4154 if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL)
4155 goto the_end; // the previous pattern was never defined
4156
4157 update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout);
4158
4159 dict_add_number(rettv->vval.v_dict, "current", stat.cur);
4160 dict_add_number(rettv->vval.v_dict, "total", stat.cnt);
4161 dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match);
4162 dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete);
4163 dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount);
4164
4165the_end:
4166 restore_last_search_pattern();
4167}
4168#endif