blob: b64b8f6ce7931ff7a2c2828e11402f438367e95a [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;
551 int match_ok;
552 long nmatched;
553 int submatch = 0;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100554 int first_match = TRUE;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000555 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556#ifdef FEAT_SEARCH_EXTRA
557 int break_loop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558#endif
559
560 if (search_regcomp(pat, RE_SEARCH, pat_use,
561 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
562 {
563 if ((options & SEARCH_MSG) && !rc_did_emsg)
564 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
565 return FAIL;
566 }
567
Bram Moolenaar280f1262006-01-30 00:14:18 +0000568 /*
569 * find the string
570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 called_emsg = FALSE;
572 do /* loop for count */
573 {
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100574 /* When not accepting a match at the start position set "extra_col" to
575 * a non-zero value. Don't do that when starting at MAXCOL, since
576 * MAXCOL + 1 is zero. */
577 if ((options & SEARCH_START) || pos->col == MAXCOL)
578 extra_col = 0;
579#ifdef FEAT_MBYTE
580 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
581 else if (dir != BACKWARD && has_mbyte
582 && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
583 && pos->col < MAXCOL - 2)
584 {
585 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
586 if (*ptr == NUL)
587 extra_col = 1;
588 else
589 extra_col = (*mb_ptr2len)(ptr);
590 }
591#endif
592 else
593 extra_col = 1;
594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 start_pos = *pos; /* remember start pos for detecting no match */
596 found = 0; /* default: not found */
597 at_first_line = TRUE; /* default: start in first line */
598 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
599 {
600 pos->lnum = 1;
601 pos->col = 0;
602 at_first_line = FALSE; /* not in first line now */
603 }
604
605 /*
606 * Start searching in current line, unless searching backwards and
607 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000608 * If we are searching backwards, in column 0, and not including the
609 * current position, gain some efficiency by skipping back a line.
610 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000612 if (dir == BACKWARD && start_pos.col == 0
613 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 lnum = pos->lnum - 1;
616 at_first_line = FALSE;
617 }
618 else
619 lnum = pos->lnum;
620
621 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
622 {
623 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
624 lnum += dir, at_first_line = FALSE)
625 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000626 /* Stop after checking "stop_lnum", if it's set. */
627 if (stop_lnum != 0 && (dir == FORWARD
628 ? lnum > stop_lnum : lnum < stop_lnum))
629 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000630#ifdef FEAT_RELTIME
631 /* Stop after passing the "tm" time limit. */
632 if (tm != NULL && profile_passed_limit(tm))
633 break;
634#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000637 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000640 lnum, (colnr_T)0,
641#ifdef FEAT_RELTIME
642 tm
643#else
644 NULL
645#endif
646 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 /* Abort searching on an error (e.g., out of stack). */
648 if (called_emsg)
649 break;
650 if (nmatched > 0)
651 {
652 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000653 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000655#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000657#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000658 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000659 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
660 ptr = (char_u *)"";
661 else
662 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664 /*
665 * Forward search in the first line: match should be after
666 * the start position. If not, continue at the end of the
667 * match (this is vi compatible) or on the next char.
668 */
669 if (dir == FORWARD && at_first_line)
670 {
671 match_ok = TRUE;
672 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000673 * When the match starts in a next line it's certainly
674 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 * When match lands on a NUL the cursor will be put
676 * one back afterwards, compare with that position,
677 * otherwise "/$" will get stuck on end of line.
678 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000679 while (matchpos.lnum == 0
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100680 && ((options & SEARCH_END) && first_match
Bram Moolenaar677ee682005-01-27 14:41:15 +0000681 ? (nmatched == 1
682 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000684 : ((int)matchpos.col
685 - (ptr[matchpos.col] == NUL)
686 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
688 /*
689 * If vi-compatible searching, continue at the end
690 * of the match, otherwise continue one position
691 * forward.
692 */
693 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
694 {
695 if (nmatched > 1)
696 {
697 /* end is in next line, thus no match in
698 * this line */
699 match_ok = FALSE;
700 break;
701 }
702 matchcol = endpos.col;
703 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000704 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 && ptr[matchcol] != NUL)
706 {
707#ifdef FEAT_MBYTE
708 if (has_mbyte)
709 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000710 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 else
712#endif
713 ++matchcol;
714 }
715 }
716 else
717 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000718 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 if (ptr[matchcol] != NUL)
720 {
721#ifdef FEAT_MBYTE
722 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000723 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 + matchcol);
725 else
726#endif
727 ++matchcol;
728 }
729 }
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200730 if (matchcol == 0 && (options & SEARCH_START))
Bram Moolenaardb333a52013-03-19 15:27:48 +0100731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 if (ptr[matchcol] == NUL
733 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000734 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000735 matchcol,
736#ifdef FEAT_RELTIME
737 tm
738#else
739 NULL
740#endif
741 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
743 match_ok = FALSE;
744 break;
745 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000746 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 endpos = regmatch.endpos[0];
748# ifdef FEAT_EVAL
749 submatch = first_submatch(&regmatch);
750# endif
751
752 /* Need to get the line pointer again, a
753 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000754 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756 if (!match_ok)
757 continue;
758 }
759 if (dir == BACKWARD)
760 {
761 /*
762 * Now, if there are multiple matches on this line,
763 * we have to get the last one. Or the last one before
764 * the cursor, if we're on that line.
765 * When putting the new cursor at the end, compare
766 * relative to the end of the match.
767 */
768 match_ok = FALSE;
769 for (;;)
770 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000771 /* Remember a position that is before the start
772 * position, we use it if it's the last match in
773 * the line. Always accept a position after
774 * wrapping around. */
775 if (loop
776 || ((options & SEARCH_END)
777 ? (lnum + regmatch.endpos[0].lnum
778 < start_pos.lnum
779 || (lnum + regmatch.endpos[0].lnum
780 == start_pos.lnum
781 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000783 <= (int)start_pos.col))
784 : (lnum + regmatch.startpos[0].lnum
785 < start_pos.lnum
786 || (lnum + regmatch.startpos[0].lnum
787 == start_pos.lnum
788 && (int)regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000790 <= (int)start_pos.col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000793 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 endpos = regmatch.endpos[0];
795# ifdef FEAT_EVAL
796 submatch = first_submatch(&regmatch);
797# endif
798 }
799 else
800 break;
801
802 /*
803 * We found a valid match, now check if there is
804 * another one after it.
805 * If vi-compatible searching, continue at the end
806 * of the match, otherwise continue one position
807 * forward.
808 */
809 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
810 {
811 if (nmatched > 1)
812 break;
813 matchcol = endpos.col;
814 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000815 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 && ptr[matchcol] != NUL)
817 {
818#ifdef FEAT_MBYTE
819 if (has_mbyte)
820 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000821 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 else
823#endif
824 ++matchcol;
825 }
826 }
827 else
828 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000829 /* Stop when the match is in a next line. */
830 if (matchpos.lnum > 0)
831 break;
832 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (ptr[matchcol] != NUL)
834 {
835#ifdef FEAT_MBYTE
836 if (has_mbyte)
837 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000838 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 else
840#endif
841 ++matchcol;
842 }
843 }
844 if (ptr[matchcol] == NUL
845 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000846 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000847 matchcol,
848#ifdef FEAT_RELTIME
849 tm
850#else
851 NULL
852#endif
853 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 break;
855
856 /* Need to get the line pointer again, a
857 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000858 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 }
860
861 /*
862 * If there is only a match after the cursor, skip
863 * this match.
864 */
865 if (!match_ok)
866 continue;
867 }
868
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000869 /* With the SEARCH_END option move to the last character
870 * of the match. Don't do it for an empty match, end
871 * should be same as start then. */
Bram Moolenaar7bcb30e2013-04-03 21:14:29 +0200872 if ((options & SEARCH_END) && !(options & SEARCH_NOOF)
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000873 && !(matchpos.lnum == endpos.lnum
874 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000876 /* For a match in the first column, set the position
877 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000878 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000879 pos->col = endpos.col;
880 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000881 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000882 if (pos->lnum > 1) /* just in case */
883 {
884 --pos->lnum;
885 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
886 pos->lnum, FALSE));
887 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000888 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000889 else
890 {
891 --pos->col;
892#ifdef FEAT_MBYTE
893 if (has_mbyte
894 && pos->lnum <= buf->b_ml.ml_line_count)
895 {
896 ptr = ml_get_buf(buf, pos->lnum, FALSE);
897 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
898 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000899#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902 else
903 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000904 pos->lnum = lnum + matchpos.lnum;
905 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 }
907#ifdef FEAT_VIRTUALEDIT
908 pos->coladd = 0;
909#endif
910 found = 1;
Bram Moolenaara3dfccc2014-11-27 17:29:56 +0100911 first_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000914 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 search_match_endcol = endpos.col;
916 break;
917 }
918 line_breakcheck(); /* stop if ctrl-C typed */
919 if (got_int)
920 break;
921
922#ifdef FEAT_SEARCH_EXTRA
923 /* Cancel searching if a character was typed. Used for
924 * 'incsearch'. Don't check too often, that would slowdown
925 * searching too much. */
926 if ((options & SEARCH_PEEK)
927 && ((lnum - pos->lnum) & 0x3f) == 0
928 && char_avail())
929 {
930 break_loop = TRUE;
931 break;
932 }
933#endif
934
935 if (loop && lnum == start_pos.lnum)
936 break; /* if second loop, stop where started */
937 }
938 at_first_line = FALSE;
939
940 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000941 * Stop the search if wrapscan isn't set, "stop_lnum" is
942 * specified, after an interrupt, after a match and after looping
943 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000945 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
Bram Moolenaar78a15312009-05-15 19:33:18 +0000946#ifdef FEAT_SEARCH_EXTRA
947 || break_loop
948#endif
949 || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 break;
951
952 /*
953 * If 'wrapscan' is set we continue at the other end of the file.
954 * If 'shortmess' does not contain 's', we give a message.
955 * This message is also remembered in keep_msg for when the screen
956 * is redrawn. The keep_msg is cleared whenever another message is
957 * written.
958 */
959 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +0000963 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
964 give_warning((char_u *)_(dir == BACKWARD
965 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
Bram Moolenaar78a15312009-05-15 19:33:18 +0000967 if (got_int || called_emsg
968#ifdef FEAT_SEARCH_EXTRA
969 || break_loop
970#endif
971 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 break;
973 }
974 while (--count > 0 && found); /* stop after count matches or no match */
975
Bram Moolenaar473de612013-06-08 18:19:48 +0200976 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar280f1262006-01-30 00:14:18 +0000978 called_emsg |= save_called_emsg;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (!found) /* did not find it */
981 {
982 if (got_int)
983 EMSG(_(e_interr));
984 else if ((options & SEARCH_MSG) == SEARCH_MSG)
985 {
986 if (p_ws)
987 EMSG2(_(e_patnotf2), mr_pattern);
988 else if (lnum == 0)
989 EMSG2(_("E384: search hit TOP without match for: %s"),
990 mr_pattern);
991 else
992 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
993 mr_pattern);
994 }
995 return FAIL;
996 }
997
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000998 /* A pattern like "\n\zs" may go past the last line. */
999 if (pos->lnum > buf->b_ml.ml_line_count)
1000 {
1001 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001002 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001003 if (pos->col > 0)
1004 --pos->col;
1005 }
1006
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 return submatch + 1;
1008}
1009
1010#ifdef FEAT_EVAL
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001011 void
1012set_search_direction(cdir)
1013 int cdir;
1014{
1015 spats[0].off.dir = cdir;
1016}
1017
1018 static void
1019set_vv_searchforward()
1020{
1021 set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/'));
1022}
1023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024/*
1025 * Return the number of the first subpat that matched.
1026 */
1027 static int
1028first_submatch(rp)
1029 regmmatch_T *rp;
1030{
1031 int submatch;
1032
1033 for (submatch = 1; ; ++submatch)
1034 {
1035 if (rp->startpos[submatch].lnum >= 0)
1036 break;
1037 if (submatch == 9)
1038 {
1039 submatch = 0;
1040 break;
1041 }
1042 }
1043 return submatch;
1044}
1045#endif
1046
1047/*
1048 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +00001049 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 * If 'dirc' is 0: use previous dir.
1051 * If 'pat' is NULL or empty : use previous string.
1052 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1053 * If 'options & SEARCH_ECHO': echo the search command and handle options
1054 * If 'options & SEARCH_MSG' : may give error message
1055 * If 'options & SEARCH_OPT' : interpret optional flags
1056 * If 'options & SEARCH_HIS' : put search pattern in history
1057 * If 'options & SEARCH_NOOF': don't add offset to position
1058 * If 'options & SEARCH_MARK': set previous context mark
1059 * If 'options & SEARCH_KEEP': keep previous search pattern
1060 * If 'options & SEARCH_START': accept match at curpos itself
1061 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1062 *
1063 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1064 * makes the movement linewise without moving the match position.
1065 *
1066 * return 0 for failure, 1 for found, 2 for found and line offset added
1067 */
1068 int
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001069do_search(oap, dirc, pat, count, options, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 oparg_T *oap; /* can be NULL */
1071 int dirc; /* '/' or '?' */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001072 char_u *pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 long count;
1074 int options;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001075 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 pos_T pos; /* position of the last match */
1078 char_u *searchstr;
1079 struct soffset old_off;
1080 int retval; /* Return value */
1081 char_u *p;
1082 long c;
1083 char_u *dircp;
1084 char_u *strcopy = NULL;
1085 char_u *ps;
1086
1087 /*
1088 * A line offset is not remembered, this is vi compatible.
1089 */
1090 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1091 {
1092 spats[0].off.line = FALSE;
1093 spats[0].off.off = 0;
1094 }
1095
1096 /*
1097 * Save the values for when (options & SEARCH_KEEP) is used.
1098 * (there is no "if ()" around this because gcc wants them initialized)
1099 */
1100 old_off = spats[0].off;
1101
1102 pos = curwin->w_cursor; /* start searching at the cursor position */
1103
1104 /*
1105 * Find out the direction of the search.
1106 */
1107 if (dirc == 0)
1108 dirc = spats[0].off.dir;
1109 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001110 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 spats[0].off.dir = dirc;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001112#if defined(FEAT_EVAL)
1113 set_vv_searchforward();
1114#endif
1115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (options & SEARCH_REV)
1117 {
1118#ifdef WIN32
1119 /* There is a bug in the Visual C++ 2.2 compiler which means that
1120 * dirc always ends up being '/' */
1121 dirc = (dirc == '/') ? '?' : '/';
1122#else
1123 if (dirc == '/')
1124 dirc = '?';
1125 else
1126 dirc = '/';
1127#endif
1128 }
1129
1130#ifdef FEAT_FOLDING
1131 /* If the cursor is in a closed fold, don't find another match in the same
1132 * fold. */
1133 if (dirc == '/')
1134 {
1135 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1136 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1137 }
1138 else
1139 {
1140 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1141 pos.col = 0;
1142 }
1143#endif
1144
1145#ifdef FEAT_SEARCH_EXTRA
1146 /*
1147 * Turn 'hlsearch' highlighting back on.
1148 */
1149 if (no_hlsearch && !(options & SEARCH_KEEP))
1150 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001151 redraw_all_later(SOME_VALID);
Bram Moolenaar8050efa2013-11-08 04:30:20 +01001152 SET_NO_HLSEARCH(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 }
1154#endif
1155
1156 /*
1157 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1158 */
1159 for (;;)
1160 {
1161 searchstr = pat;
1162 dircp = NULL;
1163 /* use previous pattern */
1164 if (pat == NULL || *pat == NUL || *pat == dirc)
1165 {
1166 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1167 {
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001168 pat = spats[RE_SUBST].pat;
1169 if (pat == NULL)
1170 {
1171 EMSG(_(e_noprevre));
1172 retval = 0;
1173 goto end_do_search;
1174 }
1175 searchstr = pat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 }
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01001177 else
1178 {
1179 /* make search_regcomp() use spats[RE_SEARCH].pat */
1180 searchstr = (char_u *)"";
1181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 }
1183
1184 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1185 {
1186 /*
1187 * Find end of regular expression.
1188 * If there is a matching '/' or '?', toss it.
1189 */
1190 ps = strcopy;
1191 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1192 if (strcopy != ps)
1193 {
1194 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001195 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 pat = strcopy;
1197 searchstr = strcopy;
1198 }
1199 if (*p == dirc)
1200 {
1201 dircp = p; /* remember where we put the NUL */
1202 *p++ = NUL;
1203 }
1204 spats[0].off.line = FALSE;
1205 spats[0].off.end = FALSE;
1206 spats[0].off.off = 0;
1207 /*
1208 * Check for a line offset or a character offset.
1209 * For get_address (echo off) we don't check for a character
1210 * offset, because it is meaningless and the 's' could be a
1211 * substitute command.
1212 */
1213 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1214 spats[0].off.line = TRUE;
1215 else if ((options & SEARCH_OPT) &&
1216 (*p == 'e' || *p == 's' || *p == 'b'))
1217 {
1218 if (*p == 'e') /* end */
1219 spats[0].off.end = SEARCH_END;
1220 ++p;
1221 }
1222 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1223 {
1224 /* 'nr' or '+nr' or '-nr' */
1225 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1226 spats[0].off.off = atol((char *)p);
1227 else if (*p == '-') /* single '-' */
1228 spats[0].off.off = -1;
1229 else /* single '+' */
1230 spats[0].off.off = 1;
1231 ++p;
1232 while (VIM_ISDIGIT(*p)) /* skip number */
1233 ++p;
1234 }
1235
1236 /* compute length of search command for get_address() */
1237 searchcmdlen += (int)(p - pat);
1238
1239 pat = p; /* put pat after search command */
1240 }
1241
1242 if ((options & SEARCH_ECHO) && messaging()
1243 && !cmd_silent && msg_silent == 0)
1244 {
1245 char_u *msgbuf;
1246 char_u *trunc;
1247
1248 if (*searchstr == NUL)
1249 p = spats[last_idx].pat;
1250 else
1251 p = searchstr;
1252 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1253 if (msgbuf != NULL)
1254 {
1255 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001256#ifdef FEAT_MBYTE
1257 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1258 {
1259 /* Use a space to draw the composing char on. */
1260 msgbuf[1] = ' ';
1261 STRCPY(msgbuf + 2, p);
1262 }
1263 else
1264#endif
1265 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1267 {
1268 p = msgbuf + STRLEN(msgbuf);
1269 *p++ = dirc;
1270 if (spats[0].off.end)
1271 *p++ = 'e';
1272 else if (!spats[0].off.line)
1273 *p++ = 's';
1274 if (spats[0].off.off > 0 || spats[0].off.line)
1275 *p++ = '+';
1276 if (spats[0].off.off != 0 || spats[0].off.line)
1277 sprintf((char *)p, "%ld", spats[0].off.off);
1278 else
1279 *p = NUL;
1280 }
1281
1282 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001283 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284
1285#ifdef FEAT_RIGHTLEFT
1286 /* The search pattern could be shown on the right in rightleft
1287 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1288 * it would be blanked out again very soon. Show it on the
1289 * left, but do reverse the text. */
1290 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1291 {
1292 char_u *r;
1293
1294 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1295 if (r != NULL)
1296 {
1297 vim_free(trunc);
1298 trunc = r;
1299 }
1300 }
1301#endif
1302 if (trunc != NULL)
1303 {
1304 msg_outtrans(trunc);
1305 vim_free(trunc);
1306 }
1307 else
1308 msg_outtrans(msgbuf);
1309 msg_clr_eos();
1310 msg_check();
1311 vim_free(msgbuf);
1312
1313 gotocmdline(FALSE);
1314 out_flush();
1315 msg_nowait = TRUE; /* don't wait for this message */
1316 }
1317 }
1318
1319 /*
1320 * If there is a character offset, subtract it from the current
1321 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001322 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 * This is not done for a line offset, because then we would not be vi
1324 * compatible.
1325 */
Bram Moolenaared203462004-06-16 11:19:22 +00001326 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 {
1328 if (spats[0].off.off > 0)
1329 {
1330 for (c = spats[0].off.off; c; --c)
1331 if (decl(&pos) == -1)
1332 break;
1333 if (c) /* at start of buffer */
1334 {
1335 pos.lnum = 0; /* allow lnum == 0 here */
1336 pos.col = MAXCOL;
1337 }
1338 }
1339 else
1340 {
1341 for (c = spats[0].off.off; c; ++c)
1342 if (incl(&pos) == -1)
1343 break;
1344 if (c) /* at end of buffer */
1345 {
1346 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1347 pos.col = 0;
1348 }
1349 }
1350 }
1351
1352#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1353 if (p_altkeymap && curwin->w_p_rl)
1354 lrFswap(searchstr,0);
1355#endif
1356
1357 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1358 searchstr, count, spats[0].off.end + (options &
1359 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1360 + SEARCH_MSG + SEARCH_START
1361 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001362 RE_LAST, (linenr_T)0, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364 if (dircp != NULL)
1365 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1366 if (c == FAIL)
1367 {
1368 retval = 0;
1369 goto end_do_search;
1370 }
1371 if (spats[0].off.end && oap != NULL)
1372 oap->inclusive = TRUE; /* 'e' includes last character */
1373
1374 retval = 1; /* pattern found */
1375
1376 /*
1377 * Add character and/or line offset
1378 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001379 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 {
1381 if (spats[0].off.line) /* Add the offset to the line number. */
1382 {
1383 c = pos.lnum + spats[0].off.off;
1384 if (c < 1)
1385 pos.lnum = 1;
1386 else if (c > curbuf->b_ml.ml_line_count)
1387 pos.lnum = curbuf->b_ml.ml_line_count;
1388 else
1389 pos.lnum = c;
1390 pos.col = 0;
1391
1392 retval = 2; /* pattern found, line offset added */
1393 }
Bram Moolenaared203462004-06-16 11:19:22 +00001394 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
1396 /* to the right, check for end of file */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001397 c = spats[0].off.off;
1398 if (c > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001400 while (c-- > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 if (incl(&pos) == -1)
1402 break;
1403 }
1404 /* to the left, check for start of file */
1405 else
1406 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001407 while (c++ < 0)
1408 if (decl(&pos) == -1)
1409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 }
1411 }
1412 }
1413
1414 /*
1415 * The search command can be followed by a ';' to do another search.
1416 * For example: "/pat/;/foo/+3;?bar"
1417 * This is like doing another search command, except:
1418 * - The remembered direction '/' or '?' is from the first search.
1419 * - When an error happens the cursor isn't moved at all.
1420 * Don't do this when called by get_address() (it handles ';' itself).
1421 */
1422 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1423 break;
1424
1425 dirc = *++pat;
1426 if (dirc != '?' && dirc != '/')
1427 {
1428 retval = 0;
1429 EMSG(_("E386: Expected '?' or '/' after ';'"));
1430 goto end_do_search;
1431 }
1432 ++pat;
1433 }
1434
1435 if (options & SEARCH_MARK)
1436 setpcmark();
1437 curwin->w_cursor = pos;
1438 curwin->w_set_curswant = TRUE;
1439
1440end_do_search:
Bram Moolenaarac8400d2014-01-14 21:31:34 +01001441 if ((options & SEARCH_KEEP) || cmdmod.keeppatterns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 spats[0].off = old_off;
1443 vim_free(strcopy);
1444
1445 return retval;
1446}
1447
1448#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1449/*
1450 * search_for_exact_line(buf, pos, dir, pat)
1451 *
1452 * Search for a line starting with the given pattern (ignoring leading
1453 * white-space), starting from pos and going in direction dir. pos will
1454 * contain the position of the match found. Blank lines match only if
1455 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1456 * Return OK for success, or FAIL if no line found.
1457 */
1458 int
1459search_for_exact_line(buf, pos, dir, pat)
1460 buf_T *buf;
1461 pos_T *pos;
1462 int dir;
1463 char_u *pat;
1464{
1465 linenr_T start = 0;
1466 char_u *ptr;
1467 char_u *p;
1468
1469 if (buf->b_ml.ml_line_count == 0)
1470 return FAIL;
1471 for (;;)
1472 {
1473 pos->lnum += dir;
1474 if (pos->lnum < 1)
1475 {
1476 if (p_ws)
1477 {
1478 pos->lnum = buf->b_ml.ml_line_count;
1479 if (!shortmess(SHM_SEARCH))
1480 give_warning((char_u *)_(top_bot_msg), TRUE);
1481 }
1482 else
1483 {
1484 pos->lnum = 1;
1485 break;
1486 }
1487 }
1488 else if (pos->lnum > buf->b_ml.ml_line_count)
1489 {
1490 if (p_ws)
1491 {
1492 pos->lnum = 1;
1493 if (!shortmess(SHM_SEARCH))
1494 give_warning((char_u *)_(bot_top_msg), TRUE);
1495 }
1496 else
1497 {
1498 pos->lnum = 1;
1499 break;
1500 }
1501 }
1502 if (pos->lnum == start)
1503 break;
1504 if (start == 0)
1505 start = pos->lnum;
1506 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1507 p = skipwhite(ptr);
1508 pos->col = (colnr_T) (p - ptr);
1509
1510 /* when adding lines the matching line may be empty but it is not
1511 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001512 if ((compl_cont_status & CONT_ADDING)
1513 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 {
1515 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1516 return OK;
1517 }
1518 else if (*p != NUL) /* ignore empty lines */
1519 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001520 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1521 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 return OK;
1523 }
1524 }
1525 return FAIL;
1526}
1527#endif /* FEAT_INS_EXPAND */
1528
1529/*
1530 * Character Searches
1531 */
1532
1533/*
1534 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1535 * position of the character, otherwise move to just before the char.
1536 * Do this "cap->count1" times.
1537 * Return FAIL or OK.
1538 */
1539 int
1540searchc(cap, t_cmd)
1541 cmdarg_T *cap;
1542 int t_cmd;
1543{
1544 int c = cap->nchar; /* char to search for */
1545 int dir = cap->arg; /* TRUE for searching forward */
1546 long count = cap->count1; /* repeat count */
1547 static int lastc = NUL; /* last character searched for */
1548 static int lastcdir; /* last direction of character search */
1549 static int last_t_cmd; /* last search t_cmd */
1550 int col;
1551 char_u *p;
1552 int len;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001553 int stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554#ifdef FEAT_MBYTE
Bram Moolenaar81340392012-06-06 16:12:59 +02001555 static char_u bytes[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 static int bytelen = 1; /* >1 for multi-byte char */
1557#endif
1558
1559 if (c != NUL) /* normal search: remember args for repeat */
1560 {
1561 if (!KeyStuffed) /* don't remember when redoing */
1562 {
1563 lastc = c;
1564 lastcdir = dir;
1565 last_t_cmd = t_cmd;
1566#ifdef FEAT_MBYTE
1567 bytelen = (*mb_char2bytes)(c, bytes);
1568 if (cap->ncharC1 != 0)
1569 {
1570 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1571 if (cap->ncharC2 != 0)
1572 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1573 }
1574#endif
1575 }
1576 }
1577 else /* repeat previous search */
1578 {
1579 if (lastc == NUL)
1580 return FAIL;
1581 if (dir) /* repeat in opposite direction */
1582 dir = -lastcdir;
1583 else
1584 dir = lastcdir;
1585 t_cmd = last_t_cmd;
1586 c = lastc;
1587 /* For multi-byte re-use last bytes[] and bytelen. */
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001588
1589 /* Force a move of at least one char, so ";" and "," will move the
1590 * cursor, even if the cursor is right in front of char we are looking
1591 * at. */
Bram Moolenaar19fd09a2011-07-15 13:21:30 +02001592 if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd)
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001593 stop = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 }
1595
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001596 if (dir == BACKWARD)
1597 cap->oap->inclusive = FALSE;
1598 else
1599 cap->oap->inclusive = TRUE;
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 p = ml_get_curline();
1602 col = curwin->w_cursor.col;
1603 len = (int)STRLEN(p);
1604
1605 while (count--)
1606 {
1607#ifdef FEAT_MBYTE
1608 if (has_mbyte)
1609 {
1610 for (;;)
1611 {
1612 if (dir > 0)
1613 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001614 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (col >= len)
1616 return FAIL;
1617 }
1618 else
1619 {
1620 if (col == 0)
1621 return FAIL;
1622 col -= (*mb_head_off)(p, p + col - 1) + 1;
1623 }
1624 if (bytelen == 1)
1625 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001626 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 break;
1628 }
1629 else
1630 {
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001631 if (vim_memcmp(p + col, bytes, bytelen) == 0 && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 break;
1633 }
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001634 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 }
1636 }
1637 else
1638#endif
1639 {
1640 for (;;)
1641 {
1642 if ((col += dir) < 0 || col >= len)
1643 return FAIL;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001644 if (p[col] == c && stop)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 break;
Bram Moolenaar8b3e0332011-06-26 05:36:34 +02001646 stop = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 }
1648 }
1649 }
1650
1651 if (t_cmd)
1652 {
1653 /* backup to before the character (possibly double-byte) */
1654 col -= dir;
1655#ifdef FEAT_MBYTE
1656 if (has_mbyte)
1657 {
1658 if (dir < 0)
1659 /* Landed on the search char which is bytelen long */
1660 col += bytelen - 1;
1661 else
1662 /* To previous char, which may be multi-byte. */
1663 col -= (*mb_head_off)(p, p + col);
1664 }
1665#endif
1666 }
1667 curwin->w_cursor.col = col;
1668
1669 return OK;
1670}
1671
1672/*
1673 * "Other" Searches
1674 */
1675
1676/*
1677 * findmatch - find the matching paren or brace
1678 *
1679 * Improvement over vi: Braces inside quotes are ignored.
1680 */
1681 pos_T *
1682findmatch(oap, initc)
1683 oparg_T *oap;
1684 int initc;
1685{
1686 return findmatchlimit(oap, initc, 0, 0);
1687}
1688
1689/*
1690 * Return TRUE if the character before "linep[col]" equals "ch".
1691 * Return FALSE if "col" is zero.
1692 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1693 * is NULL.
1694 * Handles multibyte string correctly.
1695 */
1696 static int
1697check_prevcol(linep, col, ch, prevcol)
1698 char_u *linep;
1699 int col;
1700 int ch;
1701 int *prevcol;
1702{
1703 --col;
1704#ifdef FEAT_MBYTE
1705 if (col > 0 && has_mbyte)
1706 col -= (*mb_head_off)(linep, linep + col);
1707#endif
1708 if (prevcol)
1709 *prevcol = col;
1710 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1711}
1712
1713/*
1714 * findmatchlimit -- find the matching paren or brace, if it exists within
1715 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1716 * the edge of the file.
1717 *
1718 * "initc" is the character to find a match for. NUL means to find the
1719 * character at or after the cursor.
1720 *
1721 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1722 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1723 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1724 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001725 *
1726 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1727 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 */
1729
1730 pos_T *
1731findmatchlimit(oap, initc, flags, maxtravel)
1732 oparg_T *oap;
1733 int initc;
1734 int flags;
1735 int maxtravel;
1736{
1737 static pos_T pos; /* current search position */
1738 int findc = 0; /* matching brace */
1739 int c;
1740 int count = 0; /* cumulative number of braces */
1741 int backwards = FALSE; /* init for gcc */
1742 int inquote = FALSE; /* TRUE when inside quotes */
1743 char_u *linep; /* pointer to current line */
1744 char_u *ptr;
1745 int do_quotes; /* check for quotes in current line */
1746 int at_start; /* do_quotes value at start position */
1747 int hash_dir = 0; /* Direction searched for # things */
1748 int comment_dir = 0; /* Direction searched for comments */
1749 pos_T match_pos; /* Where last slash-star was found */
1750 int start_in_quotes; /* start position is in quotes */
1751 int traveled = 0; /* how far we've searched so far */
1752 int ignore_cend = FALSE; /* ignore comment end */
1753 int cpo_match; /* vi compatible matching */
1754 int cpo_bsl; /* don't recognize backslashes */
1755 int match_escaped = 0; /* search for escaped match */
1756 int dir; /* Direction to search */
1757 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001758#ifdef FEAT_LISP
1759 int lispcomm = FALSE; /* inside of Lisp-style comment */
1760 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762
1763 pos = curwin->w_cursor;
Bram Moolenaarc56c4592013-08-14 17:45:29 +02001764#ifdef FEAT_VIRTUALEDIT
1765 pos.coladd = 0;
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 linep = ml_get(pos.lnum);
1768
1769 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1770 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1771
1772 /* Direction to search when initc is '/', '*' or '#' */
1773 if (flags & FM_BACKWARD)
1774 dir = BACKWARD;
1775 else if (flags & FM_FORWARD)
1776 dir = FORWARD;
1777 else
1778 dir = 0;
1779
1780 /*
1781 * if initc given, look in the table for the matching character
1782 * '/' and '*' are special cases: look for start or end of comment.
1783 * When '/' is used, we ignore running backwards into an star-slash, for
1784 * "[*" command, we just want to find any comment.
1785 */
1786 if (initc == '/' || initc == '*')
1787 {
1788 comment_dir = dir;
1789 if (initc == '/')
1790 ignore_cend = TRUE;
1791 backwards = (dir == FORWARD) ? FALSE : TRUE;
1792 initc = NUL;
1793 }
1794 else if (initc != '#' && initc != NUL)
1795 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001796 find_mps_values(&initc, &findc, &backwards, TRUE);
1797 if (findc == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 return NULL;
1799 }
1800 /*
1801 * Either initc is '#', or no initc was given and we need to look under the
1802 * cursor.
1803 */
1804 else
1805 {
1806 if (initc == '#')
1807 {
1808 hash_dir = dir;
1809 }
1810 else
1811 {
1812 /*
1813 * initc was not given, must look for something to match under
1814 * or near the cursor.
1815 * Only check for special things when 'cpo' doesn't have '%'.
1816 */
1817 if (!cpo_match)
1818 {
1819 /* Are we before or at #if, #else etc.? */
1820 ptr = skipwhite(linep);
1821 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1822 {
1823 ptr = skipwhite(ptr + 1);
1824 if ( STRNCMP(ptr, "if", 2) == 0
1825 || STRNCMP(ptr, "endif", 5) == 0
1826 || STRNCMP(ptr, "el", 2) == 0)
1827 hash_dir = 1;
1828 }
1829
1830 /* Are we on a comment? */
1831 else if (linep[pos.col] == '/')
1832 {
1833 if (linep[pos.col + 1] == '*')
1834 {
1835 comment_dir = FORWARD;
1836 backwards = FALSE;
1837 pos.col++;
1838 }
1839 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1840 {
1841 comment_dir = BACKWARD;
1842 backwards = TRUE;
1843 pos.col--;
1844 }
1845 }
1846 else if (linep[pos.col] == '*')
1847 {
1848 if (linep[pos.col + 1] == '/')
1849 {
1850 comment_dir = BACKWARD;
1851 backwards = TRUE;
1852 }
1853 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1854 {
1855 comment_dir = FORWARD;
1856 backwards = FALSE;
1857 }
1858 }
1859 }
1860
1861 /*
1862 * If we are not on a comment or the # at the start of a line, then
1863 * look for brace anywhere on this line after the cursor.
1864 */
1865 if (!hash_dir && !comment_dir)
1866 {
1867 /*
1868 * Find the brace under or after the cursor.
1869 * If beyond the end of the line, use the last character in
1870 * the line.
1871 */
1872 if (linep[pos.col] == NUL && pos.col)
1873 --pos.col;
1874 for (;;)
1875 {
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001876 initc = PTR2CHAR(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 if (initc == NUL)
1878 break;
1879
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001880 find_mps_values(&initc, &findc, &backwards, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (findc)
1882 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01001883 pos.col += MB_PTR2LEN(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 }
1885 if (!findc)
1886 {
1887 /* no brace in the line, maybe use " #if" then */
1888 if (!cpo_match && *skipwhite(linep) == '#')
1889 hash_dir = 1;
1890 else
1891 return NULL;
1892 }
1893 else if (!cpo_bsl)
1894 {
1895 int col, bslcnt = 0;
1896
1897 /* Set "match_escaped" if there are an odd number of
1898 * backslashes. */
1899 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1900 bslcnt++;
1901 match_escaped = (bslcnt & 1);
1902 }
1903 }
1904 }
1905 if (hash_dir)
1906 {
1907 /*
1908 * Look for matching #if, #else, #elif, or #endif
1909 */
1910 if (oap != NULL)
1911 oap->motion_type = MLINE; /* Linewise for this case only */
1912 if (initc != '#')
1913 {
1914 ptr = skipwhite(skipwhite(linep) + 1);
1915 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1916 hash_dir = 1;
1917 else if (STRNCMP(ptr, "endif", 5) == 0)
1918 hash_dir = -1;
1919 else
1920 return NULL;
1921 }
1922 pos.col = 0;
1923 while (!got_int)
1924 {
1925 if (hash_dir > 0)
1926 {
1927 if (pos.lnum == curbuf->b_ml.ml_line_count)
1928 break;
1929 }
1930 else if (pos.lnum == 1)
1931 break;
1932 pos.lnum += hash_dir;
1933 linep = ml_get(pos.lnum);
1934 line_breakcheck(); /* check for CTRL-C typed */
1935 ptr = skipwhite(linep);
1936 if (*ptr != '#')
1937 continue;
1938 pos.col = (colnr_T) (ptr - linep);
1939 ptr = skipwhite(ptr + 1);
1940 if (hash_dir > 0)
1941 {
1942 if (STRNCMP(ptr, "if", 2) == 0)
1943 count++;
1944 else if (STRNCMP(ptr, "el", 2) == 0)
1945 {
1946 if (count == 0)
1947 return &pos;
1948 }
1949 else if (STRNCMP(ptr, "endif", 5) == 0)
1950 {
1951 if (count == 0)
1952 return &pos;
1953 count--;
1954 }
1955 }
1956 else
1957 {
1958 if (STRNCMP(ptr, "if", 2) == 0)
1959 {
1960 if (count == 0)
1961 return &pos;
1962 count--;
1963 }
1964 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1965 {
1966 if (count == 0)
1967 return &pos;
1968 }
1969 else if (STRNCMP(ptr, "endif", 5) == 0)
1970 count++;
1971 }
1972 }
1973 return NULL;
1974 }
1975 }
1976
1977#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00001978 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 * paren/brace in the other direction. */
1980 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1981 backwards = !backwards;
1982#endif
1983
1984 do_quotes = -1;
1985 start_in_quotes = MAYBE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001986 clearpos(&match_pos);
1987
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001989 if ((backwards && comment_dir)
1990#ifdef FEAT_LISP
1991 || lisp
1992#endif
1993 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001995#ifdef FEAT_LISP
1996 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1997 lispcomm = TRUE; /* find match inside this comment */
1998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 while (!got_int)
2000 {
2001 /*
2002 * Go to the next position, forward or backward. We could use
2003 * inc() and dec() here, but that is much slower
2004 */
2005 if (backwards)
2006 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002007#ifdef FEAT_LISP
2008 /* char to match is inside of comment, don't search outside */
2009 if (lispcomm && pos.col < (colnr_T)comment_col)
2010 break;
2011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (pos.col == 0) /* at start of line, go to prev. one */
2013 {
2014 if (pos.lnum == 1) /* start of file */
2015 break;
2016 --pos.lnum;
2017
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002018 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 break;
2020
2021 linep = ml_get(pos.lnum);
2022 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2023 do_quotes = -1;
2024 line_breakcheck();
2025
2026 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002027 if (comment_dir
2028#ifdef FEAT_LISP
2029 || lisp
2030#endif
2031 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002033#ifdef FEAT_LISP
2034 /* skip comment */
2035 if (lisp && comment_col != MAXCOL)
2036 pos.col = comment_col;
2037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 else
2040 {
2041 --pos.col;
2042#ifdef FEAT_MBYTE
2043 if (has_mbyte)
2044 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2045#endif
2046 }
2047 }
2048 else /* forward search */
2049 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002050 if (linep[pos.col] == NUL
2051 /* at end of line, go to next one */
2052#ifdef FEAT_LISP
2053 /* don't search for match in comment */
2054 || (lisp && comment_col != MAXCOL
2055 && pos.col == (colnr_T)comment_col)
2056#endif
2057 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002059 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2060#ifdef FEAT_LISP
2061 /* line is exhausted and comment with it,
2062 * don't search for match in code */
2063 || lispcomm
2064#endif
2065 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 break;
2067 ++pos.lnum;
2068
2069 if (maxtravel && traveled++ > maxtravel)
2070 break;
2071
2072 linep = ml_get(pos.lnum);
2073 pos.col = 0;
2074 do_quotes = -1;
2075 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002076#ifdef FEAT_LISP
2077 if (lisp) /* find comment pos in new line */
2078 comment_col = check_linecomment(linep);
2079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081 else
2082 {
2083#ifdef FEAT_MBYTE
2084 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002085 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 else
2087#endif
2088 ++pos.col;
2089 }
2090 }
2091
2092 /*
2093 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2094 */
2095 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2096 (linep[0] == '{' || linep[0] == '}'))
2097 {
2098 if (linep[0] == findc && count == 0) /* match! */
2099 return &pos;
2100 break; /* out of scope */
2101 }
2102
2103 if (comment_dir)
2104 {
2105 /* Note: comments do not nest, and we ignore quotes in them */
2106 /* TODO: ignore comment brackets inside strings */
2107 if (comment_dir == FORWARD)
2108 {
2109 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2110 {
2111 pos.col++;
2112 return &pos;
2113 }
2114 }
2115 else /* Searching backwards */
2116 {
2117 /*
2118 * A comment may contain / * or / /, it may also start or end
2119 * with / * /. Ignore a / * after / /.
2120 */
2121 if (pos.col == 0)
2122 continue;
2123 else if ( linep[pos.col - 1] == '/'
2124 && linep[pos.col] == '*'
2125 && (int)pos.col < comment_col)
2126 {
2127 count++;
2128 match_pos = pos;
2129 match_pos.col--;
2130 }
2131 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2132 {
2133 if (count > 0)
2134 pos = match_pos;
2135 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2136 && (int)pos.col <= comment_col)
2137 pos.col -= 2;
2138 else if (ignore_cend)
2139 continue;
2140 else
2141 return NULL;
2142 return &pos;
2143 }
2144 }
2145 continue;
2146 }
2147
2148 /*
2149 * If smart matching ('cpoptions' does not contain '%'), braces inside
2150 * of quotes are ignored, but only if there is an even number of
2151 * quotes in the line.
2152 */
2153 if (cpo_match)
2154 do_quotes = 0;
2155 else if (do_quotes == -1)
2156 {
2157 /*
2158 * Count the number of quotes in the line, skipping \" and '"'.
2159 * Watch out for "\\".
2160 */
2161 at_start = do_quotes;
2162 for (ptr = linep; *ptr; ++ptr)
2163 {
2164 if (ptr == linep + pos.col + backwards)
2165 at_start = (do_quotes & 1);
2166 if (*ptr == '"'
2167 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2168 ++do_quotes;
2169 if (*ptr == '\\' && ptr[1] != NUL)
2170 ++ptr;
2171 }
2172 do_quotes &= 1; /* result is 1 with even number of quotes */
2173
2174 /*
2175 * If we find an uneven count, check current line and previous
2176 * one for a '\' at the end.
2177 */
2178 if (!do_quotes)
2179 {
2180 inquote = FALSE;
2181 if (ptr[-1] == '\\')
2182 {
2183 do_quotes = 1;
2184 if (start_in_quotes == MAYBE)
2185 {
2186 /* Do we need to use at_start here? */
2187 inquote = TRUE;
2188 start_in_quotes = TRUE;
2189 }
2190 else if (backwards)
2191 inquote = TRUE;
2192 }
2193 if (pos.lnum > 1)
2194 {
2195 ptr = ml_get(pos.lnum - 1);
2196 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2197 {
2198 do_quotes = 1;
2199 if (start_in_quotes == MAYBE)
2200 {
2201 inquote = at_start;
2202 if (inquote)
2203 start_in_quotes = TRUE;
2204 }
2205 else if (!backwards)
2206 inquote = TRUE;
2207 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002208
2209 /* ml_get() only keeps one line, need to get linep again */
2210 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212 }
2213 }
2214 if (start_in_quotes == MAYBE)
2215 start_in_quotes = FALSE;
2216
2217 /*
2218 * If 'smartmatch' is set:
2219 * Things inside quotes are ignored by setting 'inquote'. If we
2220 * find a quote without a preceding '\' invert 'inquote'. At the
2221 * end of a line not ending in '\' we reset 'inquote'.
2222 *
2223 * In lines with an uneven number of quotes (without preceding '\')
2224 * we do not know which part to ignore. Therefore we only set
2225 * inquote if the number of quotes in a line is even, unless this
2226 * line or the previous one ends in a '\'. Complicated, isn't it?
2227 */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002228 c = PTR2CHAR(linep + pos.col);
2229 switch (c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
2231 case NUL:
2232 /* at end of line without trailing backslash, reset inquote */
2233 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2234 {
2235 inquote = FALSE;
2236 start_in_quotes = FALSE;
2237 }
2238 break;
2239
2240 case '"':
2241 /* a quote that is preceded with an odd number of backslashes is
2242 * ignored */
2243 if (do_quotes)
2244 {
2245 int col;
2246
2247 for (col = pos.col - 1; col >= 0; --col)
2248 if (linep[col] != '\\')
2249 break;
2250 if ((((int)pos.col - 1 - col) & 1) == 0)
2251 {
2252 inquote = !inquote;
2253 start_in_quotes = FALSE;
2254 }
2255 }
2256 break;
2257
2258 /*
2259 * If smart matching ('cpoptions' does not contain '%'):
2260 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2261 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2262 * skipped, there is never a brace in them.
2263 * Ignore this when finding matches for `'.
2264 */
2265 case '\'':
2266 if (!cpo_match && initc != '\'' && findc != '\'')
2267 {
2268 if (backwards)
2269 {
2270 if (pos.col > 1)
2271 {
2272 if (linep[pos.col - 2] == '\'')
2273 {
2274 pos.col -= 2;
2275 break;
2276 }
2277 else if (linep[pos.col - 2] == '\\' &&
2278 pos.col > 2 && linep[pos.col - 3] == '\'')
2279 {
2280 pos.col -= 3;
2281 break;
2282 }
2283 }
2284 }
2285 else if (linep[pos.col + 1]) /* forward search */
2286 {
2287 if (linep[pos.col + 1] == '\\' &&
2288 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2289 {
2290 pos.col += 3;
2291 break;
2292 }
2293 else if (linep[pos.col + 2] == '\'')
2294 {
2295 pos.col += 2;
2296 break;
2297 }
2298 }
2299 }
2300 /* FALLTHROUGH */
2301
2302 default:
2303#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002304 /*
2305 * For Lisp skip over backslashed (), {} and [].
2306 * (actually, we skip #\( et al)
2307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (curbuf->b_p_lisp
2309 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002310 && pos.col > 1
2311 && check_prevcol(linep, pos.col, '\\', NULL)
2312 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 break;
2314#endif
2315
2316 /* Check for match outside of quotes, and inside of
2317 * quotes when the start is also inside of quotes. */
2318 if ((!inquote || start_in_quotes == TRUE)
2319 && (c == initc || c == findc))
2320 {
2321 int col, bslcnt = 0;
2322
2323 if (!cpo_bsl)
2324 {
2325 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2326 bslcnt++;
2327 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00002328 /* Only accept a match when 'M' is in 'cpo' or when escaping
2329 * is what we expect. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2331 {
2332 if (c == initc)
2333 count++;
2334 else
2335 {
2336 if (count == 0)
2337 return &pos;
2338 count--;
2339 }
2340 }
2341 }
2342 }
2343 }
2344
2345 if (comment_dir == BACKWARD && count > 0)
2346 {
2347 pos = match_pos;
2348 return &pos;
2349 }
2350 return (pos_T *)NULL; /* never found it */
2351}
2352
2353/*
2354 * Check if line[] contains a / / comment.
2355 * Return MAXCOL if not, otherwise return the column.
2356 * TODO: skip strings.
2357 */
2358 static int
2359check_linecomment(line)
2360 char_u *line;
2361{
2362 char_u *p;
2363
2364 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002365#ifdef FEAT_LISP
2366 /* skip Lispish one-line comments */
2367 if (curbuf->b_p_lisp)
2368 {
2369 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2370 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002371 int in_str = FALSE; /* inside of string */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002372
2373 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002374 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002375 {
2376 if (*p == '"')
2377 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002378 if (in_str)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002379 {
2380 if (*(p - 1) != '\\') /* skip escaped quote */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002381 in_str = FALSE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002382 }
2383 else if (p == line || ((p - line) >= 2
2384 /* skip #\" form */
2385 && *(p - 1) != '\\' && *(p - 2) != '#'))
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002386 in_str = TRUE;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002387 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +01002388 else if (!in_str && ((p - line) < 2
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002389 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2390 break; /* found! */
2391 ++p;
2392 }
2393 }
2394 else
2395 p = NULL;
2396 }
2397 else
2398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 while ((p = vim_strchr(p, '/')) != NULL)
2400 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002401 /* accept a double /, unless it's preceded with * and followed by *,
2402 * because * / / * is an end and start of a C comment */
2403 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 break;
2405 ++p;
2406 }
2407
2408 if (p == NULL)
2409 return MAXCOL;
2410 return (int)(p - line);
2411}
2412
2413/*
2414 * Move cursor briefly to character matching the one under the cursor.
2415 * Used for Insert mode and "r" command.
2416 * Show the match only if it is visible on the screen.
2417 * If there isn't a match, then beep.
2418 */
2419 void
2420showmatch(c)
2421 int c; /* char to show match for */
2422{
2423 pos_T *lpos, save_cursor;
2424 pos_T mpos;
2425 colnr_T vcol;
2426 long save_so;
2427 long save_siso;
2428#ifdef CURSOR_SHAPE
2429 int save_state;
2430#endif
2431 colnr_T save_dollar_vcol;
2432 char_u *p;
2433
2434 /*
2435 * Only show match for chars in the 'matchpairs' option.
2436 */
2437 /* 'matchpairs' is "x:y,x:y" */
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002438 for (p = curbuf->b_p_mps; *p != NUL; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440#ifdef FEAT_RIGHTLEFT
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002441 if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri))
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002442 break;
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002443#endif
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002444 p += MB_PTR2LEN(p) + 1;
2445 if (PTR2CHAR(p) == c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446#ifdef FEAT_RIGHTLEFT
2447 && !(curwin->w_p_rl ^ p_ri)
2448#endif
2449 )
2450 break;
Bram Moolenaar8c7694a2013-01-17 17:02:05 +01002451 p += MB_PTR2LEN(p);
2452 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return;
2454 }
2455
2456 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2457 vim_beep();
Bram Moolenaar187d3ac2013-02-20 18:39:13 +01002458 else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 if (!curwin->w_p_wrap)
2461 getvcol(curwin, lpos, NULL, &vcol, NULL);
2462 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2463 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2464 {
2465 mpos = *lpos; /* save the pos, update_screen() may change it */
2466 save_cursor = curwin->w_cursor;
2467 save_so = p_so;
2468 save_siso = p_siso;
2469 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2470 * stop displaying the "$". */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01002471 if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol)
2472 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 ++curwin->w_virtcol; /* do display ')' just before "$" */
2474 update_screen(VALID); /* show the new char first */
2475
2476 save_dollar_vcol = dollar_vcol;
2477#ifdef CURSOR_SHAPE
2478 save_state = State;
2479 State = SHOWMATCH;
2480 ui_cursor_shape(); /* may show different cursor shape */
2481#endif
2482 curwin->w_cursor = mpos; /* move to matching char */
2483 p_so = 0; /* don't use 'scrolloff' here */
2484 p_siso = 0; /* don't use 'sidescrolloff' here */
2485 showruler(FALSE);
2486 setcursor();
2487 cursor_on(); /* make sure that the cursor is shown */
2488 out_flush();
2489#ifdef FEAT_GUI
2490 if (gui.in_use)
2491 {
2492 gui_update_cursor(TRUE, FALSE);
2493 gui_mch_flush();
2494 }
2495#endif
2496 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2497 * which resets it if the matching position is in a previous line
2498 * and has a higher column number. */
2499 dollar_vcol = save_dollar_vcol;
2500
2501 /*
2502 * brief pause, unless 'm' is present in 'cpo' and a character is
2503 * available.
2504 */
2505 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2506 ui_delay(p_mat * 100L, TRUE);
2507 else if (!char_avail())
2508 ui_delay(p_mat * 100L, FALSE);
2509 curwin->w_cursor = save_cursor; /* restore cursor position */
2510 p_so = save_so;
2511 p_siso = save_siso;
2512#ifdef CURSOR_SHAPE
2513 State = save_state;
2514 ui_cursor_shape(); /* may show different cursor shape */
2515#endif
2516 }
2517 }
2518}
2519
2520/*
2521 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002522 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 * space or a line break. Also stop at an empty line.
2524 * Return OK if the next sentence was found.
2525 */
2526 int
2527findsent(dir, count)
2528 int dir;
2529 long count;
2530{
2531 pos_T pos, tpos;
2532 int c;
2533 int (*func) __ARGS((pos_T *));
2534 int startlnum;
2535 int noskip = FALSE; /* do not skip blanks */
2536 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002537 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539 pos = curwin->w_cursor;
2540 if (dir == FORWARD)
2541 func = incl;
2542 else
2543 func = decl;
2544
2545 while (count--)
2546 {
2547 /*
2548 * if on an empty line, skip upto a non-empty line
2549 */
2550 if (gchar_pos(&pos) == NUL)
2551 {
2552 do
2553 if ((*func)(&pos) == -1)
2554 break;
2555 while (gchar_pos(&pos) == NUL);
2556 if (dir == FORWARD)
2557 goto found;
2558 }
2559 /*
2560 * if on the start of a paragraph or a section and searching forward,
2561 * go to the next line
2562 */
2563 else if (dir == FORWARD && pos.col == 0 &&
2564 startPS(pos.lnum, NUL, FALSE))
2565 {
2566 if (pos.lnum == curbuf->b_ml.ml_line_count)
2567 return FAIL;
2568 ++pos.lnum;
2569 goto found;
2570 }
2571 else if (dir == BACKWARD)
2572 decl(&pos);
2573
2574 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002575 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2577 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2578 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002579 if (vim_strchr((char_u *)".!?", c) != NULL)
2580 {
2581 /* Only skip over a '.', '!' and '?' once. */
2582 if (found_dot)
2583 break;
2584 found_dot = TRUE;
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (decl(&pos) == -1)
2587 break;
2588 /* when going forward: Stop in front of empty line */
2589 if (lineempty(pos.lnum) && dir == FORWARD)
2590 {
2591 incl(&pos);
2592 goto found;
2593 }
2594 }
2595
2596 /* remember the line where the search started */
2597 startlnum = pos.lnum;
2598 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2599
2600 for (;;) /* find end of sentence */
2601 {
2602 c = gchar_pos(&pos);
2603 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2604 {
2605 if (dir == BACKWARD && pos.lnum != startlnum)
2606 ++pos.lnum;
2607 break;
2608 }
2609 if (c == '.' || c == '!' || c == '?')
2610 {
2611 tpos = pos;
2612 do
2613 if ((c = inc(&tpos)) == -1)
2614 break;
2615 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2616 != NULL);
2617 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2618 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2619 && gchar_pos(&tpos) == ' ')))
2620 {
2621 pos = tpos;
2622 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2623 inc(&pos);
2624 break;
2625 }
2626 }
2627 if ((*func)(&pos) == -1)
2628 {
2629 if (count)
2630 return FAIL;
2631 noskip = TRUE;
2632 break;
2633 }
2634 }
2635found:
2636 /* skip white space */
2637 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2638 if (incl(&pos) == -1)
2639 break;
2640 }
2641
2642 setpcmark();
2643 curwin->w_cursor = pos;
2644 return OK;
2645}
2646
2647/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002648 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002650 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 * If 'what' is '{' or '}' we go to the next section.
2652 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002653 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 */
2655 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002656findpar(pincl, dir, count, what, both)
2657 int *pincl; /* Return: TRUE if last char is to be included */
2658 int dir;
2659 long count;
2660 int what;
2661 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663 linenr_T curr;
2664 int did_skip; /* TRUE after separating lines have been skipped */
2665 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002666 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667#ifdef FEAT_FOLDING
2668 linenr_T fold_first; /* first line of a closed fold */
2669 linenr_T fold_last; /* last line of a closed fold */
2670 int fold_skipped; /* TRUE if a closed fold was skipped this
2671 iteration */
2672#endif
2673
2674 curr = curwin->w_cursor.lnum;
2675
2676 while (count--)
2677 {
2678 did_skip = FALSE;
2679 for (first = TRUE; ; first = FALSE)
2680 {
2681 if (*ml_get(curr) != NUL)
2682 did_skip = TRUE;
2683
2684#ifdef FEAT_FOLDING
2685 /* skip folded lines */
2686 fold_skipped = FALSE;
2687 if (first && hasFolding(curr, &fold_first, &fold_last))
2688 {
2689 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2690 fold_skipped = TRUE;
2691 }
2692#endif
2693
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002694 /* POSIX has it's own ideas of what a paragraph boundary is and it
2695 * doesn't match historical Vi: It also stops at a "{" in the
2696 * first column and at an empty line. */
2697 if (!first && did_skip && (startPS(curr, what, both)
2698 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 break;
2700
2701#ifdef FEAT_FOLDING
2702 if (fold_skipped)
2703 curr -= dir;
2704#endif
2705 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2706 {
2707 if (count)
2708 return FALSE;
2709 curr -= dir;
2710 break;
2711 }
2712 }
2713 }
2714 setpcmark();
2715 if (both && *ml_get(curr) == '}') /* include line with '}' */
2716 ++curr;
2717 curwin->w_cursor.lnum = curr;
2718 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2719 {
2720 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2721 {
2722 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002723 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 }
2725 }
2726 else
2727 curwin->w_cursor.col = 0;
2728 return TRUE;
2729}
2730
2731/*
2732 * check if the string 's' is a nroff macro that is in option 'opt'
2733 */
2734 static int
2735inmacro(opt, s)
2736 char_u *opt;
2737 char_u *s;
2738{
2739 char_u *macro;
2740
2741 for (macro = opt; macro[0]; ++macro)
2742 {
2743 /* Accept two characters in the option being equal to two characters
2744 * in the line. A space in the option matches with a space in the
2745 * line or the line having ended. */
2746 if ( (macro[0] == s[0]
2747 || (macro[0] == ' '
2748 && (s[0] == NUL || s[0] == ' ')))
2749 && (macro[1] == s[1]
2750 || ((macro[1] == NUL || macro[1] == ' ')
2751 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2752 break;
2753 ++macro;
2754 if (macro[0] == NUL)
2755 break;
2756 }
2757 return (macro[0] != NUL);
2758}
2759
2760/*
2761 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2762 * If 'para' is '{' or '}' only check for sections.
2763 * If 'both' is TRUE also stop at '}'
2764 */
2765 int
2766startPS(lnum, para, both)
2767 linenr_T lnum;
2768 int para;
2769 int both;
2770{
2771 char_u *s;
2772
2773 s = ml_get(lnum);
2774 if (*s == para || *s == '\f' || (both && *s == '}'))
2775 return TRUE;
2776 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2777 (!para && inmacro(p_para, s + 1))))
2778 return TRUE;
2779 return FALSE;
2780}
2781
2782/*
2783 * The following routines do the word searches performed by the 'w', 'W',
2784 * 'b', 'B', 'e', and 'E' commands.
2785 */
2786
2787/*
2788 * To perform these searches, characters are placed into one of three
2789 * classes, and transitions between classes determine word boundaries.
2790 *
2791 * The classes are:
2792 *
2793 * 0 - white space
2794 * 1 - punctuation
2795 * 2 or higher - keyword characters (letters, digits and underscore)
2796 */
2797
2798static int cls_bigword; /* TRUE for "W", "B" or "E" */
2799
2800/*
2801 * cls() - returns the class of character at curwin->w_cursor
2802 *
2803 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2804 * from class 2 and higher are reported as class 1 since only white space
2805 * boundaries are of interest.
2806 */
2807 static int
2808cls()
2809{
2810 int c;
2811
2812 c = gchar_cursor();
2813#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2814 if (p_altkeymap && c == F_BLANK)
2815 return 0;
2816#endif
2817 if (c == ' ' || c == '\t' || c == NUL)
2818 return 0;
2819#ifdef FEAT_MBYTE
2820 if (enc_dbcs != 0 && c > 0xFF)
2821 {
2822 /* If cls_bigword, report multi-byte chars as class 1. */
2823 if (enc_dbcs == DBCS_KOR && cls_bigword)
2824 return 1;
2825
2826 /* process code leading/trailing bytes */
2827 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2828 }
2829 if (enc_utf8)
2830 {
2831 c = utf_class(c);
2832 if (c != 0 && cls_bigword)
2833 return 1;
2834 return c;
2835 }
2836#endif
2837
2838 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2839 if (cls_bigword)
2840 return 1;
2841
2842 if (vim_iswordc(c))
2843 return 2;
2844 return 1;
2845}
2846
2847
2848/*
2849 * fwd_word(count, type, eol) - move forward one word
2850 *
2851 * Returns FAIL if the cursor was already at the end of the file.
2852 * If eol is TRUE, last word stops at end of line (for operators).
2853 */
2854 int
2855fwd_word(count, bigword, eol)
2856 long count;
2857 int bigword; /* "W", "E" or "B" */
2858 int eol;
2859{
2860 int sclass; /* starting class */
2861 int i;
2862 int last_line;
2863
2864#ifdef FEAT_VIRTUALEDIT
2865 curwin->w_cursor.coladd = 0;
2866#endif
2867 cls_bigword = bigword;
2868 while (--count >= 0)
2869 {
2870#ifdef FEAT_FOLDING
2871 /* When inside a range of folded lines, move to the last char of the
2872 * last line. */
2873 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2874 coladvance((colnr_T)MAXCOL);
2875#endif
2876 sclass = cls();
2877
2878 /*
2879 * We always move at least one character, unless on the last
2880 * character in the buffer.
2881 */
2882 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2883 i = inc_cursor();
2884 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2885 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00002886 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 return OK;
2888
2889 /*
2890 * Go one char past end of current word (if any)
2891 */
2892 if (sclass != 0)
2893 while (cls() == sclass)
2894 {
2895 i = inc_cursor();
2896 if (i == -1 || (i >= 1 && eol && count == 0))
2897 return OK;
2898 }
2899
2900 /*
2901 * go to next non-white
2902 */
2903 while (cls() == 0)
2904 {
2905 /*
2906 * We'll stop if we land on a blank line
2907 */
2908 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2909 break;
2910
2911 i = inc_cursor();
2912 if (i == -1 || (i >= 1 && eol && count == 0))
2913 return OK;
2914 }
2915 }
2916 return OK;
2917}
2918
2919/*
2920 * bck_word() - move backward 'count' words
2921 *
2922 * If stop is TRUE and we are already on the start of a word, move one less.
2923 *
2924 * Returns FAIL if top of the file was reached.
2925 */
2926 int
2927bck_word(count, bigword, stop)
2928 long count;
2929 int bigword;
2930 int stop;
2931{
2932 int sclass; /* starting class */
2933
2934#ifdef FEAT_VIRTUALEDIT
2935 curwin->w_cursor.coladd = 0;
2936#endif
2937 cls_bigword = bigword;
2938 while (--count >= 0)
2939 {
2940#ifdef FEAT_FOLDING
2941 /* When inside a range of folded lines, move to the first char of the
2942 * first line. */
2943 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2944 curwin->w_cursor.col = 0;
2945#endif
2946 sclass = cls();
2947 if (dec_cursor() == -1) /* started at start of file */
2948 return FAIL;
2949
2950 if (!stop || sclass == cls() || sclass == 0)
2951 {
2952 /*
2953 * Skip white space before the word.
2954 * Stop on an empty line.
2955 */
2956 while (cls() == 0)
2957 {
2958 if (curwin->w_cursor.col == 0
2959 && lineempty(curwin->w_cursor.lnum))
2960 goto finished;
2961 if (dec_cursor() == -1) /* hit start of file, stop here */
2962 return OK;
2963 }
2964
2965 /*
2966 * Move backward to start of this word.
2967 */
2968 if (skip_chars(cls(), BACKWARD))
2969 return OK;
2970 }
2971
2972 inc_cursor(); /* overshot - forward one */
2973finished:
2974 stop = FALSE;
2975 }
2976 return OK;
2977}
2978
2979/*
2980 * end_word() - move to the end of the word
2981 *
2982 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2983 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2984 * motion crosses blank lines. When the real vi crosses a blank line in an
2985 * 'e' motion, the cursor is placed on the FIRST character of the next
2986 * non-blank line. The 'E' command, however, works correctly. Since this
2987 * appears to be a bug, I have not duplicated it here.
2988 *
2989 * Returns FAIL if end of the file was reached.
2990 *
2991 * If stop is TRUE and we are already on the end of a word, move one less.
2992 * If empty is TRUE stop on an empty line.
2993 */
2994 int
2995end_word(count, bigword, stop, empty)
2996 long count;
2997 int bigword;
2998 int stop;
2999 int empty;
3000{
3001 int sclass; /* starting class */
3002
3003#ifdef FEAT_VIRTUALEDIT
3004 curwin->w_cursor.coladd = 0;
3005#endif
3006 cls_bigword = bigword;
3007 while (--count >= 0)
3008 {
3009#ifdef FEAT_FOLDING
3010 /* When inside a range of folded lines, move to the last char of the
3011 * last line. */
3012 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
3013 coladvance((colnr_T)MAXCOL);
3014#endif
3015 sclass = cls();
3016 if (inc_cursor() == -1)
3017 return FAIL;
3018
3019 /*
3020 * If we're in the middle of a word, we just have to move to the end
3021 * of it.
3022 */
3023 if (cls() == sclass && sclass != 0)
3024 {
3025 /*
3026 * Move forward to end of the current word
3027 */
3028 if (skip_chars(sclass, FORWARD))
3029 return FAIL;
3030 }
3031 else if (!stop || sclass == 0)
3032 {
3033 /*
3034 * We were at the end of a word. Go to the end of the next word.
3035 * First skip white space, if 'empty' is TRUE, stop at empty line.
3036 */
3037 while (cls() == 0)
3038 {
3039 if (empty && curwin->w_cursor.col == 0
3040 && lineempty(curwin->w_cursor.lnum))
3041 goto finished;
3042 if (inc_cursor() == -1) /* hit end of file, stop here */
3043 return FAIL;
3044 }
3045
3046 /*
3047 * Move forward to the end of this word.
3048 */
3049 if (skip_chars(cls(), FORWARD))
3050 return FAIL;
3051 }
3052 dec_cursor(); /* overshot - one char backward */
3053finished:
3054 stop = FALSE; /* we move only one word less */
3055 }
3056 return OK;
3057}
3058
3059/*
3060 * Move back to the end of the word.
3061 *
3062 * Returns FAIL if start of the file was reached.
3063 */
3064 int
3065bckend_word(count, bigword, eol)
3066 long count;
3067 int bigword; /* TRUE for "B" */
3068 int eol; /* TRUE: stop at end of line. */
3069{
3070 int sclass; /* starting class */
3071 int i;
3072
3073#ifdef FEAT_VIRTUALEDIT
3074 curwin->w_cursor.coladd = 0;
3075#endif
3076 cls_bigword = bigword;
3077 while (--count >= 0)
3078 {
3079 sclass = cls();
3080 if ((i = dec_cursor()) == -1)
3081 return FAIL;
3082 if (eol && i == 1)
3083 return OK;
3084
3085 /*
3086 * Move backward to before the start of this word.
3087 */
3088 if (sclass != 0)
3089 {
3090 while (cls() == sclass)
3091 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3092 return OK;
3093 }
3094
3095 /*
3096 * Move backward to end of the previous word
3097 */
3098 while (cls() == 0)
3099 {
3100 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3101 break;
3102 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3103 return OK;
3104 }
3105 }
3106 return OK;
3107}
3108
3109/*
3110 * Skip a row of characters of the same class.
3111 * Return TRUE when end-of-file reached, FALSE otherwise.
3112 */
3113 static int
3114skip_chars(cclass, dir)
3115 int cclass;
3116 int dir;
3117{
3118 while (cls() == cclass)
3119 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3120 return TRUE;
3121 return FALSE;
3122}
3123
3124#ifdef FEAT_TEXTOBJ
3125/*
3126 * Go back to the start of the word or the start of white space
3127 */
3128 static void
3129back_in_line()
3130{
3131 int sclass; /* starting class */
3132
3133 sclass = cls();
3134 for (;;)
3135 {
3136 if (curwin->w_cursor.col == 0) /* stop at start of line */
3137 break;
3138 dec_cursor();
3139 if (cls() != sclass) /* stop at start of word */
3140 {
3141 inc_cursor();
3142 break;
3143 }
3144 }
3145}
3146
3147 static void
3148find_first_blank(posp)
3149 pos_T *posp;
3150{
3151 int c;
3152
3153 while (decl(posp) != -1)
3154 {
3155 c = gchar_pos(posp);
3156 if (!vim_iswhite(c))
3157 {
3158 incl(posp);
3159 break;
3160 }
3161 }
3162}
3163
3164/*
3165 * Skip count/2 sentences and count/2 separating white spaces.
3166 */
3167 static void
3168findsent_forward(count, at_start_sent)
3169 long count;
3170 int at_start_sent; /* cursor is at start of sentence */
3171{
3172 while (count--)
3173 {
3174 findsent(FORWARD, 1L);
3175 if (at_start_sent)
3176 find_first_blank(&curwin->w_cursor);
3177 if (count == 0 || at_start_sent)
3178 decl(&curwin->w_cursor);
3179 at_start_sent = !at_start_sent;
3180 }
3181}
3182
3183/*
3184 * Find word under cursor, cursor at end.
3185 * Used while an operator is pending, and in Visual mode.
3186 */
3187 int
3188current_word(oap, count, include, bigword)
3189 oparg_T *oap;
3190 long count;
3191 int include; /* TRUE: include word and white space */
3192 int bigword; /* FALSE == word, TRUE == WORD */
3193{
3194 pos_T start_pos;
3195 pos_T pos;
3196 int inclusive = TRUE;
3197 int include_white = FALSE;
3198
3199 cls_bigword = bigword;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003200 clearpos(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 /* Correct cursor when 'selection' is exclusive */
3203 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3204 dec_cursor();
3205
3206 /*
3207 * When Visual mode is not active, or when the VIsual area is only one
3208 * character, select the word and/or white space under the cursor.
3209 */
3210 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 /*
3213 * Go to start of current word or white space.
3214 */
3215 back_in_line();
3216 start_pos = curwin->w_cursor;
3217
3218 /*
3219 * If the start is on white space, and white space should be included
3220 * (" word"), or start is not on white space, and white space should
3221 * not be included ("word"), find end of word.
3222 */
3223 if ((cls() == 0) == include)
3224 {
3225 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3226 return FAIL;
3227 }
3228 else
3229 {
3230 /*
3231 * If the start is not on white space, and white space should be
3232 * included ("word "), or start is on white space and white
3233 * space should not be included (" "), find start of word.
3234 * If we end up in the first column of the next line (single char
3235 * word) back up to end of the line.
3236 */
3237 fwd_word(1L, bigword, TRUE);
3238 if (curwin->w_cursor.col == 0)
3239 decl(&curwin->w_cursor);
3240 else
3241 oneleft();
3242
3243 if (include)
3244 include_white = TRUE;
3245 }
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 if (VIsual_active)
3248 {
3249 /* should do something when inclusive == FALSE ! */
3250 VIsual = start_pos;
3251 redraw_curbuf_later(INVERTED); /* update the inversion */
3252 }
3253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
3255 oap->start = start_pos;
3256 oap->motion_type = MCHAR;
3257 }
3258 --count;
3259 }
3260
3261 /*
3262 * When count is still > 0, extend with more objects.
3263 */
3264 while (count > 0)
3265 {
3266 inclusive = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3268 {
3269 /*
3270 * In Visual mode, with cursor at start: move cursor back.
3271 */
3272 if (decl(&curwin->w_cursor) == -1)
3273 return FAIL;
3274 if (include != (cls() != 0))
3275 {
3276 if (bck_word(1L, bigword, TRUE) == FAIL)
3277 return FAIL;
3278 }
3279 else
3280 {
3281 if (bckend_word(1L, bigword, TRUE) == FAIL)
3282 return FAIL;
3283 (void)incl(&curwin->w_cursor);
3284 }
3285 }
3286 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 /*
3289 * Move cursor forward one word and/or white area.
3290 */
3291 if (incl(&curwin->w_cursor) == -1)
3292 return FAIL;
3293 if (include != (cls() == 0))
3294 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003295 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 return FAIL;
3297 /*
3298 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003299 * the first character on the line.
3300 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003302 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 inclusive = FALSE;
3304 }
3305 else
3306 {
3307 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3308 return FAIL;
3309 }
3310 }
3311 --count;
3312 }
3313
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003314 if (include_white && (cls() != 0
3315 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 /*
3318 * If we don't include white space at the end, move the start
3319 * to include some white space there. This makes "daw" work
3320 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003321 * word). Also when "2daw" deletes "word." at the end of the line
3322 * (cursor is at start of next line).
3323 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
3325 pos = curwin->w_cursor; /* save cursor position */
3326 curwin->w_cursor = start_pos;
3327 if (oneleft() == OK)
3328 {
3329 back_in_line();
3330 if (cls() == 0 && curwin->w_cursor.col > 0)
3331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (VIsual_active)
3333 VIsual = curwin->w_cursor;
3334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 oap->start = curwin->w_cursor;
3336 }
3337 }
3338 curwin->w_cursor = pos; /* put cursor back at end */
3339 }
3340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 if (VIsual_active)
3342 {
3343 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3344 inc_cursor();
3345 if (VIsual_mode == 'V')
3346 {
3347 VIsual_mode = 'v';
3348 redraw_cmdline = TRUE; /* show mode later */
3349 }
3350 }
3351 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 oap->inclusive = inclusive;
3353
3354 return OK;
3355}
3356
3357/*
3358 * Find sentence(s) under the cursor, cursor at end.
3359 * When Visual active, extend it by one or more sentences.
3360 */
3361 int
3362current_sent(oap, count, include)
3363 oparg_T *oap;
3364 long count;
3365 int include;
3366{
3367 pos_T start_pos;
3368 pos_T pos;
3369 int start_blank;
3370 int c;
3371 int at_start_sent;
3372 long ncount;
3373
3374 start_pos = curwin->w_cursor;
3375 pos = start_pos;
3376 findsent(FORWARD, 1L); /* Find start of next sentence. */
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003379 * When the Visual area is bigger than one character: Extend it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
3381 if (VIsual_active && !equalpos(start_pos, VIsual))
3382 {
3383extend:
3384 if (lt(start_pos, VIsual))
3385 {
3386 /*
3387 * Cursor at start of Visual area.
3388 * Find out where we are:
3389 * - in the white space before a sentence
3390 * - in a sentence or just after it
3391 * - at the start of a sentence
3392 */
3393 at_start_sent = TRUE;
3394 decl(&pos);
3395 while (lt(pos, curwin->w_cursor))
3396 {
3397 c = gchar_pos(&pos);
3398 if (!vim_iswhite(c))
3399 {
3400 at_start_sent = FALSE;
3401 break;
3402 }
3403 incl(&pos);
3404 }
3405 if (!at_start_sent)
3406 {
3407 findsent(BACKWARD, 1L);
3408 if (equalpos(curwin->w_cursor, start_pos))
3409 at_start_sent = TRUE; /* exactly at start of sentence */
3410 else
3411 /* inside a sentence, go to its end (start of next) */
3412 findsent(FORWARD, 1L);
3413 }
3414 if (include) /* "as" gets twice as much as "is" */
3415 count *= 2;
3416 while (count--)
3417 {
3418 if (at_start_sent)
3419 find_first_blank(&curwin->w_cursor);
3420 c = gchar_cursor();
3421 if (!at_start_sent || (!include && !vim_iswhite(c)))
3422 findsent(BACKWARD, 1L);
3423 at_start_sent = !at_start_sent;
3424 }
3425 }
3426 else
3427 {
3428 /*
3429 * Cursor at end of Visual area.
3430 * Find out where we are:
3431 * - just before a sentence
3432 * - just before or in the white space before a sentence
3433 * - in a sentence
3434 */
3435 incl(&pos);
3436 at_start_sent = TRUE;
3437 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3438 {
3439 at_start_sent = FALSE;
3440 while (lt(pos, curwin->w_cursor))
3441 {
3442 c = gchar_pos(&pos);
3443 if (!vim_iswhite(c))
3444 {
3445 at_start_sent = TRUE;
3446 break;
3447 }
3448 incl(&pos);
3449 }
3450 if (at_start_sent) /* in the sentence */
3451 findsent(BACKWARD, 1L);
3452 else /* in/before white before a sentence */
3453 curwin->w_cursor = start_pos;
3454 }
3455
3456 if (include) /* "as" gets twice as much as "is" */
3457 count *= 2;
3458 findsent_forward(count, at_start_sent);
3459 if (*p_sel == 'e')
3460 ++curwin->w_cursor.col;
3461 }
3462 return OK;
3463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /*
Bram Moolenaar641e2862012-07-25 15:06:34 +02003466 * If the cursor started on a blank, check if it is just before the start
3467 * of the next sentence.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
3469 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3470 incl(&pos);
3471 if (equalpos(pos, curwin->w_cursor))
3472 {
3473 start_blank = TRUE;
3474 find_first_blank(&start_pos); /* go back to first blank */
3475 }
3476 else
3477 {
3478 start_blank = FALSE;
3479 findsent(BACKWARD, 1L);
3480 start_pos = curwin->w_cursor;
3481 }
3482 if (include)
3483 ncount = count * 2;
3484 else
3485 {
3486 ncount = count;
3487 if (start_blank)
3488 --ncount;
3489 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003490 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 findsent_forward(ncount, TRUE);
3492 else
3493 decl(&curwin->w_cursor);
3494
3495 if (include)
3496 {
3497 /*
3498 * If the blank in front of the sentence is included, exclude the
3499 * blanks at the end of the sentence, go back to the first blank.
3500 * If there are no trailing blanks, try to include leading blanks.
3501 */
3502 if (start_blank)
3503 {
3504 find_first_blank(&curwin->w_cursor);
3505 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3506 if (vim_iswhite(c))
3507 decl(&curwin->w_cursor);
3508 }
3509 else if (c = gchar_cursor(), !vim_iswhite(c))
3510 find_first_blank(&start_pos);
3511 }
3512
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 if (VIsual_active)
3514 {
Bram Moolenaar641e2862012-07-25 15:06:34 +02003515 /* Avoid getting stuck with "is" on a single space before a sentence. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 if (equalpos(start_pos, curwin->w_cursor))
3517 goto extend;
3518 if (*p_sel == 'e')
3519 ++curwin->w_cursor.col;
3520 VIsual = start_pos;
3521 VIsual_mode = 'v';
3522 redraw_curbuf_later(INVERTED); /* update the inversion */
3523 }
3524 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 /* include a newline after the sentence, if there is one */
3527 if (incl(&curwin->w_cursor) == -1)
3528 oap->inclusive = TRUE;
3529 else
3530 oap->inclusive = FALSE;
3531 oap->start = start_pos;
3532 oap->motion_type = MCHAR;
3533 }
3534 return OK;
3535}
3536
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003537/*
3538 * Find block under the cursor, cursor at end.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003539 * "what" and "other" are two matching parenthesis/brace/etc.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 int
3542current_block(oap, count, include, what, other)
3543 oparg_T *oap;
3544 long count;
3545 int include; /* TRUE == include white space */
3546 int what; /* '(', '{', etc. */
3547 int other; /* ')', '}', etc. */
3548{
3549 pos_T old_pos;
3550 pos_T *pos = NULL;
3551 pos_T start_pos;
3552 pos_T *end_pos;
3553 pos_T old_start, old_end;
3554 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003555 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003558 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 old_start = old_end;
3560
3561 /*
3562 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003567 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 while (inindent(1))
3569 if (inc_cursor() != 0)
3570 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003571 if (gchar_cursor() == what)
3572 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 ++curwin->w_cursor.col;
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 else if (lt(VIsual, curwin->w_cursor))
3576 {
3577 old_start = VIsual;
3578 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3579 }
3580 else
3581 old_end = VIsual;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /*
3584 * Search backwards for unclosed '(', '{', etc..
3585 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003586 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 */
3588 save_cpo = p_cpo;
3589 p_cpo = (char_u *)"%";
3590 while (count-- > 0)
3591 {
3592 if ((pos = findmatch(NULL, what)) == NULL)
3593 break;
3594 curwin->w_cursor = *pos;
3595 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3596 }
3597 p_cpo = save_cpo;
3598
3599 /*
3600 * Search for matching ')', '}', etc.
3601 * Put this position in curwin->w_cursor.
3602 */
3603 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3604 {
3605 curwin->w_cursor = old_pos;
3606 return FAIL;
3607 }
3608 curwin->w_cursor = *end_pos;
3609
3610 /*
3611 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003612 * If the ending '}', ')' or ']' is only preceded by indent, skip that
3613 * indent. But only if the resulting area is not smaller than what we
3614 * started with.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 */
3616 while (!include)
3617 {
3618 incl(&start_pos);
3619 sol = (curwin->w_cursor.col == 0);
3620 decl(&curwin->w_cursor);
Bram Moolenaar7a54a902014-06-17 13:50:13 +02003621 while (inindent(1))
3622 {
3623 sol = TRUE;
3624 if (decl(&curwin->w_cursor) != 0)
3625 break;
3626 }
3627
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 /*
3629 * In Visual mode, when the resulting area is not bigger than what we
3630 * started with, extend it to the next block, and then exclude again.
3631 */
3632 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3633 && VIsual_active)
3634 {
3635 curwin->w_cursor = old_start;
3636 decl(&curwin->w_cursor);
3637 if ((pos = findmatch(NULL, what)) == NULL)
3638 {
3639 curwin->w_cursor = old_pos;
3640 return FAIL;
3641 }
3642 start_pos = *pos;
3643 curwin->w_cursor = *pos;
3644 if ((end_pos = findmatch(NULL, other)) == NULL)
3645 {
3646 curwin->w_cursor = old_pos;
3647 return FAIL;
3648 }
3649 curwin->w_cursor = *end_pos;
3650 }
3651 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 break;
3653 }
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 if (VIsual_active)
3656 {
3657 if (*p_sel == 'e')
3658 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003659 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 inc(&curwin->w_cursor); /* include the line break */
3661 VIsual = start_pos;
3662 VIsual_mode = 'v';
3663 redraw_curbuf_later(INVERTED); /* update the inversion */
3664 showmode();
3665 }
3666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
3668 oap->start = start_pos;
3669 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003670 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 incl(&curwin->w_cursor);
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003673 else if (ltoreq(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003674 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003676 else
3677 /* End is before the start (no text in between <>, [], etc.): don't
3678 * operate on any text. */
3679 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681
3682 return OK;
3683}
3684
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003685static int in_html_tag __ARGS((int));
3686
3687/*
3688 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3689 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3690 */
3691 static int
3692in_html_tag(end_tag)
3693 int end_tag;
3694{
3695 char_u *line = ml_get_curline();
3696 char_u *p;
3697 int c;
3698 int lc = NUL;
3699 pos_T pos;
3700
3701#ifdef FEAT_MBYTE
3702 if (enc_dbcs)
3703 {
3704 char_u *lp = NULL;
3705
3706 /* We search forward until the cursor, because searching backwards is
3707 * very slow for DBCS encodings. */
3708 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3709 if (*p == '>' || *p == '<')
3710 {
3711 lc = *p;
3712 lp = p;
3713 }
3714 if (*p != '<') /* check for '<' under cursor */
3715 {
3716 if (lc != '<')
3717 return FALSE;
3718 p = lp;
3719 }
3720 }
3721 else
3722#endif
3723 {
3724 for (p = line + curwin->w_cursor.col; p > line; )
3725 {
3726 if (*p == '<') /* find '<' under/before cursor */
3727 break;
3728 mb_ptr_back(line, p);
3729 if (*p == '>') /* find '>' before cursor */
3730 break;
3731 }
3732 if (*p != '<')
3733 return FALSE;
3734 }
3735
3736 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003737 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003738
3739 mb_ptr_adv(p);
3740 if (end_tag)
3741 /* check that there is a '/' after the '<' */
3742 return *p == '/';
3743
3744 /* check that there is no '/' after the '<' */
3745 if (*p == '/')
3746 return FALSE;
3747
3748 /* check that the matching '>' is not preceded by '/' */
3749 for (;;)
3750 {
3751 if (inc(&pos) < 0)
3752 return FALSE;
3753 c = *ml_get_pos(&pos);
3754 if (c == '>')
3755 break;
3756 lc = c;
3757 }
3758 return lc != '/';
3759}
3760
3761/*
3762 * Find tag block under the cursor, cursor at end.
3763 */
3764 int
3765current_tagblock(oap, count_arg, include)
3766 oparg_T *oap;
3767 long count_arg;
3768 int include; /* TRUE == include white space */
3769{
3770 long count = count_arg;
3771 long n;
3772 pos_T old_pos;
3773 pos_T start_pos;
3774 pos_T end_pos;
3775 pos_T old_start, old_end;
3776 char_u *spat, *epat;
3777 char_u *p;
3778 char_u *cp;
3779 int len;
3780 int r;
3781 int do_include = include;
3782 int save_p_ws = p_ws;
3783 int retval = FAIL;
3784
3785 p_ws = FALSE;
3786
3787 old_pos = curwin->w_cursor;
3788 old_end = curwin->w_cursor; /* remember where we started */
3789 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003790 if (!VIsual_active || *p_sel == 'e')
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003791 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003792
3793 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003794 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003795 */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003796 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003797 {
3798 setpcmark();
3799
3800 /* ignore indent */
3801 while (inindent(1))
3802 if (inc_cursor() != 0)
3803 break;
3804
3805 if (in_html_tag(FALSE))
3806 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003807 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003808 while (*ml_get_cursor() != '>')
3809 if (inc_cursor() < 0)
3810 break;
3811 }
3812 else if (in_html_tag(TRUE))
3813 {
3814 /* cursor on end tag, move to just before it */
3815 while (*ml_get_cursor() != '<')
3816 if (dec_cursor() < 0)
3817 break;
3818 dec_cursor();
3819 old_end = curwin->w_cursor;
3820 }
3821 }
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003822 else if (lt(VIsual, curwin->w_cursor))
3823 {
3824 old_start = VIsual;
3825 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3826 }
3827 else
3828 old_end = VIsual;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003829
3830again:
3831 /*
3832 * Search backwards for unclosed "<aaa>".
3833 * Put this position in start_pos.
3834 */
3835 for (n = 0; n < count; ++n)
3836 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003837 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003838 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003839 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00003840 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003841 {
3842 curwin->w_cursor = old_pos;
3843 goto theend;
3844 }
3845 }
3846 start_pos = curwin->w_cursor;
3847
3848 /*
3849 * Search for matching "</aaa>". First isolate the "aaa".
3850 */
3851 inc_cursor();
3852 p = ml_get_cursor();
3853 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3854 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003855 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003856 if (len == 0)
3857 {
3858 curwin->w_cursor = old_pos;
3859 goto theend;
3860 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01003861 spat = alloc(len + 31);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003862 epat = alloc(len + 9);
3863 if (spat == NULL || epat == NULL)
3864 {
3865 vim_free(spat);
3866 vim_free(epat);
3867 curwin->w_cursor = old_pos;
3868 goto theend;
3869 }
Bram Moolenaar16c31fe2012-01-26 20:58:26 +01003870 sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003871 sprintf((char *)epat, "</%.*s>\\c", len, p);
3872
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003873 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
Bram Moolenaar76929292008-01-06 19:07:36 +00003874 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003875
3876 vim_free(spat);
3877 vim_free(epat);
3878
3879 if (r < 1 || lt(curwin->w_cursor, old_end))
3880 {
3881 /* Can't find other end or it's before the previous end. Could be a
3882 * HTML tag that doesn't have a matching end. Search backwards for
3883 * another starting tag. */
3884 count = 1;
3885 curwin->w_cursor = start_pos;
3886 goto again;
3887 }
3888
3889 if (do_include || r < 1)
3890 {
3891 /* Include up to the '>'. */
3892 while (*ml_get_cursor() != '>')
3893 if (inc_cursor() < 0)
3894 break;
3895 }
3896 else
3897 {
3898 /* Exclude the '<' of the end tag. */
3899 if (*ml_get_cursor() == '<')
3900 dec_cursor();
3901 }
3902 end_pos = curwin->w_cursor;
3903
3904 if (!do_include)
3905 {
3906 /* Exclude the start tag. */
3907 curwin->w_cursor = start_pos;
3908 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003909 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003910 {
3911 inc_cursor();
3912 start_pos = curwin->w_cursor;
3913 break;
3914 }
3915 curwin->w_cursor = end_pos;
3916
Bram Moolenaar45360022005-07-21 21:08:21 +00003917 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003918 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003919 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003920 {
3921 do_include = TRUE;
3922 curwin->w_cursor = old_start;
3923 count = count_arg;
3924 goto again;
3925 }
3926 }
3927
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003928 if (VIsual_active)
3929 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003930 /* If the end is before the start there is no text between tags, select
3931 * the char under the cursor. */
3932 if (lt(end_pos, start_pos))
3933 curwin->w_cursor = start_pos;
3934 else if (*p_sel == 'e')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003935 ++curwin->w_cursor.col;
3936 VIsual = start_pos;
3937 VIsual_mode = 'v';
3938 redraw_curbuf_later(INVERTED); /* update the inversion */
3939 showmode();
3940 }
3941 else
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003942 {
3943 oap->start = start_pos;
3944 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003945 if (lt(end_pos, start_pos))
3946 {
3947 /* End is before the start: there is no text between tags; operate
3948 * on an empty area. */
3949 curwin->w_cursor = start_pos;
3950 oap->inclusive = FALSE;
3951 }
3952 else
3953 oap->inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003954 }
3955 retval = OK;
3956
3957theend:
3958 p_ws = save_p_ws;
3959 return retval;
3960}
3961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 int
3963current_par(oap, count, include, type)
3964 oparg_T *oap;
3965 long count;
3966 int include; /* TRUE == include white space */
3967 int type; /* 'p' for paragraph, 'S' for section */
3968{
3969 linenr_T start_lnum;
3970 linenr_T end_lnum;
3971 int white_in_front;
3972 int dir;
3973 int start_is_white;
3974 int prev_start_is_white;
3975 int retval = OK;
3976 int do_white = FALSE;
3977 int t;
3978 int i;
3979
3980 if (type == 'S') /* not implemented yet */
3981 return FAIL;
3982
3983 start_lnum = curwin->w_cursor.lnum;
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 /*
3986 * When visual area is more than one line: extend it.
3987 */
3988 if (VIsual_active && start_lnum != VIsual.lnum)
3989 {
3990extend:
3991 if (start_lnum < VIsual.lnum)
3992 dir = BACKWARD;
3993 else
3994 dir = FORWARD;
3995 for (i = count; --i >= 0; )
3996 {
3997 if (start_lnum ==
3998 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3999 {
4000 retval = FAIL;
4001 break;
4002 }
4003
4004 prev_start_is_white = -1;
4005 for (t = 0; t < 2; ++t)
4006 {
4007 start_lnum += dir;
4008 start_is_white = linewhite(start_lnum);
4009 if (prev_start_is_white == start_is_white)
4010 {
4011 start_lnum -= dir;
4012 break;
4013 }
4014 for (;;)
4015 {
4016 if (start_lnum == (dir == BACKWARD
4017 ? 1 : curbuf->b_ml.ml_line_count))
4018 break;
4019 if (start_is_white != linewhite(start_lnum + dir)
4020 || (!start_is_white
4021 && startPS(start_lnum + (dir > 0
4022 ? 1 : 0), 0, 0)))
4023 break;
4024 start_lnum += dir;
4025 }
4026 if (!include)
4027 break;
4028 if (start_lnum == (dir == BACKWARD
4029 ? 1 : curbuf->b_ml.ml_line_count))
4030 break;
4031 prev_start_is_white = start_is_white;
4032 }
4033 }
4034 curwin->w_cursor.lnum = start_lnum;
4035 curwin->w_cursor.col = 0;
4036 return retval;
4037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038
4039 /*
4040 * First move back to the start_lnum of the paragraph or white lines
4041 */
4042 white_in_front = linewhite(start_lnum);
4043 while (start_lnum > 1)
4044 {
4045 if (white_in_front) /* stop at first white line */
4046 {
4047 if (!linewhite(start_lnum - 1))
4048 break;
4049 }
4050 else /* stop at first non-white line of start of paragraph */
4051 {
4052 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4053 break;
4054 }
4055 --start_lnum;
4056 }
4057
4058 /*
4059 * Move past the end of any white lines.
4060 */
4061 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004062 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4063 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064
4065 --end_lnum;
4066 i = count;
4067 if (!include && white_in_front)
4068 --i;
4069 while (i--)
4070 {
4071 if (end_lnum == curbuf->b_ml.ml_line_count)
4072 return FAIL;
4073
4074 if (!include)
4075 do_white = linewhite(end_lnum + 1);
4076
4077 if (include || !do_white)
4078 {
4079 ++end_lnum;
4080 /*
4081 * skip to end of paragraph
4082 */
4083 while (end_lnum < curbuf->b_ml.ml_line_count
4084 && !linewhite(end_lnum + 1)
4085 && !startPS(end_lnum + 1, 0, 0))
4086 ++end_lnum;
4087 }
4088
4089 if (i == 0 && white_in_front && include)
4090 break;
4091
4092 /*
4093 * skip to end of white lines after paragraph
4094 */
4095 if (include || do_white)
4096 while (end_lnum < curbuf->b_ml.ml_line_count
4097 && linewhite(end_lnum + 1))
4098 ++end_lnum;
4099 }
4100
4101 /*
4102 * If there are no empty lines at the end, try to find some empty lines at
4103 * the start (unless that has been done already).
4104 */
4105 if (!white_in_front && !linewhite(end_lnum) && include)
4106 while (start_lnum > 1 && linewhite(start_lnum - 1))
4107 --start_lnum;
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (VIsual_active)
4110 {
4111 /* Problem: when doing "Vipipip" nothing happens in a single white
4112 * line, we get stuck there. Trap this here. */
4113 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4114 goto extend;
4115 VIsual.lnum = start_lnum;
4116 VIsual_mode = 'V';
4117 redraw_curbuf_later(INVERTED); /* update the inversion */
4118 showmode();
4119 }
4120 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 oap->start.lnum = start_lnum;
4123 oap->start.col = 0;
4124 oap->motion_type = MLINE;
4125 }
4126 curwin->w_cursor.lnum = end_lnum;
4127 curwin->w_cursor.col = 0;
4128
4129 return OK;
4130}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004131
4132static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4133static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4134
4135/*
4136 * Search quote char from string line[col].
4137 * Quote character escaped by one of the characters in "escape" is not counted
4138 * as a quote.
4139 * Returns column number of "quotechar" or -1 when not found.
4140 */
4141 static int
4142find_next_quote(line, col, quotechar, escape)
4143 char_u *line;
4144 int col;
4145 int quotechar;
4146 char_u *escape; /* escape characters, can be NULL */
4147{
4148 int c;
4149
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004150 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004151 {
4152 c = line[col];
4153 if (c == NUL)
4154 return -1;
4155 else if (escape != NULL && vim_strchr(escape, c))
4156 ++col;
4157 else if (c == quotechar)
4158 break;
4159#ifdef FEAT_MBYTE
4160 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004161 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004162 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004164 ++col;
4165 }
4166 return col;
4167}
4168
4169/*
4170 * Search backwards in "line" from column "col_start" to find "quotechar".
4171 * Quote character escaped by one of the characters in "escape" is not counted
4172 * as a quote.
4173 * Return the found column or zero.
4174 */
4175 static int
4176find_prev_quote(line, col_start, quotechar, escape)
4177 char_u *line;
4178 int col_start;
4179 int quotechar;
4180 char_u *escape; /* escape characters, can be NULL */
4181{
4182 int n;
4183
4184 while (col_start > 0)
4185 {
4186 --col_start;
4187#ifdef FEAT_MBYTE
4188 col_start -= (*mb_head_off)(line, line + col_start);
4189#endif
4190 n = 0;
4191 if (escape != NULL)
4192 while (col_start - n > 0 && vim_strchr(escape,
4193 line[col_start - n - 1]) != NULL)
4194 ++n;
4195 if (n & 1)
4196 col_start -= n; /* uneven number of escape chars, skip it */
4197 else if (line[col_start] == quotechar)
4198 break;
4199 }
4200 return col_start;
4201}
4202
4203/*
4204 * Find quote under the cursor, cursor at end.
4205 * Returns TRUE if found, else FALSE.
4206 */
4207 int
4208current_quote(oap, count, include, quotechar)
4209 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004210 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004211 int include; /* TRUE == include quote char */
4212 int quotechar; /* Quote character */
4213{
4214 char_u *line = ml_get_curline();
4215 int col_end;
4216 int col_start = curwin->w_cursor.col;
4217 int inclusive = FALSE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004218 int vis_empty = TRUE; /* Visual selection <= 1 char */
4219 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004220 int inside_quotes = FALSE; /* Looks like "i'" done before */
4221 int selected_quote = FALSE; /* Has quote inside selection */
4222 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004223
4224 /* Correct cursor when 'selection' is exclusive */
4225 if (VIsual_active)
4226 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004227 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004228 if (*p_sel == 'e' && vis_bef_curs)
4229 dec_cursor();
4230 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004231 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004232
4233 if (!vis_empty)
4234 {
4235 /* Check if the existing selection exactly spans the text inside
4236 * quotes. */
4237 if (vis_bef_curs)
4238 {
4239 inside_quotes = VIsual.col > 0
4240 && line[VIsual.col - 1] == quotechar
4241 && line[curwin->w_cursor.col] != NUL
4242 && line[curwin->w_cursor.col + 1] == quotechar;
4243 i = VIsual.col;
4244 col_end = curwin->w_cursor.col;
4245 }
4246 else
4247 {
4248 inside_quotes = curwin->w_cursor.col > 0
4249 && line[curwin->w_cursor.col - 1] == quotechar
4250 && line[VIsual.col] != NUL
4251 && line[VIsual.col + 1] == quotechar;
4252 i = curwin->w_cursor.col;
4253 col_end = VIsual.col;
4254 }
4255
4256 /* Find out if we have a quote in the selection. */
4257 while (i <= col_end)
4258 if (line[i++] == quotechar)
4259 {
4260 selected_quote = TRUE;
4261 break;
4262 }
4263 }
4264
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004265 if (!vis_empty && line[col_start] == quotechar)
4266 {
4267 /* Already selecting something and on a quote character. Find the
4268 * next quoted string. */
4269 if (vis_bef_curs)
4270 {
4271 /* Assume we are on a closing quote: move to after the next
4272 * opening quote. */
4273 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4274 if (col_start < 0)
4275 return FALSE;
4276 col_end = find_next_quote(line, col_start + 1, quotechar,
4277 curbuf->b_p_qe);
4278 if (col_end < 0)
4279 {
4280 /* We were on a starting quote perhaps? */
4281 col_end = col_start;
4282 col_start = curwin->w_cursor.col;
4283 }
4284 }
4285 else
4286 {
4287 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4288 if (line[col_end] != quotechar)
4289 return FALSE;
4290 col_start = find_prev_quote(line, col_end, quotechar,
4291 curbuf->b_p_qe);
4292 if (line[col_start] != quotechar)
4293 {
4294 /* We were on an ending quote perhaps? */
4295 col_start = col_end;
4296 col_end = curwin->w_cursor.col;
4297 }
4298 }
4299 }
4300 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004301
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004302 if (line[col_start] == quotechar || !vis_empty)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004303 {
4304 int first_col = col_start;
4305
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004306 if (!vis_empty)
4307 {
4308 if (vis_bef_curs)
4309 first_col = find_next_quote(line, col_start, quotechar, NULL);
4310 else
4311 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4312 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004313
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004314 /* The cursor is on a quote, we don't know if it's the opening or
4315 * closing quote. Search from the start of the line to find out.
4316 * Also do this when there is a Visual area, a' may leave the cursor
4317 * in between two strings. */
4318 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004319 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004320 {
4321 /* Find open quote character. */
4322 col_start = find_next_quote(line, col_start, quotechar, NULL);
4323 if (col_start < 0 || col_start > first_col)
4324 return FALSE;
4325 /* Find close quote character. */
4326 col_end = find_next_quote(line, col_start + 1, quotechar,
4327 curbuf->b_p_qe);
4328 if (col_end < 0)
4329 return FALSE;
4330 /* If is cursor between start and end quote character, it is
4331 * target text object. */
4332 if (col_start <= first_col && first_col <= col_end)
4333 break;
4334 col_start = col_end + 1;
4335 }
4336 }
4337 else
4338 {
4339 /* Search backward for a starting quote. */
4340 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4341 if (line[col_start] != quotechar)
4342 {
4343 /* No quote before the cursor, look after the cursor. */
4344 col_start = find_next_quote(line, col_start, quotechar, NULL);
4345 if (col_start < 0)
4346 return FALSE;
4347 }
4348
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 }
4355
4356 /* When "include" is TRUE, include spaces after closing quote or before
4357 * the starting quote. */
4358 if (include)
4359 {
4360 if (vim_iswhite(line[col_end + 1]))
4361 while (vim_iswhite(line[col_end + 1]))
4362 ++col_end;
4363 else
4364 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4365 --col_start;
4366 }
4367
Bram Moolenaarab194812005-09-14 21:40:12 +00004368 /* Set start position. After vi" another i" must include the ".
4369 * For v2i" include the quotes. */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004370 if (!include && count < 2 && (vis_empty || !inside_quotes))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004371 ++col_start;
4372 curwin->w_cursor.col = col_start;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004373 if (VIsual_active)
4374 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004375 /* Set the start of the Visual area when the Visual area was empty, we
4376 * were just inside quotes or the Visual area didn't start at a quote
4377 * and didn't include a quote.
4378 */
4379 if (vis_empty
4380 || (vis_bef_curs
4381 && !selected_quote
4382 && (inside_quotes
4383 || (line[VIsual.col] != quotechar
4384 && (VIsual.col == 0
4385 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004386 {
4387 VIsual = curwin->w_cursor;
4388 redraw_curbuf_later(INVERTED);
4389 }
4390 }
4391 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004392 {
4393 oap->start = curwin->w_cursor;
4394 oap->motion_type = MCHAR;
4395 }
4396
4397 /* Set end position. */
4398 curwin->w_cursor.col = col_end;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004399 if ((include || count > 1 /* After vi" another i" must include the ". */
Bram Moolenaarab194812005-09-14 21:40:12 +00004400 || (!vis_empty && inside_quotes)
Bram Moolenaarab194812005-09-14 21:40:12 +00004401 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004402 inclusive = TRUE;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004403 if (VIsual_active)
4404 {
4405 if (vis_empty || vis_bef_curs)
4406 {
4407 /* decrement cursor when 'selection' is not exclusive */
4408 if (*p_sel != 'e')
4409 dec_cursor();
4410 }
4411 else
4412 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004413 /* Cursor is at start of Visual area. Set the end of the Visual
4414 * area when it was just inside quotes or it didn't end at a
4415 * quote. */
4416 if (inside_quotes
4417 || (!selected_quote
4418 && line[VIsual.col] != quotechar
4419 && (line[VIsual.col] == NUL
4420 || line[VIsual.col + 1] != quotechar)))
4421 {
4422 dec_cursor();
4423 VIsual = curwin->w_cursor;
4424 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004425 curwin->w_cursor.col = col_start;
4426 }
4427 if (VIsual_mode == 'V')
4428 {
4429 VIsual_mode = 'v';
4430 redraw_cmdline = TRUE; /* show mode later */
4431 }
4432 }
4433 else
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004434 {
4435 /* Set inclusive and other oap's flags. */
4436 oap->inclusive = inclusive;
4437 }
4438
4439 return OK;
4440}
4441
4442#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaare78495d2013-06-30 14:46:53 +02004444static int is_one_char __ARGS((char_u *pattern));
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004445
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004446/*
4447 * Find next search match under cursor, cursor at end.
4448 * Used while an operator is pending, and in Visual mode.
4449 * TODO: redo only works when used in operator pending mode
4450 */
4451 int
4452current_search(count, forward)
4453 long count;
4454 int forward; /* move forward or backwards */
4455{
4456 pos_T start_pos; /* position before the pattern */
4457 pos_T orig_pos; /* position of the cursor at beginning */
4458 pos_T pos; /* position after the pattern */
4459 int i;
4460 int dir;
4461 int result; /* result of various function calls */
4462 char_u old_p_ws = p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004463 int flags = 0;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004464 pos_T save_VIsual = VIsual;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004465 int one_char;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004466
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004467 /* wrapping should not occur */
4468 p_ws = FALSE;
4469
4470 /* Correct cursor when 'selection' is exclusive */
4471 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
4472 dec_cursor();
4473
4474 if (VIsual_active)
4475 {
4476 orig_pos = curwin->w_cursor;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004477
4478 pos = curwin->w_cursor;
4479 start_pos = VIsual;
4480
4481 /* make sure, searching further will extend the match */
4482 if (VIsual_active)
4483 {
4484 if (forward)
4485 incl(&pos);
4486 else
4487 decl(&pos);
4488 }
4489 }
4490 else
4491 orig_pos = pos = start_pos = curwin->w_cursor;
4492
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004493 /* Is the pattern is zero-width? */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004494 one_char = is_one_char(spats[last_idx].pat);
4495 if (one_char == -1)
Bram Moolenaarba2d44f2013-11-28 19:27:30 +01004496 {
4497 p_ws = old_p_ws;
4498 return FAIL; /* pattern not found */
4499 }
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004500
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004501 /*
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004502 * The trick is to first search backwards and then search forward again,
4503 * so that a match at the current cursor position will be correctly
4504 * captured.
4505 */
4506 for (i = 0; i < 2; i++)
4507 {
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004508 if (forward)
4509 dir = i;
4510 else
4511 dir = !i;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004512
4513 flags = 0;
Bram Moolenaare78495d2013-06-30 14:46:53 +02004514 if (!dir && !one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004515 flags = SEARCH_END;
4516
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004517 result = searchit(curwin, curbuf, &pos, (dir ? FORWARD : BACKWARD),
4518 spats[last_idx].pat, (long) (i ? count : 1),
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004519 SEARCH_KEEP | flags, RE_SEARCH, 0, NULL);
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004520
4521 /* First search may fail, but then start searching from the
4522 * beginning of the file (cursor might be on the search match)
4523 * except when Visual mode is active, so that extending the visual
4524 * selection works. */
4525 if (!result && i) /* not found, abort */
4526 {
4527 curwin->w_cursor = orig_pos;
4528 if (VIsual_active)
4529 VIsual = save_VIsual;
4530 p_ws = old_p_ws;
4531 return FAIL;
4532 }
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004533 else if (!i && !result)
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004534 {
4535 if (forward) /* try again from start of buffer */
4536 {
4537 clearpos(&pos);
4538 }
4539 else /* try again from end of buffer */
4540 {
4541 /* searching backwards, so set pos to last line and col */
4542 pos.lnum = curwin->w_buffer->b_ml.ml_line_count;
Bram Moolenaar09168a72012-08-02 21:24:42 +02004543 pos.col = (colnr_T)STRLEN(
4544 ml_get(curwin->w_buffer->b_ml.ml_line_count));
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004545 }
4546 }
Bram Moolenaarfcea03d2013-11-07 04:46:48 +01004547 p_ws = old_p_ws;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004548 }
4549
4550 start_pos = pos;
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004551 flags = forward ? SEARCH_END : 0;
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004552
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004553 /* move to match, except for zero-width matches, in which case, we are
4554 * already on the next match */
Bram Moolenaare78495d2013-06-30 14:46:53 +02004555 if (!one_char)
Bram Moolenaarba6ba362012-08-08 15:27:57 +02004556 result = searchit(curwin, curbuf, &pos, (forward ? FORWARD : BACKWARD),
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004557 spats[last_idx].pat, 0L, flags | SEARCH_KEEP, RE_SEARCH, 0, NULL);
4558
4559 if (!VIsual_active)
4560 VIsual = start_pos;
4561
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004562 curwin->w_cursor = pos;
4563 VIsual_active = TRUE;
4564 VIsual_mode = 'v';
4565
4566 if (VIsual_active)
4567 {
4568 redraw_curbuf_later(INVERTED); /* update the inversion */
Bram Moolenaar718f0072012-10-03 13:35:51 +02004569 if (*p_sel == 'e')
4570 {
4571 /* Correction for exclusive selection depends on the direction. */
4572 if (forward && ltoreq(VIsual, curwin->w_cursor))
4573 inc_cursor();
4574 else if (!forward && ltoreq(curwin->w_cursor, VIsual))
4575 inc(&VIsual);
4576 }
4577
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004578 }
4579
4580#ifdef FEAT_FOLDING
4581 if (fdo_flags & FDO_SEARCH && KeyTyped)
4582 foldOpenCursor();
4583#endif
4584
4585 may_start_select('c');
4586#ifdef FEAT_MOUSE
4587 setmouse();
4588#endif
4589#ifdef FEAT_CLIPBOARD
4590 /* Make sure the clipboard gets updated. Needed because start and
4591 * end are still the same, and the selection needs to be owned */
4592 clip_star.vmode = NUL;
4593#endif
4594 redraw_curbuf_later(INVERTED);
4595 showmode();
4596
4597 return OK;
4598}
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004599
4600/*
Bram Moolenaare78495d2013-06-30 14:46:53 +02004601 * Check if the pattern is one character or zero-width.
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004602 * Returns TRUE, FALSE or -1 for failure.
4603 */
4604 static int
Bram Moolenaare78495d2013-06-30 14:46:53 +02004605is_one_char(pattern)
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004606 char_u *pattern;
4607{
4608 regmmatch_T regmatch;
4609 int nmatched = 0;
4610 int result = -1;
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004611 pos_T pos;
4612 int save_called_emsg = called_emsg;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004613
4614 if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH,
4615 SEARCH_KEEP, &regmatch) == FAIL)
4616 return -1;
4617
4618 /* move to match */
4619 clearpos(&pos);
4620 if (searchit(curwin, curbuf, &pos, FORWARD, spats[last_idx].pat, 1,
4621 SEARCH_KEEP, RE_SEARCH, 0, NULL) != FAIL)
4622 {
4623 /* Zero-width pattern should match somewhere, then we can check if
4624 * start and end are in the same position. */
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004625 called_emsg = FALSE;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004626 nmatched = vim_regexec_multi(&regmatch, curwin, curbuf,
4627 pos.lnum, (colnr_T)0, NULL);
4628
4629 if (!called_emsg)
4630 result = (nmatched != 0
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004631 && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum
4632 && regmatch.startpos[0].col == regmatch.endpos[0].col);
Bram Moolenaare78495d2013-06-30 14:46:53 +02004633
Bram Moolenaar4c7cb6b2013-10-02 21:55:02 +02004634 if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col)
4635 result = TRUE;
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004636 }
4637
Bram Moolenaar57c0ea82012-09-05 12:16:45 +02004638 called_emsg |= save_called_emsg;
Bram Moolenaar473de612013-06-08 18:19:48 +02004639 vim_regfree(regmatch.regprog);
Bram Moolenaardde0efe2012-08-23 15:53:05 +02004640 return result;
4641}
Bram Moolenaar8a0f3c72012-07-29 12:55:32 +02004642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4644 || defined(PROTO)
4645/*
4646 * return TRUE if line 'lnum' is empty or has white chars only.
4647 */
4648 int
4649linewhite(lnum)
4650 linenr_T lnum;
4651{
4652 char_u *p;
4653
4654 p = skipwhite(ml_get(lnum));
4655 return (*p == NUL);
4656}
4657#endif
4658
4659#if defined(FEAT_FIND_ID) || defined(PROTO)
4660/*
4661 * Find identifiers or defines in included files.
Bram Moolenaarb4b0a082011-02-25 18:38:36 +01004662 * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 void
4665find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4666 type, count, action, start_lnum, end_lnum)
4667 char_u *ptr; /* pointer to search pattern */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004668 int dir UNUSED; /* direction of expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 int len; /* length of search pattern */
4670 int whole; /* match whole words only */
4671 int skip_comments; /* don't match inside comments */
4672 int type; /* Type of search; are we looking for a type?
4673 a macro? */
4674 long count;
4675 int action; /* What to do when we find it */
4676 linenr_T start_lnum; /* first line to start searching */
4677 linenr_T end_lnum; /* last line for searching */
4678{
4679 SearchedFile *files; /* Stack of included files */
4680 SearchedFile *bigger; /* When we need more space */
4681 int max_path_depth = 50;
4682 long match_count = 1;
4683
4684 char_u *pat;
4685 char_u *new_fname;
4686 char_u *curr_fname = curbuf->b_fname;
4687 char_u *prev_fname = NULL;
4688 linenr_T lnum;
4689 int depth;
4690 int depth_displayed; /* For type==CHECK_PATH */
4691 int old_files;
4692 int already_searched;
4693 char_u *file_line;
4694 char_u *line;
4695 char_u *p;
4696 char_u save_char;
4697 int define_matched;
4698 regmatch_T regmatch;
4699 regmatch_T incl_regmatch;
4700 regmatch_T def_regmatch;
4701 int matched = FALSE;
4702 int did_show = FALSE;
4703 int found = FALSE;
4704 int i;
4705 char_u *already = NULL;
4706 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004707 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4709 win_T *curwin_save = NULL;
4710#endif
4711
4712 regmatch.regprog = NULL;
4713 incl_regmatch.regprog = NULL;
4714 def_regmatch.regprog = NULL;
4715
4716 file_line = alloc(LSIZE);
4717 if (file_line == NULL)
4718 return;
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (type != CHECK_PATH && type != FIND_DEFINE
4721#ifdef FEAT_INS_EXPAND
4722 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4723 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004724 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725#endif
4726 )
4727 {
4728 pat = alloc(len + 5);
4729 if (pat == NULL)
4730 goto fpip_end;
4731 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4732 /* ignore case according to p_ic, p_scs and pat */
4733 regmatch.rm_ic = ignorecase(pat);
4734 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4735 vim_free(pat);
4736 if (regmatch.regprog == NULL)
4737 goto fpip_end;
4738 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004739 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4740 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004742 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 if (incl_regmatch.regprog == NULL)
4744 goto fpip_end;
4745 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4746 }
4747 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4748 {
4749 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4750 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4751 if (def_regmatch.regprog == NULL)
4752 goto fpip_end;
4753 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4754 }
4755 files = (SearchedFile *)lalloc_clear((long_u)
4756 (max_path_depth * sizeof(SearchedFile)), TRUE);
4757 if (files == NULL)
4758 goto fpip_end;
4759 old_files = max_path_depth;
4760 depth = depth_displayed = -1;
4761
4762 lnum = start_lnum;
4763 if (end_lnum > curbuf->b_ml.ml_line_count)
4764 end_lnum = curbuf->b_ml.ml_line_count;
4765 if (lnum > end_lnum) /* do at least one line */
4766 lnum = end_lnum;
4767 line = ml_get(lnum);
4768
4769 for (;;)
4770 {
4771 if (incl_regmatch.regprog != NULL
4772 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4773 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004774 char_u *p_fname = (curr_fname == curbuf->b_fname)
4775 ? curbuf->b_ffname : curr_fname;
4776
4777 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4778 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4779 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02004780 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004781 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4782 else
4783 /* Use text after match with 'include'. */
4784 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004785 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 already_searched = FALSE;
4787 if (new_fname != NULL)
4788 {
4789 /* Check whether we have already searched in this file */
4790 for (i = 0;; i++)
4791 {
4792 if (i == depth + 1)
4793 i = old_files;
4794 if (i == max_path_depth)
4795 break;
4796 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4797 {
4798 if (type != CHECK_PATH &&
4799 action == ACTION_SHOW_ALL && files[i].matched)
4800 {
4801 msg_putchar('\n'); /* cursor below last one */
4802 if (!got_int) /* don't display if 'q'
4803 typed at "--more--"
Bram Moolenaarfe81d452009-04-22 14:44:41 +00004804 message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 msg_home_replace_hl(new_fname);
4807 MSG_PUTS(_(" (includes previously listed match)"));
4808 prev_fname = NULL;
4809 }
4810 }
4811 vim_free(new_fname);
4812 new_fname = NULL;
4813 already_searched = TRUE;
4814 break;
4815 }
4816 }
4817 }
4818
4819 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4820 || (new_fname == NULL && !already_searched)))
4821 {
4822 if (did_show)
4823 msg_putchar('\n'); /* cursor below last one */
4824 else
4825 {
4826 gotocmdline(TRUE); /* cursor at status line */
4827 MSG_PUTS_TITLE(_("--- Included files "));
4828 if (action != ACTION_SHOW_ALL)
4829 MSG_PUTS_TITLE(_("not found "));
4830 MSG_PUTS_TITLE(_("in path ---\n"));
4831 }
4832 did_show = TRUE;
4833 while (depth_displayed < depth && !got_int)
4834 {
4835 ++depth_displayed;
4836 for (i = 0; i < depth_displayed; i++)
4837 MSG_PUTS(" ");
4838 msg_home_replace(files[depth_displayed].name);
4839 MSG_PUTS(" -->\n");
4840 }
4841 if (!got_int) /* don't display if 'q' typed
4842 for "--more--" message */
4843 {
4844 for (i = 0; i <= depth_displayed; i++)
4845 MSG_PUTS(" ");
4846 if (new_fname != NULL)
4847 {
4848 /* using "new_fname" is more reliable, e.g., when
4849 * 'includeexpr' is set. */
4850 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4851 }
4852 else
4853 {
4854 /*
4855 * Isolate the file name.
4856 * Include the surrounding "" or <> if present.
4857 */
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02004858 if (inc_opt != NULL
4859 && strstr((char *)inc_opt, "\\zs") != NULL)
4860 {
4861 /* pattern contains \zs, use the match */
4862 p = incl_regmatch.startp[0];
4863 i = (int)(incl_regmatch.endp[0]
4864 - incl_regmatch.startp[0]);
4865 }
4866 else
4867 {
4868 /* find the file name after the end of the match */
4869 for (p = incl_regmatch.endp[0];
4870 *p && !vim_isfilec(*p); p++)
4871 ;
4872 for (i = 0; vim_isfilec(p[i]); i++)
4873 ;
4874 }
4875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (i == 0)
4877 {
4878 /* Nothing found, use the rest of the line. */
4879 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004880 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
Bram Moolenaar058bdcf2012-07-25 13:46:30 +02004882 /* Avoid checking before the start of the line, can
4883 * happen if \zs appears in the regexp. */
4884 else if (p > line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
4886 if (p[-1] == '"' || p[-1] == '<')
4887 {
4888 --p;
4889 ++i;
4890 }
4891 if (p[i] == '"' || p[i] == '>')
4892 ++i;
4893 }
4894 save_char = p[i];
4895 p[i] = NUL;
4896 msg_outtrans_attr(p, hl_attr(HLF_D));
4897 p[i] = save_char;
4898 }
4899
4900 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4901 {
4902 if (already_searched)
4903 MSG_PUTS(_(" (Already listed)"));
4904 else
4905 MSG_PUTS(_(" NOT FOUND"));
4906 }
4907 }
4908 out_flush(); /* output each line directly */
4909 }
4910
4911 if (new_fname != NULL)
4912 {
4913 /* Push the new file onto the file stack */
4914 if (depth + 1 == old_files)
4915 {
4916 bigger = (SearchedFile *)lalloc((long_u)(
4917 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4918 if (bigger != NULL)
4919 {
4920 for (i = 0; i <= depth; i++)
4921 bigger[i] = files[i];
4922 for (i = depth + 1; i < old_files + max_path_depth; i++)
4923 {
4924 bigger[i].fp = NULL;
4925 bigger[i].name = NULL;
4926 bigger[i].lnum = 0;
4927 bigger[i].matched = FALSE;
4928 }
4929 for (i = old_files; i < max_path_depth; i++)
4930 bigger[i + max_path_depth] = files[i];
4931 old_files += max_path_depth;
4932 max_path_depth *= 2;
4933 vim_free(files);
4934 files = bigger;
4935 }
4936 }
4937 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4938 == NULL)
4939 vim_free(new_fname);
4940 else
4941 {
4942 if (++depth == old_files)
4943 {
4944 /*
4945 * lalloc() for 'bigger' must have failed above. We
4946 * will forget one of our already visited files now.
4947 */
4948 vim_free(files[old_files].name);
4949 ++old_files;
4950 }
4951 files[depth].name = curr_fname = new_fname;
4952 files[depth].lnum = 0;
4953 files[depth].matched = FALSE;
4954#ifdef FEAT_INS_EXPAND
4955 if (action == ACTION_EXPAND)
4956 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004957 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00004958 vim_snprintf((char*)IObuff, IOSIZE,
4959 _("Scanning included file: %s"),
4960 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4962 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004963 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004965 if (p_verbose >= 5)
4966 {
4967 verbose_enter();
4968 smsg((char_u *)_("Searching included file %s"),
4969 (char *)new_fname);
4970 verbose_leave();
4971 }
4972
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975 }
4976 else
4977 {
4978 /*
4979 * Check if the line is a define (type == FIND_DEFINE)
4980 */
4981 p = line;
4982search_line:
4983 define_matched = FALSE;
4984 if (def_regmatch.regprog != NULL
4985 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4986 {
4987 /*
4988 * Pattern must be first identifier after 'define', so skip
4989 * to that position before checking for match of pattern. Also
4990 * don't let it match beyond the end of this identifier.
4991 */
4992 p = def_regmatch.endp[0];
4993 while (*p && !vim_iswordc(*p))
4994 p++;
4995 define_matched = TRUE;
4996 }
4997
4998 /*
4999 * Look for a match. Don't do this if we are looking for a
5000 * define and this line didn't match define_prog above.
5001 */
5002 if (def_regmatch.regprog == NULL || define_matched)
5003 {
5004 if (define_matched
5005#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005006 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007#endif
5008 )
5009 {
5010 /* compare the first "len" chars from "ptr" */
5011 startp = skipwhite(p);
5012 if (p_ic)
5013 matched = !MB_STRNICMP(startp, ptr, len);
5014 else
5015 matched = !STRNCMP(startp, ptr, len);
5016 if (matched && define_matched && whole
5017 && vim_iswordc(startp[len]))
5018 matched = FALSE;
5019 }
5020 else if (regmatch.regprog != NULL
5021 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
5022 {
5023 matched = TRUE;
5024 startp = regmatch.startp[0];
5025 /*
5026 * Check if the line is not a comment line (unless we are
5027 * looking for a define). A line starting with "# define"
5028 * is not considered to be a comment line.
5029 */
5030 if (!define_matched && skip_comments)
5031 {
5032#ifdef FEAT_COMMENTS
5033 if ((*line != '#' ||
5034 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
Bram Moolenaar81340392012-06-06 16:12:59 +02005035 && get_leader_len(line, NULL, FALSE, TRUE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 matched = FALSE;
5037
5038 /*
5039 * Also check for a "/ *" or "/ /" before the match.
5040 * Skips lines like "int backwards; / * normal index
5041 * * /" when looking for "normal".
5042 * Note: Doesn't skip "/ *" in comments.
5043 */
5044 p = skipwhite(line);
5045 if (matched
5046 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
5047#endif
5048 for (p = line; *p && p < startp; ++p)
5049 {
5050 if (matched
5051 && p[0] == '/'
5052 && (p[1] == '*' || p[1] == '/'))
5053 {
5054 matched = FALSE;
5055 /* After "//" all text is comment */
5056 if (p[1] == '/')
5057 break;
5058 ++p;
5059 }
5060 else if (!matched && p[0] == '*' && p[1] == '/')
5061 {
5062 /* Can find match after "* /". */
5063 matched = TRUE;
5064 ++p;
5065 }
5066 }
5067 }
5068 }
5069 }
5070 }
5071 if (matched)
5072 {
5073#ifdef FEAT_INS_EXPAND
5074 if (action == ACTION_EXPAND)
5075 {
5076 int reuse = 0;
5077 int add_r;
5078 char_u *aux;
5079
5080 if (depth == -1 && lnum == curwin->w_cursor.lnum)
5081 break;
5082 found = TRUE;
5083 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (vim_iswordp(p))
5088 goto exit_matched;
5089 p = find_word_start(p);
5090 }
5091 p = find_word_end(p);
5092 i = (int)(p - aux);
5093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005096 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005098
5099 /* Get the next line: when "depth" < 0 from the current
5100 * buffer, otherwise from the included file. Jump to
5101 * exit_matched when past the last line. */
5102 if (depth < 0)
5103 {
5104 if (lnum >= end_lnum)
5105 goto exit_matched;
5106 line = ml_get(++lnum);
5107 }
5108 else if (vim_fgets(line = file_line,
5109 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 goto exit_matched;
5111
5112 /* we read a line, set "already" to check this "line" later
5113 * if depth >= 0 we'll increase files[depth].lnum far
5114 * bellow -- Acevedo */
5115 already = aux = p = skipwhite(line);
5116 p = find_word_start(p);
5117 p = find_word_end(p);
5118 if (p > aux)
5119 {
5120 if (*aux != ')' && IObuff[i-1] != TAB)
5121 {
5122 if (IObuff[i-1] != ' ')
5123 IObuff[i++] = ' ';
5124 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
5125 if (p_js
5126 && (IObuff[i-2] == '.'
5127 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
5128 && (IObuff[i-2] == '?'
5129 || IObuff[i-2] == '!'))))
5130 IObuff[i++] = ' ';
5131 }
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005132 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 if (p - aux >= IOSIZE - i)
5134 p = aux + IOSIZE - i - 1;
5135 STRNCPY(IObuff + i, aux, p - aux);
5136 i += (int)(p - aux);
5137 reuse |= CONT_S_IPOS;
5138 }
5139 IObuff[i] = NUL;
5140 aux = IObuff;
5141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005142 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 goto exit_matched;
5144 }
5145
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005146 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 curr_fname == curbuf->b_fname ? NULL : curr_fname,
5148 dir, reuse);
5149 if (add_r == OK)
5150 /* if dir was BACKWARD then honor it just once */
5151 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005152 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154 }
5155 else
5156#endif
5157 if (action == ACTION_SHOW_ALL)
5158 {
5159 found = TRUE;
5160 if (!did_show)
5161 gotocmdline(TRUE); /* cursor at status line */
5162 if (curr_fname != prev_fname)
5163 {
5164 if (did_show)
5165 msg_putchar('\n'); /* cursor below last one */
5166 if (!got_int) /* don't display if 'q' typed
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005167 at "--more--" message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 msg_home_replace_hl(curr_fname);
5169 prev_fname = curr_fname;
5170 }
5171 did_show = TRUE;
5172 if (!got_int)
5173 show_pat_in_path(line, type, TRUE, action,
5174 (depth == -1) ? NULL : files[depth].fp,
5175 (depth == -1) ? &lnum : &files[depth].lnum,
5176 match_count++);
5177
5178 /* Set matched flag for this file and all the ones that
5179 * include it */
5180 for (i = 0; i <= depth; ++i)
5181 files[i].matched = TRUE;
5182 }
5183 else if (--count <= 0)
5184 {
5185 found = TRUE;
5186 if (depth == -1 && lnum == curwin->w_cursor.lnum
5187#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5188 && g_do_tagpreview == 0
5189#endif
5190 )
5191 EMSG(_("E387: Match is on current line"));
5192 else if (action == ACTION_SHOW)
5193 {
5194 show_pat_in_path(line, type, did_show, action,
5195 (depth == -1) ? NULL : files[depth].fp,
5196 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5197 did_show = TRUE;
5198 }
5199 else
5200 {
5201#ifdef FEAT_GUI
5202 need_mouse_correct = TRUE;
5203#endif
5204#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5205 /* ":psearch" uses the preview window */
5206 if (g_do_tagpreview != 0)
5207 {
5208 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005209 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
5211#endif
5212 if (action == ACTION_SPLIT)
5213 {
5214#ifdef FEAT_WINDOWS
5215 if (win_split(0, 0) == FAIL)
5216#endif
5217 break;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005218 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220 if (depth == -1)
5221 {
5222 /* match in current file */
5223#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5224 if (g_do_tagpreview != 0)
5225 {
5226 if (getfile(0, curwin_save->w_buffer->b_fname,
5227 NULL, TRUE, lnum, FALSE) > 0)
5228 break; /* failed to jump to file */
5229 }
5230 else
5231#endif
5232 setpcmark();
5233 curwin->w_cursor.lnum = lnum;
5234 }
5235 else
5236 {
5237 if (getfile(0, files[depth].name, NULL, TRUE,
5238 files[depth].lnum, FALSE) > 0)
5239 break; /* failed to jump to file */
5240 /* autocommands may have changed the lnum, we don't
5241 * want that here */
5242 curwin->w_cursor.lnum = files[depth].lnum;
5243 }
5244 }
5245 if (action != ACTION_SHOW)
5246 {
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005247 curwin->w_cursor.col = (colnr_T)(startp - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 curwin->w_set_curswant = TRUE;
5249 }
5250
5251#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5252 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005253 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
5255 /* Return cursor to where we were */
5256 validate_cursor();
5257 redraw_later(VALID);
5258 win_enter(curwin_save, TRUE);
5259 }
5260#endif
5261 break;
5262 }
5263#ifdef FEAT_INS_EXPAND
5264exit_matched:
5265#endif
5266 matched = FALSE;
5267 /* look for other matches in the rest of the line if we
5268 * are not at the end of it already */
5269 if (def_regmatch.regprog == NULL
5270#ifdef FEAT_INS_EXPAND
5271 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005272 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273#endif
Bram Moolenaarfe81d452009-04-22 14:44:41 +00005274 && *startp != NUL
Bram Moolenaar94c465c2012-07-19 17:18:26 +02005275 && *(p = startp + MB_PTR2LEN(startp)) != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 goto search_line;
5277 }
5278 line_breakcheck();
5279#ifdef FEAT_INS_EXPAND
5280 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00005281 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283#else
5284 if (got_int)
5285#endif
5286 break;
5287
5288 /*
5289 * Read the next line. When reading an included file and encountering
5290 * end-of-file, close the file and continue in the file that included
5291 * it.
5292 */
5293 while (depth >= 0 && !already
5294 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5295 {
5296 fclose(files[depth].fp);
5297 --old_files;
5298 files[old_files].name = files[depth].name;
5299 files[old_files].matched = files[depth].matched;
5300 --depth;
5301 curr_fname = (depth == -1) ? curbuf->b_fname
5302 : files[depth].name;
5303 if (depth < depth_displayed)
5304 depth_displayed = depth;
5305 }
5306 if (depth >= 0) /* we could read the line */
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005307 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 files[depth].lnum++;
Bram Moolenaarc84e3c12013-07-03 22:28:36 +02005309 /* Remove any CR and LF from the line. */
5310 i = (int)STRLEN(line);
5311 if (i > 0 && line[i - 1] == '\n')
5312 line[--i] = NUL;
5313 if (i > 0 && line[i - 1] == '\r')
5314 line[--i] = NUL;
5315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 else if (!already)
5317 {
5318 if (++lnum > end_lnum)
5319 break;
5320 line = ml_get(lnum);
5321 }
5322 already = NULL;
5323 }
5324 /* End of big for (;;) loop. */
5325
5326 /* Close any files that are still open. */
5327 for (i = 0; i <= depth; i++)
5328 {
5329 fclose(files[i].fp);
5330 vim_free(files[i].name);
5331 }
5332 for (i = old_files; i < max_path_depth; i++)
5333 vim_free(files[i].name);
5334 vim_free(files);
5335
5336 if (type == CHECK_PATH)
5337 {
5338 if (!did_show)
5339 {
5340 if (action != ACTION_SHOW_ALL)
5341 MSG(_("All included files were found"));
5342 else
5343 MSG(_("No included files"));
5344 }
5345 }
5346 else if (!found
5347#ifdef FEAT_INS_EXPAND
5348 && action != ACTION_EXPAND
5349#endif
5350 )
5351 {
5352#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005353 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354#else
5355 if (got_int)
5356#endif
5357 EMSG(_(e_interr));
5358 else if (type == FIND_DEFINE)
5359 EMSG(_("E388: Couldn't find definition"));
5360 else
5361 EMSG(_("E389: Couldn't find pattern"));
5362 }
5363 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5364 msg_end();
5365
5366fpip_end:
5367 vim_free(file_line);
Bram Moolenaar473de612013-06-08 18:19:48 +02005368 vim_regfree(regmatch.regprog);
5369 vim_regfree(incl_regmatch.regprog);
5370 vim_regfree(def_regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371}
5372
5373 static void
5374show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5375 char_u *line;
5376 int type;
5377 int did_show;
5378 int action;
5379 FILE *fp;
5380 linenr_T *lnum;
5381 long count;
5382{
5383 char_u *p;
5384
5385 if (did_show)
5386 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005387 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 gotocmdline(TRUE); /* cursor at status line */
5389 if (got_int) /* 'q' typed at "--more--" message */
5390 return;
5391 for (;;)
5392 {
5393 p = line + STRLEN(line) - 1;
5394 if (fp != NULL)
5395 {
5396 /* We used fgets(), so get rid of newline at end */
5397 if (p >= line && *p == '\n')
5398 --p;
5399 if (p >= line && *p == '\r')
5400 --p;
5401 *(p + 1) = NUL;
5402 }
5403 if (action == ACTION_SHOW_ALL)
5404 {
5405 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5406 msg_puts(IObuff);
5407 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5408 /* Highlight line numbers */
5409 msg_puts_attr(IObuff, hl_attr(HLF_N));
5410 MSG_PUTS(" ");
5411 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005412 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 out_flush(); /* show one line at a time */
5414
5415 /* Definition continues until line that doesn't end with '\' */
5416 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5417 break;
5418
5419 if (fp != NULL)
5420 {
5421 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5422 break;
5423 ++*lnum;
5424 }
5425 else
5426 {
5427 if (++*lnum > curbuf->b_ml.ml_line_count)
5428 break;
5429 line = ml_get(*lnum);
5430 }
5431 msg_putchar('\n');
5432 }
5433}
5434#endif
5435
5436#ifdef FEAT_VIMINFO
5437 int
5438read_viminfo_search_pattern(virp, force)
5439 vir_T *virp;
5440 int force;
5441{
5442 char_u *lp;
5443 int idx = -1;
5444 int magic = FALSE;
5445 int no_scs = FALSE;
5446 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005447 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 long off = 0;
5449 int setlast = FALSE;
5450#ifdef FEAT_SEARCH_EXTRA
5451 static int hlsearch_on = FALSE;
5452#endif
5453 char_u *val;
5454
5455 /*
5456 * Old line types:
5457 * "/pat", "&pat": search/subst. pat
5458 * "~/pat", "~&pat": last used search/subst. pat
5459 * New line types:
5460 * "~h", "~H": hlsearch highlighting off/on
5461 * "~<magic><smartcase><line><end><off><last><which>pat"
5462 * <magic>: 'm' off, 'M' on
5463 * <smartcase>: 's' off, 'S' on
5464 * <line>: 'L' line offset, 'l' char offset
5465 * <end>: 'E' from end, 'e' from start
5466 * <off>: decimal, offset
5467 * <last>: '~' last used pattern
5468 * <which>: '/' search pat, '&' subst. pat
5469 */
5470 lp = virp->vir_line;
5471 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5472 {
5473 if (lp[1] == 'M') /* magic on */
5474 magic = TRUE;
5475 if (lp[2] == 's')
5476 no_scs = TRUE;
5477 if (lp[3] == 'L')
5478 off_line = TRUE;
5479 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005480 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 lp += 5;
5482 off = getdigits(&lp);
5483 }
5484 if (lp[0] == '~') /* use this pattern for last-used pattern */
5485 {
5486 setlast = TRUE;
5487 lp++;
5488 }
5489 if (lp[0] == '/')
5490 idx = RE_SEARCH;
5491 else if (lp[0] == '&')
5492 idx = RE_SUBST;
5493#ifdef FEAT_SEARCH_EXTRA
5494 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5495 hlsearch_on = FALSE;
5496 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5497 hlsearch_on = TRUE;
5498#endif
5499 if (idx >= 0)
5500 {
5501 if (force || spats[idx].pat == NULL)
5502 {
5503 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5504 TRUE);
5505 if (val != NULL)
5506 {
5507 set_last_search_pat(val, idx, magic, setlast);
5508 vim_free(val);
5509 spats[idx].no_scs = no_scs;
5510 spats[idx].off.line = off_line;
5511 spats[idx].off.end = off_end;
5512 spats[idx].off.off = off;
5513#ifdef FEAT_SEARCH_EXTRA
5514 if (setlast)
Bram Moolenaar8050efa2013-11-08 04:30:20 +01005515 {
5516 SET_NO_HLSEARCH(!hlsearch_on);
5517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518#endif
5519 }
5520 }
5521 }
5522 return viminfo_readline(virp);
5523}
5524
5525 void
5526write_viminfo_search_pattern(fp)
5527 FILE *fp;
5528{
5529 if (get_viminfo_parameter('/') != 0)
5530 {
5531#ifdef FEAT_SEARCH_EXTRA
5532 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5533 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5534#endif
5535 wvsp_one(fp, RE_SEARCH, "", '/');
Bram Moolenaar9b228712008-07-18 10:05:58 +00005536 wvsp_one(fp, RE_SUBST, _("Substitute "), '&');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
5538}
5539
5540 static void
5541wvsp_one(fp, idx, s, sc)
5542 FILE *fp; /* file to write to */
5543 int idx; /* spats[] index */
5544 char *s; /* search pat */
5545 int sc; /* dir char */
5546{
5547 if (spats[idx].pat != NULL)
5548 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005549 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 /* off.dir is not stored, it's reset to forward */
5551 fprintf(fp, "%c%c%c%c%ld%s%c",
5552 spats[idx].magic ? 'M' : 'm', /* magic */
5553 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5554 spats[idx].off.line ? 'L' : 'l', /* line offset */
5555 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5556 spats[idx].off.off, /* offset */
5557 last_idx == idx ? "~" : "", /* last used pat */
5558 sc);
5559 viminfo_writestring(fp, spats[idx].pat);
5560 }
5561}
5562#endif /* FEAT_VIMINFO */