blob: 1fa52b443cfddb22a4b6f31284774774a25e281e [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 Moolenaar9dfa3132019-05-04 21:08:40 +020029static void search_stat(int dirc, pos_T *pos, 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 Moolenaar071d4272004-06-13 20:20:40 +00001222
1223 /*
1224 * A line offset is not remembered, this is vi compatible.
1225 */
1226 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1227 {
1228 spats[0].off.line = FALSE;
1229 spats[0].off.off = 0;
1230 }
1231
1232 /*
1233 * Save the values for when (options & SEARCH_KEEP) is used.
1234 * (there is no "if ()" around this because gcc wants them initialized)
1235 */
1236 old_off = spats[0].off;
1237
1238 pos = curwin->w_cursor; /* start searching at the cursor position */
1239
1240 /*
1241 * Find out the direction of the search.
1242 */
1243 if (dirc == 0)
1244 dirc = spats[0].off.dir;
1245 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001248#if defined(FEAT_EVAL)
1249 set_vv_searchforward();
1250#endif
1251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (options & SEARCH_REV)
1253 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01001254#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 /* There is a bug in the Visual C++ 2.2 compiler which means that
1256 * dirc always ends up being '/' */
1257 dirc = (dirc == '/') ? '?' : '/';
1258#else
1259 if (dirc == '/')
1260 dirc = '?';
1261 else
1262 dirc = '/';
1263#endif
1264 }
1265
1266#ifdef FEAT_FOLDING
1267 /* If the cursor is in a closed fold, don't find another match in the same
1268 * fold. */
1269 if (dirc == '/')
1270 {
1271 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1272 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1273 }
1274 else
1275 {
1276 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1277 pos.col = 0;
1278 }
1279#endif
1280
1281#ifdef FEAT_SEARCH_EXTRA
1282 /*
1283 * Turn 'hlsearch' highlighting back on.
1284 */
1285 if (no_hlsearch && !(options & SEARCH_KEEP))
1286 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001287 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001288 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290#endif
1291
1292 /*
1293 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1294 */
1295 for (;;)
1296 {
1297 searchstr = pat;
1298 dircp = NULL;
1299 /* use previous pattern */
1300 if (pat == NULL || *pat == NUL || *pat == dirc)
1301 {
1302 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1303 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001304 searchstr = spats[RE_SUBST].pat;
1305 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001307 emsg(_(e_noprevre));
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001308 retval = 0;
1309 goto end_do_search;
1310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001312 else
1313 {
1314 /* make search_regcomp() use spats[RE_SEARCH].pat */
1315 searchstr = (char_u *)"";
1316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 }
1318
1319 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1320 {
1321 /*
1322 * Find end of regular expression.
1323 * If there is a matching '/' or '?', toss it.
1324 */
1325 ps = strcopy;
1326 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1327 if (strcopy != ps)
1328 {
1329 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001330 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 pat = strcopy;
1332 searchstr = strcopy;
1333 }
1334 if (*p == dirc)
1335 {
1336 dircp = p; /* remember where we put the NUL */
1337 *p++ = NUL;
1338 }
1339 spats[0].off.line = FALSE;
1340 spats[0].off.end = FALSE;
1341 spats[0].off.off = 0;
1342 /*
1343 * Check for a line offset or a character offset.
1344 * For get_address (echo off) we don't check for a character
1345 * offset, because it is meaningless and the 's' could be a
1346 * substitute command.
1347 */
1348 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1349 spats[0].off.line = TRUE;
1350 else if ((options & SEARCH_OPT) &&
1351 (*p == 'e' || *p == 's' || *p == 'b'))
1352 {
1353 if (*p == 'e') /* end */
1354 spats[0].off.end = SEARCH_END;
1355 ++p;
1356 }
1357 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1358 {
1359 /* 'nr' or '+nr' or '-nr' */
1360 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1361 spats[0].off.off = atol((char *)p);
1362 else if (*p == '-') /* single '-' */
1363 spats[0].off.off = -1;
1364 else /* single '+' */
1365 spats[0].off.off = 1;
1366 ++p;
1367 while (VIM_ISDIGIT(*p)) /* skip number */
1368 ++p;
1369 }
1370
1371 /* compute length of search command for get_address() */
1372 searchcmdlen += (int)(p - pat);
1373
1374 pat = p; /* put pat after search command */
1375 }
1376
1377 if ((options & SEARCH_ECHO) && messaging()
1378 && !cmd_silent && msg_silent == 0)
1379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 char_u *trunc;
1381
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001382 // Compute msg_row early.
1383 msg_start();
1384
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 if (*searchstr == NUL)
Bram Moolenaar2fb8f682018-12-01 13:14:45 +01001386 p = spats[0].pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 else
1388 p = searchstr;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001389
1390 if (!shortmess(SHM_SEARCHCOUNT))
1391 {
1392 // Reserve enough space for the search pattern + offset +
1393 // search stat.
1394 if (msg_scrolled != 0)
1395 // Use all the columns.
1396 len = (int)(Rows - msg_row) * Columns - 1;
1397 else
1398 // Use up to 'showcmd' column.
1399 len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1;
1400 if (len < STRLEN(p) + 40 + 11)
1401 len = STRLEN(p) + 40 + 11;
1402 }
1403 else
1404 // Reserve enough space for the search pattern + offset.
1405 len = STRLEN(p) + 40;
1406
1407 msgbuf = alloc((int)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 if (msgbuf != NULL)
1409 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001410 vim_memset(msgbuf, ' ', len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 msgbuf[0] = dirc;
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001412 msgbuf[len - 1] = NUL;
1413
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001414 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1415 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001416 // Use a space to draw the composing char on.
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001417 msgbuf[1] = ' ';
Bram Moolenaarb3de6c42019-05-05 13:02:28 +02001418 mch_memmove(msgbuf + 2, p, STRLEN(p));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001419 }
1420 else
Bram Moolenaarb3de6c42019-05-05 13:02:28 +02001421 mch_memmove(msgbuf + 1, p, STRLEN(p));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1423 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001424 p = msgbuf + STRLEN(p) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 *p++ = dirc;
1426 if (spats[0].off.end)
1427 *p++ = 'e';
1428 else if (!spats[0].off.line)
1429 *p++ = 's';
1430 if (spats[0].off.off > 0 || spats[0].off.line)
1431 *p++ = '+';
1432 if (spats[0].off.off != 0 || spats[0].off.line)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001433 {
1434 int l = 0;
1435 l = sprintf((char *)p, "%ld", spats[0].off.off);
1436 p[l] = ' '; // remove NUL from sprintf
1437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
1439
Bram Moolenaara4a08382005-09-09 19:52:02 +00001440 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001441 if (trunc != NULL)
1442 {
1443 vim_free(msgbuf);
1444 msgbuf = trunc;
1445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
1447#ifdef FEAT_RIGHTLEFT
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001448 // The search pattern could be shown on the right in rightleft
1449 // mode, but the 'ruler' and 'showcmd' area use it too, thus
1450 // it would be blanked out again very soon. Show it on the
1451 // left, but do reverse the text.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1453 {
1454 char_u *r;
1455
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001456 r = reverse_text(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (r != NULL)
1458 {
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001459 vim_free(msgbuf);
1460 msgbuf = r;
1461 // move reversed text to beginning of buffer
1462 while (*r != NUL && *r == ' ')
1463 r++;
1464 mch_memmove(msgbuf, r, msgbuf + STRLEN(msgbuf) - r);
1465 // overwrite old text
1466 vim_memset(r, ' ', msgbuf + STRLEN(msgbuf) - r);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468 }
1469#endif
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001470 msg_outtrans(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 msg_clr_eos();
1472 msg_check();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
1474 gotocmdline(FALSE);
1475 out_flush();
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001476 msg_nowait = TRUE; // don't wait for this message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478 }
1479
1480 /*
1481 * If there is a character offset, subtract it from the current
1482 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001483 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 * This is not done for a line offset, because then we would not be vi
1485 * compatible.
1486 */
Bram Moolenaared203462004-06-16 11:19:22 +00001487 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
1489 if (spats[0].off.off > 0)
1490 {
1491 for (c = spats[0].off.off; c; --c)
1492 if (decl(&pos) == -1)
1493 break;
1494 if (c) /* at start of buffer */
1495 {
1496 pos.lnum = 0; /* allow lnum == 0 here */
1497 pos.col = MAXCOL;
1498 }
1499 }
1500 else
1501 {
1502 for (c = spats[0].off.off; c; ++c)
1503 if (incl(&pos) == -1)
1504 break;
1505 if (c) /* at end of buffer */
1506 {
1507 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1508 pos.col = 0;
1509 }
1510 }
1511 }
1512
Bram Moolenaar14184a32019-02-16 15:10:30 +01001513 c = searchit(curwin, curbuf, &pos, NULL,
1514 dirc == '/' ? FORWARD : BACKWARD,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 searchstr, count, spats[0].off.end + (options &
1516 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1517 + SEARCH_MSG + SEARCH_START
1518 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001519 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
1521 if (dircp != NULL)
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001522 *dircp = dirc; // restore second '/' or '?' for normal_cmd()
1523
1524 if (!shortmess(SHM_SEARCH)
1525 && ((dirc == '/' && LT_POS(pos, curwin->w_cursor))
1526 || (dirc == '?' && LT_POS(curwin->w_cursor, pos))))
1527 ui_delay(500L, FALSE); // leave some time for top_bot_msg
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (c == FAIL)
1530 {
1531 retval = 0;
1532 goto end_do_search;
1533 }
1534 if (spats[0].off.end && oap != NULL)
1535 oap->inclusive = TRUE; /* 'e' includes last character */
1536
1537 retval = 1; /* pattern found */
1538
1539 /*
1540 * Add character and/or line offset
1541 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001542 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 {
1544 if (spats[0].off.line) /* Add the offset to the line number. */
1545 {
1546 c = pos.lnum + spats[0].off.off;
1547 if (c < 1)
1548 pos.lnum = 1;
1549 else if (c > curbuf->b_ml.ml_line_count)
1550 pos.lnum = curbuf->b_ml.ml_line_count;
1551 else
1552 pos.lnum = c;
1553 pos.col = 0;
1554
1555 retval = 2; /* pattern found, line offset added */
1556 }
Bram Moolenaared203462004-06-16 11:19:22 +00001557 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
1559 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560 c = spats[0].off.off;
1561 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 if (incl(&pos) == -1)
1565 break;
1566 }
1567 /* to the left, check for start of file */
1568 else
1569 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570 while (c++ < 0)
1571 if (decl(&pos) == -1)
1572 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
1574 }
1575 }
1576
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001577 // Show [1/15] if 'S' is not in 'shortmess'.
1578 if ((options & SEARCH_ECHO)
1579 && messaging()
1580 && !(cmd_silent + msg_silent)
1581 && c != FAIL
1582 && !shortmess(SHM_SEARCHCOUNT)
1583 && msgbuf != NULL)
1584 search_stat(dirc, &pos, msgbuf);
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 /*
1587 * The search command can be followed by a ';' to do another search.
1588 * For example: "/pat/;/foo/+3;?bar"
1589 * This is like doing another search command, except:
1590 * - The remembered direction '/' or '?' is from the first search.
1591 * - When an error happens the cursor isn't moved at all.
1592 * Don't do this when called by get_address() (it handles ';' itself).
1593 */
1594 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1595 break;
1596
1597 dirc = *++pat;
1598 if (dirc != '?' && dirc != '/')
1599 {
1600 retval = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001601 emsg(_("E386: Expected '?' or '/' after ';'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 goto end_do_search;
1603 }
1604 ++pat;
1605 }
1606
1607 if (options & SEARCH_MARK)
1608 setpcmark();
1609 curwin->w_cursor = pos;
1610 curwin->w_set_curswant = TRUE;
1611
1612end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001613 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 spats[0].off = old_off;
1615 vim_free(strcopy);
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02001616 vim_free(msgbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618 return retval;
1619}
1620
1621#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1622/*
1623 * search_for_exact_line(buf, pos, dir, pat)
1624 *
1625 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001626 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001628 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 * Return OK for success, or FAIL if no line found.
1630 */
1631 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001632search_for_exact_line(
1633 buf_T *buf,
1634 pos_T *pos,
1635 int dir,
1636 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 linenr_T start = 0;
1639 char_u *ptr;
1640 char_u *p;
1641
1642 if (buf->b_ml.ml_line_count == 0)
1643 return FAIL;
1644 for (;;)
1645 {
1646 pos->lnum += dir;
1647 if (pos->lnum < 1)
1648 {
1649 if (p_ws)
1650 {
1651 pos->lnum = buf->b_ml.ml_line_count;
1652 if (!shortmess(SHM_SEARCH))
1653 give_warning((char_u *)_(top_bot_msg), TRUE);
1654 }
1655 else
1656 {
1657 pos->lnum = 1;
1658 break;
1659 }
1660 }
1661 else if (pos->lnum > buf->b_ml.ml_line_count)
1662 {
1663 if (p_ws)
1664 {
1665 pos->lnum = 1;
1666 if (!shortmess(SHM_SEARCH))
1667 give_warning((char_u *)_(bot_top_msg), TRUE);
1668 }
1669 else
1670 {
1671 pos->lnum = 1;
1672 break;
1673 }
1674 }
1675 if (pos->lnum == start)
1676 break;
1677 if (start == 0)
1678 start = pos->lnum;
1679 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1680 p = skipwhite(ptr);
1681 pos->col = (colnr_T) (p - ptr);
1682
1683 /* when adding lines the matching line may be empty but it is not
1684 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001685 if ((compl_cont_status & CONT_ADDING)
1686 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 {
1688 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1689 return OK;
1690 }
1691 else if (*p != NUL) /* ignore empty lines */
1692 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001693 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1694 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 return OK;
1696 }
1697 }
1698 return FAIL;
1699}
1700#endif /* FEAT_INS_EXPAND */
1701
1702/*
1703 * Character Searches
1704 */
1705
1706/*
1707 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1708 * position of the character, otherwise move to just before the char.
1709 * Do this "cap->count1" times.
1710 * Return FAIL or OK.
1711 */
1712 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001713searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 int c = cap->nchar; /* char to search for */
1716 int dir = cap->arg; /* TRUE for searching forward */
1717 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 int col;
1719 char_u *p;
1720 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001721 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
1723 if (c != NUL) /* normal search: remember args for repeat */
1724 {
1725 if (!KeyStuffed) /* don't remember when redoing */
1726 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001727 *lastc = c;
1728 set_csearch_direction(dir);
1729 set_csearch_until(t_cmd);
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001730 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 if (cap->ncharC1 != 0)
1732 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001733 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1734 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001736 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1737 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
1740 }
1741 else /* repeat previous search */
1742 {
Bram Moolenaar264b74f2019-01-24 17:18:42 +01001743 if (*lastc == NUL && lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 return FAIL;
1745 if (dir) /* repeat in opposite direction */
1746 dir = -lastcdir;
1747 else
1748 dir = lastcdir;
1749 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001750 c = *lastc;
1751 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001752
1753 /* Force a move of at least one char, so ";" and "," will move the
1754 * cursor, even if the cursor is right in front of char we are looking
1755 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001756 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001757 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 }
1759
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001760 if (dir == BACKWARD)
1761 cap->oap->inclusive = FALSE;
1762 else
1763 cap->oap->inclusive = TRUE;
1764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 p = ml_get_curline();
1766 col = curwin->w_cursor.col;
1767 len = (int)STRLEN(p);
1768
1769 while (count--)
1770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 if (has_mbyte)
1772 {
1773 for (;;)
1774 {
1775 if (dir > 0)
1776 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001777 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 if (col >= len)
1779 return FAIL;
1780 }
1781 else
1782 {
1783 if (col == 0)
1784 return FAIL;
1785 col -= (*mb_head_off)(p, p + col - 1) + 1;
1786 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001787 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001789 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 break;
1791 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001792 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001793 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001794 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001795 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 }
1797 }
1798 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
1800 for (;;)
1801 {
1802 if ((col += dir) < 0 || col >= len)
1803 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001804 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001806 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 }
1809 }
1810
1811 if (t_cmd)
1812 {
1813 /* backup to before the character (possibly double-byte) */
1814 col -= dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (has_mbyte)
1816 {
1817 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001818 /* Landed on the search char which is lastc_bytelen long */
1819 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 else
1821 /* To previous char, which may be multi-byte. */
1822 col -= (*mb_head_off)(p, p + col);
1823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 }
1825 curwin->w_cursor.col = col;
1826
1827 return OK;
1828}
1829
1830/*
1831 * "Other" Searches
1832 */
1833
1834/*
1835 * findmatch - find the matching paren or brace
1836 *
1837 * Improvement over vi: Braces inside quotes are ignored.
1838 */
1839 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001840findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
1842 return findmatchlimit(oap, initc, 0, 0);
1843}
1844
1845/*
1846 * Return TRUE if the character before "linep[col]" equals "ch".
1847 * Return FALSE if "col" is zero.
1848 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1849 * is NULL.
1850 * Handles multibyte string correctly.
1851 */
1852 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001853check_prevcol(
1854 char_u *linep,
1855 int col,
1856 int ch,
1857 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858{
1859 --col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (col > 0 && has_mbyte)
1861 col -= (*mb_head_off)(linep, linep + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if (prevcol)
1863 *prevcol = col;
1864 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1865}
1866
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001867/*
1868 * Raw string start is found at linep[startpos.col - 1].
1869 * Return TRUE if the matching end can be found between startpos and endpos.
1870 */
1871 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001872find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001873{
1874 char_u *p;
1875 char_u *delim_copy;
1876 size_t delim_len;
1877 linenr_T lnum;
1878 int found = FALSE;
1879
1880 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1881 ;
1882 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001883 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001884 if (delim_copy == NULL)
1885 return FALSE;
1886 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1887 {
1888 char_u *line = ml_get(lnum);
1889
1890 for (p = line + (lnum == startpos->lnum
1891 ? startpos->col + 1 : 0); *p; ++p)
1892 {
1893 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1894 break;
1895 if (*p == ')' && p[delim_len + 1] == '"'
1896 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1897 {
1898 found = TRUE;
1899 break;
1900 }
1901 }
1902 if (found)
1903 break;
1904 }
1905 vim_free(delim_copy);
1906 return found;
1907}
1908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909/*
1910 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001911 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1912 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 *
1914 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001915 * character at or after the cursor. Special values:
1916 * '*' look for C-style comment / *
1917 * '/' look for C-style comment / *, ignoring comment-end
1918 * '#' look for preprocessor directives
1919 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 *
1921 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1922 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1923 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1924 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001925 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001926 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001927 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 */
1929
1930 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001931findmatchlimit(
1932 oparg_T *oap,
1933 int initc,
1934 int flags,
1935 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936{
1937 static pos_T pos; /* current search position */
1938 int findc = 0; /* matching brace */
1939 int c;
1940 int count = 0; /* cumulative number of braces */
1941 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001942 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 int inquote = FALSE; /* TRUE when inside quotes */
1944 char_u *linep; /* pointer to current line */
1945 char_u *ptr;
1946 int do_quotes; /* check for quotes in current line */
1947 int at_start; /* do_quotes value at start position */
1948 int hash_dir = 0; /* Direction searched for # things */
1949 int comment_dir = 0; /* Direction searched for comments */
1950 pos_T match_pos; /* Where last slash-star was found */
1951 int start_in_quotes; /* start position is in quotes */
1952 int traveled = 0; /* how far we've searched so far */
1953 int ignore_cend = FALSE; /* ignore comment end */
1954 int cpo_match; /* vi compatible matching */
1955 int cpo_bsl; /* don't recognize backslashes */
1956 int match_escaped = 0; /* search for escaped match */
1957 int dir; /* Direction to search */
1958 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001959#ifdef FEAT_LISP
1960 int lispcomm = FALSE; /* inside of Lisp-style comment */
1961 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001965 pos.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 linep = ml_get(pos.lnum);
1967
1968 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1969 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1970
1971 /* Direction to search when initc is '/', '*' or '#' */
1972 if (flags & FM_BACKWARD)
1973 dir = BACKWARD;
1974 else if (flags & FM_FORWARD)
1975 dir = FORWARD;
1976 else
1977 dir = 0;
1978
1979 /*
1980 * if initc given, look in the table for the matching character
1981 * '/' and '*' are special cases: look for start or end of comment.
1982 * When '/' is used, we ignore running backwards into an star-slash, for
1983 * "[*" command, we just want to find any comment.
1984 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001985 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 comment_dir = dir;
1988 if (initc == '/')
1989 ignore_cend = TRUE;
1990 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001991 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 initc = NUL;
1993 }
1994 else if (initc != '#' && initc != NUL)
1995 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001996 find_mps_values(&initc, &findc, &backwards, TRUE);
1997 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 return NULL;
1999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 else
2001 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002002 /*
2003 * Either initc is '#', or no initc was given and we need to look
2004 * under the cursor.
2005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 if (initc == '#')
2007 {
2008 hash_dir = dir;
2009 }
2010 else
2011 {
2012 /*
2013 * initc was not given, must look for something to match under
2014 * or near the cursor.
2015 * Only check for special things when 'cpo' doesn't have '%'.
2016 */
2017 if (!cpo_match)
2018 {
2019 /* Are we before or at #if, #else etc.? */
2020 ptr = skipwhite(linep);
2021 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2022 {
2023 ptr = skipwhite(ptr + 1);
2024 if ( STRNCMP(ptr, "if", 2) == 0
2025 || STRNCMP(ptr, "endif", 5) == 0
2026 || STRNCMP(ptr, "el", 2) == 0)
2027 hash_dir = 1;
2028 }
2029
2030 /* Are we on a comment? */
2031 else if (linep[pos.col] == '/')
2032 {
2033 if (linep[pos.col + 1] == '*')
2034 {
2035 comment_dir = FORWARD;
2036 backwards = FALSE;
2037 pos.col++;
2038 }
2039 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2040 {
2041 comment_dir = BACKWARD;
2042 backwards = TRUE;
2043 pos.col--;
2044 }
2045 }
2046 else if (linep[pos.col] == '*')
2047 {
2048 if (linep[pos.col + 1] == '/')
2049 {
2050 comment_dir = BACKWARD;
2051 backwards = TRUE;
2052 }
2053 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2054 {
2055 comment_dir = FORWARD;
2056 backwards = FALSE;
2057 }
2058 }
2059 }
2060
2061 /*
2062 * If we are not on a comment or the # at the start of a line, then
2063 * look for brace anywhere on this line after the cursor.
2064 */
2065 if (!hash_dir && !comment_dir)
2066 {
2067 /*
2068 * Find the brace under or after the cursor.
2069 * If beyond the end of the line, use the last character in
2070 * the line.
2071 */
2072 if (linep[pos.col] == NUL && pos.col)
2073 --pos.col;
2074 for (;;)
2075 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002076 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 if (initc == NUL)
2078 break;
2079
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002080 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (findc)
2082 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002083 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 }
2085 if (!findc)
2086 {
2087 /* no brace in the line, maybe use " #if" then */
2088 if (!cpo_match && *skipwhite(linep) == '#')
2089 hash_dir = 1;
2090 else
2091 return NULL;
2092 }
2093 else if (!cpo_bsl)
2094 {
2095 int col, bslcnt = 0;
2096
2097 /* Set "match_escaped" if there are an odd number of
2098 * backslashes. */
2099 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2100 bslcnt++;
2101 match_escaped = (bslcnt & 1);
2102 }
2103 }
2104 }
2105 if (hash_dir)
2106 {
2107 /*
2108 * Look for matching #if, #else, #elif, or #endif
2109 */
2110 if (oap != NULL)
2111 oap->motion_type = MLINE; /* Linewise for this case only */
2112 if (initc != '#')
2113 {
2114 ptr = skipwhite(skipwhite(linep) + 1);
2115 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2116 hash_dir = 1;
2117 else if (STRNCMP(ptr, "endif", 5) == 0)
2118 hash_dir = -1;
2119 else
2120 return NULL;
2121 }
2122 pos.col = 0;
2123 while (!got_int)
2124 {
2125 if (hash_dir > 0)
2126 {
2127 if (pos.lnum == curbuf->b_ml.ml_line_count)
2128 break;
2129 }
2130 else if (pos.lnum == 1)
2131 break;
2132 pos.lnum += hash_dir;
2133 linep = ml_get(pos.lnum);
2134 line_breakcheck(); /* check for CTRL-C typed */
2135 ptr = skipwhite(linep);
2136 if (*ptr != '#')
2137 continue;
2138 pos.col = (colnr_T) (ptr - linep);
2139 ptr = skipwhite(ptr + 1);
2140 if (hash_dir > 0)
2141 {
2142 if (STRNCMP(ptr, "if", 2) == 0)
2143 count++;
2144 else if (STRNCMP(ptr, "el", 2) == 0)
2145 {
2146 if (count == 0)
2147 return &pos;
2148 }
2149 else if (STRNCMP(ptr, "endif", 5) == 0)
2150 {
2151 if (count == 0)
2152 return &pos;
2153 count--;
2154 }
2155 }
2156 else
2157 {
2158 if (STRNCMP(ptr, "if", 2) == 0)
2159 {
2160 if (count == 0)
2161 return &pos;
2162 count--;
2163 }
2164 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2165 {
2166 if (count == 0)
2167 return &pos;
2168 }
2169 else if (STRNCMP(ptr, "endif", 5) == 0)
2170 count++;
2171 }
2172 }
2173 return NULL;
2174 }
2175 }
2176
2177#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002178 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 * paren/brace in the other direction. */
2180 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2181 backwards = !backwards;
2182#endif
2183
2184 do_quotes = -1;
2185 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002186 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002189 if ((backwards && comment_dir)
2190#ifdef FEAT_LISP
2191 || lisp
2192#endif
2193 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002195#ifdef FEAT_LISP
2196 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2197 lispcomm = TRUE; /* find match inside this comment */
2198#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 while (!got_int)
2200 {
2201 /*
2202 * Go to the next position, forward or backward. We could use
2203 * inc() and dec() here, but that is much slower
2204 */
2205 if (backwards)
2206 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002207#ifdef FEAT_LISP
2208 /* char to match is inside of comment, don't search outside */
2209 if (lispcomm && pos.col < (colnr_T)comment_col)
2210 break;
2211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (pos.col == 0) /* at start of line, go to prev. one */
2213 {
2214 if (pos.lnum == 1) /* start of file */
2215 break;
2216 --pos.lnum;
2217
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002218 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 break;
2220
2221 linep = ml_get(pos.lnum);
2222 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2223 do_quotes = -1;
2224 line_breakcheck();
2225
2226 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002227 if (comment_dir
2228#ifdef FEAT_LISP
2229 || lisp
2230#endif
2231 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002233#ifdef FEAT_LISP
2234 /* skip comment */
2235 if (lisp && comment_col != MAXCOL)
2236 pos.col = comment_col;
2237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 }
2239 else
2240 {
2241 --pos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (has_mbyte)
2243 pos.col -= (*mb_head_off)(linep, linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245 }
2246 else /* forward search */
2247 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002248 if (linep[pos.col] == NUL
2249 /* at end of line, go to next one */
2250#ifdef FEAT_LISP
2251 /* don't search for match in comment */
2252 || (lisp && comment_col != MAXCOL
2253 && pos.col == (colnr_T)comment_col)
2254#endif
2255 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002257 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2258#ifdef FEAT_LISP
2259 /* line is exhausted and comment with it,
2260 * don't search for match in code */
2261 || lispcomm
2262#endif
2263 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 break;
2265 ++pos.lnum;
2266
2267 if (maxtravel && traveled++ > maxtravel)
2268 break;
2269
2270 linep = ml_get(pos.lnum);
2271 pos.col = 0;
2272 do_quotes = -1;
2273 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002274#ifdef FEAT_LISP
2275 if (lisp) /* find comment pos in new line */
2276 comment_col = check_linecomment(linep);
2277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279 else
2280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002282 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 ++pos.col;
2285 }
2286 }
2287
2288 /*
2289 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2290 */
2291 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2292 (linep[0] == '{' || linep[0] == '}'))
2293 {
2294 if (linep[0] == findc && count == 0) /* match! */
2295 return &pos;
2296 break; /* out of scope */
2297 }
2298
2299 if (comment_dir)
2300 {
2301 /* Note: comments do not nest, and we ignore quotes in them */
2302 /* TODO: ignore comment brackets inside strings */
2303 if (comment_dir == FORWARD)
2304 {
2305 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2306 {
2307 pos.col++;
2308 return &pos;
2309 }
2310 }
2311 else /* Searching backwards */
2312 {
2313 /*
2314 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002315 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 */
2317 if (pos.col == 0)
2318 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002319 else if (raw_string)
2320 {
2321 if (linep[pos.col - 1] == 'R'
2322 && linep[pos.col] == '"'
2323 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2324 {
2325 /* Possible start of raw string. Now that we have the
2326 * delimiter we can check if it ends before where we
2327 * started searching, or before the previously found
2328 * raw string start. */
2329 if (!find_rawstring_end(linep, &pos,
2330 count > 0 ? &match_pos : &curwin->w_cursor))
2331 {
2332 count++;
2333 match_pos = pos;
2334 match_pos.col--;
2335 }
2336 linep = ml_get(pos.lnum); /* may have been released */
2337 }
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 else if ( linep[pos.col - 1] == '/'
2340 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002341 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 && (int)pos.col < comment_col)
2343 {
2344 count++;
2345 match_pos = pos;
2346 match_pos.col--;
2347 }
2348 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2349 {
2350 if (count > 0)
2351 pos = match_pos;
2352 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2353 && (int)pos.col <= comment_col)
2354 pos.col -= 2;
2355 else if (ignore_cend)
2356 continue;
2357 else
2358 return NULL;
2359 return &pos;
2360 }
2361 }
2362 continue;
2363 }
2364
2365 /*
2366 * If smart matching ('cpoptions' does not contain '%'), braces inside
2367 * of quotes are ignored, but only if there is an even number of
2368 * quotes in the line.
2369 */
2370 if (cpo_match)
2371 do_quotes = 0;
2372 else if (do_quotes == -1)
2373 {
2374 /*
2375 * Count the number of quotes in the line, skipping \" and '"'.
2376 * Watch out for "\\".
2377 */
2378 at_start = do_quotes;
2379 for (ptr = linep; *ptr; ++ptr)
2380 {
2381 if (ptr == linep + pos.col + backwards)
2382 at_start = (do_quotes & 1);
2383 if (*ptr == '"'
2384 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2385 ++do_quotes;
2386 if (*ptr == '\\' && ptr[1] != NUL)
2387 ++ptr;
2388 }
2389 do_quotes &= 1; /* result is 1 with even number of quotes */
2390
2391 /*
2392 * If we find an uneven count, check current line and previous
2393 * one for a '\' at the end.
2394 */
2395 if (!do_quotes)
2396 {
2397 inquote = FALSE;
2398 if (ptr[-1] == '\\')
2399 {
2400 do_quotes = 1;
2401 if (start_in_quotes == MAYBE)
2402 {
2403 /* Do we need to use at_start here? */
2404 inquote = TRUE;
2405 start_in_quotes = TRUE;
2406 }
2407 else if (backwards)
2408 inquote = TRUE;
2409 }
2410 if (pos.lnum > 1)
2411 {
2412 ptr = ml_get(pos.lnum - 1);
2413 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2414 {
2415 do_quotes = 1;
2416 if (start_in_quotes == MAYBE)
2417 {
2418 inquote = at_start;
2419 if (inquote)
2420 start_in_quotes = TRUE;
2421 }
2422 else if (!backwards)
2423 inquote = TRUE;
2424 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002425
2426 /* ml_get() only keeps one line, need to get linep again */
2427 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 }
2430 }
2431 if (start_in_quotes == MAYBE)
2432 start_in_quotes = FALSE;
2433
2434 /*
2435 * If 'smartmatch' is set:
2436 * Things inside quotes are ignored by setting 'inquote'. If we
2437 * find a quote without a preceding '\' invert 'inquote'. At the
2438 * end of a line not ending in '\' we reset 'inquote'.
2439 *
2440 * In lines with an uneven number of quotes (without preceding '\')
2441 * we do not know which part to ignore. Therefore we only set
2442 * inquote if the number of quotes in a line is even, unless this
2443 * line or the previous one ends in a '\'. Complicated, isn't it?
2444 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002445 c = PTR2CHAR(linep + pos.col);
2446 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 case NUL:
2449 /* at end of line without trailing backslash, reset inquote */
2450 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2451 {
2452 inquote = FALSE;
2453 start_in_quotes = FALSE;
2454 }
2455 break;
2456
2457 case '"':
2458 /* a quote that is preceded with an odd number of backslashes is
2459 * ignored */
2460 if (do_quotes)
2461 {
2462 int col;
2463
2464 for (col = pos.col - 1; col >= 0; --col)
2465 if (linep[col] != '\\')
2466 break;
2467 if ((((int)pos.col - 1 - col) & 1) == 0)
2468 {
2469 inquote = !inquote;
2470 start_in_quotes = FALSE;
2471 }
2472 }
2473 break;
2474
2475 /*
2476 * If smart matching ('cpoptions' does not contain '%'):
2477 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2478 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2479 * skipped, there is never a brace in them.
2480 * Ignore this when finding matches for `'.
2481 */
2482 case '\'':
2483 if (!cpo_match && initc != '\'' && findc != '\'')
2484 {
2485 if (backwards)
2486 {
2487 if (pos.col > 1)
2488 {
2489 if (linep[pos.col - 2] == '\'')
2490 {
2491 pos.col -= 2;
2492 break;
2493 }
2494 else if (linep[pos.col - 2] == '\\' &&
2495 pos.col > 2 && linep[pos.col - 3] == '\'')
2496 {
2497 pos.col -= 3;
2498 break;
2499 }
2500 }
2501 }
2502 else if (linep[pos.col + 1]) /* forward search */
2503 {
2504 if (linep[pos.col + 1] == '\\' &&
2505 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2506 {
2507 pos.col += 3;
2508 break;
2509 }
2510 else if (linep[pos.col + 2] == '\'')
2511 {
2512 pos.col += 2;
2513 break;
2514 }
2515 }
2516 }
2517 /* FALLTHROUGH */
2518
2519 default:
2520#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002521 /*
2522 * For Lisp skip over backslashed (), {} and [].
2523 * (actually, we skip #\( et al)
2524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (curbuf->b_p_lisp
2526 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002527 && pos.col > 1
2528 && check_prevcol(linep, pos.col, '\\', NULL)
2529 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 break;
2531#endif
2532
2533 /* Check for match outside of quotes, and inside of
2534 * quotes when the start is also inside of quotes. */
2535 if ((!inquote || start_in_quotes == TRUE)
2536 && (c == initc || c == findc))
2537 {
2538 int col, bslcnt = 0;
2539
2540 if (!cpo_bsl)
2541 {
2542 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2543 bslcnt++;
2544 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002545 /* Only accept a match when 'M' is in 'cpo' or when escaping
2546 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2548 {
2549 if (c == initc)
2550 count++;
2551 else
2552 {
2553 if (count == 0)
2554 return &pos;
2555 count--;
2556 }
2557 }
2558 }
2559 }
2560 }
2561
2562 if (comment_dir == BACKWARD && count > 0)
2563 {
2564 pos = match_pos;
2565 return &pos;
2566 }
2567 return (pos_T *)NULL; /* never found it */
2568}
2569
2570/*
2571 * Check if line[] contains a / / comment.
2572 * Return MAXCOL if not, otherwise return the column.
2573 * TODO: skip strings.
2574 */
2575 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002576check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578 char_u *p;
2579
2580 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002581#ifdef FEAT_LISP
2582 /* skip Lispish one-line comments */
2583 if (curbuf->b_p_lisp)
2584 {
2585 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2586 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002587 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002588
2589 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002590 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002591 {
2592 if (*p == '"')
2593 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002594 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002595 {
2596 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002597 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002598 }
2599 else if (p == line || ((p - line) >= 2
2600 /* skip #\" form */
2601 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002602 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002603 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002604 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002605 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2606 break; /* found! */
2607 ++p;
2608 }
2609 }
2610 else
2611 p = NULL;
2612 }
2613 else
2614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 while ((p = vim_strchr(p, '/')) != NULL)
2616 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002617 /* accept a double /, unless it's preceded with * and followed by *,
2618 * because * / / * is an end and start of a C comment */
2619 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 break;
2621 ++p;
2622 }
2623
2624 if (p == NULL)
2625 return MAXCOL;
2626 return (int)(p - line);
2627}
2628
2629/*
2630 * Move cursor briefly to character matching the one under the cursor.
2631 * Used for Insert mode and "r" command.
2632 * Show the match only if it is visible on the screen.
2633 * If there isn't a match, then beep.
2634 */
2635 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002636showmatch(
2637 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
2639 pos_T *lpos, save_cursor;
2640 pos_T mpos;
2641 colnr_T vcol;
2642 long save_so;
2643 long save_siso;
2644#ifdef CURSOR_SHAPE
2645 int save_state;
2646#endif
2647 colnr_T save_dollar_vcol;
2648 char_u *p;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002649 long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so;
2650 long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 /*
2653 * Only show match for chars in the 'matchpairs' option.
2654 */
2655 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002656 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002659 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002660 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002661#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002662 p += MB_PTR2LEN(p) + 1;
2663 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#ifdef FEAT_RIGHTLEFT
2665 && !(curwin->w_p_rl ^ p_ri)
2666#endif
2667 )
2668 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002669 p += MB_PTR2LEN(p);
2670 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 return;
2672 }
2673
2674 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002675 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002676 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
2678 if (!curwin->w_p_wrap)
2679 getvcol(curwin, lpos, NULL, &vcol, NULL);
2680 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002681 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {
2683 mpos = *lpos; /* save the pos, update_screen() may change it */
2684 save_cursor = curwin->w_cursor;
Bram Moolenaar375e3392019-01-31 18:26:10 +01002685 save_so = *so;
2686 save_siso = *siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2688 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002689 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2690 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 ++curwin->w_virtcol; /* do display ')' just before "$" */
2692 update_screen(VALID); /* show the new char first */
2693
2694 save_dollar_vcol = dollar_vcol;
2695#ifdef CURSOR_SHAPE
2696 save_state = State;
2697 State = SHOWMATCH;
2698 ui_cursor_shape(); /* may show different cursor shape */
2699#endif
2700 curwin->w_cursor = mpos; /* move to matching char */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002701 *so = 0; /* don't use 'scrolloff' here */
2702 *siso = 0; /* don't use 'sidescrolloff' here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 showruler(FALSE);
2704 setcursor();
2705 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002706 out_flush_cursor(TRUE, FALSE);
2707
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2709 * which resets it if the matching position is in a previous line
2710 * and has a higher column number. */
2711 dollar_vcol = save_dollar_vcol;
2712
2713 /*
2714 * brief pause, unless 'm' is present in 'cpo' and a character is
2715 * available.
2716 */
2717 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2718 ui_delay(p_mat * 100L, TRUE);
2719 else if (!char_avail())
2720 ui_delay(p_mat * 100L, FALSE);
2721 curwin->w_cursor = save_cursor; /* restore cursor position */
Bram Moolenaar375e3392019-01-31 18:26:10 +01002722 *so = save_so;
2723 *siso = save_siso;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724#ifdef CURSOR_SHAPE
2725 State = save_state;
2726 ui_cursor_shape(); /* may show different cursor shape */
2727#endif
2728 }
2729 }
2730}
2731
2732/*
Bram Moolenaar85160712018-06-19 18:27:41 +02002733 * Find the start of the next sentence, searching in the direction specified
2734 * by the "dir" argument. The cursor is positioned on the start of the next
2735 * sentence when found. If the next sentence is found, return OK. Return FAIL
2736 * otherwise. See ":h sentence" for the precise definition of a "sentence"
2737 * text object.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 */
2739 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002740findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 pos_T pos, tpos;
2743 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002744 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 int startlnum;
2746 int noskip = FALSE; /* do not skip blanks */
2747 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002748 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
2750 pos = curwin->w_cursor;
2751 if (dir == FORWARD)
2752 func = incl;
2753 else
2754 func = decl;
2755
2756 while (count--)
2757 {
2758 /*
2759 * if on an empty line, skip upto a non-empty line
2760 */
2761 if (gchar_pos(&pos) == NUL)
2762 {
2763 do
2764 if ((*func)(&pos) == -1)
2765 break;
2766 while (gchar_pos(&pos) == NUL);
2767 if (dir == FORWARD)
2768 goto found;
2769 }
2770 /*
2771 * if on the start of a paragraph or a section and searching forward,
2772 * go to the next line
2773 */
2774 else if (dir == FORWARD && pos.col == 0 &&
2775 startPS(pos.lnum, NUL, FALSE))
2776 {
2777 if (pos.lnum == curbuf->b_ml.ml_line_count)
2778 return FAIL;
2779 ++pos.lnum;
2780 goto found;
2781 }
2782 else if (dir == BACKWARD)
2783 decl(&pos);
2784
Bram Moolenaar85160712018-06-19 18:27:41 +02002785 // go back to the previous non-white non-punctuation character
Bram Moolenaardef9e822004-12-31 20:58:58 +00002786 found_dot = FALSE;
Bram Moolenaar85160712018-06-19 18:27:41 +02002787 while (c = gchar_pos(&pos), VIM_ISWHITE(c)
2788 || vim_strchr((char_u *)".!?)]\"'", c) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
Bram Moolenaar85160712018-06-19 18:27:41 +02002790 tpos = pos;
2791 if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 break;
Bram Moolenaar85160712018-06-19 18:27:41 +02002793
2794 if (found_dot)
2795 break;
2796 if (vim_strchr((char_u *) ".!?", c) != NULL)
2797 found_dot = TRUE;
2798
2799 if (vim_strchr((char_u *) ")]\"'", c) != NULL
2800 && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
2801 break;
2802
2803 decl(&pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 }
2805
2806 /* remember the line where the search started */
2807 startlnum = pos.lnum;
2808 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2809
2810 for (;;) /* find end of sentence */
2811 {
2812 c = gchar_pos(&pos);
2813 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2814 {
2815 if (dir == BACKWARD && pos.lnum != startlnum)
2816 ++pos.lnum;
2817 break;
2818 }
2819 if (c == '.' || c == '!' || c == '?')
2820 {
2821 tpos = pos;
2822 do
2823 if ((c = inc(&tpos)) == -1)
2824 break;
2825 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2826 != NULL);
2827 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2828 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2829 && gchar_pos(&tpos) == ' ')))
2830 {
2831 pos = tpos;
2832 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2833 inc(&pos);
2834 break;
2835 }
2836 }
2837 if ((*func)(&pos) == -1)
2838 {
2839 if (count)
2840 return FAIL;
2841 noskip = TRUE;
2842 break;
2843 }
2844 }
2845found:
2846 /* skip white space */
2847 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2848 if (incl(&pos) == -1)
2849 break;
2850 }
2851
2852 setpcmark();
2853 curwin->w_cursor = pos;
2854 return OK;
2855}
2856
2857/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002858 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002860 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 * If 'what' is '{' or '}' we go to the next section.
2862 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002863 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 */
2865 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002866findpar(
2867 int *pincl, /* Return: TRUE if last char is to be included */
2868 int dir,
2869 long count,
2870 int what,
2871 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 linenr_T curr;
2874 int did_skip; /* TRUE after separating lines have been skipped */
2875 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002876 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877#ifdef FEAT_FOLDING
2878 linenr_T fold_first; /* first line of a closed fold */
2879 linenr_T fold_last; /* last line of a closed fold */
2880 int fold_skipped; /* TRUE if a closed fold was skipped this
2881 iteration */
2882#endif
2883
2884 curr = curwin->w_cursor.lnum;
2885
2886 while (count--)
2887 {
2888 did_skip = FALSE;
2889 for (first = TRUE; ; first = FALSE)
2890 {
2891 if (*ml_get(curr) != NUL)
2892 did_skip = TRUE;
2893
2894#ifdef FEAT_FOLDING
2895 /* skip folded lines */
2896 fold_skipped = FALSE;
2897 if (first && hasFolding(curr, &fold_first, &fold_last))
2898 {
2899 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2900 fold_skipped = TRUE;
2901 }
2902#endif
2903
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002904 /* POSIX has its own ideas of what a paragraph boundary is and it
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002905 * doesn't match historical Vi: It also stops at a "{" in the
2906 * first column and at an empty line. */
2907 if (!first && did_skip && (startPS(curr, what, both)
2908 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 break;
2910
2911#ifdef FEAT_FOLDING
2912 if (fold_skipped)
2913 curr -= dir;
2914#endif
2915 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2916 {
2917 if (count)
2918 return FALSE;
2919 curr -= dir;
2920 break;
2921 }
2922 }
2923 }
2924 setpcmark();
2925 if (both && *ml_get(curr) == '}') /* include line with '}' */
2926 ++curr;
2927 curwin->w_cursor.lnum = curr;
2928 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2929 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002930 char_u *line = ml_get(curr);
2931
2932 /* Put the cursor on the last character in the last line and make the
2933 * motion inclusive. */
2934 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 {
2936 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002937 curwin->w_cursor.col -=
2938 (*mb_head_off)(line, line + curwin->w_cursor.col);
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002939 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941 }
2942 else
2943 curwin->w_cursor.col = 0;
2944 return TRUE;
2945}
2946
2947/*
2948 * check if the string 's' is a nroff macro that is in option 'opt'
2949 */
2950 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002951inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 char_u *macro;
2954
2955 for (macro = opt; macro[0]; ++macro)
2956 {
2957 /* Accept two characters in the option being equal to two characters
2958 * in the line. A space in the option matches with a space in the
2959 * line or the line having ended. */
2960 if ( (macro[0] == s[0]
2961 || (macro[0] == ' '
2962 && (s[0] == NUL || s[0] == ' ')))
2963 && (macro[1] == s[1]
2964 || ((macro[1] == NUL || macro[1] == ' ')
2965 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2966 break;
2967 ++macro;
2968 if (macro[0] == NUL)
2969 break;
2970 }
2971 return (macro[0] != NUL);
2972}
2973
2974/*
2975 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2976 * If 'para' is '{' or '}' only check for sections.
2977 * If 'both' is TRUE also stop at '}'
2978 */
2979 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002980startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 char_u *s;
2983
2984 s = ml_get(lnum);
2985 if (*s == para || *s == '\f' || (both && *s == '}'))
2986 return TRUE;
2987 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2988 (!para && inmacro(p_para, s + 1))))
2989 return TRUE;
2990 return FALSE;
2991}
2992
2993/*
2994 * The following routines do the word searches performed by the 'w', 'W',
2995 * 'b', 'B', 'e', and 'E' commands.
2996 */
2997
2998/*
2999 * To perform these searches, characters are placed into one of three
3000 * classes, and transitions between classes determine word boundaries.
3001 *
3002 * The classes are:
3003 *
3004 * 0 - white space
3005 * 1 - punctuation
3006 * 2 or higher - keyword characters (letters, digits and underscore)
3007 */
3008
3009static int cls_bigword; /* TRUE for "W", "B" or "E" */
3010
3011/*
3012 * cls() - returns the class of character at curwin->w_cursor
3013 *
3014 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
3015 * from class 2 and higher are reported as class 1 since only white space
3016 * boundaries are of interest.
3017 */
3018 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003019cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020{
3021 int c;
3022
3023 c = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (c == ' ' || c == '\t' || c == NUL)
3025 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (enc_dbcs != 0 && c > 0xFF)
3027 {
3028 /* If cls_bigword, report multi-byte chars as class 1. */
3029 if (enc_dbcs == DBCS_KOR && cls_bigword)
3030 return 1;
3031
3032 /* process code leading/trailing bytes */
3033 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3034 }
3035 if (enc_utf8)
3036 {
3037 c = utf_class(c);
3038 if (c != 0 && cls_bigword)
3039 return 1;
3040 return c;
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3044 if (cls_bigword)
3045 return 1;
3046
3047 if (vim_iswordc(c))
3048 return 2;
3049 return 1;
3050}
3051
3052
3053/*
3054 * fwd_word(count, type, eol) - move forward one word
3055 *
3056 * Returns FAIL if the cursor was already at the end of the file.
3057 * If eol is TRUE, last word stops at end of line (for operators).
3058 */
3059 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003060fwd_word(
3061 long count,
3062 int bigword, /* "W", "E" or "B" */
3063 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
3065 int sclass; /* starting class */
3066 int i;
3067 int last_line;
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 cls_bigword = bigword;
3071 while (--count >= 0)
3072 {
3073#ifdef FEAT_FOLDING
3074 /* When inside a range of folded lines, move to the last char of the
3075 * last line. */
3076 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3077 coladvance((colnr_T)MAXCOL);
3078#endif
3079 sclass = cls();
3080
3081 /*
3082 * We always move at least one character, unless on the last
3083 * character in the buffer.
3084 */
3085 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3086 i = inc_cursor();
3087 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3088 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003089 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 return OK;
3091
3092 /*
3093 * Go one char past end of current word (if any)
3094 */
3095 if (sclass != 0)
3096 while (cls() == sclass)
3097 {
3098 i = inc_cursor();
3099 if (i == -1 || (i >= 1 && eol && count == 0))
3100 return OK;
3101 }
3102
3103 /*
3104 * go to next non-white
3105 */
3106 while (cls() == 0)
3107 {
3108 /*
3109 * We'll stop if we land on a blank line
3110 */
3111 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3112 break;
3113
3114 i = inc_cursor();
3115 if (i == -1 || (i >= 1 && eol && count == 0))
3116 return OK;
3117 }
3118 }
3119 return OK;
3120}
3121
3122/*
3123 * bck_word() - move backward 'count' words
3124 *
3125 * If stop is TRUE and we are already on the start of a word, move one less.
3126 *
3127 * Returns FAIL if top of the file was reached.
3128 */
3129 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003130bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 int sclass; /* starting class */
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 cls_bigword = bigword;
3136 while (--count >= 0)
3137 {
3138#ifdef FEAT_FOLDING
3139 /* When inside a range of folded lines, move to the first char of the
3140 * first line. */
3141 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3142 curwin->w_cursor.col = 0;
3143#endif
3144 sclass = cls();
3145 if (dec_cursor() == -1) /* started at start of file */
3146 return FAIL;
3147
3148 if (!stop || sclass == cls() || sclass == 0)
3149 {
3150 /*
3151 * Skip white space before the word.
3152 * Stop on an empty line.
3153 */
3154 while (cls() == 0)
3155 {
3156 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003157 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 goto finished;
3159 if (dec_cursor() == -1) /* hit start of file, stop here */
3160 return OK;
3161 }
3162
3163 /*
3164 * Move backward to start of this word.
3165 */
3166 if (skip_chars(cls(), BACKWARD))
3167 return OK;
3168 }
3169
3170 inc_cursor(); /* overshot - forward one */
3171finished:
3172 stop = FALSE;
3173 }
3174 return OK;
3175}
3176
3177/*
3178 * end_word() - move to the end of the word
3179 *
3180 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3181 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3182 * motion crosses blank lines. When the real vi crosses a blank line in an
3183 * 'e' motion, the cursor is placed on the FIRST character of the next
3184 * non-blank line. The 'E' command, however, works correctly. Since this
3185 * appears to be a bug, I have not duplicated it here.
3186 *
3187 * Returns FAIL if end of the file was reached.
3188 *
3189 * If stop is TRUE and we are already on the end of a word, move one less.
3190 * If empty is TRUE stop on an empty line.
3191 */
3192 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003193end_word(
3194 long count,
3195 int bigword,
3196 int stop,
3197 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 int sclass; /* starting class */
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 cls_bigword = bigword;
3203 while (--count >= 0)
3204 {
3205#ifdef FEAT_FOLDING
3206 /* When inside a range of folded lines, move to the last char of the
3207 * last line. */
3208 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3209 coladvance((colnr_T)MAXCOL);
3210#endif
3211 sclass = cls();
3212 if (inc_cursor() == -1)
3213 return FAIL;
3214
3215 /*
3216 * If we're in the middle of a word, we just have to move to the end
3217 * of it.
3218 */
3219 if (cls() == sclass && sclass != 0)
3220 {
3221 /*
3222 * Move forward to end of the current word
3223 */
3224 if (skip_chars(sclass, FORWARD))
3225 return FAIL;
3226 }
3227 else if (!stop || sclass == 0)
3228 {
3229 /*
3230 * We were at the end of a word. Go to the end of the next word.
3231 * First skip white space, if 'empty' is TRUE, stop at empty line.
3232 */
3233 while (cls() == 0)
3234 {
3235 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003236 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 goto finished;
3238 if (inc_cursor() == -1) /* hit end of file, stop here */
3239 return FAIL;
3240 }
3241
3242 /*
3243 * Move forward to the end of this word.
3244 */
3245 if (skip_chars(cls(), FORWARD))
3246 return FAIL;
3247 }
3248 dec_cursor(); /* overshot - one char backward */
3249finished:
3250 stop = FALSE; /* we move only one word less */
3251 }
3252 return OK;
3253}
3254
3255/*
3256 * Move back to the end of the word.
3257 *
3258 * Returns FAIL if start of the file was reached.
3259 */
3260 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003261bckend_word(
3262 long count,
3263 int bigword, /* TRUE for "B" */
3264 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 int sclass; /* starting class */
3267 int i;
3268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 cls_bigword = bigword;
3271 while (--count >= 0)
3272 {
3273 sclass = cls();
3274 if ((i = dec_cursor()) == -1)
3275 return FAIL;
3276 if (eol && i == 1)
3277 return OK;
3278
3279 /*
3280 * Move backward to before the start of this word.
3281 */
3282 if (sclass != 0)
3283 {
3284 while (cls() == sclass)
3285 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3286 return OK;
3287 }
3288
3289 /*
3290 * Move backward to end of the previous word
3291 */
3292 while (cls() == 0)
3293 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003294 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 break;
3296 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3297 return OK;
3298 }
3299 }
3300 return OK;
3301}
3302
3303/*
3304 * Skip a row of characters of the same class.
3305 * Return TRUE when end-of-file reached, FALSE otherwise.
3306 */
3307 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003308skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 while (cls() == cclass)
3311 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3312 return TRUE;
3313 return FALSE;
3314}
3315
3316#ifdef FEAT_TEXTOBJ
3317/*
3318 * Go back to the start of the word or the start of white space
3319 */
3320 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003321back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 int sclass; /* starting class */
3324
3325 sclass = cls();
3326 for (;;)
3327 {
3328 if (curwin->w_cursor.col == 0) /* stop at start of line */
3329 break;
3330 dec_cursor();
3331 if (cls() != sclass) /* stop at start of word */
3332 {
3333 inc_cursor();
3334 break;
3335 }
3336 }
3337}
3338
3339 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003340find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 int c;
3343
3344 while (decl(posp) != -1)
3345 {
3346 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003347 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
3349 incl(posp);
3350 break;
3351 }
3352 }
3353}
3354
3355/*
3356 * Skip count/2 sentences and count/2 separating white spaces.
3357 */
3358 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003359findsent_forward(
3360 long count,
3361 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 while (count--)
3364 {
3365 findsent(FORWARD, 1L);
3366 if (at_start_sent)
3367 find_first_blank(&curwin->w_cursor);
3368 if (count == 0 || at_start_sent)
3369 decl(&curwin->w_cursor);
3370 at_start_sent = !at_start_sent;
3371 }
3372}
3373
3374/*
3375 * Find word under cursor, cursor at end.
3376 * Used while an operator is pending, and in Visual mode.
3377 */
3378 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003379current_word(
3380 oparg_T *oap,
3381 long count,
3382 int include, /* TRUE: include word and white space */
3383 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384{
3385 pos_T start_pos;
3386 pos_T pos;
3387 int inclusive = TRUE;
3388 int include_white = FALSE;
3389
3390 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003391 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003394 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 dec_cursor();
3396
3397 /*
3398 * When Visual mode is not active, or when the VIsual area is only one
3399 * character, select the word and/or white space under the cursor.
3400 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003401 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
3403 /*
3404 * Go to start of current word or white space.
3405 */
3406 back_in_line();
3407 start_pos = curwin->w_cursor;
3408
3409 /*
3410 * If the start is on white space, and white space should be included
3411 * (" word"), or start is not on white space, and white space should
3412 * not be included ("word"), find end of word.
3413 */
3414 if ((cls() == 0) == include)
3415 {
3416 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3417 return FAIL;
3418 }
3419 else
3420 {
3421 /*
3422 * If the start is not on white space, and white space should be
3423 * included ("word "), or start is on white space and white
3424 * space should not be included (" "), find start of word.
3425 * If we end up in the first column of the next line (single char
3426 * word) back up to end of the line.
3427 */
3428 fwd_word(1L, bigword, TRUE);
3429 if (curwin->w_cursor.col == 0)
3430 decl(&curwin->w_cursor);
3431 else
3432 oneleft();
3433
3434 if (include)
3435 include_white = TRUE;
3436 }
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (VIsual_active)
3439 {
3440 /* should do something when inclusive == FALSE ! */
3441 VIsual = start_pos;
3442 redraw_curbuf_later(INVERTED); /* update the inversion */
3443 }
3444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 oap->start = start_pos;
3447 oap->motion_type = MCHAR;
3448 }
3449 --count;
3450 }
3451
3452 /*
3453 * When count is still > 0, extend with more objects.
3454 */
3455 while (count > 0)
3456 {
3457 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003458 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
3460 /*
3461 * In Visual mode, with cursor at start: move cursor back.
3462 */
3463 if (decl(&curwin->w_cursor) == -1)
3464 return FAIL;
3465 if (include != (cls() != 0))
3466 {
3467 if (bck_word(1L, bigword, TRUE) == FAIL)
3468 return FAIL;
3469 }
3470 else
3471 {
3472 if (bckend_word(1L, bigword, TRUE) == FAIL)
3473 return FAIL;
3474 (void)incl(&curwin->w_cursor);
3475 }
3476 }
3477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
3479 /*
3480 * Move cursor forward one word and/or white area.
3481 */
3482 if (incl(&curwin->w_cursor) == -1)
3483 return FAIL;
3484 if (include != (cls() == 0))
3485 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003486 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 return FAIL;
3488 /*
3489 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003490 * the first character on the line.
3491 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003493 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 inclusive = FALSE;
3495 }
3496 else
3497 {
3498 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3499 return FAIL;
3500 }
3501 }
3502 --count;
3503 }
3504
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003505 if (include_white && (cls() != 0
3506 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 /*
3509 * If we don't include white space at the end, move the start
3510 * to include some white space there. This makes "daw" work
3511 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003512 * word). Also when "2daw" deletes "word." at the end of the line
3513 * (cursor is at start of next line).
3514 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 */
3516 pos = curwin->w_cursor; /* save cursor position */
3517 curwin->w_cursor = start_pos;
3518 if (oneleft() == OK)
3519 {
3520 back_in_line();
3521 if (cls() == 0 && curwin->w_cursor.col > 0)
3522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 if (VIsual_active)
3524 VIsual = curwin->w_cursor;
3525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 oap->start = curwin->w_cursor;
3527 }
3528 }
3529 curwin->w_cursor = pos; /* put cursor back at end */
3530 }
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (VIsual_active)
3533 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003534 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 inc_cursor();
3536 if (VIsual_mode == 'V')
3537 {
3538 VIsual_mode = 'v';
3539 redraw_cmdline = TRUE; /* show mode later */
3540 }
3541 }
3542 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 oap->inclusive = inclusive;
3544
3545 return OK;
3546}
3547
3548/*
3549 * Find sentence(s) under the cursor, cursor at end.
3550 * When Visual active, extend it by one or more sentences.
3551 */
3552 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003553current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554{
3555 pos_T start_pos;
3556 pos_T pos;
3557 int start_blank;
3558 int c;
3559 int at_start_sent;
3560 long ncount;
3561
3562 start_pos = curwin->w_cursor;
3563 pos = start_pos;
3564 findsent(FORWARD, 1L); /* Find start of next sentence. */
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003567 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003569 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003572 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 {
3574 /*
3575 * Cursor at start of Visual area.
3576 * Find out where we are:
3577 * - in the white space before a sentence
3578 * - in a sentence or just after it
3579 * - at the start of a sentence
3580 */
3581 at_start_sent = TRUE;
3582 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003583 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003586 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 {
3588 at_start_sent = FALSE;
3589 break;
3590 }
3591 incl(&pos);
3592 }
3593 if (!at_start_sent)
3594 {
3595 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003596 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 at_start_sent = TRUE; /* exactly at start of sentence */
3598 else
3599 /* inside a sentence, go to its end (start of next) */
3600 findsent(FORWARD, 1L);
3601 }
3602 if (include) /* "as" gets twice as much as "is" */
3603 count *= 2;
3604 while (count--)
3605 {
3606 if (at_start_sent)
3607 find_first_blank(&curwin->w_cursor);
3608 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003609 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 findsent(BACKWARD, 1L);
3611 at_start_sent = !at_start_sent;
3612 }
3613 }
3614 else
3615 {
3616 /*
3617 * Cursor at end of Visual area.
3618 * Find out where we are:
3619 * - just before a sentence
3620 * - just before or in the white space before a sentence
3621 * - in a sentence
3622 */
3623 incl(&pos);
3624 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003625 /* not just before a sentence */
3626 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003629 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
3631 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003632 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
3634 at_start_sent = TRUE;
3635 break;
3636 }
3637 incl(&pos);
3638 }
3639 if (at_start_sent) /* in the sentence */
3640 findsent(BACKWARD, 1L);
3641 else /* in/before white before a sentence */
3642 curwin->w_cursor = start_pos;
3643 }
3644
3645 if (include) /* "as" gets twice as much as "is" */
3646 count *= 2;
3647 findsent_forward(count, at_start_sent);
3648 if (*p_sel == 'e')
3649 ++curwin->w_cursor.col;
3650 }
3651 return OK;
3652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003655 * If the cursor started on a blank, check if it is just before the start
3656 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003658 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003660 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
3662 start_blank = TRUE;
3663 find_first_blank(&start_pos); /* go back to first blank */
3664 }
3665 else
3666 {
3667 start_blank = FALSE;
3668 findsent(BACKWARD, 1L);
3669 start_pos = curwin->w_cursor;
3670 }
3671 if (include)
3672 ncount = count * 2;
3673 else
3674 {
3675 ncount = count;
3676 if (start_blank)
3677 --ncount;
3678 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003679 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 findsent_forward(ncount, TRUE);
3681 else
3682 decl(&curwin->w_cursor);
3683
3684 if (include)
3685 {
3686 /*
3687 * If the blank in front of the sentence is included, exclude the
3688 * blanks at the end of the sentence, go back to the first blank.
3689 * If there are no trailing blanks, try to include leading blanks.
3690 */
3691 if (start_blank)
3692 {
3693 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003694 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3695 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 decl(&curwin->w_cursor);
3697 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003698 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 find_first_blank(&start_pos);
3700 }
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (VIsual_active)
3703 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003704 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003705 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 goto extend;
3707 if (*p_sel == 'e')
3708 ++curwin->w_cursor.col;
3709 VIsual = start_pos;
3710 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003711 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 redraw_curbuf_later(INVERTED); /* update the inversion */
3713 }
3714 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
3716 /* include a newline after the sentence, if there is one */
3717 if (incl(&curwin->w_cursor) == -1)
3718 oap->inclusive = TRUE;
3719 else
3720 oap->inclusive = FALSE;
3721 oap->start = start_pos;
3722 oap->motion_type = MCHAR;
3723 }
3724 return OK;
3725}
3726
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003727/*
3728 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003729 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003732current_block(
3733 oparg_T *oap,
3734 long count,
3735 int include, /* TRUE == include white space */
3736 int what, /* '(', '{', etc. */
3737 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 pos_T old_pos;
3740 pos_T *pos = NULL;
3741 pos_T start_pos;
3742 pos_T *end_pos;
3743 pos_T old_start, old_end;
3744 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003745 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
3747 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003748 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 old_start = old_end;
3750
3751 /*
3752 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3753 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003754 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 {
3756 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003757 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 while (inindent(1))
3759 if (inc_cursor() != 0)
3760 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003761 if (gchar_cursor() == what)
3762 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 ++curwin->w_cursor.col;
3764 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003765 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
3767 old_start = VIsual;
3768 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3769 }
3770 else
3771 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 /*
3774 * Search backwards for unclosed '(', '{', etc..
3775 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003776 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3777 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 */
3779 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003780 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 while (count-- > 0)
3782 {
3783 if ((pos = findmatch(NULL, what)) == NULL)
3784 break;
3785 curwin->w_cursor = *pos;
3786 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3787 }
3788 p_cpo = save_cpo;
3789
3790 /*
3791 * Search for matching ')', '}', etc.
3792 * Put this position in curwin->w_cursor.
3793 */
3794 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3795 {
3796 curwin->w_cursor = old_pos;
3797 return FAIL;
3798 }
3799 curwin->w_cursor = *end_pos;
3800
3801 /*
3802 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003803 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3804 * indent. But only if the resulting area is not smaller than what we
3805 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 */
3807 while (!include)
3808 {
3809 incl(&start_pos);
3810 sol = (curwin->w_cursor.col == 0);
3811 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003812 while (inindent(1))
3813 {
3814 sol = TRUE;
3815 if (decl(&curwin->w_cursor) != 0)
3816 break;
3817 }
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /*
3820 * In Visual mode, when the resulting area is not bigger than what we
3821 * started with, extend it to the next block, and then exclude again.
3822 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003823 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 && VIsual_active)
3825 {
3826 curwin->w_cursor = old_start;
3827 decl(&curwin->w_cursor);
3828 if ((pos = findmatch(NULL, what)) == NULL)
3829 {
3830 curwin->w_cursor = old_pos;
3831 return FAIL;
3832 }
3833 start_pos = *pos;
3834 curwin->w_cursor = *pos;
3835 if ((end_pos = findmatch(NULL, other)) == NULL)
3836 {
3837 curwin->w_cursor = old_pos;
3838 return FAIL;
3839 }
3840 curwin->w_cursor = *end_pos;
3841 }
3842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 break;
3844 }
3845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (VIsual_active)
3847 {
3848 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003849 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003850 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 inc(&curwin->w_cursor); /* include the line break */
3852 VIsual = start_pos;
3853 VIsual_mode = 'v';
3854 redraw_curbuf_later(INVERTED); /* update the inversion */
3855 showmode();
3856 }
3857 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
3859 oap->start = start_pos;
3860 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003861 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003864 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003865 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003867 else
3868 /* End is before the start (no text in between <>, [], etc.): don't
3869 * operate on any text. */
3870 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
3872
3873 return OK;
3874}
3875
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003876/*
3877 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3878 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3879 */
3880 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003881in_html_tag(
3882 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003883{
3884 char_u *line = ml_get_curline();
3885 char_u *p;
3886 int c;
3887 int lc = NUL;
3888 pos_T pos;
3889
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003890 if (enc_dbcs)
3891 {
3892 char_u *lp = NULL;
3893
3894 /* We search forward until the cursor, because searching backwards is
3895 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003896 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003897 if (*p == '>' || *p == '<')
3898 {
3899 lc = *p;
3900 lp = p;
3901 }
3902 if (*p != '<') /* check for '<' under cursor */
3903 {
3904 if (lc != '<')
3905 return FALSE;
3906 p = lp;
3907 }
3908 }
3909 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003910 {
3911 for (p = line + curwin->w_cursor.col; p > line; )
3912 {
3913 if (*p == '<') /* find '<' under/before cursor */
3914 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003915 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003916 if (*p == '>') /* find '>' before cursor */
3917 break;
3918 }
3919 if (*p != '<')
3920 return FALSE;
3921 }
3922
3923 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003924 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003925
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003926 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003927 if (end_tag)
3928 /* check that there is a '/' after the '<' */
3929 return *p == '/';
3930
3931 /* check that there is no '/' after the '<' */
3932 if (*p == '/')
3933 return FALSE;
3934
3935 /* check that the matching '>' is not preceded by '/' */
3936 for (;;)
3937 {
3938 if (inc(&pos) < 0)
3939 return FALSE;
3940 c = *ml_get_pos(&pos);
3941 if (c == '>')
3942 break;
3943 lc = c;
3944 }
3945 return lc != '/';
3946}
3947
3948/*
3949 * Find tag block under the cursor, cursor at end.
3950 */
3951 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003952current_tagblock(
3953 oparg_T *oap,
3954 long count_arg,
3955 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003956{
3957 long count = count_arg;
3958 long n;
3959 pos_T old_pos;
3960 pos_T start_pos;
3961 pos_T end_pos;
3962 pos_T old_start, old_end;
3963 char_u *spat, *epat;
3964 char_u *p;
3965 char_u *cp;
3966 int len;
3967 int r;
3968 int do_include = include;
3969 int save_p_ws = p_ws;
3970 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003971 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003972
3973 p_ws = FALSE;
3974
3975 old_pos = curwin->w_cursor;
3976 old_end = curwin->w_cursor; /* remember where we started */
3977 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003978 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003979 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003980
3981 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003982 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003983 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003984 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003985 {
3986 setpcmark();
3987
3988 /* ignore indent */
3989 while (inindent(1))
3990 if (inc_cursor() != 0)
3991 break;
3992
3993 if (in_html_tag(FALSE))
3994 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003995 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003996 while (*ml_get_cursor() != '>')
3997 if (inc_cursor() < 0)
3998 break;
3999 }
4000 else if (in_html_tag(TRUE))
4001 {
4002 /* cursor on end tag, move to just before it */
4003 while (*ml_get_cursor() != '<')
4004 if (dec_cursor() < 0)
4005 break;
4006 dec_cursor();
4007 old_end = curwin->w_cursor;
4008 }
4009 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004010 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004011 {
4012 old_start = VIsual;
4013 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4014 }
4015 else
4016 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004017
4018again:
4019 /*
4020 * Search backwards for unclosed "<aaa>".
4021 * Put this position in start_pos.
4022 */
4023 for (n = 0; n < count; ++n)
4024 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004025 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004026 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004027 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004028 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004029 {
4030 curwin->w_cursor = old_pos;
4031 goto theend;
4032 }
4033 }
4034 start_pos = curwin->w_cursor;
4035
4036 /*
4037 * Search for matching "</aaa>". First isolate the "aaa".
4038 */
4039 inc_cursor();
4040 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004041 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004042 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004043 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004044 if (len == 0)
4045 {
4046 curwin->w_cursor = old_pos;
4047 goto theend;
4048 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004049 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004050 epat = alloc(len + 9);
4051 if (spat == NULL || epat == NULL)
4052 {
4053 vim_free(spat);
4054 vim_free(epat);
4055 curwin->w_cursor = old_pos;
4056 goto theend;
4057 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004058 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004059 sprintf((char *)epat, "</%.*s>\\c", len, p);
4060
Bram Moolenaar48570482017-10-30 21:48:41 +01004061 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004062 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004063
4064 vim_free(spat);
4065 vim_free(epat);
4066
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004067 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004068 {
4069 /* Can't find other end or it's before the previous end. Could be a
4070 * HTML tag that doesn't have a matching end. Search backwards for
4071 * another starting tag. */
4072 count = 1;
4073 curwin->w_cursor = start_pos;
4074 goto again;
4075 }
4076
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02004077 if (do_include)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004078 {
4079 /* Include up to the '>'. */
4080 while (*ml_get_cursor() != '>')
4081 if (inc_cursor() < 0)
4082 break;
4083 }
4084 else
4085 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004086 char_u *c = ml_get_cursor();
4087
4088 /* Exclude the '<' of the end tag.
4089 * If the closing tag is on new line, do not decrement cursor, but
4090 * make operation exclusive, so that the linefeed will be selected */
4091 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4092 /* do not decrement cursor */
4093 is_inclusive = FALSE;
4094 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004095 dec_cursor();
4096 }
4097 end_pos = curwin->w_cursor;
4098
4099 if (!do_include)
4100 {
4101 /* Exclude the start tag. */
4102 curwin->w_cursor = start_pos;
4103 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004104 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004105 {
4106 inc_cursor();
4107 start_pos = curwin->w_cursor;
4108 break;
4109 }
4110 curwin->w_cursor = end_pos;
4111
Bram Moolenaarb476cb72018-08-16 21:37:50 +02004112 // If we are in Visual mode and now have the same text as before set
4113 // "do_include" and try again.
4114 if (VIsual_active && EQUAL_POS(start_pos, old_start)
4115 && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004116 {
4117 do_include = TRUE;
4118 curwin->w_cursor = old_start;
4119 count = count_arg;
4120 goto again;
4121 }
4122 }
4123
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004124 if (VIsual_active)
4125 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004126 /* If the end is before the start there is no text between tags, select
4127 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004128 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004129 curwin->w_cursor = start_pos;
4130 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004131 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004132 VIsual = start_pos;
4133 VIsual_mode = 'v';
4134 redraw_curbuf_later(INVERTED); /* update the inversion */
4135 showmode();
4136 }
4137 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004138 {
4139 oap->start = start_pos;
4140 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004141 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004142 {
4143 /* End is before the start: there is no text between tags; operate
4144 * on an empty area. */
4145 curwin->w_cursor = start_pos;
4146 oap->inclusive = FALSE;
4147 }
4148 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004149 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004150 }
4151 retval = OK;
4152
4153theend:
4154 p_ws = save_p_ws;
4155 return retval;
4156}
4157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004159current_par(
4160 oparg_T *oap,
4161 long count,
4162 int include, /* TRUE == include white space */
4163 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 linenr_T start_lnum;
4166 linenr_T end_lnum;
4167 int white_in_front;
4168 int dir;
4169 int start_is_white;
4170 int prev_start_is_white;
4171 int retval = OK;
4172 int do_white = FALSE;
4173 int t;
4174 int i;
4175
4176 if (type == 'S') /* not implemented yet */
4177 return FAIL;
4178
4179 start_lnum = curwin->w_cursor.lnum;
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 /*
4182 * When visual area is more than one line: extend it.
4183 */
4184 if (VIsual_active && start_lnum != VIsual.lnum)
4185 {
4186extend:
4187 if (start_lnum < VIsual.lnum)
4188 dir = BACKWARD;
4189 else
4190 dir = FORWARD;
4191 for (i = count; --i >= 0; )
4192 {
4193 if (start_lnum ==
4194 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4195 {
4196 retval = FAIL;
4197 break;
4198 }
4199
4200 prev_start_is_white = -1;
4201 for (t = 0; t < 2; ++t)
4202 {
4203 start_lnum += dir;
4204 start_is_white = linewhite(start_lnum);
4205 if (prev_start_is_white == start_is_white)
4206 {
4207 start_lnum -= dir;
4208 break;
4209 }
4210 for (;;)
4211 {
4212 if (start_lnum == (dir == BACKWARD
4213 ? 1 : curbuf->b_ml.ml_line_count))
4214 break;
4215 if (start_is_white != linewhite(start_lnum + dir)
4216 || (!start_is_white
4217 && startPS(start_lnum + (dir > 0
4218 ? 1 : 0), 0, 0)))
4219 break;
4220 start_lnum += dir;
4221 }
4222 if (!include)
4223 break;
4224 if (start_lnum == (dir == BACKWARD
4225 ? 1 : curbuf->b_ml.ml_line_count))
4226 break;
4227 prev_start_is_white = start_is_white;
4228 }
4229 }
4230 curwin->w_cursor.lnum = start_lnum;
4231 curwin->w_cursor.col = 0;
4232 return retval;
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /*
4236 * First move back to the start_lnum of the paragraph or white lines
4237 */
4238 white_in_front = linewhite(start_lnum);
4239 while (start_lnum > 1)
4240 {
4241 if (white_in_front) /* stop at first white line */
4242 {
4243 if (!linewhite(start_lnum - 1))
4244 break;
4245 }
4246 else /* stop at first non-white line of start of paragraph */
4247 {
4248 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4249 break;
4250 }
4251 --start_lnum;
4252 }
4253
4254 /*
4255 * Move past the end of any white lines.
4256 */
4257 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004258 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4259 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
4261 --end_lnum;
4262 i = count;
4263 if (!include && white_in_front)
4264 --i;
4265 while (i--)
4266 {
4267 if (end_lnum == curbuf->b_ml.ml_line_count)
4268 return FAIL;
4269
4270 if (!include)
4271 do_white = linewhite(end_lnum + 1);
4272
4273 if (include || !do_white)
4274 {
4275 ++end_lnum;
4276 /*
4277 * skip to end of paragraph
4278 */
4279 while (end_lnum < curbuf->b_ml.ml_line_count
4280 && !linewhite(end_lnum + 1)
4281 && !startPS(end_lnum + 1, 0, 0))
4282 ++end_lnum;
4283 }
4284
4285 if (i == 0 && white_in_front && include)
4286 break;
4287
4288 /*
4289 * skip to end of white lines after paragraph
4290 */
4291 if (include || do_white)
4292 while (end_lnum < curbuf->b_ml.ml_line_count
4293 && linewhite(end_lnum + 1))
4294 ++end_lnum;
4295 }
4296
4297 /*
4298 * If there are no empty lines at the end, try to find some empty lines at
4299 * the start (unless that has been done already).
4300 */
4301 if (!white_in_front && !linewhite(end_lnum) && include)
4302 while (start_lnum > 1 && linewhite(start_lnum - 1))
4303 --start_lnum;
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (VIsual_active)
4306 {
4307 /* Problem: when doing "Vipipip" nothing happens in a single white
4308 * line, we get stuck there. Trap this here. */
4309 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4310 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004311 if (VIsual.lnum != start_lnum)
4312 {
4313 VIsual.lnum = start_lnum;
4314 VIsual.col = 0;
4315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 VIsual_mode = 'V';
4317 redraw_curbuf_later(INVERTED); /* update the inversion */
4318 showmode();
4319 }
4320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322 oap->start.lnum = start_lnum;
4323 oap->start.col = 0;
4324 oap->motion_type = MLINE;
4325 }
4326 curwin->w_cursor.lnum = end_lnum;
4327 curwin->w_cursor.col = 0;
4328
4329 return OK;
4330}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004331
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004332/*
4333 * Search quote char from string line[col].
4334 * Quote character escaped by one of the characters in "escape" is not counted
4335 * as a quote.
4336 * Returns column number of "quotechar" or -1 when not found.
4337 */
4338 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004339find_next_quote(
4340 char_u *line,
4341 int col,
4342 int quotechar,
4343 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004344{
4345 int c;
4346
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004347 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004348 {
4349 c = line[col];
4350 if (c == NUL)
4351 return -1;
4352 else if (escape != NULL && vim_strchr(escape, c))
4353 ++col;
4354 else if (c == quotechar)
4355 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004356 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004357 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004358 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004359 ++col;
4360 }
4361 return col;
4362}
4363
4364/*
4365 * Search backwards in "line" from column "col_start" to find "quotechar".
4366 * Quote character escaped by one of the characters in "escape" is not counted
4367 * as a quote.
4368 * Return the found column or zero.
4369 */
4370 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004371find_prev_quote(
4372 char_u *line,
4373 int col_start,
4374 int quotechar,
4375 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004376{
4377 int n;
4378
4379 while (col_start > 0)
4380 {
4381 --col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004382 col_start -= (*mb_head_off)(line, line + col_start);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004383 n = 0;
4384 if (escape != NULL)
4385 while (col_start - n > 0 && vim_strchr(escape,
4386 line[col_start - n - 1]) != NULL)
4387 ++n;
4388 if (n & 1)
4389 col_start -= n; /* uneven number of escape chars, skip it */
4390 else if (line[col_start] == quotechar)
4391 break;
4392 }
4393 return col_start;
4394}
4395
4396/*
4397 * Find quote under the cursor, cursor at end.
4398 * Returns TRUE if found, else FALSE.
4399 */
4400 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004401current_quote(
4402 oparg_T *oap,
4403 long count,
4404 int include, /* TRUE == include quote char */
4405 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004406{
4407 char_u *line = ml_get_curline();
4408 int col_end;
4409 int col_start = curwin->w_cursor.col;
4410 int inclusive = FALSE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004411 int vis_empty = TRUE; // Visual selection <= 1 char
4412 int vis_bef_curs = FALSE; // Visual starts before cursor
4413 int inside_quotes = FALSE; // Looks like "i'" done before
4414 int selected_quote = FALSE; // Has quote inside selection
Bram Moolenaarab194812005-09-14 21:40:12 +00004415 int i;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004416 int restore_vis_bef = FALSE; // restore VIsual on abort
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004417
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004418 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004419 if (VIsual_active)
4420 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004421 /* this only works within one line */
4422 if (VIsual.lnum != curwin->w_cursor.lnum)
4423 return FALSE;
4424
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004425 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004426 if (*p_sel == 'e')
4427 {
4428 if (!vis_bef_curs)
4429 {
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004430 // VIsual needs to be the start of Visual selection.
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004431 pos_T t = curwin->w_cursor;
4432
4433 curwin->w_cursor = VIsual;
4434 VIsual = t;
4435 vis_bef_curs = TRUE;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004436 restore_vis_bef = TRUE;
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004437 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004438 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004439 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004440 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004441 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004442
4443 if (!vis_empty)
4444 {
4445 /* Check if the existing selection exactly spans the text inside
4446 * quotes. */
4447 if (vis_bef_curs)
4448 {
4449 inside_quotes = VIsual.col > 0
4450 && line[VIsual.col - 1] == quotechar
4451 && line[curwin->w_cursor.col] != NUL
4452 && line[curwin->w_cursor.col + 1] == quotechar;
4453 i = VIsual.col;
4454 col_end = curwin->w_cursor.col;
4455 }
4456 else
4457 {
4458 inside_quotes = curwin->w_cursor.col > 0
4459 && line[curwin->w_cursor.col - 1] == quotechar
4460 && line[VIsual.col] != NUL
4461 && line[VIsual.col + 1] == quotechar;
4462 i = curwin->w_cursor.col;
4463 col_end = VIsual.col;
4464 }
4465
4466 /* Find out if we have a quote in the selection. */
4467 while (i <= col_end)
4468 if (line[i++] == quotechar)
4469 {
4470 selected_quote = TRUE;
4471 break;
4472 }
4473 }
4474
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004475 if (!vis_empty && line[col_start] == quotechar)
4476 {
4477 /* Already selecting something and on a quote character. Find the
4478 * next quoted string. */
4479 if (vis_bef_curs)
4480 {
4481 /* Assume we are on a closing quote: move to after the next
4482 * opening quote. */
4483 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4484 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004485 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004486 col_end = find_next_quote(line, col_start + 1, quotechar,
4487 curbuf->b_p_qe);
4488 if (col_end < 0)
4489 {
4490 /* We were on a starting quote perhaps? */
4491 col_end = col_start;
4492 col_start = curwin->w_cursor.col;
4493 }
4494 }
4495 else
4496 {
4497 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4498 if (line[col_end] != quotechar)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004499 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004500 col_start = find_prev_quote(line, col_end, quotechar,
4501 curbuf->b_p_qe);
4502 if (line[col_start] != quotechar)
4503 {
4504 /* We were on an ending quote perhaps? */
4505 col_start = col_end;
4506 col_end = curwin->w_cursor.col;
4507 }
4508 }
4509 }
4510 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004511
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004512 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004513 {
4514 int first_col = col_start;
4515
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004516 if (!vis_empty)
4517 {
4518 if (vis_bef_curs)
4519 first_col = find_next_quote(line, col_start, quotechar, NULL);
4520 else
4521 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4522 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004523
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004524 /* The cursor is on a quote, we don't know if it's the opening or
4525 * closing quote. Search from the start of the line to find out.
4526 * Also do this when there is a Visual area, a' may leave the cursor
4527 * in between two strings. */
4528 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004529 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004530 {
4531 /* Find open quote character. */
4532 col_start = find_next_quote(line, col_start, quotechar, NULL);
4533 if (col_start < 0 || col_start > first_col)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004534 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004535 /* Find close quote character. */
4536 col_end = find_next_quote(line, col_start + 1, quotechar,
4537 curbuf->b_p_qe);
4538 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004539 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004540 /* If is cursor between start and end quote character, it is
4541 * target text object. */
4542 if (col_start <= first_col && first_col <= col_end)
4543 break;
4544 col_start = col_end + 1;
4545 }
4546 }
4547 else
4548 {
4549 /* Search backward for a starting quote. */
4550 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4551 if (line[col_start] != quotechar)
4552 {
4553 /* No quote before the cursor, look after the cursor. */
4554 col_start = find_next_quote(line, col_start, quotechar, NULL);
4555 if (col_start < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004556 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004557 }
4558
4559 /* Find close quote character. */
4560 col_end = find_next_quote(line, col_start + 1, quotechar,
4561 curbuf->b_p_qe);
4562 if (col_end < 0)
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004563 goto abort_search;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004564 }
4565
4566 /* When "include" is TRUE, include spaces after closing quote or before
4567 * the starting quote. */
4568 if (include)
4569 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004570 if (VIM_ISWHITE(line[col_end + 1]))
4571 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004572 ++col_end;
4573 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004574 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004575 --col_start;
4576 }
4577
Bram Moolenaarab194812005-09-14 21:40:12 +00004578 /* Set start position. After vi" another i" must include the ".
4579 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004580 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004581 ++col_start;
4582 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004583 if (VIsual_active)
4584 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004585 /* Set the start of the Visual area when the Visual area was empty, we
4586 * were just inside quotes or the Visual area didn't start at a quote
4587 * and didn't include a quote.
4588 */
4589 if (vis_empty
4590 || (vis_bef_curs
4591 && !selected_quote
4592 && (inside_quotes
4593 || (line[VIsual.col] != quotechar
4594 && (VIsual.col == 0
4595 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004596 {
4597 VIsual = curwin->w_cursor;
4598 redraw_curbuf_later(INVERTED);
4599 }
4600 }
4601 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004602 {
4603 oap->start = curwin->w_cursor;
4604 oap->motion_type = MCHAR;
4605 }
4606
4607 /* Set end position. */
4608 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004609 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004610 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004611 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004612 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004613 if (VIsual_active)
4614 {
4615 if (vis_empty || vis_bef_curs)
4616 {
4617 /* decrement cursor when 'selection' is not exclusive */
4618 if (*p_sel != 'e')
4619 dec_cursor();
4620 }
4621 else
4622 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004623 /* Cursor is at start of Visual area. Set the end of the Visual
4624 * area when it was just inside quotes or it didn't end at a
4625 * quote. */
4626 if (inside_quotes
4627 || (!selected_quote
4628 && line[VIsual.col] != quotechar
4629 && (line[VIsual.col] == NUL
4630 || line[VIsual.col + 1] != quotechar)))
4631 {
4632 dec_cursor();
4633 VIsual = curwin->w_cursor;
4634 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004635 curwin->w_cursor.col = col_start;
4636 }
4637 if (VIsual_mode == 'V')
4638 {
4639 VIsual_mode = 'v';
4640 redraw_cmdline = TRUE; /* show mode later */
4641 }
4642 }
4643 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004644 {
4645 /* Set inclusive and other oap's flags. */
4646 oap->inclusive = inclusive;
4647 }
4648
4649 return OK;
Bram Moolenaar55d3bdb2019-02-22 15:04:17 +01004650
4651abort_search:
4652 if (VIsual_active && *p_sel == 'e')
4653 {
4654 inc_cursor();
4655 if (restore_vis_bef)
4656 {
4657 pos_T t = curwin->w_cursor;
4658
4659 curwin->w_cursor = VIsual;
4660 VIsual = t;
4661 }
4662 }
4663 return FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004664}
4665
4666#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004668static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004669
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004670/*
4671 * Find next search match under cursor, cursor at end.
4672 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004673 */
4674 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004675current_search(
4676 long count,
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004677 int forward) // TRUE for forward, FALSE for backward
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004678{
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004679 pos_T start_pos; // start position of the pattern match
4680 pos_T end_pos; // end position of the pattern match
4681 pos_T orig_pos; // position of the cursor at beginning
4682 pos_T pos; // position after the pattern
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004683 int i;
4684 int dir;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004685 int result; // result of various function calls
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004686 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004687 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004688 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004689 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004690
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004691 /* wrapping should not occur */
4692 p_ws = FALSE;
4693
4694 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004695 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004696 dec_cursor();
4697
4698 if (VIsual_active)
4699 {
4700 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004701
4702 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004703
4704 /* make sure, searching further will extend the match */
4705 if (VIsual_active)
4706 {
4707 if (forward)
4708 incl(&pos);
4709 else
4710 decl(&pos);
4711 }
4712 }
4713 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004714 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004715
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004716 /* Is the pattern is zero-width?, this time, don't care about the direction
4717 */
4718 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4719 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004720 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004721 {
4722 p_ws = old_p_ws;
4723 return FAIL; /* pattern not found */
4724 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004725
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004726 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004727 * The trick is to first search backwards and then search forward again,
4728 * so that a match at the current cursor position will be correctly
4729 * captured.
4730 */
4731 for (i = 0; i < 2; i++)
4732 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004733 if (forward)
4734 dir = i;
4735 else
4736 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004737
4738 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004739 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004740 flags = SEARCH_END;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004741 end_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004742
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004743 result = searchit(curwin, curbuf, &pos, &end_pos,
4744 (dir ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004745 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004746 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004747
4748 /* First search may fail, but then start searching from the
4749 * beginning of the file (cursor might be on the search match)
4750 * except when Visual mode is active, so that extending the visual
4751 * selection works. */
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004752 if (i == 1 && !result) /* not found, abort */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004753 {
4754 curwin->w_cursor = orig_pos;
4755 if (VIsual_active)
4756 VIsual = save_VIsual;
4757 p_ws = old_p_ws;
4758 return FAIL;
4759 }
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004760 else if (i == 0 && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004761 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004762 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004763 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004764 /* try again from start of buffer */
4765 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004766 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004767 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004768 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004769 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004770 /* searching backwards, so set pos to last line and col */
4771 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004772 pos.col = (colnr_T)STRLEN(
4773 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004774 }
4775 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004776 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004777 }
4778
4779 start_pos = pos;
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004780 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004781
4782 if (!VIsual_active)
4783 VIsual = start_pos;
4784
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004785 // put cursor on last character of match
4786 curwin->w_cursor = end_pos;
4787 if (LT_POS(VIsual, end_pos))
4788 dec_cursor();
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004789 VIsual_active = TRUE;
4790 VIsual_mode = 'v';
4791
Bram Moolenaarb7633612019-02-10 21:48:25 +01004792 redraw_curbuf_later(INVERTED); /* update the inversion */
4793 if (*p_sel == 'e')
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004794 {
Bram Moolenaarb7633612019-02-10 21:48:25 +01004795 /* Correction for exclusive selection depends on the direction. */
4796 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
4797 inc_cursor();
4798 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
4799 inc(&VIsual);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004800 }
4801
4802#ifdef FEAT_FOLDING
4803 if (fdo_flags & FDO_SEARCH && KeyTyped)
4804 foldOpenCursor();
4805#endif
4806
4807 may_start_select('c');
4808#ifdef FEAT_MOUSE
4809 setmouse();
4810#endif
4811#ifdef FEAT_CLIPBOARD
4812 /* Make sure the clipboard gets updated. Needed because start and
4813 * end are still the same, and the selection needs to be owned */
4814 clip_star.vmode = NUL;
4815#endif
4816 redraw_curbuf_later(INVERTED);
4817 showmode();
4818
4819 return OK;
4820}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004821
4822/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004823 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004824 * If move is TRUE, check from the beginning of the buffer, else from position
4825 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004826 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004827 * Returns TRUE, FALSE or -1 for failure.
4828 */
4829 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004830is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004831{
4832 regmmatch_T regmatch;
4833 int nmatched = 0;
4834 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004835 pos_T pos;
4836 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004837 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004838
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004839 if (pattern == NULL)
4840 pattern = spats[last_idx].pat;
4841
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004842 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4843 SEARCH_KEEP, &regmatch) == FAIL)
4844 return -1;
4845
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004846 /* init startcol correctly */
4847 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004848 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004849 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004850 {
4851 CLEAR_POS(&pos);
4852 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004853 else
4854 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004855 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004856 /* accept a match at the cursor position */
4857 flag = SEARCH_START;
4858 }
4859
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004860 if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004861 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004862 {
4863 /* Zero-width pattern should match somewhere, then we can check if
4864 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004865 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004866 do
4867 {
4868 regmatch.startpos[0].col++;
4869 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004870 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004871 if (!nmatched)
4872 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004873 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4874 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004875
4876 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004877 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004878 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004879 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4880 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004881 /* one char width */
4882 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4883 result = TRUE;
4884 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004885 }
4886
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004887 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004888 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004889 return result;
4890}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4893 || defined(PROTO)
4894/*
4895 * return TRUE if line 'lnum' is empty or has white chars only.
4896 */
4897 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004898linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
4900 char_u *p;
4901
4902 p = skipwhite(ml_get(lnum));
4903 return (*p == NUL);
4904}
4905#endif
4906
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02004907/*
4908 * Add the search count "[3/19]" to "msgbuf".
4909 */
4910 static void
4911search_stat(
4912 int dirc,
4913 pos_T *pos,
4914 char_u *msgbuf)
4915{
4916 int save_ws = p_ws;
4917 int wraparound = FALSE;
4918 pos_T p = (*pos);
4919 static pos_T lastpos = {0, 0, 0};
4920 static int cur = 0;
4921 static int cnt = 0;
4922 static int chgtick = 0;
4923 static char_u *lastpat = NULL;
4924 static buf_T *lbuf = NULL;
4925#ifdef FEAT_RELTIME
4926 proftime_T start;
4927#endif
4928#define OUT_OF_TIME 999
4929
4930 wraparound = ((dirc == '?' && LT_POS(lastpos, p))
4931 || (dirc == '/' && LT_POS(p, lastpos)));
4932
4933 // If anything relevant changed the count has to be recomputed.
4934 // MB_STRNICMP ignores case, but we should not ignore case.
4935 // Unfortunately, there is no MB_STRNICMP function.
4936 if (!(chgtick == CHANGEDTICK(curbuf)
4937 && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0
4938 && STRLEN(lastpat) == STRLEN(spats[last_idx].pat)
4939 && EQUAL_POS(lastpos, curwin->w_cursor)
4940 && lbuf == curbuf) || wraparound || cur < 0 || cur > 99)
4941 {
4942 cur = 0;
4943 cnt = 0;
4944 CLEAR_POS(&lastpos);
4945 lbuf = curbuf;
4946 }
4947
4948 if (EQUAL_POS(lastpos, curwin->w_cursor) && !wraparound
4949 && (dirc == '/' ? cur < cnt : cur > 0))
4950 cur += dirc == '/' ? 1 : -1;
4951 else
4952 {
4953 p_ws = FALSE;
4954#ifdef FEAT_RELTIME
4955 profile_setlimit(20L, &start);
4956#endif
4957 while (!got_int && searchit(curwin, curbuf, &lastpos, NULL,
4958 FORWARD, NULL, 1, SEARCH_PEEK + SEARCH_KEEP,
4959 RE_LAST, (linenr_T)0, NULL, NULL) != FAIL)
4960 {
4961#ifdef FEAT_RELTIME
4962 // Stop after passing the time limit.
4963 if (profile_passed_limit(&start))
4964 {
4965 cnt = OUT_OF_TIME;
4966 cur = OUT_OF_TIME;
4967 break;
4968 }
4969#endif
4970 cnt++;
4971 if (LTOREQ_POS(lastpos, p))
4972 cur++;
4973 fast_breakcheck();
4974 if (cnt > 99)
4975 break;
4976 }
4977 if (got_int)
4978 cur = -1; // abort
4979 }
4980 if (cur > 0)
4981 {
4982#define STAT_BUF_LEN 10
4983 char t[STAT_BUF_LEN] = "";
4984
4985#ifdef FEAT_RIGHTLEFT
4986 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
4987 {
4988 if (cur == OUT_OF_TIME)
4989 vim_snprintf(t, STAT_BUF_LEN, "[?/??]");
4990 else if (cnt > 99 && cur > 99)
4991 vim_snprintf(t, STAT_BUF_LEN, "[>99/>99]");
4992 else if (cnt > 99)
4993 vim_snprintf(t, STAT_BUF_LEN, "[>99/%d]", cur);
4994 else
4995 vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cnt, cur);
4996 }
4997 else
4998#endif
4999 {
5000 if (cur == OUT_OF_TIME)
5001 vim_snprintf(t, STAT_BUF_LEN, "[?/??]");
5002 else if (cnt > 99 && cur > 99)
5003 vim_snprintf(t, STAT_BUF_LEN, "[>99/>99]");
5004 else if (cnt > 99)
5005 vim_snprintf(t, STAT_BUF_LEN, "[%d/>99]", cur);
5006 else
5007 vim_snprintf(t, STAT_BUF_LEN, "[%d/%d]", cur, cnt);
5008 }
Bram Moolenaarb3de6c42019-05-05 13:02:28 +02005009 mch_memmove(msgbuf + STRLEN(msgbuf) - STRLEN(t), t, STRLEN(t));
Bram Moolenaar9dfa3132019-05-04 21:08:40 +02005010 if (dirc == '?' && cur == 100)
5011 cur = -1;
5012
5013 vim_free(lastpat);
5014 lastpat = vim_strsave(spats[last_idx].pat);
5015 chgtick = CHANGEDTICK(curbuf);
5016 lbuf = curbuf;
5017 lastpos = p;
5018
5019 // keep the message even after redraw
5020 give_warning(msgbuf, FALSE);
5021 }
5022 p_ws = save_ws;
5023}
5024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#if defined(FEAT_FIND_ID) || defined(PROTO)
5026/*
5027 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01005028 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005031find_pattern_in_path(
5032 char_u *ptr, /* pointer to search pattern */
5033 int dir UNUSED, /* direction of expansion */
5034 int len, /* length of search pattern */
5035 int whole, /* match whole words only */
5036 int skip_comments, /* don't match inside comments */
5037 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005039 long count,
5040 int action, /* What to do when we find it */
5041 linenr_T start_lnum, /* first line to start searching */
5042 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044 SearchedFile *files; /* Stack of included files */
5045 SearchedFile *bigger; /* When we need more space */
5046 int max_path_depth = 50;
5047 long match_count = 1;
5048
5049 char_u *pat;
5050 char_u *new_fname;
5051 char_u *curr_fname = curbuf->b_fname;
5052 char_u *prev_fname = NULL;
5053 linenr_T lnum;
5054 int depth;
5055 int depth_displayed; /* For type==CHECK_PATH */
5056 int old_files;
5057 int already_searched;
5058 char_u *file_line;
5059 char_u *line;
5060 char_u *p;
5061 char_u save_char;
5062 int define_matched;
5063 regmatch_T regmatch;
5064 regmatch_T incl_regmatch;
5065 regmatch_T def_regmatch;
5066 int matched = FALSE;
5067 int did_show = FALSE;
5068 int found = FALSE;
5069 int i;
5070 char_u *already = NULL;
5071 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005072 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005073#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 win_T *curwin_save = NULL;
5075#endif
5076
5077 regmatch.regprog = NULL;
5078 incl_regmatch.regprog = NULL;
5079 def_regmatch.regprog = NULL;
5080
5081 file_line = alloc(LSIZE);
5082 if (file_line == NULL)
5083 return;
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (type != CHECK_PATH && type != FIND_DEFINE
5086#ifdef FEAT_INS_EXPAND
5087 /* when CONT_SOL is set compare "ptr" with the beginning of the line
5088 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090#endif
5091 )
5092 {
5093 pat = alloc(len + 5);
5094 if (pat == NULL)
5095 goto fpip_end;
5096 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
5097 /* ignore case according to p_ic, p_scs and pat */
5098 regmatch.rm_ic = ignorecase(pat);
5099 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5100 vim_free(pat);
5101 if (regmatch.regprog == NULL)
5102 goto fpip_end;
5103 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005104 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
5105 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005107 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (incl_regmatch.regprog == NULL)
5109 goto fpip_end;
5110 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
5111 }
5112 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
5113 {
5114 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5115 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5116 if (def_regmatch.regprog == NULL)
5117 goto fpip_end;
5118 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5119 }
5120 files = (SearchedFile *)lalloc_clear((long_u)
5121 (max_path_depth * sizeof(SearchedFile)), TRUE);
5122 if (files == NULL)
5123 goto fpip_end;
5124 old_files = max_path_depth;
5125 depth = depth_displayed = -1;
5126
5127 lnum = start_lnum;
5128 if (end_lnum > curbuf->b_ml.ml_line_count)
5129 end_lnum = curbuf->b_ml.ml_line_count;
5130 if (lnum > end_lnum) /* do at least one line */
5131 lnum = end_lnum;
5132 line = ml_get(lnum);
5133
5134 for (;;)
5135 {
5136 if (incl_regmatch.regprog != NULL
5137 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5138 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005139 char_u *p_fname = (curr_fname == curbuf->b_fname)
5140 ? curbuf->b_ffname : curr_fname;
5141
5142 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5143 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5144 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005145 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005146 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5147 else
5148 /* Use text after match with 'include'. */
5149 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005150 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 already_searched = FALSE;
5152 if (new_fname != NULL)
5153 {
5154 /* Check whether we have already searched in this file */
5155 for (i = 0;; i++)
5156 {
5157 if (i == depth + 1)
5158 i = old_files;
5159 if (i == max_path_depth)
5160 break;
5161 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5162 {
5163 if (type != CHECK_PATH &&
5164 action == ACTION_SHOW_ALL && files[i].matched)
5165 {
5166 msg_putchar('\n'); /* cursor below last one */
5167 if (!got_int) /* don't display if 'q'
5168 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005169 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
5171 msg_home_replace_hl(new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005172 msg_puts(_(" (includes previously listed match)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 prev_fname = NULL;
5174 }
5175 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01005176 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 already_searched = TRUE;
5178 break;
5179 }
5180 }
5181 }
5182
5183 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5184 || (new_fname == NULL && !already_searched)))
5185 {
5186 if (did_show)
5187 msg_putchar('\n'); /* cursor below last one */
5188 else
5189 {
5190 gotocmdline(TRUE); /* cursor at status line */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005191 msg_puts_title(_("--- Included files "));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005193 msg_puts_title(_("not found "));
5194 msg_puts_title(_("in path ---\n"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196 did_show = TRUE;
5197 while (depth_displayed < depth && !got_int)
5198 {
5199 ++depth_displayed;
5200 for (i = 0; i < depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005201 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 msg_home_replace(files[depth_displayed].name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005203 msg_puts(" -->\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205 if (!got_int) /* don't display if 'q' typed
5206 for "--more--" message */
5207 {
5208 for (i = 0; i <= depth_displayed; i++)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005209 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (new_fname != NULL)
5211 {
5212 /* using "new_fname" is more reliable, e.g., when
5213 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005214 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 else
5217 {
5218 /*
5219 * Isolate the file name.
5220 * Include the surrounding "" or <> if present.
5221 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005222 if (inc_opt != NULL
5223 && strstr((char *)inc_opt, "\\zs") != NULL)
5224 {
5225 /* pattern contains \zs, use the match */
5226 p = incl_regmatch.startp[0];
5227 i = (int)(incl_regmatch.endp[0]
5228 - incl_regmatch.startp[0]);
5229 }
5230 else
5231 {
5232 /* find the file name after the end of the match */
5233 for (p = incl_regmatch.endp[0];
5234 *p && !vim_isfilec(*p); p++)
5235 ;
5236 for (i = 0; vim_isfilec(p[i]); i++)
5237 ;
5238 }
5239
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if (i == 0)
5241 {
5242 /* Nothing found, use the rest of the line. */
5243 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005244 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005246 /* Avoid checking before the start of the line, can
5247 * happen if \zs appears in the regexp. */
5248 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 if (p[-1] == '"' || p[-1] == '<')
5251 {
5252 --p;
5253 ++i;
5254 }
5255 if (p[i] == '"' || p[i] == '>')
5256 ++i;
5257 }
5258 save_char = p[i];
5259 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005260 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 p[i] = save_char;
5262 }
5263
5264 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5265 {
5266 if (already_searched)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005267 msg_puts(_(" (Already listed)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005269 msg_puts(_(" NOT FOUND"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
5271 }
5272 out_flush(); /* output each line directly */
5273 }
5274
5275 if (new_fname != NULL)
5276 {
5277 /* Push the new file onto the file stack */
5278 if (depth + 1 == old_files)
5279 {
5280 bigger = (SearchedFile *)lalloc((long_u)(
5281 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5282 if (bigger != NULL)
5283 {
5284 for (i = 0; i <= depth; i++)
5285 bigger[i] = files[i];
5286 for (i = depth + 1; i < old_files + max_path_depth; i++)
5287 {
5288 bigger[i].fp = NULL;
5289 bigger[i].name = NULL;
5290 bigger[i].lnum = 0;
5291 bigger[i].matched = FALSE;
5292 }
5293 for (i = old_files; i < max_path_depth; i++)
5294 bigger[i + max_path_depth] = files[i];
5295 old_files += max_path_depth;
5296 max_path_depth *= 2;
5297 vim_free(files);
5298 files = bigger;
5299 }
5300 }
5301 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5302 == NULL)
5303 vim_free(new_fname);
5304 else
5305 {
5306 if (++depth == old_files)
5307 {
5308 /*
5309 * lalloc() for 'bigger' must have failed above. We
5310 * will forget one of our already visited files now.
5311 */
5312 vim_free(files[old_files].name);
5313 ++old_files;
5314 }
5315 files[depth].name = curr_fname = new_fname;
5316 files[depth].lnum = 0;
5317 files[depth].matched = FALSE;
5318#ifdef FEAT_INS_EXPAND
5319 if (action == ACTION_EXPAND)
5320 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005321 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005322 vim_snprintf((char*)IObuff, IOSIZE,
5323 _("Scanning included file: %s"),
5324 (char *)new_fname);
Bram Moolenaar32526b32019-01-19 17:43:09 +01005325 msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005329 if (p_verbose >= 5)
5330 {
5331 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005332 smsg(_("Searching included file %s"),
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005333 (char *)new_fname);
5334 verbose_leave();
5335 }
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 }
5339 }
5340 else
5341 {
5342 /*
5343 * Check if the line is a define (type == FIND_DEFINE)
5344 */
5345 p = line;
5346search_line:
5347 define_matched = FALSE;
5348 if (def_regmatch.regprog != NULL
5349 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5350 {
5351 /*
5352 * Pattern must be first identifier after 'define', so skip
5353 * to that position before checking for match of pattern. Also
5354 * don't let it match beyond the end of this identifier.
5355 */
5356 p = def_regmatch.endp[0];
5357 while (*p && !vim_iswordc(*p))
5358 p++;
5359 define_matched = TRUE;
5360 }
5361
5362 /*
5363 * Look for a match. Don't do this if we are looking for a
5364 * define and this line didn't match define_prog above.
5365 */
5366 if (def_regmatch.regprog == NULL || define_matched)
5367 {
5368 if (define_matched
5369#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#endif
5372 )
5373 {
5374 /* compare the first "len" chars from "ptr" */
5375 startp = skipwhite(p);
5376 if (p_ic)
5377 matched = !MB_STRNICMP(startp, ptr, len);
5378 else
5379 matched = !STRNCMP(startp, ptr, len);
5380 if (matched && define_matched && whole
5381 && vim_iswordc(startp[len]))
5382 matched = FALSE;
5383 }
5384 else if (regmatch.regprog != NULL
5385 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5386 {
5387 matched = TRUE;
5388 startp = regmatch.startp[0];
5389 /*
5390 * Check if the line is not a comment line (unless we are
5391 * looking for a define). A line starting with "# define"
5392 * is not considered to be a comment line.
5393 */
5394 if (!define_matched && skip_comments)
5395 {
5396#ifdef FEAT_COMMENTS
5397 if ((*line != '#' ||
5398 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005399 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 matched = FALSE;
5401
5402 /*
5403 * Also check for a "/ *" or "/ /" before the match.
5404 * Skips lines like "int backwards; / * normal index
5405 * * /" when looking for "normal".
5406 * Note: Doesn't skip "/ *" in comments.
5407 */
5408 p = skipwhite(line);
5409 if (matched
5410 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5411#endif
5412 for (p = line; *p && p < startp; ++p)
5413 {
5414 if (matched
5415 && p[0] == '/'
5416 && (p[1] == '*' || p[1] == '/'))
5417 {
5418 matched = FALSE;
5419 /* After "//" all text is comment */
5420 if (p[1] == '/')
5421 break;
5422 ++p;
5423 }
5424 else if (!matched && p[0] == '*' && p[1] == '/')
5425 {
5426 /* Can find match after "* /". */
5427 matched = TRUE;
5428 ++p;
5429 }
5430 }
5431 }
5432 }
5433 }
5434 }
5435 if (matched)
5436 {
5437#ifdef FEAT_INS_EXPAND
5438 if (action == ACTION_EXPAND)
5439 {
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005440 int cont_s_ipos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 int add_r;
5442 char_u *aux;
5443
5444 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5445 break;
5446 found = TRUE;
5447 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005448 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 if (vim_iswordp(p))
5452 goto exit_matched;
5453 p = find_word_start(p);
5454 }
5455 p = find_word_end(p);
5456 i = (int)(p - aux);
5457
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005458 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005460 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005462
5463 /* Get the next line: when "depth" < 0 from the current
5464 * buffer, otherwise from the included file. Jump to
5465 * exit_matched when past the last line. */
5466 if (depth < 0)
5467 {
5468 if (lnum >= end_lnum)
5469 goto exit_matched;
5470 line = ml_get(++lnum);
5471 }
5472 else if (vim_fgets(line = file_line,
5473 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 goto exit_matched;
5475
5476 /* we read a line, set "already" to check this "line" later
5477 * if depth >= 0 we'll increase files[depth].lnum far
5478 * bellow -- Acevedo */
5479 already = aux = p = skipwhite(line);
5480 p = find_word_start(p);
5481 p = find_word_end(p);
5482 if (p > aux)
5483 {
5484 if (*aux != ')' && IObuff[i-1] != TAB)
5485 {
5486 if (IObuff[i-1] != ' ')
5487 IObuff[i++] = ' ';
5488 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5489 if (p_js
5490 && (IObuff[i-2] == '.'
5491 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5492 && (IObuff[i-2] == '?'
5493 || IObuff[i-2] == '!'))))
5494 IObuff[i++] = ' ';
5495 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005496 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 if (p - aux >= IOSIZE - i)
5498 p = aux + IOSIZE - i - 1;
5499 STRNCPY(IObuff + i, aux, p - aux);
5500 i += (int)(p - aux);
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005501 cont_s_ipos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
5503 IObuff[i] = NUL;
5504 aux = IObuff;
5505
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 goto exit_matched;
5508 }
5509
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005510 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 curr_fname == curbuf->b_fname ? NULL : curr_fname,
Bram Moolenaard9eefe32019-04-06 14:22:21 +02005512 dir, cont_s_ipos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 if (add_r == OK)
5514 /* if dir was BACKWARD then honor it just once */
5515 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005516 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 break;
5518 }
5519 else
5520#endif
5521 if (action == ACTION_SHOW_ALL)
5522 {
5523 found = TRUE;
5524 if (!did_show)
5525 gotocmdline(TRUE); /* cursor at status line */
5526 if (curr_fname != prev_fname)
5527 {
5528 if (did_show)
5529 msg_putchar('\n'); /* cursor below last one */
5530 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005531 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 msg_home_replace_hl(curr_fname);
5533 prev_fname = curr_fname;
5534 }
5535 did_show = TRUE;
5536 if (!got_int)
5537 show_pat_in_path(line, type, TRUE, action,
5538 (depth == -1) ? NULL : files[depth].fp,
5539 (depth == -1) ? &lnum : &files[depth].lnum,
5540 match_count++);
5541
5542 /* Set matched flag for this file and all the ones that
5543 * include it */
5544 for (i = 0; i <= depth; ++i)
5545 files[i].matched = TRUE;
5546 }
5547 else if (--count <= 0)
5548 {
5549 found = TRUE;
5550 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005551#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 && g_do_tagpreview == 0
5553#endif
5554 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005555 emsg(_("E387: Match is on current line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 else if (action == ACTION_SHOW)
5557 {
5558 show_pat_in_path(line, type, did_show, action,
5559 (depth == -1) ? NULL : files[depth].fp,
5560 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5561 did_show = TRUE;
5562 }
5563 else
5564 {
5565#ifdef FEAT_GUI
5566 need_mouse_correct = TRUE;
5567#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005568#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 /* ":psearch" uses the preview window */
5570 if (g_do_tagpreview != 0)
5571 {
5572 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005573 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575#endif
5576 if (action == ACTION_SPLIT)
5577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005580 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 if (depth == -1)
5583 {
5584 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005585#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 if (g_do_tagpreview != 0)
5587 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005588 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005589 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005590 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 break; /* failed to jump to file */
5592 }
5593 else
5594#endif
5595 setpcmark();
5596 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005597 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 }
5599 else
5600 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005601 if (!GETFILE_SUCCESS(getfile(
5602 0, files[depth].name, NULL, TRUE,
5603 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 break; /* failed to jump to file */
5605 /* autocommands may have changed the lnum, we don't
5606 * want that here */
5607 curwin->w_cursor.lnum = files[depth].lnum;
5608 }
5609 }
5610 if (action != ACTION_SHOW)
5611 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005612 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 curwin->w_set_curswant = TRUE;
5614 }
5615
Bram Moolenaar4033c552017-09-16 20:54:51 +02005616#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005618 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
5620 /* Return cursor to where we were */
5621 validate_cursor();
5622 redraw_later(VALID);
5623 win_enter(curwin_save, TRUE);
5624 }
5625#endif
5626 break;
5627 }
5628#ifdef FEAT_INS_EXPAND
5629exit_matched:
5630#endif
5631 matched = FALSE;
5632 /* look for other matches in the rest of the line if we
5633 * are not at the end of it already */
5634 if (def_regmatch.regprog == NULL
5635#ifdef FEAT_INS_EXPAND
5636 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005637 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005639 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005640 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 goto search_line;
5642 }
5643 line_breakcheck();
5644#ifdef FEAT_INS_EXPAND
5645 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005646 ins_compl_check_keys(30, FALSE);
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005647 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#else
5649 if (got_int)
5650#endif
5651 break;
5652
5653 /*
5654 * Read the next line. When reading an included file and encountering
5655 * end-of-file, close the file and continue in the file that included
5656 * it.
5657 */
5658 while (depth >= 0 && !already
5659 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5660 {
5661 fclose(files[depth].fp);
5662 --old_files;
5663 files[old_files].name = files[depth].name;
5664 files[old_files].matched = files[depth].matched;
5665 --depth;
5666 curr_fname = (depth == -1) ? curbuf->b_fname
5667 : files[depth].name;
5668 if (depth < depth_displayed)
5669 depth_displayed = depth;
5670 }
5671 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005672 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005674 /* Remove any CR and LF from the line. */
5675 i = (int)STRLEN(line);
5676 if (i > 0 && line[i - 1] == '\n')
5677 line[--i] = NUL;
5678 if (i > 0 && line[i - 1] == '\r')
5679 line[--i] = NUL;
5680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 else if (!already)
5682 {
5683 if (++lnum > end_lnum)
5684 break;
5685 line = ml_get(lnum);
5686 }
5687 already = NULL;
5688 }
5689 /* End of big for (;;) loop. */
5690
5691 /* Close any files that are still open. */
5692 for (i = 0; i <= depth; i++)
5693 {
5694 fclose(files[i].fp);
5695 vim_free(files[i].name);
5696 }
5697 for (i = old_files; i < max_path_depth; i++)
5698 vim_free(files[i].name);
5699 vim_free(files);
5700
5701 if (type == CHECK_PATH)
5702 {
5703 if (!did_show)
5704 {
5705 if (action != ACTION_SHOW_ALL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005706 msg(_("All included files were found"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005708 msg(_("No included files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
5710 }
5711 else if (!found
5712#ifdef FEAT_INS_EXPAND
5713 && action != ACTION_EXPAND
5714#endif
5715 )
5716 {
5717#ifdef FEAT_INS_EXPAND
Bram Moolenaar7591bb32019-03-30 13:53:47 +01005718 if (got_int || ins_compl_interrupted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719#else
5720 if (got_int)
5721#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005722 emsg(_(e_interr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 else if (type == FIND_DEFINE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005724 emsg(_("E388: Couldn't find definition"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005726 emsg(_("E389: Couldn't find pattern"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5729 msg_end();
5730
5731fpip_end:
5732 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005733 vim_regfree(regmatch.regprog);
5734 vim_regfree(incl_regmatch.regprog);
5735 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736}
5737
5738 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005739show_pat_in_path(
5740 char_u *line,
5741 int type,
5742 int did_show,
5743 int action,
5744 FILE *fp,
5745 linenr_T *lnum,
5746 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 char_u *p;
5749
5750 if (did_show)
5751 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005752 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 gotocmdline(TRUE); /* cursor at status line */
5754 if (got_int) /* 'q' typed at "--more--" message */
5755 return;
5756 for (;;)
5757 {
5758 p = line + STRLEN(line) - 1;
5759 if (fp != NULL)
5760 {
5761 /* We used fgets(), so get rid of newline at end */
5762 if (p >= line && *p == '\n')
5763 --p;
5764 if (p >= line && *p == '\r')
5765 --p;
5766 *(p + 1) = NUL;
5767 }
5768 if (action == ACTION_SHOW_ALL)
5769 {
5770 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005771 msg_puts((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5773 /* Highlight line numbers */
Bram Moolenaar32526b32019-01-19 17:43:09 +01005774 msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N));
5775 msg_puts(" ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005777 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 out_flush(); /* show one line at a time */
5779
5780 /* Definition continues until line that doesn't end with '\' */
5781 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5782 break;
5783
5784 if (fp != NULL)
5785 {
5786 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5787 break;
5788 ++*lnum;
5789 }
5790 else
5791 {
5792 if (++*lnum > curbuf->b_ml.ml_line_count)
5793 break;
5794 line = ml_get(*lnum);
5795 }
5796 msg_putchar('\n');
5797 }
5798}
5799#endif
5800
5801#ifdef FEAT_VIMINFO
5802 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005803read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804{
5805 char_u *lp;
5806 int idx = -1;
5807 int magic = FALSE;
5808 int no_scs = FALSE;
5809 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005810 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 long off = 0;
5812 int setlast = FALSE;
5813#ifdef FEAT_SEARCH_EXTRA
5814 static int hlsearch_on = FALSE;
5815#endif
5816 char_u *val;
5817
5818 /*
5819 * Old line types:
5820 * "/pat", "&pat": search/subst. pat
5821 * "~/pat", "~&pat": last used search/subst. pat
5822 * New line types:
5823 * "~h", "~H": hlsearch highlighting off/on
5824 * "~<magic><smartcase><line><end><off><last><which>pat"
5825 * <magic>: 'm' off, 'M' on
5826 * <smartcase>: 's' off, 'S' on
5827 * <line>: 'L' line offset, 'l' char offset
5828 * <end>: 'E' from end, 'e' from start
5829 * <off>: decimal, offset
5830 * <last>: '~' last used pattern
5831 * <which>: '/' search pat, '&' subst. pat
5832 */
5833 lp = virp->vir_line;
5834 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5835 {
5836 if (lp[1] == 'M') /* magic on */
5837 magic = TRUE;
5838 if (lp[2] == 's')
5839 no_scs = TRUE;
5840 if (lp[3] == 'L')
5841 off_line = TRUE;
5842 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005843 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 lp += 5;
5845 off = getdigits(&lp);
5846 }
5847 if (lp[0] == '~') /* use this pattern for last-used pattern */
5848 {
5849 setlast = TRUE;
5850 lp++;
5851 }
5852 if (lp[0] == '/')
5853 idx = RE_SEARCH;
5854 else if (lp[0] == '&')
5855 idx = RE_SUBST;
5856#ifdef FEAT_SEARCH_EXTRA
5857 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5858 hlsearch_on = FALSE;
5859 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5860 hlsearch_on = TRUE;
5861#endif
5862 if (idx >= 0)
5863 {
5864 if (force || spats[idx].pat == NULL)
5865 {
5866 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5867 TRUE);
5868 if (val != NULL)
5869 {
5870 set_last_search_pat(val, idx, magic, setlast);
5871 vim_free(val);
5872 spats[idx].no_scs = no_scs;
5873 spats[idx].off.line = off_line;
5874 spats[idx].off.end = off_end;
5875 spats[idx].off.off = off;
5876#ifdef FEAT_SEARCH_EXTRA
5877 if (setlast)
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02005878 set_no_hlsearch(!hlsearch_on);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879#endif
5880 }
5881 }
5882 }
5883 return viminfo_readline(virp);
5884}
5885
5886 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005887write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888{
5889 if (get_viminfo_parameter('/') != 0)
5890 {
5891#ifdef FEAT_SEARCH_EXTRA
5892 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5893 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5894#endif
5895 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005896 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
5898}
5899
5900 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005901wvsp_one(
5902 FILE *fp, /* file to write to */
5903 int idx, /* spats[] index */
5904 char *s, /* search pat */
5905 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 if (spats[idx].pat != NULL)
5908 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005909 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 /* off.dir is not stored, it's reset to forward */
5911 fprintf(fp, "%c%c%c%c%ld%s%c",
5912 spats[idx].magic ? 'M' : 'm', /* magic */
5913 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5914 spats[idx].off.line ? 'L' : 'l', /* line offset */
5915 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5916 spats[idx].off.off, /* offset */
5917 last_idx == idx ? "~" : "", /* last used pat */
5918 sc);
5919 viminfo_writestring(fp, spats[idx].pat);
5920 }
5921}
5922#endif /* FEAT_VIMINFO */