blob: 42351d5e63cbed377cf50e0e6be519a5f06583d9 [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 Moolenaar071d4272004-06-13 20:20:40 +0000976 break;
977
978 /* Need to get the line pointer again, a
979 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000980 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982
983 /*
984 * If there is only a match after the cursor, skip
985 * this match.
986 */
987 if (!match_ok)
988 continue;
989 }
990
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000991 /* With the SEARCH_END option move to the last character
992 * of the match. Don't do it for an empty match, end
993 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200994 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000995 && !(matchpos.lnum == endpos.lnum
996 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000998 /* For a match in the first column, set the position
999 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001000 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001001 pos->col = endpos.col;
1002 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001003 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001004 if (pos->lnum > 1) /* just in case */
1005 {
1006 --pos->lnum;
1007 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1008 pos->lnum, FALSE));
1009 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001010 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001011 else
1012 {
1013 --pos->col;
1014#ifdef FEAT_MBYTE
1015 if (has_mbyte
1016 && pos->lnum <= buf->b_ml.ml_line_count)
1017 {
1018 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1019 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1020 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001021#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 else
1025 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001026 pos->lnum = lnum + matchpos.lnum;
1027 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029#ifdef FEAT_VIRTUALEDIT
1030 pos->coladd = 0;
1031#endif
1032 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001033 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
1035 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001036 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 search_match_endcol = endpos.col;
1038 break;
1039 }
1040 line_breakcheck(); /* stop if ctrl-C typed */
1041 if (got_int)
1042 break;
1043
1044#ifdef FEAT_SEARCH_EXTRA
1045 /* Cancel searching if a character was typed. Used for
1046 * 'incsearch'. Don't check too often, that would slowdown
1047 * searching too much. */
1048 if ((options & SEARCH_PEEK)
1049 && ((lnum - pos->lnum) & 0x3f) == 0
1050 && char_avail())
1051 {
1052 break_loop = TRUE;
1053 break;
1054 }
1055#endif
1056
1057 if (loop && lnum == start_pos.lnum)
1058 break; /* if second loop, stop where started */
1059 }
1060 at_first_line = FALSE;
1061
1062 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001063 * Stop the search if wrapscan isn't set, "stop_lnum" is
1064 * specified, after an interrupt, after a match and after looping
1065 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001067 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001068#ifdef FEAT_RELTIME
1069 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001070#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001071#ifdef FEAT_SEARCH_EXTRA
1072 || break_loop
1073#endif
1074 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 break;
1076
1077 /*
1078 * If 'wrapscan' is set we continue at the other end of the file.
1079 * If 'shortmess' does not contain 's', we give a message.
1080 * This message is also remembered in keep_msg for when the screen
1081 * is redrawn. The keep_msg is cleared whenever another message is
1082 * written.
1083 */
1084 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001088 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1089 give_warning((char_u *)_(dir == BACKWARD
1090 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001092 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001093#ifdef FEAT_RELTIME
1094 || (timed_out != NULL && *timed_out)
1095#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001096#ifdef FEAT_SEARCH_EXTRA
1097 || break_loop
1098#endif
1099 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 break;
1101 }
1102 while (--count > 0 && found); /* stop after count matches or no match */
1103
Bram Moolenaar473de612013-06-08 18:19:48 +02001104 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
Bram Moolenaar280f1262006-01-30 00:14:18 +00001106 called_emsg |= save_called_emsg;
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 if (!found) /* did not find it */
1109 {
1110 if (got_int)
1111 EMSG(_(e_interr));
1112 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1113 {
1114 if (p_ws)
1115 EMSG2(_(e_patnotf2), mr_pattern);
1116 else if (lnum == 0)
1117 EMSG2(_("E384: search hit TOP without match for: %s"),
1118 mr_pattern);
1119 else
1120 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
1121 mr_pattern);
1122 }
1123 return FAIL;
1124 }
1125
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001126 /* A pattern like "\n\zs" may go past the last line. */
1127 if (pos->lnum > buf->b_ml.ml_line_count)
1128 {
1129 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001130 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001131 if (pos->col > 0)
1132 --pos->col;
1133 }
1134
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 return submatch + 1;
1136}
1137
1138#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001139 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001140set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001141{
1142 spats[0].off.dir = cdir;
1143}
1144
1145 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001146set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001147{
1148 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1149}
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151/*
1152 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001153 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 */
1155 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001156first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
1158 int submatch;
1159
1160 for (submatch = 1; ; ++submatch)
1161 {
1162 if (rp->startpos[submatch].lnum >= 0)
1163 break;
1164 if (submatch == 9)
1165 {
1166 submatch = 0;
1167 break;
1168 }
1169 }
1170 return submatch;
1171}
1172#endif
1173
1174/*
1175 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001176 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 * If 'dirc' is 0: use previous dir.
1178 * If 'pat' is NULL or empty : use previous string.
1179 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1180 * If 'options & SEARCH_ECHO': echo the search command and handle options
1181 * If 'options & SEARCH_MSG' : may give error message
1182 * If 'options & SEARCH_OPT' : interpret optional flags
1183 * If 'options & SEARCH_HIS' : put search pattern in history
1184 * If 'options & SEARCH_NOOF': don't add offset to position
1185 * If 'options & SEARCH_MARK': set previous context mark
1186 * If 'options & SEARCH_KEEP': keep previous search pattern
1187 * If 'options & SEARCH_START': accept match at curpos itself
1188 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1189 *
1190 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1191 * makes the movement linewise without moving the match position.
1192 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001193 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 */
1195 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001196do_search(
1197 oparg_T *oap, /* can be NULL */
1198 int dirc, /* '/' or '?' */
1199 char_u *pat,
1200 long count,
1201 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001202 proftime_T *tm, /* timeout limit or NULL */
1203 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204{
1205 pos_T pos; /* position of the last match */
1206 char_u *searchstr;
1207 struct soffset old_off;
1208 int retval; /* Return value */
1209 char_u *p;
1210 long c;
1211 char_u *dircp;
1212 char_u *strcopy = NULL;
1213 char_u *ps;
1214
1215 /*
1216 * A line offset is not remembered, this is vi compatible.
1217 */
1218 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1219 {
1220 spats[0].off.line = FALSE;
1221 spats[0].off.off = 0;
1222 }
1223
1224 /*
1225 * Save the values for when (options & SEARCH_KEEP) is used.
1226 * (there is no "if ()" around this because gcc wants them initialized)
1227 */
1228 old_off = spats[0].off;
1229
1230 pos = curwin->w_cursor; /* start searching at the cursor position */
1231
1232 /*
1233 * Find out the direction of the search.
1234 */
1235 if (dirc == 0)
1236 dirc = spats[0].off.dir;
1237 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001240#if defined(FEAT_EVAL)
1241 set_vv_searchforward();
1242#endif
1243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (options & SEARCH_REV)
1245 {
1246#ifdef WIN32
1247 /* There is a bug in the Visual C++ 2.2 compiler which means that
1248 * dirc always ends up being '/' */
1249 dirc = (dirc == '/') ? '?' : '/';
1250#else
1251 if (dirc == '/')
1252 dirc = '?';
1253 else
1254 dirc = '/';
1255#endif
1256 }
1257
1258#ifdef FEAT_FOLDING
1259 /* If the cursor is in a closed fold, don't find another match in the same
1260 * fold. */
1261 if (dirc == '/')
1262 {
1263 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1264 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1265 }
1266 else
1267 {
1268 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1269 pos.col = 0;
1270 }
1271#endif
1272
1273#ifdef FEAT_SEARCH_EXTRA
1274 /*
1275 * Turn 'hlsearch' highlighting back on.
1276 */
1277 if (no_hlsearch && !(options & SEARCH_KEEP))
1278 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001279 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01001280 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282#endif
1283
1284 /*
1285 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1286 */
1287 for (;;)
1288 {
1289 searchstr = pat;
1290 dircp = NULL;
1291 /* use previous pattern */
1292 if (pat == NULL || *pat == NUL || *pat == dirc)
1293 {
1294 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1295 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001296 searchstr = spats[RE_SUBST].pat;
1297 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001298 {
1299 EMSG(_(e_noprevre));
1300 retval = 0;
1301 goto end_do_search;
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001304 else
1305 {
1306 /* make search_regcomp() use spats[RE_SEARCH].pat */
1307 searchstr = (char_u *)"";
1308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310
1311 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1312 {
1313 /*
1314 * Find end of regular expression.
1315 * If there is a matching '/' or '?', toss it.
1316 */
1317 ps = strcopy;
1318 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1319 if (strcopy != ps)
1320 {
1321 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001322 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 pat = strcopy;
1324 searchstr = strcopy;
1325 }
1326 if (*p == dirc)
1327 {
1328 dircp = p; /* remember where we put the NUL */
1329 *p++ = NUL;
1330 }
1331 spats[0].off.line = FALSE;
1332 spats[0].off.end = FALSE;
1333 spats[0].off.off = 0;
1334 /*
1335 * Check for a line offset or a character offset.
1336 * For get_address (echo off) we don't check for a character
1337 * offset, because it is meaningless and the 's' could be a
1338 * substitute command.
1339 */
1340 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1341 spats[0].off.line = TRUE;
1342 else if ((options & SEARCH_OPT) &&
1343 (*p == 'e' || *p == 's' || *p == 'b'))
1344 {
1345 if (*p == 'e') /* end */
1346 spats[0].off.end = SEARCH_END;
1347 ++p;
1348 }
1349 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1350 {
1351 /* 'nr' or '+nr' or '-nr' */
1352 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1353 spats[0].off.off = atol((char *)p);
1354 else if (*p == '-') /* single '-' */
1355 spats[0].off.off = -1;
1356 else /* single '+' */
1357 spats[0].off.off = 1;
1358 ++p;
1359 while (VIM_ISDIGIT(*p)) /* skip number */
1360 ++p;
1361 }
1362
1363 /* compute length of search command for get_address() */
1364 searchcmdlen += (int)(p - pat);
1365
1366 pat = p; /* put pat after search command */
1367 }
1368
1369 if ((options & SEARCH_ECHO) && messaging()
1370 && !cmd_silent && msg_silent == 0)
1371 {
1372 char_u *msgbuf;
1373 char_u *trunc;
1374
1375 if (*searchstr == NUL)
1376 p = spats[last_idx].pat;
1377 else
1378 p = searchstr;
1379 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1380 if (msgbuf != NULL)
1381 {
1382 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001383#ifdef FEAT_MBYTE
1384 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1385 {
1386 /* Use a space to draw the composing char on. */
1387 msgbuf[1] = ' ';
1388 STRCPY(msgbuf + 2, p);
1389 }
1390 else
1391#endif
1392 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1394 {
1395 p = msgbuf + STRLEN(msgbuf);
1396 *p++ = dirc;
1397 if (spats[0].off.end)
1398 *p++ = 'e';
1399 else if (!spats[0].off.line)
1400 *p++ = 's';
1401 if (spats[0].off.off > 0 || spats[0].off.line)
1402 *p++ = '+';
1403 if (spats[0].off.off != 0 || spats[0].off.line)
1404 sprintf((char *)p, "%ld", spats[0].off.off);
1405 else
1406 *p = NUL;
1407 }
1408
1409 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001410 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412#ifdef FEAT_RIGHTLEFT
1413 /* The search pattern could be shown on the right in rightleft
1414 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1415 * it would be blanked out again very soon. Show it on the
1416 * left, but do reverse the text. */
1417 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1418 {
1419 char_u *r;
1420
1421 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1422 if (r != NULL)
1423 {
1424 vim_free(trunc);
1425 trunc = r;
1426 }
1427 }
1428#endif
1429 if (trunc != NULL)
1430 {
1431 msg_outtrans(trunc);
1432 vim_free(trunc);
1433 }
1434 else
1435 msg_outtrans(msgbuf);
1436 msg_clr_eos();
1437 msg_check();
1438 vim_free(msgbuf);
1439
1440 gotocmdline(FALSE);
1441 out_flush();
1442 msg_nowait = TRUE; /* don't wait for this message */
1443 }
1444 }
1445
1446 /*
1447 * If there is a character offset, subtract it from the current
1448 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001449 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 * This is not done for a line offset, because then we would not be vi
1451 * compatible.
1452 */
Bram Moolenaared203462004-06-16 11:19:22 +00001453 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 {
1455 if (spats[0].off.off > 0)
1456 {
1457 for (c = spats[0].off.off; c; --c)
1458 if (decl(&pos) == -1)
1459 break;
1460 if (c) /* at start of buffer */
1461 {
1462 pos.lnum = 0; /* allow lnum == 0 here */
1463 pos.col = MAXCOL;
1464 }
1465 }
1466 else
1467 {
1468 for (c = spats[0].off.off; c; ++c)
1469 if (incl(&pos) == -1)
1470 break;
1471 if (c) /* at end of buffer */
1472 {
1473 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1474 pos.col = 0;
1475 }
1476 }
1477 }
1478
1479#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1480 if (p_altkeymap && curwin->w_p_rl)
1481 lrFswap(searchstr,0);
1482#endif
1483
1484 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1485 searchstr, count, spats[0].off.end + (options &
1486 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1487 + SEARCH_MSG + SEARCH_START
1488 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001489 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491 if (dircp != NULL)
1492 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1493 if (c == FAIL)
1494 {
1495 retval = 0;
1496 goto end_do_search;
1497 }
1498 if (spats[0].off.end && oap != NULL)
1499 oap->inclusive = TRUE; /* 'e' includes last character */
1500
1501 retval = 1; /* pattern found */
1502
1503 /*
1504 * Add character and/or line offset
1505 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001506 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 if (spats[0].off.line) /* Add the offset to the line number. */
1509 {
1510 c = pos.lnum + spats[0].off.off;
1511 if (c < 1)
1512 pos.lnum = 1;
1513 else if (c > curbuf->b_ml.ml_line_count)
1514 pos.lnum = curbuf->b_ml.ml_line_count;
1515 else
1516 pos.lnum = c;
1517 pos.col = 0;
1518
1519 retval = 2; /* pattern found, line offset added */
1520 }
Bram Moolenaared203462004-06-16 11:19:22 +00001521 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 {
1523 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001524 c = spats[0].off.off;
1525 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001527 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (incl(&pos) == -1)
1529 break;
1530 }
1531 /* to the left, check for start of file */
1532 else
1533 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001534 while (c++ < 0)
1535 if (decl(&pos) == -1)
1536 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 }
1539 }
1540
1541 /*
1542 * The search command can be followed by a ';' to do another search.
1543 * For example: "/pat/;/foo/+3;?bar"
1544 * This is like doing another search command, except:
1545 * - The remembered direction '/' or '?' is from the first search.
1546 * - When an error happens the cursor isn't moved at all.
1547 * Don't do this when called by get_address() (it handles ';' itself).
1548 */
1549 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1550 break;
1551
1552 dirc = *++pat;
1553 if (dirc != '?' && dirc != '/')
1554 {
1555 retval = 0;
1556 EMSG(_("E386: Expected '?' or '/' after ';'"));
1557 goto end_do_search;
1558 }
1559 ++pat;
1560 }
1561
1562 if (options & SEARCH_MARK)
1563 setpcmark();
1564 curwin->w_cursor = pos;
1565 curwin->w_set_curswant = TRUE;
1566
1567end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001568 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 spats[0].off = old_off;
1570 vim_free(strcopy);
1571
1572 return retval;
1573}
1574
1575#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1576/*
1577 * search_for_exact_line(buf, pos, dir, pat)
1578 *
1579 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001580 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001582 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 * Return OK for success, or FAIL if no line found.
1584 */
1585 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001586search_for_exact_line(
1587 buf_T *buf,
1588 pos_T *pos,
1589 int dir,
1590 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
1592 linenr_T start = 0;
1593 char_u *ptr;
1594 char_u *p;
1595
1596 if (buf->b_ml.ml_line_count == 0)
1597 return FAIL;
1598 for (;;)
1599 {
1600 pos->lnum += dir;
1601 if (pos->lnum < 1)
1602 {
1603 if (p_ws)
1604 {
1605 pos->lnum = buf->b_ml.ml_line_count;
1606 if (!shortmess(SHM_SEARCH))
1607 give_warning((char_u *)_(top_bot_msg), TRUE);
1608 }
1609 else
1610 {
1611 pos->lnum = 1;
1612 break;
1613 }
1614 }
1615 else if (pos->lnum > buf->b_ml.ml_line_count)
1616 {
1617 if (p_ws)
1618 {
1619 pos->lnum = 1;
1620 if (!shortmess(SHM_SEARCH))
1621 give_warning((char_u *)_(bot_top_msg), TRUE);
1622 }
1623 else
1624 {
1625 pos->lnum = 1;
1626 break;
1627 }
1628 }
1629 if (pos->lnum == start)
1630 break;
1631 if (start == 0)
1632 start = pos->lnum;
1633 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1634 p = skipwhite(ptr);
1635 pos->col = (colnr_T) (p - ptr);
1636
1637 /* when adding lines the matching line may be empty but it is not
1638 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001639 if ((compl_cont_status & CONT_ADDING)
1640 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 {
1642 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1643 return OK;
1644 }
1645 else if (*p != NUL) /* ignore empty lines */
1646 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001647 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1648 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 return OK;
1650 }
1651 }
1652 return FAIL;
1653}
1654#endif /* FEAT_INS_EXPAND */
1655
1656/*
1657 * Character Searches
1658 */
1659
1660/*
1661 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1662 * position of the character, otherwise move to just before the char.
1663 * Do this "cap->count1" times.
1664 * Return FAIL or OK.
1665 */
1666 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001667searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
1669 int c = cap->nchar; /* char to search for */
1670 int dir = cap->arg; /* TRUE for searching forward */
1671 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 int col;
1673 char_u *p;
1674 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001675 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 if (c != NUL) /* normal search: remember args for repeat */
1678 {
1679 if (!KeyStuffed) /* don't remember when redoing */
1680 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001681 *lastc = c;
1682 set_csearch_direction(dir);
1683 set_csearch_until(t_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001685 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (cap->ncharC1 != 0)
1687 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001688 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1689 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001691 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1692 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694#endif
1695 }
1696 }
1697 else /* repeat previous search */
1698 {
Bram Moolenaar454709b2017-03-12 16:37:14 +01001699 if (*lastc == NUL
1700#ifdef FEAT_MBYTE
1701 && lastc_bytelen == 1
1702#endif
1703 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 return FAIL;
1705 if (dir) /* repeat in opposite direction */
1706 dir = -lastcdir;
1707 else
1708 dir = lastcdir;
1709 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001710 c = *lastc;
1711 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001712
1713 /* Force a move of at least one char, so ";" and "," will move the
1714 * cursor, even if the cursor is right in front of char we are looking
1715 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001716 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001717 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 }
1719
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001720 if (dir == BACKWARD)
1721 cap->oap->inclusive = FALSE;
1722 else
1723 cap->oap->inclusive = TRUE;
1724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 p = ml_get_curline();
1726 col = curwin->w_cursor.col;
1727 len = (int)STRLEN(p);
1728
1729 while (count--)
1730 {
1731#ifdef FEAT_MBYTE
1732 if (has_mbyte)
1733 {
1734 for (;;)
1735 {
1736 if (dir > 0)
1737 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001738 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (col >= len)
1740 return FAIL;
1741 }
1742 else
1743 {
1744 if (col == 0)
1745 return FAIL;
1746 col -= (*mb_head_off)(p, p + col - 1) + 1;
1747 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001748 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001750 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 break;
1752 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001753 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001754 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001755 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001756 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 }
1758 }
1759 else
1760#endif
1761 {
1762 for (;;)
1763 {
1764 if ((col += dir) < 0 || col >= len)
1765 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001766 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001768 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 }
1770 }
1771 }
1772
1773 if (t_cmd)
1774 {
1775 /* backup to before the character (possibly double-byte) */
1776 col -= dir;
1777#ifdef FEAT_MBYTE
1778 if (has_mbyte)
1779 {
1780 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001781 /* Landed on the search char which is lastc_bytelen long */
1782 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 else
1784 /* To previous char, which may be multi-byte. */
1785 col -= (*mb_head_off)(p, p + col);
1786 }
1787#endif
1788 }
1789 curwin->w_cursor.col = col;
1790
1791 return OK;
1792}
1793
1794/*
1795 * "Other" Searches
1796 */
1797
1798/*
1799 * findmatch - find the matching paren or brace
1800 *
1801 * Improvement over vi: Braces inside quotes are ignored.
1802 */
1803 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001804findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805{
1806 return findmatchlimit(oap, initc, 0, 0);
1807}
1808
1809/*
1810 * Return TRUE if the character before "linep[col]" equals "ch".
1811 * Return FALSE if "col" is zero.
1812 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1813 * is NULL.
1814 * Handles multibyte string correctly.
1815 */
1816 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001817check_prevcol(
1818 char_u *linep,
1819 int col,
1820 int ch,
1821 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
1823 --col;
1824#ifdef FEAT_MBYTE
1825 if (col > 0 && has_mbyte)
1826 col -= (*mb_head_off)(linep, linep + col);
1827#endif
1828 if (prevcol)
1829 *prevcol = col;
1830 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1831}
1832
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001833static int find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001834
1835/*
1836 * Raw string start is found at linep[startpos.col - 1].
1837 * Return TRUE if the matching end can be found between startpos and endpos.
1838 */
1839 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001840find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001841{
1842 char_u *p;
1843 char_u *delim_copy;
1844 size_t delim_len;
1845 linenr_T lnum;
1846 int found = FALSE;
1847
1848 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1849 ;
1850 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001851 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001852 if (delim_copy == NULL)
1853 return FALSE;
1854 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1855 {
1856 char_u *line = ml_get(lnum);
1857
1858 for (p = line + (lnum == startpos->lnum
1859 ? startpos->col + 1 : 0); *p; ++p)
1860 {
1861 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1862 break;
1863 if (*p == ')' && p[delim_len + 1] == '"'
1864 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1865 {
1866 found = TRUE;
1867 break;
1868 }
1869 }
1870 if (found)
1871 break;
1872 }
1873 vim_free(delim_copy);
1874 return found;
1875}
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877/*
1878 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001879 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1880 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 *
1882 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001883 * character at or after the cursor. Special values:
1884 * '*' look for C-style comment / *
1885 * '/' look for C-style comment / *, ignoring comment-end
1886 * '#' look for preprocessor directives
1887 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 *
1889 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1890 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1891 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1892 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001893 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001894 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001895 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897
1898 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001899findmatchlimit(
1900 oparg_T *oap,
1901 int initc,
1902 int flags,
1903 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904{
1905 static pos_T pos; /* current search position */
1906 int findc = 0; /* matching brace */
1907 int c;
1908 int count = 0; /* cumulative number of braces */
1909 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001910 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 int inquote = FALSE; /* TRUE when inside quotes */
1912 char_u *linep; /* pointer to current line */
1913 char_u *ptr;
1914 int do_quotes; /* check for quotes in current line */
1915 int at_start; /* do_quotes value at start position */
1916 int hash_dir = 0; /* Direction searched for # things */
1917 int comment_dir = 0; /* Direction searched for comments */
1918 pos_T match_pos; /* Where last slash-star was found */
1919 int start_in_quotes; /* start position is in quotes */
1920 int traveled = 0; /* how far we've searched so far */
1921 int ignore_cend = FALSE; /* ignore comment end */
1922 int cpo_match; /* vi compatible matching */
1923 int cpo_bsl; /* don't recognize backslashes */
1924 int match_escaped = 0; /* search for escaped match */
1925 int dir; /* Direction to search */
1926 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001927#ifdef FEAT_LISP
1928 int lispcomm = FALSE; /* inside of Lisp-style comment */
1929 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001933#ifdef FEAT_VIRTUALEDIT
1934 pos.coladd = 0;
1935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 linep = ml_get(pos.lnum);
1937
1938 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1939 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1940
1941 /* Direction to search when initc is '/', '*' or '#' */
1942 if (flags & FM_BACKWARD)
1943 dir = BACKWARD;
1944 else if (flags & FM_FORWARD)
1945 dir = FORWARD;
1946 else
1947 dir = 0;
1948
1949 /*
1950 * if initc given, look in the table for the matching character
1951 * '/' and '*' are special cases: look for start or end of comment.
1952 * When '/' is used, we ignore running backwards into an star-slash, for
1953 * "[*" command, we just want to find any comment.
1954 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001955 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 comment_dir = dir;
1958 if (initc == '/')
1959 ignore_cend = TRUE;
1960 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001961 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 initc = NUL;
1963 }
1964 else if (initc != '#' && initc != NUL)
1965 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001966 find_mps_values(&initc, &findc, &backwards, TRUE);
1967 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 return NULL;
1969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 else
1971 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001972 /*
1973 * Either initc is '#', or no initc was given and we need to look
1974 * under the cursor.
1975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (initc == '#')
1977 {
1978 hash_dir = dir;
1979 }
1980 else
1981 {
1982 /*
1983 * initc was not given, must look for something to match under
1984 * or near the cursor.
1985 * Only check for special things when 'cpo' doesn't have '%'.
1986 */
1987 if (!cpo_match)
1988 {
1989 /* Are we before or at #if, #else etc.? */
1990 ptr = skipwhite(linep);
1991 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1992 {
1993 ptr = skipwhite(ptr + 1);
1994 if ( STRNCMP(ptr, "if", 2) == 0
1995 || STRNCMP(ptr, "endif", 5) == 0
1996 || STRNCMP(ptr, "el", 2) == 0)
1997 hash_dir = 1;
1998 }
1999
2000 /* Are we on a comment? */
2001 else if (linep[pos.col] == '/')
2002 {
2003 if (linep[pos.col + 1] == '*')
2004 {
2005 comment_dir = FORWARD;
2006 backwards = FALSE;
2007 pos.col++;
2008 }
2009 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2010 {
2011 comment_dir = BACKWARD;
2012 backwards = TRUE;
2013 pos.col--;
2014 }
2015 }
2016 else if (linep[pos.col] == '*')
2017 {
2018 if (linep[pos.col + 1] == '/')
2019 {
2020 comment_dir = BACKWARD;
2021 backwards = TRUE;
2022 }
2023 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2024 {
2025 comment_dir = FORWARD;
2026 backwards = FALSE;
2027 }
2028 }
2029 }
2030
2031 /*
2032 * If we are not on a comment or the # at the start of a line, then
2033 * look for brace anywhere on this line after the cursor.
2034 */
2035 if (!hash_dir && !comment_dir)
2036 {
2037 /*
2038 * Find the brace under or after the cursor.
2039 * If beyond the end of the line, use the last character in
2040 * the line.
2041 */
2042 if (linep[pos.col] == NUL && pos.col)
2043 --pos.col;
2044 for (;;)
2045 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002046 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 if (initc == NUL)
2048 break;
2049
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002050 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (findc)
2052 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002053 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 if (!findc)
2056 {
2057 /* no brace in the line, maybe use " #if" then */
2058 if (!cpo_match && *skipwhite(linep) == '#')
2059 hash_dir = 1;
2060 else
2061 return NULL;
2062 }
2063 else if (!cpo_bsl)
2064 {
2065 int col, bslcnt = 0;
2066
2067 /* Set "match_escaped" if there are an odd number of
2068 * backslashes. */
2069 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2070 bslcnt++;
2071 match_escaped = (bslcnt & 1);
2072 }
2073 }
2074 }
2075 if (hash_dir)
2076 {
2077 /*
2078 * Look for matching #if, #else, #elif, or #endif
2079 */
2080 if (oap != NULL)
2081 oap->motion_type = MLINE; /* Linewise for this case only */
2082 if (initc != '#')
2083 {
2084 ptr = skipwhite(skipwhite(linep) + 1);
2085 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2086 hash_dir = 1;
2087 else if (STRNCMP(ptr, "endif", 5) == 0)
2088 hash_dir = -1;
2089 else
2090 return NULL;
2091 }
2092 pos.col = 0;
2093 while (!got_int)
2094 {
2095 if (hash_dir > 0)
2096 {
2097 if (pos.lnum == curbuf->b_ml.ml_line_count)
2098 break;
2099 }
2100 else if (pos.lnum == 1)
2101 break;
2102 pos.lnum += hash_dir;
2103 linep = ml_get(pos.lnum);
2104 line_breakcheck(); /* check for CTRL-C typed */
2105 ptr = skipwhite(linep);
2106 if (*ptr != '#')
2107 continue;
2108 pos.col = (colnr_T) (ptr - linep);
2109 ptr = skipwhite(ptr + 1);
2110 if (hash_dir > 0)
2111 {
2112 if (STRNCMP(ptr, "if", 2) == 0)
2113 count++;
2114 else if (STRNCMP(ptr, "el", 2) == 0)
2115 {
2116 if (count == 0)
2117 return &pos;
2118 }
2119 else if (STRNCMP(ptr, "endif", 5) == 0)
2120 {
2121 if (count == 0)
2122 return &pos;
2123 count--;
2124 }
2125 }
2126 else
2127 {
2128 if (STRNCMP(ptr, "if", 2) == 0)
2129 {
2130 if (count == 0)
2131 return &pos;
2132 count--;
2133 }
2134 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2135 {
2136 if (count == 0)
2137 return &pos;
2138 }
2139 else if (STRNCMP(ptr, "endif", 5) == 0)
2140 count++;
2141 }
2142 }
2143 return NULL;
2144 }
2145 }
2146
2147#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002148 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 * paren/brace in the other direction. */
2150 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2151 backwards = !backwards;
2152#endif
2153
2154 do_quotes = -1;
2155 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002156 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002159 if ((backwards && comment_dir)
2160#ifdef FEAT_LISP
2161 || lisp
2162#endif
2163 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002165#ifdef FEAT_LISP
2166 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2167 lispcomm = TRUE; /* find match inside this comment */
2168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 while (!got_int)
2170 {
2171 /*
2172 * Go to the next position, forward or backward. We could use
2173 * inc() and dec() here, but that is much slower
2174 */
2175 if (backwards)
2176 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002177#ifdef FEAT_LISP
2178 /* char to match is inside of comment, don't search outside */
2179 if (lispcomm && pos.col < (colnr_T)comment_col)
2180 break;
2181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (pos.col == 0) /* at start of line, go to prev. one */
2183 {
2184 if (pos.lnum == 1) /* start of file */
2185 break;
2186 --pos.lnum;
2187
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002188 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 break;
2190
2191 linep = ml_get(pos.lnum);
2192 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2193 do_quotes = -1;
2194 line_breakcheck();
2195
2196 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002197 if (comment_dir
2198#ifdef FEAT_LISP
2199 || lisp
2200#endif
2201 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002203#ifdef FEAT_LISP
2204 /* skip comment */
2205 if (lisp && comment_col != MAXCOL)
2206 pos.col = comment_col;
2207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 else
2210 {
2211 --pos.col;
2212#ifdef FEAT_MBYTE
2213 if (has_mbyte)
2214 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2215#endif
2216 }
2217 }
2218 else /* forward search */
2219 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002220 if (linep[pos.col] == NUL
2221 /* at end of line, go to next one */
2222#ifdef FEAT_LISP
2223 /* don't search for match in comment */
2224 || (lisp && comment_col != MAXCOL
2225 && pos.col == (colnr_T)comment_col)
2226#endif
2227 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002229 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2230#ifdef FEAT_LISP
2231 /* line is exhausted and comment with it,
2232 * don't search for match in code */
2233 || lispcomm
2234#endif
2235 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 break;
2237 ++pos.lnum;
2238
2239 if (maxtravel && traveled++ > maxtravel)
2240 break;
2241
2242 linep = ml_get(pos.lnum);
2243 pos.col = 0;
2244 do_quotes = -1;
2245 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002246#ifdef FEAT_LISP
2247 if (lisp) /* find comment pos in new line */
2248 comment_col = check_linecomment(linep);
2249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251 else
2252 {
2253#ifdef FEAT_MBYTE
2254 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002255 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 else
2257#endif
2258 ++pos.col;
2259 }
2260 }
2261
2262 /*
2263 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2264 */
2265 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2266 (linep[0] == '{' || linep[0] == '}'))
2267 {
2268 if (linep[0] == findc && count == 0) /* match! */
2269 return &pos;
2270 break; /* out of scope */
2271 }
2272
2273 if (comment_dir)
2274 {
2275 /* Note: comments do not nest, and we ignore quotes in them */
2276 /* TODO: ignore comment brackets inside strings */
2277 if (comment_dir == FORWARD)
2278 {
2279 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2280 {
2281 pos.col++;
2282 return &pos;
2283 }
2284 }
2285 else /* Searching backwards */
2286 {
2287 /*
2288 * A comment may contain / * or / /, it may also start or end
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002289 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 */
2291 if (pos.col == 0)
2292 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002293 else if (raw_string)
2294 {
2295 if (linep[pos.col - 1] == 'R'
2296 && linep[pos.col] == '"'
2297 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2298 {
2299 /* Possible start of raw string. Now that we have the
2300 * delimiter we can check if it ends before where we
2301 * started searching, or before the previously found
2302 * raw string start. */
2303 if (!find_rawstring_end(linep, &pos,
2304 count > 0 ? &match_pos : &curwin->w_cursor))
2305 {
2306 count++;
2307 match_pos = pos;
2308 match_pos.col--;
2309 }
2310 linep = ml_get(pos.lnum); /* may have been released */
2311 }
2312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 else if ( linep[pos.col - 1] == '/'
2314 && linep[pos.col] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002315 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 && (int)pos.col < comment_col)
2317 {
2318 count++;
2319 match_pos = pos;
2320 match_pos.col--;
2321 }
2322 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2323 {
2324 if (count > 0)
2325 pos = match_pos;
2326 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2327 && (int)pos.col <= comment_col)
2328 pos.col -= 2;
2329 else if (ignore_cend)
2330 continue;
2331 else
2332 return NULL;
2333 return &pos;
2334 }
2335 }
2336 continue;
2337 }
2338
2339 /*
2340 * If smart matching ('cpoptions' does not contain '%'), braces inside
2341 * of quotes are ignored, but only if there is an even number of
2342 * quotes in the line.
2343 */
2344 if (cpo_match)
2345 do_quotes = 0;
2346 else if (do_quotes == -1)
2347 {
2348 /*
2349 * Count the number of quotes in the line, skipping \" and '"'.
2350 * Watch out for "\\".
2351 */
2352 at_start = do_quotes;
2353 for (ptr = linep; *ptr; ++ptr)
2354 {
2355 if (ptr == linep + pos.col + backwards)
2356 at_start = (do_quotes & 1);
2357 if (*ptr == '"'
2358 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2359 ++do_quotes;
2360 if (*ptr == '\\' && ptr[1] != NUL)
2361 ++ptr;
2362 }
2363 do_quotes &= 1; /* result is 1 with even number of quotes */
2364
2365 /*
2366 * If we find an uneven count, check current line and previous
2367 * one for a '\' at the end.
2368 */
2369 if (!do_quotes)
2370 {
2371 inquote = FALSE;
2372 if (ptr[-1] == '\\')
2373 {
2374 do_quotes = 1;
2375 if (start_in_quotes == MAYBE)
2376 {
2377 /* Do we need to use at_start here? */
2378 inquote = TRUE;
2379 start_in_quotes = TRUE;
2380 }
2381 else if (backwards)
2382 inquote = TRUE;
2383 }
2384 if (pos.lnum > 1)
2385 {
2386 ptr = ml_get(pos.lnum - 1);
2387 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2388 {
2389 do_quotes = 1;
2390 if (start_in_quotes == MAYBE)
2391 {
2392 inquote = at_start;
2393 if (inquote)
2394 start_in_quotes = TRUE;
2395 }
2396 else if (!backwards)
2397 inquote = TRUE;
2398 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002399
2400 /* ml_get() only keeps one line, need to get linep again */
2401 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 }
2404 }
2405 if (start_in_quotes == MAYBE)
2406 start_in_quotes = FALSE;
2407
2408 /*
2409 * If 'smartmatch' is set:
2410 * Things inside quotes are ignored by setting 'inquote'. If we
2411 * find a quote without a preceding '\' invert 'inquote'. At the
2412 * end of a line not ending in '\' we reset 'inquote'.
2413 *
2414 * In lines with an uneven number of quotes (without preceding '\')
2415 * we do not know which part to ignore. Therefore we only set
2416 * inquote if the number of quotes in a line is even, unless this
2417 * line or the previous one ends in a '\'. Complicated, isn't it?
2418 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002419 c = PTR2CHAR(linep + pos.col);
2420 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 case NUL:
2423 /* at end of line without trailing backslash, reset inquote */
2424 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2425 {
2426 inquote = FALSE;
2427 start_in_quotes = FALSE;
2428 }
2429 break;
2430
2431 case '"':
2432 /* a quote that is preceded with an odd number of backslashes is
2433 * ignored */
2434 if (do_quotes)
2435 {
2436 int col;
2437
2438 for (col = pos.col - 1; col >= 0; --col)
2439 if (linep[col] != '\\')
2440 break;
2441 if ((((int)pos.col - 1 - col) & 1) == 0)
2442 {
2443 inquote = !inquote;
2444 start_in_quotes = FALSE;
2445 }
2446 }
2447 break;
2448
2449 /*
2450 * If smart matching ('cpoptions' does not contain '%'):
2451 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2452 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2453 * skipped, there is never a brace in them.
2454 * Ignore this when finding matches for `'.
2455 */
2456 case '\'':
2457 if (!cpo_match && initc != '\'' && findc != '\'')
2458 {
2459 if (backwards)
2460 {
2461 if (pos.col > 1)
2462 {
2463 if (linep[pos.col - 2] == '\'')
2464 {
2465 pos.col -= 2;
2466 break;
2467 }
2468 else if (linep[pos.col - 2] == '\\' &&
2469 pos.col > 2 && linep[pos.col - 3] == '\'')
2470 {
2471 pos.col -= 3;
2472 break;
2473 }
2474 }
2475 }
2476 else if (linep[pos.col + 1]) /* forward search */
2477 {
2478 if (linep[pos.col + 1] == '\\' &&
2479 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2480 {
2481 pos.col += 3;
2482 break;
2483 }
2484 else if (linep[pos.col + 2] == '\'')
2485 {
2486 pos.col += 2;
2487 break;
2488 }
2489 }
2490 }
2491 /* FALLTHROUGH */
2492
2493 default:
2494#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002495 /*
2496 * For Lisp skip over backslashed (), {} and [].
2497 * (actually, we skip #\( et al)
2498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (curbuf->b_p_lisp
2500 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002501 && pos.col > 1
2502 && check_prevcol(linep, pos.col, '\\', NULL)
2503 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 break;
2505#endif
2506
2507 /* Check for match outside of quotes, and inside of
2508 * quotes when the start is also inside of quotes. */
2509 if ((!inquote || start_in_quotes == TRUE)
2510 && (c == initc || c == findc))
2511 {
2512 int col, bslcnt = 0;
2513
2514 if (!cpo_bsl)
2515 {
2516 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2517 bslcnt++;
2518 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002519 /* Only accept a match when 'M' is in 'cpo' or when escaping
2520 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2522 {
2523 if (c == initc)
2524 count++;
2525 else
2526 {
2527 if (count == 0)
2528 return &pos;
2529 count--;
2530 }
2531 }
2532 }
2533 }
2534 }
2535
2536 if (comment_dir == BACKWARD && count > 0)
2537 {
2538 pos = match_pos;
2539 return &pos;
2540 }
2541 return (pos_T *)NULL; /* never found it */
2542}
2543
2544/*
2545 * Check if line[] contains a / / comment.
2546 * Return MAXCOL if not, otherwise return the column.
2547 * TODO: skip strings.
2548 */
2549 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002550check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 char_u *p;
2553
2554 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002555#ifdef FEAT_LISP
2556 /* skip Lispish one-line comments */
2557 if (curbuf->b_p_lisp)
2558 {
2559 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2560 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002561 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002562
2563 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002564 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002565 {
2566 if (*p == '"')
2567 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002568 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002569 {
2570 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002571 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002572 }
2573 else if (p == line || ((p - line) >= 2
2574 /* skip #\" form */
2575 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002576 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002577 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002578 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002579 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2580 break; /* found! */
2581 ++p;
2582 }
2583 }
2584 else
2585 p = NULL;
2586 }
2587 else
2588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 while ((p = vim_strchr(p, '/')) != NULL)
2590 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002591 /* accept a double /, unless it's preceded with * and followed by *,
2592 * because * / / * is an end and start of a C comment */
2593 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 break;
2595 ++p;
2596 }
2597
2598 if (p == NULL)
2599 return MAXCOL;
2600 return (int)(p - line);
2601}
2602
2603/*
2604 * Move cursor briefly to character matching the one under the cursor.
2605 * Used for Insert mode and "r" command.
2606 * Show the match only if it is visible on the screen.
2607 * If there isn't a match, then beep.
2608 */
2609 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002610showmatch(
2611 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
2613 pos_T *lpos, save_cursor;
2614 pos_T mpos;
2615 colnr_T vcol;
2616 long save_so;
2617 long save_siso;
2618#ifdef CURSOR_SHAPE
2619 int save_state;
2620#endif
2621 colnr_T save_dollar_vcol;
2622 char_u *p;
2623
2624 /*
2625 * Only show match for chars in the 'matchpairs' option.
2626 */
2627 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002628 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002631 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002632 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002633#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002634 p += MB_PTR2LEN(p) + 1;
2635 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef FEAT_RIGHTLEFT
2637 && !(curwin->w_p_rl ^ p_ri)
2638#endif
2639 )
2640 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002641 p += MB_PTR2LEN(p);
2642 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 return;
2644 }
2645
2646 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002647 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002648 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
2650 if (!curwin->w_p_wrap)
2651 getvcol(curwin, lpos, NULL, &vcol, NULL);
2652 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002653 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
2655 mpos = *lpos; /* save the pos, update_screen() may change it */
2656 save_cursor = curwin->w_cursor;
2657 save_so = p_so;
2658 save_siso = p_siso;
2659 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2660 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002661 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2662 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 ++curwin->w_virtcol; /* do display ')' just before "$" */
2664 update_screen(VALID); /* show the new char first */
2665
2666 save_dollar_vcol = dollar_vcol;
2667#ifdef CURSOR_SHAPE
2668 save_state = State;
2669 State = SHOWMATCH;
2670 ui_cursor_shape(); /* may show different cursor shape */
2671#endif
2672 curwin->w_cursor = mpos; /* move to matching char */
2673 p_so = 0; /* don't use 'scrolloff' here */
2674 p_siso = 0; /* don't use 'sidescrolloff' here */
2675 showruler(FALSE);
2676 setcursor();
2677 cursor_on(); /* make sure that the cursor is shown */
Bram Moolenaara338adc2018-01-31 20:51:47 +01002678 out_flush_cursor(TRUE, FALSE);
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2681 * which resets it if the matching position is in a previous line
2682 * and has a higher column number. */
2683 dollar_vcol = save_dollar_vcol;
2684
2685 /*
2686 * brief pause, unless 'm' is present in 'cpo' and a character is
2687 * available.
2688 */
2689 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2690 ui_delay(p_mat * 100L, TRUE);
2691 else if (!char_avail())
2692 ui_delay(p_mat * 100L, FALSE);
2693 curwin->w_cursor = save_cursor; /* restore cursor position */
2694 p_so = save_so;
2695 p_siso = save_siso;
2696#ifdef CURSOR_SHAPE
2697 State = save_state;
2698 ui_cursor_shape(); /* may show different cursor shape */
2699#endif
2700 }
2701 }
2702}
2703
2704/*
2705 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002706 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 * space or a line break. Also stop at an empty line.
2708 * Return OK if the next sentence was found.
2709 */
2710 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002711findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 pos_T pos, tpos;
2714 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002715 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int startlnum;
2717 int noskip = FALSE; /* do not skip blanks */
2718 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002719 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 pos = curwin->w_cursor;
2722 if (dir == FORWARD)
2723 func = incl;
2724 else
2725 func = decl;
2726
2727 while (count--)
2728 {
2729 /*
2730 * if on an empty line, skip upto a non-empty line
2731 */
2732 if (gchar_pos(&pos) == NUL)
2733 {
2734 do
2735 if ((*func)(&pos) == -1)
2736 break;
2737 while (gchar_pos(&pos) == NUL);
2738 if (dir == FORWARD)
2739 goto found;
2740 }
2741 /*
2742 * if on the start of a paragraph or a section and searching forward,
2743 * go to the next line
2744 */
2745 else if (dir == FORWARD && pos.col == 0 &&
2746 startPS(pos.lnum, NUL, FALSE))
2747 {
2748 if (pos.lnum == curbuf->b_ml.ml_line_count)
2749 return FAIL;
2750 ++pos.lnum;
2751 goto found;
2752 }
2753 else if (dir == BACKWARD)
2754 decl(&pos);
2755
2756 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002757 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2759 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2760 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002761 if (vim_strchr((char_u *)".!?", c) != NULL)
2762 {
2763 /* Only skip over a '.', '!' and '?' once. */
2764 if (found_dot)
2765 break;
2766 found_dot = TRUE;
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (decl(&pos) == -1)
2769 break;
2770 /* when going forward: Stop in front of empty line */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002771 if (LINEEMPTY(pos.lnum) && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 incl(&pos);
2774 goto found;
2775 }
2776 }
2777
2778 /* remember the line where the search started */
2779 startlnum = pos.lnum;
2780 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2781
2782 for (;;) /* find end of sentence */
2783 {
2784 c = gchar_pos(&pos);
2785 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2786 {
2787 if (dir == BACKWARD && pos.lnum != startlnum)
2788 ++pos.lnum;
2789 break;
2790 }
2791 if (c == '.' || c == '!' || c == '?')
2792 {
2793 tpos = pos;
2794 do
2795 if ((c = inc(&tpos)) == -1)
2796 break;
2797 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2798 != NULL);
2799 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2800 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2801 && gchar_pos(&tpos) == ' ')))
2802 {
2803 pos = tpos;
2804 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2805 inc(&pos);
2806 break;
2807 }
2808 }
2809 if ((*func)(&pos) == -1)
2810 {
2811 if (count)
2812 return FAIL;
2813 noskip = TRUE;
2814 break;
2815 }
2816 }
2817found:
2818 /* skip white space */
2819 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2820 if (incl(&pos) == -1)
2821 break;
2822 }
2823
2824 setpcmark();
2825 curwin->w_cursor = pos;
2826 return OK;
2827}
2828
2829/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002830 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002832 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 * If 'what' is '{' or '}' we go to the next section.
2834 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002835 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 */
2837 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002838findpar(
2839 int *pincl, /* Return: TRUE if last char is to be included */
2840 int dir,
2841 long count,
2842 int what,
2843 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
2845 linenr_T curr;
2846 int did_skip; /* TRUE after separating lines have been skipped */
2847 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002848 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#ifdef FEAT_FOLDING
2850 linenr_T fold_first; /* first line of a closed fold */
2851 linenr_T fold_last; /* last line of a closed fold */
2852 int fold_skipped; /* TRUE if a closed fold was skipped this
2853 iteration */
2854#endif
2855
2856 curr = curwin->w_cursor.lnum;
2857
2858 while (count--)
2859 {
2860 did_skip = FALSE;
2861 for (first = TRUE; ; first = FALSE)
2862 {
2863 if (*ml_get(curr) != NUL)
2864 did_skip = TRUE;
2865
2866#ifdef FEAT_FOLDING
2867 /* skip folded lines */
2868 fold_skipped = FALSE;
2869 if (first && hasFolding(curr, &fold_first, &fold_last))
2870 {
2871 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2872 fold_skipped = TRUE;
2873 }
2874#endif
2875
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002876 /* POSIX has it's own ideas of what a paragraph boundary is and it
2877 * doesn't match historical Vi: It also stops at a "{" in the
2878 * first column and at an empty line. */
2879 if (!first && did_skip && (startPS(curr, what, both)
2880 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 break;
2882
2883#ifdef FEAT_FOLDING
2884 if (fold_skipped)
2885 curr -= dir;
2886#endif
2887 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2888 {
2889 if (count)
2890 return FALSE;
2891 curr -= dir;
2892 break;
2893 }
2894 }
2895 }
2896 setpcmark();
2897 if (both && *ml_get(curr) == '}') /* include line with '}' */
2898 ++curr;
2899 curwin->w_cursor.lnum = curr;
2900 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2901 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002902 char_u *line = ml_get(curr);
2903
2904 /* Put the cursor on the last character in the last line and make the
2905 * motion inclusive. */
2906 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002909#ifdef FEAT_MBYTE
2910 curwin->w_cursor.col -=
2911 (*mb_head_off)(line, line + curwin->w_cursor.col);
2912#endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002913 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 }
2916 else
2917 curwin->w_cursor.col = 0;
2918 return TRUE;
2919}
2920
2921/*
2922 * check if the string 's' is a nroff macro that is in option 'opt'
2923 */
2924 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002925inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
2927 char_u *macro;
2928
2929 for (macro = opt; macro[0]; ++macro)
2930 {
2931 /* Accept two characters in the option being equal to two characters
2932 * in the line. A space in the option matches with a space in the
2933 * line or the line having ended. */
2934 if ( (macro[0] == s[0]
2935 || (macro[0] == ' '
2936 && (s[0] == NUL || s[0] == ' ')))
2937 && (macro[1] == s[1]
2938 || ((macro[1] == NUL || macro[1] == ' ')
2939 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2940 break;
2941 ++macro;
2942 if (macro[0] == NUL)
2943 break;
2944 }
2945 return (macro[0] != NUL);
2946}
2947
2948/*
2949 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2950 * If 'para' is '{' or '}' only check for sections.
2951 * If 'both' is TRUE also stop at '}'
2952 */
2953 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002954startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 char_u *s;
2957
2958 s = ml_get(lnum);
2959 if (*s == para || *s == '\f' || (both && *s == '}'))
2960 return TRUE;
2961 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2962 (!para && inmacro(p_para, s + 1))))
2963 return TRUE;
2964 return FALSE;
2965}
2966
2967/*
2968 * The following routines do the word searches performed by the 'w', 'W',
2969 * 'b', 'B', 'e', and 'E' commands.
2970 */
2971
2972/*
2973 * To perform these searches, characters are placed into one of three
2974 * classes, and transitions between classes determine word boundaries.
2975 *
2976 * The classes are:
2977 *
2978 * 0 - white space
2979 * 1 - punctuation
2980 * 2 or higher - keyword characters (letters, digits and underscore)
2981 */
2982
2983static int cls_bigword; /* TRUE for "W", "B" or "E" */
2984
2985/*
2986 * cls() - returns the class of character at curwin->w_cursor
2987 *
2988 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2989 * from class 2 and higher are reported as class 1 since only white space
2990 * boundaries are of interest.
2991 */
2992 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002993cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 int c;
2996
2997 c = gchar_cursor();
2998#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2999 if (p_altkeymap && c == F_BLANK)
3000 return 0;
3001#endif
3002 if (c == ' ' || c == '\t' || c == NUL)
3003 return 0;
3004#ifdef FEAT_MBYTE
3005 if (enc_dbcs != 0 && c > 0xFF)
3006 {
3007 /* If cls_bigword, report multi-byte chars as class 1. */
3008 if (enc_dbcs == DBCS_KOR && cls_bigword)
3009 return 1;
3010
3011 /* process code leading/trailing bytes */
3012 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3013 }
3014 if (enc_utf8)
3015 {
3016 c = utf_class(c);
3017 if (c != 0 && cls_bigword)
3018 return 1;
3019 return c;
3020 }
3021#endif
3022
3023 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3024 if (cls_bigword)
3025 return 1;
3026
3027 if (vim_iswordc(c))
3028 return 2;
3029 return 1;
3030}
3031
3032
3033/*
3034 * fwd_word(count, type, eol) - move forward one word
3035 *
3036 * Returns FAIL if the cursor was already at the end of the file.
3037 * If eol is TRUE, last word stops at end of line (for operators).
3038 */
3039 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003040fwd_word(
3041 long count,
3042 int bigword, /* "W", "E" or "B" */
3043 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 int sclass; /* starting class */
3046 int i;
3047 int last_line;
3048
3049#ifdef FEAT_VIRTUALEDIT
3050 curwin->w_cursor.coladd = 0;
3051#endif
3052 cls_bigword = bigword;
3053 while (--count >= 0)
3054 {
3055#ifdef FEAT_FOLDING
3056 /* When inside a range of folded lines, move to the last char of the
3057 * last line. */
3058 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3059 coladvance((colnr_T)MAXCOL);
3060#endif
3061 sclass = cls();
3062
3063 /*
3064 * We always move at least one character, unless on the last
3065 * character in the buffer.
3066 */
3067 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3068 i = inc_cursor();
3069 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3070 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003071 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 return OK;
3073
3074 /*
3075 * Go one char past end of current word (if any)
3076 */
3077 if (sclass != 0)
3078 while (cls() == sclass)
3079 {
3080 i = inc_cursor();
3081 if (i == -1 || (i >= 1 && eol && count == 0))
3082 return OK;
3083 }
3084
3085 /*
3086 * go to next non-white
3087 */
3088 while (cls() == 0)
3089 {
3090 /*
3091 * We'll stop if we land on a blank line
3092 */
3093 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3094 break;
3095
3096 i = inc_cursor();
3097 if (i == -1 || (i >= 1 && eol && count == 0))
3098 return OK;
3099 }
3100 }
3101 return OK;
3102}
3103
3104/*
3105 * bck_word() - move backward 'count' words
3106 *
3107 * If stop is TRUE and we are already on the start of a word, move one less.
3108 *
3109 * Returns FAIL if top of the file was reached.
3110 */
3111 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003112bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int sclass; /* starting class */
3115
3116#ifdef FEAT_VIRTUALEDIT
3117 curwin->w_cursor.coladd = 0;
3118#endif
3119 cls_bigword = bigword;
3120 while (--count >= 0)
3121 {
3122#ifdef FEAT_FOLDING
3123 /* When inside a range of folded lines, move to the first char of the
3124 * first line. */
3125 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3126 curwin->w_cursor.col = 0;
3127#endif
3128 sclass = cls();
3129 if (dec_cursor() == -1) /* started at start of file */
3130 return FAIL;
3131
3132 if (!stop || sclass == cls() || sclass == 0)
3133 {
3134 /*
3135 * Skip white space before the word.
3136 * Stop on an empty line.
3137 */
3138 while (cls() == 0)
3139 {
3140 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003141 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 goto finished;
3143 if (dec_cursor() == -1) /* hit start of file, stop here */
3144 return OK;
3145 }
3146
3147 /*
3148 * Move backward to start of this word.
3149 */
3150 if (skip_chars(cls(), BACKWARD))
3151 return OK;
3152 }
3153
3154 inc_cursor(); /* overshot - forward one */
3155finished:
3156 stop = FALSE;
3157 }
3158 return OK;
3159}
3160
3161/*
3162 * end_word() - move to the end of the word
3163 *
3164 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3165 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3166 * motion crosses blank lines. When the real vi crosses a blank line in an
3167 * 'e' motion, the cursor is placed on the FIRST character of the next
3168 * non-blank line. The 'E' command, however, works correctly. Since this
3169 * appears to be a bug, I have not duplicated it here.
3170 *
3171 * Returns FAIL if end of the file was reached.
3172 *
3173 * If stop is TRUE and we are already on the end of a word, move one less.
3174 * If empty is TRUE stop on an empty line.
3175 */
3176 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003177end_word(
3178 long count,
3179 int bigword,
3180 int stop,
3181 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 int sclass; /* starting class */
3184
3185#ifdef FEAT_VIRTUALEDIT
3186 curwin->w_cursor.coladd = 0;
3187#endif
3188 cls_bigword = bigword;
3189 while (--count >= 0)
3190 {
3191#ifdef FEAT_FOLDING
3192 /* When inside a range of folded lines, move to the last char of the
3193 * last line. */
3194 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3195 coladvance((colnr_T)MAXCOL);
3196#endif
3197 sclass = cls();
3198 if (inc_cursor() == -1)
3199 return FAIL;
3200
3201 /*
3202 * If we're in the middle of a word, we just have to move to the end
3203 * of it.
3204 */
3205 if (cls() == sclass && sclass != 0)
3206 {
3207 /*
3208 * Move forward to end of the current word
3209 */
3210 if (skip_chars(sclass, FORWARD))
3211 return FAIL;
3212 }
3213 else if (!stop || sclass == 0)
3214 {
3215 /*
3216 * We were at the end of a word. Go to the end of the next word.
3217 * First skip white space, if 'empty' is TRUE, stop at empty line.
3218 */
3219 while (cls() == 0)
3220 {
3221 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003222 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 goto finished;
3224 if (inc_cursor() == -1) /* hit end of file, stop here */
3225 return FAIL;
3226 }
3227
3228 /*
3229 * Move forward to the end of this word.
3230 */
3231 if (skip_chars(cls(), FORWARD))
3232 return FAIL;
3233 }
3234 dec_cursor(); /* overshot - one char backward */
3235finished:
3236 stop = FALSE; /* we move only one word less */
3237 }
3238 return OK;
3239}
3240
3241/*
3242 * Move back to the end of the word.
3243 *
3244 * Returns FAIL if start of the file was reached.
3245 */
3246 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003247bckend_word(
3248 long count,
3249 int bigword, /* TRUE for "B" */
3250 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 int sclass; /* starting class */
3253 int i;
3254
3255#ifdef FEAT_VIRTUALEDIT
3256 curwin->w_cursor.coladd = 0;
3257#endif
3258 cls_bigword = bigword;
3259 while (--count >= 0)
3260 {
3261 sclass = cls();
3262 if ((i = dec_cursor()) == -1)
3263 return FAIL;
3264 if (eol && i == 1)
3265 return OK;
3266
3267 /*
3268 * Move backward to before the start of this word.
3269 */
3270 if (sclass != 0)
3271 {
3272 while (cls() == sclass)
3273 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3274 return OK;
3275 }
3276
3277 /*
3278 * Move backward to end of the previous word
3279 */
3280 while (cls() == 0)
3281 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003282 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 break;
3284 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3285 return OK;
3286 }
3287 }
3288 return OK;
3289}
3290
3291/*
3292 * Skip a row of characters of the same class.
3293 * Return TRUE when end-of-file reached, FALSE otherwise.
3294 */
3295 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003296skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 while (cls() == cclass)
3299 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3300 return TRUE;
3301 return FALSE;
3302}
3303
3304#ifdef FEAT_TEXTOBJ
3305/*
3306 * Go back to the start of the word or the start of white space
3307 */
3308 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003309back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 int sclass; /* starting class */
3312
3313 sclass = cls();
3314 for (;;)
3315 {
3316 if (curwin->w_cursor.col == 0) /* stop at start of line */
3317 break;
3318 dec_cursor();
3319 if (cls() != sclass) /* stop at start of word */
3320 {
3321 inc_cursor();
3322 break;
3323 }
3324 }
3325}
3326
3327 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003328find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 int c;
3331
3332 while (decl(posp) != -1)
3333 {
3334 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003335 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 incl(posp);
3338 break;
3339 }
3340 }
3341}
3342
3343/*
3344 * Skip count/2 sentences and count/2 separating white spaces.
3345 */
3346 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003347findsent_forward(
3348 long count,
3349 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 while (count--)
3352 {
3353 findsent(FORWARD, 1L);
3354 if (at_start_sent)
3355 find_first_blank(&curwin->w_cursor);
3356 if (count == 0 || at_start_sent)
3357 decl(&curwin->w_cursor);
3358 at_start_sent = !at_start_sent;
3359 }
3360}
3361
3362/*
3363 * Find word under cursor, cursor at end.
3364 * Used while an operator is pending, and in Visual mode.
3365 */
3366 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003367current_word(
3368 oparg_T *oap,
3369 long count,
3370 int include, /* TRUE: include word and white space */
3371 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 pos_T start_pos;
3374 pos_T pos;
3375 int inclusive = TRUE;
3376 int include_white = FALSE;
3377
3378 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003379 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003382 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 dec_cursor();
3384
3385 /*
3386 * When Visual mode is not active, or when the VIsual area is only one
3387 * character, select the word and/or white space under the cursor.
3388 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003389 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 /*
3392 * Go to start of current word or white space.
3393 */
3394 back_in_line();
3395 start_pos = curwin->w_cursor;
3396
3397 /*
3398 * If the start is on white space, and white space should be included
3399 * (" word"), or start is not on white space, and white space should
3400 * not be included ("word"), find end of word.
3401 */
3402 if ((cls() == 0) == include)
3403 {
3404 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3405 return FAIL;
3406 }
3407 else
3408 {
3409 /*
3410 * If the start is not on white space, and white space should be
3411 * included ("word "), or start is on white space and white
3412 * space should not be included (" "), find start of word.
3413 * If we end up in the first column of the next line (single char
3414 * word) back up to end of the line.
3415 */
3416 fwd_word(1L, bigword, TRUE);
3417 if (curwin->w_cursor.col == 0)
3418 decl(&curwin->w_cursor);
3419 else
3420 oneleft();
3421
3422 if (include)
3423 include_white = TRUE;
3424 }
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (VIsual_active)
3427 {
3428 /* should do something when inclusive == FALSE ! */
3429 VIsual = start_pos;
3430 redraw_curbuf_later(INVERTED); /* update the inversion */
3431 }
3432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
3434 oap->start = start_pos;
3435 oap->motion_type = MCHAR;
3436 }
3437 --count;
3438 }
3439
3440 /*
3441 * When count is still > 0, extend with more objects.
3442 */
3443 while (count > 0)
3444 {
3445 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003446 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 /*
3449 * In Visual mode, with cursor at start: move cursor back.
3450 */
3451 if (decl(&curwin->w_cursor) == -1)
3452 return FAIL;
3453 if (include != (cls() != 0))
3454 {
3455 if (bck_word(1L, bigword, TRUE) == FAIL)
3456 return FAIL;
3457 }
3458 else
3459 {
3460 if (bckend_word(1L, bigword, TRUE) == FAIL)
3461 return FAIL;
3462 (void)incl(&curwin->w_cursor);
3463 }
3464 }
3465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 /*
3468 * Move cursor forward one word and/or white area.
3469 */
3470 if (incl(&curwin->w_cursor) == -1)
3471 return FAIL;
3472 if (include != (cls() == 0))
3473 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003474 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 return FAIL;
3476 /*
3477 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003478 * the first character on the line.
3479 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003481 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 inclusive = FALSE;
3483 }
3484 else
3485 {
3486 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3487 return FAIL;
3488 }
3489 }
3490 --count;
3491 }
3492
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003493 if (include_white && (cls() != 0
3494 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 /*
3497 * If we don't include white space at the end, move the start
3498 * to include some white space there. This makes "daw" work
3499 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003500 * word). Also when "2daw" deletes "word." at the end of the line
3501 * (cursor is at start of next line).
3502 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 */
3504 pos = curwin->w_cursor; /* save cursor position */
3505 curwin->w_cursor = start_pos;
3506 if (oneleft() == OK)
3507 {
3508 back_in_line();
3509 if (cls() == 0 && curwin->w_cursor.col > 0)
3510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (VIsual_active)
3512 VIsual = curwin->w_cursor;
3513 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 oap->start = curwin->w_cursor;
3515 }
3516 }
3517 curwin->w_cursor = pos; /* put cursor back at end */
3518 }
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (VIsual_active)
3521 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003522 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 inc_cursor();
3524 if (VIsual_mode == 'V')
3525 {
3526 VIsual_mode = 'v';
3527 redraw_cmdline = TRUE; /* show mode later */
3528 }
3529 }
3530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 oap->inclusive = inclusive;
3532
3533 return OK;
3534}
3535
3536/*
3537 * Find sentence(s) under the cursor, cursor at end.
3538 * When Visual active, extend it by one or more sentences.
3539 */
3540 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003541current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542{
3543 pos_T start_pos;
3544 pos_T pos;
3545 int start_blank;
3546 int c;
3547 int at_start_sent;
3548 long ncount;
3549
3550 start_pos = curwin->w_cursor;
3551 pos = start_pos;
3552 findsent(FORWARD, 1L); /* Find start of next sentence. */
3553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003555 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003557 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003560 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 /*
3563 * Cursor at start of Visual area.
3564 * Find out where we are:
3565 * - in the white space before a sentence
3566 * - in a sentence or just after it
3567 * - at the start of a sentence
3568 */
3569 at_start_sent = TRUE;
3570 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003571 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003574 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576 at_start_sent = FALSE;
3577 break;
3578 }
3579 incl(&pos);
3580 }
3581 if (!at_start_sent)
3582 {
3583 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003584 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 at_start_sent = TRUE; /* exactly at start of sentence */
3586 else
3587 /* inside a sentence, go to its end (start of next) */
3588 findsent(FORWARD, 1L);
3589 }
3590 if (include) /* "as" gets twice as much as "is" */
3591 count *= 2;
3592 while (count--)
3593 {
3594 if (at_start_sent)
3595 find_first_blank(&curwin->w_cursor);
3596 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003597 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 findsent(BACKWARD, 1L);
3599 at_start_sent = !at_start_sent;
3600 }
3601 }
3602 else
3603 {
3604 /*
3605 * Cursor at end of Visual area.
3606 * Find out where we are:
3607 * - just before a sentence
3608 * - just before or in the white space before a sentence
3609 * - in a sentence
3610 */
3611 incl(&pos);
3612 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003613 /* not just before a sentence */
3614 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003617 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003620 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 at_start_sent = TRUE;
3623 break;
3624 }
3625 incl(&pos);
3626 }
3627 if (at_start_sent) /* in the sentence */
3628 findsent(BACKWARD, 1L);
3629 else /* in/before white before a sentence */
3630 curwin->w_cursor = start_pos;
3631 }
3632
3633 if (include) /* "as" gets twice as much as "is" */
3634 count *= 2;
3635 findsent_forward(count, at_start_sent);
3636 if (*p_sel == 'e')
3637 ++curwin->w_cursor.col;
3638 }
3639 return OK;
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003643 * If the cursor started on a blank, check if it is just before the start
3644 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003646 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003648 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
3650 start_blank = TRUE;
3651 find_first_blank(&start_pos); /* go back to first blank */
3652 }
3653 else
3654 {
3655 start_blank = FALSE;
3656 findsent(BACKWARD, 1L);
3657 start_pos = curwin->w_cursor;
3658 }
3659 if (include)
3660 ncount = count * 2;
3661 else
3662 {
3663 ncount = count;
3664 if (start_blank)
3665 --ncount;
3666 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003667 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 findsent_forward(ncount, TRUE);
3669 else
3670 decl(&curwin->w_cursor);
3671
3672 if (include)
3673 {
3674 /*
3675 * If the blank in front of the sentence is included, exclude the
3676 * blanks at the end of the sentence, go back to the first blank.
3677 * If there are no trailing blanks, try to include leading blanks.
3678 */
3679 if (start_blank)
3680 {
3681 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003682 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3683 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 decl(&curwin->w_cursor);
3685 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003686 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 find_first_blank(&start_pos);
3688 }
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (VIsual_active)
3691 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003692 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003693 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 goto extend;
3695 if (*p_sel == 'e')
3696 ++curwin->w_cursor.col;
3697 VIsual = start_pos;
3698 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003699 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 redraw_curbuf_later(INVERTED); /* update the inversion */
3701 }
3702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 /* include a newline after the sentence, if there is one */
3705 if (incl(&curwin->w_cursor) == -1)
3706 oap->inclusive = TRUE;
3707 else
3708 oap->inclusive = FALSE;
3709 oap->start = start_pos;
3710 oap->motion_type = MCHAR;
3711 }
3712 return OK;
3713}
3714
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003715/*
3716 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003717 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003720current_block(
3721 oparg_T *oap,
3722 long count,
3723 int include, /* TRUE == include white space */
3724 int what, /* '(', '{', etc. */
3725 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 pos_T old_pos;
3728 pos_T *pos = NULL;
3729 pos_T start_pos;
3730 pos_T *end_pos;
3731 pos_T old_start, old_end;
3732 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003733 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003736 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 old_start = old_end;
3738
3739 /*
3740 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3741 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003742 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
3744 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003745 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 while (inindent(1))
3747 if (inc_cursor() != 0)
3748 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003749 if (gchar_cursor() == what)
3750 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 ++curwin->w_cursor.col;
3752 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003753 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
3755 old_start = VIsual;
3756 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3757 }
3758 else
3759 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
3761 /*
3762 * Search backwards for unclosed '(', '{', etc..
3763 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003764 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3765 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
3767 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003768 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 while (count-- > 0)
3770 {
3771 if ((pos = findmatch(NULL, what)) == NULL)
3772 break;
3773 curwin->w_cursor = *pos;
3774 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3775 }
3776 p_cpo = save_cpo;
3777
3778 /*
3779 * Search for matching ')', '}', etc.
3780 * Put this position in curwin->w_cursor.
3781 */
3782 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3783 {
3784 curwin->w_cursor = old_pos;
3785 return FAIL;
3786 }
3787 curwin->w_cursor = *end_pos;
3788
3789 /*
3790 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003791 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3792 * indent. But only if the resulting area is not smaller than what we
3793 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 while (!include)
3796 {
3797 incl(&start_pos);
3798 sol = (curwin->w_cursor.col == 0);
3799 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003800 while (inindent(1))
3801 {
3802 sol = TRUE;
3803 if (decl(&curwin->w_cursor) != 0)
3804 break;
3805 }
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 /*
3808 * In Visual mode, when the resulting area is not bigger than what we
3809 * started with, extend it to the next block, and then exclude again.
3810 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003811 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 && VIsual_active)
3813 {
3814 curwin->w_cursor = old_start;
3815 decl(&curwin->w_cursor);
3816 if ((pos = findmatch(NULL, what)) == NULL)
3817 {
3818 curwin->w_cursor = old_pos;
3819 return FAIL;
3820 }
3821 start_pos = *pos;
3822 curwin->w_cursor = *pos;
3823 if ((end_pos = findmatch(NULL, other)) == NULL)
3824 {
3825 curwin->w_cursor = old_pos;
3826 return FAIL;
3827 }
3828 curwin->w_cursor = *end_pos;
3829 }
3830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 break;
3832 }
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (VIsual_active)
3835 {
3836 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003837 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003838 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 inc(&curwin->w_cursor); /* include the line break */
3840 VIsual = start_pos;
3841 VIsual_mode = 'v';
3842 redraw_curbuf_later(INVERTED); /* update the inversion */
3843 showmode();
3844 }
3845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 oap->start = start_pos;
3848 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003849 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003852 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003853 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003855 else
3856 /* End is before the start (no text in between <>, [], etc.): don't
3857 * operate on any text. */
3858 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
3860
3861 return OK;
3862}
3863
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003864static int in_html_tag(int);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003865
3866/*
3867 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3868 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3869 */
3870 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003871in_html_tag(
3872 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003873{
3874 char_u *line = ml_get_curline();
3875 char_u *p;
3876 int c;
3877 int lc = NUL;
3878 pos_T pos;
3879
3880#ifdef FEAT_MBYTE
3881 if (enc_dbcs)
3882 {
3883 char_u *lp = NULL;
3884
3885 /* We search forward until the cursor, because searching backwards is
3886 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003887 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003888 if (*p == '>' || *p == '<')
3889 {
3890 lc = *p;
3891 lp = p;
3892 }
3893 if (*p != '<') /* check for '<' under cursor */
3894 {
3895 if (lc != '<')
3896 return FALSE;
3897 p = lp;
3898 }
3899 }
3900 else
3901#endif
3902 {
3903 for (p = line + curwin->w_cursor.col; p > line; )
3904 {
3905 if (*p == '<') /* find '<' under/before cursor */
3906 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003907 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003908 if (*p == '>') /* find '>' before cursor */
3909 break;
3910 }
3911 if (*p != '<')
3912 return FALSE;
3913 }
3914
3915 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003916 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003917
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003918 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003919 if (end_tag)
3920 /* check that there is a '/' after the '<' */
3921 return *p == '/';
3922
3923 /* check that there is no '/' after the '<' */
3924 if (*p == '/')
3925 return FALSE;
3926
3927 /* check that the matching '>' is not preceded by '/' */
3928 for (;;)
3929 {
3930 if (inc(&pos) < 0)
3931 return FALSE;
3932 c = *ml_get_pos(&pos);
3933 if (c == '>')
3934 break;
3935 lc = c;
3936 }
3937 return lc != '/';
3938}
3939
3940/*
3941 * Find tag block under the cursor, cursor at end.
3942 */
3943 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003944current_tagblock(
3945 oparg_T *oap,
3946 long count_arg,
3947 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003948{
3949 long count = count_arg;
3950 long n;
3951 pos_T old_pos;
3952 pos_T start_pos;
3953 pos_T end_pos;
3954 pos_T old_start, old_end;
3955 char_u *spat, *epat;
3956 char_u *p;
3957 char_u *cp;
3958 int len;
3959 int r;
3960 int do_include = include;
3961 int save_p_ws = p_ws;
3962 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003963 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003964
3965 p_ws = FALSE;
3966
3967 old_pos = curwin->w_cursor;
3968 old_end = curwin->w_cursor; /* remember where we started */
3969 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003970 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003971 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003972
3973 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003974 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003975 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003976 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003977 {
3978 setpcmark();
3979
3980 /* ignore indent */
3981 while (inindent(1))
3982 if (inc_cursor() != 0)
3983 break;
3984
3985 if (in_html_tag(FALSE))
3986 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003987 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003988 while (*ml_get_cursor() != '>')
3989 if (inc_cursor() < 0)
3990 break;
3991 }
3992 else if (in_html_tag(TRUE))
3993 {
3994 /* cursor on end tag, move to just before it */
3995 while (*ml_get_cursor() != '<')
3996 if (dec_cursor() < 0)
3997 break;
3998 dec_cursor();
3999 old_end = curwin->w_cursor;
4000 }
4001 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004002 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004003 {
4004 old_start = VIsual;
4005 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4006 }
4007 else
4008 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004009
4010again:
4011 /*
4012 * Search backwards for unclosed "<aaa>".
4013 * Put this position in start_pos.
4014 */
4015 for (n = 0; n < count; ++n)
4016 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004017 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004018 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004019 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004020 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004021 {
4022 curwin->w_cursor = old_pos;
4023 goto theend;
4024 }
4025 }
4026 start_pos = curwin->w_cursor;
4027
4028 /*
4029 * Search for matching "</aaa>". First isolate the "aaa".
4030 */
4031 inc_cursor();
4032 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004033 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004034 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004035 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004036 if (len == 0)
4037 {
4038 curwin->w_cursor = old_pos;
4039 goto theend;
4040 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004041 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004042 epat = alloc(len + 9);
4043 if (spat == NULL || epat == NULL)
4044 {
4045 vim_free(spat);
4046 vim_free(epat);
4047 curwin->w_cursor = old_pos;
4048 goto theend;
4049 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004050 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004051 sprintf((char *)epat, "</%.*s>\\c", len, p);
4052
Bram Moolenaar48570482017-10-30 21:48:41 +01004053 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004054 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004055
4056 vim_free(spat);
4057 vim_free(epat);
4058
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004059 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004060 {
4061 /* Can't find other end or it's before the previous end. Could be a
4062 * HTML tag that doesn't have a matching end. Search backwards for
4063 * another starting tag. */
4064 count = 1;
4065 curwin->w_cursor = start_pos;
4066 goto again;
4067 }
4068
4069 if (do_include || r < 1)
4070 {
4071 /* Include up to the '>'. */
4072 while (*ml_get_cursor() != '>')
4073 if (inc_cursor() < 0)
4074 break;
4075 }
4076 else
4077 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004078 char_u *c = ml_get_cursor();
4079
4080 /* Exclude the '<' of the end tag.
4081 * If the closing tag is on new line, do not decrement cursor, but
4082 * make operation exclusive, so that the linefeed will be selected */
4083 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4084 /* do not decrement cursor */
4085 is_inclusive = FALSE;
4086 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004087 dec_cursor();
4088 }
4089 end_pos = curwin->w_cursor;
4090
4091 if (!do_include)
4092 {
4093 /* Exclude the start tag. */
4094 curwin->w_cursor = start_pos;
4095 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004096 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004097 {
4098 inc_cursor();
4099 start_pos = curwin->w_cursor;
4100 break;
4101 }
4102 curwin->w_cursor = end_pos;
4103
Bram Moolenaar45360022005-07-21 21:08:21 +00004104 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004105 * again. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004106 if (EQUAL_POS(start_pos, old_start) && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004107 {
4108 do_include = TRUE;
4109 curwin->w_cursor = old_start;
4110 count = count_arg;
4111 goto again;
4112 }
4113 }
4114
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004115 if (VIsual_active)
4116 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004117 /* If the end is before the start there is no text between tags, select
4118 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004119 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004120 curwin->w_cursor = start_pos;
4121 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004122 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004123 VIsual = start_pos;
4124 VIsual_mode = 'v';
4125 redraw_curbuf_later(INVERTED); /* update the inversion */
4126 showmode();
4127 }
4128 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004129 {
4130 oap->start = start_pos;
4131 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004132 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004133 {
4134 /* End is before the start: there is no text between tags; operate
4135 * on an empty area. */
4136 curwin->w_cursor = start_pos;
4137 oap->inclusive = FALSE;
4138 }
4139 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004140 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004141 }
4142 retval = OK;
4143
4144theend:
4145 p_ws = save_p_ws;
4146 return retval;
4147}
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004150current_par(
4151 oparg_T *oap,
4152 long count,
4153 int include, /* TRUE == include white space */
4154 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 linenr_T start_lnum;
4157 linenr_T end_lnum;
4158 int white_in_front;
4159 int dir;
4160 int start_is_white;
4161 int prev_start_is_white;
4162 int retval = OK;
4163 int do_white = FALSE;
4164 int t;
4165 int i;
4166
4167 if (type == 'S') /* not implemented yet */
4168 return FAIL;
4169
4170 start_lnum = curwin->w_cursor.lnum;
4171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /*
4173 * When visual area is more than one line: extend it.
4174 */
4175 if (VIsual_active && start_lnum != VIsual.lnum)
4176 {
4177extend:
4178 if (start_lnum < VIsual.lnum)
4179 dir = BACKWARD;
4180 else
4181 dir = FORWARD;
4182 for (i = count; --i >= 0; )
4183 {
4184 if (start_lnum ==
4185 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4186 {
4187 retval = FAIL;
4188 break;
4189 }
4190
4191 prev_start_is_white = -1;
4192 for (t = 0; t < 2; ++t)
4193 {
4194 start_lnum += dir;
4195 start_is_white = linewhite(start_lnum);
4196 if (prev_start_is_white == start_is_white)
4197 {
4198 start_lnum -= dir;
4199 break;
4200 }
4201 for (;;)
4202 {
4203 if (start_lnum == (dir == BACKWARD
4204 ? 1 : curbuf->b_ml.ml_line_count))
4205 break;
4206 if (start_is_white != linewhite(start_lnum + dir)
4207 || (!start_is_white
4208 && startPS(start_lnum + (dir > 0
4209 ? 1 : 0), 0, 0)))
4210 break;
4211 start_lnum += dir;
4212 }
4213 if (!include)
4214 break;
4215 if (start_lnum == (dir == BACKWARD
4216 ? 1 : curbuf->b_ml.ml_line_count))
4217 break;
4218 prev_start_is_white = start_is_white;
4219 }
4220 }
4221 curwin->w_cursor.lnum = start_lnum;
4222 curwin->w_cursor.col = 0;
4223 return retval;
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 /*
4227 * First move back to the start_lnum of the paragraph or white lines
4228 */
4229 white_in_front = linewhite(start_lnum);
4230 while (start_lnum > 1)
4231 {
4232 if (white_in_front) /* stop at first white line */
4233 {
4234 if (!linewhite(start_lnum - 1))
4235 break;
4236 }
4237 else /* stop at first non-white line of start of paragraph */
4238 {
4239 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4240 break;
4241 }
4242 --start_lnum;
4243 }
4244
4245 /*
4246 * Move past the end of any white lines.
4247 */
4248 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004249 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4250 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 --end_lnum;
4253 i = count;
4254 if (!include && white_in_front)
4255 --i;
4256 while (i--)
4257 {
4258 if (end_lnum == curbuf->b_ml.ml_line_count)
4259 return FAIL;
4260
4261 if (!include)
4262 do_white = linewhite(end_lnum + 1);
4263
4264 if (include || !do_white)
4265 {
4266 ++end_lnum;
4267 /*
4268 * skip to end of paragraph
4269 */
4270 while (end_lnum < curbuf->b_ml.ml_line_count
4271 && !linewhite(end_lnum + 1)
4272 && !startPS(end_lnum + 1, 0, 0))
4273 ++end_lnum;
4274 }
4275
4276 if (i == 0 && white_in_front && include)
4277 break;
4278
4279 /*
4280 * skip to end of white lines after paragraph
4281 */
4282 if (include || do_white)
4283 while (end_lnum < curbuf->b_ml.ml_line_count
4284 && linewhite(end_lnum + 1))
4285 ++end_lnum;
4286 }
4287
4288 /*
4289 * If there are no empty lines at the end, try to find some empty lines at
4290 * the start (unless that has been done already).
4291 */
4292 if (!white_in_front && !linewhite(end_lnum) && include)
4293 while (start_lnum > 1 && linewhite(start_lnum - 1))
4294 --start_lnum;
4295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (VIsual_active)
4297 {
4298 /* Problem: when doing "Vipipip" nothing happens in a single white
4299 * line, we get stuck there. Trap this here. */
4300 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4301 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004302 if (VIsual.lnum != start_lnum)
4303 {
4304 VIsual.lnum = start_lnum;
4305 VIsual.col = 0;
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 VIsual_mode = 'V';
4308 redraw_curbuf_later(INVERTED); /* update the inversion */
4309 showmode();
4310 }
4311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 oap->start.lnum = start_lnum;
4314 oap->start.col = 0;
4315 oap->motion_type = MLINE;
4316 }
4317 curwin->w_cursor.lnum = end_lnum;
4318 curwin->w_cursor.col = 0;
4319
4320 return OK;
4321}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004322
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004323static int find_next_quote(char_u *top_ptr, int col, int quotechar, char_u *escape);
4324static int find_prev_quote(char_u *line, int col_start, int quotechar, char_u *escape);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004325
4326/*
4327 * Search quote char from string line[col].
4328 * Quote character escaped by one of the characters in "escape" is not counted
4329 * as a quote.
4330 * Returns column number of "quotechar" or -1 when not found.
4331 */
4332 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004333find_next_quote(
4334 char_u *line,
4335 int col,
4336 int quotechar,
4337 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004338{
4339 int c;
4340
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004341 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004342 {
4343 c = line[col];
4344 if (c == NUL)
4345 return -1;
4346 else if (escape != NULL && vim_strchr(escape, c))
4347 ++col;
4348 else if (c == quotechar)
4349 break;
4350#ifdef FEAT_MBYTE
4351 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004352 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004355 ++col;
4356 }
4357 return col;
4358}
4359
4360/*
4361 * Search backwards in "line" from column "col_start" to find "quotechar".
4362 * Quote character escaped by one of the characters in "escape" is not counted
4363 * as a quote.
4364 * Return the found column or zero.
4365 */
4366 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004367find_prev_quote(
4368 char_u *line,
4369 int col_start,
4370 int quotechar,
4371 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004372{
4373 int n;
4374
4375 while (col_start > 0)
4376 {
4377 --col_start;
4378#ifdef FEAT_MBYTE
4379 col_start -= (*mb_head_off)(line, line + col_start);
4380#endif
4381 n = 0;
4382 if (escape != NULL)
4383 while (col_start - n > 0 && vim_strchr(escape,
4384 line[col_start - n - 1]) != NULL)
4385 ++n;
4386 if (n & 1)
4387 col_start -= n; /* uneven number of escape chars, skip it */
4388 else if (line[col_start] == quotechar)
4389 break;
4390 }
4391 return col_start;
4392}
4393
4394/*
4395 * Find quote under the cursor, cursor at end.
4396 * Returns TRUE if found, else FALSE.
4397 */
4398 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004399current_quote(
4400 oparg_T *oap,
4401 long count,
4402 int include, /* TRUE == include quote char */
4403 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004404{
4405 char_u *line = ml_get_curline();
4406 int col_end;
4407 int col_start = curwin->w_cursor.col;
4408 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004409 int vis_empty = TRUE; /* Visual selection <= 1 char */
4410 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004411 int inside_quotes = FALSE; /* Looks like "i'" done before */
4412 int selected_quote = FALSE; /* Has quote inside selection */
4413 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004414
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004415 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004416 if (VIsual_active)
4417 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004418 /* this only works within one line */
4419 if (VIsual.lnum != curwin->w_cursor.lnum)
4420 return FALSE;
4421
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004422 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004423 if (*p_sel == 'e')
4424 {
4425 if (!vis_bef_curs)
4426 {
4427 /* VIsual needs to be start of Visual selection. */
4428 pos_T t = curwin->w_cursor;
4429
4430 curwin->w_cursor = VIsual;
4431 VIsual = t;
4432 vis_bef_curs = TRUE;
4433 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004434 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004435 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004436 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004437 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004438
4439 if (!vis_empty)
4440 {
4441 /* Check if the existing selection exactly spans the text inside
4442 * quotes. */
4443 if (vis_bef_curs)
4444 {
4445 inside_quotes = VIsual.col > 0
4446 && line[VIsual.col - 1] == quotechar
4447 && line[curwin->w_cursor.col] != NUL
4448 && line[curwin->w_cursor.col + 1] == quotechar;
4449 i = VIsual.col;
4450 col_end = curwin->w_cursor.col;
4451 }
4452 else
4453 {
4454 inside_quotes = curwin->w_cursor.col > 0
4455 && line[curwin->w_cursor.col - 1] == quotechar
4456 && line[VIsual.col] != NUL
4457 && line[VIsual.col + 1] == quotechar;
4458 i = curwin->w_cursor.col;
4459 col_end = VIsual.col;
4460 }
4461
4462 /* Find out if we have a quote in the selection. */
4463 while (i <= col_end)
4464 if (line[i++] == quotechar)
4465 {
4466 selected_quote = TRUE;
4467 break;
4468 }
4469 }
4470
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004471 if (!vis_empty && line[col_start] == quotechar)
4472 {
4473 /* Already selecting something and on a quote character. Find the
4474 * next quoted string. */
4475 if (vis_bef_curs)
4476 {
4477 /* Assume we are on a closing quote: move to after the next
4478 * opening quote. */
4479 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4480 if (col_start < 0)
4481 return FALSE;
4482 col_end = find_next_quote(line, col_start + 1, quotechar,
4483 curbuf->b_p_qe);
4484 if (col_end < 0)
4485 {
4486 /* We were on a starting quote perhaps? */
4487 col_end = col_start;
4488 col_start = curwin->w_cursor.col;
4489 }
4490 }
4491 else
4492 {
4493 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4494 if (line[col_end] != quotechar)
4495 return FALSE;
4496 col_start = find_prev_quote(line, col_end, quotechar,
4497 curbuf->b_p_qe);
4498 if (line[col_start] != quotechar)
4499 {
4500 /* We were on an ending quote perhaps? */
4501 col_start = col_end;
4502 col_end = curwin->w_cursor.col;
4503 }
4504 }
4505 }
4506 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004507
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004508 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004509 {
4510 int first_col = col_start;
4511
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004512 if (!vis_empty)
4513 {
4514 if (vis_bef_curs)
4515 first_col = find_next_quote(line, col_start, quotechar, NULL);
4516 else
4517 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4518 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004519
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004520 /* The cursor is on a quote, we don't know if it's the opening or
4521 * closing quote. Search from the start of the line to find out.
4522 * Also do this when there is a Visual area, a' may leave the cursor
4523 * in between two strings. */
4524 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004525 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004526 {
4527 /* Find open quote character. */
4528 col_start = find_next_quote(line, col_start, quotechar, NULL);
4529 if (col_start < 0 || col_start > first_col)
4530 return FALSE;
4531 /* Find close quote character. */
4532 col_end = find_next_quote(line, col_start + 1, quotechar,
4533 curbuf->b_p_qe);
4534 if (col_end < 0)
4535 return FALSE;
4536 /* If is cursor between start and end quote character, it is
4537 * target text object. */
4538 if (col_start <= first_col && first_col <= col_end)
4539 break;
4540 col_start = col_end + 1;
4541 }
4542 }
4543 else
4544 {
4545 /* Search backward for a starting quote. */
4546 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4547 if (line[col_start] != quotechar)
4548 {
4549 /* No quote before the cursor, look after the cursor. */
4550 col_start = find_next_quote(line, col_start, quotechar, NULL);
4551 if (col_start < 0)
4552 return FALSE;
4553 }
4554
4555 /* Find close quote character. */
4556 col_end = find_next_quote(line, col_start + 1, quotechar,
4557 curbuf->b_p_qe);
4558 if (col_end < 0)
4559 return FALSE;
4560 }
4561
4562 /* When "include" is TRUE, include spaces after closing quote or before
4563 * the starting quote. */
4564 if (include)
4565 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004566 if (VIM_ISWHITE(line[col_end + 1]))
4567 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004568 ++col_end;
4569 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004570 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004571 --col_start;
4572 }
4573
Bram Moolenaarab194812005-09-14 21:40:12 +00004574 /* Set start position. After vi" another i" must include the ".
4575 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004576 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004577 ++col_start;
4578 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004579 if (VIsual_active)
4580 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004581 /* Set the start of the Visual area when the Visual area was empty, we
4582 * were just inside quotes or the Visual area didn't start at a quote
4583 * and didn't include a quote.
4584 */
4585 if (vis_empty
4586 || (vis_bef_curs
4587 && !selected_quote
4588 && (inside_quotes
4589 || (line[VIsual.col] != quotechar
4590 && (VIsual.col == 0
4591 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004592 {
4593 VIsual = curwin->w_cursor;
4594 redraw_curbuf_later(INVERTED);
4595 }
4596 }
4597 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004598 {
4599 oap->start = curwin->w_cursor;
4600 oap->motion_type = MCHAR;
4601 }
4602
4603 /* Set end position. */
4604 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004605 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004606 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004607 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004608 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004609 if (VIsual_active)
4610 {
4611 if (vis_empty || vis_bef_curs)
4612 {
4613 /* decrement cursor when 'selection' is not exclusive */
4614 if (*p_sel != 'e')
4615 dec_cursor();
4616 }
4617 else
4618 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004619 /* Cursor is at start of Visual area. Set the end of the Visual
4620 * area when it was just inside quotes or it didn't end at a
4621 * quote. */
4622 if (inside_quotes
4623 || (!selected_quote
4624 && line[VIsual.col] != quotechar
4625 && (line[VIsual.col] == NUL
4626 || line[VIsual.col + 1] != quotechar)))
4627 {
4628 dec_cursor();
4629 VIsual = curwin->w_cursor;
4630 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004631 curwin->w_cursor.col = col_start;
4632 }
4633 if (VIsual_mode == 'V')
4634 {
4635 VIsual_mode = 'v';
4636 redraw_cmdline = TRUE; /* show mode later */
4637 }
4638 }
4639 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004640 {
4641 /* Set inclusive and other oap's flags. */
4642 oap->inclusive = inclusive;
4643 }
4644
4645 return OK;
4646}
4647
4648#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004650static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004651
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004652/*
4653 * Find next search match under cursor, cursor at end.
4654 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004655 */
4656 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004657current_search(
4658 long count,
4659 int forward) /* move forward or backwards */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004660{
4661 pos_T start_pos; /* position before the pattern */
4662 pos_T orig_pos; /* position of the cursor at beginning */
4663 pos_T pos; /* position after the pattern */
4664 int i;
4665 int dir;
4666 int result; /* result of various function calls */
4667 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004668 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004669 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004670 int one_char;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004671 int direction = forward ? FORWARD : BACKWARD;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004672
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004673 /* wrapping should not occur */
4674 p_ws = FALSE;
4675
4676 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004677 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004678 dec_cursor();
4679
4680 if (VIsual_active)
4681 {
4682 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004683
4684 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004685
4686 /* make sure, searching further will extend the match */
4687 if (VIsual_active)
4688 {
4689 if (forward)
4690 incl(&pos);
4691 else
4692 decl(&pos);
4693 }
4694 }
4695 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004696 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004697
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004698 /* Is the pattern is zero-width?, this time, don't care about the direction
4699 */
4700 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4701 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004702 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004703 {
4704 p_ws = old_p_ws;
4705 return FAIL; /* pattern not found */
4706 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004707
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004708 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004709 * The trick is to first search backwards and then search forward again,
4710 * so that a match at the current cursor position will be correctly
4711 * captured.
4712 */
4713 for (i = 0; i < 2; i++)
4714 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004715 if (forward)
4716 dir = i;
4717 else
4718 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004719
4720 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004721 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004722 flags = SEARCH_END;
4723
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004724 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4725 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004726 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004727
4728 /* First search may fail, but then start searching from the
4729 * beginning of the file (cursor might be on the search match)
4730 * except when Visual mode is active, so that extending the visual
4731 * selection works. */
4732 if (!result && i) /* not found, abort */
4733 {
4734 curwin->w_cursor = orig_pos;
4735 if (VIsual_active)
4736 VIsual = save_VIsual;
4737 p_ws = old_p_ws;
4738 return FAIL;
4739 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004740 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004741 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004742 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004743 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004744 /* try again from start of buffer */
4745 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004746 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004747 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004748 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004749 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004750 /* searching backwards, so set pos to last line and col */
4751 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004752 pos.col = (colnr_T)STRLEN(
4753 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004754 }
4755 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004756 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004757 }
4758
4759 start_pos = pos;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004760 flags = forward ? SEARCH_END : SEARCH_START;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004761
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004762 /* Check again from the current cursor position,
4763 * since the next match might actually by only one char wide */
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004764 one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004765 if (one_char < 0)
4766 /* search failed, abort */
4767 return FAIL;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004768
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004769 /* move to match, except for zero-width matches, in which case, we are
4770 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004771 if (!one_char)
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004772 result = searchit(curwin, curbuf, &pos, direction,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004773 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
4774 NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004775
4776 if (!VIsual_active)
4777 VIsual = start_pos;
4778
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004779 curwin->w_cursor = pos;
4780 VIsual_active = TRUE;
4781 VIsual_mode = 'v';
4782
4783 if (VIsual_active)
4784 {
4785 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004786 if (*p_sel == 'e')
4787 {
4788 /* Correction for exclusive selection depends on the direction. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004789 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004790 inc_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004791 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004792 inc(&VIsual);
4793 }
4794
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004795 }
4796
4797#ifdef FEAT_FOLDING
4798 if (fdo_flags & FDO_SEARCH && KeyTyped)
4799 foldOpenCursor();
4800#endif
4801
4802 may_start_select('c');
4803#ifdef FEAT_MOUSE
4804 setmouse();
4805#endif
4806#ifdef FEAT_CLIPBOARD
4807 /* Make sure the clipboard gets updated. Needed because start and
4808 * end are still the same, and the selection needs to be owned */
4809 clip_star.vmode = NUL;
4810#endif
4811 redraw_curbuf_later(INVERTED);
4812 showmode();
4813
4814 return OK;
4815}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004816
4817/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004818 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004819 * If move is TRUE, check from the beginning of the buffer, else from position
4820 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004821 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004822 * Returns TRUE, FALSE or -1 for failure.
4823 */
4824 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004825is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004826{
4827 regmmatch_T regmatch;
4828 int nmatched = 0;
4829 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004830 pos_T pos;
4831 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004832 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004833
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004834 if (pattern == NULL)
4835 pattern = spats[last_idx].pat;
4836
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004837 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4838 SEARCH_KEEP, &regmatch) == FAIL)
4839 return -1;
4840
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004841 /* init startcol correctly */
4842 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004843 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004844 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004845 {
4846 CLEAR_POS(&pos);
4847 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004848 else
4849 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004850 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004851 /* accept a match at the cursor position */
4852 flag = SEARCH_START;
4853 }
4854
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004855 if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004856 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004857 {
4858 /* Zero-width pattern should match somewhere, then we can check if
4859 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004860 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004861 do
4862 {
4863 regmatch.startpos[0].col++;
4864 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004865 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004866 if (!nmatched)
4867 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004868 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4869 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004870
4871 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004872 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004873 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004874 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4875 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004876 /* one char width */
4877 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4878 result = TRUE;
4879 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004880 }
4881
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004882 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004883 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004884 return result;
4885}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4888 || defined(PROTO)
4889/*
4890 * return TRUE if line 'lnum' is empty or has white chars only.
4891 */
4892 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004893linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 char_u *p;
4896
4897 p = skipwhite(ml_get(lnum));
4898 return (*p == NUL);
4899}
4900#endif
4901
4902#if defined(FEAT_FIND_ID) || defined(PROTO)
4903/*
4904 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004905 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004908find_pattern_in_path(
4909 char_u *ptr, /* pointer to search pattern */
4910 int dir UNUSED, /* direction of expansion */
4911 int len, /* length of search pattern */
4912 int whole, /* match whole words only */
4913 int skip_comments, /* don't match inside comments */
4914 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004916 long count,
4917 int action, /* What to do when we find it */
4918 linenr_T start_lnum, /* first line to start searching */
4919 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 SearchedFile *files; /* Stack of included files */
4922 SearchedFile *bigger; /* When we need more space */
4923 int max_path_depth = 50;
4924 long match_count = 1;
4925
4926 char_u *pat;
4927 char_u *new_fname;
4928 char_u *curr_fname = curbuf->b_fname;
4929 char_u *prev_fname = NULL;
4930 linenr_T lnum;
4931 int depth;
4932 int depth_displayed; /* For type==CHECK_PATH */
4933 int old_files;
4934 int already_searched;
4935 char_u *file_line;
4936 char_u *line;
4937 char_u *p;
4938 char_u save_char;
4939 int define_matched;
4940 regmatch_T regmatch;
4941 regmatch_T incl_regmatch;
4942 regmatch_T def_regmatch;
4943 int matched = FALSE;
4944 int did_show = FALSE;
4945 int found = FALSE;
4946 int i;
4947 char_u *already = NULL;
4948 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004949 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004950#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 win_T *curwin_save = NULL;
4952#endif
4953
4954 regmatch.regprog = NULL;
4955 incl_regmatch.regprog = NULL;
4956 def_regmatch.regprog = NULL;
4957
4958 file_line = alloc(LSIZE);
4959 if (file_line == NULL)
4960 return;
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (type != CHECK_PATH && type != FIND_DEFINE
4963#ifdef FEAT_INS_EXPAND
4964 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4965 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004966 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967#endif
4968 )
4969 {
4970 pat = alloc(len + 5);
4971 if (pat == NULL)
4972 goto fpip_end;
4973 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4974 /* ignore case according to p_ic, p_scs and pat */
4975 regmatch.rm_ic = ignorecase(pat);
4976 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4977 vim_free(pat);
4978 if (regmatch.regprog == NULL)
4979 goto fpip_end;
4980 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004981 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4982 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004984 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (incl_regmatch.regprog == NULL)
4986 goto fpip_end;
4987 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4988 }
4989 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4990 {
4991 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4992 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4993 if (def_regmatch.regprog == NULL)
4994 goto fpip_end;
4995 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4996 }
4997 files = (SearchedFile *)lalloc_clear((long_u)
4998 (max_path_depth * sizeof(SearchedFile)), TRUE);
4999 if (files == NULL)
5000 goto fpip_end;
5001 old_files = max_path_depth;
5002 depth = depth_displayed = -1;
5003
5004 lnum = start_lnum;
5005 if (end_lnum > curbuf->b_ml.ml_line_count)
5006 end_lnum = curbuf->b_ml.ml_line_count;
5007 if (lnum > end_lnum) /* do at least one line */
5008 lnum = end_lnum;
5009 line = ml_get(lnum);
5010
5011 for (;;)
5012 {
5013 if (incl_regmatch.regprog != NULL
5014 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5015 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005016 char_u *p_fname = (curr_fname == curbuf->b_fname)
5017 ? curbuf->b_ffname : curr_fname;
5018
5019 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5020 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5021 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005022 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005023 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5024 else
5025 /* Use text after match with 'include'. */
5026 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005027 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 already_searched = FALSE;
5029 if (new_fname != NULL)
5030 {
5031 /* Check whether we have already searched in this file */
5032 for (i = 0;; i++)
5033 {
5034 if (i == depth + 1)
5035 i = old_files;
5036 if (i == max_path_depth)
5037 break;
5038 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5039 {
5040 if (type != CHECK_PATH &&
5041 action == ACTION_SHOW_ALL && files[i].matched)
5042 {
5043 msg_putchar('\n'); /* cursor below last one */
5044 if (!got_int) /* don't display if 'q'
5045 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005046 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 msg_home_replace_hl(new_fname);
5049 MSG_PUTS(_(" (includes previously listed match)"));
5050 prev_fname = NULL;
5051 }
5052 }
5053 vim_free(new_fname);
5054 new_fname = NULL;
5055 already_searched = TRUE;
5056 break;
5057 }
5058 }
5059 }
5060
5061 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5062 || (new_fname == NULL && !already_searched)))
5063 {
5064 if (did_show)
5065 msg_putchar('\n'); /* cursor below last one */
5066 else
5067 {
5068 gotocmdline(TRUE); /* cursor at status line */
5069 MSG_PUTS_TITLE(_("--- Included files "));
5070 if (action != ACTION_SHOW_ALL)
5071 MSG_PUTS_TITLE(_("not found "));
5072 MSG_PUTS_TITLE(_("in path ---\n"));
5073 }
5074 did_show = TRUE;
5075 while (depth_displayed < depth && !got_int)
5076 {
5077 ++depth_displayed;
5078 for (i = 0; i < depth_displayed; i++)
5079 MSG_PUTS(" ");
5080 msg_home_replace(files[depth_displayed].name);
5081 MSG_PUTS(" -->\n");
5082 }
5083 if (!got_int) /* don't display if 'q' typed
5084 for "--more--" message */
5085 {
5086 for (i = 0; i <= depth_displayed; i++)
5087 MSG_PUTS(" ");
5088 if (new_fname != NULL)
5089 {
5090 /* using "new_fname" is more reliable, e.g., when
5091 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005092 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 else
5095 {
5096 /*
5097 * Isolate the file name.
5098 * Include the surrounding "" or <> if present.
5099 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005100 if (inc_opt != NULL
5101 && strstr((char *)inc_opt, "\\zs") != NULL)
5102 {
5103 /* pattern contains \zs, use the match */
5104 p = incl_regmatch.startp[0];
5105 i = (int)(incl_regmatch.endp[0]
5106 - incl_regmatch.startp[0]);
5107 }
5108 else
5109 {
5110 /* find the file name after the end of the match */
5111 for (p = incl_regmatch.endp[0];
5112 *p && !vim_isfilec(*p); p++)
5113 ;
5114 for (i = 0; vim_isfilec(p[i]); i++)
5115 ;
5116 }
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (i == 0)
5119 {
5120 /* Nothing found, use the rest of the line. */
5121 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005122 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005124 /* Avoid checking before the start of the line, can
5125 * happen if \zs appears in the regexp. */
5126 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 if (p[-1] == '"' || p[-1] == '<')
5129 {
5130 --p;
5131 ++i;
5132 }
5133 if (p[i] == '"' || p[i] == '>')
5134 ++i;
5135 }
5136 save_char = p[i];
5137 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005138 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 p[i] = save_char;
5140 }
5141
5142 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5143 {
5144 if (already_searched)
5145 MSG_PUTS(_(" (Already listed)"));
5146 else
5147 MSG_PUTS(_(" NOT FOUND"));
5148 }
5149 }
5150 out_flush(); /* output each line directly */
5151 }
5152
5153 if (new_fname != NULL)
5154 {
5155 /* Push the new file onto the file stack */
5156 if (depth + 1 == old_files)
5157 {
5158 bigger = (SearchedFile *)lalloc((long_u)(
5159 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5160 if (bigger != NULL)
5161 {
5162 for (i = 0; i <= depth; i++)
5163 bigger[i] = files[i];
5164 for (i = depth + 1; i < old_files + max_path_depth; i++)
5165 {
5166 bigger[i].fp = NULL;
5167 bigger[i].name = NULL;
5168 bigger[i].lnum = 0;
5169 bigger[i].matched = FALSE;
5170 }
5171 for (i = old_files; i < max_path_depth; i++)
5172 bigger[i + max_path_depth] = files[i];
5173 old_files += max_path_depth;
5174 max_path_depth *= 2;
5175 vim_free(files);
5176 files = bigger;
5177 }
5178 }
5179 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5180 == NULL)
5181 vim_free(new_fname);
5182 else
5183 {
5184 if (++depth == old_files)
5185 {
5186 /*
5187 * lalloc() for 'bigger' must have failed above. We
5188 * will forget one of our already visited files now.
5189 */
5190 vim_free(files[old_files].name);
5191 ++old_files;
5192 }
5193 files[depth].name = curr_fname = new_fname;
5194 files[depth].lnum = 0;
5195 files[depth].matched = FALSE;
5196#ifdef FEAT_INS_EXPAND
5197 if (action == ACTION_EXPAND)
5198 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005199 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005200 vim_snprintf((char*)IObuff, IOSIZE,
5201 _("Scanning included file: %s"),
5202 (char *)new_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005203 msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005207 if (p_verbose >= 5)
5208 {
5209 verbose_enter();
5210 smsg((char_u *)_("Searching included file %s"),
5211 (char *)new_fname);
5212 verbose_leave();
5213 }
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 }
5217 }
5218 else
5219 {
5220 /*
5221 * Check if the line is a define (type == FIND_DEFINE)
5222 */
5223 p = line;
5224search_line:
5225 define_matched = FALSE;
5226 if (def_regmatch.regprog != NULL
5227 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5228 {
5229 /*
5230 * Pattern must be first identifier after 'define', so skip
5231 * to that position before checking for match of pattern. Also
5232 * don't let it match beyond the end of this identifier.
5233 */
5234 p = def_regmatch.endp[0];
5235 while (*p && !vim_iswordc(*p))
5236 p++;
5237 define_matched = TRUE;
5238 }
5239
5240 /*
5241 * Look for a match. Don't do this if we are looking for a
5242 * define and this line didn't match define_prog above.
5243 */
5244 if (def_regmatch.regprog == NULL || define_matched)
5245 {
5246 if (define_matched
5247#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005248 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#endif
5250 )
5251 {
5252 /* compare the first "len" chars from "ptr" */
5253 startp = skipwhite(p);
5254 if (p_ic)
5255 matched = !MB_STRNICMP(startp, ptr, len);
5256 else
5257 matched = !STRNCMP(startp, ptr, len);
5258 if (matched && define_matched && whole
5259 && vim_iswordc(startp[len]))
5260 matched = FALSE;
5261 }
5262 else if (regmatch.regprog != NULL
5263 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5264 {
5265 matched = TRUE;
5266 startp = regmatch.startp[0];
5267 /*
5268 * Check if the line is not a comment line (unless we are
5269 * looking for a define). A line starting with "# define"
5270 * is not considered to be a comment line.
5271 */
5272 if (!define_matched && skip_comments)
5273 {
5274#ifdef FEAT_COMMENTS
5275 if ((*line != '#' ||
5276 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005277 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 matched = FALSE;
5279
5280 /*
5281 * Also check for a "/ *" or "/ /" before the match.
5282 * Skips lines like "int backwards; / * normal index
5283 * * /" when looking for "normal".
5284 * Note: Doesn't skip "/ *" in comments.
5285 */
5286 p = skipwhite(line);
5287 if (matched
5288 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5289#endif
5290 for (p = line; *p && p < startp; ++p)
5291 {
5292 if (matched
5293 && p[0] == '/'
5294 && (p[1] == '*' || p[1] == '/'))
5295 {
5296 matched = FALSE;
5297 /* After "//" all text is comment */
5298 if (p[1] == '/')
5299 break;
5300 ++p;
5301 }
5302 else if (!matched && p[0] == '*' && p[1] == '/')
5303 {
5304 /* Can find match after "* /". */
5305 matched = TRUE;
5306 ++p;
5307 }
5308 }
5309 }
5310 }
5311 }
5312 }
5313 if (matched)
5314 {
5315#ifdef FEAT_INS_EXPAND
5316 if (action == ACTION_EXPAND)
5317 {
5318 int reuse = 0;
5319 int add_r;
5320 char_u *aux;
5321
5322 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5323 break;
5324 found = TRUE;
5325 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (vim_iswordp(p))
5330 goto exit_matched;
5331 p = find_word_start(p);
5332 }
5333 p = find_word_end(p);
5334 i = (int)(p - aux);
5335
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005340
5341 /* Get the next line: when "depth" < 0 from the current
5342 * buffer, otherwise from the included file. Jump to
5343 * exit_matched when past the last line. */
5344 if (depth < 0)
5345 {
5346 if (lnum >= end_lnum)
5347 goto exit_matched;
5348 line = ml_get(++lnum);
5349 }
5350 else if (vim_fgets(line = file_line,
5351 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 goto exit_matched;
5353
5354 /* we read a line, set "already" to check this "line" later
5355 * if depth >= 0 we'll increase files[depth].lnum far
5356 * bellow -- Acevedo */
5357 already = aux = p = skipwhite(line);
5358 p = find_word_start(p);
5359 p = find_word_end(p);
5360 if (p > aux)
5361 {
5362 if (*aux != ')' && IObuff[i-1] != TAB)
5363 {
5364 if (IObuff[i-1] != ' ')
5365 IObuff[i++] = ' ';
5366 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5367 if (p_js
5368 && (IObuff[i-2] == '.'
5369 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5370 && (IObuff[i-2] == '?'
5371 || IObuff[i-2] == '!'))))
5372 IObuff[i++] = ' ';
5373 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005374 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (p - aux >= IOSIZE - i)
5376 p = aux + IOSIZE - i - 1;
5377 STRNCPY(IObuff + i, aux, p - aux);
5378 i += (int)(p - aux);
5379 reuse |= CONT_S_IPOS;
5380 }
5381 IObuff[i] = NUL;
5382 aux = IObuff;
5383
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 goto exit_matched;
5386 }
5387
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005388 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5390 dir, reuse);
5391 if (add_r == OK)
5392 /* if dir was BACKWARD then honor it just once */
5393 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005394 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 break;
5396 }
5397 else
5398#endif
5399 if (action == ACTION_SHOW_ALL)
5400 {
5401 found = TRUE;
5402 if (!did_show)
5403 gotocmdline(TRUE); /* cursor at status line */
5404 if (curr_fname != prev_fname)
5405 {
5406 if (did_show)
5407 msg_putchar('\n'); /* cursor below last one */
5408 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005409 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 msg_home_replace_hl(curr_fname);
5411 prev_fname = curr_fname;
5412 }
5413 did_show = TRUE;
5414 if (!got_int)
5415 show_pat_in_path(line, type, TRUE, action,
5416 (depth == -1) ? NULL : files[depth].fp,
5417 (depth == -1) ? &lnum : &files[depth].lnum,
5418 match_count++);
5419
5420 /* Set matched flag for this file and all the ones that
5421 * include it */
5422 for (i = 0; i <= depth; ++i)
5423 files[i].matched = TRUE;
5424 }
5425 else if (--count <= 0)
5426 {
5427 found = TRUE;
5428 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005429#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 && g_do_tagpreview == 0
5431#endif
5432 )
5433 EMSG(_("E387: Match is on current line"));
5434 else if (action == ACTION_SHOW)
5435 {
5436 show_pat_in_path(line, type, did_show, action,
5437 (depth == -1) ? NULL : files[depth].fp,
5438 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5439 did_show = TRUE;
5440 }
5441 else
5442 {
5443#ifdef FEAT_GUI
5444 need_mouse_correct = TRUE;
5445#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005446#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 /* ":psearch" uses the preview window */
5448 if (g_do_tagpreview != 0)
5449 {
5450 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005451 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453#endif
5454 if (action == ACTION_SPLIT)
5455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005458 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460 if (depth == -1)
5461 {
5462 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005463#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 if (g_do_tagpreview != 0)
5465 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005466 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005467 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005468 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 break; /* failed to jump to file */
5470 }
5471 else
5472#endif
5473 setpcmark();
5474 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005475 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477 else
5478 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005479 if (!GETFILE_SUCCESS(getfile(
5480 0, files[depth].name, NULL, TRUE,
5481 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 break; /* failed to jump to file */
5483 /* autocommands may have changed the lnum, we don't
5484 * want that here */
5485 curwin->w_cursor.lnum = files[depth].lnum;
5486 }
5487 }
5488 if (action != ACTION_SHOW)
5489 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005490 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 curwin->w_set_curswant = TRUE;
5492 }
5493
Bram Moolenaar4033c552017-09-16 20:54:51 +02005494#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005496 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
5498 /* Return cursor to where we were */
5499 validate_cursor();
5500 redraw_later(VALID);
5501 win_enter(curwin_save, TRUE);
5502 }
5503#endif
5504 break;
5505 }
5506#ifdef FEAT_INS_EXPAND
5507exit_matched:
5508#endif
5509 matched = FALSE;
5510 /* look for other matches in the rest of the line if we
5511 * are not at the end of it already */
5512 if (def_regmatch.regprog == NULL
5513#ifdef FEAT_INS_EXPAND
5514 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005517 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005518 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 goto search_line;
5520 }
5521 line_breakcheck();
5522#ifdef FEAT_INS_EXPAND
5523 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005524 ins_compl_check_keys(30, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005525 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526#else
5527 if (got_int)
5528#endif
5529 break;
5530
5531 /*
5532 * Read the next line. When reading an included file and encountering
5533 * end-of-file, close the file and continue in the file that included
5534 * it.
5535 */
5536 while (depth >= 0 && !already
5537 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5538 {
5539 fclose(files[depth].fp);
5540 --old_files;
5541 files[old_files].name = files[depth].name;
5542 files[old_files].matched = files[depth].matched;
5543 --depth;
5544 curr_fname = (depth == -1) ? curbuf->b_fname
5545 : files[depth].name;
5546 if (depth < depth_displayed)
5547 depth_displayed = depth;
5548 }
5549 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005552 /* Remove any CR and LF from the line. */
5553 i = (int)STRLEN(line);
5554 if (i > 0 && line[i - 1] == '\n')
5555 line[--i] = NUL;
5556 if (i > 0 && line[i - 1] == '\r')
5557 line[--i] = NUL;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else if (!already)
5560 {
5561 if (++lnum > end_lnum)
5562 break;
5563 line = ml_get(lnum);
5564 }
5565 already = NULL;
5566 }
5567 /* End of big for (;;) loop. */
5568
5569 /* Close any files that are still open. */
5570 for (i = 0; i <= depth; i++)
5571 {
5572 fclose(files[i].fp);
5573 vim_free(files[i].name);
5574 }
5575 for (i = old_files; i < max_path_depth; i++)
5576 vim_free(files[i].name);
5577 vim_free(files);
5578
5579 if (type == CHECK_PATH)
5580 {
5581 if (!did_show)
5582 {
5583 if (action != ACTION_SHOW_ALL)
5584 MSG(_("All included files were found"));
5585 else
5586 MSG(_("No included files"));
5587 }
5588 }
5589 else if (!found
5590#ifdef FEAT_INS_EXPAND
5591 && action != ACTION_EXPAND
5592#endif
5593 )
5594 {
5595#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005596 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#else
5598 if (got_int)
5599#endif
5600 EMSG(_(e_interr));
5601 else if (type == FIND_DEFINE)
5602 EMSG(_("E388: Couldn't find definition"));
5603 else
5604 EMSG(_("E389: Couldn't find pattern"));
5605 }
5606 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5607 msg_end();
5608
5609fpip_end:
5610 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005611 vim_regfree(regmatch.regprog);
5612 vim_regfree(incl_regmatch.regprog);
5613 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614}
5615
5616 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005617show_pat_in_path(
5618 char_u *line,
5619 int type,
5620 int did_show,
5621 int action,
5622 FILE *fp,
5623 linenr_T *lnum,
5624 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 char_u *p;
5627
5628 if (did_show)
5629 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005630 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 gotocmdline(TRUE); /* cursor at status line */
5632 if (got_int) /* 'q' typed at "--more--" message */
5633 return;
5634 for (;;)
5635 {
5636 p = line + STRLEN(line) - 1;
5637 if (fp != NULL)
5638 {
5639 /* We used fgets(), so get rid of newline at end */
5640 if (p >= line && *p == '\n')
5641 --p;
5642 if (p >= line && *p == '\r')
5643 --p;
5644 *(p + 1) = NUL;
5645 }
5646 if (action == ACTION_SHOW_ALL)
5647 {
5648 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5649 msg_puts(IObuff);
5650 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5651 /* Highlight line numbers */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005652 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 MSG_PUTS(" ");
5654 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005655 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 out_flush(); /* show one line at a time */
5657
5658 /* Definition continues until line that doesn't end with '\' */
5659 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5660 break;
5661
5662 if (fp != NULL)
5663 {
5664 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5665 break;
5666 ++*lnum;
5667 }
5668 else
5669 {
5670 if (++*lnum > curbuf->b_ml.ml_line_count)
5671 break;
5672 line = ml_get(*lnum);
5673 }
5674 msg_putchar('\n');
5675 }
5676}
5677#endif
5678
5679#ifdef FEAT_VIMINFO
5680 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005681read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 char_u *lp;
5684 int idx = -1;
5685 int magic = FALSE;
5686 int no_scs = FALSE;
5687 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005688 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 long off = 0;
5690 int setlast = FALSE;
5691#ifdef FEAT_SEARCH_EXTRA
5692 static int hlsearch_on = FALSE;
5693#endif
5694 char_u *val;
5695
5696 /*
5697 * Old line types:
5698 * "/pat", "&pat": search/subst. pat
5699 * "~/pat", "~&pat": last used search/subst. pat
5700 * New line types:
5701 * "~h", "~H": hlsearch highlighting off/on
5702 * "~<magic><smartcase><line><end><off><last><which>pat"
5703 * <magic>: 'm' off, 'M' on
5704 * <smartcase>: 's' off, 'S' on
5705 * <line>: 'L' line offset, 'l' char offset
5706 * <end>: 'E' from end, 'e' from start
5707 * <off>: decimal, offset
5708 * <last>: '~' last used pattern
5709 * <which>: '/' search pat, '&' subst. pat
5710 */
5711 lp = virp->vir_line;
5712 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5713 {
5714 if (lp[1] == 'M') /* magic on */
5715 magic = TRUE;
5716 if (lp[2] == 's')
5717 no_scs = TRUE;
5718 if (lp[3] == 'L')
5719 off_line = TRUE;
5720 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005721 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 lp += 5;
5723 off = getdigits(&lp);
5724 }
5725 if (lp[0] == '~') /* use this pattern for last-used pattern */
5726 {
5727 setlast = TRUE;
5728 lp++;
5729 }
5730 if (lp[0] == '/')
5731 idx = RE_SEARCH;
5732 else if (lp[0] == '&')
5733 idx = RE_SUBST;
5734#ifdef FEAT_SEARCH_EXTRA
5735 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5736 hlsearch_on = FALSE;
5737 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5738 hlsearch_on = TRUE;
5739#endif
5740 if (idx >= 0)
5741 {
5742 if (force || spats[idx].pat == NULL)
5743 {
5744 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5745 TRUE);
5746 if (val != NULL)
5747 {
5748 set_last_search_pat(val, idx, magic, setlast);
5749 vim_free(val);
5750 spats[idx].no_scs = no_scs;
5751 spats[idx].off.line = off_line;
5752 spats[idx].off.end = off_end;
5753 spats[idx].off.off = off;
5754#ifdef FEAT_SEARCH_EXTRA
5755 if (setlast)
Bram Moolenaar8050efa2013-11-08 04:30:20 +01005756 {
5757 SET_NO_HLSEARCH(!hlsearch_on);
5758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759#endif
5760 }
5761 }
5762 }
5763 return viminfo_readline(virp);
5764}
5765
5766 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005767write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
5769 if (get_viminfo_parameter('/') != 0)
5770 {
5771#ifdef FEAT_SEARCH_EXTRA
5772 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5773 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5774#endif
5775 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005776 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778}
5779
5780 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005781wvsp_one(
5782 FILE *fp, /* file to write to */
5783 int idx, /* spats[] index */
5784 char *s, /* search pat */
5785 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 if (spats[idx].pat != NULL)
5788 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005789 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 /* off.dir is not stored, it's reset to forward */
5791 fprintf(fp, "%c%c%c%c%ld%s%c",
5792 spats[idx].magic ? 'M' : 'm', /* magic */
5793 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5794 spats[idx].off.line ? 'L' : 'l', /* line offset */
5795 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5796 spats[idx].off.off, /* offset */
5797 last_idx == idx ? "~" : "", /* last used pat */
5798 sc);
5799 viminfo_writestring(fp, spats[idx].pat);
5800 }
5801}
5802#endif /* FEAT_VIMINFO */