blob: fc689db2df60804900adbb7b41ce08b3f998dbeb [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
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002283 * with / * /. Ignore a / * after / / and after *.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 */
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] == '*'
Bram Moolenaarf8c53d32017-11-12 15:36:38 +01002309 && (pos.col == 1 || linep[pos.col - 2] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 && (int)pos.col < comment_col)
2311 {
2312 count++;
2313 match_pos = pos;
2314 match_pos.col--;
2315 }
2316 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2317 {
2318 if (count > 0)
2319 pos = match_pos;
2320 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2321 && (int)pos.col <= comment_col)
2322 pos.col -= 2;
2323 else if (ignore_cend)
2324 continue;
2325 else
2326 return NULL;
2327 return &pos;
2328 }
2329 }
2330 continue;
2331 }
2332
2333 /*
2334 * If smart matching ('cpoptions' does not contain '%'), braces inside
2335 * of quotes are ignored, but only if there is an even number of
2336 * quotes in the line.
2337 */
2338 if (cpo_match)
2339 do_quotes = 0;
2340 else if (do_quotes == -1)
2341 {
2342 /*
2343 * Count the number of quotes in the line, skipping \" and '"'.
2344 * Watch out for "\\".
2345 */
2346 at_start = do_quotes;
2347 for (ptr = linep; *ptr; ++ptr)
2348 {
2349 if (ptr == linep + pos.col + backwards)
2350 at_start = (do_quotes & 1);
2351 if (*ptr == '"'
2352 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2353 ++do_quotes;
2354 if (*ptr == '\\' && ptr[1] != NUL)
2355 ++ptr;
2356 }
2357 do_quotes &= 1; /* result is 1 with even number of quotes */
2358
2359 /*
2360 * If we find an uneven count, check current line and previous
2361 * one for a '\' at the end.
2362 */
2363 if (!do_quotes)
2364 {
2365 inquote = FALSE;
2366 if (ptr[-1] == '\\')
2367 {
2368 do_quotes = 1;
2369 if (start_in_quotes == MAYBE)
2370 {
2371 /* Do we need to use at_start here? */
2372 inquote = TRUE;
2373 start_in_quotes = TRUE;
2374 }
2375 else if (backwards)
2376 inquote = TRUE;
2377 }
2378 if (pos.lnum > 1)
2379 {
2380 ptr = ml_get(pos.lnum - 1);
2381 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2382 {
2383 do_quotes = 1;
2384 if (start_in_quotes == MAYBE)
2385 {
2386 inquote = at_start;
2387 if (inquote)
2388 start_in_quotes = TRUE;
2389 }
2390 else if (!backwards)
2391 inquote = TRUE;
2392 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002393
2394 /* ml_get() only keeps one line, need to get linep again */
2395 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 }
2397 }
2398 }
2399 if (start_in_quotes == MAYBE)
2400 start_in_quotes = FALSE;
2401
2402 /*
2403 * If 'smartmatch' is set:
2404 * Things inside quotes are ignored by setting 'inquote'. If we
2405 * find a quote without a preceding '\' invert 'inquote'. At the
2406 * end of a line not ending in '\' we reset 'inquote'.
2407 *
2408 * In lines with an uneven number of quotes (without preceding '\')
2409 * we do not know which part to ignore. Therefore we only set
2410 * inquote if the number of quotes in a line is even, unless this
2411 * line or the previous one ends in a '\'. Complicated, isn't it?
2412 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002413 c = PTR2CHAR(linep + pos.col);
2414 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 case NUL:
2417 /* at end of line without trailing backslash, reset inquote */
2418 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2419 {
2420 inquote = FALSE;
2421 start_in_quotes = FALSE;
2422 }
2423 break;
2424
2425 case '"':
2426 /* a quote that is preceded with an odd number of backslashes is
2427 * ignored */
2428 if (do_quotes)
2429 {
2430 int col;
2431
2432 for (col = pos.col - 1; col >= 0; --col)
2433 if (linep[col] != '\\')
2434 break;
2435 if ((((int)pos.col - 1 - col) & 1) == 0)
2436 {
2437 inquote = !inquote;
2438 start_in_quotes = FALSE;
2439 }
2440 }
2441 break;
2442
2443 /*
2444 * If smart matching ('cpoptions' does not contain '%'):
2445 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2446 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2447 * skipped, there is never a brace in them.
2448 * Ignore this when finding matches for `'.
2449 */
2450 case '\'':
2451 if (!cpo_match && initc != '\'' && findc != '\'')
2452 {
2453 if (backwards)
2454 {
2455 if (pos.col > 1)
2456 {
2457 if (linep[pos.col - 2] == '\'')
2458 {
2459 pos.col -= 2;
2460 break;
2461 }
2462 else if (linep[pos.col - 2] == '\\' &&
2463 pos.col > 2 && linep[pos.col - 3] == '\'')
2464 {
2465 pos.col -= 3;
2466 break;
2467 }
2468 }
2469 }
2470 else if (linep[pos.col + 1]) /* forward search */
2471 {
2472 if (linep[pos.col + 1] == '\\' &&
2473 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2474 {
2475 pos.col += 3;
2476 break;
2477 }
2478 else if (linep[pos.col + 2] == '\'')
2479 {
2480 pos.col += 2;
2481 break;
2482 }
2483 }
2484 }
2485 /* FALLTHROUGH */
2486
2487 default:
2488#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002489 /*
2490 * For Lisp skip over backslashed (), {} and [].
2491 * (actually, we skip #\( et al)
2492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 if (curbuf->b_p_lisp
2494 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002495 && pos.col > 1
2496 && check_prevcol(linep, pos.col, '\\', NULL)
2497 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 break;
2499#endif
2500
2501 /* Check for match outside of quotes, and inside of
2502 * quotes when the start is also inside of quotes. */
2503 if ((!inquote || start_in_quotes == TRUE)
2504 && (c == initc || c == findc))
2505 {
2506 int col, bslcnt = 0;
2507
2508 if (!cpo_bsl)
2509 {
2510 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2511 bslcnt++;
2512 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002513 /* Only accept a match when 'M' is in 'cpo' or when escaping
2514 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2516 {
2517 if (c == initc)
2518 count++;
2519 else
2520 {
2521 if (count == 0)
2522 return &pos;
2523 count--;
2524 }
2525 }
2526 }
2527 }
2528 }
2529
2530 if (comment_dir == BACKWARD && count > 0)
2531 {
2532 pos = match_pos;
2533 return &pos;
2534 }
2535 return (pos_T *)NULL; /* never found it */
2536}
2537
2538/*
2539 * Check if line[] contains a / / comment.
2540 * Return MAXCOL if not, otherwise return the column.
2541 * TODO: skip strings.
2542 */
2543 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002544check_linecomment(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545{
2546 char_u *p;
2547
2548 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002549#ifdef FEAT_LISP
2550 /* skip Lispish one-line comments */
2551 if (curbuf->b_p_lisp)
2552 {
2553 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2554 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002555 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002556
2557 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002558 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002559 {
2560 if (*p == '"')
2561 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002562 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002563 {
2564 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002565 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002566 }
2567 else if (p == line || ((p - line) >= 2
2568 /* skip #\" form */
2569 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002570 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002571 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002572 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002573 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2574 break; /* found! */
2575 ++p;
2576 }
2577 }
2578 else
2579 p = NULL;
2580 }
2581 else
2582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 while ((p = vim_strchr(p, '/')) != NULL)
2584 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002585 /* accept a double /, unless it's preceded with * and followed by *,
2586 * because * / / * is an end and start of a C comment */
2587 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 break;
2589 ++p;
2590 }
2591
2592 if (p == NULL)
2593 return MAXCOL;
2594 return (int)(p - line);
2595}
2596
2597/*
2598 * Move cursor briefly to character matching the one under the cursor.
2599 * Used for Insert mode and "r" command.
2600 * Show the match only if it is visible on the screen.
2601 * If there isn't a match, then beep.
2602 */
2603 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002604showmatch(
2605 int c) /* char to show match for */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 pos_T *lpos, save_cursor;
2608 pos_T mpos;
2609 colnr_T vcol;
2610 long save_so;
2611 long save_siso;
2612#ifdef CURSOR_SHAPE
2613 int save_state;
2614#endif
2615 colnr_T save_dollar_vcol;
2616 char_u *p;
2617
2618 /*
2619 * Only show match for chars in the 'matchpairs' option.
2620 */
2621 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002622 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {
2624#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002625 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002626 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002627#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002628 p += MB_PTR2LEN(p) + 1;
2629 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630#ifdef FEAT_RIGHTLEFT
2631 && !(curwin->w_p_rl ^ p_ri)
2632#endif
2633 )
2634 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002635 p += MB_PTR2LEN(p);
2636 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return;
2638 }
2639
2640 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002641 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002642 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
2644 if (!curwin->w_p_wrap)
2645 getvcol(curwin, lpos, NULL, &vcol, NULL);
2646 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
Bram Moolenaar02631462017-09-22 15:20:32 +02002647 && vcol < curwin->w_leftcol + curwin->w_width))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 {
2649 mpos = *lpos; /* save the pos, update_screen() may change it */
2650 save_cursor = curwin->w_cursor;
2651 save_so = p_so;
2652 save_siso = p_siso;
2653 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2654 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002655 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2656 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 ++curwin->w_virtcol; /* do display ')' just before "$" */
2658 update_screen(VALID); /* show the new char first */
2659
2660 save_dollar_vcol = dollar_vcol;
2661#ifdef CURSOR_SHAPE
2662 save_state = State;
2663 State = SHOWMATCH;
2664 ui_cursor_shape(); /* may show different cursor shape */
2665#endif
2666 curwin->w_cursor = mpos; /* move to matching char */
2667 p_so = 0; /* don't use 'scrolloff' here */
2668 p_siso = 0; /* don't use 'sidescrolloff' here */
2669 showruler(FALSE);
2670 setcursor();
2671 cursor_on(); /* make sure that the cursor is shown */
2672 out_flush();
2673#ifdef FEAT_GUI
2674 if (gui.in_use)
2675 {
2676 gui_update_cursor(TRUE, FALSE);
2677 gui_mch_flush();
2678 }
2679#endif
2680 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2681 * which resets it if the matching position is in a previous line
2682 * and has a higher column number. */
2683 dollar_vcol = save_dollar_vcol;
2684
2685 /*
2686 * brief pause, unless 'm' is present in 'cpo' and a character is
2687 * available.
2688 */
2689 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2690 ui_delay(p_mat * 100L, TRUE);
2691 else if (!char_avail())
2692 ui_delay(p_mat * 100L, FALSE);
2693 curwin->w_cursor = save_cursor; /* restore cursor position */
2694 p_so = save_so;
2695 p_siso = save_siso;
2696#ifdef CURSOR_SHAPE
2697 State = save_state;
2698 ui_cursor_shape(); /* may show different cursor shape */
2699#endif
2700 }
2701 }
2702}
2703
2704/*
2705 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002706 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 * space or a line break. Also stop at an empty line.
2708 * Return OK if the next sentence was found.
2709 */
2710 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002711findsent(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
2713 pos_T pos, tpos;
2714 int c;
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01002715 int (*func)(pos_T *);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 int startlnum;
2717 int noskip = FALSE; /* do not skip blanks */
2718 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002719 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 pos = curwin->w_cursor;
2722 if (dir == FORWARD)
2723 func = incl;
2724 else
2725 func = decl;
2726
2727 while (count--)
2728 {
2729 /*
2730 * if on an empty line, skip upto a non-empty line
2731 */
2732 if (gchar_pos(&pos) == NUL)
2733 {
2734 do
2735 if ((*func)(&pos) == -1)
2736 break;
2737 while (gchar_pos(&pos) == NUL);
2738 if (dir == FORWARD)
2739 goto found;
2740 }
2741 /*
2742 * if on the start of a paragraph or a section and searching forward,
2743 * go to the next line
2744 */
2745 else if (dir == FORWARD && pos.col == 0 &&
2746 startPS(pos.lnum, NUL, FALSE))
2747 {
2748 if (pos.lnum == curbuf->b_ml.ml_line_count)
2749 return FAIL;
2750 ++pos.lnum;
2751 goto found;
2752 }
2753 else if (dir == BACKWARD)
2754 decl(&pos);
2755
2756 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002757 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2759 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2760 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002761 if (vim_strchr((char_u *)".!?", c) != NULL)
2762 {
2763 /* Only skip over a '.', '!' and '?' once. */
2764 if (found_dot)
2765 break;
2766 found_dot = TRUE;
2767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 if (decl(&pos) == -1)
2769 break;
2770 /* when going forward: Stop in front of empty line */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002771 if (LINEEMPTY(pos.lnum) && dir == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 {
2773 incl(&pos);
2774 goto found;
2775 }
2776 }
2777
2778 /* remember the line where the search started */
2779 startlnum = pos.lnum;
2780 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2781
2782 for (;;) /* find end of sentence */
2783 {
2784 c = gchar_pos(&pos);
2785 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2786 {
2787 if (dir == BACKWARD && pos.lnum != startlnum)
2788 ++pos.lnum;
2789 break;
2790 }
2791 if (c == '.' || c == '!' || c == '?')
2792 {
2793 tpos = pos;
2794 do
2795 if ((c = inc(&tpos)) == -1)
2796 break;
2797 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2798 != NULL);
2799 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2800 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2801 && gchar_pos(&tpos) == ' ')))
2802 {
2803 pos = tpos;
2804 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2805 inc(&pos);
2806 break;
2807 }
2808 }
2809 if ((*func)(&pos) == -1)
2810 {
2811 if (count)
2812 return FAIL;
2813 noskip = TRUE;
2814 break;
2815 }
2816 }
2817found:
2818 /* skip white space */
2819 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2820 if (incl(&pos) == -1)
2821 break;
2822 }
2823
2824 setpcmark();
2825 curwin->w_cursor = pos;
2826 return OK;
2827}
2828
2829/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002830 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002832 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 * If 'what' is '{' or '}' we go to the next section.
2834 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002835 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 */
2837 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002838findpar(
2839 int *pincl, /* Return: TRUE if last char is to be included */
2840 int dir,
2841 long count,
2842 int what,
2843 int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844{
2845 linenr_T curr;
2846 int did_skip; /* TRUE after separating lines have been skipped */
2847 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002848 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849#ifdef FEAT_FOLDING
2850 linenr_T fold_first; /* first line of a closed fold */
2851 linenr_T fold_last; /* last line of a closed fold */
2852 int fold_skipped; /* TRUE if a closed fold was skipped this
2853 iteration */
2854#endif
2855
2856 curr = curwin->w_cursor.lnum;
2857
2858 while (count--)
2859 {
2860 did_skip = FALSE;
2861 for (first = TRUE; ; first = FALSE)
2862 {
2863 if (*ml_get(curr) != NUL)
2864 did_skip = TRUE;
2865
2866#ifdef FEAT_FOLDING
2867 /* skip folded lines */
2868 fold_skipped = FALSE;
2869 if (first && hasFolding(curr, &fold_first, &fold_last))
2870 {
2871 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2872 fold_skipped = TRUE;
2873 }
2874#endif
2875
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002876 /* POSIX has it's own ideas of what a paragraph boundary is and it
2877 * doesn't match historical Vi: It also stops at a "{" in the
2878 * first column and at an empty line. */
2879 if (!first && did_skip && (startPS(curr, what, both)
2880 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 break;
2882
2883#ifdef FEAT_FOLDING
2884 if (fold_skipped)
2885 curr -= dir;
2886#endif
2887 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2888 {
2889 if (count)
2890 return FALSE;
2891 curr -= dir;
2892 break;
2893 }
2894 }
2895 }
2896 setpcmark();
2897 if (both && *ml_get(curr) == '}') /* include line with '}' */
2898 ++curr;
2899 curwin->w_cursor.lnum = curr;
2900 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2901 {
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002902 char_u *line = ml_get(curr);
2903
2904 /* Put the cursor on the last character in the last line and make the
2905 * motion inclusive. */
2906 if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 --curwin->w_cursor.col;
Bram Moolenaarbf3d5802017-03-29 19:48:11 +02002909#ifdef FEAT_MBYTE
2910 curwin->w_cursor.col -=
2911 (*mb_head_off)(line, line + curwin->w_cursor.col);
2912#endif
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002913 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 }
2916 else
2917 curwin->w_cursor.col = 0;
2918 return TRUE;
2919}
2920
2921/*
2922 * check if the string 's' is a nroff macro that is in option 'opt'
2923 */
2924 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002925inmacro(char_u *opt, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
2927 char_u *macro;
2928
2929 for (macro = opt; macro[0]; ++macro)
2930 {
2931 /* Accept two characters in the option being equal to two characters
2932 * in the line. A space in the option matches with a space in the
2933 * line or the line having ended. */
2934 if ( (macro[0] == s[0]
2935 || (macro[0] == ' '
2936 && (s[0] == NUL || s[0] == ' ')))
2937 && (macro[1] == s[1]
2938 || ((macro[1] == NUL || macro[1] == ' ')
2939 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2940 break;
2941 ++macro;
2942 if (macro[0] == NUL)
2943 break;
2944 }
2945 return (macro[0] != NUL);
2946}
2947
2948/*
2949 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2950 * If 'para' is '{' or '}' only check for sections.
2951 * If 'both' is TRUE also stop at '}'
2952 */
2953 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002954startPS(linenr_T lnum, int para, int both)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 char_u *s;
2957
2958 s = ml_get(lnum);
2959 if (*s == para || *s == '\f' || (both && *s == '}'))
2960 return TRUE;
2961 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2962 (!para && inmacro(p_para, s + 1))))
2963 return TRUE;
2964 return FALSE;
2965}
2966
2967/*
2968 * The following routines do the word searches performed by the 'w', 'W',
2969 * 'b', 'B', 'e', and 'E' commands.
2970 */
2971
2972/*
2973 * To perform these searches, characters are placed into one of three
2974 * classes, and transitions between classes determine word boundaries.
2975 *
2976 * The classes are:
2977 *
2978 * 0 - white space
2979 * 1 - punctuation
2980 * 2 or higher - keyword characters (letters, digits and underscore)
2981 */
2982
2983static int cls_bigword; /* TRUE for "W", "B" or "E" */
2984
2985/*
2986 * cls() - returns the class of character at curwin->w_cursor
2987 *
2988 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2989 * from class 2 and higher are reported as class 1 since only white space
2990 * boundaries are of interest.
2991 */
2992 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01002993cls(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 int c;
2996
2997 c = gchar_cursor();
2998#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2999 if (p_altkeymap && c == F_BLANK)
3000 return 0;
3001#endif
3002 if (c == ' ' || c == '\t' || c == NUL)
3003 return 0;
3004#ifdef FEAT_MBYTE
3005 if (enc_dbcs != 0 && c > 0xFF)
3006 {
3007 /* If cls_bigword, report multi-byte chars as class 1. */
3008 if (enc_dbcs == DBCS_KOR && cls_bigword)
3009 return 1;
3010
3011 /* process code leading/trailing bytes */
3012 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
3013 }
3014 if (enc_utf8)
3015 {
3016 c = utf_class(c);
3017 if (c != 0 && cls_bigword)
3018 return 1;
3019 return c;
3020 }
3021#endif
3022
3023 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
3024 if (cls_bigword)
3025 return 1;
3026
3027 if (vim_iswordc(c))
3028 return 2;
3029 return 1;
3030}
3031
3032
3033/*
3034 * fwd_word(count, type, eol) - move forward one word
3035 *
3036 * Returns FAIL if the cursor was already at the end of the file.
3037 * If eol is TRUE, last word stops at end of line (for operators).
3038 */
3039 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003040fwd_word(
3041 long count,
3042 int bigword, /* "W", "E" or "B" */
3043 int eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044{
3045 int sclass; /* starting class */
3046 int i;
3047 int last_line;
3048
3049#ifdef FEAT_VIRTUALEDIT
3050 curwin->w_cursor.coladd = 0;
3051#endif
3052 cls_bigword = bigword;
3053 while (--count >= 0)
3054 {
3055#ifdef FEAT_FOLDING
3056 /* When inside a range of folded lines, move to the last char of the
3057 * last line. */
3058 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3059 coladvance((colnr_T)MAXCOL);
3060#endif
3061 sclass = cls();
3062
3063 /*
3064 * We always move at least one character, unless on the last
3065 * character in the buffer.
3066 */
3067 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
3068 i = inc_cursor();
3069 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
3070 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00003071 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 return OK;
3073
3074 /*
3075 * Go one char past end of current word (if any)
3076 */
3077 if (sclass != 0)
3078 while (cls() == sclass)
3079 {
3080 i = inc_cursor();
3081 if (i == -1 || (i >= 1 && eol && count == 0))
3082 return OK;
3083 }
3084
3085 /*
3086 * go to next non-white
3087 */
3088 while (cls() == 0)
3089 {
3090 /*
3091 * We'll stop if we land on a blank line
3092 */
3093 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
3094 break;
3095
3096 i = inc_cursor();
3097 if (i == -1 || (i >= 1 && eol && count == 0))
3098 return OK;
3099 }
3100 }
3101 return OK;
3102}
3103
3104/*
3105 * bck_word() - move backward 'count' words
3106 *
3107 * If stop is TRUE and we are already on the start of a word, move one less.
3108 *
3109 * Returns FAIL if top of the file was reached.
3110 */
3111 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003112bck_word(long count, int bigword, int stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int sclass; /* starting class */
3115
3116#ifdef FEAT_VIRTUALEDIT
3117 curwin->w_cursor.coladd = 0;
3118#endif
3119 cls_bigword = bigword;
3120 while (--count >= 0)
3121 {
3122#ifdef FEAT_FOLDING
3123 /* When inside a range of folded lines, move to the first char of the
3124 * first line. */
3125 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
3126 curwin->w_cursor.col = 0;
3127#endif
3128 sclass = cls();
3129 if (dec_cursor() == -1) /* started at start of file */
3130 return FAIL;
3131
3132 if (!stop || sclass == cls() || sclass == 0)
3133 {
3134 /*
3135 * Skip white space before the word.
3136 * Stop on an empty line.
3137 */
3138 while (cls() == 0)
3139 {
3140 if (curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003141 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 goto finished;
3143 if (dec_cursor() == -1) /* hit start of file, stop here */
3144 return OK;
3145 }
3146
3147 /*
3148 * Move backward to start of this word.
3149 */
3150 if (skip_chars(cls(), BACKWARD))
3151 return OK;
3152 }
3153
3154 inc_cursor(); /* overshot - forward one */
3155finished:
3156 stop = FALSE;
3157 }
3158 return OK;
3159}
3160
3161/*
3162 * end_word() - move to the end of the word
3163 *
3164 * There is an apparent bug in the 'e' motion of the real vi. At least on the
3165 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
3166 * motion crosses blank lines. When the real vi crosses a blank line in an
3167 * 'e' motion, the cursor is placed on the FIRST character of the next
3168 * non-blank line. The 'E' command, however, works correctly. Since this
3169 * appears to be a bug, I have not duplicated it here.
3170 *
3171 * Returns FAIL if end of the file was reached.
3172 *
3173 * If stop is TRUE and we are already on the end of a word, move one less.
3174 * If empty is TRUE stop on an empty line.
3175 */
3176 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003177end_word(
3178 long count,
3179 int bigword,
3180 int stop,
3181 int empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 int sclass; /* starting class */
3184
3185#ifdef FEAT_VIRTUALEDIT
3186 curwin->w_cursor.coladd = 0;
3187#endif
3188 cls_bigword = bigword;
3189 while (--count >= 0)
3190 {
3191#ifdef FEAT_FOLDING
3192 /* When inside a range of folded lines, move to the last char of the
3193 * last line. */
3194 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3195 coladvance((colnr_T)MAXCOL);
3196#endif
3197 sclass = cls();
3198 if (inc_cursor() == -1)
3199 return FAIL;
3200
3201 /*
3202 * If we're in the middle of a word, we just have to move to the end
3203 * of it.
3204 */
3205 if (cls() == sclass && sclass != 0)
3206 {
3207 /*
3208 * Move forward to end of the current word
3209 */
3210 if (skip_chars(sclass, FORWARD))
3211 return FAIL;
3212 }
3213 else if (!stop || sclass == 0)
3214 {
3215 /*
3216 * We were at the end of a word. Go to the end of the next word.
3217 * First skip white space, if 'empty' is TRUE, stop at empty line.
3218 */
3219 while (cls() == 0)
3220 {
3221 if (empty && curwin->w_cursor.col == 0
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003222 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 goto finished;
3224 if (inc_cursor() == -1) /* hit end of file, stop here */
3225 return FAIL;
3226 }
3227
3228 /*
3229 * Move forward to the end of this word.
3230 */
3231 if (skip_chars(cls(), FORWARD))
3232 return FAIL;
3233 }
3234 dec_cursor(); /* overshot - one char backward */
3235finished:
3236 stop = FALSE; /* we move only one word less */
3237 }
3238 return OK;
3239}
3240
3241/*
3242 * Move back to the end of the word.
3243 *
3244 * Returns FAIL if start of the file was reached.
3245 */
3246 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003247bckend_word(
3248 long count,
3249 int bigword, /* TRUE for "B" */
3250 int eol) /* TRUE: stop at end of line. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 int sclass; /* starting class */
3253 int i;
3254
3255#ifdef FEAT_VIRTUALEDIT
3256 curwin->w_cursor.coladd = 0;
3257#endif
3258 cls_bigword = bigword;
3259 while (--count >= 0)
3260 {
3261 sclass = cls();
3262 if ((i = dec_cursor()) == -1)
3263 return FAIL;
3264 if (eol && i == 1)
3265 return OK;
3266
3267 /*
3268 * Move backward to before the start of this word.
3269 */
3270 if (sclass != 0)
3271 {
3272 while (cls() == sclass)
3273 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3274 return OK;
3275 }
3276
3277 /*
3278 * Move backward to end of the previous word
3279 */
3280 while (cls() == 0)
3281 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003282 if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 break;
3284 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3285 return OK;
3286 }
3287 }
3288 return OK;
3289}
3290
3291/*
3292 * Skip a row of characters of the same class.
3293 * Return TRUE when end-of-file reached, FALSE otherwise.
3294 */
3295 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003296skip_chars(int cclass, int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 while (cls() == cclass)
3299 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3300 return TRUE;
3301 return FALSE;
3302}
3303
3304#ifdef FEAT_TEXTOBJ
3305/*
3306 * Go back to the start of the word or the start of white space
3307 */
3308 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003309back_in_line(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 int sclass; /* starting class */
3312
3313 sclass = cls();
3314 for (;;)
3315 {
3316 if (curwin->w_cursor.col == 0) /* stop at start of line */
3317 break;
3318 dec_cursor();
3319 if (cls() != sclass) /* stop at start of word */
3320 {
3321 inc_cursor();
3322 break;
3323 }
3324 }
3325}
3326
3327 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003328find_first_blank(pos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330 int c;
3331
3332 while (decl(posp) != -1)
3333 {
3334 c = gchar_pos(posp);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003335 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
3337 incl(posp);
3338 break;
3339 }
3340 }
3341}
3342
3343/*
3344 * Skip count/2 sentences and count/2 separating white spaces.
3345 */
3346 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003347findsent_forward(
3348 long count,
3349 int at_start_sent) /* cursor is at start of sentence */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 while (count--)
3352 {
3353 findsent(FORWARD, 1L);
3354 if (at_start_sent)
3355 find_first_blank(&curwin->w_cursor);
3356 if (count == 0 || at_start_sent)
3357 decl(&curwin->w_cursor);
3358 at_start_sent = !at_start_sent;
3359 }
3360}
3361
3362/*
3363 * Find word under cursor, cursor at end.
3364 * Used while an operator is pending, and in Visual mode.
3365 */
3366 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003367current_word(
3368 oparg_T *oap,
3369 long count,
3370 int include, /* TRUE: include word and white space */
3371 int bigword) /* FALSE == word, TRUE == WORD */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 pos_T start_pos;
3374 pos_T pos;
3375 int inclusive = TRUE;
3376 int include_white = FALSE;
3377
3378 cls_bigword = bigword;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003379 CLEAR_POS(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003382 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 dec_cursor();
3384
3385 /*
3386 * When Visual mode is not active, or when the VIsual area is only one
3387 * character, select the word and/or white space under the cursor.
3388 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003389 if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 {
3391 /*
3392 * Go to start of current word or white space.
3393 */
3394 back_in_line();
3395 start_pos = curwin->w_cursor;
3396
3397 /*
3398 * If the start is on white space, and white space should be included
3399 * (" word"), or start is not on white space, and white space should
3400 * not be included ("word"), find end of word.
3401 */
3402 if ((cls() == 0) == include)
3403 {
3404 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3405 return FAIL;
3406 }
3407 else
3408 {
3409 /*
3410 * If the start is not on white space, and white space should be
3411 * included ("word "), or start is on white space and white
3412 * space should not be included (" "), find start of word.
3413 * If we end up in the first column of the next line (single char
3414 * word) back up to end of the line.
3415 */
3416 fwd_word(1L, bigword, TRUE);
3417 if (curwin->w_cursor.col == 0)
3418 decl(&curwin->w_cursor);
3419 else
3420 oneleft();
3421
3422 if (include)
3423 include_white = TRUE;
3424 }
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (VIsual_active)
3427 {
3428 /* should do something when inclusive == FALSE ! */
3429 VIsual = start_pos;
3430 redraw_curbuf_later(INVERTED); /* update the inversion */
3431 }
3432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
3434 oap->start = start_pos;
3435 oap->motion_type = MCHAR;
3436 }
3437 --count;
3438 }
3439
3440 /*
3441 * When count is still > 0, extend with more objects.
3442 */
3443 while (count > 0)
3444 {
3445 inclusive = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003446 if (VIsual_active && LT_POS(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 /*
3449 * In Visual mode, with cursor at start: move cursor back.
3450 */
3451 if (decl(&curwin->w_cursor) == -1)
3452 return FAIL;
3453 if (include != (cls() != 0))
3454 {
3455 if (bck_word(1L, bigword, TRUE) == FAIL)
3456 return FAIL;
3457 }
3458 else
3459 {
3460 if (bckend_word(1L, bigword, TRUE) == FAIL)
3461 return FAIL;
3462 (void)incl(&curwin->w_cursor);
3463 }
3464 }
3465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 /*
3468 * Move cursor forward one word and/or white area.
3469 */
3470 if (incl(&curwin->w_cursor) == -1)
3471 return FAIL;
3472 if (include != (cls() == 0))
3473 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003474 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 return FAIL;
3476 /*
3477 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003478 * the first character on the line.
3479 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003481 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 inclusive = FALSE;
3483 }
3484 else
3485 {
3486 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3487 return FAIL;
3488 }
3489 }
3490 --count;
3491 }
3492
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003493 if (include_white && (cls() != 0
3494 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 /*
3497 * If we don't include white space at the end, move the start
3498 * to include some white space there. This makes "daw" work
3499 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003500 * word). Also when "2daw" deletes "word." at the end of the line
3501 * (cursor is at start of next line).
3502 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 */
3504 pos = curwin->w_cursor; /* save cursor position */
3505 curwin->w_cursor = start_pos;
3506 if (oneleft() == OK)
3507 {
3508 back_in_line();
3509 if (cls() == 0 && curwin->w_cursor.col > 0)
3510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 if (VIsual_active)
3512 VIsual = curwin->w_cursor;
3513 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 oap->start = curwin->w_cursor;
3515 }
3516 }
3517 curwin->w_cursor = pos; /* put cursor back at end */
3518 }
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (VIsual_active)
3521 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003522 if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 inc_cursor();
3524 if (VIsual_mode == 'V')
3525 {
3526 VIsual_mode = 'v';
3527 redraw_cmdline = TRUE; /* show mode later */
3528 }
3529 }
3530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 oap->inclusive = inclusive;
3532
3533 return OK;
3534}
3535
3536/*
3537 * Find sentence(s) under the cursor, cursor at end.
3538 * When Visual active, extend it by one or more sentences.
3539 */
3540 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003541current_sent(oparg_T *oap, long count, int include)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542{
3543 pos_T start_pos;
3544 pos_T pos;
3545 int start_blank;
3546 int c;
3547 int at_start_sent;
3548 long ncount;
3549
3550 start_pos = curwin->w_cursor;
3551 pos = start_pos;
3552 findsent(FORWARD, 1L); /* Find start of next sentence. */
3553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003555 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003557 if (VIsual_active && !EQUAL_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
3559extend:
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003560 if (LT_POS(start_pos, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
3562 /*
3563 * Cursor at start of Visual area.
3564 * Find out where we are:
3565 * - in the white space before a sentence
3566 * - in a sentence or just after it
3567 * - at the start of a sentence
3568 */
3569 at_start_sent = TRUE;
3570 decl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003571 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
3573 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003574 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576 at_start_sent = FALSE;
3577 break;
3578 }
3579 incl(&pos);
3580 }
3581 if (!at_start_sent)
3582 {
3583 findsent(BACKWARD, 1L);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003584 if (EQUAL_POS(curwin->w_cursor, start_pos))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 at_start_sent = TRUE; /* exactly at start of sentence */
3586 else
3587 /* inside a sentence, go to its end (start of next) */
3588 findsent(FORWARD, 1L);
3589 }
3590 if (include) /* "as" gets twice as much as "is" */
3591 count *= 2;
3592 while (count--)
3593 {
3594 if (at_start_sent)
3595 find_first_blank(&curwin->w_cursor);
3596 c = gchar_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01003597 if (!at_start_sent || (!include && !VIM_ISWHITE(c)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 findsent(BACKWARD, 1L);
3599 at_start_sent = !at_start_sent;
3600 }
3601 }
3602 else
3603 {
3604 /*
3605 * Cursor at end of Visual area.
3606 * Find out where we are:
3607 * - just before a sentence
3608 * - just before or in the white space before a sentence
3609 * - in a sentence
3610 */
3611 incl(&pos);
3612 at_start_sent = TRUE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003613 /* not just before a sentence */
3614 if (!EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 {
3616 at_start_sent = FALSE;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003617 while (LT_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 c = gchar_pos(&pos);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003620 if (!VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 at_start_sent = TRUE;
3623 break;
3624 }
3625 incl(&pos);
3626 }
3627 if (at_start_sent) /* in the sentence */
3628 findsent(BACKWARD, 1L);
3629 else /* in/before white before a sentence */
3630 curwin->w_cursor = start_pos;
3631 }
3632
3633 if (include) /* "as" gets twice as much as "is" */
3634 count *= 2;
3635 findsent_forward(count, at_start_sent);
3636 if (*p_sel == 'e')
3637 ++curwin->w_cursor.col;
3638 }
3639 return OK;
3640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003643 * If the cursor started on a blank, check if it is just before the start
3644 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003646 while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 incl(&pos);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003648 if (EQUAL_POS(pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
3650 start_blank = TRUE;
3651 find_first_blank(&start_pos); /* go back to first blank */
3652 }
3653 else
3654 {
3655 start_blank = FALSE;
3656 findsent(BACKWARD, 1L);
3657 start_pos = curwin->w_cursor;
3658 }
3659 if (include)
3660 ncount = count * 2;
3661 else
3662 {
3663 ncount = count;
3664 if (start_blank)
3665 --ncount;
3666 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003667 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 findsent_forward(ncount, TRUE);
3669 else
3670 decl(&curwin->w_cursor);
3671
3672 if (include)
3673 {
3674 /*
3675 * If the blank in front of the sentence is included, exclude the
3676 * blanks at the end of the sentence, go back to the first blank.
3677 * If there are no trailing blanks, try to include leading blanks.
3678 */
3679 if (start_blank)
3680 {
3681 find_first_blank(&curwin->w_cursor);
Bram Moolenaar1c465442017-03-12 20:10:05 +01003682 c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */
3683 if (VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 decl(&curwin->w_cursor);
3685 }
Bram Moolenaar1c465442017-03-12 20:10:05 +01003686 else if (c = gchar_cursor(), !VIM_ISWHITE(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 find_first_blank(&start_pos);
3688 }
3689
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 if (VIsual_active)
3691 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003692 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003693 if (EQUAL_POS(start_pos, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 goto extend;
3695 if (*p_sel == 'e')
3696 ++curwin->w_cursor.col;
3697 VIsual = start_pos;
3698 VIsual_mode = 'v';
Bram Moolenaar779f2fc2016-09-01 20:58:24 +02003699 redraw_cmdline = TRUE; /* show mode later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 redraw_curbuf_later(INVERTED); /* update the inversion */
3701 }
3702 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704 /* include a newline after the sentence, if there is one */
3705 if (incl(&curwin->w_cursor) == -1)
3706 oap->inclusive = TRUE;
3707 else
3708 oap->inclusive = FALSE;
3709 oap->start = start_pos;
3710 oap->motion_type = MCHAR;
3711 }
3712 return OK;
3713}
3714
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003715/*
3716 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003717 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003720current_block(
3721 oparg_T *oap,
3722 long count,
3723 int include, /* TRUE == include white space */
3724 int what, /* '(', '{', etc. */
3725 int other) /* ')', '}', etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726{
3727 pos_T old_pos;
3728 pos_T *pos = NULL;
3729 pos_T start_pos;
3730 pos_T *end_pos;
3731 pos_T old_start, old_end;
3732 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003733 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
3735 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003736 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 old_start = old_end;
3738
3739 /*
3740 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3741 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003742 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
3744 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003745 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 while (inindent(1))
3747 if (inc_cursor() != 0)
3748 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003749 if (gchar_cursor() == what)
3750 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 ++curwin->w_cursor.col;
3752 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003753 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 {
3755 old_start = VIsual;
3756 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3757 }
3758 else
3759 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
3761 /*
3762 * Search backwards for unclosed '(', '{', etc..
3763 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003764 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3765 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
3767 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003768 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 while (count-- > 0)
3770 {
3771 if ((pos = findmatch(NULL, what)) == NULL)
3772 break;
3773 curwin->w_cursor = *pos;
3774 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3775 }
3776 p_cpo = save_cpo;
3777
3778 /*
3779 * Search for matching ')', '}', etc.
3780 * Put this position in curwin->w_cursor.
3781 */
3782 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3783 {
3784 curwin->w_cursor = old_pos;
3785 return FAIL;
3786 }
3787 curwin->w_cursor = *end_pos;
3788
3789 /*
3790 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003791 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3792 * indent. But only if the resulting area is not smaller than what we
3793 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 */
3795 while (!include)
3796 {
3797 incl(&start_pos);
3798 sol = (curwin->w_cursor.col == 0);
3799 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003800 while (inindent(1))
3801 {
3802 sol = TRUE;
3803 if (decl(&curwin->w_cursor) != 0)
3804 break;
3805 }
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 /*
3808 * In Visual mode, when the resulting area is not bigger than what we
3809 * started with, extend it to the next block, and then exclude again.
3810 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003811 if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 && VIsual_active)
3813 {
3814 curwin->w_cursor = old_start;
3815 decl(&curwin->w_cursor);
3816 if ((pos = findmatch(NULL, what)) == NULL)
3817 {
3818 curwin->w_cursor = old_pos;
3819 return FAIL;
3820 }
3821 start_pos = *pos;
3822 curwin->w_cursor = *pos;
3823 if ((end_pos = findmatch(NULL, other)) == NULL)
3824 {
3825 curwin->w_cursor = old_pos;
3826 return FAIL;
3827 }
3828 curwin->w_cursor = *end_pos;
3829 }
3830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 break;
3832 }
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (VIsual_active)
3835 {
3836 if (*p_sel == 'e')
Bram Moolenaar8667d662015-09-01 18:27:49 +02003837 inc(&curwin->w_cursor);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003838 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 inc(&curwin->w_cursor); /* include the line break */
3840 VIsual = start_pos;
3841 VIsual_mode = 'v';
3842 redraw_curbuf_later(INVERTED); /* update the inversion */
3843 showmode();
3844 }
3845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 {
3847 oap->start = start_pos;
3848 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003849 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 incl(&curwin->w_cursor);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003852 else if (LTOREQ_POS(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003853 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003855 else
3856 /* End is before the start (no text in between <>, [], etc.): don't
3857 * operate on any text. */
3858 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 }
3860
3861 return OK;
3862}
3863
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003864static int in_html_tag(int);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003865
3866/*
3867 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3868 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3869 */
3870 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003871in_html_tag(
3872 int end_tag)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003873{
3874 char_u *line = ml_get_curline();
3875 char_u *p;
3876 int c;
3877 int lc = NUL;
3878 pos_T pos;
3879
3880#ifdef FEAT_MBYTE
3881 if (enc_dbcs)
3882 {
3883 char_u *lp = NULL;
3884
3885 /* We search forward until the cursor, because searching backwards is
3886 * very slow for DBCS encodings. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003887 for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003888 if (*p == '>' || *p == '<')
3889 {
3890 lc = *p;
3891 lp = p;
3892 }
3893 if (*p != '<') /* check for '<' under cursor */
3894 {
3895 if (lc != '<')
3896 return FALSE;
3897 p = lp;
3898 }
3899 }
3900 else
3901#endif
3902 {
3903 for (p = line + curwin->w_cursor.col; p > line; )
3904 {
3905 if (*p == '<') /* find '<' under/before cursor */
3906 break;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003907 MB_PTR_BACK(line, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003908 if (*p == '>') /* find '>' before cursor */
3909 break;
3910 }
3911 if (*p != '<')
3912 return FALSE;
3913 }
3914
3915 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003916 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003917
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003918 MB_PTR_ADV(p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003919 if (end_tag)
3920 /* check that there is a '/' after the '<' */
3921 return *p == '/';
3922
3923 /* check that there is no '/' after the '<' */
3924 if (*p == '/')
3925 return FALSE;
3926
3927 /* check that the matching '>' is not preceded by '/' */
3928 for (;;)
3929 {
3930 if (inc(&pos) < 0)
3931 return FALSE;
3932 c = *ml_get_pos(&pos);
3933 if (c == '>')
3934 break;
3935 lc = c;
3936 }
3937 return lc != '/';
3938}
3939
3940/*
3941 * Find tag block under the cursor, cursor at end.
3942 */
3943 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01003944current_tagblock(
3945 oparg_T *oap,
3946 long count_arg,
3947 int include) /* TRUE == include white space */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003948{
3949 long count = count_arg;
3950 long n;
3951 pos_T old_pos;
3952 pos_T start_pos;
3953 pos_T end_pos;
3954 pos_T old_start, old_end;
3955 char_u *spat, *epat;
3956 char_u *p;
3957 char_u *cp;
3958 int len;
3959 int r;
3960 int do_include = include;
3961 int save_p_ws = p_ws;
3962 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003963 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003964
3965 p_ws = FALSE;
3966
3967 old_pos = curwin->w_cursor;
3968 old_end = curwin->w_cursor; /* remember where we started */
3969 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003970 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003971 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003972
3973 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003974 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003975 */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003976 if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003977 {
3978 setpcmark();
3979
3980 /* ignore indent */
3981 while (inindent(1))
3982 if (inc_cursor() != 0)
3983 break;
3984
3985 if (in_html_tag(FALSE))
3986 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003987 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003988 while (*ml_get_cursor() != '>')
3989 if (inc_cursor() < 0)
3990 break;
3991 }
3992 else if (in_html_tag(TRUE))
3993 {
3994 /* cursor on end tag, move to just before it */
3995 while (*ml_get_cursor() != '<')
3996 if (dec_cursor() < 0)
3997 break;
3998 dec_cursor();
3999 old_end = curwin->w_cursor;
4000 }
4001 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004002 else if (LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004003 {
4004 old_start = VIsual;
4005 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
4006 }
4007 else
4008 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004009
4010again:
4011 /*
4012 * Search backwards for unclosed "<aaa>".
4013 * Put this position in start_pos.
4014 */
4015 for (n = 0; n < count; ++n)
4016 {
Bram Moolenaar45360022005-07-21 21:08:21 +00004017 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004018 (char_u *)"",
Bram Moolenaar48570482017-10-30 21:48:41 +01004019 (char_u *)"</[^>]*>", BACKWARD, NULL, 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00004020 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004021 {
4022 curwin->w_cursor = old_pos;
4023 goto theend;
4024 }
4025 }
4026 start_pos = curwin->w_cursor;
4027
4028 /*
4029 * Search for matching "</aaa>". First isolate the "aaa".
4030 */
4031 inc_cursor();
4032 p = ml_get_cursor();
Bram Moolenaar1c465442017-03-12 20:10:05 +01004033 for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004034 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004035 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004036 if (len == 0)
4037 {
4038 curwin->w_cursor = old_pos;
4039 goto theend;
4040 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004041 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004042 epat = alloc(len + 9);
4043 if (spat == NULL || epat == NULL)
4044 {
4045 vim_free(spat);
4046 vim_free(epat);
4047 curwin->w_cursor = old_pos;
4048 goto theend;
4049 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01004050 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004051 sprintf((char *)epat, "</%.*s>\\c", len, p);
4052
Bram Moolenaar48570482017-10-30 21:48:41 +01004053 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL,
Bram Moolenaar76929292008-01-06 19:07:36 +00004054 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004055
4056 vim_free(spat);
4057 vim_free(epat);
4058
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004059 if (r < 1 || LT_POS(curwin->w_cursor, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004060 {
4061 /* Can't find other end or it's before the previous end. Could be a
4062 * HTML tag that doesn't have a matching end. Search backwards for
4063 * another starting tag. */
4064 count = 1;
4065 curwin->w_cursor = start_pos;
4066 goto again;
4067 }
4068
4069 if (do_include || r < 1)
4070 {
4071 /* Include up to the '>'. */
4072 while (*ml_get_cursor() != '>')
4073 if (inc_cursor() < 0)
4074 break;
4075 }
4076 else
4077 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004078 char_u *c = ml_get_cursor();
4079
4080 /* Exclude the '<' of the end tag.
4081 * If the closing tag is on new line, do not decrement cursor, but
4082 * make operation exclusive, so that the linefeed will be selected */
4083 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
4084 /* do not decrement cursor */
4085 is_inclusive = FALSE;
4086 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004087 dec_cursor();
4088 }
4089 end_pos = curwin->w_cursor;
4090
4091 if (!do_include)
4092 {
4093 /* Exclude the start tag. */
4094 curwin->w_cursor = start_pos;
4095 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004096 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004097 {
4098 inc_cursor();
4099 start_pos = curwin->w_cursor;
4100 break;
4101 }
4102 curwin->w_cursor = end_pos;
4103
Bram Moolenaar45360022005-07-21 21:08:21 +00004104 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004105 * again. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004106 if (EQUAL_POS(start_pos, old_start) && EQUAL_POS(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004107 {
4108 do_include = TRUE;
4109 curwin->w_cursor = old_start;
4110 count = count_arg;
4111 goto again;
4112 }
4113 }
4114
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004115 if (VIsual_active)
4116 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004117 /* If the end is before the start there is no text between tags, select
4118 * the char under the cursor. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004119 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004120 curwin->w_cursor = start_pos;
4121 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01004122 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004123 VIsual = start_pos;
4124 VIsual_mode = 'v';
4125 redraw_curbuf_later(INVERTED); /* update the inversion */
4126 showmode();
4127 }
4128 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004129 {
4130 oap->start = start_pos;
4131 oap->motion_type = MCHAR;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004132 if (LT_POS(end_pos, start_pos))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00004133 {
4134 /* End is before the start: there is no text between tags; operate
4135 * on an empty area. */
4136 curwin->w_cursor = start_pos;
4137 oap->inclusive = FALSE;
4138 }
4139 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01004140 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00004141 }
4142 retval = OK;
4143
4144theend:
4145 p_ws = save_p_ws;
4146 return retval;
4147}
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004150current_par(
4151 oparg_T *oap,
4152 long count,
4153 int include, /* TRUE == include white space */
4154 int type) /* 'p' for paragraph, 'S' for section */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 linenr_T start_lnum;
4157 linenr_T end_lnum;
4158 int white_in_front;
4159 int dir;
4160 int start_is_white;
4161 int prev_start_is_white;
4162 int retval = OK;
4163 int do_white = FALSE;
4164 int t;
4165 int i;
4166
4167 if (type == 'S') /* not implemented yet */
4168 return FAIL;
4169
4170 start_lnum = curwin->w_cursor.lnum;
4171
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /*
4173 * When visual area is more than one line: extend it.
4174 */
4175 if (VIsual_active && start_lnum != VIsual.lnum)
4176 {
4177extend:
4178 if (start_lnum < VIsual.lnum)
4179 dir = BACKWARD;
4180 else
4181 dir = FORWARD;
4182 for (i = count; --i >= 0; )
4183 {
4184 if (start_lnum ==
4185 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4186 {
4187 retval = FAIL;
4188 break;
4189 }
4190
4191 prev_start_is_white = -1;
4192 for (t = 0; t < 2; ++t)
4193 {
4194 start_lnum += dir;
4195 start_is_white = linewhite(start_lnum);
4196 if (prev_start_is_white == start_is_white)
4197 {
4198 start_lnum -= dir;
4199 break;
4200 }
4201 for (;;)
4202 {
4203 if (start_lnum == (dir == BACKWARD
4204 ? 1 : curbuf->b_ml.ml_line_count))
4205 break;
4206 if (start_is_white != linewhite(start_lnum + dir)
4207 || (!start_is_white
4208 && startPS(start_lnum + (dir > 0
4209 ? 1 : 0), 0, 0)))
4210 break;
4211 start_lnum += dir;
4212 }
4213 if (!include)
4214 break;
4215 if (start_lnum == (dir == BACKWARD
4216 ? 1 : curbuf->b_ml.ml_line_count))
4217 break;
4218 prev_start_is_white = start_is_white;
4219 }
4220 }
4221 curwin->w_cursor.lnum = start_lnum;
4222 curwin->w_cursor.col = 0;
4223 return retval;
4224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 /*
4227 * First move back to the start_lnum of the paragraph or white lines
4228 */
4229 white_in_front = linewhite(start_lnum);
4230 while (start_lnum > 1)
4231 {
4232 if (white_in_front) /* stop at first white line */
4233 {
4234 if (!linewhite(start_lnum - 1))
4235 break;
4236 }
4237 else /* stop at first non-white line of start of paragraph */
4238 {
4239 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4240 break;
4241 }
4242 --start_lnum;
4243 }
4244
4245 /*
4246 * Move past the end of any white lines.
4247 */
4248 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004249 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4250 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
4252 --end_lnum;
4253 i = count;
4254 if (!include && white_in_front)
4255 --i;
4256 while (i--)
4257 {
4258 if (end_lnum == curbuf->b_ml.ml_line_count)
4259 return FAIL;
4260
4261 if (!include)
4262 do_white = linewhite(end_lnum + 1);
4263
4264 if (include || !do_white)
4265 {
4266 ++end_lnum;
4267 /*
4268 * skip to end of paragraph
4269 */
4270 while (end_lnum < curbuf->b_ml.ml_line_count
4271 && !linewhite(end_lnum + 1)
4272 && !startPS(end_lnum + 1, 0, 0))
4273 ++end_lnum;
4274 }
4275
4276 if (i == 0 && white_in_front && include)
4277 break;
4278
4279 /*
4280 * skip to end of white lines after paragraph
4281 */
4282 if (include || do_white)
4283 while (end_lnum < curbuf->b_ml.ml_line_count
4284 && linewhite(end_lnum + 1))
4285 ++end_lnum;
4286 }
4287
4288 /*
4289 * If there are no empty lines at the end, try to find some empty lines at
4290 * the start (unless that has been done already).
4291 */
4292 if (!white_in_front && !linewhite(end_lnum) && include)
4293 while (start_lnum > 1 && linewhite(start_lnum - 1))
4294 --start_lnum;
4295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if (VIsual_active)
4297 {
4298 /* Problem: when doing "Vipipip" nothing happens in a single white
4299 * line, we get stuck there. Trap this here. */
4300 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4301 goto extend;
Bram Moolenaar84b2a382017-02-17 11:40:00 +01004302 if (VIsual.lnum != start_lnum)
4303 {
4304 VIsual.lnum = start_lnum;
4305 VIsual.col = 0;
4306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 VIsual_mode = 'V';
4308 redraw_curbuf_later(INVERTED); /* update the inversion */
4309 showmode();
4310 }
4311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 oap->start.lnum = start_lnum;
4314 oap->start.col = 0;
4315 oap->motion_type = MLINE;
4316 }
4317 curwin->w_cursor.lnum = end_lnum;
4318 curwin->w_cursor.col = 0;
4319
4320 return OK;
4321}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004322
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004323static int find_next_quote(char_u *top_ptr, int col, int quotechar, char_u *escape);
4324static int find_prev_quote(char_u *line, int col_start, int quotechar, char_u *escape);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004325
4326/*
4327 * Search quote char from string line[col].
4328 * Quote character escaped by one of the characters in "escape" is not counted
4329 * as a quote.
4330 * Returns column number of "quotechar" or -1 when not found.
4331 */
4332 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004333find_next_quote(
4334 char_u *line,
4335 int col,
4336 int quotechar,
4337 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004338{
4339 int c;
4340
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004341 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004342 {
4343 c = line[col];
4344 if (c == NUL)
4345 return -1;
4346 else if (escape != NULL && vim_strchr(escape, c))
4347 ++col;
4348 else if (c == quotechar)
4349 break;
4350#ifdef FEAT_MBYTE
4351 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004352 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004355 ++col;
4356 }
4357 return col;
4358}
4359
4360/*
4361 * Search backwards in "line" from column "col_start" to find "quotechar".
4362 * Quote character escaped by one of the characters in "escape" is not counted
4363 * as a quote.
4364 * Return the found column or zero.
4365 */
4366 static int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004367find_prev_quote(
4368 char_u *line,
4369 int col_start,
4370 int quotechar,
4371 char_u *escape) /* escape characters, can be NULL */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004372{
4373 int n;
4374
4375 while (col_start > 0)
4376 {
4377 --col_start;
4378#ifdef FEAT_MBYTE
4379 col_start -= (*mb_head_off)(line, line + col_start);
4380#endif
4381 n = 0;
4382 if (escape != NULL)
4383 while (col_start - n > 0 && vim_strchr(escape,
4384 line[col_start - n - 1]) != NULL)
4385 ++n;
4386 if (n & 1)
4387 col_start -= n; /* uneven number of escape chars, skip it */
4388 else if (line[col_start] == quotechar)
4389 break;
4390 }
4391 return col_start;
4392}
4393
4394/*
4395 * Find quote under the cursor, cursor at end.
4396 * Returns TRUE if found, else FALSE.
4397 */
4398 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004399current_quote(
4400 oparg_T *oap,
4401 long count,
4402 int include, /* TRUE == include quote char */
4403 int quotechar) /* Quote character */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004404{
4405 char_u *line = ml_get_curline();
4406 int col_end;
4407 int col_start = curwin->w_cursor.col;
4408 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004409 int vis_empty = TRUE; /* Visual selection <= 1 char */
4410 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004411 int inside_quotes = FALSE; /* Looks like "i'" done before */
4412 int selected_quote = FALSE; /* Has quote inside selection */
4413 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004414
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004415 /* Correct cursor when 'selection' is "exclusive". */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004416 if (VIsual_active)
4417 {
Bram Moolenaar46522af2017-02-18 23:12:01 +01004418 /* this only works within one line */
4419 if (VIsual.lnum != curwin->w_cursor.lnum)
4420 return FALSE;
4421
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004422 vis_bef_curs = LT_POS(VIsual, curwin->w_cursor);
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004423 if (*p_sel == 'e')
4424 {
4425 if (!vis_bef_curs)
4426 {
4427 /* VIsual needs to be start of Visual selection. */
4428 pos_T t = curwin->w_cursor;
4429
4430 curwin->w_cursor = VIsual;
4431 VIsual = t;
4432 vis_bef_curs = TRUE;
4433 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004434 dec_cursor();
Bram Moolenaarc5e2b042017-06-05 16:37:07 +02004435 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004436 vis_empty = EQUAL_POS(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004437 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004438
4439 if (!vis_empty)
4440 {
4441 /* Check if the existing selection exactly spans the text inside
4442 * quotes. */
4443 if (vis_bef_curs)
4444 {
4445 inside_quotes = VIsual.col > 0
4446 && line[VIsual.col - 1] == quotechar
4447 && line[curwin->w_cursor.col] != NUL
4448 && line[curwin->w_cursor.col + 1] == quotechar;
4449 i = VIsual.col;
4450 col_end = curwin->w_cursor.col;
4451 }
4452 else
4453 {
4454 inside_quotes = curwin->w_cursor.col > 0
4455 && line[curwin->w_cursor.col - 1] == quotechar
4456 && line[VIsual.col] != NUL
4457 && line[VIsual.col + 1] == quotechar;
4458 i = curwin->w_cursor.col;
4459 col_end = VIsual.col;
4460 }
4461
4462 /* Find out if we have a quote in the selection. */
4463 while (i <= col_end)
4464 if (line[i++] == quotechar)
4465 {
4466 selected_quote = TRUE;
4467 break;
4468 }
4469 }
4470
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004471 if (!vis_empty && line[col_start] == quotechar)
4472 {
4473 /* Already selecting something and on a quote character. Find the
4474 * next quoted string. */
4475 if (vis_bef_curs)
4476 {
4477 /* Assume we are on a closing quote: move to after the next
4478 * opening quote. */
4479 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4480 if (col_start < 0)
4481 return FALSE;
4482 col_end = find_next_quote(line, col_start + 1, quotechar,
4483 curbuf->b_p_qe);
4484 if (col_end < 0)
4485 {
4486 /* We were on a starting quote perhaps? */
4487 col_end = col_start;
4488 col_start = curwin->w_cursor.col;
4489 }
4490 }
4491 else
4492 {
4493 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4494 if (line[col_end] != quotechar)
4495 return FALSE;
4496 col_start = find_prev_quote(line, col_end, quotechar,
4497 curbuf->b_p_qe);
4498 if (line[col_start] != quotechar)
4499 {
4500 /* We were on an ending quote perhaps? */
4501 col_start = col_end;
4502 col_end = curwin->w_cursor.col;
4503 }
4504 }
4505 }
4506 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004507
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004508 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004509 {
4510 int first_col = col_start;
4511
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004512 if (!vis_empty)
4513 {
4514 if (vis_bef_curs)
4515 first_col = find_next_quote(line, col_start, quotechar, NULL);
4516 else
4517 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4518 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004519
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004520 /* The cursor is on a quote, we don't know if it's the opening or
4521 * closing quote. Search from the start of the line to find out.
4522 * Also do this when there is a Visual area, a' may leave the cursor
4523 * in between two strings. */
4524 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004525 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004526 {
4527 /* Find open quote character. */
4528 col_start = find_next_quote(line, col_start, quotechar, NULL);
4529 if (col_start < 0 || col_start > first_col)
4530 return FALSE;
4531 /* Find close quote character. */
4532 col_end = find_next_quote(line, col_start + 1, quotechar,
4533 curbuf->b_p_qe);
4534 if (col_end < 0)
4535 return FALSE;
4536 /* If is cursor between start and end quote character, it is
4537 * target text object. */
4538 if (col_start <= first_col && first_col <= col_end)
4539 break;
4540 col_start = col_end + 1;
4541 }
4542 }
4543 else
4544 {
4545 /* Search backward for a starting quote. */
4546 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4547 if (line[col_start] != quotechar)
4548 {
4549 /* No quote before the cursor, look after the cursor. */
4550 col_start = find_next_quote(line, col_start, quotechar, NULL);
4551 if (col_start < 0)
4552 return FALSE;
4553 }
4554
4555 /* Find close quote character. */
4556 col_end = find_next_quote(line, col_start + 1, quotechar,
4557 curbuf->b_p_qe);
4558 if (col_end < 0)
4559 return FALSE;
4560 }
4561
4562 /* When "include" is TRUE, include spaces after closing quote or before
4563 * the starting quote. */
4564 if (include)
4565 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01004566 if (VIM_ISWHITE(line[col_end + 1]))
4567 while (VIM_ISWHITE(line[col_end + 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004568 ++col_end;
4569 else
Bram Moolenaar1c465442017-03-12 20:10:05 +01004570 while (col_start > 0 && VIM_ISWHITE(line[col_start - 1]))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004571 --col_start;
4572 }
4573
Bram Moolenaarab194812005-09-14 21:40:12 +00004574 /* Set start position. After vi" another i" must include the ".
4575 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004576 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004577 ++col_start;
4578 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004579 if (VIsual_active)
4580 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004581 /* Set the start of the Visual area when the Visual area was empty, we
4582 * were just inside quotes or the Visual area didn't start at a quote
4583 * and didn't include a quote.
4584 */
4585 if (vis_empty
4586 || (vis_bef_curs
4587 && !selected_quote
4588 && (inside_quotes
4589 || (line[VIsual.col] != quotechar
4590 && (VIsual.col == 0
4591 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004592 {
4593 VIsual = curwin->w_cursor;
4594 redraw_curbuf_later(INVERTED);
4595 }
4596 }
4597 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004598 {
4599 oap->start = curwin->w_cursor;
4600 oap->motion_type = MCHAR;
4601 }
4602
4603 /* Set end position. */
4604 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004605 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004606 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004607 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004608 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004609 if (VIsual_active)
4610 {
4611 if (vis_empty || vis_bef_curs)
4612 {
4613 /* decrement cursor when 'selection' is not exclusive */
4614 if (*p_sel != 'e')
4615 dec_cursor();
4616 }
4617 else
4618 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004619 /* Cursor is at start of Visual area. Set the end of the Visual
4620 * area when it was just inside quotes or it didn't end at a
4621 * quote. */
4622 if (inside_quotes
4623 || (!selected_quote
4624 && line[VIsual.col] != quotechar
4625 && (line[VIsual.col] == NUL
4626 || line[VIsual.col + 1] != quotechar)))
4627 {
4628 dec_cursor();
4629 VIsual = curwin->w_cursor;
4630 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004631 curwin->w_cursor.col = col_start;
4632 }
4633 if (VIsual_mode == 'V')
4634 {
4635 VIsual_mode = 'v';
4636 redraw_cmdline = TRUE; /* show mode later */
4637 }
4638 }
4639 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004640 {
4641 /* Set inclusive and other oap's flags. */
4642 oap->inclusive = inclusive;
4643 }
4644
4645 return OK;
4646}
4647
4648#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004650static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004651
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004652/*
4653 * Find next search match under cursor, cursor at end.
4654 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004655 */
4656 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004657current_search(
4658 long count,
4659 int forward) /* move forward or backwards */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004660{
4661 pos_T start_pos; /* position before the pattern */
4662 pos_T orig_pos; /* position of the cursor at beginning */
4663 pos_T pos; /* position after the pattern */
4664 int i;
4665 int dir;
4666 int result; /* result of various function calls */
4667 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004668 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004669 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004670 int one_char;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004671 int direction = forward ? FORWARD : BACKWARD;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004672
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004673 /* wrapping should not occur */
4674 p_ws = FALSE;
4675
4676 /* Correct cursor when 'selection' is exclusive */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004677 if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor))
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004678 dec_cursor();
4679
4680 if (VIsual_active)
4681 {
4682 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004683
4684 pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004685
4686 /* make sure, searching further will extend the match */
4687 if (VIsual_active)
4688 {
4689 if (forward)
4690 incl(&pos);
4691 else
4692 decl(&pos);
4693 }
4694 }
4695 else
Bram Moolenaar268a06c2016-04-21 09:07:01 +02004696 orig_pos = pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004697
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004698 /* Is the pattern is zero-width?, this time, don't care about the direction
4699 */
4700 one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor,
4701 FORWARD);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004702 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004703 {
4704 p_ws = old_p_ws;
4705 return FAIL; /* pattern not found */
4706 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004707
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004708 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004709 * The trick is to first search backwards and then search forward again,
4710 * so that a match at the current cursor position will be correctly
4711 * captured.
4712 */
4713 for (i = 0; i < 2; i++)
4714 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004715 if (forward)
4716 dir = i;
4717 else
4718 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004719
4720 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004721 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004722 flags = SEARCH_END;
4723
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004724 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4725 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004726 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004727
4728 /* First search may fail, but then start searching from the
4729 * beginning of the file (cursor might be on the search match)
4730 * except when Visual mode is active, so that extending the visual
4731 * selection works. */
4732 if (!result && i) /* not found, abort */
4733 {
4734 curwin->w_cursor = orig_pos;
4735 if (VIsual_active)
4736 VIsual = save_VIsual;
4737 p_ws = old_p_ws;
4738 return FAIL;
4739 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004740 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004741 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004742 if (forward)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004743 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004744 /* try again from start of buffer */
4745 CLEAR_POS(&pos);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004746 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004747 else
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004748 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004749 /* try again from end of buffer */
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004750 /* searching backwards, so set pos to last line and col */
4751 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004752 pos.col = (colnr_T)STRLEN(
4753 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004754 }
4755 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004756 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004757 }
4758
4759 start_pos = pos;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004760 flags = forward ? SEARCH_END : SEARCH_START;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004761
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004762 /* Check again from the current cursor position,
4763 * since the next match might actually by only one char wide */
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004764 one_char = is_one_char(spats[last_idx].pat, FALSE, &pos, direction);
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004765 if (one_char < 0)
4766 /* search failed, abort */
4767 return FAIL;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004768
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004769 /* move to match, except for zero-width matches, in which case, we are
4770 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004771 if (!one_char)
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004772 result = searchit(curwin, curbuf, &pos, direction,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004773 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0,
4774 NULL, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004775
4776 if (!VIsual_active)
4777 VIsual = start_pos;
4778
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004779 curwin->w_cursor = pos;
4780 VIsual_active = TRUE;
4781 VIsual_mode = 'v';
4782
4783 if (VIsual_active)
4784 {
4785 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004786 if (*p_sel == 'e')
4787 {
4788 /* Correction for exclusive selection depends on the direction. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004789 if (forward && LTOREQ_POS(VIsual, curwin->w_cursor))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004790 inc_cursor();
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004791 else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual))
Bram Moolenaar718f0072012-10-03 13:35:51 +02004792 inc(&VIsual);
4793 }
4794
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004795 }
4796
4797#ifdef FEAT_FOLDING
4798 if (fdo_flags & FDO_SEARCH && KeyTyped)
4799 foldOpenCursor();
4800#endif
4801
4802 may_start_select('c');
4803#ifdef FEAT_MOUSE
4804 setmouse();
4805#endif
4806#ifdef FEAT_CLIPBOARD
4807 /* Make sure the clipboard gets updated. Needed because start and
4808 * end are still the same, and the selection needs to be owned */
4809 clip_star.vmode = NUL;
4810#endif
4811 redraw_curbuf_later(INVERTED);
4812 showmode();
4813
4814 return OK;
4815}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004816
4817/*
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004818 * Check if the pattern is one character long or zero-width.
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004819 * If move is TRUE, check from the beginning of the buffer, else from position
4820 * "cur".
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004821 * "direction" is FORWARD or BACKWARD.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004822 * Returns TRUE, FALSE or -1 for failure.
4823 */
4824 static int
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004825is_one_char(char_u *pattern, int move, pos_T *cur, int direction)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004826{
4827 regmmatch_T regmatch;
4828 int nmatched = 0;
4829 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004830 pos_T pos;
4831 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004832 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004833
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004834 if (pattern == NULL)
4835 pattern = spats[last_idx].pat;
4836
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004837 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4838 SEARCH_KEEP, &regmatch) == FAIL)
4839 return -1;
4840
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004841 /* init startcol correctly */
4842 regmatch.startpos[0].col = -1;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004843 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004844 if (move)
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004845 {
4846 CLEAR_POS(&pos);
4847 }
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004848 else
4849 {
Bram Moolenaaradd8dce2017-06-05 19:56:04 +02004850 pos = *cur;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004851 /* accept a match at the cursor position */
4852 flag = SEARCH_START;
4853 }
4854
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004855 if (searchit(curwin, curbuf, &pos, direction, pattern, 1,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004856 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004857 {
4858 /* Zero-width pattern should match somewhere, then we can check if
4859 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004860 called_emsg = FALSE;
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004861 do
4862 {
4863 regmatch.startpos[0].col++;
4864 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004865 pos.lnum, regmatch.startpos[0].col, NULL, NULL);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004866 if (!nmatched)
4867 break;
Bram Moolenaar22ab5472017-09-26 12:28:45 +02004868 } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col
4869 : regmatch.startpos[0].col > pos.col);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004870
4871 if (!called_emsg)
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004872 {
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004873 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004874 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4875 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaar6835dc62016-07-24 17:33:05 +02004876 /* one char width */
4877 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4878 result = TRUE;
4879 }
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004880 }
4881
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004882 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004883 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004884 return result;
4885}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4888 || defined(PROTO)
4889/*
4890 * return TRUE if line 'lnum' is empty or has white chars only.
4891 */
4892 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004893linewhite(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 char_u *p;
4896
4897 p = skipwhite(ml_get(lnum));
4898 return (*p == NUL);
4899}
4900#endif
4901
4902#if defined(FEAT_FIND_ID) || defined(PROTO)
4903/*
4904 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004905 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004908find_pattern_in_path(
4909 char_u *ptr, /* pointer to search pattern */
4910 int dir UNUSED, /* direction of expansion */
4911 int len, /* length of search pattern */
4912 int whole, /* match whole words only */
4913 int skip_comments, /* don't match inside comments */
4914 int type, /* Type of search; are we looking for a type?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 a macro? */
Bram Moolenaar764b23c2016-01-30 21:10:09 +01004916 long count,
4917 int action, /* What to do when we find it */
4918 linenr_T start_lnum, /* first line to start searching */
4919 linenr_T end_lnum) /* last line for searching */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 SearchedFile *files; /* Stack of included files */
4922 SearchedFile *bigger; /* When we need more space */
4923 int max_path_depth = 50;
4924 long match_count = 1;
4925
4926 char_u *pat;
4927 char_u *new_fname;
4928 char_u *curr_fname = curbuf->b_fname;
4929 char_u *prev_fname = NULL;
4930 linenr_T lnum;
4931 int depth;
4932 int depth_displayed; /* For type==CHECK_PATH */
4933 int old_files;
4934 int already_searched;
4935 char_u *file_line;
4936 char_u *line;
4937 char_u *p;
4938 char_u save_char;
4939 int define_matched;
4940 regmatch_T regmatch;
4941 regmatch_T incl_regmatch;
4942 regmatch_T def_regmatch;
4943 int matched = FALSE;
4944 int did_show = FALSE;
4945 int found = FALSE;
4946 int i;
4947 char_u *already = NULL;
4948 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004949 char_u *inc_opt = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004950#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 win_T *curwin_save = NULL;
4952#endif
4953
4954 regmatch.regprog = NULL;
4955 incl_regmatch.regprog = NULL;
4956 def_regmatch.regprog = NULL;
4957
4958 file_line = alloc(LSIZE);
4959 if (file_line == NULL)
4960 return;
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (type != CHECK_PATH && type != FIND_DEFINE
4963#ifdef FEAT_INS_EXPAND
4964 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4965 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004966 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967#endif
4968 )
4969 {
4970 pat = alloc(len + 5);
4971 if (pat == NULL)
4972 goto fpip_end;
4973 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4974 /* ignore case according to p_ic, p_scs and pat */
4975 regmatch.rm_ic = ignorecase(pat);
4976 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4977 vim_free(pat);
4978 if (regmatch.regprog == NULL)
4979 goto fpip_end;
4980 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004981 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4982 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004984 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (incl_regmatch.regprog == NULL)
4986 goto fpip_end;
4987 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4988 }
4989 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4990 {
4991 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4992 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4993 if (def_regmatch.regprog == NULL)
4994 goto fpip_end;
4995 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4996 }
4997 files = (SearchedFile *)lalloc_clear((long_u)
4998 (max_path_depth * sizeof(SearchedFile)), TRUE);
4999 if (files == NULL)
5000 goto fpip_end;
5001 old_files = max_path_depth;
5002 depth = depth_displayed = -1;
5003
5004 lnum = start_lnum;
5005 if (end_lnum > curbuf->b_ml.ml_line_count)
5006 end_lnum = curbuf->b_ml.ml_line_count;
5007 if (lnum > end_lnum) /* do at least one line */
5008 lnum = end_lnum;
5009 line = ml_get(lnum);
5010
5011 for (;;)
5012 {
5013 if (incl_regmatch.regprog != NULL
5014 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
5015 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005016 char_u *p_fname = (curr_fname == curbuf->b_fname)
5017 ? curbuf->b_ffname : curr_fname;
5018
5019 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
5020 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
5021 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005022 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00005023 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
5024 else
5025 /* Use text after match with 'include'. */
5026 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005027 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 already_searched = FALSE;
5029 if (new_fname != NULL)
5030 {
5031 /* Check whether we have already searched in this file */
5032 for (i = 0;; i++)
5033 {
5034 if (i == depth + 1)
5035 i = old_files;
5036 if (i == max_path_depth)
5037 break;
5038 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
5039 {
5040 if (type != CHECK_PATH &&
5041 action == ACTION_SHOW_ALL && files[i].matched)
5042 {
5043 msg_putchar('\n'); /* cursor below last one */
5044 if (!got_int) /* don't display if 'q'
5045 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005046 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 msg_home_replace_hl(new_fname);
5049 MSG_PUTS(_(" (includes previously listed match)"));
5050 prev_fname = NULL;
5051 }
5052 }
5053 vim_free(new_fname);
5054 new_fname = NULL;
5055 already_searched = TRUE;
5056 break;
5057 }
5058 }
5059 }
5060
5061 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
5062 || (new_fname == NULL && !already_searched)))
5063 {
5064 if (did_show)
5065 msg_putchar('\n'); /* cursor below last one */
5066 else
5067 {
5068 gotocmdline(TRUE); /* cursor at status line */
5069 MSG_PUTS_TITLE(_("--- Included files "));
5070 if (action != ACTION_SHOW_ALL)
5071 MSG_PUTS_TITLE(_("not found "));
5072 MSG_PUTS_TITLE(_("in path ---\n"));
5073 }
5074 did_show = TRUE;
5075 while (depth_displayed < depth && !got_int)
5076 {
5077 ++depth_displayed;
5078 for (i = 0; i < depth_displayed; i++)
5079 MSG_PUTS(" ");
5080 msg_home_replace(files[depth_displayed].name);
5081 MSG_PUTS(" -->\n");
5082 }
5083 if (!got_int) /* don't display if 'q' typed
5084 for "--more--" message */
5085 {
5086 for (i = 0; i <= depth_displayed; i++)
5087 MSG_PUTS(" ");
5088 if (new_fname != NULL)
5089 {
5090 /* using "new_fname" is more reliable, e.g., when
5091 * 'includeexpr' is set. */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005092 msg_outtrans_attr(new_fname, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 else
5095 {
5096 /*
5097 * Isolate the file name.
5098 * Include the surrounding "" or <> if present.
5099 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005100 if (inc_opt != NULL
5101 && strstr((char *)inc_opt, "\\zs") != NULL)
5102 {
5103 /* pattern contains \zs, use the match */
5104 p = incl_regmatch.startp[0];
5105 i = (int)(incl_regmatch.endp[0]
5106 - incl_regmatch.startp[0]);
5107 }
5108 else
5109 {
5110 /* find the file name after the end of the match */
5111 for (p = incl_regmatch.endp[0];
5112 *p && !vim_isfilec(*p); p++)
5113 ;
5114 for (i = 0; vim_isfilec(p[i]); i++)
5115 ;
5116 }
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (i == 0)
5119 {
5120 /* Nothing found, use the rest of the line. */
5121 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005122 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02005124 /* Avoid checking before the start of the line, can
5125 * happen if \zs appears in the regexp. */
5126 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 if (p[-1] == '"' || p[-1] == '<')
5129 {
5130 --p;
5131 ++i;
5132 }
5133 if (p[i] == '"' || p[i] == '>')
5134 ++i;
5135 }
5136 save_char = p[i];
5137 p[i] = NUL;
Bram Moolenaar8820b482017-03-16 17:23:31 +01005138 msg_outtrans_attr(p, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 p[i] = save_char;
5140 }
5141
5142 if (new_fname == NULL && action == ACTION_SHOW_ALL)
5143 {
5144 if (already_searched)
5145 MSG_PUTS(_(" (Already listed)"));
5146 else
5147 MSG_PUTS(_(" NOT FOUND"));
5148 }
5149 }
5150 out_flush(); /* output each line directly */
5151 }
5152
5153 if (new_fname != NULL)
5154 {
5155 /* Push the new file onto the file stack */
5156 if (depth + 1 == old_files)
5157 {
5158 bigger = (SearchedFile *)lalloc((long_u)(
5159 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
5160 if (bigger != NULL)
5161 {
5162 for (i = 0; i <= depth; i++)
5163 bigger[i] = files[i];
5164 for (i = depth + 1; i < old_files + max_path_depth; i++)
5165 {
5166 bigger[i].fp = NULL;
5167 bigger[i].name = NULL;
5168 bigger[i].lnum = 0;
5169 bigger[i].matched = FALSE;
5170 }
5171 for (i = old_files; i < max_path_depth; i++)
5172 bigger[i + max_path_depth] = files[i];
5173 old_files += max_path_depth;
5174 max_path_depth *= 2;
5175 vim_free(files);
5176 files = bigger;
5177 }
5178 }
5179 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
5180 == NULL)
5181 vim_free(new_fname);
5182 else
5183 {
5184 if (++depth == old_files)
5185 {
5186 /*
5187 * lalloc() for 'bigger' must have failed above. We
5188 * will forget one of our already visited files now.
5189 */
5190 vim_free(files[old_files].name);
5191 ++old_files;
5192 }
5193 files[depth].name = curr_fname = new_fname;
5194 files[depth].lnum = 0;
5195 files[depth].matched = FALSE;
5196#ifdef FEAT_INS_EXPAND
5197 if (action == ACTION_EXPAND)
5198 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00005199 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00005200 vim_snprintf((char*)IObuff, IOSIZE,
5201 _("Scanning included file: %s"),
5202 (char *)new_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005203 msg_trunc_attr(IObuff, TRUE, HL_ATTR(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005205 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005207 if (p_verbose >= 5)
5208 {
5209 verbose_enter();
5210 smsg((char_u *)_("Searching included file %s"),
5211 (char *)new_fname);
5212 verbose_leave();
5213 }
5214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 }
5217 }
5218 else
5219 {
5220 /*
5221 * Check if the line is a define (type == FIND_DEFINE)
5222 */
5223 p = line;
5224search_line:
5225 define_matched = FALSE;
5226 if (def_regmatch.regprog != NULL
5227 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5228 {
5229 /*
5230 * Pattern must be first identifier after 'define', so skip
5231 * to that position before checking for match of pattern. Also
5232 * don't let it match beyond the end of this identifier.
5233 */
5234 p = def_regmatch.endp[0];
5235 while (*p && !vim_iswordc(*p))
5236 p++;
5237 define_matched = TRUE;
5238 }
5239
5240 /*
5241 * Look for a match. Don't do this if we are looking for a
5242 * define and this line didn't match define_prog above.
5243 */
5244 if (def_regmatch.regprog == NULL || define_matched)
5245 {
5246 if (define_matched
5247#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005248 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#endif
5250 )
5251 {
5252 /* compare the first "len" chars from "ptr" */
5253 startp = skipwhite(p);
5254 if (p_ic)
5255 matched = !MB_STRNICMP(startp, ptr, len);
5256 else
5257 matched = !STRNCMP(startp, ptr, len);
5258 if (matched && define_matched && whole
5259 && vim_iswordc(startp[len]))
5260 matched = FALSE;
5261 }
5262 else if (regmatch.regprog != NULL
5263 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5264 {
5265 matched = TRUE;
5266 startp = regmatch.startp[0];
5267 /*
5268 * Check if the line is not a comment line (unless we are
5269 * looking for a define). A line starting with "# define"
5270 * is not considered to be a comment line.
5271 */
5272 if (!define_matched && skip_comments)
5273 {
5274#ifdef FEAT_COMMENTS
5275 if ((*line != '#' ||
5276 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005277 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 matched = FALSE;
5279
5280 /*
5281 * Also check for a "/ *" or "/ /" before the match.
5282 * Skips lines like "int backwards; / * normal index
5283 * * /" when looking for "normal".
5284 * Note: Doesn't skip "/ *" in comments.
5285 */
5286 p = skipwhite(line);
5287 if (matched
5288 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5289#endif
5290 for (p = line; *p && p < startp; ++p)
5291 {
5292 if (matched
5293 && p[0] == '/'
5294 && (p[1] == '*' || p[1] == '/'))
5295 {
5296 matched = FALSE;
5297 /* After "//" all text is comment */
5298 if (p[1] == '/')
5299 break;
5300 ++p;
5301 }
5302 else if (!matched && p[0] == '*' && p[1] == '/')
5303 {
5304 /* Can find match after "* /". */
5305 matched = TRUE;
5306 ++p;
5307 }
5308 }
5309 }
5310 }
5311 }
5312 }
5313 if (matched)
5314 {
5315#ifdef FEAT_INS_EXPAND
5316 if (action == ACTION_EXPAND)
5317 {
5318 int reuse = 0;
5319 int add_r;
5320 char_u *aux;
5321
5322 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5323 break;
5324 found = TRUE;
5325 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (vim_iswordp(p))
5330 goto exit_matched;
5331 p = find_word_start(p);
5332 }
5333 p = find_word_end(p);
5334 i = (int)(p - aux);
5335
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005340
5341 /* Get the next line: when "depth" < 0 from the current
5342 * buffer, otherwise from the included file. Jump to
5343 * exit_matched when past the last line. */
5344 if (depth < 0)
5345 {
5346 if (lnum >= end_lnum)
5347 goto exit_matched;
5348 line = ml_get(++lnum);
5349 }
5350 else if (vim_fgets(line = file_line,
5351 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 goto exit_matched;
5353
5354 /* we read a line, set "already" to check this "line" later
5355 * if depth >= 0 we'll increase files[depth].lnum far
5356 * bellow -- Acevedo */
5357 already = aux = p = skipwhite(line);
5358 p = find_word_start(p);
5359 p = find_word_end(p);
5360 if (p > aux)
5361 {
5362 if (*aux != ')' && IObuff[i-1] != TAB)
5363 {
5364 if (IObuff[i-1] != ' ')
5365 IObuff[i++] = ' ';
5366 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5367 if (p_js
5368 && (IObuff[i-2] == '.'
5369 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5370 && (IObuff[i-2] == '?'
5371 || IObuff[i-2] == '!'))))
5372 IObuff[i++] = ' ';
5373 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005374 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (p - aux >= IOSIZE - i)
5376 p = aux + IOSIZE - i - 1;
5377 STRNCPY(IObuff + i, aux, p - aux);
5378 i += (int)(p - aux);
5379 reuse |= CONT_S_IPOS;
5380 }
5381 IObuff[i] = NUL;
5382 aux = IObuff;
5383
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 goto exit_matched;
5386 }
5387
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005388 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5390 dir, reuse);
5391 if (add_r == OK)
5392 /* if dir was BACKWARD then honor it just once */
5393 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005394 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 break;
5396 }
5397 else
5398#endif
5399 if (action == ACTION_SHOW_ALL)
5400 {
5401 found = TRUE;
5402 if (!did_show)
5403 gotocmdline(TRUE); /* cursor at status line */
5404 if (curr_fname != prev_fname)
5405 {
5406 if (did_show)
5407 msg_putchar('\n'); /* cursor below last one */
5408 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005409 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 msg_home_replace_hl(curr_fname);
5411 prev_fname = curr_fname;
5412 }
5413 did_show = TRUE;
5414 if (!got_int)
5415 show_pat_in_path(line, type, TRUE, action,
5416 (depth == -1) ? NULL : files[depth].fp,
5417 (depth == -1) ? &lnum : &files[depth].lnum,
5418 match_count++);
5419
5420 /* Set matched flag for this file and all the ones that
5421 * include it */
5422 for (i = 0; i <= depth; ++i)
5423 files[i].matched = TRUE;
5424 }
5425 else if (--count <= 0)
5426 {
5427 found = TRUE;
5428 if (depth == -1 && lnum == curwin->w_cursor.lnum
Bram Moolenaar4033c552017-09-16 20:54:51 +02005429#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 && g_do_tagpreview == 0
5431#endif
5432 )
5433 EMSG(_("E387: Match is on current line"));
5434 else if (action == ACTION_SHOW)
5435 {
5436 show_pat_in_path(line, type, did_show, action,
5437 (depth == -1) ? NULL : files[depth].fp,
5438 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5439 did_show = TRUE;
5440 }
5441 else
5442 {
5443#ifdef FEAT_GUI
5444 need_mouse_correct = TRUE;
5445#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02005446#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 /* ":psearch" uses the preview window */
5448 if (g_do_tagpreview != 0)
5449 {
5450 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005451 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 }
5453#endif
5454 if (action == ACTION_SPLIT)
5455 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005458 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460 if (depth == -1)
5461 {
5462 /* match in current file */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005463#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 if (g_do_tagpreview != 0)
5465 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005466 if (!GETFILE_SUCCESS(getfile(
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005467 curwin_save->w_buffer->b_fnum, NULL,
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005468 NULL, TRUE, lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 break; /* failed to jump to file */
5470 }
5471 else
5472#endif
5473 setpcmark();
5474 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc31f9ae2017-07-23 22:02:02 +02005475 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477 else
5478 {
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02005479 if (!GETFILE_SUCCESS(getfile(
5480 0, files[depth].name, NULL, TRUE,
5481 files[depth].lnum, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 break; /* failed to jump to file */
5483 /* autocommands may have changed the lnum, we don't
5484 * want that here */
5485 curwin->w_cursor.lnum = files[depth].lnum;
5486 }
5487 }
5488 if (action != ACTION_SHOW)
5489 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005490 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 curwin->w_set_curswant = TRUE;
5492 }
5493
Bram Moolenaar4033c552017-09-16 20:54:51 +02005494#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005496 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
5498 /* Return cursor to where we were */
5499 validate_cursor();
5500 redraw_later(VALID);
5501 win_enter(curwin_save, TRUE);
5502 }
5503#endif
5504 break;
5505 }
5506#ifdef FEAT_INS_EXPAND
5507exit_matched:
5508#endif
5509 matched = FALSE;
5510 /* look for other matches in the rest of the line if we
5511 * are not at the end of it already */
5512 if (def_regmatch.regprog == NULL
5513#ifdef FEAT_INS_EXPAND
5514 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005517 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005518 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 goto search_line;
5520 }
5521 line_breakcheck();
5522#ifdef FEAT_INS_EXPAND
5523 if (action == ACTION_EXPAND)
Bram Moolenaar472e8592016-10-15 17:06:47 +02005524 ins_compl_check_keys(30, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005525 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526#else
5527 if (got_int)
5528#endif
5529 break;
5530
5531 /*
5532 * Read the next line. When reading an included file and encountering
5533 * end-of-file, close the file and continue in the file that included
5534 * it.
5535 */
5536 while (depth >= 0 && !already
5537 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5538 {
5539 fclose(files[depth].fp);
5540 --old_files;
5541 files[old_files].name = files[depth].name;
5542 files[old_files].matched = files[depth].matched;
5543 --depth;
5544 curr_fname = (depth == -1) ? curbuf->b_fname
5545 : files[depth].name;
5546 if (depth < depth_displayed)
5547 depth_displayed = depth;
5548 }
5549 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005552 /* Remove any CR and LF from the line. */
5553 i = (int)STRLEN(line);
5554 if (i > 0 && line[i - 1] == '\n')
5555 line[--i] = NUL;
5556 if (i > 0 && line[i - 1] == '\r')
5557 line[--i] = NUL;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 else if (!already)
5560 {
5561 if (++lnum > end_lnum)
5562 break;
5563 line = ml_get(lnum);
5564 }
5565 already = NULL;
5566 }
5567 /* End of big for (;;) loop. */
5568
5569 /* Close any files that are still open. */
5570 for (i = 0; i <= depth; i++)
5571 {
5572 fclose(files[i].fp);
5573 vim_free(files[i].name);
5574 }
5575 for (i = old_files; i < max_path_depth; i++)
5576 vim_free(files[i].name);
5577 vim_free(files);
5578
5579 if (type == CHECK_PATH)
5580 {
5581 if (!did_show)
5582 {
5583 if (action != ACTION_SHOW_ALL)
5584 MSG(_("All included files were found"));
5585 else
5586 MSG(_("No included files"));
5587 }
5588 }
5589 else if (!found
5590#ifdef FEAT_INS_EXPAND
5591 && action != ACTION_EXPAND
5592#endif
5593 )
5594 {
5595#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005596 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#else
5598 if (got_int)
5599#endif
5600 EMSG(_(e_interr));
5601 else if (type == FIND_DEFINE)
5602 EMSG(_("E388: Couldn't find definition"));
5603 else
5604 EMSG(_("E389: Couldn't find pattern"));
5605 }
5606 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5607 msg_end();
5608
5609fpip_end:
5610 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005611 vim_regfree(regmatch.regprog);
5612 vim_regfree(incl_regmatch.regprog);
5613 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614}
5615
5616 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005617show_pat_in_path(
5618 char_u *line,
5619 int type,
5620 int did_show,
5621 int action,
5622 FILE *fp,
5623 linenr_T *lnum,
5624 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 char_u *p;
5627
5628 if (did_show)
5629 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005630 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 gotocmdline(TRUE); /* cursor at status line */
5632 if (got_int) /* 'q' typed at "--more--" message */
5633 return;
5634 for (;;)
5635 {
5636 p = line + STRLEN(line) - 1;
5637 if (fp != NULL)
5638 {
5639 /* We used fgets(), so get rid of newline at end */
5640 if (p >= line && *p == '\n')
5641 --p;
5642 if (p >= line && *p == '\r')
5643 --p;
5644 *(p + 1) = NUL;
5645 }
5646 if (action == ACTION_SHOW_ALL)
5647 {
5648 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5649 msg_puts(IObuff);
5650 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5651 /* Highlight line numbers */
Bram Moolenaar8820b482017-03-16 17:23:31 +01005652 msg_puts_attr(IObuff, HL_ATTR(HLF_N));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 MSG_PUTS(" ");
5654 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005655 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 out_flush(); /* show one line at a time */
5657
5658 /* Definition continues until line that doesn't end with '\' */
5659 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5660 break;
5661
5662 if (fp != NULL)
5663 {
5664 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5665 break;
5666 ++*lnum;
5667 }
5668 else
5669 {
5670 if (++*lnum > curbuf->b_ml.ml_line_count)
5671 break;
5672 line = ml_get(*lnum);
5673 }
5674 msg_putchar('\n');
5675 }
5676}
5677#endif
5678
5679#ifdef FEAT_VIMINFO
5680 int
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005681read_viminfo_search_pattern(vir_T *virp, int force)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 char_u *lp;
5684 int idx = -1;
5685 int magic = FALSE;
5686 int no_scs = FALSE;
5687 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005688 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 long off = 0;
5690 int setlast = FALSE;
5691#ifdef FEAT_SEARCH_EXTRA
5692 static int hlsearch_on = FALSE;
5693#endif
5694 char_u *val;
5695
5696 /*
5697 * Old line types:
5698 * "/pat", "&pat": search/subst. pat
5699 * "~/pat", "~&pat": last used search/subst. pat
5700 * New line types:
5701 * "~h", "~H": hlsearch highlighting off/on
5702 * "~<magic><smartcase><line><end><off><last><which>pat"
5703 * <magic>: 'm' off, 'M' on
5704 * <smartcase>: 's' off, 'S' on
5705 * <line>: 'L' line offset, 'l' char offset
5706 * <end>: 'E' from end, 'e' from start
5707 * <off>: decimal, offset
5708 * <last>: '~' last used pattern
5709 * <which>: '/' search pat, '&' subst. pat
5710 */
5711 lp = virp->vir_line;
5712 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5713 {
5714 if (lp[1] == 'M') /* magic on */
5715 magic = TRUE;
5716 if (lp[2] == 's')
5717 no_scs = TRUE;
5718 if (lp[3] == 'L')
5719 off_line = TRUE;
5720 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005721 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 lp += 5;
5723 off = getdigits(&lp);
5724 }
5725 if (lp[0] == '~') /* use this pattern for last-used pattern */
5726 {
5727 setlast = TRUE;
5728 lp++;
5729 }
5730 if (lp[0] == '/')
5731 idx = RE_SEARCH;
5732 else if (lp[0] == '&')
5733 idx = RE_SUBST;
5734#ifdef FEAT_SEARCH_EXTRA
5735 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5736 hlsearch_on = FALSE;
5737 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5738 hlsearch_on = TRUE;
5739#endif
5740 if (idx >= 0)
5741 {
5742 if (force || spats[idx].pat == NULL)
5743 {
5744 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5745 TRUE);
5746 if (val != NULL)
5747 {
5748 set_last_search_pat(val, idx, magic, setlast);
5749 vim_free(val);
5750 spats[idx].no_scs = no_scs;
5751 spats[idx].off.line = off_line;
5752 spats[idx].off.end = off_end;
5753 spats[idx].off.off = off;
5754#ifdef FEAT_SEARCH_EXTRA
5755 if (setlast)
Bram Moolenaar8050efa2013-11-08 04:30:20 +01005756 {
5757 SET_NO_HLSEARCH(!hlsearch_on);
5758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759#endif
5760 }
5761 }
5762 }
5763 return viminfo_readline(virp);
5764}
5765
5766 void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005767write_viminfo_search_pattern(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768{
5769 if (get_viminfo_parameter('/') != 0)
5770 {
5771#ifdef FEAT_SEARCH_EXTRA
5772 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5773 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5774#endif
5775 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005776 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 }
5778}
5779
5780 static void
Bram Moolenaar764b23c2016-01-30 21:10:09 +01005781wvsp_one(
5782 FILE *fp, /* file to write to */
5783 int idx, /* spats[] index */
5784 char *s, /* search pat */
5785 int sc) /* dir char */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786{
5787 if (spats[idx].pat != NULL)
5788 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005789 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 /* off.dir is not stored, it's reset to forward */
5791 fprintf(fp, "%c%c%c%c%ld%s%c",
5792 spats[idx].magic ? 'M' : 'm', /* magic */
5793 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5794 spats[idx].off.line ? 'L' : 'l', /* line offset */
5795 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5796 spats[idx].off.off, /* offset */
5797 last_idx == idx ? "~" : "", /* last used pat */
5798 sc);
5799 viminfo_writestring(fp, spats[idx].pat);
5800 }
5801}
5802#endif /* FEAT_VIMINFO */