blob: 9eb192550fe80188c89ebacfd6e48c38d5556ac6 [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);
20static int cls(void);
21static int skip_chars(int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_FIND_ID
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010023static void show_pat_in_path(char_u *, int,
24 int, int, FILE *, linenr_T *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025#endif
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +020026static void search_stat(int dirc, pos_T *pos, int show_top_bot_msg, char_u *msgbuf, int recompute);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027
Bram Moolenaar071d4272004-06-13 20:20:40 +000028/*
29 * This file contains various searching-related routines. These fall into
30 * three groups:
31 * 1. string searches (for /, ?, n, and N)
32 * 2. character searches within a single line (for f, F, t, T, etc)
33 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
34 */
35
36/*
37 * String searches
38 *
39 * The string search functions are divided into two levels:
40 * lowest: searchit(); uses an pos_T for starting position and found match.
41 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
42 *
43 * The last search pattern is remembered for repeating the same search.
44 * This pattern is shared between the :g, :s, ? and / commands.
45 * This is in search_regcomp().
46 *
47 * The actual string matching is done using a heavily modified version of
48 * Henry Spencer's regular expression library. See regexp.c.
49 */
50
Bram Moolenaar071d4272004-06-13 20:20:40 +000051/*
52 * Two search patterns are remembered: One for the :substitute command and
53 * one for other searches. last_idx points to the one that was used the last
54 * time.
55 */
Bram Moolenaarc3328162019-07-23 22:15:25 +020056static spat_T spats[2] =
Bram Moolenaar071d4272004-06-13 20:20:40 +000057{
58 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
59 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
60};
61
62static int last_idx = 0; /* index in spats[] for RE_LAST */
63
Bram Moolenaardbd24b52015-08-11 14:26:19 +020064static char_u lastc[2] = {NUL, NUL}; /* last character searched for */
65static int lastcdir = FORWARD; /* last direction of character search */
66static int last_t_cmd = TRUE; /* last search t_cmd */
Bram Moolenaardbd24b52015-08-11 14:26:19 +020067static char_u lastc_bytes[MB_MAXBYTES + 1];
68static int lastc_bytelen = 1; /* >1 for multi-byte char */
Bram Moolenaardbd24b52015-08-11 14:26:19 +020069
Bram Moolenaar071d4272004-06-13 20:20:40 +000070/* copy of spats[], for keeping the search patterns while executing autocmds */
Bram Moolenaarc3328162019-07-23 22:15:25 +020071static spat_T saved_spats[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +010073static int saved_spats_last_idx = 0;
74static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
77static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
78#ifdef FEAT_RIGHTLEFT
79static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
81
82#ifdef FEAT_FIND_ID
83/*
84 * Type used by find_pattern_in_path() to remember which included files have
85 * been searched already.
86 */
87typedef struct SearchedFile
88{
89 FILE *fp; /* File pointer */
90 char_u *name; /* Full name of file */
91 linenr_T lnum; /* Line we were up to in file */
92 int matched; /* Found a match in this file */
93} SearchedFile;
94#endif
95
96/*
97 * translate search pattern for vim_regcomp()
98 *
99 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
100 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
101 * pat_save == RE_BOTH: save pat in both patterns (:global command)
102 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000103 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
105 * options & SEARCH_HIS: put search string in history
106 * options & SEARCH_KEEP: keep previous search pattern
107 *
108 * returns FAIL if failed, OK otherwise.
109 */
110 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100111search_regcomp(
112 char_u *pat,
113 int pat_save,
114 int pat_use,
115 int options,
116 regmmatch_T *regmatch) /* return: pattern and ignore-case flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117{
118 int magic;
119 int i;
120
121 rc_did_emsg = FALSE;
122 magic = p_magic;
123
124 /*
125 * If no pattern given, use a previously defined pattern.
126 */
127 if (pat == NULL || *pat == NUL)
128 {
129 if (pat_use == RE_LAST)
130 i = last_idx;
131 else
132 i = pat_use;
133 if (spats[i].pat == NULL) /* pattern was never defined */
134 {
135 if (pat_use == RE_SUBST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100136 emsg(_(e_nopresub));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100138 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 rc_did_emsg = TRUE;
140 return FAIL;
141 }
142 pat = spats[i].pat;
143 magic = spats[i].magic;
144 no_smartcase = spats[i].no_scs;
145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 else if (options & SEARCH_HIS) /* put new pattern in history */
147 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148
149#ifdef FEAT_RIGHTLEFT
150 if (mr_pattern_alloced)
151 {
152 vim_free(mr_pattern);
153 mr_pattern_alloced = FALSE;
154 }
155
156 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
157 {
158 char_u *rev_pattern;
159
160 rev_pattern = reverse_text(pat);
161 if (rev_pattern == NULL)
162 mr_pattern = pat; /* out of memory, keep normal pattern. */
163 else
164 {
165 mr_pattern = rev_pattern;
166 mr_pattern_alloced = TRUE;
167 }
168 }
169 else
170#endif
171 mr_pattern = pat;
172
173 /*
174 * Save the currently used pattern in the appropriate place,
175 * unless the pattern should not be remembered.
176 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100177 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 {
179 /* search or global command */
180 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
181 save_re_pat(RE_SEARCH, pat, magic);
182 /* substitute or global command */
183 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
184 save_re_pat(RE_SUBST, pat, magic);
185 }
186
187 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000188 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
190 if (regmatch->regprog == NULL)
191 return FAIL;
192 return OK;
193}
194
195/*
196 * Get search pattern used by search_regcomp().
197 */
198 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100199get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200{
201 return mr_pattern;
202}
203
Bram Moolenaarabc97732007-08-08 20:49:37 +0000204#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205/*
206 * Reverse text into allocated memory.
207 * Returns the allocated string, NULL when out of memory.
208 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000209 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100210reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211{
212 unsigned len;
213 unsigned s_i, rev_i;
214 char_u *rev;
215
216 /*
217 * Reverse the pattern.
218 */
219 len = (unsigned)STRLEN(s);
220 rev = alloc(len + 1);
221 if (rev != NULL)
222 {
223 rev_i = len;
224 for (s_i = 0; s_i < len; ++s_i)
225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 if (has_mbyte)
227 {
228 int mb_len;
229
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000230 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 rev_i -= mb_len;
232 mch_memmove(rev + rev_i, s + s_i, mb_len);
233 s_i += mb_len - 1;
234 }
235 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 rev[--rev_i] = s[s_i];
237
238 }
239 rev[len] = NUL;
240 }
241 return rev;
242}
243#endif
244
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100245 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100246save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247{
248 if (spats[idx].pat != pat)
249 {
250 vim_free(spats[idx].pat);
251 spats[idx].pat = vim_strsave(pat);
252 spats[idx].magic = magic;
253 spats[idx].no_scs = no_smartcase;
254 last_idx = idx;
255#ifdef FEAT_SEARCH_EXTRA
256 /* If 'hlsearch' set and search pat changed: need redraw. */
257 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000258 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200259 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#endif
261 }
262}
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264/*
265 * Save the search patterns, so they can be restored later.
266 * Used before/after executing autocommands and user functions.
267 */
268static int save_level = 0;
269
270 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100271save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272{
273 if (save_level++ == 0)
274 {
275 saved_spats[0] = spats[0];
276 if (spats[0].pat != NULL)
277 saved_spats[0].pat = vim_strsave(spats[0].pat);
278 saved_spats[1] = spats[1];
279 if (spats[1].pat != NULL)
280 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100281#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100282 saved_spats_last_idx = last_idx;
283 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 }
286}
287
288 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100289restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290{
291 if (--save_level == 0)
292 {
293 vim_free(spats[0].pat);
294 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100295#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000296 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 vim_free(spats[1].pat);
299 spats[1] = saved_spats[1];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100300#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100301 last_idx = saved_spats_last_idx;
302 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 }
305}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000307#if defined(EXITFREE) || defined(PROTO)
308 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100309free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000310{
311 vim_free(spats[0].pat);
312 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000313
314# ifdef FEAT_RIGHTLEFT
315 if (mr_pattern_alloced)
316 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200317 vim_free(mr_pattern);
318 mr_pattern_alloced = FALSE;
319 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000320 }
321# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000322}
323#endif
324
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100325#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100326// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
327// searching
Bram Moolenaarc3328162019-07-23 22:15:25 +0200328static spat_T saved_last_search_spat;
Bram Moolenaared8bc782018-12-01 21:08:21 +0100329static int did_save_last_search_spat = 0;
330static int saved_last_idx = 0;
331static int saved_no_hlsearch = 0;
332
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100333/*
334 * Save and restore the search pattern for incremental highlight search
335 * feature.
336 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100337 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100338 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100339 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100340 */
341 void
342save_last_search_pattern(void)
343{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100344 if (did_save_last_search_spat != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100345 iemsg("did_save_last_search_spat is not zero");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100346 else
347 ++did_save_last_search_spat;
348
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100349 saved_last_search_spat = spats[RE_SEARCH];
350 if (spats[RE_SEARCH].pat != NULL)
351 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
352 saved_last_idx = last_idx;
353 saved_no_hlsearch = no_hlsearch;
354}
355
356 void
357restore_last_search_pattern(void)
358{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100359 if (did_save_last_search_spat != 1)
360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100361 iemsg("did_save_last_search_spat is not one");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100362 return;
363 }
364 --did_save_last_search_spat;
365
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100366 vim_free(spats[RE_SEARCH].pat);
367 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100368 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100369# if defined(FEAT_EVAL)
370 set_vv_searchforward();
371# endif
372 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200373 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100374}
Bram Moolenaard0480092017-11-16 22:20:39 +0100375
376 char_u *
377last_search_pattern(void)
378{
379 return spats[RE_SEARCH].pat;
380}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100381#endif
382
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383/*
384 * Return TRUE when case should be ignored for search pattern "pat".
385 * Uses the 'ignorecase' and 'smartcase' options.
386 */
387 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100388ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200390 return ignorecase_opt(pat, p_ic, p_scs);
391}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200393/*
394 * As ignorecase() put pass the "ic" and "scs" flags.
395 */
396 int
397ignorecase_opt(char_u *pat, int ic_in, int scs)
398{
399 int ic = ic_in;
400
401 if (ic && !no_smartcase && scs
Bram Moolenaare2c453d2019-08-21 14:37:09 +0200402 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf))
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200403 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 no_smartcase = FALSE;
405
406 return ic;
407}
408
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200409/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200410 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200411 */
412 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100413pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200414{
415 char_u *p = pat;
416
417 while (*p != NUL)
418 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200419 int l;
420
421 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
422 {
423 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
424 return TRUE;
425 p += l;
426 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100427 else if (*p == '\\')
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200428 {
429 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
430 p += 3;
431 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
432 p += 3;
433 else if (p[1] != NUL) /* skip "\X" */
434 p += 2;
435 else
436 p += 1;
437 }
438 else if (MB_ISUPPER(*p))
439 return TRUE;
440 else
441 ++p;
442 }
443 return FALSE;
444}
445
Bram Moolenaar113e1072019-01-20 15:30:40 +0100446#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100448last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200449{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200450 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200451}
452
453 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100454last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200455{
456 return lastcdir == FORWARD;
457}
458
459 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100460last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200461{
462 return last_t_cmd == TRUE;
463}
464
465 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100466set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200467{
468 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200469 lastc_bytelen = len;
470 if (len)
471 memcpy(lastc_bytes, s, len);
472 else
473 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200474}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100475#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200476
477 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100478set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200479{
480 lastcdir = cdir;
481}
482
483 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100484set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200485{
486 last_t_cmd = t_cmd;
487}
488
489 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100490last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 return spats[last_idx].pat;
493}
494
495/*
496 * Reset search direction to forward. For "gd" and "gD" commands.
497 */
498 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100499reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#if defined(FEAT_EVAL)
503 set_vv_searchforward();
504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505}
506
507#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
508/*
509 * Set the last search pattern. For ":let @/ =" and viminfo.
510 * Also set the saved search pattern, so that this works in an autocommand.
511 */
512 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100513set_last_search_pat(
514 char_u *s,
515 int idx,
516 int magic,
517 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 vim_free(spats[idx].pat);
520 /* An empty string means that nothing should be matched. */
521 if (*s == NUL)
522 spats[idx].pat = NULL;
523 else
524 spats[idx].pat = vim_strsave(s);
525 spats[idx].magic = magic;
526 spats[idx].no_scs = FALSE;
527 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#if defined(FEAT_EVAL)
529 set_vv_searchforward();
530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 spats[idx].off.line = FALSE;
532 spats[idx].off.end = FALSE;
533 spats[idx].off.off = 0;
534 if (setlast)
535 last_idx = idx;
536 if (save_level)
537 {
538 vim_free(saved_spats[idx].pat);
539 saved_spats[idx] = spats[0];
540 if (spats[idx].pat == NULL)
541 saved_spats[idx].pat = NULL;
542 else
543 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100544# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100545 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100546# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
548# ifdef FEAT_SEARCH_EXTRA
549 /* If 'hlsearch' set and search pat changed: need redraw. */
550 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000551 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552# endif
553}
554#endif
555
556#ifdef FEAT_SEARCH_EXTRA
557/*
558 * Get a regexp program for the last used search pattern.
559 * This is used for highlighting all matches in a window.
560 * Values returned in regmatch->regprog and regmatch->rmm_ic.
561 */
562 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100563last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 if (spats[last_idx].pat == NULL)
566 {
567 regmatch->regprog = NULL;
568 return;
569 }
570 ++emsg_off; /* So it doesn't beep if bad expr */
571 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
572 --emsg_off;
573}
574#endif
575
576/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100577 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100578 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
579 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 *
581 * if (options & SEARCH_MSG) == 0 don't give any messages
582 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
583 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
584 * if (options & SEARCH_HIS) put search pattern in history
585 * if (options & SEARCH_END) return position at end of match
586 * if (options & SEARCH_START) accept match at pos itself
587 * if (options & SEARCH_KEEP) keep previous search pattern
588 * if (options & SEARCH_FOLD) match only once in a closed fold
589 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100590 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 *
592 * Return FAIL (zero) for failure, non-zero for success.
593 * When FEAT_EVAL is defined, returns the index of the first matching
594 * subpattern plus one; one if there was none.
595 */
596 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100597searchit(
598 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100600 buf_T *buf,
601 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100602 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100603 int dir,
604 char_u *pat,
605 long count,
606 int options,
607 int pat_use, /* which pattern to use when "pat" is empty */
608 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200609 proftime_T *tm UNUSED, /* timeout limit or NULL */
610 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 int found;
613 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100614 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 regmmatch_T regmatch;
616 char_u *ptr;
617 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000619 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 int loop;
621 pos_T start_pos;
622 int at_first_line;
623 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200624 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 int match_ok;
626 long nmatched;
627 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100628 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000629 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#ifdef FEAT_SEARCH_EXTRA
631 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633
634 if (search_regcomp(pat, RE_SEARCH, pat_use,
635 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
636 {
637 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100638 semsg(_("E383: Invalid search string: %s"), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 return FAIL;
640 }
641
Bram Moolenaar280f1262006-01-30 00:14:18 +0000642 /*
643 * find the string
644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 called_emsg = FALSE;
646 do /* loop for count */
647 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100648 /* When not accepting a match at the start position set "extra_col" to
649 * a non-zero value. Don't do that when starting at MAXCOL, since
650 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200651 if (pos->col == MAXCOL)
652 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100653 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200654 else if (has_mbyte
655 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
656 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100657 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100658 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100659 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200660 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100661 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100662 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100663 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100664 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200665 start_char_len = 1;
666 if (dir == FORWARD)
667 {
668 if (options & SEARCH_START)
669 extra_col = 0;
670 else
671 extra_col = start_char_len;
672 }
673 else
674 {
675 if (options & SEARCH_START)
676 extra_col = start_char_len;
677 else
678 extra_col = 0;
679 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 start_pos = *pos; /* remember start pos for detecting no match */
682 found = 0; /* default: not found */
683 at_first_line = TRUE; /* default: start in first line */
684 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
685 {
686 pos->lnum = 1;
687 pos->col = 0;
688 at_first_line = FALSE; /* not in first line now */
689 }
690
691 /*
692 * Start searching in current line, unless searching backwards and
693 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000694 * If we are searching backwards, in column 0, and not including the
695 * current position, gain some efficiency by skipping back a line.
696 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000698 if (dir == BACKWARD && start_pos.col == 0
699 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 lnum = pos->lnum - 1;
702 at_first_line = FALSE;
703 }
704 else
705 lnum = pos->lnum;
706
707 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
708 {
709 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
710 lnum += dir, at_first_line = FALSE)
711 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000712 /* Stop after checking "stop_lnum", if it's set. */
713 if (stop_lnum != 0 && (dir == FORWARD
714 ? lnum > stop_lnum : lnum < stop_lnum))
715 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000716#ifdef FEAT_RELTIME
717 /* Stop after passing the "tm" time limit. */
718 if (tm != NULL && profile_passed_limit(tm))
719 break;
720#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000721
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000723 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100725 col = at_first_line && (options & SEARCH_COL) ? pos->col
726 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100728 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000729#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200730 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000731#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200732 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000733#endif
734 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200736 if (called_emsg
737#ifdef FEAT_RELTIME
738 || (timed_out != NULL && *timed_out)
739#endif
740 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 break;
742 if (nmatched > 0)
743 {
744 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000745 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000747#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000749#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000750 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000751 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
752 ptr = (char_u *)"";
753 else
754 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756 /*
757 * Forward search in the first line: match should be after
758 * the start position. If not, continue at the end of the
759 * match (this is vi compatible) or on the next char.
760 */
761 if (dir == FORWARD && at_first_line)
762 {
763 match_ok = TRUE;
764 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000765 * When the match starts in a next line it's certainly
766 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 * When match lands on a NUL the cursor will be put
768 * one back afterwards, compare with that position,
769 * otherwise "/$" will get stuck on end of line.
770 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000771 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100772 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000773 ? (nmatched == 1
774 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000776 : ((int)matchpos.col
777 - (ptr[matchpos.col] == NUL)
778 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 {
780 /*
781 * If vi-compatible searching, continue at the end
782 * of the match, otherwise continue one position
783 * forward.
784 */
785 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
786 {
787 if (nmatched > 1)
788 {
789 /* end is in next line, thus no match in
790 * this line */
791 match_ok = FALSE;
792 break;
793 }
794 matchcol = endpos.col;
795 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000796 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 && ptr[matchcol] != NUL)
798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (has_mbyte)
800 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000801 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 ++matchcol;
804 }
805 }
806 else
807 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000808 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 if (ptr[matchcol] != NUL)
810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000812 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 + matchcol);
814 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 ++matchcol;
816 }
817 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200818 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (ptr[matchcol] == NUL
821 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000822 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000823 matchcol,
824#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200825 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000826#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200827 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000828#endif
829 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 {
831 match_ok = FALSE;
832 break;
833 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000834 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 endpos = regmatch.endpos[0];
836# ifdef FEAT_EVAL
837 submatch = first_submatch(&regmatch);
838# endif
839
840 /* Need to get the line pointer again, a
841 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000842 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 if (!match_ok)
845 continue;
846 }
847 if (dir == BACKWARD)
848 {
849 /*
850 * Now, if there are multiple matches on this line,
851 * we have to get the last one. Or the last one before
852 * the cursor, if we're on that line.
853 * When putting the new cursor at the end, compare
854 * relative to the end of the match.
855 */
856 match_ok = FALSE;
857 for (;;)
858 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000859 /* Remember a position that is before the start
860 * position, we use it if it's the last match in
861 * the line. Always accept a position after
862 * wrapping around. */
863 if (loop
864 || ((options & SEARCH_END)
865 ? (lnum + regmatch.endpos[0].lnum
866 < start_pos.lnum
867 || (lnum + regmatch.endpos[0].lnum
868 == start_pos.lnum
869 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200870 < (int)start_pos.col
871 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000872 : (lnum + regmatch.startpos[0].lnum
873 < start_pos.lnum
874 || (lnum + regmatch.startpos[0].lnum
875 == start_pos.lnum
876 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200877 < (int)start_pos.col
878 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000881 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 endpos = regmatch.endpos[0];
883# ifdef FEAT_EVAL
884 submatch = first_submatch(&regmatch);
885# endif
886 }
887 else
888 break;
889
890 /*
891 * We found a valid match, now check if there is
892 * another one after it.
893 * If vi-compatible searching, continue at the end
894 * of the match, otherwise continue one position
895 * forward.
896 */
897 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
898 {
899 if (nmatched > 1)
900 break;
901 matchcol = endpos.col;
902 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000903 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 && ptr[matchcol] != NUL)
905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (has_mbyte)
907 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000908 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 ++matchcol;
911 }
912 }
913 else
914 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000915 /* Stop when the match is in a next line. */
916 if (matchpos.lnum > 0)
917 break;
918 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 if (ptr[matchcol] != NUL)
920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (has_mbyte)
922 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000923 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 ++matchcol;
926 }
927 }
928 if (ptr[matchcol] == NUL
929 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000930 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000931 matchcol,
932#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200933 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000934#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200935 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000936#endif
937 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100938 {
939#ifdef FEAT_RELTIME
940 /* If the search timed out, we did find a match
941 * but it might be the wrong one, so that's not
942 * OK. */
943 if (timed_out != NULL && *timed_out)
944 match_ok = FALSE;
945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 /* Need to get the line pointer again, a
950 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000951 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953
954 /*
955 * If there is only a match after the cursor, skip
956 * this match.
957 */
958 if (!match_ok)
959 continue;
960 }
961
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000962 /* With the SEARCH_END option move to the last character
963 * of the match. Don't do it for an empty match, end
964 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200965 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000966 && !(matchpos.lnum == endpos.lnum
967 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000969 /* For a match in the first column, set the position
970 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000971 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000972 pos->col = endpos.col;
973 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000974 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000975 if (pos->lnum > 1) /* just in case */
976 {
977 --pos->lnum;
978 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
979 pos->lnum, FALSE));
980 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000981 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000982 else
983 {
984 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000985 if (has_mbyte
986 && pos->lnum <= buf->b_ml.ml_line_count)
987 {
988 ptr = ml_get_buf(buf, pos->lnum, FALSE);
989 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
990 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000991 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100992 if (end_pos != NULL)
993 {
994 end_pos->lnum = lnum + matchpos.lnum;
995 end_pos->col = matchpos.col;
996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 }
998 else
999 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001000 pos->lnum = lnum + matchpos.lnum;
1001 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001002 if (end_pos != NULL)
1003 {
1004 end_pos->lnum = lnum + endpos.lnum;
1005 end_pos->col = endpos.col;
1006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001009 if (end_pos != NULL)
1010 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001012 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001015 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 search_match_endcol = endpos.col;
1017 break;
1018 }
1019 line_breakcheck(); /* stop if ctrl-C typed */
1020 if (got_int)
1021 break;
1022
1023#ifdef FEAT_SEARCH_EXTRA
1024 /* Cancel searching if a character was typed. Used for
1025 * 'incsearch'. Don't check too often, that would slowdown
1026 * searching too much. */
1027 if ((options & SEARCH_PEEK)
1028 && ((lnum - pos->lnum) & 0x3f) == 0
1029 && char_avail())
1030 {
1031 break_loop = TRUE;
1032 break;
1033 }
1034#endif
1035
1036 if (loop && lnum == start_pos.lnum)
1037 break; /* if second loop, stop where started */
1038 }
1039 at_first_line = FALSE;
1040
1041 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001042 * Stop the search if wrapscan isn't set, "stop_lnum" is
1043 * specified, after an interrupt, after a match and after looping
1044 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001046 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001047#ifdef FEAT_RELTIME
1048 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001049#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001050#ifdef FEAT_SEARCH_EXTRA
1051 || break_loop
1052#endif
1053 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 break;
1055
1056 /*
1057 * If 'wrapscan' is set we continue at the other end of the file.
1058 * If 'shortmess' does not contain 's', we give a message.
1059 * This message is also remembered in keep_msg for when the screen
1060 * is redrawn. The keep_msg is cleared whenever another message is
1061 * written.
1062 */
1063 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001067 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1068 give_warning((char_u *)_(dir == BACKWARD
1069 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001071 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001072#ifdef FEAT_RELTIME
1073 || (timed_out != NULL && *timed_out)
1074#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001075#ifdef FEAT_SEARCH_EXTRA
1076 || break_loop
1077#endif
1078 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 break;
1080 }
1081 while (--count > 0 && found); /* stop after count matches or no match */
1082
Bram Moolenaar473de612013-06-08 18:19:48 +02001083 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar280f1262006-01-30 00:14:18 +00001085 called_emsg |= save_called_emsg;
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (!found) /* did not find it */
1088 {
1089 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001090 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1092 {
1093 if (p_ws)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001094 semsg(_(e_patnotf2), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 else if (lnum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001096 semsg(_("E384: search hit TOP without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 mr_pattern);
1098 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001099 semsg(_("E385: search hit BOTTOM without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 mr_pattern);
1101 }
1102 return FAIL;
1103 }
1104
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001105 /* A pattern like "\n\zs" may go past the last line. */
1106 if (pos->lnum > buf->b_ml.ml_line_count)
1107 {
1108 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001109 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001110 if (pos->col > 0)
1111 --pos->col;
1112 }
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 return submatch + 1;
1115}
1116
1117#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001118 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001119set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001120{
1121 spats[0].off.dir = cdir;
1122}
1123
1124 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001125set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001126{
1127 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1128}
1129
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130/*
1131 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001132 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 */
1134 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001135first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 int submatch;
1138
1139 for (submatch = 1; ; ++submatch)
1140 {
1141 if (rp->startpos[submatch].lnum >= 0)
1142 break;
1143 if (submatch == 9)
1144 {
1145 submatch = 0;
1146 break;
1147 }
1148 }
1149 return submatch;
1150}
1151#endif
1152
1153/*
1154 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001155 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * If 'dirc' is 0: use previous dir.
1157 * If 'pat' is NULL or empty : use previous string.
1158 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1159 * If 'options & SEARCH_ECHO': echo the search command and handle options
1160 * If 'options & SEARCH_MSG' : may give error message
1161 * If 'options & SEARCH_OPT' : interpret optional flags
1162 * If 'options & SEARCH_HIS' : put search pattern in history
1163 * If 'options & SEARCH_NOOF': don't add offset to position
1164 * If 'options & SEARCH_MARK': set previous context mark
1165 * If 'options & SEARCH_KEEP': keep previous search pattern
1166 * If 'options & SEARCH_START': accept match at curpos itself
1167 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1168 *
1169 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1170 * makes the movement linewise without moving the match position.
1171 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001172 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 */
1174 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001175do_search(
1176 oparg_T *oap, /* can be NULL */
1177 int dirc, /* '/' or '?' */
1178 char_u *pat,
1179 long count,
1180 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001181 proftime_T *tm, /* timeout limit or NULL */
1182 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183{
1184 pos_T pos; /* position of the last match */
1185 char_u *searchstr;
Bram Moolenaarc3328162019-07-23 22:15:25 +02001186 soffset_T old_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 int retval; /* Return value */
1188 char_u *p;
1189 long c;
1190 char_u *dircp;
1191 char_u *strcopy = NULL;
1192 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001193 char_u *msgbuf = NULL;
1194 size_t len;
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001195 int has_offset = FALSE;
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02001196#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
1198 /*
1199 * A line offset is not remembered, this is vi compatible.
1200 */
1201 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1202 {
1203 spats[0].off.line = FALSE;
1204 spats[0].off.off = 0;
1205 }
1206
1207 /*
1208 * Save the values for when (options & SEARCH_KEEP) is used.
1209 * (there is no "if ()" around this because gcc wants them initialized)
1210 */
1211 old_off = spats[0].off;
1212
1213 pos = curwin->w_cursor; /* start searching at the cursor position */
1214
1215 /*
1216 * Find out the direction of the search.
1217 */
1218 if (dirc == 0)
1219 dirc = spats[0].off.dir;
1220 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001223#if defined(FEAT_EVAL)
1224 set_vv_searchforward();
1225#endif
1226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (options & SEARCH_REV)
1228 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001229#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 /* There is a bug in the Visual C++ 2.2 compiler which means that
1231 * dirc always ends up being '/' */
1232 dirc = (dirc == '/') ? '?' : '/';
1233#else
1234 if (dirc == '/')
1235 dirc = '?';
1236 else
1237 dirc = '/';
1238#endif
1239 }
1240
1241#ifdef FEAT_FOLDING
1242 /* If the cursor is in a closed fold, don't find another match in the same
1243 * fold. */
1244 if (dirc == '/')
1245 {
1246 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1247 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1248 }
1249 else
1250 {
1251 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1252 pos.col = 0;
1253 }
1254#endif
1255
1256#ifdef FEAT_SEARCH_EXTRA
1257 /*
1258 * Turn 'hlsearch' highlighting back on.
1259 */
1260 if (no_hlsearch && !(options & SEARCH_KEEP))
1261 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001262 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001263 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265#endif
1266
1267 /*
1268 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1269 */
1270 for (;;)
1271 {
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001272 int show_top_bot_msg = FALSE;
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 searchstr = pat;
1275 dircp = NULL;
1276 /* use previous pattern */
1277 if (pat == NULL || *pat == NUL || *pat == dirc)
1278 {
1279 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1280 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001281 searchstr = spats[RE_SUBST].pat;
1282 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001284 emsg(_(e_noprevre));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001285 retval = 0;
1286 goto end_do_search;
1287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001289 else
1290 {
1291 /* make search_regcomp() use spats[RE_SEARCH].pat */
1292 searchstr = (char_u *)"";
1293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295
1296 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1297 {
1298 /*
1299 * Find end of regular expression.
1300 * If there is a matching '/' or '?', toss it.
1301 */
1302 ps = strcopy;
1303 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1304 if (strcopy != ps)
1305 {
1306 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001307 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 pat = strcopy;
1309 searchstr = strcopy;
1310 }
1311 if (*p == dirc)
1312 {
1313 dircp = p; /* remember where we put the NUL */
1314 *p++ = NUL;
1315 }
1316 spats[0].off.line = FALSE;
1317 spats[0].off.end = FALSE;
1318 spats[0].off.off = 0;
1319 /*
1320 * Check for a line offset or a character offset.
1321 * For get_address (echo off) we don't check for a character
1322 * offset, because it is meaningless and the 's' could be a
1323 * substitute command.
1324 */
1325 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1326 spats[0].off.line = TRUE;
1327 else if ((options & SEARCH_OPT) &&
1328 (*p == 'e' || *p == 's' || *p == 'b'))
1329 {
1330 if (*p == 'e') /* end */
1331 spats[0].off.end = SEARCH_END;
1332 ++p;
1333 }
1334 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1335 {
1336 /* 'nr' or '+nr' or '-nr' */
1337 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1338 spats[0].off.off = atol((char *)p);
1339 else if (*p == '-') /* single '-' */
1340 spats[0].off.off = -1;
1341 else /* single '+' */
1342 spats[0].off.off = 1;
1343 ++p;
1344 while (VIM_ISDIGIT(*p)) /* skip number */
1345 ++p;
1346 }
1347
1348 /* compute length of search command for get_address() */
1349 searchcmdlen += (int)(p - pat);
1350
1351 pat = p; /* put pat after search command */
1352 }
1353
1354 if ((options & SEARCH_ECHO) && messaging()
1355 && !cmd_silent && msg_silent == 0)
1356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001358 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001359 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001361 // Compute msg_row early.
1362 msg_start();
1363
Bram Moolenaar984f0312019-05-24 13:11:47 +02001364 // Get the offset, so we know how long it is.
1365 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1366 {
1367 p = off_buf;
1368 *p++ = dirc;
1369 if (spats[0].off.end)
1370 *p++ = 'e';
1371 else if (!spats[0].off.line)
1372 *p++ = 's';
1373 if (spats[0].off.off > 0 || spats[0].off.line)
1374 *p++ = '+';
1375 *p = NUL;
1376 if (spats[0].off.off != 0 || spats[0].off.line)
1377 sprintf((char *)p, "%ld", spats[0].off.off);
1378 off_len = STRLEN(off_buf);
1379 }
1380
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001382 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 else
1384 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001385
1386 if (!shortmess(SHM_SEARCHCOUNT))
1387 {
1388 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001389 // search stat. Use all the space available, so that the
1390 // search state is right aligned. If there is not enough space
1391 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001392 if (msg_scrolled != 0)
1393 // Use all the columns.
1394 len = (int)(Rows - msg_row) * Columns - 1;
1395 else
1396 // Use up to 'showcmd' column.
1397 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001398 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1399 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001400 }
1401 else
1402 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001403 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001404
Bram Moolenaar51e14382019-05-25 20:21:28 +02001405 msgbuf = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (msgbuf != NULL)
1407 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001408 vim_memset(msgbuf, ' ', len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 msgbuf[0] = dirc;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001410 msgbuf[len - 1] = NUL;
1411
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001412 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1413 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001414 // Use a space to draw the composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001415 msgbuf[1] = ' ';
Bram Moolenaarb3de6c42019-05-05 13:02:28 +02001416 mch_memmove(msgbuf + 2, p, STRLEN(p));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001417 }
1418 else
Bram Moolenaarb3de6c42019-05-05 13:02:28 +02001419 mch_memmove(msgbuf + 1, p, STRLEN(p));
Bram Moolenaar984f0312019-05-24 13:11:47 +02001420 if (off_len > 0)
1421 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
Bram Moolenaar984f0312019-05-24 13:11:47 +02001423 trunc = msg_strtrunc(msgbuf, TRUE);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001424 if (trunc != NULL)
1425 {
1426 vim_free(msgbuf);
1427 msgbuf = trunc;
1428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430#ifdef FEAT_RIGHTLEFT
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001431 // The search pattern could be shown on the right in rightleft
1432 // mode, but the 'ruler' and 'showcmd' area use it too, thus
1433 // it would be blanked out again very soon. Show it on the
1434 // left, but do reverse the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1436 {
1437 char_u *r;
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001438 size_t pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001440 r = reverse_text(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (r != NULL)
1442 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001443 vim_free(msgbuf);
1444 msgbuf = r;
1445 // move reversed text to beginning of buffer
1446 while (*r != NUL && *r == ' ')
1447 r++;
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001448 pat_len = msgbuf + STRLEN(msgbuf) - r;
1449 mch_memmove(msgbuf, r, pat_len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001450 // overwrite old text
Bram Moolenaardb294ad2019-06-06 12:49:29 +02001451 if ((size_t)(r - msgbuf) >= pat_len)
1452 vim_memset(r, ' ', pat_len);
1453 else
1454 vim_memset(msgbuf + pat_len, ' ', r - msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
1456 }
1457#endif
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001458 msg_outtrans(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 msg_clr_eos();
1460 msg_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
1462 gotocmdline(FALSE);
1463 out_flush();
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001464 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466 }
1467
1468 /*
1469 * If there is a character offset, subtract it from the current
1470 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001471 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 * This is not done for a line offset, because then we would not be vi
1473 * compatible.
1474 */
Bram Moolenaared203462004-06-16 11:19:22 +00001475 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 {
1477 if (spats[0].off.off > 0)
1478 {
1479 for (c = spats[0].off.off; c; --c)
1480 if (decl(&pos) == -1)
1481 break;
1482 if (c) /* at start of buffer */
1483 {
1484 pos.lnum = 0; /* allow lnum == 0 here */
1485 pos.col = MAXCOL;
1486 }
1487 }
1488 else
1489 {
1490 for (c = spats[0].off.off; c; ++c)
1491 if (incl(&pos) == -1)
1492 break;
1493 if (c) /* at end of buffer */
1494 {
1495 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1496 pos.col = 0;
1497 }
1498 }
1499 }
1500
Bram Moolenaar14184a32019-02-16 15:10:30 +01001501 c = searchit(curwin, curbuf, &pos, NULL,
1502 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 searchstr, count, spats[0].off.end + (options &
1504 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1505 + SEARCH_MSG + SEARCH_START
1506 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001507 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
1509 if (dircp != NULL)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001510 *dircp = dirc; // restore second '/' or '?' for normal_cmd()
1511
1512 if (!shortmess(SHM_SEARCH)
1513 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1514 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001515 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 if (c == FAIL)
1518 {
1519 retval = 0;
1520 goto end_do_search;
1521 }
1522 if (spats[0].off.end && oap != NULL)
1523 oap->inclusive = TRUE; /* 'e' includes last character */
1524
1525 retval = 1; /* pattern found */
1526
1527 /*
1528 * Add character and/or line offset
1529 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001530 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001532 pos_T org_pos = pos;
1533
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 if (spats[0].off.line) /* Add the offset to the line number. */
1535 {
1536 c = pos.lnum + spats[0].off.off;
1537 if (c < 1)
1538 pos.lnum = 1;
1539 else if (c > curbuf->b_ml.ml_line_count)
1540 pos.lnum = curbuf->b_ml.ml_line_count;
1541 else
1542 pos.lnum = c;
1543 pos.col = 0;
1544
1545 retval = 2; /* pattern found, line offset added */
1546 }
Bram Moolenaared203462004-06-16 11:19:22 +00001547 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 {
1549 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001550 c = spats[0].off.off;
1551 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001553 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 if (incl(&pos) == -1)
1555 break;
1556 }
1557 /* to the left, check for start of file */
1558 else
1559 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560 while (c++ < 0)
1561 if (decl(&pos) == -1)
1562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 }
1564 }
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001565 if (!EQUAL_POS(pos, org_pos))
1566 has_offset = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001569 // Show [1/15] if 'S' is not in 'shortmess'.
1570 if ((options & SEARCH_ECHO)
1571 && messaging()
1572 && !(cmd_silent + msg_silent)
1573 && c != FAIL
1574 && !shortmess(SHM_SEARCHCOUNT)
1575 && msgbuf != NULL)
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02001576 search_stat(dirc, &pos, show_top_bot_msg, msgbuf,
1577 (count != 1 || has_offset));
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001578
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 /*
1580 * The search command can be followed by a ';' to do another search.
1581 * For example: "/pat/;/foo/+3;?bar"
1582 * This is like doing another search command, except:
1583 * - The remembered direction '/' or '?' is from the first search.
1584 * - When an error happens the cursor isn't moved at all.
1585 * Don't do this when called by get_address() (it handles ';' itself).
1586 */
1587 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1588 break;
1589
1590 dirc = *++pat;
1591 if (dirc != '?' && dirc != '/')
1592 {
1593 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 goto end_do_search;
1596 }
1597 ++pat;
1598 }
1599
1600 if (options & SEARCH_MARK)
1601 setpcmark();
1602 curwin->w_cursor = pos;
1603 curwin->w_set_curswant = TRUE;
1604
1605end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001606 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 spats[0].off = old_off;
1608 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001609 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
1611 return retval;
1612}
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614/*
1615 * search_for_exact_line(buf, pos, dir, pat)
1616 *
1617 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001618 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001620 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 * Return OK for success, or FAIL if no line found.
1622 */
1623 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001624search_for_exact_line(
1625 buf_T *buf,
1626 pos_T *pos,
1627 int dir,
1628 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 linenr_T start = 0;
1631 char_u *ptr;
1632 char_u *p;
1633
1634 if (buf->b_ml.ml_line_count == 0)
1635 return FAIL;
1636 for (;;)
1637 {
1638 pos->lnum += dir;
1639 if (pos->lnum < 1)
1640 {
1641 if (p_ws)
1642 {
1643 pos->lnum = buf->b_ml.ml_line_count;
1644 if (!shortmess(SHM_SEARCH))
1645 give_warning((char_u *)_(top_bot_msg), TRUE);
1646 }
1647 else
1648 {
1649 pos->lnum = 1;
1650 break;
1651 }
1652 }
1653 else if (pos->lnum > buf->b_ml.ml_line_count)
1654 {
1655 if (p_ws)
1656 {
1657 pos->lnum = 1;
1658 if (!shortmess(SHM_SEARCH))
1659 give_warning((char_u *)_(bot_top_msg), TRUE);
1660 }
1661 else
1662 {
1663 pos->lnum = 1;
1664 break;
1665 }
1666 }
1667 if (pos->lnum == start)
1668 break;
1669 if (start == 0)
1670 start = pos->lnum;
1671 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1672 p = skipwhite(ptr);
1673 pos->col = (colnr_T) (p - ptr);
1674
1675 /* when adding lines the matching line may be empty but it is not
1676 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001677 if ((compl_cont_status & CONT_ADDING)
1678 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1681 return OK;
1682 }
1683 else if (*p != NUL) /* ignore empty lines */
1684 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001685 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1686 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return OK;
1688 }
1689 }
1690 return FAIL;
1691}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693/*
1694 * Character Searches
1695 */
1696
1697/*
1698 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1699 * position of the character, otherwise move to just before the char.
1700 * Do this "cap->count1" times.
1701 * Return FAIL or OK.
1702 */
1703 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001704searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 int c = cap->nchar; /* char to search for */
1707 int dir = cap->arg; /* TRUE for searching forward */
1708 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 int col;
1710 char_u *p;
1711 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001712 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714 if (c != NUL) /* normal search: remember args for repeat */
1715 {
1716 if (!KeyStuffed) /* don't remember when redoing */
1717 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001718 *lastc = c;
1719 set_csearch_direction(dir);
1720 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001721 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 if (cap->ncharC1 != 0)
1723 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001724 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1725 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001727 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1728 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
1731 }
1732 else /* repeat previous search */
1733 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001734 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return FAIL;
1736 if (dir) /* repeat in opposite direction */
1737 dir = -lastcdir;
1738 else
1739 dir = lastcdir;
1740 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001741 c = *lastc;
1742 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001743
1744 /* Force a move of at least one char, so ";" and "," will move the
1745 * cursor, even if the cursor is right in front of char we are looking
1746 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001747 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001748 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001751 if (dir == BACKWARD)
1752 cap->oap->inclusive = FALSE;
1753 else
1754 cap->oap->inclusive = TRUE;
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 p = ml_get_curline();
1757 col = curwin->w_cursor.col;
1758 len = (int)STRLEN(p);
1759
1760 while (count--)
1761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (has_mbyte)
1763 {
1764 for (;;)
1765 {
1766 if (dir > 0)
1767 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001768 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 if (col >= len)
1770 return FAIL;
1771 }
1772 else
1773 {
1774 if (col == 0)
1775 return FAIL;
1776 col -= (*mb_head_off)(p, p + col - 1) + 1;
1777 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001778 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001780 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 break;
1782 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001783 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001784 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001785 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001786 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 }
1788 }
1789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 {
1791 for (;;)
1792 {
1793 if ((col += dir) < 0 || col >= len)
1794 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001795 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001797 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 }
1799 }
1800 }
1801
1802 if (t_cmd)
1803 {
1804 /* backup to before the character (possibly double-byte) */
1805 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (has_mbyte)
1807 {
1808 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001809 /* Landed on the search char which is lastc_bytelen long */
1810 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 else
1812 /* To previous char, which may be multi-byte. */
1813 col -= (*mb_head_off)(p, p + col);
1814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816 curwin->w_cursor.col = col;
1817
1818 return OK;
1819}
1820
1821/*
1822 * "Other" Searches
1823 */
1824
1825/*
1826 * findmatch - find the matching paren or brace
1827 *
1828 * Improvement over vi: Braces inside quotes are ignored.
1829 */
1830 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001831findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832{
1833 return findmatchlimit(oap, initc, 0, 0);
1834}
1835
1836/*
1837 * Return TRUE if the character before "linep[col]" equals "ch".
1838 * Return FALSE if "col" is zero.
1839 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1840 * is NULL.
1841 * Handles multibyte string correctly.
1842 */
1843 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001844check_prevcol(
1845 char_u *linep,
1846 int col,
1847 int ch,
1848 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
1850 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (col > 0 && has_mbyte)
1852 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (prevcol)
1854 *prevcol = col;
1855 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1856}
1857
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001858/*
1859 * Raw string start is found at linep[startpos.col - 1].
1860 * Return TRUE if the matching end can be found between startpos and endpos.
1861 */
1862 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001863find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001864{
1865 char_u *p;
1866 char_u *delim_copy;
1867 size_t delim_len;
1868 linenr_T lnum;
1869 int found = FALSE;
1870
1871 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1872 ;
1873 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001874 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001875 if (delim_copy == NULL)
1876 return FALSE;
1877 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1878 {
1879 char_u *line = ml_get(lnum);
1880
1881 for (p = line + (lnum == startpos->lnum
1882 ? startpos->col + 1 : 0); *p; ++p)
1883 {
1884 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1885 break;
1886 if (*p == ')' && p[delim_len + 1] == '"'
1887 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1888 {
1889 found = TRUE;
1890 break;
1891 }
1892 }
1893 if (found)
1894 break;
1895 }
1896 vim_free(delim_copy);
1897 return found;
1898}
1899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900/*
1901 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001902 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1903 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 *
1905 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001906 * character at or after the cursor. Special values:
1907 * '*' look for C-style comment / *
1908 * '/' look for C-style comment / *, ignoring comment-end
1909 * '#' look for preprocessor directives
1910 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 *
1912 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1913 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1914 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1915 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001916 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001917 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001918 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 */
1920
1921 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001922findmatchlimit(
1923 oparg_T *oap,
1924 int initc,
1925 int flags,
1926 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927{
1928 static pos_T pos; /* current search position */
1929 int findc = 0; /* matching brace */
1930 int c;
1931 int count = 0; /* cumulative number of braces */
1932 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001933 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 int inquote = FALSE; /* TRUE when inside quotes */
1935 char_u *linep; /* pointer to current line */
1936 char_u *ptr;
1937 int do_quotes; /* check for quotes in current line */
1938 int at_start; /* do_quotes value at start position */
1939 int hash_dir = 0; /* Direction searched for # things */
1940 int comment_dir = 0; /* Direction searched for comments */
1941 pos_T match_pos; /* Where last slash-star was found */
1942 int start_in_quotes; /* start position is in quotes */
1943 int traveled = 0; /* how far we've searched so far */
1944 int ignore_cend = FALSE; /* ignore comment end */
1945 int cpo_match; /* vi compatible matching */
1946 int cpo_bsl; /* don't recognize backslashes */
1947 int match_escaped = 0; /* search for escaped match */
1948 int dir; /* Direction to search */
1949 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001950#ifdef FEAT_LISP
1951 int lispcomm = FALSE; /* inside of Lisp-style comment */
1952 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001956 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 linep = ml_get(pos.lnum);
1958
1959 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1960 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1961
1962 /* Direction to search when initc is '/', '*' or '#' */
1963 if (flags & FM_BACKWARD)
1964 dir = BACKWARD;
1965 else if (flags & FM_FORWARD)
1966 dir = FORWARD;
1967 else
1968 dir = 0;
1969
1970 /*
1971 * if initc given, look in the table for the matching character
1972 * '/' and '*' are special cases: look for start or end of comment.
1973 * When '/' is used, we ignore running backwards into an star-slash, for
1974 * "[*" command, we just want to find any comment.
1975 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001976 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 comment_dir = dir;
1979 if (initc == '/')
1980 ignore_cend = TRUE;
1981 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001982 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 initc = NUL;
1984 }
1985 else if (initc != '#' && initc != NUL)
1986 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001987 find_mps_values(&initc, &findc, &backwards, TRUE);
1988 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 return NULL;
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 else
1992 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001993 /*
1994 * Either initc is '#', or no initc was given and we need to look
1995 * under the cursor.
1996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (initc == '#')
1998 {
1999 hash_dir = dir;
2000 }
2001 else
2002 {
2003 /*
2004 * initc was not given, must look for something to match under
2005 * or near the cursor.
2006 * Only check for special things when 'cpo' doesn't have '%'.
2007 */
2008 if (!cpo_match)
2009 {
2010 /* Are we before or at #if, #else etc.? */
2011 ptr = skipwhite(linep);
2012 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2013 {
2014 ptr = skipwhite(ptr + 1);
2015 if ( STRNCMP(ptr, "if", 2) == 0
2016 || STRNCMP(ptr, "endif", 5) == 0
2017 || STRNCMP(ptr, "el", 2) == 0)
2018 hash_dir = 1;
2019 }
2020
2021 /* Are we on a comment? */
2022 else if (linep[pos.col] == '/')
2023 {
2024 if (linep[pos.col + 1] == '*')
2025 {
2026 comment_dir = FORWARD;
2027 backwards = FALSE;
2028 pos.col++;
2029 }
2030 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2031 {
2032 comment_dir = BACKWARD;
2033 backwards = TRUE;
2034 pos.col--;
2035 }
2036 }
2037 else if (linep[pos.col] == '*')
2038 {
2039 if (linep[pos.col + 1] == '/')
2040 {
2041 comment_dir = BACKWARD;
2042 backwards = TRUE;
2043 }
2044 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2045 {
2046 comment_dir = FORWARD;
2047 backwards = FALSE;
2048 }
2049 }
2050 }
2051
2052 /*
2053 * If we are not on a comment or the # at the start of a line, then
2054 * look for brace anywhere on this line after the cursor.
2055 */
2056 if (!hash_dir && !comment_dir)
2057 {
2058 /*
2059 * Find the brace under or after the cursor.
2060 * If beyond the end of the line, use the last character in
2061 * the line.
2062 */
2063 if (linep[pos.col] == NUL && pos.col)
2064 --pos.col;
2065 for (;;)
2066 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002067 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (initc == NUL)
2069 break;
2070
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002071 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 if (findc)
2073 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002074 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076 if (!findc)
2077 {
2078 /* no brace in the line, maybe use " #if" then */
2079 if (!cpo_match && *skipwhite(linep) == '#')
2080 hash_dir = 1;
2081 else
2082 return NULL;
2083 }
2084 else if (!cpo_bsl)
2085 {
2086 int col, bslcnt = 0;
2087
2088 /* Set "match_escaped" if there are an odd number of
2089 * backslashes. */
2090 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2091 bslcnt++;
2092 match_escaped = (bslcnt & 1);
2093 }
2094 }
2095 }
2096 if (hash_dir)
2097 {
2098 /*
2099 * Look for matching #if, #else, #elif, or #endif
2100 */
2101 if (oap != NULL)
2102 oap->motion_type = MLINE; /* Linewise for this case only */
2103 if (initc != '#')
2104 {
2105 ptr = skipwhite(skipwhite(linep) + 1);
2106 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2107 hash_dir = 1;
2108 else if (STRNCMP(ptr, "endif", 5) == 0)
2109 hash_dir = -1;
2110 else
2111 return NULL;
2112 }
2113 pos.col = 0;
2114 while (!got_int)
2115 {
2116 if (hash_dir > 0)
2117 {
2118 if (pos.lnum == curbuf->b_ml.ml_line_count)
2119 break;
2120 }
2121 else if (pos.lnum == 1)
2122 break;
2123 pos.lnum += hash_dir;
2124 linep = ml_get(pos.lnum);
2125 line_breakcheck(); /* check for CTRL-C typed */
2126 ptr = skipwhite(linep);
2127 if (*ptr != '#')
2128 continue;
2129 pos.col = (colnr_T) (ptr - linep);
2130 ptr = skipwhite(ptr + 1);
2131 if (hash_dir > 0)
2132 {
2133 if (STRNCMP(ptr, "if", 2) == 0)
2134 count++;
2135 else if (STRNCMP(ptr, "el", 2) == 0)
2136 {
2137 if (count == 0)
2138 return &pos;
2139 }
2140 else if (STRNCMP(ptr, "endif", 5) == 0)
2141 {
2142 if (count == 0)
2143 return &pos;
2144 count--;
2145 }
2146 }
2147 else
2148 {
2149 if (STRNCMP(ptr, "if", 2) == 0)
2150 {
2151 if (count == 0)
2152 return &pos;
2153 count--;
2154 }
2155 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2156 {
2157 if (count == 0)
2158 return &pos;
2159 }
2160 else if (STRNCMP(ptr, "endif", 5) == 0)
2161 count++;
2162 }
2163 }
2164 return NULL;
2165 }
2166 }
2167
2168#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002169 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 * paren/brace in the other direction. */
2171 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2172 backwards = !backwards;
2173#endif
2174
2175 do_quotes = -1;
2176 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002177 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002180 if ((backwards && comment_dir)
2181#ifdef FEAT_LISP
2182 || lisp
2183#endif
2184 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002186#ifdef FEAT_LISP
2187 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2188 lispcomm = TRUE; /* find match inside this comment */
2189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 while (!got_int)
2191 {
2192 /*
2193 * Go to the next position, forward or backward. We could use
2194 * inc() and dec() here, but that is much slower
2195 */
2196 if (backwards)
2197 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002198#ifdef FEAT_LISP
2199 /* char to match is inside of comment, don't search outside */
2200 if (lispcomm && pos.col < (colnr_T)comment_col)
2201 break;
2202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (pos.col == 0) /* at start of line, go to prev. one */
2204 {
2205 if (pos.lnum == 1) /* start of file */
2206 break;
2207 --pos.lnum;
2208
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002209 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 break;
2211
2212 linep = ml_get(pos.lnum);
2213 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2214 do_quotes = -1;
2215 line_breakcheck();
2216
2217 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002218 if (comment_dir
2219#ifdef FEAT_LISP
2220 || lisp
2221#endif
2222 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002224#ifdef FEAT_LISP
2225 /* skip comment */
2226 if (lisp && comment_col != MAXCOL)
2227 pos.col = comment_col;
2228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230 else
2231 {
2232 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (has_mbyte)
2234 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 }
2236 }
2237 else /* forward search */
2238 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002239 if (linep[pos.col] == NUL
2240 /* at end of line, go to next one */
2241#ifdef FEAT_LISP
2242 /* don't search for match in comment */
2243 || (lisp && comment_col != MAXCOL
2244 && pos.col == (colnr_T)comment_col)
2245#endif
2246 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002248 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2249#ifdef FEAT_LISP
2250 /* line is exhausted and comment with it,
2251 * don't search for match in code */
2252 || lispcomm
2253#endif
2254 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 break;
2256 ++pos.lnum;
2257
2258 if (maxtravel && traveled++ > maxtravel)
2259 break;
2260
2261 linep = ml_get(pos.lnum);
2262 pos.col = 0;
2263 do_quotes = -1;
2264 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002265#ifdef FEAT_LISP
2266 if (lisp) /* find comment pos in new line */
2267 comment_col = check_linecomment(linep);
2268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270 else
2271 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002273 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ++pos.col;
2276 }
2277 }
2278
2279 /*
2280 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2281 */
2282 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2283 (linep[0] == '{' || linep[0] == '}'))
2284 {
2285 if (linep[0] == findc && count == 0) /* match! */
2286 return &pos;
2287 break; /* out of scope */
2288 }
2289
2290 if (comment_dir)
2291 {
2292 /* Note: comments do not nest, and we ignore quotes in them */
2293 /* TODO: ignore comment brackets inside strings */
2294 if (comment_dir == FORWARD)
2295 {
2296 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2297 {
2298 pos.col++;
2299 return &pos;
2300 }
2301 }
2302 else /* Searching backwards */
2303 {
2304 /*
2305 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002306 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
2308 if (pos.col == 0)
2309 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002310 else if (raw_string)
2311 {
2312 if (linep[pos.col - 1] == 'R'
2313 && linep[pos.col] == '"'
2314 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2315 {
2316 /* Possible start of raw string. Now that we have the
2317 * delimiter we can check if it ends before where we
2318 * started searching, or before the previously found
2319 * raw string start. */
2320 if (!find_rawstring_end(linep, &pos,
2321 count > 0 ? &match_pos : &curwin->w_cursor))
2322 {
2323 count++;
2324 match_pos = pos;
2325 match_pos.col--;
2326 }
2327 linep = ml_get(pos.lnum); /* may have been released */
2328 }
2329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 else if ( linep[pos.col - 1] == '/'
2331 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002332 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 && (int)pos.col < comment_col)
2334 {
2335 count++;
2336 match_pos = pos;
2337 match_pos.col--;
2338 }
2339 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2340 {
2341 if (count > 0)
2342 pos = match_pos;
2343 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2344 && (int)pos.col <= comment_col)
2345 pos.col -= 2;
2346 else if (ignore_cend)
2347 continue;
2348 else
2349 return NULL;
2350 return &pos;
2351 }
2352 }
2353 continue;
2354 }
2355
2356 /*
2357 * If smart matching ('cpoptions' does not contain '%'), braces inside
2358 * of quotes are ignored, but only if there is an even number of
2359 * quotes in the line.
2360 */
2361 if (cpo_match)
2362 do_quotes = 0;
2363 else if (do_quotes == -1)
2364 {
2365 /*
2366 * Count the number of quotes in the line, skipping \" and '"'.
2367 * Watch out for "\\".
2368 */
2369 at_start = do_quotes;
2370 for (ptr = linep; *ptr; ++ptr)
2371 {
2372 if (ptr == linep + pos.col + backwards)
2373 at_start = (do_quotes & 1);
2374 if (*ptr == '"'
2375 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2376 ++do_quotes;
2377 if (*ptr == '\\' && ptr[1] != NUL)
2378 ++ptr;
2379 }
2380 do_quotes &= 1; /* result is 1 with even number of quotes */
2381
2382 /*
2383 * If we find an uneven count, check current line and previous
2384 * one for a '\' at the end.
2385 */
2386 if (!do_quotes)
2387 {
2388 inquote = FALSE;
2389 if (ptr[-1] == '\\')
2390 {
2391 do_quotes = 1;
2392 if (start_in_quotes == MAYBE)
2393 {
2394 /* Do we need to use at_start here? */
2395 inquote = TRUE;
2396 start_in_quotes = TRUE;
2397 }
2398 else if (backwards)
2399 inquote = TRUE;
2400 }
2401 if (pos.lnum > 1)
2402 {
2403 ptr = ml_get(pos.lnum - 1);
2404 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2405 {
2406 do_quotes = 1;
2407 if (start_in_quotes == MAYBE)
2408 {
2409 inquote = at_start;
2410 if (inquote)
2411 start_in_quotes = TRUE;
2412 }
2413 else if (!backwards)
2414 inquote = TRUE;
2415 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002416
2417 /* ml_get() only keeps one line, need to get linep again */
2418 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420 }
2421 }
2422 if (start_in_quotes == MAYBE)
2423 start_in_quotes = FALSE;
2424
2425 /*
2426 * If 'smartmatch' is set:
2427 * Things inside quotes are ignored by setting 'inquote'. If we
2428 * find a quote without a preceding '\' invert 'inquote'. At the
2429 * end of a line not ending in '\' we reset 'inquote'.
2430 *
2431 * In lines with an uneven number of quotes (without preceding '\')
2432 * we do not know which part to ignore. Therefore we only set
2433 * inquote if the number of quotes in a line is even, unless this
2434 * line or the previous one ends in a '\'. Complicated, isn't it?
2435 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002436 c = PTR2CHAR(linep + pos.col);
2437 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 {
2439 case NUL:
2440 /* at end of line without trailing backslash, reset inquote */
2441 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2442 {
2443 inquote = FALSE;
2444 start_in_quotes = FALSE;
2445 }
2446 break;
2447
2448 case '"':
2449 /* a quote that is preceded with an odd number of backslashes is
2450 * ignored */
2451 if (do_quotes)
2452 {
2453 int col;
2454
2455 for (col = pos.col - 1; col >= 0; --col)
2456 if (linep[col] != '\\')
2457 break;
2458 if ((((int)pos.col - 1 - col) & 1) == 0)
2459 {
2460 inquote = !inquote;
2461 start_in_quotes = FALSE;
2462 }
2463 }
2464 break;
2465
2466 /*
2467 * If smart matching ('cpoptions' does not contain '%'):
2468 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2469 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2470 * skipped, there is never a brace in them.
2471 * Ignore this when finding matches for `'.
2472 */
2473 case '\'':
2474 if (!cpo_match && initc != '\'' && findc != '\'')
2475 {
2476 if (backwards)
2477 {
2478 if (pos.col > 1)
2479 {
2480 if (linep[pos.col - 2] == '\'')
2481 {
2482 pos.col -= 2;
2483 break;
2484 }
2485 else if (linep[pos.col - 2] == '\\' &&
2486 pos.col > 2 && linep[pos.col - 3] == '\'')
2487 {
2488 pos.col -= 3;
2489 break;
2490 }
2491 }
2492 }
2493 else if (linep[pos.col + 1]) /* forward search */
2494 {
2495 if (linep[pos.col + 1] == '\\' &&
2496 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2497 {
2498 pos.col += 3;
2499 break;
2500 }
2501 else if (linep[pos.col + 2] == '\'')
2502 {
2503 pos.col += 2;
2504 break;
2505 }
2506 }
2507 }
2508 /* FALLTHROUGH */
2509
2510 default:
2511#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002512 /*
2513 * For Lisp skip over backslashed (), {} and [].
2514 * (actually, we skip #\( et al)
2515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 if (curbuf->b_p_lisp
2517 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002518 && pos.col > 1
2519 && check_prevcol(linep, pos.col, '\\', NULL)
2520 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 break;
2522#endif
2523
2524 /* Check for match outside of quotes, and inside of
2525 * quotes when the start is also inside of quotes. */
2526 if ((!inquote || start_in_quotes == TRUE)
2527 && (c == initc || c == findc))
2528 {
2529 int col, bslcnt = 0;
2530
2531 if (!cpo_bsl)
2532 {
2533 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2534 bslcnt++;
2535 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002536 /* Only accept a match when 'M' is in 'cpo' or when escaping
2537 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2539 {
2540 if (c == initc)
2541 count++;
2542 else
2543 {
2544 if (count == 0)
2545 return &pos;
2546 count--;
2547 }
2548 }
2549 }
2550 }
2551 }
2552
2553 if (comment_dir == BACKWARD && count > 0)
2554 {
2555 pos = match_pos;
2556 return &pos;
2557 }
2558 return (pos_T *)NULL; /* never found it */
2559}
2560
2561/*
2562 * Check if line[] contains a / / comment.
2563 * Return MAXCOL if not, otherwise return the column.
2564 * TODO: skip strings.
2565 */
2566 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002567check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568{
2569 char_u *p;
2570
2571 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002572#ifdef FEAT_LISP
2573 /* skip Lispish one-line comments */
2574 if (curbuf->b_p_lisp)
2575 {
2576 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2577 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002578 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002579
2580 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002581 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002582 {
2583 if (*p == '"')
2584 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002585 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586 {
2587 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002588 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002589 }
2590 else if (p == line || ((p - line) >= 2
2591 /* skip #\" form */
2592 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002593 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002594 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002595 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002596 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2597 break; /* found! */
2598 ++p;
2599 }
2600 }
2601 else
2602 p = NULL;
2603 }
2604 else
2605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 while ((p = vim_strchr(p, '/')) != NULL)
2607 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002608 /* accept a double /, unless it's preceded with * and followed by *,
2609 * because * / / * is an end and start of a C comment */
2610 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 break;
2612 ++p;
2613 }
2614
2615 if (p == NULL)
2616 return MAXCOL;
2617 return (int)(p - line);
2618}
2619
2620/*
2621 * Move cursor briefly to character matching the one under the cursor.
2622 * Used for Insert mode and "r" command.
2623 * Show the match only if it is visible on the screen.
2624 * If there isn't a match, then beep.
2625 */
2626 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002627showmatch(
2628 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 pos_T *lpos, save_cursor;
2631 pos_T mpos;
2632 colnr_T vcol;
2633 long save_so;
2634 long save_siso;
2635#ifdef CURSOR_SHAPE
2636 int save_state;
2637#endif
2638 colnr_T save_dollar_vcol;
2639 char_u *p;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002640 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2641 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
2643 /*
2644 * Only show match for chars in the 'matchpairs' option.
2645 */
2646 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002647 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 {
2649#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002650 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002651 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002652#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002653 p += MB_PTR2LEN(p) + 1;
2654 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655#ifdef FEAT_RIGHTLEFT
2656 && !(curwin->w_p_rl ^ p_ri)
2657#endif
2658 )
2659 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002660 p += MB_PTR2LEN(p);
2661 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 return;
2663 }
2664
2665 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002666 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002667 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {
2669 if (!curwin->w_p_wrap)
2670 getvcol(curwin, lpos, NULL, &vcol, NULL);
2671 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002672 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 {
2674 mpos = *lpos; /* save the pos, update_screen() may change it */
2675 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002676 save_so = *so;
2677 save_siso = *siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2679 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002680 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2681 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 ++curwin->w_virtcol; /* do display ')' just before "$" */
2683 update_screen(VALID); /* show the new char first */
2684
2685 save_dollar_vcol = dollar_vcol;
2686#ifdef CURSOR_SHAPE
2687 save_state = State;
2688 State = SHOWMATCH;
2689 ui_cursor_shape(); /* may show different cursor shape */
2690#endif
2691 curwin->w_cursor = mpos; /* move to matching char */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002692 *so = 0; /* don't use 'scrolloff' here */
2693 *siso = 0; /* don't use 'sidescrolloff' here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 showruler(FALSE);
2695 setcursor();
2696 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002697 out_flush_cursor(TRUE, FALSE);
2698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2700 * which resets it if the matching position is in a previous line
2701 * and has a higher column number. */
2702 dollar_vcol = save_dollar_vcol;
2703
2704 /*
2705 * brief pause, unless 'm' is present in 'cpo' and a character is
2706 * available.
2707 */
2708 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2709 ui_delay(p_mat * 100L, TRUE);
2710 else if (!char_avail())
2711 ui_delay(p_mat * 100L, FALSE);
2712 curwin->w_cursor = save_cursor; /* restore cursor position */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002713 *so = save_so;
2714 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#ifdef CURSOR_SHAPE
2716 State = save_state;
2717 ui_cursor_shape(); /* may show different cursor shape */
2718#endif
2719 }
2720 }
2721}
2722
2723/*
Bram Moolenaar85160712018-06-19 18:27:41 +02002724 * Find the start of the next sentence, searching in the direction specified
2725 * by the "dir" argument. The cursor is positioned on the start of the next
2726 * sentence when found. If the next sentence is found, return OK. Return FAIL
2727 * otherwise. See ":h sentence" for the precise definition of a "sentence"
2728 * text object.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 */
2730 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002731findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732{
2733 pos_T pos, tpos;
2734 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002735 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 int startlnum;
2737 int noskip = FALSE; /* do not skip blanks */
2738 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002739 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 pos = curwin->w_cursor;
2742 if (dir == FORWARD)
2743 func = incl;
2744 else
2745 func = decl;
2746
2747 while (count--)
2748 {
2749 /*
2750 * if on an empty line, skip upto a non-empty line
2751 */
2752 if (gchar_pos(&pos) == NUL)
2753 {
2754 do
2755 if ((*func)(&pos) == -1)
2756 break;
2757 while (gchar_pos(&pos) == NUL);
2758 if (dir == FORWARD)
2759 goto found;
2760 }
2761 /*
2762 * if on the start of a paragraph or a section and searching forward,
2763 * go to the next line
2764 */
2765 else if (dir == FORWARD && pos.col == 0 &&
2766 startPS(pos.lnum, NUL, FALSE))
2767 {
2768 if (pos.lnum == curbuf->b_ml.ml_line_count)
2769 return FAIL;
2770 ++pos.lnum;
2771 goto found;
2772 }
2773 else if (dir == BACKWARD)
2774 decl(&pos);
2775
Bram Moolenaar85160712018-06-19 18:27:41 +02002776 // go back to the previous non-white non-punctuation character
Bram Moolenaardef9e822004-12-31 20:58:58 +00002777 found_dot = FALSE;
Bram Moolenaar85160712018-06-19 18:27:41 +02002778 while (c = gchar_pos(&pos), VIM_ISWHITE(c)
2779 || vim_strchr((char_u *)".!?)]\"'", c) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 {
Bram Moolenaar85160712018-06-19 18:27:41 +02002781 tpos = pos;
2782 if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 break;
Bram Moolenaar85160712018-06-19 18:27:41 +02002784
2785 if (found_dot)
2786 break;
2787 if (vim_strchr((char_u *) ".!?", c) != NULL)
2788 found_dot = TRUE;
2789
2790 if (vim_strchr((char_u *) ")]\"'", c) != NULL
2791 && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
2792 break;
2793
2794 decl(&pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 }
2796
2797 /* remember the line where the search started */
2798 startlnum = pos.lnum;
2799 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2800
2801 for (;;) /* find end of sentence */
2802 {
2803 c = gchar_pos(&pos);
2804 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2805 {
2806 if (dir == BACKWARD && pos.lnum != startlnum)
2807 ++pos.lnum;
2808 break;
2809 }
2810 if (c == '.' || c == '!' || c == '?')
2811 {
2812 tpos = pos;
2813 do
2814 if ((c = inc(&tpos)) == -1)
2815 break;
2816 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2817 != NULL);
2818 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2819 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2820 && gchar_pos(&tpos) == ' ')))
2821 {
2822 pos = tpos;
2823 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2824 inc(&pos);
2825 break;
2826 }
2827 }
2828 if ((*func)(&pos) == -1)
2829 {
2830 if (count)
2831 return FAIL;
2832 noskip = TRUE;
2833 break;
2834 }
2835 }
2836found:
2837 /* skip white space */
2838 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2839 if (incl(&pos) == -1)
2840 break;
2841 }
2842
2843 setpcmark();
2844 curwin->w_cursor = pos;
2845 return OK;
2846}
2847
2848/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002849 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002851 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * If 'what' is '{' or '}' we go to the next section.
2853 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002854 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
2856 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002857findpar(
2858 int *pincl, /* Return: TRUE if last char is to be included */
2859 int dir,
2860 long count,
2861 int what,
2862 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863{
2864 linenr_T curr;
2865 int did_skip; /* TRUE after separating lines have been skipped */
2866 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002867 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868#ifdef FEAT_FOLDING
2869 linenr_T fold_first; /* first line of a closed fold */
2870 linenr_T fold_last; /* last line of a closed fold */
2871 int fold_skipped; /* TRUE if a closed fold was skipped this
2872 iteration */
2873#endif
2874
2875 curr = curwin->w_cursor.lnum;
2876
2877 while (count--)
2878 {
2879 did_skip = FALSE;
2880 for (first = TRUE; ; first = FALSE)
2881 {
2882 if (*ml_get(curr) != NUL)
2883 did_skip = TRUE;
2884
2885#ifdef FEAT_FOLDING
2886 /* skip folded lines */
2887 fold_skipped = FALSE;
2888 if (first && hasFolding(curr, &fold_first, &fold_last))
2889 {
2890 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2891 fold_skipped = TRUE;
2892 }
2893#endif
2894
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002895 /* POSIX has its own ideas of what a paragraph boundary is and it
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002896 * doesn't match historical Vi: It also stops at a "{" in the
2897 * first column and at an empty line. */
2898 if (!first && did_skip && (startPS(curr, what, both)
2899 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 break;
2901
2902#ifdef FEAT_FOLDING
2903 if (fold_skipped)
2904 curr -= dir;
2905#endif
2906 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2907 {
2908 if (count)
2909 return FALSE;
2910 curr -= dir;
2911 break;
2912 }
2913 }
2914 }
2915 setpcmark();
2916 if (both && *ml_get(curr) == '}') /* include line with '}' */
2917 ++curr;
2918 curwin->w_cursor.lnum = curr;
2919 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2920 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002921 char_u *line = ml_get(curr);
2922
2923 /* Put the cursor on the last character in the last line and make the
2924 * motion inclusive. */
2925 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 {
2927 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002928 curwin->w_cursor.col -=
2929 (*mb_head_off)(line, line + curwin->w_cursor.col);
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002930 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 }
2932 }
2933 else
2934 curwin->w_cursor.col = 0;
2935 return TRUE;
2936}
2937
2938/*
2939 * check if the string 's' is a nroff macro that is in option 'opt'
2940 */
2941 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002942inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943{
2944 char_u *macro;
2945
2946 for (macro = opt; macro[0]; ++macro)
2947 {
2948 /* Accept two characters in the option being equal to two characters
2949 * in the line. A space in the option matches with a space in the
2950 * line or the line having ended. */
2951 if ( (macro[0] == s[0]
2952 || (macro[0] == ' '
2953 && (s[0] == NUL || s[0] == ' ')))
2954 && (macro[1] == s[1]
2955 || ((macro[1] == NUL || macro[1] == ' ')
2956 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2957 break;
2958 ++macro;
2959 if (macro[0] == NUL)
2960 break;
2961 }
2962 return (macro[0] != NUL);
2963}
2964
2965/*
2966 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2967 * If 'para' is '{' or '}' only check for sections.
2968 * If 'both' is TRUE also stop at '}'
2969 */
2970 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002971startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
2973 char_u *s;
2974
2975 s = ml_get(lnum);
2976 if (*s == para || *s == '\f' || (both && *s == '}'))
2977 return TRUE;
2978 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2979 (!para && inmacro(p_para, s + 1))))
2980 return TRUE;
2981 return FALSE;
2982}
2983
2984/*
2985 * The following routines do the word searches performed by the 'w', 'W',
2986 * 'b', 'B', 'e', and 'E' commands.
2987 */
2988
2989/*
2990 * To perform these searches, characters are placed into one of three
2991 * classes, and transitions between classes determine word boundaries.
2992 *
2993 * The classes are:
2994 *
2995 * 0 - white space
2996 * 1 - punctuation
2997 * 2 or higher - keyword characters (letters, digits and underscore)
2998 */
2999
3000static int cls_bigword; /* TRUE for "W", "B" or "E" */
3001
3002/*
3003 * cls() - returns the class of character at curwin->w_cursor
3004 *
3005 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
3006 * from class 2 and higher are reported as class 1 since only white space
3007 * boundaries are of interest.
3008 */
3009 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003010cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 int c;
3013
3014 c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 if (c == ' ' || c == '\t' || c == NUL)
3016 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (enc_dbcs != 0 && c > 0xFF)
3018 {
3019 /* If cls_bigword, report multi-byte chars as class 1. */
3020 if (enc_dbcs == DBCS_KOR && cls_bigword)
3021 return 1;
3022
3023 /* process code leading/trailing bytes */
3024 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3025 }
3026 if (enc_utf8)
3027 {
3028 c = utf_class(c);
3029 if (c != 0 && cls_bigword)
3030 return 1;
3031 return c;
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3035 if (cls_bigword)
3036 return 1;
3037
3038 if (vim_iswordc(c))
3039 return 2;
3040 return 1;
3041}
3042
3043
3044/*
3045 * fwd_word(count, type, eol) - move forward one word
3046 *
3047 * Returns FAIL if the cursor was already at the end of the file.
3048 * If eol is TRUE, last word stops at end of line (for operators).
3049 */
3050 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003051fwd_word(
3052 long count,
3053 int bigword, /* "W", "E" or "B" */
3054 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
3056 int sclass; /* starting class */
3057 int i;
3058 int last_line;
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 cls_bigword = bigword;
3062 while (--count >= 0)
3063 {
3064#ifdef FEAT_FOLDING
3065 /* When inside a range of folded lines, move to the last char of the
3066 * last line. */
3067 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3068 coladvance((colnr_T)MAXCOL);
3069#endif
3070 sclass = cls();
3071
3072 /*
3073 * We always move at least one character, unless on the last
3074 * character in the buffer.
3075 */
3076 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3077 i = inc_cursor();
3078 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3079 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003080 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return OK;
3082
3083 /*
3084 * Go one char past end of current word (if any)
3085 */
3086 if (sclass != 0)
3087 while (cls() == sclass)
3088 {
3089 i = inc_cursor();
3090 if (i == -1 || (i >= 1 && eol && count == 0))
3091 return OK;
3092 }
3093
3094 /*
3095 * go to next non-white
3096 */
3097 while (cls() == 0)
3098 {
3099 /*
3100 * We'll stop if we land on a blank line
3101 */
3102 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3103 break;
3104
3105 i = inc_cursor();
3106 if (i == -1 || (i >= 1 && eol && count == 0))
3107 return OK;
3108 }
3109 }
3110 return OK;
3111}
3112
3113/*
3114 * bck_word() - move backward 'count' words
3115 *
3116 * If stop is TRUE and we are already on the start of a word, move one less.
3117 *
3118 * Returns FAIL if top of the file was reached.
3119 */
3120 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003121bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 int sclass; /* starting class */
3124
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 cls_bigword = bigword;
3127 while (--count >= 0)
3128 {
3129#ifdef FEAT_FOLDING
3130 /* When inside a range of folded lines, move to the first char of the
3131 * first line. */
3132 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3133 curwin->w_cursor.col = 0;
3134#endif
3135 sclass = cls();
3136 if (dec_cursor() == -1) /* started at start of file */
3137 return FAIL;
3138
3139 if (!stop || sclass == cls() || sclass == 0)
3140 {
3141 /*
3142 * Skip white space before the word.
3143 * Stop on an empty line.
3144 */
3145 while (cls() == 0)
3146 {
3147 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003148 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 goto finished;
3150 if (dec_cursor() == -1) /* hit start of file, stop here */
3151 return OK;
3152 }
3153
3154 /*
3155 * Move backward to start of this word.
3156 */
3157 if (skip_chars(cls(), BACKWARD))
3158 return OK;
3159 }
3160
3161 inc_cursor(); /* overshot - forward one */
3162finished:
3163 stop = FALSE;
3164 }
3165 return OK;
3166}
3167
3168/*
3169 * end_word() - move to the end of the word
3170 *
3171 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3172 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3173 * motion crosses blank lines. When the real vi crosses a blank line in an
3174 * 'e' motion, the cursor is placed on the FIRST character of the next
3175 * non-blank line. The 'E' command, however, works correctly. Since this
3176 * appears to be a bug, I have not duplicated it here.
3177 *
3178 * Returns FAIL if end of the file was reached.
3179 *
3180 * If stop is TRUE and we are already on the end of a word, move one less.
3181 * If empty is TRUE stop on an empty line.
3182 */
3183 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003184end_word(
3185 long count,
3186 int bigword,
3187 int stop,
3188 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 int sclass; /* starting class */
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 cls_bigword = bigword;
3194 while (--count >= 0)
3195 {
3196#ifdef FEAT_FOLDING
3197 /* When inside a range of folded lines, move to the last char of the
3198 * last line. */
3199 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3200 coladvance((colnr_T)MAXCOL);
3201#endif
3202 sclass = cls();
3203 if (inc_cursor() == -1)
3204 return FAIL;
3205
3206 /*
3207 * If we're in the middle of a word, we just have to move to the end
3208 * of it.
3209 */
3210 if (cls() == sclass && sclass != 0)
3211 {
3212 /*
3213 * Move forward to end of the current word
3214 */
3215 if (skip_chars(sclass, FORWARD))
3216 return FAIL;
3217 }
3218 else if (!stop || sclass == 0)
3219 {
3220 /*
3221 * We were at the end of a word. Go to the end of the next word.
3222 * First skip white space, if 'empty' is TRUE, stop at empty line.
3223 */
3224 while (cls() == 0)
3225 {
3226 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003227 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 goto finished;
3229 if (inc_cursor() == -1) /* hit end of file, stop here */
3230 return FAIL;
3231 }
3232
3233 /*
3234 * Move forward to the end of this word.
3235 */
3236 if (skip_chars(cls(), FORWARD))
3237 return FAIL;
3238 }
3239 dec_cursor(); /* overshot - one char backward */
3240finished:
3241 stop = FALSE; /* we move only one word less */
3242 }
3243 return OK;
3244}
3245
3246/*
3247 * Move back to the end of the word.
3248 *
3249 * Returns FAIL if start of the file was reached.
3250 */
3251 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003252bckend_word(
3253 long count,
3254 int bigword, /* TRUE for "B" */
3255 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 int sclass; /* starting class */
3258 int i;
3259
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 cls_bigword = bigword;
3262 while (--count >= 0)
3263 {
3264 sclass = cls();
3265 if ((i = dec_cursor()) == -1)
3266 return FAIL;
3267 if (eol && i == 1)
3268 return OK;
3269
3270 /*
3271 * Move backward to before the start of this word.
3272 */
3273 if (sclass != 0)
3274 {
3275 while (cls() == sclass)
3276 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3277 return OK;
3278 }
3279
3280 /*
3281 * Move backward to end of the previous word
3282 */
3283 while (cls() == 0)
3284 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003285 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 break;
3287 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3288 return OK;
3289 }
3290 }
3291 return OK;
3292}
3293
3294/*
3295 * Skip a row of characters of the same class.
3296 * Return TRUE when end-of-file reached, FALSE otherwise.
3297 */
3298 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003299skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 while (cls() == cclass)
3302 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3303 return TRUE;
3304 return FALSE;
3305}
3306
3307#ifdef FEAT_TEXTOBJ
3308/*
3309 * Go back to the start of the word or the start of white space
3310 */
3311 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003312back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 int sclass; /* starting class */
3315
3316 sclass = cls();
3317 for (;;)
3318 {
3319 if (curwin->w_cursor.col == 0) /* stop at start of line */
3320 break;
3321 dec_cursor();
3322 if (cls() != sclass) /* stop at start of word */
3323 {
3324 inc_cursor();
3325 break;
3326 }
3327 }
3328}
3329
3330 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003331find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 int c;
3334
3335 while (decl(posp) != -1)
3336 {
3337 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003338 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 {
3340 incl(posp);
3341 break;
3342 }
3343 }
3344}
3345
3346/*
3347 * Skip count/2 sentences and count/2 separating white spaces.
3348 */
3349 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003350findsent_forward(
3351 long count,
3352 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 while (count--)
3355 {
3356 findsent(FORWARD, 1L);
3357 if (at_start_sent)
3358 find_first_blank(&curwin->w_cursor);
3359 if (count == 0 || at_start_sent)
3360 decl(&curwin->w_cursor);
3361 at_start_sent = !at_start_sent;
3362 }
3363}
3364
3365/*
3366 * Find word under cursor, cursor at end.
3367 * Used while an operator is pending, and in Visual mode.
3368 */
3369 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003370current_word(
3371 oparg_T *oap,
3372 long count,
3373 int include, /* TRUE: include word and white space */
3374 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 pos_T start_pos;
3377 pos_T pos;
3378 int inclusive = TRUE;
3379 int include_white = FALSE;
3380
3381 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003382 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003385 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 dec_cursor();
3387
3388 /*
3389 * When Visual mode is not active, or when the VIsual area is only one
3390 * character, select the word and/or white space under the cursor.
3391 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003392 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 {
3394 /*
3395 * Go to start of current word or white space.
3396 */
3397 back_in_line();
3398 start_pos = curwin->w_cursor;
3399
3400 /*
3401 * If the start is on white space, and white space should be included
3402 * (" word"), or start is not on white space, and white space should
3403 * not be included ("word"), find end of word.
3404 */
3405 if ((cls() == 0) == include)
3406 {
3407 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3408 return FAIL;
3409 }
3410 else
3411 {
3412 /*
3413 * If the start is not on white space, and white space should be
3414 * included ("word "), or start is on white space and white
3415 * space should not be included (" "), find start of word.
3416 * If we end up in the first column of the next line (single char
3417 * word) back up to end of the line.
3418 */
3419 fwd_word(1L, bigword, TRUE);
3420 if (curwin->w_cursor.col == 0)
3421 decl(&curwin->w_cursor);
3422 else
3423 oneleft();
3424
3425 if (include)
3426 include_white = TRUE;
3427 }
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (VIsual_active)
3430 {
3431 /* should do something when inclusive == FALSE ! */
3432 VIsual = start_pos;
3433 redraw_curbuf_later(INVERTED); /* update the inversion */
3434 }
3435 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 {
3437 oap->start = start_pos;
3438 oap->motion_type = MCHAR;
3439 }
3440 --count;
3441 }
3442
3443 /*
3444 * When count is still > 0, extend with more objects.
3445 */
3446 while (count > 0)
3447 {
3448 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003449 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 {
3451 /*
3452 * In Visual mode, with cursor at start: move cursor back.
3453 */
3454 if (decl(&curwin->w_cursor) == -1)
3455 return FAIL;
3456 if (include != (cls() != 0))
3457 {
3458 if (bck_word(1L, bigword, TRUE) == FAIL)
3459 return FAIL;
3460 }
3461 else
3462 {
3463 if (bckend_word(1L, bigword, TRUE) == FAIL)
3464 return FAIL;
3465 (void)incl(&curwin->w_cursor);
3466 }
3467 }
3468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 /*
3471 * Move cursor forward one word and/or white area.
3472 */
3473 if (incl(&curwin->w_cursor) == -1)
3474 return FAIL;
3475 if (include != (cls() == 0))
3476 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003477 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 return FAIL;
3479 /*
3480 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003481 * the first character on the line.
3482 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003484 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 inclusive = FALSE;
3486 }
3487 else
3488 {
3489 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3490 return FAIL;
3491 }
3492 }
3493 --count;
3494 }
3495
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003496 if (include_white && (cls() != 0
3497 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 /*
3500 * If we don't include white space at the end, move the start
3501 * to include some white space there. This makes "daw" work
3502 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003503 * word). Also when "2daw" deletes "word." at the end of the line
3504 * (cursor is at start of next line).
3505 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 */
3507 pos = curwin->w_cursor; /* save cursor position */
3508 curwin->w_cursor = start_pos;
3509 if (oneleft() == OK)
3510 {
3511 back_in_line();
3512 if (cls() == 0 && curwin->w_cursor.col > 0)
3513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 if (VIsual_active)
3515 VIsual = curwin->w_cursor;
3516 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 oap->start = curwin->w_cursor;
3518 }
3519 }
3520 curwin->w_cursor = pos; /* put cursor back at end */
3521 }
3522
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (VIsual_active)
3524 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003525 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 inc_cursor();
3527 if (VIsual_mode == 'V')
3528 {
3529 VIsual_mode = 'v';
3530 redraw_cmdline = TRUE; /* show mode later */
3531 }
3532 }
3533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 oap->inclusive = inclusive;
3535
3536 return OK;
3537}
3538
3539/*
3540 * Find sentence(s) under the cursor, cursor at end.
3541 * When Visual active, extend it by one or more sentences.
3542 */
3543 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003544current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545{
3546 pos_T start_pos;
3547 pos_T pos;
3548 int start_blank;
3549 int c;
3550 int at_start_sent;
3551 long ncount;
3552
3553 start_pos = curwin->w_cursor;
3554 pos = start_pos;
3555 findsent(FORWARD, 1L); /* Find start of next sentence. */
3556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003558 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003560 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003563 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 /*
3566 * Cursor at start of Visual area.
3567 * Find out where we are:
3568 * - in the white space before a sentence
3569 * - in a sentence or just after it
3570 * - at the start of a sentence
3571 */
3572 at_start_sent = TRUE;
3573 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003574 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003577 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 at_start_sent = FALSE;
3580 break;
3581 }
3582 incl(&pos);
3583 }
3584 if (!at_start_sent)
3585 {
3586 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003587 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 at_start_sent = TRUE; /* exactly at start of sentence */
3589 else
3590 /* inside a sentence, go to its end (start of next) */
3591 findsent(FORWARD, 1L);
3592 }
3593 if (include) /* "as" gets twice as much as "is" */
3594 count *= 2;
3595 while (count--)
3596 {
3597 if (at_start_sent)
3598 find_first_blank(&curwin->w_cursor);
3599 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003600 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 findsent(BACKWARD, 1L);
3602 at_start_sent = !at_start_sent;
3603 }
3604 }
3605 else
3606 {
3607 /*
3608 * Cursor at end of Visual area.
3609 * Find out where we are:
3610 * - just before a sentence
3611 * - just before or in the white space before a sentence
3612 * - in a sentence
3613 */
3614 incl(&pos);
3615 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003616 /* not just before a sentence */
3617 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003620 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003623 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 at_start_sent = TRUE;
3626 break;
3627 }
3628 incl(&pos);
3629 }
3630 if (at_start_sent) /* in the sentence */
3631 findsent(BACKWARD, 1L);
3632 else /* in/before white before a sentence */
3633 curwin->w_cursor = start_pos;
3634 }
3635
3636 if (include) /* "as" gets twice as much as "is" */
3637 count *= 2;
3638 findsent_forward(count, at_start_sent);
3639 if (*p_sel == 'e')
3640 ++curwin->w_cursor.col;
3641 }
3642 return OK;
3643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003646 * If the cursor started on a blank, check if it is just before the start
3647 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003649 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003651 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
3653 start_blank = TRUE;
3654 find_first_blank(&start_pos); /* go back to first blank */
3655 }
3656 else
3657 {
3658 start_blank = FALSE;
3659 findsent(BACKWARD, 1L);
3660 start_pos = curwin->w_cursor;
3661 }
3662 if (include)
3663 ncount = count * 2;
3664 else
3665 {
3666 ncount = count;
3667 if (start_blank)
3668 --ncount;
3669 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003670 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 findsent_forward(ncount, TRUE);
3672 else
3673 decl(&curwin->w_cursor);
3674
3675 if (include)
3676 {
3677 /*
3678 * If the blank in front of the sentence is included, exclude the
3679 * blanks at the end of the sentence, go back to the first blank.
3680 * If there are no trailing blanks, try to include leading blanks.
3681 */
3682 if (start_blank)
3683 {
3684 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003685 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3686 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 decl(&curwin->w_cursor);
3688 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003689 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 find_first_blank(&start_pos);
3691 }
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (VIsual_active)
3694 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003695 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003696 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 goto extend;
3698 if (*p_sel == 'e')
3699 ++curwin->w_cursor.col;
3700 VIsual = start_pos;
3701 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003702 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 redraw_curbuf_later(INVERTED); /* update the inversion */
3704 }
3705 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 {
3707 /* include a newline after the sentence, if there is one */
3708 if (incl(&curwin->w_cursor) == -1)
3709 oap->inclusive = TRUE;
3710 else
3711 oap->inclusive = FALSE;
3712 oap->start = start_pos;
3713 oap->motion_type = MCHAR;
3714 }
3715 return OK;
3716}
3717
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003718/*
3719 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003720 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003723current_block(
3724 oparg_T *oap,
3725 long count,
3726 int include, /* TRUE == include white space */
3727 int what, /* '(', '{', etc. */
3728 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
3730 pos_T old_pos;
3731 pos_T *pos = NULL;
3732 pos_T start_pos;
3733 pos_T *end_pos;
3734 pos_T old_start, old_end;
3735 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003736 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003739 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 old_start = old_end;
3741
3742 /*
3743 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3744 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003745 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
3747 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003748 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 while (inindent(1))
3750 if (inc_cursor() != 0)
3751 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003752 if (gchar_cursor() == what)
3753 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 ++curwin->w_cursor.col;
3755 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003756 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758 old_start = VIsual;
3759 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3760 }
3761 else
3762 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763
3764 /*
3765 * Search backwards for unclosed '(', '{', etc..
3766 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003767 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3768 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 */
3770 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003771 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 while (count-- > 0)
3773 {
3774 if ((pos = findmatch(NULL, what)) == NULL)
3775 break;
3776 curwin->w_cursor = *pos;
3777 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3778 }
3779 p_cpo = save_cpo;
3780
3781 /*
3782 * Search for matching ')', '}', etc.
3783 * Put this position in curwin->w_cursor.
3784 */
3785 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3786 {
3787 curwin->w_cursor = old_pos;
3788 return FAIL;
3789 }
3790 curwin->w_cursor = *end_pos;
3791
3792 /*
3793 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003794 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3795 * indent. But only if the resulting area is not smaller than what we
3796 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 */
3798 while (!include)
3799 {
3800 incl(&start_pos);
3801 sol = (curwin->w_cursor.col == 0);
3802 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003803 while (inindent(1))
3804 {
3805 sol = TRUE;
3806 if (decl(&curwin->w_cursor) != 0)
3807 break;
3808 }
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 /*
3811 * In Visual mode, when the resulting area is not bigger than what we
3812 * started with, extend it to the next block, and then exclude again.
3813 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003814 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 && VIsual_active)
3816 {
3817 curwin->w_cursor = old_start;
3818 decl(&curwin->w_cursor);
3819 if ((pos = findmatch(NULL, what)) == NULL)
3820 {
3821 curwin->w_cursor = old_pos;
3822 return FAIL;
3823 }
3824 start_pos = *pos;
3825 curwin->w_cursor = *pos;
3826 if ((end_pos = findmatch(NULL, other)) == NULL)
3827 {
3828 curwin->w_cursor = old_pos;
3829 return FAIL;
3830 }
3831 curwin->w_cursor = *end_pos;
3832 }
3833 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 break;
3835 }
3836
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 if (VIsual_active)
3838 {
3839 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003840 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003841 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 inc(&curwin->w_cursor); /* include the line break */
3843 VIsual = start_pos;
3844 VIsual_mode = 'v';
3845 redraw_curbuf_later(INVERTED); /* update the inversion */
3846 showmode();
3847 }
3848 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850 oap->start = start_pos;
3851 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003852 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003855 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003856 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003858 else
3859 /* End is before the start (no text in between <>, [], etc.): don't
3860 * operate on any text. */
3861 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863
3864 return OK;
3865}
3866
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003867/*
3868 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3869 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3870 */
3871 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003872in_html_tag(
3873 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003874{
3875 char_u *line = ml_get_curline();
3876 char_u *p;
3877 int c;
3878 int lc = NUL;
3879 pos_T pos;
3880
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003881 if (enc_dbcs)
3882 {
3883 char_u *lp = NULL;
3884
3885 /* We search forward until the cursor, because searching backwards is
3886 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003887 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003888 if (*p == '>' || *p == '<')
3889 {
3890 lc = *p;
3891 lp = p;
3892 }
3893 if (*p != '<') /* check for '<' under cursor */
3894 {
3895 if (lc != '<')
3896 return FALSE;
3897 p = lp;
3898 }
3899 }
3900 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003901 {
3902 for (p = line + curwin->w_cursor.col; p > line; )
3903 {
3904 if (*p == '<') /* find '<' under/before cursor */
3905 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003906 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003907 if (*p == '>') /* find '>' before cursor */
3908 break;
3909 }
3910 if (*p != '<')
3911 return FALSE;
3912 }
3913
3914 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003915 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003916
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003917 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003918 if (end_tag)
3919 /* check that there is a '/' after the '<' */
3920 return *p == '/';
3921
3922 /* check that there is no '/' after the '<' */
3923 if (*p == '/')
3924 return FALSE;
3925
3926 /* check that the matching '>' is not preceded by '/' */
3927 for (;;)
3928 {
3929 if (inc(&pos) < 0)
3930 return FALSE;
3931 c = *ml_get_pos(&pos);
3932 if (c == '>')
3933 break;
3934 lc = c;
3935 }
3936 return lc != '/';
3937}
3938
3939/*
3940 * Find tag block under the cursor, cursor at end.
3941 */
3942 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003943current_tagblock(
3944 oparg_T *oap,
3945 long count_arg,
3946 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003947{
3948 long count = count_arg;
3949 long n;
3950 pos_T old_pos;
3951 pos_T start_pos;
3952 pos_T end_pos;
3953 pos_T old_start, old_end;
3954 char_u *spat, *epat;
3955 char_u *p;
3956 char_u *cp;
3957 int len;
3958 int r;
3959 int do_include = include;
3960 int save_p_ws = p_ws;
3961 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003962 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003963
3964 p_ws = FALSE;
3965
3966 old_pos = curwin->w_cursor;
3967 old_end = curwin->w_cursor; /* remember where we started */
3968 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003969 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003970 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003971
3972 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003973 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003974 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003975 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003976 {
3977 setpcmark();
3978
3979 /* ignore indent */
3980 while (inindent(1))
3981 if (inc_cursor() != 0)
3982 break;
3983
3984 if (in_html_tag(FALSE))
3985 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003986 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003987 while (*ml_get_cursor() != '>')
3988 if (inc_cursor() < 0)
3989 break;
3990 }
3991 else if (in_html_tag(TRUE))
3992 {
3993 /* cursor on end tag, move to just before it */
3994 while (*ml_get_cursor() != '<')
3995 if (dec_cursor() < 0)
3996 break;
3997 dec_cursor();
3998 old_end = curwin->w_cursor;
3999 }
4000 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004001 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004002 {
4003 old_start = VIsual;
4004 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4005 }
4006 else
4007 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004008
4009again:
4010 /*
4011 * Search backwards for unclosed "<aaa>".
4012 * Put this position in start_pos.
4013 */
4014 for (n = 0; n < count; ++n)
4015 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004016 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004017 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004018 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004019 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004020 {
4021 curwin->w_cursor = old_pos;
4022 goto theend;
4023 }
4024 }
4025 start_pos = curwin->w_cursor;
4026
4027 /*
4028 * Search for matching "</aaa>". First isolate the "aaa".
4029 */
4030 inc_cursor();
4031 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004032 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004033 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004034 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004035 if (len == 0)
4036 {
4037 curwin->w_cursor = old_pos;
4038 goto theend;
4039 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004040 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004041 epat = alloc(len + 9);
4042 if (spat == NULL || epat == NULL)
4043 {
4044 vim_free(spat);
4045 vim_free(epat);
4046 curwin->w_cursor = old_pos;
4047 goto theend;
4048 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004049 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004050 sprintf((char *)epat, "</%.*s>\\c", len, p);
4051
Bram Moolenaar48570482017-10-30 21:48:41 +01004052 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004053 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004054
4055 vim_free(spat);
4056 vim_free(epat);
4057
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004058 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004059 {
4060 /* Can't find other end or it's before the previous end. Could be a
4061 * HTML tag that doesn't have a matching end. Search backwards for
4062 * another starting tag. */
4063 count = 1;
4064 curwin->w_cursor = start_pos;
4065 goto again;
4066 }
4067
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02004068 if (do_include)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004069 {
4070 /* Include up to the '>'. */
4071 while (*ml_get_cursor() != '>')
4072 if (inc_cursor() < 0)
4073 break;
4074 }
4075 else
4076 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004077 char_u *c = ml_get_cursor();
4078
4079 /* Exclude the '<' of the end tag.
4080 * If the closing tag is on new line, do not decrement cursor, but
4081 * make operation exclusive, so that the linefeed will be selected */
4082 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4083 /* do not decrement cursor */
4084 is_inclusive = FALSE;
4085 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004086 dec_cursor();
4087 }
4088 end_pos = curwin->w_cursor;
4089
4090 if (!do_include)
4091 {
4092 /* Exclude the start tag. */
4093 curwin->w_cursor = start_pos;
4094 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004095 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004096 {
4097 inc_cursor();
4098 start_pos = curwin->w_cursor;
4099 break;
4100 }
4101 curwin->w_cursor = end_pos;
4102
Bram Moolenaarb476cb72018-08-16 21:37:50 +02004103 // If we are in Visual mode and now have the same text as before set
4104 // "do_include" and try again.
4105 if (VIsual_active && EQUAL_POS(start_pos, old_start)
4106 && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004107 {
4108 do_include = TRUE;
4109 curwin->w_cursor = old_start;
4110 count = count_arg;
4111 goto again;
4112 }
4113 }
4114
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004115 if (VIsual_active)
4116 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004117 /* If the end is before the start there is no text between tags, select
4118 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004119 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004120 curwin->w_cursor = start_pos;
4121 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004122 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004123 VIsual = start_pos;
4124 VIsual_mode = 'v';
4125 redraw_curbuf_later(INVERTED); /* update the inversion */
4126 showmode();
4127 }
4128 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004129 {
4130 oap->start = start_pos;
4131 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004132 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004133 {
4134 /* End is before the start: there is no text between tags; operate
4135 * on an empty area. */
4136 curwin->w_cursor = start_pos;
4137 oap->inclusive = FALSE;
4138 }
4139 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004140 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004141 }
4142 retval = OK;
4143
4144theend:
4145 p_ws = save_p_ws;
4146 return retval;
4147}
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004150current_par(
4151 oparg_T *oap,
4152 long count,
4153 int include, /* TRUE == include white space */
4154 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 linenr_T start_lnum;
4157 linenr_T end_lnum;
4158 int white_in_front;
4159 int dir;
4160 int start_is_white;
4161 int prev_start_is_white;
4162 int retval = OK;
4163 int do_white = FALSE;
4164 int t;
4165 int i;
4166
4167 if (type == 'S') /* not implemented yet */
4168 return FAIL;
4169
4170 start_lnum = curwin->w_cursor.lnum;
4171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /*
4173 * When visual area is more than one line: extend it.
4174 */
4175 if (VIsual_active && start_lnum != VIsual.lnum)
4176 {
4177extend:
4178 if (start_lnum < VIsual.lnum)
4179 dir = BACKWARD;
4180 else
4181 dir = FORWARD;
4182 for (i = count; --i >= 0; )
4183 {
4184 if (start_lnum ==
4185 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4186 {
4187 retval = FAIL;
4188 break;
4189 }
4190
4191 prev_start_is_white = -1;
4192 for (t = 0; t < 2; ++t)
4193 {
4194 start_lnum += dir;
4195 start_is_white = linewhite(start_lnum);
4196 if (prev_start_is_white == start_is_white)
4197 {
4198 start_lnum -= dir;
4199 break;
4200 }
4201 for (;;)
4202 {
4203 if (start_lnum == (dir == BACKWARD
4204 ? 1 : curbuf->b_ml.ml_line_count))
4205 break;
4206 if (start_is_white != linewhite(start_lnum + dir)
4207 || (!start_is_white
4208 && startPS(start_lnum + (dir > 0
4209 ? 1 : 0), 0, 0)))
4210 break;
4211 start_lnum += dir;
4212 }
4213 if (!include)
4214 break;
4215 if (start_lnum == (dir == BACKWARD
4216 ? 1 : curbuf->b_ml.ml_line_count))
4217 break;
4218 prev_start_is_white = start_is_white;
4219 }
4220 }
4221 curwin->w_cursor.lnum = start_lnum;
4222 curwin->w_cursor.col = 0;
4223 return retval;
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 /*
4227 * First move back to the start_lnum of the paragraph or white lines
4228 */
4229 white_in_front = linewhite(start_lnum);
4230 while (start_lnum > 1)
4231 {
4232 if (white_in_front) /* stop at first white line */
4233 {
4234 if (!linewhite(start_lnum - 1))
4235 break;
4236 }
4237 else /* stop at first non-white line of start of paragraph */
4238 {
4239 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4240 break;
4241 }
4242 --start_lnum;
4243 }
4244
4245 /*
4246 * Move past the end of any white lines.
4247 */
4248 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004249 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4250 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 --end_lnum;
4253 i = count;
4254 if (!include && white_in_front)
4255 --i;
4256 while (i--)
4257 {
4258 if (end_lnum == curbuf->b_ml.ml_line_count)
4259 return FAIL;
4260
4261 if (!include)
4262 do_white = linewhite(end_lnum + 1);
4263
4264 if (include || !do_white)
4265 {
4266 ++end_lnum;
4267 /*
4268 * skip to end of paragraph
4269 */
4270 while (end_lnum < curbuf->b_ml.ml_line_count
4271 && !linewhite(end_lnum + 1)
4272 && !startPS(end_lnum + 1, 0, 0))
4273 ++end_lnum;
4274 }
4275
4276 if (i == 0 && white_in_front && include)
4277 break;
4278
4279 /*
4280 * skip to end of white lines after paragraph
4281 */
4282 if (include || do_white)
4283 while (end_lnum < curbuf->b_ml.ml_line_count
4284 && linewhite(end_lnum + 1))
4285 ++end_lnum;
4286 }
4287
4288 /*
4289 * If there are no empty lines at the end, try to find some empty lines at
4290 * the start (unless that has been done already).
4291 */
4292 if (!white_in_front && !linewhite(end_lnum) && include)
4293 while (start_lnum > 1 && linewhite(start_lnum - 1))
4294 --start_lnum;
4295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (VIsual_active)
4297 {
4298 /* Problem: when doing "Vipipip" nothing happens in a single white
4299 * line, we get stuck there. Trap this here. */
4300 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4301 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004302 if (VIsual.lnum != start_lnum)
4303 {
4304 VIsual.lnum = start_lnum;
4305 VIsual.col = 0;
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 VIsual_mode = 'V';
4308 redraw_curbuf_later(INVERTED); /* update the inversion */
4309 showmode();
4310 }
4311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 oap->start.lnum = start_lnum;
4314 oap->start.col = 0;
4315 oap->motion_type = MLINE;
4316 }
4317 curwin->w_cursor.lnum = end_lnum;
4318 curwin->w_cursor.col = 0;
4319
4320 return OK;
4321}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004322
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004323/*
4324 * Search quote char from string line[col].
4325 * Quote character escaped by one of the characters in "escape" is not counted
4326 * as a quote.
4327 * Returns column number of "quotechar" or -1 when not found.
4328 */
4329 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004330find_next_quote(
4331 char_u *line,
4332 int col,
4333 int quotechar,
4334 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004335{
4336 int c;
4337
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004338 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004339 {
4340 c = line[col];
4341 if (c == NUL)
4342 return -1;
4343 else if (escape != NULL && vim_strchr(escape, c))
4344 ++col;
4345 else if (c == quotechar)
4346 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004347 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004348 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004349 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004350 ++col;
4351 }
4352 return col;
4353}
4354
4355/*
4356 * Search backwards in "line" from column "col_start" to find "quotechar".
4357 * Quote character escaped by one of the characters in "escape" is not counted
4358 * as a quote.
4359 * Return the found column or zero.
4360 */
4361 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004362find_prev_quote(
4363 char_u *line,
4364 int col_start,
4365 int quotechar,
4366 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004367{
4368 int n;
4369
4370 while (col_start > 0)
4371 {
4372 --col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004373 col_start -= (*mb_head_off)(line, line + col_start);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004374 n = 0;
4375 if (escape != NULL)
4376 while (col_start - n > 0 && vim_strchr(escape,
4377 line[col_start - n - 1]) != NULL)
4378 ++n;
4379 if (n & 1)
4380 col_start -= n; /* uneven number of escape chars, skip it */
4381 else if (line[col_start] == quotechar)
4382 break;
4383 }
4384 return col_start;
4385}
4386
4387/*
4388 * Find quote under the cursor, cursor at end.
4389 * Returns TRUE if found, else FALSE.
4390 */
4391 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004392current_quote(
4393 oparg_T *oap,
4394 long count,
4395 int include, /* TRUE == include quote char */
4396 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004397{
4398 char_u *line = ml_get_curline();
4399 int col_end;
4400 int col_start = curwin->w_cursor.col;
4401 int inclusive = FALSE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004402 int vis_empty = TRUE; // Visual selection <= 1 char
4403 int vis_bef_curs = FALSE; // Visual starts before cursor
4404 int inside_quotes = FALSE; // Looks like "i'" done before
4405 int selected_quote = FALSE; // Has quote inside selection
Bram Moolenaarab194812005-09-14 21:40:12 +00004406 int i;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004407 int restore_vis_bef = FALSE; // restore VIsual on abort
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004408
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004409 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004410 if (VIsual_active)
4411 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004412 /* this only works within one line */
4413 if (VIsual.lnum != curwin->w_cursor.lnum)
4414 return FALSE;
4415
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004416 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004417 if (*p_sel == 'e')
4418 {
4419 if (!vis_bef_curs)
4420 {
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004421 // VIsual needs to be the start of Visual selection.
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004422 pos_T t = curwin->w_cursor;
4423
4424 curwin->w_cursor = VIsual;
4425 VIsual = t;
4426 vis_bef_curs = TRUE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004427 restore_vis_bef = TRUE;
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004428 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004429 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004430 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004431 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004432 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004433
4434 if (!vis_empty)
4435 {
4436 /* Check if the existing selection exactly spans the text inside
4437 * quotes. */
4438 if (vis_bef_curs)
4439 {
4440 inside_quotes = VIsual.col > 0
4441 && line[VIsual.col - 1] == quotechar
4442 && line[curwin->w_cursor.col] != NUL
4443 && line[curwin->w_cursor.col + 1] == quotechar;
4444 i = VIsual.col;
4445 col_end = curwin->w_cursor.col;
4446 }
4447 else
4448 {
4449 inside_quotes = curwin->w_cursor.col > 0
4450 && line[curwin->w_cursor.col - 1] == quotechar
4451 && line[VIsual.col] != NUL
4452 && line[VIsual.col + 1] == quotechar;
4453 i = curwin->w_cursor.col;
4454 col_end = VIsual.col;
4455 }
4456
4457 /* Find out if we have a quote in the selection. */
4458 while (i <= col_end)
4459 if (line[i++] == quotechar)
4460 {
4461 selected_quote = TRUE;
4462 break;
4463 }
4464 }
4465
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004466 if (!vis_empty && line[col_start] == quotechar)
4467 {
4468 /* Already selecting something and on a quote character. Find the
4469 * next quoted string. */
4470 if (vis_bef_curs)
4471 {
4472 /* Assume we are on a closing quote: move to after the next
4473 * opening quote. */
4474 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4475 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004476 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004477 col_end = find_next_quote(line, col_start + 1, quotechar,
4478 curbuf->b_p_qe);
4479 if (col_end < 0)
4480 {
4481 /* We were on a starting quote perhaps? */
4482 col_end = col_start;
4483 col_start = curwin->w_cursor.col;
4484 }
4485 }
4486 else
4487 {
4488 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4489 if (line[col_end] != quotechar)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004490 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004491 col_start = find_prev_quote(line, col_end, quotechar,
4492 curbuf->b_p_qe);
4493 if (line[col_start] != quotechar)
4494 {
4495 /* We were on an ending quote perhaps? */
4496 col_start = col_end;
4497 col_end = curwin->w_cursor.col;
4498 }
4499 }
4500 }
4501 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004502
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004503 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004504 {
4505 int first_col = col_start;
4506
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004507 if (!vis_empty)
4508 {
4509 if (vis_bef_curs)
4510 first_col = find_next_quote(line, col_start, quotechar, NULL);
4511 else
4512 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4513 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004514
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004515 /* The cursor is on a quote, we don't know if it's the opening or
4516 * closing quote. Search from the start of the line to find out.
4517 * Also do this when there is a Visual area, a' may leave the cursor
4518 * in between two strings. */
4519 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004520 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004521 {
4522 /* Find open quote character. */
4523 col_start = find_next_quote(line, col_start, quotechar, NULL);
4524 if (col_start < 0 || col_start > first_col)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004525 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004526 /* Find close quote character. */
4527 col_end = find_next_quote(line, col_start + 1, quotechar,
4528 curbuf->b_p_qe);
4529 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004530 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004531 /* If is cursor between start and end quote character, it is
4532 * target text object. */
4533 if (col_start <= first_col && first_col <= col_end)
4534 break;
4535 col_start = col_end + 1;
4536 }
4537 }
4538 else
4539 {
4540 /* Search backward for a starting quote. */
4541 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4542 if (line[col_start] != quotechar)
4543 {
4544 /* No quote before the cursor, look after the cursor. */
4545 col_start = find_next_quote(line, col_start, quotechar, NULL);
4546 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004547 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004548 }
4549
4550 /* Find close quote character. */
4551 col_end = find_next_quote(line, col_start + 1, quotechar,
4552 curbuf->b_p_qe);
4553 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004554 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004555 }
4556
4557 /* When "include" is TRUE, include spaces after closing quote or before
4558 * the starting quote. */
4559 if (include)
4560 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004561 if (VIM_ISWHITE(line[col_end + 1]))
4562 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004563 ++col_end;
4564 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004565 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004566 --col_start;
4567 }
4568
Bram Moolenaarab194812005-09-14 21:40:12 +00004569 /* Set start position. After vi" another i" must include the ".
4570 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004571 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004572 ++col_start;
4573 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004574 if (VIsual_active)
4575 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004576 /* Set the start of the Visual area when the Visual area was empty, we
4577 * were just inside quotes or the Visual area didn't start at a quote
4578 * and didn't include a quote.
4579 */
4580 if (vis_empty
4581 || (vis_bef_curs
4582 && !selected_quote
4583 && (inside_quotes
4584 || (line[VIsual.col] != quotechar
4585 && (VIsual.col == 0
4586 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004587 {
4588 VIsual = curwin->w_cursor;
4589 redraw_curbuf_later(INVERTED);
4590 }
4591 }
4592 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004593 {
4594 oap->start = curwin->w_cursor;
4595 oap->motion_type = MCHAR;
4596 }
4597
4598 /* Set end position. */
4599 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004600 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004601 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004602 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004603 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004604 if (VIsual_active)
4605 {
4606 if (vis_empty || vis_bef_curs)
4607 {
4608 /* decrement cursor when 'selection' is not exclusive */
4609 if (*p_sel != 'e')
4610 dec_cursor();
4611 }
4612 else
4613 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004614 /* Cursor is at start of Visual area. Set the end of the Visual
4615 * area when it was just inside quotes or it didn't end at a
4616 * quote. */
4617 if (inside_quotes
4618 || (!selected_quote
4619 && line[VIsual.col] != quotechar
4620 && (line[VIsual.col] == NUL
4621 || line[VIsual.col + 1] != quotechar)))
4622 {
4623 dec_cursor();
4624 VIsual = curwin->w_cursor;
4625 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004626 curwin->w_cursor.col = col_start;
4627 }
4628 if (VIsual_mode == 'V')
4629 {
4630 VIsual_mode = 'v';
4631 redraw_cmdline = TRUE; /* show mode later */
4632 }
4633 }
4634 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004635 {
4636 /* Set inclusive and other oap's flags. */
4637 oap->inclusive = inclusive;
4638 }
4639
4640 return OK;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004641
4642abort_search:
4643 if (VIsual_active && *p_sel == 'e')
4644 {
4645 inc_cursor();
4646 if (restore_vis_bef)
4647 {
4648 pos_T t = curwin->w_cursor;
4649
4650 curwin->w_cursor = VIsual;
4651 VIsual = t;
4652 }
4653 }
4654 return FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004655}
4656
4657#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004659static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004660
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004661/*
4662 * Find next search match under cursor, cursor at end.
4663 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004664 */
4665 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004666current_search(
4667 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004668 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004669{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004670 pos_T start_pos; // start position of the pattern match
4671 pos_T end_pos; // end position of the pattern match
4672 pos_T orig_pos; // position of the cursor at beginning
4673 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004674 int i;
4675 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004676 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004677 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004678 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004679 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004680 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004681
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004682 /* wrapping should not occur */
4683 p_ws = FALSE;
4684
4685 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004686 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004687 dec_cursor();
4688
4689 if (VIsual_active)
4690 {
4691 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004692
4693 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004694
4695 /* make sure, searching further will extend the match */
4696 if (VIsual_active)
4697 {
4698 if (forward)
4699 incl(&pos);
4700 else
4701 decl(&pos);
4702 }
4703 }
4704 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004705 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004706
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004707 /* Is the pattern is zero-width?, this time, don't care about the direction
4708 */
4709 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4710 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004711 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004712 {
4713 p_ws = old_p_ws;
4714 return FAIL; /* pattern not found */
4715 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004716
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004717 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004718 * The trick is to first search backwards and then search forward again,
4719 * so that a match at the current cursor position will be correctly
4720 * captured.
4721 */
4722 for (i = 0; i < 2; i++)
4723 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004724 if (forward)
4725 dir = i;
4726 else
4727 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004728
4729 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004730 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004731 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004732 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004733
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004734 result = searchit(curwin, curbuf, &pos, &end_pos,
4735 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004736 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004737 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004738
4739 /* First search may fail, but then start searching from the
4740 * beginning of the file (cursor might be on the search match)
4741 * except when Visual mode is active, so that extending the visual
4742 * selection works. */
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004743 if (i == 1 && !result) /* not found, abort */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004744 {
4745 curwin->w_cursor = orig_pos;
4746 if (VIsual_active)
4747 VIsual = save_VIsual;
4748 p_ws = old_p_ws;
4749 return FAIL;
4750 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004751 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004752 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004753 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004754 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004755 /* try again from start of buffer */
4756 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004757 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004758 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004759 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004760 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004761 /* searching backwards, so set pos to last line and col */
4762 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004763 pos.col = (colnr_T)STRLEN(
4764 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004765 }
4766 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004767 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004768 }
4769
4770 start_pos = pos;
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004771 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004772
4773 if (!VIsual_active)
4774 VIsual = start_pos;
4775
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004776 // put cursor on last character of match
4777 curwin->w_cursor = end_pos;
4778 if (LT_POS(VIsual, end_pos))
4779 dec_cursor();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004780 VIsual_active = TRUE;
4781 VIsual_mode = 'v';
4782
Bram Moolenaarb7633612019-02-10 21:48:25 +01004783 redraw_curbuf_later(INVERTED); /* update the inversion */
4784 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004785 {
Bram Moolenaarb7633612019-02-10 21:48:25 +01004786 /* Correction for exclusive selection depends on the direction. */
4787 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
4788 inc_cursor();
4789 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
4790 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004791 }
4792
4793#ifdef FEAT_FOLDING
4794 if (fdo_flags & FDO_SEARCH && KeyTyped)
4795 foldOpenCursor();
4796#endif
4797
4798 may_start_select('c');
4799#ifdef FEAT_MOUSE
4800 setmouse();
4801#endif
4802#ifdef FEAT_CLIPBOARD
4803 /* Make sure the clipboard gets updated. Needed because start and
4804 * end are still the same, and the selection needs to be owned */
4805 clip_star.vmode = NUL;
4806#endif
4807 redraw_curbuf_later(INVERTED);
4808 showmode();
4809
4810 return OK;
4811}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004812
4813/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004814 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004815 * If move is TRUE, check from the beginning of the buffer, else from position
4816 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004817 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004818 * Returns TRUE, FALSE or -1 for failure.
4819 */
4820 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004821is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004822{
4823 regmmatch_T regmatch;
4824 int nmatched = 0;
4825 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004826 pos_T pos;
4827 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004828 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004829
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004830 if (pattern == NULL)
4831 pattern = spats[last_idx].pat;
4832
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004833 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4834 SEARCH_KEEP, &regmatch) == FAIL)
4835 return -1;
4836
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004837 /* init startcol correctly */
4838 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004839 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004840 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004841 {
4842 CLEAR_POS(&pos);
4843 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004844 else
4845 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004846 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004847 /* accept a match at the cursor position */
4848 flag = SEARCH_START;
4849 }
4850
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004851 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004852 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004853 {
4854 /* Zero-width pattern should match somewhere, then we can check if
4855 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004856 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004857 do
4858 {
4859 regmatch.startpos[0].col++;
4860 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004861 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004862 if (!nmatched)
4863 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004864 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4865 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004866
4867 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004868 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004869 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004870 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4871 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004872 /* one char width */
4873 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4874 result = TRUE;
4875 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004876 }
4877
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004878 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004879 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004880 return result;
4881}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004882
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4884 || defined(PROTO)
4885/*
4886 * return TRUE if line 'lnum' is empty or has white chars only.
4887 */
4888 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004889linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
4891 char_u *p;
4892
4893 p = skipwhite(ml_get(lnum));
4894 return (*p == NUL);
4895}
4896#endif
4897
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004898/*
4899 * Add the search count "[3/19]" to "msgbuf".
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02004900 * When "recompute" is TRUE always recompute the numbers.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004901 */
4902 static void
4903search_stat(
4904 int dirc,
4905 pos_T *pos,
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02004906 int show_top_bot_msg,
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02004907 char_u *msgbuf,
4908 int recompute)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004909{
4910 int save_ws = p_ws;
4911 int wraparound = FALSE;
4912 pos_T p = (*pos);
4913 static pos_T lastpos = {0, 0, 0};
4914 static int cur = 0;
4915 static int cnt = 0;
4916 static int chgtick = 0;
4917 static char_u *lastpat = NULL;
4918 static buf_T *lbuf = NULL;
4919#ifdef FEAT_RELTIME
4920 proftime_T start;
4921#endif
4922#define OUT_OF_TIME 999
4923
4924 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
4925 || (dirc == '/' && LT_POS(p, lastpos)));
4926
4927 // If anything relevant changed the count has to be recomputed.
4928 // MB_STRNICMP ignores case, but we should not ignore case.
4929 // Unfortunately, there is no MB_STRNICMP function.
4930 if (!(chgtick == CHANGEDTICK(curbuf)
4931 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
4932 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
4933 && EQUAL_POS(lastpos, curwin->w_cursor)
Bram Moolenaar8f46e4c2019-05-24 22:08:15 +02004934 && lbuf == curbuf) || wraparound || cur < 0 || cur > 99 || recompute)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004935 {
4936 cur = 0;
4937 cnt = 0;
4938 CLEAR_POS(&lastpos);
4939 lbuf = curbuf;
4940 }
4941
4942 if (EQUAL_POS(lastpos, curwin->w_cursor) && !wraparound
4943 && (dirc == '/' ? cur < cnt : cur > 0))
4944 cur += dirc == '/' ? 1 : -1;
4945 else
4946 {
4947 p_ws = FALSE;
4948#ifdef FEAT_RELTIME
4949 profile_setlimit(20L, &start);
4950#endif
4951 while (!got_int && searchit(curwin, curbuf, &lastpos, NULL,
Bram Moolenaar9ce3fa82019-05-07 21:29:11 +02004952 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST,
4953 (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004954 {
4955#ifdef FEAT_RELTIME
4956 // Stop after passing the time limit.
4957 if (profile_passed_limit(&start))
4958 {
4959 cnt = OUT_OF_TIME;
4960 cur = OUT_OF_TIME;
4961 break;
4962 }
4963#endif
4964 cnt++;
4965 if (LTOREQ_POS(lastpos, p))
4966 cur++;
4967 fast_breakcheck();
4968 if (cnt > 99)
4969 break;
4970 }
4971 if (got_int)
4972 cur = -1; // abort
4973 }
4974 if (cur > 0)
4975 {
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004976 char t[SEARCH_STAT_BUF_LEN] = "";
Bram Moolenaare2ad8262019-05-24 13:22:22 +02004977 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004978
4979#ifdef FEAT_RIGHTLEFT
4980 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
4981 {
4982 if (cur == OUT_OF_TIME)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004983 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004984 else if (cnt > 99 && cur > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004985 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004986 else if (cnt > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004987 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/%d]", cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004988 else
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004989 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cnt, cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004990 }
4991 else
4992#endif
4993 {
4994 if (cur == OUT_OF_TIME)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004995 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004996 else if (cnt > 99 && cur > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004997 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004998 else if (cnt > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004999 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>99]", cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005000 else
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005001 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cur, cnt);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005002 }
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005003
5004 len = STRLEN(t);
Bram Moolenaardc6855a2019-05-18 19:26:29 +02005005 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005006 {
5007 STRCPY(t + len, " W");
5008 len += 2;
5009 }
5010
5011 mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005012 if (dirc == '?' && cur == 100)
5013 cur = -1;
5014
5015 vim_free(lastpat);
5016 lastpat = vim_strsave(spats[last_idx].pat);
5017 chgtick = CHANGEDTICK(curbuf);
5018 lbuf = curbuf;
5019 lastpos = p;
5020
Bram Moolenaar984f0312019-05-24 13:11:47 +02005021 // keep the message even after redraw, but don't put in history
5022 msg_hist_off = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005023 give_warning(msgbuf, FALSE);
Bram Moolenaar984f0312019-05-24 13:11:47 +02005024 msg_hist_off = FALSE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005025 }
5026 p_ws = save_ws;
5027}
5028
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029#if defined(FEAT_FIND_ID) || defined(PROTO)
5030/*
5031 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01005032 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005035find_pattern_in_path(
5036 char_u *ptr, /* pointer to search pattern */
5037 int dir UNUSED, /* direction of expansion */
5038 int len, /* length of search pattern */
5039 int whole, /* match whole words only */
5040 int skip_comments, /* don't match inside comments */
5041 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005043 long count,
5044 int action, /* What to do when we find it */
5045 linenr_T start_lnum, /* first line to start searching */
5046 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
5048 SearchedFile *files; /* Stack of included files */
5049 SearchedFile *bigger; /* When we need more space */
5050 int max_path_depth = 50;
5051 long match_count = 1;
5052
5053 char_u *pat;
5054 char_u *new_fname;
5055 char_u *curr_fname = curbuf->b_fname;
5056 char_u *prev_fname = NULL;
5057 linenr_T lnum;
5058 int depth;
5059 int depth_displayed; /* For type==CHECK_PATH */
5060 int old_files;
5061 int already_searched;
5062 char_u *file_line;
5063 char_u *line;
5064 char_u *p;
5065 char_u save_char;
5066 int define_matched;
5067 regmatch_T regmatch;
5068 regmatch_T incl_regmatch;
5069 regmatch_T def_regmatch;
5070 int matched = FALSE;
5071 int did_show = FALSE;
5072 int found = FALSE;
5073 int i;
5074 char_u *already = NULL;
5075 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005076 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005077#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 win_T *curwin_save = NULL;
5079#endif
5080
5081 regmatch.regprog = NULL;
5082 incl_regmatch.regprog = NULL;
5083 def_regmatch.regprog = NULL;
5084
5085 file_line = alloc(LSIZE);
5086 if (file_line == NULL)
5087 return;
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (type != CHECK_PATH && type != FIND_DEFINE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 /* when CONT_SOL is set compare "ptr" with the beginning of the line
5091 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005092 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 pat = alloc(len + 5);
5095 if (pat == NULL)
5096 goto fpip_end;
5097 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
5098 /* ignore case according to p_ic, p_scs and pat */
5099 regmatch.rm_ic = ignorecase(pat);
5100 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5101 vim_free(pat);
5102 if (regmatch.regprog == NULL)
5103 goto fpip_end;
5104 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005105 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
5106 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005108 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (incl_regmatch.regprog == NULL)
5110 goto fpip_end;
5111 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
5112 }
5113 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
5114 {
5115 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5116 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5117 if (def_regmatch.regprog == NULL)
5118 goto fpip_end;
5119 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5120 }
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005121 files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (files == NULL)
5123 goto fpip_end;
5124 old_files = max_path_depth;
5125 depth = depth_displayed = -1;
5126
5127 lnum = start_lnum;
5128 if (end_lnum > curbuf->b_ml.ml_line_count)
5129 end_lnum = curbuf->b_ml.ml_line_count;
5130 if (lnum > end_lnum) /* do at least one line */
5131 lnum = end_lnum;
5132 line = ml_get(lnum);
5133
5134 for (;;)
5135 {
5136 if (incl_regmatch.regprog != NULL
5137 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5138 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005139 char_u *p_fname = (curr_fname == curbuf->b_fname)
5140 ? curbuf->b_ffname : curr_fname;
5141
5142 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5143 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5144 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005145 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005146 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5147 else
5148 /* Use text after match with 'include'. */
5149 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005150 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 already_searched = FALSE;
5152 if (new_fname != NULL)
5153 {
5154 /* Check whether we have already searched in this file */
5155 for (i = 0;; i++)
5156 {
5157 if (i == depth + 1)
5158 i = old_files;
5159 if (i == max_path_depth)
5160 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02005161 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
5162 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 if (type != CHECK_PATH &&
5165 action == ACTION_SHOW_ALL && files[i].matched)
5166 {
5167 msg_putchar('\n'); /* cursor below last one */
5168 if (!got_int) /* don't display if 'q'
5169 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005170 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
5172 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005173 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 prev_fname = NULL;
5175 }
5176 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01005177 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 already_searched = TRUE;
5179 break;
5180 }
5181 }
5182 }
5183
5184 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5185 || (new_fname == NULL && !already_searched)))
5186 {
5187 if (did_show)
5188 msg_putchar('\n'); /* cursor below last one */
5189 else
5190 {
5191 gotocmdline(TRUE); /* cursor at status line */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005192 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005194 msg_puts_title(_("not found "));
5195 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
5197 did_show = TRUE;
5198 while (depth_displayed < depth && !got_int)
5199 {
5200 ++depth_displayed;
5201 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005202 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005204 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 if (!got_int) /* don't display if 'q' typed
5207 for "--more--" message */
5208 {
5209 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005210 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (new_fname != NULL)
5212 {
5213 /* using "new_fname" is more reliable, e.g., when
5214 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005215 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 else
5218 {
5219 /*
5220 * Isolate the file name.
5221 * Include the surrounding "" or <> if present.
5222 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005223 if (inc_opt != NULL
5224 && strstr((char *)inc_opt, "\\zs") != NULL)
5225 {
5226 /* pattern contains \zs, use the match */
5227 p = incl_regmatch.startp[0];
5228 i = (int)(incl_regmatch.endp[0]
5229 - incl_regmatch.startp[0]);
5230 }
5231 else
5232 {
5233 /* find the file name after the end of the match */
5234 for (p = incl_regmatch.endp[0];
5235 *p && !vim_isfilec(*p); p++)
5236 ;
5237 for (i = 0; vim_isfilec(p[i]); i++)
5238 ;
5239 }
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 if (i == 0)
5242 {
5243 /* Nothing found, use the rest of the line. */
5244 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005245 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005247 /* Avoid checking before the start of the line, can
5248 * happen if \zs appears in the regexp. */
5249 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 if (p[-1] == '"' || p[-1] == '<')
5252 {
5253 --p;
5254 ++i;
5255 }
5256 if (p[i] == '"' || p[i] == '>')
5257 ++i;
5258 }
5259 save_char = p[i];
5260 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005261 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 p[i] = save_char;
5263 }
5264
5265 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5266 {
5267 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005268 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005270 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272 }
5273 out_flush(); /* output each line directly */
5274 }
5275
5276 if (new_fname != NULL)
5277 {
5278 /* Push the new file onto the file stack */
5279 if (depth + 1 == old_files)
5280 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005281 bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (bigger != NULL)
5283 {
5284 for (i = 0; i <= depth; i++)
5285 bigger[i] = files[i];
5286 for (i = depth + 1; i < old_files + max_path_depth; i++)
5287 {
5288 bigger[i].fp = NULL;
5289 bigger[i].name = NULL;
5290 bigger[i].lnum = 0;
5291 bigger[i].matched = FALSE;
5292 }
5293 for (i = old_files; i < max_path_depth; i++)
5294 bigger[i + max_path_depth] = files[i];
5295 old_files += max_path_depth;
5296 max_path_depth *= 2;
5297 vim_free(files);
5298 files = bigger;
5299 }
5300 }
5301 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5302 == NULL)
5303 vim_free(new_fname);
5304 else
5305 {
5306 if (++depth == old_files)
5307 {
5308 /*
5309 * lalloc() for 'bigger' must have failed above. We
5310 * will forget one of our already visited files now.
5311 */
5312 vim_free(files[old_files].name);
5313 ++old_files;
5314 }
5315 files[depth].name = curr_fname = new_fname;
5316 files[depth].lnum = 0;
5317 files[depth].matched = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 if (action == ACTION_EXPAND)
5319 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005320 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005321 vim_snprintf((char*)IObuff, IOSIZE,
5322 _("Scanning included file: %s"),
5323 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005324 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005326 else if (p_verbose >= 5)
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005327 {
5328 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005329 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005330 (char *)new_fname);
5331 verbose_leave();
5332 }
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335 }
5336 }
5337 else
5338 {
5339 /*
5340 * Check if the line is a define (type == FIND_DEFINE)
5341 */
5342 p = line;
5343search_line:
5344 define_matched = FALSE;
5345 if (def_regmatch.regprog != NULL
5346 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5347 {
5348 /*
5349 * Pattern must be first identifier after 'define', so skip
5350 * to that position before checking for match of pattern. Also
5351 * don't let it match beyond the end of this identifier.
5352 */
5353 p = def_regmatch.endp[0];
5354 while (*p && !vim_iswordc(*p))
5355 p++;
5356 define_matched = TRUE;
5357 }
5358
5359 /*
5360 * Look for a match. Don't do this if we are looking for a
5361 * define and this line didn't match define_prog above.
5362 */
5363 if (def_regmatch.regprog == NULL || define_matched)
5364 {
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005365 if (define_matched || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
5367 /* compare the first "len" chars from "ptr" */
5368 startp = skipwhite(p);
5369 if (p_ic)
5370 matched = !MB_STRNICMP(startp, ptr, len);
5371 else
5372 matched = !STRNCMP(startp, ptr, len);
5373 if (matched && define_matched && whole
5374 && vim_iswordc(startp[len]))
5375 matched = FALSE;
5376 }
5377 else if (regmatch.regprog != NULL
5378 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5379 {
5380 matched = TRUE;
5381 startp = regmatch.startp[0];
5382 /*
5383 * Check if the line is not a comment line (unless we are
5384 * looking for a define). A line starting with "# define"
5385 * is not considered to be a comment line.
5386 */
5387 if (!define_matched && skip_comments)
5388 {
5389#ifdef FEAT_COMMENTS
5390 if ((*line != '#' ||
5391 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005392 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 matched = FALSE;
5394
5395 /*
5396 * Also check for a "/ *" or "/ /" before the match.
5397 * Skips lines like "int backwards; / * normal index
5398 * * /" when looking for "normal".
5399 * Note: Doesn't skip "/ *" in comments.
5400 */
5401 p = skipwhite(line);
5402 if (matched
5403 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5404#endif
5405 for (p = line; *p && p < startp; ++p)
5406 {
5407 if (matched
5408 && p[0] == '/'
5409 && (p[1] == '*' || p[1] == '/'))
5410 {
5411 matched = FALSE;
5412 /* After "//" all text is comment */
5413 if (p[1] == '/')
5414 break;
5415 ++p;
5416 }
5417 else if (!matched && p[0] == '*' && p[1] == '/')
5418 {
5419 /* Can find match after "* /". */
5420 matched = TRUE;
5421 ++p;
5422 }
5423 }
5424 }
5425 }
5426 }
5427 }
5428 if (matched)
5429 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 if (action == ACTION_EXPAND)
5431 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005432 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 int add_r;
5434 char_u *aux;
5435
5436 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5437 break;
5438 found = TRUE;
5439 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005440 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 if (vim_iswordp(p))
5444 goto exit_matched;
5445 p = find_word_start(p);
5446 }
5447 p = find_word_end(p);
5448 i = (int)(p - aux);
5449
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005454
5455 /* Get the next line: when "depth" < 0 from the current
5456 * buffer, otherwise from the included file. Jump to
5457 * exit_matched when past the last line. */
5458 if (depth < 0)
5459 {
5460 if (lnum >= end_lnum)
5461 goto exit_matched;
5462 line = ml_get(++lnum);
5463 }
5464 else if (vim_fgets(line = file_line,
5465 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 goto exit_matched;
5467
5468 /* we read a line, set "already" to check this "line" later
5469 * if depth >= 0 we'll increase files[depth].lnum far
5470 * bellow -- Acevedo */
5471 already = aux = p = skipwhite(line);
5472 p = find_word_start(p);
5473 p = find_word_end(p);
5474 if (p > aux)
5475 {
5476 if (*aux != ')' && IObuff[i-1] != TAB)
5477 {
5478 if (IObuff[i-1] != ' ')
5479 IObuff[i++] = ' ';
5480 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5481 if (p_js
5482 && (IObuff[i-2] == '.'
5483 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5484 && (IObuff[i-2] == '?'
5485 || IObuff[i-2] == '!'))))
5486 IObuff[i++] = ' ';
5487 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005488 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 if (p - aux >= IOSIZE - i)
5490 p = aux + IOSIZE - i - 1;
5491 STRNCPY(IObuff + i, aux, p - aux);
5492 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005493 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 IObuff[i] = NUL;
5496 aux = IObuff;
5497
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005498 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 goto exit_matched;
5500 }
5501
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005502 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005504 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (add_r == OK)
5506 /* if dir was BACKWARD then honor it just once */
5507 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005508 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 break;
5510 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005511 else if (action == ACTION_SHOW_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
5513 found = TRUE;
5514 if (!did_show)
5515 gotocmdline(TRUE); /* cursor at status line */
5516 if (curr_fname != prev_fname)
5517 {
5518 if (did_show)
5519 msg_putchar('\n'); /* cursor below last one */
5520 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005521 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 msg_home_replace_hl(curr_fname);
5523 prev_fname = curr_fname;
5524 }
5525 did_show = TRUE;
5526 if (!got_int)
5527 show_pat_in_path(line, type, TRUE, action,
5528 (depth == -1) ? NULL : files[depth].fp,
5529 (depth == -1) ? &lnum : &files[depth].lnum,
5530 match_count++);
5531
5532 /* Set matched flag for this file and all the ones that
5533 * include it */
5534 for (i = 0; i <= depth; ++i)
5535 files[i].matched = TRUE;
5536 }
5537 else if (--count <= 0)
5538 {
5539 found = TRUE;
5540 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005541#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 && g_do_tagpreview == 0
5543#endif
5544 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005545 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 else if (action == ACTION_SHOW)
5547 {
5548 show_pat_in_path(line, type, did_show, action,
5549 (depth == -1) ? NULL : files[depth].fp,
5550 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5551 did_show = TRUE;
5552 }
5553 else
5554 {
5555#ifdef FEAT_GUI
5556 need_mouse_correct = TRUE;
5557#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005558#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 /* ":psearch" uses the preview window */
5560 if (g_do_tagpreview != 0)
5561 {
5562 curwin_save = curwin;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02005563 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565#endif
5566 if (action == ACTION_SPLIT)
5567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005570 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572 if (depth == -1)
5573 {
5574 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005575#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 if (g_do_tagpreview != 0)
5577 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005578 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005579 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005580 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 break; /* failed to jump to file */
5582 }
5583 else
5584#endif
5585 setpcmark();
5586 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005587 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589 else
5590 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005591 if (!GETFILE_SUCCESS(getfile(
5592 0, files[depth].name, NULL, TRUE,
5593 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 break; /* failed to jump to file */
5595 /* autocommands may have changed the lnum, we don't
5596 * want that here */
5597 curwin->w_cursor.lnum = files[depth].lnum;
5598 }
5599 }
5600 if (action != ACTION_SHOW)
5601 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005602 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 curwin->w_set_curswant = TRUE;
5604 }
5605
Bram Moolenaar4033c552017-09-16 20:54:51 +02005606#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005608 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 /* Return cursor to where we were */
5611 validate_cursor();
5612 redraw_later(VALID);
5613 win_enter(curwin_save, TRUE);
5614 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02005615# ifdef FEAT_TEXT_PROP
5616 else if (WIN_IS_POPUP(curwin))
5617 // can't keep focus in popup window
5618 win_enter(firstwin, TRUE);
5619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620#endif
5621 break;
5622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623exit_matched:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 matched = FALSE;
5625 /* look for other matches in the rest of the line if we
5626 * are not at the end of it already */
5627 if (def_regmatch.regprog == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005629 && !(compl_cont_status & CONT_SOL)
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005630 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005631 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 goto search_line;
5633 }
5634 line_breakcheck();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005636 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005637 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 break;
5639
5640 /*
5641 * Read the next line. When reading an included file and encountering
5642 * end-of-file, close the file and continue in the file that included
5643 * it.
5644 */
5645 while (depth >= 0 && !already
5646 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5647 {
5648 fclose(files[depth].fp);
5649 --old_files;
5650 files[old_files].name = files[depth].name;
5651 files[old_files].matched = files[depth].matched;
5652 --depth;
5653 curr_fname = (depth == -1) ? curbuf->b_fname
5654 : files[depth].name;
5655 if (depth < depth_displayed)
5656 depth_displayed = depth;
5657 }
5658 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005661 /* Remove any CR and LF from the line. */
5662 i = (int)STRLEN(line);
5663 if (i > 0 && line[i - 1] == '\n')
5664 line[--i] = NUL;
5665 if (i > 0 && line[i - 1] == '\r')
5666 line[--i] = NUL;
5667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 else if (!already)
5669 {
5670 if (++lnum > end_lnum)
5671 break;
5672 line = ml_get(lnum);
5673 }
5674 already = NULL;
5675 }
5676 /* End of big for (;;) loop. */
5677
5678 /* Close any files that are still open. */
5679 for (i = 0; i <= depth; i++)
5680 {
5681 fclose(files[i].fp);
5682 vim_free(files[i].name);
5683 }
5684 for (i = old_files; i < max_path_depth; i++)
5685 vim_free(files[i].name);
5686 vim_free(files);
5687
5688 if (type == CHECK_PATH)
5689 {
5690 if (!did_show)
5691 {
5692 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005693 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005695 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 }
Bram Moolenaare2c453d2019-08-21 14:37:09 +02005698 else if (!found && action != ACTION_EXPAND)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005700 if (got_int || ins_compl_interrupted())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005701 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005703 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005705 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5708 msg_end();
5709
5710fpip_end:
5711 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005712 vim_regfree(regmatch.regprog);
5713 vim_regfree(incl_regmatch.regprog);
5714 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715}
5716
5717 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005718show_pat_in_path(
5719 char_u *line,
5720 int type,
5721 int did_show,
5722 int action,
5723 FILE *fp,
5724 linenr_T *lnum,
5725 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726{
5727 char_u *p;
5728
5729 if (did_show)
5730 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005731 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 gotocmdline(TRUE); /* cursor at status line */
5733 if (got_int) /* 'q' typed at "--more--" message */
5734 return;
5735 for (;;)
5736 {
5737 p = line + STRLEN(line) - 1;
5738 if (fp != NULL)
5739 {
5740 /* We used fgets(), so get rid of newline at end */
5741 if (p >= line && *p == '\n')
5742 --p;
5743 if (p >= line && *p == '\r')
5744 --p;
5745 *(p + 1) = NUL;
5746 }
5747 if (action == ACTION_SHOW_ALL)
5748 {
5749 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005750 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5752 /* Highlight line numbers */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005753 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
5754 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005756 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 out_flush(); /* show one line at a time */
5758
5759 /* Definition continues until line that doesn't end with '\' */
5760 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5761 break;
5762
5763 if (fp != NULL)
5764 {
5765 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5766 break;
5767 ++*lnum;
5768 }
5769 else
5770 {
5771 if (++*lnum > curbuf->b_ml.ml_line_count)
5772 break;
5773 line = ml_get(*lnum);
5774 }
5775 msg_putchar('\n');
5776 }
5777}
5778#endif
5779
5780#ifdef FEAT_VIMINFO
Bram Moolenaarc3328162019-07-23 22:15:25 +02005781 spat_T *
5782get_spat(int idx)
5783{
5784 return &spats[idx];
5785}
5786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 int
Bram Moolenaarc3328162019-07-23 22:15:25 +02005788get_spat_last_idx(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaarc3328162019-07-23 22:15:25 +02005790 return last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792#endif