blob: e56cb1b41c31b066eb3472a5986fc5feb32da9c1 [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}
396#endif
397
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398/*
399 * Return TRUE when case should be ignored for search pattern "pat".
400 * Uses the 'ignorecase' and 'smartcase' options.
401 */
402 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100403ignorecase(char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200405 return ignorecase_opt(pat, p_ic, p_scs);
406}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
Bram Moolenaar66e29d72016-08-20 16:57:02 +0200408/*
409 * As ignorecase() put pass the "ic" and "scs" flags.
410 */
411 int
412ignorecase_opt(char_u *pat, int ic_in, int scs)
413{
414 int ic = ic_in;
415
416 if (ic && !no_smartcase && scs
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_INS_EXPAND
418 && !(ctrl_x_mode && curbuf->b_p_inf)
419#endif
420 )
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200421 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 no_smartcase = FALSE;
423
424 return ic;
425}
426
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200427/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200428 * Return TRUE if pattern "pat" has an uppercase character.
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200429 */
430 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100431pat_has_uppercase(char_u *pat)
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200432{
433 char_u *p = pat;
434
435 while (*p != NUL)
436 {
437#ifdef FEAT_MBYTE
438 int l;
439
440 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
441 {
442 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
443 return TRUE;
444 p += l;
445 }
446 else
447#endif
448 if (*p == '\\')
449 {
450 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
451 p += 3;
452 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
453 p += 3;
454 else if (p[1] != NUL) /* skip "\X" */
455 p += 2;
456 else
457 p += 1;
458 }
459 else if (MB_ISUPPER(*p))
460 return TRUE;
461 else
462 ++p;
463 }
464 return FALSE;
465}
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100468last_csearch(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200469{
470#ifdef FEAT_MBYTE
471 return lastc_bytes;
472#else
473 return lastc;
474#endif
475}
476
477 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100478last_csearch_forward(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200479{
480 return lastcdir == FORWARD;
481}
482
483 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100484last_csearch_until(void)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200485{
486 return last_t_cmd == TRUE;
487}
488
489 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100490set_last_csearch(int c, char_u *s UNUSED, int len UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200491{
492 *lastc = c;
493#ifdef FEAT_MBYTE
494 lastc_bytelen = len;
495 if (len)
496 memcpy(lastc_bytes, s, len);
497 else
498 vim_memset(lastc_bytes, 0, sizeof(lastc_bytes));
499#endif
500}
501
502 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100503set_csearch_direction(int cdir)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200504{
505 lastcdir = cdir;
506}
507
508 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100509set_csearch_until(int t_cmd)
Bram Moolenaardbd24b52015-08-11 14:26:19 +0200510{
511 last_t_cmd = t_cmd;
512}
513
514 char_u *
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100515last_search_pat(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516{
517 return spats[last_idx].pat;
518}
519
520/*
521 * Reset search direction to forward. For "gd" and "gD" commands.
522 */
523 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100524reset_search_dir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525{
526 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#if defined(FEAT_EVAL)
528 set_vv_searchforward();
529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530}
531
532#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
533/*
534 * Set the last search pattern. For ":let @/ =" and viminfo.
535 * Also set the saved search pattern, so that this works in an autocommand.
536 */
537 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100538set_last_search_pat(
539 char_u *s,
540 int idx,
541 int magic,
542 int setlast)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
544 vim_free(spats[idx].pat);
545 /* An empty string means that nothing should be matched. */
546 if (*s == NUL)
547 spats[idx].pat = NULL;
548 else
549 spats[idx].pat = vim_strsave(s);
550 spats[idx].magic = magic;
551 spats[idx].no_scs = FALSE;
552 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000553#if defined(FEAT_EVAL)
554 set_vv_searchforward();
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 spats[idx].off.line = FALSE;
557 spats[idx].off.end = FALSE;
558 spats[idx].off.off = 0;
559 if (setlast)
560 last_idx = idx;
561 if (save_level)
562 {
563 vim_free(saved_spats[idx].pat);
564 saved_spats[idx] = spats[0];
565 if (spats[idx].pat == NULL)
566 saved_spats[idx].pat = NULL;
567 else
568 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
569 saved_last_idx = last_idx;
570 }
571# ifdef FEAT_SEARCH_EXTRA
572 /* If 'hlsearch' set and search pat changed: need redraw. */
573 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000574 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575# endif
576}
577#endif
578
579#ifdef FEAT_SEARCH_EXTRA
580/*
581 * Get a regexp program for the last used search pattern.
582 * This is used for highlighting all matches in a window.
583 * Values returned in regmatch->regprog and regmatch->rmm_ic.
584 */
585 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100586last_pat_prog(regmmatch_T *regmatch)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 if (spats[last_idx].pat == NULL)
589 {
590 regmatch->regprog = NULL;
591 return;
592 }
593 ++emsg_off; /* So it doesn't beep if bad expr */
594 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
595 --emsg_off;
596}
597#endif
598
599/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100600 * Lowest level search function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
602 * Start at position 'pos' and return the found position in 'pos'.
603 *
604 * if (options & SEARCH_MSG) == 0 don't give any messages
605 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
606 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
607 * if (options & SEARCH_HIS) put search pattern in history
608 * if (options & SEARCH_END) return position at end of match
609 * if (options & SEARCH_START) accept match at pos itself
610 * if (options & SEARCH_KEEP) keep previous search pattern
611 * if (options & SEARCH_FOLD) match only once in a closed fold
612 * if (options & SEARCH_PEEK) check for typed char, cancel search
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100613 * if (options & SEARCH_COL) start at pos->col instead of zero
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 *
615 * Return FAIL (zero) for failure, non-zero for success.
616 * When FEAT_EVAL is defined, returns the index of the first matching
617 * subpattern plus one; one if there was none.
618 */
619 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100620searchit(
621 win_T *win, /* window to search in; can be NULL for a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 buffer without a window! */
Bram Moolenaar764b23c2016-01-30 21:10:09 +0100623 buf_T *buf,
624 pos_T *pos,
625 int dir,
626 char_u *pat,
627 long count,
628 int options,
629 int pat_use, /* which pattern to use when "pat" is empty */
630 linenr_T stop_lnum, /* stop after this line number when != 0 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200631 proftime_T *tm UNUSED, /* timeout limit or NULL */
632 int *timed_out UNUSED) /* set when timed out or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
634 int found;
635 linenr_T lnum; /* no init to shut up Apollo cc */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100636 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 regmmatch_T regmatch;
638 char_u *ptr;
639 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000641 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int loop;
643 pos_T start_pos;
644 int at_first_line;
645 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200646 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 int match_ok;
648 long nmatched;
649 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100650 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000651 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#ifdef FEAT_SEARCH_EXTRA
653 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654#endif
655
656 if (search_regcomp(pat, RE_SEARCH, pat_use,
657 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
658 {
659 if ((options & SEARCH_MSG) && !rc_did_emsg)
660 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
661 return FAIL;
662 }
663
Bram Moolenaar280f1262006-01-30 00:14:18 +0000664 /*
665 * find the string
666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 called_emsg = FALSE;
668 do /* loop for count */
669 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100670 /* When not accepting a match at the start position set "extra_col" to
671 * a non-zero value. Don't do that when starting at MAXCOL, since
672 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200673 if (pos->col == MAXCOL)
674 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100675#ifdef FEAT_MBYTE
676 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200677 else if (has_mbyte
678 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
679 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100680 {
681 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
682 if (*ptr == NUL)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200683 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100684 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200685 start_char_len = (*mb_ptr2len)(ptr);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100686 }
687#endif
688 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200689 start_char_len = 1;
690 if (dir == FORWARD)
691 {
692 if (options & SEARCH_START)
693 extra_col = 0;
694 else
695 extra_col = start_char_len;
696 }
697 else
698 {
699 if (options & SEARCH_START)
700 extra_col = start_char_len;
701 else
702 extra_col = 0;
703 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100704
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 start_pos = *pos; /* remember start pos for detecting no match */
706 found = 0; /* default: not found */
707 at_first_line = TRUE; /* default: start in first line */
708 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
709 {
710 pos->lnum = 1;
711 pos->col = 0;
712 at_first_line = FALSE; /* not in first line now */
713 }
714
715 /*
716 * Start searching in current line, unless searching backwards and
717 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000718 * If we are searching backwards, in column 0, and not including the
719 * current position, gain some efficiency by skipping back a line.
720 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000722 if (dir == BACKWARD && start_pos.col == 0
723 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
725 lnum = pos->lnum - 1;
726 at_first_line = FALSE;
727 }
728 else
729 lnum = pos->lnum;
730
731 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
732 {
733 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
734 lnum += dir, at_first_line = FALSE)
735 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000736 /* Stop after checking "stop_lnum", if it's set. */
737 if (stop_lnum != 0 && (dir == FORWARD
738 ? lnum > stop_lnum : lnum < stop_lnum))
739 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000740#ifdef FEAT_RELTIME
741 /* Stop after passing the "tm" time limit. */
742 if (tm != NULL && profile_passed_limit(tm))
743 break;
744#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000747 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100749 col = at_first_line && (options & SEARCH_COL) ? pos->col
750 : (colnr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaarad4d8a12015-12-28 19:20:36 +0100752 lnum, col,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000753#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200754 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000755#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200756 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000757#endif
758 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 /* Abort searching on an error (e.g., out of stack). */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200760 if (called_emsg
761#ifdef FEAT_RELTIME
762 || (timed_out != NULL && *timed_out)
763#endif
764 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 break;
766 if (nmatched > 0)
767 {
768 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000769 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000771#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000773#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000774 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000775 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
776 ptr = (char_u *)"";
777 else
778 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 /*
781 * Forward search in the first line: match should be after
782 * the start position. If not, continue at the end of the
783 * match (this is vi compatible) or on the next char.
784 */
785 if (dir == FORWARD && at_first_line)
786 {
787 match_ok = TRUE;
788 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000789 * When the match starts in a next line it's certainly
790 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 * When match lands on a NUL the cursor will be put
792 * one back afterwards, compare with that position,
793 * otherwise "/$" will get stuck on end of line.
794 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000795 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100796 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000797 ? (nmatched == 1
798 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000800 : ((int)matchpos.col
801 - (ptr[matchpos.col] == NUL)
802 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 {
804 /*
805 * If vi-compatible searching, continue at the end
806 * of the match, otherwise continue one position
807 * forward.
808 */
809 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
810 {
811 if (nmatched > 1)
812 {
813 /* end is in next line, thus no match in
814 * this line */
815 match_ok = FALSE;
816 break;
817 }
818 matchcol = endpos.col;
819 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000820 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 && ptr[matchcol] != NUL)
822 {
823#ifdef FEAT_MBYTE
824 if (has_mbyte)
825 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000826 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 else
828#endif
829 ++matchcol;
830 }
831 }
832 else
833 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000834 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (ptr[matchcol] != NUL)
836 {
837#ifdef FEAT_MBYTE
838 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000839 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 + matchcol);
841 else
842#endif
843 ++matchcol;
844 }
845 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200846 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100847 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (ptr[matchcol] == NUL
849 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000850 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000851 matchcol,
852#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200853 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000854#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200855 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000856#endif
857 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
859 match_ok = FALSE;
860 break;
861 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000862 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 endpos = regmatch.endpos[0];
864# ifdef FEAT_EVAL
865 submatch = first_submatch(&regmatch);
866# endif
867
868 /* Need to get the line pointer again, a
869 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000870 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 if (!match_ok)
873 continue;
874 }
875 if (dir == BACKWARD)
876 {
877 /*
878 * Now, if there are multiple matches on this line,
879 * we have to get the last one. Or the last one before
880 * the cursor, if we're on that line.
881 * When putting the new cursor at the end, compare
882 * relative to the end of the match.
883 */
884 match_ok = FALSE;
885 for (;;)
886 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000887 /* Remember a position that is before the start
888 * position, we use it if it's the last match in
889 * the line. Always accept a position after
890 * wrapping around. */
891 if (loop
892 || ((options & SEARCH_END)
893 ? (lnum + regmatch.endpos[0].lnum
894 < start_pos.lnum
895 || (lnum + regmatch.endpos[0].lnum
896 == start_pos.lnum
897 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200898 < (int)start_pos.col
899 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000900 : (lnum + regmatch.startpos[0].lnum
901 < start_pos.lnum
902 || (lnum + regmatch.startpos[0].lnum
903 == start_pos.lnum
904 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200905 < (int)start_pos.col
906 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000909 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 endpos = regmatch.endpos[0];
911# ifdef FEAT_EVAL
912 submatch = first_submatch(&regmatch);
913# endif
914 }
915 else
916 break;
917
918 /*
919 * We found a valid match, now check if there is
920 * another one after it.
921 * If vi-compatible searching, continue at the end
922 * of the match, otherwise continue one position
923 * forward.
924 */
925 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
926 {
927 if (nmatched > 1)
928 break;
929 matchcol = endpos.col;
930 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000931 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 && ptr[matchcol] != NUL)
933 {
934#ifdef FEAT_MBYTE
935 if (has_mbyte)
936 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000937 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 else
939#endif
940 ++matchcol;
941 }
942 }
943 else
944 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000945 /* Stop when the match is in a next line. */
946 if (matchpos.lnum > 0)
947 break;
948 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (ptr[matchcol] != NUL)
950 {
951#ifdef FEAT_MBYTE
952 if (has_mbyte)
953 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000954 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 else
956#endif
957 ++matchcol;
958 }
959 }
960 if (ptr[matchcol] == NUL
961 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000962 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000963 matchcol,
964#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200965 tm, timed_out
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000966#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +0200967 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000968#endif
969 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 break;
971
972 /* Need to get the line pointer again, a
973 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000974 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976
977 /*
978 * If there is only a match after the cursor, skip
979 * this match.
980 */
981 if (!match_ok)
982 continue;
983 }
984
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000985 /* With the SEARCH_END option move to the last character
986 * of the match. Don't do it for an empty match, end
987 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200988 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000989 && !(matchpos.lnum == endpos.lnum
990 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000992 /* For a match in the first column, set the position
993 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000994 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000995 pos->col = endpos.col;
996 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000997 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000998 if (pos->lnum > 1) /* just in case */
999 {
1000 --pos->lnum;
1001 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
1002 pos->lnum, FALSE));
1003 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001004 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001005 else
1006 {
1007 --pos->col;
1008#ifdef FEAT_MBYTE
1009 if (has_mbyte
1010 && pos->lnum <= buf->b_ml.ml_line_count)
1011 {
1012 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1013 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
1014 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001015#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +00001016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018 else
1019 {
Bram Moolenaar677ee682005-01-27 14:41:15 +00001020 pos->lnum = lnum + matchpos.lnum;
1021 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 }
1023#ifdef FEAT_VIRTUALEDIT
1024 pos->coladd = 0;
1025#endif
1026 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +01001027 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +00001030 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 search_match_endcol = endpos.col;
1032 break;
1033 }
1034 line_breakcheck(); /* stop if ctrl-C typed */
1035 if (got_int)
1036 break;
1037
1038#ifdef FEAT_SEARCH_EXTRA
1039 /* Cancel searching if a character was typed. Used for
1040 * 'incsearch'. Don't check too often, that would slowdown
1041 * searching too much. */
1042 if ((options & SEARCH_PEEK)
1043 && ((lnum - pos->lnum) & 0x3f) == 0
1044 && char_avail())
1045 {
1046 break_loop = TRUE;
1047 break;
1048 }
1049#endif
1050
1051 if (loop && lnum == start_pos.lnum)
1052 break; /* if second loop, stop where started */
1053 }
1054 at_first_line = FALSE;
1055
1056 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001057 * Stop the search if wrapscan isn't set, "stop_lnum" is
1058 * specified, after an interrupt, after a match and after looping
1059 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001061 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001062#ifdef FEAT_RELTIME
1063 || (timed_out != NULL && *timed_out)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001064#endif
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001065#ifdef FEAT_SEARCH_EXTRA
1066 || break_loop
1067#endif
1068 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 break;
1070
1071 /*
1072 * If 'wrapscan' is set we continue at the other end of the file.
1073 * If 'shortmess' does not contain 's', we give a message.
1074 * This message is also remembered in keep_msg for when the screen
1075 * is redrawn. The keep_msg is cleared whenever another message is
1076 * written.
1077 */
1078 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00001082 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
1083 give_warning((char_u *)_(dir == BACKWARD
1084 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
Bram Moolenaar78a15312009-05-15 19:33:18 +00001086 if (got_int || called_emsg
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001087#ifdef FEAT_RELTIME
1088 || (timed_out != NULL && *timed_out)
1089#endif
Bram Moolenaar78a15312009-05-15 19:33:18 +00001090#ifdef FEAT_SEARCH_EXTRA
1091 || break_loop
1092#endif
1093 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 break;
1095 }
1096 while (--count > 0 && found); /* stop after count matches or no match */
1097
Bram Moolenaar473de612013-06-08 18:19:48 +02001098 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
Bram Moolenaar280f1262006-01-30 00:14:18 +00001100 called_emsg |= save_called_emsg;
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (!found) /* did not find it */
1103 {
1104 if (got_int)
1105 EMSG(_(e_interr));
1106 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1107 {
1108 if (p_ws)
1109 EMSG2(_(e_patnotf2), mr_pattern);
1110 else if (lnum == 0)
1111 EMSG2(_("E384: search hit TOP without match for: %s"),
1112 mr_pattern);
1113 else
1114 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
1115 mr_pattern);
1116 }
1117 return FAIL;
1118 }
1119
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001120 /* A pattern like "\n\zs" may go past the last line. */
1121 if (pos->lnum > buf->b_ml.ml_line_count)
1122 {
1123 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001124 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001125 if (pos->col > 0)
1126 --pos->col;
1127 }
1128
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 return submatch + 1;
1130}
1131
1132#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001133 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001134set_search_direction(int cdir)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001135{
1136 spats[0].off.dir = cdir;
1137}
1138
1139 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001140set_vv_searchforward(void)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001141{
1142 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1143}
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145/*
1146 * Return the number of the first subpat that matched.
Bram Moolenaarad4d8a12015-12-28 19:20:36 +01001147 * Return zero if none of them matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 */
1149 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001150first_submatch(regmmatch_T *rp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151{
1152 int submatch;
1153
1154 for (submatch = 1; ; ++submatch)
1155 {
1156 if (rp->startpos[submatch].lnum >= 0)
1157 break;
1158 if (submatch == 9)
1159 {
1160 submatch = 0;
1161 break;
1162 }
1163 }
1164 return submatch;
1165}
1166#endif
1167
1168/*
1169 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001170 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 * If 'dirc' is 0: use previous dir.
1172 * If 'pat' is NULL or empty : use previous string.
1173 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1174 * If 'options & SEARCH_ECHO': echo the search command and handle options
1175 * If 'options & SEARCH_MSG' : may give error message
1176 * If 'options & SEARCH_OPT' : interpret optional flags
1177 * If 'options & SEARCH_HIS' : put search pattern in history
1178 * If 'options & SEARCH_NOOF': don't add offset to position
1179 * If 'options & SEARCH_MARK': set previous context mark
1180 * If 'options & SEARCH_KEEP': keep previous search pattern
1181 * If 'options & SEARCH_START': accept match at curpos itself
1182 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1183 *
1184 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1185 * makes the movement linewise without moving the match position.
1186 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001187 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 */
1189 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001190do_search(
1191 oparg_T *oap, /* can be NULL */
1192 int dirc, /* '/' or '?' */
1193 char_u *pat,
1194 long count,
1195 int options,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001196 proftime_T *tm, /* timeout limit or NULL */
1197 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198{
1199 pos_T pos; /* position of the last match */
1200 char_u *searchstr;
1201 struct soffset old_off;
1202 int retval; /* Return value */
1203 char_u *p;
1204 long c;
1205 char_u *dircp;
1206 char_u *strcopy = NULL;
1207 char_u *ps;
1208
1209 /*
1210 * A line offset is not remembered, this is vi compatible.
1211 */
1212 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1213 {
1214 spats[0].off.line = FALSE;
1215 spats[0].off.off = 0;
1216 }
1217
1218 /*
1219 * Save the values for when (options & SEARCH_KEEP) is used.
1220 * (there is no "if ()" around this because gcc wants them initialized)
1221 */
1222 old_off = spats[0].off;
1223
1224 pos = curwin->w_cursor; /* start searching at the cursor position */
1225
1226 /*
1227 * Find out the direction of the search.
1228 */
1229 if (dirc == 0)
1230 dirc = spats[0].off.dir;
1231 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001234#if defined(FEAT_EVAL)
1235 set_vv_searchforward();
1236#endif
1237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (options & SEARCH_REV)
1239 {
1240#ifdef WIN32
1241 /* There is a bug in the Visual C++ 2.2 compiler which means that
1242 * dirc always ends up being '/' */
1243 dirc = (dirc == '/') ? '?' : '/';
1244#else
1245 if (dirc == '/')
1246 dirc = '?';
1247 else
1248 dirc = '/';
1249#endif
1250 }
1251
1252#ifdef FEAT_FOLDING
1253 /* If the cursor is in a closed fold, don't find another match in the same
1254 * fold. */
1255 if (dirc == '/')
1256 {
1257 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1258 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1259 }
1260 else
1261 {
1262 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1263 pos.col = 0;
1264 }
1265#endif
1266
1267#ifdef FEAT_SEARCH_EXTRA
1268 /*
1269 * Turn 'hlsearch' highlighting back on.
1270 */
1271 if (no_hlsearch && !(options & SEARCH_KEEP))
1272 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001273 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01001274 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276#endif
1277
1278 /*
1279 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1280 */
1281 for (;;)
1282 {
1283 searchstr = pat;
1284 dircp = NULL;
1285 /* use previous pattern */
1286 if (pat == NULL || *pat == NUL || *pat == dirc)
1287 {
1288 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1289 {
Bram Moolenaarea683da2016-09-09 21:41:34 +02001290 searchstr = spats[RE_SUBST].pat;
1291 if (searchstr == NULL)
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001292 {
1293 EMSG(_(e_noprevre));
1294 retval = 0;
1295 goto end_do_search;
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001298 else
1299 {
1300 /* make search_regcomp() use spats[RE_SEARCH].pat */
1301 searchstr = (char_u *)"";
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304
1305 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1306 {
1307 /*
1308 * Find end of regular expression.
1309 * If there is a matching '/' or '?', toss it.
1310 */
1311 ps = strcopy;
1312 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1313 if (strcopy != ps)
1314 {
1315 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001316 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 pat = strcopy;
1318 searchstr = strcopy;
1319 }
1320 if (*p == dirc)
1321 {
1322 dircp = p; /* remember where we put the NUL */
1323 *p++ = NUL;
1324 }
1325 spats[0].off.line = FALSE;
1326 spats[0].off.end = FALSE;
1327 spats[0].off.off = 0;
1328 /*
1329 * Check for a line offset or a character offset.
1330 * For get_address (echo off) we don't check for a character
1331 * offset, because it is meaningless and the 's' could be a
1332 * substitute command.
1333 */
1334 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1335 spats[0].off.line = TRUE;
1336 else if ((options & SEARCH_OPT) &&
1337 (*p == 'e' || *p == 's' || *p == 'b'))
1338 {
1339 if (*p == 'e') /* end */
1340 spats[0].off.end = SEARCH_END;
1341 ++p;
1342 }
1343 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1344 {
1345 /* 'nr' or '+nr' or '-nr' */
1346 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1347 spats[0].off.off = atol((char *)p);
1348 else if (*p == '-') /* single '-' */
1349 spats[0].off.off = -1;
1350 else /* single '+' */
1351 spats[0].off.off = 1;
1352 ++p;
1353 while (VIM_ISDIGIT(*p)) /* skip number */
1354 ++p;
1355 }
1356
1357 /* compute length of search command for get_address() */
1358 searchcmdlen += (int)(p - pat);
1359
1360 pat = p; /* put pat after search command */
1361 }
1362
1363 if ((options & SEARCH_ECHO) && messaging()
1364 && !cmd_silent && msg_silent == 0)
1365 {
1366 char_u *msgbuf;
1367 char_u *trunc;
1368
1369 if (*searchstr == NUL)
1370 p = spats[last_idx].pat;
1371 else
1372 p = searchstr;
1373 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1374 if (msgbuf != NULL)
1375 {
1376 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001377#ifdef FEAT_MBYTE
1378 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1379 {
1380 /* Use a space to draw the composing char on. */
1381 msgbuf[1] = ' ';
1382 STRCPY(msgbuf + 2, p);
1383 }
1384 else
1385#endif
1386 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1388 {
1389 p = msgbuf + STRLEN(msgbuf);
1390 *p++ = dirc;
1391 if (spats[0].off.end)
1392 *p++ = 'e';
1393 else if (!spats[0].off.line)
1394 *p++ = 's';
1395 if (spats[0].off.off > 0 || spats[0].off.line)
1396 *p++ = '+';
1397 if (spats[0].off.off != 0 || spats[0].off.line)
1398 sprintf((char *)p, "%ld", spats[0].off.off);
1399 else
1400 *p = NUL;
1401 }
1402
1403 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001404 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406#ifdef FEAT_RIGHTLEFT
1407 /* The search pattern could be shown on the right in rightleft
1408 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1409 * it would be blanked out again very soon. Show it on the
1410 * left, but do reverse the text. */
1411 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1412 {
1413 char_u *r;
1414
1415 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1416 if (r != NULL)
1417 {
1418 vim_free(trunc);
1419 trunc = r;
1420 }
1421 }
1422#endif
1423 if (trunc != NULL)
1424 {
1425 msg_outtrans(trunc);
1426 vim_free(trunc);
1427 }
1428 else
1429 msg_outtrans(msgbuf);
1430 msg_clr_eos();
1431 msg_check();
1432 vim_free(msgbuf);
1433
1434 gotocmdline(FALSE);
1435 out_flush();
1436 msg_nowait = TRUE; /* don't wait for this message */
1437 }
1438 }
1439
1440 /*
1441 * If there is a character offset, subtract it from the current
1442 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001443 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 * This is not done for a line offset, because then we would not be vi
1445 * compatible.
1446 */
Bram Moolenaared203462004-06-16 11:19:22 +00001447 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 {
1449 if (spats[0].off.off > 0)
1450 {
1451 for (c = spats[0].off.off; c; --c)
1452 if (decl(&pos) == -1)
1453 break;
1454 if (c) /* at start of buffer */
1455 {
1456 pos.lnum = 0; /* allow lnum == 0 here */
1457 pos.col = MAXCOL;
1458 }
1459 }
1460 else
1461 {
1462 for (c = spats[0].off.off; c; ++c)
1463 if (incl(&pos) == -1)
1464 break;
1465 if (c) /* at end of buffer */
1466 {
1467 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1468 pos.col = 0;
1469 }
1470 }
1471 }
1472
1473#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1474 if (p_altkeymap && curwin->w_p_rl)
1475 lrFswap(searchstr,0);
1476#endif
1477
1478 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1479 searchstr, count, spats[0].off.end + (options &
1480 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1481 + SEARCH_MSG + SEARCH_START
1482 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001483 RE_LAST, (linenr_T)0, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 if (dircp != NULL)
1486 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1487 if (c == FAIL)
1488 {
1489 retval = 0;
1490 goto end_do_search;
1491 }
1492 if (spats[0].off.end && oap != NULL)
1493 oap->inclusive = TRUE; /* 'e' includes last character */
1494
1495 retval = 1; /* pattern found */
1496
1497 /*
1498 * Add character and/or line offset
1499 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001500 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 if (spats[0].off.line) /* Add the offset to the line number. */
1503 {
1504 c = pos.lnum + spats[0].off.off;
1505 if (c < 1)
1506 pos.lnum = 1;
1507 else if (c > curbuf->b_ml.ml_line_count)
1508 pos.lnum = curbuf->b_ml.ml_line_count;
1509 else
1510 pos.lnum = c;
1511 pos.col = 0;
1512
1513 retval = 2; /* pattern found, line offset added */
1514 }
Bram Moolenaared203462004-06-16 11:19:22 +00001515 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 {
1517 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001518 c = spats[0].off.off;
1519 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001521 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (incl(&pos) == -1)
1523 break;
1524 }
1525 /* to the left, check for start of file */
1526 else
1527 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001528 while (c++ < 0)
1529 if (decl(&pos) == -1)
1530 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 }
1533 }
1534
1535 /*
1536 * The search command can be followed by a ';' to do another search.
1537 * For example: "/pat/;/foo/+3;?bar"
1538 * This is like doing another search command, except:
1539 * - The remembered direction '/' or '?' is from the first search.
1540 * - When an error happens the cursor isn't moved at all.
1541 * Don't do this when called by get_address() (it handles ';' itself).
1542 */
1543 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1544 break;
1545
1546 dirc = *++pat;
1547 if (dirc != '?' && dirc != '/')
1548 {
1549 retval = 0;
1550 EMSG(_("E386: Expected '?' or '/' after ';'"));
1551 goto end_do_search;
1552 }
1553 ++pat;
1554 }
1555
1556 if (options & SEARCH_MARK)
1557 setpcmark();
1558 curwin->w_cursor = pos;
1559 curwin->w_set_curswant = TRUE;
1560
1561end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001562 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 spats[0].off = old_off;
1564 vim_free(strcopy);
1565
1566 return retval;
1567}
1568
1569#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1570/*
1571 * search_for_exact_line(buf, pos, dir, pat)
1572 *
1573 * Search for a line starting with the given pattern (ignoring leading
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001574 * white-space), starting from pos and going in direction "dir". "pos" will
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 * contain the position of the match found. Blank lines match only if
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02001576 * ADDING is set. If p_ic is set then the pattern must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 * Return OK for success, or FAIL if no line found.
1578 */
1579 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001580search_for_exact_line(
1581 buf_T *buf,
1582 pos_T *pos,
1583 int dir,
1584 char_u *pat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 linenr_T start = 0;
1587 char_u *ptr;
1588 char_u *p;
1589
1590 if (buf->b_ml.ml_line_count == 0)
1591 return FAIL;
1592 for (;;)
1593 {
1594 pos->lnum += dir;
1595 if (pos->lnum < 1)
1596 {
1597 if (p_ws)
1598 {
1599 pos->lnum = buf->b_ml.ml_line_count;
1600 if (!shortmess(SHM_SEARCH))
1601 give_warning((char_u *)_(top_bot_msg), TRUE);
1602 }
1603 else
1604 {
1605 pos->lnum = 1;
1606 break;
1607 }
1608 }
1609 else if (pos->lnum > buf->b_ml.ml_line_count)
1610 {
1611 if (p_ws)
1612 {
1613 pos->lnum = 1;
1614 if (!shortmess(SHM_SEARCH))
1615 give_warning((char_u *)_(bot_top_msg), TRUE);
1616 }
1617 else
1618 {
1619 pos->lnum = 1;
1620 break;
1621 }
1622 }
1623 if (pos->lnum == start)
1624 break;
1625 if (start == 0)
1626 start = pos->lnum;
1627 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1628 p = skipwhite(ptr);
1629 pos->col = (colnr_T) (p - ptr);
1630
1631 /* when adding lines the matching line may be empty but it is not
1632 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001633 if ((compl_cont_status & CONT_ADDING)
1634 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1637 return OK;
1638 }
1639 else if (*p != NUL) /* ignore empty lines */
1640 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001641 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1642 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 return OK;
1644 }
1645 }
1646 return FAIL;
1647}
1648#endif /* FEAT_INS_EXPAND */
1649
1650/*
1651 * Character Searches
1652 */
1653
1654/*
1655 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1656 * position of the character, otherwise move to just before the char.
1657 * Do this "cap->count1" times.
1658 * Return FAIL or OK.
1659 */
1660 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001661searchc(cmdarg_T *cap, int t_cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 int c = cap->nchar; /* char to search for */
1664 int dir = cap->arg; /* TRUE for searching forward */
1665 long count = cap->count1; /* repeat count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 int col;
1667 char_u *p;
1668 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001669 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671 if (c != NUL) /* normal search: remember args for repeat */
1672 {
1673 if (!KeyStuffed) /* don't remember when redoing */
1674 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001675 *lastc = c;
1676 set_csearch_direction(dir);
1677 set_csearch_until(t_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001679 lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (cap->ncharC1 != 0)
1681 {
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001682 lastc_bytelen += (*mb_char2bytes)(cap->ncharC1,
1683 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (cap->ncharC2 != 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001685 lastc_bytelen += (*mb_char2bytes)(cap->ncharC2,
1686 lastc_bytes + lastc_bytelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688#endif
1689 }
1690 }
1691 else /* repeat previous search */
1692 {
Bram Moolenaar454709b2017-03-12 16:37:14 +01001693 if (*lastc == NUL
1694#ifdef FEAT_MBYTE
1695 && lastc_bytelen == 1
1696#endif
1697 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 return FAIL;
1699 if (dir) /* repeat in opposite direction */
1700 dir = -lastcdir;
1701 else
1702 dir = lastcdir;
1703 t_cmd = last_t_cmd;
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001704 c = *lastc;
1705 /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001706
1707 /* Force a move of at least one char, so ";" and "," will move the
1708 * cursor, even if the cursor is right in front of char we are looking
1709 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001710 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001711 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 }
1713
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001714 if (dir == BACKWARD)
1715 cap->oap->inclusive = FALSE;
1716 else
1717 cap->oap->inclusive = TRUE;
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 p = ml_get_curline();
1720 col = curwin->w_cursor.col;
1721 len = (int)STRLEN(p);
1722
1723 while (count--)
1724 {
1725#ifdef FEAT_MBYTE
1726 if (has_mbyte)
1727 {
1728 for (;;)
1729 {
1730 if (dir > 0)
1731 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001732 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 if (col >= len)
1734 return FAIL;
1735 }
1736 else
1737 {
1738 if (col == 0)
1739 return FAIL;
1740 col -= (*mb_head_off)(p, p + col - 1) + 1;
1741 }
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001742 if (lastc_bytelen == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001744 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 break;
1746 }
Bram Moolenaar66727e12017-03-01 22:17:05 +01001747 else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0
Bram Moolenaarb129a442016-12-01 17:25:20 +01001748 && stop)
Bram Moolenaar66727e12017-03-01 22:17:05 +01001749 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001750 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
1752 }
1753 else
1754#endif
1755 {
1756 for (;;)
1757 {
1758 if ((col += dir) < 0 || col >= len)
1759 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001760 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001762 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 }
1764 }
1765 }
1766
1767 if (t_cmd)
1768 {
1769 /* backup to before the character (possibly double-byte) */
1770 col -= dir;
1771#ifdef FEAT_MBYTE
1772 if (has_mbyte)
1773 {
1774 if (dir < 0)
Bram Moolenaardbd24b52015-08-11 14:26:19 +02001775 /* Landed on the search char which is lastc_bytelen long */
1776 col += lastc_bytelen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 else
1778 /* To previous char, which may be multi-byte. */
1779 col -= (*mb_head_off)(p, p + col);
1780 }
1781#endif
1782 }
1783 curwin->w_cursor.col = col;
1784
1785 return OK;
1786}
1787
1788/*
1789 * "Other" Searches
1790 */
1791
1792/*
1793 * findmatch - find the matching paren or brace
1794 *
1795 * Improvement over vi: Braces inside quotes are ignored.
1796 */
1797 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001798findmatch(oparg_T *oap, int initc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 return findmatchlimit(oap, initc, 0, 0);
1801}
1802
1803/*
1804 * Return TRUE if the character before "linep[col]" equals "ch".
1805 * Return FALSE if "col" is zero.
1806 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1807 * is NULL.
1808 * Handles multibyte string correctly.
1809 */
1810 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001811check_prevcol(
1812 char_u *linep,
1813 int col,
1814 int ch,
1815 int *prevcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
1817 --col;
1818#ifdef FEAT_MBYTE
1819 if (col > 0 && has_mbyte)
1820 col -= (*mb_head_off)(linep, linep + col);
1821#endif
1822 if (prevcol)
1823 *prevcol = col;
1824 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1825}
1826
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001827static int find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001828
1829/*
1830 * Raw string start is found at linep[startpos.col - 1].
1831 * Return TRUE if the matching end can be found between startpos and endpos.
1832 */
1833 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001834find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos)
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001835{
1836 char_u *p;
1837 char_u *delim_copy;
1838 size_t delim_len;
1839 linenr_T lnum;
1840 int found = FALSE;
1841
1842 for (p = linep + startpos->col + 1; *p && *p != '('; ++p)
1843 ;
1844 delim_len = (p - linep) - startpos->col - 1;
Bram Moolenaar6ed535d2015-08-26 23:01:21 +02001845 delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len);
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001846 if (delim_copy == NULL)
1847 return FALSE;
1848 for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum)
1849 {
1850 char_u *line = ml_get(lnum);
1851
1852 for (p = line + (lnum == startpos->lnum
1853 ? startpos->col + 1 : 0); *p; ++p)
1854 {
1855 if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col)
1856 break;
1857 if (*p == ')' && p[delim_len + 1] == '"'
1858 && STRNCMP(delim_copy, p + 1, delim_len) == 0)
1859 {
1860 found = TRUE;
1861 break;
1862 }
1863 }
1864 if (found)
1865 break;
1866 }
1867 vim_free(delim_copy);
1868 return found;
1869}
1870
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871/*
1872 * findmatchlimit -- find the matching paren or brace, if it exists within
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001873 * maxtravel lines of the cursor. A maxtravel of 0 means search until falling
1874 * off the edge of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 *
1876 * "initc" is the character to find a match for. NUL means to find the
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001877 * character at or after the cursor. Special values:
1878 * '*' look for C-style comment / *
1879 * '/' look for C-style comment / *, ignoring comment-end
1880 * '#' look for preprocessor directives
1881 * 'R' look for raw string start: R"delim(text)delim" (only backwards)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 *
1883 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1884 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1885 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1886 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001887 *
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001888 * "oap" is only used to set oap->motion_type for a linewise motion, it can be
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001889 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891
1892 pos_T *
Bram Moolenaar764b23c2016-01-30 21:10:09 +01001893findmatchlimit(
1894 oparg_T *oap,
1895 int initc,
1896 int flags,
1897 int maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 static pos_T pos; /* current search position */
1900 int findc = 0; /* matching brace */
1901 int c;
1902 int count = 0; /* cumulative number of braces */
1903 int backwards = FALSE; /* init for gcc */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001904 int raw_string = FALSE; /* search for raw string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 int inquote = FALSE; /* TRUE when inside quotes */
1906 char_u *linep; /* pointer to current line */
1907 char_u *ptr;
1908 int do_quotes; /* check for quotes in current line */
1909 int at_start; /* do_quotes value at start position */
1910 int hash_dir = 0; /* Direction searched for # things */
1911 int comment_dir = 0; /* Direction searched for comments */
1912 pos_T match_pos; /* Where last slash-star was found */
1913 int start_in_quotes; /* start position is in quotes */
1914 int traveled = 0; /* how far we've searched so far */
1915 int ignore_cend = FALSE; /* ignore comment end */
1916 int cpo_match; /* vi compatible matching */
1917 int cpo_bsl; /* don't recognize backslashes */
1918 int match_escaped = 0; /* search for escaped match */
1919 int dir; /* Direction to search */
1920 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001921#ifdef FEAT_LISP
1922 int lispcomm = FALSE; /* inside of Lisp-style comment */
1923 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001927#ifdef FEAT_VIRTUALEDIT
1928 pos.coladd = 0;
1929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 linep = ml_get(pos.lnum);
1931
1932 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1933 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1934
1935 /* Direction to search when initc is '/', '*' or '#' */
1936 if (flags & FM_BACKWARD)
1937 dir = BACKWARD;
1938 else if (flags & FM_FORWARD)
1939 dir = FORWARD;
1940 else
1941 dir = 0;
1942
1943 /*
1944 * if initc given, look in the table for the matching character
1945 * '/' and '*' are special cases: look for start or end of comment.
1946 * When '/' is used, we ignore running backwards into an star-slash, for
1947 * "[*" command, we just want to find any comment.
1948 */
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001949 if (initc == '/' || initc == '*' || initc == 'R')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 comment_dir = dir;
1952 if (initc == '/')
1953 ignore_cend = TRUE;
1954 backwards = (dir == FORWARD) ? FALSE : TRUE;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001955 raw_string = (initc == 'R');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 initc = NUL;
1957 }
1958 else if (initc != '#' && initc != NUL)
1959 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001960 find_mps_values(&initc, &findc, &backwards, TRUE);
1961 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 return NULL;
1963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 else
1965 {
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02001966 /*
1967 * Either initc is '#', or no initc was given and we need to look
1968 * under the cursor.
1969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if (initc == '#')
1971 {
1972 hash_dir = dir;
1973 }
1974 else
1975 {
1976 /*
1977 * initc was not given, must look for something to match under
1978 * or near the cursor.
1979 * Only check for special things when 'cpo' doesn't have '%'.
1980 */
1981 if (!cpo_match)
1982 {
1983 /* Are we before or at #if, #else etc.? */
1984 ptr = skipwhite(linep);
1985 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1986 {
1987 ptr = skipwhite(ptr + 1);
1988 if ( STRNCMP(ptr, "if", 2) == 0
1989 || STRNCMP(ptr, "endif", 5) == 0
1990 || STRNCMP(ptr, "el", 2) == 0)
1991 hash_dir = 1;
1992 }
1993
1994 /* Are we on a comment? */
1995 else if (linep[pos.col] == '/')
1996 {
1997 if (linep[pos.col + 1] == '*')
1998 {
1999 comment_dir = FORWARD;
2000 backwards = FALSE;
2001 pos.col++;
2002 }
2003 else if (pos.col > 0 && linep[pos.col - 1] == '*')
2004 {
2005 comment_dir = BACKWARD;
2006 backwards = TRUE;
2007 pos.col--;
2008 }
2009 }
2010 else if (linep[pos.col] == '*')
2011 {
2012 if (linep[pos.col + 1] == '/')
2013 {
2014 comment_dir = BACKWARD;
2015 backwards = TRUE;
2016 }
2017 else if (pos.col > 0 && linep[pos.col - 1] == '/')
2018 {
2019 comment_dir = FORWARD;
2020 backwards = FALSE;
2021 }
2022 }
2023 }
2024
2025 /*
2026 * If we are not on a comment or the # at the start of a line, then
2027 * look for brace anywhere on this line after the cursor.
2028 */
2029 if (!hash_dir && !comment_dir)
2030 {
2031 /*
2032 * Find the brace under or after the cursor.
2033 * If beyond the end of the line, use the last character in
2034 * the line.
2035 */
2036 if (linep[pos.col] == NUL && pos.col)
2037 --pos.col;
2038 for (;;)
2039 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002040 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 if (initc == NUL)
2042 break;
2043
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002044 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (findc)
2046 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002047 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 if (!findc)
2050 {
2051 /* no brace in the line, maybe use " #if" then */
2052 if (!cpo_match && *skipwhite(linep) == '#')
2053 hash_dir = 1;
2054 else
2055 return NULL;
2056 }
2057 else if (!cpo_bsl)
2058 {
2059 int col, bslcnt = 0;
2060
2061 /* Set "match_escaped" if there are an odd number of
2062 * backslashes. */
2063 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2064 bslcnt++;
2065 match_escaped = (bslcnt & 1);
2066 }
2067 }
2068 }
2069 if (hash_dir)
2070 {
2071 /*
2072 * Look for matching #if, #else, #elif, or #endif
2073 */
2074 if (oap != NULL)
2075 oap->motion_type = MLINE; /* Linewise for this case only */
2076 if (initc != '#')
2077 {
2078 ptr = skipwhite(skipwhite(linep) + 1);
2079 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
2080 hash_dir = 1;
2081 else if (STRNCMP(ptr, "endif", 5) == 0)
2082 hash_dir = -1;
2083 else
2084 return NULL;
2085 }
2086 pos.col = 0;
2087 while (!got_int)
2088 {
2089 if (hash_dir > 0)
2090 {
2091 if (pos.lnum == curbuf->b_ml.ml_line_count)
2092 break;
2093 }
2094 else if (pos.lnum == 1)
2095 break;
2096 pos.lnum += hash_dir;
2097 linep = ml_get(pos.lnum);
2098 line_breakcheck(); /* check for CTRL-C typed */
2099 ptr = skipwhite(linep);
2100 if (*ptr != '#')
2101 continue;
2102 pos.col = (colnr_T) (ptr - linep);
2103 ptr = skipwhite(ptr + 1);
2104 if (hash_dir > 0)
2105 {
2106 if (STRNCMP(ptr, "if", 2) == 0)
2107 count++;
2108 else if (STRNCMP(ptr, "el", 2) == 0)
2109 {
2110 if (count == 0)
2111 return &pos;
2112 }
2113 else if (STRNCMP(ptr, "endif", 5) == 0)
2114 {
2115 if (count == 0)
2116 return &pos;
2117 count--;
2118 }
2119 }
2120 else
2121 {
2122 if (STRNCMP(ptr, "if", 2) == 0)
2123 {
2124 if (count == 0)
2125 return &pos;
2126 count--;
2127 }
2128 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
2129 {
2130 if (count == 0)
2131 return &pos;
2132 }
2133 else if (STRNCMP(ptr, "endif", 5) == 0)
2134 count++;
2135 }
2136 }
2137 return NULL;
2138 }
2139 }
2140
2141#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00002142 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 * paren/brace in the other direction. */
2144 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
2145 backwards = !backwards;
2146#endif
2147
2148 do_quotes = -1;
2149 start_in_quotes = MAYBE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002150 CLEAR_POS(&match_pos);
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002153 if ((backwards && comment_dir)
2154#ifdef FEAT_LISP
2155 || lisp
2156#endif
2157 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002159#ifdef FEAT_LISP
2160 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2161 lispcomm = TRUE; /* find match inside this comment */
2162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 while (!got_int)
2164 {
2165 /*
2166 * Go to the next position, forward or backward. We could use
2167 * inc() and dec() here, but that is much slower
2168 */
2169 if (backwards)
2170 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002171#ifdef FEAT_LISP
2172 /* char to match is inside of comment, don't search outside */
2173 if (lispcomm && pos.col < (colnr_T)comment_col)
2174 break;
2175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (pos.col == 0) /* at start of line, go to prev. one */
2177 {
2178 if (pos.lnum == 1) /* start of file */
2179 break;
2180 --pos.lnum;
2181
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002182 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 break;
2184
2185 linep = ml_get(pos.lnum);
2186 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2187 do_quotes = -1;
2188 line_breakcheck();
2189
2190 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002191 if (comment_dir
2192#ifdef FEAT_LISP
2193 || lisp
2194#endif
2195 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002197#ifdef FEAT_LISP
2198 /* skip comment */
2199 if (lisp && comment_col != MAXCOL)
2200 pos.col = comment_col;
2201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203 else
2204 {
2205 --pos.col;
2206#ifdef FEAT_MBYTE
2207 if (has_mbyte)
2208 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2209#endif
2210 }
2211 }
2212 else /* forward search */
2213 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002214 if (linep[pos.col] == NUL
2215 /* at end of line, go to next one */
2216#ifdef FEAT_LISP
2217 /* don't search for match in comment */
2218 || (lisp && comment_col != MAXCOL
2219 && pos.col == (colnr_T)comment_col)
2220#endif
2221 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002223 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2224#ifdef FEAT_LISP
2225 /* line is exhausted and comment with it,
2226 * don't search for match in code */
2227 || lispcomm
2228#endif
2229 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 break;
2231 ++pos.lnum;
2232
2233 if (maxtravel && traveled++ > maxtravel)
2234 break;
2235
2236 linep = ml_get(pos.lnum);
2237 pos.col = 0;
2238 do_quotes = -1;
2239 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002240#ifdef FEAT_LISP
2241 if (lisp) /* find comment pos in new line */
2242 comment_col = check_linecomment(linep);
2243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245 else
2246 {
2247#ifdef FEAT_MBYTE
2248 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002249 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 else
2251#endif
2252 ++pos.col;
2253 }
2254 }
2255
2256 /*
2257 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2258 */
2259 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2260 (linep[0] == '{' || linep[0] == '}'))
2261 {
2262 if (linep[0] == findc && count == 0) /* match! */
2263 return &pos;
2264 break; /* out of scope */
2265 }
2266
2267 if (comment_dir)
2268 {
2269 /* Note: comments do not nest, and we ignore quotes in them */
2270 /* TODO: ignore comment brackets inside strings */
2271 if (comment_dir == FORWARD)
2272 {
2273 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2274 {
2275 pos.col++;
2276 return &pos;
2277 }
2278 }
2279 else /* Searching backwards */
2280 {
2281 /*
2282 * A comment may contain / * or / /, it may also start or end
2283 * with / * /. Ignore a / * after / /.
2284 */
2285 if (pos.col == 0)
2286 continue;
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02002287 else if (raw_string)
2288 {
2289 if (linep[pos.col - 1] == 'R'
2290 && linep[pos.col] == '"'
2291 && vim_strchr(linep + pos.col + 1, '(') != NULL)
2292 {
2293 /* Possible start of raw string. Now that we have the
2294 * delimiter we can check if it ends before where we
2295 * started searching, or before the previously found
2296 * raw string start. */
2297 if (!find_rawstring_end(linep, &pos,
2298 count > 0 ? &match_pos : &curwin->w_cursor))
2299 {
2300 count++;
2301 match_pos = pos;
2302 match_pos.col--;
2303 }
2304 linep = ml_get(pos.lnum); /* may have been released */
2305 }
2306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 else if ( linep[pos.col - 1] == '/'
2308 && linep[pos.col] == '*'
2309 && (int)pos.col < comment_col)
2310 {
2311 count++;
2312 match_pos = pos;
2313 match_pos.col--;
2314 }
2315 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2316 {
2317 if (count > 0)
2318 pos = match_pos;
2319 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2320 && (int)pos.col <= comment_col)
2321 pos.col -= 2;
2322 else if (ignore_cend)
2323 continue;
2324 else
2325 return NULL;
2326 return &pos;
2327 }
2328 }
2329 continue;
2330 }
2331
2332 /*
2333 * If smart matching ('cpoptions' does not contain '%'), braces inside
2334 * of quotes are ignored, but only if there is an even number of
2335 * quotes in the line.
2336 */
2337 if (cpo_match)
2338 do_quotes = 0;
2339 else if (do_quotes == -1)
2340 {
2341 /*
2342 * Count the number of quotes in the line, skipping \" and '"'.
2343 * Watch out for "\\".
2344 */
2345 at_start = do_quotes;
2346 for (ptr = linep; *ptr; ++ptr)
2347 {
2348 if (ptr == linep + pos.col + backwards)
2349 at_start = (do_quotes & 1);
2350 if (*ptr == '"'
2351 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2352 ++do_quotes;
2353 if (*ptr == '\\' && ptr[1] != NUL)
2354 ++ptr;
2355 }
2356 do_quotes &= 1; /* result is 1 with even number of quotes */
2357
2358 /*
2359 * If we find an uneven count, check current line and previous
2360 * one for a '\' at the end.
2361 */
2362 if (!do_quotes)
2363 {
2364 inquote = FALSE;
2365 if (ptr[-1] == '\\')
2366 {
2367 do_quotes = 1;
2368 if (start_in_quotes == MAYBE)
2369 {
2370 /* Do we need to use at_start here? */
2371 inquote = TRUE;
2372 start_in_quotes = TRUE;
2373 }
2374 else if (backwards)
2375 inquote = TRUE;
2376 }
2377 if (pos.lnum > 1)
2378 {
2379 ptr = ml_get(pos.lnum - 1);
2380 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2381 {
2382 do_quotes = 1;
2383 if (start_in_quotes == MAYBE)
2384 {
2385 inquote = at_start;
2386 if (inquote)
2387 start_in_quotes = TRUE;
2388 }
2389 else if (!backwards)
2390 inquote = TRUE;
2391 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002392
2393 /* ml_get() only keeps one line, need to get linep again */
2394 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396 }
2397 }
2398 if (start_in_quotes == MAYBE)
2399 start_in_quotes = FALSE;
2400
2401 /*
2402 * If 'smartmatch' is set:
2403 * Things inside quotes are ignored by setting 'inquote'. If we
2404 * find a quote without a preceding '\' invert 'inquote'. At the
2405 * end of a line not ending in '\' we reset 'inquote'.
2406 *
2407 * In lines with an uneven number of quotes (without preceding '\')
2408 * we do not know which part to ignore. Therefore we only set
2409 * inquote if the number of quotes in a line is even, unless this
2410 * line or the previous one ends in a '\'. Complicated, isn't it?
2411 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002412 c = PTR2CHAR(linep + pos.col);
2413 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
2415 case NUL:
2416 /* at end of line without trailing backslash, reset inquote */
2417 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2418 {
2419 inquote = FALSE;
2420 start_in_quotes = FALSE;
2421 }
2422 break;
2423
2424 case '"':
2425 /* a quote that is preceded with an odd number of backslashes is
2426 * ignored */
2427 if (do_quotes)
2428 {
2429 int col;
2430
2431 for (col = pos.col - 1; col >= 0; --col)
2432 if (linep[col] != '\\')
2433 break;
2434 if ((((int)pos.col - 1 - col) & 1) == 0)
2435 {
2436 inquote = !inquote;
2437 start_in_quotes = FALSE;
2438 }
2439 }
2440 break;
2441
2442 /*
2443 * If smart matching ('cpoptions' does not contain '%'):
2444 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2445 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2446 * skipped, there is never a brace in them.
2447 * Ignore this when finding matches for `'.
2448 */
2449 case '\'':
2450 if (!cpo_match && initc != '\'' && findc != '\'')
2451 {
2452 if (backwards)
2453 {
2454 if (pos.col > 1)
2455 {
2456 if (linep[pos.col - 2] == '\'')
2457 {
2458 pos.col -= 2;
2459 break;
2460 }
2461 else if (linep[pos.col - 2] == '\\' &&
2462 pos.col > 2 && linep[pos.col - 3] == '\'')
2463 {
2464 pos.col -= 3;
2465 break;
2466 }
2467 }
2468 }
2469 else if (linep[pos.col + 1]) /* forward search */
2470 {
2471 if (linep[pos.col + 1] == '\\' &&
2472 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2473 {
2474 pos.col += 3;
2475 break;
2476 }
2477 else if (linep[pos.col + 2] == '\'')
2478 {
2479 pos.col += 2;
2480 break;
2481 }
2482 }
2483 }
2484 /* FALLTHROUGH */
2485
2486 default:
2487#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002488 /*
2489 * For Lisp skip over backslashed (), {} and [].
2490 * (actually, we skip #\( et al)
2491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 if (curbuf->b_p_lisp
2493 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002494 && pos.col > 1
2495 && check_prevcol(linep, pos.col, '\\', NULL)
2496 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 break;
2498#endif
2499
2500 /* Check for match outside of quotes, and inside of
2501 * quotes when the start is also inside of quotes. */
2502 if ((!inquote || start_in_quotes == TRUE)
2503 && (c == initc || c == findc))
2504 {
2505 int col, bslcnt = 0;
2506
2507 if (!cpo_bsl)
2508 {
2509 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2510 bslcnt++;
2511 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002512 /* Only accept a match when 'M' is in 'cpo' or when escaping
2513 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2515 {
2516 if (c == initc)
2517 count++;
2518 else
2519 {
2520 if (count == 0)
2521 return &pos;
2522 count--;
2523 }
2524 }
2525 }
2526 }
2527 }
2528
2529 if (comment_dir == BACKWARD && count > 0)
2530 {
2531 pos = match_pos;
2532 return &pos;
2533 }
2534 return (pos_T *)NULL; /* never found it */
2535}
2536
2537/*
2538 * Check if line[] contains a / / comment.
2539 * Return MAXCOL if not, otherwise return the column.
2540 * TODO: skip strings.
2541 */
2542 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002543check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 char_u *p;
2546
2547 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002548#ifdef FEAT_LISP
2549 /* skip Lispish one-line comments */
2550 if (curbuf->b_p_lisp)
2551 {
2552 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2553 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002554 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002555
2556 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002557 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002558 {
2559 if (*p == '"')
2560 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002561 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002562 {
2563 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002564 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002565 }
2566 else if (p == line || ((p - line) >= 2
2567 /* skip #\" form */
2568 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002569 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002570 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002571 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002572 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2573 break; /* found! */
2574 ++p;
2575 }
2576 }
2577 else
2578 p = NULL;
2579 }
2580 else
2581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 while ((p = vim_strchr(p, '/')) != NULL)
2583 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002584 /* accept a double /, unless it's preceded with * and followed by *,
2585 * because * / / * is an end and start of a C comment */
2586 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 break;
2588 ++p;
2589 }
2590
2591 if (p == NULL)
2592 return MAXCOL;
2593 return (int)(p - line);
2594}
2595
2596/*
2597 * Move cursor briefly to character matching the one under the cursor.
2598 * Used for Insert mode and "r" command.
2599 * Show the match only if it is visible on the screen.
2600 * If there isn't a match, then beep.
2601 */
2602 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002603showmatch(
2604 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
2606 pos_T *lpos, save_cursor;
2607 pos_T mpos;
2608 colnr_T vcol;
2609 long save_so;
2610 long save_siso;
2611#ifdef CURSOR_SHAPE
2612 int save_state;
2613#endif
2614 colnr_T save_dollar_vcol;
2615 char_u *p;
2616
2617 /*
2618 * Only show match for chars in the 'matchpairs' option.
2619 */
2620 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002621 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
2623#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002624 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002625 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002626#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002627 p += MB_PTR2LEN(p) + 1;
2628 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#ifdef FEAT_RIGHTLEFT
2630 && !(curwin->w_p_rl ^ p_ri)
2631#endif
2632 )
2633 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002634 p += MB_PTR2LEN(p);
2635 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 return;
2637 }
2638
2639 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002640 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002641 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
2643 if (!curwin->w_p_wrap)
2644 getvcol(curwin, lpos, NULL, &vcol, NULL);
2645 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002646 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
2648 mpos = *lpos; /* save the pos, update_screen() may change it */
2649 save_cursor = curwin->w_cursor;
2650 save_so = p_so;
2651 save_siso = p_siso;
2652 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2653 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002654 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2655 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 ++curwin->w_virtcol; /* do display ')' just before "$" */
2657 update_screen(VALID); /* show the new char first */
2658
2659 save_dollar_vcol = dollar_vcol;
2660#ifdef CURSOR_SHAPE
2661 save_state = State;
2662 State = SHOWMATCH;
2663 ui_cursor_shape(); /* may show different cursor shape */
2664#endif
2665 curwin->w_cursor = mpos; /* move to matching char */
2666 p_so = 0; /* don't use 'scrolloff' here */
2667 p_siso = 0; /* don't use 'sidescrolloff' here */
2668 showruler(FALSE);
2669 setcursor();
2670 cursor_on(); /* make sure that the cursor is shown */
2671 out_flush();
2672#ifdef FEAT_GUI
2673 if (gui.in_use)
2674 {
2675 gui_update_cursor(TRUE, FALSE);
2676 gui_mch_flush();
2677 }
2678#endif
2679 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2680 * which resets it if the matching position is in a previous line
2681 * and has a higher column number. */
2682 dollar_vcol = save_dollar_vcol;
2683
2684 /*
2685 * brief pause, unless 'm' is present in 'cpo' and a character is
2686 * available.
2687 */
2688 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2689 ui_delay(p_mat * 100L, TRUE);
2690 else if (!char_avail())
2691 ui_delay(p_mat * 100L, FALSE);
2692 curwin->w_cursor = save_cursor; /* restore cursor position */
2693 p_so = save_so;
2694 p_siso = save_siso;
2695#ifdef CURSOR_SHAPE
2696 State = save_state;
2697 ui_cursor_shape(); /* may show different cursor shape */
2698#endif
2699 }
2700 }
2701}
2702
2703/*
2704 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002705 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 * space or a line break. Also stop at an empty line.
2707 * Return OK if the next sentence was found.
2708 */
2709 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002710findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 pos_T pos, tpos;
2713 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002714 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 int startlnum;
2716 int noskip = FALSE; /* do not skip blanks */
2717 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002718 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 pos = curwin->w_cursor;
2721 if (dir == FORWARD)
2722 func = incl;
2723 else
2724 func = decl;
2725
2726 while (count--)
2727 {
2728 /*
2729 * if on an empty line, skip upto a non-empty line
2730 */
2731 if (gchar_pos(&pos) == NUL)
2732 {
2733 do
2734 if ((*func)(&pos) == -1)
2735 break;
2736 while (gchar_pos(&pos) == NUL);
2737 if (dir == FORWARD)
2738 goto found;
2739 }
2740 /*
2741 * if on the start of a paragraph or a section and searching forward,
2742 * go to the next line
2743 */
2744 else if (dir == FORWARD && pos.col == 0 &&
2745 startPS(pos.lnum, NUL, FALSE))
2746 {
2747 if (pos.lnum == curbuf->b_ml.ml_line_count)
2748 return FAIL;
2749 ++pos.lnum;
2750 goto found;
2751 }
2752 else if (dir == BACKWARD)
2753 decl(&pos);
2754
2755 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002756 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2758 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2759 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002760 if (vim_strchr((char_u *)".!?", c) != NULL)
2761 {
2762 /* Only skip over a '.', '!' and '?' once. */
2763 if (found_dot)
2764 break;
2765 found_dot = TRUE;
2766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (decl(&pos) == -1)
2768 break;
2769 /* when going forward: Stop in front of empty line */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002770 if (LINEEMPTY(pos.lnum) && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 {
2772 incl(&pos);
2773 goto found;
2774 }
2775 }
2776
2777 /* remember the line where the search started */
2778 startlnum = pos.lnum;
2779 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2780
2781 for (;;) /* find end of sentence */
2782 {
2783 c = gchar_pos(&pos);
2784 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2785 {
2786 if (dir == BACKWARD && pos.lnum != startlnum)
2787 ++pos.lnum;
2788 break;
2789 }
2790 if (c == '.' || c == '!' || c == '?')
2791 {
2792 tpos = pos;
2793 do
2794 if ((c = inc(&tpos)) == -1)
2795 break;
2796 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2797 != NULL);
2798 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2799 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2800 && gchar_pos(&tpos) == ' ')))
2801 {
2802 pos = tpos;
2803 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2804 inc(&pos);
2805 break;
2806 }
2807 }
2808 if ((*func)(&pos) == -1)
2809 {
2810 if (count)
2811 return FAIL;
2812 noskip = TRUE;
2813 break;
2814 }
2815 }
2816found:
2817 /* skip white space */
2818 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2819 if (incl(&pos) == -1)
2820 break;
2821 }
2822
2823 setpcmark();
2824 curwin->w_cursor = pos;
2825 return OK;
2826}
2827
2828/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002829 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002831 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 * If 'what' is '{' or '}' we go to the next section.
2833 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002834 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
2836 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002837findpar(
2838 int *pincl, /* Return: TRUE if last char is to be included */
2839 int dir,
2840 long count,
2841 int what,
2842 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843{
2844 linenr_T curr;
2845 int did_skip; /* TRUE after separating lines have been skipped */
2846 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002847 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848#ifdef FEAT_FOLDING
2849 linenr_T fold_first; /* first line of a closed fold */
2850 linenr_T fold_last; /* last line of a closed fold */
2851 int fold_skipped; /* TRUE if a closed fold was skipped this
2852 iteration */
2853#endif
2854
2855 curr = curwin->w_cursor.lnum;
2856
2857 while (count--)
2858 {
2859 did_skip = FALSE;
2860 for (first = TRUE; ; first = FALSE)
2861 {
2862 if (*ml_get(curr) != NUL)
2863 did_skip = TRUE;
2864
2865#ifdef FEAT_FOLDING
2866 /* skip folded lines */
2867 fold_skipped = FALSE;
2868 if (first && hasFolding(curr, &fold_first, &fold_last))
2869 {
2870 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2871 fold_skipped = TRUE;
2872 }
2873#endif
2874
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002875 /* POSIX has it's own ideas of what a paragraph boundary is and it
2876 * doesn't match historical Vi: It also stops at a "{" in the
2877 * first column and at an empty line. */
2878 if (!first && did_skip && (startPS(curr, what, both)
2879 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 break;
2881
2882#ifdef FEAT_FOLDING
2883 if (fold_skipped)
2884 curr -= dir;
2885#endif
2886 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2887 {
2888 if (count)
2889 return FALSE;
2890 curr -= dir;
2891 break;
2892 }
2893 }
2894 }
2895 setpcmark();
2896 if (both && *ml_get(curr) == '}') /* include line with '}' */
2897 ++curr;
2898 curwin->w_cursor.lnum = curr;
2899 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2900 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002901 char_u *line = ml_get(curr);
2902
2903 /* Put the cursor on the last character in the last line and make the
2904 * motion inclusive. */
2905 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 {
2907 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002908#ifdef FEAT_MBYTE
2909 curwin->w_cursor.col -=
2910 (*mb_head_off)(line, line + curwin->w_cursor.col);
2911#endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002912 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
2914 }
2915 else
2916 curwin->w_cursor.col = 0;
2917 return TRUE;
2918}
2919
2920/*
2921 * check if the string 's' is a nroff macro that is in option 'opt'
2922 */
2923 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002924inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 char_u *macro;
2927
2928 for (macro = opt; macro[0]; ++macro)
2929 {
2930 /* Accept two characters in the option being equal to two characters
2931 * in the line. A space in the option matches with a space in the
2932 * line or the line having ended. */
2933 if ( (macro[0] == s[0]
2934 || (macro[0] == ' '
2935 && (s[0] == NUL || s[0] == ' ')))
2936 && (macro[1] == s[1]
2937 || ((macro[1] == NUL || macro[1] == ' ')
2938 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2939 break;
2940 ++macro;
2941 if (macro[0] == NUL)
2942 break;
2943 }
2944 return (macro[0] != NUL);
2945}
2946
2947/*
2948 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2949 * If 'para' is '{' or '}' only check for sections.
2950 * If 'both' is TRUE also stop at '}'
2951 */
2952 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002953startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 char_u *s;
2956
2957 s = ml_get(lnum);
2958 if (*s == para || *s == '\f' || (both && *s == '}'))
2959 return TRUE;
2960 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2961 (!para && inmacro(p_para, s + 1))))
2962 return TRUE;
2963 return FALSE;
2964}
2965
2966/*
2967 * The following routines do the word searches performed by the 'w', 'W',
2968 * 'b', 'B', 'e', and 'E' commands.
2969 */
2970
2971/*
2972 * To perform these searches, characters are placed into one of three
2973 * classes, and transitions between classes determine word boundaries.
2974 *
2975 * The classes are:
2976 *
2977 * 0 - white space
2978 * 1 - punctuation
2979 * 2 or higher - keyword characters (letters, digits and underscore)
2980 */
2981
2982static int cls_bigword; /* TRUE for "W", "B" or "E" */
2983
2984/*
2985 * cls() - returns the class of character at curwin->w_cursor
2986 *
2987 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2988 * from class 2 and higher are reported as class 1 since only white space
2989 * boundaries are of interest.
2990 */
2991 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002992cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 int c;
2995
2996 c = gchar_cursor();
2997#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2998 if (p_altkeymap && c == F_BLANK)
2999 return 0;
3000#endif
3001 if (c == ' ' || c == '\t' || c == NUL)
3002 return 0;
3003#ifdef FEAT_MBYTE
3004 if (enc_dbcs != 0 && c > 0xFF)
3005 {
3006 /* If cls_bigword, report multi-byte chars as class 1. */
3007 if (enc_dbcs == DBCS_KOR && cls_bigword)
3008 return 1;
3009
3010 /* process code leading/trailing bytes */
3011 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3012 }
3013 if (enc_utf8)
3014 {
3015 c = utf_class(c);
3016 if (c != 0 && cls_bigword)
3017 return 1;
3018 return c;
3019 }
3020#endif
3021
3022 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3023 if (cls_bigword)
3024 return 1;
3025
3026 if (vim_iswordc(c))
3027 return 2;
3028 return 1;
3029}
3030
3031
3032/*
3033 * fwd_word(count, type, eol) - move forward one word
3034 *
3035 * Returns FAIL if the cursor was already at the end of the file.
3036 * If eol is TRUE, last word stops at end of line (for operators).
3037 */
3038 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003039fwd_word(
3040 long count,
3041 int bigword, /* "W", "E" or "B" */
3042 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043{
3044 int sclass; /* starting class */
3045 int i;
3046 int last_line;
3047
3048#ifdef FEAT_VIRTUALEDIT
3049 curwin->w_cursor.coladd = 0;
3050#endif
3051 cls_bigword = bigword;
3052 while (--count >= 0)
3053 {
3054#ifdef FEAT_FOLDING
3055 /* When inside a range of folded lines, move to the last char of the
3056 * last line. */
3057 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3058 coladvance((colnr_T)MAXCOL);
3059#endif
3060 sclass = cls();
3061
3062 /*
3063 * We always move at least one character, unless on the last
3064 * character in the buffer.
3065 */
3066 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3067 i = inc_cursor();
3068 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3069 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003070 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 return OK;
3072
3073 /*
3074 * Go one char past end of current word (if any)
3075 */
3076 if (sclass != 0)
3077 while (cls() == sclass)
3078 {
3079 i = inc_cursor();
3080 if (i == -1 || (i >= 1 && eol && count == 0))
3081 return OK;
3082 }
3083
3084 /*
3085 * go to next non-white
3086 */
3087 while (cls() == 0)
3088 {
3089 /*
3090 * We'll stop if we land on a blank line
3091 */
3092 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3093 break;
3094
3095 i = inc_cursor();
3096 if (i == -1 || (i >= 1 && eol && count == 0))
3097 return OK;
3098 }
3099 }
3100 return OK;
3101}
3102
3103/*
3104 * bck_word() - move backward 'count' words
3105 *
3106 * If stop is TRUE and we are already on the start of a word, move one less.
3107 *
3108 * Returns FAIL if top of the file was reached.
3109 */
3110 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003111bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
3113 int sclass; /* starting class */
3114
3115#ifdef FEAT_VIRTUALEDIT
3116 curwin->w_cursor.coladd = 0;
3117#endif
3118 cls_bigword = bigword;
3119 while (--count >= 0)
3120 {
3121#ifdef FEAT_FOLDING
3122 /* When inside a range of folded lines, move to the first char of the
3123 * first line. */
3124 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3125 curwin->w_cursor.col = 0;
3126#endif
3127 sclass = cls();
3128 if (dec_cursor() == -1) /* started at start of file */
3129 return FAIL;
3130
3131 if (!stop || sclass == cls() || sclass == 0)
3132 {
3133 /*
3134 * Skip white space before the word.
3135 * Stop on an empty line.
3136 */
3137 while (cls() == 0)
3138 {
3139 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003140 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 goto finished;
3142 if (dec_cursor() == -1) /* hit start of file, stop here */
3143 return OK;
3144 }
3145
3146 /*
3147 * Move backward to start of this word.
3148 */
3149 if (skip_chars(cls(), BACKWARD))
3150 return OK;
3151 }
3152
3153 inc_cursor(); /* overshot - forward one */
3154finished:
3155 stop = FALSE;
3156 }
3157 return OK;
3158}
3159
3160/*
3161 * end_word() - move to the end of the word
3162 *
3163 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3164 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3165 * motion crosses blank lines. When the real vi crosses a blank line in an
3166 * 'e' motion, the cursor is placed on the FIRST character of the next
3167 * non-blank line. The 'E' command, however, works correctly. Since this
3168 * appears to be a bug, I have not duplicated it here.
3169 *
3170 * Returns FAIL if end of the file was reached.
3171 *
3172 * If stop is TRUE and we are already on the end of a word, move one less.
3173 * If empty is TRUE stop on an empty line.
3174 */
3175 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003176end_word(
3177 long count,
3178 int bigword,
3179 int stop,
3180 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 int sclass; /* starting class */
3183
3184#ifdef FEAT_VIRTUALEDIT
3185 curwin->w_cursor.coladd = 0;
3186#endif
3187 cls_bigword = bigword;
3188 while (--count >= 0)
3189 {
3190#ifdef FEAT_FOLDING
3191 /* When inside a range of folded lines, move to the last char of the
3192 * last line. */
3193 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3194 coladvance((colnr_T)MAXCOL);
3195#endif
3196 sclass = cls();
3197 if (inc_cursor() == -1)
3198 return FAIL;
3199
3200 /*
3201 * If we're in the middle of a word, we just have to move to the end
3202 * of it.
3203 */
3204 if (cls() == sclass && sclass != 0)
3205 {
3206 /*
3207 * Move forward to end of the current word
3208 */
3209 if (skip_chars(sclass, FORWARD))
3210 return FAIL;
3211 }
3212 else if (!stop || sclass == 0)
3213 {
3214 /*
3215 * We were at the end of a word. Go to the end of the next word.
3216 * First skip white space, if 'empty' is TRUE, stop at empty line.
3217 */
3218 while (cls() == 0)
3219 {
3220 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003221 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 goto finished;
3223 if (inc_cursor() == -1) /* hit end of file, stop here */
3224 return FAIL;
3225 }
3226
3227 /*
3228 * Move forward to the end of this word.
3229 */
3230 if (skip_chars(cls(), FORWARD))
3231 return FAIL;
3232 }
3233 dec_cursor(); /* overshot - one char backward */
3234finished:
3235 stop = FALSE; /* we move only one word less */
3236 }
3237 return OK;
3238}
3239
3240/*
3241 * Move back to the end of the word.
3242 *
3243 * Returns FAIL if start of the file was reached.
3244 */
3245 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003246bckend_word(
3247 long count,
3248 int bigword, /* TRUE for "B" */
3249 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250{
3251 int sclass; /* starting class */
3252 int i;
3253
3254#ifdef FEAT_VIRTUALEDIT
3255 curwin->w_cursor.coladd = 0;
3256#endif
3257 cls_bigword = bigword;
3258 while (--count >= 0)
3259 {
3260 sclass = cls();
3261 if ((i = dec_cursor()) == -1)
3262 return FAIL;
3263 if (eol && i == 1)
3264 return OK;
3265
3266 /*
3267 * Move backward to before the start of this word.
3268 */
3269 if (sclass != 0)
3270 {
3271 while (cls() == sclass)
3272 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3273 return OK;
3274 }
3275
3276 /*
3277 * Move backward to end of the previous word
3278 */
3279 while (cls() == 0)
3280 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003281 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 break;
3283 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3284 return OK;
3285 }
3286 }
3287 return OK;
3288}
3289
3290/*
3291 * Skip a row of characters of the same class.
3292 * Return TRUE when end-of-file reached, FALSE otherwise.
3293 */
3294 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003295skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296{
3297 while (cls() == cclass)
3298 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3299 return TRUE;
3300 return FALSE;
3301}
3302
3303#ifdef FEAT_TEXTOBJ
3304/*
3305 * Go back to the start of the word or the start of white space
3306 */
3307 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003308back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 int sclass; /* starting class */
3311
3312 sclass = cls();
3313 for (;;)
3314 {
3315 if (curwin->w_cursor.col == 0) /* stop at start of line */
3316 break;
3317 dec_cursor();
3318 if (cls() != sclass) /* stop at start of word */
3319 {
3320 inc_cursor();
3321 break;
3322 }
3323 }
3324}
3325
3326 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003327find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 int c;
3330
3331 while (decl(posp) != -1)
3332 {
3333 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003334 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
3336 incl(posp);
3337 break;
3338 }
3339 }
3340}
3341
3342/*
3343 * Skip count/2 sentences and count/2 separating white spaces.
3344 */
3345 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003346findsent_forward(
3347 long count,
3348 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 while (count--)
3351 {
3352 findsent(FORWARD, 1L);
3353 if (at_start_sent)
3354 find_first_blank(&curwin->w_cursor);
3355 if (count == 0 || at_start_sent)
3356 decl(&curwin->w_cursor);
3357 at_start_sent = !at_start_sent;
3358 }
3359}
3360
3361/*
3362 * Find word under cursor, cursor at end.
3363 * Used while an operator is pending, and in Visual mode.
3364 */
3365 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003366current_word(
3367 oparg_T *oap,
3368 long count,
3369 int include, /* TRUE: include word and white space */
3370 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371{
3372 pos_T start_pos;
3373 pos_T pos;
3374 int inclusive = TRUE;
3375 int include_white = FALSE;
3376
3377 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003378 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003381 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 dec_cursor();
3383
3384 /*
3385 * When Visual mode is not active, or when the VIsual area is only one
3386 * character, select the word and/or white space under the cursor.
3387 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003388 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
3390 /*
3391 * Go to start of current word or white space.
3392 */
3393 back_in_line();
3394 start_pos = curwin->w_cursor;
3395
3396 /*
3397 * If the start is on white space, and white space should be included
3398 * (" word"), or start is not on white space, and white space should
3399 * not be included ("word"), find end of word.
3400 */
3401 if ((cls() == 0) == include)
3402 {
3403 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3404 return FAIL;
3405 }
3406 else
3407 {
3408 /*
3409 * If the start is not on white space, and white space should be
3410 * included ("word "), or start is on white space and white
3411 * space should not be included (" "), find start of word.
3412 * If we end up in the first column of the next line (single char
3413 * word) back up to end of the line.
3414 */
3415 fwd_word(1L, bigword, TRUE);
3416 if (curwin->w_cursor.col == 0)
3417 decl(&curwin->w_cursor);
3418 else
3419 oneleft();
3420
3421 if (include)
3422 include_white = TRUE;
3423 }
3424
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 if (VIsual_active)
3426 {
3427 /* should do something when inclusive == FALSE ! */
3428 VIsual = start_pos;
3429 redraw_curbuf_later(INVERTED); /* update the inversion */
3430 }
3431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
3433 oap->start = start_pos;
3434 oap->motion_type = MCHAR;
3435 }
3436 --count;
3437 }
3438
3439 /*
3440 * When count is still > 0, extend with more objects.
3441 */
3442 while (count > 0)
3443 {
3444 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003445 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 /*
3448 * In Visual mode, with cursor at start: move cursor back.
3449 */
3450 if (decl(&curwin->w_cursor) == -1)
3451 return FAIL;
3452 if (include != (cls() != 0))
3453 {
3454 if (bck_word(1L, bigword, TRUE) == FAIL)
3455 return FAIL;
3456 }
3457 else
3458 {
3459 if (bckend_word(1L, bigword, TRUE) == FAIL)
3460 return FAIL;
3461 (void)incl(&curwin->w_cursor);
3462 }
3463 }
3464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 /*
3467 * Move cursor forward one word and/or white area.
3468 */
3469 if (incl(&curwin->w_cursor) == -1)
3470 return FAIL;
3471 if (include != (cls() == 0))
3472 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003473 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 return FAIL;
3475 /*
3476 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003477 * the first character on the line.
3478 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003480 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 inclusive = FALSE;
3482 }
3483 else
3484 {
3485 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3486 return FAIL;
3487 }
3488 }
3489 --count;
3490 }
3491
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003492 if (include_white && (cls() != 0
3493 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 /*
3496 * If we don't include white space at the end, move the start
3497 * to include some white space there. This makes "daw" work
3498 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003499 * word). Also when "2daw" deletes "word." at the end of the line
3500 * (cursor is at start of next line).
3501 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 */
3503 pos = curwin->w_cursor; /* save cursor position */
3504 curwin->w_cursor = start_pos;
3505 if (oneleft() == OK)
3506 {
3507 back_in_line();
3508 if (cls() == 0 && curwin->w_cursor.col > 0)
3509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (VIsual_active)
3511 VIsual = curwin->w_cursor;
3512 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 oap->start = curwin->w_cursor;
3514 }
3515 }
3516 curwin->w_cursor = pos; /* put cursor back at end */
3517 }
3518
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 if (VIsual_active)
3520 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003521 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 inc_cursor();
3523 if (VIsual_mode == 'V')
3524 {
3525 VIsual_mode = 'v';
3526 redraw_cmdline = TRUE; /* show mode later */
3527 }
3528 }
3529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 oap->inclusive = inclusive;
3531
3532 return OK;
3533}
3534
3535/*
3536 * Find sentence(s) under the cursor, cursor at end.
3537 * When Visual active, extend it by one or more sentences.
3538 */
3539 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003540current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541{
3542 pos_T start_pos;
3543 pos_T pos;
3544 int start_blank;
3545 int c;
3546 int at_start_sent;
3547 long ncount;
3548
3549 start_pos = curwin->w_cursor;
3550 pos = start_pos;
3551 findsent(FORWARD, 1L); /* Find start of next sentence. */
3552
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003554 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003556 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
3558extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003559 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
3561 /*
3562 * Cursor at start of Visual area.
3563 * Find out where we are:
3564 * - in the white space before a sentence
3565 * - in a sentence or just after it
3566 * - at the start of a sentence
3567 */
3568 at_start_sent = TRUE;
3569 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003570 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
3572 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003573 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 {
3575 at_start_sent = FALSE;
3576 break;
3577 }
3578 incl(&pos);
3579 }
3580 if (!at_start_sent)
3581 {
3582 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003583 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 at_start_sent = TRUE; /* exactly at start of sentence */
3585 else
3586 /* inside a sentence, go to its end (start of next) */
3587 findsent(FORWARD, 1L);
3588 }
3589 if (include) /* "as" gets twice as much as "is" */
3590 count *= 2;
3591 while (count--)
3592 {
3593 if (at_start_sent)
3594 find_first_blank(&curwin->w_cursor);
3595 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003596 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 findsent(BACKWARD, 1L);
3598 at_start_sent = !at_start_sent;
3599 }
3600 }
3601 else
3602 {
3603 /*
3604 * Cursor at end of Visual area.
3605 * Find out where we are:
3606 * - just before a sentence
3607 * - just before or in the white space before a sentence
3608 * - in a sentence
3609 */
3610 incl(&pos);
3611 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003612 /* not just before a sentence */
3613 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003616 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003619 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 at_start_sent = TRUE;
3622 break;
3623 }
3624 incl(&pos);
3625 }
3626 if (at_start_sent) /* in the sentence */
3627 findsent(BACKWARD, 1L);
3628 else /* in/before white before a sentence */
3629 curwin->w_cursor = start_pos;
3630 }
3631
3632 if (include) /* "as" gets twice as much as "is" */
3633 count *= 2;
3634 findsent_forward(count, at_start_sent);
3635 if (*p_sel == 'e')
3636 ++curwin->w_cursor.col;
3637 }
3638 return OK;
3639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003642 * If the cursor started on a blank, check if it is just before the start
3643 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003645 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003647 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 {
3649 start_blank = TRUE;
3650 find_first_blank(&start_pos); /* go back to first blank */
3651 }
3652 else
3653 {
3654 start_blank = FALSE;
3655 findsent(BACKWARD, 1L);
3656 start_pos = curwin->w_cursor;
3657 }
3658 if (include)
3659 ncount = count * 2;
3660 else
3661 {
3662 ncount = count;
3663 if (start_blank)
3664 --ncount;
3665 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003666 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 findsent_forward(ncount, TRUE);
3668 else
3669 decl(&curwin->w_cursor);
3670
3671 if (include)
3672 {
3673 /*
3674 * If the blank in front of the sentence is included, exclude the
3675 * blanks at the end of the sentence, go back to the first blank.
3676 * If there are no trailing blanks, try to include leading blanks.
3677 */
3678 if (start_blank)
3679 {
3680 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003681 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3682 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 decl(&curwin->w_cursor);
3684 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003685 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 find_first_blank(&start_pos);
3687 }
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 if (VIsual_active)
3690 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003691 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003692 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 goto extend;
3694 if (*p_sel == 'e')
3695 ++curwin->w_cursor.col;
3696 VIsual = start_pos;
3697 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003698 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 redraw_curbuf_later(INVERTED); /* update the inversion */
3700 }
3701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
3703 /* include a newline after the sentence, if there is one */
3704 if (incl(&curwin->w_cursor) == -1)
3705 oap->inclusive = TRUE;
3706 else
3707 oap->inclusive = FALSE;
3708 oap->start = start_pos;
3709 oap->motion_type = MCHAR;
3710 }
3711 return OK;
3712}
3713
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003714/*
3715 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003716 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003719current_block(
3720 oparg_T *oap,
3721 long count,
3722 int include, /* TRUE == include white space */
3723 int what, /* '(', '{', etc. */
3724 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 pos_T old_pos;
3727 pos_T *pos = NULL;
3728 pos_T start_pos;
3729 pos_T *end_pos;
3730 pos_T old_start, old_end;
3731 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003732 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
3734 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003735 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 old_start = old_end;
3737
3738 /*
3739 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3740 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003741 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 {
3743 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003744 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 while (inindent(1))
3746 if (inc_cursor() != 0)
3747 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003748 if (gchar_cursor() == what)
3749 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 ++curwin->w_cursor.col;
3751 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003752 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
3754 old_start = VIsual;
3755 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3756 }
3757 else
3758 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
3760 /*
3761 * Search backwards for unclosed '(', '{', etc..
3762 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003763 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3764 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
3766 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003767 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 while (count-- > 0)
3769 {
3770 if ((pos = findmatch(NULL, what)) == NULL)
3771 break;
3772 curwin->w_cursor = *pos;
3773 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3774 }
3775 p_cpo = save_cpo;
3776
3777 /*
3778 * Search for matching ')', '}', etc.
3779 * Put this position in curwin->w_cursor.
3780 */
3781 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3782 {
3783 curwin->w_cursor = old_pos;
3784 return FAIL;
3785 }
3786 curwin->w_cursor = *end_pos;
3787
3788 /*
3789 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003790 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3791 * indent. But only if the resulting area is not smaller than what we
3792 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 */
3794 while (!include)
3795 {
3796 incl(&start_pos);
3797 sol = (curwin->w_cursor.col == 0);
3798 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003799 while (inindent(1))
3800 {
3801 sol = TRUE;
3802 if (decl(&curwin->w_cursor) != 0)
3803 break;
3804 }
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 /*
3807 * In Visual mode, when the resulting area is not bigger than what we
3808 * started with, extend it to the next block, and then exclude again.
3809 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003810 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 && VIsual_active)
3812 {
3813 curwin->w_cursor = old_start;
3814 decl(&curwin->w_cursor);
3815 if ((pos = findmatch(NULL, what)) == NULL)
3816 {
3817 curwin->w_cursor = old_pos;
3818 return FAIL;
3819 }
3820 start_pos = *pos;
3821 curwin->w_cursor = *pos;
3822 if ((end_pos = findmatch(NULL, other)) == NULL)
3823 {
3824 curwin->w_cursor = old_pos;
3825 return FAIL;
3826 }
3827 curwin->w_cursor = *end_pos;
3828 }
3829 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 break;
3831 }
3832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (VIsual_active)
3834 {
3835 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003836 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003837 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 inc(&curwin->w_cursor); /* include the line break */
3839 VIsual = start_pos;
3840 VIsual_mode = 'v';
3841 redraw_curbuf_later(INVERTED); /* update the inversion */
3842 showmode();
3843 }
3844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 oap->start = start_pos;
3847 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003848 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003851 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003852 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003854 else
3855 /* End is before the start (no text in between <>, [], etc.): don't
3856 * operate on any text. */
3857 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 }
3859
3860 return OK;
3861}
3862
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003863static int in_html_tag(int);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003864
3865/*
3866 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3867 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3868 */
3869 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003870in_html_tag(
3871 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003872{
3873 char_u *line = ml_get_curline();
3874 char_u *p;
3875 int c;
3876 int lc = NUL;
3877 pos_T pos;
3878
3879#ifdef FEAT_MBYTE
3880 if (enc_dbcs)
3881 {
3882 char_u *lp = NULL;
3883
3884 /* We search forward until the cursor, because searching backwards is
3885 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003886 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003887 if (*p == '>' || *p == '<')
3888 {
3889 lc = *p;
3890 lp = p;
3891 }
3892 if (*p != '<') /* check for '<' under cursor */
3893 {
3894 if (lc != '<')
3895 return FALSE;
3896 p = lp;
3897 }
3898 }
3899 else
3900#endif
3901 {
3902 for (p = line + curwin->w_cursor.col; p > line; )
3903 {
3904 if (*p == '<') /* find '<' under/before cursor */
3905 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003906 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003907 if (*p == '>') /* find '>' before cursor */
3908 break;
3909 }
3910 if (*p != '<')
3911 return FALSE;
3912 }
3913
3914 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003915 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003916
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003917 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003918 if (end_tag)
3919 /* check that there is a '/' after the '<' */
3920 return *p == '/';
3921
3922 /* check that there is no '/' after the '<' */
3923 if (*p == '/')
3924 return FALSE;
3925
3926 /* check that the matching '>' is not preceded by '/' */
3927 for (;;)
3928 {
3929 if (inc(&pos) < 0)
3930 return FALSE;
3931 c = *ml_get_pos(&pos);
3932 if (c == '>')
3933 break;
3934 lc = c;
3935 }
3936 return lc != '/';
3937}
3938
3939/*
3940 * Find tag block under the cursor, cursor at end.
3941 */
3942 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003943current_tagblock(
3944 oparg_T *oap,
3945 long count_arg,
3946 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003947{
3948 long count = count_arg;
3949 long n;
3950 pos_T old_pos;
3951 pos_T start_pos;
3952 pos_T end_pos;
3953 pos_T old_start, old_end;
3954 char_u *spat, *epat;
3955 char_u *p;
3956 char_u *cp;
3957 int len;
3958 int r;
3959 int do_include = include;
3960 int save_p_ws = p_ws;
3961 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003962 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003963
3964 p_ws = FALSE;
3965
3966 old_pos = curwin->w_cursor;
3967 old_end = curwin->w_cursor; /* remember where we started */
3968 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003969 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003970 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003971
3972 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003973 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003974 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003975 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003976 {
3977 setpcmark();
3978
3979 /* ignore indent */
3980 while (inindent(1))
3981 if (inc_cursor() != 0)
3982 break;
3983
3984 if (in_html_tag(FALSE))
3985 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003986 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003987 while (*ml_get_cursor() != '>')
3988 if (inc_cursor() < 0)
3989 break;
3990 }
3991 else if (in_html_tag(TRUE))
3992 {
3993 /* cursor on end tag, move to just before it */
3994 while (*ml_get_cursor() != '<')
3995 if (dec_cursor() < 0)
3996 break;
3997 dec_cursor();
3998 old_end = curwin->w_cursor;
3999 }
4000 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004001 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004002 {
4003 old_start = VIsual;
4004 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4005 }
4006 else
4007 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004008
4009again:
4010 /*
4011 * Search backwards for unclosed "<aaa>".
4012 * Put this position in start_pos.
4013 */
4014 for (n = 0; n < count; ++n)
4015 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004016 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004017 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004018 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004019 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004020 {
4021 curwin->w_cursor = old_pos;
4022 goto theend;
4023 }
4024 }
4025 start_pos = curwin->w_cursor;
4026
4027 /*
4028 * Search for matching "</aaa>". First isolate the "aaa".
4029 */
4030 inc_cursor();
4031 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004032 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004033 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004034 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004035 if (len == 0)
4036 {
4037 curwin->w_cursor = old_pos;
4038 goto theend;
4039 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004040 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004041 epat = alloc(len + 9);
4042 if (spat == NULL || epat == NULL)
4043 {
4044 vim_free(spat);
4045 vim_free(epat);
4046 curwin->w_cursor = old_pos;
4047 goto theend;
4048 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004049 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004050 sprintf((char *)epat, "</%.*s>\\c", len, p);
4051
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004052 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
Bram Moolenaar76929292008-01-06 19:07:36 +00004053 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004054
4055 vim_free(spat);
4056 vim_free(epat);
4057
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004058 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004059 {
4060 /* Can't find other end or it's before the previous end. Could be a
4061 * HTML tag that doesn't have a matching end. Search backwards for
4062 * another starting tag. */
4063 count = 1;
4064 curwin->w_cursor = start_pos;
4065 goto again;
4066 }
4067
4068 if (do_include || r < 1)
4069 {
4070 /* Include up to the '>'. */
4071 while (*ml_get_cursor() != '>')
4072 if (inc_cursor() < 0)
4073 break;
4074 }
4075 else
4076 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004077 char_u *c = ml_get_cursor();
4078
4079 /* Exclude the '<' of the end tag.
4080 * If the closing tag is on new line, do not decrement cursor, but
4081 * make operation exclusive, so that the linefeed will be selected */
4082 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4083 /* do not decrement cursor */
4084 is_inclusive = FALSE;
4085 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004086 dec_cursor();
4087 }
4088 end_pos = curwin->w_cursor;
4089
4090 if (!do_include)
4091 {
4092 /* Exclude the start tag. */
4093 curwin->w_cursor = start_pos;
4094 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004095 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004096 {
4097 inc_cursor();
4098 start_pos = curwin->w_cursor;
4099 break;
4100 }
4101 curwin->w_cursor = end_pos;
4102
Bram Moolenaar45360022005-07-21 21:08:21 +00004103 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004104 * again. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004105 if (EQUAL_POS(start_pos, old_start) && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004106 {
4107 do_include = TRUE;
4108 curwin->w_cursor = old_start;
4109 count = count_arg;
4110 goto again;
4111 }
4112 }
4113
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004114 if (VIsual_active)
4115 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004116 /* If the end is before the start there is no text between tags, select
4117 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004118 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004119 curwin->w_cursor = start_pos;
4120 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004121 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004122 VIsual = start_pos;
4123 VIsual_mode = 'v';
4124 redraw_curbuf_later(INVERTED); /* update the inversion */
4125 showmode();
4126 }
4127 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004128 {
4129 oap->start = start_pos;
4130 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004131 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004132 {
4133 /* End is before the start: there is no text between tags; operate
4134 * on an empty area. */
4135 curwin->w_cursor = start_pos;
4136 oap->inclusive = FALSE;
4137 }
4138 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004139 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004140 }
4141 retval = OK;
4142
4143theend:
4144 p_ws = save_p_ws;
4145 return retval;
4146}
4147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004149current_par(
4150 oparg_T *oap,
4151 long count,
4152 int include, /* TRUE == include white space */
4153 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154{
4155 linenr_T start_lnum;
4156 linenr_T end_lnum;
4157 int white_in_front;
4158 int dir;
4159 int start_is_white;
4160 int prev_start_is_white;
4161 int retval = OK;
4162 int do_white = FALSE;
4163 int t;
4164 int i;
4165
4166 if (type == 'S') /* not implemented yet */
4167 return FAIL;
4168
4169 start_lnum = curwin->w_cursor.lnum;
4170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 /*
4172 * When visual area is more than one line: extend it.
4173 */
4174 if (VIsual_active && start_lnum != VIsual.lnum)
4175 {
4176extend:
4177 if (start_lnum < VIsual.lnum)
4178 dir = BACKWARD;
4179 else
4180 dir = FORWARD;
4181 for (i = count; --i >= 0; )
4182 {
4183 if (start_lnum ==
4184 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4185 {
4186 retval = FAIL;
4187 break;
4188 }
4189
4190 prev_start_is_white = -1;
4191 for (t = 0; t < 2; ++t)
4192 {
4193 start_lnum += dir;
4194 start_is_white = linewhite(start_lnum);
4195 if (prev_start_is_white == start_is_white)
4196 {
4197 start_lnum -= dir;
4198 break;
4199 }
4200 for (;;)
4201 {
4202 if (start_lnum == (dir == BACKWARD
4203 ? 1 : curbuf->b_ml.ml_line_count))
4204 break;
4205 if (start_is_white != linewhite(start_lnum + dir)
4206 || (!start_is_white
4207 && startPS(start_lnum + (dir > 0
4208 ? 1 : 0), 0, 0)))
4209 break;
4210 start_lnum += dir;
4211 }
4212 if (!include)
4213 break;
4214 if (start_lnum == (dir == BACKWARD
4215 ? 1 : curbuf->b_ml.ml_line_count))
4216 break;
4217 prev_start_is_white = start_is_white;
4218 }
4219 }
4220 curwin->w_cursor.lnum = start_lnum;
4221 curwin->w_cursor.col = 0;
4222 return retval;
4223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 /*
4226 * First move back to the start_lnum of the paragraph or white lines
4227 */
4228 white_in_front = linewhite(start_lnum);
4229 while (start_lnum > 1)
4230 {
4231 if (white_in_front) /* stop at first white line */
4232 {
4233 if (!linewhite(start_lnum - 1))
4234 break;
4235 }
4236 else /* stop at first non-white line of start of paragraph */
4237 {
4238 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4239 break;
4240 }
4241 --start_lnum;
4242 }
4243
4244 /*
4245 * Move past the end of any white lines.
4246 */
4247 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004248 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4249 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 --end_lnum;
4252 i = count;
4253 if (!include && white_in_front)
4254 --i;
4255 while (i--)
4256 {
4257 if (end_lnum == curbuf->b_ml.ml_line_count)
4258 return FAIL;
4259
4260 if (!include)
4261 do_white = linewhite(end_lnum + 1);
4262
4263 if (include || !do_white)
4264 {
4265 ++end_lnum;
4266 /*
4267 * skip to end of paragraph
4268 */
4269 while (end_lnum < curbuf->b_ml.ml_line_count
4270 && !linewhite(end_lnum + 1)
4271 && !startPS(end_lnum + 1, 0, 0))
4272 ++end_lnum;
4273 }
4274
4275 if (i == 0 && white_in_front && include)
4276 break;
4277
4278 /*
4279 * skip to end of white lines after paragraph
4280 */
4281 if (include || do_white)
4282 while (end_lnum < curbuf->b_ml.ml_line_count
4283 && linewhite(end_lnum + 1))
4284 ++end_lnum;
4285 }
4286
4287 /*
4288 * If there are no empty lines at the end, try to find some empty lines at
4289 * the start (unless that has been done already).
4290 */
4291 if (!white_in_front && !linewhite(end_lnum) && include)
4292 while (start_lnum > 1 && linewhite(start_lnum - 1))
4293 --start_lnum;
4294
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 if (VIsual_active)
4296 {
4297 /* Problem: when doing "Vipipip" nothing happens in a single white
4298 * line, we get stuck there. Trap this here. */
4299 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4300 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004301 if (VIsual.lnum != start_lnum)
4302 {
4303 VIsual.lnum = start_lnum;
4304 VIsual.col = 0;
4305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 VIsual_mode = 'V';
4307 redraw_curbuf_later(INVERTED); /* update the inversion */
4308 showmode();
4309 }
4310 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 oap->start.lnum = start_lnum;
4313 oap->start.col = 0;
4314 oap->motion_type = MLINE;
4315 }
4316 curwin->w_cursor.lnum = end_lnum;
4317 curwin->w_cursor.col = 0;
4318
4319 return OK;
4320}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004321
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004322static int find_next_quote(char_u *top_ptr, int col, int quotechar, char_u *escape);
4323static int find_prev_quote(char_u *line, int col_start, int quotechar, char_u *escape);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004324
4325/*
4326 * Search quote char from string line[col].
4327 * Quote character escaped by one of the characters in "escape" is not counted
4328 * as a quote.
4329 * Returns column number of "quotechar" or -1 when not found.
4330 */
4331 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004332find_next_quote(
4333 char_u *line,
4334 int col,
4335 int quotechar,
4336 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004337{
4338 int c;
4339
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004340 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004341 {
4342 c = line[col];
4343 if (c == NUL)
4344 return -1;
4345 else if (escape != NULL && vim_strchr(escape, c))
4346 ++col;
4347 else if (c == quotechar)
4348 break;
4349#ifdef FEAT_MBYTE
4350 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004351 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004352 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004354 ++col;
4355 }
4356 return col;
4357}
4358
4359/*
4360 * Search backwards in "line" from column "col_start" to find "quotechar".
4361 * Quote character escaped by one of the characters in "escape" is not counted
4362 * as a quote.
4363 * Return the found column or zero.
4364 */
4365 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004366find_prev_quote(
4367 char_u *line,
4368 int col_start,
4369 int quotechar,
4370 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004371{
4372 int n;
4373
4374 while (col_start > 0)
4375 {
4376 --col_start;
4377#ifdef FEAT_MBYTE
4378 col_start -= (*mb_head_off)(line, line + col_start);
4379#endif
4380 n = 0;
4381 if (escape != NULL)
4382 while (col_start - n > 0 && vim_strchr(escape,
4383 line[col_start - n - 1]) != NULL)
4384 ++n;
4385 if (n & 1)
4386 col_start -= n; /* uneven number of escape chars, skip it */
4387 else if (line[col_start] == quotechar)
4388 break;
4389 }
4390 return col_start;
4391}
4392
4393/*
4394 * Find quote under the cursor, cursor at end.
4395 * Returns TRUE if found, else FALSE.
4396 */
4397 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004398current_quote(
4399 oparg_T *oap,
4400 long count,
4401 int include, /* TRUE == include quote char */
4402 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004403{
4404 char_u *line = ml_get_curline();
4405 int col_end;
4406 int col_start = curwin->w_cursor.col;
4407 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004408 int vis_empty = TRUE; /* Visual selection <= 1 char */
4409 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004410 int inside_quotes = FALSE; /* Looks like "i'" done before */
4411 int selected_quote = FALSE; /* Has quote inside selection */
4412 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004413
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004414 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004415 if (VIsual_active)
4416 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004417 /* this only works within one line */
4418 if (VIsual.lnum != curwin->w_cursor.lnum)
4419 return FALSE;
4420
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004421 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004422 if (*p_sel == 'e')
4423 {
4424 if (!vis_bef_curs)
4425 {
4426 /* VIsual needs to be start of Visual selection. */
4427 pos_T t = curwin->w_cursor;
4428
4429 curwin->w_cursor = VIsual;
4430 VIsual = t;
4431 vis_bef_curs = TRUE;
4432 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004433 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004434 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004435 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004436 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004437
4438 if (!vis_empty)
4439 {
4440 /* Check if the existing selection exactly spans the text inside
4441 * quotes. */
4442 if (vis_bef_curs)
4443 {
4444 inside_quotes = VIsual.col > 0
4445 && line[VIsual.col - 1] == quotechar
4446 && line[curwin->w_cursor.col] != NUL
4447 && line[curwin->w_cursor.col + 1] == quotechar;
4448 i = VIsual.col;
4449 col_end = curwin->w_cursor.col;
4450 }
4451 else
4452 {
4453 inside_quotes = curwin->w_cursor.col > 0
4454 && line[curwin->w_cursor.col - 1] == quotechar
4455 && line[VIsual.col] != NUL
4456 && line[VIsual.col + 1] == quotechar;
4457 i = curwin->w_cursor.col;
4458 col_end = VIsual.col;
4459 }
4460
4461 /* Find out if we have a quote in the selection. */
4462 while (i <= col_end)
4463 if (line[i++] == quotechar)
4464 {
4465 selected_quote = TRUE;
4466 break;
4467 }
4468 }
4469
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004470 if (!vis_empty && line[col_start] == quotechar)
4471 {
4472 /* Already selecting something and on a quote character. Find the
4473 * next quoted string. */
4474 if (vis_bef_curs)
4475 {
4476 /* Assume we are on a closing quote: move to after the next
4477 * opening quote. */
4478 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4479 if (col_start < 0)
4480 return FALSE;
4481 col_end = find_next_quote(line, col_start + 1, quotechar,
4482 curbuf->b_p_qe);
4483 if (col_end < 0)
4484 {
4485 /* We were on a starting quote perhaps? */
4486 col_end = col_start;
4487 col_start = curwin->w_cursor.col;
4488 }
4489 }
4490 else
4491 {
4492 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4493 if (line[col_end] != quotechar)
4494 return FALSE;
4495 col_start = find_prev_quote(line, col_end, quotechar,
4496 curbuf->b_p_qe);
4497 if (line[col_start] != quotechar)
4498 {
4499 /* We were on an ending quote perhaps? */
4500 col_start = col_end;
4501 col_end = curwin->w_cursor.col;
4502 }
4503 }
4504 }
4505 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004506
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004507 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004508 {
4509 int first_col = col_start;
4510
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004511 if (!vis_empty)
4512 {
4513 if (vis_bef_curs)
4514 first_col = find_next_quote(line, col_start, quotechar, NULL);
4515 else
4516 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4517 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004518
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004519 /* The cursor is on a quote, we don't know if it's the opening or
4520 * closing quote. Search from the start of the line to find out.
4521 * Also do this when there is a Visual area, a' may leave the cursor
4522 * in between two strings. */
4523 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004524 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004525 {
4526 /* Find open quote character. */
4527 col_start = find_next_quote(line, col_start, quotechar, NULL);
4528 if (col_start < 0 || col_start > first_col)
4529 return FALSE;
4530 /* Find close quote character. */
4531 col_end = find_next_quote(line, col_start + 1, quotechar,
4532 curbuf->b_p_qe);
4533 if (col_end < 0)
4534 return FALSE;
4535 /* If is cursor between start and end quote character, it is
4536 * target text object. */
4537 if (col_start <= first_col && first_col <= col_end)
4538 break;
4539 col_start = col_end + 1;
4540 }
4541 }
4542 else
4543 {
4544 /* Search backward for a starting quote. */
4545 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4546 if (line[col_start] != quotechar)
4547 {
4548 /* No quote before the cursor, look after the cursor. */
4549 col_start = find_next_quote(line, col_start, quotechar, NULL);
4550 if (col_start < 0)
4551 return FALSE;
4552 }
4553
4554 /* Find close quote character. */
4555 col_end = find_next_quote(line, col_start + 1, quotechar,
4556 curbuf->b_p_qe);
4557 if (col_end < 0)
4558 return FALSE;
4559 }
4560
4561 /* When "include" is TRUE, include spaces after closing quote or before
4562 * the starting quote. */
4563 if (include)
4564 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004565 if (VIM_ISWHITE(line[col_end + 1]))
4566 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004567 ++col_end;
4568 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004569 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004570 --col_start;
4571 }
4572
Bram Moolenaarab194812005-09-14 21:40:12 +00004573 /* Set start position. After vi" another i" must include the ".
4574 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004575 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004576 ++col_start;
4577 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004578 if (VIsual_active)
4579 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004580 /* Set the start of the Visual area when the Visual area was empty, we
4581 * were just inside quotes or the Visual area didn't start at a quote
4582 * and didn't include a quote.
4583 */
4584 if (vis_empty
4585 || (vis_bef_curs
4586 && !selected_quote
4587 && (inside_quotes
4588 || (line[VIsual.col] != quotechar
4589 && (VIsual.col == 0
4590 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004591 {
4592 VIsual = curwin->w_cursor;
4593 redraw_curbuf_later(INVERTED);
4594 }
4595 }
4596 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004597 {
4598 oap->start = curwin->w_cursor;
4599 oap->motion_type = MCHAR;
4600 }
4601
4602 /* Set end position. */
4603 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004604 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004605 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004606 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004607 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004608 if (VIsual_active)
4609 {
4610 if (vis_empty || vis_bef_curs)
4611 {
4612 /* decrement cursor when 'selection' is not exclusive */
4613 if (*p_sel != 'e')
4614 dec_cursor();
4615 }
4616 else
4617 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004618 /* Cursor is at start of Visual area. Set the end of the Visual
4619 * area when it was just inside quotes or it didn't end at a
4620 * quote. */
4621 if (inside_quotes
4622 || (!selected_quote
4623 && line[VIsual.col] != quotechar
4624 && (line[VIsual.col] == NUL
4625 || line[VIsual.col + 1] != quotechar)))
4626 {
4627 dec_cursor();
4628 VIsual = curwin->w_cursor;
4629 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004630 curwin->w_cursor.col = col_start;
4631 }
4632 if (VIsual_mode == 'V')
4633 {
4634 VIsual_mode = 'v';
4635 redraw_cmdline = TRUE; /* show mode later */
4636 }
4637 }
4638 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004639 {
4640 /* Set inclusive and other oap's flags. */
4641 oap->inclusive = inclusive;
4642 }
4643
4644 return OK;
4645}
4646
4647#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004649static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004650
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004651/*
4652 * Find next search match under cursor, cursor at end.
4653 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004654 */
4655 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004656current_search(
4657 long count,
4658 int forward) /* move forward or backwards */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004659{
4660 pos_T start_pos; /* position before the pattern */
4661 pos_T orig_pos; /* position of the cursor at beginning */
4662 pos_T pos; /* position after the pattern */
4663 int i;
4664 int dir;
4665 int result; /* result of various function calls */
4666 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004667 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004668 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004669 int one_char;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004670 int direction = forward ? FORWARD : BACKWARD;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004671
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004672 /* wrapping should not occur */
4673 p_ws = FALSE;
4674
4675 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004676 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004677 dec_cursor();
4678
4679 if (VIsual_active)
4680 {
4681 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004682
4683 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004684
4685 /* make sure, searching further will extend the match */
4686 if (VIsual_active)
4687 {
4688 if (forward)
4689 incl(&pos);
4690 else
4691 decl(&pos);
4692 }
4693 }
4694 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004695 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004696
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004697 /* Is the pattern is zero-width?, this time, don't care about the direction
4698 */
4699 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4700 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004701 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004702 {
4703 p_ws = old_p_ws;
4704 return FAIL; /* pattern not found */
4705 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004706
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004707 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004708 * The trick is to first search backwards and then search forward again,
4709 * so that a match at the current cursor position will be correctly
4710 * captured.
4711 */
4712 for (i = 0; i < 2; i++)
4713 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004714 if (forward)
4715 dir = i;
4716 else
4717 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004718
4719 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004720 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004721 flags = SEARCH_END;
4722
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004723 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4724 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004725 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004726
4727 /* First search may fail, but then start searching from the
4728 * beginning of the file (cursor might be on the search match)
4729 * except when Visual mode is active, so that extending the visual
4730 * selection works. */
4731 if (!result && i) /* not found, abort */
4732 {
4733 curwin->w_cursor = orig_pos;
4734 if (VIsual_active)
4735 VIsual = save_VIsual;
4736 p_ws = old_p_ws;
4737 return FAIL;
4738 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004739 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004740 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004741 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004742 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004743 /* try again from start of buffer */
4744 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004745 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004746 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004747 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004748 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004749 /* searching backwards, so set pos to last line and col */
4750 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004751 pos.col = (colnr_T)STRLEN(
4752 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004753 }
4754 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004755 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004756 }
4757
4758 start_pos = pos;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004759 flags = forward ? SEARCH_END : SEARCH_START;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004760
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004761 /* Check again from the current cursor position,
4762 * since the next match might actually by only one char wide */
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004763 one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004764 if (one_char < 0)
4765 /* search failed, abort */
4766 return FAIL;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004767
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004768 /* move to match, except for zero-width matches, in which case, we are
4769 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004770 if (!one_char)
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004771 result = searchit(curwin, curbuf, &pos, direction,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004772 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
4773 NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004774
4775 if (!VIsual_active)
4776 VIsual = start_pos;
4777
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004778 curwin->w_cursor = pos;
4779 VIsual_active = TRUE;
4780 VIsual_mode = 'v';
4781
4782 if (VIsual_active)
4783 {
4784 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004785 if (*p_sel == 'e')
4786 {
4787 /* Correction for exclusive selection depends on the direction. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004788 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004789 inc_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004790 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004791 inc(&VIsual);
4792 }
4793
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004794 }
4795
4796#ifdef FEAT_FOLDING
4797 if (fdo_flags & FDO_SEARCH && KeyTyped)
4798 foldOpenCursor();
4799#endif
4800
4801 may_start_select('c');
4802#ifdef FEAT_MOUSE
4803 setmouse();
4804#endif
4805#ifdef FEAT_CLIPBOARD
4806 /* Make sure the clipboard gets updated. Needed because start and
4807 * end are still the same, and the selection needs to be owned */
4808 clip_star.vmode = NUL;
4809#endif
4810 redraw_curbuf_later(INVERTED);
4811 showmode();
4812
4813 return OK;
4814}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004815
4816/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004817 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004818 * If move is TRUE, check from the beginning of the buffer, else from position
4819 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004820 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004821 * Returns TRUE, FALSE or -1 for failure.
4822 */
4823 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004824is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004825{
4826 regmmatch_T regmatch;
4827 int nmatched = 0;
4828 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004829 pos_T pos;
4830 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004831 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004832
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004833 if (pattern == NULL)
4834 pattern = spats[last_idx].pat;
4835
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004836 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4837 SEARCH_KEEP, &regmatch) == FAIL)
4838 return -1;
4839
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004840 /* init startcol correctly */
4841 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004842 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004843 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004844 {
4845 CLEAR_POS(&pos);
4846 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004847 else
4848 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004849 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004850 /* accept a match at the cursor position */
4851 flag = SEARCH_START;
4852 }
4853
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004854 if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004855 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004856 {
4857 /* Zero-width pattern should match somewhere, then we can check if
4858 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004859 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004860 do
4861 {
4862 regmatch.startpos[0].col++;
4863 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004864 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004865 if (!nmatched)
4866 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004867 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4868 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004869
4870 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004871 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004872 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004873 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4874 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004875 /* one char width */
4876 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4877 result = TRUE;
4878 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004879 }
4880
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004881 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004882 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004883 return result;
4884}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004885
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4887 || defined(PROTO)
4888/*
4889 * return TRUE if line 'lnum' is empty or has white chars only.
4890 */
4891 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004892linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
4894 char_u *p;
4895
4896 p = skipwhite(ml_get(lnum));
4897 return (*p == NUL);
4898}
4899#endif
4900
4901#if defined(FEAT_FIND_ID) || defined(PROTO)
4902/*
4903 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004904 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004907find_pattern_in_path(
4908 char_u *ptr, /* pointer to search pattern */
4909 int dir UNUSED, /* direction of expansion */
4910 int len, /* length of search pattern */
4911 int whole, /* match whole words only */
4912 int skip_comments, /* don't match inside comments */
4913 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004915 long count,
4916 int action, /* What to do when we find it */
4917 linenr_T start_lnum, /* first line to start searching */
4918 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 SearchedFile *files; /* Stack of included files */
4921 SearchedFile *bigger; /* When we need more space */
4922 int max_path_depth = 50;
4923 long match_count = 1;
4924
4925 char_u *pat;
4926 char_u *new_fname;
4927 char_u *curr_fname = curbuf->b_fname;
4928 char_u *prev_fname = NULL;
4929 linenr_T lnum;
4930 int depth;
4931 int depth_displayed; /* For type==CHECK_PATH */
4932 int old_files;
4933 int already_searched;
4934 char_u *file_line;
4935 char_u *line;
4936 char_u *p;
4937 char_u save_char;
4938 int define_matched;
4939 regmatch_T regmatch;
4940 regmatch_T incl_regmatch;
4941 regmatch_T def_regmatch;
4942 int matched = FALSE;
4943 int did_show = FALSE;
4944 int found = FALSE;
4945 int i;
4946 char_u *already = NULL;
4947 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004948 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004949#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 win_T *curwin_save = NULL;
4951#endif
4952
4953 regmatch.regprog = NULL;
4954 incl_regmatch.regprog = NULL;
4955 def_regmatch.regprog = NULL;
4956
4957 file_line = alloc(LSIZE);
4958 if (file_line == NULL)
4959 return;
4960
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (type != CHECK_PATH && type != FIND_DEFINE
4962#ifdef FEAT_INS_EXPAND
4963 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4964 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004965 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
4967 )
4968 {
4969 pat = alloc(len + 5);
4970 if (pat == NULL)
4971 goto fpip_end;
4972 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4973 /* ignore case according to p_ic, p_scs and pat */
4974 regmatch.rm_ic = ignorecase(pat);
4975 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4976 vim_free(pat);
4977 if (regmatch.regprog == NULL)
4978 goto fpip_end;
4979 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004980 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4981 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004983 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 if (incl_regmatch.regprog == NULL)
4985 goto fpip_end;
4986 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4987 }
4988 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4989 {
4990 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4991 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4992 if (def_regmatch.regprog == NULL)
4993 goto fpip_end;
4994 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4995 }
4996 files = (SearchedFile *)lalloc_clear((long_u)
4997 (max_path_depth * sizeof(SearchedFile)), TRUE);
4998 if (files == NULL)
4999 goto fpip_end;
5000 old_files = max_path_depth;
5001 depth = depth_displayed = -1;
5002
5003 lnum = start_lnum;
5004 if (end_lnum > curbuf->b_ml.ml_line_count)
5005 end_lnum = curbuf->b_ml.ml_line_count;
5006 if (lnum > end_lnum) /* do at least one line */
5007 lnum = end_lnum;
5008 line = ml_get(lnum);
5009
5010 for (;;)
5011 {
5012 if (incl_regmatch.regprog != NULL
5013 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5014 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005015 char_u *p_fname = (curr_fname == curbuf->b_fname)
5016 ? curbuf->b_ffname : curr_fname;
5017
5018 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5019 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5020 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005021 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005022 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5023 else
5024 /* Use text after match with 'include'. */
5025 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005026 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 already_searched = FALSE;
5028 if (new_fname != NULL)
5029 {
5030 /* Check whether we have already searched in this file */
5031 for (i = 0;; i++)
5032 {
5033 if (i == depth + 1)
5034 i = old_files;
5035 if (i == max_path_depth)
5036 break;
5037 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5038 {
5039 if (type != CHECK_PATH &&
5040 action == ACTION_SHOW_ALL && files[i].matched)
5041 {
5042 msg_putchar('\n'); /* cursor below last one */
5043 if (!got_int) /* don't display if 'q'
5044 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005045 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 msg_home_replace_hl(new_fname);
5048 MSG_PUTS(_(" (includes previously listed match)"));
5049 prev_fname = NULL;
5050 }
5051 }
5052 vim_free(new_fname);
5053 new_fname = NULL;
5054 already_searched = TRUE;
5055 break;
5056 }
5057 }
5058 }
5059
5060 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5061 || (new_fname == NULL && !already_searched)))
5062 {
5063 if (did_show)
5064 msg_putchar('\n'); /* cursor below last one */
5065 else
5066 {
5067 gotocmdline(TRUE); /* cursor at status line */
5068 MSG_PUTS_TITLE(_("--- Included files "));
5069 if (action != ACTION_SHOW_ALL)
5070 MSG_PUTS_TITLE(_("not found "));
5071 MSG_PUTS_TITLE(_("in path ---\n"));
5072 }
5073 did_show = TRUE;
5074 while (depth_displayed < depth && !got_int)
5075 {
5076 ++depth_displayed;
5077 for (i = 0; i < depth_displayed; i++)
5078 MSG_PUTS(" ");
5079 msg_home_replace(files[depth_displayed].name);
5080 MSG_PUTS(" -->\n");
5081 }
5082 if (!got_int) /* don't display if 'q' typed
5083 for "--more--" message */
5084 {
5085 for (i = 0; i <= depth_displayed; i++)
5086 MSG_PUTS(" ");
5087 if (new_fname != NULL)
5088 {
5089 /* using "new_fname" is more reliable, e.g., when
5090 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005091 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 else
5094 {
5095 /*
5096 * Isolate the file name.
5097 * Include the surrounding "" or <> if present.
5098 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005099 if (inc_opt != NULL
5100 && strstr((char *)inc_opt, "\\zs") != NULL)
5101 {
5102 /* pattern contains \zs, use the match */
5103 p = incl_regmatch.startp[0];
5104 i = (int)(incl_regmatch.endp[0]
5105 - incl_regmatch.startp[0]);
5106 }
5107 else
5108 {
5109 /* find the file name after the end of the match */
5110 for (p = incl_regmatch.endp[0];
5111 *p && !vim_isfilec(*p); p++)
5112 ;
5113 for (i = 0; vim_isfilec(p[i]); i++)
5114 ;
5115 }
5116
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (i == 0)
5118 {
5119 /* Nothing found, use the rest of the line. */
5120 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005121 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005123 /* Avoid checking before the start of the line, can
5124 * happen if \zs appears in the regexp. */
5125 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 if (p[-1] == '"' || p[-1] == '<')
5128 {
5129 --p;
5130 ++i;
5131 }
5132 if (p[i] == '"' || p[i] == '>')
5133 ++i;
5134 }
5135 save_char = p[i];
5136 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005137 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 p[i] = save_char;
5139 }
5140
5141 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5142 {
5143 if (already_searched)
5144 MSG_PUTS(_(" (Already listed)"));
5145 else
5146 MSG_PUTS(_(" NOT FOUND"));
5147 }
5148 }
5149 out_flush(); /* output each line directly */
5150 }
5151
5152 if (new_fname != NULL)
5153 {
5154 /* Push the new file onto the file stack */
5155 if (depth + 1 == old_files)
5156 {
5157 bigger = (SearchedFile *)lalloc((long_u)(
5158 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5159 if (bigger != NULL)
5160 {
5161 for (i = 0; i <= depth; i++)
5162 bigger[i] = files[i];
5163 for (i = depth + 1; i < old_files + max_path_depth; i++)
5164 {
5165 bigger[i].fp = NULL;
5166 bigger[i].name = NULL;
5167 bigger[i].lnum = 0;
5168 bigger[i].matched = FALSE;
5169 }
5170 for (i = old_files; i < max_path_depth; i++)
5171 bigger[i + max_path_depth] = files[i];
5172 old_files += max_path_depth;
5173 max_path_depth *= 2;
5174 vim_free(files);
5175 files = bigger;
5176 }
5177 }
5178 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5179 == NULL)
5180 vim_free(new_fname);
5181 else
5182 {
5183 if (++depth == old_files)
5184 {
5185 /*
5186 * lalloc() for 'bigger' must have failed above. We
5187 * will forget one of our already visited files now.
5188 */
5189 vim_free(files[old_files].name);
5190 ++old_files;
5191 }
5192 files[depth].name = curr_fname = new_fname;
5193 files[depth].lnum = 0;
5194 files[depth].matched = FALSE;
5195#ifdef FEAT_INS_EXPAND
5196 if (action == ACTION_EXPAND)
5197 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005198 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005199 vim_snprintf((char*)IObuff, IOSIZE,
5200 _("Scanning included file: %s"),
5201 (char *)new_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005202 msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005206 if (p_verbose >= 5)
5207 {
5208 verbose_enter();
5209 smsg((char_u *)_("Searching included file %s"),
5210 (char *)new_fname);
5211 verbose_leave();
5212 }
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215 }
5216 }
5217 else
5218 {
5219 /*
5220 * Check if the line is a define (type == FIND_DEFINE)
5221 */
5222 p = line;
5223search_line:
5224 define_matched = FALSE;
5225 if (def_regmatch.regprog != NULL
5226 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5227 {
5228 /*
5229 * Pattern must be first identifier after 'define', so skip
5230 * to that position before checking for match of pattern. Also
5231 * don't let it match beyond the end of this identifier.
5232 */
5233 p = def_regmatch.endp[0];
5234 while (*p && !vim_iswordc(*p))
5235 p++;
5236 define_matched = TRUE;
5237 }
5238
5239 /*
5240 * Look for a match. Don't do this if we are looking for a
5241 * define and this line didn't match define_prog above.
5242 */
5243 if (def_regmatch.regprog == NULL || define_matched)
5244 {
5245 if (define_matched
5246#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#endif
5249 )
5250 {
5251 /* compare the first "len" chars from "ptr" */
5252 startp = skipwhite(p);
5253 if (p_ic)
5254 matched = !MB_STRNICMP(startp, ptr, len);
5255 else
5256 matched = !STRNCMP(startp, ptr, len);
5257 if (matched && define_matched && whole
5258 && vim_iswordc(startp[len]))
5259 matched = FALSE;
5260 }
5261 else if (regmatch.regprog != NULL
5262 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5263 {
5264 matched = TRUE;
5265 startp = regmatch.startp[0];
5266 /*
5267 * Check if the line is not a comment line (unless we are
5268 * looking for a define). A line starting with "# define"
5269 * is not considered to be a comment line.
5270 */
5271 if (!define_matched && skip_comments)
5272 {
5273#ifdef FEAT_COMMENTS
5274 if ((*line != '#' ||
5275 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005276 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 matched = FALSE;
5278
5279 /*
5280 * Also check for a "/ *" or "/ /" before the match.
5281 * Skips lines like "int backwards; / * normal index
5282 * * /" when looking for "normal".
5283 * Note: Doesn't skip "/ *" in comments.
5284 */
5285 p = skipwhite(line);
5286 if (matched
5287 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5288#endif
5289 for (p = line; *p && p < startp; ++p)
5290 {
5291 if (matched
5292 && p[0] == '/'
5293 && (p[1] == '*' || p[1] == '/'))
5294 {
5295 matched = FALSE;
5296 /* After "//" all text is comment */
5297 if (p[1] == '/')
5298 break;
5299 ++p;
5300 }
5301 else if (!matched && p[0] == '*' && p[1] == '/')
5302 {
5303 /* Can find match after "* /". */
5304 matched = TRUE;
5305 ++p;
5306 }
5307 }
5308 }
5309 }
5310 }
5311 }
5312 if (matched)
5313 {
5314#ifdef FEAT_INS_EXPAND
5315 if (action == ACTION_EXPAND)
5316 {
5317 int reuse = 0;
5318 int add_r;
5319 char_u *aux;
5320
5321 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5322 break;
5323 found = TRUE;
5324 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005327 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (vim_iswordp(p))
5329 goto exit_matched;
5330 p = find_word_start(p);
5331 }
5332 p = find_word_end(p);
5333 i = (int)(p - aux);
5334
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005339
5340 /* Get the next line: when "depth" < 0 from the current
5341 * buffer, otherwise from the included file. Jump to
5342 * exit_matched when past the last line. */
5343 if (depth < 0)
5344 {
5345 if (lnum >= end_lnum)
5346 goto exit_matched;
5347 line = ml_get(++lnum);
5348 }
5349 else if (vim_fgets(line = file_line,
5350 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 goto exit_matched;
5352
5353 /* we read a line, set "already" to check this "line" later
5354 * if depth >= 0 we'll increase files[depth].lnum far
5355 * bellow -- Acevedo */
5356 already = aux = p = skipwhite(line);
5357 p = find_word_start(p);
5358 p = find_word_end(p);
5359 if (p > aux)
5360 {
5361 if (*aux != ')' && IObuff[i-1] != TAB)
5362 {
5363 if (IObuff[i-1] != ' ')
5364 IObuff[i++] = ' ';
5365 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5366 if (p_js
5367 && (IObuff[i-2] == '.'
5368 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5369 && (IObuff[i-2] == '?'
5370 || IObuff[i-2] == '!'))))
5371 IObuff[i++] = ' ';
5372 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005373 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (p - aux >= IOSIZE - i)
5375 p = aux + IOSIZE - i - 1;
5376 STRNCPY(IObuff + i, aux, p - aux);
5377 i += (int)(p - aux);
5378 reuse |= CONT_S_IPOS;
5379 }
5380 IObuff[i] = NUL;
5381 aux = IObuff;
5382
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005383 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 goto exit_matched;
5385 }
5386
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005387 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5389 dir, reuse);
5390 if (add_r == OK)
5391 /* if dir was BACKWARD then honor it just once */
5392 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005393 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 break;
5395 }
5396 else
5397#endif
5398 if (action == ACTION_SHOW_ALL)
5399 {
5400 found = TRUE;
5401 if (!did_show)
5402 gotocmdline(TRUE); /* cursor at status line */
5403 if (curr_fname != prev_fname)
5404 {
5405 if (did_show)
5406 msg_putchar('\n'); /* cursor below last one */
5407 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005408 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 msg_home_replace_hl(curr_fname);
5410 prev_fname = curr_fname;
5411 }
5412 did_show = TRUE;
5413 if (!got_int)
5414 show_pat_in_path(line, type, TRUE, action,
5415 (depth == -1) ? NULL : files[depth].fp,
5416 (depth == -1) ? &lnum : &files[depth].lnum,
5417 match_count++);
5418
5419 /* Set matched flag for this file and all the ones that
5420 * include it */
5421 for (i = 0; i <= depth; ++i)
5422 files[i].matched = TRUE;
5423 }
5424 else if (--count <= 0)
5425 {
5426 found = TRUE;
5427 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005428#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 && g_do_tagpreview == 0
5430#endif
5431 )
5432 EMSG(_("E387: Match is on current line"));
5433 else if (action == ACTION_SHOW)
5434 {
5435 show_pat_in_path(line, type, did_show, action,
5436 (depth == -1) ? NULL : files[depth].fp,
5437 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5438 did_show = TRUE;
5439 }
5440 else
5441 {
5442#ifdef FEAT_GUI
5443 need_mouse_correct = TRUE;
5444#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005445#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 /* ":psearch" uses the preview window */
5447 if (g_do_tagpreview != 0)
5448 {
5449 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005450 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 }
5452#endif
5453 if (action == ACTION_SPLIT)
5454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005457 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 }
5459 if (depth == -1)
5460 {
5461 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005462#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 if (g_do_tagpreview != 0)
5464 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005465 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005466 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005467 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 break; /* failed to jump to file */
5469 }
5470 else
5471#endif
5472 setpcmark();
5473 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005474 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476 else
5477 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005478 if (!GETFILE_SUCCESS(getfile(
5479 0, files[depth].name, NULL, TRUE,
5480 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 break; /* failed to jump to file */
5482 /* autocommands may have changed the lnum, we don't
5483 * want that here */
5484 curwin->w_cursor.lnum = files[depth].lnum;
5485 }
5486 }
5487 if (action != ACTION_SHOW)
5488 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005489 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 curwin->w_set_curswant = TRUE;
5491 }
5492
Bram Moolenaar4033c552017-09-16 20:54:51 +02005493#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005495 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
5497 /* Return cursor to where we were */
5498 validate_cursor();
5499 redraw_later(VALID);
5500 win_enter(curwin_save, TRUE);
5501 }
5502#endif
5503 break;
5504 }
5505#ifdef FEAT_INS_EXPAND
5506exit_matched:
5507#endif
5508 matched = FALSE;
5509 /* look for other matches in the rest of the line if we
5510 * are not at the end of it already */
5511 if (def_regmatch.regprog == NULL
5512#ifdef FEAT_INS_EXPAND
5513 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005514 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005516 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005517 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 goto search_line;
5519 }
5520 line_breakcheck();
5521#ifdef FEAT_INS_EXPAND
5522 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005523 ins_compl_check_keys(30, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005524 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#else
5526 if (got_int)
5527#endif
5528 break;
5529
5530 /*
5531 * Read the next line. When reading an included file and encountering
5532 * end-of-file, close the file and continue in the file that included
5533 * it.
5534 */
5535 while (depth >= 0 && !already
5536 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5537 {
5538 fclose(files[depth].fp);
5539 --old_files;
5540 files[old_files].name = files[depth].name;
5541 files[old_files].matched = files[depth].matched;
5542 --depth;
5543 curr_fname = (depth == -1) ? curbuf->b_fname
5544 : files[depth].name;
5545 if (depth < depth_displayed)
5546 depth_displayed = depth;
5547 }
5548 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005549 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005551 /* Remove any CR and LF from the line. */
5552 i = (int)STRLEN(line);
5553 if (i > 0 && line[i - 1] == '\n')
5554 line[--i] = NUL;
5555 if (i > 0 && line[i - 1] == '\r')
5556 line[--i] = NUL;
5557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 else if (!already)
5559 {
5560 if (++lnum > end_lnum)
5561 break;
5562 line = ml_get(lnum);
5563 }
5564 already = NULL;
5565 }
5566 /* End of big for (;;) loop. */
5567
5568 /* Close any files that are still open. */
5569 for (i = 0; i <= depth; i++)
5570 {
5571 fclose(files[i].fp);
5572 vim_free(files[i].name);
5573 }
5574 for (i = old_files; i < max_path_depth; i++)
5575 vim_free(files[i].name);
5576 vim_free(files);
5577
5578 if (type == CHECK_PATH)
5579 {
5580 if (!did_show)
5581 {
5582 if (action != ACTION_SHOW_ALL)
5583 MSG(_("All included files were found"));
5584 else
5585 MSG(_("No included files"));
5586 }
5587 }
5588 else if (!found
5589#ifdef FEAT_INS_EXPAND
5590 && action != ACTION_EXPAND
5591#endif
5592 )
5593 {
5594#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005595 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596#else
5597 if (got_int)
5598#endif
5599 EMSG(_(e_interr));
5600 else if (type == FIND_DEFINE)
5601 EMSG(_("E388: Couldn't find definition"));
5602 else
5603 EMSG(_("E389: Couldn't find pattern"));
5604 }
5605 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5606 msg_end();
5607
5608fpip_end:
5609 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005610 vim_regfree(regmatch.regprog);
5611 vim_regfree(incl_regmatch.regprog);
5612 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
5615 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005616show_pat_in_path(
5617 char_u *line,
5618 int type,
5619 int did_show,
5620 int action,
5621 FILE *fp,
5622 linenr_T *lnum,
5623 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 char_u *p;
5626
5627 if (did_show)
5628 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005629 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 gotocmdline(TRUE); /* cursor at status line */
5631 if (got_int) /* 'q' typed at "--more--" message */
5632 return;
5633 for (;;)
5634 {
5635 p = line + STRLEN(line) - 1;
5636 if (fp != NULL)
5637 {
5638 /* We used fgets(), so get rid of newline at end */
5639 if (p >= line && *p == '\n')
5640 --p;
5641 if (p >= line && *p == '\r')
5642 --p;
5643 *(p + 1) = NUL;
5644 }
5645 if (action == ACTION_SHOW_ALL)
5646 {
5647 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5648 msg_puts(IObuff);
5649 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5650 /* Highlight line numbers */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005651 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 MSG_PUTS(" ");
5653 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005654 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 out_flush(); /* show one line at a time */
5656
5657 /* Definition continues until line that doesn't end with '\' */
5658 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5659 break;
5660
5661 if (fp != NULL)
5662 {
5663 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5664 break;
5665 ++*lnum;
5666 }
5667 else
5668 {
5669 if (++*lnum > curbuf->b_ml.ml_line_count)
5670 break;
5671 line = ml_get(*lnum);
5672 }
5673 msg_putchar('\n');
5674 }
5675}
5676#endif
5677
5678#ifdef FEAT_VIMINFO
5679 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005680read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 char_u *lp;
5683 int idx = -1;
5684 int magic = FALSE;
5685 int no_scs = FALSE;
5686 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005687 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 long off = 0;
5689 int setlast = FALSE;
5690#ifdef FEAT_SEARCH_EXTRA
5691 static int hlsearch_on = FALSE;
5692#endif
5693 char_u *val;
5694
5695 /*
5696 * Old line types:
5697 * "/pat", "&pat": search/subst. pat
5698 * "~/pat", "~&pat": last used search/subst. pat
5699 * New line types:
5700 * "~h", "~H": hlsearch highlighting off/on
5701 * "~<magic><smartcase><line><end><off><last><which>pat"
5702 * <magic>: 'm' off, 'M' on
5703 * <smartcase>: 's' off, 'S' on
5704 * <line>: 'L' line offset, 'l' char offset
5705 * <end>: 'E' from end, 'e' from start
5706 * <off>: decimal, offset
5707 * <last>: '~' last used pattern
5708 * <which>: '/' search pat, '&' subst. pat
5709 */
5710 lp = virp->vir_line;
5711 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5712 {
5713 if (lp[1] == 'M') /* magic on */
5714 magic = TRUE;
5715 if (lp[2] == 's')
5716 no_scs = TRUE;
5717 if (lp[3] == 'L')
5718 off_line = TRUE;
5719 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005720 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 lp += 5;
5722 off = getdigits(&lp);
5723 }
5724 if (lp[0] == '~') /* use this pattern for last-used pattern */
5725 {
5726 setlast = TRUE;
5727 lp++;
5728 }
5729 if (lp[0] == '/')
5730 idx = RE_SEARCH;
5731 else if (lp[0] == '&')
5732 idx = RE_SUBST;
5733#ifdef FEAT_SEARCH_EXTRA
5734 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5735 hlsearch_on = FALSE;
5736 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5737 hlsearch_on = TRUE;
5738#endif
5739 if (idx >= 0)
5740 {
5741 if (force || spats[idx].pat == NULL)
5742 {
5743 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5744 TRUE);
5745 if (val != NULL)
5746 {
5747 set_last_search_pat(val, idx, magic, setlast);
5748 vim_free(val);
5749 spats[idx].no_scs = no_scs;
5750 spats[idx].off.line = off_line;
5751 spats[idx].off.end = off_end;
5752 spats[idx].off.off = off;
5753#ifdef FEAT_SEARCH_EXTRA
5754 if (setlast)
Bram Moolenaar8050efa2013-11-08 04:30:20 +01005755 {
5756 SET_NO_HLSEARCH(!hlsearch_on);
5757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758#endif
5759 }
5760 }
5761 }
5762 return viminfo_readline(virp);
5763}
5764
5765 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005766write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 if (get_viminfo_parameter('/') != 0)
5769 {
5770#ifdef FEAT_SEARCH_EXTRA
5771 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5772 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5773#endif
5774 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005775 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
5777}
5778
5779 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005780wvsp_one(
5781 FILE *fp, /* file to write to */
5782 int idx, /* spats[] index */
5783 char *s, /* search pat */
5784 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785{
5786 if (spats[idx].pat != NULL)
5787 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005788 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 /* off.dir is not stored, it's reset to forward */
5790 fprintf(fp, "%c%c%c%c%ld%s%c",
5791 spats[idx].magic ? 'M' : 'm', /* magic */
5792 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5793 spats[idx].off.line ? 'L' : 'l', /* line offset */
5794 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5795 spats[idx].off.off, /* offset */
5796 last_idx == idx ? "~" : "", /* last used pat */
5797 sc);
5798 viminfo_writestring(fp, spats[idx].pat);
5799 }
5800}
5801#endif /* FEAT_VIMINFO */