blob: efcf3d96a494c319e0beadc66976af9908db43d6 [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_prevcol(char_u *linep, int col, int ch, int *prevcol);
20static int inmacro(char_u *, char_u *);
21static int check_linecomment(char_u *line);
22static int cls(void);
23static int skip_chars(int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#ifdef FEAT_TEXTOBJ
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010025static void back_in_line(void);
26static void find_first_blank(pos_T *);
27static void findsent_forward(long count, int at_start_sent);
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#endif
29#ifdef FEAT_FIND_ID
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010030static void show_pat_in_path(char_u *, int,
31 int, int, FILE *, linenr_T *, long);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33#ifdef FEAT_VIMINFO
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010034static void wvsp_one(FILE *fp, int idx, char *s, int sc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
Bram Moolenaar071d4272004-06-13 20:20:40 +000037/*
38 * This file contains various searching-related routines. These fall into
39 * three groups:
40 * 1. string searches (for /, ?, n, and N)
41 * 2. character searches within a single line (for f, F, t, T, etc)
42 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
43 */
44
45/*
46 * String searches
47 *
48 * The string search functions are divided into two levels:
49 * lowest: searchit(); uses an pos_T for starting position and found match.
50 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
51 *
52 * The last search pattern is remembered for repeating the same search.
53 * This pattern is shared between the :g, :s, ? and / commands.
54 * This is in search_regcomp().
55 *
56 * The actual string matching is done using a heavily modified version of
57 * Henry Spencer's regular expression library. See regexp.c.
58 */
59
60/* The offset for a search command is store in a soff struct */
61/* Note: only spats[0].off is really used */
62struct soffset
63{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000064 int dir; /* search direction, '/' or '?' */
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 int line; /* search has line offset */
66 int end; /* search set cursor at end */
67 long off; /* line or char offset */
68};
69
70/* A search pattern and its attributes are stored in a spat struct */
71struct spat
72{
73 char_u *pat; /* the pattern (in allocated memory) or NULL */
74 int magic; /* magicness of the pattern */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020075 int no_scs; /* no smartcase for this pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +000076 struct soffset off;
77};
78
79/*
80 * Two search patterns are remembered: One for the :substitute command and
81 * one for other searches. last_idx points to the one that was used the last
82 * time.
83 */
84static struct spat spats[2] =
85{
86 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
87 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
88};
89
90static int last_idx = 0; /* index in spats[] for RE_LAST */
91
Bram Moolenaardbd24b52015-08-11 14:26:19 +020092static char_u lastc[2] = {NUL, NUL}; /* last character searched for */
93static int lastcdir = FORWARD; /* last direction of character search */
94static int last_t_cmd = TRUE; /* last search t_cmd */
95#ifdef FEAT_MBYTE
96static char_u lastc_bytes[MB_MAXBYTES + 1];
97static int lastc_bytelen = 1; /* >1 for multi-byte char */
98#endif
99
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
101/* copy of spats[], for keeping the search patterns while executing autocmds */
102static struct spat saved_spats[2];
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100105/* copy of spats[RE_SEARCH], for keeping the search patterns while incremental
106 * searching */
107static struct spat saved_last_search_spat;
108static int saved_last_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109static int saved_no_hlsearch = 0;
110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
112static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
113#ifdef FEAT_RIGHTLEFT
114static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#endif
116
117#ifdef FEAT_FIND_ID
118/*
119 * Type used by find_pattern_in_path() to remember which included files have
120 * been searched already.
121 */
122typedef struct SearchedFile
123{
124 FILE *fp; /* File pointer */
125 char_u *name; /* Full name of file */
126 linenr_T lnum; /* Line we were up to in file */
127 int matched; /* Found a match in this file */
128} SearchedFile;
129#endif
130
131/*
132 * translate search pattern for vim_regcomp()
133 *
134 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
135 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
136 * pat_save == RE_BOTH: save pat in both patterns (:global command)
137 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000138 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
140 * options & SEARCH_HIS: put search string in history
141 * options & SEARCH_KEEP: keep previous search pattern
142 *
143 * returns FAIL if failed, OK otherwise.
144 */
145 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100146search_regcomp(
147 char_u *pat,
148 int pat_save,
149 int pat_use,
150 int options,
151 regmmatch_T *regmatch) /* return: pattern and ignore-case flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152{
153 int magic;
154 int i;
155
156 rc_did_emsg = FALSE;
157 magic = p_magic;
158
159 /*
160 * If no pattern given, use a previously defined pattern.
161 */
162 if (pat == NULL || *pat == NUL)
163 {
164 if (pat_use == RE_LAST)
165 i = last_idx;
166 else
167 i = pat_use;
168 if (spats[i].pat == NULL) /* pattern was never defined */
169 {
170 if (pat_use == RE_SUBST)
171 EMSG(_(e_nopresub));
172 else
173 EMSG(_(e_noprevre));
174 rc_did_emsg = TRUE;
175 return FAIL;
176 }
177 pat = spats[i].pat;
178 magic = spats[i].magic;
179 no_smartcase = spats[i].no_scs;
180 }
181#ifdef FEAT_CMDHIST
182 else if (options & SEARCH_HIS) /* put new pattern in history */
183 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
184#endif
185
186#ifdef FEAT_RIGHTLEFT
187 if (mr_pattern_alloced)
188 {
189 vim_free(mr_pattern);
190 mr_pattern_alloced = FALSE;
191 }
192
193 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
194 {
195 char_u *rev_pattern;
196
197 rev_pattern = reverse_text(pat);
198 if (rev_pattern == NULL)
199 mr_pattern = pat; /* out of memory, keep normal pattern. */
200 else
201 {
202 mr_pattern = rev_pattern;
203 mr_pattern_alloced = TRUE;
204 }
205 }
206 else
207#endif
208 mr_pattern = pat;
209
210 /*
211 * Save the currently used pattern in the appropriate place,
212 * unless the pattern should not be remembered.
213 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100214 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 {
216 /* search or global command */
217 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
218 save_re_pat(RE_SEARCH, pat, magic);
219 /* substitute or global command */
220 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
221 save_re_pat(RE_SUBST, pat, magic);
222 }
223
224 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000225 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
227 if (regmatch->regprog == NULL)
228 return FAIL;
229 return OK;
230}
231
232/*
233 * Get search pattern used by search_regcomp().
234 */
235 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100236get_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237{
238 return mr_pattern;
239}
240
Bram Moolenaarabc97732007-08-08 20:49:37 +0000241#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242/*
243 * Reverse text into allocated memory.
244 * Returns the allocated string, NULL when out of memory.
245 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000246 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100247reverse_text(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248{
249 unsigned len;
250 unsigned s_i, rev_i;
251 char_u *rev;
252
253 /*
254 * Reverse the pattern.
255 */
256 len = (unsigned)STRLEN(s);
257 rev = alloc(len + 1);
258 if (rev != NULL)
259 {
260 rev_i = len;
261 for (s_i = 0; s_i < len; ++s_i)
262 {
263# ifdef FEAT_MBYTE
264 if (has_mbyte)
265 {
266 int mb_len;
267
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000268 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 rev_i -= mb_len;
270 mch_memmove(rev + rev_i, s + s_i, mb_len);
271 s_i += mb_len - 1;
272 }
273 else
274# endif
275 rev[--rev_i] = s[s_i];
276
277 }
278 rev[len] = NUL;
279 }
280 return rev;
281}
282#endif
283
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100284 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100285save_re_pat(int idx, char_u *pat, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286{
287 if (spats[idx].pat != pat)
288 {
289 vim_free(spats[idx].pat);
290 spats[idx].pat = vim_strsave(pat);
291 spats[idx].magic = magic;
292 spats[idx].no_scs = no_smartcase;
293 last_idx = idx;
294#ifdef FEAT_SEARCH_EXTRA
295 /* If 'hlsearch' set and search pat changed: need redraw. */
296 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000297 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100298 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299#endif
300 }
301}
302
303#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
304/*
305 * Save the search patterns, so they can be restored later.
306 * Used before/after executing autocommands and user functions.
307 */
308static int save_level = 0;
309
310 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100311save_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
313 if (save_level++ == 0)
314 {
315 saved_spats[0] = spats[0];
316 if (spats[0].pat != NULL)
317 saved_spats[0].pat = vim_strsave(spats[0].pat);
318 saved_spats[1] = spats[1];
319 if (spats[1].pat != NULL)
320 saved_spats[1].pat = vim_strsave(spats[1].pat);
321 saved_last_idx = last_idx;
322# ifdef FEAT_SEARCH_EXTRA
323 saved_no_hlsearch = no_hlsearch;
324# endif
325 }
326}
327
328 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100329restore_search_patterns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 if (--save_level == 0)
332 {
333 vim_free(spats[0].pat);
334 spats[0] = saved_spats[0];
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100335# if defined(FEAT_EVAL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000336 set_vv_searchforward();
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 vim_free(spats[1].pat);
339 spats[1] = saved_spats[1];
340 last_idx = saved_last_idx;
341# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100342 SET_NO_HLSEARCH(saved_no_hlsearch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343# endif
344 }
345}
346#endif
347
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000348#if defined(EXITFREE) || defined(PROTO)
349 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100350free_search_patterns(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000351{
352 vim_free(spats[0].pat);
353 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000354
355# ifdef FEAT_RIGHTLEFT
356 if (mr_pattern_alloced)
357 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200358 vim_free(mr_pattern);
359 mr_pattern_alloced = FALSE;
360 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000361 }
362# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000363}
364#endif
365
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100366#ifdef FEAT_SEARCH_EXTRA
367/*
368 * Save and restore the search pattern for incremental highlight search
369 * feature.
370 *
371 * It's similar but differnt from save_search_patterns() and
372 * restore_search_patterns(), because the search pattern must be restored when
373 * cannceling incremental searching even if it's called inside user functions.
374 */
375 void
376save_last_search_pattern(void)
377{
378 saved_last_search_spat = spats[RE_SEARCH];
379 if (spats[RE_SEARCH].pat != NULL)
380 saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat);
381 saved_last_idx = last_idx;
382 saved_no_hlsearch = no_hlsearch;
383}
384
385 void
386restore_last_search_pattern(void)
387{
388 vim_free(spats[RE_SEARCH].pat);
389 spats[RE_SEARCH] = saved_last_search_spat;
390# if defined(FEAT_EVAL)
391 set_vv_searchforward();
392# endif
393 last_idx = saved_last_idx;
394 SET_NO_HLSEARCH(saved_no_hlsearch);
395}
Bram Moolenaard0480092017-11-16 22:20:39 +0100396
397 char_u *
398last_search_pattern(void)
399{
400 return spats[RE_SEARCH].pat;
401}
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +0100402#endif
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
405 * Return TRUE when case should be ignored for search pattern "pat".
406 * Uses the 'ignorecase' and 'smartcase' options.
407 */
408 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100409ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200411 return ignorecase_opt(pat, p_ic, p_scs);
412}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200414/*
415 * As ignorecase() put pass the "ic" and "scs" flags.
416 */
417 int
418ignorecase_opt(char_u *pat, int ic_in, int scs)
419{
420 int ic = ic_in;
421
422 if (ic && !no_smartcase && scs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#ifdef FEAT_INS_EXPAND
Bram Moolenaarbc0e9ad2018-02-09 12:13:34 +0100424 && !(ctrl_x_mode_not_default() && curbuf->b_p_inf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425#endif
426 )
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200427 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 no_smartcase = FALSE;
429
430 return ic;
431}
432
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200433/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200434 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200435 */
436 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100437pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200438{
439 char_u *p = pat;
440
441 while (*p != NUL)
442 {
443#ifdef FEAT_MBYTE
444 int l;
445
446 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
447 {
448 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
449 return TRUE;
450 p += l;
451 }
452 else
453#endif
454 if (*p == '\\')
455 {
456 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
457 p += 3;
458 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
459 p += 3;
460 else if (p[1] != NUL) /* skip "\X" */
461 p += 2;
462 else
463 p += 1;
464 }
465 else if (MB_ISUPPER(*p))
466 return TRUE;
467 else
468 ++p;
469 }
470 return FALSE;
471}
472
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100474last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200475{
476#ifdef FEAT_MBYTE
477 return lastc_bytes;
478#else
479 return lastc;
480#endif
481}
482
483 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100484last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200485{
486 return lastcdir == FORWARD;
487}
488
489 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100490last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200491{
492 return last_t_cmd == TRUE;
493}
494
495 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100496set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200497{
498 *lastc = c;
499#ifdef FEAT_MBYTE
500 lastc_bytelen = len;
501 if (len)
502 memcpy(lastc_bytes, s, len);
503 else
504 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
505#endif
506}
507
508 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100509set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200510{
511 lastcdir = cdir;
512}
513
514 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100515set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200516{
517 last_t_cmd = t_cmd;
518}
519
520 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100521last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522{
523 return spats[last_idx].pat;
524}
525
526/*
527 * Reset search direction to forward. For "gd" and "gD" commands.
528 */
529 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100530reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531{
532 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#if defined(FEAT_EVAL)
534 set_vv_searchforward();
535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536}
537
538#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
539/*
540 * Set the last search pattern. For ":let @/ =" and viminfo.
541 * Also set the saved search pattern, so that this works in an autocommand.
542 */
543 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100544set_last_search_pat(
545 char_u *s,
546 int idx,
547 int magic,
548 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 vim_free(spats[idx].pat);
551 /* An empty string means that nothing should be matched. */
552 if (*s == NUL)
553 spats[idx].pat = NULL;
554 else
555 spats[idx].pat = vim_strsave(s);
556 spats[idx].magic = magic;
557 spats[idx].no_scs = FALSE;
558 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000559#if defined(FEAT_EVAL)
560 set_vv_searchforward();
561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 spats[idx].off.line = FALSE;
563 spats[idx].off.end = FALSE;
564 spats[idx].off.off = 0;
565 if (setlast)
566 last_idx = idx;
567 if (save_level)
568 {
569 vim_free(saved_spats[idx].pat);
570 saved_spats[idx] = spats[0];
571 if (spats[idx].pat == NULL)
572 saved_spats[idx].pat = NULL;
573 else
574 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
575 saved_last_idx = last_idx;
576 }
577# ifdef FEAT_SEARCH_EXTRA
578 /* If 'hlsearch' set and search pat changed: need redraw. */
579 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000580 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581# endif
582}
583#endif
584
585#ifdef FEAT_SEARCH_EXTRA
586/*
587 * Get a regexp program for the last used search pattern.
588 * This is used for highlighting all matches in a window.
589 * Values returned in regmatch->regprog and regmatch->rmm_ic.
590 */
591 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100592last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 if (spats[last_idx].pat == NULL)
595 {
596 regmatch->regprog = NULL;
597 return;
598 }
599 ++emsg_off; /* So it doesn't beep if bad expr */
600 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
601 --emsg_off;
602}
603#endif
604
605/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100606 * Lowest level search function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
608 * Start at position 'pos' and return the found position in 'pos'.
609 *
610 * if (options & SEARCH_MSG) == 0 don't give any messages
611 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
612 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
613 * if (options & SEARCH_HIS) put search pattern in history
614 * if (options & SEARCH_END) return position at end of match
615 * if (options & SEARCH_START) accept match at pos itself
616 * if (options & SEARCH_KEEP) keep previous search pattern
617 * if (options & SEARCH_FOLD) match only once in a closed fold
618 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100619 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 *
621 * Return FAIL (zero) for failure, non-zero for success.
622 * When FEAT_EVAL is defined, returns the index of the first matching
623 * subpattern plus one; one if there was none.
624 */
625 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100626searchit(
627 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100629 buf_T *buf,
630 pos_T *pos,
631 int dir,
632 char_u *pat,
633 long count,
634 int options,
635 int pat_use, /* which pattern to use when "pat" is empty */
636 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200637 proftime_T *tm UNUSED, /* timeout limit or NULL */
638 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639{
640 int found;
641 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100642 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 regmmatch_T regmatch;
644 char_u *ptr;
645 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000647 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 int loop;
649 pos_T start_pos;
650 int at_first_line;
651 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200652 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 int match_ok;
654 long nmatched;
655 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100656 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000657 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658#ifdef FEAT_SEARCH_EXTRA
659 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#endif
661
662 if (search_regcomp(pat, RE_SEARCH, pat_use,
663 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
664 {
665 if ((options & SEARCH_MSG) && !rc_did_emsg)
666 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
667 return FAIL;
668 }
669
Bram Moolenaar280f1262006-01-30 00:14:18 +0000670 /*
671 * find the string
672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 called_emsg = FALSE;
674 do /* loop for count */
675 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100676 /* When not accepting a match at the start position set "extra_col" to
677 * a non-zero value. Don't do that when starting at MAXCOL, since
678 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200679 if (pos->col == MAXCOL)
680 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100681#ifdef FEAT_MBYTE
682 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200683 else if (has_mbyte
684 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
685 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100686 {
687 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
688 if (*ptr == NUL)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200689 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100690 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200691 start_char_len = (*mb_ptr2len)(ptr);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100692 }
693#endif
694 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200695 start_char_len = 1;
696 if (dir == FORWARD)
697 {
698 if (options & SEARCH_START)
699 extra_col = 0;
700 else
701 extra_col = start_char_len;
702 }
703 else
704 {
705 if (options & SEARCH_START)
706 extra_col = start_char_len;
707 else
708 extra_col = 0;
709 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 start_pos = *pos; /* remember start pos for detecting no match */
712 found = 0; /* default: not found */
713 at_first_line = TRUE; /* default: start in first line */
714 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
715 {
716 pos->lnum = 1;
717 pos->col = 0;
718 at_first_line = FALSE; /* not in first line now */
719 }
720
721 /*
722 * Start searching in current line, unless searching backwards and
723 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000724 * If we are searching backwards, in column 0, and not including the
725 * current position, gain some efficiency by skipping back a line.
726 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000728 if (dir == BACKWARD && start_pos.col == 0
729 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
731 lnum = pos->lnum - 1;
732 at_first_line = FALSE;
733 }
734 else
735 lnum = pos->lnum;
736
737 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
738 {
739 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
740 lnum += dir, at_first_line = FALSE)
741 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000742 /* Stop after checking "stop_lnum", if it's set. */
743 if (stop_lnum != 0 && (dir == FORWARD
744 ? lnum > stop_lnum : lnum < stop_lnum))
745 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000746#ifdef FEAT_RELTIME
747 /* Stop after passing the "tm" time limit. */
748 if (tm != NULL && profile_passed_limit(tm))
749 break;
750#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000753 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100755 col = at_first_line && (options & SEARCH_COL) ? pos->col
756 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100758 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000759#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200760 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000761#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200762 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000763#endif
764 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200766 if (called_emsg
767#ifdef FEAT_RELTIME
768 || (timed_out != NULL && *timed_out)
769#endif
770 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 break;
772 if (nmatched > 0)
773 {
774 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000775 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000777#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000779#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000780 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000781 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
782 ptr = (char_u *)"";
783 else
784 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 /*
787 * Forward search in the first line: match should be after
788 * the start position. If not, continue at the end of the
789 * match (this is vi compatible) or on the next char.
790 */
791 if (dir == FORWARD && at_first_line)
792 {
793 match_ok = TRUE;
794 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000795 * When the match starts in a next line it's certainly
796 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 * When match lands on a NUL the cursor will be put
798 * one back afterwards, compare with that position,
799 * otherwise "/$" will get stuck on end of line.
800 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000801 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100802 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000803 ? (nmatched == 1
804 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000806 : ((int)matchpos.col
807 - (ptr[matchpos.col] == NUL)
808 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
810 /*
811 * If vi-compatible searching, continue at the end
812 * of the match, otherwise continue one position
813 * forward.
814 */
815 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
816 {
817 if (nmatched > 1)
818 {
819 /* end is in next line, thus no match in
820 * this line */
821 match_ok = FALSE;
822 break;
823 }
824 matchcol = endpos.col;
825 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000826 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 && ptr[matchcol] != NUL)
828 {
829#ifdef FEAT_MBYTE
830 if (has_mbyte)
831 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000832 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 else
834#endif
835 ++matchcol;
836 }
837 }
838 else
839 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000840 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 if (ptr[matchcol] != NUL)
842 {
843#ifdef FEAT_MBYTE
844 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000845 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 + matchcol);
847 else
848#endif
849 ++matchcol;
850 }
851 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200852 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (ptr[matchcol] == NUL
855 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000856 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000857 matchcol,
858#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200859 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000860#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200861 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000862#endif
863 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 {
865 match_ok = FALSE;
866 break;
867 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000868 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 endpos = regmatch.endpos[0];
870# ifdef FEAT_EVAL
871 submatch = first_submatch(&regmatch);
872# endif
873
874 /* Need to get the line pointer again, a
875 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000876 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 }
878 if (!match_ok)
879 continue;
880 }
881 if (dir == BACKWARD)
882 {
883 /*
884 * Now, if there are multiple matches on this line,
885 * we have to get the last one. Or the last one before
886 * the cursor, if we're on that line.
887 * When putting the new cursor at the end, compare
888 * relative to the end of the match.
889 */
890 match_ok = FALSE;
891 for (;;)
892 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000893 /* Remember a position that is before the start
894 * position, we use it if it's the last match in
895 * the line. Always accept a position after
896 * wrapping around. */
897 if (loop
898 || ((options & SEARCH_END)
899 ? (lnum + regmatch.endpos[0].lnum
900 < start_pos.lnum
901 || (lnum + regmatch.endpos[0].lnum
902 == start_pos.lnum
903 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200904 < (int)start_pos.col
905 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000906 : (lnum + regmatch.startpos[0].lnum
907 < start_pos.lnum
908 || (lnum + regmatch.startpos[0].lnum
909 == start_pos.lnum
910 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200911 < (int)start_pos.col
912 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000915 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 endpos = regmatch.endpos[0];
917# ifdef FEAT_EVAL
918 submatch = first_submatch(&regmatch);
919# endif
920 }
921 else
922 break;
923
924 /*
925 * We found a valid match, now check if there is
926 * another one after it.
927 * If vi-compatible searching, continue at the end
928 * of the match, otherwise continue one position
929 * forward.
930 */
931 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
932 {
933 if (nmatched > 1)
934 break;
935 matchcol = endpos.col;
936 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000937 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 && ptr[matchcol] != NUL)
939 {
940#ifdef FEAT_MBYTE
941 if (has_mbyte)
942 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000943 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else
945#endif
946 ++matchcol;
947 }
948 }
949 else
950 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000951 /* Stop when the match is in a next line. */
952 if (matchpos.lnum > 0)
953 break;
954 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (ptr[matchcol] != NUL)
956 {
957#ifdef FEAT_MBYTE
958 if (has_mbyte)
959 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000960 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 else
962#endif
963 ++matchcol;
964 }
965 }
966 if (ptr[matchcol] == NUL
967 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000968 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000969 matchcol,
970#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200971 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000972#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200973 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000974#endif
975 )) == 0)
Bram Moolenaar9d322762018-02-09 16:04:25 +0100976 {
977#ifdef FEAT_RELTIME
978 /* If the search timed out, we did find a match
979 * but it might be the wrong one, so that's not
980 * OK. */
981 if (timed_out != NULL && *timed_out)
982 match_ok = FALSE;
983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 break;
Bram Moolenaar9d322762018-02-09 16:04:25 +0100985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 /* Need to get the line pointer again, a
988 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000989 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 }
991
992 /*
993 * If there is only a match after the cursor, skip
994 * this match.
995 */
996 if (!match_ok)
997 continue;
998 }
999
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001000 /* With the SEARCH_END option move to the last character
1001 * of the match. Don't do it for an empty match, end
1002 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +02001003 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001004 && !(matchpos.lnum == endpos.lnum
1005 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001007 /* For a match in the first column, set the position
1008 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001009 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001010 pos->col = endpos.col;
1011 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001012 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001013 if (pos->lnum > 1) /* just in case */
1014 {
1015 --pos->lnum;
1016 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1017 pos->lnum, FALSE));
1018 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001019 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001020 else
1021 {
1022 --pos->col;
1023#ifdef FEAT_MBYTE
1024 if (has_mbyte
1025 && pos->lnum <= buf->b_ml.ml_line_count)
1026 {
1027 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1028 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1029 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001030#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 else
1034 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001035 pos->lnum = lnum + matchpos.lnum;
1036 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038#ifdef FEAT_VIRTUALEDIT
1039 pos->coladd = 0;
1040#endif
1041 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001042 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
1044 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001045 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 search_match_endcol = endpos.col;
1047 break;
1048 }
1049 line_breakcheck(); /* stop if ctrl-C typed */
1050 if (got_int)
1051 break;
1052
1053#ifdef FEAT_SEARCH_EXTRA
1054 /* Cancel searching if a character was typed. Used for
1055 * 'incsearch'. Don't check too often, that would slowdown
1056 * searching too much. */
1057 if ((options & SEARCH_PEEK)
1058 && ((lnum - pos->lnum) & 0x3f) == 0
1059 && char_avail())
1060 {
1061 break_loop = TRUE;
1062 break;
1063 }
1064#endif
1065
1066 if (loop && lnum == start_pos.lnum)
1067 break; /* if second loop, stop where started */
1068 }
1069 at_first_line = FALSE;
1070
1071 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001072 * Stop the search if wrapscan isn't set, "stop_lnum" is
1073 * specified, after an interrupt, after a match and after looping
1074 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001076 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001077#ifdef FEAT_RELTIME
1078 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001079#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001080#ifdef FEAT_SEARCH_EXTRA
1081 || break_loop
1082#endif
1083 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 break;
1085
1086 /*
1087 * If 'wrapscan' is set we continue at the other end of the file.
1088 * If 'shortmess' does not contain 's', we give a message.
1089 * This message is also remembered in keep_msg for when the screen
1090 * is redrawn. The keep_msg is cleared whenever another message is
1091 * written.
1092 */
1093 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001097 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1098 give_warning((char_u *)_(dir == BACKWARD
1099 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001101 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001102#ifdef FEAT_RELTIME
1103 || (timed_out != NULL && *timed_out)
1104#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001105#ifdef FEAT_SEARCH_EXTRA
1106 || break_loop
1107#endif
1108 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 break;
1110 }
1111 while (--count > 0 && found); /* stop after count matches or no match */
1112
Bram Moolenaar473de612013-06-08 18:19:48 +02001113 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
Bram Moolenaar280f1262006-01-30 00:14:18 +00001115 called_emsg |= save_called_emsg;
1116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (!found) /* did not find it */
1118 {
1119 if (got_int)
1120 EMSG(_(e_interr));
1121 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1122 {
1123 if (p_ws)
1124 EMSG2(_(e_patnotf2), mr_pattern);
1125 else if (lnum == 0)
1126 EMSG2(_("E384: search hit TOP without match for: %s"),
1127 mr_pattern);
1128 else
1129 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
1130 mr_pattern);
1131 }
1132 return FAIL;
1133 }
1134
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001135 /* A pattern like "\n\zs" may go past the last line. */
1136 if (pos->lnum > buf->b_ml.ml_line_count)
1137 {
1138 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001139 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001140 if (pos->col > 0)
1141 --pos->col;
1142 }
1143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 return submatch + 1;
1145}
1146
1147#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001148 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001149set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001150{
1151 spats[0].off.dir = cdir;
1152}
1153
1154 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001155set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001156{
1157 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1158}
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160/*
1161 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001162 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 */
1164 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001165first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166{
1167 int submatch;
1168
1169 for (submatch = 1; ; ++submatch)
1170 {
1171 if (rp->startpos[submatch].lnum >= 0)
1172 break;
1173 if (submatch == 9)
1174 {
1175 submatch = 0;
1176 break;
1177 }
1178 }
1179 return submatch;
1180}
1181#endif
1182
1183/*
1184 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001185 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 * If 'dirc' is 0: use previous dir.
1187 * If 'pat' is NULL or empty : use previous string.
1188 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1189 * If 'options & SEARCH_ECHO': echo the search command and handle options
1190 * If 'options & SEARCH_MSG' : may give error message
1191 * If 'options & SEARCH_OPT' : interpret optional flags
1192 * If 'options & SEARCH_HIS' : put search pattern in history
1193 * If 'options & SEARCH_NOOF': don't add offset to position
1194 * If 'options & SEARCH_MARK': set previous context mark
1195 * If 'options & SEARCH_KEEP': keep previous search pattern
1196 * If 'options & SEARCH_START': accept match at curpos itself
1197 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1198 *
1199 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1200 * makes the movement linewise without moving the match position.
1201 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001202 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 */
1204 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001205do_search(
1206 oparg_T *oap, /* can be NULL */
1207 int dirc, /* '/' or '?' */
1208 char_u *pat,
1209 long count,
1210 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001211 proftime_T *tm, /* timeout limit or NULL */
1212 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213{
1214 pos_T pos; /* position of the last match */
1215 char_u *searchstr;
1216 struct soffset old_off;
1217 int retval; /* Return value */
1218 char_u *p;
1219 long c;
1220 char_u *dircp;
1221 char_u *strcopy = NULL;
1222 char_u *ps;
1223
1224 /*
1225 * A line offset is not remembered, this is vi compatible.
1226 */
1227 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1228 {
1229 spats[0].off.line = FALSE;
1230 spats[0].off.off = 0;
1231 }
1232
1233 /*
1234 * Save the values for when (options & SEARCH_KEEP) is used.
1235 * (there is no "if ()" around this because gcc wants them initialized)
1236 */
1237 old_off = spats[0].off;
1238
1239 pos = curwin->w_cursor; /* start searching at the cursor position */
1240
1241 /*
1242 * Find out the direction of the search.
1243 */
1244 if (dirc == 0)
1245 dirc = spats[0].off.dir;
1246 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001249#if defined(FEAT_EVAL)
1250 set_vv_searchforward();
1251#endif
1252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 if (options & SEARCH_REV)
1254 {
1255#ifdef WIN32
1256 /* There is a bug in the Visual C++ 2.2 compiler which means that
1257 * dirc always ends up being '/' */
1258 dirc = (dirc == '/') ? '?' : '/';
1259#else
1260 if (dirc == '/')
1261 dirc = '?';
1262 else
1263 dirc = '/';
1264#endif
1265 }
1266
1267#ifdef FEAT_FOLDING
1268 /* If the cursor is in a closed fold, don't find another match in the same
1269 * fold. */
1270 if (dirc == '/')
1271 {
1272 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1273 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1274 }
1275 else
1276 {
1277 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1278 pos.col = 0;
1279 }
1280#endif
1281
1282#ifdef FEAT_SEARCH_EXTRA
1283 /*
1284 * Turn 'hlsearch' highlighting back on.
1285 */
1286 if (no_hlsearch && !(options & SEARCH_KEEP))
1287 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001288 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01001289 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291#endif
1292
1293 /*
1294 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1295 */
1296 for (;;)
1297 {
1298 searchstr = pat;
1299 dircp = NULL;
1300 /* use previous pattern */
1301 if (pat == NULL || *pat == NUL || *pat == dirc)
1302 {
1303 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1304 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001305 searchstr = spats[RE_SUBST].pat;
1306 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001307 {
1308 EMSG(_(e_noprevre));
1309 retval = 0;
1310 goto end_do_search;
1311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001313 else
1314 {
1315 /* make search_regcomp() use spats[RE_SEARCH].pat */
1316 searchstr = (char_u *)"";
1317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 }
1319
1320 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1321 {
1322 /*
1323 * Find end of regular expression.
1324 * If there is a matching '/' or '?', toss it.
1325 */
1326 ps = strcopy;
1327 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1328 if (strcopy != ps)
1329 {
1330 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001331 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 pat = strcopy;
1333 searchstr = strcopy;
1334 }
1335 if (*p == dirc)
1336 {
1337 dircp = p; /* remember where we put the NUL */
1338 *p++ = NUL;
1339 }
1340 spats[0].off.line = FALSE;
1341 spats[0].off.end = FALSE;
1342 spats[0].off.off = 0;
1343 /*
1344 * Check for a line offset or a character offset.
1345 * For get_address (echo off) we don't check for a character
1346 * offset, because it is meaningless and the 's' could be a
1347 * substitute command.
1348 */
1349 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1350 spats[0].off.line = TRUE;
1351 else if ((options & SEARCH_OPT) &&
1352 (*p == 'e' || *p == 's' || *p == 'b'))
1353 {
1354 if (*p == 'e') /* end */
1355 spats[0].off.end = SEARCH_END;
1356 ++p;
1357 }
1358 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1359 {
1360 /* 'nr' or '+nr' or '-nr' */
1361 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1362 spats[0].off.off = atol((char *)p);
1363 else if (*p == '-') /* single '-' */
1364 spats[0].off.off = -1;
1365 else /* single '+' */
1366 spats[0].off.off = 1;
1367 ++p;
1368 while (VIM_ISDIGIT(*p)) /* skip number */
1369 ++p;
1370 }
1371
1372 /* compute length of search command for get_address() */
1373 searchcmdlen += (int)(p - pat);
1374
1375 pat = p; /* put pat after search command */
1376 }
1377
1378 if ((options & SEARCH_ECHO) && messaging()
1379 && !cmd_silent && msg_silent == 0)
1380 {
1381 char_u *msgbuf;
1382 char_u *trunc;
1383
1384 if (*searchstr == NUL)
1385 p = spats[last_idx].pat;
1386 else
1387 p = searchstr;
1388 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1389 if (msgbuf != NULL)
1390 {
1391 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001392#ifdef FEAT_MBYTE
1393 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1394 {
1395 /* Use a space to draw the composing char on. */
1396 msgbuf[1] = ' ';
1397 STRCPY(msgbuf + 2, p);
1398 }
1399 else
1400#endif
1401 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1403 {
1404 p = msgbuf + STRLEN(msgbuf);
1405 *p++ = dirc;
1406 if (spats[0].off.end)
1407 *p++ = 'e';
1408 else if (!spats[0].off.line)
1409 *p++ = 's';
1410 if (spats[0].off.off > 0 || spats[0].off.line)
1411 *p++ = '+';
1412 if (spats[0].off.off != 0 || spats[0].off.line)
1413 sprintf((char *)p, "%ld", spats[0].off.off);
1414 else
1415 *p = NUL;
1416 }
1417
1418 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001419 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421#ifdef FEAT_RIGHTLEFT
1422 /* The search pattern could be shown on the right in rightleft
1423 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1424 * it would be blanked out again very soon. Show it on the
1425 * left, but do reverse the text. */
1426 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1427 {
1428 char_u *r;
1429
1430 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1431 if (r != NULL)
1432 {
1433 vim_free(trunc);
1434 trunc = r;
1435 }
1436 }
1437#endif
1438 if (trunc != NULL)
1439 {
1440 msg_outtrans(trunc);
1441 vim_free(trunc);
1442 }
1443 else
1444 msg_outtrans(msgbuf);
1445 msg_clr_eos();
1446 msg_check();
1447 vim_free(msgbuf);
1448
1449 gotocmdline(FALSE);
1450 out_flush();
1451 msg_nowait = TRUE; /* don't wait for this message */
1452 }
1453 }
1454
1455 /*
1456 * If there is a character offset, subtract it from the current
1457 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001458 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 * This is not done for a line offset, because then we would not be vi
1460 * compatible.
1461 */
Bram Moolenaared203462004-06-16 11:19:22 +00001462 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
1464 if (spats[0].off.off > 0)
1465 {
1466 for (c = spats[0].off.off; c; --c)
1467 if (decl(&pos) == -1)
1468 break;
1469 if (c) /* at start of buffer */
1470 {
1471 pos.lnum = 0; /* allow lnum == 0 here */
1472 pos.col = MAXCOL;
1473 }
1474 }
1475 else
1476 {
1477 for (c = spats[0].off.off; c; ++c)
1478 if (incl(&pos) == -1)
1479 break;
1480 if (c) /* at end of buffer */
1481 {
1482 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1483 pos.col = 0;
1484 }
1485 }
1486 }
1487
1488#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1489 if (p_altkeymap && curwin->w_p_rl)
1490 lrFswap(searchstr,0);
1491#endif
1492
1493 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1494 searchstr, count, spats[0].off.end + (options &
1495 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1496 + SEARCH_MSG + SEARCH_START
1497 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001498 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
1500 if (dircp != NULL)
1501 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1502 if (c == FAIL)
1503 {
1504 retval = 0;
1505 goto end_do_search;
1506 }
1507 if (spats[0].off.end && oap != NULL)
1508 oap->inclusive = TRUE; /* 'e' includes last character */
1509
1510 retval = 1; /* pattern found */
1511
1512 /*
1513 * Add character and/or line offset
1514 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001515 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
1517 if (spats[0].off.line) /* Add the offset to the line number. */
1518 {
1519 c = pos.lnum + spats[0].off.off;
1520 if (c < 1)
1521 pos.lnum = 1;
1522 else if (c > curbuf->b_ml.ml_line_count)
1523 pos.lnum = curbuf->b_ml.ml_line_count;
1524 else
1525 pos.lnum = c;
1526 pos.col = 0;
1527
1528 retval = 2; /* pattern found, line offset added */
1529 }
Bram Moolenaared203462004-06-16 11:19:22 +00001530 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001533 c = spats[0].off.off;
1534 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001536 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (incl(&pos) == -1)
1538 break;
1539 }
1540 /* to the left, check for start of file */
1541 else
1542 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001543 while (c++ < 0)
1544 if (decl(&pos) == -1)
1545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547 }
1548 }
1549
1550 /*
1551 * The search command can be followed by a ';' to do another search.
1552 * For example: "/pat/;/foo/+3;?bar"
1553 * This is like doing another search command, except:
1554 * - The remembered direction '/' or '?' is from the first search.
1555 * - When an error happens the cursor isn't moved at all.
1556 * Don't do this when called by get_address() (it handles ';' itself).
1557 */
1558 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1559 break;
1560
1561 dirc = *++pat;
1562 if (dirc != '?' && dirc != '/')
1563 {
1564 retval = 0;
1565 EMSG(_("E386: Expected '?' or '/' after ';'"));
1566 goto end_do_search;
1567 }
1568 ++pat;
1569 }
1570
1571 if (options & SEARCH_MARK)
1572 setpcmark();
1573 curwin->w_cursor = pos;
1574 curwin->w_set_curswant = TRUE;
1575
1576end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001577 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 spats[0].off = old_off;
1579 vim_free(strcopy);
1580
1581 return retval;
1582}
1583
1584#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1585/*
1586 * search_for_exact_line(buf, pos, dir, pat)
1587 *
1588 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001589 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001591 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 * Return OK for success, or FAIL if no line found.
1593 */
1594 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001595search_for_exact_line(
1596 buf_T *buf,
1597 pos_T *pos,
1598 int dir,
1599 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600{
1601 linenr_T start = 0;
1602 char_u *ptr;
1603 char_u *p;
1604
1605 if (buf->b_ml.ml_line_count == 0)
1606 return FAIL;
1607 for (;;)
1608 {
1609 pos->lnum += dir;
1610 if (pos->lnum < 1)
1611 {
1612 if (p_ws)
1613 {
1614 pos->lnum = buf->b_ml.ml_line_count;
1615 if (!shortmess(SHM_SEARCH))
1616 give_warning((char_u *)_(top_bot_msg), TRUE);
1617 }
1618 else
1619 {
1620 pos->lnum = 1;
1621 break;
1622 }
1623 }
1624 else if (pos->lnum > buf->b_ml.ml_line_count)
1625 {
1626 if (p_ws)
1627 {
1628 pos->lnum = 1;
1629 if (!shortmess(SHM_SEARCH))
1630 give_warning((char_u *)_(bot_top_msg), TRUE);
1631 }
1632 else
1633 {
1634 pos->lnum = 1;
1635 break;
1636 }
1637 }
1638 if (pos->lnum == start)
1639 break;
1640 if (start == 0)
1641 start = pos->lnum;
1642 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1643 p = skipwhite(ptr);
1644 pos->col = (colnr_T) (p - ptr);
1645
1646 /* when adding lines the matching line may be empty but it is not
1647 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001648 if ((compl_cont_status & CONT_ADDING)
1649 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1652 return OK;
1653 }
1654 else if (*p != NUL) /* ignore empty lines */
1655 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001656 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1657 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 return OK;
1659 }
1660 }
1661 return FAIL;
1662}
1663#endif /* FEAT_INS_EXPAND */
1664
1665/*
1666 * Character Searches
1667 */
1668
1669/*
1670 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1671 * position of the character, otherwise move to just before the char.
1672 * Do this "cap->count1" times.
1673 * Return FAIL or OK.
1674 */
1675 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001676searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677{
1678 int c = cap->nchar; /* char to search for */
1679 int dir = cap->arg; /* TRUE for searching forward */
1680 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 int col;
1682 char_u *p;
1683 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001684 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686 if (c != NUL) /* normal search: remember args for repeat */
1687 {
1688 if (!KeyStuffed) /* don't remember when redoing */
1689 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001690 *lastc = c;
1691 set_csearch_direction(dir);
1692 set_csearch_until(t_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001694 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (cap->ncharC1 != 0)
1696 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001697 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1698 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001700 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1701 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703#endif
1704 }
1705 }
1706 else /* repeat previous search */
1707 {
Bram Moolenaar454709b2017-03-12 16:37:14 +01001708 if (*lastc == NUL
1709#ifdef FEAT_MBYTE
1710 && lastc_bytelen == 1
1711#endif
1712 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 return FAIL;
1714 if (dir) /* repeat in opposite direction */
1715 dir = -lastcdir;
1716 else
1717 dir = lastcdir;
1718 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001719 c = *lastc;
1720 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001721
1722 /* Force a move of at least one char, so ";" and "," will move the
1723 * cursor, even if the cursor is right in front of char we are looking
1724 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001725 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001726 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 }
1728
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001729 if (dir == BACKWARD)
1730 cap->oap->inclusive = FALSE;
1731 else
1732 cap->oap->inclusive = TRUE;
1733
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 p = ml_get_curline();
1735 col = curwin->w_cursor.col;
1736 len = (int)STRLEN(p);
1737
1738 while (count--)
1739 {
1740#ifdef FEAT_MBYTE
1741 if (has_mbyte)
1742 {
1743 for (;;)
1744 {
1745 if (dir > 0)
1746 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001747 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (col >= len)
1749 return FAIL;
1750 }
1751 else
1752 {
1753 if (col == 0)
1754 return FAIL;
1755 col -= (*mb_head_off)(p, p + col - 1) + 1;
1756 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001757 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001759 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 break;
1761 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001762 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001763 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001764 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001765 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 }
1767 }
1768 else
1769#endif
1770 {
1771 for (;;)
1772 {
1773 if ((col += dir) < 0 || col >= len)
1774 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001775 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001777 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 }
1779 }
1780 }
1781
1782 if (t_cmd)
1783 {
1784 /* backup to before the character (possibly double-byte) */
1785 col -= dir;
1786#ifdef FEAT_MBYTE
1787 if (has_mbyte)
1788 {
1789 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001790 /* Landed on the search char which is lastc_bytelen long */
1791 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 else
1793 /* To previous char, which may be multi-byte. */
1794 col -= (*mb_head_off)(p, p + col);
1795 }
1796#endif
1797 }
1798 curwin->w_cursor.col = col;
1799
1800 return OK;
1801}
1802
1803/*
1804 * "Other" Searches
1805 */
1806
1807/*
1808 * findmatch - find the matching paren or brace
1809 *
1810 * Improvement over vi: Braces inside quotes are ignored.
1811 */
1812 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001813findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 return findmatchlimit(oap, initc, 0, 0);
1816}
1817
1818/*
1819 * Return TRUE if the character before "linep[col]" equals "ch".
1820 * Return FALSE if "col" is zero.
1821 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1822 * is NULL.
1823 * Handles multibyte string correctly.
1824 */
1825 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001826check_prevcol(
1827 char_u *linep,
1828 int col,
1829 int ch,
1830 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 --col;
1833#ifdef FEAT_MBYTE
1834 if (col > 0 && has_mbyte)
1835 col -= (*mb_head_off)(linep, linep + col);
1836#endif
1837 if (prevcol)
1838 *prevcol = col;
1839 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1840}
1841
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001842static int find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001843
1844/*
1845 * Raw string start is found at linep[startpos.col - 1].
1846 * Return TRUE if the matching end can be found between startpos and endpos.
1847 */
1848 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001849find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001850{
1851 char_u *p;
1852 char_u *delim_copy;
1853 size_t delim_len;
1854 linenr_T lnum;
1855 int found = FALSE;
1856
1857 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1858 ;
1859 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001860 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001861 if (delim_copy == NULL)
1862 return FALSE;
1863 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1864 {
1865 char_u *line = ml_get(lnum);
1866
1867 for (p = line + (lnum == startpos->lnum
1868 ? startpos->col + 1 : 0); *p; ++p)
1869 {
1870 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1871 break;
1872 if (*p == ')' && p[delim_len + 1] == '"'
1873 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1874 {
1875 found = TRUE;
1876 break;
1877 }
1878 }
1879 if (found)
1880 break;
1881 }
1882 vim_free(delim_copy);
1883 return found;
1884}
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886/*
1887 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001888 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1889 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 *
1891 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001892 * character at or after the cursor. Special values:
1893 * '*' look for C-style comment / *
1894 * '/' look for C-style comment / *, ignoring comment-end
1895 * '#' look for preprocessor directives
1896 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 *
1898 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1899 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1900 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1901 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001902 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001903 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001904 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 */
1906
1907 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001908findmatchlimit(
1909 oparg_T *oap,
1910 int initc,
1911 int flags,
1912 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
1914 static pos_T pos; /* current search position */
1915 int findc = 0; /* matching brace */
1916 int c;
1917 int count = 0; /* cumulative number of braces */
1918 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001919 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 int inquote = FALSE; /* TRUE when inside quotes */
1921 char_u *linep; /* pointer to current line */
1922 char_u *ptr;
1923 int do_quotes; /* check for quotes in current line */
1924 int at_start; /* do_quotes value at start position */
1925 int hash_dir = 0; /* Direction searched for # things */
1926 int comment_dir = 0; /* Direction searched for comments */
1927 pos_T match_pos; /* Where last slash-star was found */
1928 int start_in_quotes; /* start position is in quotes */
1929 int traveled = 0; /* how far we've searched so far */
1930 int ignore_cend = FALSE; /* ignore comment end */
1931 int cpo_match; /* vi compatible matching */
1932 int cpo_bsl; /* don't recognize backslashes */
1933 int match_escaped = 0; /* search for escaped match */
1934 int dir; /* Direction to search */
1935 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001936#ifdef FEAT_LISP
1937 int lispcomm = FALSE; /* inside of Lisp-style comment */
1938 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
1941 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001942#ifdef FEAT_VIRTUALEDIT
1943 pos.coladd = 0;
1944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 linep = ml_get(pos.lnum);
1946
1947 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1948 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1949
1950 /* Direction to search when initc is '/', '*' or '#' */
1951 if (flags & FM_BACKWARD)
1952 dir = BACKWARD;
1953 else if (flags & FM_FORWARD)
1954 dir = FORWARD;
1955 else
1956 dir = 0;
1957
1958 /*
1959 * if initc given, look in the table for the matching character
1960 * '/' and '*' are special cases: look for start or end of comment.
1961 * When '/' is used, we ignore running backwards into an star-slash, for
1962 * "[*" command, we just want to find any comment.
1963 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001964 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 {
1966 comment_dir = dir;
1967 if (initc == '/')
1968 ignore_cend = TRUE;
1969 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001970 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 initc = NUL;
1972 }
1973 else if (initc != '#' && initc != NUL)
1974 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001975 find_mps_values(&initc, &findc, &backwards, TRUE);
1976 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 return NULL;
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
1980 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001981 /*
1982 * Either initc is '#', or no initc was given and we need to look
1983 * under the cursor.
1984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (initc == '#')
1986 {
1987 hash_dir = dir;
1988 }
1989 else
1990 {
1991 /*
1992 * initc was not given, must look for something to match under
1993 * or near the cursor.
1994 * Only check for special things when 'cpo' doesn't have '%'.
1995 */
1996 if (!cpo_match)
1997 {
1998 /* Are we before or at #if, #else etc.? */
1999 ptr = skipwhite(linep);
2000 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
2001 {
2002 ptr = skipwhite(ptr + 1);
2003 if ( STRNCMP(ptr, "if", 2) == 0
2004 || STRNCMP(ptr, "endif", 5) == 0
2005 || STRNCMP(ptr, "el", 2) == 0)
2006 hash_dir = 1;
2007 }
2008
2009 /* Are we on a comment? */
2010 else if (linep[pos.col] == '/')
2011 {
2012 if (linep[pos.col + 1] == '*')
2013 {
2014 comment_dir = FORWARD;
2015 backwards = FALSE;
2016 pos.col++;
2017 }
2018 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2019 {
2020 comment_dir = BACKWARD;
2021 backwards = TRUE;
2022 pos.col--;
2023 }
2024 }
2025 else if (linep[pos.col] == '*')
2026 {
2027 if (linep[pos.col + 1] == '/')
2028 {
2029 comment_dir = BACKWARD;
2030 backwards = TRUE;
2031 }
2032 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2033 {
2034 comment_dir = FORWARD;
2035 backwards = FALSE;
2036 }
2037 }
2038 }
2039
2040 /*
2041 * If we are not on a comment or the # at the start of a line, then
2042 * look for brace anywhere on this line after the cursor.
2043 */
2044 if (!hash_dir && !comment_dir)
2045 {
2046 /*
2047 * Find the brace under or after the cursor.
2048 * If beyond the end of the line, use the last character in
2049 * the line.
2050 */
2051 if (linep[pos.col] == NUL && pos.col)
2052 --pos.col;
2053 for (;;)
2054 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002055 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 if (initc == NUL)
2057 break;
2058
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002059 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 if (findc)
2061 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002062 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064 if (!findc)
2065 {
2066 /* no brace in the line, maybe use " #if" then */
2067 if (!cpo_match && *skipwhite(linep) == '#')
2068 hash_dir = 1;
2069 else
2070 return NULL;
2071 }
2072 else if (!cpo_bsl)
2073 {
2074 int col, bslcnt = 0;
2075
2076 /* Set "match_escaped" if there are an odd number of
2077 * backslashes. */
2078 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2079 bslcnt++;
2080 match_escaped = (bslcnt & 1);
2081 }
2082 }
2083 }
2084 if (hash_dir)
2085 {
2086 /*
2087 * Look for matching #if, #else, #elif, or #endif
2088 */
2089 if (oap != NULL)
2090 oap->motion_type = MLINE; /* Linewise for this case only */
2091 if (initc != '#')
2092 {
2093 ptr = skipwhite(skipwhite(linep) + 1);
2094 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2095 hash_dir = 1;
2096 else if (STRNCMP(ptr, "endif", 5) == 0)
2097 hash_dir = -1;
2098 else
2099 return NULL;
2100 }
2101 pos.col = 0;
2102 while (!got_int)
2103 {
2104 if (hash_dir > 0)
2105 {
2106 if (pos.lnum == curbuf->b_ml.ml_line_count)
2107 break;
2108 }
2109 else if (pos.lnum == 1)
2110 break;
2111 pos.lnum += hash_dir;
2112 linep = ml_get(pos.lnum);
2113 line_breakcheck(); /* check for CTRL-C typed */
2114 ptr = skipwhite(linep);
2115 if (*ptr != '#')
2116 continue;
2117 pos.col = (colnr_T) (ptr - linep);
2118 ptr = skipwhite(ptr + 1);
2119 if (hash_dir > 0)
2120 {
2121 if (STRNCMP(ptr, "if", 2) == 0)
2122 count++;
2123 else if (STRNCMP(ptr, "el", 2) == 0)
2124 {
2125 if (count == 0)
2126 return &pos;
2127 }
2128 else if (STRNCMP(ptr, "endif", 5) == 0)
2129 {
2130 if (count == 0)
2131 return &pos;
2132 count--;
2133 }
2134 }
2135 else
2136 {
2137 if (STRNCMP(ptr, "if", 2) == 0)
2138 {
2139 if (count == 0)
2140 return &pos;
2141 count--;
2142 }
2143 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2144 {
2145 if (count == 0)
2146 return &pos;
2147 }
2148 else if (STRNCMP(ptr, "endif", 5) == 0)
2149 count++;
2150 }
2151 }
2152 return NULL;
2153 }
2154 }
2155
2156#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002157 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 * paren/brace in the other direction. */
2159 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2160 backwards = !backwards;
2161#endif
2162
2163 do_quotes = -1;
2164 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002165 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002168 if ((backwards && comment_dir)
2169#ifdef FEAT_LISP
2170 || lisp
2171#endif
2172 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002174#ifdef FEAT_LISP
2175 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2176 lispcomm = TRUE; /* find match inside this comment */
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 while (!got_int)
2179 {
2180 /*
2181 * Go to the next position, forward or backward. We could use
2182 * inc() and dec() here, but that is much slower
2183 */
2184 if (backwards)
2185 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002186#ifdef FEAT_LISP
2187 /* char to match is inside of comment, don't search outside */
2188 if (lispcomm && pos.col < (colnr_T)comment_col)
2189 break;
2190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (pos.col == 0) /* at start of line, go to prev. one */
2192 {
2193 if (pos.lnum == 1) /* start of file */
2194 break;
2195 --pos.lnum;
2196
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002197 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 break;
2199
2200 linep = ml_get(pos.lnum);
2201 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2202 do_quotes = -1;
2203 line_breakcheck();
2204
2205 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002206 if (comment_dir
2207#ifdef FEAT_LISP
2208 || lisp
2209#endif
2210 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002212#ifdef FEAT_LISP
2213 /* skip comment */
2214 if (lisp && comment_col != MAXCOL)
2215 pos.col = comment_col;
2216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 }
2218 else
2219 {
2220 --pos.col;
2221#ifdef FEAT_MBYTE
2222 if (has_mbyte)
2223 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2224#endif
2225 }
2226 }
2227 else /* forward search */
2228 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002229 if (linep[pos.col] == NUL
2230 /* at end of line, go to next one */
2231#ifdef FEAT_LISP
2232 /* don't search for match in comment */
2233 || (lisp && comment_col != MAXCOL
2234 && pos.col == (colnr_T)comment_col)
2235#endif
2236 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002238 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2239#ifdef FEAT_LISP
2240 /* line is exhausted and comment with it,
2241 * don't search for match in code */
2242 || lispcomm
2243#endif
2244 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 break;
2246 ++pos.lnum;
2247
2248 if (maxtravel && traveled++ > maxtravel)
2249 break;
2250
2251 linep = ml_get(pos.lnum);
2252 pos.col = 0;
2253 do_quotes = -1;
2254 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002255#ifdef FEAT_LISP
2256 if (lisp) /* find comment pos in new line */
2257 comment_col = check_linecomment(linep);
2258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 }
2260 else
2261 {
2262#ifdef FEAT_MBYTE
2263 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002264 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 else
2266#endif
2267 ++pos.col;
2268 }
2269 }
2270
2271 /*
2272 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2273 */
2274 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2275 (linep[0] == '{' || linep[0] == '}'))
2276 {
2277 if (linep[0] == findc && count == 0) /* match! */
2278 return &pos;
2279 break; /* out of scope */
2280 }
2281
2282 if (comment_dir)
2283 {
2284 /* Note: comments do not nest, and we ignore quotes in them */
2285 /* TODO: ignore comment brackets inside strings */
2286 if (comment_dir == FORWARD)
2287 {
2288 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2289 {
2290 pos.col++;
2291 return &pos;
2292 }
2293 }
2294 else /* Searching backwards */
2295 {
2296 /*
2297 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002298 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 */
2300 if (pos.col == 0)
2301 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002302 else if (raw_string)
2303 {
2304 if (linep[pos.col - 1] == 'R'
2305 && linep[pos.col] == '"'
2306 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2307 {
2308 /* Possible start of raw string. Now that we have the
2309 * delimiter we can check if it ends before where we
2310 * started searching, or before the previously found
2311 * raw string start. */
2312 if (!find_rawstring_end(linep, &pos,
2313 count > 0 ? &match_pos : &curwin->w_cursor))
2314 {
2315 count++;
2316 match_pos = pos;
2317 match_pos.col--;
2318 }
2319 linep = ml_get(pos.lnum); /* may have been released */
2320 }
2321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 else if ( linep[pos.col - 1] == '/'
2323 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002324 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 && (int)pos.col < comment_col)
2326 {
2327 count++;
2328 match_pos = pos;
2329 match_pos.col--;
2330 }
2331 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2332 {
2333 if (count > 0)
2334 pos = match_pos;
2335 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2336 && (int)pos.col <= comment_col)
2337 pos.col -= 2;
2338 else if (ignore_cend)
2339 continue;
2340 else
2341 return NULL;
2342 return &pos;
2343 }
2344 }
2345 continue;
2346 }
2347
2348 /*
2349 * If smart matching ('cpoptions' does not contain '%'), braces inside
2350 * of quotes are ignored, but only if there is an even number of
2351 * quotes in the line.
2352 */
2353 if (cpo_match)
2354 do_quotes = 0;
2355 else if (do_quotes == -1)
2356 {
2357 /*
2358 * Count the number of quotes in the line, skipping \" and '"'.
2359 * Watch out for "\\".
2360 */
2361 at_start = do_quotes;
2362 for (ptr = linep; *ptr; ++ptr)
2363 {
2364 if (ptr == linep + pos.col + backwards)
2365 at_start = (do_quotes & 1);
2366 if (*ptr == '"'
2367 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2368 ++do_quotes;
2369 if (*ptr == '\\' && ptr[1] != NUL)
2370 ++ptr;
2371 }
2372 do_quotes &= 1; /* result is 1 with even number of quotes */
2373
2374 /*
2375 * If we find an uneven count, check current line and previous
2376 * one for a '\' at the end.
2377 */
2378 if (!do_quotes)
2379 {
2380 inquote = FALSE;
2381 if (ptr[-1] == '\\')
2382 {
2383 do_quotes = 1;
2384 if (start_in_quotes == MAYBE)
2385 {
2386 /* Do we need to use at_start here? */
2387 inquote = TRUE;
2388 start_in_quotes = TRUE;
2389 }
2390 else if (backwards)
2391 inquote = TRUE;
2392 }
2393 if (pos.lnum > 1)
2394 {
2395 ptr = ml_get(pos.lnum - 1);
2396 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2397 {
2398 do_quotes = 1;
2399 if (start_in_quotes == MAYBE)
2400 {
2401 inquote = at_start;
2402 if (inquote)
2403 start_in_quotes = TRUE;
2404 }
2405 else if (!backwards)
2406 inquote = TRUE;
2407 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002408
2409 /* ml_get() only keeps one line, need to get linep again */
2410 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412 }
2413 }
2414 if (start_in_quotes == MAYBE)
2415 start_in_quotes = FALSE;
2416
2417 /*
2418 * If 'smartmatch' is set:
2419 * Things inside quotes are ignored by setting 'inquote'. If we
2420 * find a quote without a preceding '\' invert 'inquote'. At the
2421 * end of a line not ending in '\' we reset 'inquote'.
2422 *
2423 * In lines with an uneven number of quotes (without preceding '\')
2424 * we do not know which part to ignore. Therefore we only set
2425 * inquote if the number of quotes in a line is even, unless this
2426 * line or the previous one ends in a '\'. Complicated, isn't it?
2427 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002428 c = PTR2CHAR(linep + pos.col);
2429 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 case NUL:
2432 /* at end of line without trailing backslash, reset inquote */
2433 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2434 {
2435 inquote = FALSE;
2436 start_in_quotes = FALSE;
2437 }
2438 break;
2439
2440 case '"':
2441 /* a quote that is preceded with an odd number of backslashes is
2442 * ignored */
2443 if (do_quotes)
2444 {
2445 int col;
2446
2447 for (col = pos.col - 1; col >= 0; --col)
2448 if (linep[col] != '\\')
2449 break;
2450 if ((((int)pos.col - 1 - col) & 1) == 0)
2451 {
2452 inquote = !inquote;
2453 start_in_quotes = FALSE;
2454 }
2455 }
2456 break;
2457
2458 /*
2459 * If smart matching ('cpoptions' does not contain '%'):
2460 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2461 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2462 * skipped, there is never a brace in them.
2463 * Ignore this when finding matches for `'.
2464 */
2465 case '\'':
2466 if (!cpo_match && initc != '\'' && findc != '\'')
2467 {
2468 if (backwards)
2469 {
2470 if (pos.col > 1)
2471 {
2472 if (linep[pos.col - 2] == '\'')
2473 {
2474 pos.col -= 2;
2475 break;
2476 }
2477 else if (linep[pos.col - 2] == '\\' &&
2478 pos.col > 2 && linep[pos.col - 3] == '\'')
2479 {
2480 pos.col -= 3;
2481 break;
2482 }
2483 }
2484 }
2485 else if (linep[pos.col + 1]) /* forward search */
2486 {
2487 if (linep[pos.col + 1] == '\\' &&
2488 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2489 {
2490 pos.col += 3;
2491 break;
2492 }
2493 else if (linep[pos.col + 2] == '\'')
2494 {
2495 pos.col += 2;
2496 break;
2497 }
2498 }
2499 }
2500 /* FALLTHROUGH */
2501
2502 default:
2503#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002504 /*
2505 * For Lisp skip over backslashed (), {} and [].
2506 * (actually, we skip #\( et al)
2507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (curbuf->b_p_lisp
2509 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002510 && pos.col > 1
2511 && check_prevcol(linep, pos.col, '\\', NULL)
2512 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 break;
2514#endif
2515
2516 /* Check for match outside of quotes, and inside of
2517 * quotes when the start is also inside of quotes. */
2518 if ((!inquote || start_in_quotes == TRUE)
2519 && (c == initc || c == findc))
2520 {
2521 int col, bslcnt = 0;
2522
2523 if (!cpo_bsl)
2524 {
2525 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2526 bslcnt++;
2527 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002528 /* Only accept a match when 'M' is in 'cpo' or when escaping
2529 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2531 {
2532 if (c == initc)
2533 count++;
2534 else
2535 {
2536 if (count == 0)
2537 return &pos;
2538 count--;
2539 }
2540 }
2541 }
2542 }
2543 }
2544
2545 if (comment_dir == BACKWARD && count > 0)
2546 {
2547 pos = match_pos;
2548 return &pos;
2549 }
2550 return (pos_T *)NULL; /* never found it */
2551}
2552
2553/*
2554 * Check if line[] contains a / / comment.
2555 * Return MAXCOL if not, otherwise return the column.
2556 * TODO: skip strings.
2557 */
2558 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002559check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 char_u *p;
2562
2563 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002564#ifdef FEAT_LISP
2565 /* skip Lispish one-line comments */
2566 if (curbuf->b_p_lisp)
2567 {
2568 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2569 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002570 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002571
2572 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002573 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002574 {
2575 if (*p == '"')
2576 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002577 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002578 {
2579 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002580 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002581 }
2582 else if (p == line || ((p - line) >= 2
2583 /* skip #\" form */
2584 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002585 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002586 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002587 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002588 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2589 break; /* found! */
2590 ++p;
2591 }
2592 }
2593 else
2594 p = NULL;
2595 }
2596 else
2597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 while ((p = vim_strchr(p, '/')) != NULL)
2599 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002600 /* accept a double /, unless it's preceded with * and followed by *,
2601 * because * / / * is an end and start of a C comment */
2602 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 break;
2604 ++p;
2605 }
2606
2607 if (p == NULL)
2608 return MAXCOL;
2609 return (int)(p - line);
2610}
2611
2612/*
2613 * Move cursor briefly to character matching the one under the cursor.
2614 * Used for Insert mode and "r" command.
2615 * Show the match only if it is visible on the screen.
2616 * If there isn't a match, then beep.
2617 */
2618 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002619showmatch(
2620 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621{
2622 pos_T *lpos, save_cursor;
2623 pos_T mpos;
2624 colnr_T vcol;
2625 long save_so;
2626 long save_siso;
2627#ifdef CURSOR_SHAPE
2628 int save_state;
2629#endif
2630 colnr_T save_dollar_vcol;
2631 char_u *p;
2632
2633 /*
2634 * Only show match for chars in the 'matchpairs' option.
2635 */
2636 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002637 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002640 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002641 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002642#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002643 p += MB_PTR2LEN(p) + 1;
2644 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#ifdef FEAT_RIGHTLEFT
2646 && !(curwin->w_p_rl ^ p_ri)
2647#endif
2648 )
2649 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002650 p += MB_PTR2LEN(p);
2651 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return;
2653 }
2654
2655 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002656 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002657 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
2659 if (!curwin->w_p_wrap)
2660 getvcol(curwin, lpos, NULL, &vcol, NULL);
2661 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002662 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
2664 mpos = *lpos; /* save the pos, update_screen() may change it */
2665 save_cursor = curwin->w_cursor;
2666 save_so = p_so;
2667 save_siso = p_siso;
2668 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2669 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002670 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2671 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 ++curwin->w_virtcol; /* do display ')' just before "$" */
2673 update_screen(VALID); /* show the new char first */
2674
2675 save_dollar_vcol = dollar_vcol;
2676#ifdef CURSOR_SHAPE
2677 save_state = State;
2678 State = SHOWMATCH;
2679 ui_cursor_shape(); /* may show different cursor shape */
2680#endif
2681 curwin->w_cursor = mpos; /* move to matching char */
2682 p_so = 0; /* don't use 'scrolloff' here */
2683 p_siso = 0; /* don't use 'sidescrolloff' here */
2684 showruler(FALSE);
2685 setcursor();
2686 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002687 out_flush_cursor(TRUE, FALSE);
2688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2690 * which resets it if the matching position is in a previous line
2691 * and has a higher column number. */
2692 dollar_vcol = save_dollar_vcol;
2693
2694 /*
2695 * brief pause, unless 'm' is present in 'cpo' and a character is
2696 * available.
2697 */
2698 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2699 ui_delay(p_mat * 100L, TRUE);
2700 else if (!char_avail())
2701 ui_delay(p_mat * 100L, FALSE);
2702 curwin->w_cursor = save_cursor; /* restore cursor position */
2703 p_so = save_so;
2704 p_siso = save_siso;
2705#ifdef CURSOR_SHAPE
2706 State = save_state;
2707 ui_cursor_shape(); /* may show different cursor shape */
2708#endif
2709 }
2710 }
2711}
2712
2713/*
2714 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002715 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 * space or a line break. Also stop at an empty line.
2717 * Return OK if the next sentence was found.
2718 */
2719 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002720findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 pos_T pos, tpos;
2723 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002724 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 int startlnum;
2726 int noskip = FALSE; /* do not skip blanks */
2727 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002728 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 pos = curwin->w_cursor;
2731 if (dir == FORWARD)
2732 func = incl;
2733 else
2734 func = decl;
2735
2736 while (count--)
2737 {
2738 /*
2739 * if on an empty line, skip upto a non-empty line
2740 */
2741 if (gchar_pos(&pos) == NUL)
2742 {
2743 do
2744 if ((*func)(&pos) == -1)
2745 break;
2746 while (gchar_pos(&pos) == NUL);
2747 if (dir == FORWARD)
2748 goto found;
2749 }
2750 /*
2751 * if on the start of a paragraph or a section and searching forward,
2752 * go to the next line
2753 */
2754 else if (dir == FORWARD && pos.col == 0 &&
2755 startPS(pos.lnum, NUL, FALSE))
2756 {
2757 if (pos.lnum == curbuf->b_ml.ml_line_count)
2758 return FAIL;
2759 ++pos.lnum;
2760 goto found;
2761 }
2762 else if (dir == BACKWARD)
2763 decl(&pos);
2764
2765 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002766 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2768 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2769 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002770 if (vim_strchr((char_u *)".!?", c) != NULL)
2771 {
2772 /* Only skip over a '.', '!' and '?' once. */
2773 if (found_dot)
2774 break;
2775 found_dot = TRUE;
2776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (decl(&pos) == -1)
2778 break;
2779 /* when going forward: Stop in front of empty line */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002780 if (LINEEMPTY(pos.lnum) && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
2782 incl(&pos);
2783 goto found;
2784 }
2785 }
2786
2787 /* remember the line where the search started */
2788 startlnum = pos.lnum;
2789 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2790
2791 for (;;) /* find end of sentence */
2792 {
2793 c = gchar_pos(&pos);
2794 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2795 {
2796 if (dir == BACKWARD && pos.lnum != startlnum)
2797 ++pos.lnum;
2798 break;
2799 }
2800 if (c == '.' || c == '!' || c == '?')
2801 {
2802 tpos = pos;
2803 do
2804 if ((c = inc(&tpos)) == -1)
2805 break;
2806 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2807 != NULL);
2808 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2809 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2810 && gchar_pos(&tpos) == ' ')))
2811 {
2812 pos = tpos;
2813 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2814 inc(&pos);
2815 break;
2816 }
2817 }
2818 if ((*func)(&pos) == -1)
2819 {
2820 if (count)
2821 return FAIL;
2822 noskip = TRUE;
2823 break;
2824 }
2825 }
2826found:
2827 /* skip white space */
2828 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2829 if (incl(&pos) == -1)
2830 break;
2831 }
2832
2833 setpcmark();
2834 curwin->w_cursor = pos;
2835 return OK;
2836}
2837
2838/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002839 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002841 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 * If 'what' is '{' or '}' we go to the next section.
2843 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002844 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 */
2846 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002847findpar(
2848 int *pincl, /* Return: TRUE if last char is to be included */
2849 int dir,
2850 long count,
2851 int what,
2852 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 linenr_T curr;
2855 int did_skip; /* TRUE after separating lines have been skipped */
2856 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002857 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858#ifdef FEAT_FOLDING
2859 linenr_T fold_first; /* first line of a closed fold */
2860 linenr_T fold_last; /* last line of a closed fold */
2861 int fold_skipped; /* TRUE if a closed fold was skipped this
2862 iteration */
2863#endif
2864
2865 curr = curwin->w_cursor.lnum;
2866
2867 while (count--)
2868 {
2869 did_skip = FALSE;
2870 for (first = TRUE; ; first = FALSE)
2871 {
2872 if (*ml_get(curr) != NUL)
2873 did_skip = TRUE;
2874
2875#ifdef FEAT_FOLDING
2876 /* skip folded lines */
2877 fold_skipped = FALSE;
2878 if (first && hasFolding(curr, &fold_first, &fold_last))
2879 {
2880 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2881 fold_skipped = TRUE;
2882 }
2883#endif
2884
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002885 /* POSIX has it's own ideas of what a paragraph boundary is and it
2886 * doesn't match historical Vi: It also stops at a "{" in the
2887 * first column and at an empty line. */
2888 if (!first && did_skip && (startPS(curr, what, both)
2889 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 break;
2891
2892#ifdef FEAT_FOLDING
2893 if (fold_skipped)
2894 curr -= dir;
2895#endif
2896 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2897 {
2898 if (count)
2899 return FALSE;
2900 curr -= dir;
2901 break;
2902 }
2903 }
2904 }
2905 setpcmark();
2906 if (both && *ml_get(curr) == '}') /* include line with '}' */
2907 ++curr;
2908 curwin->w_cursor.lnum = curr;
2909 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2910 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002911 char_u *line = ml_get(curr);
2912
2913 /* Put the cursor on the last character in the last line and make the
2914 * motion inclusive. */
2915 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
2917 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002918#ifdef FEAT_MBYTE
2919 curwin->w_cursor.col -=
2920 (*mb_head_off)(line, line + curwin->w_cursor.col);
2921#endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002922 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 }
2924 }
2925 else
2926 curwin->w_cursor.col = 0;
2927 return TRUE;
2928}
2929
2930/*
2931 * check if the string 's' is a nroff macro that is in option 'opt'
2932 */
2933 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002934inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935{
2936 char_u *macro;
2937
2938 for (macro = opt; macro[0]; ++macro)
2939 {
2940 /* Accept two characters in the option being equal to two characters
2941 * in the line. A space in the option matches with a space in the
2942 * line or the line having ended. */
2943 if ( (macro[0] == s[0]
2944 || (macro[0] == ' '
2945 && (s[0] == NUL || s[0] == ' ')))
2946 && (macro[1] == s[1]
2947 || ((macro[1] == NUL || macro[1] == ' ')
2948 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2949 break;
2950 ++macro;
2951 if (macro[0] == NUL)
2952 break;
2953 }
2954 return (macro[0] != NUL);
2955}
2956
2957/*
2958 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2959 * If 'para' is '{' or '}' only check for sections.
2960 * If 'both' is TRUE also stop at '}'
2961 */
2962 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002963startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 char_u *s;
2966
2967 s = ml_get(lnum);
2968 if (*s == para || *s == '\f' || (both && *s == '}'))
2969 return TRUE;
2970 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2971 (!para && inmacro(p_para, s + 1))))
2972 return TRUE;
2973 return FALSE;
2974}
2975
2976/*
2977 * The following routines do the word searches performed by the 'w', 'W',
2978 * 'b', 'B', 'e', and 'E' commands.
2979 */
2980
2981/*
2982 * To perform these searches, characters are placed into one of three
2983 * classes, and transitions between classes determine word boundaries.
2984 *
2985 * The classes are:
2986 *
2987 * 0 - white space
2988 * 1 - punctuation
2989 * 2 or higher - keyword characters (letters, digits and underscore)
2990 */
2991
2992static int cls_bigword; /* TRUE for "W", "B" or "E" */
2993
2994/*
2995 * cls() - returns the class of character at curwin->w_cursor
2996 *
2997 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2998 * from class 2 and higher are reported as class 1 since only white space
2999 * boundaries are of interest.
3000 */
3001 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003002cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 int c;
3005
3006 c = gchar_cursor();
3007#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
3008 if (p_altkeymap && c == F_BLANK)
3009 return 0;
3010#endif
3011 if (c == ' ' || c == '\t' || c == NUL)
3012 return 0;
3013#ifdef FEAT_MBYTE
3014 if (enc_dbcs != 0 && c > 0xFF)
3015 {
3016 /* If cls_bigword, report multi-byte chars as class 1. */
3017 if (enc_dbcs == DBCS_KOR && cls_bigword)
3018 return 1;
3019
3020 /* process code leading/trailing bytes */
3021 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3022 }
3023 if (enc_utf8)
3024 {
3025 c = utf_class(c);
3026 if (c != 0 && cls_bigword)
3027 return 1;
3028 return c;
3029 }
3030#endif
3031
3032 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3033 if (cls_bigword)
3034 return 1;
3035
3036 if (vim_iswordc(c))
3037 return 2;
3038 return 1;
3039}
3040
3041
3042/*
3043 * fwd_word(count, type, eol) - move forward one word
3044 *
3045 * Returns FAIL if the cursor was already at the end of the file.
3046 * If eol is TRUE, last word stops at end of line (for operators).
3047 */
3048 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003049fwd_word(
3050 long count,
3051 int bigword, /* "W", "E" or "B" */
3052 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 int sclass; /* starting class */
3055 int i;
3056 int last_line;
3057
3058#ifdef FEAT_VIRTUALEDIT
3059 curwin->w_cursor.coladd = 0;
3060#endif
3061 cls_bigword = bigword;
3062 while (--count >= 0)
3063 {
3064#ifdef FEAT_FOLDING
3065 /* When inside a range of folded lines, move to the last char of the
3066 * last line. */
3067 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3068 coladvance((colnr_T)MAXCOL);
3069#endif
3070 sclass = cls();
3071
3072 /*
3073 * We always move at least one character, unless on the last
3074 * character in the buffer.
3075 */
3076 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3077 i = inc_cursor();
3078 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3079 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003080 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 return OK;
3082
3083 /*
3084 * Go one char past end of current word (if any)
3085 */
3086 if (sclass != 0)
3087 while (cls() == sclass)
3088 {
3089 i = inc_cursor();
3090 if (i == -1 || (i >= 1 && eol && count == 0))
3091 return OK;
3092 }
3093
3094 /*
3095 * go to next non-white
3096 */
3097 while (cls() == 0)
3098 {
3099 /*
3100 * We'll stop if we land on a blank line
3101 */
3102 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3103 break;
3104
3105 i = inc_cursor();
3106 if (i == -1 || (i >= 1 && eol && count == 0))
3107 return OK;
3108 }
3109 }
3110 return OK;
3111}
3112
3113/*
3114 * bck_word() - move backward 'count' words
3115 *
3116 * If stop is TRUE and we are already on the start of a word, move one less.
3117 *
3118 * Returns FAIL if top of the file was reached.
3119 */
3120 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003121bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122{
3123 int sclass; /* starting class */
3124
3125#ifdef FEAT_VIRTUALEDIT
3126 curwin->w_cursor.coladd = 0;
3127#endif
3128 cls_bigword = bigword;
3129 while (--count >= 0)
3130 {
3131#ifdef FEAT_FOLDING
3132 /* When inside a range of folded lines, move to the first char of the
3133 * first line. */
3134 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3135 curwin->w_cursor.col = 0;
3136#endif
3137 sclass = cls();
3138 if (dec_cursor() == -1) /* started at start of file */
3139 return FAIL;
3140
3141 if (!stop || sclass == cls() || sclass == 0)
3142 {
3143 /*
3144 * Skip white space before the word.
3145 * Stop on an empty line.
3146 */
3147 while (cls() == 0)
3148 {
3149 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003150 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 goto finished;
3152 if (dec_cursor() == -1) /* hit start of file, stop here */
3153 return OK;
3154 }
3155
3156 /*
3157 * Move backward to start of this word.
3158 */
3159 if (skip_chars(cls(), BACKWARD))
3160 return OK;
3161 }
3162
3163 inc_cursor(); /* overshot - forward one */
3164finished:
3165 stop = FALSE;
3166 }
3167 return OK;
3168}
3169
3170/*
3171 * end_word() - move to the end of the word
3172 *
3173 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3174 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3175 * motion crosses blank lines. When the real vi crosses a blank line in an
3176 * 'e' motion, the cursor is placed on the FIRST character of the next
3177 * non-blank line. The 'E' command, however, works correctly. Since this
3178 * appears to be a bug, I have not duplicated it here.
3179 *
3180 * Returns FAIL if end of the file was reached.
3181 *
3182 * If stop is TRUE and we are already on the end of a word, move one less.
3183 * If empty is TRUE stop on an empty line.
3184 */
3185 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003186end_word(
3187 long count,
3188 int bigword,
3189 int stop,
3190 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 int sclass; /* starting class */
3193
3194#ifdef FEAT_VIRTUALEDIT
3195 curwin->w_cursor.coladd = 0;
3196#endif
3197 cls_bigword = bigword;
3198 while (--count >= 0)
3199 {
3200#ifdef FEAT_FOLDING
3201 /* When inside a range of folded lines, move to the last char of the
3202 * last line. */
3203 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3204 coladvance((colnr_T)MAXCOL);
3205#endif
3206 sclass = cls();
3207 if (inc_cursor() == -1)
3208 return FAIL;
3209
3210 /*
3211 * If we're in the middle of a word, we just have to move to the end
3212 * of it.
3213 */
3214 if (cls() == sclass && sclass != 0)
3215 {
3216 /*
3217 * Move forward to end of the current word
3218 */
3219 if (skip_chars(sclass, FORWARD))
3220 return FAIL;
3221 }
3222 else if (!stop || sclass == 0)
3223 {
3224 /*
3225 * We were at the end of a word. Go to the end of the next word.
3226 * First skip white space, if 'empty' is TRUE, stop at empty line.
3227 */
3228 while (cls() == 0)
3229 {
3230 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003231 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 goto finished;
3233 if (inc_cursor() == -1) /* hit end of file, stop here */
3234 return FAIL;
3235 }
3236
3237 /*
3238 * Move forward to the end of this word.
3239 */
3240 if (skip_chars(cls(), FORWARD))
3241 return FAIL;
3242 }
3243 dec_cursor(); /* overshot - one char backward */
3244finished:
3245 stop = FALSE; /* we move only one word less */
3246 }
3247 return OK;
3248}
3249
3250/*
3251 * Move back to the end of the word.
3252 *
3253 * Returns FAIL if start of the file was reached.
3254 */
3255 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003256bckend_word(
3257 long count,
3258 int bigword, /* TRUE for "B" */
3259 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260{
3261 int sclass; /* starting class */
3262 int i;
3263
3264#ifdef FEAT_VIRTUALEDIT
3265 curwin->w_cursor.coladd = 0;
3266#endif
3267 cls_bigword = bigword;
3268 while (--count >= 0)
3269 {
3270 sclass = cls();
3271 if ((i = dec_cursor()) == -1)
3272 return FAIL;
3273 if (eol && i == 1)
3274 return OK;
3275
3276 /*
3277 * Move backward to before the start of this word.
3278 */
3279 if (sclass != 0)
3280 {
3281 while (cls() == sclass)
3282 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3283 return OK;
3284 }
3285
3286 /*
3287 * Move backward to end of the previous word
3288 */
3289 while (cls() == 0)
3290 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003291 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 break;
3293 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3294 return OK;
3295 }
3296 }
3297 return OK;
3298}
3299
3300/*
3301 * Skip a row of characters of the same class.
3302 * Return TRUE when end-of-file reached, FALSE otherwise.
3303 */
3304 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003305skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 while (cls() == cclass)
3308 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3309 return TRUE;
3310 return FALSE;
3311}
3312
3313#ifdef FEAT_TEXTOBJ
3314/*
3315 * Go back to the start of the word or the start of white space
3316 */
3317 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003318back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 int sclass; /* starting class */
3321
3322 sclass = cls();
3323 for (;;)
3324 {
3325 if (curwin->w_cursor.col == 0) /* stop at start of line */
3326 break;
3327 dec_cursor();
3328 if (cls() != sclass) /* stop at start of word */
3329 {
3330 inc_cursor();
3331 break;
3332 }
3333 }
3334}
3335
3336 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003337find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 int c;
3340
3341 while (decl(posp) != -1)
3342 {
3343 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003344 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
3346 incl(posp);
3347 break;
3348 }
3349 }
3350}
3351
3352/*
3353 * Skip count/2 sentences and count/2 separating white spaces.
3354 */
3355 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003356findsent_forward(
3357 long count,
3358 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 while (count--)
3361 {
3362 findsent(FORWARD, 1L);
3363 if (at_start_sent)
3364 find_first_blank(&curwin->w_cursor);
3365 if (count == 0 || at_start_sent)
3366 decl(&curwin->w_cursor);
3367 at_start_sent = !at_start_sent;
3368 }
3369}
3370
3371/*
3372 * Find word under cursor, cursor at end.
3373 * Used while an operator is pending, and in Visual mode.
3374 */
3375 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003376current_word(
3377 oparg_T *oap,
3378 long count,
3379 int include, /* TRUE: include word and white space */
3380 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 pos_T start_pos;
3383 pos_T pos;
3384 int inclusive = TRUE;
3385 int include_white = FALSE;
3386
3387 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003388 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003391 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 dec_cursor();
3393
3394 /*
3395 * When Visual mode is not active, or when the VIsual area is only one
3396 * character, select the word and/or white space under the cursor.
3397 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003398 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
3400 /*
3401 * Go to start of current word or white space.
3402 */
3403 back_in_line();
3404 start_pos = curwin->w_cursor;
3405
3406 /*
3407 * If the start is on white space, and white space should be included
3408 * (" word"), or start is not on white space, and white space should
3409 * not be included ("word"), find end of word.
3410 */
3411 if ((cls() == 0) == include)
3412 {
3413 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3414 return FAIL;
3415 }
3416 else
3417 {
3418 /*
3419 * If the start is not on white space, and white space should be
3420 * included ("word "), or start is on white space and white
3421 * space should not be included (" "), find start of word.
3422 * If we end up in the first column of the next line (single char
3423 * word) back up to end of the line.
3424 */
3425 fwd_word(1L, bigword, TRUE);
3426 if (curwin->w_cursor.col == 0)
3427 decl(&curwin->w_cursor);
3428 else
3429 oneleft();
3430
3431 if (include)
3432 include_white = TRUE;
3433 }
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (VIsual_active)
3436 {
3437 /* should do something when inclusive == FALSE ! */
3438 VIsual = start_pos;
3439 redraw_curbuf_later(INVERTED); /* update the inversion */
3440 }
3441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
3443 oap->start = start_pos;
3444 oap->motion_type = MCHAR;
3445 }
3446 --count;
3447 }
3448
3449 /*
3450 * When count is still > 0, extend with more objects.
3451 */
3452 while (count > 0)
3453 {
3454 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003455 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 {
3457 /*
3458 * In Visual mode, with cursor at start: move cursor back.
3459 */
3460 if (decl(&curwin->w_cursor) == -1)
3461 return FAIL;
3462 if (include != (cls() != 0))
3463 {
3464 if (bck_word(1L, bigword, TRUE) == FAIL)
3465 return FAIL;
3466 }
3467 else
3468 {
3469 if (bckend_word(1L, bigword, TRUE) == FAIL)
3470 return FAIL;
3471 (void)incl(&curwin->w_cursor);
3472 }
3473 }
3474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 /*
3477 * Move cursor forward one word and/or white area.
3478 */
3479 if (incl(&curwin->w_cursor) == -1)
3480 return FAIL;
3481 if (include != (cls() == 0))
3482 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003483 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 return FAIL;
3485 /*
3486 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003487 * the first character on the line.
3488 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003490 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 inclusive = FALSE;
3492 }
3493 else
3494 {
3495 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3496 return FAIL;
3497 }
3498 }
3499 --count;
3500 }
3501
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003502 if (include_white && (cls() != 0
3503 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 /*
3506 * If we don't include white space at the end, move the start
3507 * to include some white space there. This makes "daw" work
3508 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003509 * word). Also when "2daw" deletes "word." at the end of the line
3510 * (cursor is at start of next line).
3511 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 */
3513 pos = curwin->w_cursor; /* save cursor position */
3514 curwin->w_cursor = start_pos;
3515 if (oneleft() == OK)
3516 {
3517 back_in_line();
3518 if (cls() == 0 && curwin->w_cursor.col > 0)
3519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (VIsual_active)
3521 VIsual = curwin->w_cursor;
3522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 oap->start = curwin->w_cursor;
3524 }
3525 }
3526 curwin->w_cursor = pos; /* put cursor back at end */
3527 }
3528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 if (VIsual_active)
3530 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003531 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 inc_cursor();
3533 if (VIsual_mode == 'V')
3534 {
3535 VIsual_mode = 'v';
3536 redraw_cmdline = TRUE; /* show mode later */
3537 }
3538 }
3539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 oap->inclusive = inclusive;
3541
3542 return OK;
3543}
3544
3545/*
3546 * Find sentence(s) under the cursor, cursor at end.
3547 * When Visual active, extend it by one or more sentences.
3548 */
3549 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003550current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
3552 pos_T start_pos;
3553 pos_T pos;
3554 int start_blank;
3555 int c;
3556 int at_start_sent;
3557 long ncount;
3558
3559 start_pos = curwin->w_cursor;
3560 pos = start_pos;
3561 findsent(FORWARD, 1L); /* Find start of next sentence. */
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003564 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003566 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 {
3568extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003569 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 /*
3572 * Cursor at start of Visual area.
3573 * Find out where we are:
3574 * - in the white space before a sentence
3575 * - in a sentence or just after it
3576 * - at the start of a sentence
3577 */
3578 at_start_sent = TRUE;
3579 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003580 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003583 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 at_start_sent = FALSE;
3586 break;
3587 }
3588 incl(&pos);
3589 }
3590 if (!at_start_sent)
3591 {
3592 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003593 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 at_start_sent = TRUE; /* exactly at start of sentence */
3595 else
3596 /* inside a sentence, go to its end (start of next) */
3597 findsent(FORWARD, 1L);
3598 }
3599 if (include) /* "as" gets twice as much as "is" */
3600 count *= 2;
3601 while (count--)
3602 {
3603 if (at_start_sent)
3604 find_first_blank(&curwin->w_cursor);
3605 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003606 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 findsent(BACKWARD, 1L);
3608 at_start_sent = !at_start_sent;
3609 }
3610 }
3611 else
3612 {
3613 /*
3614 * Cursor at end of Visual area.
3615 * Find out where we are:
3616 * - just before a sentence
3617 * - just before or in the white space before a sentence
3618 * - in a sentence
3619 */
3620 incl(&pos);
3621 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003622 /* not just before a sentence */
3623 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 {
3625 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003626 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003629 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 {
3631 at_start_sent = TRUE;
3632 break;
3633 }
3634 incl(&pos);
3635 }
3636 if (at_start_sent) /* in the sentence */
3637 findsent(BACKWARD, 1L);
3638 else /* in/before white before a sentence */
3639 curwin->w_cursor = start_pos;
3640 }
3641
3642 if (include) /* "as" gets twice as much as "is" */
3643 count *= 2;
3644 findsent_forward(count, at_start_sent);
3645 if (*p_sel == 'e')
3646 ++curwin->w_cursor.col;
3647 }
3648 return OK;
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003652 * If the cursor started on a blank, check if it is just before the start
3653 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003655 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003657 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 start_blank = TRUE;
3660 find_first_blank(&start_pos); /* go back to first blank */
3661 }
3662 else
3663 {
3664 start_blank = FALSE;
3665 findsent(BACKWARD, 1L);
3666 start_pos = curwin->w_cursor;
3667 }
3668 if (include)
3669 ncount = count * 2;
3670 else
3671 {
3672 ncount = count;
3673 if (start_blank)
3674 --ncount;
3675 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003676 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 findsent_forward(ncount, TRUE);
3678 else
3679 decl(&curwin->w_cursor);
3680
3681 if (include)
3682 {
3683 /*
3684 * If the blank in front of the sentence is included, exclude the
3685 * blanks at the end of the sentence, go back to the first blank.
3686 * If there are no trailing blanks, try to include leading blanks.
3687 */
3688 if (start_blank)
3689 {
3690 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003691 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3692 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 decl(&curwin->w_cursor);
3694 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003695 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 find_first_blank(&start_pos);
3697 }
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 if (VIsual_active)
3700 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003701 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003702 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 goto extend;
3704 if (*p_sel == 'e')
3705 ++curwin->w_cursor.col;
3706 VIsual = start_pos;
3707 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003708 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 redraw_curbuf_later(INVERTED); /* update the inversion */
3710 }
3711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 {
3713 /* include a newline after the sentence, if there is one */
3714 if (incl(&curwin->w_cursor) == -1)
3715 oap->inclusive = TRUE;
3716 else
3717 oap->inclusive = FALSE;
3718 oap->start = start_pos;
3719 oap->motion_type = MCHAR;
3720 }
3721 return OK;
3722}
3723
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003724/*
3725 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003726 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003729current_block(
3730 oparg_T *oap,
3731 long count,
3732 int include, /* TRUE == include white space */
3733 int what, /* '(', '{', etc. */
3734 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
3736 pos_T old_pos;
3737 pos_T *pos = NULL;
3738 pos_T start_pos;
3739 pos_T *end_pos;
3740 pos_T old_start, old_end;
3741 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003742 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003745 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 old_start = old_end;
3747
3748 /*
3749 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3750 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003751 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003754 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 while (inindent(1))
3756 if (inc_cursor() != 0)
3757 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003758 if (gchar_cursor() == what)
3759 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 ++curwin->w_cursor.col;
3761 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003762 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 {
3764 old_start = VIsual;
3765 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3766 }
3767 else
3768 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 /*
3771 * Search backwards for unclosed '(', '{', etc..
3772 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003773 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3774 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003777 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 while (count-- > 0)
3779 {
3780 if ((pos = findmatch(NULL, what)) == NULL)
3781 break;
3782 curwin->w_cursor = *pos;
3783 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3784 }
3785 p_cpo = save_cpo;
3786
3787 /*
3788 * Search for matching ')', '}', etc.
3789 * Put this position in curwin->w_cursor.
3790 */
3791 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3792 {
3793 curwin->w_cursor = old_pos;
3794 return FAIL;
3795 }
3796 curwin->w_cursor = *end_pos;
3797
3798 /*
3799 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003800 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3801 * indent. But only if the resulting area is not smaller than what we
3802 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 */
3804 while (!include)
3805 {
3806 incl(&start_pos);
3807 sol = (curwin->w_cursor.col == 0);
3808 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003809 while (inindent(1))
3810 {
3811 sol = TRUE;
3812 if (decl(&curwin->w_cursor) != 0)
3813 break;
3814 }
3815
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 /*
3817 * In Visual mode, when the resulting area is not bigger than what we
3818 * started with, extend it to the next block, and then exclude again.
3819 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003820 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 && VIsual_active)
3822 {
3823 curwin->w_cursor = old_start;
3824 decl(&curwin->w_cursor);
3825 if ((pos = findmatch(NULL, what)) == NULL)
3826 {
3827 curwin->w_cursor = old_pos;
3828 return FAIL;
3829 }
3830 start_pos = *pos;
3831 curwin->w_cursor = *pos;
3832 if ((end_pos = findmatch(NULL, other)) == NULL)
3833 {
3834 curwin->w_cursor = old_pos;
3835 return FAIL;
3836 }
3837 curwin->w_cursor = *end_pos;
3838 }
3839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 break;
3841 }
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (VIsual_active)
3844 {
3845 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003846 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003847 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 inc(&curwin->w_cursor); /* include the line break */
3849 VIsual = start_pos;
3850 VIsual_mode = 'v';
3851 redraw_curbuf_later(INVERTED); /* update the inversion */
3852 showmode();
3853 }
3854 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856 oap->start = start_pos;
3857 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003858 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003861 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003862 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003864 else
3865 /* End is before the start (no text in between <>, [], etc.): don't
3866 * operate on any text. */
3867 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869
3870 return OK;
3871}
3872
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003873static int in_html_tag(int);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003874
3875/*
3876 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3877 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3878 */
3879 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003880in_html_tag(
3881 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003882{
3883 char_u *line = ml_get_curline();
3884 char_u *p;
3885 int c;
3886 int lc = NUL;
3887 pos_T pos;
3888
3889#ifdef FEAT_MBYTE
3890 if (enc_dbcs)
3891 {
3892 char_u *lp = NULL;
3893
3894 /* We search forward until the cursor, because searching backwards is
3895 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003896 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003897 if (*p == '>' || *p == '<')
3898 {
3899 lc = *p;
3900 lp = p;
3901 }
3902 if (*p != '<') /* check for '<' under cursor */
3903 {
3904 if (lc != '<')
3905 return FALSE;
3906 p = lp;
3907 }
3908 }
3909 else
3910#endif
3911 {
3912 for (p = line + curwin->w_cursor.col; p > line; )
3913 {
3914 if (*p == '<') /* find '<' under/before cursor */
3915 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003916 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003917 if (*p == '>') /* find '>' before cursor */
3918 break;
3919 }
3920 if (*p != '<')
3921 return FALSE;
3922 }
3923
3924 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003925 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003926
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003927 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003928 if (end_tag)
3929 /* check that there is a '/' after the '<' */
3930 return *p == '/';
3931
3932 /* check that there is no '/' after the '<' */
3933 if (*p == '/')
3934 return FALSE;
3935
3936 /* check that the matching '>' is not preceded by '/' */
3937 for (;;)
3938 {
3939 if (inc(&pos) < 0)
3940 return FALSE;
3941 c = *ml_get_pos(&pos);
3942 if (c == '>')
3943 break;
3944 lc = c;
3945 }
3946 return lc != '/';
3947}
3948
3949/*
3950 * Find tag block under the cursor, cursor at end.
3951 */
3952 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003953current_tagblock(
3954 oparg_T *oap,
3955 long count_arg,
3956 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003957{
3958 long count = count_arg;
3959 long n;
3960 pos_T old_pos;
3961 pos_T start_pos;
3962 pos_T end_pos;
3963 pos_T old_start, old_end;
3964 char_u *spat, *epat;
3965 char_u *p;
3966 char_u *cp;
3967 int len;
3968 int r;
3969 int do_include = include;
3970 int save_p_ws = p_ws;
3971 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003972 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003973
3974 p_ws = FALSE;
3975
3976 old_pos = curwin->w_cursor;
3977 old_end = curwin->w_cursor; /* remember where we started */
3978 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003979 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003980 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003981
3982 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003983 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003984 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003985 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003986 {
3987 setpcmark();
3988
3989 /* ignore indent */
3990 while (inindent(1))
3991 if (inc_cursor() != 0)
3992 break;
3993
3994 if (in_html_tag(FALSE))
3995 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003996 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003997 while (*ml_get_cursor() != '>')
3998 if (inc_cursor() < 0)
3999 break;
4000 }
4001 else if (in_html_tag(TRUE))
4002 {
4003 /* cursor on end tag, move to just before it */
4004 while (*ml_get_cursor() != '<')
4005 if (dec_cursor() < 0)
4006 break;
4007 dec_cursor();
4008 old_end = curwin->w_cursor;
4009 }
4010 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004011 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004012 {
4013 old_start = VIsual;
4014 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4015 }
4016 else
4017 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004018
4019again:
4020 /*
4021 * Search backwards for unclosed "<aaa>".
4022 * Put this position in start_pos.
4023 */
4024 for (n = 0; n < count; ++n)
4025 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004026 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004027 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004028 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004029 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004030 {
4031 curwin->w_cursor = old_pos;
4032 goto theend;
4033 }
4034 }
4035 start_pos = curwin->w_cursor;
4036
4037 /*
4038 * Search for matching "</aaa>". First isolate the "aaa".
4039 */
4040 inc_cursor();
4041 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004042 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004043 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004044 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004045 if (len == 0)
4046 {
4047 curwin->w_cursor = old_pos;
4048 goto theend;
4049 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004050 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004051 epat = alloc(len + 9);
4052 if (spat == NULL || epat == NULL)
4053 {
4054 vim_free(spat);
4055 vim_free(epat);
4056 curwin->w_cursor = old_pos;
4057 goto theend;
4058 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004059 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004060 sprintf((char *)epat, "</%.*s>\\c", len, p);
4061
Bram Moolenaar48570482017-10-30 21:48:41 +01004062 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004063 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004064
4065 vim_free(spat);
4066 vim_free(epat);
4067
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004068 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004069 {
4070 /* Can't find other end or it's before the previous end. Could be a
4071 * HTML tag that doesn't have a matching end. Search backwards for
4072 * another starting tag. */
4073 count = 1;
4074 curwin->w_cursor = start_pos;
4075 goto again;
4076 }
4077
4078 if (do_include || r < 1)
4079 {
4080 /* Include up to the '>'. */
4081 while (*ml_get_cursor() != '>')
4082 if (inc_cursor() < 0)
4083 break;
4084 }
4085 else
4086 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004087 char_u *c = ml_get_cursor();
4088
4089 /* Exclude the '<' of the end tag.
4090 * If the closing tag is on new line, do not decrement cursor, but
4091 * make operation exclusive, so that the linefeed will be selected */
4092 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4093 /* do not decrement cursor */
4094 is_inclusive = FALSE;
4095 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004096 dec_cursor();
4097 }
4098 end_pos = curwin->w_cursor;
4099
4100 if (!do_include)
4101 {
4102 /* Exclude the start tag. */
4103 curwin->w_cursor = start_pos;
4104 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004105 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004106 {
4107 inc_cursor();
4108 start_pos = curwin->w_cursor;
4109 break;
4110 }
4111 curwin->w_cursor = end_pos;
4112
Bram Moolenaar45360022005-07-21 21:08:21 +00004113 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004114 * again. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004115 if (EQUAL_POS(start_pos, old_start) && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004116 {
4117 do_include = TRUE;
4118 curwin->w_cursor = old_start;
4119 count = count_arg;
4120 goto again;
4121 }
4122 }
4123
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004124 if (VIsual_active)
4125 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004126 /* If the end is before the start there is no text between tags, select
4127 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004128 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004129 curwin->w_cursor = start_pos;
4130 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004131 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004132 VIsual = start_pos;
4133 VIsual_mode = 'v';
4134 redraw_curbuf_later(INVERTED); /* update the inversion */
4135 showmode();
4136 }
4137 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004138 {
4139 oap->start = start_pos;
4140 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004141 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004142 {
4143 /* End is before the start: there is no text between tags; operate
4144 * on an empty area. */
4145 curwin->w_cursor = start_pos;
4146 oap->inclusive = FALSE;
4147 }
4148 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004149 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004150 }
4151 retval = OK;
4152
4153theend:
4154 p_ws = save_p_ws;
4155 return retval;
4156}
4157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004159current_par(
4160 oparg_T *oap,
4161 long count,
4162 int include, /* TRUE == include white space */
4163 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 linenr_T start_lnum;
4166 linenr_T end_lnum;
4167 int white_in_front;
4168 int dir;
4169 int start_is_white;
4170 int prev_start_is_white;
4171 int retval = OK;
4172 int do_white = FALSE;
4173 int t;
4174 int i;
4175
4176 if (type == 'S') /* not implemented yet */
4177 return FAIL;
4178
4179 start_lnum = curwin->w_cursor.lnum;
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 /*
4182 * When visual area is more than one line: extend it.
4183 */
4184 if (VIsual_active && start_lnum != VIsual.lnum)
4185 {
4186extend:
4187 if (start_lnum < VIsual.lnum)
4188 dir = BACKWARD;
4189 else
4190 dir = FORWARD;
4191 for (i = count; --i >= 0; )
4192 {
4193 if (start_lnum ==
4194 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4195 {
4196 retval = FAIL;
4197 break;
4198 }
4199
4200 prev_start_is_white = -1;
4201 for (t = 0; t < 2; ++t)
4202 {
4203 start_lnum += dir;
4204 start_is_white = linewhite(start_lnum);
4205 if (prev_start_is_white == start_is_white)
4206 {
4207 start_lnum -= dir;
4208 break;
4209 }
4210 for (;;)
4211 {
4212 if (start_lnum == (dir == BACKWARD
4213 ? 1 : curbuf->b_ml.ml_line_count))
4214 break;
4215 if (start_is_white != linewhite(start_lnum + dir)
4216 || (!start_is_white
4217 && startPS(start_lnum + (dir > 0
4218 ? 1 : 0), 0, 0)))
4219 break;
4220 start_lnum += dir;
4221 }
4222 if (!include)
4223 break;
4224 if (start_lnum == (dir == BACKWARD
4225 ? 1 : curbuf->b_ml.ml_line_count))
4226 break;
4227 prev_start_is_white = start_is_white;
4228 }
4229 }
4230 curwin->w_cursor.lnum = start_lnum;
4231 curwin->w_cursor.col = 0;
4232 return retval;
4233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /*
4236 * First move back to the start_lnum of the paragraph or white lines
4237 */
4238 white_in_front = linewhite(start_lnum);
4239 while (start_lnum > 1)
4240 {
4241 if (white_in_front) /* stop at first white line */
4242 {
4243 if (!linewhite(start_lnum - 1))
4244 break;
4245 }
4246 else /* stop at first non-white line of start of paragraph */
4247 {
4248 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4249 break;
4250 }
4251 --start_lnum;
4252 }
4253
4254 /*
4255 * Move past the end of any white lines.
4256 */
4257 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004258 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4259 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
4261 --end_lnum;
4262 i = count;
4263 if (!include && white_in_front)
4264 --i;
4265 while (i--)
4266 {
4267 if (end_lnum == curbuf->b_ml.ml_line_count)
4268 return FAIL;
4269
4270 if (!include)
4271 do_white = linewhite(end_lnum + 1);
4272
4273 if (include || !do_white)
4274 {
4275 ++end_lnum;
4276 /*
4277 * skip to end of paragraph
4278 */
4279 while (end_lnum < curbuf->b_ml.ml_line_count
4280 && !linewhite(end_lnum + 1)
4281 && !startPS(end_lnum + 1, 0, 0))
4282 ++end_lnum;
4283 }
4284
4285 if (i == 0 && white_in_front && include)
4286 break;
4287
4288 /*
4289 * skip to end of white lines after paragraph
4290 */
4291 if (include || do_white)
4292 while (end_lnum < curbuf->b_ml.ml_line_count
4293 && linewhite(end_lnum + 1))
4294 ++end_lnum;
4295 }
4296
4297 /*
4298 * If there are no empty lines at the end, try to find some empty lines at
4299 * the start (unless that has been done already).
4300 */
4301 if (!white_in_front && !linewhite(end_lnum) && include)
4302 while (start_lnum > 1 && linewhite(start_lnum - 1))
4303 --start_lnum;
4304
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 if (VIsual_active)
4306 {
4307 /* Problem: when doing "Vipipip" nothing happens in a single white
4308 * line, we get stuck there. Trap this here. */
4309 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4310 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004311 if (VIsual.lnum != start_lnum)
4312 {
4313 VIsual.lnum = start_lnum;
4314 VIsual.col = 0;
4315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 VIsual_mode = 'V';
4317 redraw_curbuf_later(INVERTED); /* update the inversion */
4318 showmode();
4319 }
4320 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322 oap->start.lnum = start_lnum;
4323 oap->start.col = 0;
4324 oap->motion_type = MLINE;
4325 }
4326 curwin->w_cursor.lnum = end_lnum;
4327 curwin->w_cursor.col = 0;
4328
4329 return OK;
4330}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004331
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004332static int find_next_quote(char_u *top_ptr, int col, int quotechar, char_u *escape);
4333static int find_prev_quote(char_u *line, int col_start, int quotechar, char_u *escape);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004334
4335/*
4336 * Search quote char from string line[col].
4337 * Quote character escaped by one of the characters in "escape" is not counted
4338 * as a quote.
4339 * Returns column number of "quotechar" or -1 when not found.
4340 */
4341 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004342find_next_quote(
4343 char_u *line,
4344 int col,
4345 int quotechar,
4346 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004347{
4348 int c;
4349
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004350 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004351 {
4352 c = line[col];
4353 if (c == NUL)
4354 return -1;
4355 else if (escape != NULL && vim_strchr(escape, c))
4356 ++col;
4357 else if (c == quotechar)
4358 break;
4359#ifdef FEAT_MBYTE
4360 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004361 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004364 ++col;
4365 }
4366 return col;
4367}
4368
4369/*
4370 * Search backwards in "line" from column "col_start" to find "quotechar".
4371 * Quote character escaped by one of the characters in "escape" is not counted
4372 * as a quote.
4373 * Return the found column or zero.
4374 */
4375 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004376find_prev_quote(
4377 char_u *line,
4378 int col_start,
4379 int quotechar,
4380 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004381{
4382 int n;
4383
4384 while (col_start > 0)
4385 {
4386 --col_start;
4387#ifdef FEAT_MBYTE
4388 col_start -= (*mb_head_off)(line, line + col_start);
4389#endif
4390 n = 0;
4391 if (escape != NULL)
4392 while (col_start - n > 0 && vim_strchr(escape,
4393 line[col_start - n - 1]) != NULL)
4394 ++n;
4395 if (n & 1)
4396 col_start -= n; /* uneven number of escape chars, skip it */
4397 else if (line[col_start] == quotechar)
4398 break;
4399 }
4400 return col_start;
4401}
4402
4403/*
4404 * Find quote under the cursor, cursor at end.
4405 * Returns TRUE if found, else FALSE.
4406 */
4407 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004408current_quote(
4409 oparg_T *oap,
4410 long count,
4411 int include, /* TRUE == include quote char */
4412 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004413{
4414 char_u *line = ml_get_curline();
4415 int col_end;
4416 int col_start = curwin->w_cursor.col;
4417 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004418 int vis_empty = TRUE; /* Visual selection <= 1 char */
4419 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004420 int inside_quotes = FALSE; /* Looks like "i'" done before */
4421 int selected_quote = FALSE; /* Has quote inside selection */
4422 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004423
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004424 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004425 if (VIsual_active)
4426 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004427 /* this only works within one line */
4428 if (VIsual.lnum != curwin->w_cursor.lnum)
4429 return FALSE;
4430
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004431 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004432 if (*p_sel == 'e')
4433 {
4434 if (!vis_bef_curs)
4435 {
4436 /* VIsual needs to be start of Visual selection. */
4437 pos_T t = curwin->w_cursor;
4438
4439 curwin->w_cursor = VIsual;
4440 VIsual = t;
4441 vis_bef_curs = TRUE;
4442 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004443 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004444 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004445 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004446 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004447
4448 if (!vis_empty)
4449 {
4450 /* Check if the existing selection exactly spans the text inside
4451 * quotes. */
4452 if (vis_bef_curs)
4453 {
4454 inside_quotes = VIsual.col > 0
4455 && line[VIsual.col - 1] == quotechar
4456 && line[curwin->w_cursor.col] != NUL
4457 && line[curwin->w_cursor.col + 1] == quotechar;
4458 i = VIsual.col;
4459 col_end = curwin->w_cursor.col;
4460 }
4461 else
4462 {
4463 inside_quotes = curwin->w_cursor.col > 0
4464 && line[curwin->w_cursor.col - 1] == quotechar
4465 && line[VIsual.col] != NUL
4466 && line[VIsual.col + 1] == quotechar;
4467 i = curwin->w_cursor.col;
4468 col_end = VIsual.col;
4469 }
4470
4471 /* Find out if we have a quote in the selection. */
4472 while (i <= col_end)
4473 if (line[i++] == quotechar)
4474 {
4475 selected_quote = TRUE;
4476 break;
4477 }
4478 }
4479
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004480 if (!vis_empty && line[col_start] == quotechar)
4481 {
4482 /* Already selecting something and on a quote character. Find the
4483 * next quoted string. */
4484 if (vis_bef_curs)
4485 {
4486 /* Assume we are on a closing quote: move to after the next
4487 * opening quote. */
4488 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4489 if (col_start < 0)
4490 return FALSE;
4491 col_end = find_next_quote(line, col_start + 1, quotechar,
4492 curbuf->b_p_qe);
4493 if (col_end < 0)
4494 {
4495 /* We were on a starting quote perhaps? */
4496 col_end = col_start;
4497 col_start = curwin->w_cursor.col;
4498 }
4499 }
4500 else
4501 {
4502 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4503 if (line[col_end] != quotechar)
4504 return FALSE;
4505 col_start = find_prev_quote(line, col_end, quotechar,
4506 curbuf->b_p_qe);
4507 if (line[col_start] != quotechar)
4508 {
4509 /* We were on an ending quote perhaps? */
4510 col_start = col_end;
4511 col_end = curwin->w_cursor.col;
4512 }
4513 }
4514 }
4515 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004516
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004517 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004518 {
4519 int first_col = col_start;
4520
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004521 if (!vis_empty)
4522 {
4523 if (vis_bef_curs)
4524 first_col = find_next_quote(line, col_start, quotechar, NULL);
4525 else
4526 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4527 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004528
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004529 /* The cursor is on a quote, we don't know if it's the opening or
4530 * closing quote. Search from the start of the line to find out.
4531 * Also do this when there is a Visual area, a' may leave the cursor
4532 * in between two strings. */
4533 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004534 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004535 {
4536 /* Find open quote character. */
4537 col_start = find_next_quote(line, col_start, quotechar, NULL);
4538 if (col_start < 0 || col_start > first_col)
4539 return FALSE;
4540 /* Find close quote character. */
4541 col_end = find_next_quote(line, col_start + 1, quotechar,
4542 curbuf->b_p_qe);
4543 if (col_end < 0)
4544 return FALSE;
4545 /* If is cursor between start and end quote character, it is
4546 * target text object. */
4547 if (col_start <= first_col && first_col <= col_end)
4548 break;
4549 col_start = col_end + 1;
4550 }
4551 }
4552 else
4553 {
4554 /* Search backward for a starting quote. */
4555 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4556 if (line[col_start] != quotechar)
4557 {
4558 /* No quote before the cursor, look after the cursor. */
4559 col_start = find_next_quote(line, col_start, quotechar, NULL);
4560 if (col_start < 0)
4561 return FALSE;
4562 }
4563
4564 /* Find close quote character. */
4565 col_end = find_next_quote(line, col_start + 1, quotechar,
4566 curbuf->b_p_qe);
4567 if (col_end < 0)
4568 return FALSE;
4569 }
4570
4571 /* When "include" is TRUE, include spaces after closing quote or before
4572 * the starting quote. */
4573 if (include)
4574 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004575 if (VIM_ISWHITE(line[col_end + 1]))
4576 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004577 ++col_end;
4578 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004579 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004580 --col_start;
4581 }
4582
Bram Moolenaarab194812005-09-14 21:40:12 +00004583 /* Set start position. After vi" another i" must include the ".
4584 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004585 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004586 ++col_start;
4587 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004588 if (VIsual_active)
4589 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004590 /* Set the start of the Visual area when the Visual area was empty, we
4591 * were just inside quotes or the Visual area didn't start at a quote
4592 * and didn't include a quote.
4593 */
4594 if (vis_empty
4595 || (vis_bef_curs
4596 && !selected_quote
4597 && (inside_quotes
4598 || (line[VIsual.col] != quotechar
4599 && (VIsual.col == 0
4600 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004601 {
4602 VIsual = curwin->w_cursor;
4603 redraw_curbuf_later(INVERTED);
4604 }
4605 }
4606 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004607 {
4608 oap->start = curwin->w_cursor;
4609 oap->motion_type = MCHAR;
4610 }
4611
4612 /* Set end position. */
4613 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004614 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004615 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004616 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004617 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004618 if (VIsual_active)
4619 {
4620 if (vis_empty || vis_bef_curs)
4621 {
4622 /* decrement cursor when 'selection' is not exclusive */
4623 if (*p_sel != 'e')
4624 dec_cursor();
4625 }
4626 else
4627 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004628 /* Cursor is at start of Visual area. Set the end of the Visual
4629 * area when it was just inside quotes or it didn't end at a
4630 * quote. */
4631 if (inside_quotes
4632 || (!selected_quote
4633 && line[VIsual.col] != quotechar
4634 && (line[VIsual.col] == NUL
4635 || line[VIsual.col + 1] != quotechar)))
4636 {
4637 dec_cursor();
4638 VIsual = curwin->w_cursor;
4639 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004640 curwin->w_cursor.col = col_start;
4641 }
4642 if (VIsual_mode == 'V')
4643 {
4644 VIsual_mode = 'v';
4645 redraw_cmdline = TRUE; /* show mode later */
4646 }
4647 }
4648 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004649 {
4650 /* Set inclusive and other oap's flags. */
4651 oap->inclusive = inclusive;
4652 }
4653
4654 return OK;
4655}
4656
4657#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004659static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004660
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004661/*
4662 * Find next search match under cursor, cursor at end.
4663 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004664 */
4665 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004666current_search(
4667 long count,
4668 int forward) /* move forward or backwards */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004669{
4670 pos_T start_pos; /* position before the pattern */
4671 pos_T orig_pos; /* position of the cursor at beginning */
4672 pos_T pos; /* position after the pattern */
4673 int i;
4674 int dir;
4675 int result; /* result of various function calls */
4676 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004677 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004678 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004679 int one_char;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004680 int direction = forward ? FORWARD : BACKWARD;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004681
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004682 /* wrapping should not occur */
4683 p_ws = FALSE;
4684
4685 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004686 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004687 dec_cursor();
4688
4689 if (VIsual_active)
4690 {
4691 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004692
4693 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004694
4695 /* make sure, searching further will extend the match */
4696 if (VIsual_active)
4697 {
4698 if (forward)
4699 incl(&pos);
4700 else
4701 decl(&pos);
4702 }
4703 }
4704 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004705 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004706
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004707 /* Is the pattern is zero-width?, this time, don't care about the direction
4708 */
4709 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4710 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004711 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004712 {
4713 p_ws = old_p_ws;
4714 return FAIL; /* pattern not found */
4715 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004716
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004717 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004718 * The trick is to first search backwards and then search forward again,
4719 * so that a match at the current cursor position will be correctly
4720 * captured.
4721 */
4722 for (i = 0; i < 2; i++)
4723 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004724 if (forward)
4725 dir = i;
4726 else
4727 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004728
4729 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004730 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004731 flags = SEARCH_END;
4732
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004733 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4734 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004735 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004736
4737 /* First search may fail, but then start searching from the
4738 * beginning of the file (cursor might be on the search match)
4739 * except when Visual mode is active, so that extending the visual
4740 * selection works. */
4741 if (!result && i) /* not found, abort */
4742 {
4743 curwin->w_cursor = orig_pos;
4744 if (VIsual_active)
4745 VIsual = save_VIsual;
4746 p_ws = old_p_ws;
4747 return FAIL;
4748 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004749 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004750 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004751 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004752 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004753 /* try again from start of buffer */
4754 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004755 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004756 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004757 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004758 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004759 /* searching backwards, so set pos to last line and col */
4760 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004761 pos.col = (colnr_T)STRLEN(
4762 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004763 }
4764 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004765 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004766 }
4767
4768 start_pos = pos;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004769 flags = forward ? SEARCH_END : SEARCH_START;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004770
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004771 /* Check again from the current cursor position,
4772 * since the next match might actually by only one char wide */
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004773 one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004774 if (one_char < 0)
4775 /* search failed, abort */
4776 return FAIL;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004777
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004778 /* move to match, except for zero-width matches, in which case, we are
4779 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004780 if (!one_char)
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004781 result = searchit(curwin, curbuf, &pos, direction,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004782 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
4783 NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004784
4785 if (!VIsual_active)
4786 VIsual = start_pos;
4787
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004788 curwin->w_cursor = pos;
4789 VIsual_active = TRUE;
4790 VIsual_mode = 'v';
4791
4792 if (VIsual_active)
4793 {
4794 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004795 if (*p_sel == 'e')
4796 {
4797 /* Correction for exclusive selection depends on the direction. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004798 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004799 inc_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004800 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004801 inc(&VIsual);
4802 }
4803
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004804 }
4805
4806#ifdef FEAT_FOLDING
4807 if (fdo_flags & FDO_SEARCH && KeyTyped)
4808 foldOpenCursor();
4809#endif
4810
4811 may_start_select('c');
4812#ifdef FEAT_MOUSE
4813 setmouse();
4814#endif
4815#ifdef FEAT_CLIPBOARD
4816 /* Make sure the clipboard gets updated. Needed because start and
4817 * end are still the same, and the selection needs to be owned */
4818 clip_star.vmode = NUL;
4819#endif
4820 redraw_curbuf_later(INVERTED);
4821 showmode();
4822
4823 return OK;
4824}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004825
4826/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004827 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004828 * If move is TRUE, check from the beginning of the buffer, else from position
4829 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004830 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004831 * Returns TRUE, FALSE or -1 for failure.
4832 */
4833 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004834is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004835{
4836 regmmatch_T regmatch;
4837 int nmatched = 0;
4838 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004839 pos_T pos;
4840 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004841 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004842
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004843 if (pattern == NULL)
4844 pattern = spats[last_idx].pat;
4845
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004846 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4847 SEARCH_KEEP, &regmatch) == FAIL)
4848 return -1;
4849
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004850 /* init startcol correctly */
4851 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004852 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004853 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004854 {
4855 CLEAR_POS(&pos);
4856 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004857 else
4858 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004859 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004860 /* accept a match at the cursor position */
4861 flag = SEARCH_START;
4862 }
4863
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004864 if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004865 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004866 {
4867 /* Zero-width pattern should match somewhere, then we can check if
4868 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004869 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004870 do
4871 {
4872 regmatch.startpos[0].col++;
4873 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004874 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004875 if (!nmatched)
4876 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004877 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4878 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004879
4880 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004881 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004882 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004883 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4884 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004885 /* one char width */
4886 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4887 result = TRUE;
4888 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004889 }
4890
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004891 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004892 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004893 return result;
4894}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4897 || defined(PROTO)
4898/*
4899 * return TRUE if line 'lnum' is empty or has white chars only.
4900 */
4901 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004902linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
4904 char_u *p;
4905
4906 p = skipwhite(ml_get(lnum));
4907 return (*p == NUL);
4908}
4909#endif
4910
4911#if defined(FEAT_FIND_ID) || defined(PROTO)
4912/*
4913 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004914 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004917find_pattern_in_path(
4918 char_u *ptr, /* pointer to search pattern */
4919 int dir UNUSED, /* direction of expansion */
4920 int len, /* length of search pattern */
4921 int whole, /* match whole words only */
4922 int skip_comments, /* don't match inside comments */
4923 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004925 long count,
4926 int action, /* What to do when we find it */
4927 linenr_T start_lnum, /* first line to start searching */
4928 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
4930 SearchedFile *files; /* Stack of included files */
4931 SearchedFile *bigger; /* When we need more space */
4932 int max_path_depth = 50;
4933 long match_count = 1;
4934
4935 char_u *pat;
4936 char_u *new_fname;
4937 char_u *curr_fname = curbuf->b_fname;
4938 char_u *prev_fname = NULL;
4939 linenr_T lnum;
4940 int depth;
4941 int depth_displayed; /* For type==CHECK_PATH */
4942 int old_files;
4943 int already_searched;
4944 char_u *file_line;
4945 char_u *line;
4946 char_u *p;
4947 char_u save_char;
4948 int define_matched;
4949 regmatch_T regmatch;
4950 regmatch_T incl_regmatch;
4951 regmatch_T def_regmatch;
4952 int matched = FALSE;
4953 int did_show = FALSE;
4954 int found = FALSE;
4955 int i;
4956 char_u *already = NULL;
4957 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004958 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004959#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 win_T *curwin_save = NULL;
4961#endif
4962
4963 regmatch.regprog = NULL;
4964 incl_regmatch.regprog = NULL;
4965 def_regmatch.regprog = NULL;
4966
4967 file_line = alloc(LSIZE);
4968 if (file_line == NULL)
4969 return;
4970
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (type != CHECK_PATH && type != FIND_DEFINE
4972#ifdef FEAT_INS_EXPAND
4973 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4974 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004975 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#endif
4977 )
4978 {
4979 pat = alloc(len + 5);
4980 if (pat == NULL)
4981 goto fpip_end;
4982 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4983 /* ignore case according to p_ic, p_scs and pat */
4984 regmatch.rm_ic = ignorecase(pat);
4985 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4986 vim_free(pat);
4987 if (regmatch.regprog == NULL)
4988 goto fpip_end;
4989 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004990 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4991 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004993 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 if (incl_regmatch.regprog == NULL)
4995 goto fpip_end;
4996 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4997 }
4998 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4999 {
5000 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
5001 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
5002 if (def_regmatch.regprog == NULL)
5003 goto fpip_end;
5004 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
5005 }
5006 files = (SearchedFile *)lalloc_clear((long_u)
5007 (max_path_depth * sizeof(SearchedFile)), TRUE);
5008 if (files == NULL)
5009 goto fpip_end;
5010 old_files = max_path_depth;
5011 depth = depth_displayed = -1;
5012
5013 lnum = start_lnum;
5014 if (end_lnum > curbuf->b_ml.ml_line_count)
5015 end_lnum = curbuf->b_ml.ml_line_count;
5016 if (lnum > end_lnum) /* do at least one line */
5017 lnum = end_lnum;
5018 line = ml_get(lnum);
5019
5020 for (;;)
5021 {
5022 if (incl_regmatch.regprog != NULL
5023 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5024 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005025 char_u *p_fname = (curr_fname == curbuf->b_fname)
5026 ? curbuf->b_ffname : curr_fname;
5027
5028 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5029 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5030 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005031 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005032 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5033 else
5034 /* Use text after match with 'include'. */
5035 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005036 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 already_searched = FALSE;
5038 if (new_fname != NULL)
5039 {
5040 /* Check whether we have already searched in this file */
5041 for (i = 0;; i++)
5042 {
5043 if (i == depth + 1)
5044 i = old_files;
5045 if (i == max_path_depth)
5046 break;
5047 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5048 {
5049 if (type != CHECK_PATH &&
5050 action == ACTION_SHOW_ALL && files[i].matched)
5051 {
5052 msg_putchar('\n'); /* cursor below last one */
5053 if (!got_int) /* don't display if 'q'
5054 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005055 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 msg_home_replace_hl(new_fname);
5058 MSG_PUTS(_(" (includes previously listed match)"));
5059 prev_fname = NULL;
5060 }
5061 }
5062 vim_free(new_fname);
5063 new_fname = NULL;
5064 already_searched = TRUE;
5065 break;
5066 }
5067 }
5068 }
5069
5070 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5071 || (new_fname == NULL && !already_searched)))
5072 {
5073 if (did_show)
5074 msg_putchar('\n'); /* cursor below last one */
5075 else
5076 {
5077 gotocmdline(TRUE); /* cursor at status line */
5078 MSG_PUTS_TITLE(_("--- Included files "));
5079 if (action != ACTION_SHOW_ALL)
5080 MSG_PUTS_TITLE(_("not found "));
5081 MSG_PUTS_TITLE(_("in path ---\n"));
5082 }
5083 did_show = TRUE;
5084 while (depth_displayed < depth && !got_int)
5085 {
5086 ++depth_displayed;
5087 for (i = 0; i < depth_displayed; i++)
5088 MSG_PUTS(" ");
5089 msg_home_replace(files[depth_displayed].name);
5090 MSG_PUTS(" -->\n");
5091 }
5092 if (!got_int) /* don't display if 'q' typed
5093 for "--more--" message */
5094 {
5095 for (i = 0; i <= depth_displayed; i++)
5096 MSG_PUTS(" ");
5097 if (new_fname != NULL)
5098 {
5099 /* using "new_fname" is more reliable, e.g., when
5100 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005101 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
5103 else
5104 {
5105 /*
5106 * Isolate the file name.
5107 * Include the surrounding "" or <> if present.
5108 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005109 if (inc_opt != NULL
5110 && strstr((char *)inc_opt, "\\zs") != NULL)
5111 {
5112 /* pattern contains \zs, use the match */
5113 p = incl_regmatch.startp[0];
5114 i = (int)(incl_regmatch.endp[0]
5115 - incl_regmatch.startp[0]);
5116 }
5117 else
5118 {
5119 /* find the file name after the end of the match */
5120 for (p = incl_regmatch.endp[0];
5121 *p && !vim_isfilec(*p); p++)
5122 ;
5123 for (i = 0; vim_isfilec(p[i]); i++)
5124 ;
5125 }
5126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (i == 0)
5128 {
5129 /* Nothing found, use the rest of the line. */
5130 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005131 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005133 /* Avoid checking before the start of the line, can
5134 * happen if \zs appears in the regexp. */
5135 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
5137 if (p[-1] == '"' || p[-1] == '<')
5138 {
5139 --p;
5140 ++i;
5141 }
5142 if (p[i] == '"' || p[i] == '>')
5143 ++i;
5144 }
5145 save_char = p[i];
5146 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005147 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 p[i] = save_char;
5149 }
5150
5151 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5152 {
5153 if (already_searched)
5154 MSG_PUTS(_(" (Already listed)"));
5155 else
5156 MSG_PUTS(_(" NOT FOUND"));
5157 }
5158 }
5159 out_flush(); /* output each line directly */
5160 }
5161
5162 if (new_fname != NULL)
5163 {
5164 /* Push the new file onto the file stack */
5165 if (depth + 1 == old_files)
5166 {
5167 bigger = (SearchedFile *)lalloc((long_u)(
5168 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5169 if (bigger != NULL)
5170 {
5171 for (i = 0; i <= depth; i++)
5172 bigger[i] = files[i];
5173 for (i = depth + 1; i < old_files + max_path_depth; i++)
5174 {
5175 bigger[i].fp = NULL;
5176 bigger[i].name = NULL;
5177 bigger[i].lnum = 0;
5178 bigger[i].matched = FALSE;
5179 }
5180 for (i = old_files; i < max_path_depth; i++)
5181 bigger[i + max_path_depth] = files[i];
5182 old_files += max_path_depth;
5183 max_path_depth *= 2;
5184 vim_free(files);
5185 files = bigger;
5186 }
5187 }
5188 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5189 == NULL)
5190 vim_free(new_fname);
5191 else
5192 {
5193 if (++depth == old_files)
5194 {
5195 /*
5196 * lalloc() for 'bigger' must have failed above. We
5197 * will forget one of our already visited files now.
5198 */
5199 vim_free(files[old_files].name);
5200 ++old_files;
5201 }
5202 files[depth].name = curr_fname = new_fname;
5203 files[depth].lnum = 0;
5204 files[depth].matched = FALSE;
5205#ifdef FEAT_INS_EXPAND
5206 if (action == ACTION_EXPAND)
5207 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005208 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005209 vim_snprintf((char*)IObuff, IOSIZE,
5210 _("Scanning included file: %s"),
5211 (char *)new_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005212 msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005214 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005216 if (p_verbose >= 5)
5217 {
5218 verbose_enter();
5219 smsg((char_u *)_("Searching included file %s"),
5220 (char *)new_fname);
5221 verbose_leave();
5222 }
5223
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225 }
5226 }
5227 else
5228 {
5229 /*
5230 * Check if the line is a define (type == FIND_DEFINE)
5231 */
5232 p = line;
5233search_line:
5234 define_matched = FALSE;
5235 if (def_regmatch.regprog != NULL
5236 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5237 {
5238 /*
5239 * Pattern must be first identifier after 'define', so skip
5240 * to that position before checking for match of pattern. Also
5241 * don't let it match beyond the end of this identifier.
5242 */
5243 p = def_regmatch.endp[0];
5244 while (*p && !vim_iswordc(*p))
5245 p++;
5246 define_matched = TRUE;
5247 }
5248
5249 /*
5250 * Look for a match. Don't do this if we are looking for a
5251 * define and this line didn't match define_prog above.
5252 */
5253 if (def_regmatch.regprog == NULL || define_matched)
5254 {
5255 if (define_matched
5256#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#endif
5259 )
5260 {
5261 /* compare the first "len" chars from "ptr" */
5262 startp = skipwhite(p);
5263 if (p_ic)
5264 matched = !MB_STRNICMP(startp, ptr, len);
5265 else
5266 matched = !STRNCMP(startp, ptr, len);
5267 if (matched && define_matched && whole
5268 && vim_iswordc(startp[len]))
5269 matched = FALSE;
5270 }
5271 else if (regmatch.regprog != NULL
5272 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5273 {
5274 matched = TRUE;
5275 startp = regmatch.startp[0];
5276 /*
5277 * Check if the line is not a comment line (unless we are
5278 * looking for a define). A line starting with "# define"
5279 * is not considered to be a comment line.
5280 */
5281 if (!define_matched && skip_comments)
5282 {
5283#ifdef FEAT_COMMENTS
5284 if ((*line != '#' ||
5285 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005286 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 matched = FALSE;
5288
5289 /*
5290 * Also check for a "/ *" or "/ /" before the match.
5291 * Skips lines like "int backwards; / * normal index
5292 * * /" when looking for "normal".
5293 * Note: Doesn't skip "/ *" in comments.
5294 */
5295 p = skipwhite(line);
5296 if (matched
5297 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5298#endif
5299 for (p = line; *p && p < startp; ++p)
5300 {
5301 if (matched
5302 && p[0] == '/'
5303 && (p[1] == '*' || p[1] == '/'))
5304 {
5305 matched = FALSE;
5306 /* After "//" all text is comment */
5307 if (p[1] == '/')
5308 break;
5309 ++p;
5310 }
5311 else if (!matched && p[0] == '*' && p[1] == '/')
5312 {
5313 /* Can find match after "* /". */
5314 matched = TRUE;
5315 ++p;
5316 }
5317 }
5318 }
5319 }
5320 }
5321 }
5322 if (matched)
5323 {
5324#ifdef FEAT_INS_EXPAND
5325 if (action == ACTION_EXPAND)
5326 {
5327 int reuse = 0;
5328 int add_r;
5329 char_u *aux;
5330
5331 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5332 break;
5333 found = TRUE;
5334 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (vim_iswordp(p))
5339 goto exit_matched;
5340 p = find_word_start(p);
5341 }
5342 p = find_word_end(p);
5343 i = (int)(p - aux);
5344
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005349
5350 /* Get the next line: when "depth" < 0 from the current
5351 * buffer, otherwise from the included file. Jump to
5352 * exit_matched when past the last line. */
5353 if (depth < 0)
5354 {
5355 if (lnum >= end_lnum)
5356 goto exit_matched;
5357 line = ml_get(++lnum);
5358 }
5359 else if (vim_fgets(line = file_line,
5360 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 goto exit_matched;
5362
5363 /* we read a line, set "already" to check this "line" later
5364 * if depth >= 0 we'll increase files[depth].lnum far
5365 * bellow -- Acevedo */
5366 already = aux = p = skipwhite(line);
5367 p = find_word_start(p);
5368 p = find_word_end(p);
5369 if (p > aux)
5370 {
5371 if (*aux != ')' && IObuff[i-1] != TAB)
5372 {
5373 if (IObuff[i-1] != ' ')
5374 IObuff[i++] = ' ';
5375 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5376 if (p_js
5377 && (IObuff[i-2] == '.'
5378 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5379 && (IObuff[i-2] == '?'
5380 || IObuff[i-2] == '!'))))
5381 IObuff[i++] = ' ';
5382 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005383 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 if (p - aux >= IOSIZE - i)
5385 p = aux + IOSIZE - i - 1;
5386 STRNCPY(IObuff + i, aux, p - aux);
5387 i += (int)(p - aux);
5388 reuse |= CONT_S_IPOS;
5389 }
5390 IObuff[i] = NUL;
5391 aux = IObuff;
5392
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005393 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 goto exit_matched;
5395 }
5396
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005397 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5399 dir, reuse);
5400 if (add_r == OK)
5401 /* if dir was BACKWARD then honor it just once */
5402 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005403 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 break;
5405 }
5406 else
5407#endif
5408 if (action == ACTION_SHOW_ALL)
5409 {
5410 found = TRUE;
5411 if (!did_show)
5412 gotocmdline(TRUE); /* cursor at status line */
5413 if (curr_fname != prev_fname)
5414 {
5415 if (did_show)
5416 msg_putchar('\n'); /* cursor below last one */
5417 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005418 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 msg_home_replace_hl(curr_fname);
5420 prev_fname = curr_fname;
5421 }
5422 did_show = TRUE;
5423 if (!got_int)
5424 show_pat_in_path(line, type, TRUE, action,
5425 (depth == -1) ? NULL : files[depth].fp,
5426 (depth == -1) ? &lnum : &files[depth].lnum,
5427 match_count++);
5428
5429 /* Set matched flag for this file and all the ones that
5430 * include it */
5431 for (i = 0; i <= depth; ++i)
5432 files[i].matched = TRUE;
5433 }
5434 else if (--count <= 0)
5435 {
5436 found = TRUE;
5437 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005438#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 && g_do_tagpreview == 0
5440#endif
5441 )
5442 EMSG(_("E387: Match is on current line"));
5443 else if (action == ACTION_SHOW)
5444 {
5445 show_pat_in_path(line, type, did_show, action,
5446 (depth == -1) ? NULL : files[depth].fp,
5447 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5448 did_show = TRUE;
5449 }
5450 else
5451 {
5452#ifdef FEAT_GUI
5453 need_mouse_correct = TRUE;
5454#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005455#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 /* ":psearch" uses the preview window */
5457 if (g_do_tagpreview != 0)
5458 {
5459 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005460 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462#endif
5463 if (action == ACTION_SPLIT)
5464 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005467 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 }
5469 if (depth == -1)
5470 {
5471 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005472#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if (g_do_tagpreview != 0)
5474 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005475 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005476 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005477 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 break; /* failed to jump to file */
5479 }
5480 else
5481#endif
5482 setpcmark();
5483 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005484 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486 else
5487 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005488 if (!GETFILE_SUCCESS(getfile(
5489 0, files[depth].name, NULL, TRUE,
5490 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 break; /* failed to jump to file */
5492 /* autocommands may have changed the lnum, we don't
5493 * want that here */
5494 curwin->w_cursor.lnum = files[depth].lnum;
5495 }
5496 }
5497 if (action != ACTION_SHOW)
5498 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005499 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 curwin->w_set_curswant = TRUE;
5501 }
5502
Bram Moolenaar4033c552017-09-16 20:54:51 +02005503#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005505 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 /* Return cursor to where we were */
5508 validate_cursor();
5509 redraw_later(VALID);
5510 win_enter(curwin_save, TRUE);
5511 }
5512#endif
5513 break;
5514 }
5515#ifdef FEAT_INS_EXPAND
5516exit_matched:
5517#endif
5518 matched = FALSE;
5519 /* look for other matches in the rest of the line if we
5520 * are not at the end of it already */
5521 if (def_regmatch.regprog == NULL
5522#ifdef FEAT_INS_EXPAND
5523 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005524 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005526 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005527 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 goto search_line;
5529 }
5530 line_breakcheck();
5531#ifdef FEAT_INS_EXPAND
5532 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005533 ins_compl_check_keys(30, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005534 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#else
5536 if (got_int)
5537#endif
5538 break;
5539
5540 /*
5541 * Read the next line. When reading an included file and encountering
5542 * end-of-file, close the file and continue in the file that included
5543 * it.
5544 */
5545 while (depth >= 0 && !already
5546 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5547 {
5548 fclose(files[depth].fp);
5549 --old_files;
5550 files[old_files].name = files[depth].name;
5551 files[old_files].matched = files[depth].matched;
5552 --depth;
5553 curr_fname = (depth == -1) ? curbuf->b_fname
5554 : files[depth].name;
5555 if (depth < depth_displayed)
5556 depth_displayed = depth;
5557 }
5558 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005559 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005561 /* Remove any CR and LF from the line. */
5562 i = (int)STRLEN(line);
5563 if (i > 0 && line[i - 1] == '\n')
5564 line[--i] = NUL;
5565 if (i > 0 && line[i - 1] == '\r')
5566 line[--i] = NUL;
5567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 else if (!already)
5569 {
5570 if (++lnum > end_lnum)
5571 break;
5572 line = ml_get(lnum);
5573 }
5574 already = NULL;
5575 }
5576 /* End of big for (;;) loop. */
5577
5578 /* Close any files that are still open. */
5579 for (i = 0; i <= depth; i++)
5580 {
5581 fclose(files[i].fp);
5582 vim_free(files[i].name);
5583 }
5584 for (i = old_files; i < max_path_depth; i++)
5585 vim_free(files[i].name);
5586 vim_free(files);
5587
5588 if (type == CHECK_PATH)
5589 {
5590 if (!did_show)
5591 {
5592 if (action != ACTION_SHOW_ALL)
5593 MSG(_("All included files were found"));
5594 else
5595 MSG(_("No included files"));
5596 }
5597 }
5598 else if (!found
5599#ifdef FEAT_INS_EXPAND
5600 && action != ACTION_EXPAND
5601#endif
5602 )
5603 {
5604#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005605 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606#else
5607 if (got_int)
5608#endif
5609 EMSG(_(e_interr));
5610 else if (type == FIND_DEFINE)
5611 EMSG(_("E388: Couldn't find definition"));
5612 else
5613 EMSG(_("E389: Couldn't find pattern"));
5614 }
5615 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5616 msg_end();
5617
5618fpip_end:
5619 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005620 vim_regfree(regmatch.regprog);
5621 vim_regfree(incl_regmatch.regprog);
5622 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623}
5624
5625 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005626show_pat_in_path(
5627 char_u *line,
5628 int type,
5629 int did_show,
5630 int action,
5631 FILE *fp,
5632 linenr_T *lnum,
5633 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 char_u *p;
5636
5637 if (did_show)
5638 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005639 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 gotocmdline(TRUE); /* cursor at status line */
5641 if (got_int) /* 'q' typed at "--more--" message */
5642 return;
5643 for (;;)
5644 {
5645 p = line + STRLEN(line) - 1;
5646 if (fp != NULL)
5647 {
5648 /* We used fgets(), so get rid of newline at end */
5649 if (p >= line && *p == '\n')
5650 --p;
5651 if (p >= line && *p == '\r')
5652 --p;
5653 *(p + 1) = NUL;
5654 }
5655 if (action == ACTION_SHOW_ALL)
5656 {
5657 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5658 msg_puts(IObuff);
5659 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5660 /* Highlight line numbers */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005661 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 MSG_PUTS(" ");
5663 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005664 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 out_flush(); /* show one line at a time */
5666
5667 /* Definition continues until line that doesn't end with '\' */
5668 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5669 break;
5670
5671 if (fp != NULL)
5672 {
5673 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5674 break;
5675 ++*lnum;
5676 }
5677 else
5678 {
5679 if (++*lnum > curbuf->b_ml.ml_line_count)
5680 break;
5681 line = ml_get(*lnum);
5682 }
5683 msg_putchar('\n');
5684 }
5685}
5686#endif
5687
5688#ifdef FEAT_VIMINFO
5689 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005690read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 char_u *lp;
5693 int idx = -1;
5694 int magic = FALSE;
5695 int no_scs = FALSE;
5696 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005697 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 long off = 0;
5699 int setlast = FALSE;
5700#ifdef FEAT_SEARCH_EXTRA
5701 static int hlsearch_on = FALSE;
5702#endif
5703 char_u *val;
5704
5705 /*
5706 * Old line types:
5707 * "/pat", "&pat": search/subst. pat
5708 * "~/pat", "~&pat": last used search/subst. pat
5709 * New line types:
5710 * "~h", "~H": hlsearch highlighting off/on
5711 * "~<magic><smartcase><line><end><off><last><which>pat"
5712 * <magic>: 'm' off, 'M' on
5713 * <smartcase>: 's' off, 'S' on
5714 * <line>: 'L' line offset, 'l' char offset
5715 * <end>: 'E' from end, 'e' from start
5716 * <off>: decimal, offset
5717 * <last>: '~' last used pattern
5718 * <which>: '/' search pat, '&' subst. pat
5719 */
5720 lp = virp->vir_line;
5721 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5722 {
5723 if (lp[1] == 'M') /* magic on */
5724 magic = TRUE;
5725 if (lp[2] == 's')
5726 no_scs = TRUE;
5727 if (lp[3] == 'L')
5728 off_line = TRUE;
5729 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005730 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 lp += 5;
5732 off = getdigits(&lp);
5733 }
5734 if (lp[0] == '~') /* use this pattern for last-used pattern */
5735 {
5736 setlast = TRUE;
5737 lp++;
5738 }
5739 if (lp[0] == '/')
5740 idx = RE_SEARCH;
5741 else if (lp[0] == '&')
5742 idx = RE_SUBST;
5743#ifdef FEAT_SEARCH_EXTRA
5744 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5745 hlsearch_on = FALSE;
5746 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5747 hlsearch_on = TRUE;
5748#endif
5749 if (idx >= 0)
5750 {
5751 if (force || spats[idx].pat == NULL)
5752 {
5753 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5754 TRUE);
5755 if (val != NULL)
5756 {
5757 set_last_search_pat(val, idx, magic, setlast);
5758 vim_free(val);
5759 spats[idx].no_scs = no_scs;
5760 spats[idx].off.line = off_line;
5761 spats[idx].off.end = off_end;
5762 spats[idx].off.off = off;
5763#ifdef FEAT_SEARCH_EXTRA
5764 if (setlast)
Bram Moolenaar8050efa2013-11-08 04:30:20 +01005765 {
5766 SET_NO_HLSEARCH(!hlsearch_on);
5767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768#endif
5769 }
5770 }
5771 }
5772 return viminfo_readline(virp);
5773}
5774
5775 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005776write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
5778 if (get_viminfo_parameter('/') != 0)
5779 {
5780#ifdef FEAT_SEARCH_EXTRA
5781 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5782 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5783#endif
5784 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005785 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787}
5788
5789 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005790wvsp_one(
5791 FILE *fp, /* file to write to */
5792 int idx, /* spats[] index */
5793 char *s, /* search pat */
5794 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
5796 if (spats[idx].pat != NULL)
5797 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005798 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 /* off.dir is not stored, it's reset to forward */
5800 fprintf(fp, "%c%c%c%c%ld%s%c",
5801 spats[idx].magic ? 'M' : 'm', /* magic */
5802 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5803 spats[idx].off.line ? 'L' : 'l', /* line offset */
5804 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5805 spats[idx].off.off, /* offset */
5806 last_idx == idx ? "~" : "", /* last used pat */
5807 sc);
5808 viminfo_writestring(fp, spats[idx].pat);
5809 }
5810}
5811#endif /* FEAT_VIMINFO */