blob: 98665f795fc07c2dcb5349ede72a840c1c133d87 [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
29
Bram Moolenaar071d4272004-06-13 20:20:40 +000030/*
31 * This file contains various searching-related routines. These fall into
32 * three groups:
33 * 1. string searches (for /, ?, n, and N)
34 * 2. character searches within a single line (for f, F, t, T, etc)
35 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
36 */
37
38/*
39 * String searches
40 *
41 * The string search functions are divided into two levels:
42 * lowest: searchit(); uses an pos_T for starting position and found match.
43 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
44 *
45 * The last search pattern is remembered for repeating the same search.
46 * This pattern is shared between the :g, :s, ? and / commands.
47 * This is in search_regcomp().
48 *
49 * The actual string matching is done using a heavily modified version of
50 * Henry Spencer's regular expression library. See regexp.c.
51 */
52
53/* The offset for a search command is store in a soff struct */
54/* Note: only spats[0].off is really used */
55struct soffset
56{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000057 int dir; /* search direction, '/' or '?' */
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 int line; /* search has line offset */
59 int end; /* search set cursor at end */
60 long off; /* line or char offset */
61};
62
63/* A search pattern and its attributes are stored in a spat struct */
64struct spat
65{
66 char_u *pat; /* the pattern (in allocated memory) or NULL */
67 int magic; /* magicness of the pattern */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020068 int no_scs; /* no smartcase for this pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 struct soffset off;
70};
71
72/*
73 * Two search patterns are remembered: One for the :substitute command and
74 * one for other searches. last_idx points to the one that was used the last
75 * time.
76 */
77static struct spat spats[2] =
78{
79 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
80 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
81};
82
83static int last_idx = 0; /* index in spats[] for RE_LAST */
84
Bram Moolenaardbd24b52015-08-11 14:26:19 +020085static char_u lastc[2] = {NUL, NUL}; /* last character searched for */
86static int lastcdir = FORWARD; /* last direction of character search */
87static int last_t_cmd = TRUE; /* last search t_cmd */
88#ifdef FEAT_MBYTE
89static char_u lastc_bytes[MB_MAXBYTES + 1];
90static int lastc_bytelen = 1; /* >1 for multi-byte char */
91#endif
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093/* copy of spats[], for keeping the search patterns while executing autocmds */
94static struct spat saved_spats[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000095# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +010096/* copy of spats[RE_SEARCH], for keeping the search patterns while incremental
97 * searching */
98static struct spat saved_last_search_spat;
99static int saved_last_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100static int saved_no_hlsearch = 0;
101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102
103static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
104#ifdef FEAT_RIGHTLEFT
105static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#endif
107
108#ifdef FEAT_FIND_ID
109/*
110 * Type used by find_pattern_in_path() to remember which included files have
111 * been searched already.
112 */
113typedef struct SearchedFile
114{
115 FILE *fp; /* File pointer */
116 char_u *name; /* Full name of file */
117 linenr_T lnum; /* Line we were up to in file */
118 int matched; /* Found a match in this file */
119} SearchedFile;
120#endif
121
122/*
123 * translate search pattern for vim_regcomp()
124 *
125 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
126 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
127 * pat_save == RE_BOTH: save pat in both patterns (:global command)
128 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000129 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
131 * options & SEARCH_HIS: put search string in history
132 * options & SEARCH_KEEP: keep previous search pattern
133 *
134 * returns FAIL if failed, OK otherwise.
135 */
136 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100137search_regcomp(
138 char_u *pat,
139 int pat_save,
140 int pat_use,
141 int options,
142 regmmatch_T *regmatch) /* return: pattern and ignore-case flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143{
144 int magic;
145 int i;
146
147 rc_did_emsg = FALSE;
148 magic = p_magic;
149
150 /*
151 * If no pattern given, use a previously defined pattern.
152 */
153 if (pat == NULL || *pat == NUL)
154 {
155 if (pat_use == RE_LAST)
156 i = last_idx;
157 else
158 i = pat_use;
159 if (spats[i].pat == NULL) /* pattern was never defined */
160 {
161 if (pat_use == RE_SUBST)
162 EMSG(_(e_nopresub));
163 else
164 EMSG(_(e_noprevre));
165 rc_did_emsg = TRUE;
166 return FAIL;
167 }
168 pat = spats[i].pat;
169 magic = spats[i].magic;
170 no_smartcase = spats[i].no_scs;
171 }
172#ifdef FEAT_CMDHIST
173 else if (options & SEARCH_HIS) /* put new pattern in history */
174 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
175#endif
176
177#ifdef FEAT_RIGHTLEFT
178 if (mr_pattern_alloced)
179 {
180 vim_free(mr_pattern);
181 mr_pattern_alloced = FALSE;
182 }
183
184 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
185 {
186 char_u *rev_pattern;
187
188 rev_pattern = reverse_text(pat);
189 if (rev_pattern == NULL)
190 mr_pattern = pat; /* out of memory, keep normal pattern. */
191 else
192 {
193 mr_pattern = rev_pattern;
194 mr_pattern_alloced = TRUE;
195 }
196 }
197 else
198#endif
199 mr_pattern = pat;
200
201 /*
202 * Save the currently used pattern in the appropriate place,
203 * unless the pattern should not be remembered.
204 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100205 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 {
207 /* search or global command */
208 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
209 save_re_pat(RE_SEARCH, pat, magic);
210 /* substitute or global command */
211 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
212 save_re_pat(RE_SUBST, pat, magic);
213 }
214
215 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000216 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
218 if (regmatch->regprog == NULL)
219 return FAIL;
220 return OK;
221}
222
223/*
224 * Get search pattern used by search_regcomp().
225 */
226 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100227get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
229 return mr_pattern;
230}
231
Bram Moolenaarabc97732007-08-08 20:49:37 +0000232#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233/*
234 * Reverse text into allocated memory.
235 * Returns the allocated string, NULL when out of memory.
236 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000237 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100238reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239{
240 unsigned len;
241 unsigned s_i, rev_i;
242 char_u *rev;
243
244 /*
245 * Reverse the pattern.
246 */
247 len = (unsigned)STRLEN(s);
248 rev = alloc(len + 1);
249 if (rev != NULL)
250 {
251 rev_i = len;
252 for (s_i = 0; s_i < len; ++s_i)
253 {
254# ifdef FEAT_MBYTE
255 if (has_mbyte)
256 {
257 int mb_len;
258
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000259 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 rev_i -= mb_len;
261 mch_memmove(rev + rev_i, s + s_i, mb_len);
262 s_i += mb_len - 1;
263 }
264 else
265# endif
266 rev[--rev_i] = s[s_i];
267
268 }
269 rev[len] = NUL;
270 }
271 return rev;
272}
273#endif
274
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100275 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100276save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277{
278 if (spats[idx].pat != pat)
279 {
280 vim_free(spats[idx].pat);
281 spats[idx].pat = vim_strsave(pat);
282 spats[idx].magic = magic;
283 spats[idx].no_scs = no_smartcase;
284 last_idx = idx;
285#ifdef FEAT_SEARCH_EXTRA
286 /* If 'hlsearch' set and search pat changed: need redraw. */
287 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000288 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200289 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000290#endif
291 }
292}
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294/*
295 * Save the search patterns, so they can be restored later.
296 * Used before/after executing autocommands and user functions.
297 */
298static int save_level = 0;
299
300 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100301save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302{
303 if (save_level++ == 0)
304 {
305 saved_spats[0] = spats[0];
306 if (spats[0].pat != NULL)
307 saved_spats[0].pat = vim_strsave(spats[0].pat);
308 saved_spats[1] = spats[1];
309 if (spats[1].pat != NULL)
310 saved_spats[1].pat = vim_strsave(spats[1].pat);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100311#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 saved_last_idx = last_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 saved_no_hlsearch = no_hlsearch;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 }
316}
317
318 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100319restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320{
321 if (--save_level == 0)
322 {
323 vim_free(spats[0].pat);
324 spats[0] = saved_spats[0];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100325#if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000326 set_vv_searchforward();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 vim_free(spats[1].pat);
329 spats[1] = saved_spats[1];
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200332 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 }
335}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000337#if defined(EXITFREE) || defined(PROTO)
338 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100339free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000340{
341 vim_free(spats[0].pat);
342 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000343
344# ifdef FEAT_RIGHTLEFT
345 if (mr_pattern_alloced)
346 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200347 vim_free(mr_pattern);
348 mr_pattern_alloced = FALSE;
349 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000350 }
351# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000352}
353#endif
354
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100355#ifdef FEAT_SEARCH_EXTRA
356/*
357 * Save and restore the search pattern for incremental highlight search
358 * feature.
359 *
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100360 * It's similar to but different from save_search_patterns() and
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100361 * restore_search_patterns(), because the search pattern must be restored when
Bram Moolenaarc4568ab2018-11-16 16:21:05 +0100362 * canceling incremental searching even if it's called inside user functions.
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100363 */
364 void
365save_last_search_pattern(void)
366{
367 saved_last_search_spat = spats[RE_SEARCH];
368 if (spats[RE_SEARCH].pat != NULL)
369 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
370 saved_last_idx = last_idx;
371 saved_no_hlsearch = no_hlsearch;
372}
373
374 void
375restore_last_search_pattern(void)
376{
377 vim_free(spats[RE_SEARCH].pat);
378 spats[RE_SEARCH] = saved_last_search_spat;
379# if defined(FEAT_EVAL)
380 set_vv_searchforward();
381# endif
382 last_idx = saved_last_idx;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +0200383 set_no_hlsearch(saved_no_hlsearch);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100384}
Bram Moolenaard0480092017-11-16 22:20:39 +0100385
386 char_u *
387last_search_pattern(void)
388{
389 return spats[RE_SEARCH].pat;
390}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100391#endif
392
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393/*
394 * Return TRUE when case should be ignored for search pattern "pat".
395 * Uses the 'ignorecase' and 'smartcase' options.
396 */
397 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100398ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200400 return ignorecase_opt(pat, p_ic, p_scs);
401}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200403/*
404 * As ignorecase() put pass the "ic" and "scs" flags.
405 */
406 int
407ignorecase_opt(char_u *pat, int ic_in, int scs)
408{
409 int ic = ic_in;
410
411 if (ic && !no_smartcase && scs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#ifdef FEAT_INS_EXPAND
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100413 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414#endif
415 )
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200416 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 no_smartcase = FALSE;
418
419 return ic;
420}
421
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200422/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200423 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200424 */
425 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100426pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200427{
428 char_u *p = pat;
429
430 while (*p != NUL)
431 {
432#ifdef FEAT_MBYTE
433 int l;
434
435 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
436 {
437 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
438 return TRUE;
439 p += l;
440 }
441 else
442#endif
443 if (*p == '\\')
444 {
445 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
446 p += 3;
447 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
448 p += 3;
449 else if (p[1] != NUL) /* skip "\X" */
450 p += 2;
451 else
452 p += 1;
453 }
454 else if (MB_ISUPPER(*p))
455 return TRUE;
456 else
457 ++p;
458 }
459 return FALSE;
460}
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100463last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200464{
465#ifdef FEAT_MBYTE
466 return lastc_bytes;
467#else
468 return lastc;
469#endif
470}
471
472 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100473last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200474{
475 return lastcdir == FORWARD;
476}
477
478 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100479last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200480{
481 return last_t_cmd == TRUE;
482}
483
484 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100485set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200486{
487 *lastc = c;
488#ifdef FEAT_MBYTE
489 lastc_bytelen = len;
490 if (len)
491 memcpy(lastc_bytes, s, len);
492 else
493 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
494#endif
495}
496
497 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100498set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200499{
500 lastcdir = cdir;
501}
502
503 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100504set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200505{
506 last_t_cmd = t_cmd;
507}
508
509 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100510last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
512 return spats[last_idx].pat;
513}
514
515/*
516 * Reset search direction to forward. For "gd" and "gD" commands.
517 */
518 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100519reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520{
521 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000522#if defined(FEAT_EVAL)
523 set_vv_searchforward();
524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525}
526
527#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
528/*
529 * Set the last search pattern. For ":let @/ =" and viminfo.
530 * Also set the saved search pattern, so that this works in an autocommand.
531 */
532 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100533set_last_search_pat(
534 char_u *s,
535 int idx,
536 int magic,
537 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538{
539 vim_free(spats[idx].pat);
540 /* An empty string means that nothing should be matched. */
541 if (*s == NUL)
542 spats[idx].pat = NULL;
543 else
544 spats[idx].pat = vim_strsave(s);
545 spats[idx].magic = magic;
546 spats[idx].no_scs = FALSE;
547 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000548#if defined(FEAT_EVAL)
549 set_vv_searchforward();
550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 spats[idx].off.line = FALSE;
552 spats[idx].off.end = FALSE;
553 spats[idx].off.off = 0;
554 if (setlast)
555 last_idx = idx;
556 if (save_level)
557 {
558 vim_free(saved_spats[idx].pat);
559 saved_spats[idx] = spats[0];
560 if (spats[idx].pat == NULL)
561 saved_spats[idx].pat = NULL;
562 else
563 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
564 saved_last_idx = last_idx;
565 }
566# ifdef FEAT_SEARCH_EXTRA
567 /* If 'hlsearch' set and search pat changed: need redraw. */
568 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000569 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570# endif
571}
572#endif
573
574#ifdef FEAT_SEARCH_EXTRA
575/*
576 * Get a regexp program for the last used search pattern.
577 * This is used for highlighting all matches in a window.
578 * Values returned in regmatch->regprog and regmatch->rmm_ic.
579 */
580 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100581last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583 if (spats[last_idx].pat == NULL)
584 {
585 regmatch->regprog = NULL;
586 return;
587 }
588 ++emsg_off; /* So it doesn't beep if bad expr */
589 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
590 --emsg_off;
591}
592#endif
593
594/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100595 * Lowest level search function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
597 * Start at position 'pos' and return the found position in 'pos'.
598 *
599 * if (options & SEARCH_MSG) == 0 don't give any messages
600 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
601 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
602 * if (options & SEARCH_HIS) put search pattern in history
603 * if (options & SEARCH_END) return position at end of match
604 * if (options & SEARCH_START) accept match at pos itself
605 * if (options & SEARCH_KEEP) keep previous search pattern
606 * if (options & SEARCH_FOLD) match only once in a closed fold
607 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100608 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 *
610 * Return FAIL (zero) for failure, non-zero for success.
611 * When FEAT_EVAL is defined, returns the index of the first matching
612 * subpattern plus one; one if there was none.
613 */
614 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100615searchit(
616 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100618 buf_T *buf,
619 pos_T *pos,
620 int dir,
621 char_u *pat,
622 long count,
623 int options,
624 int pat_use, /* which pattern to use when "pat" is empty */
625 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200626 proftime_T *tm UNUSED, /* timeout limit or NULL */
627 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628{
629 int found;
630 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100631 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 regmmatch_T regmatch;
633 char_u *ptr;
634 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000636 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int loop;
638 pos_T start_pos;
639 int at_first_line;
640 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200641 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int match_ok;
643 long nmatched;
644 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100645 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000646 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#ifdef FEAT_SEARCH_EXTRA
648 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#endif
650
651 if (search_regcomp(pat, RE_SEARCH, pat_use,
652 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
653 {
654 if ((options & SEARCH_MSG) && !rc_did_emsg)
655 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
656 return FAIL;
657 }
658
Bram Moolenaar280f1262006-01-30 00:14:18 +0000659 /*
660 * find the string
661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 called_emsg = FALSE;
663 do /* loop for count */
664 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100665 /* When not accepting a match at the start position set "extra_col" to
666 * a non-zero value. Don't do that when starting at MAXCOL, since
667 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200668 if (pos->col == MAXCOL)
669 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100670#ifdef FEAT_MBYTE
671 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200672 else if (has_mbyte
673 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
674 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100675 {
Bram Moolenaar82846a02018-02-09 18:09:54 +0100676 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar8846ac52018-02-09 19:24:01 +0100677 if ((int)STRLEN(ptr) <= pos->col)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200678 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100679 else
Bram Moolenaar82846a02018-02-09 18:09:54 +0100680 start_char_len = (*mb_ptr2len)(ptr + pos->col);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100681 }
682#endif
683 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200684 start_char_len = 1;
685 if (dir == FORWARD)
686 {
687 if (options & SEARCH_START)
688 extra_col = 0;
689 else
690 extra_col = start_char_len;
691 }
692 else
693 {
694 if (options & SEARCH_START)
695 extra_col = start_char_len;
696 else
697 extra_col = 0;
698 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100699
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 start_pos = *pos; /* remember start pos for detecting no match */
701 found = 0; /* default: not found */
702 at_first_line = TRUE; /* default: start in first line */
703 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
704 {
705 pos->lnum = 1;
706 pos->col = 0;
707 at_first_line = FALSE; /* not in first line now */
708 }
709
710 /*
711 * Start searching in current line, unless searching backwards and
712 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000713 * If we are searching backwards, in column 0, and not including the
714 * current position, gain some efficiency by skipping back a line.
715 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000717 if (dir == BACKWARD && start_pos.col == 0
718 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
720 lnum = pos->lnum - 1;
721 at_first_line = FALSE;
722 }
723 else
724 lnum = pos->lnum;
725
726 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
727 {
728 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
729 lnum += dir, at_first_line = FALSE)
730 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000731 /* Stop after checking "stop_lnum", if it's set. */
732 if (stop_lnum != 0 && (dir == FORWARD
733 ? lnum > stop_lnum : lnum < stop_lnum))
734 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000735#ifdef FEAT_RELTIME
736 /* Stop after passing the "tm" time limit. */
737 if (tm != NULL && profile_passed_limit(tm))
738 break;
739#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000740
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000742 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100744 col = at_first_line && (options & SEARCH_COL) ? pos->col
745 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100747 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000748#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200749 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000750#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200751 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000752#endif
753 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200755 if (called_emsg
756#ifdef FEAT_RELTIME
757 || (timed_out != NULL && *timed_out)
758#endif
759 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 break;
761 if (nmatched > 0)
762 {
763 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000764 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000766#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000768#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000769 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000770 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
771 ptr = (char_u *)"";
772 else
773 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 /*
776 * Forward search in the first line: match should be after
777 * the start position. If not, continue at the end of the
778 * match (this is vi compatible) or on the next char.
779 */
780 if (dir == FORWARD && at_first_line)
781 {
782 match_ok = TRUE;
783 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000784 * When the match starts in a next line it's certainly
785 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * When match lands on a NUL the cursor will be put
787 * one back afterwards, compare with that position,
788 * otherwise "/$" will get stuck on end of line.
789 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000790 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100791 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000792 ? (nmatched == 1
793 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000795 : ((int)matchpos.col
796 - (ptr[matchpos.col] == NUL)
797 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
799 /*
800 * If vi-compatible searching, continue at the end
801 * of the match, otherwise continue one position
802 * forward.
803 */
804 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
805 {
806 if (nmatched > 1)
807 {
808 /* end is in next line, thus no match in
809 * this line */
810 match_ok = FALSE;
811 break;
812 }
813 matchcol = endpos.col;
814 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000815 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 && ptr[matchcol] != NUL)
817 {
818#ifdef FEAT_MBYTE
819 if (has_mbyte)
820 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000821 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 else
823#endif
824 ++matchcol;
825 }
826 }
827 else
828 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000829 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (ptr[matchcol] != NUL)
831 {
832#ifdef FEAT_MBYTE
833 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000834 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 + matchcol);
836 else
837#endif
838 ++matchcol;
839 }
840 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200841 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (ptr[matchcol] == NUL
844 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000845 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000846 matchcol,
847#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200848 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000849#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200850 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000851#endif
852 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
854 match_ok = FALSE;
855 break;
856 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000857 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 endpos = regmatch.endpos[0];
859# ifdef FEAT_EVAL
860 submatch = first_submatch(&regmatch);
861# endif
862
863 /* Need to get the line pointer again, a
864 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000865 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 if (!match_ok)
868 continue;
869 }
870 if (dir == BACKWARD)
871 {
872 /*
873 * Now, if there are multiple matches on this line,
874 * we have to get the last one. Or the last one before
875 * the cursor, if we're on that line.
876 * When putting the new cursor at the end, compare
877 * relative to the end of the match.
878 */
879 match_ok = FALSE;
880 for (;;)
881 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000882 /* Remember a position that is before the start
883 * position, we use it if it's the last match in
884 * the line. Always accept a position after
885 * wrapping around. */
886 if (loop
887 || ((options & SEARCH_END)
888 ? (lnum + regmatch.endpos[0].lnum
889 < start_pos.lnum
890 || (lnum + regmatch.endpos[0].lnum
891 == start_pos.lnum
892 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200893 < (int)start_pos.col
894 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000895 : (lnum + regmatch.startpos[0].lnum
896 < start_pos.lnum
897 || (lnum + regmatch.startpos[0].lnum
898 == start_pos.lnum
899 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200900 < (int)start_pos.col
901 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000904 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 endpos = regmatch.endpos[0];
906# ifdef FEAT_EVAL
907 submatch = first_submatch(&regmatch);
908# endif
909 }
910 else
911 break;
912
913 /*
914 * We found a valid match, now check if there is
915 * another one after it.
916 * If vi-compatible searching, continue at the end
917 * of the match, otherwise continue one position
918 * forward.
919 */
920 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
921 {
922 if (nmatched > 1)
923 break;
924 matchcol = endpos.col;
925 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000926 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 && ptr[matchcol] != NUL)
928 {
929#ifdef FEAT_MBYTE
930 if (has_mbyte)
931 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000932 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 else
934#endif
935 ++matchcol;
936 }
937 }
938 else
939 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000940 /* Stop when the match is in a next line. */
941 if (matchpos.lnum > 0)
942 break;
943 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (ptr[matchcol] != NUL)
945 {
946#ifdef FEAT_MBYTE
947 if (has_mbyte)
948 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000949 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 else
951#endif
952 ++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;
1012#ifdef FEAT_MBYTE
1013 if (has_mbyte
1014 && pos->lnum <= buf->b_ml.ml_line_count)
1015 {
1016 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1017 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1018 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001019#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 else
1023 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001024 pos->lnum = lnum + matchpos.lnum;
1025 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027#ifdef FEAT_VIRTUALEDIT
1028 pos->coladd = 0;
1029#endif
1030 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001031 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
1033 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001034 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 search_match_endcol = endpos.col;
1036 break;
1037 }
1038 line_breakcheck(); /* stop if ctrl-C typed */
1039 if (got_int)
1040 break;
1041
1042#ifdef FEAT_SEARCH_EXTRA
1043 /* Cancel searching if a character was typed. Used for
1044 * 'incsearch'. Don't check too often, that would slowdown
1045 * searching too much. */
1046 if ((options & SEARCH_PEEK)
1047 && ((lnum - pos->lnum) & 0x3f) == 0
1048 && char_avail())
1049 {
1050 break_loop = TRUE;
1051 break;
1052 }
1053#endif
1054
1055 if (loop && lnum == start_pos.lnum)
1056 break; /* if second loop, stop where started */
1057 }
1058 at_first_line = FALSE;
1059
1060 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001061 * Stop the search if wrapscan isn't set, "stop_lnum" is
1062 * specified, after an interrupt, after a match and after looping
1063 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001065 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001066#ifdef FEAT_RELTIME
1067 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001068#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001069#ifdef FEAT_SEARCH_EXTRA
1070 || break_loop
1071#endif
1072 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 break;
1074
1075 /*
1076 * If 'wrapscan' is set we continue at the other end of the file.
1077 * If 'shortmess' does not contain 's', we give a message.
1078 * This message is also remembered in keep_msg for when the screen
1079 * is redrawn. The keep_msg is cleared whenever another message is
1080 * written.
1081 */
1082 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001086 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1087 give_warning((char_u *)_(dir == BACKWARD
1088 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001090 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001091#ifdef FEAT_RELTIME
1092 || (timed_out != NULL && *timed_out)
1093#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001094#ifdef FEAT_SEARCH_EXTRA
1095 || break_loop
1096#endif
1097 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 break;
1099 }
1100 while (--count > 0 && found); /* stop after count matches or no match */
1101
Bram Moolenaar473de612013-06-08 18:19:48 +02001102 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103
Bram Moolenaar280f1262006-01-30 00:14:18 +00001104 called_emsg |= save_called_emsg;
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (!found) /* did not find it */
1107 {
1108 if (got_int)
1109 EMSG(_(e_interr));
1110 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1111 {
1112 if (p_ws)
1113 EMSG2(_(e_patnotf2), mr_pattern);
1114 else if (lnum == 0)
1115 EMSG2(_("E384: search hit TOP without match for: %s"),
1116 mr_pattern);
1117 else
1118 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
1119 mr_pattern);
1120 }
1121 return FAIL;
1122 }
1123
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001124 /* A pattern like "\n\zs" may go past the last line. */
1125 if (pos->lnum > buf->b_ml.ml_line_count)
1126 {
1127 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001128 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001129 if (pos->col > 0)
1130 --pos->col;
1131 }
1132
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 return submatch + 1;
1134}
1135
1136#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001137 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001138set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001139{
1140 spats[0].off.dir = cdir;
1141}
1142
1143 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001144set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001145{
1146 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1147}
1148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149/*
1150 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001151 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 */
1153 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001154first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155{
1156 int submatch;
1157
1158 for (submatch = 1; ; ++submatch)
1159 {
1160 if (rp->startpos[submatch].lnum >= 0)
1161 break;
1162 if (submatch == 9)
1163 {
1164 submatch = 0;
1165 break;
1166 }
1167 }
1168 return submatch;
1169}
1170#endif
1171
1172/*
1173 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001174 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * If 'dirc' is 0: use previous dir.
1176 * If 'pat' is NULL or empty : use previous string.
1177 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1178 * If 'options & SEARCH_ECHO': echo the search command and handle options
1179 * If 'options & SEARCH_MSG' : may give error message
1180 * If 'options & SEARCH_OPT' : interpret optional flags
1181 * If 'options & SEARCH_HIS' : put search pattern in history
1182 * If 'options & SEARCH_NOOF': don't add offset to position
1183 * If 'options & SEARCH_MARK': set previous context mark
1184 * If 'options & SEARCH_KEEP': keep previous search pattern
1185 * If 'options & SEARCH_START': accept match at curpos itself
1186 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1187 *
1188 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1189 * makes the movement linewise without moving the match position.
1190 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001191 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 */
1193 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001194do_search(
1195 oparg_T *oap, /* can be NULL */
1196 int dirc, /* '/' or '?' */
1197 char_u *pat,
1198 long count,
1199 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001200 proftime_T *tm, /* timeout limit or NULL */
1201 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
1203 pos_T pos; /* position of the last match */
1204 char_u *searchstr;
1205 struct soffset old_off;
1206 int retval; /* Return value */
1207 char_u *p;
1208 long c;
1209 char_u *dircp;
1210 char_u *strcopy = NULL;
1211 char_u *ps;
1212
1213 /*
1214 * A line offset is not remembered, this is vi compatible.
1215 */
1216 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1217 {
1218 spats[0].off.line = FALSE;
1219 spats[0].off.off = 0;
1220 }
1221
1222 /*
1223 * Save the values for when (options & SEARCH_KEEP) is used.
1224 * (there is no "if ()" around this because gcc wants them initialized)
1225 */
1226 old_off = spats[0].off;
1227
1228 pos = curwin->w_cursor; /* start searching at the cursor position */
1229
1230 /*
1231 * Find out the direction of the search.
1232 */
1233 if (dirc == 0)
1234 dirc = spats[0].off.dir;
1235 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001238#if defined(FEAT_EVAL)
1239 set_vv_searchforward();
1240#endif
1241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 if (options & SEARCH_REV)
1243 {
1244#ifdef WIN32
1245 /* There is a bug in the Visual C++ 2.2 compiler which means that
1246 * dirc always ends up being '/' */
1247 dirc = (dirc == '/') ? '?' : '/';
1248#else
1249 if (dirc == '/')
1250 dirc = '?';
1251 else
1252 dirc = '/';
1253#endif
1254 }
1255
1256#ifdef FEAT_FOLDING
1257 /* If the cursor is in a closed fold, don't find another match in the same
1258 * fold. */
1259 if (dirc == '/')
1260 {
1261 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1262 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1263 }
1264 else
1265 {
1266 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1267 pos.col = 0;
1268 }
1269#endif
1270
1271#ifdef FEAT_SEARCH_EXTRA
1272 /*
1273 * Turn 'hlsearch' highlighting back on.
1274 */
1275 if (no_hlsearch && !(options & SEARCH_KEEP))
1276 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001277 redraw_all_later(SOME_VALID);
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001278 set_no_hlsearch(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280#endif
1281
1282 /*
1283 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1284 */
1285 for (;;)
1286 {
1287 searchstr = pat;
1288 dircp = NULL;
1289 /* use previous pattern */
1290 if (pat == NULL || *pat == NUL || *pat == dirc)
1291 {
1292 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1293 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001294 searchstr = spats[RE_SUBST].pat;
1295 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001296 {
1297 EMSG(_(e_noprevre));
1298 retval = 0;
1299 goto end_do_search;
1300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001302 else
1303 {
1304 /* make search_regcomp() use spats[RE_SEARCH].pat */
1305 searchstr = (char_u *)"";
1306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308
1309 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1310 {
1311 /*
1312 * Find end of regular expression.
1313 * If there is a matching '/' or '?', toss it.
1314 */
1315 ps = strcopy;
1316 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1317 if (strcopy != ps)
1318 {
1319 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001320 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 pat = strcopy;
1322 searchstr = strcopy;
1323 }
1324 if (*p == dirc)
1325 {
1326 dircp = p; /* remember where we put the NUL */
1327 *p++ = NUL;
1328 }
1329 spats[0].off.line = FALSE;
1330 spats[0].off.end = FALSE;
1331 spats[0].off.off = 0;
1332 /*
1333 * Check for a line offset or a character offset.
1334 * For get_address (echo off) we don't check for a character
1335 * offset, because it is meaningless and the 's' could be a
1336 * substitute command.
1337 */
1338 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1339 spats[0].off.line = TRUE;
1340 else if ((options & SEARCH_OPT) &&
1341 (*p == 'e' || *p == 's' || *p == 'b'))
1342 {
1343 if (*p == 'e') /* end */
1344 spats[0].off.end = SEARCH_END;
1345 ++p;
1346 }
1347 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1348 {
1349 /* 'nr' or '+nr' or '-nr' */
1350 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1351 spats[0].off.off = atol((char *)p);
1352 else if (*p == '-') /* single '-' */
1353 spats[0].off.off = -1;
1354 else /* single '+' */
1355 spats[0].off.off = 1;
1356 ++p;
1357 while (VIM_ISDIGIT(*p)) /* skip number */
1358 ++p;
1359 }
1360
1361 /* compute length of search command for get_address() */
1362 searchcmdlen += (int)(p - pat);
1363
1364 pat = p; /* put pat after search command */
1365 }
1366
1367 if ((options & SEARCH_ECHO) && messaging()
1368 && !cmd_silent && msg_silent == 0)
1369 {
1370 char_u *msgbuf;
1371 char_u *trunc;
1372
1373 if (*searchstr == NUL)
1374 p = spats[last_idx].pat;
1375 else
1376 p = searchstr;
1377 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1378 if (msgbuf != NULL)
1379 {
1380 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001381#ifdef FEAT_MBYTE
1382 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1383 {
1384 /* Use a space to draw the composing char on. */
1385 msgbuf[1] = ' ';
1386 STRCPY(msgbuf + 2, p);
1387 }
1388 else
1389#endif
1390 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1392 {
1393 p = msgbuf + STRLEN(msgbuf);
1394 *p++ = dirc;
1395 if (spats[0].off.end)
1396 *p++ = 'e';
1397 else if (!spats[0].off.line)
1398 *p++ = 's';
1399 if (spats[0].off.off > 0 || spats[0].off.line)
1400 *p++ = '+';
1401 if (spats[0].off.off != 0 || spats[0].off.line)
1402 sprintf((char *)p, "%ld", spats[0].off.off);
1403 else
1404 *p = NUL;
1405 }
1406
1407 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001408 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409
1410#ifdef FEAT_RIGHTLEFT
1411 /* The search pattern could be shown on the right in rightleft
1412 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1413 * it would be blanked out again very soon. Show it on the
1414 * left, but do reverse the text. */
1415 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1416 {
1417 char_u *r;
1418
1419 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1420 if (r != NULL)
1421 {
1422 vim_free(trunc);
1423 trunc = r;
1424 }
1425 }
1426#endif
1427 if (trunc != NULL)
1428 {
1429 msg_outtrans(trunc);
1430 vim_free(trunc);
1431 }
1432 else
1433 msg_outtrans(msgbuf);
1434 msg_clr_eos();
1435 msg_check();
1436 vim_free(msgbuf);
1437
1438 gotocmdline(FALSE);
1439 out_flush();
1440 msg_nowait = TRUE; /* don't wait for this message */
1441 }
1442 }
1443
1444 /*
1445 * If there is a character offset, subtract it from the current
1446 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001447 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 * This is not done for a line offset, because then we would not be vi
1449 * compatible.
1450 */
Bram Moolenaared203462004-06-16 11:19:22 +00001451 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
1453 if (spats[0].off.off > 0)
1454 {
1455 for (c = spats[0].off.off; c; --c)
1456 if (decl(&pos) == -1)
1457 break;
1458 if (c) /* at start of buffer */
1459 {
1460 pos.lnum = 0; /* allow lnum == 0 here */
1461 pos.col = MAXCOL;
1462 }
1463 }
1464 else
1465 {
1466 for (c = spats[0].off.off; c; ++c)
1467 if (incl(&pos) == -1)
1468 break;
1469 if (c) /* at end of buffer */
1470 {
1471 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1472 pos.col = 0;
1473 }
1474 }
1475 }
1476
1477#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1478 if (p_altkeymap && curwin->w_p_rl)
1479 lrFswap(searchstr,0);
1480#endif
1481
1482 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1483 searchstr, count, spats[0].off.end + (options &
1484 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1485 + SEARCH_MSG + SEARCH_START
1486 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001487 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 if (dircp != NULL)
1490 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1491 if (c == FAIL)
1492 {
1493 retval = 0;
1494 goto end_do_search;
1495 }
1496 if (spats[0].off.end && oap != NULL)
1497 oap->inclusive = TRUE; /* 'e' includes last character */
1498
1499 retval = 1; /* pattern found */
1500
1501 /*
1502 * Add character and/or line offset
1503 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001504 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 {
1506 if (spats[0].off.line) /* Add the offset to the line number. */
1507 {
1508 c = pos.lnum + spats[0].off.off;
1509 if (c < 1)
1510 pos.lnum = 1;
1511 else if (c > curbuf->b_ml.ml_line_count)
1512 pos.lnum = curbuf->b_ml.ml_line_count;
1513 else
1514 pos.lnum = c;
1515 pos.col = 0;
1516
1517 retval = 2; /* pattern found, line offset added */
1518 }
Bram Moolenaared203462004-06-16 11:19:22 +00001519 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
1521 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001522 c = spats[0].off.off;
1523 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001525 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (incl(&pos) == -1)
1527 break;
1528 }
1529 /* to the left, check for start of file */
1530 else
1531 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001532 while (c++ < 0)
1533 if (decl(&pos) == -1)
1534 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 }
1536 }
1537 }
1538
1539 /*
1540 * The search command can be followed by a ';' to do another search.
1541 * For example: "/pat/;/foo/+3;?bar"
1542 * This is like doing another search command, except:
1543 * - The remembered direction '/' or '?' is from the first search.
1544 * - When an error happens the cursor isn't moved at all.
1545 * Don't do this when called by get_address() (it handles ';' itself).
1546 */
1547 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1548 break;
1549
1550 dirc = *++pat;
1551 if (dirc != '?' && dirc != '/')
1552 {
1553 retval = 0;
1554 EMSG(_("E386: Expected '?' or '/' after ';'"));
1555 goto end_do_search;
1556 }
1557 ++pat;
1558 }
1559
1560 if (options & SEARCH_MARK)
1561 setpcmark();
1562 curwin->w_cursor = pos;
1563 curwin->w_set_curswant = TRUE;
1564
1565end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001566 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 spats[0].off = old_off;
1568 vim_free(strcopy);
1569
1570 return retval;
1571}
1572
1573#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1574/*
1575 * search_for_exact_line(buf, pos, dir, pat)
1576 *
1577 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001578 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001580 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 * Return OK for success, or FAIL if no line found.
1582 */
1583 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001584search_for_exact_line(
1585 buf_T *buf,
1586 pos_T *pos,
1587 int dir,
1588 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 linenr_T start = 0;
1591 char_u *ptr;
1592 char_u *p;
1593
1594 if (buf->b_ml.ml_line_count == 0)
1595 return FAIL;
1596 for (;;)
1597 {
1598 pos->lnum += dir;
1599 if (pos->lnum < 1)
1600 {
1601 if (p_ws)
1602 {
1603 pos->lnum = buf->b_ml.ml_line_count;
1604 if (!shortmess(SHM_SEARCH))
1605 give_warning((char_u *)_(top_bot_msg), TRUE);
1606 }
1607 else
1608 {
1609 pos->lnum = 1;
1610 break;
1611 }
1612 }
1613 else if (pos->lnum > buf->b_ml.ml_line_count)
1614 {
1615 if (p_ws)
1616 {
1617 pos->lnum = 1;
1618 if (!shortmess(SHM_SEARCH))
1619 give_warning((char_u *)_(bot_top_msg), TRUE);
1620 }
1621 else
1622 {
1623 pos->lnum = 1;
1624 break;
1625 }
1626 }
1627 if (pos->lnum == start)
1628 break;
1629 if (start == 0)
1630 start = pos->lnum;
1631 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1632 p = skipwhite(ptr);
1633 pos->col = (colnr_T) (p - ptr);
1634
1635 /* when adding lines the matching line may be empty but it is not
1636 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001637 if ((compl_cont_status & CONT_ADDING)
1638 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 {
1640 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1641 return OK;
1642 }
1643 else if (*p != NUL) /* ignore empty lines */
1644 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001645 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1646 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 return OK;
1648 }
1649 }
1650 return FAIL;
1651}
1652#endif /* FEAT_INS_EXPAND */
1653
1654/*
1655 * Character Searches
1656 */
1657
1658/*
1659 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1660 * position of the character, otherwise move to just before the char.
1661 * Do this "cap->count1" times.
1662 * Return FAIL or OK.
1663 */
1664 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001665searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 int c = cap->nchar; /* char to search for */
1668 int dir = cap->arg; /* TRUE for searching forward */
1669 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 int col;
1671 char_u *p;
1672 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001673 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675 if (c != NUL) /* normal search: remember args for repeat */
1676 {
1677 if (!KeyStuffed) /* don't remember when redoing */
1678 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001679 *lastc = c;
1680 set_csearch_direction(dir);
1681 set_csearch_until(t_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001683 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (cap->ncharC1 != 0)
1685 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001686 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1687 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001689 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1690 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 }
1692#endif
1693 }
1694 }
1695 else /* repeat previous search */
1696 {
Bram Moolenaar454709b2017-03-12 16:37:14 +01001697 if (*lastc == NUL
1698#ifdef FEAT_MBYTE
1699 && lastc_bytelen == 1
1700#endif
1701 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 return FAIL;
1703 if (dir) /* repeat in opposite direction */
1704 dir = -lastcdir;
1705 else
1706 dir = lastcdir;
1707 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001708 c = *lastc;
1709 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001710
1711 /* Force a move of at least one char, so ";" and "," will move the
1712 * cursor, even if the cursor is right in front of char we are looking
1713 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001714 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001715 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 }
1717
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001718 if (dir == BACKWARD)
1719 cap->oap->inclusive = FALSE;
1720 else
1721 cap->oap->inclusive = TRUE;
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 p = ml_get_curline();
1724 col = curwin->w_cursor.col;
1725 len = (int)STRLEN(p);
1726
1727 while (count--)
1728 {
1729#ifdef FEAT_MBYTE
1730 if (has_mbyte)
1731 {
1732 for (;;)
1733 {
1734 if (dir > 0)
1735 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001736 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 if (col >= len)
1738 return FAIL;
1739 }
1740 else
1741 {
1742 if (col == 0)
1743 return FAIL;
1744 col -= (*mb_head_off)(p, p + col - 1) + 1;
1745 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001746 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001748 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 break;
1750 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001751 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001752 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001753 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001754 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756 }
1757 else
1758#endif
1759 {
1760 for (;;)
1761 {
1762 if ((col += dir) < 0 || col >= len)
1763 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001764 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001766 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
1768 }
1769 }
1770
1771 if (t_cmd)
1772 {
1773 /* backup to before the character (possibly double-byte) */
1774 col -= dir;
1775#ifdef FEAT_MBYTE
1776 if (has_mbyte)
1777 {
1778 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001779 /* Landed on the search char which is lastc_bytelen long */
1780 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 else
1782 /* To previous char, which may be multi-byte. */
1783 col -= (*mb_head_off)(p, p + col);
1784 }
1785#endif
1786 }
1787 curwin->w_cursor.col = col;
1788
1789 return OK;
1790}
1791
1792/*
1793 * "Other" Searches
1794 */
1795
1796/*
1797 * findmatch - find the matching paren or brace
1798 *
1799 * Improvement over vi: Braces inside quotes are ignored.
1800 */
1801 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001802findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 return findmatchlimit(oap, initc, 0, 0);
1805}
1806
1807/*
1808 * Return TRUE if the character before "linep[col]" equals "ch".
1809 * Return FALSE if "col" is zero.
1810 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1811 * is NULL.
1812 * Handles multibyte string correctly.
1813 */
1814 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001815check_prevcol(
1816 char_u *linep,
1817 int col,
1818 int ch,
1819 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
1821 --col;
1822#ifdef FEAT_MBYTE
1823 if (col > 0 && has_mbyte)
1824 col -= (*mb_head_off)(linep, linep + col);
1825#endif
1826 if (prevcol)
1827 *prevcol = col;
1828 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1829}
1830
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001831/*
1832 * Raw string start is found at linep[startpos.col - 1].
1833 * Return TRUE if the matching end can be found between startpos and endpos.
1834 */
1835 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001836find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001837{
1838 char_u *p;
1839 char_u *delim_copy;
1840 size_t delim_len;
1841 linenr_T lnum;
1842 int found = FALSE;
1843
1844 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1845 ;
1846 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001847 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001848 if (delim_copy == NULL)
1849 return FALSE;
1850 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1851 {
1852 char_u *line = ml_get(lnum);
1853
1854 for (p = line + (lnum == startpos->lnum
1855 ? startpos->col + 1 : 0); *p; ++p)
1856 {
1857 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1858 break;
1859 if (*p == ')' && p[delim_len + 1] == '"'
1860 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1861 {
1862 found = TRUE;
1863 break;
1864 }
1865 }
1866 if (found)
1867 break;
1868 }
1869 vim_free(delim_copy);
1870 return found;
1871}
1872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873/*
1874 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001875 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1876 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 *
1878 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001879 * character at or after the cursor. Special values:
1880 * '*' look for C-style comment / *
1881 * '/' look for C-style comment / *, ignoring comment-end
1882 * '#' look for preprocessor directives
1883 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 *
1885 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1886 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1887 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1888 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001889 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001890 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001891 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 */
1893
1894 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001895findmatchlimit(
1896 oparg_T *oap,
1897 int initc,
1898 int flags,
1899 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
1901 static pos_T pos; /* current search position */
1902 int findc = 0; /* matching brace */
1903 int c;
1904 int count = 0; /* cumulative number of braces */
1905 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001906 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 int inquote = FALSE; /* TRUE when inside quotes */
1908 char_u *linep; /* pointer to current line */
1909 char_u *ptr;
1910 int do_quotes; /* check for quotes in current line */
1911 int at_start; /* do_quotes value at start position */
1912 int hash_dir = 0; /* Direction searched for # things */
1913 int comment_dir = 0; /* Direction searched for comments */
1914 pos_T match_pos; /* Where last slash-star was found */
1915 int start_in_quotes; /* start position is in quotes */
1916 int traveled = 0; /* how far we've searched so far */
1917 int ignore_cend = FALSE; /* ignore comment end */
1918 int cpo_match; /* vi compatible matching */
1919 int cpo_bsl; /* don't recognize backslashes */
1920 int match_escaped = 0; /* search for escaped match */
1921 int dir; /* Direction to search */
1922 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001923#ifdef FEAT_LISP
1924 int lispcomm = FALSE; /* inside of Lisp-style comment */
1925 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1926#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001929#ifdef FEAT_VIRTUALEDIT
1930 pos.coladd = 0;
1931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 linep = ml_get(pos.lnum);
1933
1934 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1935 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1936
1937 /* Direction to search when initc is '/', '*' or '#' */
1938 if (flags & FM_BACKWARD)
1939 dir = BACKWARD;
1940 else if (flags & FM_FORWARD)
1941 dir = FORWARD;
1942 else
1943 dir = 0;
1944
1945 /*
1946 * if initc given, look in the table for the matching character
1947 * '/' and '*' are special cases: look for start or end of comment.
1948 * When '/' is used, we ignore running backwards into an star-slash, for
1949 * "[*" command, we just want to find any comment.
1950 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001951 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 {
1953 comment_dir = dir;
1954 if (initc == '/')
1955 ignore_cend = TRUE;
1956 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001957 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 initc = NUL;
1959 }
1960 else if (initc != '#' && initc != NUL)
1961 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001962 find_mps_values(&initc, &findc, &backwards, TRUE);
1963 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 return NULL;
1965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 else
1967 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001968 /*
1969 * Either initc is '#', or no initc was given and we need to look
1970 * under the cursor.
1971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (initc == '#')
1973 {
1974 hash_dir = dir;
1975 }
1976 else
1977 {
1978 /*
1979 * initc was not given, must look for something to match under
1980 * or near the cursor.
1981 * Only check for special things when 'cpo' doesn't have '%'.
1982 */
1983 if (!cpo_match)
1984 {
1985 /* Are we before or at #if, #else etc.? */
1986 ptr = skipwhite(linep);
1987 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1988 {
1989 ptr = skipwhite(ptr + 1);
1990 if ( STRNCMP(ptr, "if", 2) == 0
1991 || STRNCMP(ptr, "endif", 5) == 0
1992 || STRNCMP(ptr, "el", 2) == 0)
1993 hash_dir = 1;
1994 }
1995
1996 /* Are we on a comment? */
1997 else if (linep[pos.col] == '/')
1998 {
1999 if (linep[pos.col + 1] == '*')
2000 {
2001 comment_dir = FORWARD;
2002 backwards = FALSE;
2003 pos.col++;
2004 }
2005 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2006 {
2007 comment_dir = BACKWARD;
2008 backwards = TRUE;
2009 pos.col--;
2010 }
2011 }
2012 else if (linep[pos.col] == '*')
2013 {
2014 if (linep[pos.col + 1] == '/')
2015 {
2016 comment_dir = BACKWARD;
2017 backwards = TRUE;
2018 }
2019 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2020 {
2021 comment_dir = FORWARD;
2022 backwards = FALSE;
2023 }
2024 }
2025 }
2026
2027 /*
2028 * If we are not on a comment or the # at the start of a line, then
2029 * look for brace anywhere on this line after the cursor.
2030 */
2031 if (!hash_dir && !comment_dir)
2032 {
2033 /*
2034 * Find the brace under or after the cursor.
2035 * If beyond the end of the line, use the last character in
2036 * the line.
2037 */
2038 if (linep[pos.col] == NUL && pos.col)
2039 --pos.col;
2040 for (;;)
2041 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002042 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (initc == NUL)
2044 break;
2045
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002046 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (findc)
2048 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002049 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 }
2051 if (!findc)
2052 {
2053 /* no brace in the line, maybe use " #if" then */
2054 if (!cpo_match && *skipwhite(linep) == '#')
2055 hash_dir = 1;
2056 else
2057 return NULL;
2058 }
2059 else if (!cpo_bsl)
2060 {
2061 int col, bslcnt = 0;
2062
2063 /* Set "match_escaped" if there are an odd number of
2064 * backslashes. */
2065 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2066 bslcnt++;
2067 match_escaped = (bslcnt & 1);
2068 }
2069 }
2070 }
2071 if (hash_dir)
2072 {
2073 /*
2074 * Look for matching #if, #else, #elif, or #endif
2075 */
2076 if (oap != NULL)
2077 oap->motion_type = MLINE; /* Linewise for this case only */
2078 if (initc != '#')
2079 {
2080 ptr = skipwhite(skipwhite(linep) + 1);
2081 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2082 hash_dir = 1;
2083 else if (STRNCMP(ptr, "endif", 5) == 0)
2084 hash_dir = -1;
2085 else
2086 return NULL;
2087 }
2088 pos.col = 0;
2089 while (!got_int)
2090 {
2091 if (hash_dir > 0)
2092 {
2093 if (pos.lnum == curbuf->b_ml.ml_line_count)
2094 break;
2095 }
2096 else if (pos.lnum == 1)
2097 break;
2098 pos.lnum += hash_dir;
2099 linep = ml_get(pos.lnum);
2100 line_breakcheck(); /* check for CTRL-C typed */
2101 ptr = skipwhite(linep);
2102 if (*ptr != '#')
2103 continue;
2104 pos.col = (colnr_T) (ptr - linep);
2105 ptr = skipwhite(ptr + 1);
2106 if (hash_dir > 0)
2107 {
2108 if (STRNCMP(ptr, "if", 2) == 0)
2109 count++;
2110 else if (STRNCMP(ptr, "el", 2) == 0)
2111 {
2112 if (count == 0)
2113 return &pos;
2114 }
2115 else if (STRNCMP(ptr, "endif", 5) == 0)
2116 {
2117 if (count == 0)
2118 return &pos;
2119 count--;
2120 }
2121 }
2122 else
2123 {
2124 if (STRNCMP(ptr, "if", 2) == 0)
2125 {
2126 if (count == 0)
2127 return &pos;
2128 count--;
2129 }
2130 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2131 {
2132 if (count == 0)
2133 return &pos;
2134 }
2135 else if (STRNCMP(ptr, "endif", 5) == 0)
2136 count++;
2137 }
2138 }
2139 return NULL;
2140 }
2141 }
2142
2143#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002144 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 * paren/brace in the other direction. */
2146 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2147 backwards = !backwards;
2148#endif
2149
2150 do_quotes = -1;
2151 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002152 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002155 if ((backwards && comment_dir)
2156#ifdef FEAT_LISP
2157 || lisp
2158#endif
2159 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002161#ifdef FEAT_LISP
2162 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2163 lispcomm = TRUE; /* find match inside this comment */
2164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 while (!got_int)
2166 {
2167 /*
2168 * Go to the next position, forward or backward. We could use
2169 * inc() and dec() here, but that is much slower
2170 */
2171 if (backwards)
2172 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002173#ifdef FEAT_LISP
2174 /* char to match is inside of comment, don't search outside */
2175 if (lispcomm && pos.col < (colnr_T)comment_col)
2176 break;
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (pos.col == 0) /* at start of line, go to prev. one */
2179 {
2180 if (pos.lnum == 1) /* start of file */
2181 break;
2182 --pos.lnum;
2183
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002184 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 break;
2186
2187 linep = ml_get(pos.lnum);
2188 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2189 do_quotes = -1;
2190 line_breakcheck();
2191
2192 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002193 if (comment_dir
2194#ifdef FEAT_LISP
2195 || lisp
2196#endif
2197 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002199#ifdef FEAT_LISP
2200 /* skip comment */
2201 if (lisp && comment_col != MAXCOL)
2202 pos.col = comment_col;
2203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205 else
2206 {
2207 --pos.col;
2208#ifdef FEAT_MBYTE
2209 if (has_mbyte)
2210 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2211#endif
2212 }
2213 }
2214 else /* forward search */
2215 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002216 if (linep[pos.col] == NUL
2217 /* at end of line, go to next one */
2218#ifdef FEAT_LISP
2219 /* don't search for match in comment */
2220 || (lisp && comment_col != MAXCOL
2221 && pos.col == (colnr_T)comment_col)
2222#endif
2223 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002225 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2226#ifdef FEAT_LISP
2227 /* line is exhausted and comment with it,
2228 * don't search for match in code */
2229 || lispcomm
2230#endif
2231 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 break;
2233 ++pos.lnum;
2234
2235 if (maxtravel && traveled++ > maxtravel)
2236 break;
2237
2238 linep = ml_get(pos.lnum);
2239 pos.col = 0;
2240 do_quotes = -1;
2241 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002242#ifdef FEAT_LISP
2243 if (lisp) /* find comment pos in new line */
2244 comment_col = check_linecomment(linep);
2245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 else
2248 {
2249#ifdef FEAT_MBYTE
2250 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002251 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 else
2253#endif
2254 ++pos.col;
2255 }
2256 }
2257
2258 /*
2259 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2260 */
2261 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2262 (linep[0] == '{' || linep[0] == '}'))
2263 {
2264 if (linep[0] == findc && count == 0) /* match! */
2265 return &pos;
2266 break; /* out of scope */
2267 }
2268
2269 if (comment_dir)
2270 {
2271 /* Note: comments do not nest, and we ignore quotes in them */
2272 /* TODO: ignore comment brackets inside strings */
2273 if (comment_dir == FORWARD)
2274 {
2275 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2276 {
2277 pos.col++;
2278 return &pos;
2279 }
2280 }
2281 else /* Searching backwards */
2282 {
2283 /*
2284 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002285 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 */
2287 if (pos.col == 0)
2288 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002289 else if (raw_string)
2290 {
2291 if (linep[pos.col - 1] == 'R'
2292 && linep[pos.col] == '"'
2293 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2294 {
2295 /* Possible start of raw string. Now that we have the
2296 * delimiter we can check if it ends before where we
2297 * started searching, or before the previously found
2298 * raw string start. */
2299 if (!find_rawstring_end(linep, &pos,
2300 count > 0 ? &match_pos : &curwin->w_cursor))
2301 {
2302 count++;
2303 match_pos = pos;
2304 match_pos.col--;
2305 }
2306 linep = ml_get(pos.lnum); /* may have been released */
2307 }
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 else if ( linep[pos.col - 1] == '/'
2310 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002311 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 && (int)pos.col < comment_col)
2313 {
2314 count++;
2315 match_pos = pos;
2316 match_pos.col--;
2317 }
2318 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2319 {
2320 if (count > 0)
2321 pos = match_pos;
2322 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2323 && (int)pos.col <= comment_col)
2324 pos.col -= 2;
2325 else if (ignore_cend)
2326 continue;
2327 else
2328 return NULL;
2329 return &pos;
2330 }
2331 }
2332 continue;
2333 }
2334
2335 /*
2336 * If smart matching ('cpoptions' does not contain '%'), braces inside
2337 * of quotes are ignored, but only if there is an even number of
2338 * quotes in the line.
2339 */
2340 if (cpo_match)
2341 do_quotes = 0;
2342 else if (do_quotes == -1)
2343 {
2344 /*
2345 * Count the number of quotes in the line, skipping \" and '"'.
2346 * Watch out for "\\".
2347 */
2348 at_start = do_quotes;
2349 for (ptr = linep; *ptr; ++ptr)
2350 {
2351 if (ptr == linep + pos.col + backwards)
2352 at_start = (do_quotes & 1);
2353 if (*ptr == '"'
2354 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2355 ++do_quotes;
2356 if (*ptr == '\\' && ptr[1] != NUL)
2357 ++ptr;
2358 }
2359 do_quotes &= 1; /* result is 1 with even number of quotes */
2360
2361 /*
2362 * If we find an uneven count, check current line and previous
2363 * one for a '\' at the end.
2364 */
2365 if (!do_quotes)
2366 {
2367 inquote = FALSE;
2368 if (ptr[-1] == '\\')
2369 {
2370 do_quotes = 1;
2371 if (start_in_quotes == MAYBE)
2372 {
2373 /* Do we need to use at_start here? */
2374 inquote = TRUE;
2375 start_in_quotes = TRUE;
2376 }
2377 else if (backwards)
2378 inquote = TRUE;
2379 }
2380 if (pos.lnum > 1)
2381 {
2382 ptr = ml_get(pos.lnum - 1);
2383 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2384 {
2385 do_quotes = 1;
2386 if (start_in_quotes == MAYBE)
2387 {
2388 inquote = at_start;
2389 if (inquote)
2390 start_in_quotes = TRUE;
2391 }
2392 else if (!backwards)
2393 inquote = TRUE;
2394 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002395
2396 /* ml_get() only keeps one line, need to get linep again */
2397 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 }
2399 }
2400 }
2401 if (start_in_quotes == MAYBE)
2402 start_in_quotes = FALSE;
2403
2404 /*
2405 * If 'smartmatch' is set:
2406 * Things inside quotes are ignored by setting 'inquote'. If we
2407 * find a quote without a preceding '\' invert 'inquote'. At the
2408 * end of a line not ending in '\' we reset 'inquote'.
2409 *
2410 * In lines with an uneven number of quotes (without preceding '\')
2411 * we do not know which part to ignore. Therefore we only set
2412 * inquote if the number of quotes in a line is even, unless this
2413 * line or the previous one ends in a '\'. Complicated, isn't it?
2414 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002415 c = PTR2CHAR(linep + pos.col);
2416 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 case NUL:
2419 /* at end of line without trailing backslash, reset inquote */
2420 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2421 {
2422 inquote = FALSE;
2423 start_in_quotes = FALSE;
2424 }
2425 break;
2426
2427 case '"':
2428 /* a quote that is preceded with an odd number of backslashes is
2429 * ignored */
2430 if (do_quotes)
2431 {
2432 int col;
2433
2434 for (col = pos.col - 1; col >= 0; --col)
2435 if (linep[col] != '\\')
2436 break;
2437 if ((((int)pos.col - 1 - col) & 1) == 0)
2438 {
2439 inquote = !inquote;
2440 start_in_quotes = FALSE;
2441 }
2442 }
2443 break;
2444
2445 /*
2446 * If smart matching ('cpoptions' does not contain '%'):
2447 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2448 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2449 * skipped, there is never a brace in them.
2450 * Ignore this when finding matches for `'.
2451 */
2452 case '\'':
2453 if (!cpo_match && initc != '\'' && findc != '\'')
2454 {
2455 if (backwards)
2456 {
2457 if (pos.col > 1)
2458 {
2459 if (linep[pos.col - 2] == '\'')
2460 {
2461 pos.col -= 2;
2462 break;
2463 }
2464 else if (linep[pos.col - 2] == '\\' &&
2465 pos.col > 2 && linep[pos.col - 3] == '\'')
2466 {
2467 pos.col -= 3;
2468 break;
2469 }
2470 }
2471 }
2472 else if (linep[pos.col + 1]) /* forward search */
2473 {
2474 if (linep[pos.col + 1] == '\\' &&
2475 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2476 {
2477 pos.col += 3;
2478 break;
2479 }
2480 else if (linep[pos.col + 2] == '\'')
2481 {
2482 pos.col += 2;
2483 break;
2484 }
2485 }
2486 }
2487 /* FALLTHROUGH */
2488
2489 default:
2490#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002491 /*
2492 * For Lisp skip over backslashed (), {} and [].
2493 * (actually, we skip #\( et al)
2494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (curbuf->b_p_lisp
2496 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002497 && pos.col > 1
2498 && check_prevcol(linep, pos.col, '\\', NULL)
2499 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 break;
2501#endif
2502
2503 /* Check for match outside of quotes, and inside of
2504 * quotes when the start is also inside of quotes. */
2505 if ((!inquote || start_in_quotes == TRUE)
2506 && (c == initc || c == findc))
2507 {
2508 int col, bslcnt = 0;
2509
2510 if (!cpo_bsl)
2511 {
2512 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2513 bslcnt++;
2514 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002515 /* Only accept a match when 'M' is in 'cpo' or when escaping
2516 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2518 {
2519 if (c == initc)
2520 count++;
2521 else
2522 {
2523 if (count == 0)
2524 return &pos;
2525 count--;
2526 }
2527 }
2528 }
2529 }
2530 }
2531
2532 if (comment_dir == BACKWARD && count > 0)
2533 {
2534 pos = match_pos;
2535 return &pos;
2536 }
2537 return (pos_T *)NULL; /* never found it */
2538}
2539
2540/*
2541 * Check if line[] contains a / / comment.
2542 * Return MAXCOL if not, otherwise return the column.
2543 * TODO: skip strings.
2544 */
2545 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002546check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547{
2548 char_u *p;
2549
2550 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002551#ifdef FEAT_LISP
2552 /* skip Lispish one-line comments */
2553 if (curbuf->b_p_lisp)
2554 {
2555 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2556 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002557 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002558
2559 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002560 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002561 {
2562 if (*p == '"')
2563 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002564 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002565 {
2566 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002567 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002568 }
2569 else if (p == line || ((p - line) >= 2
2570 /* skip #\" form */
2571 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002572 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002573 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002574 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002575 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2576 break; /* found! */
2577 ++p;
2578 }
2579 }
2580 else
2581 p = NULL;
2582 }
2583 else
2584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 while ((p = vim_strchr(p, '/')) != NULL)
2586 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002587 /* accept a double /, unless it's preceded with * and followed by *,
2588 * because * / / * is an end and start of a C comment */
2589 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 break;
2591 ++p;
2592 }
2593
2594 if (p == NULL)
2595 return MAXCOL;
2596 return (int)(p - line);
2597}
2598
2599/*
2600 * Move cursor briefly to character matching the one under the cursor.
2601 * Used for Insert mode and "r" command.
2602 * Show the match only if it is visible on the screen.
2603 * If there isn't a match, then beep.
2604 */
2605 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002606showmatch(
2607 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 pos_T *lpos, save_cursor;
2610 pos_T mpos;
2611 colnr_T vcol;
2612 long save_so;
2613 long save_siso;
2614#ifdef CURSOR_SHAPE
2615 int save_state;
2616#endif
2617 colnr_T save_dollar_vcol;
2618 char_u *p;
2619
2620 /*
2621 * Only show match for chars in the 'matchpairs' option.
2622 */
2623 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002624 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {
2626#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002627 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002628 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002629#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002630 p += MB_PTR2LEN(p) + 1;
2631 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632#ifdef FEAT_RIGHTLEFT
2633 && !(curwin->w_p_rl ^ p_ri)
2634#endif
2635 )
2636 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002637 p += MB_PTR2LEN(p);
2638 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return;
2640 }
2641
2642 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002643 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002644 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 {
2646 if (!curwin->w_p_wrap)
2647 getvcol(curwin, lpos, NULL, &vcol, NULL);
2648 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002649 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 {
2651 mpos = *lpos; /* save the pos, update_screen() may change it */
2652 save_cursor = curwin->w_cursor;
2653 save_so = p_so;
2654 save_siso = p_siso;
2655 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2656 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002657 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2658 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ++curwin->w_virtcol; /* do display ')' just before "$" */
2660 update_screen(VALID); /* show the new char first */
2661
2662 save_dollar_vcol = dollar_vcol;
2663#ifdef CURSOR_SHAPE
2664 save_state = State;
2665 State = SHOWMATCH;
2666 ui_cursor_shape(); /* may show different cursor shape */
2667#endif
2668 curwin->w_cursor = mpos; /* move to matching char */
2669 p_so = 0; /* don't use 'scrolloff' here */
2670 p_siso = 0; /* don't use 'sidescrolloff' here */
2671 showruler(FALSE);
2672 setcursor();
2673 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002674 out_flush_cursor(TRUE, FALSE);
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2677 * which resets it if the matching position is in a previous line
2678 * and has a higher column number. */
2679 dollar_vcol = save_dollar_vcol;
2680
2681 /*
2682 * brief pause, unless 'm' is present in 'cpo' and a character is
2683 * available.
2684 */
2685 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2686 ui_delay(p_mat * 100L, TRUE);
2687 else if (!char_avail())
2688 ui_delay(p_mat * 100L, FALSE);
2689 curwin->w_cursor = save_cursor; /* restore cursor position */
2690 p_so = save_so;
2691 p_siso = save_siso;
2692#ifdef CURSOR_SHAPE
2693 State = save_state;
2694 ui_cursor_shape(); /* may show different cursor shape */
2695#endif
2696 }
2697 }
2698}
2699
2700/*
Bram Moolenaar85160712018-06-19 18:27:41 +02002701 * Find the start of the next sentence, searching in the direction specified
2702 * by the "dir" argument. The cursor is positioned on the start of the next
2703 * sentence when found. If the next sentence is found, return OK. Return FAIL
2704 * otherwise. See ":h sentence" for the precise definition of a "sentence"
2705 * text object.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
2707 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002708findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 pos_T pos, tpos;
2711 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002712 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 int startlnum;
2714 int noskip = FALSE; /* do not skip blanks */
2715 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002716 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717
2718 pos = curwin->w_cursor;
2719 if (dir == FORWARD)
2720 func = incl;
2721 else
2722 func = decl;
2723
2724 while (count--)
2725 {
2726 /*
2727 * if on an empty line, skip upto a non-empty line
2728 */
2729 if (gchar_pos(&pos) == NUL)
2730 {
2731 do
2732 if ((*func)(&pos) == -1)
2733 break;
2734 while (gchar_pos(&pos) == NUL);
2735 if (dir == FORWARD)
2736 goto found;
2737 }
2738 /*
2739 * if on the start of a paragraph or a section and searching forward,
2740 * go to the next line
2741 */
2742 else if (dir == FORWARD && pos.col == 0 &&
2743 startPS(pos.lnum, NUL, FALSE))
2744 {
2745 if (pos.lnum == curbuf->b_ml.ml_line_count)
2746 return FAIL;
2747 ++pos.lnum;
2748 goto found;
2749 }
2750 else if (dir == BACKWARD)
2751 decl(&pos);
2752
Bram Moolenaar85160712018-06-19 18:27:41 +02002753 // go back to the previous non-white non-punctuation character
Bram Moolenaardef9e822004-12-31 20:58:58 +00002754 found_dot = FALSE;
Bram Moolenaar85160712018-06-19 18:27:41 +02002755 while (c = gchar_pos(&pos), VIM_ISWHITE(c)
2756 || vim_strchr((char_u *)".!?)]\"'", c) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Bram Moolenaar85160712018-06-19 18:27:41 +02002758 tpos = pos;
2759 if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 break;
Bram Moolenaar85160712018-06-19 18:27:41 +02002761
2762 if (found_dot)
2763 break;
2764 if (vim_strchr((char_u *) ".!?", c) != NULL)
2765 found_dot = TRUE;
2766
2767 if (vim_strchr((char_u *) ")]\"'", c) != NULL
2768 && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL)
2769 break;
2770
2771 decl(&pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773
2774 /* remember the line where the search started */
2775 startlnum = pos.lnum;
2776 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2777
2778 for (;;) /* find end of sentence */
2779 {
2780 c = gchar_pos(&pos);
2781 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2782 {
2783 if (dir == BACKWARD && pos.lnum != startlnum)
2784 ++pos.lnum;
2785 break;
2786 }
2787 if (c == '.' || c == '!' || c == '?')
2788 {
2789 tpos = pos;
2790 do
2791 if ((c = inc(&tpos)) == -1)
2792 break;
2793 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2794 != NULL);
2795 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2796 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2797 && gchar_pos(&tpos) == ' ')))
2798 {
2799 pos = tpos;
2800 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2801 inc(&pos);
2802 break;
2803 }
2804 }
2805 if ((*func)(&pos) == -1)
2806 {
2807 if (count)
2808 return FAIL;
2809 noskip = TRUE;
2810 break;
2811 }
2812 }
2813found:
2814 /* skip white space */
2815 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2816 if (incl(&pos) == -1)
2817 break;
2818 }
2819
2820 setpcmark();
2821 curwin->w_cursor = pos;
2822 return OK;
2823}
2824
2825/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002826 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002828 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 * If 'what' is '{' or '}' we go to the next section.
2830 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002831 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 */
2833 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002834findpar(
2835 int *pincl, /* Return: TRUE if last char is to be included */
2836 int dir,
2837 long count,
2838 int what,
2839 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 linenr_T curr;
2842 int did_skip; /* TRUE after separating lines have been skipped */
2843 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002844 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845#ifdef FEAT_FOLDING
2846 linenr_T fold_first; /* first line of a closed fold */
2847 linenr_T fold_last; /* last line of a closed fold */
2848 int fold_skipped; /* TRUE if a closed fold was skipped this
2849 iteration */
2850#endif
2851
2852 curr = curwin->w_cursor.lnum;
2853
2854 while (count--)
2855 {
2856 did_skip = FALSE;
2857 for (first = TRUE; ; first = FALSE)
2858 {
2859 if (*ml_get(curr) != NUL)
2860 did_skip = TRUE;
2861
2862#ifdef FEAT_FOLDING
2863 /* skip folded lines */
2864 fold_skipped = FALSE;
2865 if (first && hasFolding(curr, &fold_first, &fold_last))
2866 {
2867 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2868 fold_skipped = TRUE;
2869 }
2870#endif
2871
Bram Moolenaarc4568ab2018-11-16 16:21:05 +01002872 /* POSIX has its own ideas of what a paragraph boundary is and it
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002873 * doesn't match historical Vi: It also stops at a "{" in the
2874 * first column and at an empty line. */
2875 if (!first && did_skip && (startPS(curr, what, both)
2876 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 break;
2878
2879#ifdef FEAT_FOLDING
2880 if (fold_skipped)
2881 curr -= dir;
2882#endif
2883 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2884 {
2885 if (count)
2886 return FALSE;
2887 curr -= dir;
2888 break;
2889 }
2890 }
2891 }
2892 setpcmark();
2893 if (both && *ml_get(curr) == '}') /* include line with '}' */
2894 ++curr;
2895 curwin->w_cursor.lnum = curr;
2896 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2897 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002898 char_u *line = ml_get(curr);
2899
2900 /* Put the cursor on the last character in the last line and make the
2901 * motion inclusive. */
2902 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 {
2904 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002905#ifdef FEAT_MBYTE
2906 curwin->w_cursor.col -=
2907 (*mb_head_off)(line, line + curwin->w_cursor.col);
2908#endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002909 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
2911 }
2912 else
2913 curwin->w_cursor.col = 0;
2914 return TRUE;
2915}
2916
2917/*
2918 * check if the string 's' is a nroff macro that is in option 'opt'
2919 */
2920 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002921inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 char_u *macro;
2924
2925 for (macro = opt; macro[0]; ++macro)
2926 {
2927 /* Accept two characters in the option being equal to two characters
2928 * in the line. A space in the option matches with a space in the
2929 * line or the line having ended. */
2930 if ( (macro[0] == s[0]
2931 || (macro[0] == ' '
2932 && (s[0] == NUL || s[0] == ' ')))
2933 && (macro[1] == s[1]
2934 || ((macro[1] == NUL || macro[1] == ' ')
2935 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2936 break;
2937 ++macro;
2938 if (macro[0] == NUL)
2939 break;
2940 }
2941 return (macro[0] != NUL);
2942}
2943
2944/*
2945 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2946 * If 'para' is '{' or '}' only check for sections.
2947 * If 'both' is TRUE also stop at '}'
2948 */
2949 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002950startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
2952 char_u *s;
2953
2954 s = ml_get(lnum);
2955 if (*s == para || *s == '\f' || (both && *s == '}'))
2956 return TRUE;
2957 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2958 (!para && inmacro(p_para, s + 1))))
2959 return TRUE;
2960 return FALSE;
2961}
2962
2963/*
2964 * The following routines do the word searches performed by the 'w', 'W',
2965 * 'b', 'B', 'e', and 'E' commands.
2966 */
2967
2968/*
2969 * To perform these searches, characters are placed into one of three
2970 * classes, and transitions between classes determine word boundaries.
2971 *
2972 * The classes are:
2973 *
2974 * 0 - white space
2975 * 1 - punctuation
2976 * 2 or higher - keyword characters (letters, digits and underscore)
2977 */
2978
2979static int cls_bigword; /* TRUE for "W", "B" or "E" */
2980
2981/*
2982 * cls() - returns the class of character at curwin->w_cursor
2983 *
2984 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2985 * from class 2 and higher are reported as class 1 since only white space
2986 * boundaries are of interest.
2987 */
2988 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002989cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 int c;
2992
2993 c = gchar_cursor();
2994#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2995 if (p_altkeymap && c == F_BLANK)
2996 return 0;
2997#endif
2998 if (c == ' ' || c == '\t' || c == NUL)
2999 return 0;
3000#ifdef FEAT_MBYTE
3001 if (enc_dbcs != 0 && c > 0xFF)
3002 {
3003 /* If cls_bigword, report multi-byte chars as class 1. */
3004 if (enc_dbcs == DBCS_KOR && cls_bigword)
3005 return 1;
3006
3007 /* process code leading/trailing bytes */
3008 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3009 }
3010 if (enc_utf8)
3011 {
3012 c = utf_class(c);
3013 if (c != 0 && cls_bigword)
3014 return 1;
3015 return c;
3016 }
3017#endif
3018
3019 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3020 if (cls_bigword)
3021 return 1;
3022
3023 if (vim_iswordc(c))
3024 return 2;
3025 return 1;
3026}
3027
3028
3029/*
3030 * fwd_word(count, type, eol) - move forward one word
3031 *
3032 * Returns FAIL if the cursor was already at the end of the file.
3033 * If eol is TRUE, last word stops at end of line (for operators).
3034 */
3035 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003036fwd_word(
3037 long count,
3038 int bigword, /* "W", "E" or "B" */
3039 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 int sclass; /* starting class */
3042 int i;
3043 int last_line;
3044
3045#ifdef FEAT_VIRTUALEDIT
3046 curwin->w_cursor.coladd = 0;
3047#endif
3048 cls_bigword = bigword;
3049 while (--count >= 0)
3050 {
3051#ifdef FEAT_FOLDING
3052 /* When inside a range of folded lines, move to the last char of the
3053 * last line. */
3054 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3055 coladvance((colnr_T)MAXCOL);
3056#endif
3057 sclass = cls();
3058
3059 /*
3060 * We always move at least one character, unless on the last
3061 * character in the buffer.
3062 */
3063 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3064 i = inc_cursor();
3065 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3066 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003067 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 return OK;
3069
3070 /*
3071 * Go one char past end of current word (if any)
3072 */
3073 if (sclass != 0)
3074 while (cls() == sclass)
3075 {
3076 i = inc_cursor();
3077 if (i == -1 || (i >= 1 && eol && count == 0))
3078 return OK;
3079 }
3080
3081 /*
3082 * go to next non-white
3083 */
3084 while (cls() == 0)
3085 {
3086 /*
3087 * We'll stop if we land on a blank line
3088 */
3089 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3090 break;
3091
3092 i = inc_cursor();
3093 if (i == -1 || (i >= 1 && eol && count == 0))
3094 return OK;
3095 }
3096 }
3097 return OK;
3098}
3099
3100/*
3101 * bck_word() - move backward 'count' words
3102 *
3103 * If stop is TRUE and we are already on the start of a word, move one less.
3104 *
3105 * Returns FAIL if top of the file was reached.
3106 */
3107 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003108bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int sclass; /* starting class */
3111
3112#ifdef FEAT_VIRTUALEDIT
3113 curwin->w_cursor.coladd = 0;
3114#endif
3115 cls_bigword = bigword;
3116 while (--count >= 0)
3117 {
3118#ifdef FEAT_FOLDING
3119 /* When inside a range of folded lines, move to the first char of the
3120 * first line. */
3121 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3122 curwin->w_cursor.col = 0;
3123#endif
3124 sclass = cls();
3125 if (dec_cursor() == -1) /* started at start of file */
3126 return FAIL;
3127
3128 if (!stop || sclass == cls() || sclass == 0)
3129 {
3130 /*
3131 * Skip white space before the word.
3132 * Stop on an empty line.
3133 */
3134 while (cls() == 0)
3135 {
3136 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003137 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 goto finished;
3139 if (dec_cursor() == -1) /* hit start of file, stop here */
3140 return OK;
3141 }
3142
3143 /*
3144 * Move backward to start of this word.
3145 */
3146 if (skip_chars(cls(), BACKWARD))
3147 return OK;
3148 }
3149
3150 inc_cursor(); /* overshot - forward one */
3151finished:
3152 stop = FALSE;
3153 }
3154 return OK;
3155}
3156
3157/*
3158 * end_word() - move to the end of the word
3159 *
3160 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3161 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3162 * motion crosses blank lines. When the real vi crosses a blank line in an
3163 * 'e' motion, the cursor is placed on the FIRST character of the next
3164 * non-blank line. The 'E' command, however, works correctly. Since this
3165 * appears to be a bug, I have not duplicated it here.
3166 *
3167 * Returns FAIL if end of the file was reached.
3168 *
3169 * If stop is TRUE and we are already on the end of a word, move one less.
3170 * If empty is TRUE stop on an empty line.
3171 */
3172 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003173end_word(
3174 long count,
3175 int bigword,
3176 int stop,
3177 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178{
3179 int sclass; /* starting class */
3180
3181#ifdef FEAT_VIRTUALEDIT
3182 curwin->w_cursor.coladd = 0;
3183#endif
3184 cls_bigword = bigword;
3185 while (--count >= 0)
3186 {
3187#ifdef FEAT_FOLDING
3188 /* When inside a range of folded lines, move to the last char of the
3189 * last line. */
3190 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3191 coladvance((colnr_T)MAXCOL);
3192#endif
3193 sclass = cls();
3194 if (inc_cursor() == -1)
3195 return FAIL;
3196
3197 /*
3198 * If we're in the middle of a word, we just have to move to the end
3199 * of it.
3200 */
3201 if (cls() == sclass && sclass != 0)
3202 {
3203 /*
3204 * Move forward to end of the current word
3205 */
3206 if (skip_chars(sclass, FORWARD))
3207 return FAIL;
3208 }
3209 else if (!stop || sclass == 0)
3210 {
3211 /*
3212 * We were at the end of a word. Go to the end of the next word.
3213 * First skip white space, if 'empty' is TRUE, stop at empty line.
3214 */
3215 while (cls() == 0)
3216 {
3217 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003218 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 goto finished;
3220 if (inc_cursor() == -1) /* hit end of file, stop here */
3221 return FAIL;
3222 }
3223
3224 /*
3225 * Move forward to the end of this word.
3226 */
3227 if (skip_chars(cls(), FORWARD))
3228 return FAIL;
3229 }
3230 dec_cursor(); /* overshot - one char backward */
3231finished:
3232 stop = FALSE; /* we move only one word less */
3233 }
3234 return OK;
3235}
3236
3237/*
3238 * Move back to the end of the word.
3239 *
3240 * Returns FAIL if start of the file was reached.
3241 */
3242 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003243bckend_word(
3244 long count,
3245 int bigword, /* TRUE for "B" */
3246 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247{
3248 int sclass; /* starting class */
3249 int i;
3250
3251#ifdef FEAT_VIRTUALEDIT
3252 curwin->w_cursor.coladd = 0;
3253#endif
3254 cls_bigword = bigword;
3255 while (--count >= 0)
3256 {
3257 sclass = cls();
3258 if ((i = dec_cursor()) == -1)
3259 return FAIL;
3260 if (eol && i == 1)
3261 return OK;
3262
3263 /*
3264 * Move backward to before the start of this word.
3265 */
3266 if (sclass != 0)
3267 {
3268 while (cls() == sclass)
3269 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3270 return OK;
3271 }
3272
3273 /*
3274 * Move backward to end of the previous word
3275 */
3276 while (cls() == 0)
3277 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003278 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 break;
3280 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3281 return OK;
3282 }
3283 }
3284 return OK;
3285}
3286
3287/*
3288 * Skip a row of characters of the same class.
3289 * Return TRUE when end-of-file reached, FALSE otherwise.
3290 */
3291 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003292skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 while (cls() == cclass)
3295 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3296 return TRUE;
3297 return FALSE;
3298}
3299
3300#ifdef FEAT_TEXTOBJ
3301/*
3302 * Go back to the start of the word or the start of white space
3303 */
3304 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003305back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 int sclass; /* starting class */
3308
3309 sclass = cls();
3310 for (;;)
3311 {
3312 if (curwin->w_cursor.col == 0) /* stop at start of line */
3313 break;
3314 dec_cursor();
3315 if (cls() != sclass) /* stop at start of word */
3316 {
3317 inc_cursor();
3318 break;
3319 }
3320 }
3321}
3322
3323 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003324find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
3326 int c;
3327
3328 while (decl(posp) != -1)
3329 {
3330 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003331 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 {
3333 incl(posp);
3334 break;
3335 }
3336 }
3337}
3338
3339/*
3340 * Skip count/2 sentences and count/2 separating white spaces.
3341 */
3342 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003343findsent_forward(
3344 long count,
3345 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 while (count--)
3348 {
3349 findsent(FORWARD, 1L);
3350 if (at_start_sent)
3351 find_first_blank(&curwin->w_cursor);
3352 if (count == 0 || at_start_sent)
3353 decl(&curwin->w_cursor);
3354 at_start_sent = !at_start_sent;
3355 }
3356}
3357
3358/*
3359 * Find word under cursor, cursor at end.
3360 * Used while an operator is pending, and in Visual mode.
3361 */
3362 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003363current_word(
3364 oparg_T *oap,
3365 long count,
3366 int include, /* TRUE: include word and white space */
3367 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 pos_T start_pos;
3370 pos_T pos;
3371 int inclusive = TRUE;
3372 int include_white = FALSE;
3373
3374 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003375 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003378 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 dec_cursor();
3380
3381 /*
3382 * When Visual mode is not active, or when the VIsual area is only one
3383 * character, select the word and/or white space under the cursor.
3384 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003385 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
3387 /*
3388 * Go to start of current word or white space.
3389 */
3390 back_in_line();
3391 start_pos = curwin->w_cursor;
3392
3393 /*
3394 * If the start is on white space, and white space should be included
3395 * (" word"), or start is not on white space, and white space should
3396 * not be included ("word"), find end of word.
3397 */
3398 if ((cls() == 0) == include)
3399 {
3400 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3401 return FAIL;
3402 }
3403 else
3404 {
3405 /*
3406 * If the start is not on white space, and white space should be
3407 * included ("word "), or start is on white space and white
3408 * space should not be included (" "), find start of word.
3409 * If we end up in the first column of the next line (single char
3410 * word) back up to end of the line.
3411 */
3412 fwd_word(1L, bigword, TRUE);
3413 if (curwin->w_cursor.col == 0)
3414 decl(&curwin->w_cursor);
3415 else
3416 oneleft();
3417
3418 if (include)
3419 include_white = TRUE;
3420 }
3421
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 if (VIsual_active)
3423 {
3424 /* should do something when inclusive == FALSE ! */
3425 VIsual = start_pos;
3426 redraw_curbuf_later(INVERTED); /* update the inversion */
3427 }
3428 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 {
3430 oap->start = start_pos;
3431 oap->motion_type = MCHAR;
3432 }
3433 --count;
3434 }
3435
3436 /*
3437 * When count is still > 0, extend with more objects.
3438 */
3439 while (count > 0)
3440 {
3441 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003442 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 {
3444 /*
3445 * In Visual mode, with cursor at start: move cursor back.
3446 */
3447 if (decl(&curwin->w_cursor) == -1)
3448 return FAIL;
3449 if (include != (cls() != 0))
3450 {
3451 if (bck_word(1L, bigword, TRUE) == FAIL)
3452 return FAIL;
3453 }
3454 else
3455 {
3456 if (bckend_word(1L, bigword, TRUE) == FAIL)
3457 return FAIL;
3458 (void)incl(&curwin->w_cursor);
3459 }
3460 }
3461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 /*
3464 * Move cursor forward one word and/or white area.
3465 */
3466 if (incl(&curwin->w_cursor) == -1)
3467 return FAIL;
3468 if (include != (cls() == 0))
3469 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003470 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return FAIL;
3472 /*
3473 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003474 * the first character on the line.
3475 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003477 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 inclusive = FALSE;
3479 }
3480 else
3481 {
3482 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3483 return FAIL;
3484 }
3485 }
3486 --count;
3487 }
3488
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003489 if (include_white && (cls() != 0
3490 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 /*
3493 * If we don't include white space at the end, move the start
3494 * to include some white space there. This makes "daw" work
3495 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003496 * word). Also when "2daw" deletes "word." at the end of the line
3497 * (cursor is at start of next line).
3498 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 */
3500 pos = curwin->w_cursor; /* save cursor position */
3501 curwin->w_cursor = start_pos;
3502 if (oneleft() == OK)
3503 {
3504 back_in_line();
3505 if (cls() == 0 && curwin->w_cursor.col > 0)
3506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (VIsual_active)
3508 VIsual = curwin->w_cursor;
3509 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 oap->start = curwin->w_cursor;
3511 }
3512 }
3513 curwin->w_cursor = pos; /* put cursor back at end */
3514 }
3515
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (VIsual_active)
3517 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003518 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 inc_cursor();
3520 if (VIsual_mode == 'V')
3521 {
3522 VIsual_mode = 'v';
3523 redraw_cmdline = TRUE; /* show mode later */
3524 }
3525 }
3526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 oap->inclusive = inclusive;
3528
3529 return OK;
3530}
3531
3532/*
3533 * Find sentence(s) under the cursor, cursor at end.
3534 * When Visual active, extend it by one or more sentences.
3535 */
3536 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003537current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
3539 pos_T start_pos;
3540 pos_T pos;
3541 int start_blank;
3542 int c;
3543 int at_start_sent;
3544 long ncount;
3545
3546 start_pos = curwin->w_cursor;
3547 pos = start_pos;
3548 findsent(FORWARD, 1L); /* Find start of next sentence. */
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003551 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003553 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003556 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558 /*
3559 * Cursor at start of Visual area.
3560 * Find out where we are:
3561 * - in the white space before a sentence
3562 * - in a sentence or just after it
3563 * - at the start of a sentence
3564 */
3565 at_start_sent = TRUE;
3566 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003567 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
3569 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003570 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 at_start_sent = FALSE;
3573 break;
3574 }
3575 incl(&pos);
3576 }
3577 if (!at_start_sent)
3578 {
3579 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003580 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 at_start_sent = TRUE; /* exactly at start of sentence */
3582 else
3583 /* inside a sentence, go to its end (start of next) */
3584 findsent(FORWARD, 1L);
3585 }
3586 if (include) /* "as" gets twice as much as "is" */
3587 count *= 2;
3588 while (count--)
3589 {
3590 if (at_start_sent)
3591 find_first_blank(&curwin->w_cursor);
3592 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003593 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 findsent(BACKWARD, 1L);
3595 at_start_sent = !at_start_sent;
3596 }
3597 }
3598 else
3599 {
3600 /*
3601 * Cursor at end of Visual area.
3602 * Find out where we are:
3603 * - just before a sentence
3604 * - just before or in the white space before a sentence
3605 * - in a sentence
3606 */
3607 incl(&pos);
3608 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003609 /* not just before a sentence */
3610 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003613 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003616 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 at_start_sent = TRUE;
3619 break;
3620 }
3621 incl(&pos);
3622 }
3623 if (at_start_sent) /* in the sentence */
3624 findsent(BACKWARD, 1L);
3625 else /* in/before white before a sentence */
3626 curwin->w_cursor = start_pos;
3627 }
3628
3629 if (include) /* "as" gets twice as much as "is" */
3630 count *= 2;
3631 findsent_forward(count, at_start_sent);
3632 if (*p_sel == 'e')
3633 ++curwin->w_cursor.col;
3634 }
3635 return OK;
3636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003639 * If the cursor started on a blank, check if it is just before the start
3640 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003642 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003644 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 {
3646 start_blank = TRUE;
3647 find_first_blank(&start_pos); /* go back to first blank */
3648 }
3649 else
3650 {
3651 start_blank = FALSE;
3652 findsent(BACKWARD, 1L);
3653 start_pos = curwin->w_cursor;
3654 }
3655 if (include)
3656 ncount = count * 2;
3657 else
3658 {
3659 ncount = count;
3660 if (start_blank)
3661 --ncount;
3662 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003663 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 findsent_forward(ncount, TRUE);
3665 else
3666 decl(&curwin->w_cursor);
3667
3668 if (include)
3669 {
3670 /*
3671 * If the blank in front of the sentence is included, exclude the
3672 * blanks at the end of the sentence, go back to the first blank.
3673 * If there are no trailing blanks, try to include leading blanks.
3674 */
3675 if (start_blank)
3676 {
3677 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003678 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3679 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 decl(&curwin->w_cursor);
3681 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003682 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 find_first_blank(&start_pos);
3684 }
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (VIsual_active)
3687 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003688 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003689 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 goto extend;
3691 if (*p_sel == 'e')
3692 ++curwin->w_cursor.col;
3693 VIsual = start_pos;
3694 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003695 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 redraw_curbuf_later(INVERTED); /* update the inversion */
3697 }
3698 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
3700 /* include a newline after the sentence, if there is one */
3701 if (incl(&curwin->w_cursor) == -1)
3702 oap->inclusive = TRUE;
3703 else
3704 oap->inclusive = FALSE;
3705 oap->start = start_pos;
3706 oap->motion_type = MCHAR;
3707 }
3708 return OK;
3709}
3710
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003711/*
3712 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003713 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003716current_block(
3717 oparg_T *oap,
3718 long count,
3719 int include, /* TRUE == include white space */
3720 int what, /* '(', '{', etc. */
3721 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
3723 pos_T old_pos;
3724 pos_T *pos = NULL;
3725 pos_T start_pos;
3726 pos_T *end_pos;
3727 pos_T old_start, old_end;
3728 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003729 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003732 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 old_start = old_end;
3734
3735 /*
3736 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3737 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003738 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
3740 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003741 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 while (inindent(1))
3743 if (inc_cursor() != 0)
3744 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003745 if (gchar_cursor() == what)
3746 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 ++curwin->w_cursor.col;
3748 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003749 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 {
3751 old_start = VIsual;
3752 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3753 }
3754 else
3755 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
3757 /*
3758 * Search backwards for unclosed '(', '{', etc..
3759 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003760 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3761 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 */
3763 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003764 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 while (count-- > 0)
3766 {
3767 if ((pos = findmatch(NULL, what)) == NULL)
3768 break;
3769 curwin->w_cursor = *pos;
3770 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3771 }
3772 p_cpo = save_cpo;
3773
3774 /*
3775 * Search for matching ')', '}', etc.
3776 * Put this position in curwin->w_cursor.
3777 */
3778 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3779 {
3780 curwin->w_cursor = old_pos;
3781 return FAIL;
3782 }
3783 curwin->w_cursor = *end_pos;
3784
3785 /*
3786 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003787 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3788 * indent. But only if the resulting area is not smaller than what we
3789 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
3791 while (!include)
3792 {
3793 incl(&start_pos);
3794 sol = (curwin->w_cursor.col == 0);
3795 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003796 while (inindent(1))
3797 {
3798 sol = TRUE;
3799 if (decl(&curwin->w_cursor) != 0)
3800 break;
3801 }
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 /*
3804 * In Visual mode, when the resulting area is not bigger than what we
3805 * started with, extend it to the next block, and then exclude again.
3806 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003807 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 && VIsual_active)
3809 {
3810 curwin->w_cursor = old_start;
3811 decl(&curwin->w_cursor);
3812 if ((pos = findmatch(NULL, what)) == NULL)
3813 {
3814 curwin->w_cursor = old_pos;
3815 return FAIL;
3816 }
3817 start_pos = *pos;
3818 curwin->w_cursor = *pos;
3819 if ((end_pos = findmatch(NULL, other)) == NULL)
3820 {
3821 curwin->w_cursor = old_pos;
3822 return FAIL;
3823 }
3824 curwin->w_cursor = *end_pos;
3825 }
3826 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 break;
3828 }
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (VIsual_active)
3831 {
3832 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003833 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003834 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 inc(&curwin->w_cursor); /* include the line break */
3836 VIsual = start_pos;
3837 VIsual_mode = 'v';
3838 redraw_curbuf_later(INVERTED); /* update the inversion */
3839 showmode();
3840 }
3841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
3843 oap->start = start_pos;
3844 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003845 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003848 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003849 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003851 else
3852 /* End is before the start (no text in between <>, [], etc.): don't
3853 * operate on any text. */
3854 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856
3857 return OK;
3858}
3859
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003860/*
3861 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3862 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3863 */
3864 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003865in_html_tag(
3866 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003867{
3868 char_u *line = ml_get_curline();
3869 char_u *p;
3870 int c;
3871 int lc = NUL;
3872 pos_T pos;
3873
3874#ifdef FEAT_MBYTE
3875 if (enc_dbcs)
3876 {
3877 char_u *lp = NULL;
3878
3879 /* We search forward until the cursor, because searching backwards is
3880 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003881 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003882 if (*p == '>' || *p == '<')
3883 {
3884 lc = *p;
3885 lp = p;
3886 }
3887 if (*p != '<') /* check for '<' under cursor */
3888 {
3889 if (lc != '<')
3890 return FALSE;
3891 p = lp;
3892 }
3893 }
3894 else
3895#endif
3896 {
3897 for (p = line + curwin->w_cursor.col; p > line; )
3898 {
3899 if (*p == '<') /* find '<' under/before cursor */
3900 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003901 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003902 if (*p == '>') /* find '>' before cursor */
3903 break;
3904 }
3905 if (*p != '<')
3906 return FALSE;
3907 }
3908
3909 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003910 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003911
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003912 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003913 if (end_tag)
3914 /* check that there is a '/' after the '<' */
3915 return *p == '/';
3916
3917 /* check that there is no '/' after the '<' */
3918 if (*p == '/')
3919 return FALSE;
3920
3921 /* check that the matching '>' is not preceded by '/' */
3922 for (;;)
3923 {
3924 if (inc(&pos) < 0)
3925 return FALSE;
3926 c = *ml_get_pos(&pos);
3927 if (c == '>')
3928 break;
3929 lc = c;
3930 }
3931 return lc != '/';
3932}
3933
3934/*
3935 * Find tag block under the cursor, cursor at end.
3936 */
3937 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003938current_tagblock(
3939 oparg_T *oap,
3940 long count_arg,
3941 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003942{
3943 long count = count_arg;
3944 long n;
3945 pos_T old_pos;
3946 pos_T start_pos;
3947 pos_T end_pos;
3948 pos_T old_start, old_end;
3949 char_u *spat, *epat;
3950 char_u *p;
3951 char_u *cp;
3952 int len;
3953 int r;
3954 int do_include = include;
3955 int save_p_ws = p_ws;
3956 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003957 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003958
3959 p_ws = FALSE;
3960
3961 old_pos = curwin->w_cursor;
3962 old_end = curwin->w_cursor; /* remember where we started */
3963 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003964 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003965 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003966
3967 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003968 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003969 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003970 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003971 {
3972 setpcmark();
3973
3974 /* ignore indent */
3975 while (inindent(1))
3976 if (inc_cursor() != 0)
3977 break;
3978
3979 if (in_html_tag(FALSE))
3980 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003981 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003982 while (*ml_get_cursor() != '>')
3983 if (inc_cursor() < 0)
3984 break;
3985 }
3986 else if (in_html_tag(TRUE))
3987 {
3988 /* cursor on end tag, move to just before it */
3989 while (*ml_get_cursor() != '<')
3990 if (dec_cursor() < 0)
3991 break;
3992 dec_cursor();
3993 old_end = curwin->w_cursor;
3994 }
3995 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003996 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003997 {
3998 old_start = VIsual;
3999 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4000 }
4001 else
4002 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004003
4004again:
4005 /*
4006 * Search backwards for unclosed "<aaa>".
4007 * Put this position in start_pos.
4008 */
4009 for (n = 0; n < count; ++n)
4010 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004011 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004012 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004013 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004014 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004015 {
4016 curwin->w_cursor = old_pos;
4017 goto theend;
4018 }
4019 }
4020 start_pos = curwin->w_cursor;
4021
4022 /*
4023 * Search for matching "</aaa>". First isolate the "aaa".
4024 */
4025 inc_cursor();
4026 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004027 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004028 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004029 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004030 if (len == 0)
4031 {
4032 curwin->w_cursor = old_pos;
4033 goto theend;
4034 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004035 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004036 epat = alloc(len + 9);
4037 if (spat == NULL || epat == NULL)
4038 {
4039 vim_free(spat);
4040 vim_free(epat);
4041 curwin->w_cursor = old_pos;
4042 goto theend;
4043 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004044 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004045 sprintf((char *)epat, "</%.*s>\\c", len, p);
4046
Bram Moolenaar48570482017-10-30 21:48:41 +01004047 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004048 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004049
4050 vim_free(spat);
4051 vim_free(epat);
4052
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004053 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004054 {
4055 /* Can't find other end or it's before the previous end. Could be a
4056 * HTML tag that doesn't have a matching end. Search backwards for
4057 * another starting tag. */
4058 count = 1;
4059 curwin->w_cursor = start_pos;
4060 goto again;
4061 }
4062
Bram Moolenaar1c17ffa2018-04-24 15:19:04 +02004063 if (do_include)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004064 {
4065 /* Include up to the '>'. */
4066 while (*ml_get_cursor() != '>')
4067 if (inc_cursor() < 0)
4068 break;
4069 }
4070 else
4071 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004072 char_u *c = ml_get_cursor();
4073
4074 /* Exclude the '<' of the end tag.
4075 * If the closing tag is on new line, do not decrement cursor, but
4076 * make operation exclusive, so that the linefeed will be selected */
4077 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4078 /* do not decrement cursor */
4079 is_inclusive = FALSE;
4080 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004081 dec_cursor();
4082 }
4083 end_pos = curwin->w_cursor;
4084
4085 if (!do_include)
4086 {
4087 /* Exclude the start tag. */
4088 curwin->w_cursor = start_pos;
4089 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004090 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004091 {
4092 inc_cursor();
4093 start_pos = curwin->w_cursor;
4094 break;
4095 }
4096 curwin->w_cursor = end_pos;
4097
Bram Moolenaarb476cb72018-08-16 21:37:50 +02004098 // If we are in Visual mode and now have the same text as before set
4099 // "do_include" and try again.
4100 if (VIsual_active && EQUAL_POS(start_pos, old_start)
4101 && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004102 {
4103 do_include = TRUE;
4104 curwin->w_cursor = old_start;
4105 count = count_arg;
4106 goto again;
4107 }
4108 }
4109
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004110 if (VIsual_active)
4111 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004112 /* If the end is before the start there is no text between tags, select
4113 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004114 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004115 curwin->w_cursor = start_pos;
4116 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004117 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004118 VIsual = start_pos;
4119 VIsual_mode = 'v';
4120 redraw_curbuf_later(INVERTED); /* update the inversion */
4121 showmode();
4122 }
4123 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004124 {
4125 oap->start = start_pos;
4126 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004127 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004128 {
4129 /* End is before the start: there is no text between tags; operate
4130 * on an empty area. */
4131 curwin->w_cursor = start_pos;
4132 oap->inclusive = FALSE;
4133 }
4134 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004135 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004136 }
4137 retval = OK;
4138
4139theend:
4140 p_ws = save_p_ws;
4141 return retval;
4142}
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004145current_par(
4146 oparg_T *oap,
4147 long count,
4148 int include, /* TRUE == include white space */
4149 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150{
4151 linenr_T start_lnum;
4152 linenr_T end_lnum;
4153 int white_in_front;
4154 int dir;
4155 int start_is_white;
4156 int prev_start_is_white;
4157 int retval = OK;
4158 int do_white = FALSE;
4159 int t;
4160 int i;
4161
4162 if (type == 'S') /* not implemented yet */
4163 return FAIL;
4164
4165 start_lnum = curwin->w_cursor.lnum;
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 /*
4168 * When visual area is more than one line: extend it.
4169 */
4170 if (VIsual_active && start_lnum != VIsual.lnum)
4171 {
4172extend:
4173 if (start_lnum < VIsual.lnum)
4174 dir = BACKWARD;
4175 else
4176 dir = FORWARD;
4177 for (i = count; --i >= 0; )
4178 {
4179 if (start_lnum ==
4180 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4181 {
4182 retval = FAIL;
4183 break;
4184 }
4185
4186 prev_start_is_white = -1;
4187 for (t = 0; t < 2; ++t)
4188 {
4189 start_lnum += dir;
4190 start_is_white = linewhite(start_lnum);
4191 if (prev_start_is_white == start_is_white)
4192 {
4193 start_lnum -= dir;
4194 break;
4195 }
4196 for (;;)
4197 {
4198 if (start_lnum == (dir == BACKWARD
4199 ? 1 : curbuf->b_ml.ml_line_count))
4200 break;
4201 if (start_is_white != linewhite(start_lnum + dir)
4202 || (!start_is_white
4203 && startPS(start_lnum + (dir > 0
4204 ? 1 : 0), 0, 0)))
4205 break;
4206 start_lnum += dir;
4207 }
4208 if (!include)
4209 break;
4210 if (start_lnum == (dir == BACKWARD
4211 ? 1 : curbuf->b_ml.ml_line_count))
4212 break;
4213 prev_start_is_white = start_is_white;
4214 }
4215 }
4216 curwin->w_cursor.lnum = start_lnum;
4217 curwin->w_cursor.col = 0;
4218 return retval;
4219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 /*
4222 * First move back to the start_lnum of the paragraph or white lines
4223 */
4224 white_in_front = linewhite(start_lnum);
4225 while (start_lnum > 1)
4226 {
4227 if (white_in_front) /* stop at first white line */
4228 {
4229 if (!linewhite(start_lnum - 1))
4230 break;
4231 }
4232 else /* stop at first non-white line of start of paragraph */
4233 {
4234 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4235 break;
4236 }
4237 --start_lnum;
4238 }
4239
4240 /*
4241 * Move past the end of any white lines.
4242 */
4243 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004244 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4245 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
4247 --end_lnum;
4248 i = count;
4249 if (!include && white_in_front)
4250 --i;
4251 while (i--)
4252 {
4253 if (end_lnum == curbuf->b_ml.ml_line_count)
4254 return FAIL;
4255
4256 if (!include)
4257 do_white = linewhite(end_lnum + 1);
4258
4259 if (include || !do_white)
4260 {
4261 ++end_lnum;
4262 /*
4263 * skip to end of paragraph
4264 */
4265 while (end_lnum < curbuf->b_ml.ml_line_count
4266 && !linewhite(end_lnum + 1)
4267 && !startPS(end_lnum + 1, 0, 0))
4268 ++end_lnum;
4269 }
4270
4271 if (i == 0 && white_in_front && include)
4272 break;
4273
4274 /*
4275 * skip to end of white lines after paragraph
4276 */
4277 if (include || do_white)
4278 while (end_lnum < curbuf->b_ml.ml_line_count
4279 && linewhite(end_lnum + 1))
4280 ++end_lnum;
4281 }
4282
4283 /*
4284 * If there are no empty lines at the end, try to find some empty lines at
4285 * the start (unless that has been done already).
4286 */
4287 if (!white_in_front && !linewhite(end_lnum) && include)
4288 while (start_lnum > 1 && linewhite(start_lnum - 1))
4289 --start_lnum;
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (VIsual_active)
4292 {
4293 /* Problem: when doing "Vipipip" nothing happens in a single white
4294 * line, we get stuck there. Trap this here. */
4295 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4296 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004297 if (VIsual.lnum != start_lnum)
4298 {
4299 VIsual.lnum = start_lnum;
4300 VIsual.col = 0;
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 VIsual_mode = 'V';
4303 redraw_curbuf_later(INVERTED); /* update the inversion */
4304 showmode();
4305 }
4306 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 oap->start.lnum = start_lnum;
4309 oap->start.col = 0;
4310 oap->motion_type = MLINE;
4311 }
4312 curwin->w_cursor.lnum = end_lnum;
4313 curwin->w_cursor.col = 0;
4314
4315 return OK;
4316}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004317
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004318/*
4319 * Search quote char from string line[col].
4320 * Quote character escaped by one of the characters in "escape" is not counted
4321 * as a quote.
4322 * Returns column number of "quotechar" or -1 when not found.
4323 */
4324 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004325find_next_quote(
4326 char_u *line,
4327 int col,
4328 int quotechar,
4329 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004330{
4331 int c;
4332
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004333 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004334 {
4335 c = line[col];
4336 if (c == NUL)
4337 return -1;
4338 else if (escape != NULL && vim_strchr(escape, c))
4339 ++col;
4340 else if (c == quotechar)
4341 break;
4342#ifdef FEAT_MBYTE
4343 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004344 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004347 ++col;
4348 }
4349 return col;
4350}
4351
4352/*
4353 * Search backwards in "line" from column "col_start" to find "quotechar".
4354 * Quote character escaped by one of the characters in "escape" is not counted
4355 * as a quote.
4356 * Return the found column or zero.
4357 */
4358 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004359find_prev_quote(
4360 char_u *line,
4361 int col_start,
4362 int quotechar,
4363 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004364{
4365 int n;
4366
4367 while (col_start > 0)
4368 {
4369 --col_start;
4370#ifdef FEAT_MBYTE
4371 col_start -= (*mb_head_off)(line, line + col_start);
4372#endif
4373 n = 0;
4374 if (escape != NULL)
4375 while (col_start - n > 0 && vim_strchr(escape,
4376 line[col_start - n - 1]) != NULL)
4377 ++n;
4378 if (n & 1)
4379 col_start -= n; /* uneven number of escape chars, skip it */
4380 else if (line[col_start] == quotechar)
4381 break;
4382 }
4383 return col_start;
4384}
4385
4386/*
4387 * Find quote under the cursor, cursor at end.
4388 * Returns TRUE if found, else FALSE.
4389 */
4390 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004391current_quote(
4392 oparg_T *oap,
4393 long count,
4394 int include, /* TRUE == include quote char */
4395 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004396{
4397 char_u *line = ml_get_curline();
4398 int col_end;
4399 int col_start = curwin->w_cursor.col;
4400 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004401 int vis_empty = TRUE; /* Visual selection <= 1 char */
4402 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004403 int inside_quotes = FALSE; /* Looks like "i'" done before */
4404 int selected_quote = FALSE; /* Has quote inside selection */
4405 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004406
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004407 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004408 if (VIsual_active)
4409 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004410 /* this only works within one line */
4411 if (VIsual.lnum != curwin->w_cursor.lnum)
4412 return FALSE;
4413
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004414 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004415 if (*p_sel == 'e')
4416 {
4417 if (!vis_bef_curs)
4418 {
4419 /* VIsual needs to be start of Visual selection. */
4420 pos_T t = curwin->w_cursor;
4421
4422 curwin->w_cursor = VIsual;
4423 VIsual = t;
4424 vis_bef_curs = TRUE;
4425 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004426 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004427 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004428 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004429 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004430
4431 if (!vis_empty)
4432 {
4433 /* Check if the existing selection exactly spans the text inside
4434 * quotes. */
4435 if (vis_bef_curs)
4436 {
4437 inside_quotes = VIsual.col > 0
4438 && line[VIsual.col - 1] == quotechar
4439 && line[curwin->w_cursor.col] != NUL
4440 && line[curwin->w_cursor.col + 1] == quotechar;
4441 i = VIsual.col;
4442 col_end = curwin->w_cursor.col;
4443 }
4444 else
4445 {
4446 inside_quotes = curwin->w_cursor.col > 0
4447 && line[curwin->w_cursor.col - 1] == quotechar
4448 && line[VIsual.col] != NUL
4449 && line[VIsual.col + 1] == quotechar;
4450 i = curwin->w_cursor.col;
4451 col_end = VIsual.col;
4452 }
4453
4454 /* Find out if we have a quote in the selection. */
4455 while (i <= col_end)
4456 if (line[i++] == quotechar)
4457 {
4458 selected_quote = TRUE;
4459 break;
4460 }
4461 }
4462
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004463 if (!vis_empty && line[col_start] == quotechar)
4464 {
4465 /* Already selecting something and on a quote character. Find the
4466 * next quoted string. */
4467 if (vis_bef_curs)
4468 {
4469 /* Assume we are on a closing quote: move to after the next
4470 * opening quote. */
4471 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4472 if (col_start < 0)
4473 return FALSE;
4474 col_end = find_next_quote(line, col_start + 1, quotechar,
4475 curbuf->b_p_qe);
4476 if (col_end < 0)
4477 {
4478 /* We were on a starting quote perhaps? */
4479 col_end = col_start;
4480 col_start = curwin->w_cursor.col;
4481 }
4482 }
4483 else
4484 {
4485 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4486 if (line[col_end] != quotechar)
4487 return FALSE;
4488 col_start = find_prev_quote(line, col_end, quotechar,
4489 curbuf->b_p_qe);
4490 if (line[col_start] != quotechar)
4491 {
4492 /* We were on an ending quote perhaps? */
4493 col_start = col_end;
4494 col_end = curwin->w_cursor.col;
4495 }
4496 }
4497 }
4498 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004499
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004500 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004501 {
4502 int first_col = col_start;
4503
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004504 if (!vis_empty)
4505 {
4506 if (vis_bef_curs)
4507 first_col = find_next_quote(line, col_start, quotechar, NULL);
4508 else
4509 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4510 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004511
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004512 /* The cursor is on a quote, we don't know if it's the opening or
4513 * closing quote. Search from the start of the line to find out.
4514 * Also do this when there is a Visual area, a' may leave the cursor
4515 * in between two strings. */
4516 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004517 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004518 {
4519 /* Find open quote character. */
4520 col_start = find_next_quote(line, col_start, quotechar, NULL);
4521 if (col_start < 0 || col_start > first_col)
4522 return FALSE;
4523 /* Find close quote character. */
4524 col_end = find_next_quote(line, col_start + 1, quotechar,
4525 curbuf->b_p_qe);
4526 if (col_end < 0)
4527 return FALSE;
4528 /* If is cursor between start and end quote character, it is
4529 * target text object. */
4530 if (col_start <= first_col && first_col <= col_end)
4531 break;
4532 col_start = col_end + 1;
4533 }
4534 }
4535 else
4536 {
4537 /* Search backward for a starting quote. */
4538 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4539 if (line[col_start] != quotechar)
4540 {
4541 /* No quote before the cursor, look after the cursor. */
4542 col_start = find_next_quote(line, col_start, quotechar, NULL);
4543 if (col_start < 0)
4544 return FALSE;
4545 }
4546
4547 /* Find close quote character. */
4548 col_end = find_next_quote(line, col_start + 1, quotechar,
4549 curbuf->b_p_qe);
4550 if (col_end < 0)
4551 return FALSE;
4552 }
4553
4554 /* When "include" is TRUE, include spaces after closing quote or before
4555 * the starting quote. */
4556 if (include)
4557 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004558 if (VIM_ISWHITE(line[col_end + 1]))
4559 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004560 ++col_end;
4561 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004562 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004563 --col_start;
4564 }
4565
Bram Moolenaarab194812005-09-14 21:40:12 +00004566 /* Set start position. After vi" another i" must include the ".
4567 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004568 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004569 ++col_start;
4570 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004571 if (VIsual_active)
4572 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004573 /* Set the start of the Visual area when the Visual area was empty, we
4574 * were just inside quotes or the Visual area didn't start at a quote
4575 * and didn't include a quote.
4576 */
4577 if (vis_empty
4578 || (vis_bef_curs
4579 && !selected_quote
4580 && (inside_quotes
4581 || (line[VIsual.col] != quotechar
4582 && (VIsual.col == 0
4583 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004584 {
4585 VIsual = curwin->w_cursor;
4586 redraw_curbuf_later(INVERTED);
4587 }
4588 }
4589 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004590 {
4591 oap->start = curwin->w_cursor;
4592 oap->motion_type = MCHAR;
4593 }
4594
4595 /* Set end position. */
4596 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004597 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004598 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004599 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004600 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004601 if (VIsual_active)
4602 {
4603 if (vis_empty || vis_bef_curs)
4604 {
4605 /* decrement cursor when 'selection' is not exclusive */
4606 if (*p_sel != 'e')
4607 dec_cursor();
4608 }
4609 else
4610 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004611 /* Cursor is at start of Visual area. Set the end of the Visual
4612 * area when it was just inside quotes or it didn't end at a
4613 * quote. */
4614 if (inside_quotes
4615 || (!selected_quote
4616 && line[VIsual.col] != quotechar
4617 && (line[VIsual.col] == NUL
4618 || line[VIsual.col + 1] != quotechar)))
4619 {
4620 dec_cursor();
4621 VIsual = curwin->w_cursor;
4622 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004623 curwin->w_cursor.col = col_start;
4624 }
4625 if (VIsual_mode == 'V')
4626 {
4627 VIsual_mode = 'v';
4628 redraw_cmdline = TRUE; /* show mode later */
4629 }
4630 }
4631 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004632 {
4633 /* Set inclusive and other oap's flags. */
4634 oap->inclusive = inclusive;
4635 }
4636
4637 return OK;
4638}
4639
4640#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004642static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004643
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004644/*
4645 * Find next search match under cursor, cursor at end.
4646 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004647 */
4648 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004649current_search(
4650 long count,
4651 int forward) /* move forward or backwards */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004652{
4653 pos_T start_pos; /* position before the pattern */
4654 pos_T orig_pos; /* position of the cursor at beginning */
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004655 pos_T first_match; /* position of first match */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004656 pos_T pos; /* position after the pattern */
4657 int i;
4658 int dir;
4659 int result; /* result of various function calls */
4660 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004661 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004662 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004663 int one_char;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004664 int direction = forward ? FORWARD : BACKWARD;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004665
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004666 /* wrapping should not occur */
4667 p_ws = FALSE;
4668
4669 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004670 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004671 dec_cursor();
4672
4673 if (VIsual_active)
4674 {
4675 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004676
4677 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004678
4679 /* make sure, searching further will extend the match */
4680 if (VIsual_active)
4681 {
4682 if (forward)
4683 incl(&pos);
4684 else
4685 decl(&pos);
4686 }
4687 }
4688 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004689 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004690
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004691 /* Is the pattern is zero-width?, this time, don't care about the direction
4692 */
4693 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4694 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004695 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004696 {
4697 p_ws = old_p_ws;
4698 return FAIL; /* pattern not found */
4699 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004700
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004701 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004702 * The trick is to first search backwards and then search forward again,
4703 * so that a match at the current cursor position will be correctly
4704 * captured.
4705 */
4706 for (i = 0; i < 2; i++)
4707 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004708 if (forward)
4709 dir = i;
4710 else
4711 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004712
4713 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004714 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004715 flags = SEARCH_END;
4716
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004717 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4718 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004719 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004720
4721 /* First search may fail, but then start searching from the
4722 * beginning of the file (cursor might be on the search match)
4723 * except when Visual mode is active, so that extending the visual
4724 * selection works. */
4725 if (!result && i) /* not found, abort */
4726 {
4727 curwin->w_cursor = orig_pos;
4728 if (VIsual_active)
4729 VIsual = save_VIsual;
4730 p_ws = old_p_ws;
4731 return FAIL;
4732 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004733 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004734 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004735 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004736 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004737 /* try again from start of buffer */
4738 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004739 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004740 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004741 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004742 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004743 /* searching backwards, so set pos to last line and col */
4744 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004745 pos.col = (colnr_T)STRLEN(
4746 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004747 }
4748 }
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004749 if (i == 0)
4750 first_match = pos;
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004751 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004752 }
4753
4754 start_pos = pos;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004755 flags = forward ? SEARCH_END : SEARCH_START;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004756
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004757 /* Check again from the current cursor position,
4758 * since the next match might actually by only one char wide */
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004759 one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004760 if (one_char < 0)
4761 /* search failed, abort */
4762 return FAIL;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004763
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004764 /* move to match, except for zero-width matches, in which case, we are
4765 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004766 if (!one_char)
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004767 {
4768 p_ws = FALSE;
4769 for (i = 0; i < 2; i++)
4770 {
4771 result = searchit(curwin, curbuf, &pos, direction,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004772 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
4773 NULL, NULL);
Bram Moolenaarbdb65792018-05-22 17:50:42 +02004774 /* Search successfull, break out from the loop */
4775 if (result)
4776 break;
4777 /* search failed, try again from the last search position match */
4778 pos = first_match;
4779 }
4780 }
4781
4782 p_ws = old_p_ws;
4783 /* not found */
4784 if (!result)
4785 return FAIL;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004786
4787 if (!VIsual_active)
4788 VIsual = start_pos;
4789
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004790 curwin->w_cursor = pos;
4791 VIsual_active = TRUE;
4792 VIsual_mode = 'v';
4793
4794 if (VIsual_active)
4795 {
4796 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004797 if (*p_sel == 'e')
4798 {
4799 /* Correction for exclusive selection depends on the direction. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004800 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004801 inc_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004802 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004803 inc(&VIsual);
4804 }
4805
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004806 }
4807
4808#ifdef FEAT_FOLDING
4809 if (fdo_flags & FDO_SEARCH && KeyTyped)
4810 foldOpenCursor();
4811#endif
4812
4813 may_start_select('c');
4814#ifdef FEAT_MOUSE
4815 setmouse();
4816#endif
4817#ifdef FEAT_CLIPBOARD
4818 /* Make sure the clipboard gets updated. Needed because start and
4819 * end are still the same, and the selection needs to be owned */
4820 clip_star.vmode = NUL;
4821#endif
4822 redraw_curbuf_later(INVERTED);
4823 showmode();
4824
4825 return OK;
4826}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004827
4828/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004829 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004830 * If move is TRUE, check from the beginning of the buffer, else from position
4831 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004832 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004833 * Returns TRUE, FALSE or -1 for failure.
4834 */
4835 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004836is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004837{
4838 regmmatch_T regmatch;
4839 int nmatched = 0;
4840 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004841 pos_T pos;
4842 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004843 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004844
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004845 if (pattern == NULL)
4846 pattern = spats[last_idx].pat;
4847
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004848 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4849 SEARCH_KEEP, &regmatch) == FAIL)
4850 return -1;
4851
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004852 /* init startcol correctly */
4853 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004854 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004855 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004856 {
4857 CLEAR_POS(&pos);
4858 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004859 else
4860 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004861 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004862 /* accept a match at the cursor position */
4863 flag = SEARCH_START;
4864 }
4865
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004866 if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004867 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004868 {
4869 /* Zero-width pattern should match somewhere, then we can check if
4870 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004871 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004872 do
4873 {
4874 regmatch.startpos[0].col++;
4875 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004876 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004877 if (!nmatched)
4878 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004879 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4880 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004881
4882 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004883 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004884 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004885 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4886 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004887 /* one char width */
4888 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4889 result = TRUE;
4890 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004891 }
4892
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004893 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004894 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004895 return result;
4896}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004897
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4899 || defined(PROTO)
4900/*
4901 * return TRUE if line 'lnum' is empty or has white chars only.
4902 */
4903 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004904linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905{
4906 char_u *p;
4907
4908 p = skipwhite(ml_get(lnum));
4909 return (*p == NUL);
4910}
4911#endif
4912
4913#if defined(FEAT_FIND_ID) || defined(PROTO)
4914/*
4915 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004916 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004919find_pattern_in_path(
4920 char_u *ptr, /* pointer to search pattern */
4921 int dir UNUSED, /* direction of expansion */
4922 int len, /* length of search pattern */
4923 int whole, /* match whole words only */
4924 int skip_comments, /* don't match inside comments */
4925 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004927 long count,
4928 int action, /* What to do when we find it */
4929 linenr_T start_lnum, /* first line to start searching */
4930 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
4932 SearchedFile *files; /* Stack of included files */
4933 SearchedFile *bigger; /* When we need more space */
4934 int max_path_depth = 50;
4935 long match_count = 1;
4936
4937 char_u *pat;
4938 char_u *new_fname;
4939 char_u *curr_fname = curbuf->b_fname;
4940 char_u *prev_fname = NULL;
4941 linenr_T lnum;
4942 int depth;
4943 int depth_displayed; /* For type==CHECK_PATH */
4944 int old_files;
4945 int already_searched;
4946 char_u *file_line;
4947 char_u *line;
4948 char_u *p;
4949 char_u save_char;
4950 int define_matched;
4951 regmatch_T regmatch;
4952 regmatch_T incl_regmatch;
4953 regmatch_T def_regmatch;
4954 int matched = FALSE;
4955 int did_show = FALSE;
4956 int found = FALSE;
4957 int i;
4958 char_u *already = NULL;
4959 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004960 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004961#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 win_T *curwin_save = NULL;
4963#endif
4964
4965 regmatch.regprog = NULL;
4966 incl_regmatch.regprog = NULL;
4967 def_regmatch.regprog = NULL;
4968
4969 file_line = alloc(LSIZE);
4970 if (file_line == NULL)
4971 return;
4972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (type != CHECK_PATH && type != FIND_DEFINE
4974#ifdef FEAT_INS_EXPAND
4975 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4976 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004977 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978#endif
4979 )
4980 {
4981 pat = alloc(len + 5);
4982 if (pat == NULL)
4983 goto fpip_end;
4984 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4985 /* ignore case according to p_ic, p_scs and pat */
4986 regmatch.rm_ic = ignorecase(pat);
4987 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4988 vim_free(pat);
4989 if (regmatch.regprog == NULL)
4990 goto fpip_end;
4991 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004992 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4993 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004995 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 if (incl_regmatch.regprog == NULL)
4997 goto fpip_end;
4998 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4999 }
5000 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
5001 {
5002 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5003 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5004 if (def_regmatch.regprog == NULL)
5005 goto fpip_end;
5006 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5007 }
5008 files = (SearchedFile *)lalloc_clear((long_u)
5009 (max_path_depth * sizeof(SearchedFile)), TRUE);
5010 if (files == NULL)
5011 goto fpip_end;
5012 old_files = max_path_depth;
5013 depth = depth_displayed = -1;
5014
5015 lnum = start_lnum;
5016 if (end_lnum > curbuf->b_ml.ml_line_count)
5017 end_lnum = curbuf->b_ml.ml_line_count;
5018 if (lnum > end_lnum) /* do at least one line */
5019 lnum = end_lnum;
5020 line = ml_get(lnum);
5021
5022 for (;;)
5023 {
5024 if (incl_regmatch.regprog != NULL
5025 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5026 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005027 char_u *p_fname = (curr_fname == curbuf->b_fname)
5028 ? curbuf->b_ffname : curr_fname;
5029
5030 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5031 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5032 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005033 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005034 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5035 else
5036 /* Use text after match with 'include'. */
5037 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005038 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 already_searched = FALSE;
5040 if (new_fname != NULL)
5041 {
5042 /* Check whether we have already searched in this file */
5043 for (i = 0;; i++)
5044 {
5045 if (i == depth + 1)
5046 i = old_files;
5047 if (i == max_path_depth)
5048 break;
5049 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5050 {
5051 if (type != CHECK_PATH &&
5052 action == ACTION_SHOW_ALL && files[i].matched)
5053 {
5054 msg_putchar('\n'); /* cursor below last one */
5055 if (!got_int) /* don't display if 'q'
5056 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005057 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 msg_home_replace_hl(new_fname);
5060 MSG_PUTS(_(" (includes previously listed match)"));
5061 prev_fname = NULL;
5062 }
5063 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01005064 VIM_CLEAR(new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 already_searched = TRUE;
5066 break;
5067 }
5068 }
5069 }
5070
5071 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5072 || (new_fname == NULL && !already_searched)))
5073 {
5074 if (did_show)
5075 msg_putchar('\n'); /* cursor below last one */
5076 else
5077 {
5078 gotocmdline(TRUE); /* cursor at status line */
5079 MSG_PUTS_TITLE(_("--- Included files "));
5080 if (action != ACTION_SHOW_ALL)
5081 MSG_PUTS_TITLE(_("not found "));
5082 MSG_PUTS_TITLE(_("in path ---\n"));
5083 }
5084 did_show = TRUE;
5085 while (depth_displayed < depth && !got_int)
5086 {
5087 ++depth_displayed;
5088 for (i = 0; i < depth_displayed; i++)
5089 MSG_PUTS(" ");
5090 msg_home_replace(files[depth_displayed].name);
5091 MSG_PUTS(" -->\n");
5092 }
5093 if (!got_int) /* don't display if 'q' typed
5094 for "--more--" message */
5095 {
5096 for (i = 0; i <= depth_displayed; i++)
5097 MSG_PUTS(" ");
5098 if (new_fname != NULL)
5099 {
5100 /* using "new_fname" is more reliable, e.g., when
5101 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005102 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104 else
5105 {
5106 /*
5107 * Isolate the file name.
5108 * Include the surrounding "" or <> if present.
5109 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005110 if (inc_opt != NULL
5111 && strstr((char *)inc_opt, "\\zs") != NULL)
5112 {
5113 /* pattern contains \zs, use the match */
5114 p = incl_regmatch.startp[0];
5115 i = (int)(incl_regmatch.endp[0]
5116 - incl_regmatch.startp[0]);
5117 }
5118 else
5119 {
5120 /* find the file name after the end of the match */
5121 for (p = incl_regmatch.endp[0];
5122 *p && !vim_isfilec(*p); p++)
5123 ;
5124 for (i = 0; vim_isfilec(p[i]); i++)
5125 ;
5126 }
5127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (i == 0)
5129 {
5130 /* Nothing found, use the rest of the line. */
5131 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005132 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005134 /* Avoid checking before the start of the line, can
5135 * happen if \zs appears in the regexp. */
5136 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 if (p[-1] == '"' || p[-1] == '<')
5139 {
5140 --p;
5141 ++i;
5142 }
5143 if (p[i] == '"' || p[i] == '>')
5144 ++i;
5145 }
5146 save_char = p[i];
5147 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005148 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 p[i] = save_char;
5150 }
5151
5152 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5153 {
5154 if (already_searched)
5155 MSG_PUTS(_(" (Already listed)"));
5156 else
5157 MSG_PUTS(_(" NOT FOUND"));
5158 }
5159 }
5160 out_flush(); /* output each line directly */
5161 }
5162
5163 if (new_fname != NULL)
5164 {
5165 /* Push the new file onto the file stack */
5166 if (depth + 1 == old_files)
5167 {
5168 bigger = (SearchedFile *)lalloc((long_u)(
5169 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5170 if (bigger != NULL)
5171 {
5172 for (i = 0; i <= depth; i++)
5173 bigger[i] = files[i];
5174 for (i = depth + 1; i < old_files + max_path_depth; i++)
5175 {
5176 bigger[i].fp = NULL;
5177 bigger[i].name = NULL;
5178 bigger[i].lnum = 0;
5179 bigger[i].matched = FALSE;
5180 }
5181 for (i = old_files; i < max_path_depth; i++)
5182 bigger[i + max_path_depth] = files[i];
5183 old_files += max_path_depth;
5184 max_path_depth *= 2;
5185 vim_free(files);
5186 files = bigger;
5187 }
5188 }
5189 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5190 == NULL)
5191 vim_free(new_fname);
5192 else
5193 {
5194 if (++depth == old_files)
5195 {
5196 /*
5197 * lalloc() for 'bigger' must have failed above. We
5198 * will forget one of our already visited files now.
5199 */
5200 vim_free(files[old_files].name);
5201 ++old_files;
5202 }
5203 files[depth].name = curr_fname = new_fname;
5204 files[depth].lnum = 0;
5205 files[depth].matched = FALSE;
5206#ifdef FEAT_INS_EXPAND
5207 if (action == ACTION_EXPAND)
5208 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005209 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005210 vim_snprintf((char*)IObuff, IOSIZE,
5211 _("Scanning included file: %s"),
5212 (char *)new_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005213 msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005215 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005217 if (p_verbose >= 5)
5218 {
5219 verbose_enter();
5220 smsg((char_u *)_("Searching included file %s"),
5221 (char *)new_fname);
5222 verbose_leave();
5223 }
5224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 }
5227 }
5228 else
5229 {
5230 /*
5231 * Check if the line is a define (type == FIND_DEFINE)
5232 */
5233 p = line;
5234search_line:
5235 define_matched = FALSE;
5236 if (def_regmatch.regprog != NULL
5237 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5238 {
5239 /*
5240 * Pattern must be first identifier after 'define', so skip
5241 * to that position before checking for match of pattern. Also
5242 * don't let it match beyond the end of this identifier.
5243 */
5244 p = def_regmatch.endp[0];
5245 while (*p && !vim_iswordc(*p))
5246 p++;
5247 define_matched = TRUE;
5248 }
5249
5250 /*
5251 * Look for a match. Don't do this if we are looking for a
5252 * define and this line didn't match define_prog above.
5253 */
5254 if (def_regmatch.regprog == NULL || define_matched)
5255 {
5256 if (define_matched
5257#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005258 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259#endif
5260 )
5261 {
5262 /* compare the first "len" chars from "ptr" */
5263 startp = skipwhite(p);
5264 if (p_ic)
5265 matched = !MB_STRNICMP(startp, ptr, len);
5266 else
5267 matched = !STRNCMP(startp, ptr, len);
5268 if (matched && define_matched && whole
5269 && vim_iswordc(startp[len]))
5270 matched = FALSE;
5271 }
5272 else if (regmatch.regprog != NULL
5273 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5274 {
5275 matched = TRUE;
5276 startp = regmatch.startp[0];
5277 /*
5278 * Check if the line is not a comment line (unless we are
5279 * looking for a define). A line starting with "# define"
5280 * is not considered to be a comment line.
5281 */
5282 if (!define_matched && skip_comments)
5283 {
5284#ifdef FEAT_COMMENTS
5285 if ((*line != '#' ||
5286 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005287 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 matched = FALSE;
5289
5290 /*
5291 * Also check for a "/ *" or "/ /" before the match.
5292 * Skips lines like "int backwards; / * normal index
5293 * * /" when looking for "normal".
5294 * Note: Doesn't skip "/ *" in comments.
5295 */
5296 p = skipwhite(line);
5297 if (matched
5298 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5299#endif
5300 for (p = line; *p && p < startp; ++p)
5301 {
5302 if (matched
5303 && p[0] == '/'
5304 && (p[1] == '*' || p[1] == '/'))
5305 {
5306 matched = FALSE;
5307 /* After "//" all text is comment */
5308 if (p[1] == '/')
5309 break;
5310 ++p;
5311 }
5312 else if (!matched && p[0] == '*' && p[1] == '/')
5313 {
5314 /* Can find match after "* /". */
5315 matched = TRUE;
5316 ++p;
5317 }
5318 }
5319 }
5320 }
5321 }
5322 }
5323 if (matched)
5324 {
5325#ifdef FEAT_INS_EXPAND
5326 if (action == ACTION_EXPAND)
5327 {
5328 int reuse = 0;
5329 int add_r;
5330 char_u *aux;
5331
5332 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5333 break;
5334 found = TRUE;
5335 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (vim_iswordp(p))
5340 goto exit_matched;
5341 p = find_word_start(p);
5342 }
5343 p = find_word_end(p);
5344 i = (int)(p - aux);
5345
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005346 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005350
5351 /* Get the next line: when "depth" < 0 from the current
5352 * buffer, otherwise from the included file. Jump to
5353 * exit_matched when past the last line. */
5354 if (depth < 0)
5355 {
5356 if (lnum >= end_lnum)
5357 goto exit_matched;
5358 line = ml_get(++lnum);
5359 }
5360 else if (vim_fgets(line = file_line,
5361 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 goto exit_matched;
5363
5364 /* we read a line, set "already" to check this "line" later
5365 * if depth >= 0 we'll increase files[depth].lnum far
5366 * bellow -- Acevedo */
5367 already = aux = p = skipwhite(line);
5368 p = find_word_start(p);
5369 p = find_word_end(p);
5370 if (p > aux)
5371 {
5372 if (*aux != ')' && IObuff[i-1] != TAB)
5373 {
5374 if (IObuff[i-1] != ' ')
5375 IObuff[i++] = ' ';
5376 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5377 if (p_js
5378 && (IObuff[i-2] == '.'
5379 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5380 && (IObuff[i-2] == '?'
5381 || IObuff[i-2] == '!'))))
5382 IObuff[i++] = ' ';
5383 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005384 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 if (p - aux >= IOSIZE - i)
5386 p = aux + IOSIZE - i - 1;
5387 STRNCPY(IObuff + i, aux, p - aux);
5388 i += (int)(p - aux);
5389 reuse |= CONT_S_IPOS;
5390 }
5391 IObuff[i] = NUL;
5392 aux = IObuff;
5393
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005394 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 goto exit_matched;
5396 }
5397
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005398 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5400 dir, reuse);
5401 if (add_r == OK)
5402 /* if dir was BACKWARD then honor it just once */
5403 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005404 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 break;
5406 }
5407 else
5408#endif
5409 if (action == ACTION_SHOW_ALL)
5410 {
5411 found = TRUE;
5412 if (!did_show)
5413 gotocmdline(TRUE); /* cursor at status line */
5414 if (curr_fname != prev_fname)
5415 {
5416 if (did_show)
5417 msg_putchar('\n'); /* cursor below last one */
5418 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005419 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 msg_home_replace_hl(curr_fname);
5421 prev_fname = curr_fname;
5422 }
5423 did_show = TRUE;
5424 if (!got_int)
5425 show_pat_in_path(line, type, TRUE, action,
5426 (depth == -1) ? NULL : files[depth].fp,
5427 (depth == -1) ? &lnum : &files[depth].lnum,
5428 match_count++);
5429
5430 /* Set matched flag for this file and all the ones that
5431 * include it */
5432 for (i = 0; i <= depth; ++i)
5433 files[i].matched = TRUE;
5434 }
5435 else if (--count <= 0)
5436 {
5437 found = TRUE;
5438 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005439#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 && g_do_tagpreview == 0
5441#endif
5442 )
5443 EMSG(_("E387: Match is on current line"));
5444 else if (action == ACTION_SHOW)
5445 {
5446 show_pat_in_path(line, type, did_show, action,
5447 (depth == -1) ? NULL : files[depth].fp,
5448 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5449 did_show = TRUE;
5450 }
5451 else
5452 {
5453#ifdef FEAT_GUI
5454 need_mouse_correct = TRUE;
5455#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005456#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 /* ":psearch" uses the preview window */
5458 if (g_do_tagpreview != 0)
5459 {
5460 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005461 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 }
5463#endif
5464 if (action == ACTION_SPLIT)
5465 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005468 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
5470 if (depth == -1)
5471 {
5472 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005473#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 if (g_do_tagpreview != 0)
5475 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005476 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005477 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005478 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 break; /* failed to jump to file */
5480 }
5481 else
5482#endif
5483 setpcmark();
5484 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005485 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else
5488 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005489 if (!GETFILE_SUCCESS(getfile(
5490 0, files[depth].name, NULL, TRUE,
5491 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 break; /* failed to jump to file */
5493 /* autocommands may have changed the lnum, we don't
5494 * want that here */
5495 curwin->w_cursor.lnum = files[depth].lnum;
5496 }
5497 }
5498 if (action != ACTION_SHOW)
5499 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005500 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 curwin->w_set_curswant = TRUE;
5502 }
5503
Bram Moolenaar4033c552017-09-16 20:54:51 +02005504#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005506 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
5508 /* Return cursor to where we were */
5509 validate_cursor();
5510 redraw_later(VALID);
5511 win_enter(curwin_save, TRUE);
5512 }
5513#endif
5514 break;
5515 }
5516#ifdef FEAT_INS_EXPAND
5517exit_matched:
5518#endif
5519 matched = FALSE;
5520 /* look for other matches in the rest of the line if we
5521 * are not at the end of it already */
5522 if (def_regmatch.regprog == NULL
5523#ifdef FEAT_INS_EXPAND
5524 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005525 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005527 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005528 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 goto search_line;
5530 }
5531 line_breakcheck();
5532#ifdef FEAT_INS_EXPAND
5533 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005534 ins_compl_check_keys(30, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536#else
5537 if (got_int)
5538#endif
5539 break;
5540
5541 /*
5542 * Read the next line. When reading an included file and encountering
5543 * end-of-file, close the file and continue in the file that included
5544 * it.
5545 */
5546 while (depth >= 0 && !already
5547 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5548 {
5549 fclose(files[depth].fp);
5550 --old_files;
5551 files[old_files].name = files[depth].name;
5552 files[old_files].matched = files[depth].matched;
5553 --depth;
5554 curr_fname = (depth == -1) ? curbuf->b_fname
5555 : files[depth].name;
5556 if (depth < depth_displayed)
5557 depth_displayed = depth;
5558 }
5559 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005562 /* Remove any CR and LF from the line. */
5563 i = (int)STRLEN(line);
5564 if (i > 0 && line[i - 1] == '\n')
5565 line[--i] = NUL;
5566 if (i > 0 && line[i - 1] == '\r')
5567 line[--i] = NUL;
5568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 else if (!already)
5570 {
5571 if (++lnum > end_lnum)
5572 break;
5573 line = ml_get(lnum);
5574 }
5575 already = NULL;
5576 }
5577 /* End of big for (;;) loop. */
5578
5579 /* Close any files that are still open. */
5580 for (i = 0; i <= depth; i++)
5581 {
5582 fclose(files[i].fp);
5583 vim_free(files[i].name);
5584 }
5585 for (i = old_files; i < max_path_depth; i++)
5586 vim_free(files[i].name);
5587 vim_free(files);
5588
5589 if (type == CHECK_PATH)
5590 {
5591 if (!did_show)
5592 {
5593 if (action != ACTION_SHOW_ALL)
5594 MSG(_("All included files were found"));
5595 else
5596 MSG(_("No included files"));
5597 }
5598 }
5599 else if (!found
5600#ifdef FEAT_INS_EXPAND
5601 && action != ACTION_EXPAND
5602#endif
5603 )
5604 {
5605#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005606 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607#else
5608 if (got_int)
5609#endif
5610 EMSG(_(e_interr));
5611 else if (type == FIND_DEFINE)
5612 EMSG(_("E388: Couldn't find definition"));
5613 else
5614 EMSG(_("E389: Couldn't find pattern"));
5615 }
5616 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5617 msg_end();
5618
5619fpip_end:
5620 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005621 vim_regfree(regmatch.regprog);
5622 vim_regfree(incl_regmatch.regprog);
5623 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624}
5625
5626 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005627show_pat_in_path(
5628 char_u *line,
5629 int type,
5630 int did_show,
5631 int action,
5632 FILE *fp,
5633 linenr_T *lnum,
5634 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635{
5636 char_u *p;
5637
5638 if (did_show)
5639 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005640 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 gotocmdline(TRUE); /* cursor at status line */
5642 if (got_int) /* 'q' typed at "--more--" message */
5643 return;
5644 for (;;)
5645 {
5646 p = line + STRLEN(line) - 1;
5647 if (fp != NULL)
5648 {
5649 /* We used fgets(), so get rid of newline at end */
5650 if (p >= line && *p == '\n')
5651 --p;
5652 if (p >= line && *p == '\r')
5653 --p;
5654 *(p + 1) = NUL;
5655 }
5656 if (action == ACTION_SHOW_ALL)
5657 {
5658 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5659 msg_puts(IObuff);
5660 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5661 /* Highlight line numbers */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005662 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 MSG_PUTS(" ");
5664 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005665 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 out_flush(); /* show one line at a time */
5667
5668 /* Definition continues until line that doesn't end with '\' */
5669 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5670 break;
5671
5672 if (fp != NULL)
5673 {
5674 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5675 break;
5676 ++*lnum;
5677 }
5678 else
5679 {
5680 if (++*lnum > curbuf->b_ml.ml_line_count)
5681 break;
5682 line = ml_get(*lnum);
5683 }
5684 msg_putchar('\n');
5685 }
5686}
5687#endif
5688
5689#ifdef FEAT_VIMINFO
5690 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005691read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 char_u *lp;
5694 int idx = -1;
5695 int magic = FALSE;
5696 int no_scs = FALSE;
5697 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005698 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 long off = 0;
5700 int setlast = FALSE;
5701#ifdef FEAT_SEARCH_EXTRA
5702 static int hlsearch_on = FALSE;
5703#endif
5704 char_u *val;
5705
5706 /*
5707 * Old line types:
5708 * "/pat", "&pat": search/subst. pat
5709 * "~/pat", "~&pat": last used search/subst. pat
5710 * New line types:
5711 * "~h", "~H": hlsearch highlighting off/on
5712 * "~<magic><smartcase><line><end><off><last><which>pat"
5713 * <magic>: 'm' off, 'M' on
5714 * <smartcase>: 's' off, 'S' on
5715 * <line>: 'L' line offset, 'l' char offset
5716 * <end>: 'E' from end, 'e' from start
5717 * <off>: decimal, offset
5718 * <last>: '~' last used pattern
5719 * <which>: '/' search pat, '&' subst. pat
5720 */
5721 lp = virp->vir_line;
5722 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5723 {
5724 if (lp[1] == 'M') /* magic on */
5725 magic = TRUE;
5726 if (lp[2] == 's')
5727 no_scs = TRUE;
5728 if (lp[3] == 'L')
5729 off_line = TRUE;
5730 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005731 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 lp += 5;
5733 off = getdigits(&lp);
5734 }
5735 if (lp[0] == '~') /* use this pattern for last-used pattern */
5736 {
5737 setlast = TRUE;
5738 lp++;
5739 }
5740 if (lp[0] == '/')
5741 idx = RE_SEARCH;
5742 else if (lp[0] == '&')
5743 idx = RE_SUBST;
5744#ifdef FEAT_SEARCH_EXTRA
5745 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5746 hlsearch_on = FALSE;
5747 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5748 hlsearch_on = TRUE;
5749#endif
5750 if (idx >= 0)
5751 {
5752 if (force || spats[idx].pat == NULL)
5753 {
5754 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5755 TRUE);
5756 if (val != NULL)
5757 {
5758 set_last_search_pat(val, idx, magic, setlast);
5759 vim_free(val);
5760 spats[idx].no_scs = no_scs;
5761 spats[idx].off.line = off_line;
5762 spats[idx].off.end = off_end;
5763 spats[idx].off.off = off;
5764#ifdef FEAT_SEARCH_EXTRA
5765 if (setlast)
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02005766 set_no_hlsearch(!hlsearch_on);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#endif
5768 }
5769 }
5770 }
5771 return viminfo_readline(virp);
5772}
5773
5774 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005775write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 if (get_viminfo_parameter('/') != 0)
5778 {
5779#ifdef FEAT_SEARCH_EXTRA
5780 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5781 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5782#endif
5783 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005784 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786}
5787
5788 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005789wvsp_one(
5790 FILE *fp, /* file to write to */
5791 int idx, /* spats[] index */
5792 char *s, /* search pat */
5793 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 if (spats[idx].pat != NULL)
5796 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005797 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 /* off.dir is not stored, it's reset to forward */
5799 fprintf(fp, "%c%c%c%c%ld%s%c",
5800 spats[idx].magic ? 'M' : 'm', /* magic */
5801 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5802 spats[idx].off.line ? 'L' : 'l', /* line offset */
5803 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5804 spats[idx].off.off, /* offset */
5805 last_idx == idx ? "~" : "", /* last used pat */
5806 sc);
5807 viminfo_writestring(fp, spats[idx].pat);
5808 }
5809}
5810#endif /* FEAT_VIMINFO */