blob: f7b3dda3123091e9dd2f7f808140ec623b11f0ef [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
26#ifdef FEAT_VIMINFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010027static void wvsp_one(FILE *fp, int idx, char *s, int sc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#endif
Bram Moolenaarc7a10b32019-05-06 21:37:18 +020029static void search_stat(int dirc, pos_T *pos, int show_top_bot_msg, char_u *msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031/*
32 * This file contains various searching-related routines. These fall into
33 * three groups:
34 * 1. string searches (for /, ?, n, and N)
35 * 2. character searches within a single line (for f, F, t, T, etc)
36 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
37 */
38
39/*
40 * String searches
41 *
42 * The string search functions are divided into two levels:
43 * lowest: searchit(); uses an pos_T for starting position and found match.
44 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
45 *
46 * The last search pattern is remembered for repeating the same search.
47 * This pattern is shared between the :g, :s, ? and / commands.
48 * This is in search_regcomp().
49 *
50 * The actual string matching is done using a heavily modified version of
51 * Henry Spencer's regular expression library. See regexp.c.
52 */
53
54/* The offset for a search command is store in a soff struct */
55/* Note: only spats[0].off is really used */
56struct soffset
57{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000058 int dir; /* search direction, '/' or '?' */
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 int line; /* search has line offset */
60 int end; /* search set cursor at end */
61 long off; /* line or char offset */
62};
63
64/* A search pattern and its attributes are stored in a spat struct */
65struct spat
66{
67 char_u *pat; /* the pattern (in allocated memory) or NULL */
68 int magic; /* magicness of the pattern */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020069 int no_scs; /* no smartcase for this pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 struct soffset off;
71};
72
73/*
74 * Two search patterns are remembered: One for the :substitute command and
75 * one for other searches. last_idx points to the one that was used the last
76 * time.
77 */
78static struct spat spats[2] =
79{
80 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
81 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
82};
83
84static int last_idx = 0; /* index in spats[] for RE_LAST */
85
Bram Moolenaardbd24b52015-08-11 14:26:19 +020086static char_u lastc[2] = {NUL, NUL}; /* last character searched for */
87static int lastcdir = FORWARD; /* last direction of character search */
88static int last_t_cmd = TRUE; /* last search t_cmd */
Bram Moolenaardbd24b52015-08-11 14:26:19 +020089static char_u lastc_bytes[MB_MAXBYTES + 1];
90static int lastc_bytelen = 1; /* >1 for multi-byte char */
Bram Moolenaardbd24b52015-08-11 14:26:19 +020091
Bram Moolenaar071d4272004-06-13 20:20:40 +000092/* copy of spats[], for keeping the search patterns while executing autocmds */
93static struct spat saved_spats[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000094# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +010095static int saved_spats_last_idx = 0;
96static int saved_spats_no_hlsearch = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000097# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000098
99static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
100#ifdef FEAT_RIGHTLEFT
101static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
103
104#ifdef FEAT_FIND_ID
105/*
106 * Type used by find_pattern_in_path() to remember which included files have
107 * been searched already.
108 */
109typedef struct SearchedFile
110{
111 FILE *fp; /* File pointer */
112 char_u *name; /* Full name of file */
113 linenr_T lnum; /* Line we were up to in file */
114 int matched; /* Found a match in this file */
115} SearchedFile;
116#endif
117
118/*
119 * translate search pattern for vim_regcomp()
120 *
121 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
122 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
123 * pat_save == RE_BOTH: save pat in both patterns (:global command)
124 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000125 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
127 * options & SEARCH_HIS: put search string in history
128 * options & SEARCH_KEEP: keep previous search pattern
129 *
130 * returns FAIL if failed, OK otherwise.
131 */
132 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100133search_regcomp(
134 char_u *pat,
135 int pat_save,
136 int pat_use,
137 int options,
138 regmmatch_T *regmatch) /* return: pattern and ignore-case flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139{
140 int magic;
141 int i;
142
143 rc_did_emsg = FALSE;
144 magic = p_magic;
145
146 /*
147 * If no pattern given, use a previously defined pattern.
148 */
149 if (pat == NULL || *pat == NUL)
150 {
151 if (pat_use == RE_LAST)
152 i = last_idx;
153 else
154 i = pat_use;
155 if (spats[i].pat == NULL) /* pattern was never defined */
156 {
157 if (pat_use == RE_SUBST)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100158 emsg(_(e_nopresub));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100160 emsg(_(e_noprevre));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 rc_did_emsg = TRUE;
162 return FAIL;
163 }
164 pat = spats[i].pat;
165 magic = spats[i].magic;
166 no_smartcase = spats[i].no_scs;
167 }
168#ifdef FEAT_CMDHIST
169 else if (options & SEARCH_HIS) /* put new pattern in history */
170 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
171#endif
172
173#ifdef FEAT_RIGHTLEFT
174 if (mr_pattern_alloced)
175 {
176 vim_free(mr_pattern);
177 mr_pattern_alloced = FALSE;
178 }
179
180 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
181 {
182 char_u *rev_pattern;
183
184 rev_pattern = reverse_text(pat);
185 if (rev_pattern == NULL)
186 mr_pattern = pat; /* out of memory, keep normal pattern. */
187 else
188 {
189 mr_pattern = rev_pattern;
190 mr_pattern_alloced = TRUE;
191 }
192 }
193 else
194#endif
195 mr_pattern = pat;
196
197 /*
198 * Save the currently used pattern in the appropriate place,
199 * unless the pattern should not be remembered.
200 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100201 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 {
203 /* search or global command */
204 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
205 save_re_pat(RE_SEARCH, pat, magic);
206 /* substitute or global command */
207 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
208 save_re_pat(RE_SUBST, pat, magic);
209 }
210
211 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000212 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
214 if (regmatch->regprog == NULL)
215 return FAIL;
216 return OK;
217}
218
219/*
220 * Get search pattern used by search_regcomp().
221 */
222 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100223get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224{
225 return mr_pattern;
226}
227
Bram Moolenaarabc97732007-08-08 20:49:37 +0000228#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/*
230 * Reverse text into allocated memory.
231 * Returns the allocated string, NULL when out of memory.
232 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000233 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100234reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 unsigned len;
237 unsigned s_i, rev_i;
238 char_u *rev;
239
240 /*
241 * Reverse the pattern.
242 */
243 len = (unsigned)STRLEN(s);
244 rev = alloc(len + 1);
245 if (rev != NULL)
246 {
247 rev_i = len;
248 for (s_i = 0; s_i < len; ++s_i)
249 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 if (has_mbyte)
251 {
252 int mb_len;
253
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000254 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 rev_i -= mb_len;
256 mch_memmove(rev + rev_i, s + s_i, mb_len);
257 s_i += mb_len - 1;
258 }
259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 rev[--rev_i] = s[s_i];
261
262 }
263 rev[len] = NUL;
264 }
265 return rev;
266}
267#endif
268
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100269 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100270save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271{
272 if (spats[idx].pat != pat)
273 {
274 vim_free(spats[idx].pat);
275 spats[idx].pat = vim_strsave(pat);
276 spats[idx].magic = magic;
277 spats[idx].no_scs = no_smartcase;
278 last_idx = idx;
279#ifdef FEAT_SEARCH_EXTRA
280 /* If 'hlsearch' set and search pat changed: need redraw. */
281 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000282 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200283 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#endif
285 }
286}
287
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288/*
289 * Save the search patterns, so they can be restored later.
290 * Used before/after executing autocommands and user functions.
291 */
292static int save_level = 0;
293
294 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100295save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296{
297 if (save_level++ == 0)
298 {
299 saved_spats[0] = spats[0];
300 if (spats[0].pat != NULL)
301 saved_spats[0].pat = vim_strsave(spats[0].pat);
302 saved_spats[1] = spats[1];
303 if (spats[1].pat != NULL)
304 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100305#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100306 saved_spats_last_idx = last_idx;
307 saved_spats_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 }
310}
311
312 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100313restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314{
315 if (--save_level == 0)
316 {
317 vim_free(spats[0].pat);
318 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100319#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000320 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 vim_free(spats[1].pat);
323 spats[1] = saved_spats[1];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100324#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100325 last_idx = saved_spats_last_idx;
326 set_no_hlsearch(saved_spats_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 }
329}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000331#if defined(EXITFREE) || defined(PROTO)
332 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100333free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000334{
335 vim_free(spats[0].pat);
336 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000337
338# ifdef FEAT_RIGHTLEFT
339 if (mr_pattern_alloced)
340 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200341 vim_free(mr_pattern);
342 mr_pattern_alloced = FALSE;
343 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000344 }
345# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000346}
347#endif
348
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100349#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100350// copy of spats[RE_SEARCH], for keeping the search patterns while incremental
351// searching
352static struct spat saved_last_search_spat;
353static int did_save_last_search_spat = 0;
354static int saved_last_idx = 0;
355static int saved_no_hlsearch = 0;
356
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100357/*
358 * Save and restore the search pattern for incremental highlight search
359 * feature.
360 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100361 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100362 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100363 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100364 */
365 void
366save_last_search_pattern(void)
367{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100368 if (did_save_last_search_spat != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100369 iemsg("did_save_last_search_spat is not zero");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100370 else
371 ++did_save_last_search_spat;
372
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100373 saved_last_search_spat = spats[RE_SEARCH];
374 if (spats[RE_SEARCH].pat != NULL)
375 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
376 saved_last_idx = last_idx;
377 saved_no_hlsearch = no_hlsearch;
378}
379
380 void
381restore_last_search_pattern(void)
382{
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100383 if (did_save_last_search_spat != 1)
384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100385 iemsg("did_save_last_search_spat is not one");
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100386 return;
387 }
388 --did_save_last_search_spat;
389
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100390 vim_free(spats[RE_SEARCH].pat);
391 spats[RE_SEARCH] = saved_last_search_spat;
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100392 saved_last_search_spat.pat = NULL;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100393# if defined(FEAT_EVAL)
394 set_vv_searchforward();
395# endif
396 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200397 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100398}
Bram Moolenaard0480092017-11-16 22:20:39 +0100399
400 char_u *
401last_search_pattern(void)
402{
403 return spats[RE_SEARCH].pat;
404}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100405#endif
406
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407/*
408 * Return TRUE when case should be ignored for search pattern "pat".
409 * Uses the 'ignorecase' and 'smartcase' options.
410 */
411 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100412ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200414 return ignorecase_opt(pat, p_ic, p_scs);
415}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200417/*
418 * As ignorecase() put pass the "ic" and "scs" flags.
419 */
420 int
421ignorecase_opt(char_u *pat, int ic_in, int scs)
422{
423 int ic = ic_in;
424
425 if (ic && !no_smartcase && scs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#ifdef FEAT_INS_EXPAND
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100427 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#endif
429 )
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200430 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 no_smartcase = FALSE;
432
433 return ic;
434}
435
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200436/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200437 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200438 */
439 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100440pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200441{
442 char_u *p = pat;
443
444 while (*p != NUL)
445 {
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200446 int l;
447
448 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
449 {
450 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
451 return TRUE;
452 p += l;
453 }
Bram Moolenaar264b74f2019-01-24 17:18:42 +0100454 else if (*p == '\\')
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200455 {
456 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
457 p += 3;
458 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
459 p += 3;
460 else if (p[1] != NUL) /* skip "\X" */
461 p += 2;
462 else
463 p += 1;
464 }
465 else if (MB_ISUPPER(*p))
466 return TRUE;
467 else
468 ++p;
469 }
470 return FALSE;
471}
472
Bram Moolenaar113e1072019-01-20 15:30:40 +0100473#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100475last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200476{
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200477 return lastc_bytes;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200478}
479
480 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100481last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200482{
483 return lastcdir == FORWARD;
484}
485
486 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100487last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200488{
489 return last_t_cmd == TRUE;
490}
491
492 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100493set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200494{
495 *lastc = c;
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200496 lastc_bytelen = len;
497 if (len)
498 memcpy(lastc_bytes, s, len);
499 else
500 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200501}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100502#endif
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200503
504 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100505set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200506{
507 lastcdir = cdir;
508}
509
510 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100511set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200512{
513 last_t_cmd = t_cmd;
514}
515
516 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100517last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518{
519 return spats[last_idx].pat;
520}
521
522/*
523 * Reset search direction to forward. For "gd" and "gD" commands.
524 */
525 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100526reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527{
528 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#if defined(FEAT_EVAL)
530 set_vv_searchforward();
531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532}
533
534#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
535/*
536 * Set the last search pattern. For ":let @/ =" and viminfo.
537 * Also set the saved search pattern, so that this works in an autocommand.
538 */
539 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100540set_last_search_pat(
541 char_u *s,
542 int idx,
543 int magic,
544 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545{
546 vim_free(spats[idx].pat);
547 /* An empty string means that nothing should be matched. */
548 if (*s == NUL)
549 spats[idx].pat = NULL;
550 else
551 spats[idx].pat = vim_strsave(s);
552 spats[idx].magic = magic;
553 spats[idx].no_scs = FALSE;
554 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000555#if defined(FEAT_EVAL)
556 set_vv_searchforward();
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 spats[idx].off.line = FALSE;
559 spats[idx].off.end = FALSE;
560 spats[idx].off.off = 0;
561 if (setlast)
562 last_idx = idx;
563 if (save_level)
564 {
565 vim_free(saved_spats[idx].pat);
566 saved_spats[idx] = spats[0];
567 if (spats[idx].pat == NULL)
568 saved_spats[idx].pat = NULL;
569 else
570 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
Bram Moolenaar975880b2019-03-03 14:42:11 +0100571# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaared8bc782018-12-01 21:08:21 +0100572 saved_spats_last_idx = last_idx;
Bram Moolenaar975880b2019-03-03 14:42:11 +0100573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575# ifdef FEAT_SEARCH_EXTRA
576 /* If 'hlsearch' set and search pat changed: need redraw. */
577 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000578 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579# endif
580}
581#endif
582
583#ifdef FEAT_SEARCH_EXTRA
584/*
585 * Get a regexp program for the last used search pattern.
586 * This is used for highlighting all matches in a window.
587 * Values returned in regmatch->regprog and regmatch->rmm_ic.
588 */
589 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100590last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 if (spats[last_idx].pat == NULL)
593 {
594 regmatch->regprog = NULL;
595 return;
596 }
597 ++emsg_off; /* So it doesn't beep if bad expr */
598 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
599 --emsg_off;
600}
601#endif
602
603/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100604 * Lowest level search function.
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100605 * Search for 'count'th occurrence of pattern "pat" in direction "dir".
606 * Start at position "pos" and return the found position in "pos".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 *
608 * if (options & SEARCH_MSG) == 0 don't give any messages
609 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
610 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
611 * if (options & SEARCH_HIS) put search pattern in history
612 * if (options & SEARCH_END) return position at end of match
613 * if (options & SEARCH_START) accept match at pos itself
614 * if (options & SEARCH_KEEP) keep previous search pattern
615 * if (options & SEARCH_FOLD) match only once in a closed fold
616 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100617 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 *
619 * Return FAIL (zero) for failure, non-zero for success.
620 * When FEAT_EVAL is defined, returns the index of the first matching
621 * subpattern plus one; one if there was none.
622 */
623 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100624searchit(
625 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100627 buf_T *buf,
628 pos_T *pos,
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100629 pos_T *end_pos, // set to end of the match, unless NULL
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100630 int dir,
631 char_u *pat,
632 long count,
633 int options,
634 int pat_use, /* which pattern to use when "pat" is empty */
635 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200636 proftime_T *tm UNUSED, /* timeout limit or NULL */
637 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
639 int found;
640 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100641 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 regmmatch_T regmatch;
643 char_u *ptr;
644 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000646 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 int loop;
648 pos_T start_pos;
649 int at_first_line;
650 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200651 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 int match_ok;
653 long nmatched;
654 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100655 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000656 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657#ifdef FEAT_SEARCH_EXTRA
658 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#endif
660
661 if (search_regcomp(pat, RE_SEARCH, pat_use,
662 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
663 {
664 if ((options & SEARCH_MSG) && !rc_did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100665 semsg(_("E383: Invalid search string: %s"), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 return FAIL;
667 }
668
Bram Moolenaar280f1262006-01-30 00:14:18 +0000669 /*
670 * find the string
671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 called_emsg = FALSE;
673 do /* loop for count */
674 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100675 /* When not accepting a match at the start position set "extra_col" to
676 * a non-zero value. Don't do that when starting at MAXCOL, since
677 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200678 if (pos->col == MAXCOL)
679 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100680 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200681 else if (has_mbyte
682 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
683 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100684 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100685 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100686 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200687 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100688 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100689 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100690 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100691 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200692 start_char_len = 1;
693 if (dir == FORWARD)
694 {
695 if (options & SEARCH_START)
696 extra_col = 0;
697 else
698 extra_col = start_char_len;
699 }
700 else
701 {
702 if (options & SEARCH_START)
703 extra_col = start_char_len;
704 else
705 extra_col = 0;
706 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 start_pos = *pos; /* remember start pos for detecting no match */
709 found = 0; /* default: not found */
710 at_first_line = TRUE; /* default: start in first line */
711 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
712 {
713 pos->lnum = 1;
714 pos->col = 0;
715 at_first_line = FALSE; /* not in first line now */
716 }
717
718 /*
719 * Start searching in current line, unless searching backwards and
720 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000721 * If we are searching backwards, in column 0, and not including the
722 * current position, gain some efficiency by skipping back a line.
723 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000725 if (dir == BACKWARD && start_pos.col == 0
726 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
728 lnum = pos->lnum - 1;
729 at_first_line = FALSE;
730 }
731 else
732 lnum = pos->lnum;
733
734 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
735 {
736 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
737 lnum += dir, at_first_line = FALSE)
738 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000739 /* Stop after checking "stop_lnum", if it's set. */
740 if (stop_lnum != 0 && (dir == FORWARD
741 ? lnum > stop_lnum : lnum < stop_lnum))
742 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000743#ifdef FEAT_RELTIME
744 /* Stop after passing the "tm" time limit. */
745 if (tm != NULL && profile_passed_limit(tm))
746 break;
747#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000750 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100752 col = at_first_line && (options & SEARCH_COL) ? pos->col
753 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100755 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000756#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200757 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000758#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200759 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000760#endif
761 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200763 if (called_emsg
764#ifdef FEAT_RELTIME
765 || (timed_out != NULL && *timed_out)
766#endif
767 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 break;
769 if (nmatched > 0)
770 {
771 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000772 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000774#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000776#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000777 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000778 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
779 ptr = (char_u *)"";
780 else
781 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
783 /*
784 * Forward search in the first line: match should be after
785 * the start position. If not, continue at the end of the
786 * match (this is vi compatible) or on the next char.
787 */
788 if (dir == FORWARD && at_first_line)
789 {
790 match_ok = TRUE;
791 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000792 * When the match starts in a next line it's certainly
793 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * When match lands on a NUL the cursor will be put
795 * one back afterwards, compare with that position,
796 * otherwise "/$" will get stuck on end of line.
797 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000798 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100799 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000800 ? (nmatched == 1
801 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000803 : ((int)matchpos.col
804 - (ptr[matchpos.col] == NUL)
805 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
807 /*
808 * If vi-compatible searching, continue at the end
809 * of the match, otherwise continue one position
810 * forward.
811 */
812 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
813 {
814 if (nmatched > 1)
815 {
816 /* end is in next line, thus no match in
817 * this line */
818 match_ok = FALSE;
819 break;
820 }
821 matchcol = endpos.col;
822 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000823 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 && ptr[matchcol] != NUL)
825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 if (has_mbyte)
827 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000828 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++matchcol;
831 }
832 }
833 else
834 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000835 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (ptr[matchcol] != NUL)
837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000839 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 + matchcol);
841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 ++matchcol;
843 }
844 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200845 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 if (ptr[matchcol] == NUL
848 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000849 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000850 matchcol,
851#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200852 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000853#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200854 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000855#endif
856 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
858 match_ok = FALSE;
859 break;
860 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000861 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 endpos = regmatch.endpos[0];
863# ifdef FEAT_EVAL
864 submatch = first_submatch(&regmatch);
865# endif
866
867 /* Need to get the line pointer again, a
868 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000869 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 if (!match_ok)
872 continue;
873 }
874 if (dir == BACKWARD)
875 {
876 /*
877 * Now, if there are multiple matches on this line,
878 * we have to get the last one. Or the last one before
879 * the cursor, if we're on that line.
880 * When putting the new cursor at the end, compare
881 * relative to the end of the match.
882 */
883 match_ok = FALSE;
884 for (;;)
885 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000886 /* Remember a position that is before the start
887 * position, we use it if it's the last match in
888 * the line. Always accept a position after
889 * wrapping around. */
890 if (loop
891 || ((options & SEARCH_END)
892 ? (lnum + regmatch.endpos[0].lnum
893 < start_pos.lnum
894 || (lnum + regmatch.endpos[0].lnum
895 == start_pos.lnum
896 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200897 < (int)start_pos.col
898 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000899 : (lnum + regmatch.startpos[0].lnum
900 < start_pos.lnum
901 || (lnum + regmatch.startpos[0].lnum
902 == start_pos.lnum
903 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200904 < (int)start_pos.col
905 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000908 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 endpos = regmatch.endpos[0];
910# ifdef FEAT_EVAL
911 submatch = first_submatch(&regmatch);
912# endif
913 }
914 else
915 break;
916
917 /*
918 * We found a valid match, now check if there is
919 * another one after it.
920 * If vi-compatible searching, continue at the end
921 * of the match, otherwise continue one position
922 * forward.
923 */
924 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
925 {
926 if (nmatched > 1)
927 break;
928 matchcol = endpos.col;
929 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000930 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 && ptr[matchcol] != NUL)
932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if (has_mbyte)
934 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000935 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 ++matchcol;
938 }
939 }
940 else
941 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000942 /* Stop when the match is in a next line. */
943 if (matchpos.lnum > 0)
944 break;
945 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (ptr[matchcol] != NUL)
947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 if (has_mbyte)
949 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000950 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 ++matchcol;
953 }
954 }
955 if (ptr[matchcol] == NUL
956 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000957 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000958 matchcol,
959#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200960 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000961#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200962 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000963#endif
964 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100965 {
966#ifdef FEAT_RELTIME
967 /* If the search timed out, we did find a match
968 * but it might be the wrong one, so that's not
969 * OK. */
970 if (timed_out != NULL && *timed_out)
971 match_ok = FALSE;
972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 /* Need to get the line pointer again, a
977 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000978 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980
981 /*
982 * If there is only a match after the cursor, skip
983 * this match.
984 */
985 if (!match_ok)
986 continue;
987 }
988
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000989 /* With the SEARCH_END option move to the last character
990 * of the match. Don't do it for an empty match, end
991 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200992 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000993 && !(matchpos.lnum == endpos.lnum
994 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000996 /* For a match in the first column, set the position
997 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000998 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000999 pos->col = endpos.col;
1000 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001001 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001002 if (pos->lnum > 1) /* just in case */
1003 {
1004 --pos->lnum;
1005 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1006 pos->lnum, FALSE));
1007 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001008 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001009 else
1010 {
1011 --pos->col;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001012 if (has_mbyte
1013 && pos->lnum <= buf->b_ml.ml_line_count)
1014 {
1015 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1016 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1017 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001018 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001019 if (end_pos != NULL)
1020 {
1021 end_pos->lnum = lnum + matchpos.lnum;
1022 end_pos->col = matchpos.col;
1023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025 else
1026 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001027 pos->lnum = lnum + matchpos.lnum;
1028 pos->col = matchpos.col;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001029 if (end_pos != NULL)
1030 {
1031 end_pos->lnum = lnum + endpos.lnum;
1032 end_pos->col = endpos.col;
1033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 pos->coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01001036 if (end_pos != NULL)
1037 end_pos->coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001039 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
1041 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001042 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 search_match_endcol = endpos.col;
1044 break;
1045 }
1046 line_breakcheck(); /* stop if ctrl-C typed */
1047 if (got_int)
1048 break;
1049
1050#ifdef FEAT_SEARCH_EXTRA
1051 /* Cancel searching if a character was typed. Used for
1052 * 'incsearch'. Don't check too often, that would slowdown
1053 * searching too much. */
1054 if ((options & SEARCH_PEEK)
1055 && ((lnum - pos->lnum) & 0x3f) == 0
1056 && char_avail())
1057 {
1058 break_loop = TRUE;
1059 break;
1060 }
1061#endif
1062
1063 if (loop && lnum == start_pos.lnum)
1064 break; /* if second loop, stop where started */
1065 }
1066 at_first_line = FALSE;
1067
1068 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001069 * Stop the search if wrapscan isn't set, "stop_lnum" is
1070 * specified, after an interrupt, after a match and after looping
1071 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001073 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001074#ifdef FEAT_RELTIME
1075 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001076#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001077#ifdef FEAT_SEARCH_EXTRA
1078 || break_loop
1079#endif
1080 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 break;
1082
1083 /*
1084 * If 'wrapscan' is set we continue at the other end of the file.
1085 * If 'shortmess' does not contain 's', we give a message.
1086 * This message is also remembered in keep_msg for when the screen
1087 * is redrawn. The keep_msg is cleared whenever another message is
1088 * written.
1089 */
1090 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001094 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1095 give_warning((char_u *)_(dir == BACKWARD
1096 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001098 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001099#ifdef FEAT_RELTIME
1100 || (timed_out != NULL && *timed_out)
1101#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001102#ifdef FEAT_SEARCH_EXTRA
1103 || break_loop
1104#endif
1105 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 break;
1107 }
1108 while (--count > 0 && found); /* stop after count matches or no match */
1109
Bram Moolenaar473de612013-06-08 18:19:48 +02001110 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
Bram Moolenaar280f1262006-01-30 00:14:18 +00001112 called_emsg |= save_called_emsg;
1113
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 if (!found) /* did not find it */
1115 {
1116 if (got_int)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001117 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1119 {
1120 if (p_ws)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001121 semsg(_(e_patnotf2), mr_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 else if (lnum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001123 semsg(_("E384: search hit TOP without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 mr_pattern);
1125 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001126 semsg(_("E385: search hit BOTTOM without match for: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 mr_pattern);
1128 }
1129 return FAIL;
1130 }
1131
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001132 /* A pattern like "\n\zs" may go past the last line. */
1133 if (pos->lnum > buf->b_ml.ml_line_count)
1134 {
1135 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001136 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001137 if (pos->col > 0)
1138 --pos->col;
1139 }
1140
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 return submatch + 1;
1142}
1143
1144#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001145 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001146set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001147{
1148 spats[0].off.dir = cdir;
1149}
1150
1151 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001152set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001153{
1154 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1155}
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157/*
1158 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001159 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 */
1161 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001162first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163{
1164 int submatch;
1165
1166 for (submatch = 1; ; ++submatch)
1167 {
1168 if (rp->startpos[submatch].lnum >= 0)
1169 break;
1170 if (submatch == 9)
1171 {
1172 submatch = 0;
1173 break;
1174 }
1175 }
1176 return submatch;
1177}
1178#endif
1179
1180/*
1181 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001182 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 * If 'dirc' is 0: use previous dir.
1184 * If 'pat' is NULL or empty : use previous string.
1185 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1186 * If 'options & SEARCH_ECHO': echo the search command and handle options
1187 * If 'options & SEARCH_MSG' : may give error message
1188 * If 'options & SEARCH_OPT' : interpret optional flags
1189 * If 'options & SEARCH_HIS' : put search pattern in history
1190 * If 'options & SEARCH_NOOF': don't add offset to position
1191 * If 'options & SEARCH_MARK': set previous context mark
1192 * If 'options & SEARCH_KEEP': keep previous search pattern
1193 * If 'options & SEARCH_START': accept match at curpos itself
1194 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1195 *
1196 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1197 * makes the movement linewise without moving the match position.
1198 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001199 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
1201 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001202do_search(
1203 oparg_T *oap, /* can be NULL */
1204 int dirc, /* '/' or '?' */
1205 char_u *pat,
1206 long count,
1207 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001208 proftime_T *tm, /* timeout limit or NULL */
1209 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210{
1211 pos_T pos; /* position of the last match */
1212 char_u *searchstr;
1213 struct soffset old_off;
1214 int retval; /* Return value */
1215 char_u *p;
1216 long c;
1217 char_u *dircp;
1218 char_u *strcopy = NULL;
1219 char_u *ps;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001220 char_u *msgbuf = NULL;
1221 size_t len;
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02001222#define SEARCH_STAT_BUF_LEN 12
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224 /*
1225 * A line offset is not remembered, this is vi compatible.
1226 */
1227 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1228 {
1229 spats[0].off.line = FALSE;
1230 spats[0].off.off = 0;
1231 }
1232
1233 /*
1234 * Save the values for when (options & SEARCH_KEEP) is used.
1235 * (there is no "if ()" around this because gcc wants them initialized)
1236 */
1237 old_off = spats[0].off;
1238
1239 pos = curwin->w_cursor; /* start searching at the cursor position */
1240
1241 /*
1242 * Find out the direction of the search.
1243 */
1244 if (dirc == 0)
1245 dirc = spats[0].off.dir;
1246 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001249#if defined(FEAT_EVAL)
1250 set_vv_searchforward();
1251#endif
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if (options & SEARCH_REV)
1254 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001255#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 /* There is a bug in the Visual C++ 2.2 compiler which means that
1257 * dirc always ends up being '/' */
1258 dirc = (dirc == '/') ? '?' : '/';
1259#else
1260 if (dirc == '/')
1261 dirc = '?';
1262 else
1263 dirc = '/';
1264#endif
1265 }
1266
1267#ifdef FEAT_FOLDING
1268 /* If the cursor is in a closed fold, don't find another match in the same
1269 * fold. */
1270 if (dirc == '/')
1271 {
1272 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1273 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1274 }
1275 else
1276 {
1277 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1278 pos.col = 0;
1279 }
1280#endif
1281
1282#ifdef FEAT_SEARCH_EXTRA
1283 /*
1284 * Turn 'hlsearch' highlighting back on.
1285 */
1286 if (no_hlsearch && !(options & SEARCH_KEEP))
1287 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001288 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001289 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291#endif
1292
1293 /*
1294 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1295 */
1296 for (;;)
1297 {
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001298 int show_top_bot_msg = FALSE;
1299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 searchstr = pat;
1301 dircp = NULL;
1302 /* use previous pattern */
1303 if (pat == NULL || *pat == NUL || *pat == dirc)
1304 {
1305 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1306 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001307 searchstr = spats[RE_SUBST].pat;
1308 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001310 emsg(_(e_noprevre));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001311 retval = 0;
1312 goto end_do_search;
1313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001315 else
1316 {
1317 /* make search_regcomp() use spats[RE_SEARCH].pat */
1318 searchstr = (char_u *)"";
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321
1322 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1323 {
1324 /*
1325 * Find end of regular expression.
1326 * If there is a matching '/' or '?', toss it.
1327 */
1328 ps = strcopy;
1329 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1330 if (strcopy != ps)
1331 {
1332 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001333 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 pat = strcopy;
1335 searchstr = strcopy;
1336 }
1337 if (*p == dirc)
1338 {
1339 dircp = p; /* remember where we put the NUL */
1340 *p++ = NUL;
1341 }
1342 spats[0].off.line = FALSE;
1343 spats[0].off.end = FALSE;
1344 spats[0].off.off = 0;
1345 /*
1346 * Check for a line offset or a character offset.
1347 * For get_address (echo off) we don't check for a character
1348 * offset, because it is meaningless and the 's' could be a
1349 * substitute command.
1350 */
1351 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1352 spats[0].off.line = TRUE;
1353 else if ((options & SEARCH_OPT) &&
1354 (*p == 'e' || *p == 's' || *p == 'b'))
1355 {
1356 if (*p == 'e') /* end */
1357 spats[0].off.end = SEARCH_END;
1358 ++p;
1359 }
1360 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1361 {
1362 /* 'nr' or '+nr' or '-nr' */
1363 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1364 spats[0].off.off = atol((char *)p);
1365 else if (*p == '-') /* single '-' */
1366 spats[0].off.off = -1;
1367 else /* single '+' */
1368 spats[0].off.off = 1;
1369 ++p;
1370 while (VIM_ISDIGIT(*p)) /* skip number */
1371 ++p;
1372 }
1373
1374 /* compute length of search command for get_address() */
1375 searchcmdlen += (int)(p - pat);
1376
1377 pat = p; /* put pat after search command */
1378 }
1379
1380 if ((options & SEARCH_ECHO) && messaging()
1381 && !cmd_silent && msg_silent == 0)
1382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 char_u *trunc;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001384 char_u off_buf[40];
Bram Moolenaard33a7642019-05-24 17:56:14 +02001385 size_t off_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001387 // Compute msg_row early.
1388 msg_start();
1389
Bram Moolenaar984f0312019-05-24 13:11:47 +02001390 // Get the offset, so we know how long it is.
1391 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1392 {
1393 p = off_buf;
1394 *p++ = dirc;
1395 if (spats[0].off.end)
1396 *p++ = 'e';
1397 else if (!spats[0].off.line)
1398 *p++ = 's';
1399 if (spats[0].off.off > 0 || spats[0].off.line)
1400 *p++ = '+';
1401 *p = NUL;
1402 if (spats[0].off.off != 0 || spats[0].off.line)
1403 sprintf((char *)p, "%ld", spats[0].off.off);
1404 off_len = STRLEN(off_buf);
1405 }
1406
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001408 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 else
1410 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001411
1412 if (!shortmess(SHM_SEARCHCOUNT))
1413 {
1414 // Reserve enough space for the search pattern + offset +
Bram Moolenaar984f0312019-05-24 13:11:47 +02001415 // search stat. Use all the space available, so that the
1416 // search state is right aligned. If there is not enough space
1417 // msg_strtrunc() will shorten in the middle.
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001418 if (msg_scrolled != 0)
1419 // Use all the columns.
1420 len = (int)(Rows - msg_row) * Columns - 1;
1421 else
1422 // Use up to 'showcmd' column.
1423 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
Bram Moolenaar984f0312019-05-24 13:11:47 +02001424 if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3)
1425 len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001426 }
1427 else
1428 // Reserve enough space for the search pattern + offset.
Bram Moolenaar984f0312019-05-24 13:11:47 +02001429 len = STRLEN(p) + off_len + 3;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001430
1431 msgbuf = alloc((int)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (msgbuf != NULL)
1433 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001434 vim_memset(msgbuf, ' ', len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 msgbuf[0] = dirc;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001436 msgbuf[len - 1] = NUL;
1437
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001438 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1439 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001440 // Use a space to draw the composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001441 msgbuf[1] = ' ';
Bram Moolenaarb3de6c42019-05-05 13:02:28 +02001442 mch_memmove(msgbuf + 2, p, STRLEN(p));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001443 }
1444 else
Bram Moolenaarb3de6c42019-05-05 13:02:28 +02001445 mch_memmove(msgbuf + 1, p, STRLEN(p));
Bram Moolenaar984f0312019-05-24 13:11:47 +02001446 if (off_len > 0)
1447 mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
Bram Moolenaar984f0312019-05-24 13:11:47 +02001449 trunc = msg_strtrunc(msgbuf, TRUE);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001450 if (trunc != NULL)
1451 {
1452 vim_free(msgbuf);
1453 msgbuf = trunc;
1454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
1456#ifdef FEAT_RIGHTLEFT
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001457 // The search pattern could be shown on the right in rightleft
1458 // mode, but the 'ruler' and 'showcmd' area use it too, thus
1459 // it would be blanked out again very soon. Show it on the
1460 // left, but do reverse the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1462 {
1463 char_u *r;
1464
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001465 r = reverse_text(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (r != NULL)
1467 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001468 vim_free(msgbuf);
1469 msgbuf = r;
1470 // move reversed text to beginning of buffer
1471 while (*r != NUL && *r == ' ')
1472 r++;
1473 mch_memmove(msgbuf, r, msgbuf + STRLEN(msgbuf) - r);
1474 // overwrite old text
1475 vim_memset(r, ' ', msgbuf + STRLEN(msgbuf) - r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477 }
1478#endif
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001479 msg_outtrans(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 msg_clr_eos();
1481 msg_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482
1483 gotocmdline(FALSE);
1484 out_flush();
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001485 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 }
1487 }
1488
1489 /*
1490 * If there is a character offset, subtract it from the current
1491 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001492 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 * This is not done for a line offset, because then we would not be vi
1494 * compatible.
1495 */
Bram Moolenaared203462004-06-16 11:19:22 +00001496 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 if (spats[0].off.off > 0)
1499 {
1500 for (c = spats[0].off.off; c; --c)
1501 if (decl(&pos) == -1)
1502 break;
1503 if (c) /* at start of buffer */
1504 {
1505 pos.lnum = 0; /* allow lnum == 0 here */
1506 pos.col = MAXCOL;
1507 }
1508 }
1509 else
1510 {
1511 for (c = spats[0].off.off; c; ++c)
1512 if (incl(&pos) == -1)
1513 break;
1514 if (c) /* at end of buffer */
1515 {
1516 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1517 pos.col = 0;
1518 }
1519 }
1520 }
1521
Bram Moolenaar14184a32019-02-16 15:10:30 +01001522 c = searchit(curwin, curbuf, &pos, NULL,
1523 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 searchstr, count, spats[0].off.end + (options &
1525 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1526 + SEARCH_MSG + SEARCH_START
1527 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001528 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529
1530 if (dircp != NULL)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001531 *dircp = dirc; // restore second '/' or '?' for normal_cmd()
1532
1533 if (!shortmess(SHM_SEARCH)
1534 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1535 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001536 show_top_bot_msg = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 if (c == FAIL)
1539 {
1540 retval = 0;
1541 goto end_do_search;
1542 }
1543 if (spats[0].off.end && oap != NULL)
1544 oap->inclusive = TRUE; /* 'e' includes last character */
1545
1546 retval = 1; /* pattern found */
1547
1548 /*
1549 * Add character and/or line offset
1550 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001551 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 {
1553 if (spats[0].off.line) /* Add the offset to the line number. */
1554 {
1555 c = pos.lnum + spats[0].off.off;
1556 if (c < 1)
1557 pos.lnum = 1;
1558 else if (c > curbuf->b_ml.ml_line_count)
1559 pos.lnum = curbuf->b_ml.ml_line_count;
1560 else
1561 pos.lnum = c;
1562 pos.col = 0;
1563
1564 retval = 2; /* pattern found, line offset added */
1565 }
Bram Moolenaared203462004-06-16 11:19:22 +00001566 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 {
1568 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001569 c = spats[0].off.off;
1570 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001572 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 if (incl(&pos) == -1)
1574 break;
1575 }
1576 /* to the left, check for start of file */
1577 else
1578 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001579 while (c++ < 0)
1580 if (decl(&pos) == -1)
1581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 }
1584 }
1585
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001586 // Show [1/15] if 'S' is not in 'shortmess'.
1587 if ((options & SEARCH_ECHO)
1588 && messaging()
1589 && !(cmd_silent + msg_silent)
1590 && c != FAIL
1591 && !shortmess(SHM_SEARCHCOUNT)
1592 && msgbuf != NULL)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02001593 search_stat(dirc, &pos, show_top_bot_msg, msgbuf);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 /*
1596 * The search command can be followed by a ';' to do another search.
1597 * For example: "/pat/;/foo/+3;?bar"
1598 * This is like doing another search command, except:
1599 * - The remembered direction '/' or '?' is from the first search.
1600 * - When an error happens the cursor isn't moved at all.
1601 * Don't do this when called by get_address() (it handles ';' itself).
1602 */
1603 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1604 break;
1605
1606 dirc = *++pat;
1607 if (dirc != '?' && dirc != '/')
1608 {
1609 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001610 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 goto end_do_search;
1612 }
1613 ++pat;
1614 }
1615
1616 if (options & SEARCH_MARK)
1617 setpcmark();
1618 curwin->w_cursor = pos;
1619 curwin->w_set_curswant = TRUE;
1620
1621end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001622 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 spats[0].off = old_off;
1624 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001625 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 return retval;
1628}
1629
1630#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1631/*
1632 * search_for_exact_line(buf, pos, dir, pat)
1633 *
1634 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001635 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001637 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * Return OK for success, or FAIL if no line found.
1639 */
1640 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001641search_for_exact_line(
1642 buf_T *buf,
1643 pos_T *pos,
1644 int dir,
1645 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646{
1647 linenr_T start = 0;
1648 char_u *ptr;
1649 char_u *p;
1650
1651 if (buf->b_ml.ml_line_count == 0)
1652 return FAIL;
1653 for (;;)
1654 {
1655 pos->lnum += dir;
1656 if (pos->lnum < 1)
1657 {
1658 if (p_ws)
1659 {
1660 pos->lnum = buf->b_ml.ml_line_count;
1661 if (!shortmess(SHM_SEARCH))
1662 give_warning((char_u *)_(top_bot_msg), TRUE);
1663 }
1664 else
1665 {
1666 pos->lnum = 1;
1667 break;
1668 }
1669 }
1670 else if (pos->lnum > buf->b_ml.ml_line_count)
1671 {
1672 if (p_ws)
1673 {
1674 pos->lnum = 1;
1675 if (!shortmess(SHM_SEARCH))
1676 give_warning((char_u *)_(bot_top_msg), TRUE);
1677 }
1678 else
1679 {
1680 pos->lnum = 1;
1681 break;
1682 }
1683 }
1684 if (pos->lnum == start)
1685 break;
1686 if (start == 0)
1687 start = pos->lnum;
1688 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1689 p = skipwhite(ptr);
1690 pos->col = (colnr_T) (p - ptr);
1691
1692 /* when adding lines the matching line may be empty but it is not
1693 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001694 if ((compl_cont_status & CONT_ADDING)
1695 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 {
1697 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1698 return OK;
1699 }
1700 else if (*p != NUL) /* ignore empty lines */
1701 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001702 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1703 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 return OK;
1705 }
1706 }
1707 return FAIL;
1708}
1709#endif /* FEAT_INS_EXPAND */
1710
1711/*
1712 * Character Searches
1713 */
1714
1715/*
1716 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1717 * position of the character, otherwise move to just before the char.
1718 * Do this "cap->count1" times.
1719 * Return FAIL or OK.
1720 */
1721 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001722searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 int c = cap->nchar; /* char to search for */
1725 int dir = cap->arg; /* TRUE for searching forward */
1726 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 int col;
1728 char_u *p;
1729 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001730 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
1732 if (c != NUL) /* normal search: remember args for repeat */
1733 {
1734 if (!KeyStuffed) /* don't remember when redoing */
1735 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001736 *lastc = c;
1737 set_csearch_direction(dir);
1738 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001739 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 if (cap->ncharC1 != 0)
1741 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001742 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1743 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001745 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1746 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749 }
1750 else /* repeat previous search */
1751 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001752 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 return FAIL;
1754 if (dir) /* repeat in opposite direction */
1755 dir = -lastcdir;
1756 else
1757 dir = lastcdir;
1758 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001759 c = *lastc;
1760 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001761
1762 /* Force a move of at least one char, so ";" and "," will move the
1763 * cursor, even if the cursor is right in front of char we are looking
1764 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001765 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001766 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001769 if (dir == BACKWARD)
1770 cap->oap->inclusive = FALSE;
1771 else
1772 cap->oap->inclusive = TRUE;
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 p = ml_get_curline();
1775 col = curwin->w_cursor.col;
1776 len = (int)STRLEN(p);
1777
1778 while (count--)
1779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (has_mbyte)
1781 {
1782 for (;;)
1783 {
1784 if (dir > 0)
1785 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001786 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (col >= len)
1788 return FAIL;
1789 }
1790 else
1791 {
1792 if (col == 0)
1793 return FAIL;
1794 col -= (*mb_head_off)(p, p + col - 1) + 1;
1795 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001796 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001798 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 break;
1800 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001801 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001802 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001803 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001804 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 }
1806 }
1807 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
1809 for (;;)
1810 {
1811 if ((col += dir) < 0 || col >= len)
1812 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001813 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001815 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
1817 }
1818 }
1819
1820 if (t_cmd)
1821 {
1822 /* backup to before the character (possibly double-byte) */
1823 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (has_mbyte)
1825 {
1826 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001827 /* Landed on the search char which is lastc_bytelen long */
1828 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 else
1830 /* To previous char, which may be multi-byte. */
1831 col -= (*mb_head_off)(p, p + col);
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 curwin->w_cursor.col = col;
1835
1836 return OK;
1837}
1838
1839/*
1840 * "Other" Searches
1841 */
1842
1843/*
1844 * findmatch - find the matching paren or brace
1845 *
1846 * Improvement over vi: Braces inside quotes are ignored.
1847 */
1848 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001849findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
1851 return findmatchlimit(oap, initc, 0, 0);
1852}
1853
1854/*
1855 * Return TRUE if the character before "linep[col]" equals "ch".
1856 * Return FALSE if "col" is zero.
1857 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1858 * is NULL.
1859 * Handles multibyte string correctly.
1860 */
1861 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001862check_prevcol(
1863 char_u *linep,
1864 int col,
1865 int ch,
1866 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
1868 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (col > 0 && has_mbyte)
1870 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (prevcol)
1872 *prevcol = col;
1873 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1874}
1875
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001876/*
1877 * Raw string start is found at linep[startpos.col - 1].
1878 * Return TRUE if the matching end can be found between startpos and endpos.
1879 */
1880 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001881find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001882{
1883 char_u *p;
1884 char_u *delim_copy;
1885 size_t delim_len;
1886 linenr_T lnum;
1887 int found = FALSE;
1888
1889 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1890 ;
1891 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001892 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001893 if (delim_copy == NULL)
1894 return FALSE;
1895 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1896 {
1897 char_u *line = ml_get(lnum);
1898
1899 for (p = line + (lnum == startpos->lnum
1900 ? startpos->col + 1 : 0); *p; ++p)
1901 {
1902 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1903 break;
1904 if (*p == ')' && p[delim_len + 1] == '"'
1905 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1906 {
1907 found = TRUE;
1908 break;
1909 }
1910 }
1911 if (found)
1912 break;
1913 }
1914 vim_free(delim_copy);
1915 return found;
1916}
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918/*
1919 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001920 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1921 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 *
1923 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001924 * character at or after the cursor. Special values:
1925 * '*' look for C-style comment / *
1926 * '/' look for C-style comment / *, ignoring comment-end
1927 * '#' look for preprocessor directives
1928 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 *
1930 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1931 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1932 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1933 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001934 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001935 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001936 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 */
1938
1939 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001940findmatchlimit(
1941 oparg_T *oap,
1942 int initc,
1943 int flags,
1944 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 static pos_T pos; /* current search position */
1947 int findc = 0; /* matching brace */
1948 int c;
1949 int count = 0; /* cumulative number of braces */
1950 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001951 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 int inquote = FALSE; /* TRUE when inside quotes */
1953 char_u *linep; /* pointer to current line */
1954 char_u *ptr;
1955 int do_quotes; /* check for quotes in current line */
1956 int at_start; /* do_quotes value at start position */
1957 int hash_dir = 0; /* Direction searched for # things */
1958 int comment_dir = 0; /* Direction searched for comments */
1959 pos_T match_pos; /* Where last slash-star was found */
1960 int start_in_quotes; /* start position is in quotes */
1961 int traveled = 0; /* how far we've searched so far */
1962 int ignore_cend = FALSE; /* ignore comment end */
1963 int cpo_match; /* vi compatible matching */
1964 int cpo_bsl; /* don't recognize backslashes */
1965 int match_escaped = 0; /* search for escaped match */
1966 int dir; /* Direction to search */
1967 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001968#ifdef FEAT_LISP
1969 int lispcomm = FALSE; /* inside of Lisp-style comment */
1970 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001974 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 linep = ml_get(pos.lnum);
1976
1977 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1978 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1979
1980 /* Direction to search when initc is '/', '*' or '#' */
1981 if (flags & FM_BACKWARD)
1982 dir = BACKWARD;
1983 else if (flags & FM_FORWARD)
1984 dir = FORWARD;
1985 else
1986 dir = 0;
1987
1988 /*
1989 * if initc given, look in the table for the matching character
1990 * '/' and '*' are special cases: look for start or end of comment.
1991 * When '/' is used, we ignore running backwards into an star-slash, for
1992 * "[*" command, we just want to find any comment.
1993 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001994 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996 comment_dir = dir;
1997 if (initc == '/')
1998 ignore_cend = TRUE;
1999 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002000 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 initc = NUL;
2002 }
2003 else if (initc != '#' && initc != NUL)
2004 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002005 find_mps_values(&initc, &findc, &backwards, TRUE);
2006 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 return NULL;
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 else
2010 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002011 /*
2012 * Either initc is '#', or no initc was given and we need to look
2013 * under the cursor.
2014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 if (initc == '#')
2016 {
2017 hash_dir = dir;
2018 }
2019 else
2020 {
2021 /*
2022 * initc was not given, must look for something to match under
2023 * or near the cursor.
2024 * Only check for special things when 'cpo' doesn't have '%'.
2025 */
2026 if (!cpo_match)
2027 {
2028 /* Are we before or at #if, #else etc.? */
2029 ptr = skipwhite(linep);
2030 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2031 {
2032 ptr = skipwhite(ptr + 1);
2033 if ( STRNCMP(ptr, "if", 2) == 0
2034 || STRNCMP(ptr, "endif", 5) == 0
2035 || STRNCMP(ptr, "el", 2) == 0)
2036 hash_dir = 1;
2037 }
2038
2039 /* Are we on a comment? */
2040 else if (linep[pos.col] == '/')
2041 {
2042 if (linep[pos.col + 1] == '*')
2043 {
2044 comment_dir = FORWARD;
2045 backwards = FALSE;
2046 pos.col++;
2047 }
2048 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2049 {
2050 comment_dir = BACKWARD;
2051 backwards = TRUE;
2052 pos.col--;
2053 }
2054 }
2055 else if (linep[pos.col] == '*')
2056 {
2057 if (linep[pos.col + 1] == '/')
2058 {
2059 comment_dir = BACKWARD;
2060 backwards = TRUE;
2061 }
2062 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2063 {
2064 comment_dir = FORWARD;
2065 backwards = FALSE;
2066 }
2067 }
2068 }
2069
2070 /*
2071 * If we are not on a comment or the # at the start of a line, then
2072 * look for brace anywhere on this line after the cursor.
2073 */
2074 if (!hash_dir && !comment_dir)
2075 {
2076 /*
2077 * Find the brace under or after the cursor.
2078 * If beyond the end of the line, use the last character in
2079 * the line.
2080 */
2081 if (linep[pos.col] == NUL && pos.col)
2082 --pos.col;
2083 for (;;)
2084 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002085 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (initc == NUL)
2087 break;
2088
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002089 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (findc)
2091 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002092 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
2094 if (!findc)
2095 {
2096 /* no brace in the line, maybe use " #if" then */
2097 if (!cpo_match && *skipwhite(linep) == '#')
2098 hash_dir = 1;
2099 else
2100 return NULL;
2101 }
2102 else if (!cpo_bsl)
2103 {
2104 int col, bslcnt = 0;
2105
2106 /* Set "match_escaped" if there are an odd number of
2107 * backslashes. */
2108 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2109 bslcnt++;
2110 match_escaped = (bslcnt & 1);
2111 }
2112 }
2113 }
2114 if (hash_dir)
2115 {
2116 /*
2117 * Look for matching #if, #else, #elif, or #endif
2118 */
2119 if (oap != NULL)
2120 oap->motion_type = MLINE; /* Linewise for this case only */
2121 if (initc != '#')
2122 {
2123 ptr = skipwhite(skipwhite(linep) + 1);
2124 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2125 hash_dir = 1;
2126 else if (STRNCMP(ptr, "endif", 5) == 0)
2127 hash_dir = -1;
2128 else
2129 return NULL;
2130 }
2131 pos.col = 0;
2132 while (!got_int)
2133 {
2134 if (hash_dir > 0)
2135 {
2136 if (pos.lnum == curbuf->b_ml.ml_line_count)
2137 break;
2138 }
2139 else if (pos.lnum == 1)
2140 break;
2141 pos.lnum += hash_dir;
2142 linep = ml_get(pos.lnum);
2143 line_breakcheck(); /* check for CTRL-C typed */
2144 ptr = skipwhite(linep);
2145 if (*ptr != '#')
2146 continue;
2147 pos.col = (colnr_T) (ptr - linep);
2148 ptr = skipwhite(ptr + 1);
2149 if (hash_dir > 0)
2150 {
2151 if (STRNCMP(ptr, "if", 2) == 0)
2152 count++;
2153 else if (STRNCMP(ptr, "el", 2) == 0)
2154 {
2155 if (count == 0)
2156 return &pos;
2157 }
2158 else if (STRNCMP(ptr, "endif", 5) == 0)
2159 {
2160 if (count == 0)
2161 return &pos;
2162 count--;
2163 }
2164 }
2165 else
2166 {
2167 if (STRNCMP(ptr, "if", 2) == 0)
2168 {
2169 if (count == 0)
2170 return &pos;
2171 count--;
2172 }
2173 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2174 {
2175 if (count == 0)
2176 return &pos;
2177 }
2178 else if (STRNCMP(ptr, "endif", 5) == 0)
2179 count++;
2180 }
2181 }
2182 return NULL;
2183 }
2184 }
2185
2186#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002187 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 * paren/brace in the other direction. */
2189 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2190 backwards = !backwards;
2191#endif
2192
2193 do_quotes = -1;
2194 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002195 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002196
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002198 if ((backwards && comment_dir)
2199#ifdef FEAT_LISP
2200 || lisp
2201#endif
2202 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002204#ifdef FEAT_LISP
2205 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2206 lispcomm = TRUE; /* find match inside this comment */
2207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 while (!got_int)
2209 {
2210 /*
2211 * Go to the next position, forward or backward. We could use
2212 * inc() and dec() here, but that is much slower
2213 */
2214 if (backwards)
2215 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002216#ifdef FEAT_LISP
2217 /* char to match is inside of comment, don't search outside */
2218 if (lispcomm && pos.col < (colnr_T)comment_col)
2219 break;
2220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 if (pos.col == 0) /* at start of line, go to prev. one */
2222 {
2223 if (pos.lnum == 1) /* start of file */
2224 break;
2225 --pos.lnum;
2226
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002227 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 break;
2229
2230 linep = ml_get(pos.lnum);
2231 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2232 do_quotes = -1;
2233 line_breakcheck();
2234
2235 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002236 if (comment_dir
2237#ifdef FEAT_LISP
2238 || lisp
2239#endif
2240 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002242#ifdef FEAT_LISP
2243 /* skip comment */
2244 if (lisp && comment_col != MAXCOL)
2245 pos.col = comment_col;
2246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
2248 else
2249 {
2250 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 if (has_mbyte)
2252 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254 }
2255 else /* forward search */
2256 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002257 if (linep[pos.col] == NUL
2258 /* at end of line, go to next one */
2259#ifdef FEAT_LISP
2260 /* don't search for match in comment */
2261 || (lisp && comment_col != MAXCOL
2262 && pos.col == (colnr_T)comment_col)
2263#endif
2264 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002266 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2267#ifdef FEAT_LISP
2268 /* line is exhausted and comment with it,
2269 * don't search for match in code */
2270 || lispcomm
2271#endif
2272 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 break;
2274 ++pos.lnum;
2275
2276 if (maxtravel && traveled++ > maxtravel)
2277 break;
2278
2279 linep = ml_get(pos.lnum);
2280 pos.col = 0;
2281 do_quotes = -1;
2282 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002283#ifdef FEAT_LISP
2284 if (lisp) /* find comment pos in new line */
2285 comment_col = check_linecomment(linep);
2286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288 else
2289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002291 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 ++pos.col;
2294 }
2295 }
2296
2297 /*
2298 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2299 */
2300 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2301 (linep[0] == '{' || linep[0] == '}'))
2302 {
2303 if (linep[0] == findc && count == 0) /* match! */
2304 return &pos;
2305 break; /* out of scope */
2306 }
2307
2308 if (comment_dir)
2309 {
2310 /* Note: comments do not nest, and we ignore quotes in them */
2311 /* TODO: ignore comment brackets inside strings */
2312 if (comment_dir == FORWARD)
2313 {
2314 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2315 {
2316 pos.col++;
2317 return &pos;
2318 }
2319 }
2320 else /* Searching backwards */
2321 {
2322 /*
2323 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002324 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 */
2326 if (pos.col == 0)
2327 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002328 else if (raw_string)
2329 {
2330 if (linep[pos.col - 1] == 'R'
2331 && linep[pos.col] == '"'
2332 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2333 {
2334 /* Possible start of raw string. Now that we have the
2335 * delimiter we can check if it ends before where we
2336 * started searching, or before the previously found
2337 * raw string start. */
2338 if (!find_rawstring_end(linep, &pos,
2339 count > 0 ? &match_pos : &curwin->w_cursor))
2340 {
2341 count++;
2342 match_pos = pos;
2343 match_pos.col--;
2344 }
2345 linep = ml_get(pos.lnum); /* may have been released */
2346 }
2347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 else if ( linep[pos.col - 1] == '/'
2349 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002350 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 && (int)pos.col < comment_col)
2352 {
2353 count++;
2354 match_pos = pos;
2355 match_pos.col--;
2356 }
2357 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2358 {
2359 if (count > 0)
2360 pos = match_pos;
2361 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2362 && (int)pos.col <= comment_col)
2363 pos.col -= 2;
2364 else if (ignore_cend)
2365 continue;
2366 else
2367 return NULL;
2368 return &pos;
2369 }
2370 }
2371 continue;
2372 }
2373
2374 /*
2375 * If smart matching ('cpoptions' does not contain '%'), braces inside
2376 * of quotes are ignored, but only if there is an even number of
2377 * quotes in the line.
2378 */
2379 if (cpo_match)
2380 do_quotes = 0;
2381 else if (do_quotes == -1)
2382 {
2383 /*
2384 * Count the number of quotes in the line, skipping \" and '"'.
2385 * Watch out for "\\".
2386 */
2387 at_start = do_quotes;
2388 for (ptr = linep; *ptr; ++ptr)
2389 {
2390 if (ptr == linep + pos.col + backwards)
2391 at_start = (do_quotes & 1);
2392 if (*ptr == '"'
2393 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2394 ++do_quotes;
2395 if (*ptr == '\\' && ptr[1] != NUL)
2396 ++ptr;
2397 }
2398 do_quotes &= 1; /* result is 1 with even number of quotes */
2399
2400 /*
2401 * If we find an uneven count, check current line and previous
2402 * one for a '\' at the end.
2403 */
2404 if (!do_quotes)
2405 {
2406 inquote = FALSE;
2407 if (ptr[-1] == '\\')
2408 {
2409 do_quotes = 1;
2410 if (start_in_quotes == MAYBE)
2411 {
2412 /* Do we need to use at_start here? */
2413 inquote = TRUE;
2414 start_in_quotes = TRUE;
2415 }
2416 else if (backwards)
2417 inquote = TRUE;
2418 }
2419 if (pos.lnum > 1)
2420 {
2421 ptr = ml_get(pos.lnum - 1);
2422 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2423 {
2424 do_quotes = 1;
2425 if (start_in_quotes == MAYBE)
2426 {
2427 inquote = at_start;
2428 if (inquote)
2429 start_in_quotes = TRUE;
2430 }
2431 else if (!backwards)
2432 inquote = TRUE;
2433 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002434
2435 /* ml_get() only keeps one line, need to get linep again */
2436 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438 }
2439 }
2440 if (start_in_quotes == MAYBE)
2441 start_in_quotes = FALSE;
2442
2443 /*
2444 * If 'smartmatch' is set:
2445 * Things inside quotes are ignored by setting 'inquote'. If we
2446 * find a quote without a preceding '\' invert 'inquote'. At the
2447 * end of a line not ending in '\' we reset 'inquote'.
2448 *
2449 * In lines with an uneven number of quotes (without preceding '\')
2450 * we do not know which part to ignore. Therefore we only set
2451 * inquote if the number of quotes in a line is even, unless this
2452 * line or the previous one ends in a '\'. Complicated, isn't it?
2453 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002454 c = PTR2CHAR(linep + pos.col);
2455 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
2457 case NUL:
2458 /* at end of line without trailing backslash, reset inquote */
2459 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2460 {
2461 inquote = FALSE;
2462 start_in_quotes = FALSE;
2463 }
2464 break;
2465
2466 case '"':
2467 /* a quote that is preceded with an odd number of backslashes is
2468 * ignored */
2469 if (do_quotes)
2470 {
2471 int col;
2472
2473 for (col = pos.col - 1; col >= 0; --col)
2474 if (linep[col] != '\\')
2475 break;
2476 if ((((int)pos.col - 1 - col) & 1) == 0)
2477 {
2478 inquote = !inquote;
2479 start_in_quotes = FALSE;
2480 }
2481 }
2482 break;
2483
2484 /*
2485 * If smart matching ('cpoptions' does not contain '%'):
2486 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2487 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2488 * skipped, there is never a brace in them.
2489 * Ignore this when finding matches for `'.
2490 */
2491 case '\'':
2492 if (!cpo_match && initc != '\'' && findc != '\'')
2493 {
2494 if (backwards)
2495 {
2496 if (pos.col > 1)
2497 {
2498 if (linep[pos.col - 2] == '\'')
2499 {
2500 pos.col -= 2;
2501 break;
2502 }
2503 else if (linep[pos.col - 2] == '\\' &&
2504 pos.col > 2 && linep[pos.col - 3] == '\'')
2505 {
2506 pos.col -= 3;
2507 break;
2508 }
2509 }
2510 }
2511 else if (linep[pos.col + 1]) /* forward search */
2512 {
2513 if (linep[pos.col + 1] == '\\' &&
2514 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2515 {
2516 pos.col += 3;
2517 break;
2518 }
2519 else if (linep[pos.col + 2] == '\'')
2520 {
2521 pos.col += 2;
2522 break;
2523 }
2524 }
2525 }
2526 /* FALLTHROUGH */
2527
2528 default:
2529#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002530 /*
2531 * For Lisp skip over backslashed (), {} and [].
2532 * (actually, we skip #\( et al)
2533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (curbuf->b_p_lisp
2535 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002536 && pos.col > 1
2537 && check_prevcol(linep, pos.col, '\\', NULL)
2538 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 break;
2540#endif
2541
2542 /* Check for match outside of quotes, and inside of
2543 * quotes when the start is also inside of quotes. */
2544 if ((!inquote || start_in_quotes == TRUE)
2545 && (c == initc || c == findc))
2546 {
2547 int col, bslcnt = 0;
2548
2549 if (!cpo_bsl)
2550 {
2551 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2552 bslcnt++;
2553 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002554 /* Only accept a match when 'M' is in 'cpo' or when escaping
2555 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2557 {
2558 if (c == initc)
2559 count++;
2560 else
2561 {
2562 if (count == 0)
2563 return &pos;
2564 count--;
2565 }
2566 }
2567 }
2568 }
2569 }
2570
2571 if (comment_dir == BACKWARD && count > 0)
2572 {
2573 pos = match_pos;
2574 return &pos;
2575 }
2576 return (pos_T *)NULL; /* never found it */
2577}
2578
2579/*
2580 * Check if line[] contains a / / comment.
2581 * Return MAXCOL if not, otherwise return the column.
2582 * TODO: skip strings.
2583 */
2584 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002585check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 char_u *p;
2588
2589 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002590#ifdef FEAT_LISP
2591 /* skip Lispish one-line comments */
2592 if (curbuf->b_p_lisp)
2593 {
2594 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2595 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002596 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002597
2598 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002599 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002600 {
2601 if (*p == '"')
2602 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002603 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002604 {
2605 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002606 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002607 }
2608 else if (p == line || ((p - line) >= 2
2609 /* skip #\" form */
2610 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002611 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002612 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002613 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002614 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2615 break; /* found! */
2616 ++p;
2617 }
2618 }
2619 else
2620 p = NULL;
2621 }
2622 else
2623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 while ((p = vim_strchr(p, '/')) != NULL)
2625 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002626 /* accept a double /, unless it's preceded with * and followed by *,
2627 * because * / / * is an end and start of a C comment */
2628 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 break;
2630 ++p;
2631 }
2632
2633 if (p == NULL)
2634 return MAXCOL;
2635 return (int)(p - line);
2636}
2637
2638/*
2639 * Move cursor briefly to character matching the one under the cursor.
2640 * Used for Insert mode and "r" command.
2641 * Show the match only if it is visible on the screen.
2642 * If there isn't a match, then beep.
2643 */
2644 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002645showmatch(
2646 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647{
2648 pos_T *lpos, save_cursor;
2649 pos_T mpos;
2650 colnr_T vcol;
2651 long save_so;
2652 long save_siso;
2653#ifdef CURSOR_SHAPE
2654 int save_state;
2655#endif
2656 colnr_T save_dollar_vcol;
2657 char_u *p;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002658 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2659 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 /*
2662 * Only show match for chars in the 'matchpairs' option.
2663 */
2664 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002665 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
2667#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002668 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002669 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002670#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002671 p += MB_PTR2LEN(p) + 1;
2672 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673#ifdef FEAT_RIGHTLEFT
2674 && !(curwin->w_p_rl ^ p_ri)
2675#endif
2676 )
2677 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002678 p += MB_PTR2LEN(p);
2679 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 return;
2681 }
2682
2683 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002684 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002685 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 if (!curwin->w_p_wrap)
2688 getvcol(curwin, lpos, NULL, &vcol, NULL);
2689 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002690 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
2692 mpos = *lpos; /* save the pos, update_screen() may change it */
2693 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002694 save_so = *so;
2695 save_siso = *siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2697 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002698 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2699 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 ++curwin->w_virtcol; /* do display ')' just before "$" */
2701 update_screen(VALID); /* show the new char first */
2702
2703 save_dollar_vcol = dollar_vcol;
2704#ifdef CURSOR_SHAPE
2705 save_state = State;
2706 State = SHOWMATCH;
2707 ui_cursor_shape(); /* may show different cursor shape */
2708#endif
2709 curwin->w_cursor = mpos; /* move to matching char */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002710 *so = 0; /* don't use 'scrolloff' here */
2711 *siso = 0; /* don't use 'sidescrolloff' here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 showruler(FALSE);
2713 setcursor();
2714 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002715 out_flush_cursor(TRUE, FALSE);
2716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2718 * which resets it if the matching position is in a previous line
2719 * and has a higher column number. */
2720 dollar_vcol = save_dollar_vcol;
2721
2722 /*
2723 * brief pause, unless 'm' is present in 'cpo' and a character is
2724 * available.
2725 */
2726 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2727 ui_delay(p_mat * 100L, TRUE);
2728 else if (!char_avail())
2729 ui_delay(p_mat * 100L, FALSE);
2730 curwin->w_cursor = save_cursor; /* restore cursor position */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002731 *so = save_so;
2732 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733#ifdef CURSOR_SHAPE
2734 State = save_state;
2735 ui_cursor_shape(); /* may show different cursor shape */
2736#endif
2737 }
2738 }
2739}
2740
2741/*
Bram Moolenaar85160712018-06-19 18:27:41 +02002742 * Find the start of the next sentence, searching in the direction specified
2743 * by the "dir" argument. The cursor is positioned on the start of the next
2744 * sentence when found. If the next sentence is found, return OK. Return FAIL
2745 * otherwise. See ":h sentence" for the precise definition of a "sentence"
2746 * text object.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 */
2748 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002749findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 pos_T pos, tpos;
2752 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002753 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 int startlnum;
2755 int noskip = FALSE; /* do not skip blanks */
2756 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002757 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
2759 pos = curwin->w_cursor;
2760 if (dir == FORWARD)
2761 func = incl;
2762 else
2763 func = decl;
2764
2765 while (count--)
2766 {
2767 /*
2768 * if on an empty line, skip upto a non-empty line
2769 */
2770 if (gchar_pos(&pos) == NUL)
2771 {
2772 do
2773 if ((*func)(&pos) == -1)
2774 break;
2775 while (gchar_pos(&pos) == NUL);
2776 if (dir == FORWARD)
2777 goto found;
2778 }
2779 /*
2780 * if on the start of a paragraph or a section and searching forward,
2781 * go to the next line
2782 */
2783 else if (dir == FORWARD && pos.col == 0 &&
2784 startPS(pos.lnum, NUL, FALSE))
2785 {
2786 if (pos.lnum == curbuf->b_ml.ml_line_count)
2787 return FAIL;
2788 ++pos.lnum;
2789 goto found;
2790 }
2791 else if (dir == BACKWARD)
2792 decl(&pos);
2793
Bram Moolenaar85160712018-06-19 18:27:41 +02002794 // go back to the previous non-white non-punctuation character
Bram Moolenaardef9e822004-12-31 20:58:58 +00002795 found_dot = FALSE;
Bram Moolenaar85160712018-06-19 18:27:41 +02002796 while (c = gchar_pos(&pos), VIM_ISWHITE(c)
2797 || vim_strchr((char_u *)".!?)]\"'", c) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaar85160712018-06-19 18:27:41 +02002799 tpos = pos;
2800 if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 break;
Bram Moolenaar85160712018-06-19 18:27:41 +02002802
2803 if (found_dot)
2804 break;
2805 if (vim_strchr((char_u *) ".!?", c) != NULL)
2806 found_dot = TRUE;
2807
2808 if (vim_strchr((char_u *) ")]\"'", c) != NULL
2809 && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
2810 break;
2811
2812 decl(&pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 }
2814
2815 /* remember the line where the search started */
2816 startlnum = pos.lnum;
2817 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2818
2819 for (;;) /* find end of sentence */
2820 {
2821 c = gchar_pos(&pos);
2822 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2823 {
2824 if (dir == BACKWARD && pos.lnum != startlnum)
2825 ++pos.lnum;
2826 break;
2827 }
2828 if (c == '.' || c == '!' || c == '?')
2829 {
2830 tpos = pos;
2831 do
2832 if ((c = inc(&tpos)) == -1)
2833 break;
2834 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2835 != NULL);
2836 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2837 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2838 && gchar_pos(&tpos) == ' ')))
2839 {
2840 pos = tpos;
2841 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2842 inc(&pos);
2843 break;
2844 }
2845 }
2846 if ((*func)(&pos) == -1)
2847 {
2848 if (count)
2849 return FAIL;
2850 noskip = TRUE;
2851 break;
2852 }
2853 }
2854found:
2855 /* skip white space */
2856 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2857 if (incl(&pos) == -1)
2858 break;
2859 }
2860
2861 setpcmark();
2862 curwin->w_cursor = pos;
2863 return OK;
2864}
2865
2866/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002867 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002869 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 * If 'what' is '{' or '}' we go to the next section.
2871 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002872 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 */
2874 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002875findpar(
2876 int *pincl, /* Return: TRUE if last char is to be included */
2877 int dir,
2878 long count,
2879 int what,
2880 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
2882 linenr_T curr;
2883 int did_skip; /* TRUE after separating lines have been skipped */
2884 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002885 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886#ifdef FEAT_FOLDING
2887 linenr_T fold_first; /* first line of a closed fold */
2888 linenr_T fold_last; /* last line of a closed fold */
2889 int fold_skipped; /* TRUE if a closed fold was skipped this
2890 iteration */
2891#endif
2892
2893 curr = curwin->w_cursor.lnum;
2894
2895 while (count--)
2896 {
2897 did_skip = FALSE;
2898 for (first = TRUE; ; first = FALSE)
2899 {
2900 if (*ml_get(curr) != NUL)
2901 did_skip = TRUE;
2902
2903#ifdef FEAT_FOLDING
2904 /* skip folded lines */
2905 fold_skipped = FALSE;
2906 if (first && hasFolding(curr, &fold_first, &fold_last))
2907 {
2908 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2909 fold_skipped = TRUE;
2910 }
2911#endif
2912
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002913 /* POSIX has its own ideas of what a paragraph boundary is and it
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002914 * doesn't match historical Vi: It also stops at a "{" in the
2915 * first column and at an empty line. */
2916 if (!first && did_skip && (startPS(curr, what, both)
2917 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 break;
2919
2920#ifdef FEAT_FOLDING
2921 if (fold_skipped)
2922 curr -= dir;
2923#endif
2924 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2925 {
2926 if (count)
2927 return FALSE;
2928 curr -= dir;
2929 break;
2930 }
2931 }
2932 }
2933 setpcmark();
2934 if (both && *ml_get(curr) == '}') /* include line with '}' */
2935 ++curr;
2936 curwin->w_cursor.lnum = curr;
2937 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2938 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002939 char_u *line = ml_get(curr);
2940
2941 /* Put the cursor on the last character in the last line and make the
2942 * motion inclusive. */
2943 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 {
2945 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002946 curwin->w_cursor.col -=
2947 (*mb_head_off)(line, line + curwin->w_cursor.col);
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002948 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 }
2951 else
2952 curwin->w_cursor.col = 0;
2953 return TRUE;
2954}
2955
2956/*
2957 * check if the string 's' is a nroff macro that is in option 'opt'
2958 */
2959 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002960inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 char_u *macro;
2963
2964 for (macro = opt; macro[0]; ++macro)
2965 {
2966 /* Accept two characters in the option being equal to two characters
2967 * in the line. A space in the option matches with a space in the
2968 * line or the line having ended. */
2969 if ( (macro[0] == s[0]
2970 || (macro[0] == ' '
2971 && (s[0] == NUL || s[0] == ' ')))
2972 && (macro[1] == s[1]
2973 || ((macro[1] == NUL || macro[1] == ' ')
2974 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2975 break;
2976 ++macro;
2977 if (macro[0] == NUL)
2978 break;
2979 }
2980 return (macro[0] != NUL);
2981}
2982
2983/*
2984 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2985 * If 'para' is '{' or '}' only check for sections.
2986 * If 'both' is TRUE also stop at '}'
2987 */
2988 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 char_u *s;
2992
2993 s = ml_get(lnum);
2994 if (*s == para || *s == '\f' || (both && *s == '}'))
2995 return TRUE;
2996 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2997 (!para && inmacro(p_para, s + 1))))
2998 return TRUE;
2999 return FALSE;
3000}
3001
3002/*
3003 * The following routines do the word searches performed by the 'w', 'W',
3004 * 'b', 'B', 'e', and 'E' commands.
3005 */
3006
3007/*
3008 * To perform these searches, characters are placed into one of three
3009 * classes, and transitions between classes determine word boundaries.
3010 *
3011 * The classes are:
3012 *
3013 * 0 - white space
3014 * 1 - punctuation
3015 * 2 or higher - keyword characters (letters, digits and underscore)
3016 */
3017
3018static int cls_bigword; /* TRUE for "W", "B" or "E" */
3019
3020/*
3021 * cls() - returns the class of character at curwin->w_cursor
3022 *
3023 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
3024 * from class 2 and higher are reported as class 1 since only white space
3025 * boundaries are of interest.
3026 */
3027 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003028cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 int c;
3031
3032 c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (c == ' ' || c == '\t' || c == NUL)
3034 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 if (enc_dbcs != 0 && c > 0xFF)
3036 {
3037 /* If cls_bigword, report multi-byte chars as class 1. */
3038 if (enc_dbcs == DBCS_KOR && cls_bigword)
3039 return 1;
3040
3041 /* process code leading/trailing bytes */
3042 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3043 }
3044 if (enc_utf8)
3045 {
3046 c = utf_class(c);
3047 if (c != 0 && cls_bigword)
3048 return 1;
3049 return c;
3050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051
3052 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3053 if (cls_bigword)
3054 return 1;
3055
3056 if (vim_iswordc(c))
3057 return 2;
3058 return 1;
3059}
3060
3061
3062/*
3063 * fwd_word(count, type, eol) - move forward one word
3064 *
3065 * Returns FAIL if the cursor was already at the end of the file.
3066 * If eol is TRUE, last word stops at end of line (for operators).
3067 */
3068 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003069fwd_word(
3070 long count,
3071 int bigword, /* "W", "E" or "B" */
3072 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 int sclass; /* starting class */
3075 int i;
3076 int last_line;
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 cls_bigword = bigword;
3080 while (--count >= 0)
3081 {
3082#ifdef FEAT_FOLDING
3083 /* When inside a range of folded lines, move to the last char of the
3084 * last line. */
3085 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3086 coladvance((colnr_T)MAXCOL);
3087#endif
3088 sclass = cls();
3089
3090 /*
3091 * We always move at least one character, unless on the last
3092 * character in the buffer.
3093 */
3094 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3095 i = inc_cursor();
3096 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3097 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003098 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 return OK;
3100
3101 /*
3102 * Go one char past end of current word (if any)
3103 */
3104 if (sclass != 0)
3105 while (cls() == sclass)
3106 {
3107 i = inc_cursor();
3108 if (i == -1 || (i >= 1 && eol && count == 0))
3109 return OK;
3110 }
3111
3112 /*
3113 * go to next non-white
3114 */
3115 while (cls() == 0)
3116 {
3117 /*
3118 * We'll stop if we land on a blank line
3119 */
3120 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3121 break;
3122
3123 i = inc_cursor();
3124 if (i == -1 || (i >= 1 && eol && count == 0))
3125 return OK;
3126 }
3127 }
3128 return OK;
3129}
3130
3131/*
3132 * bck_word() - move backward 'count' words
3133 *
3134 * If stop is TRUE and we are already on the start of a word, move one less.
3135 *
3136 * Returns FAIL if top of the file was reached.
3137 */
3138 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003139bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
3141 int sclass; /* starting class */
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 cls_bigword = bigword;
3145 while (--count >= 0)
3146 {
3147#ifdef FEAT_FOLDING
3148 /* When inside a range of folded lines, move to the first char of the
3149 * first line. */
3150 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3151 curwin->w_cursor.col = 0;
3152#endif
3153 sclass = cls();
3154 if (dec_cursor() == -1) /* started at start of file */
3155 return FAIL;
3156
3157 if (!stop || sclass == cls() || sclass == 0)
3158 {
3159 /*
3160 * Skip white space before the word.
3161 * Stop on an empty line.
3162 */
3163 while (cls() == 0)
3164 {
3165 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003166 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 goto finished;
3168 if (dec_cursor() == -1) /* hit start of file, stop here */
3169 return OK;
3170 }
3171
3172 /*
3173 * Move backward to start of this word.
3174 */
3175 if (skip_chars(cls(), BACKWARD))
3176 return OK;
3177 }
3178
3179 inc_cursor(); /* overshot - forward one */
3180finished:
3181 stop = FALSE;
3182 }
3183 return OK;
3184}
3185
3186/*
3187 * end_word() - move to the end of the word
3188 *
3189 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3190 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3191 * motion crosses blank lines. When the real vi crosses a blank line in an
3192 * 'e' motion, the cursor is placed on the FIRST character of the next
3193 * non-blank line. The 'E' command, however, works correctly. Since this
3194 * appears to be a bug, I have not duplicated it here.
3195 *
3196 * Returns FAIL if end of the file was reached.
3197 *
3198 * If stop is TRUE and we are already on the end of a word, move one less.
3199 * If empty is TRUE stop on an empty line.
3200 */
3201 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003202end_word(
3203 long count,
3204 int bigword,
3205 int stop,
3206 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207{
3208 int sclass; /* starting class */
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 cls_bigword = bigword;
3212 while (--count >= 0)
3213 {
3214#ifdef FEAT_FOLDING
3215 /* When inside a range of folded lines, move to the last char of the
3216 * last line. */
3217 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3218 coladvance((colnr_T)MAXCOL);
3219#endif
3220 sclass = cls();
3221 if (inc_cursor() == -1)
3222 return FAIL;
3223
3224 /*
3225 * If we're in the middle of a word, we just have to move to the end
3226 * of it.
3227 */
3228 if (cls() == sclass && sclass != 0)
3229 {
3230 /*
3231 * Move forward to end of the current word
3232 */
3233 if (skip_chars(sclass, FORWARD))
3234 return FAIL;
3235 }
3236 else if (!stop || sclass == 0)
3237 {
3238 /*
3239 * We were at the end of a word. Go to the end of the next word.
3240 * First skip white space, if 'empty' is TRUE, stop at empty line.
3241 */
3242 while (cls() == 0)
3243 {
3244 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003245 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 goto finished;
3247 if (inc_cursor() == -1) /* hit end of file, stop here */
3248 return FAIL;
3249 }
3250
3251 /*
3252 * Move forward to the end of this word.
3253 */
3254 if (skip_chars(cls(), FORWARD))
3255 return FAIL;
3256 }
3257 dec_cursor(); /* overshot - one char backward */
3258finished:
3259 stop = FALSE; /* we move only one word less */
3260 }
3261 return OK;
3262}
3263
3264/*
3265 * Move back to the end of the word.
3266 *
3267 * Returns FAIL if start of the file was reached.
3268 */
3269 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003270bckend_word(
3271 long count,
3272 int bigword, /* TRUE for "B" */
3273 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274{
3275 int sclass; /* starting class */
3276 int i;
3277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 cls_bigword = bigword;
3280 while (--count >= 0)
3281 {
3282 sclass = cls();
3283 if ((i = dec_cursor()) == -1)
3284 return FAIL;
3285 if (eol && i == 1)
3286 return OK;
3287
3288 /*
3289 * Move backward to before the start of this word.
3290 */
3291 if (sclass != 0)
3292 {
3293 while (cls() == sclass)
3294 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3295 return OK;
3296 }
3297
3298 /*
3299 * Move backward to end of the previous word
3300 */
3301 while (cls() == 0)
3302 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003303 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 break;
3305 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3306 return OK;
3307 }
3308 }
3309 return OK;
3310}
3311
3312/*
3313 * Skip a row of characters of the same class.
3314 * Return TRUE when end-of-file reached, FALSE otherwise.
3315 */
3316 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003317skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319 while (cls() == cclass)
3320 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3321 return TRUE;
3322 return FALSE;
3323}
3324
3325#ifdef FEAT_TEXTOBJ
3326/*
3327 * Go back to the start of the word or the start of white space
3328 */
3329 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003330back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
3332 int sclass; /* starting class */
3333
3334 sclass = cls();
3335 for (;;)
3336 {
3337 if (curwin->w_cursor.col == 0) /* stop at start of line */
3338 break;
3339 dec_cursor();
3340 if (cls() != sclass) /* stop at start of word */
3341 {
3342 inc_cursor();
3343 break;
3344 }
3345 }
3346}
3347
3348 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003349find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 int c;
3352
3353 while (decl(posp) != -1)
3354 {
3355 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003356 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
3358 incl(posp);
3359 break;
3360 }
3361 }
3362}
3363
3364/*
3365 * Skip count/2 sentences and count/2 separating white spaces.
3366 */
3367 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003368findsent_forward(
3369 long count,
3370 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 while (count--)
3373 {
3374 findsent(FORWARD, 1L);
3375 if (at_start_sent)
3376 find_first_blank(&curwin->w_cursor);
3377 if (count == 0 || at_start_sent)
3378 decl(&curwin->w_cursor);
3379 at_start_sent = !at_start_sent;
3380 }
3381}
3382
3383/*
3384 * Find word under cursor, cursor at end.
3385 * Used while an operator is pending, and in Visual mode.
3386 */
3387 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003388current_word(
3389 oparg_T *oap,
3390 long count,
3391 int include, /* TRUE: include word and white space */
3392 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 pos_T start_pos;
3395 pos_T pos;
3396 int inclusive = TRUE;
3397 int include_white = FALSE;
3398
3399 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003400 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003403 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 dec_cursor();
3405
3406 /*
3407 * When Visual mode is not active, or when the VIsual area is only one
3408 * character, select the word and/or white space under the cursor.
3409 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003410 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
3412 /*
3413 * Go to start of current word or white space.
3414 */
3415 back_in_line();
3416 start_pos = curwin->w_cursor;
3417
3418 /*
3419 * If the start is on white space, and white space should be included
3420 * (" word"), or start is not on white space, and white space should
3421 * not be included ("word"), find end of word.
3422 */
3423 if ((cls() == 0) == include)
3424 {
3425 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3426 return FAIL;
3427 }
3428 else
3429 {
3430 /*
3431 * If the start is not on white space, and white space should be
3432 * included ("word "), or start is on white space and white
3433 * space should not be included (" "), find start of word.
3434 * If we end up in the first column of the next line (single char
3435 * word) back up to end of the line.
3436 */
3437 fwd_word(1L, bigword, TRUE);
3438 if (curwin->w_cursor.col == 0)
3439 decl(&curwin->w_cursor);
3440 else
3441 oneleft();
3442
3443 if (include)
3444 include_white = TRUE;
3445 }
3446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (VIsual_active)
3448 {
3449 /* should do something when inclusive == FALSE ! */
3450 VIsual = start_pos;
3451 redraw_curbuf_later(INVERTED); /* update the inversion */
3452 }
3453 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
3455 oap->start = start_pos;
3456 oap->motion_type = MCHAR;
3457 }
3458 --count;
3459 }
3460
3461 /*
3462 * When count is still > 0, extend with more objects.
3463 */
3464 while (count > 0)
3465 {
3466 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003467 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 /*
3470 * In Visual mode, with cursor at start: move cursor back.
3471 */
3472 if (decl(&curwin->w_cursor) == -1)
3473 return FAIL;
3474 if (include != (cls() != 0))
3475 {
3476 if (bck_word(1L, bigword, TRUE) == FAIL)
3477 return FAIL;
3478 }
3479 else
3480 {
3481 if (bckend_word(1L, bigword, TRUE) == FAIL)
3482 return FAIL;
3483 (void)incl(&curwin->w_cursor);
3484 }
3485 }
3486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
3488 /*
3489 * Move cursor forward one word and/or white area.
3490 */
3491 if (incl(&curwin->w_cursor) == -1)
3492 return FAIL;
3493 if (include != (cls() == 0))
3494 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003495 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 return FAIL;
3497 /*
3498 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003499 * the first character on the line.
3500 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003502 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 inclusive = FALSE;
3504 }
3505 else
3506 {
3507 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3508 return FAIL;
3509 }
3510 }
3511 --count;
3512 }
3513
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003514 if (include_white && (cls() != 0
3515 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 /*
3518 * If we don't include white space at the end, move the start
3519 * to include some white space there. This makes "daw" work
3520 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003521 * word). Also when "2daw" deletes "word." at the end of the line
3522 * (cursor is at start of next line).
3523 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 */
3525 pos = curwin->w_cursor; /* save cursor position */
3526 curwin->w_cursor = start_pos;
3527 if (oneleft() == OK)
3528 {
3529 back_in_line();
3530 if (cls() == 0 && curwin->w_cursor.col > 0)
3531 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (VIsual_active)
3533 VIsual = curwin->w_cursor;
3534 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 oap->start = curwin->w_cursor;
3536 }
3537 }
3538 curwin->w_cursor = pos; /* put cursor back at end */
3539 }
3540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (VIsual_active)
3542 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003543 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 inc_cursor();
3545 if (VIsual_mode == 'V')
3546 {
3547 VIsual_mode = 'v';
3548 redraw_cmdline = TRUE; /* show mode later */
3549 }
3550 }
3551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 oap->inclusive = inclusive;
3553
3554 return OK;
3555}
3556
3557/*
3558 * Find sentence(s) under the cursor, cursor at end.
3559 * When Visual active, extend it by one or more sentences.
3560 */
3561 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003562current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563{
3564 pos_T start_pos;
3565 pos_T pos;
3566 int start_blank;
3567 int c;
3568 int at_start_sent;
3569 long ncount;
3570
3571 start_pos = curwin->w_cursor;
3572 pos = start_pos;
3573 findsent(FORWARD, 1L); /* Find start of next sentence. */
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003576 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003578 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003581 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 /*
3584 * Cursor at start of Visual area.
3585 * Find out where we are:
3586 * - in the white space before a sentence
3587 * - in a sentence or just after it
3588 * - at the start of a sentence
3589 */
3590 at_start_sent = TRUE;
3591 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003592 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003595 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 {
3597 at_start_sent = FALSE;
3598 break;
3599 }
3600 incl(&pos);
3601 }
3602 if (!at_start_sent)
3603 {
3604 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003605 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 at_start_sent = TRUE; /* exactly at start of sentence */
3607 else
3608 /* inside a sentence, go to its end (start of next) */
3609 findsent(FORWARD, 1L);
3610 }
3611 if (include) /* "as" gets twice as much as "is" */
3612 count *= 2;
3613 while (count--)
3614 {
3615 if (at_start_sent)
3616 find_first_blank(&curwin->w_cursor);
3617 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003618 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 findsent(BACKWARD, 1L);
3620 at_start_sent = !at_start_sent;
3621 }
3622 }
3623 else
3624 {
3625 /*
3626 * Cursor at end of Visual area.
3627 * Find out where we are:
3628 * - just before a sentence
3629 * - just before or in the white space before a sentence
3630 * - in a sentence
3631 */
3632 incl(&pos);
3633 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003634 /* not just before a sentence */
3635 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003638 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003641 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643 at_start_sent = TRUE;
3644 break;
3645 }
3646 incl(&pos);
3647 }
3648 if (at_start_sent) /* in the sentence */
3649 findsent(BACKWARD, 1L);
3650 else /* in/before white before a sentence */
3651 curwin->w_cursor = start_pos;
3652 }
3653
3654 if (include) /* "as" gets twice as much as "is" */
3655 count *= 2;
3656 findsent_forward(count, at_start_sent);
3657 if (*p_sel == 'e')
3658 ++curwin->w_cursor.col;
3659 }
3660 return OK;
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
3663 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003664 * If the cursor started on a blank, check if it is just before the start
3665 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003667 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003669 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 {
3671 start_blank = TRUE;
3672 find_first_blank(&start_pos); /* go back to first blank */
3673 }
3674 else
3675 {
3676 start_blank = FALSE;
3677 findsent(BACKWARD, 1L);
3678 start_pos = curwin->w_cursor;
3679 }
3680 if (include)
3681 ncount = count * 2;
3682 else
3683 {
3684 ncount = count;
3685 if (start_blank)
3686 --ncount;
3687 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003688 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 findsent_forward(ncount, TRUE);
3690 else
3691 decl(&curwin->w_cursor);
3692
3693 if (include)
3694 {
3695 /*
3696 * If the blank in front of the sentence is included, exclude the
3697 * blanks at the end of the sentence, go back to the first blank.
3698 * If there are no trailing blanks, try to include leading blanks.
3699 */
3700 if (start_blank)
3701 {
3702 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003703 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3704 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 decl(&curwin->w_cursor);
3706 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003707 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 find_first_blank(&start_pos);
3709 }
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (VIsual_active)
3712 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003713 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003714 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 goto extend;
3716 if (*p_sel == 'e')
3717 ++curwin->w_cursor.col;
3718 VIsual = start_pos;
3719 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003720 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 redraw_curbuf_later(INVERTED); /* update the inversion */
3722 }
3723 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 /* include a newline after the sentence, if there is one */
3726 if (incl(&curwin->w_cursor) == -1)
3727 oap->inclusive = TRUE;
3728 else
3729 oap->inclusive = FALSE;
3730 oap->start = start_pos;
3731 oap->motion_type = MCHAR;
3732 }
3733 return OK;
3734}
3735
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003736/*
3737 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003738 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003741current_block(
3742 oparg_T *oap,
3743 long count,
3744 int include, /* TRUE == include white space */
3745 int what, /* '(', '{', etc. */
3746 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
3748 pos_T old_pos;
3749 pos_T *pos = NULL;
3750 pos_T start_pos;
3751 pos_T *end_pos;
3752 pos_T old_start, old_end;
3753 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003754 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
3756 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003757 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 old_start = old_end;
3759
3760 /*
3761 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3762 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003763 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
3765 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003766 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 while (inindent(1))
3768 if (inc_cursor() != 0)
3769 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003770 if (gchar_cursor() == what)
3771 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 ++curwin->w_cursor.col;
3773 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003774 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
3776 old_start = VIsual;
3777 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3778 }
3779 else
3780 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 /*
3783 * Search backwards for unclosed '(', '{', etc..
3784 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003785 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3786 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 */
3788 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003789 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 while (count-- > 0)
3791 {
3792 if ((pos = findmatch(NULL, what)) == NULL)
3793 break;
3794 curwin->w_cursor = *pos;
3795 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3796 }
3797 p_cpo = save_cpo;
3798
3799 /*
3800 * Search for matching ')', '}', etc.
3801 * Put this position in curwin->w_cursor.
3802 */
3803 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3804 {
3805 curwin->w_cursor = old_pos;
3806 return FAIL;
3807 }
3808 curwin->w_cursor = *end_pos;
3809
3810 /*
3811 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003812 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3813 * indent. But only if the resulting area is not smaller than what we
3814 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 */
3816 while (!include)
3817 {
3818 incl(&start_pos);
3819 sol = (curwin->w_cursor.col == 0);
3820 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003821 while (inindent(1))
3822 {
3823 sol = TRUE;
3824 if (decl(&curwin->w_cursor) != 0)
3825 break;
3826 }
3827
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 /*
3829 * In Visual mode, when the resulting area is not bigger than what we
3830 * started with, extend it to the next block, and then exclude again.
3831 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003832 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 && VIsual_active)
3834 {
3835 curwin->w_cursor = old_start;
3836 decl(&curwin->w_cursor);
3837 if ((pos = findmatch(NULL, what)) == NULL)
3838 {
3839 curwin->w_cursor = old_pos;
3840 return FAIL;
3841 }
3842 start_pos = *pos;
3843 curwin->w_cursor = *pos;
3844 if ((end_pos = findmatch(NULL, other)) == NULL)
3845 {
3846 curwin->w_cursor = old_pos;
3847 return FAIL;
3848 }
3849 curwin->w_cursor = *end_pos;
3850 }
3851 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 break;
3853 }
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (VIsual_active)
3856 {
3857 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003858 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003859 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 inc(&curwin->w_cursor); /* include the line break */
3861 VIsual = start_pos;
3862 VIsual_mode = 'v';
3863 redraw_curbuf_later(INVERTED); /* update the inversion */
3864 showmode();
3865 }
3866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
3868 oap->start = start_pos;
3869 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003870 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003873 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003874 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003876 else
3877 /* End is before the start (no text in between <>, [], etc.): don't
3878 * operate on any text. */
3879 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
3881
3882 return OK;
3883}
3884
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003885/*
3886 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3887 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3888 */
3889 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003890in_html_tag(
3891 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003892{
3893 char_u *line = ml_get_curline();
3894 char_u *p;
3895 int c;
3896 int lc = NUL;
3897 pos_T pos;
3898
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003899 if (enc_dbcs)
3900 {
3901 char_u *lp = NULL;
3902
3903 /* We search forward until the cursor, because searching backwards is
3904 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003905 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003906 if (*p == '>' || *p == '<')
3907 {
3908 lc = *p;
3909 lp = p;
3910 }
3911 if (*p != '<') /* check for '<' under cursor */
3912 {
3913 if (lc != '<')
3914 return FALSE;
3915 p = lp;
3916 }
3917 }
3918 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003919 {
3920 for (p = line + curwin->w_cursor.col; p > line; )
3921 {
3922 if (*p == '<') /* find '<' under/before cursor */
3923 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003924 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003925 if (*p == '>') /* find '>' before cursor */
3926 break;
3927 }
3928 if (*p != '<')
3929 return FALSE;
3930 }
3931
3932 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003933 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003934
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003935 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003936 if (end_tag)
3937 /* check that there is a '/' after the '<' */
3938 return *p == '/';
3939
3940 /* check that there is no '/' after the '<' */
3941 if (*p == '/')
3942 return FALSE;
3943
3944 /* check that the matching '>' is not preceded by '/' */
3945 for (;;)
3946 {
3947 if (inc(&pos) < 0)
3948 return FALSE;
3949 c = *ml_get_pos(&pos);
3950 if (c == '>')
3951 break;
3952 lc = c;
3953 }
3954 return lc != '/';
3955}
3956
3957/*
3958 * Find tag block under the cursor, cursor at end.
3959 */
3960 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003961current_tagblock(
3962 oparg_T *oap,
3963 long count_arg,
3964 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003965{
3966 long count = count_arg;
3967 long n;
3968 pos_T old_pos;
3969 pos_T start_pos;
3970 pos_T end_pos;
3971 pos_T old_start, old_end;
3972 char_u *spat, *epat;
3973 char_u *p;
3974 char_u *cp;
3975 int len;
3976 int r;
3977 int do_include = include;
3978 int save_p_ws = p_ws;
3979 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003980 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003981
3982 p_ws = FALSE;
3983
3984 old_pos = curwin->w_cursor;
3985 old_end = curwin->w_cursor; /* remember where we started */
3986 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003987 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003988 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003989
3990 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003991 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003992 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003993 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003994 {
3995 setpcmark();
3996
3997 /* ignore indent */
3998 while (inindent(1))
3999 if (inc_cursor() != 0)
4000 break;
4001
4002 if (in_html_tag(FALSE))
4003 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004004 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004005 while (*ml_get_cursor() != '>')
4006 if (inc_cursor() < 0)
4007 break;
4008 }
4009 else if (in_html_tag(TRUE))
4010 {
4011 /* cursor on end tag, move to just before it */
4012 while (*ml_get_cursor() != '<')
4013 if (dec_cursor() < 0)
4014 break;
4015 dec_cursor();
4016 old_end = curwin->w_cursor;
4017 }
4018 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004019 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004020 {
4021 old_start = VIsual;
4022 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4023 }
4024 else
4025 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004026
4027again:
4028 /*
4029 * Search backwards for unclosed "<aaa>".
4030 * Put this position in start_pos.
4031 */
4032 for (n = 0; n < count; ++n)
4033 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004034 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004035 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004036 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004037 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004038 {
4039 curwin->w_cursor = old_pos;
4040 goto theend;
4041 }
4042 }
4043 start_pos = curwin->w_cursor;
4044
4045 /*
4046 * Search for matching "</aaa>". First isolate the "aaa".
4047 */
4048 inc_cursor();
4049 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004050 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004051 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004052 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004053 if (len == 0)
4054 {
4055 curwin->w_cursor = old_pos;
4056 goto theend;
4057 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004058 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004059 epat = alloc(len + 9);
4060 if (spat == NULL || epat == NULL)
4061 {
4062 vim_free(spat);
4063 vim_free(epat);
4064 curwin->w_cursor = old_pos;
4065 goto theend;
4066 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004067 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004068 sprintf((char *)epat, "</%.*s>\\c", len, p);
4069
Bram Moolenaar48570482017-10-30 21:48:41 +01004070 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004071 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004072
4073 vim_free(spat);
4074 vim_free(epat);
4075
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004076 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004077 {
4078 /* Can't find other end or it's before the previous end. Could be a
4079 * HTML tag that doesn't have a matching end. Search backwards for
4080 * another starting tag. */
4081 count = 1;
4082 curwin->w_cursor = start_pos;
4083 goto again;
4084 }
4085
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02004086 if (do_include)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004087 {
4088 /* Include up to the '>'. */
4089 while (*ml_get_cursor() != '>')
4090 if (inc_cursor() < 0)
4091 break;
4092 }
4093 else
4094 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004095 char_u *c = ml_get_cursor();
4096
4097 /* Exclude the '<' of the end tag.
4098 * If the closing tag is on new line, do not decrement cursor, but
4099 * make operation exclusive, so that the linefeed will be selected */
4100 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4101 /* do not decrement cursor */
4102 is_inclusive = FALSE;
4103 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004104 dec_cursor();
4105 }
4106 end_pos = curwin->w_cursor;
4107
4108 if (!do_include)
4109 {
4110 /* Exclude the start tag. */
4111 curwin->w_cursor = start_pos;
4112 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004113 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004114 {
4115 inc_cursor();
4116 start_pos = curwin->w_cursor;
4117 break;
4118 }
4119 curwin->w_cursor = end_pos;
4120
Bram Moolenaarb476cb72018-08-16 21:37:50 +02004121 // If we are in Visual mode and now have the same text as before set
4122 // "do_include" and try again.
4123 if (VIsual_active && EQUAL_POS(start_pos, old_start)
4124 && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004125 {
4126 do_include = TRUE;
4127 curwin->w_cursor = old_start;
4128 count = count_arg;
4129 goto again;
4130 }
4131 }
4132
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004133 if (VIsual_active)
4134 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004135 /* If the end is before the start there is no text between tags, select
4136 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004137 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004138 curwin->w_cursor = start_pos;
4139 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004140 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004141 VIsual = start_pos;
4142 VIsual_mode = 'v';
4143 redraw_curbuf_later(INVERTED); /* update the inversion */
4144 showmode();
4145 }
4146 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004147 {
4148 oap->start = start_pos;
4149 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004150 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004151 {
4152 /* End is before the start: there is no text between tags; operate
4153 * on an empty area. */
4154 curwin->w_cursor = start_pos;
4155 oap->inclusive = FALSE;
4156 }
4157 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004158 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004159 }
4160 retval = OK;
4161
4162theend:
4163 p_ws = save_p_ws;
4164 return retval;
4165}
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004168current_par(
4169 oparg_T *oap,
4170 long count,
4171 int include, /* TRUE == include white space */
4172 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173{
4174 linenr_T start_lnum;
4175 linenr_T end_lnum;
4176 int white_in_front;
4177 int dir;
4178 int start_is_white;
4179 int prev_start_is_white;
4180 int retval = OK;
4181 int do_white = FALSE;
4182 int t;
4183 int i;
4184
4185 if (type == 'S') /* not implemented yet */
4186 return FAIL;
4187
4188 start_lnum = curwin->w_cursor.lnum;
4189
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 /*
4191 * When visual area is more than one line: extend it.
4192 */
4193 if (VIsual_active && start_lnum != VIsual.lnum)
4194 {
4195extend:
4196 if (start_lnum < VIsual.lnum)
4197 dir = BACKWARD;
4198 else
4199 dir = FORWARD;
4200 for (i = count; --i >= 0; )
4201 {
4202 if (start_lnum ==
4203 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4204 {
4205 retval = FAIL;
4206 break;
4207 }
4208
4209 prev_start_is_white = -1;
4210 for (t = 0; t < 2; ++t)
4211 {
4212 start_lnum += dir;
4213 start_is_white = linewhite(start_lnum);
4214 if (prev_start_is_white == start_is_white)
4215 {
4216 start_lnum -= dir;
4217 break;
4218 }
4219 for (;;)
4220 {
4221 if (start_lnum == (dir == BACKWARD
4222 ? 1 : curbuf->b_ml.ml_line_count))
4223 break;
4224 if (start_is_white != linewhite(start_lnum + dir)
4225 || (!start_is_white
4226 && startPS(start_lnum + (dir > 0
4227 ? 1 : 0), 0, 0)))
4228 break;
4229 start_lnum += dir;
4230 }
4231 if (!include)
4232 break;
4233 if (start_lnum == (dir == BACKWARD
4234 ? 1 : curbuf->b_ml.ml_line_count))
4235 break;
4236 prev_start_is_white = start_is_white;
4237 }
4238 }
4239 curwin->w_cursor.lnum = start_lnum;
4240 curwin->w_cursor.col = 0;
4241 return retval;
4242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243
4244 /*
4245 * First move back to the start_lnum of the paragraph or white lines
4246 */
4247 white_in_front = linewhite(start_lnum);
4248 while (start_lnum > 1)
4249 {
4250 if (white_in_front) /* stop at first white line */
4251 {
4252 if (!linewhite(start_lnum - 1))
4253 break;
4254 }
4255 else /* stop at first non-white line of start of paragraph */
4256 {
4257 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4258 break;
4259 }
4260 --start_lnum;
4261 }
4262
4263 /*
4264 * Move past the end of any white lines.
4265 */
4266 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004267 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4268 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
4270 --end_lnum;
4271 i = count;
4272 if (!include && white_in_front)
4273 --i;
4274 while (i--)
4275 {
4276 if (end_lnum == curbuf->b_ml.ml_line_count)
4277 return FAIL;
4278
4279 if (!include)
4280 do_white = linewhite(end_lnum + 1);
4281
4282 if (include || !do_white)
4283 {
4284 ++end_lnum;
4285 /*
4286 * skip to end of paragraph
4287 */
4288 while (end_lnum < curbuf->b_ml.ml_line_count
4289 && !linewhite(end_lnum + 1)
4290 && !startPS(end_lnum + 1, 0, 0))
4291 ++end_lnum;
4292 }
4293
4294 if (i == 0 && white_in_front && include)
4295 break;
4296
4297 /*
4298 * skip to end of white lines after paragraph
4299 */
4300 if (include || do_white)
4301 while (end_lnum < curbuf->b_ml.ml_line_count
4302 && linewhite(end_lnum + 1))
4303 ++end_lnum;
4304 }
4305
4306 /*
4307 * If there are no empty lines at the end, try to find some empty lines at
4308 * the start (unless that has been done already).
4309 */
4310 if (!white_in_front && !linewhite(end_lnum) && include)
4311 while (start_lnum > 1 && linewhite(start_lnum - 1))
4312 --start_lnum;
4313
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 if (VIsual_active)
4315 {
4316 /* Problem: when doing "Vipipip" nothing happens in a single white
4317 * line, we get stuck there. Trap this here. */
4318 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4319 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004320 if (VIsual.lnum != start_lnum)
4321 {
4322 VIsual.lnum = start_lnum;
4323 VIsual.col = 0;
4324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 VIsual_mode = 'V';
4326 redraw_curbuf_later(INVERTED); /* update the inversion */
4327 showmode();
4328 }
4329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 {
4331 oap->start.lnum = start_lnum;
4332 oap->start.col = 0;
4333 oap->motion_type = MLINE;
4334 }
4335 curwin->w_cursor.lnum = end_lnum;
4336 curwin->w_cursor.col = 0;
4337
4338 return OK;
4339}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004340
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004341/*
4342 * Search quote char from string line[col].
4343 * Quote character escaped by one of the characters in "escape" is not counted
4344 * as a quote.
4345 * Returns column number of "quotechar" or -1 when not found.
4346 */
4347 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004348find_next_quote(
4349 char_u *line,
4350 int col,
4351 int quotechar,
4352 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004353{
4354 int c;
4355
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004356 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004357 {
4358 c = line[col];
4359 if (c == NUL)
4360 return -1;
4361 else if (escape != NULL && vim_strchr(escape, c))
4362 ++col;
4363 else if (c == quotechar)
4364 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004365 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004366 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004367 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004368 ++col;
4369 }
4370 return col;
4371}
4372
4373/*
4374 * Search backwards in "line" from column "col_start" to find "quotechar".
4375 * Quote character escaped by one of the characters in "escape" is not counted
4376 * as a quote.
4377 * Return the found column or zero.
4378 */
4379 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004380find_prev_quote(
4381 char_u *line,
4382 int col_start,
4383 int quotechar,
4384 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004385{
4386 int n;
4387
4388 while (col_start > 0)
4389 {
4390 --col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004391 col_start -= (*mb_head_off)(line, line + col_start);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004392 n = 0;
4393 if (escape != NULL)
4394 while (col_start - n > 0 && vim_strchr(escape,
4395 line[col_start - n - 1]) != NULL)
4396 ++n;
4397 if (n & 1)
4398 col_start -= n; /* uneven number of escape chars, skip it */
4399 else if (line[col_start] == quotechar)
4400 break;
4401 }
4402 return col_start;
4403}
4404
4405/*
4406 * Find quote under the cursor, cursor at end.
4407 * Returns TRUE if found, else FALSE.
4408 */
4409 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004410current_quote(
4411 oparg_T *oap,
4412 long count,
4413 int include, /* TRUE == include quote char */
4414 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004415{
4416 char_u *line = ml_get_curline();
4417 int col_end;
4418 int col_start = curwin->w_cursor.col;
4419 int inclusive = FALSE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004420 int vis_empty = TRUE; // Visual selection <= 1 char
4421 int vis_bef_curs = FALSE; // Visual starts before cursor
4422 int inside_quotes = FALSE; // Looks like "i'" done before
4423 int selected_quote = FALSE; // Has quote inside selection
Bram Moolenaarab194812005-09-14 21:40:12 +00004424 int i;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004425 int restore_vis_bef = FALSE; // restore VIsual on abort
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004426
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004427 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004428 if (VIsual_active)
4429 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004430 /* this only works within one line */
4431 if (VIsual.lnum != curwin->w_cursor.lnum)
4432 return FALSE;
4433
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004434 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004435 if (*p_sel == 'e')
4436 {
4437 if (!vis_bef_curs)
4438 {
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004439 // VIsual needs to be the start of Visual selection.
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004440 pos_T t = curwin->w_cursor;
4441
4442 curwin->w_cursor = VIsual;
4443 VIsual = t;
4444 vis_bef_curs = TRUE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004445 restore_vis_bef = TRUE;
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004446 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004447 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004448 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004449 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004450 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004451
4452 if (!vis_empty)
4453 {
4454 /* Check if the existing selection exactly spans the text inside
4455 * quotes. */
4456 if (vis_bef_curs)
4457 {
4458 inside_quotes = VIsual.col > 0
4459 && line[VIsual.col - 1] == quotechar
4460 && line[curwin->w_cursor.col] != NUL
4461 && line[curwin->w_cursor.col + 1] == quotechar;
4462 i = VIsual.col;
4463 col_end = curwin->w_cursor.col;
4464 }
4465 else
4466 {
4467 inside_quotes = curwin->w_cursor.col > 0
4468 && line[curwin->w_cursor.col - 1] == quotechar
4469 && line[VIsual.col] != NUL
4470 && line[VIsual.col + 1] == quotechar;
4471 i = curwin->w_cursor.col;
4472 col_end = VIsual.col;
4473 }
4474
4475 /* Find out if we have a quote in the selection. */
4476 while (i <= col_end)
4477 if (line[i++] == quotechar)
4478 {
4479 selected_quote = TRUE;
4480 break;
4481 }
4482 }
4483
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004484 if (!vis_empty && line[col_start] == quotechar)
4485 {
4486 /* Already selecting something and on a quote character. Find the
4487 * next quoted string. */
4488 if (vis_bef_curs)
4489 {
4490 /* Assume we are on a closing quote: move to after the next
4491 * opening quote. */
4492 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4493 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004494 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004495 col_end = find_next_quote(line, col_start + 1, quotechar,
4496 curbuf->b_p_qe);
4497 if (col_end < 0)
4498 {
4499 /* We were on a starting quote perhaps? */
4500 col_end = col_start;
4501 col_start = curwin->w_cursor.col;
4502 }
4503 }
4504 else
4505 {
4506 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4507 if (line[col_end] != quotechar)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004508 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004509 col_start = find_prev_quote(line, col_end, quotechar,
4510 curbuf->b_p_qe);
4511 if (line[col_start] != quotechar)
4512 {
4513 /* We were on an ending quote perhaps? */
4514 col_start = col_end;
4515 col_end = curwin->w_cursor.col;
4516 }
4517 }
4518 }
4519 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004520
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004521 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004522 {
4523 int first_col = col_start;
4524
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004525 if (!vis_empty)
4526 {
4527 if (vis_bef_curs)
4528 first_col = find_next_quote(line, col_start, quotechar, NULL);
4529 else
4530 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4531 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004532
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004533 /* The cursor is on a quote, we don't know if it's the opening or
4534 * closing quote. Search from the start of the line to find out.
4535 * Also do this when there is a Visual area, a' may leave the cursor
4536 * in between two strings. */
4537 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004538 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004539 {
4540 /* Find open quote character. */
4541 col_start = find_next_quote(line, col_start, quotechar, NULL);
4542 if (col_start < 0 || col_start > first_col)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004543 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004544 /* Find close quote character. */
4545 col_end = find_next_quote(line, col_start + 1, quotechar,
4546 curbuf->b_p_qe);
4547 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004548 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004549 /* If is cursor between start and end quote character, it is
4550 * target text object. */
4551 if (col_start <= first_col && first_col <= col_end)
4552 break;
4553 col_start = col_end + 1;
4554 }
4555 }
4556 else
4557 {
4558 /* Search backward for a starting quote. */
4559 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4560 if (line[col_start] != quotechar)
4561 {
4562 /* No quote before the cursor, look after the cursor. */
4563 col_start = find_next_quote(line, col_start, quotechar, NULL);
4564 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004565 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004566 }
4567
4568 /* Find close quote character. */
4569 col_end = find_next_quote(line, col_start + 1, quotechar,
4570 curbuf->b_p_qe);
4571 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004572 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004573 }
4574
4575 /* When "include" is TRUE, include spaces after closing quote or before
4576 * the starting quote. */
4577 if (include)
4578 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004579 if (VIM_ISWHITE(line[col_end + 1]))
4580 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004581 ++col_end;
4582 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004583 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004584 --col_start;
4585 }
4586
Bram Moolenaarab194812005-09-14 21:40:12 +00004587 /* Set start position. After vi" another i" must include the ".
4588 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004589 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004590 ++col_start;
4591 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004592 if (VIsual_active)
4593 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004594 /* Set the start of the Visual area when the Visual area was empty, we
4595 * were just inside quotes or the Visual area didn't start at a quote
4596 * and didn't include a quote.
4597 */
4598 if (vis_empty
4599 || (vis_bef_curs
4600 && !selected_quote
4601 && (inside_quotes
4602 || (line[VIsual.col] != quotechar
4603 && (VIsual.col == 0
4604 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004605 {
4606 VIsual = curwin->w_cursor;
4607 redraw_curbuf_later(INVERTED);
4608 }
4609 }
4610 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004611 {
4612 oap->start = curwin->w_cursor;
4613 oap->motion_type = MCHAR;
4614 }
4615
4616 /* Set end position. */
4617 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004618 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004619 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004620 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004621 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004622 if (VIsual_active)
4623 {
4624 if (vis_empty || vis_bef_curs)
4625 {
4626 /* decrement cursor when 'selection' is not exclusive */
4627 if (*p_sel != 'e')
4628 dec_cursor();
4629 }
4630 else
4631 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004632 /* Cursor is at start of Visual area. Set the end of the Visual
4633 * area when it was just inside quotes or it didn't end at a
4634 * quote. */
4635 if (inside_quotes
4636 || (!selected_quote
4637 && line[VIsual.col] != quotechar
4638 && (line[VIsual.col] == NUL
4639 || line[VIsual.col + 1] != quotechar)))
4640 {
4641 dec_cursor();
4642 VIsual = curwin->w_cursor;
4643 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004644 curwin->w_cursor.col = col_start;
4645 }
4646 if (VIsual_mode == 'V')
4647 {
4648 VIsual_mode = 'v';
4649 redraw_cmdline = TRUE; /* show mode later */
4650 }
4651 }
4652 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004653 {
4654 /* Set inclusive and other oap's flags. */
4655 oap->inclusive = inclusive;
4656 }
4657
4658 return OK;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004659
4660abort_search:
4661 if (VIsual_active && *p_sel == 'e')
4662 {
4663 inc_cursor();
4664 if (restore_vis_bef)
4665 {
4666 pos_T t = curwin->w_cursor;
4667
4668 curwin->w_cursor = VIsual;
4669 VIsual = t;
4670 }
4671 }
4672 return FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004673}
4674
4675#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004677static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004678
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004679/*
4680 * Find next search match under cursor, cursor at end.
4681 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004682 */
4683 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004684current_search(
4685 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004686 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004687{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004688 pos_T start_pos; // start position of the pattern match
4689 pos_T end_pos; // end position of the pattern match
4690 pos_T orig_pos; // position of the cursor at beginning
4691 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004692 int i;
4693 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004694 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004695 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004696 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004697 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004698 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004699
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004700 /* wrapping should not occur */
4701 p_ws = FALSE;
4702
4703 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004704 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004705 dec_cursor();
4706
4707 if (VIsual_active)
4708 {
4709 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004710
4711 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004712
4713 /* make sure, searching further will extend the match */
4714 if (VIsual_active)
4715 {
4716 if (forward)
4717 incl(&pos);
4718 else
4719 decl(&pos);
4720 }
4721 }
4722 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004723 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004724
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004725 /* Is the pattern is zero-width?, this time, don't care about the direction
4726 */
4727 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4728 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004729 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004730 {
4731 p_ws = old_p_ws;
4732 return FAIL; /* pattern not found */
4733 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004734
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004735 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004736 * The trick is to first search backwards and then search forward again,
4737 * so that a match at the current cursor position will be correctly
4738 * captured.
4739 */
4740 for (i = 0; i < 2; i++)
4741 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004742 if (forward)
4743 dir = i;
4744 else
4745 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004746
4747 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004748 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004749 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004750 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004751
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004752 result = searchit(curwin, curbuf, &pos, &end_pos,
4753 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004754 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004755 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004756
4757 /* First search may fail, but then start searching from the
4758 * beginning of the file (cursor might be on the search match)
4759 * except when Visual mode is active, so that extending the visual
4760 * selection works. */
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004761 if (i == 1 && !result) /* not found, abort */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004762 {
4763 curwin->w_cursor = orig_pos;
4764 if (VIsual_active)
4765 VIsual = save_VIsual;
4766 p_ws = old_p_ws;
4767 return FAIL;
4768 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004769 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004770 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004771 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004772 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004773 /* try again from start of buffer */
4774 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004775 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004776 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004777 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004778 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004779 /* searching backwards, so set pos to last line and col */
4780 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004781 pos.col = (colnr_T)STRLEN(
4782 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004783 }
4784 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004785 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004786 }
4787
4788 start_pos = pos;
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004789 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004790
4791 if (!VIsual_active)
4792 VIsual = start_pos;
4793
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004794 // put cursor on last character of match
4795 curwin->w_cursor = end_pos;
4796 if (LT_POS(VIsual, end_pos))
4797 dec_cursor();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004798 VIsual_active = TRUE;
4799 VIsual_mode = 'v';
4800
Bram Moolenaarb7633612019-02-10 21:48:25 +01004801 redraw_curbuf_later(INVERTED); /* update the inversion */
4802 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004803 {
Bram Moolenaarb7633612019-02-10 21:48:25 +01004804 /* Correction for exclusive selection depends on the direction. */
4805 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
4806 inc_cursor();
4807 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
4808 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004809 }
4810
4811#ifdef FEAT_FOLDING
4812 if (fdo_flags & FDO_SEARCH && KeyTyped)
4813 foldOpenCursor();
4814#endif
4815
4816 may_start_select('c');
4817#ifdef FEAT_MOUSE
4818 setmouse();
4819#endif
4820#ifdef FEAT_CLIPBOARD
4821 /* Make sure the clipboard gets updated. Needed because start and
4822 * end are still the same, and the selection needs to be owned */
4823 clip_star.vmode = NUL;
4824#endif
4825 redraw_curbuf_later(INVERTED);
4826 showmode();
4827
4828 return OK;
4829}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004830
4831/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004832 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004833 * If move is TRUE, check from the beginning of the buffer, else from position
4834 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004835 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004836 * Returns TRUE, FALSE or -1 for failure.
4837 */
4838 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004839is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004840{
4841 regmmatch_T regmatch;
4842 int nmatched = 0;
4843 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004844 pos_T pos;
4845 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004846 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004847
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004848 if (pattern == NULL)
4849 pattern = spats[last_idx].pat;
4850
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004851 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4852 SEARCH_KEEP, &regmatch) == FAIL)
4853 return -1;
4854
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004855 /* init startcol correctly */
4856 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004857 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004858 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004859 {
4860 CLEAR_POS(&pos);
4861 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004862 else
4863 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004864 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004865 /* accept a match at the cursor position */
4866 flag = SEARCH_START;
4867 }
4868
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004869 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004870 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004871 {
4872 /* Zero-width pattern should match somewhere, then we can check if
4873 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004874 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004875 do
4876 {
4877 regmatch.startpos[0].col++;
4878 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004879 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004880 if (!nmatched)
4881 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004882 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4883 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004884
4885 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004886 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004887 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004888 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4889 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004890 /* one char width */
4891 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4892 result = TRUE;
4893 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004894 }
4895
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004896 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004897 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004898 return result;
4899}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004900
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4902 || defined(PROTO)
4903/*
4904 * return TRUE if line 'lnum' is empty or has white chars only.
4905 */
4906 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004907linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
4909 char_u *p;
4910
4911 p = skipwhite(ml_get(lnum));
4912 return (*p == NUL);
4913}
4914#endif
4915
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004916/*
4917 * Add the search count "[3/19]" to "msgbuf".
4918 */
4919 static void
4920search_stat(
4921 int dirc,
4922 pos_T *pos,
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02004923 int show_top_bot_msg,
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004924 char_u *msgbuf)
4925{
4926 int save_ws = p_ws;
4927 int wraparound = FALSE;
4928 pos_T p = (*pos);
4929 static pos_T lastpos = {0, 0, 0};
4930 static int cur = 0;
4931 static int cnt = 0;
4932 static int chgtick = 0;
4933 static char_u *lastpat = NULL;
4934 static buf_T *lbuf = NULL;
4935#ifdef FEAT_RELTIME
4936 proftime_T start;
4937#endif
4938#define OUT_OF_TIME 999
4939
4940 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
4941 || (dirc == '/' && LT_POS(p, lastpos)));
4942
4943 // If anything relevant changed the count has to be recomputed.
4944 // MB_STRNICMP ignores case, but we should not ignore case.
4945 // Unfortunately, there is no MB_STRNICMP function.
4946 if (!(chgtick == CHANGEDTICK(curbuf)
4947 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
4948 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
4949 && EQUAL_POS(lastpos, curwin->w_cursor)
4950 && lbuf == curbuf) || wraparound || cur < 0 || cur > 99)
4951 {
4952 cur = 0;
4953 cnt = 0;
4954 CLEAR_POS(&lastpos);
4955 lbuf = curbuf;
4956 }
4957
4958 if (EQUAL_POS(lastpos, curwin->w_cursor) && !wraparound
4959 && (dirc == '/' ? cur < cnt : cur > 0))
4960 cur += dirc == '/' ? 1 : -1;
4961 else
4962 {
4963 p_ws = FALSE;
4964#ifdef FEAT_RELTIME
4965 profile_setlimit(20L, &start);
4966#endif
4967 while (!got_int && searchit(curwin, curbuf, &lastpos, NULL,
Bram Moolenaar9ce3fa82019-05-07 21:29:11 +02004968 FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST,
4969 (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004970 {
4971#ifdef FEAT_RELTIME
4972 // Stop after passing the time limit.
4973 if (profile_passed_limit(&start))
4974 {
4975 cnt = OUT_OF_TIME;
4976 cur = OUT_OF_TIME;
4977 break;
4978 }
4979#endif
4980 cnt++;
4981 if (LTOREQ_POS(lastpos, p))
4982 cur++;
4983 fast_breakcheck();
4984 if (cnt > 99)
4985 break;
4986 }
4987 if (got_int)
4988 cur = -1; // abort
4989 }
4990 if (cur > 0)
4991 {
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004992 char t[SEARCH_STAT_BUF_LEN] = "";
Bram Moolenaare2ad8262019-05-24 13:22:22 +02004993 size_t len;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004994
4995#ifdef FEAT_RIGHTLEFT
4996 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
4997 {
4998 if (cur == OUT_OF_TIME)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02004999 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005000 else if (cnt > 99 && cur > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005001 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005002 else if (cnt > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005003 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/%d]", cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005004 else
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005005 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cnt, cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005006 }
5007 else
5008#endif
5009 {
5010 if (cur == OUT_OF_TIME)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005011 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005012 else if (cnt > 99 && cur > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005013 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]");
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005014 else if (cnt > 99)
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005015 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>99]", cur);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005016 else
Bram Moolenaarb6cb26f2019-05-07 21:34:37 +02005017 vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cur, cnt);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005018 }
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005019
5020 len = STRLEN(t);
Bram Moolenaardc6855a2019-05-18 19:26:29 +02005021 if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN)
Bram Moolenaarc7a10b32019-05-06 21:37:18 +02005022 {
5023 STRCPY(t + len, " W");
5024 len += 2;
5025 }
5026
5027 mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005028 if (dirc == '?' && cur == 100)
5029 cur = -1;
5030
5031 vim_free(lastpat);
5032 lastpat = vim_strsave(spats[last_idx].pat);
5033 chgtick = CHANGEDTICK(curbuf);
5034 lbuf = curbuf;
5035 lastpos = p;
5036
Bram Moolenaar984f0312019-05-24 13:11:47 +02005037 // keep the message even after redraw, but don't put in history
5038 msg_hist_off = TRUE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005039 give_warning(msgbuf, FALSE);
Bram Moolenaar984f0312019-05-24 13:11:47 +02005040 msg_hist_off = FALSE;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005041 }
5042 p_ws = save_ws;
5043}
5044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045#if defined(FEAT_FIND_ID) || defined(PROTO)
5046/*
5047 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01005048 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005051find_pattern_in_path(
5052 char_u *ptr, /* pointer to search pattern */
5053 int dir UNUSED, /* direction of expansion */
5054 int len, /* length of search pattern */
5055 int whole, /* match whole words only */
5056 int skip_comments, /* don't match inside comments */
5057 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005059 long count,
5060 int action, /* What to do when we find it */
5061 linenr_T start_lnum, /* first line to start searching */
5062 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
5064 SearchedFile *files; /* Stack of included files */
5065 SearchedFile *bigger; /* When we need more space */
5066 int max_path_depth = 50;
5067 long match_count = 1;
5068
5069 char_u *pat;
5070 char_u *new_fname;
5071 char_u *curr_fname = curbuf->b_fname;
5072 char_u *prev_fname = NULL;
5073 linenr_T lnum;
5074 int depth;
5075 int depth_displayed; /* For type==CHECK_PATH */
5076 int old_files;
5077 int already_searched;
5078 char_u *file_line;
5079 char_u *line;
5080 char_u *p;
5081 char_u save_char;
5082 int define_matched;
5083 regmatch_T regmatch;
5084 regmatch_T incl_regmatch;
5085 regmatch_T def_regmatch;
5086 int matched = FALSE;
5087 int did_show = FALSE;
5088 int found = FALSE;
5089 int i;
5090 char_u *already = NULL;
5091 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005092 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005093#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 win_T *curwin_save = NULL;
5095#endif
5096
5097 regmatch.regprog = NULL;
5098 incl_regmatch.regprog = NULL;
5099 def_regmatch.regprog = NULL;
5100
5101 file_line = alloc(LSIZE);
5102 if (file_line == NULL)
5103 return;
5104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (type != CHECK_PATH && type != FIND_DEFINE
5106#ifdef FEAT_INS_EXPAND
5107 /* when CONT_SOL is set compare "ptr" with the beginning of the line
5108 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#endif
5111 )
5112 {
5113 pat = alloc(len + 5);
5114 if (pat == NULL)
5115 goto fpip_end;
5116 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
5117 /* ignore case according to p_ic, p_scs and pat */
5118 regmatch.rm_ic = ignorecase(pat);
5119 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5120 vim_free(pat);
5121 if (regmatch.regprog == NULL)
5122 goto fpip_end;
5123 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005124 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
5125 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005127 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (incl_regmatch.regprog == NULL)
5129 goto fpip_end;
5130 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
5131 }
5132 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
5133 {
5134 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5135 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5136 if (def_regmatch.regprog == NULL)
5137 goto fpip_end;
5138 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5139 }
5140 files = (SearchedFile *)lalloc_clear((long_u)
5141 (max_path_depth * sizeof(SearchedFile)), TRUE);
5142 if (files == NULL)
5143 goto fpip_end;
5144 old_files = max_path_depth;
5145 depth = depth_displayed = -1;
5146
5147 lnum = start_lnum;
5148 if (end_lnum > curbuf->b_ml.ml_line_count)
5149 end_lnum = curbuf->b_ml.ml_line_count;
5150 if (lnum > end_lnum) /* do at least one line */
5151 lnum = end_lnum;
5152 line = ml_get(lnum);
5153
5154 for (;;)
5155 {
5156 if (incl_regmatch.regprog != NULL
5157 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5158 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005159 char_u *p_fname = (curr_fname == curbuf->b_fname)
5160 ? curbuf->b_ffname : curr_fname;
5161
5162 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5163 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5164 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005165 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005166 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5167 else
5168 /* Use text after match with 'include'. */
5169 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005170 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 already_searched = FALSE;
5172 if (new_fname != NULL)
5173 {
5174 /* Check whether we have already searched in this file */
5175 for (i = 0;; i++)
5176 {
5177 if (i == depth + 1)
5178 i = old_files;
5179 if (i == max_path_depth)
5180 break;
Bram Moolenaar99499b12019-05-23 21:35:48 +02005181 if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE)
5182 & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
5184 if (type != CHECK_PATH &&
5185 action == ACTION_SHOW_ALL && files[i].matched)
5186 {
5187 msg_putchar('\n'); /* cursor below last one */
5188 if (!got_int) /* don't display if 'q'
5189 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005190 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
5192 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005193 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 prev_fname = NULL;
5195 }
5196 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01005197 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 already_searched = TRUE;
5199 break;
5200 }
5201 }
5202 }
5203
5204 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5205 || (new_fname == NULL && !already_searched)))
5206 {
5207 if (did_show)
5208 msg_putchar('\n'); /* cursor below last one */
5209 else
5210 {
5211 gotocmdline(TRUE); /* cursor at status line */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005212 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005214 msg_puts_title(_("not found "));
5215 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 did_show = TRUE;
5218 while (depth_displayed < depth && !got_int)
5219 {
5220 ++depth_displayed;
5221 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005222 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005224 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 if (!got_int) /* don't display if 'q' typed
5227 for "--more--" message */
5228 {
5229 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005230 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 if (new_fname != NULL)
5232 {
5233 /* using "new_fname" is more reliable, e.g., when
5234 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005235 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 }
5237 else
5238 {
5239 /*
5240 * Isolate the file name.
5241 * Include the surrounding "" or <> if present.
5242 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005243 if (inc_opt != NULL
5244 && strstr((char *)inc_opt, "\\zs") != NULL)
5245 {
5246 /* pattern contains \zs, use the match */
5247 p = incl_regmatch.startp[0];
5248 i = (int)(incl_regmatch.endp[0]
5249 - incl_regmatch.startp[0]);
5250 }
5251 else
5252 {
5253 /* find the file name after the end of the match */
5254 for (p = incl_regmatch.endp[0];
5255 *p && !vim_isfilec(*p); p++)
5256 ;
5257 for (i = 0; vim_isfilec(p[i]); i++)
5258 ;
5259 }
5260
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 if (i == 0)
5262 {
5263 /* Nothing found, use the rest of the line. */
5264 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005265 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005267 /* Avoid checking before the start of the line, can
5268 * happen if \zs appears in the regexp. */
5269 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 if (p[-1] == '"' || p[-1] == '<')
5272 {
5273 --p;
5274 ++i;
5275 }
5276 if (p[i] == '"' || p[i] == '>')
5277 ++i;
5278 }
5279 save_char = p[i];
5280 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005281 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 p[i] = save_char;
5283 }
5284
5285 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5286 {
5287 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005288 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005290 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 }
5293 out_flush(); /* output each line directly */
5294 }
5295
5296 if (new_fname != NULL)
5297 {
5298 /* Push the new file onto the file stack */
5299 if (depth + 1 == old_files)
5300 {
5301 bigger = (SearchedFile *)lalloc((long_u)(
5302 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5303 if (bigger != NULL)
5304 {
5305 for (i = 0; i <= depth; i++)
5306 bigger[i] = files[i];
5307 for (i = depth + 1; i < old_files + max_path_depth; i++)
5308 {
5309 bigger[i].fp = NULL;
5310 bigger[i].name = NULL;
5311 bigger[i].lnum = 0;
5312 bigger[i].matched = FALSE;
5313 }
5314 for (i = old_files; i < max_path_depth; i++)
5315 bigger[i + max_path_depth] = files[i];
5316 old_files += max_path_depth;
5317 max_path_depth *= 2;
5318 vim_free(files);
5319 files = bigger;
5320 }
5321 }
5322 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5323 == NULL)
5324 vim_free(new_fname);
5325 else
5326 {
5327 if (++depth == old_files)
5328 {
5329 /*
5330 * lalloc() for 'bigger' must have failed above. We
5331 * will forget one of our already visited files now.
5332 */
5333 vim_free(files[old_files].name);
5334 ++old_files;
5335 }
5336 files[depth].name = curr_fname = new_fname;
5337 files[depth].lnum = 0;
5338 files[depth].matched = FALSE;
5339#ifdef FEAT_INS_EXPAND
5340 if (action == ACTION_EXPAND)
5341 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005342 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005343 vim_snprintf((char*)IObuff, IOSIZE,
5344 _("Scanning included file: %s"),
5345 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005346 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005350 if (p_verbose >= 5)
5351 {
5352 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005353 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005354 (char *)new_fname);
5355 verbose_leave();
5356 }
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
5359 }
5360 }
5361 else
5362 {
5363 /*
5364 * Check if the line is a define (type == FIND_DEFINE)
5365 */
5366 p = line;
5367search_line:
5368 define_matched = FALSE;
5369 if (def_regmatch.regprog != NULL
5370 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5371 {
5372 /*
5373 * Pattern must be first identifier after 'define', so skip
5374 * to that position before checking for match of pattern. Also
5375 * don't let it match beyond the end of this identifier.
5376 */
5377 p = def_regmatch.endp[0];
5378 while (*p && !vim_iswordc(*p))
5379 p++;
5380 define_matched = TRUE;
5381 }
5382
5383 /*
5384 * Look for a match. Don't do this if we are looking for a
5385 * define and this line didn't match define_prog above.
5386 */
5387 if (def_regmatch.regprog == NULL || define_matched)
5388 {
5389 if (define_matched
5390#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005391 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#endif
5393 )
5394 {
5395 /* compare the first "len" chars from "ptr" */
5396 startp = skipwhite(p);
5397 if (p_ic)
5398 matched = !MB_STRNICMP(startp, ptr, len);
5399 else
5400 matched = !STRNCMP(startp, ptr, len);
5401 if (matched && define_matched && whole
5402 && vim_iswordc(startp[len]))
5403 matched = FALSE;
5404 }
5405 else if (regmatch.regprog != NULL
5406 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5407 {
5408 matched = TRUE;
5409 startp = regmatch.startp[0];
5410 /*
5411 * Check if the line is not a comment line (unless we are
5412 * looking for a define). A line starting with "# define"
5413 * is not considered to be a comment line.
5414 */
5415 if (!define_matched && skip_comments)
5416 {
5417#ifdef FEAT_COMMENTS
5418 if ((*line != '#' ||
5419 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005420 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 matched = FALSE;
5422
5423 /*
5424 * Also check for a "/ *" or "/ /" before the match.
5425 * Skips lines like "int backwards; / * normal index
5426 * * /" when looking for "normal".
5427 * Note: Doesn't skip "/ *" in comments.
5428 */
5429 p = skipwhite(line);
5430 if (matched
5431 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5432#endif
5433 for (p = line; *p && p < startp; ++p)
5434 {
5435 if (matched
5436 && p[0] == '/'
5437 && (p[1] == '*' || p[1] == '/'))
5438 {
5439 matched = FALSE;
5440 /* After "//" all text is comment */
5441 if (p[1] == '/')
5442 break;
5443 ++p;
5444 }
5445 else if (!matched && p[0] == '*' && p[1] == '/')
5446 {
5447 /* Can find match after "* /". */
5448 matched = TRUE;
5449 ++p;
5450 }
5451 }
5452 }
5453 }
5454 }
5455 }
5456 if (matched)
5457 {
5458#ifdef FEAT_INS_EXPAND
5459 if (action == ACTION_EXPAND)
5460 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005461 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 int add_r;
5463 char_u *aux;
5464
5465 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5466 break;
5467 found = TRUE;
5468 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005469 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005471 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if (vim_iswordp(p))
5473 goto exit_matched;
5474 p = find_word_start(p);
5475 }
5476 p = find_word_end(p);
5477 i = (int)(p - aux);
5478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005483
5484 /* Get the next line: when "depth" < 0 from the current
5485 * buffer, otherwise from the included file. Jump to
5486 * exit_matched when past the last line. */
5487 if (depth < 0)
5488 {
5489 if (lnum >= end_lnum)
5490 goto exit_matched;
5491 line = ml_get(++lnum);
5492 }
5493 else if (vim_fgets(line = file_line,
5494 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 goto exit_matched;
5496
5497 /* we read a line, set "already" to check this "line" later
5498 * if depth >= 0 we'll increase files[depth].lnum far
5499 * bellow -- Acevedo */
5500 already = aux = p = skipwhite(line);
5501 p = find_word_start(p);
5502 p = find_word_end(p);
5503 if (p > aux)
5504 {
5505 if (*aux != ')' && IObuff[i-1] != TAB)
5506 {
5507 if (IObuff[i-1] != ' ')
5508 IObuff[i++] = ' ';
5509 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5510 if (p_js
5511 && (IObuff[i-2] == '.'
5512 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5513 && (IObuff[i-2] == '?'
5514 || IObuff[i-2] == '!'))))
5515 IObuff[i++] = ' ';
5516 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005517 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 if (p - aux >= IOSIZE - i)
5519 p = aux + IOSIZE - i - 1;
5520 STRNCPY(IObuff + i, aux, p - aux);
5521 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005522 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524 IObuff[i] = NUL;
5525 aux = IObuff;
5526
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005527 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 goto exit_matched;
5529 }
5530
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005531 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005533 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (add_r == OK)
5535 /* if dir was BACKWARD then honor it just once */
5536 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005537 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 break;
5539 }
5540 else
5541#endif
5542 if (action == ACTION_SHOW_ALL)
5543 {
5544 found = TRUE;
5545 if (!did_show)
5546 gotocmdline(TRUE); /* cursor at status line */
5547 if (curr_fname != prev_fname)
5548 {
5549 if (did_show)
5550 msg_putchar('\n'); /* cursor below last one */
5551 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005552 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 msg_home_replace_hl(curr_fname);
5554 prev_fname = curr_fname;
5555 }
5556 did_show = TRUE;
5557 if (!got_int)
5558 show_pat_in_path(line, type, TRUE, action,
5559 (depth == -1) ? NULL : files[depth].fp,
5560 (depth == -1) ? &lnum : &files[depth].lnum,
5561 match_count++);
5562
5563 /* Set matched flag for this file and all the ones that
5564 * include it */
5565 for (i = 0; i <= depth; ++i)
5566 files[i].matched = TRUE;
5567 }
5568 else if (--count <= 0)
5569 {
5570 found = TRUE;
5571 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005572#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 && g_do_tagpreview == 0
5574#endif
5575 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005576 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 else if (action == ACTION_SHOW)
5578 {
5579 show_pat_in_path(line, type, did_show, action,
5580 (depth == -1) ? NULL : files[depth].fp,
5581 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5582 did_show = TRUE;
5583 }
5584 else
5585 {
5586#ifdef FEAT_GUI
5587 need_mouse_correct = TRUE;
5588#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005589#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 /* ":psearch" uses the preview window */
5591 if (g_do_tagpreview != 0)
5592 {
5593 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005594 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
5596#endif
5597 if (action == ACTION_SPLIT)
5598 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005601 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603 if (depth == -1)
5604 {
5605 /* match in current file */
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)
5608 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005609 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005610 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005611 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 break; /* failed to jump to file */
5613 }
5614 else
5615#endif
5616 setpcmark();
5617 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005618 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620 else
5621 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005622 if (!GETFILE_SUCCESS(getfile(
5623 0, files[depth].name, NULL, TRUE,
5624 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 break; /* failed to jump to file */
5626 /* autocommands may have changed the lnum, we don't
5627 * want that here */
5628 curwin->w_cursor.lnum = files[depth].lnum;
5629 }
5630 }
5631 if (action != ACTION_SHOW)
5632 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005633 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 curwin->w_set_curswant = TRUE;
5635 }
5636
Bram Moolenaar4033c552017-09-16 20:54:51 +02005637#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005639 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
5641 /* Return cursor to where we were */
5642 validate_cursor();
5643 redraw_later(VALID);
5644 win_enter(curwin_save, TRUE);
5645 }
5646#endif
5647 break;
5648 }
5649#ifdef FEAT_INS_EXPAND
5650exit_matched:
5651#endif
5652 matched = FALSE;
5653 /* look for other matches in the rest of the line if we
5654 * are not at the end of it already */
5655 if (def_regmatch.regprog == NULL
5656#ifdef FEAT_INS_EXPAND
5657 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005658 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005660 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005661 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 goto search_line;
5663 }
5664 line_breakcheck();
5665#ifdef FEAT_INS_EXPAND
5666 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005667 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005668 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669#else
5670 if (got_int)
5671#endif
5672 break;
5673
5674 /*
5675 * Read the next line. When reading an included file and encountering
5676 * end-of-file, close the file and continue in the file that included
5677 * it.
5678 */
5679 while (depth >= 0 && !already
5680 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5681 {
5682 fclose(files[depth].fp);
5683 --old_files;
5684 files[old_files].name = files[depth].name;
5685 files[old_files].matched = files[depth].matched;
5686 --depth;
5687 curr_fname = (depth == -1) ? curbuf->b_fname
5688 : files[depth].name;
5689 if (depth < depth_displayed)
5690 depth_displayed = depth;
5691 }
5692 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005693 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005695 /* Remove any CR and LF from the line. */
5696 i = (int)STRLEN(line);
5697 if (i > 0 && line[i - 1] == '\n')
5698 line[--i] = NUL;
5699 if (i > 0 && line[i - 1] == '\r')
5700 line[--i] = NUL;
5701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 else if (!already)
5703 {
5704 if (++lnum > end_lnum)
5705 break;
5706 line = ml_get(lnum);
5707 }
5708 already = NULL;
5709 }
5710 /* End of big for (;;) loop. */
5711
5712 /* Close any files that are still open. */
5713 for (i = 0; i <= depth; i++)
5714 {
5715 fclose(files[i].fp);
5716 vim_free(files[i].name);
5717 }
5718 for (i = old_files; i < max_path_depth; i++)
5719 vim_free(files[i].name);
5720 vim_free(files);
5721
5722 if (type == CHECK_PATH)
5723 {
5724 if (!did_show)
5725 {
5726 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005727 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005729 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731 }
5732 else if (!found
5733#ifdef FEAT_INS_EXPAND
5734 && action != ACTION_EXPAND
5735#endif
5736 )
5737 {
5738#ifdef FEAT_INS_EXPAND
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005739 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740#else
5741 if (got_int)
5742#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005743 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005745 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005747 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5750 msg_end();
5751
5752fpip_end:
5753 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005754 vim_regfree(regmatch.regprog);
5755 vim_regfree(incl_regmatch.regprog);
5756 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
5758
5759 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005760show_pat_in_path(
5761 char_u *line,
5762 int type,
5763 int did_show,
5764 int action,
5765 FILE *fp,
5766 linenr_T *lnum,
5767 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
5769 char_u *p;
5770
5771 if (did_show)
5772 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005773 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 gotocmdline(TRUE); /* cursor at status line */
5775 if (got_int) /* 'q' typed at "--more--" message */
5776 return;
5777 for (;;)
5778 {
5779 p = line + STRLEN(line) - 1;
5780 if (fp != NULL)
5781 {
5782 /* We used fgets(), so get rid of newline at end */
5783 if (p >= line && *p == '\n')
5784 --p;
5785 if (p >= line && *p == '\r')
5786 --p;
5787 *(p + 1) = NUL;
5788 }
5789 if (action == ACTION_SHOW_ALL)
5790 {
5791 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005792 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5794 /* Highlight line numbers */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005795 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
5796 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005798 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 out_flush(); /* show one line at a time */
5800
5801 /* Definition continues until line that doesn't end with '\' */
5802 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5803 break;
5804
5805 if (fp != NULL)
5806 {
5807 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5808 break;
5809 ++*lnum;
5810 }
5811 else
5812 {
5813 if (++*lnum > curbuf->b_ml.ml_line_count)
5814 break;
5815 line = ml_get(*lnum);
5816 }
5817 msg_putchar('\n');
5818 }
5819}
5820#endif
5821
5822#ifdef FEAT_VIMINFO
5823 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005824read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 char_u *lp;
5827 int idx = -1;
5828 int magic = FALSE;
5829 int no_scs = FALSE;
5830 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005831 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 long off = 0;
5833 int setlast = FALSE;
5834#ifdef FEAT_SEARCH_EXTRA
5835 static int hlsearch_on = FALSE;
5836#endif
5837 char_u *val;
5838
5839 /*
5840 * Old line types:
5841 * "/pat", "&pat": search/subst. pat
5842 * "~/pat", "~&pat": last used search/subst. pat
5843 * New line types:
5844 * "~h", "~H": hlsearch highlighting off/on
5845 * "~<magic><smartcase><line><end><off><last><which>pat"
5846 * <magic>: 'm' off, 'M' on
5847 * <smartcase>: 's' off, 'S' on
5848 * <line>: 'L' line offset, 'l' char offset
5849 * <end>: 'E' from end, 'e' from start
5850 * <off>: decimal, offset
5851 * <last>: '~' last used pattern
5852 * <which>: '/' search pat, '&' subst. pat
5853 */
5854 lp = virp->vir_line;
5855 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5856 {
5857 if (lp[1] == 'M') /* magic on */
5858 magic = TRUE;
5859 if (lp[2] == 's')
5860 no_scs = TRUE;
5861 if (lp[3] == 'L')
5862 off_line = TRUE;
5863 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005864 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 lp += 5;
5866 off = getdigits(&lp);
5867 }
5868 if (lp[0] == '~') /* use this pattern for last-used pattern */
5869 {
5870 setlast = TRUE;
5871 lp++;
5872 }
5873 if (lp[0] == '/')
5874 idx = RE_SEARCH;
5875 else if (lp[0] == '&')
5876 idx = RE_SUBST;
5877#ifdef FEAT_SEARCH_EXTRA
5878 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5879 hlsearch_on = FALSE;
5880 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5881 hlsearch_on = TRUE;
5882#endif
5883 if (idx >= 0)
5884 {
5885 if (force || spats[idx].pat == NULL)
5886 {
5887 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5888 TRUE);
5889 if (val != NULL)
5890 {
5891 set_last_search_pat(val, idx, magic, setlast);
5892 vim_free(val);
5893 spats[idx].no_scs = no_scs;
5894 spats[idx].off.line = off_line;
5895 spats[idx].off.end = off_end;
5896 spats[idx].off.off = off;
5897#ifdef FEAT_SEARCH_EXTRA
5898 if (setlast)
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02005899 set_no_hlsearch(!hlsearch_on);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900#endif
5901 }
5902 }
5903 }
5904 return viminfo_readline(virp);
5905}
5906
5907 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005908write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 if (get_viminfo_parameter('/') != 0)
5911 {
5912#ifdef FEAT_SEARCH_EXTRA
5913 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5914 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5915#endif
5916 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005917 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 }
5919}
5920
5921 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005922wvsp_one(
5923 FILE *fp, /* file to write to */
5924 int idx, /* spats[] index */
5925 char *s, /* search pat */
5926 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927{
5928 if (spats[idx].pat != NULL)
5929 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005930 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 /* off.dir is not stored, it's reset to forward */
5932 fprintf(fp, "%c%c%c%c%ld%s%c",
5933 spats[idx].magic ? 'M' : 'm', /* magic */
5934 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5935 spats[idx].off.line ? 'L' : 'l', /* line offset */
5936 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5937 spats[idx].off.off, /* offset */
5938 last_idx == idx ? "~" : "", /* last used pat */
5939 sc);
5940 viminfo_writestring(fp, spats[idx].pat);
5941 }
5942}
5943#endif /* FEAT_VIMINFO */