blob: 4905a49d7f79c1c9ec481ad83fc2266cc7032de5 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar8c8de832008-06-24 22:58:06 +000016static void set_vv_searchforward __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017static int first_submatch __ARGS((regmmatch_T *rp));
18#endif
19static int check_prevcol __ARGS((char_u *linep, int col, int ch, int *prevcol));
20static int inmacro __ARGS((char_u *, char_u *));
21static int check_linecomment __ARGS((char_u *line));
22static int cls __ARGS((void));
23static int skip_chars __ARGS((int, int));
24#ifdef FEAT_TEXTOBJ
25static void back_in_line __ARGS((void));
26static void find_first_blank __ARGS((pos_T *));
27static void findsent_forward __ARGS((long count, int at_start_sent));
28#endif
29#ifdef FEAT_FIND_ID
30static void show_pat_in_path __ARGS((char_u *, int,
31 int, int, FILE *, linenr_T *, long));
32#endif
33#ifdef FEAT_VIMINFO
34static void wvsp_one __ARGS((FILE *fp, int idx, char *s, int sc));
35#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
92#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
93/* copy of spats[], for keeping the search patterns while executing autocmds */
94static struct spat saved_spats[2];
95static int saved_last_idx = 0;
96# ifdef FEAT_SEARCH_EXTRA
97static int saved_no_hlsearch = 0;
98# endif
99#endif
100
101static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
102#ifdef FEAT_RIGHTLEFT
103static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
106#ifdef FEAT_FIND_ID
107/*
108 * Type used by find_pattern_in_path() to remember which included files have
109 * been searched already.
110 */
111typedef struct SearchedFile
112{
113 FILE *fp; /* File pointer */
114 char_u *name; /* Full name of file */
115 linenr_T lnum; /* Line we were up to in file */
116 int matched; /* Found a match in this file */
117} SearchedFile;
118#endif
119
120/*
121 * translate search pattern for vim_regcomp()
122 *
123 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
124 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
125 * pat_save == RE_BOTH: save pat in both patterns (:global command)
126 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000127 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
129 * options & SEARCH_HIS: put search string in history
130 * options & SEARCH_KEEP: keep previous search pattern
131 *
132 * returns FAIL if failed, OK otherwise.
133 */
134 int
135search_regcomp(pat, pat_save, pat_use, options, regmatch)
136 char_u *pat;
137 int pat_save;
138 int pat_use;
139 int options;
140 regmmatch_T *regmatch; /* return: pattern and ignore-case flag */
141{
142 int magic;
143 int i;
144
145 rc_did_emsg = FALSE;
146 magic = p_magic;
147
148 /*
149 * If no pattern given, use a previously defined pattern.
150 */
151 if (pat == NULL || *pat == NUL)
152 {
153 if (pat_use == RE_LAST)
154 i = last_idx;
155 else
156 i = pat_use;
157 if (spats[i].pat == NULL) /* pattern was never defined */
158 {
159 if (pat_use == RE_SUBST)
160 EMSG(_(e_nopresub));
161 else
162 EMSG(_(e_noprevre));
163 rc_did_emsg = TRUE;
164 return FAIL;
165 }
166 pat = spats[i].pat;
167 magic = spats[i].magic;
168 no_smartcase = spats[i].no_scs;
169 }
170#ifdef FEAT_CMDHIST
171 else if (options & SEARCH_HIS) /* put new pattern in history */
172 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
173#endif
174
175#ifdef FEAT_RIGHTLEFT
176 if (mr_pattern_alloced)
177 {
178 vim_free(mr_pattern);
179 mr_pattern_alloced = FALSE;
180 }
181
182 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
183 {
184 char_u *rev_pattern;
185
186 rev_pattern = reverse_text(pat);
187 if (rev_pattern == NULL)
188 mr_pattern = pat; /* out of memory, keep normal pattern. */
189 else
190 {
191 mr_pattern = rev_pattern;
192 mr_pattern_alloced = TRUE;
193 }
194 }
195 else
196#endif
197 mr_pattern = pat;
198
199 /*
200 * Save the currently used pattern in the appropriate place,
201 * unless the pattern should not be remembered.
202 */
Bram Moolenaar14177b72014-01-14 15:53:51 +0100203 if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 {
205 /* search or global command */
206 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
207 save_re_pat(RE_SEARCH, pat, magic);
208 /* substitute or global command */
209 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
210 save_re_pat(RE_SUBST, pat, magic);
211 }
212
213 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000214 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
216 if (regmatch->regprog == NULL)
217 return FAIL;
218 return OK;
219}
220
221/*
222 * Get search pattern used by search_regcomp().
223 */
224 char_u *
225get_search_pat()
226{
227 return mr_pattern;
228}
229
Bram Moolenaarabc97732007-08-08 20:49:37 +0000230#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/*
232 * Reverse text into allocated memory.
233 * Returns the allocated string, NULL when out of memory.
234 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000235 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236reverse_text(s)
237 char_u *s;
238{
239 unsigned len;
240 unsigned s_i, rev_i;
241 char_u *rev;
242
243 /*
244 * Reverse the pattern.
245 */
246 len = (unsigned)STRLEN(s);
247 rev = alloc(len + 1);
248 if (rev != NULL)
249 {
250 rev_i = len;
251 for (s_i = 0; s_i < len; ++s_i)
252 {
253# ifdef FEAT_MBYTE
254 if (has_mbyte)
255 {
256 int mb_len;
257
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000258 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 rev_i -= mb_len;
260 mch_memmove(rev + rev_i, s + s_i, mb_len);
261 s_i += mb_len - 1;
262 }
263 else
264# endif
265 rev[--rev_i] = s[s_i];
266
267 }
268 rev[len] = NUL;
269 }
270 return rev;
271}
272#endif
273
Bram Moolenaarcc2b9d52014-12-13 03:17:11 +0100274 void
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275save_re_pat(idx, pat, magic)
276 int idx;
277 char_u *pat;
278 int magic;
279{
280 if (spats[idx].pat != pat)
281 {
282 vim_free(spats[idx].pat);
283 spats[idx].pat = vim_strsave(pat);
284 spats[idx].magic = magic;
285 spats[idx].no_scs = no_smartcase;
286 last_idx = idx;
287#ifdef FEAT_SEARCH_EXTRA
288 /* If 'hlsearch' set and search pat changed: need redraw. */
289 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000290 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100291 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
293 }
294}
295
296#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
297/*
298 * Save the search patterns, so they can be restored later.
299 * Used before/after executing autocommands and user functions.
300 */
301static int save_level = 0;
302
303 void
304save_search_patterns()
305{
306 if (save_level++ == 0)
307 {
308 saved_spats[0] = spats[0];
309 if (spats[0].pat != NULL)
310 saved_spats[0].pat = vim_strsave(spats[0].pat);
311 saved_spats[1] = spats[1];
312 if (spats[1].pat != NULL)
313 saved_spats[1].pat = vim_strsave(spats[1].pat);
314 saved_last_idx = last_idx;
315# ifdef FEAT_SEARCH_EXTRA
316 saved_no_hlsearch = no_hlsearch;
317# endif
318 }
319}
320
321 void
322restore_search_patterns()
323{
324 if (--save_level == 0)
325 {
326 vim_free(spats[0].pat);
327 spats[0] = saved_spats[0];
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000328#if defined(FEAT_EVAL)
329 set_vv_searchforward();
330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 vim_free(spats[1].pat);
332 spats[1] = saved_spats[1];
333 last_idx = saved_last_idx;
334# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100335 SET_NO_HLSEARCH(saved_no_hlsearch);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336# endif
337 }
338}
339#endif
340
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000341#if defined(EXITFREE) || defined(PROTO)
342 void
343free_search_patterns()
344{
345 vim_free(spats[0].pat);
346 vim_free(spats[1].pat);
Bram Moolenaarf2427622009-04-22 16:45:21 +0000347
348# ifdef FEAT_RIGHTLEFT
349 if (mr_pattern_alloced)
350 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200351 vim_free(mr_pattern);
352 mr_pattern_alloced = FALSE;
353 mr_pattern = NULL;
Bram Moolenaarf2427622009-04-22 16:45:21 +0000354 }
355# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000356}
357#endif
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359/*
360 * Return TRUE when case should be ignored for search pattern "pat".
361 * Uses the 'ignorecase' and 'smartcase' options.
362 */
363 int
364ignorecase(pat)
365 char_u *pat;
366{
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200367 int ic = p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 if (ic && !no_smartcase && p_scs
370#ifdef FEAT_INS_EXPAND
371 && !(ctrl_x_mode && curbuf->b_p_inf)
372#endif
373 )
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200374 ic = !pat_has_uppercase(pat);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 no_smartcase = FALSE;
376
377 return ic;
378}
379
Bram Moolenaara9dc3752010-07-11 20:46:53 +0200380/*
381 * Return TRUE if patter "pat" has an uppercase character.
382 */
383 int
384pat_has_uppercase(pat)
385 char_u *pat;
386{
387 char_u *p = pat;
388
389 while (*p != NUL)
390 {
391#ifdef FEAT_MBYTE
392 int l;
393
394 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
395 {
396 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
397 return TRUE;
398 p += l;
399 }
400 else
401#endif
402 if (*p == '\\')
403 {
404 if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */
405 p += 3;
406 else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */
407 p += 3;
408 else if (p[1] != NUL) /* skip "\X" */
409 p += 2;
410 else
411 p += 1;
412 }
413 else if (MB_ISUPPER(*p))
414 return TRUE;
415 else
416 ++p;
417 }
418 return FALSE;
419}
420
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 char_u *
422last_search_pat()
423{
424 return spats[last_idx].pat;
425}
426
427/*
428 * Reset search direction to forward. For "gd" and "gD" commands.
429 */
430 void
431reset_search_dir()
432{
433 spats[0].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000434#if defined(FEAT_EVAL)
435 set_vv_searchforward();
436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437}
438
439#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
440/*
441 * Set the last search pattern. For ":let @/ =" and viminfo.
442 * Also set the saved search pattern, so that this works in an autocommand.
443 */
444 void
445set_last_search_pat(s, idx, magic, setlast)
446 char_u *s;
447 int idx;
448 int magic;
449 int setlast;
450{
451 vim_free(spats[idx].pat);
452 /* An empty string means that nothing should be matched. */
453 if (*s == NUL)
454 spats[idx].pat = NULL;
455 else
456 spats[idx].pat = vim_strsave(s);
457 spats[idx].magic = magic;
458 spats[idx].no_scs = FALSE;
459 spats[idx].off.dir = '/';
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#if defined(FEAT_EVAL)
461 set_vv_searchforward();
462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 spats[idx].off.line = FALSE;
464 spats[idx].off.end = FALSE;
465 spats[idx].off.off = 0;
466 if (setlast)
467 last_idx = idx;
468 if (save_level)
469 {
470 vim_free(saved_spats[idx].pat);
471 saved_spats[idx] = spats[0];
472 if (spats[idx].pat == NULL)
473 saved_spats[idx].pat = NULL;
474 else
475 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
476 saved_last_idx = last_idx;
477 }
478# ifdef FEAT_SEARCH_EXTRA
479 /* If 'hlsearch' set and search pat changed: need redraw. */
480 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000481 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482# endif
483}
484#endif
485
486#ifdef FEAT_SEARCH_EXTRA
487/*
488 * Get a regexp program for the last used search pattern.
489 * This is used for highlighting all matches in a window.
490 * Values returned in regmatch->regprog and regmatch->rmm_ic.
491 */
492 void
493last_pat_prog(regmatch)
494 regmmatch_T *regmatch;
495{
496 if (spats[last_idx].pat == NULL)
497 {
498 regmatch->regprog = NULL;
499 return;
500 }
501 ++emsg_off; /* So it doesn't beep if bad expr */
502 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
503 --emsg_off;
504}
505#endif
506
507/*
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +0100508 * Lowest level search function.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
510 * Start at position 'pos' and return the found position in 'pos'.
511 *
512 * if (options & SEARCH_MSG) == 0 don't give any messages
513 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
514 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
515 * if (options & SEARCH_HIS) put search pattern in history
516 * if (options & SEARCH_END) return position at end of match
517 * if (options & SEARCH_START) accept match at pos itself
518 * if (options & SEARCH_KEEP) keep previous search pattern
519 * if (options & SEARCH_FOLD) match only once in a closed fold
520 * if (options & SEARCH_PEEK) check for typed char, cancel search
521 *
522 * Return FAIL (zero) for failure, non-zero for success.
523 * When FEAT_EVAL is defined, returns the index of the first matching
524 * subpattern plus one; one if there was none.
525 */
526 int
Bram Moolenaar76929292008-01-06 19:07:36 +0000527searchit(win, buf, pos, dir, pat, count, options, pat_use, stop_lnum, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 win_T *win; /* window to search in; can be NULL for a
529 buffer without a window! */
530 buf_T *buf;
531 pos_T *pos;
532 int dir;
533 char_u *pat;
534 long count;
535 int options;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000536 int pat_use; /* which pattern to use when "pat" is empty */
537 linenr_T stop_lnum; /* stop after this line number when != 0 */
Bram Moolenaar78a15312009-05-15 19:33:18 +0000538 proftime_T *tm UNUSED; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539{
540 int found;
541 linenr_T lnum; /* no init to shut up Apollo cc */
542 regmmatch_T regmatch;
543 char_u *ptr;
544 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000546 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 int loop;
548 pos_T start_pos;
549 int at_first_line;
550 int extra_col;
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200551 int start_char_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 int match_ok;
553 long nmatched;
554 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100555 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000556 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#ifdef FEAT_SEARCH_EXTRA
558 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560
561 if (search_regcomp(pat, RE_SEARCH, pat_use,
562 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
563 {
564 if ((options & SEARCH_MSG) && !rc_did_emsg)
565 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
566 return FAIL;
567 }
568
Bram Moolenaar280f1262006-01-30 00:14:18 +0000569 /*
570 * find the string
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 called_emsg = FALSE;
573 do /* loop for count */
574 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100575 /* When not accepting a match at the start position set "extra_col" to
576 * a non-zero value. Don't do that when starting at MAXCOL, since
577 * MAXCOL + 1 is zero. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200578 if (pos->col == MAXCOL)
579 start_char_len = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100580#ifdef FEAT_MBYTE
581 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200582 else if (has_mbyte
583 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
584 && pos->col < MAXCOL - 2)
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100585 {
586 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
587 if (*ptr == NUL)
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200588 start_char_len = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100589 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200590 start_char_len = (*mb_ptr2len)(ptr);
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100591 }
592#endif
593 else
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200594 start_char_len = 1;
595 if (dir == FORWARD)
596 {
597 if (options & SEARCH_START)
598 extra_col = 0;
599 else
600 extra_col = start_char_len;
601 }
602 else
603 {
604 if (options & SEARCH_START)
605 extra_col = start_char_len;
606 else
607 extra_col = 0;
608 }
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 start_pos = *pos; /* remember start pos for detecting no match */
611 found = 0; /* default: not found */
612 at_first_line = TRUE; /* default: start in first line */
613 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
614 {
615 pos->lnum = 1;
616 pos->col = 0;
617 at_first_line = FALSE; /* not in first line now */
618 }
619
620 /*
621 * Start searching in current line, unless searching backwards and
622 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000623 * If we are searching backwards, in column 0, and not including the
624 * current position, gain some efficiency by skipping back a line.
625 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000627 if (dir == BACKWARD && start_pos.col == 0
628 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 {
630 lnum = pos->lnum - 1;
631 at_first_line = FALSE;
632 }
633 else
634 lnum = pos->lnum;
635
636 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
637 {
638 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
639 lnum += dir, at_first_line = FALSE)
640 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000641 /* Stop after checking "stop_lnum", if it's set. */
642 if (stop_lnum != 0 && (dir == FORWARD
643 ? lnum > stop_lnum : lnum < stop_lnum))
644 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000645#ifdef FEAT_RELTIME
646 /* Stop after passing the "tm" time limit. */
647 if (tm != NULL && profile_passed_limit(tm))
648 break;
649#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000652 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000655 lnum, (colnr_T)0,
656#ifdef FEAT_RELTIME
657 tm
658#else
659 NULL
660#endif
661 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 /* Abort searching on an error (e.g., out of stack). */
663 if (called_emsg)
664 break;
665 if (nmatched > 0)
666 {
667 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000668 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000670#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000672#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000673 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000674 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
675 ptr = (char_u *)"";
676 else
677 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679 /*
680 * Forward search in the first line: match should be after
681 * the start position. If not, continue at the end of the
682 * match (this is vi compatible) or on the next char.
683 */
684 if (dir == FORWARD && at_first_line)
685 {
686 match_ok = TRUE;
687 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000688 * When the match starts in a next line it's certainly
689 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 * When match lands on a NUL the cursor will be put
691 * one back afterwards, compare with that position,
692 * otherwise "/$" will get stuck on end of line.
693 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000694 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100695 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000696 ? (nmatched == 1
697 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000699 : ((int)matchpos.col
700 - (ptr[matchpos.col] == NUL)
701 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 /*
704 * If vi-compatible searching, continue at the end
705 * of the match, otherwise continue one position
706 * forward.
707 */
708 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
709 {
710 if (nmatched > 1)
711 {
712 /* end is in next line, thus no match in
713 * this line */
714 match_ok = FALSE;
715 break;
716 }
717 matchcol = endpos.col;
718 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000719 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 && ptr[matchcol] != NUL)
721 {
722#ifdef FEAT_MBYTE
723 if (has_mbyte)
724 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000725 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 else
727#endif
728 ++matchcol;
729 }
730 }
731 else
732 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000733 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 if (ptr[matchcol] != NUL)
735 {
736#ifdef FEAT_MBYTE
737 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000738 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 + matchcol);
740 else
741#endif
742 ++matchcol;
743 }
744 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200745 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 if (ptr[matchcol] == NUL
748 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000749 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000750 matchcol,
751#ifdef FEAT_RELTIME
752 tm
753#else
754 NULL
755#endif
756 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
758 match_ok = FALSE;
759 break;
760 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000761 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 endpos = regmatch.endpos[0];
763# ifdef FEAT_EVAL
764 submatch = first_submatch(&regmatch);
765# endif
766
767 /* Need to get the line pointer again, a
768 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000769 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771 if (!match_ok)
772 continue;
773 }
774 if (dir == BACKWARD)
775 {
776 /*
777 * Now, if there are multiple matches on this line,
778 * we have to get the last one. Or the last one before
779 * the cursor, if we're on that line.
780 * When putting the new cursor at the end, compare
781 * relative to the end of the match.
782 */
783 match_ok = FALSE;
784 for (;;)
785 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000786 /* Remember a position that is before the start
787 * position, we use it if it's the last match in
788 * the line. Always accept a position after
789 * wrapping around. */
790 if (loop
791 || ((options & SEARCH_END)
792 ? (lnum + regmatch.endpos[0].lnum
793 < start_pos.lnum
794 || (lnum + regmatch.endpos[0].lnum
795 == start_pos.lnum
796 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200797 < (int)start_pos.col
798 + extra_col))
Bram Moolenaar677ee682005-01-27 14:41:15 +0000799 : (lnum + regmatch.startpos[0].lnum
800 < start_pos.lnum
801 || (lnum + regmatch.startpos[0].lnum
802 == start_pos.lnum
803 && (int)regmatch.startpos[0].col
Bram Moolenaar5f1e68b2015-07-10 14:43:35 +0200804 < (int)start_pos.col
805 + extra_col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000808 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 endpos = regmatch.endpos[0];
810# ifdef FEAT_EVAL
811 submatch = first_submatch(&regmatch);
812# endif
813 }
814 else
815 break;
816
817 /*
818 * We found a valid match, now check if there is
819 * another one after it.
820 * If vi-compatible searching, continue at the end
821 * of the match, otherwise continue one position
822 * forward.
823 */
824 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
825 {
826 if (nmatched > 1)
827 break;
828 matchcol = endpos.col;
829 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000830 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 && ptr[matchcol] != NUL)
832 {
833#ifdef FEAT_MBYTE
834 if (has_mbyte)
835 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000836 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 else
838#endif
839 ++matchcol;
840 }
841 }
842 else
843 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000844 /* Stop when the match is in a next line. */
845 if (matchpos.lnum > 0)
846 break;
847 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 if (ptr[matchcol] != NUL)
849 {
850#ifdef FEAT_MBYTE
851 if (has_mbyte)
852 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000853 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 else
855#endif
856 ++matchcol;
857 }
858 }
859 if (ptr[matchcol] == NUL
860 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000861 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000862 matchcol,
863#ifdef FEAT_RELTIME
864 tm
865#else
866 NULL
867#endif
868 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 break;
870
871 /* Need to get the line pointer again, a
872 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000873 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875
876 /*
877 * If there is only a match after the cursor, skip
878 * this match.
879 */
880 if (!match_ok)
881 continue;
882 }
883
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000884 /* With the SEARCH_END option move to the last character
885 * of the match. Don't do it for an empty match, end
886 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200887 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000888 && !(matchpos.lnum == endpos.lnum
889 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000891 /* For a match in the first column, set the position
892 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000893 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000894 pos->col = endpos.col;
895 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000896 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000897 if (pos->lnum > 1) /* just in case */
898 {
899 --pos->lnum;
900 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
901 pos->lnum, FALSE));
902 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000903 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000904 else
905 {
906 --pos->col;
907#ifdef FEAT_MBYTE
908 if (has_mbyte
909 && pos->lnum <= buf->b_ml.ml_line_count)
910 {
911 ptr = ml_get_buf(buf, pos->lnum, FALSE);
912 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
913 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000914#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 }
917 else
918 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000919 pos->lnum = lnum + matchpos.lnum;
920 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922#ifdef FEAT_VIRTUALEDIT
923 pos->coladd = 0;
924#endif
925 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100926 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
928 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000929 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 search_match_endcol = endpos.col;
931 break;
932 }
933 line_breakcheck(); /* stop if ctrl-C typed */
934 if (got_int)
935 break;
936
937#ifdef FEAT_SEARCH_EXTRA
938 /* Cancel searching if a character was typed. Used for
939 * 'incsearch'. Don't check too often, that would slowdown
940 * searching too much. */
941 if ((options & SEARCH_PEEK)
942 && ((lnum - pos->lnum) & 0x3f) == 0
943 && char_avail())
944 {
945 break_loop = TRUE;
946 break;
947 }
948#endif
949
950 if (loop && lnum == start_pos.lnum)
951 break; /* if second loop, stop where started */
952 }
953 at_first_line = FALSE;
954
955 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000956 * Stop the search if wrapscan isn't set, "stop_lnum" is
957 * specified, after an interrupt, after a match and after looping
958 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000960 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaar78a15312009-05-15 19:33:18 +0000961#ifdef FEAT_SEARCH_EXTRA
962 || break_loop
963#endif
964 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 break;
966
967 /*
968 * If 'wrapscan' is set we continue at the other end of the file.
969 * If 'shortmess' does not contain 's', we give a message.
970 * This message is also remembered in keep_msg for when the screen
971 * is redrawn. The keep_msg is cleared whenever another message is
972 * written.
973 */
974 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +0000978 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
979 give_warning((char_u *)_(dir == BACKWARD
980 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
Bram Moolenaar78a15312009-05-15 19:33:18 +0000982 if (got_int || called_emsg
983#ifdef FEAT_SEARCH_EXTRA
984 || break_loop
985#endif
986 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 break;
988 }
989 while (--count > 0 && found); /* stop after count matches or no match */
990
Bram Moolenaar473de612013-06-08 18:19:48 +0200991 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar280f1262006-01-30 00:14:18 +0000993 called_emsg |= save_called_emsg;
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 if (!found) /* did not find it */
996 {
997 if (got_int)
998 EMSG(_(e_interr));
999 else if ((options & SEARCH_MSG) == SEARCH_MSG)
1000 {
1001 if (p_ws)
1002 EMSG2(_(e_patnotf2), mr_pattern);
1003 else if (lnum == 0)
1004 EMSG2(_("E384: search hit TOP without match for: %s"),
1005 mr_pattern);
1006 else
1007 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
1008 mr_pattern);
1009 }
1010 return FAIL;
1011 }
1012
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001013 /* A pattern like "\n\zs" may go past the last line. */
1014 if (pos->lnum > buf->b_ml.ml_line_count)
1015 {
1016 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001017 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001018 if (pos->col > 0)
1019 --pos->col;
1020 }
1021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 return submatch + 1;
1023}
1024
1025#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001026 void
1027set_search_direction(cdir)
1028 int cdir;
1029{
1030 spats[0].off.dir = cdir;
1031}
1032
1033 static void
1034set_vv_searchforward()
1035{
1036 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1037}
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039/*
1040 * Return the number of the first subpat that matched.
1041 */
1042 static int
1043first_submatch(rp)
1044 regmmatch_T *rp;
1045{
1046 int submatch;
1047
1048 for (submatch = 1; ; ++submatch)
1049 {
1050 if (rp->startpos[submatch].lnum >= 0)
1051 break;
1052 if (submatch == 9)
1053 {
1054 submatch = 0;
1055 break;
1056 }
1057 }
1058 return submatch;
1059}
1060#endif
1061
1062/*
1063 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001064 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 * If 'dirc' is 0: use previous dir.
1066 * If 'pat' is NULL or empty : use previous string.
1067 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1068 * If 'options & SEARCH_ECHO': echo the search command and handle options
1069 * If 'options & SEARCH_MSG' : may give error message
1070 * If 'options & SEARCH_OPT' : interpret optional flags
1071 * If 'options & SEARCH_HIS' : put search pattern in history
1072 * If 'options & SEARCH_NOOF': don't add offset to position
1073 * If 'options & SEARCH_MARK': set previous context mark
1074 * If 'options & SEARCH_KEEP': keep previous search pattern
1075 * If 'options & SEARCH_START': accept match at curpos itself
1076 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1077 *
1078 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1079 * makes the movement linewise without moving the match position.
1080 *
Bram Moolenaarb6c27352015-03-05 19:57:49 +01001081 * Return 0 for failure, 1 for found, 2 for found and line offset added.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 */
1083 int
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001084do_search(oap, dirc, pat, count, options, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 oparg_T *oap; /* can be NULL */
1086 int dirc; /* '/' or '?' */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001087 char_u *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 long count;
1089 int options;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001090 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091{
1092 pos_T pos; /* position of the last match */
1093 char_u *searchstr;
1094 struct soffset old_off;
1095 int retval; /* Return value */
1096 char_u *p;
1097 long c;
1098 char_u *dircp;
1099 char_u *strcopy = NULL;
1100 char_u *ps;
1101
1102 /*
1103 * A line offset is not remembered, this is vi compatible.
1104 */
1105 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1106 {
1107 spats[0].off.line = FALSE;
1108 spats[0].off.off = 0;
1109 }
1110
1111 /*
1112 * Save the values for when (options & SEARCH_KEEP) is used.
1113 * (there is no "if ()" around this because gcc wants them initialized)
1114 */
1115 old_off = spats[0].off;
1116
1117 pos = curwin->w_cursor; /* start searching at the cursor position */
1118
1119 /*
1120 * Find out the direction of the search.
1121 */
1122 if (dirc == 0)
1123 dirc = spats[0].off.dir;
1124 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001127#if defined(FEAT_EVAL)
1128 set_vv_searchforward();
1129#endif
1130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (options & SEARCH_REV)
1132 {
1133#ifdef WIN32
1134 /* There is a bug in the Visual C++ 2.2 compiler which means that
1135 * dirc always ends up being '/' */
1136 dirc = (dirc == '/') ? '?' : '/';
1137#else
1138 if (dirc == '/')
1139 dirc = '?';
1140 else
1141 dirc = '/';
1142#endif
1143 }
1144
1145#ifdef FEAT_FOLDING
1146 /* If the cursor is in a closed fold, don't find another match in the same
1147 * fold. */
1148 if (dirc == '/')
1149 {
1150 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1151 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1152 }
1153 else
1154 {
1155 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1156 pos.col = 0;
1157 }
1158#endif
1159
1160#ifdef FEAT_SEARCH_EXTRA
1161 /*
1162 * Turn 'hlsearch' highlighting back on.
1163 */
1164 if (no_hlsearch && !(options & SEARCH_KEEP))
1165 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001166 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01001167 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169#endif
1170
1171 /*
1172 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1173 */
1174 for (;;)
1175 {
1176 searchstr = pat;
1177 dircp = NULL;
1178 /* use previous pattern */
1179 if (pat == NULL || *pat == NUL || *pat == dirc)
1180 {
1181 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1182 {
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001183 pat = spats[RE_SUBST].pat;
1184 if (pat == NULL)
1185 {
1186 EMSG(_(e_noprevre));
1187 retval = 0;
1188 goto end_do_search;
1189 }
1190 searchstr = pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001192 else
1193 {
1194 /* make search_regcomp() use spats[RE_SEARCH].pat */
1195 searchstr = (char_u *)"";
1196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198
1199 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1200 {
1201 /*
1202 * Find end of regular expression.
1203 * If there is a matching '/' or '?', toss it.
1204 */
1205 ps = strcopy;
1206 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1207 if (strcopy != ps)
1208 {
1209 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001210 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 pat = strcopy;
1212 searchstr = strcopy;
1213 }
1214 if (*p == dirc)
1215 {
1216 dircp = p; /* remember where we put the NUL */
1217 *p++ = NUL;
1218 }
1219 spats[0].off.line = FALSE;
1220 spats[0].off.end = FALSE;
1221 spats[0].off.off = 0;
1222 /*
1223 * Check for a line offset or a character offset.
1224 * For get_address (echo off) we don't check for a character
1225 * offset, because it is meaningless and the 's' could be a
1226 * substitute command.
1227 */
1228 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1229 spats[0].off.line = TRUE;
1230 else if ((options & SEARCH_OPT) &&
1231 (*p == 'e' || *p == 's' || *p == 'b'))
1232 {
1233 if (*p == 'e') /* end */
1234 spats[0].off.end = SEARCH_END;
1235 ++p;
1236 }
1237 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1238 {
1239 /* 'nr' or '+nr' or '-nr' */
1240 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1241 spats[0].off.off = atol((char *)p);
1242 else if (*p == '-') /* single '-' */
1243 spats[0].off.off = -1;
1244 else /* single '+' */
1245 spats[0].off.off = 1;
1246 ++p;
1247 while (VIM_ISDIGIT(*p)) /* skip number */
1248 ++p;
1249 }
1250
1251 /* compute length of search command for get_address() */
1252 searchcmdlen += (int)(p - pat);
1253
1254 pat = p; /* put pat after search command */
1255 }
1256
1257 if ((options & SEARCH_ECHO) && messaging()
1258 && !cmd_silent && msg_silent == 0)
1259 {
1260 char_u *msgbuf;
1261 char_u *trunc;
1262
1263 if (*searchstr == NUL)
1264 p = spats[last_idx].pat;
1265 else
1266 p = searchstr;
1267 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1268 if (msgbuf != NULL)
1269 {
1270 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001271#ifdef FEAT_MBYTE
1272 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1273 {
1274 /* Use a space to draw the composing char on. */
1275 msgbuf[1] = ' ';
1276 STRCPY(msgbuf + 2, p);
1277 }
1278 else
1279#endif
1280 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1282 {
1283 p = msgbuf + STRLEN(msgbuf);
1284 *p++ = dirc;
1285 if (spats[0].off.end)
1286 *p++ = 'e';
1287 else if (!spats[0].off.line)
1288 *p++ = 's';
1289 if (spats[0].off.off > 0 || spats[0].off.line)
1290 *p++ = '+';
1291 if (spats[0].off.off != 0 || spats[0].off.line)
1292 sprintf((char *)p, "%ld", spats[0].off.off);
1293 else
1294 *p = NUL;
1295 }
1296
1297 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001298 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300#ifdef FEAT_RIGHTLEFT
1301 /* The search pattern could be shown on the right in rightleft
1302 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1303 * it would be blanked out again very soon. Show it on the
1304 * left, but do reverse the text. */
1305 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1306 {
1307 char_u *r;
1308
1309 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1310 if (r != NULL)
1311 {
1312 vim_free(trunc);
1313 trunc = r;
1314 }
1315 }
1316#endif
1317 if (trunc != NULL)
1318 {
1319 msg_outtrans(trunc);
1320 vim_free(trunc);
1321 }
1322 else
1323 msg_outtrans(msgbuf);
1324 msg_clr_eos();
1325 msg_check();
1326 vim_free(msgbuf);
1327
1328 gotocmdline(FALSE);
1329 out_flush();
1330 msg_nowait = TRUE; /* don't wait for this message */
1331 }
1332 }
1333
1334 /*
1335 * If there is a character offset, subtract it from the current
1336 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001337 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 * This is not done for a line offset, because then we would not be vi
1339 * compatible.
1340 */
Bram Moolenaared203462004-06-16 11:19:22 +00001341 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 {
1343 if (spats[0].off.off > 0)
1344 {
1345 for (c = spats[0].off.off; c; --c)
1346 if (decl(&pos) == -1)
1347 break;
1348 if (c) /* at start of buffer */
1349 {
1350 pos.lnum = 0; /* allow lnum == 0 here */
1351 pos.col = MAXCOL;
1352 }
1353 }
1354 else
1355 {
1356 for (c = spats[0].off.off; c; ++c)
1357 if (incl(&pos) == -1)
1358 break;
1359 if (c) /* at end of buffer */
1360 {
1361 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1362 pos.col = 0;
1363 }
1364 }
1365 }
1366
1367#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1368 if (p_altkeymap && curwin->w_p_rl)
1369 lrFswap(searchstr,0);
1370#endif
1371
1372 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1373 searchstr, count, spats[0].off.end + (options &
1374 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1375 + SEARCH_MSG + SEARCH_START
1376 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001377 RE_LAST, (linenr_T)0, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 if (dircp != NULL)
1380 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1381 if (c == FAIL)
1382 {
1383 retval = 0;
1384 goto end_do_search;
1385 }
1386 if (spats[0].off.end && oap != NULL)
1387 oap->inclusive = TRUE; /* 'e' includes last character */
1388
1389 retval = 1; /* pattern found */
1390
1391 /*
1392 * Add character and/or line offset
1393 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001394 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 if (spats[0].off.line) /* Add the offset to the line number. */
1397 {
1398 c = pos.lnum + spats[0].off.off;
1399 if (c < 1)
1400 pos.lnum = 1;
1401 else if (c > curbuf->b_ml.ml_line_count)
1402 pos.lnum = curbuf->b_ml.ml_line_count;
1403 else
1404 pos.lnum = c;
1405 pos.col = 0;
1406
1407 retval = 2; /* pattern found, line offset added */
1408 }
Bram Moolenaared203462004-06-16 11:19:22 +00001409 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 {
1411 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001412 c = spats[0].off.off;
1413 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001415 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (incl(&pos) == -1)
1417 break;
1418 }
1419 /* to the left, check for start of file */
1420 else
1421 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001422 while (c++ < 0)
1423 if (decl(&pos) == -1)
1424 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 }
1427 }
1428
1429 /*
1430 * The search command can be followed by a ';' to do another search.
1431 * For example: "/pat/;/foo/+3;?bar"
1432 * This is like doing another search command, except:
1433 * - The remembered direction '/' or '?' is from the first search.
1434 * - When an error happens the cursor isn't moved at all.
1435 * Don't do this when called by get_address() (it handles ';' itself).
1436 */
1437 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1438 break;
1439
1440 dirc = *++pat;
1441 if (dirc != '?' && dirc != '/')
1442 {
1443 retval = 0;
1444 EMSG(_("E386: Expected '?' or '/' after ';'"));
1445 goto end_do_search;
1446 }
1447 ++pat;
1448 }
1449
1450 if (options & SEARCH_MARK)
1451 setpcmark();
1452 curwin->w_cursor = pos;
1453 curwin->w_set_curswant = TRUE;
1454
1455end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001456 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 spats[0].off = old_off;
1458 vim_free(strcopy);
1459
1460 return retval;
1461}
1462
1463#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1464/*
1465 * search_for_exact_line(buf, pos, dir, pat)
1466 *
1467 * Search for a line starting with the given pattern (ignoring leading
1468 * white-space), starting from pos and going in direction dir. pos will
1469 * contain the position of the match found. Blank lines match only if
1470 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1471 * Return OK for success, or FAIL if no line found.
1472 */
1473 int
1474search_for_exact_line(buf, pos, dir, pat)
1475 buf_T *buf;
1476 pos_T *pos;
1477 int dir;
1478 char_u *pat;
1479{
1480 linenr_T start = 0;
1481 char_u *ptr;
1482 char_u *p;
1483
1484 if (buf->b_ml.ml_line_count == 0)
1485 return FAIL;
1486 for (;;)
1487 {
1488 pos->lnum += dir;
1489 if (pos->lnum < 1)
1490 {
1491 if (p_ws)
1492 {
1493 pos->lnum = buf->b_ml.ml_line_count;
1494 if (!shortmess(SHM_SEARCH))
1495 give_warning((char_u *)_(top_bot_msg), TRUE);
1496 }
1497 else
1498 {
1499 pos->lnum = 1;
1500 break;
1501 }
1502 }
1503 else if (pos->lnum > buf->b_ml.ml_line_count)
1504 {
1505 if (p_ws)
1506 {
1507 pos->lnum = 1;
1508 if (!shortmess(SHM_SEARCH))
1509 give_warning((char_u *)_(bot_top_msg), TRUE);
1510 }
1511 else
1512 {
1513 pos->lnum = 1;
1514 break;
1515 }
1516 }
1517 if (pos->lnum == start)
1518 break;
1519 if (start == 0)
1520 start = pos->lnum;
1521 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1522 p = skipwhite(ptr);
1523 pos->col = (colnr_T) (p - ptr);
1524
1525 /* when adding lines the matching line may be empty but it is not
1526 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001527 if ((compl_cont_status & CONT_ADDING)
1528 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1531 return OK;
1532 }
1533 else if (*p != NUL) /* ignore empty lines */
1534 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001535 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1536 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return OK;
1538 }
1539 }
1540 return FAIL;
1541}
1542#endif /* FEAT_INS_EXPAND */
1543
1544/*
1545 * Character Searches
1546 */
1547
1548/*
1549 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1550 * position of the character, otherwise move to just before the char.
1551 * Do this "cap->count1" times.
1552 * Return FAIL or OK.
1553 */
1554 int
1555searchc(cap, t_cmd)
1556 cmdarg_T *cap;
1557 int t_cmd;
1558{
1559 int c = cap->nchar; /* char to search for */
1560 int dir = cap->arg; /* TRUE for searching forward */
1561 long count = cap->count1; /* repeat count */
1562 static int lastc = NUL; /* last character searched for */
1563 static int lastcdir; /* last direction of character search */
1564 static int last_t_cmd; /* last search t_cmd */
1565 int col;
1566 char_u *p;
1567 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001568 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569#ifdef FEAT_MBYTE
Bram Moolenaar81340392012-06-06 16:12:59 +02001570 static char_u bytes[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 static int bytelen = 1; /* >1 for multi-byte char */
1572#endif
1573
1574 if (c != NUL) /* normal search: remember args for repeat */
1575 {
1576 if (!KeyStuffed) /* don't remember when redoing */
1577 {
1578 lastc = c;
1579 lastcdir = dir;
1580 last_t_cmd = t_cmd;
1581#ifdef FEAT_MBYTE
1582 bytelen = (*mb_char2bytes)(c, bytes);
1583 if (cap->ncharC1 != 0)
1584 {
1585 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1586 if (cap->ncharC2 != 0)
1587 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1588 }
1589#endif
1590 }
1591 }
1592 else /* repeat previous search */
1593 {
1594 if (lastc == NUL)
1595 return FAIL;
1596 if (dir) /* repeat in opposite direction */
1597 dir = -lastcdir;
1598 else
1599 dir = lastcdir;
1600 t_cmd = last_t_cmd;
1601 c = lastc;
1602 /* For multi-byte re-use last bytes[] and bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001603
1604 /* Force a move of at least one char, so ";" and "," will move the
1605 * cursor, even if the cursor is right in front of char we are looking
1606 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001607 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001608 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001611 if (dir == BACKWARD)
1612 cap->oap->inclusive = FALSE;
1613 else
1614 cap->oap->inclusive = TRUE;
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 p = ml_get_curline();
1617 col = curwin->w_cursor.col;
1618 len = (int)STRLEN(p);
1619
1620 while (count--)
1621 {
1622#ifdef FEAT_MBYTE
1623 if (has_mbyte)
1624 {
1625 for (;;)
1626 {
1627 if (dir > 0)
1628 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001629 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 if (col >= len)
1631 return FAIL;
1632 }
1633 else
1634 {
1635 if (col == 0)
1636 return FAIL;
1637 col -= (*mb_head_off)(p, p + col - 1) + 1;
1638 }
1639 if (bytelen == 1)
1640 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001641 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 break;
1643 }
1644 else
1645 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001646 if (vim_memcmp(p + col, bytes, bytelen) == 0 && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 break;
1648 }
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001649 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 }
1652 else
1653#endif
1654 {
1655 for (;;)
1656 {
1657 if ((col += dir) < 0 || col >= len)
1658 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001659 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001661 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 }
1664 }
1665
1666 if (t_cmd)
1667 {
1668 /* backup to before the character (possibly double-byte) */
1669 col -= dir;
1670#ifdef FEAT_MBYTE
1671 if (has_mbyte)
1672 {
1673 if (dir < 0)
1674 /* Landed on the search char which is bytelen long */
1675 col += bytelen - 1;
1676 else
1677 /* To previous char, which may be multi-byte. */
1678 col -= (*mb_head_off)(p, p + col);
1679 }
1680#endif
1681 }
1682 curwin->w_cursor.col = col;
1683
1684 return OK;
1685}
1686
1687/*
1688 * "Other" Searches
1689 */
1690
1691/*
1692 * findmatch - find the matching paren or brace
1693 *
1694 * Improvement over vi: Braces inside quotes are ignored.
1695 */
1696 pos_T *
1697findmatch(oap, initc)
1698 oparg_T *oap;
1699 int initc;
1700{
1701 return findmatchlimit(oap, initc, 0, 0);
1702}
1703
1704/*
1705 * Return TRUE if the character before "linep[col]" equals "ch".
1706 * Return FALSE if "col" is zero.
1707 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1708 * is NULL.
1709 * Handles multibyte string correctly.
1710 */
1711 static int
1712check_prevcol(linep, col, ch, prevcol)
1713 char_u *linep;
1714 int col;
1715 int ch;
1716 int *prevcol;
1717{
1718 --col;
1719#ifdef FEAT_MBYTE
1720 if (col > 0 && has_mbyte)
1721 col -= (*mb_head_off)(linep, linep + col);
1722#endif
1723 if (prevcol)
1724 *prevcol = col;
1725 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1726}
1727
1728/*
1729 * findmatchlimit -- find the matching paren or brace, if it exists within
1730 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1731 * the edge of the file.
1732 *
1733 * "initc" is the character to find a match for. NUL means to find the
1734 * character at or after the cursor.
1735 *
1736 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1737 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1738 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1739 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001740 *
1741 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1742 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 */
1744
1745 pos_T *
1746findmatchlimit(oap, initc, flags, maxtravel)
1747 oparg_T *oap;
1748 int initc;
1749 int flags;
1750 int maxtravel;
1751{
1752 static pos_T pos; /* current search position */
1753 int findc = 0; /* matching brace */
1754 int c;
1755 int count = 0; /* cumulative number of braces */
1756 int backwards = FALSE; /* init for gcc */
1757 int inquote = FALSE; /* TRUE when inside quotes */
1758 char_u *linep; /* pointer to current line */
1759 char_u *ptr;
1760 int do_quotes; /* check for quotes in current line */
1761 int at_start; /* do_quotes value at start position */
1762 int hash_dir = 0; /* Direction searched for # things */
1763 int comment_dir = 0; /* Direction searched for comments */
1764 pos_T match_pos; /* Where last slash-star was found */
1765 int start_in_quotes; /* start position is in quotes */
1766 int traveled = 0; /* how far we've searched so far */
1767 int ignore_cend = FALSE; /* ignore comment end */
1768 int cpo_match; /* vi compatible matching */
1769 int cpo_bsl; /* don't recognize backslashes */
1770 int match_escaped = 0; /* search for escaped match */
1771 int dir; /* Direction to search */
1772 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001773#ifdef FEAT_LISP
1774 int lispcomm = FALSE; /* inside of Lisp-style comment */
1775 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001779#ifdef FEAT_VIRTUALEDIT
1780 pos.coladd = 0;
1781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 linep = ml_get(pos.lnum);
1783
1784 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1785 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1786
1787 /* Direction to search when initc is '/', '*' or '#' */
1788 if (flags & FM_BACKWARD)
1789 dir = BACKWARD;
1790 else if (flags & FM_FORWARD)
1791 dir = FORWARD;
1792 else
1793 dir = 0;
1794
1795 /*
1796 * if initc given, look in the table for the matching character
1797 * '/' and '*' are special cases: look for start or end of comment.
1798 * When '/' is used, we ignore running backwards into an star-slash, for
1799 * "[*" command, we just want to find any comment.
1800 */
1801 if (initc == '/' || initc == '*')
1802 {
1803 comment_dir = dir;
1804 if (initc == '/')
1805 ignore_cend = TRUE;
1806 backwards = (dir == FORWARD) ? FALSE : TRUE;
1807 initc = NUL;
1808 }
1809 else if (initc != '#' && initc != NUL)
1810 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001811 find_mps_values(&initc, &findc, &backwards, TRUE);
1812 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 return NULL;
1814 }
1815 /*
1816 * Either initc is '#', or no initc was given and we need to look under the
1817 * cursor.
1818 */
1819 else
1820 {
1821 if (initc == '#')
1822 {
1823 hash_dir = dir;
1824 }
1825 else
1826 {
1827 /*
1828 * initc was not given, must look for something to match under
1829 * or near the cursor.
1830 * Only check for special things when 'cpo' doesn't have '%'.
1831 */
1832 if (!cpo_match)
1833 {
1834 /* Are we before or at #if, #else etc.? */
1835 ptr = skipwhite(linep);
1836 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1837 {
1838 ptr = skipwhite(ptr + 1);
1839 if ( STRNCMP(ptr, "if", 2) == 0
1840 || STRNCMP(ptr, "endif", 5) == 0
1841 || STRNCMP(ptr, "el", 2) == 0)
1842 hash_dir = 1;
1843 }
1844
1845 /* Are we on a comment? */
1846 else if (linep[pos.col] == '/')
1847 {
1848 if (linep[pos.col + 1] == '*')
1849 {
1850 comment_dir = FORWARD;
1851 backwards = FALSE;
1852 pos.col++;
1853 }
1854 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1855 {
1856 comment_dir = BACKWARD;
1857 backwards = TRUE;
1858 pos.col--;
1859 }
1860 }
1861 else if (linep[pos.col] == '*')
1862 {
1863 if (linep[pos.col + 1] == '/')
1864 {
1865 comment_dir = BACKWARD;
1866 backwards = TRUE;
1867 }
1868 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1869 {
1870 comment_dir = FORWARD;
1871 backwards = FALSE;
1872 }
1873 }
1874 }
1875
1876 /*
1877 * If we are not on a comment or the # at the start of a line, then
1878 * look for brace anywhere on this line after the cursor.
1879 */
1880 if (!hash_dir && !comment_dir)
1881 {
1882 /*
1883 * Find the brace under or after the cursor.
1884 * If beyond the end of the line, use the last character in
1885 * the line.
1886 */
1887 if (linep[pos.col] == NUL && pos.col)
1888 --pos.col;
1889 for (;;)
1890 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001891 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (initc == NUL)
1893 break;
1894
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001895 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (findc)
1897 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001898 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
1900 if (!findc)
1901 {
1902 /* no brace in the line, maybe use " #if" then */
1903 if (!cpo_match && *skipwhite(linep) == '#')
1904 hash_dir = 1;
1905 else
1906 return NULL;
1907 }
1908 else if (!cpo_bsl)
1909 {
1910 int col, bslcnt = 0;
1911
1912 /* Set "match_escaped" if there are an odd number of
1913 * backslashes. */
1914 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1915 bslcnt++;
1916 match_escaped = (bslcnt & 1);
1917 }
1918 }
1919 }
1920 if (hash_dir)
1921 {
1922 /*
1923 * Look for matching #if, #else, #elif, or #endif
1924 */
1925 if (oap != NULL)
1926 oap->motion_type = MLINE; /* Linewise for this case only */
1927 if (initc != '#')
1928 {
1929 ptr = skipwhite(skipwhite(linep) + 1);
1930 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1931 hash_dir = 1;
1932 else if (STRNCMP(ptr, "endif", 5) == 0)
1933 hash_dir = -1;
1934 else
1935 return NULL;
1936 }
1937 pos.col = 0;
1938 while (!got_int)
1939 {
1940 if (hash_dir > 0)
1941 {
1942 if (pos.lnum == curbuf->b_ml.ml_line_count)
1943 break;
1944 }
1945 else if (pos.lnum == 1)
1946 break;
1947 pos.lnum += hash_dir;
1948 linep = ml_get(pos.lnum);
1949 line_breakcheck(); /* check for CTRL-C typed */
1950 ptr = skipwhite(linep);
1951 if (*ptr != '#')
1952 continue;
1953 pos.col = (colnr_T) (ptr - linep);
1954 ptr = skipwhite(ptr + 1);
1955 if (hash_dir > 0)
1956 {
1957 if (STRNCMP(ptr, "if", 2) == 0)
1958 count++;
1959 else if (STRNCMP(ptr, "el", 2) == 0)
1960 {
1961 if (count == 0)
1962 return &pos;
1963 }
1964 else if (STRNCMP(ptr, "endif", 5) == 0)
1965 {
1966 if (count == 0)
1967 return &pos;
1968 count--;
1969 }
1970 }
1971 else
1972 {
1973 if (STRNCMP(ptr, "if", 2) == 0)
1974 {
1975 if (count == 0)
1976 return &pos;
1977 count--;
1978 }
1979 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1980 {
1981 if (count == 0)
1982 return &pos;
1983 }
1984 else if (STRNCMP(ptr, "endif", 5) == 0)
1985 count++;
1986 }
1987 }
1988 return NULL;
1989 }
1990 }
1991
1992#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00001993 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 * paren/brace in the other direction. */
1995 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1996 backwards = !backwards;
1997#endif
1998
1999 do_quotes = -1;
2000 start_in_quotes = MAYBE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00002001 clearpos(&match_pos);
2002
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002004 if ((backwards && comment_dir)
2005#ifdef FEAT_LISP
2006 || lisp
2007#endif
2008 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002010#ifdef FEAT_LISP
2011 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
2012 lispcomm = TRUE; /* find match inside this comment */
2013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 while (!got_int)
2015 {
2016 /*
2017 * Go to the next position, forward or backward. We could use
2018 * inc() and dec() here, but that is much slower
2019 */
2020 if (backwards)
2021 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002022#ifdef FEAT_LISP
2023 /* char to match is inside of comment, don't search outside */
2024 if (lispcomm && pos.col < (colnr_T)comment_col)
2025 break;
2026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 if (pos.col == 0) /* at start of line, go to prev. one */
2028 {
2029 if (pos.lnum == 1) /* start of file */
2030 break;
2031 --pos.lnum;
2032
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002033 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 break;
2035
2036 linep = ml_get(pos.lnum);
2037 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2038 do_quotes = -1;
2039 line_breakcheck();
2040
2041 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002042 if (comment_dir
2043#ifdef FEAT_LISP
2044 || lisp
2045#endif
2046 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002048#ifdef FEAT_LISP
2049 /* skip comment */
2050 if (lisp && comment_col != MAXCOL)
2051 pos.col = comment_col;
2052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054 else
2055 {
2056 --pos.col;
2057#ifdef FEAT_MBYTE
2058 if (has_mbyte)
2059 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2060#endif
2061 }
2062 }
2063 else /* forward search */
2064 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002065 if (linep[pos.col] == NUL
2066 /* at end of line, go to next one */
2067#ifdef FEAT_LISP
2068 /* don't search for match in comment */
2069 || (lisp && comment_col != MAXCOL
2070 && pos.col == (colnr_T)comment_col)
2071#endif
2072 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002074 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2075#ifdef FEAT_LISP
2076 /* line is exhausted and comment with it,
2077 * don't search for match in code */
2078 || lispcomm
2079#endif
2080 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 break;
2082 ++pos.lnum;
2083
2084 if (maxtravel && traveled++ > maxtravel)
2085 break;
2086
2087 linep = ml_get(pos.lnum);
2088 pos.col = 0;
2089 do_quotes = -1;
2090 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002091#ifdef FEAT_LISP
2092 if (lisp) /* find comment pos in new line */
2093 comment_col = check_linecomment(linep);
2094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
2096 else
2097 {
2098#ifdef FEAT_MBYTE
2099 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002100 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
2102#endif
2103 ++pos.col;
2104 }
2105 }
2106
2107 /*
2108 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2109 */
2110 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2111 (linep[0] == '{' || linep[0] == '}'))
2112 {
2113 if (linep[0] == findc && count == 0) /* match! */
2114 return &pos;
2115 break; /* out of scope */
2116 }
2117
2118 if (comment_dir)
2119 {
2120 /* Note: comments do not nest, and we ignore quotes in them */
2121 /* TODO: ignore comment brackets inside strings */
2122 if (comment_dir == FORWARD)
2123 {
2124 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2125 {
2126 pos.col++;
2127 return &pos;
2128 }
2129 }
2130 else /* Searching backwards */
2131 {
2132 /*
2133 * A comment may contain / * or / /, it may also start or end
2134 * with / * /. Ignore a / * after / /.
2135 */
2136 if (pos.col == 0)
2137 continue;
2138 else if ( linep[pos.col - 1] == '/'
2139 && linep[pos.col] == '*'
2140 && (int)pos.col < comment_col)
2141 {
2142 count++;
2143 match_pos = pos;
2144 match_pos.col--;
2145 }
2146 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2147 {
2148 if (count > 0)
2149 pos = match_pos;
2150 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2151 && (int)pos.col <= comment_col)
2152 pos.col -= 2;
2153 else if (ignore_cend)
2154 continue;
2155 else
2156 return NULL;
2157 return &pos;
2158 }
2159 }
2160 continue;
2161 }
2162
2163 /*
2164 * If smart matching ('cpoptions' does not contain '%'), braces inside
2165 * of quotes are ignored, but only if there is an even number of
2166 * quotes in the line.
2167 */
2168 if (cpo_match)
2169 do_quotes = 0;
2170 else if (do_quotes == -1)
2171 {
2172 /*
2173 * Count the number of quotes in the line, skipping \" and '"'.
2174 * Watch out for "\\".
2175 */
2176 at_start = do_quotes;
2177 for (ptr = linep; *ptr; ++ptr)
2178 {
2179 if (ptr == linep + pos.col + backwards)
2180 at_start = (do_quotes & 1);
2181 if (*ptr == '"'
2182 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2183 ++do_quotes;
2184 if (*ptr == '\\' && ptr[1] != NUL)
2185 ++ptr;
2186 }
2187 do_quotes &= 1; /* result is 1 with even number of quotes */
2188
2189 /*
2190 * If we find an uneven count, check current line and previous
2191 * one for a '\' at the end.
2192 */
2193 if (!do_quotes)
2194 {
2195 inquote = FALSE;
2196 if (ptr[-1] == '\\')
2197 {
2198 do_quotes = 1;
2199 if (start_in_quotes == MAYBE)
2200 {
2201 /* Do we need to use at_start here? */
2202 inquote = TRUE;
2203 start_in_quotes = TRUE;
2204 }
2205 else if (backwards)
2206 inquote = TRUE;
2207 }
2208 if (pos.lnum > 1)
2209 {
2210 ptr = ml_get(pos.lnum - 1);
2211 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2212 {
2213 do_quotes = 1;
2214 if (start_in_quotes == MAYBE)
2215 {
2216 inquote = at_start;
2217 if (inquote)
2218 start_in_quotes = TRUE;
2219 }
2220 else if (!backwards)
2221 inquote = TRUE;
2222 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002223
2224 /* ml_get() only keeps one line, need to get linep again */
2225 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227 }
2228 }
2229 if (start_in_quotes == MAYBE)
2230 start_in_quotes = FALSE;
2231
2232 /*
2233 * If 'smartmatch' is set:
2234 * Things inside quotes are ignored by setting 'inquote'. If we
2235 * find a quote without a preceding '\' invert 'inquote'. At the
2236 * end of a line not ending in '\' we reset 'inquote'.
2237 *
2238 * In lines with an uneven number of quotes (without preceding '\')
2239 * we do not know which part to ignore. Therefore we only set
2240 * inquote if the number of quotes in a line is even, unless this
2241 * line or the previous one ends in a '\'. Complicated, isn't it?
2242 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002243 c = PTR2CHAR(linep + pos.col);
2244 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 case NUL:
2247 /* at end of line without trailing backslash, reset inquote */
2248 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2249 {
2250 inquote = FALSE;
2251 start_in_quotes = FALSE;
2252 }
2253 break;
2254
2255 case '"':
2256 /* a quote that is preceded with an odd number of backslashes is
2257 * ignored */
2258 if (do_quotes)
2259 {
2260 int col;
2261
2262 for (col = pos.col - 1; col >= 0; --col)
2263 if (linep[col] != '\\')
2264 break;
2265 if ((((int)pos.col - 1 - col) & 1) == 0)
2266 {
2267 inquote = !inquote;
2268 start_in_quotes = FALSE;
2269 }
2270 }
2271 break;
2272
2273 /*
2274 * If smart matching ('cpoptions' does not contain '%'):
2275 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2276 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2277 * skipped, there is never a brace in them.
2278 * Ignore this when finding matches for `'.
2279 */
2280 case '\'':
2281 if (!cpo_match && initc != '\'' && findc != '\'')
2282 {
2283 if (backwards)
2284 {
2285 if (pos.col > 1)
2286 {
2287 if (linep[pos.col - 2] == '\'')
2288 {
2289 pos.col -= 2;
2290 break;
2291 }
2292 else if (linep[pos.col - 2] == '\\' &&
2293 pos.col > 2 && linep[pos.col - 3] == '\'')
2294 {
2295 pos.col -= 3;
2296 break;
2297 }
2298 }
2299 }
2300 else if (linep[pos.col + 1]) /* forward search */
2301 {
2302 if (linep[pos.col + 1] == '\\' &&
2303 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2304 {
2305 pos.col += 3;
2306 break;
2307 }
2308 else if (linep[pos.col + 2] == '\'')
2309 {
2310 pos.col += 2;
2311 break;
2312 }
2313 }
2314 }
2315 /* FALLTHROUGH */
2316
2317 default:
2318#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002319 /*
2320 * For Lisp skip over backslashed (), {} and [].
2321 * (actually, we skip #\( et al)
2322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (curbuf->b_p_lisp
2324 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002325 && pos.col > 1
2326 && check_prevcol(linep, pos.col, '\\', NULL)
2327 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 break;
2329#endif
2330
2331 /* Check for match outside of quotes, and inside of
2332 * quotes when the start is also inside of quotes. */
2333 if ((!inquote || start_in_quotes == TRUE)
2334 && (c == initc || c == findc))
2335 {
2336 int col, bslcnt = 0;
2337
2338 if (!cpo_bsl)
2339 {
2340 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2341 bslcnt++;
2342 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002343 /* Only accept a match when 'M' is in 'cpo' or when escaping
2344 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2346 {
2347 if (c == initc)
2348 count++;
2349 else
2350 {
2351 if (count == 0)
2352 return &pos;
2353 count--;
2354 }
2355 }
2356 }
2357 }
2358 }
2359
2360 if (comment_dir == BACKWARD && count > 0)
2361 {
2362 pos = match_pos;
2363 return &pos;
2364 }
2365 return (pos_T *)NULL; /* never found it */
2366}
2367
2368/*
2369 * Check if line[] contains a / / comment.
2370 * Return MAXCOL if not, otherwise return the column.
2371 * TODO: skip strings.
2372 */
2373 static int
2374check_linecomment(line)
2375 char_u *line;
2376{
2377 char_u *p;
2378
2379 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002380#ifdef FEAT_LISP
2381 /* skip Lispish one-line comments */
2382 if (curbuf->b_p_lisp)
2383 {
2384 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2385 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002386 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002387
2388 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002389 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002390 {
2391 if (*p == '"')
2392 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002393 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002394 {
2395 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002396 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002397 }
2398 else if (p == line || ((p - line) >= 2
2399 /* skip #\" form */
2400 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002401 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002402 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002403 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002404 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2405 break; /* found! */
2406 ++p;
2407 }
2408 }
2409 else
2410 p = NULL;
2411 }
2412 else
2413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 while ((p = vim_strchr(p, '/')) != NULL)
2415 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002416 /* accept a double /, unless it's preceded with * and followed by *,
2417 * because * / / * is an end and start of a C comment */
2418 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 break;
2420 ++p;
2421 }
2422
2423 if (p == NULL)
2424 return MAXCOL;
2425 return (int)(p - line);
2426}
2427
2428/*
2429 * Move cursor briefly to character matching the one under the cursor.
2430 * Used for Insert mode and "r" command.
2431 * Show the match only if it is visible on the screen.
2432 * If there isn't a match, then beep.
2433 */
2434 void
2435showmatch(c)
2436 int c; /* char to show match for */
2437{
2438 pos_T *lpos, save_cursor;
2439 pos_T mpos;
2440 colnr_T vcol;
2441 long save_so;
2442 long save_siso;
2443#ifdef CURSOR_SHAPE
2444 int save_state;
2445#endif
2446 colnr_T save_dollar_vcol;
2447 char_u *p;
2448
2449 /*
2450 * Only show match for chars in the 'matchpairs' option.
2451 */
2452 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002453 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 {
2455#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002456 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002457 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002458#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002459 p += MB_PTR2LEN(p) + 1;
2460 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461#ifdef FEAT_RIGHTLEFT
2462 && !(curwin->w_p_rl ^ p_ri)
2463#endif
2464 )
2465 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002466 p += MB_PTR2LEN(p);
2467 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 return;
2469 }
2470
2471 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
Bram Moolenaar165bc692015-07-21 17:53:25 +02002472 vim_beep(BO_MATCH);
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002473 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 if (!curwin->w_p_wrap)
2476 getvcol(curwin, lpos, NULL, &vcol, NULL);
2477 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2478 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2479 {
2480 mpos = *lpos; /* save the pos, update_screen() may change it */
2481 save_cursor = curwin->w_cursor;
2482 save_so = p_so;
2483 save_siso = p_siso;
2484 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2485 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002486 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2487 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 ++curwin->w_virtcol; /* do display ')' just before "$" */
2489 update_screen(VALID); /* show the new char first */
2490
2491 save_dollar_vcol = dollar_vcol;
2492#ifdef CURSOR_SHAPE
2493 save_state = State;
2494 State = SHOWMATCH;
2495 ui_cursor_shape(); /* may show different cursor shape */
2496#endif
2497 curwin->w_cursor = mpos; /* move to matching char */
2498 p_so = 0; /* don't use 'scrolloff' here */
2499 p_siso = 0; /* don't use 'sidescrolloff' here */
2500 showruler(FALSE);
2501 setcursor();
2502 cursor_on(); /* make sure that the cursor is shown */
2503 out_flush();
2504#ifdef FEAT_GUI
2505 if (gui.in_use)
2506 {
2507 gui_update_cursor(TRUE, FALSE);
2508 gui_mch_flush();
2509 }
2510#endif
2511 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2512 * which resets it if the matching position is in a previous line
2513 * and has a higher column number. */
2514 dollar_vcol = save_dollar_vcol;
2515
2516 /*
2517 * brief pause, unless 'm' is present in 'cpo' and a character is
2518 * available.
2519 */
2520 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2521 ui_delay(p_mat * 100L, TRUE);
2522 else if (!char_avail())
2523 ui_delay(p_mat * 100L, FALSE);
2524 curwin->w_cursor = save_cursor; /* restore cursor position */
2525 p_so = save_so;
2526 p_siso = save_siso;
2527#ifdef CURSOR_SHAPE
2528 State = save_state;
2529 ui_cursor_shape(); /* may show different cursor shape */
2530#endif
2531 }
2532 }
2533}
2534
2535/*
2536 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002537 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 * space or a line break. Also stop at an empty line.
2539 * Return OK if the next sentence was found.
2540 */
2541 int
2542findsent(dir, count)
2543 int dir;
2544 long count;
2545{
2546 pos_T pos, tpos;
2547 int c;
2548 int (*func) __ARGS((pos_T *));
2549 int startlnum;
2550 int noskip = FALSE; /* do not skip blanks */
2551 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002552 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 pos = curwin->w_cursor;
2555 if (dir == FORWARD)
2556 func = incl;
2557 else
2558 func = decl;
2559
2560 while (count--)
2561 {
2562 /*
2563 * if on an empty line, skip upto a non-empty line
2564 */
2565 if (gchar_pos(&pos) == NUL)
2566 {
2567 do
2568 if ((*func)(&pos) == -1)
2569 break;
2570 while (gchar_pos(&pos) == NUL);
2571 if (dir == FORWARD)
2572 goto found;
2573 }
2574 /*
2575 * if on the start of a paragraph or a section and searching forward,
2576 * go to the next line
2577 */
2578 else if (dir == FORWARD && pos.col == 0 &&
2579 startPS(pos.lnum, NUL, FALSE))
2580 {
2581 if (pos.lnum == curbuf->b_ml.ml_line_count)
2582 return FAIL;
2583 ++pos.lnum;
2584 goto found;
2585 }
2586 else if (dir == BACKWARD)
2587 decl(&pos);
2588
2589 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002590 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2592 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2593 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002594 if (vim_strchr((char_u *)".!?", c) != NULL)
2595 {
2596 /* Only skip over a '.', '!' and '?' once. */
2597 if (found_dot)
2598 break;
2599 found_dot = TRUE;
2600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 if (decl(&pos) == -1)
2602 break;
2603 /* when going forward: Stop in front of empty line */
2604 if (lineempty(pos.lnum) && dir == FORWARD)
2605 {
2606 incl(&pos);
2607 goto found;
2608 }
2609 }
2610
2611 /* remember the line where the search started */
2612 startlnum = pos.lnum;
2613 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2614
2615 for (;;) /* find end of sentence */
2616 {
2617 c = gchar_pos(&pos);
2618 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2619 {
2620 if (dir == BACKWARD && pos.lnum != startlnum)
2621 ++pos.lnum;
2622 break;
2623 }
2624 if (c == '.' || c == '!' || c == '?')
2625 {
2626 tpos = pos;
2627 do
2628 if ((c = inc(&tpos)) == -1)
2629 break;
2630 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2631 != NULL);
2632 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2633 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2634 && gchar_pos(&tpos) == ' ')))
2635 {
2636 pos = tpos;
2637 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2638 inc(&pos);
2639 break;
2640 }
2641 }
2642 if ((*func)(&pos) == -1)
2643 {
2644 if (count)
2645 return FAIL;
2646 noskip = TRUE;
2647 break;
2648 }
2649 }
2650found:
2651 /* skip white space */
2652 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2653 if (incl(&pos) == -1)
2654 break;
2655 }
2656
2657 setpcmark();
2658 curwin->w_cursor = pos;
2659 return OK;
2660}
2661
2662/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002663 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002665 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 * If 'what' is '{' or '}' we go to the next section.
2667 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002668 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 */
2670 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002671findpar(pincl, dir, count, what, both)
2672 int *pincl; /* Return: TRUE if last char is to be included */
2673 int dir;
2674 long count;
2675 int what;
2676 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 linenr_T curr;
2679 int did_skip; /* TRUE after separating lines have been skipped */
2680 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002681 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682#ifdef FEAT_FOLDING
2683 linenr_T fold_first; /* first line of a closed fold */
2684 linenr_T fold_last; /* last line of a closed fold */
2685 int fold_skipped; /* TRUE if a closed fold was skipped this
2686 iteration */
2687#endif
2688
2689 curr = curwin->w_cursor.lnum;
2690
2691 while (count--)
2692 {
2693 did_skip = FALSE;
2694 for (first = TRUE; ; first = FALSE)
2695 {
2696 if (*ml_get(curr) != NUL)
2697 did_skip = TRUE;
2698
2699#ifdef FEAT_FOLDING
2700 /* skip folded lines */
2701 fold_skipped = FALSE;
2702 if (first && hasFolding(curr, &fold_first, &fold_last))
2703 {
2704 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2705 fold_skipped = TRUE;
2706 }
2707#endif
2708
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002709 /* POSIX has it's own ideas of what a paragraph boundary is and it
2710 * doesn't match historical Vi: It also stops at a "{" in the
2711 * first column and at an empty line. */
2712 if (!first && did_skip && (startPS(curr, what, both)
2713 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 break;
2715
2716#ifdef FEAT_FOLDING
2717 if (fold_skipped)
2718 curr -= dir;
2719#endif
2720 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2721 {
2722 if (count)
2723 return FALSE;
2724 curr -= dir;
2725 break;
2726 }
2727 }
2728 }
2729 setpcmark();
2730 if (both && *ml_get(curr) == '}') /* include line with '}' */
2731 ++curr;
2732 curwin->w_cursor.lnum = curr;
2733 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2734 {
2735 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2736 {
2737 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002738 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740 }
2741 else
2742 curwin->w_cursor.col = 0;
2743 return TRUE;
2744}
2745
2746/*
2747 * check if the string 's' is a nroff macro that is in option 'opt'
2748 */
2749 static int
2750inmacro(opt, s)
2751 char_u *opt;
2752 char_u *s;
2753{
2754 char_u *macro;
2755
2756 for (macro = opt; macro[0]; ++macro)
2757 {
2758 /* Accept two characters in the option being equal to two characters
2759 * in the line. A space in the option matches with a space in the
2760 * line or the line having ended. */
2761 if ( (macro[0] == s[0]
2762 || (macro[0] == ' '
2763 && (s[0] == NUL || s[0] == ' ')))
2764 && (macro[1] == s[1]
2765 || ((macro[1] == NUL || macro[1] == ' ')
2766 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2767 break;
2768 ++macro;
2769 if (macro[0] == NUL)
2770 break;
2771 }
2772 return (macro[0] != NUL);
2773}
2774
2775/*
2776 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2777 * If 'para' is '{' or '}' only check for sections.
2778 * If 'both' is TRUE also stop at '}'
2779 */
2780 int
2781startPS(lnum, para, both)
2782 linenr_T lnum;
2783 int para;
2784 int both;
2785{
2786 char_u *s;
2787
2788 s = ml_get(lnum);
2789 if (*s == para || *s == '\f' || (both && *s == '}'))
2790 return TRUE;
2791 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2792 (!para && inmacro(p_para, s + 1))))
2793 return TRUE;
2794 return FALSE;
2795}
2796
2797/*
2798 * The following routines do the word searches performed by the 'w', 'W',
2799 * 'b', 'B', 'e', and 'E' commands.
2800 */
2801
2802/*
2803 * To perform these searches, characters are placed into one of three
2804 * classes, and transitions between classes determine word boundaries.
2805 *
2806 * The classes are:
2807 *
2808 * 0 - white space
2809 * 1 - punctuation
2810 * 2 or higher - keyword characters (letters, digits and underscore)
2811 */
2812
2813static int cls_bigword; /* TRUE for "W", "B" or "E" */
2814
2815/*
2816 * cls() - returns the class of character at curwin->w_cursor
2817 *
2818 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2819 * from class 2 and higher are reported as class 1 since only white space
2820 * boundaries are of interest.
2821 */
2822 static int
2823cls()
2824{
2825 int c;
2826
2827 c = gchar_cursor();
2828#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2829 if (p_altkeymap && c == F_BLANK)
2830 return 0;
2831#endif
2832 if (c == ' ' || c == '\t' || c == NUL)
2833 return 0;
2834#ifdef FEAT_MBYTE
2835 if (enc_dbcs != 0 && c > 0xFF)
2836 {
2837 /* If cls_bigword, report multi-byte chars as class 1. */
2838 if (enc_dbcs == DBCS_KOR && cls_bigword)
2839 return 1;
2840
2841 /* process code leading/trailing bytes */
2842 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2843 }
2844 if (enc_utf8)
2845 {
2846 c = utf_class(c);
2847 if (c != 0 && cls_bigword)
2848 return 1;
2849 return c;
2850 }
2851#endif
2852
2853 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2854 if (cls_bigword)
2855 return 1;
2856
2857 if (vim_iswordc(c))
2858 return 2;
2859 return 1;
2860}
2861
2862
2863/*
2864 * fwd_word(count, type, eol) - move forward one word
2865 *
2866 * Returns FAIL if the cursor was already at the end of the file.
2867 * If eol is TRUE, last word stops at end of line (for operators).
2868 */
2869 int
2870fwd_word(count, bigword, eol)
2871 long count;
2872 int bigword; /* "W", "E" or "B" */
2873 int eol;
2874{
2875 int sclass; /* starting class */
2876 int i;
2877 int last_line;
2878
2879#ifdef FEAT_VIRTUALEDIT
2880 curwin->w_cursor.coladd = 0;
2881#endif
2882 cls_bigword = bigword;
2883 while (--count >= 0)
2884 {
2885#ifdef FEAT_FOLDING
2886 /* When inside a range of folded lines, move to the last char of the
2887 * last line. */
2888 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2889 coladvance((colnr_T)MAXCOL);
2890#endif
2891 sclass = cls();
2892
2893 /*
2894 * We always move at least one character, unless on the last
2895 * character in the buffer.
2896 */
2897 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2898 i = inc_cursor();
2899 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2900 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00002901 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 return OK;
2903
2904 /*
2905 * Go one char past end of current word (if any)
2906 */
2907 if (sclass != 0)
2908 while (cls() == sclass)
2909 {
2910 i = inc_cursor();
2911 if (i == -1 || (i >= 1 && eol && count == 0))
2912 return OK;
2913 }
2914
2915 /*
2916 * go to next non-white
2917 */
2918 while (cls() == 0)
2919 {
2920 /*
2921 * We'll stop if we land on a blank line
2922 */
2923 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2924 break;
2925
2926 i = inc_cursor();
2927 if (i == -1 || (i >= 1 && eol && count == 0))
2928 return OK;
2929 }
2930 }
2931 return OK;
2932}
2933
2934/*
2935 * bck_word() - move backward 'count' words
2936 *
2937 * If stop is TRUE and we are already on the start of a word, move one less.
2938 *
2939 * Returns FAIL if top of the file was reached.
2940 */
2941 int
2942bck_word(count, bigword, stop)
2943 long count;
2944 int bigword;
2945 int stop;
2946{
2947 int sclass; /* starting class */
2948
2949#ifdef FEAT_VIRTUALEDIT
2950 curwin->w_cursor.coladd = 0;
2951#endif
2952 cls_bigword = bigword;
2953 while (--count >= 0)
2954 {
2955#ifdef FEAT_FOLDING
2956 /* When inside a range of folded lines, move to the first char of the
2957 * first line. */
2958 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2959 curwin->w_cursor.col = 0;
2960#endif
2961 sclass = cls();
2962 if (dec_cursor() == -1) /* started at start of file */
2963 return FAIL;
2964
2965 if (!stop || sclass == cls() || sclass == 0)
2966 {
2967 /*
2968 * Skip white space before the word.
2969 * Stop on an empty line.
2970 */
2971 while (cls() == 0)
2972 {
2973 if (curwin->w_cursor.col == 0
2974 && lineempty(curwin->w_cursor.lnum))
2975 goto finished;
2976 if (dec_cursor() == -1) /* hit start of file, stop here */
2977 return OK;
2978 }
2979
2980 /*
2981 * Move backward to start of this word.
2982 */
2983 if (skip_chars(cls(), BACKWARD))
2984 return OK;
2985 }
2986
2987 inc_cursor(); /* overshot - forward one */
2988finished:
2989 stop = FALSE;
2990 }
2991 return OK;
2992}
2993
2994/*
2995 * end_word() - move to the end of the word
2996 *
2997 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2998 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2999 * motion crosses blank lines. When the real vi crosses a blank line in an
3000 * 'e' motion, the cursor is placed on the FIRST character of the next
3001 * non-blank line. The 'E' command, however, works correctly. Since this
3002 * appears to be a bug, I have not duplicated it here.
3003 *
3004 * Returns FAIL if end of the file was reached.
3005 *
3006 * If stop is TRUE and we are already on the end of a word, move one less.
3007 * If empty is TRUE stop on an empty line.
3008 */
3009 int
3010end_word(count, bigword, stop, empty)
3011 long count;
3012 int bigword;
3013 int stop;
3014 int empty;
3015{
3016 int sclass; /* starting class */
3017
3018#ifdef FEAT_VIRTUALEDIT
3019 curwin->w_cursor.coladd = 0;
3020#endif
3021 cls_bigword = bigword;
3022 while (--count >= 0)
3023 {
3024#ifdef FEAT_FOLDING
3025 /* When inside a range of folded lines, move to the last char of the
3026 * last line. */
3027 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3028 coladvance((colnr_T)MAXCOL);
3029#endif
3030 sclass = cls();
3031 if (inc_cursor() == -1)
3032 return FAIL;
3033
3034 /*
3035 * If we're in the middle of a word, we just have to move to the end
3036 * of it.
3037 */
3038 if (cls() == sclass && sclass != 0)
3039 {
3040 /*
3041 * Move forward to end of the current word
3042 */
3043 if (skip_chars(sclass, FORWARD))
3044 return FAIL;
3045 }
3046 else if (!stop || sclass == 0)
3047 {
3048 /*
3049 * We were at the end of a word. Go to the end of the next word.
3050 * First skip white space, if 'empty' is TRUE, stop at empty line.
3051 */
3052 while (cls() == 0)
3053 {
3054 if (empty && curwin->w_cursor.col == 0
3055 && lineempty(curwin->w_cursor.lnum))
3056 goto finished;
3057 if (inc_cursor() == -1) /* hit end of file, stop here */
3058 return FAIL;
3059 }
3060
3061 /*
3062 * Move forward to the end of this word.
3063 */
3064 if (skip_chars(cls(), FORWARD))
3065 return FAIL;
3066 }
3067 dec_cursor(); /* overshot - one char backward */
3068finished:
3069 stop = FALSE; /* we move only one word less */
3070 }
3071 return OK;
3072}
3073
3074/*
3075 * Move back to the end of the word.
3076 *
3077 * Returns FAIL if start of the file was reached.
3078 */
3079 int
3080bckend_word(count, bigword, eol)
3081 long count;
3082 int bigword; /* TRUE for "B" */
3083 int eol; /* TRUE: stop at end of line. */
3084{
3085 int sclass; /* starting class */
3086 int i;
3087
3088#ifdef FEAT_VIRTUALEDIT
3089 curwin->w_cursor.coladd = 0;
3090#endif
3091 cls_bigword = bigword;
3092 while (--count >= 0)
3093 {
3094 sclass = cls();
3095 if ((i = dec_cursor()) == -1)
3096 return FAIL;
3097 if (eol && i == 1)
3098 return OK;
3099
3100 /*
3101 * Move backward to before the start of this word.
3102 */
3103 if (sclass != 0)
3104 {
3105 while (cls() == sclass)
3106 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3107 return OK;
3108 }
3109
3110 /*
3111 * Move backward to end of the previous word
3112 */
3113 while (cls() == 0)
3114 {
3115 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3116 break;
3117 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3118 return OK;
3119 }
3120 }
3121 return OK;
3122}
3123
3124/*
3125 * Skip a row of characters of the same class.
3126 * Return TRUE when end-of-file reached, FALSE otherwise.
3127 */
3128 static int
3129skip_chars(cclass, dir)
3130 int cclass;
3131 int dir;
3132{
3133 while (cls() == cclass)
3134 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3135 return TRUE;
3136 return FALSE;
3137}
3138
3139#ifdef FEAT_TEXTOBJ
3140/*
3141 * Go back to the start of the word or the start of white space
3142 */
3143 static void
3144back_in_line()
3145{
3146 int sclass; /* starting class */
3147
3148 sclass = cls();
3149 for (;;)
3150 {
3151 if (curwin->w_cursor.col == 0) /* stop at start of line */
3152 break;
3153 dec_cursor();
3154 if (cls() != sclass) /* stop at start of word */
3155 {
3156 inc_cursor();
3157 break;
3158 }
3159 }
3160}
3161
3162 static void
3163find_first_blank(posp)
3164 pos_T *posp;
3165{
3166 int c;
3167
3168 while (decl(posp) != -1)
3169 {
3170 c = gchar_pos(posp);
3171 if (!vim_iswhite(c))
3172 {
3173 incl(posp);
3174 break;
3175 }
3176 }
3177}
3178
3179/*
3180 * Skip count/2 sentences and count/2 separating white spaces.
3181 */
3182 static void
3183findsent_forward(count, at_start_sent)
3184 long count;
3185 int at_start_sent; /* cursor is at start of sentence */
3186{
3187 while (count--)
3188 {
3189 findsent(FORWARD, 1L);
3190 if (at_start_sent)
3191 find_first_blank(&curwin->w_cursor);
3192 if (count == 0 || at_start_sent)
3193 decl(&curwin->w_cursor);
3194 at_start_sent = !at_start_sent;
3195 }
3196}
3197
3198/*
3199 * Find word under cursor, cursor at end.
3200 * Used while an operator is pending, and in Visual mode.
3201 */
3202 int
3203current_word(oap, count, include, bigword)
3204 oparg_T *oap;
3205 long count;
3206 int include; /* TRUE: include word and white space */
3207 int bigword; /* FALSE == word, TRUE == WORD */
3208{
3209 pos_T start_pos;
3210 pos_T pos;
3211 int inclusive = TRUE;
3212 int include_white = FALSE;
3213
3214 cls_bigword = bigword;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003215 clearpos(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 /* Correct cursor when 'selection' is exclusive */
3218 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3219 dec_cursor();
3220
3221 /*
3222 * When Visual mode is not active, or when the VIsual area is only one
3223 * character, select the word and/or white space under the cursor.
3224 */
3225 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 {
3227 /*
3228 * Go to start of current word or white space.
3229 */
3230 back_in_line();
3231 start_pos = curwin->w_cursor;
3232
3233 /*
3234 * If the start is on white space, and white space should be included
3235 * (" word"), or start is not on white space, and white space should
3236 * not be included ("word"), find end of word.
3237 */
3238 if ((cls() == 0) == include)
3239 {
3240 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3241 return FAIL;
3242 }
3243 else
3244 {
3245 /*
3246 * If the start is not on white space, and white space should be
3247 * included ("word "), or start is on white space and white
3248 * space should not be included (" "), find start of word.
3249 * If we end up in the first column of the next line (single char
3250 * word) back up to end of the line.
3251 */
3252 fwd_word(1L, bigword, TRUE);
3253 if (curwin->w_cursor.col == 0)
3254 decl(&curwin->w_cursor);
3255 else
3256 oneleft();
3257
3258 if (include)
3259 include_white = TRUE;
3260 }
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (VIsual_active)
3263 {
3264 /* should do something when inclusive == FALSE ! */
3265 VIsual = start_pos;
3266 redraw_curbuf_later(INVERTED); /* update the inversion */
3267 }
3268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
3270 oap->start = start_pos;
3271 oap->motion_type = MCHAR;
3272 }
3273 --count;
3274 }
3275
3276 /*
3277 * When count is still > 0, extend with more objects.
3278 */
3279 while (count > 0)
3280 {
3281 inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3283 {
3284 /*
3285 * In Visual mode, with cursor at start: move cursor back.
3286 */
3287 if (decl(&curwin->w_cursor) == -1)
3288 return FAIL;
3289 if (include != (cls() != 0))
3290 {
3291 if (bck_word(1L, bigword, TRUE) == FAIL)
3292 return FAIL;
3293 }
3294 else
3295 {
3296 if (bckend_word(1L, bigword, TRUE) == FAIL)
3297 return FAIL;
3298 (void)incl(&curwin->w_cursor);
3299 }
3300 }
3301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 {
3303 /*
3304 * Move cursor forward one word and/or white area.
3305 */
3306 if (incl(&curwin->w_cursor) == -1)
3307 return FAIL;
3308 if (include != (cls() == 0))
3309 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003310 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return FAIL;
3312 /*
3313 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003314 * the first character on the line.
3315 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003317 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 inclusive = FALSE;
3319 }
3320 else
3321 {
3322 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3323 return FAIL;
3324 }
3325 }
3326 --count;
3327 }
3328
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003329 if (include_white && (cls() != 0
3330 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 {
3332 /*
3333 * If we don't include white space at the end, move the start
3334 * to include some white space there. This makes "daw" work
3335 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003336 * word). Also when "2daw" deletes "word." at the end of the line
3337 * (cursor is at start of next line).
3338 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
3340 pos = curwin->w_cursor; /* save cursor position */
3341 curwin->w_cursor = start_pos;
3342 if (oneleft() == OK)
3343 {
3344 back_in_line();
3345 if (cls() == 0 && curwin->w_cursor.col > 0)
3346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 if (VIsual_active)
3348 VIsual = curwin->w_cursor;
3349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 oap->start = curwin->w_cursor;
3351 }
3352 }
3353 curwin->w_cursor = pos; /* put cursor back at end */
3354 }
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 if (VIsual_active)
3357 {
3358 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3359 inc_cursor();
3360 if (VIsual_mode == 'V')
3361 {
3362 VIsual_mode = 'v';
3363 redraw_cmdline = TRUE; /* show mode later */
3364 }
3365 }
3366 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 oap->inclusive = inclusive;
3368
3369 return OK;
3370}
3371
3372/*
3373 * Find sentence(s) under the cursor, cursor at end.
3374 * When Visual active, extend it by one or more sentences.
3375 */
3376 int
3377current_sent(oap, count, include)
3378 oparg_T *oap;
3379 long count;
3380 int include;
3381{
3382 pos_T start_pos;
3383 pos_T pos;
3384 int start_blank;
3385 int c;
3386 int at_start_sent;
3387 long ncount;
3388
3389 start_pos = curwin->w_cursor;
3390 pos = start_pos;
3391 findsent(FORWARD, 1L); /* Find start of next sentence. */
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003394 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 */
3396 if (VIsual_active && !equalpos(start_pos, VIsual))
3397 {
3398extend:
3399 if (lt(start_pos, VIsual))
3400 {
3401 /*
3402 * Cursor at start of Visual area.
3403 * Find out where we are:
3404 * - in the white space before a sentence
3405 * - in a sentence or just after it
3406 * - at the start of a sentence
3407 */
3408 at_start_sent = TRUE;
3409 decl(&pos);
3410 while (lt(pos, curwin->w_cursor))
3411 {
3412 c = gchar_pos(&pos);
3413 if (!vim_iswhite(c))
3414 {
3415 at_start_sent = FALSE;
3416 break;
3417 }
3418 incl(&pos);
3419 }
3420 if (!at_start_sent)
3421 {
3422 findsent(BACKWARD, 1L);
3423 if (equalpos(curwin->w_cursor, start_pos))
3424 at_start_sent = TRUE; /* exactly at start of sentence */
3425 else
3426 /* inside a sentence, go to its end (start of next) */
3427 findsent(FORWARD, 1L);
3428 }
3429 if (include) /* "as" gets twice as much as "is" */
3430 count *= 2;
3431 while (count--)
3432 {
3433 if (at_start_sent)
3434 find_first_blank(&curwin->w_cursor);
3435 c = gchar_cursor();
3436 if (!at_start_sent || (!include && !vim_iswhite(c)))
3437 findsent(BACKWARD, 1L);
3438 at_start_sent = !at_start_sent;
3439 }
3440 }
3441 else
3442 {
3443 /*
3444 * Cursor at end of Visual area.
3445 * Find out where we are:
3446 * - just before a sentence
3447 * - just before or in the white space before a sentence
3448 * - in a sentence
3449 */
3450 incl(&pos);
3451 at_start_sent = TRUE;
3452 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3453 {
3454 at_start_sent = FALSE;
3455 while (lt(pos, curwin->w_cursor))
3456 {
3457 c = gchar_pos(&pos);
3458 if (!vim_iswhite(c))
3459 {
3460 at_start_sent = TRUE;
3461 break;
3462 }
3463 incl(&pos);
3464 }
3465 if (at_start_sent) /* in the sentence */
3466 findsent(BACKWARD, 1L);
3467 else /* in/before white before a sentence */
3468 curwin->w_cursor = start_pos;
3469 }
3470
3471 if (include) /* "as" gets twice as much as "is" */
3472 count *= 2;
3473 findsent_forward(count, at_start_sent);
3474 if (*p_sel == 'e')
3475 ++curwin->w_cursor.col;
3476 }
3477 return OK;
3478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003481 * If the cursor started on a blank, check if it is just before the start
3482 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
3484 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3485 incl(&pos);
3486 if (equalpos(pos, curwin->w_cursor))
3487 {
3488 start_blank = TRUE;
3489 find_first_blank(&start_pos); /* go back to first blank */
3490 }
3491 else
3492 {
3493 start_blank = FALSE;
3494 findsent(BACKWARD, 1L);
3495 start_pos = curwin->w_cursor;
3496 }
3497 if (include)
3498 ncount = count * 2;
3499 else
3500 {
3501 ncount = count;
3502 if (start_blank)
3503 --ncount;
3504 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003505 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 findsent_forward(ncount, TRUE);
3507 else
3508 decl(&curwin->w_cursor);
3509
3510 if (include)
3511 {
3512 /*
3513 * If the blank in front of the sentence is included, exclude the
3514 * blanks at the end of the sentence, go back to the first blank.
3515 * If there are no trailing blanks, try to include leading blanks.
3516 */
3517 if (start_blank)
3518 {
3519 find_first_blank(&curwin->w_cursor);
3520 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3521 if (vim_iswhite(c))
3522 decl(&curwin->w_cursor);
3523 }
3524 else if (c = gchar_cursor(), !vim_iswhite(c))
3525 find_first_blank(&start_pos);
3526 }
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (VIsual_active)
3529 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003530 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 if (equalpos(start_pos, curwin->w_cursor))
3532 goto extend;
3533 if (*p_sel == 'e')
3534 ++curwin->w_cursor.col;
3535 VIsual = start_pos;
3536 VIsual_mode = 'v';
3537 redraw_curbuf_later(INVERTED); /* update the inversion */
3538 }
3539 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 /* include a newline after the sentence, if there is one */
3542 if (incl(&curwin->w_cursor) == -1)
3543 oap->inclusive = TRUE;
3544 else
3545 oap->inclusive = FALSE;
3546 oap->start = start_pos;
3547 oap->motion_type = MCHAR;
3548 }
3549 return OK;
3550}
3551
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003552/*
3553 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003554 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 int
3557current_block(oap, count, include, what, other)
3558 oparg_T *oap;
3559 long count;
3560 int include; /* TRUE == include white space */
3561 int what; /* '(', '{', etc. */
3562 int other; /* ')', '}', etc. */
3563{
3564 pos_T old_pos;
3565 pos_T *pos = NULL;
3566 pos_T start_pos;
3567 pos_T *end_pos;
3568 pos_T old_start, old_end;
3569 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003570 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003573 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 old_start = old_end;
3575
3576 /*
3577 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003582 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 while (inindent(1))
3584 if (inc_cursor() != 0)
3585 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003586 if (gchar_cursor() == what)
3587 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 ++curwin->w_cursor.col;
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 else if (lt(VIsual, curwin->w_cursor))
3591 {
3592 old_start = VIsual;
3593 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3594 }
3595 else
3596 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
3598 /*
3599 * Search backwards for unclosed '(', '{', etc..
3600 * Put this position in start_pos.
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003601 * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the
3602 * user wants.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 */
3604 save_cpo = p_cpo;
Bram Moolenaar438b64a2015-03-13 15:03:00 +01003605 p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 while (count-- > 0)
3607 {
3608 if ((pos = findmatch(NULL, what)) == NULL)
3609 break;
3610 curwin->w_cursor = *pos;
3611 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3612 }
3613 p_cpo = save_cpo;
3614
3615 /*
3616 * Search for matching ')', '}', etc.
3617 * Put this position in curwin->w_cursor.
3618 */
3619 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3620 {
3621 curwin->w_cursor = old_pos;
3622 return FAIL;
3623 }
3624 curwin->w_cursor = *end_pos;
3625
3626 /*
3627 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003628 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3629 * indent. But only if the resulting area is not smaller than what we
3630 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 */
3632 while (!include)
3633 {
3634 incl(&start_pos);
3635 sol = (curwin->w_cursor.col == 0);
3636 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003637 while (inindent(1))
3638 {
3639 sol = TRUE;
3640 if (decl(&curwin->w_cursor) != 0)
3641 break;
3642 }
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 /*
3645 * In Visual mode, when the resulting area is not bigger than what we
3646 * started with, extend it to the next block, and then exclude again.
3647 */
3648 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3649 && VIsual_active)
3650 {
3651 curwin->w_cursor = old_start;
3652 decl(&curwin->w_cursor);
3653 if ((pos = findmatch(NULL, what)) == NULL)
3654 {
3655 curwin->w_cursor = old_pos;
3656 return FAIL;
3657 }
3658 start_pos = *pos;
3659 curwin->w_cursor = *pos;
3660 if ((end_pos = findmatch(NULL, other)) == NULL)
3661 {
3662 curwin->w_cursor = old_pos;
3663 return FAIL;
3664 }
3665 curwin->w_cursor = *end_pos;
3666 }
3667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 break;
3669 }
3670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (VIsual_active)
3672 {
3673 if (*p_sel == 'e')
3674 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003675 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 inc(&curwin->w_cursor); /* include the line break */
3677 VIsual = start_pos;
3678 VIsual_mode = 'v';
3679 redraw_curbuf_later(INVERTED); /* update the inversion */
3680 showmode();
3681 }
3682 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
3684 oap->start = start_pos;
3685 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003686 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 incl(&curwin->w_cursor);
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003689 else if (ltoreq(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003690 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003692 else
3693 /* End is before the start (no text in between <>, [], etc.): don't
3694 * operate on any text. */
3695 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697
3698 return OK;
3699}
3700
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003701static int in_html_tag __ARGS((int));
3702
3703/*
3704 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3705 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3706 */
3707 static int
3708in_html_tag(end_tag)
3709 int end_tag;
3710{
3711 char_u *line = ml_get_curline();
3712 char_u *p;
3713 int c;
3714 int lc = NUL;
3715 pos_T pos;
3716
3717#ifdef FEAT_MBYTE
3718 if (enc_dbcs)
3719 {
3720 char_u *lp = NULL;
3721
3722 /* We search forward until the cursor, because searching backwards is
3723 * very slow for DBCS encodings. */
3724 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3725 if (*p == '>' || *p == '<')
3726 {
3727 lc = *p;
3728 lp = p;
3729 }
3730 if (*p != '<') /* check for '<' under cursor */
3731 {
3732 if (lc != '<')
3733 return FALSE;
3734 p = lp;
3735 }
3736 }
3737 else
3738#endif
3739 {
3740 for (p = line + curwin->w_cursor.col; p > line; )
3741 {
3742 if (*p == '<') /* find '<' under/before cursor */
3743 break;
3744 mb_ptr_back(line, p);
3745 if (*p == '>') /* find '>' before cursor */
3746 break;
3747 }
3748 if (*p != '<')
3749 return FALSE;
3750 }
3751
3752 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003753 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003754
3755 mb_ptr_adv(p);
3756 if (end_tag)
3757 /* check that there is a '/' after the '<' */
3758 return *p == '/';
3759
3760 /* check that there is no '/' after the '<' */
3761 if (*p == '/')
3762 return FALSE;
3763
3764 /* check that the matching '>' is not preceded by '/' */
3765 for (;;)
3766 {
3767 if (inc(&pos) < 0)
3768 return FALSE;
3769 c = *ml_get_pos(&pos);
3770 if (c == '>')
3771 break;
3772 lc = c;
3773 }
3774 return lc != '/';
3775}
3776
3777/*
3778 * Find tag block under the cursor, cursor at end.
3779 */
3780 int
3781current_tagblock(oap, count_arg, include)
3782 oparg_T *oap;
3783 long count_arg;
3784 int include; /* TRUE == include white space */
3785{
3786 long count = count_arg;
3787 long n;
3788 pos_T old_pos;
3789 pos_T start_pos;
3790 pos_T end_pos;
3791 pos_T old_start, old_end;
3792 char_u *spat, *epat;
3793 char_u *p;
3794 char_u *cp;
3795 int len;
3796 int r;
3797 int do_include = include;
3798 int save_p_ws = p_ws;
3799 int retval = FAIL;
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003800 int is_inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003801
3802 p_ws = FALSE;
3803
3804 old_pos = curwin->w_cursor;
3805 old_end = curwin->w_cursor; /* remember where we started */
3806 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003807 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003808 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003809
3810 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003811 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003812 */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003813 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003814 {
3815 setpcmark();
3816
3817 /* ignore indent */
3818 while (inindent(1))
3819 if (inc_cursor() != 0)
3820 break;
3821
3822 if (in_html_tag(FALSE))
3823 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003824 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003825 while (*ml_get_cursor() != '>')
3826 if (inc_cursor() < 0)
3827 break;
3828 }
3829 else if (in_html_tag(TRUE))
3830 {
3831 /* cursor on end tag, move to just before it */
3832 while (*ml_get_cursor() != '<')
3833 if (dec_cursor() < 0)
3834 break;
3835 dec_cursor();
3836 old_end = curwin->w_cursor;
3837 }
3838 }
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003839 else if (lt(VIsual, curwin->w_cursor))
3840 {
3841 old_start = VIsual;
3842 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3843 }
3844 else
3845 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003846
3847again:
3848 /*
3849 * Search backwards for unclosed "<aaa>".
3850 * Put this position in start_pos.
3851 */
3852 for (n = 0; n < count; ++n)
3853 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003854 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003855 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003856 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00003857 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003858 {
3859 curwin->w_cursor = old_pos;
3860 goto theend;
3861 }
3862 }
3863 start_pos = curwin->w_cursor;
3864
3865 /*
3866 * Search for matching "</aaa>". First isolate the "aaa".
3867 */
3868 inc_cursor();
3869 p = ml_get_cursor();
3870 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3871 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003872 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003873 if (len == 0)
3874 {
3875 curwin->w_cursor = old_pos;
3876 goto theend;
3877 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01003878 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003879 epat = alloc(len + 9);
3880 if (spat == NULL || epat == NULL)
3881 {
3882 vim_free(spat);
3883 vim_free(epat);
3884 curwin->w_cursor = old_pos;
3885 goto theend;
3886 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01003887 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003888 sprintf((char *)epat, "</%.*s>\\c", len, p);
3889
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003890 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
Bram Moolenaar76929292008-01-06 19:07:36 +00003891 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003892
3893 vim_free(spat);
3894 vim_free(epat);
3895
3896 if (r < 1 || lt(curwin->w_cursor, old_end))
3897 {
3898 /* Can't find other end or it's before the previous end. Could be a
3899 * HTML tag that doesn't have a matching end. Search backwards for
3900 * another starting tag. */
3901 count = 1;
3902 curwin->w_cursor = start_pos;
3903 goto again;
3904 }
3905
3906 if (do_include || r < 1)
3907 {
3908 /* Include up to the '>'. */
3909 while (*ml_get_cursor() != '>')
3910 if (inc_cursor() < 0)
3911 break;
3912 }
3913 else
3914 {
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003915 char_u *c = ml_get_cursor();
3916
3917 /* Exclude the '<' of the end tag.
3918 * If the closing tag is on new line, do not decrement cursor, but
3919 * make operation exclusive, so that the linefeed will be selected */
3920 if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0)
3921 /* do not decrement cursor */
3922 is_inclusive = FALSE;
3923 else if (*c == '<')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003924 dec_cursor();
3925 }
3926 end_pos = curwin->w_cursor;
3927
3928 if (!do_include)
3929 {
3930 /* Exclude the start tag. */
3931 curwin->w_cursor = start_pos;
3932 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003933 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003934 {
3935 inc_cursor();
3936 start_pos = curwin->w_cursor;
3937 break;
3938 }
3939 curwin->w_cursor = end_pos;
3940
Bram Moolenaar45360022005-07-21 21:08:21 +00003941 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003942 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003943 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003944 {
3945 do_include = TRUE;
3946 curwin->w_cursor = old_start;
3947 count = count_arg;
3948 goto again;
3949 }
3950 }
3951
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003952 if (VIsual_active)
3953 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003954 /* If the end is before the start there is no text between tags, select
3955 * the char under the cursor. */
3956 if (lt(end_pos, start_pos))
3957 curwin->w_cursor = start_pos;
3958 else if (*p_sel == 'e')
Bram Moolenaar8340dd92014-12-13 20:11:33 +01003959 inc_cursor();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003960 VIsual = start_pos;
3961 VIsual_mode = 'v';
3962 redraw_curbuf_later(INVERTED); /* update the inversion */
3963 showmode();
3964 }
3965 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003966 {
3967 oap->start = start_pos;
3968 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003969 if (lt(end_pos, start_pos))
3970 {
3971 /* End is before the start: there is no text between tags; operate
3972 * on an empty area. */
3973 curwin->w_cursor = start_pos;
3974 oap->inclusive = FALSE;
3975 }
3976 else
Bram Moolenaarb6c27352015-03-05 19:57:49 +01003977 oap->inclusive = is_inclusive;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003978 }
3979 retval = OK;
3980
3981theend:
3982 p_ws = save_p_ws;
3983 return retval;
3984}
3985
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 int
3987current_par(oap, count, include, type)
3988 oparg_T *oap;
3989 long count;
3990 int include; /* TRUE == include white space */
3991 int type; /* 'p' for paragraph, 'S' for section */
3992{
3993 linenr_T start_lnum;
3994 linenr_T end_lnum;
3995 int white_in_front;
3996 int dir;
3997 int start_is_white;
3998 int prev_start_is_white;
3999 int retval = OK;
4000 int do_white = FALSE;
4001 int t;
4002 int i;
4003
4004 if (type == 'S') /* not implemented yet */
4005 return FAIL;
4006
4007 start_lnum = curwin->w_cursor.lnum;
4008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 /*
4010 * When visual area is more than one line: extend it.
4011 */
4012 if (VIsual_active && start_lnum != VIsual.lnum)
4013 {
4014extend:
4015 if (start_lnum < VIsual.lnum)
4016 dir = BACKWARD;
4017 else
4018 dir = FORWARD;
4019 for (i = count; --i >= 0; )
4020 {
4021 if (start_lnum ==
4022 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4023 {
4024 retval = FAIL;
4025 break;
4026 }
4027
4028 prev_start_is_white = -1;
4029 for (t = 0; t < 2; ++t)
4030 {
4031 start_lnum += dir;
4032 start_is_white = linewhite(start_lnum);
4033 if (prev_start_is_white == start_is_white)
4034 {
4035 start_lnum -= dir;
4036 break;
4037 }
4038 for (;;)
4039 {
4040 if (start_lnum == (dir == BACKWARD
4041 ? 1 : curbuf->b_ml.ml_line_count))
4042 break;
4043 if (start_is_white != linewhite(start_lnum + dir)
4044 || (!start_is_white
4045 && startPS(start_lnum + (dir > 0
4046 ? 1 : 0), 0, 0)))
4047 break;
4048 start_lnum += dir;
4049 }
4050 if (!include)
4051 break;
4052 if (start_lnum == (dir == BACKWARD
4053 ? 1 : curbuf->b_ml.ml_line_count))
4054 break;
4055 prev_start_is_white = start_is_white;
4056 }
4057 }
4058 curwin->w_cursor.lnum = start_lnum;
4059 curwin->w_cursor.col = 0;
4060 return retval;
4061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062
4063 /*
4064 * First move back to the start_lnum of the paragraph or white lines
4065 */
4066 white_in_front = linewhite(start_lnum);
4067 while (start_lnum > 1)
4068 {
4069 if (white_in_front) /* stop at first white line */
4070 {
4071 if (!linewhite(start_lnum - 1))
4072 break;
4073 }
4074 else /* stop at first non-white line of start of paragraph */
4075 {
4076 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4077 break;
4078 }
4079 --start_lnum;
4080 }
4081
4082 /*
4083 * Move past the end of any white lines.
4084 */
4085 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004086 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4087 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 --end_lnum;
4090 i = count;
4091 if (!include && white_in_front)
4092 --i;
4093 while (i--)
4094 {
4095 if (end_lnum == curbuf->b_ml.ml_line_count)
4096 return FAIL;
4097
4098 if (!include)
4099 do_white = linewhite(end_lnum + 1);
4100
4101 if (include || !do_white)
4102 {
4103 ++end_lnum;
4104 /*
4105 * skip to end of paragraph
4106 */
4107 while (end_lnum < curbuf->b_ml.ml_line_count
4108 && !linewhite(end_lnum + 1)
4109 && !startPS(end_lnum + 1, 0, 0))
4110 ++end_lnum;
4111 }
4112
4113 if (i == 0 && white_in_front && include)
4114 break;
4115
4116 /*
4117 * skip to end of white lines after paragraph
4118 */
4119 if (include || do_white)
4120 while (end_lnum < curbuf->b_ml.ml_line_count
4121 && linewhite(end_lnum + 1))
4122 ++end_lnum;
4123 }
4124
4125 /*
4126 * If there are no empty lines at the end, try to find some empty lines at
4127 * the start (unless that has been done already).
4128 */
4129 if (!white_in_front && !linewhite(end_lnum) && include)
4130 while (start_lnum > 1 && linewhite(start_lnum - 1))
4131 --start_lnum;
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (VIsual_active)
4134 {
4135 /* Problem: when doing "Vipipip" nothing happens in a single white
4136 * line, we get stuck there. Trap this here. */
4137 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4138 goto extend;
4139 VIsual.lnum = start_lnum;
4140 VIsual_mode = 'V';
4141 redraw_curbuf_later(INVERTED); /* update the inversion */
4142 showmode();
4143 }
4144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 oap->start.lnum = start_lnum;
4147 oap->start.col = 0;
4148 oap->motion_type = MLINE;
4149 }
4150 curwin->w_cursor.lnum = end_lnum;
4151 curwin->w_cursor.col = 0;
4152
4153 return OK;
4154}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004155
4156static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4157static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4158
4159/*
4160 * Search quote char from string line[col].
4161 * Quote character escaped by one of the characters in "escape" is not counted
4162 * as a quote.
4163 * Returns column number of "quotechar" or -1 when not found.
4164 */
4165 static int
4166find_next_quote(line, col, quotechar, escape)
4167 char_u *line;
4168 int col;
4169 int quotechar;
4170 char_u *escape; /* escape characters, can be NULL */
4171{
4172 int c;
4173
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004174 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004175 {
4176 c = line[col];
4177 if (c == NUL)
4178 return -1;
4179 else if (escape != NULL && vim_strchr(escape, c))
4180 ++col;
4181 else if (c == quotechar)
4182 break;
4183#ifdef FEAT_MBYTE
4184 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004185 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004186 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004188 ++col;
4189 }
4190 return col;
4191}
4192
4193/*
4194 * Search backwards in "line" from column "col_start" to find "quotechar".
4195 * Quote character escaped by one of the characters in "escape" is not counted
4196 * as a quote.
4197 * Return the found column or zero.
4198 */
4199 static int
4200find_prev_quote(line, col_start, quotechar, escape)
4201 char_u *line;
4202 int col_start;
4203 int quotechar;
4204 char_u *escape; /* escape characters, can be NULL */
4205{
4206 int n;
4207
4208 while (col_start > 0)
4209 {
4210 --col_start;
4211#ifdef FEAT_MBYTE
4212 col_start -= (*mb_head_off)(line, line + col_start);
4213#endif
4214 n = 0;
4215 if (escape != NULL)
4216 while (col_start - n > 0 && vim_strchr(escape,
4217 line[col_start - n - 1]) != NULL)
4218 ++n;
4219 if (n & 1)
4220 col_start -= n; /* uneven number of escape chars, skip it */
4221 else if (line[col_start] == quotechar)
4222 break;
4223 }
4224 return col_start;
4225}
4226
4227/*
4228 * Find quote under the cursor, cursor at end.
4229 * Returns TRUE if found, else FALSE.
4230 */
4231 int
4232current_quote(oap, count, include, quotechar)
4233 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004234 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004235 int include; /* TRUE == include quote char */
4236 int quotechar; /* Quote character */
4237{
4238 char_u *line = ml_get_curline();
4239 int col_end;
4240 int col_start = curwin->w_cursor.col;
4241 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004242 int vis_empty = TRUE; /* Visual selection <= 1 char */
4243 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004244 int inside_quotes = FALSE; /* Looks like "i'" done before */
4245 int selected_quote = FALSE; /* Has quote inside selection */
4246 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004247
4248 /* Correct cursor when 'selection' is exclusive */
4249 if (VIsual_active)
4250 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004251 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004252 if (*p_sel == 'e' && vis_bef_curs)
4253 dec_cursor();
4254 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004255 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004256
4257 if (!vis_empty)
4258 {
4259 /* Check if the existing selection exactly spans the text inside
4260 * quotes. */
4261 if (vis_bef_curs)
4262 {
4263 inside_quotes = VIsual.col > 0
4264 && line[VIsual.col - 1] == quotechar
4265 && line[curwin->w_cursor.col] != NUL
4266 && line[curwin->w_cursor.col + 1] == quotechar;
4267 i = VIsual.col;
4268 col_end = curwin->w_cursor.col;
4269 }
4270 else
4271 {
4272 inside_quotes = curwin->w_cursor.col > 0
4273 && line[curwin->w_cursor.col - 1] == quotechar
4274 && line[VIsual.col] != NUL
4275 && line[VIsual.col + 1] == quotechar;
4276 i = curwin->w_cursor.col;
4277 col_end = VIsual.col;
4278 }
4279
4280 /* Find out if we have a quote in the selection. */
4281 while (i <= col_end)
4282 if (line[i++] == quotechar)
4283 {
4284 selected_quote = TRUE;
4285 break;
4286 }
4287 }
4288
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004289 if (!vis_empty && line[col_start] == quotechar)
4290 {
4291 /* Already selecting something and on a quote character. Find the
4292 * next quoted string. */
4293 if (vis_bef_curs)
4294 {
4295 /* Assume we are on a closing quote: move to after the next
4296 * opening quote. */
4297 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4298 if (col_start < 0)
4299 return FALSE;
4300 col_end = find_next_quote(line, col_start + 1, quotechar,
4301 curbuf->b_p_qe);
4302 if (col_end < 0)
4303 {
4304 /* We were on a starting quote perhaps? */
4305 col_end = col_start;
4306 col_start = curwin->w_cursor.col;
4307 }
4308 }
4309 else
4310 {
4311 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4312 if (line[col_end] != quotechar)
4313 return FALSE;
4314 col_start = find_prev_quote(line, col_end, quotechar,
4315 curbuf->b_p_qe);
4316 if (line[col_start] != quotechar)
4317 {
4318 /* We were on an ending quote perhaps? */
4319 col_start = col_end;
4320 col_end = curwin->w_cursor.col;
4321 }
4322 }
4323 }
4324 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004325
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004326 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004327 {
4328 int first_col = col_start;
4329
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004330 if (!vis_empty)
4331 {
4332 if (vis_bef_curs)
4333 first_col = find_next_quote(line, col_start, quotechar, NULL);
4334 else
4335 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4336 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004337
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004338 /* The cursor is on a quote, we don't know if it's the opening or
4339 * closing quote. Search from the start of the line to find out.
4340 * Also do this when there is a Visual area, a' may leave the cursor
4341 * in between two strings. */
4342 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004343 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004344 {
4345 /* Find open quote character. */
4346 col_start = find_next_quote(line, col_start, quotechar, NULL);
4347 if (col_start < 0 || col_start > first_col)
4348 return FALSE;
4349 /* Find close quote character. */
4350 col_end = find_next_quote(line, col_start + 1, quotechar,
4351 curbuf->b_p_qe);
4352 if (col_end < 0)
4353 return FALSE;
4354 /* If is cursor between start and end quote character, it is
4355 * target text object. */
4356 if (col_start <= first_col && first_col <= col_end)
4357 break;
4358 col_start = col_end + 1;
4359 }
4360 }
4361 else
4362 {
4363 /* Search backward for a starting quote. */
4364 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4365 if (line[col_start] != quotechar)
4366 {
4367 /* No quote before the cursor, look after the cursor. */
4368 col_start = find_next_quote(line, col_start, quotechar, NULL);
4369 if (col_start < 0)
4370 return FALSE;
4371 }
4372
4373 /* Find close quote character. */
4374 col_end = find_next_quote(line, col_start + 1, quotechar,
4375 curbuf->b_p_qe);
4376 if (col_end < 0)
4377 return FALSE;
4378 }
4379
4380 /* When "include" is TRUE, include spaces after closing quote or before
4381 * the starting quote. */
4382 if (include)
4383 {
4384 if (vim_iswhite(line[col_end + 1]))
4385 while (vim_iswhite(line[col_end + 1]))
4386 ++col_end;
4387 else
4388 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4389 --col_start;
4390 }
4391
Bram Moolenaarab194812005-09-14 21:40:12 +00004392 /* Set start position. After vi" another i" must include the ".
4393 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004394 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004395 ++col_start;
4396 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004397 if (VIsual_active)
4398 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004399 /* Set the start of the Visual area when the Visual area was empty, we
4400 * were just inside quotes or the Visual area didn't start at a quote
4401 * and didn't include a quote.
4402 */
4403 if (vis_empty
4404 || (vis_bef_curs
4405 && !selected_quote
4406 && (inside_quotes
4407 || (line[VIsual.col] != quotechar
4408 && (VIsual.col == 0
4409 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004410 {
4411 VIsual = curwin->w_cursor;
4412 redraw_curbuf_later(INVERTED);
4413 }
4414 }
4415 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004416 {
4417 oap->start = curwin->w_cursor;
4418 oap->motion_type = MCHAR;
4419 }
4420
4421 /* Set end position. */
4422 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004423 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004424 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004425 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004426 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004427 if (VIsual_active)
4428 {
4429 if (vis_empty || vis_bef_curs)
4430 {
4431 /* decrement cursor when 'selection' is not exclusive */
4432 if (*p_sel != 'e')
4433 dec_cursor();
4434 }
4435 else
4436 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004437 /* Cursor is at start of Visual area. Set the end of the Visual
4438 * area when it was just inside quotes or it didn't end at a
4439 * quote. */
4440 if (inside_quotes
4441 || (!selected_quote
4442 && line[VIsual.col] != quotechar
4443 && (line[VIsual.col] == NUL
4444 || line[VIsual.col + 1] != quotechar)))
4445 {
4446 dec_cursor();
4447 VIsual = curwin->w_cursor;
4448 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004449 curwin->w_cursor.col = col_start;
4450 }
4451 if (VIsual_mode == 'V')
4452 {
4453 VIsual_mode = 'v';
4454 redraw_cmdline = TRUE; /* show mode later */
4455 }
4456 }
4457 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004458 {
4459 /* Set inclusive and other oap's flags. */
4460 oap->inclusive = inclusive;
4461 }
4462
4463 return OK;
4464}
4465
4466#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004468static int is_one_char __ARGS((char_u *pattern, int move));
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004469
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004470/*
4471 * Find next search match under cursor, cursor at end.
4472 * Used while an operator is pending, and in Visual mode.
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004473 */
4474 int
4475current_search(count, forward)
4476 long count;
4477 int forward; /* move forward or backwards */
4478{
4479 pos_T start_pos; /* position before the pattern */
4480 pos_T orig_pos; /* position of the cursor at beginning */
4481 pos_T pos; /* position after the pattern */
4482 int i;
4483 int dir;
4484 int result; /* result of various function calls */
4485 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004486 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004487 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004488 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004489
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004490 /* wrapping should not occur */
4491 p_ws = FALSE;
4492
4493 /* Correct cursor when 'selection' is exclusive */
4494 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
4495 dec_cursor();
4496
4497 if (VIsual_active)
4498 {
4499 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004500
4501 pos = curwin->w_cursor;
4502 start_pos = VIsual;
4503
4504 /* make sure, searching further will extend the match */
4505 if (VIsual_active)
4506 {
4507 if (forward)
4508 incl(&pos);
4509 else
4510 decl(&pos);
4511 }
4512 }
4513 else
4514 orig_pos = pos = start_pos = curwin->w_cursor;
4515
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004516 /* Is the pattern is zero-width? */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004517 one_char = is_one_char(spats[last_idx].pat, TRUE);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004518 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004519 {
4520 p_ws = old_p_ws;
4521 return FAIL; /* pattern not found */
4522 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004523
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004524 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004525 * The trick is to first search backwards and then search forward again,
4526 * so that a match at the current cursor position will be correctly
4527 * captured.
4528 */
4529 for (i = 0; i < 2; i++)
4530 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004531 if (forward)
4532 dir = i;
4533 else
4534 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004535
4536 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004537 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004538 flags = SEARCH_END;
4539
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004540 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4541 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004542 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004543
4544 /* First search may fail, but then start searching from the
4545 * beginning of the file (cursor might be on the search match)
4546 * except when Visual mode is active, so that extending the visual
4547 * selection works. */
4548 if (!result && i) /* not found, abort */
4549 {
4550 curwin->w_cursor = orig_pos;
4551 if (VIsual_active)
4552 VIsual = save_VIsual;
4553 p_ws = old_p_ws;
4554 return FAIL;
4555 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004556 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004557 {
4558 if (forward) /* try again from start of buffer */
4559 {
4560 clearpos(&pos);
4561 }
4562 else /* try again from end of buffer */
4563 {
4564 /* searching backwards, so set pos to last line and col */
4565 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004566 pos.col = (colnr_T)STRLEN(
4567 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004568 }
4569 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004570 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004571 }
4572
4573 start_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004574 flags = forward ? SEARCH_END : 0;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004575
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004576 /* Check again from the current cursor position,
4577 * since the next match might actually by only one char wide */
4578 one_char = is_one_char(spats[last_idx].pat, FALSE);
4579
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004580 /* move to match, except for zero-width matches, in which case, we are
4581 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004582 if (!one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004583 result = searchit(curwin, curbuf, &pos, (forward ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004584 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0, NULL);
4585
4586 if (!VIsual_active)
4587 VIsual = start_pos;
4588
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004589 curwin->w_cursor = pos;
4590 VIsual_active = TRUE;
4591 VIsual_mode = 'v';
4592
4593 if (VIsual_active)
4594 {
4595 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004596 if (*p_sel == 'e')
4597 {
4598 /* Correction for exclusive selection depends on the direction. */
4599 if (forward && ltoreq(VIsual, curwin->w_cursor))
4600 inc_cursor();
4601 else if (!forward && ltoreq(curwin->w_cursor, VIsual))
4602 inc(&VIsual);
4603 }
4604
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004605 }
4606
4607#ifdef FEAT_FOLDING
4608 if (fdo_flags & FDO_SEARCH && KeyTyped)
4609 foldOpenCursor();
4610#endif
4611
4612 may_start_select('c');
4613#ifdef FEAT_MOUSE
4614 setmouse();
4615#endif
4616#ifdef FEAT_CLIPBOARD
4617 /* Make sure the clipboard gets updated. Needed because start and
4618 * end are still the same, and the selection needs to be owned */
4619 clip_star.vmode = NUL;
4620#endif
4621 redraw_curbuf_later(INVERTED);
4622 showmode();
4623
4624 return OK;
4625}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004626
4627/*
Bram Moolenaare78495d2013-06-30 14:46:53 +02004628 * Check if the pattern is one character or zero-width.
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004629 * If move is TRUE, check from the beginning of the buffer, else from the
4630 * current cursor position.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004631 * Returns TRUE, FALSE or -1 for failure.
4632 */
4633 static int
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004634is_one_char(pattern, move)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004635 char_u *pattern;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004636 int move;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004637{
4638 regmmatch_T regmatch;
4639 int nmatched = 0;
4640 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004641 pos_T pos;
4642 int save_called_emsg = called_emsg;
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004643 int flag = 0;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004644
4645 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4646 SEARCH_KEEP, &regmatch) == FAIL)
4647 return -1;
4648
4649 /* move to match */
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004650 if (move)
4651 clearpos(&pos)
4652 else
4653 {
4654 pos = curwin->w_cursor;
4655 /* accept a match at the cursor position */
4656 flag = SEARCH_START;
4657 }
4658
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004659 if (searchit(curwin, curbuf, &pos, FORWARD, spats[last_idx].pat, 1,
Bram Moolenaarb12db9f2014-12-13 22:00:22 +01004660 SEARCH_KEEP + flag, RE_SEARCH, 0, NULL) != FAIL)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004661 {
4662 /* Zero-width pattern should match somewhere, then we can check if
4663 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004664 called_emsg = FALSE;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004665 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
4666 pos.lnum, (colnr_T)0, NULL);
4667
4668 if (!called_emsg)
4669 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004670 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4671 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004672
Bram Moolenaar4c7cb6b2013-10-02 21:55:02 +02004673 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4674 result = TRUE;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004675 }
4676
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004677 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004678 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004679 return result;
4680}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004681
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4683 || defined(PROTO)
4684/*
4685 * return TRUE if line 'lnum' is empty or has white chars only.
4686 */
4687 int
4688linewhite(lnum)
4689 linenr_T lnum;
4690{
4691 char_u *p;
4692
4693 p = skipwhite(ml_get(lnum));
4694 return (*p == NUL);
4695}
4696#endif
4697
4698#if defined(FEAT_FIND_ID) || defined(PROTO)
4699/*
4700 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004701 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 void
4704find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4705 type, count, action, start_lnum, end_lnum)
4706 char_u *ptr; /* pointer to search pattern */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004707 int dir UNUSED; /* direction of expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 int len; /* length of search pattern */
4709 int whole; /* match whole words only */
4710 int skip_comments; /* don't match inside comments */
4711 int type; /* Type of search; are we looking for a type?
4712 a macro? */
4713 long count;
4714 int action; /* What to do when we find it */
4715 linenr_T start_lnum; /* first line to start searching */
4716 linenr_T end_lnum; /* last line for searching */
4717{
4718 SearchedFile *files; /* Stack of included files */
4719 SearchedFile *bigger; /* When we need more space */
4720 int max_path_depth = 50;
4721 long match_count = 1;
4722
4723 char_u *pat;
4724 char_u *new_fname;
4725 char_u *curr_fname = curbuf->b_fname;
4726 char_u *prev_fname = NULL;
4727 linenr_T lnum;
4728 int depth;
4729 int depth_displayed; /* For type==CHECK_PATH */
4730 int old_files;
4731 int already_searched;
4732 char_u *file_line;
4733 char_u *line;
4734 char_u *p;
4735 char_u save_char;
4736 int define_matched;
4737 regmatch_T regmatch;
4738 regmatch_T incl_regmatch;
4739 regmatch_T def_regmatch;
4740 int matched = FALSE;
4741 int did_show = FALSE;
4742 int found = FALSE;
4743 int i;
4744 char_u *already = NULL;
4745 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004746 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4748 win_T *curwin_save = NULL;
4749#endif
4750
4751 regmatch.regprog = NULL;
4752 incl_regmatch.regprog = NULL;
4753 def_regmatch.regprog = NULL;
4754
4755 file_line = alloc(LSIZE);
4756 if (file_line == NULL)
4757 return;
4758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (type != CHECK_PATH && type != FIND_DEFINE
4760#ifdef FEAT_INS_EXPAND
4761 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4762 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004763 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764#endif
4765 )
4766 {
4767 pat = alloc(len + 5);
4768 if (pat == NULL)
4769 goto fpip_end;
4770 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4771 /* ignore case according to p_ic, p_scs and pat */
4772 regmatch.rm_ic = ignorecase(pat);
4773 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4774 vim_free(pat);
4775 if (regmatch.regprog == NULL)
4776 goto fpip_end;
4777 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004778 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4779 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004781 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (incl_regmatch.regprog == NULL)
4783 goto fpip_end;
4784 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4785 }
4786 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4787 {
4788 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4789 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4790 if (def_regmatch.regprog == NULL)
4791 goto fpip_end;
4792 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4793 }
4794 files = (SearchedFile *)lalloc_clear((long_u)
4795 (max_path_depth * sizeof(SearchedFile)), TRUE);
4796 if (files == NULL)
4797 goto fpip_end;
4798 old_files = max_path_depth;
4799 depth = depth_displayed = -1;
4800
4801 lnum = start_lnum;
4802 if (end_lnum > curbuf->b_ml.ml_line_count)
4803 end_lnum = curbuf->b_ml.ml_line_count;
4804 if (lnum > end_lnum) /* do at least one line */
4805 lnum = end_lnum;
4806 line = ml_get(lnum);
4807
4808 for (;;)
4809 {
4810 if (incl_regmatch.regprog != NULL
4811 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4812 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004813 char_u *p_fname = (curr_fname == curbuf->b_fname)
4814 ? curbuf->b_ffname : curr_fname;
4815
4816 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4817 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4818 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004819 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004820 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4821 else
4822 /* Use text after match with 'include'. */
4823 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004824 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 already_searched = FALSE;
4826 if (new_fname != NULL)
4827 {
4828 /* Check whether we have already searched in this file */
4829 for (i = 0;; i++)
4830 {
4831 if (i == depth + 1)
4832 i = old_files;
4833 if (i == max_path_depth)
4834 break;
4835 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4836 {
4837 if (type != CHECK_PATH &&
4838 action == ACTION_SHOW_ALL && files[i].matched)
4839 {
4840 msg_putchar('\n'); /* cursor below last one */
4841 if (!got_int) /* don't display if 'q'
4842 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00004843 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 msg_home_replace_hl(new_fname);
4846 MSG_PUTS(_(" (includes previously listed match)"));
4847 prev_fname = NULL;
4848 }
4849 }
4850 vim_free(new_fname);
4851 new_fname = NULL;
4852 already_searched = TRUE;
4853 break;
4854 }
4855 }
4856 }
4857
4858 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4859 || (new_fname == NULL && !already_searched)))
4860 {
4861 if (did_show)
4862 msg_putchar('\n'); /* cursor below last one */
4863 else
4864 {
4865 gotocmdline(TRUE); /* cursor at status line */
4866 MSG_PUTS_TITLE(_("--- Included files "));
4867 if (action != ACTION_SHOW_ALL)
4868 MSG_PUTS_TITLE(_("not found "));
4869 MSG_PUTS_TITLE(_("in path ---\n"));
4870 }
4871 did_show = TRUE;
4872 while (depth_displayed < depth && !got_int)
4873 {
4874 ++depth_displayed;
4875 for (i = 0; i < depth_displayed; i++)
4876 MSG_PUTS(" ");
4877 msg_home_replace(files[depth_displayed].name);
4878 MSG_PUTS(" -->\n");
4879 }
4880 if (!got_int) /* don't display if 'q' typed
4881 for "--more--" message */
4882 {
4883 for (i = 0; i <= depth_displayed; i++)
4884 MSG_PUTS(" ");
4885 if (new_fname != NULL)
4886 {
4887 /* using "new_fname" is more reliable, e.g., when
4888 * 'includeexpr' is set. */
4889 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4890 }
4891 else
4892 {
4893 /*
4894 * Isolate the file name.
4895 * Include the surrounding "" or <> if present.
4896 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02004897 if (inc_opt != NULL
4898 && strstr((char *)inc_opt, "\\zs") != NULL)
4899 {
4900 /* pattern contains \zs, use the match */
4901 p = incl_regmatch.startp[0];
4902 i = (int)(incl_regmatch.endp[0]
4903 - incl_regmatch.startp[0]);
4904 }
4905 else
4906 {
4907 /* find the file name after the end of the match */
4908 for (p = incl_regmatch.endp[0];
4909 *p && !vim_isfilec(*p); p++)
4910 ;
4911 for (i = 0; vim_isfilec(p[i]); i++)
4912 ;
4913 }
4914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 if (i == 0)
4916 {
4917 /* Nothing found, use the rest of the line. */
4918 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004919 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02004921 /* Avoid checking before the start of the line, can
4922 * happen if \zs appears in the regexp. */
4923 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
4925 if (p[-1] == '"' || p[-1] == '<')
4926 {
4927 --p;
4928 ++i;
4929 }
4930 if (p[i] == '"' || p[i] == '>')
4931 ++i;
4932 }
4933 save_char = p[i];
4934 p[i] = NUL;
4935 msg_outtrans_attr(p, hl_attr(HLF_D));
4936 p[i] = save_char;
4937 }
4938
4939 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4940 {
4941 if (already_searched)
4942 MSG_PUTS(_(" (Already listed)"));
4943 else
4944 MSG_PUTS(_(" NOT FOUND"));
4945 }
4946 }
4947 out_flush(); /* output each line directly */
4948 }
4949
4950 if (new_fname != NULL)
4951 {
4952 /* Push the new file onto the file stack */
4953 if (depth + 1 == old_files)
4954 {
4955 bigger = (SearchedFile *)lalloc((long_u)(
4956 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4957 if (bigger != NULL)
4958 {
4959 for (i = 0; i <= depth; i++)
4960 bigger[i] = files[i];
4961 for (i = depth + 1; i < old_files + max_path_depth; i++)
4962 {
4963 bigger[i].fp = NULL;
4964 bigger[i].name = NULL;
4965 bigger[i].lnum = 0;
4966 bigger[i].matched = FALSE;
4967 }
4968 for (i = old_files; i < max_path_depth; i++)
4969 bigger[i + max_path_depth] = files[i];
4970 old_files += max_path_depth;
4971 max_path_depth *= 2;
4972 vim_free(files);
4973 files = bigger;
4974 }
4975 }
4976 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4977 == NULL)
4978 vim_free(new_fname);
4979 else
4980 {
4981 if (++depth == old_files)
4982 {
4983 /*
4984 * lalloc() for 'bigger' must have failed above. We
4985 * will forget one of our already visited files now.
4986 */
4987 vim_free(files[old_files].name);
4988 ++old_files;
4989 }
4990 files[depth].name = curr_fname = new_fname;
4991 files[depth].lnum = 0;
4992 files[depth].matched = FALSE;
4993#ifdef FEAT_INS_EXPAND
4994 if (action == ACTION_EXPAND)
4995 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004996 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00004997 vim_snprintf((char*)IObuff, IOSIZE,
4998 _("Scanning included file: %s"),
4999 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
5001 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00005004 if (p_verbose >= 5)
5005 {
5006 verbose_enter();
5007 smsg((char_u *)_("Searching included file %s"),
5008 (char *)new_fname);
5009 verbose_leave();
5010 }
5011
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 }
5014 }
5015 else
5016 {
5017 /*
5018 * Check if the line is a define (type == FIND_DEFINE)
5019 */
5020 p = line;
5021search_line:
5022 define_matched = FALSE;
5023 if (def_regmatch.regprog != NULL
5024 && vim_regexec(&def_regmatch, line, (colnr_T)0))
5025 {
5026 /*
5027 * Pattern must be first identifier after 'define', so skip
5028 * to that position before checking for match of pattern. Also
5029 * don't let it match beyond the end of this identifier.
5030 */
5031 p = def_regmatch.endp[0];
5032 while (*p && !vim_iswordc(*p))
5033 p++;
5034 define_matched = TRUE;
5035 }
5036
5037 /*
5038 * Look for a match. Don't do this if we are looking for a
5039 * define and this line didn't match define_prog above.
5040 */
5041 if (def_regmatch.regprog == NULL || define_matched)
5042 {
5043 if (define_matched
5044#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005045 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#endif
5047 )
5048 {
5049 /* compare the first "len" chars from "ptr" */
5050 startp = skipwhite(p);
5051 if (p_ic)
5052 matched = !MB_STRNICMP(startp, ptr, len);
5053 else
5054 matched = !STRNCMP(startp, ptr, len);
5055 if (matched && define_matched && whole
5056 && vim_iswordc(startp[len]))
5057 matched = FALSE;
5058 }
5059 else if (regmatch.regprog != NULL
5060 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5061 {
5062 matched = TRUE;
5063 startp = regmatch.startp[0];
5064 /*
5065 * Check if the line is not a comment line (unless we are
5066 * looking for a define). A line starting with "# define"
5067 * is not considered to be a comment line.
5068 */
5069 if (!define_matched && skip_comments)
5070 {
5071#ifdef FEAT_COMMENTS
5072 if ((*line != '#' ||
5073 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005074 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 matched = FALSE;
5076
5077 /*
5078 * Also check for a "/ *" or "/ /" before the match.
5079 * Skips lines like "int backwards; / * normal index
5080 * * /" when looking for "normal".
5081 * Note: Doesn't skip "/ *" in comments.
5082 */
5083 p = skipwhite(line);
5084 if (matched
5085 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5086#endif
5087 for (p = line; *p && p < startp; ++p)
5088 {
5089 if (matched
5090 && p[0] == '/'
5091 && (p[1] == '*' || p[1] == '/'))
5092 {
5093 matched = FALSE;
5094 /* After "//" all text is comment */
5095 if (p[1] == '/')
5096 break;
5097 ++p;
5098 }
5099 else if (!matched && p[0] == '*' && p[1] == '/')
5100 {
5101 /* Can find match after "* /". */
5102 matched = TRUE;
5103 ++p;
5104 }
5105 }
5106 }
5107 }
5108 }
5109 }
5110 if (matched)
5111 {
5112#ifdef FEAT_INS_EXPAND
5113 if (action == ACTION_EXPAND)
5114 {
5115 int reuse = 0;
5116 int add_r;
5117 char_u *aux;
5118
5119 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5120 break;
5121 found = TRUE;
5122 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (vim_iswordp(p))
5127 goto exit_matched;
5128 p = find_word_start(p);
5129 }
5130 p = find_word_end(p);
5131 i = (int)(p - aux);
5132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005135 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005137
5138 /* Get the next line: when "depth" < 0 from the current
5139 * buffer, otherwise from the included file. Jump to
5140 * exit_matched when past the last line. */
5141 if (depth < 0)
5142 {
5143 if (lnum >= end_lnum)
5144 goto exit_matched;
5145 line = ml_get(++lnum);
5146 }
5147 else if (vim_fgets(line = file_line,
5148 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 goto exit_matched;
5150
5151 /* we read a line, set "already" to check this "line" later
5152 * if depth >= 0 we'll increase files[depth].lnum far
5153 * bellow -- Acevedo */
5154 already = aux = p = skipwhite(line);
5155 p = find_word_start(p);
5156 p = find_word_end(p);
5157 if (p > aux)
5158 {
5159 if (*aux != ')' && IObuff[i-1] != TAB)
5160 {
5161 if (IObuff[i-1] != ' ')
5162 IObuff[i++] = ' ';
5163 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5164 if (p_js
5165 && (IObuff[i-2] == '.'
5166 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5167 && (IObuff[i-2] == '?'
5168 || IObuff[i-2] == '!'))))
5169 IObuff[i++] = ' ';
5170 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005171 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 if (p - aux >= IOSIZE - i)
5173 p = aux + IOSIZE - i - 1;
5174 STRNCPY(IObuff + i, aux, p - aux);
5175 i += (int)(p - aux);
5176 reuse |= CONT_S_IPOS;
5177 }
5178 IObuff[i] = NUL;
5179 aux = IObuff;
5180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 goto exit_matched;
5183 }
5184
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005185 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5187 dir, reuse);
5188 if (add_r == OK)
5189 /* if dir was BACKWARD then honor it just once */
5190 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005191 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 break;
5193 }
5194 else
5195#endif
5196 if (action == ACTION_SHOW_ALL)
5197 {
5198 found = TRUE;
5199 if (!did_show)
5200 gotocmdline(TRUE); /* cursor at status line */
5201 if (curr_fname != prev_fname)
5202 {
5203 if (did_show)
5204 msg_putchar('\n'); /* cursor below last one */
5205 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005206 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 msg_home_replace_hl(curr_fname);
5208 prev_fname = curr_fname;
5209 }
5210 did_show = TRUE;
5211 if (!got_int)
5212 show_pat_in_path(line, type, TRUE, action,
5213 (depth == -1) ? NULL : files[depth].fp,
5214 (depth == -1) ? &lnum : &files[depth].lnum,
5215 match_count++);
5216
5217 /* Set matched flag for this file and all the ones that
5218 * include it */
5219 for (i = 0; i <= depth; ++i)
5220 files[i].matched = TRUE;
5221 }
5222 else if (--count <= 0)
5223 {
5224 found = TRUE;
5225 if (depth == -1 && lnum == curwin->w_cursor.lnum
5226#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5227 && g_do_tagpreview == 0
5228#endif
5229 )
5230 EMSG(_("E387: Match is on current line"));
5231 else if (action == ACTION_SHOW)
5232 {
5233 show_pat_in_path(line, type, did_show, action,
5234 (depth == -1) ? NULL : files[depth].fp,
5235 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5236 did_show = TRUE;
5237 }
5238 else
5239 {
5240#ifdef FEAT_GUI
5241 need_mouse_correct = TRUE;
5242#endif
5243#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5244 /* ":psearch" uses the preview window */
5245 if (g_do_tagpreview != 0)
5246 {
5247 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005248 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250#endif
5251 if (action == ACTION_SPLIT)
5252 {
5253#ifdef FEAT_WINDOWS
5254 if (win_split(0, 0) == FAIL)
5255#endif
5256 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005257 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259 if (depth == -1)
5260 {
5261 /* match in current file */
5262#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5263 if (g_do_tagpreview != 0)
5264 {
5265 if (getfile(0, curwin_save->w_buffer->b_fname,
5266 NULL, TRUE, lnum, FALSE) > 0)
5267 break; /* failed to jump to file */
5268 }
5269 else
5270#endif
5271 setpcmark();
5272 curwin->w_cursor.lnum = lnum;
5273 }
5274 else
5275 {
5276 if (getfile(0, files[depth].name, NULL, TRUE,
5277 files[depth].lnum, FALSE) > 0)
5278 break; /* failed to jump to file */
5279 /* autocommands may have changed the lnum, we don't
5280 * want that here */
5281 curwin->w_cursor.lnum = files[depth].lnum;
5282 }
5283 }
5284 if (action != ACTION_SHOW)
5285 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005286 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 curwin->w_set_curswant = TRUE;
5288 }
5289
5290#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5291 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005292 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 /* Return cursor to where we were */
5295 validate_cursor();
5296 redraw_later(VALID);
5297 win_enter(curwin_save, TRUE);
5298 }
5299#endif
5300 break;
5301 }
5302#ifdef FEAT_INS_EXPAND
5303exit_matched:
5304#endif
5305 matched = FALSE;
5306 /* look for other matches in the rest of the line if we
5307 * are not at the end of it already */
5308 if (def_regmatch.regprog == NULL
5309#ifdef FEAT_INS_EXPAND
5310 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005311 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005313 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005314 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 goto search_line;
5316 }
5317 line_breakcheck();
5318#ifdef FEAT_INS_EXPAND
5319 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00005320 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005321 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#else
5323 if (got_int)
5324#endif
5325 break;
5326
5327 /*
5328 * Read the next line. When reading an included file and encountering
5329 * end-of-file, close the file and continue in the file that included
5330 * it.
5331 */
5332 while (depth >= 0 && !already
5333 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5334 {
5335 fclose(files[depth].fp);
5336 --old_files;
5337 files[old_files].name = files[depth].name;
5338 files[old_files].matched = files[depth].matched;
5339 --depth;
5340 curr_fname = (depth == -1) ? curbuf->b_fname
5341 : files[depth].name;
5342 if (depth < depth_displayed)
5343 depth_displayed = depth;
5344 }
5345 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005348 /* Remove any CR and LF from the line. */
5349 i = (int)STRLEN(line);
5350 if (i > 0 && line[i - 1] == '\n')
5351 line[--i] = NUL;
5352 if (i > 0 && line[i - 1] == '\r')
5353 line[--i] = NUL;
5354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 else if (!already)
5356 {
5357 if (++lnum > end_lnum)
5358 break;
5359 line = ml_get(lnum);
5360 }
5361 already = NULL;
5362 }
5363 /* End of big for (;;) loop. */
5364
5365 /* Close any files that are still open. */
5366 for (i = 0; i <= depth; i++)
5367 {
5368 fclose(files[i].fp);
5369 vim_free(files[i].name);
5370 }
5371 for (i = old_files; i < max_path_depth; i++)
5372 vim_free(files[i].name);
5373 vim_free(files);
5374
5375 if (type == CHECK_PATH)
5376 {
5377 if (!did_show)
5378 {
5379 if (action != ACTION_SHOW_ALL)
5380 MSG(_("All included files were found"));
5381 else
5382 MSG(_("No included files"));
5383 }
5384 }
5385 else if (!found
5386#ifdef FEAT_INS_EXPAND
5387 && action != ACTION_EXPAND
5388#endif
5389 )
5390 {
5391#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393#else
5394 if (got_int)
5395#endif
5396 EMSG(_(e_interr));
5397 else if (type == FIND_DEFINE)
5398 EMSG(_("E388: Couldn't find definition"));
5399 else
5400 EMSG(_("E389: Couldn't find pattern"));
5401 }
5402 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5403 msg_end();
5404
5405fpip_end:
5406 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005407 vim_regfree(regmatch.regprog);
5408 vim_regfree(incl_regmatch.regprog);
5409 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410}
5411
5412 static void
5413show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5414 char_u *line;
5415 int type;
5416 int did_show;
5417 int action;
5418 FILE *fp;
5419 linenr_T *lnum;
5420 long count;
5421{
5422 char_u *p;
5423
5424 if (did_show)
5425 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005426 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 gotocmdline(TRUE); /* cursor at status line */
5428 if (got_int) /* 'q' typed at "--more--" message */
5429 return;
5430 for (;;)
5431 {
5432 p = line + STRLEN(line) - 1;
5433 if (fp != NULL)
5434 {
5435 /* We used fgets(), so get rid of newline at end */
5436 if (p >= line && *p == '\n')
5437 --p;
5438 if (p >= line && *p == '\r')
5439 --p;
5440 *(p + 1) = NUL;
5441 }
5442 if (action == ACTION_SHOW_ALL)
5443 {
5444 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5445 msg_puts(IObuff);
5446 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5447 /* Highlight line numbers */
5448 msg_puts_attr(IObuff, hl_attr(HLF_N));
5449 MSG_PUTS(" ");
5450 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005451 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 out_flush(); /* show one line at a time */
5453
5454 /* Definition continues until line that doesn't end with '\' */
5455 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5456 break;
5457
5458 if (fp != NULL)
5459 {
5460 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5461 break;
5462 ++*lnum;
5463 }
5464 else
5465 {
5466 if (++*lnum > curbuf->b_ml.ml_line_count)
5467 break;
5468 line = ml_get(*lnum);
5469 }
5470 msg_putchar('\n');
5471 }
5472}
5473#endif
5474
5475#ifdef FEAT_VIMINFO
5476 int
5477read_viminfo_search_pattern(virp, force)
5478 vir_T *virp;
5479 int force;
5480{
5481 char_u *lp;
5482 int idx = -1;
5483 int magic = FALSE;
5484 int no_scs = FALSE;
5485 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005486 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 long off = 0;
5488 int setlast = FALSE;
5489#ifdef FEAT_SEARCH_EXTRA
5490 static int hlsearch_on = FALSE;
5491#endif
5492 char_u *val;
5493
5494 /*
5495 * Old line types:
5496 * "/pat", "&pat": search/subst. pat
5497 * "~/pat", "~&pat": last used search/subst. pat
5498 * New line types:
5499 * "~h", "~H": hlsearch highlighting off/on
5500 * "~<magic><smartcase><line><end><off><last><which>pat"
5501 * <magic>: 'm' off, 'M' on
5502 * <smartcase>: 's' off, 'S' on
5503 * <line>: 'L' line offset, 'l' char offset
5504 * <end>: 'E' from end, 'e' from start
5505 * <off>: decimal, offset
5506 * <last>: '~' last used pattern
5507 * <which>: '/' search pat, '&' subst. pat
5508 */
5509 lp = virp->vir_line;
5510 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5511 {
5512 if (lp[1] == 'M') /* magic on */
5513 magic = TRUE;
5514 if (lp[2] == 's')
5515 no_scs = TRUE;
5516 if (lp[3] == 'L')
5517 off_line = TRUE;
5518 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005519 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 lp += 5;
5521 off = getdigits(&lp);
5522 }
5523 if (lp[0] == '~') /* use this pattern for last-used pattern */
5524 {
5525 setlast = TRUE;
5526 lp++;
5527 }
5528 if (lp[0] == '/')
5529 idx = RE_SEARCH;
5530 else if (lp[0] == '&')
5531 idx = RE_SUBST;
5532#ifdef FEAT_SEARCH_EXTRA
5533 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5534 hlsearch_on = FALSE;
5535 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5536 hlsearch_on = TRUE;
5537#endif
5538 if (idx >= 0)
5539 {
5540 if (force || spats[idx].pat == NULL)
5541 {
5542 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5543 TRUE);
5544 if (val != NULL)
5545 {
5546 set_last_search_pat(val, idx, magic, setlast);
5547 vim_free(val);
5548 spats[idx].no_scs = no_scs;
5549 spats[idx].off.line = off_line;
5550 spats[idx].off.end = off_end;
5551 spats[idx].off.off = off;
5552#ifdef FEAT_SEARCH_EXTRA
5553 if (setlast)
Bram Moolenaar8050efa2013-11-08 04:30:20 +01005554 {
5555 SET_NO_HLSEARCH(!hlsearch_on);
5556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557#endif
5558 }
5559 }
5560 }
5561 return viminfo_readline(virp);
5562}
5563
5564 void
5565write_viminfo_search_pattern(fp)
5566 FILE *fp;
5567{
5568 if (get_viminfo_parameter('/') != 0)
5569 {
5570#ifdef FEAT_SEARCH_EXTRA
5571 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5572 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5573#endif
5574 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005575 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577}
5578
5579 static void
5580wvsp_one(fp, idx, s, sc)
5581 FILE *fp; /* file to write to */
5582 int idx; /* spats[] index */
5583 char *s; /* search pat */
5584 int sc; /* dir char */
5585{
5586 if (spats[idx].pat != NULL)
5587 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005588 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 /* off.dir is not stored, it's reset to forward */
5590 fprintf(fp, "%c%c%c%c%ld%s%c",
5591 spats[idx].magic ? 'M' : 'm', /* magic */
5592 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5593 spats[idx].off.line ? 'L' : 'l', /* line offset */
5594 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5595 spats[idx].off.off, /* offset */
5596 last_idx == idx ? "~" : "", /* last used pat */
5597 sc);
5598 viminfo_writestring(fp, spats[idx].pat);
5599 }
5600}
5601#endif /* FEAT_VIMINFO */