blob: ff952d745ece99901e44e8896b0386f954854f26 [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
15static void save_re_pat __ARGS((int idx, char_u *pat, int magic));
16#ifdef FEAT_EVAL
17static 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{
64 int dir; /* search direction */
65 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 */
75 int no_scs; /* no smarcase for this pattern */
76 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 */
203 if (!(options & SEARCH_KEEP))
204 {
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
274 static void
275save_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 Moolenaar071d4272004-06-13 20:20:40 +0000291 no_hlsearch = FALSE;
292#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];
328 vim_free(spats[1].pat);
329 spats[1] = saved_spats[1];
330 last_idx = saved_last_idx;
331# ifdef FEAT_SEARCH_EXTRA
332 no_hlsearch = saved_no_hlsearch;
333# endif
334 }
335}
336#endif
337
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000338#if defined(EXITFREE) || defined(PROTO)
339 void
340free_search_patterns()
341{
342 vim_free(spats[0].pat);
343 vim_free(spats[1].pat);
344}
345#endif
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Return TRUE when case should be ignored for search pattern "pat".
349 * Uses the 'ignorecase' and 'smartcase' options.
350 */
351 int
352ignorecase(pat)
353 char_u *pat;
354{
355 char_u *p;
356 int ic;
357
358 ic = p_ic;
359 if (ic && !no_smartcase && p_scs
360#ifdef FEAT_INS_EXPAND
361 && !(ctrl_x_mode && curbuf->b_p_inf)
362#endif
363 )
364 {
365 /* don't ignore case if pattern has uppercase */
366 for (p = pat; *p; )
367 {
368#ifdef FEAT_MBYTE
369 int l;
370
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000371 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
373 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
374 {
375 ic = FALSE;
376 break;
377 }
378 p += l;
379 }
380 else
381#endif
382 if (*p == '\\' && p[1] != NUL) /* skip "\S" et al. */
383 p += 2;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000384 else if (isupper(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {
386 ic = FALSE;
387 break;
388 }
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000389 else
390 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 }
392 }
393 no_smartcase = FALSE;
394
395 return ic;
396}
397
398 char_u *
399last_search_pat()
400{
401 return spats[last_idx].pat;
402}
403
404/*
405 * Reset search direction to forward. For "gd" and "gD" commands.
406 */
407 void
408reset_search_dir()
409{
410 spats[0].off.dir = '/';
411}
412
413#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
414/*
415 * Set the last search pattern. For ":let @/ =" and viminfo.
416 * Also set the saved search pattern, so that this works in an autocommand.
417 */
418 void
419set_last_search_pat(s, idx, magic, setlast)
420 char_u *s;
421 int idx;
422 int magic;
423 int setlast;
424{
425 vim_free(spats[idx].pat);
426 /* An empty string means that nothing should be matched. */
427 if (*s == NUL)
428 spats[idx].pat = NULL;
429 else
430 spats[idx].pat = vim_strsave(s);
431 spats[idx].magic = magic;
432 spats[idx].no_scs = FALSE;
433 spats[idx].off.dir = '/';
434 spats[idx].off.line = FALSE;
435 spats[idx].off.end = FALSE;
436 spats[idx].off.off = 0;
437 if (setlast)
438 last_idx = idx;
439 if (save_level)
440 {
441 vim_free(saved_spats[idx].pat);
442 saved_spats[idx] = spats[0];
443 if (spats[idx].pat == NULL)
444 saved_spats[idx].pat = NULL;
445 else
446 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
447 saved_last_idx = last_idx;
448 }
449# ifdef FEAT_SEARCH_EXTRA
450 /* If 'hlsearch' set and search pat changed: need redraw. */
451 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000452 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453# endif
454}
455#endif
456
457#ifdef FEAT_SEARCH_EXTRA
458/*
459 * Get a regexp program for the last used search pattern.
460 * This is used for highlighting all matches in a window.
461 * Values returned in regmatch->regprog and regmatch->rmm_ic.
462 */
463 void
464last_pat_prog(regmatch)
465 regmmatch_T *regmatch;
466{
467 if (spats[last_idx].pat == NULL)
468 {
469 regmatch->regprog = NULL;
470 return;
471 }
472 ++emsg_off; /* So it doesn't beep if bad expr */
473 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
474 --emsg_off;
475}
476#endif
477
478/*
479 * lowest level search function.
480 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
481 * Start at position 'pos' and return the found position in 'pos'.
482 *
483 * if (options & SEARCH_MSG) == 0 don't give any messages
484 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
485 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
486 * if (options & SEARCH_HIS) put search pattern in history
487 * if (options & SEARCH_END) return position at end of match
488 * if (options & SEARCH_START) accept match at pos itself
489 * if (options & SEARCH_KEEP) keep previous search pattern
490 * if (options & SEARCH_FOLD) match only once in a closed fold
491 * if (options & SEARCH_PEEK) check for typed char, cancel search
492 *
493 * Return FAIL (zero) for failure, non-zero for success.
494 * When FEAT_EVAL is defined, returns the index of the first matching
495 * subpattern plus one; one if there was none.
496 */
Bram Moolenaar76929292008-01-06 19:07:36 +0000497/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 int
Bram Moolenaar76929292008-01-06 19:07:36 +0000499searchit(win, buf, pos, dir, pat, count, options, pat_use, stop_lnum, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 win_T *win; /* window to search in; can be NULL for a
501 buffer without a window! */
502 buf_T *buf;
503 pos_T *pos;
504 int dir;
505 char_u *pat;
506 long count;
507 int options;
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000508 int pat_use; /* which pattern to use when "pat" is empty */
509 linenr_T stop_lnum; /* stop after this line number when != 0 */
Bram Moolenaar76929292008-01-06 19:07:36 +0000510 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
512 int found;
513 linenr_T lnum; /* no init to shut up Apollo cc */
514 regmmatch_T regmatch;
515 char_u *ptr;
516 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000518 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 int loop;
520 pos_T start_pos;
521 int at_first_line;
522 int extra_col;
523 int match_ok;
524 long nmatched;
525 int submatch = 0;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000526 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527#ifdef FEAT_SEARCH_EXTRA
528 int break_loop = FALSE;
529#else
530# define break_loop FALSE
531#endif
532
533 if (search_regcomp(pat, RE_SEARCH, pat_use,
534 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
535 {
536 if ((options & SEARCH_MSG) && !rc_did_emsg)
537 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
538 return FAIL;
539 }
540
Bram Moolenaaraad86642008-03-10 20:34:59 +0000541 /* When not accepting a match at the start position set "extra_col" to a
542 * non-zero value. Don't do that when starting at MAXCOL, since MAXCOL +
543 * 1 is zero. */
544 if ((options & SEARCH_START) || pos->col == MAXCOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 extra_col = 0;
546#ifdef FEAT_MBYTE
547 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
548 else if (has_mbyte && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
549 && pos->col < MAXCOL - 2)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000550 {
551 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
552 if (*ptr == NUL)
553 extra_col = 1;
554 else
555 extra_col = (*mb_ptr2len)(ptr);
556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557#endif
558 else
559 extra_col = 1;
560
Bram Moolenaar280f1262006-01-30 00:14:18 +0000561 /*
562 * find the string
563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 called_emsg = FALSE;
565 do /* loop for count */
566 {
567 start_pos = *pos; /* remember start pos for detecting no match */
568 found = 0; /* default: not found */
569 at_first_line = TRUE; /* default: start in first line */
570 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
571 {
572 pos->lnum = 1;
573 pos->col = 0;
574 at_first_line = FALSE; /* not in first line now */
575 }
576
577 /*
578 * Start searching in current line, unless searching backwards and
579 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000580 * If we are searching backwards, in column 0, and not including the
581 * current position, gain some efficiency by skipping back a line.
582 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000584 if (dir == BACKWARD && start_pos.col == 0
585 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 {
587 lnum = pos->lnum - 1;
588 at_first_line = FALSE;
589 }
590 else
591 lnum = pos->lnum;
592
593 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
594 {
595 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
596 lnum += dir, at_first_line = FALSE)
597 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000598 /* Stop after checking "stop_lnum", if it's set. */
599 if (stop_lnum != 0 && (dir == FORWARD
600 ? lnum > stop_lnum : lnum < stop_lnum))
601 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000602#ifdef FEAT_RELTIME
603 /* Stop after passing the "tm" time limit. */
604 if (tm != NULL && profile_passed_limit(tm))
605 break;
606#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000609 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000612 lnum, (colnr_T)0,
613#ifdef FEAT_RELTIME
614 tm
615#else
616 NULL
617#endif
618 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 /* Abort searching on an error (e.g., out of stack). */
620 if (called_emsg)
621 break;
622 if (nmatched > 0)
623 {
624 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000625 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000627#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000629#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000630 /* "lnum" may be past end of buffer for "\n\zs". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000631 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
632 ptr = (char_u *)"";
633 else
634 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 /*
637 * Forward search in the first line: match should be after
638 * the start position. If not, continue at the end of the
639 * match (this is vi compatible) or on the next char.
640 */
641 if (dir == FORWARD && at_first_line)
642 {
643 match_ok = TRUE;
644 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000645 * When the match starts in a next line it's certainly
646 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 * When match lands on a NUL the cursor will be put
648 * one back afterwards, compare with that position,
649 * otherwise "/$" will get stuck on end of line.
650 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000651 while (matchpos.lnum == 0
652 && ((options & SEARCH_END)
653 ? (nmatched == 1
654 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000656 : ((int)matchpos.col
657 - (ptr[matchpos.col] == NUL)
658 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660 /*
661 * If vi-compatible searching, continue at the end
662 * of the match, otherwise continue one position
663 * forward.
664 */
665 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
666 {
667 if (nmatched > 1)
668 {
669 /* end is in next line, thus no match in
670 * this line */
671 match_ok = FALSE;
672 break;
673 }
674 matchcol = endpos.col;
675 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000676 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 && ptr[matchcol] != NUL)
678 {
679#ifdef FEAT_MBYTE
680 if (has_mbyte)
681 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000682 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 else
684#endif
685 ++matchcol;
686 }
687 }
688 else
689 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000690 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 if (ptr[matchcol] != NUL)
692 {
693#ifdef FEAT_MBYTE
694 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000695 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 + matchcol);
697 else
698#endif
699 ++matchcol;
700 }
701 }
702 if (ptr[matchcol] == NUL
703 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000704 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000705 matchcol,
706#ifdef FEAT_RELTIME
707 tm
708#else
709 NULL
710#endif
711 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 match_ok = FALSE;
714 break;
715 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000716 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 endpos = regmatch.endpos[0];
718# ifdef FEAT_EVAL
719 submatch = first_submatch(&regmatch);
720# endif
721
722 /* Need to get the line pointer again, a
723 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000724 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 }
726 if (!match_ok)
727 continue;
728 }
729 if (dir == BACKWARD)
730 {
731 /*
732 * Now, if there are multiple matches on this line,
733 * we have to get the last one. Or the last one before
734 * the cursor, if we're on that line.
735 * When putting the new cursor at the end, compare
736 * relative to the end of the match.
737 */
738 match_ok = FALSE;
739 for (;;)
740 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000741 /* Remember a position that is before the start
742 * position, we use it if it's the last match in
743 * the line. Always accept a position after
744 * wrapping around. */
745 if (loop
746 || ((options & SEARCH_END)
747 ? (lnum + regmatch.endpos[0].lnum
748 < start_pos.lnum
749 || (lnum + regmatch.endpos[0].lnum
750 == start_pos.lnum
751 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000753 <= (int)start_pos.col))
754 : (lnum + regmatch.startpos[0].lnum
755 < start_pos.lnum
756 || (lnum + regmatch.startpos[0].lnum
757 == start_pos.lnum
758 && (int)regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000760 <= (int)start_pos.col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000763 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 endpos = regmatch.endpos[0];
765# ifdef FEAT_EVAL
766 submatch = first_submatch(&regmatch);
767# endif
768 }
769 else
770 break;
771
772 /*
773 * We found a valid match, now check if there is
774 * another one after it.
775 * If vi-compatible searching, continue at the end
776 * of the match, otherwise continue one position
777 * forward.
778 */
779 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
780 {
781 if (nmatched > 1)
782 break;
783 matchcol = endpos.col;
784 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000785 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 && ptr[matchcol] != NUL)
787 {
788#ifdef FEAT_MBYTE
789 if (has_mbyte)
790 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000791 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 else
793#endif
794 ++matchcol;
795 }
796 }
797 else
798 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000799 /* Stop when the match is in a next line. */
800 if (matchpos.lnum > 0)
801 break;
802 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 if (ptr[matchcol] != NUL)
804 {
805#ifdef FEAT_MBYTE
806 if (has_mbyte)
807 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000808 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 else
810#endif
811 ++matchcol;
812 }
813 }
814 if (ptr[matchcol] == NUL
815 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000816 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000817 matchcol,
818#ifdef FEAT_RELTIME
819 tm
820#else
821 NULL
822#endif
823 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 break;
825
826 /* Need to get the line pointer again, a
827 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000828 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 }
830
831 /*
832 * If there is only a match after the cursor, skip
833 * this match.
834 */
835 if (!match_ok)
836 continue;
837 }
838
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000839 /* With the SEARCH_END option move to the last character
840 * of the match. Don't do it for an empty match, end
841 * should be same as start then. */
842 if (options & SEARCH_END && !(options & SEARCH_NOOF)
843 && !(matchpos.lnum == endpos.lnum
844 && matchpos.col == endpos.col))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000846 /* For a match in the first column, set the position
847 * on the NUL in the previous line. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000848 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000849 pos->col = endpos.col;
850 if (endpos.col == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000851 {
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000852 if (pos->lnum > 1) /* just in case */
853 {
854 --pos->lnum;
855 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
856 pos->lnum, FALSE));
857 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000858 }
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000859 else
860 {
861 --pos->col;
862#ifdef FEAT_MBYTE
863 if (has_mbyte
864 && pos->lnum <= buf->b_ml.ml_line_count)
865 {
866 ptr = ml_get_buf(buf, pos->lnum, FALSE);
867 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
868 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000869#endif
Bram Moolenaar5bcbd532008-02-20 12:43:01 +0000870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 else
873 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000874 pos->lnum = lnum + matchpos.lnum;
875 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
877#ifdef FEAT_VIRTUALEDIT
878 pos->coladd = 0;
879#endif
880 found = 1;
881
882 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000883 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 search_match_endcol = endpos.col;
885 break;
886 }
887 line_breakcheck(); /* stop if ctrl-C typed */
888 if (got_int)
889 break;
890
891#ifdef FEAT_SEARCH_EXTRA
892 /* Cancel searching if a character was typed. Used for
893 * 'incsearch'. Don't check too often, that would slowdown
894 * searching too much. */
895 if ((options & SEARCH_PEEK)
896 && ((lnum - pos->lnum) & 0x3f) == 0
897 && char_avail())
898 {
899 break_loop = TRUE;
900 break;
901 }
902#endif
903
904 if (loop && lnum == start_pos.lnum)
905 break; /* if second loop, stop where started */
906 }
907 at_first_line = FALSE;
908
909 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000910 * Stop the search if wrapscan isn't set, "stop_lnum" is
911 * specified, after an interrupt, after a match and after looping
912 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000914 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
915 || break_loop || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 break;
917
918 /*
919 * If 'wrapscan' is set we continue at the other end of the file.
920 * If 'shortmess' does not contain 's', we give a message.
921 * This message is also remembered in keep_msg for when the screen
922 * is redrawn. The keep_msg is cleared whenever another message is
923 * written.
924 */
925 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +0000929 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
930 give_warning((char_u *)_(dir == BACKWARD
931 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
933 if (got_int || called_emsg || break_loop)
934 break;
935 }
936 while (--count > 0 && found); /* stop after count matches or no match */
937
938 vim_free(regmatch.regprog);
939
Bram Moolenaar280f1262006-01-30 00:14:18 +0000940 called_emsg |= save_called_emsg;
941
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 if (!found) /* did not find it */
943 {
944 if (got_int)
945 EMSG(_(e_interr));
946 else if ((options & SEARCH_MSG) == SEARCH_MSG)
947 {
948 if (p_ws)
949 EMSG2(_(e_patnotf2), mr_pattern);
950 else if (lnum == 0)
951 EMSG2(_("E384: search hit TOP without match for: %s"),
952 mr_pattern);
953 else
954 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
955 mr_pattern);
956 }
957 return FAIL;
958 }
959
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000960 /* A pattern like "\n\zs" may go past the last line. */
961 if (pos->lnum > buf->b_ml.ml_line_count)
962 {
963 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000964 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000965 if (pos->col > 0)
966 --pos->col;
967 }
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 return submatch + 1;
970}
971
972#ifdef FEAT_EVAL
973/*
974 * Return the number of the first subpat that matched.
975 */
976 static int
977first_submatch(rp)
978 regmmatch_T *rp;
979{
980 int submatch;
981
982 for (submatch = 1; ; ++submatch)
983 {
984 if (rp->startpos[submatch].lnum >= 0)
985 break;
986 if (submatch == 9)
987 {
988 submatch = 0;
989 break;
990 }
991 }
992 return submatch;
993}
994#endif
995
996/*
997 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000998 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 * If 'dirc' is 0: use previous dir.
1000 * If 'pat' is NULL or empty : use previous string.
1001 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1002 * If 'options & SEARCH_ECHO': echo the search command and handle options
1003 * If 'options & SEARCH_MSG' : may give error message
1004 * If 'options & SEARCH_OPT' : interpret optional flags
1005 * If 'options & SEARCH_HIS' : put search pattern in history
1006 * If 'options & SEARCH_NOOF': don't add offset to position
1007 * If 'options & SEARCH_MARK': set previous context mark
1008 * If 'options & SEARCH_KEEP': keep previous search pattern
1009 * If 'options & SEARCH_START': accept match at curpos itself
1010 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1011 *
1012 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1013 * makes the movement linewise without moving the match position.
1014 *
1015 * return 0 for failure, 1 for found, 2 for found and line offset added
1016 */
1017 int
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001018do_search(oap, dirc, pat, count, options, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 oparg_T *oap; /* can be NULL */
1020 int dirc; /* '/' or '?' */
1021 char_u *pat;
1022 long count;
1023 int options;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001024 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
1026 pos_T pos; /* position of the last match */
1027 char_u *searchstr;
1028 struct soffset old_off;
1029 int retval; /* Return value */
1030 char_u *p;
1031 long c;
1032 char_u *dircp;
1033 char_u *strcopy = NULL;
1034 char_u *ps;
1035
1036 /*
1037 * A line offset is not remembered, this is vi compatible.
1038 */
1039 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1040 {
1041 spats[0].off.line = FALSE;
1042 spats[0].off.off = 0;
1043 }
1044
1045 /*
1046 * Save the values for when (options & SEARCH_KEEP) is used.
1047 * (there is no "if ()" around this because gcc wants them initialized)
1048 */
1049 old_off = spats[0].off;
1050
1051 pos = curwin->w_cursor; /* start searching at the cursor position */
1052
1053 /*
1054 * Find out the direction of the search.
1055 */
1056 if (dirc == 0)
1057 dirc = spats[0].off.dir;
1058 else
1059 spats[0].off.dir = dirc;
1060 if (options & SEARCH_REV)
1061 {
1062#ifdef WIN32
1063 /* There is a bug in the Visual C++ 2.2 compiler which means that
1064 * dirc always ends up being '/' */
1065 dirc = (dirc == '/') ? '?' : '/';
1066#else
1067 if (dirc == '/')
1068 dirc = '?';
1069 else
1070 dirc = '/';
1071#endif
1072 }
1073
1074#ifdef FEAT_FOLDING
1075 /* If the cursor is in a closed fold, don't find another match in the same
1076 * fold. */
1077 if (dirc == '/')
1078 {
1079 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1080 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1081 }
1082 else
1083 {
1084 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1085 pos.col = 0;
1086 }
1087#endif
1088
1089#ifdef FEAT_SEARCH_EXTRA
1090 /*
1091 * Turn 'hlsearch' highlighting back on.
1092 */
1093 if (no_hlsearch && !(options & SEARCH_KEEP))
1094 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001095 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 no_hlsearch = FALSE;
1097 }
1098#endif
1099
1100 /*
1101 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1102 */
1103 for (;;)
1104 {
1105 searchstr = pat;
1106 dircp = NULL;
1107 /* use previous pattern */
1108 if (pat == NULL || *pat == NUL || *pat == dirc)
1109 {
1110 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1111 {
1112 EMSG(_(e_noprevre));
1113 retval = 0;
1114 goto end_do_search;
1115 }
1116 /* make search_regcomp() use spats[RE_SEARCH].pat */
1117 searchstr = (char_u *)"";
1118 }
1119
1120 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1121 {
1122 /*
1123 * Find end of regular expression.
1124 * If there is a matching '/' or '?', toss it.
1125 */
1126 ps = strcopy;
1127 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1128 if (strcopy != ps)
1129 {
1130 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001131 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 pat = strcopy;
1133 searchstr = strcopy;
1134 }
1135 if (*p == dirc)
1136 {
1137 dircp = p; /* remember where we put the NUL */
1138 *p++ = NUL;
1139 }
1140 spats[0].off.line = FALSE;
1141 spats[0].off.end = FALSE;
1142 spats[0].off.off = 0;
1143 /*
1144 * Check for a line offset or a character offset.
1145 * For get_address (echo off) we don't check for a character
1146 * offset, because it is meaningless and the 's' could be a
1147 * substitute command.
1148 */
1149 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1150 spats[0].off.line = TRUE;
1151 else if ((options & SEARCH_OPT) &&
1152 (*p == 'e' || *p == 's' || *p == 'b'))
1153 {
1154 if (*p == 'e') /* end */
1155 spats[0].off.end = SEARCH_END;
1156 ++p;
1157 }
1158 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1159 {
1160 /* 'nr' or '+nr' or '-nr' */
1161 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1162 spats[0].off.off = atol((char *)p);
1163 else if (*p == '-') /* single '-' */
1164 spats[0].off.off = -1;
1165 else /* single '+' */
1166 spats[0].off.off = 1;
1167 ++p;
1168 while (VIM_ISDIGIT(*p)) /* skip number */
1169 ++p;
1170 }
1171
1172 /* compute length of search command for get_address() */
1173 searchcmdlen += (int)(p - pat);
1174
1175 pat = p; /* put pat after search command */
1176 }
1177
1178 if ((options & SEARCH_ECHO) && messaging()
1179 && !cmd_silent && msg_silent == 0)
1180 {
1181 char_u *msgbuf;
1182 char_u *trunc;
1183
1184 if (*searchstr == NUL)
1185 p = spats[last_idx].pat;
1186 else
1187 p = searchstr;
1188 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1189 if (msgbuf != NULL)
1190 {
1191 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001192#ifdef FEAT_MBYTE
1193 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1194 {
1195 /* Use a space to draw the composing char on. */
1196 msgbuf[1] = ' ';
1197 STRCPY(msgbuf + 2, p);
1198 }
1199 else
1200#endif
1201 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1203 {
1204 p = msgbuf + STRLEN(msgbuf);
1205 *p++ = dirc;
1206 if (spats[0].off.end)
1207 *p++ = 'e';
1208 else if (!spats[0].off.line)
1209 *p++ = 's';
1210 if (spats[0].off.off > 0 || spats[0].off.line)
1211 *p++ = '+';
1212 if (spats[0].off.off != 0 || spats[0].off.line)
1213 sprintf((char *)p, "%ld", spats[0].off.off);
1214 else
1215 *p = NUL;
1216 }
1217
1218 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001219 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
1221#ifdef FEAT_RIGHTLEFT
1222 /* The search pattern could be shown on the right in rightleft
1223 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1224 * it would be blanked out again very soon. Show it on the
1225 * left, but do reverse the text. */
1226 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1227 {
1228 char_u *r;
1229
1230 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1231 if (r != NULL)
1232 {
1233 vim_free(trunc);
1234 trunc = r;
1235 }
1236 }
1237#endif
1238 if (trunc != NULL)
1239 {
1240 msg_outtrans(trunc);
1241 vim_free(trunc);
1242 }
1243 else
1244 msg_outtrans(msgbuf);
1245 msg_clr_eos();
1246 msg_check();
1247 vim_free(msgbuf);
1248
1249 gotocmdline(FALSE);
1250 out_flush();
1251 msg_nowait = TRUE; /* don't wait for this message */
1252 }
1253 }
1254
1255 /*
1256 * If there is a character offset, subtract it from the current
1257 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001258 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 * This is not done for a line offset, because then we would not be vi
1260 * compatible.
1261 */
Bram Moolenaared203462004-06-16 11:19:22 +00001262 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264 if (spats[0].off.off > 0)
1265 {
1266 for (c = spats[0].off.off; c; --c)
1267 if (decl(&pos) == -1)
1268 break;
1269 if (c) /* at start of buffer */
1270 {
1271 pos.lnum = 0; /* allow lnum == 0 here */
1272 pos.col = MAXCOL;
1273 }
1274 }
1275 else
1276 {
1277 for (c = spats[0].off.off; c; ++c)
1278 if (incl(&pos) == -1)
1279 break;
1280 if (c) /* at end of buffer */
1281 {
1282 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1283 pos.col = 0;
1284 }
1285 }
1286 }
1287
1288#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1289 if (p_altkeymap && curwin->w_p_rl)
1290 lrFswap(searchstr,0);
1291#endif
1292
1293 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1294 searchstr, count, spats[0].off.end + (options &
1295 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1296 + SEARCH_MSG + SEARCH_START
1297 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001298 RE_LAST, (linenr_T)0, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 if (dircp != NULL)
1301 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1302 if (c == FAIL)
1303 {
1304 retval = 0;
1305 goto end_do_search;
1306 }
1307 if (spats[0].off.end && oap != NULL)
1308 oap->inclusive = TRUE; /* 'e' includes last character */
1309
1310 retval = 1; /* pattern found */
1311
1312 /*
1313 * Add character and/or line offset
1314 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001315 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 {
1317 if (spats[0].off.line) /* Add the offset to the line number. */
1318 {
1319 c = pos.lnum + spats[0].off.off;
1320 if (c < 1)
1321 pos.lnum = 1;
1322 else if (c > curbuf->b_ml.ml_line_count)
1323 pos.lnum = curbuf->b_ml.ml_line_count;
1324 else
1325 pos.lnum = c;
1326 pos.col = 0;
1327
1328 retval = 2; /* pattern found, line offset added */
1329 }
Bram Moolenaared203462004-06-16 11:19:22 +00001330 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 {
1332 /* to the right, check for end of file */
1333 if (spats[0].off.off > 0)
1334 {
1335 for (c = spats[0].off.off; c; --c)
1336 if (incl(&pos) == -1)
1337 break;
1338 }
1339 /* to the left, check for start of file */
1340 else
1341 {
1342 if ((c = pos.col + spats[0].off.off) >= 0)
1343 pos.col = c;
1344 else
1345 for (c = spats[0].off.off; c; ++c)
1346 if (decl(&pos) == -1)
1347 break;
1348 }
1349 }
1350 }
1351
1352 /*
1353 * The search command can be followed by a ';' to do another search.
1354 * For example: "/pat/;/foo/+3;?bar"
1355 * This is like doing another search command, except:
1356 * - The remembered direction '/' or '?' is from the first search.
1357 * - When an error happens the cursor isn't moved at all.
1358 * Don't do this when called by get_address() (it handles ';' itself).
1359 */
1360 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1361 break;
1362
1363 dirc = *++pat;
1364 if (dirc != '?' && dirc != '/')
1365 {
1366 retval = 0;
1367 EMSG(_("E386: Expected '?' or '/' after ';'"));
1368 goto end_do_search;
1369 }
1370 ++pat;
1371 }
1372
1373 if (options & SEARCH_MARK)
1374 setpcmark();
1375 curwin->w_cursor = pos;
1376 curwin->w_set_curswant = TRUE;
1377
1378end_do_search:
1379 if (options & SEARCH_KEEP)
1380 spats[0].off = old_off;
1381 vim_free(strcopy);
1382
1383 return retval;
1384}
1385
1386#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1387/*
1388 * search_for_exact_line(buf, pos, dir, pat)
1389 *
1390 * Search for a line starting with the given pattern (ignoring leading
1391 * white-space), starting from pos and going in direction dir. pos will
1392 * contain the position of the match found. Blank lines match only if
1393 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1394 * Return OK for success, or FAIL if no line found.
1395 */
1396 int
1397search_for_exact_line(buf, pos, dir, pat)
1398 buf_T *buf;
1399 pos_T *pos;
1400 int dir;
1401 char_u *pat;
1402{
1403 linenr_T start = 0;
1404 char_u *ptr;
1405 char_u *p;
1406
1407 if (buf->b_ml.ml_line_count == 0)
1408 return FAIL;
1409 for (;;)
1410 {
1411 pos->lnum += dir;
1412 if (pos->lnum < 1)
1413 {
1414 if (p_ws)
1415 {
1416 pos->lnum = buf->b_ml.ml_line_count;
1417 if (!shortmess(SHM_SEARCH))
1418 give_warning((char_u *)_(top_bot_msg), TRUE);
1419 }
1420 else
1421 {
1422 pos->lnum = 1;
1423 break;
1424 }
1425 }
1426 else if (pos->lnum > buf->b_ml.ml_line_count)
1427 {
1428 if (p_ws)
1429 {
1430 pos->lnum = 1;
1431 if (!shortmess(SHM_SEARCH))
1432 give_warning((char_u *)_(bot_top_msg), TRUE);
1433 }
1434 else
1435 {
1436 pos->lnum = 1;
1437 break;
1438 }
1439 }
1440 if (pos->lnum == start)
1441 break;
1442 if (start == 0)
1443 start = pos->lnum;
1444 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1445 p = skipwhite(ptr);
1446 pos->col = (colnr_T) (p - ptr);
1447
1448 /* when adding lines the matching line may be empty but it is not
1449 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001450 if ((compl_cont_status & CONT_ADDING)
1451 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 {
1453 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1454 return OK;
1455 }
1456 else if (*p != NUL) /* ignore empty lines */
1457 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001458 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1459 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 return OK;
1461 }
1462 }
1463 return FAIL;
1464}
1465#endif /* FEAT_INS_EXPAND */
1466
1467/*
1468 * Character Searches
1469 */
1470
1471/*
1472 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1473 * position of the character, otherwise move to just before the char.
1474 * Do this "cap->count1" times.
1475 * Return FAIL or OK.
1476 */
1477 int
1478searchc(cap, t_cmd)
1479 cmdarg_T *cap;
1480 int t_cmd;
1481{
1482 int c = cap->nchar; /* char to search for */
1483 int dir = cap->arg; /* TRUE for searching forward */
1484 long count = cap->count1; /* repeat count */
1485 static int lastc = NUL; /* last character searched for */
1486 static int lastcdir; /* last direction of character search */
1487 static int last_t_cmd; /* last search t_cmd */
1488 int col;
1489 char_u *p;
1490 int len;
1491#ifdef FEAT_MBYTE
1492 static char_u bytes[MB_MAXBYTES];
1493 static int bytelen = 1; /* >1 for multi-byte char */
1494#endif
1495
1496 if (c != NUL) /* normal search: remember args for repeat */
1497 {
1498 if (!KeyStuffed) /* don't remember when redoing */
1499 {
1500 lastc = c;
1501 lastcdir = dir;
1502 last_t_cmd = t_cmd;
1503#ifdef FEAT_MBYTE
1504 bytelen = (*mb_char2bytes)(c, bytes);
1505 if (cap->ncharC1 != 0)
1506 {
1507 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1508 if (cap->ncharC2 != 0)
1509 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1510 }
1511#endif
1512 }
1513 }
1514 else /* repeat previous search */
1515 {
1516 if (lastc == NUL)
1517 return FAIL;
1518 if (dir) /* repeat in opposite direction */
1519 dir = -lastcdir;
1520 else
1521 dir = lastcdir;
1522 t_cmd = last_t_cmd;
1523 c = lastc;
1524 /* For multi-byte re-use last bytes[] and bytelen. */
1525 }
1526
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001527 if (dir == BACKWARD)
1528 cap->oap->inclusive = FALSE;
1529 else
1530 cap->oap->inclusive = TRUE;
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 p = ml_get_curline();
1533 col = curwin->w_cursor.col;
1534 len = (int)STRLEN(p);
1535
1536 while (count--)
1537 {
1538#ifdef FEAT_MBYTE
1539 if (has_mbyte)
1540 {
1541 for (;;)
1542 {
1543 if (dir > 0)
1544 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001545 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (col >= len)
1547 return FAIL;
1548 }
1549 else
1550 {
1551 if (col == 0)
1552 return FAIL;
1553 col -= (*mb_head_off)(p, p + col - 1) + 1;
1554 }
1555 if (bytelen == 1)
1556 {
1557 if (p[col] == c)
1558 break;
1559 }
1560 else
1561 {
1562 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1563 break;
1564 }
1565 }
1566 }
1567 else
1568#endif
1569 {
1570 for (;;)
1571 {
1572 if ((col += dir) < 0 || col >= len)
1573 return FAIL;
1574 if (p[col] == c)
1575 break;
1576 }
1577 }
1578 }
1579
1580 if (t_cmd)
1581 {
1582 /* backup to before the character (possibly double-byte) */
1583 col -= dir;
1584#ifdef FEAT_MBYTE
1585 if (has_mbyte)
1586 {
1587 if (dir < 0)
1588 /* Landed on the search char which is bytelen long */
1589 col += bytelen - 1;
1590 else
1591 /* To previous char, which may be multi-byte. */
1592 col -= (*mb_head_off)(p, p + col);
1593 }
1594#endif
1595 }
1596 curwin->w_cursor.col = col;
1597
1598 return OK;
1599}
1600
1601/*
1602 * "Other" Searches
1603 */
1604
1605/*
1606 * findmatch - find the matching paren or brace
1607 *
1608 * Improvement over vi: Braces inside quotes are ignored.
1609 */
1610 pos_T *
1611findmatch(oap, initc)
1612 oparg_T *oap;
1613 int initc;
1614{
1615 return findmatchlimit(oap, initc, 0, 0);
1616}
1617
1618/*
1619 * Return TRUE if the character before "linep[col]" equals "ch".
1620 * Return FALSE if "col" is zero.
1621 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1622 * is NULL.
1623 * Handles multibyte string correctly.
1624 */
1625 static int
1626check_prevcol(linep, col, ch, prevcol)
1627 char_u *linep;
1628 int col;
1629 int ch;
1630 int *prevcol;
1631{
1632 --col;
1633#ifdef FEAT_MBYTE
1634 if (col > 0 && has_mbyte)
1635 col -= (*mb_head_off)(linep, linep + col);
1636#endif
1637 if (prevcol)
1638 *prevcol = col;
1639 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1640}
1641
1642/*
1643 * findmatchlimit -- find the matching paren or brace, if it exists within
1644 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1645 * the edge of the file.
1646 *
1647 * "initc" is the character to find a match for. NUL means to find the
1648 * character at or after the cursor.
1649 *
1650 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1651 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1652 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1653 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001654 *
1655 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1656 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
1658
1659 pos_T *
1660findmatchlimit(oap, initc, flags, maxtravel)
1661 oparg_T *oap;
1662 int initc;
1663 int flags;
1664 int maxtravel;
1665{
1666 static pos_T pos; /* current search position */
1667 int findc = 0; /* matching brace */
1668 int c;
1669 int count = 0; /* cumulative number of braces */
1670 int backwards = FALSE; /* init for gcc */
1671 int inquote = FALSE; /* TRUE when inside quotes */
1672 char_u *linep; /* pointer to current line */
1673 char_u *ptr;
1674 int do_quotes; /* check for quotes in current line */
1675 int at_start; /* do_quotes value at start position */
1676 int hash_dir = 0; /* Direction searched for # things */
1677 int comment_dir = 0; /* Direction searched for comments */
1678 pos_T match_pos; /* Where last slash-star was found */
1679 int start_in_quotes; /* start position is in quotes */
1680 int traveled = 0; /* how far we've searched so far */
1681 int ignore_cend = FALSE; /* ignore comment end */
1682 int cpo_match; /* vi compatible matching */
1683 int cpo_bsl; /* don't recognize backslashes */
1684 int match_escaped = 0; /* search for escaped match */
1685 int dir; /* Direction to search */
1686 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001687#ifdef FEAT_LISP
1688 int lispcomm = FALSE; /* inside of Lisp-style comment */
1689 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 pos = curwin->w_cursor;
1693 linep = ml_get(pos.lnum);
1694
1695 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1696 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1697
1698 /* Direction to search when initc is '/', '*' or '#' */
1699 if (flags & FM_BACKWARD)
1700 dir = BACKWARD;
1701 else if (flags & FM_FORWARD)
1702 dir = FORWARD;
1703 else
1704 dir = 0;
1705
1706 /*
1707 * if initc given, look in the table for the matching character
1708 * '/' and '*' are special cases: look for start or end of comment.
1709 * When '/' is used, we ignore running backwards into an star-slash, for
1710 * "[*" command, we just want to find any comment.
1711 */
1712 if (initc == '/' || initc == '*')
1713 {
1714 comment_dir = dir;
1715 if (initc == '/')
1716 ignore_cend = TRUE;
1717 backwards = (dir == FORWARD) ? FALSE : TRUE;
1718 initc = NUL;
1719 }
1720 else if (initc != '#' && initc != NUL)
1721 {
1722 /* 'matchpairs' is "x:y,x:y" */
1723 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1724 {
1725 if (*ptr == initc)
1726 {
1727 findc = initc;
1728 initc = ptr[2];
1729 backwards = TRUE;
1730 break;
1731 }
1732 ptr += 2;
1733 if (*ptr == initc)
1734 {
1735 findc = initc;
1736 initc = ptr[-2];
1737 backwards = FALSE;
1738 break;
1739 }
1740 if (ptr[1] != ',')
1741 break;
1742 }
1743 if (!findc) /* invalid initc! */
1744 return NULL;
1745 }
1746 /*
1747 * Either initc is '#', or no initc was given and we need to look under the
1748 * cursor.
1749 */
1750 else
1751 {
1752 if (initc == '#')
1753 {
1754 hash_dir = dir;
1755 }
1756 else
1757 {
1758 /*
1759 * initc was not given, must look for something to match under
1760 * or near the cursor.
1761 * Only check for special things when 'cpo' doesn't have '%'.
1762 */
1763 if (!cpo_match)
1764 {
1765 /* Are we before or at #if, #else etc.? */
1766 ptr = skipwhite(linep);
1767 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1768 {
1769 ptr = skipwhite(ptr + 1);
1770 if ( STRNCMP(ptr, "if", 2) == 0
1771 || STRNCMP(ptr, "endif", 5) == 0
1772 || STRNCMP(ptr, "el", 2) == 0)
1773 hash_dir = 1;
1774 }
1775
1776 /* Are we on a comment? */
1777 else if (linep[pos.col] == '/')
1778 {
1779 if (linep[pos.col + 1] == '*')
1780 {
1781 comment_dir = FORWARD;
1782 backwards = FALSE;
1783 pos.col++;
1784 }
1785 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1786 {
1787 comment_dir = BACKWARD;
1788 backwards = TRUE;
1789 pos.col--;
1790 }
1791 }
1792 else if (linep[pos.col] == '*')
1793 {
1794 if (linep[pos.col + 1] == '/')
1795 {
1796 comment_dir = BACKWARD;
1797 backwards = TRUE;
1798 }
1799 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1800 {
1801 comment_dir = FORWARD;
1802 backwards = FALSE;
1803 }
1804 }
1805 }
1806
1807 /*
1808 * If we are not on a comment or the # at the start of a line, then
1809 * look for brace anywhere on this line after the cursor.
1810 */
1811 if (!hash_dir && !comment_dir)
1812 {
1813 /*
1814 * Find the brace under or after the cursor.
1815 * If beyond the end of the line, use the last character in
1816 * the line.
1817 */
1818 if (linep[pos.col] == NUL && pos.col)
1819 --pos.col;
1820 for (;;)
1821 {
1822 initc = linep[pos.col];
1823 if (initc == NUL)
1824 break;
1825
1826 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1827 {
1828 if (*ptr == initc)
1829 {
1830 findc = ptr[2];
1831 backwards = FALSE;
1832 break;
1833 }
1834 ptr += 2;
1835 if (*ptr == initc)
1836 {
1837 findc = ptr[-2];
1838 backwards = TRUE;
1839 break;
1840 }
1841 if (!*++ptr)
1842 break;
1843 }
1844 if (findc)
1845 break;
1846#ifdef FEAT_MBYTE
1847 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001848 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 else
1850#endif
1851 ++pos.col;
1852 }
1853 if (!findc)
1854 {
1855 /* no brace in the line, maybe use " #if" then */
1856 if (!cpo_match && *skipwhite(linep) == '#')
1857 hash_dir = 1;
1858 else
1859 return NULL;
1860 }
1861 else if (!cpo_bsl)
1862 {
1863 int col, bslcnt = 0;
1864
1865 /* Set "match_escaped" if there are an odd number of
1866 * backslashes. */
1867 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1868 bslcnt++;
1869 match_escaped = (bslcnt & 1);
1870 }
1871 }
1872 }
1873 if (hash_dir)
1874 {
1875 /*
1876 * Look for matching #if, #else, #elif, or #endif
1877 */
1878 if (oap != NULL)
1879 oap->motion_type = MLINE; /* Linewise for this case only */
1880 if (initc != '#')
1881 {
1882 ptr = skipwhite(skipwhite(linep) + 1);
1883 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1884 hash_dir = 1;
1885 else if (STRNCMP(ptr, "endif", 5) == 0)
1886 hash_dir = -1;
1887 else
1888 return NULL;
1889 }
1890 pos.col = 0;
1891 while (!got_int)
1892 {
1893 if (hash_dir > 0)
1894 {
1895 if (pos.lnum == curbuf->b_ml.ml_line_count)
1896 break;
1897 }
1898 else if (pos.lnum == 1)
1899 break;
1900 pos.lnum += hash_dir;
1901 linep = ml_get(pos.lnum);
1902 line_breakcheck(); /* check for CTRL-C typed */
1903 ptr = skipwhite(linep);
1904 if (*ptr != '#')
1905 continue;
1906 pos.col = (colnr_T) (ptr - linep);
1907 ptr = skipwhite(ptr + 1);
1908 if (hash_dir > 0)
1909 {
1910 if (STRNCMP(ptr, "if", 2) == 0)
1911 count++;
1912 else if (STRNCMP(ptr, "el", 2) == 0)
1913 {
1914 if (count == 0)
1915 return &pos;
1916 }
1917 else if (STRNCMP(ptr, "endif", 5) == 0)
1918 {
1919 if (count == 0)
1920 return &pos;
1921 count--;
1922 }
1923 }
1924 else
1925 {
1926 if (STRNCMP(ptr, "if", 2) == 0)
1927 {
1928 if (count == 0)
1929 return &pos;
1930 count--;
1931 }
1932 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1933 {
1934 if (count == 0)
1935 return &pos;
1936 }
1937 else if (STRNCMP(ptr, "endif", 5) == 0)
1938 count++;
1939 }
1940 }
1941 return NULL;
1942 }
1943 }
1944
1945#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00001946 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 * paren/brace in the other direction. */
1948 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1949 backwards = !backwards;
1950#endif
1951
1952 do_quotes = -1;
1953 start_in_quotes = MAYBE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001954 clearpos(&match_pos);
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001957 if ((backwards && comment_dir)
1958#ifdef FEAT_LISP
1959 || lisp
1960#endif
1961 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001963#ifdef FEAT_LISP
1964 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1965 lispcomm = TRUE; /* find match inside this comment */
1966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 while (!got_int)
1968 {
1969 /*
1970 * Go to the next position, forward or backward. We could use
1971 * inc() and dec() here, but that is much slower
1972 */
1973 if (backwards)
1974 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001975#ifdef FEAT_LISP
1976 /* char to match is inside of comment, don't search outside */
1977 if (lispcomm && pos.col < (colnr_T)comment_col)
1978 break;
1979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (pos.col == 0) /* at start of line, go to prev. one */
1981 {
1982 if (pos.lnum == 1) /* start of file */
1983 break;
1984 --pos.lnum;
1985
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001986 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 break;
1988
1989 linep = ml_get(pos.lnum);
1990 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1991 do_quotes = -1;
1992 line_breakcheck();
1993
1994 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001995 if (comment_dir
1996#ifdef FEAT_LISP
1997 || lisp
1998#endif
1999 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002001#ifdef FEAT_LISP
2002 /* skip comment */
2003 if (lisp && comment_col != MAXCOL)
2004 pos.col = comment_col;
2005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007 else
2008 {
2009 --pos.col;
2010#ifdef FEAT_MBYTE
2011 if (has_mbyte)
2012 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2013#endif
2014 }
2015 }
2016 else /* forward search */
2017 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002018 if (linep[pos.col] == NUL
2019 /* at end of line, go to next one */
2020#ifdef FEAT_LISP
2021 /* don't search for match in comment */
2022 || (lisp && comment_col != MAXCOL
2023 && pos.col == (colnr_T)comment_col)
2024#endif
2025 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002027 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2028#ifdef FEAT_LISP
2029 /* line is exhausted and comment with it,
2030 * don't search for match in code */
2031 || lispcomm
2032#endif
2033 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 break;
2035 ++pos.lnum;
2036
2037 if (maxtravel && traveled++ > maxtravel)
2038 break;
2039
2040 linep = ml_get(pos.lnum);
2041 pos.col = 0;
2042 do_quotes = -1;
2043 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002044#ifdef FEAT_LISP
2045 if (lisp) /* find comment pos in new line */
2046 comment_col = check_linecomment(linep);
2047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 else
2050 {
2051#ifdef FEAT_MBYTE
2052 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002053 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 else
2055#endif
2056 ++pos.col;
2057 }
2058 }
2059
2060 /*
2061 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2062 */
2063 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2064 (linep[0] == '{' || linep[0] == '}'))
2065 {
2066 if (linep[0] == findc && count == 0) /* match! */
2067 return &pos;
2068 break; /* out of scope */
2069 }
2070
2071 if (comment_dir)
2072 {
2073 /* Note: comments do not nest, and we ignore quotes in them */
2074 /* TODO: ignore comment brackets inside strings */
2075 if (comment_dir == FORWARD)
2076 {
2077 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2078 {
2079 pos.col++;
2080 return &pos;
2081 }
2082 }
2083 else /* Searching backwards */
2084 {
2085 /*
2086 * A comment may contain / * or / /, it may also start or end
2087 * with / * /. Ignore a / * after / /.
2088 */
2089 if (pos.col == 0)
2090 continue;
2091 else if ( linep[pos.col - 1] == '/'
2092 && linep[pos.col] == '*'
2093 && (int)pos.col < comment_col)
2094 {
2095 count++;
2096 match_pos = pos;
2097 match_pos.col--;
2098 }
2099 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2100 {
2101 if (count > 0)
2102 pos = match_pos;
2103 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2104 && (int)pos.col <= comment_col)
2105 pos.col -= 2;
2106 else if (ignore_cend)
2107 continue;
2108 else
2109 return NULL;
2110 return &pos;
2111 }
2112 }
2113 continue;
2114 }
2115
2116 /*
2117 * If smart matching ('cpoptions' does not contain '%'), braces inside
2118 * of quotes are ignored, but only if there is an even number of
2119 * quotes in the line.
2120 */
2121 if (cpo_match)
2122 do_quotes = 0;
2123 else if (do_quotes == -1)
2124 {
2125 /*
2126 * Count the number of quotes in the line, skipping \" and '"'.
2127 * Watch out for "\\".
2128 */
2129 at_start = do_quotes;
2130 for (ptr = linep; *ptr; ++ptr)
2131 {
2132 if (ptr == linep + pos.col + backwards)
2133 at_start = (do_quotes & 1);
2134 if (*ptr == '"'
2135 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2136 ++do_quotes;
2137 if (*ptr == '\\' && ptr[1] != NUL)
2138 ++ptr;
2139 }
2140 do_quotes &= 1; /* result is 1 with even number of quotes */
2141
2142 /*
2143 * If we find an uneven count, check current line and previous
2144 * one for a '\' at the end.
2145 */
2146 if (!do_quotes)
2147 {
2148 inquote = FALSE;
2149 if (ptr[-1] == '\\')
2150 {
2151 do_quotes = 1;
2152 if (start_in_quotes == MAYBE)
2153 {
2154 /* Do we need to use at_start here? */
2155 inquote = TRUE;
2156 start_in_quotes = TRUE;
2157 }
2158 else if (backwards)
2159 inquote = TRUE;
2160 }
2161 if (pos.lnum > 1)
2162 {
2163 ptr = ml_get(pos.lnum - 1);
2164 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2165 {
2166 do_quotes = 1;
2167 if (start_in_quotes == MAYBE)
2168 {
2169 inquote = at_start;
2170 if (inquote)
2171 start_in_quotes = TRUE;
2172 }
2173 else if (!backwards)
2174 inquote = TRUE;
2175 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002176
2177 /* ml_get() only keeps one line, need to get linep again */
2178 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 }
2181 }
2182 if (start_in_quotes == MAYBE)
2183 start_in_quotes = FALSE;
2184
2185 /*
2186 * If 'smartmatch' is set:
2187 * Things inside quotes are ignored by setting 'inquote'. If we
2188 * find a quote without a preceding '\' invert 'inquote'. At the
2189 * end of a line not ending in '\' we reset 'inquote'.
2190 *
2191 * In lines with an uneven number of quotes (without preceding '\')
2192 * we do not know which part to ignore. Therefore we only set
2193 * inquote if the number of quotes in a line is even, unless this
2194 * line or the previous one ends in a '\'. Complicated, isn't it?
2195 */
2196 switch (c = linep[pos.col])
2197 {
2198 case NUL:
2199 /* at end of line without trailing backslash, reset inquote */
2200 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2201 {
2202 inquote = FALSE;
2203 start_in_quotes = FALSE;
2204 }
2205 break;
2206
2207 case '"':
2208 /* a quote that is preceded with an odd number of backslashes is
2209 * ignored */
2210 if (do_quotes)
2211 {
2212 int col;
2213
2214 for (col = pos.col - 1; col >= 0; --col)
2215 if (linep[col] != '\\')
2216 break;
2217 if ((((int)pos.col - 1 - col) & 1) == 0)
2218 {
2219 inquote = !inquote;
2220 start_in_quotes = FALSE;
2221 }
2222 }
2223 break;
2224
2225 /*
2226 * If smart matching ('cpoptions' does not contain '%'):
2227 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2228 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2229 * skipped, there is never a brace in them.
2230 * Ignore this when finding matches for `'.
2231 */
2232 case '\'':
2233 if (!cpo_match && initc != '\'' && findc != '\'')
2234 {
2235 if (backwards)
2236 {
2237 if (pos.col > 1)
2238 {
2239 if (linep[pos.col - 2] == '\'')
2240 {
2241 pos.col -= 2;
2242 break;
2243 }
2244 else if (linep[pos.col - 2] == '\\' &&
2245 pos.col > 2 && linep[pos.col - 3] == '\'')
2246 {
2247 pos.col -= 3;
2248 break;
2249 }
2250 }
2251 }
2252 else if (linep[pos.col + 1]) /* forward search */
2253 {
2254 if (linep[pos.col + 1] == '\\' &&
2255 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2256 {
2257 pos.col += 3;
2258 break;
2259 }
2260 else if (linep[pos.col + 2] == '\'')
2261 {
2262 pos.col += 2;
2263 break;
2264 }
2265 }
2266 }
2267 /* FALLTHROUGH */
2268
2269 default:
2270#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002271 /*
2272 * For Lisp skip over backslashed (), {} and [].
2273 * (actually, we skip #\( et al)
2274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (curbuf->b_p_lisp
2276 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002277 && pos.col > 1
2278 && check_prevcol(linep, pos.col, '\\', NULL)
2279 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 break;
2281#endif
2282
2283 /* Check for match outside of quotes, and inside of
2284 * quotes when the start is also inside of quotes. */
2285 if ((!inquote || start_in_quotes == TRUE)
2286 && (c == initc || c == findc))
2287 {
2288 int col, bslcnt = 0;
2289
2290 if (!cpo_bsl)
2291 {
2292 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2293 bslcnt++;
2294 }
2295 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2296 * what we expect. */
2297 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2298 {
2299 if (c == initc)
2300 count++;
2301 else
2302 {
2303 if (count == 0)
2304 return &pos;
2305 count--;
2306 }
2307 }
2308 }
2309 }
2310 }
2311
2312 if (comment_dir == BACKWARD && count > 0)
2313 {
2314 pos = match_pos;
2315 return &pos;
2316 }
2317 return (pos_T *)NULL; /* never found it */
2318}
2319
2320/*
2321 * Check if line[] contains a / / comment.
2322 * Return MAXCOL if not, otherwise return the column.
2323 * TODO: skip strings.
2324 */
2325 static int
2326check_linecomment(line)
2327 char_u *line;
2328{
2329 char_u *p;
2330
2331 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002332#ifdef FEAT_LISP
2333 /* skip Lispish one-line comments */
2334 if (curbuf->b_p_lisp)
2335 {
2336 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2337 {
2338 int instr = FALSE; /* inside of string */
2339
2340 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002341 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002342 {
2343 if (*p == '"')
2344 {
2345 if (instr)
2346 {
2347 if (*(p - 1) != '\\') /* skip escaped quote */
2348 instr = FALSE;
2349 }
2350 else if (p == line || ((p - line) >= 2
2351 /* skip #\" form */
2352 && *(p - 1) != '\\' && *(p - 2) != '#'))
2353 instr = TRUE;
2354 }
2355 else if (!instr && ((p - line) < 2
2356 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2357 break; /* found! */
2358 ++p;
2359 }
2360 }
2361 else
2362 p = NULL;
2363 }
2364 else
2365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 while ((p = vim_strchr(p, '/')) != NULL)
2367 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002368 /* accept a double /, unless it's preceded with * and followed by *,
2369 * because * / / * is an end and start of a C comment */
2370 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 break;
2372 ++p;
2373 }
2374
2375 if (p == NULL)
2376 return MAXCOL;
2377 return (int)(p - line);
2378}
2379
2380/*
2381 * Move cursor briefly to character matching the one under the cursor.
2382 * Used for Insert mode and "r" command.
2383 * Show the match only if it is visible on the screen.
2384 * If there isn't a match, then beep.
2385 */
2386 void
2387showmatch(c)
2388 int c; /* char to show match for */
2389{
2390 pos_T *lpos, save_cursor;
2391 pos_T mpos;
2392 colnr_T vcol;
2393 long save_so;
2394 long save_siso;
2395#ifdef CURSOR_SHAPE
2396 int save_state;
2397#endif
2398 colnr_T save_dollar_vcol;
2399 char_u *p;
2400
2401 /*
2402 * Only show match for chars in the 'matchpairs' option.
2403 */
2404 /* 'matchpairs' is "x:y,x:y" */
2405 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2406 {
2407#ifdef FEAT_RIGHTLEFT
2408 if (*p == c && (curwin->w_p_rl ^ p_ri))
2409 break;
2410#endif
2411 p += 2;
2412 if (*p == c
2413#ifdef FEAT_RIGHTLEFT
2414 && !(curwin->w_p_rl ^ p_ri)
2415#endif
2416 )
2417 break;
2418 if (p[1] != ',')
2419 return;
2420 }
2421
2422 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2423 vim_beep();
2424 else if (lpos->lnum >= curwin->w_topline)
2425 {
2426 if (!curwin->w_p_wrap)
2427 getvcol(curwin, lpos, NULL, &vcol, NULL);
2428 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2429 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2430 {
2431 mpos = *lpos; /* save the pos, update_screen() may change it */
2432 save_cursor = curwin->w_cursor;
2433 save_so = p_so;
2434 save_siso = p_siso;
2435 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2436 * stop displaying the "$". */
2437 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2438 dollar_vcol = 0;
2439 ++curwin->w_virtcol; /* do display ')' just before "$" */
2440 update_screen(VALID); /* show the new char first */
2441
2442 save_dollar_vcol = dollar_vcol;
2443#ifdef CURSOR_SHAPE
2444 save_state = State;
2445 State = SHOWMATCH;
2446 ui_cursor_shape(); /* may show different cursor shape */
2447#endif
2448 curwin->w_cursor = mpos; /* move to matching char */
2449 p_so = 0; /* don't use 'scrolloff' here */
2450 p_siso = 0; /* don't use 'sidescrolloff' here */
2451 showruler(FALSE);
2452 setcursor();
2453 cursor_on(); /* make sure that the cursor is shown */
2454 out_flush();
2455#ifdef FEAT_GUI
2456 if (gui.in_use)
2457 {
2458 gui_update_cursor(TRUE, FALSE);
2459 gui_mch_flush();
2460 }
2461#endif
2462 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2463 * which resets it if the matching position is in a previous line
2464 * and has a higher column number. */
2465 dollar_vcol = save_dollar_vcol;
2466
2467 /*
2468 * brief pause, unless 'm' is present in 'cpo' and a character is
2469 * available.
2470 */
2471 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2472 ui_delay(p_mat * 100L, TRUE);
2473 else if (!char_avail())
2474 ui_delay(p_mat * 100L, FALSE);
2475 curwin->w_cursor = save_cursor; /* restore cursor position */
2476 p_so = save_so;
2477 p_siso = save_siso;
2478#ifdef CURSOR_SHAPE
2479 State = save_state;
2480 ui_cursor_shape(); /* may show different cursor shape */
2481#endif
2482 }
2483 }
2484}
2485
2486/*
2487 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002488 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 * space or a line break. Also stop at an empty line.
2490 * Return OK if the next sentence was found.
2491 */
2492 int
2493findsent(dir, count)
2494 int dir;
2495 long count;
2496{
2497 pos_T pos, tpos;
2498 int c;
2499 int (*func) __ARGS((pos_T *));
2500 int startlnum;
2501 int noskip = FALSE; /* do not skip blanks */
2502 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002503 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505 pos = curwin->w_cursor;
2506 if (dir == FORWARD)
2507 func = incl;
2508 else
2509 func = decl;
2510
2511 while (count--)
2512 {
2513 /*
2514 * if on an empty line, skip upto a non-empty line
2515 */
2516 if (gchar_pos(&pos) == NUL)
2517 {
2518 do
2519 if ((*func)(&pos) == -1)
2520 break;
2521 while (gchar_pos(&pos) == NUL);
2522 if (dir == FORWARD)
2523 goto found;
2524 }
2525 /*
2526 * if on the start of a paragraph or a section and searching forward,
2527 * go to the next line
2528 */
2529 else if (dir == FORWARD && pos.col == 0 &&
2530 startPS(pos.lnum, NUL, FALSE))
2531 {
2532 if (pos.lnum == curbuf->b_ml.ml_line_count)
2533 return FAIL;
2534 ++pos.lnum;
2535 goto found;
2536 }
2537 else if (dir == BACKWARD)
2538 decl(&pos);
2539
2540 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002541 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2543 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2544 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002545 if (vim_strchr((char_u *)".!?", c) != NULL)
2546 {
2547 /* Only skip over a '.', '!' and '?' once. */
2548 if (found_dot)
2549 break;
2550 found_dot = TRUE;
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (decl(&pos) == -1)
2553 break;
2554 /* when going forward: Stop in front of empty line */
2555 if (lineempty(pos.lnum) && dir == FORWARD)
2556 {
2557 incl(&pos);
2558 goto found;
2559 }
2560 }
2561
2562 /* remember the line where the search started */
2563 startlnum = pos.lnum;
2564 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2565
2566 for (;;) /* find end of sentence */
2567 {
2568 c = gchar_pos(&pos);
2569 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2570 {
2571 if (dir == BACKWARD && pos.lnum != startlnum)
2572 ++pos.lnum;
2573 break;
2574 }
2575 if (c == '.' || c == '!' || c == '?')
2576 {
2577 tpos = pos;
2578 do
2579 if ((c = inc(&tpos)) == -1)
2580 break;
2581 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2582 != NULL);
2583 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2584 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2585 && gchar_pos(&tpos) == ' ')))
2586 {
2587 pos = tpos;
2588 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2589 inc(&pos);
2590 break;
2591 }
2592 }
2593 if ((*func)(&pos) == -1)
2594 {
2595 if (count)
2596 return FAIL;
2597 noskip = TRUE;
2598 break;
2599 }
2600 }
2601found:
2602 /* skip white space */
2603 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2604 if (incl(&pos) == -1)
2605 break;
2606 }
2607
2608 setpcmark();
2609 curwin->w_cursor = pos;
2610 return OK;
2611}
2612
2613/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002614 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002616 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 * If 'what' is '{' or '}' we go to the next section.
2618 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002619 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
2621 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002622findpar(pincl, dir, count, what, both)
2623 int *pincl; /* Return: TRUE if last char is to be included */
2624 int dir;
2625 long count;
2626 int what;
2627 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 linenr_T curr;
2630 int did_skip; /* TRUE after separating lines have been skipped */
2631 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002632 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#ifdef FEAT_FOLDING
2634 linenr_T fold_first; /* first line of a closed fold */
2635 linenr_T fold_last; /* last line of a closed fold */
2636 int fold_skipped; /* TRUE if a closed fold was skipped this
2637 iteration */
2638#endif
2639
2640 curr = curwin->w_cursor.lnum;
2641
2642 while (count--)
2643 {
2644 did_skip = FALSE;
2645 for (first = TRUE; ; first = FALSE)
2646 {
2647 if (*ml_get(curr) != NUL)
2648 did_skip = TRUE;
2649
2650#ifdef FEAT_FOLDING
2651 /* skip folded lines */
2652 fold_skipped = FALSE;
2653 if (first && hasFolding(curr, &fold_first, &fold_last))
2654 {
2655 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2656 fold_skipped = TRUE;
2657 }
2658#endif
2659
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002660 /* POSIX has it's own ideas of what a paragraph boundary is and it
2661 * doesn't match historical Vi: It also stops at a "{" in the
2662 * first column and at an empty line. */
2663 if (!first && did_skip && (startPS(curr, what, both)
2664 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 break;
2666
2667#ifdef FEAT_FOLDING
2668 if (fold_skipped)
2669 curr -= dir;
2670#endif
2671 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2672 {
2673 if (count)
2674 return FALSE;
2675 curr -= dir;
2676 break;
2677 }
2678 }
2679 }
2680 setpcmark();
2681 if (both && *ml_get(curr) == '}') /* include line with '}' */
2682 ++curr;
2683 curwin->w_cursor.lnum = curr;
2684 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2685 {
2686 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2687 {
2688 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002689 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
2691 }
2692 else
2693 curwin->w_cursor.col = 0;
2694 return TRUE;
2695}
2696
2697/*
2698 * check if the string 's' is a nroff macro that is in option 'opt'
2699 */
2700 static int
2701inmacro(opt, s)
2702 char_u *opt;
2703 char_u *s;
2704{
2705 char_u *macro;
2706
2707 for (macro = opt; macro[0]; ++macro)
2708 {
2709 /* Accept two characters in the option being equal to two characters
2710 * in the line. A space in the option matches with a space in the
2711 * line or the line having ended. */
2712 if ( (macro[0] == s[0]
2713 || (macro[0] == ' '
2714 && (s[0] == NUL || s[0] == ' ')))
2715 && (macro[1] == s[1]
2716 || ((macro[1] == NUL || macro[1] == ' ')
2717 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2718 break;
2719 ++macro;
2720 if (macro[0] == NUL)
2721 break;
2722 }
2723 return (macro[0] != NUL);
2724}
2725
2726/*
2727 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2728 * If 'para' is '{' or '}' only check for sections.
2729 * If 'both' is TRUE also stop at '}'
2730 */
2731 int
2732startPS(lnum, para, both)
2733 linenr_T lnum;
2734 int para;
2735 int both;
2736{
2737 char_u *s;
2738
2739 s = ml_get(lnum);
2740 if (*s == para || *s == '\f' || (both && *s == '}'))
2741 return TRUE;
2742 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2743 (!para && inmacro(p_para, s + 1))))
2744 return TRUE;
2745 return FALSE;
2746}
2747
2748/*
2749 * The following routines do the word searches performed by the 'w', 'W',
2750 * 'b', 'B', 'e', and 'E' commands.
2751 */
2752
2753/*
2754 * To perform these searches, characters are placed into one of three
2755 * classes, and transitions between classes determine word boundaries.
2756 *
2757 * The classes are:
2758 *
2759 * 0 - white space
2760 * 1 - punctuation
2761 * 2 or higher - keyword characters (letters, digits and underscore)
2762 */
2763
2764static int cls_bigword; /* TRUE for "W", "B" or "E" */
2765
2766/*
2767 * cls() - returns the class of character at curwin->w_cursor
2768 *
2769 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2770 * from class 2 and higher are reported as class 1 since only white space
2771 * boundaries are of interest.
2772 */
2773 static int
2774cls()
2775{
2776 int c;
2777
2778 c = gchar_cursor();
2779#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2780 if (p_altkeymap && c == F_BLANK)
2781 return 0;
2782#endif
2783 if (c == ' ' || c == '\t' || c == NUL)
2784 return 0;
2785#ifdef FEAT_MBYTE
2786 if (enc_dbcs != 0 && c > 0xFF)
2787 {
2788 /* If cls_bigword, report multi-byte chars as class 1. */
2789 if (enc_dbcs == DBCS_KOR && cls_bigword)
2790 return 1;
2791
2792 /* process code leading/trailing bytes */
2793 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2794 }
2795 if (enc_utf8)
2796 {
2797 c = utf_class(c);
2798 if (c != 0 && cls_bigword)
2799 return 1;
2800 return c;
2801 }
2802#endif
2803
2804 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2805 if (cls_bigword)
2806 return 1;
2807
2808 if (vim_iswordc(c))
2809 return 2;
2810 return 1;
2811}
2812
2813
2814/*
2815 * fwd_word(count, type, eol) - move forward one word
2816 *
2817 * Returns FAIL if the cursor was already at the end of the file.
2818 * If eol is TRUE, last word stops at end of line (for operators).
2819 */
2820 int
2821fwd_word(count, bigword, eol)
2822 long count;
2823 int bigword; /* "W", "E" or "B" */
2824 int eol;
2825{
2826 int sclass; /* starting class */
2827 int i;
2828 int last_line;
2829
2830#ifdef FEAT_VIRTUALEDIT
2831 curwin->w_cursor.coladd = 0;
2832#endif
2833 cls_bigword = bigword;
2834 while (--count >= 0)
2835 {
2836#ifdef FEAT_FOLDING
2837 /* When inside a range of folded lines, move to the last char of the
2838 * last line. */
2839 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2840 coladvance((colnr_T)MAXCOL);
2841#endif
2842 sclass = cls();
2843
2844 /*
2845 * We always move at least one character, unless on the last
2846 * character in the buffer.
2847 */
2848 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2849 i = inc_cursor();
2850 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2851 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00002852 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 return OK;
2854
2855 /*
2856 * Go one char past end of current word (if any)
2857 */
2858 if (sclass != 0)
2859 while (cls() == sclass)
2860 {
2861 i = inc_cursor();
2862 if (i == -1 || (i >= 1 && eol && count == 0))
2863 return OK;
2864 }
2865
2866 /*
2867 * go to next non-white
2868 */
2869 while (cls() == 0)
2870 {
2871 /*
2872 * We'll stop if we land on a blank line
2873 */
2874 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2875 break;
2876
2877 i = inc_cursor();
2878 if (i == -1 || (i >= 1 && eol && count == 0))
2879 return OK;
2880 }
2881 }
2882 return OK;
2883}
2884
2885/*
2886 * bck_word() - move backward 'count' words
2887 *
2888 * If stop is TRUE and we are already on the start of a word, move one less.
2889 *
2890 * Returns FAIL if top of the file was reached.
2891 */
2892 int
2893bck_word(count, bigword, stop)
2894 long count;
2895 int bigword;
2896 int stop;
2897{
2898 int sclass; /* starting class */
2899
2900#ifdef FEAT_VIRTUALEDIT
2901 curwin->w_cursor.coladd = 0;
2902#endif
2903 cls_bigword = bigword;
2904 while (--count >= 0)
2905 {
2906#ifdef FEAT_FOLDING
2907 /* When inside a range of folded lines, move to the first char of the
2908 * first line. */
2909 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2910 curwin->w_cursor.col = 0;
2911#endif
2912 sclass = cls();
2913 if (dec_cursor() == -1) /* started at start of file */
2914 return FAIL;
2915
2916 if (!stop || sclass == cls() || sclass == 0)
2917 {
2918 /*
2919 * Skip white space before the word.
2920 * Stop on an empty line.
2921 */
2922 while (cls() == 0)
2923 {
2924 if (curwin->w_cursor.col == 0
2925 && lineempty(curwin->w_cursor.lnum))
2926 goto finished;
2927 if (dec_cursor() == -1) /* hit start of file, stop here */
2928 return OK;
2929 }
2930
2931 /*
2932 * Move backward to start of this word.
2933 */
2934 if (skip_chars(cls(), BACKWARD))
2935 return OK;
2936 }
2937
2938 inc_cursor(); /* overshot - forward one */
2939finished:
2940 stop = FALSE;
2941 }
2942 return OK;
2943}
2944
2945/*
2946 * end_word() - move to the end of the word
2947 *
2948 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2949 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2950 * motion crosses blank lines. When the real vi crosses a blank line in an
2951 * 'e' motion, the cursor is placed on the FIRST character of the next
2952 * non-blank line. The 'E' command, however, works correctly. Since this
2953 * appears to be a bug, I have not duplicated it here.
2954 *
2955 * Returns FAIL if end of the file was reached.
2956 *
2957 * If stop is TRUE and we are already on the end of a word, move one less.
2958 * If empty is TRUE stop on an empty line.
2959 */
2960 int
2961end_word(count, bigword, stop, empty)
2962 long count;
2963 int bigword;
2964 int stop;
2965 int empty;
2966{
2967 int sclass; /* starting class */
2968
2969#ifdef FEAT_VIRTUALEDIT
2970 curwin->w_cursor.coladd = 0;
2971#endif
2972 cls_bigword = bigword;
2973 while (--count >= 0)
2974 {
2975#ifdef FEAT_FOLDING
2976 /* When inside a range of folded lines, move to the last char of the
2977 * last line. */
2978 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2979 coladvance((colnr_T)MAXCOL);
2980#endif
2981 sclass = cls();
2982 if (inc_cursor() == -1)
2983 return FAIL;
2984
2985 /*
2986 * If we're in the middle of a word, we just have to move to the end
2987 * of it.
2988 */
2989 if (cls() == sclass && sclass != 0)
2990 {
2991 /*
2992 * Move forward to end of the current word
2993 */
2994 if (skip_chars(sclass, FORWARD))
2995 return FAIL;
2996 }
2997 else if (!stop || sclass == 0)
2998 {
2999 /*
3000 * We were at the end of a word. Go to the end of the next word.
3001 * First skip white space, if 'empty' is TRUE, stop at empty line.
3002 */
3003 while (cls() == 0)
3004 {
3005 if (empty && curwin->w_cursor.col == 0
3006 && lineempty(curwin->w_cursor.lnum))
3007 goto finished;
3008 if (inc_cursor() == -1) /* hit end of file, stop here */
3009 return FAIL;
3010 }
3011
3012 /*
3013 * Move forward to the end of this word.
3014 */
3015 if (skip_chars(cls(), FORWARD))
3016 return FAIL;
3017 }
3018 dec_cursor(); /* overshot - one char backward */
3019finished:
3020 stop = FALSE; /* we move only one word less */
3021 }
3022 return OK;
3023}
3024
3025/*
3026 * Move back to the end of the word.
3027 *
3028 * Returns FAIL if start of the file was reached.
3029 */
3030 int
3031bckend_word(count, bigword, eol)
3032 long count;
3033 int bigword; /* TRUE for "B" */
3034 int eol; /* TRUE: stop at end of line. */
3035{
3036 int sclass; /* starting class */
3037 int i;
3038
3039#ifdef FEAT_VIRTUALEDIT
3040 curwin->w_cursor.coladd = 0;
3041#endif
3042 cls_bigword = bigword;
3043 while (--count >= 0)
3044 {
3045 sclass = cls();
3046 if ((i = dec_cursor()) == -1)
3047 return FAIL;
3048 if (eol && i == 1)
3049 return OK;
3050
3051 /*
3052 * Move backward to before the start of this word.
3053 */
3054 if (sclass != 0)
3055 {
3056 while (cls() == sclass)
3057 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3058 return OK;
3059 }
3060
3061 /*
3062 * Move backward to end of the previous word
3063 */
3064 while (cls() == 0)
3065 {
3066 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3067 break;
3068 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3069 return OK;
3070 }
3071 }
3072 return OK;
3073}
3074
3075/*
3076 * Skip a row of characters of the same class.
3077 * Return TRUE when end-of-file reached, FALSE otherwise.
3078 */
3079 static int
3080skip_chars(cclass, dir)
3081 int cclass;
3082 int dir;
3083{
3084 while (cls() == cclass)
3085 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3086 return TRUE;
3087 return FALSE;
3088}
3089
3090#ifdef FEAT_TEXTOBJ
3091/*
3092 * Go back to the start of the word or the start of white space
3093 */
3094 static void
3095back_in_line()
3096{
3097 int sclass; /* starting class */
3098
3099 sclass = cls();
3100 for (;;)
3101 {
3102 if (curwin->w_cursor.col == 0) /* stop at start of line */
3103 break;
3104 dec_cursor();
3105 if (cls() != sclass) /* stop at start of word */
3106 {
3107 inc_cursor();
3108 break;
3109 }
3110 }
3111}
3112
3113 static void
3114find_first_blank(posp)
3115 pos_T *posp;
3116{
3117 int c;
3118
3119 while (decl(posp) != -1)
3120 {
3121 c = gchar_pos(posp);
3122 if (!vim_iswhite(c))
3123 {
3124 incl(posp);
3125 break;
3126 }
3127 }
3128}
3129
3130/*
3131 * Skip count/2 sentences and count/2 separating white spaces.
3132 */
3133 static void
3134findsent_forward(count, at_start_sent)
3135 long count;
3136 int at_start_sent; /* cursor is at start of sentence */
3137{
3138 while (count--)
3139 {
3140 findsent(FORWARD, 1L);
3141 if (at_start_sent)
3142 find_first_blank(&curwin->w_cursor);
3143 if (count == 0 || at_start_sent)
3144 decl(&curwin->w_cursor);
3145 at_start_sent = !at_start_sent;
3146 }
3147}
3148
3149/*
3150 * Find word under cursor, cursor at end.
3151 * Used while an operator is pending, and in Visual mode.
3152 */
3153 int
3154current_word(oap, count, include, bigword)
3155 oparg_T *oap;
3156 long count;
3157 int include; /* TRUE: include word and white space */
3158 int bigword; /* FALSE == word, TRUE == WORD */
3159{
3160 pos_T start_pos;
3161 pos_T pos;
3162 int inclusive = TRUE;
3163 int include_white = FALSE;
3164
3165 cls_bigword = bigword;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003166 clearpos(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167
3168#ifdef FEAT_VISUAL
3169 /* Correct cursor when 'selection' is exclusive */
3170 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3171 dec_cursor();
3172
3173 /*
3174 * When Visual mode is not active, or when the VIsual area is only one
3175 * character, select the word and/or white space under the cursor.
3176 */
3177 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3178#endif
3179 {
3180 /*
3181 * Go to start of current word or white space.
3182 */
3183 back_in_line();
3184 start_pos = curwin->w_cursor;
3185
3186 /*
3187 * If the start is on white space, and white space should be included
3188 * (" word"), or start is not on white space, and white space should
3189 * not be included ("word"), find end of word.
3190 */
3191 if ((cls() == 0) == include)
3192 {
3193 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3194 return FAIL;
3195 }
3196 else
3197 {
3198 /*
3199 * If the start is not on white space, and white space should be
3200 * included ("word "), or start is on white space and white
3201 * space should not be included (" "), find start of word.
3202 * If we end up in the first column of the next line (single char
3203 * word) back up to end of the line.
3204 */
3205 fwd_word(1L, bigword, TRUE);
3206 if (curwin->w_cursor.col == 0)
3207 decl(&curwin->w_cursor);
3208 else
3209 oneleft();
3210
3211 if (include)
3212 include_white = TRUE;
3213 }
3214
3215#ifdef FEAT_VISUAL
3216 if (VIsual_active)
3217 {
3218 /* should do something when inclusive == FALSE ! */
3219 VIsual = start_pos;
3220 redraw_curbuf_later(INVERTED); /* update the inversion */
3221 }
3222 else
3223#endif
3224 {
3225 oap->start = start_pos;
3226 oap->motion_type = MCHAR;
3227 }
3228 --count;
3229 }
3230
3231 /*
3232 * When count is still > 0, extend with more objects.
3233 */
3234 while (count > 0)
3235 {
3236 inclusive = TRUE;
3237#ifdef FEAT_VISUAL
3238 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3239 {
3240 /*
3241 * In Visual mode, with cursor at start: move cursor back.
3242 */
3243 if (decl(&curwin->w_cursor) == -1)
3244 return FAIL;
3245 if (include != (cls() != 0))
3246 {
3247 if (bck_word(1L, bigword, TRUE) == FAIL)
3248 return FAIL;
3249 }
3250 else
3251 {
3252 if (bckend_word(1L, bigword, TRUE) == FAIL)
3253 return FAIL;
3254 (void)incl(&curwin->w_cursor);
3255 }
3256 }
3257 else
3258#endif
3259 {
3260 /*
3261 * Move cursor forward one word and/or white area.
3262 */
3263 if (incl(&curwin->w_cursor) == -1)
3264 return FAIL;
3265 if (include != (cls() == 0))
3266 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003267 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 return FAIL;
3269 /*
3270 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003271 * the first character on the line.
3272 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003274 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 inclusive = FALSE;
3276 }
3277 else
3278 {
3279 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3280 return FAIL;
3281 }
3282 }
3283 --count;
3284 }
3285
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003286 if (include_white && (cls() != 0
3287 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
3289 /*
3290 * If we don't include white space at the end, move the start
3291 * to include some white space there. This makes "daw" work
3292 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003293 * word). Also when "2daw" deletes "word." at the end of the line
3294 * (cursor is at start of next line).
3295 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 */
3297 pos = curwin->w_cursor; /* save cursor position */
3298 curwin->w_cursor = start_pos;
3299 if (oneleft() == OK)
3300 {
3301 back_in_line();
3302 if (cls() == 0 && curwin->w_cursor.col > 0)
3303 {
3304#ifdef FEAT_VISUAL
3305 if (VIsual_active)
3306 VIsual = curwin->w_cursor;
3307 else
3308#endif
3309 oap->start = curwin->w_cursor;
3310 }
3311 }
3312 curwin->w_cursor = pos; /* put cursor back at end */
3313 }
3314
3315#ifdef FEAT_VISUAL
3316 if (VIsual_active)
3317 {
3318 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3319 inc_cursor();
3320 if (VIsual_mode == 'V')
3321 {
3322 VIsual_mode = 'v';
3323 redraw_cmdline = TRUE; /* show mode later */
3324 }
3325 }
3326 else
3327#endif
3328 oap->inclusive = inclusive;
3329
3330 return OK;
3331}
3332
3333/*
3334 * Find sentence(s) under the cursor, cursor at end.
3335 * When Visual active, extend it by one or more sentences.
3336 */
3337 int
3338current_sent(oap, count, include)
3339 oparg_T *oap;
3340 long count;
3341 int include;
3342{
3343 pos_T start_pos;
3344 pos_T pos;
3345 int start_blank;
3346 int c;
3347 int at_start_sent;
3348 long ncount;
3349
3350 start_pos = curwin->w_cursor;
3351 pos = start_pos;
3352 findsent(FORWARD, 1L); /* Find start of next sentence. */
3353
3354#ifdef FEAT_VISUAL
3355 /*
3356 * When visual area is bigger than one character: Extend it.
3357 */
3358 if (VIsual_active && !equalpos(start_pos, VIsual))
3359 {
3360extend:
3361 if (lt(start_pos, VIsual))
3362 {
3363 /*
3364 * Cursor at start of Visual area.
3365 * Find out where we are:
3366 * - in the white space before a sentence
3367 * - in a sentence or just after it
3368 * - at the start of a sentence
3369 */
3370 at_start_sent = TRUE;
3371 decl(&pos);
3372 while (lt(pos, curwin->w_cursor))
3373 {
3374 c = gchar_pos(&pos);
3375 if (!vim_iswhite(c))
3376 {
3377 at_start_sent = FALSE;
3378 break;
3379 }
3380 incl(&pos);
3381 }
3382 if (!at_start_sent)
3383 {
3384 findsent(BACKWARD, 1L);
3385 if (equalpos(curwin->w_cursor, start_pos))
3386 at_start_sent = TRUE; /* exactly at start of sentence */
3387 else
3388 /* inside a sentence, go to its end (start of next) */
3389 findsent(FORWARD, 1L);
3390 }
3391 if (include) /* "as" gets twice as much as "is" */
3392 count *= 2;
3393 while (count--)
3394 {
3395 if (at_start_sent)
3396 find_first_blank(&curwin->w_cursor);
3397 c = gchar_cursor();
3398 if (!at_start_sent || (!include && !vim_iswhite(c)))
3399 findsent(BACKWARD, 1L);
3400 at_start_sent = !at_start_sent;
3401 }
3402 }
3403 else
3404 {
3405 /*
3406 * Cursor at end of Visual area.
3407 * Find out where we are:
3408 * - just before a sentence
3409 * - just before or in the white space before a sentence
3410 * - in a sentence
3411 */
3412 incl(&pos);
3413 at_start_sent = TRUE;
3414 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3415 {
3416 at_start_sent = FALSE;
3417 while (lt(pos, curwin->w_cursor))
3418 {
3419 c = gchar_pos(&pos);
3420 if (!vim_iswhite(c))
3421 {
3422 at_start_sent = TRUE;
3423 break;
3424 }
3425 incl(&pos);
3426 }
3427 if (at_start_sent) /* in the sentence */
3428 findsent(BACKWARD, 1L);
3429 else /* in/before white before a sentence */
3430 curwin->w_cursor = start_pos;
3431 }
3432
3433 if (include) /* "as" gets twice as much as "is" */
3434 count *= 2;
3435 findsent_forward(count, at_start_sent);
3436 if (*p_sel == 'e')
3437 ++curwin->w_cursor.col;
3438 }
3439 return OK;
3440 }
3441#endif
3442
3443 /*
3444 * If cursor started on blank, check if it is just before the start of the
3445 * next sentence.
3446 */
3447 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3448 incl(&pos);
3449 if (equalpos(pos, curwin->w_cursor))
3450 {
3451 start_blank = TRUE;
3452 find_first_blank(&start_pos); /* go back to first blank */
3453 }
3454 else
3455 {
3456 start_blank = FALSE;
3457 findsent(BACKWARD, 1L);
3458 start_pos = curwin->w_cursor;
3459 }
3460 if (include)
3461 ncount = count * 2;
3462 else
3463 {
3464 ncount = count;
3465 if (start_blank)
3466 --ncount;
3467 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003468 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 findsent_forward(ncount, TRUE);
3470 else
3471 decl(&curwin->w_cursor);
3472
3473 if (include)
3474 {
3475 /*
3476 * If the blank in front of the sentence is included, exclude the
3477 * blanks at the end of the sentence, go back to the first blank.
3478 * If there are no trailing blanks, try to include leading blanks.
3479 */
3480 if (start_blank)
3481 {
3482 find_first_blank(&curwin->w_cursor);
3483 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3484 if (vim_iswhite(c))
3485 decl(&curwin->w_cursor);
3486 }
3487 else if (c = gchar_cursor(), !vim_iswhite(c))
3488 find_first_blank(&start_pos);
3489 }
3490
3491#ifdef FEAT_VISUAL
3492 if (VIsual_active)
3493 {
3494 /* avoid getting stuck with "is" on a single space before a sent. */
3495 if (equalpos(start_pos, curwin->w_cursor))
3496 goto extend;
3497 if (*p_sel == 'e')
3498 ++curwin->w_cursor.col;
3499 VIsual = start_pos;
3500 VIsual_mode = 'v';
3501 redraw_curbuf_later(INVERTED); /* update the inversion */
3502 }
3503 else
3504#endif
3505 {
3506 /* include a newline after the sentence, if there is one */
3507 if (incl(&curwin->w_cursor) == -1)
3508 oap->inclusive = TRUE;
3509 else
3510 oap->inclusive = FALSE;
3511 oap->start = start_pos;
3512 oap->motion_type = MCHAR;
3513 }
3514 return OK;
3515}
3516
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003517/*
3518 * Find block under the cursor, cursor at end.
3519 * "what" and "other" are two matching parenthesis/paren/etc.
3520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 int
3522current_block(oap, count, include, what, other)
3523 oparg_T *oap;
3524 long count;
3525 int include; /* TRUE == include white space */
3526 int what; /* '(', '{', etc. */
3527 int other; /* ')', '}', etc. */
3528{
3529 pos_T old_pos;
3530 pos_T *pos = NULL;
3531 pos_T start_pos;
3532 pos_T *end_pos;
3533 pos_T old_start, old_end;
3534 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003535 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003538 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 old_start = old_end;
3540
3541 /*
3542 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3543 */
3544#ifdef FEAT_VISUAL
3545 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3546#endif
3547 {
3548 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003549 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 while (inindent(1))
3551 if (inc_cursor() != 0)
3552 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003553 if (gchar_cursor() == what)
3554 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 ++curwin->w_cursor.col;
3556 }
3557#ifdef FEAT_VISUAL
3558 else if (lt(VIsual, curwin->w_cursor))
3559 {
3560 old_start = VIsual;
3561 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3562 }
3563 else
3564 old_end = VIsual;
3565#endif
3566
3567 /*
3568 * Search backwards for unclosed '(', '{', etc..
3569 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003570 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 */
3572 save_cpo = p_cpo;
3573 p_cpo = (char_u *)"%";
3574 while (count-- > 0)
3575 {
3576 if ((pos = findmatch(NULL, what)) == NULL)
3577 break;
3578 curwin->w_cursor = *pos;
3579 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3580 }
3581 p_cpo = save_cpo;
3582
3583 /*
3584 * Search for matching ')', '}', etc.
3585 * Put this position in curwin->w_cursor.
3586 */
3587 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3588 {
3589 curwin->w_cursor = old_pos;
3590 return FAIL;
3591 }
3592 curwin->w_cursor = *end_pos;
3593
3594 /*
3595 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3596 * If the ending '}' is only preceded by indent, skip that indent.
3597 * But only if the resulting area is not smaller than what we started with.
3598 */
3599 while (!include)
3600 {
3601 incl(&start_pos);
3602 sol = (curwin->w_cursor.col == 0);
3603 decl(&curwin->w_cursor);
3604 if (what == '{')
3605 while (inindent(1))
3606 {
3607 sol = TRUE;
3608 if (decl(&curwin->w_cursor) != 0)
3609 break;
3610 }
3611#ifdef FEAT_VISUAL
3612 /*
3613 * In Visual mode, when the resulting area is not bigger than what we
3614 * started with, extend it to the next block, and then exclude again.
3615 */
3616 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3617 && VIsual_active)
3618 {
3619 curwin->w_cursor = old_start;
3620 decl(&curwin->w_cursor);
3621 if ((pos = findmatch(NULL, what)) == NULL)
3622 {
3623 curwin->w_cursor = old_pos;
3624 return FAIL;
3625 }
3626 start_pos = *pos;
3627 curwin->w_cursor = *pos;
3628 if ((end_pos = findmatch(NULL, other)) == NULL)
3629 {
3630 curwin->w_cursor = old_pos;
3631 return FAIL;
3632 }
3633 curwin->w_cursor = *end_pos;
3634 }
3635 else
3636#endif
3637 break;
3638 }
3639
3640#ifdef FEAT_VISUAL
3641 if (VIsual_active)
3642 {
3643 if (*p_sel == 'e')
3644 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003645 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 inc(&curwin->w_cursor); /* include the line break */
3647 VIsual = start_pos;
3648 VIsual_mode = 'v';
3649 redraw_curbuf_later(INVERTED); /* update the inversion */
3650 showmode();
3651 }
3652 else
3653#endif
3654 {
3655 oap->start = start_pos;
3656 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003657 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 incl(&curwin->w_cursor);
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003660 else if (ltoreq(start_pos, curwin->w_cursor))
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003661 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003663 else
3664 /* End is before the start (no text in between <>, [], etc.): don't
3665 * operate on any text. */
3666 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668
3669 return OK;
3670}
3671
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003672static int in_html_tag __ARGS((int));
3673
3674/*
3675 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3676 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3677 */
3678 static int
3679in_html_tag(end_tag)
3680 int end_tag;
3681{
3682 char_u *line = ml_get_curline();
3683 char_u *p;
3684 int c;
3685 int lc = NUL;
3686 pos_T pos;
3687
3688#ifdef FEAT_MBYTE
3689 if (enc_dbcs)
3690 {
3691 char_u *lp = NULL;
3692
3693 /* We search forward until the cursor, because searching backwards is
3694 * very slow for DBCS encodings. */
3695 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3696 if (*p == '>' || *p == '<')
3697 {
3698 lc = *p;
3699 lp = p;
3700 }
3701 if (*p != '<') /* check for '<' under cursor */
3702 {
3703 if (lc != '<')
3704 return FALSE;
3705 p = lp;
3706 }
3707 }
3708 else
3709#endif
3710 {
3711 for (p = line + curwin->w_cursor.col; p > line; )
3712 {
3713 if (*p == '<') /* find '<' under/before cursor */
3714 break;
3715 mb_ptr_back(line, p);
3716 if (*p == '>') /* find '>' before cursor */
3717 break;
3718 }
3719 if (*p != '<')
3720 return FALSE;
3721 }
3722
3723 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003724 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003725
3726 mb_ptr_adv(p);
3727 if (end_tag)
3728 /* check that there is a '/' after the '<' */
3729 return *p == '/';
3730
3731 /* check that there is no '/' after the '<' */
3732 if (*p == '/')
3733 return FALSE;
3734
3735 /* check that the matching '>' is not preceded by '/' */
3736 for (;;)
3737 {
3738 if (inc(&pos) < 0)
3739 return FALSE;
3740 c = *ml_get_pos(&pos);
3741 if (c == '>')
3742 break;
3743 lc = c;
3744 }
3745 return lc != '/';
3746}
3747
3748/*
3749 * Find tag block under the cursor, cursor at end.
3750 */
3751 int
3752current_tagblock(oap, count_arg, include)
3753 oparg_T *oap;
3754 long count_arg;
3755 int include; /* TRUE == include white space */
3756{
3757 long count = count_arg;
3758 long n;
3759 pos_T old_pos;
3760 pos_T start_pos;
3761 pos_T end_pos;
3762 pos_T old_start, old_end;
3763 char_u *spat, *epat;
3764 char_u *p;
3765 char_u *cp;
3766 int len;
3767 int r;
3768 int do_include = include;
3769 int save_p_ws = p_ws;
3770 int retval = FAIL;
3771
3772 p_ws = FALSE;
3773
3774 old_pos = curwin->w_cursor;
3775 old_end = curwin->w_cursor; /* remember where we started */
3776 old_start = old_end;
Bram Moolenaar2a6f2112008-01-26 20:15:46 +00003777#ifdef FEAT_VISUAL
3778 if (!VIsual_active || *p_sel == 'e')
3779#endif
3780 decl(&old_end); /* old_end is inclusive */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003781
3782 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003783 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003784 */
3785#ifdef FEAT_VISUAL
3786 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3787#endif
3788 {
3789 setpcmark();
3790
3791 /* ignore indent */
3792 while (inindent(1))
3793 if (inc_cursor() != 0)
3794 break;
3795
3796 if (in_html_tag(FALSE))
3797 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003798 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003799 while (*ml_get_cursor() != '>')
3800 if (inc_cursor() < 0)
3801 break;
3802 }
3803 else if (in_html_tag(TRUE))
3804 {
3805 /* cursor on end tag, move to just before it */
3806 while (*ml_get_cursor() != '<')
3807 if (dec_cursor() < 0)
3808 break;
3809 dec_cursor();
3810 old_end = curwin->w_cursor;
3811 }
3812 }
3813#ifdef FEAT_VISUAL
3814 else if (lt(VIsual, curwin->w_cursor))
3815 {
3816 old_start = VIsual;
3817 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3818 }
3819 else
3820 old_end = VIsual;
3821#endif
3822
3823again:
3824 /*
3825 * Search backwards for unclosed "<aaa>".
3826 * Put this position in start_pos.
3827 */
3828 for (n = 0; n < count; ++n)
3829 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003830 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003831 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003832 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00003833 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003834 {
3835 curwin->w_cursor = old_pos;
3836 goto theend;
3837 }
3838 }
3839 start_pos = curwin->w_cursor;
3840
3841 /*
3842 * Search for matching "</aaa>". First isolate the "aaa".
3843 */
3844 inc_cursor();
3845 p = ml_get_cursor();
3846 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3847 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003848 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003849 if (len == 0)
3850 {
3851 curwin->w_cursor = old_pos;
3852 goto theend;
3853 }
3854 spat = alloc(len + 29);
3855 epat = alloc(len + 9);
3856 if (spat == NULL || epat == NULL)
3857 {
3858 vim_free(spat);
3859 vim_free(epat);
3860 curwin->w_cursor = old_pos;
3861 goto theend;
3862 }
3863 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3864 sprintf((char *)epat, "</%.*s>\\c", len, p);
3865
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003866 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
Bram Moolenaar76929292008-01-06 19:07:36 +00003867 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003868
3869 vim_free(spat);
3870 vim_free(epat);
3871
3872 if (r < 1 || lt(curwin->w_cursor, old_end))
3873 {
3874 /* Can't find other end or it's before the previous end. Could be a
3875 * HTML tag that doesn't have a matching end. Search backwards for
3876 * another starting tag. */
3877 count = 1;
3878 curwin->w_cursor = start_pos;
3879 goto again;
3880 }
3881
3882 if (do_include || r < 1)
3883 {
3884 /* Include up to the '>'. */
3885 while (*ml_get_cursor() != '>')
3886 if (inc_cursor() < 0)
3887 break;
3888 }
3889 else
3890 {
3891 /* Exclude the '<' of the end tag. */
3892 if (*ml_get_cursor() == '<')
3893 dec_cursor();
3894 }
3895 end_pos = curwin->w_cursor;
3896
3897 if (!do_include)
3898 {
3899 /* Exclude the start tag. */
3900 curwin->w_cursor = start_pos;
3901 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003902 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003903 {
3904 inc_cursor();
3905 start_pos = curwin->w_cursor;
3906 break;
3907 }
3908 curwin->w_cursor = end_pos;
3909
Bram Moolenaar45360022005-07-21 21:08:21 +00003910 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003911 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003912 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003913 {
3914 do_include = TRUE;
3915 curwin->w_cursor = old_start;
3916 count = count_arg;
3917 goto again;
3918 }
3919 }
3920
3921#ifdef FEAT_VISUAL
3922 if (VIsual_active)
3923 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003924 /* If the end is before the start there is no text between tags, select
3925 * the char under the cursor. */
3926 if (lt(end_pos, start_pos))
3927 curwin->w_cursor = start_pos;
3928 else if (*p_sel == 'e')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003929 ++curwin->w_cursor.col;
3930 VIsual = start_pos;
3931 VIsual_mode = 'v';
3932 redraw_curbuf_later(INVERTED); /* update the inversion */
3933 showmode();
3934 }
3935 else
3936#endif
3937 {
3938 oap->start = start_pos;
3939 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003940 if (lt(end_pos, start_pos))
3941 {
3942 /* End is before the start: there is no text between tags; operate
3943 * on an empty area. */
3944 curwin->w_cursor = start_pos;
3945 oap->inclusive = FALSE;
3946 }
3947 else
3948 oap->inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003949 }
3950 retval = OK;
3951
3952theend:
3953 p_ws = save_p_ws;
3954 return retval;
3955}
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 int
3958current_par(oap, count, include, type)
3959 oparg_T *oap;
3960 long count;
3961 int include; /* TRUE == include white space */
3962 int type; /* 'p' for paragraph, 'S' for section */
3963{
3964 linenr_T start_lnum;
3965 linenr_T end_lnum;
3966 int white_in_front;
3967 int dir;
3968 int start_is_white;
3969 int prev_start_is_white;
3970 int retval = OK;
3971 int do_white = FALSE;
3972 int t;
3973 int i;
3974
3975 if (type == 'S') /* not implemented yet */
3976 return FAIL;
3977
3978 start_lnum = curwin->w_cursor.lnum;
3979
3980#ifdef FEAT_VISUAL
3981 /*
3982 * When visual area is more than one line: extend it.
3983 */
3984 if (VIsual_active && start_lnum != VIsual.lnum)
3985 {
3986extend:
3987 if (start_lnum < VIsual.lnum)
3988 dir = BACKWARD;
3989 else
3990 dir = FORWARD;
3991 for (i = count; --i >= 0; )
3992 {
3993 if (start_lnum ==
3994 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3995 {
3996 retval = FAIL;
3997 break;
3998 }
3999
4000 prev_start_is_white = -1;
4001 for (t = 0; t < 2; ++t)
4002 {
4003 start_lnum += dir;
4004 start_is_white = linewhite(start_lnum);
4005 if (prev_start_is_white == start_is_white)
4006 {
4007 start_lnum -= dir;
4008 break;
4009 }
4010 for (;;)
4011 {
4012 if (start_lnum == (dir == BACKWARD
4013 ? 1 : curbuf->b_ml.ml_line_count))
4014 break;
4015 if (start_is_white != linewhite(start_lnum + dir)
4016 || (!start_is_white
4017 && startPS(start_lnum + (dir > 0
4018 ? 1 : 0), 0, 0)))
4019 break;
4020 start_lnum += dir;
4021 }
4022 if (!include)
4023 break;
4024 if (start_lnum == (dir == BACKWARD
4025 ? 1 : curbuf->b_ml.ml_line_count))
4026 break;
4027 prev_start_is_white = start_is_white;
4028 }
4029 }
4030 curwin->w_cursor.lnum = start_lnum;
4031 curwin->w_cursor.col = 0;
4032 return retval;
4033 }
4034#endif
4035
4036 /*
4037 * First move back to the start_lnum of the paragraph or white lines
4038 */
4039 white_in_front = linewhite(start_lnum);
4040 while (start_lnum > 1)
4041 {
4042 if (white_in_front) /* stop at first white line */
4043 {
4044 if (!linewhite(start_lnum - 1))
4045 break;
4046 }
4047 else /* stop at first non-white line of start of paragraph */
4048 {
4049 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4050 break;
4051 }
4052 --start_lnum;
4053 }
4054
4055 /*
4056 * Move past the end of any white lines.
4057 */
4058 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004059 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4060 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061
4062 --end_lnum;
4063 i = count;
4064 if (!include && white_in_front)
4065 --i;
4066 while (i--)
4067 {
4068 if (end_lnum == curbuf->b_ml.ml_line_count)
4069 return FAIL;
4070
4071 if (!include)
4072 do_white = linewhite(end_lnum + 1);
4073
4074 if (include || !do_white)
4075 {
4076 ++end_lnum;
4077 /*
4078 * skip to end of paragraph
4079 */
4080 while (end_lnum < curbuf->b_ml.ml_line_count
4081 && !linewhite(end_lnum + 1)
4082 && !startPS(end_lnum + 1, 0, 0))
4083 ++end_lnum;
4084 }
4085
4086 if (i == 0 && white_in_front && include)
4087 break;
4088
4089 /*
4090 * skip to end of white lines after paragraph
4091 */
4092 if (include || do_white)
4093 while (end_lnum < curbuf->b_ml.ml_line_count
4094 && linewhite(end_lnum + 1))
4095 ++end_lnum;
4096 }
4097
4098 /*
4099 * If there are no empty lines at the end, try to find some empty lines at
4100 * the start (unless that has been done already).
4101 */
4102 if (!white_in_front && !linewhite(end_lnum) && include)
4103 while (start_lnum > 1 && linewhite(start_lnum - 1))
4104 --start_lnum;
4105
4106#ifdef FEAT_VISUAL
4107 if (VIsual_active)
4108 {
4109 /* Problem: when doing "Vipipip" nothing happens in a single white
4110 * line, we get stuck there. Trap this here. */
4111 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4112 goto extend;
4113 VIsual.lnum = start_lnum;
4114 VIsual_mode = 'V';
4115 redraw_curbuf_later(INVERTED); /* update the inversion */
4116 showmode();
4117 }
4118 else
4119#endif
4120 {
4121 oap->start.lnum = start_lnum;
4122 oap->start.col = 0;
4123 oap->motion_type = MLINE;
4124 }
4125 curwin->w_cursor.lnum = end_lnum;
4126 curwin->w_cursor.col = 0;
4127
4128 return OK;
4129}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004130
4131static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4132static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4133
4134/*
4135 * Search quote char from string line[col].
4136 * Quote character escaped by one of the characters in "escape" is not counted
4137 * as a quote.
4138 * Returns column number of "quotechar" or -1 when not found.
4139 */
4140 static int
4141find_next_quote(line, col, quotechar, escape)
4142 char_u *line;
4143 int col;
4144 int quotechar;
4145 char_u *escape; /* escape characters, can be NULL */
4146{
4147 int c;
4148
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004149 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004150 {
4151 c = line[col];
4152 if (c == NUL)
4153 return -1;
4154 else if (escape != NULL && vim_strchr(escape, c))
4155 ++col;
4156 else if (c == quotechar)
4157 break;
4158#ifdef FEAT_MBYTE
4159 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004160 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004161 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004163 ++col;
4164 }
4165 return col;
4166}
4167
4168/*
4169 * Search backwards in "line" from column "col_start" to find "quotechar".
4170 * Quote character escaped by one of the characters in "escape" is not counted
4171 * as a quote.
4172 * Return the found column or zero.
4173 */
4174 static int
4175find_prev_quote(line, col_start, quotechar, escape)
4176 char_u *line;
4177 int col_start;
4178 int quotechar;
4179 char_u *escape; /* escape characters, can be NULL */
4180{
4181 int n;
4182
4183 while (col_start > 0)
4184 {
4185 --col_start;
4186#ifdef FEAT_MBYTE
4187 col_start -= (*mb_head_off)(line, line + col_start);
4188#endif
4189 n = 0;
4190 if (escape != NULL)
4191 while (col_start - n > 0 && vim_strchr(escape,
4192 line[col_start - n - 1]) != NULL)
4193 ++n;
4194 if (n & 1)
4195 col_start -= n; /* uneven number of escape chars, skip it */
4196 else if (line[col_start] == quotechar)
4197 break;
4198 }
4199 return col_start;
4200}
4201
4202/*
4203 * Find quote under the cursor, cursor at end.
4204 * Returns TRUE if found, else FALSE.
4205 */
4206 int
4207current_quote(oap, count, include, quotechar)
4208 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004209 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004210 int include; /* TRUE == include quote char */
4211 int quotechar; /* Quote character */
4212{
4213 char_u *line = ml_get_curline();
4214 int col_end;
4215 int col_start = curwin->w_cursor.col;
4216 int inclusive = FALSE;
4217#ifdef FEAT_VISUAL
4218 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
4301#endif
4302
4303 if (line[col_start] == quotechar
4304#ifdef FEAT_VISUAL
4305 || !vis_empty
4306#endif
4307 )
4308 {
4309 int first_col = col_start;
4310
4311#ifdef FEAT_VISUAL
4312 if (!vis_empty)
4313 {
4314 if (vis_bef_curs)
4315 first_col = find_next_quote(line, col_start, quotechar, NULL);
4316 else
4317 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4318 }
4319#endif
4320 /* The cursor is on a quote, we don't know if it's the opening or
4321 * closing quote. Search from the start of the line to find out.
4322 * Also do this when there is a Visual area, a' may leave the cursor
4323 * in between two strings. */
4324 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004325 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004326 {
4327 /* Find open quote character. */
4328 col_start = find_next_quote(line, col_start, quotechar, NULL);
4329 if (col_start < 0 || col_start > first_col)
4330 return FALSE;
4331 /* Find close quote character. */
4332 col_end = find_next_quote(line, col_start + 1, quotechar,
4333 curbuf->b_p_qe);
4334 if (col_end < 0)
4335 return FALSE;
4336 /* If is cursor between start and end quote character, it is
4337 * target text object. */
4338 if (col_start <= first_col && first_col <= col_end)
4339 break;
4340 col_start = col_end + 1;
4341 }
4342 }
4343 else
4344 {
4345 /* Search backward for a starting quote. */
4346 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4347 if (line[col_start] != quotechar)
4348 {
4349 /* No quote before the cursor, look after the cursor. */
4350 col_start = find_next_quote(line, col_start, quotechar, NULL);
4351 if (col_start < 0)
4352 return FALSE;
4353 }
4354
4355 /* Find close quote character. */
4356 col_end = find_next_quote(line, col_start + 1, quotechar,
4357 curbuf->b_p_qe);
4358 if (col_end < 0)
4359 return FALSE;
4360 }
4361
4362 /* When "include" is TRUE, include spaces after closing quote or before
4363 * the starting quote. */
4364 if (include)
4365 {
4366 if (vim_iswhite(line[col_end + 1]))
4367 while (vim_iswhite(line[col_end + 1]))
4368 ++col_end;
4369 else
4370 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4371 --col_start;
4372 }
4373
Bram Moolenaarab194812005-09-14 21:40:12 +00004374 /* Set start position. After vi" another i" must include the ".
4375 * For v2i" include the quotes. */
4376 if (!include && count < 2
4377#ifdef FEAT_VISUAL
4378 && (vis_empty || !inside_quotes)
4379#endif
4380 )
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004381 ++col_start;
4382 curwin->w_cursor.col = col_start;
4383#ifdef FEAT_VISUAL
4384 if (VIsual_active)
4385 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004386 /* Set the start of the Visual area when the Visual area was empty, we
4387 * were just inside quotes or the Visual area didn't start at a quote
4388 * and didn't include a quote.
4389 */
4390 if (vis_empty
4391 || (vis_bef_curs
4392 && !selected_quote
4393 && (inside_quotes
4394 || (line[VIsual.col] != quotechar
4395 && (VIsual.col == 0
4396 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004397 {
4398 VIsual = curwin->w_cursor;
4399 redraw_curbuf_later(INVERTED);
4400 }
4401 }
4402 else
4403#endif
4404 {
4405 oap->start = curwin->w_cursor;
4406 oap->motion_type = MCHAR;
4407 }
4408
4409 /* Set end position. */
4410 curwin->w_cursor.col = col_end;
Bram Moolenaarab194812005-09-14 21:40:12 +00004411 if ((include || count > 1
4412#ifdef FEAT_VISUAL
4413 /* After vi" another i" must include the ". */
4414 || (!vis_empty && inside_quotes)
4415#endif
4416 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004417 inclusive = TRUE;
4418#ifdef FEAT_VISUAL
4419 if (VIsual_active)
4420 {
4421 if (vis_empty || vis_bef_curs)
4422 {
4423 /* decrement cursor when 'selection' is not exclusive */
4424 if (*p_sel != 'e')
4425 dec_cursor();
4426 }
4427 else
4428 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004429 /* Cursor is at start of Visual area. Set the end of the Visual
4430 * area when it was just inside quotes or it didn't end at a
4431 * quote. */
4432 if (inside_quotes
4433 || (!selected_quote
4434 && line[VIsual.col] != quotechar
4435 && (line[VIsual.col] == NUL
4436 || line[VIsual.col + 1] != quotechar)))
4437 {
4438 dec_cursor();
4439 VIsual = curwin->w_cursor;
4440 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004441 curwin->w_cursor.col = col_start;
4442 }
4443 if (VIsual_mode == 'V')
4444 {
4445 VIsual_mode = 'v';
4446 redraw_cmdline = TRUE; /* show mode later */
4447 }
4448 }
4449 else
4450#endif
4451 {
4452 /* Set inclusive and other oap's flags. */
4453 oap->inclusive = inclusive;
4454 }
4455
4456 return OK;
4457}
4458
4459#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4462 || defined(PROTO)
4463/*
4464 * return TRUE if line 'lnum' is empty or has white chars only.
4465 */
4466 int
4467linewhite(lnum)
4468 linenr_T lnum;
4469{
4470 char_u *p;
4471
4472 p = skipwhite(ml_get(lnum));
4473 return (*p == NUL);
4474}
4475#endif
4476
4477#if defined(FEAT_FIND_ID) || defined(PROTO)
4478/*
4479 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 */
4482/*ARGSUSED*/
4483 void
4484find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4485 type, count, action, start_lnum, end_lnum)
4486 char_u *ptr; /* pointer to search pattern */
4487 int dir; /* direction of expansion */
4488 int len; /* length of search pattern */
4489 int whole; /* match whole words only */
4490 int skip_comments; /* don't match inside comments */
4491 int type; /* Type of search; are we looking for a type?
4492 a macro? */
4493 long count;
4494 int action; /* What to do when we find it */
4495 linenr_T start_lnum; /* first line to start searching */
4496 linenr_T end_lnum; /* last line for searching */
4497{
4498 SearchedFile *files; /* Stack of included files */
4499 SearchedFile *bigger; /* When we need more space */
4500 int max_path_depth = 50;
4501 long match_count = 1;
4502
4503 char_u *pat;
4504 char_u *new_fname;
4505 char_u *curr_fname = curbuf->b_fname;
4506 char_u *prev_fname = NULL;
4507 linenr_T lnum;
4508 int depth;
4509 int depth_displayed; /* For type==CHECK_PATH */
4510 int old_files;
4511 int already_searched;
4512 char_u *file_line;
4513 char_u *line;
4514 char_u *p;
4515 char_u save_char;
4516 int define_matched;
4517 regmatch_T regmatch;
4518 regmatch_T incl_regmatch;
4519 regmatch_T def_regmatch;
4520 int matched = FALSE;
4521 int did_show = FALSE;
4522 int found = FALSE;
4523 int i;
4524 char_u *already = NULL;
4525 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004526 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#ifdef RISCOS
4528 int previous_munging = __riscosify_control;
4529#endif
4530#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4531 win_T *curwin_save = NULL;
4532#endif
4533
4534 regmatch.regprog = NULL;
4535 incl_regmatch.regprog = NULL;
4536 def_regmatch.regprog = NULL;
4537
4538 file_line = alloc(LSIZE);
4539 if (file_line == NULL)
4540 return;
4541
4542#ifdef RISCOS
4543 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004544 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545#endif
4546
4547 if (type != CHECK_PATH && type != FIND_DEFINE
4548#ifdef FEAT_INS_EXPAND
4549 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4550 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#endif
4553 )
4554 {
4555 pat = alloc(len + 5);
4556 if (pat == NULL)
4557 goto fpip_end;
4558 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4559 /* ignore case according to p_ic, p_scs and pat */
4560 regmatch.rm_ic = ignorecase(pat);
4561 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4562 vim_free(pat);
4563 if (regmatch.regprog == NULL)
4564 goto fpip_end;
4565 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004566 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4567 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004569 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 if (incl_regmatch.regprog == NULL)
4571 goto fpip_end;
4572 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4573 }
4574 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4575 {
4576 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4577 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4578 if (def_regmatch.regprog == NULL)
4579 goto fpip_end;
4580 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4581 }
4582 files = (SearchedFile *)lalloc_clear((long_u)
4583 (max_path_depth * sizeof(SearchedFile)), TRUE);
4584 if (files == NULL)
4585 goto fpip_end;
4586 old_files = max_path_depth;
4587 depth = depth_displayed = -1;
4588
4589 lnum = start_lnum;
4590 if (end_lnum > curbuf->b_ml.ml_line_count)
4591 end_lnum = curbuf->b_ml.ml_line_count;
4592 if (lnum > end_lnum) /* do at least one line */
4593 lnum = end_lnum;
4594 line = ml_get(lnum);
4595
4596 for (;;)
4597 {
4598 if (incl_regmatch.regprog != NULL
4599 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4600 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004601 char_u *p_fname = (curr_fname == curbuf->b_fname)
4602 ? curbuf->b_ffname : curr_fname;
4603
4604 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4605 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4606 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004607 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004608 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4609 else
4610 /* Use text after match with 'include'. */
4611 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004612 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 already_searched = FALSE;
4614 if (new_fname != NULL)
4615 {
4616 /* Check whether we have already searched in this file */
4617 for (i = 0;; i++)
4618 {
4619 if (i == depth + 1)
4620 i = old_files;
4621 if (i == max_path_depth)
4622 break;
4623 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4624 {
4625 if (type != CHECK_PATH &&
4626 action == ACTION_SHOW_ALL && files[i].matched)
4627 {
4628 msg_putchar('\n'); /* cursor below last one */
4629 if (!got_int) /* don't display if 'q'
4630 typed at "--more--"
4631 mesage */
4632 {
4633 msg_home_replace_hl(new_fname);
4634 MSG_PUTS(_(" (includes previously listed match)"));
4635 prev_fname = NULL;
4636 }
4637 }
4638 vim_free(new_fname);
4639 new_fname = NULL;
4640 already_searched = TRUE;
4641 break;
4642 }
4643 }
4644 }
4645
4646 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4647 || (new_fname == NULL && !already_searched)))
4648 {
4649 if (did_show)
4650 msg_putchar('\n'); /* cursor below last one */
4651 else
4652 {
4653 gotocmdline(TRUE); /* cursor at status line */
4654 MSG_PUTS_TITLE(_("--- Included files "));
4655 if (action != ACTION_SHOW_ALL)
4656 MSG_PUTS_TITLE(_("not found "));
4657 MSG_PUTS_TITLE(_("in path ---\n"));
4658 }
4659 did_show = TRUE;
4660 while (depth_displayed < depth && !got_int)
4661 {
4662 ++depth_displayed;
4663 for (i = 0; i < depth_displayed; i++)
4664 MSG_PUTS(" ");
4665 msg_home_replace(files[depth_displayed].name);
4666 MSG_PUTS(" -->\n");
4667 }
4668 if (!got_int) /* don't display if 'q' typed
4669 for "--more--" message */
4670 {
4671 for (i = 0; i <= depth_displayed; i++)
4672 MSG_PUTS(" ");
4673 if (new_fname != NULL)
4674 {
4675 /* using "new_fname" is more reliable, e.g., when
4676 * 'includeexpr' is set. */
4677 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4678 }
4679 else
4680 {
4681 /*
4682 * Isolate the file name.
4683 * Include the surrounding "" or <> if present.
4684 */
4685 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4686 ;
4687 for (i = 0; vim_isfilec(p[i]); i++)
4688 ;
4689 if (i == 0)
4690 {
4691 /* Nothing found, use the rest of the line. */
4692 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004693 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 else
4696 {
4697 if (p[-1] == '"' || p[-1] == '<')
4698 {
4699 --p;
4700 ++i;
4701 }
4702 if (p[i] == '"' || p[i] == '>')
4703 ++i;
4704 }
4705 save_char = p[i];
4706 p[i] = NUL;
4707 msg_outtrans_attr(p, hl_attr(HLF_D));
4708 p[i] = save_char;
4709 }
4710
4711 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4712 {
4713 if (already_searched)
4714 MSG_PUTS(_(" (Already listed)"));
4715 else
4716 MSG_PUTS(_(" NOT FOUND"));
4717 }
4718 }
4719 out_flush(); /* output each line directly */
4720 }
4721
4722 if (new_fname != NULL)
4723 {
4724 /* Push the new file onto the file stack */
4725 if (depth + 1 == old_files)
4726 {
4727 bigger = (SearchedFile *)lalloc((long_u)(
4728 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4729 if (bigger != NULL)
4730 {
4731 for (i = 0; i <= depth; i++)
4732 bigger[i] = files[i];
4733 for (i = depth + 1; i < old_files + max_path_depth; i++)
4734 {
4735 bigger[i].fp = NULL;
4736 bigger[i].name = NULL;
4737 bigger[i].lnum = 0;
4738 bigger[i].matched = FALSE;
4739 }
4740 for (i = old_files; i < max_path_depth; i++)
4741 bigger[i + max_path_depth] = files[i];
4742 old_files += max_path_depth;
4743 max_path_depth *= 2;
4744 vim_free(files);
4745 files = bigger;
4746 }
4747 }
4748 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4749 == NULL)
4750 vim_free(new_fname);
4751 else
4752 {
4753 if (++depth == old_files)
4754 {
4755 /*
4756 * lalloc() for 'bigger' must have failed above. We
4757 * will forget one of our already visited files now.
4758 */
4759 vim_free(files[old_files].name);
4760 ++old_files;
4761 }
4762 files[depth].name = curr_fname = new_fname;
4763 files[depth].lnum = 0;
4764 files[depth].matched = FALSE;
4765#ifdef FEAT_INS_EXPAND
4766 if (action == ACTION_EXPAND)
4767 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004768 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00004769 vim_snprintf((char*)IObuff, IOSIZE,
4770 _("Scanning included file: %s"),
4771 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4773 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004774 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004776 if (p_verbose >= 5)
4777 {
4778 verbose_enter();
4779 smsg((char_u *)_("Searching included file %s"),
4780 (char *)new_fname);
4781 verbose_leave();
4782 }
4783
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
4785 }
4786 }
4787 else
4788 {
4789 /*
4790 * Check if the line is a define (type == FIND_DEFINE)
4791 */
4792 p = line;
4793search_line:
4794 define_matched = FALSE;
4795 if (def_regmatch.regprog != NULL
4796 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4797 {
4798 /*
4799 * Pattern must be first identifier after 'define', so skip
4800 * to that position before checking for match of pattern. Also
4801 * don't let it match beyond the end of this identifier.
4802 */
4803 p = def_regmatch.endp[0];
4804 while (*p && !vim_iswordc(*p))
4805 p++;
4806 define_matched = TRUE;
4807 }
4808
4809 /*
4810 * Look for a match. Don't do this if we are looking for a
4811 * define and this line didn't match define_prog above.
4812 */
4813 if (def_regmatch.regprog == NULL || define_matched)
4814 {
4815 if (define_matched
4816#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818#endif
4819 )
4820 {
4821 /* compare the first "len" chars from "ptr" */
4822 startp = skipwhite(p);
4823 if (p_ic)
4824 matched = !MB_STRNICMP(startp, ptr, len);
4825 else
4826 matched = !STRNCMP(startp, ptr, len);
4827 if (matched && define_matched && whole
4828 && vim_iswordc(startp[len]))
4829 matched = FALSE;
4830 }
4831 else if (regmatch.regprog != NULL
4832 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4833 {
4834 matched = TRUE;
4835 startp = regmatch.startp[0];
4836 /*
4837 * Check if the line is not a comment line (unless we are
4838 * looking for a define). A line starting with "# define"
4839 * is not considered to be a comment line.
4840 */
4841 if (!define_matched && skip_comments)
4842 {
4843#ifdef FEAT_COMMENTS
4844 if ((*line != '#' ||
4845 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4846 && get_leader_len(line, NULL, FALSE))
4847 matched = FALSE;
4848
4849 /*
4850 * Also check for a "/ *" or "/ /" before the match.
4851 * Skips lines like "int backwards; / * normal index
4852 * * /" when looking for "normal".
4853 * Note: Doesn't skip "/ *" in comments.
4854 */
4855 p = skipwhite(line);
4856 if (matched
4857 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4858#endif
4859 for (p = line; *p && p < startp; ++p)
4860 {
4861 if (matched
4862 && p[0] == '/'
4863 && (p[1] == '*' || p[1] == '/'))
4864 {
4865 matched = FALSE;
4866 /* After "//" all text is comment */
4867 if (p[1] == '/')
4868 break;
4869 ++p;
4870 }
4871 else if (!matched && p[0] == '*' && p[1] == '/')
4872 {
4873 /* Can find match after "* /". */
4874 matched = TRUE;
4875 ++p;
4876 }
4877 }
4878 }
4879 }
4880 }
4881 }
4882 if (matched)
4883 {
4884#ifdef FEAT_INS_EXPAND
4885 if (action == ACTION_EXPAND)
4886 {
4887 int reuse = 0;
4888 int add_r;
4889 char_u *aux;
4890
4891 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4892 break;
4893 found = TRUE;
4894 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004895 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004897 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (vim_iswordp(p))
4899 goto exit_matched;
4900 p = find_word_start(p);
4901 }
4902 p = find_word_end(p);
4903 i = (int)(p - aux);
4904
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004905 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00004909
4910 /* Get the next line: when "depth" < 0 from the current
4911 * buffer, otherwise from the included file. Jump to
4912 * exit_matched when past the last line. */
4913 if (depth < 0)
4914 {
4915 if (lnum >= end_lnum)
4916 goto exit_matched;
4917 line = ml_get(++lnum);
4918 }
4919 else if (vim_fgets(line = file_line,
4920 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 goto exit_matched;
4922
4923 /* we read a line, set "already" to check this "line" later
4924 * if depth >= 0 we'll increase files[depth].lnum far
4925 * bellow -- Acevedo */
4926 already = aux = p = skipwhite(line);
4927 p = find_word_start(p);
4928 p = find_word_end(p);
4929 if (p > aux)
4930 {
4931 if (*aux != ')' && IObuff[i-1] != TAB)
4932 {
4933 if (IObuff[i-1] != ' ')
4934 IObuff[i++] = ' ';
4935 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4936 if (p_js
4937 && (IObuff[i-2] == '.'
4938 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4939 && (IObuff[i-2] == '?'
4940 || IObuff[i-2] == '!'))))
4941 IObuff[i++] = ' ';
4942 }
4943 /* copy as much as posible of the new word */
4944 if (p - aux >= IOSIZE - i)
4945 p = aux + IOSIZE - i - 1;
4946 STRNCPY(IObuff + i, aux, p - aux);
4947 i += (int)(p - aux);
4948 reuse |= CONT_S_IPOS;
4949 }
4950 IObuff[i] = NUL;
4951 aux = IObuff;
4952
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004953 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 goto exit_matched;
4955 }
4956
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004957 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4959 dir, reuse);
4960 if (add_r == OK)
4961 /* if dir was BACKWARD then honor it just once */
4962 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004963 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 break;
4965 }
4966 else
4967#endif
4968 if (action == ACTION_SHOW_ALL)
4969 {
4970 found = TRUE;
4971 if (!did_show)
4972 gotocmdline(TRUE); /* cursor at status line */
4973 if (curr_fname != prev_fname)
4974 {
4975 if (did_show)
4976 msg_putchar('\n'); /* cursor below last one */
4977 if (!got_int) /* don't display if 'q' typed
4978 at "--more--" mesage */
4979 msg_home_replace_hl(curr_fname);
4980 prev_fname = curr_fname;
4981 }
4982 did_show = TRUE;
4983 if (!got_int)
4984 show_pat_in_path(line, type, TRUE, action,
4985 (depth == -1) ? NULL : files[depth].fp,
4986 (depth == -1) ? &lnum : &files[depth].lnum,
4987 match_count++);
4988
4989 /* Set matched flag for this file and all the ones that
4990 * include it */
4991 for (i = 0; i <= depth; ++i)
4992 files[i].matched = TRUE;
4993 }
4994 else if (--count <= 0)
4995 {
4996 found = TRUE;
4997 if (depth == -1 && lnum == curwin->w_cursor.lnum
4998#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4999 && g_do_tagpreview == 0
5000#endif
5001 )
5002 EMSG(_("E387: Match is on current line"));
5003 else if (action == ACTION_SHOW)
5004 {
5005 show_pat_in_path(line, type, did_show, action,
5006 (depth == -1) ? NULL : files[depth].fp,
5007 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5008 did_show = TRUE;
5009 }
5010 else
5011 {
5012#ifdef FEAT_GUI
5013 need_mouse_correct = TRUE;
5014#endif
5015#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5016 /* ":psearch" uses the preview window */
5017 if (g_do_tagpreview != 0)
5018 {
5019 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00005020 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022#endif
5023 if (action == ACTION_SPLIT)
5024 {
5025#ifdef FEAT_WINDOWS
5026 if (win_split(0, 0) == FAIL)
5027#endif
5028 break;
5029#ifdef FEAT_SCROLLBIND
5030 curwin->w_p_scb = FALSE;
5031#endif
5032 }
5033 if (depth == -1)
5034 {
5035 /* match in current file */
5036#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5037 if (g_do_tagpreview != 0)
5038 {
5039 if (getfile(0, curwin_save->w_buffer->b_fname,
5040 NULL, TRUE, lnum, FALSE) > 0)
5041 break; /* failed to jump to file */
5042 }
5043 else
5044#endif
5045 setpcmark();
5046 curwin->w_cursor.lnum = lnum;
5047 }
5048 else
5049 {
5050 if (getfile(0, files[depth].name, NULL, TRUE,
5051 files[depth].lnum, FALSE) > 0)
5052 break; /* failed to jump to file */
5053 /* autocommands may have changed the lnum, we don't
5054 * want that here */
5055 curwin->w_cursor.lnum = files[depth].lnum;
5056 }
5057 }
5058 if (action != ACTION_SHOW)
5059 {
5060 curwin->w_cursor.col = (colnr_T) (startp - line);
5061 curwin->w_set_curswant = TRUE;
5062 }
5063
5064#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5065 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005066 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
5068 /* Return cursor to where we were */
5069 validate_cursor();
5070 redraw_later(VALID);
5071 win_enter(curwin_save, TRUE);
5072 }
5073#endif
5074 break;
5075 }
5076#ifdef FEAT_INS_EXPAND
5077exit_matched:
5078#endif
5079 matched = FALSE;
5080 /* look for other matches in the rest of the line if we
5081 * are not at the end of it already */
5082 if (def_regmatch.regprog == NULL
5083#ifdef FEAT_INS_EXPAND
5084 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005085 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086#endif
5087 && *(p = startp + 1))
5088 goto search_line;
5089 }
5090 line_breakcheck();
5091#ifdef FEAT_INS_EXPAND
5092 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00005093 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095#else
5096 if (got_int)
5097#endif
5098 break;
5099
5100 /*
5101 * Read the next line. When reading an included file and encountering
5102 * end-of-file, close the file and continue in the file that included
5103 * it.
5104 */
5105 while (depth >= 0 && !already
5106 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5107 {
5108 fclose(files[depth].fp);
5109 --old_files;
5110 files[old_files].name = files[depth].name;
5111 files[old_files].matched = files[depth].matched;
5112 --depth;
5113 curr_fname = (depth == -1) ? curbuf->b_fname
5114 : files[depth].name;
5115 if (depth < depth_displayed)
5116 depth_displayed = depth;
5117 }
5118 if (depth >= 0) /* we could read the line */
5119 files[depth].lnum++;
5120 else if (!already)
5121 {
5122 if (++lnum > end_lnum)
5123 break;
5124 line = ml_get(lnum);
5125 }
5126 already = NULL;
5127 }
5128 /* End of big for (;;) loop. */
5129
5130 /* Close any files that are still open. */
5131 for (i = 0; i <= depth; i++)
5132 {
5133 fclose(files[i].fp);
5134 vim_free(files[i].name);
5135 }
5136 for (i = old_files; i < max_path_depth; i++)
5137 vim_free(files[i].name);
5138 vim_free(files);
5139
5140 if (type == CHECK_PATH)
5141 {
5142 if (!did_show)
5143 {
5144 if (action != ACTION_SHOW_ALL)
5145 MSG(_("All included files were found"));
5146 else
5147 MSG(_("No included files"));
5148 }
5149 }
5150 else if (!found
5151#ifdef FEAT_INS_EXPAND
5152 && action != ACTION_EXPAND
5153#endif
5154 )
5155 {
5156#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158#else
5159 if (got_int)
5160#endif
5161 EMSG(_(e_interr));
5162 else if (type == FIND_DEFINE)
5163 EMSG(_("E388: Couldn't find definition"));
5164 else
5165 EMSG(_("E389: Couldn't find pattern"));
5166 }
5167 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5168 msg_end();
5169
5170fpip_end:
5171 vim_free(file_line);
5172 vim_free(regmatch.regprog);
5173 vim_free(incl_regmatch.regprog);
5174 vim_free(def_regmatch.regprog);
5175
5176#ifdef RISCOS
5177 /* Restore previous file munging state. */
5178 __riscosify_control = previous_munging;
5179#endif
5180}
5181
5182 static void
5183show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5184 char_u *line;
5185 int type;
5186 int did_show;
5187 int action;
5188 FILE *fp;
5189 linenr_T *lnum;
5190 long count;
5191{
5192 char_u *p;
5193
5194 if (did_show)
5195 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005196 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 gotocmdline(TRUE); /* cursor at status line */
5198 if (got_int) /* 'q' typed at "--more--" message */
5199 return;
5200 for (;;)
5201 {
5202 p = line + STRLEN(line) - 1;
5203 if (fp != NULL)
5204 {
5205 /* We used fgets(), so get rid of newline at end */
5206 if (p >= line && *p == '\n')
5207 --p;
5208 if (p >= line && *p == '\r')
5209 --p;
5210 *(p + 1) = NUL;
5211 }
5212 if (action == ACTION_SHOW_ALL)
5213 {
5214 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5215 msg_puts(IObuff);
5216 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5217 /* Highlight line numbers */
5218 msg_puts_attr(IObuff, hl_attr(HLF_N));
5219 MSG_PUTS(" ");
5220 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005221 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 out_flush(); /* show one line at a time */
5223
5224 /* Definition continues until line that doesn't end with '\' */
5225 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5226 break;
5227
5228 if (fp != NULL)
5229 {
5230 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5231 break;
5232 ++*lnum;
5233 }
5234 else
5235 {
5236 if (++*lnum > curbuf->b_ml.ml_line_count)
5237 break;
5238 line = ml_get(*lnum);
5239 }
5240 msg_putchar('\n');
5241 }
5242}
5243#endif
5244
5245#ifdef FEAT_VIMINFO
5246 int
5247read_viminfo_search_pattern(virp, force)
5248 vir_T *virp;
5249 int force;
5250{
5251 char_u *lp;
5252 int idx = -1;
5253 int magic = FALSE;
5254 int no_scs = FALSE;
5255 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005256 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 long off = 0;
5258 int setlast = FALSE;
5259#ifdef FEAT_SEARCH_EXTRA
5260 static int hlsearch_on = FALSE;
5261#endif
5262 char_u *val;
5263
5264 /*
5265 * Old line types:
5266 * "/pat", "&pat": search/subst. pat
5267 * "~/pat", "~&pat": last used search/subst. pat
5268 * New line types:
5269 * "~h", "~H": hlsearch highlighting off/on
5270 * "~<magic><smartcase><line><end><off><last><which>pat"
5271 * <magic>: 'm' off, 'M' on
5272 * <smartcase>: 's' off, 'S' on
5273 * <line>: 'L' line offset, 'l' char offset
5274 * <end>: 'E' from end, 'e' from start
5275 * <off>: decimal, offset
5276 * <last>: '~' last used pattern
5277 * <which>: '/' search pat, '&' subst. pat
5278 */
5279 lp = virp->vir_line;
5280 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5281 {
5282 if (lp[1] == 'M') /* magic on */
5283 magic = TRUE;
5284 if (lp[2] == 's')
5285 no_scs = TRUE;
5286 if (lp[3] == 'L')
5287 off_line = TRUE;
5288 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005289 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 lp += 5;
5291 off = getdigits(&lp);
5292 }
5293 if (lp[0] == '~') /* use this pattern for last-used pattern */
5294 {
5295 setlast = TRUE;
5296 lp++;
5297 }
5298 if (lp[0] == '/')
5299 idx = RE_SEARCH;
5300 else if (lp[0] == '&')
5301 idx = RE_SUBST;
5302#ifdef FEAT_SEARCH_EXTRA
5303 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5304 hlsearch_on = FALSE;
5305 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5306 hlsearch_on = TRUE;
5307#endif
5308 if (idx >= 0)
5309 {
5310 if (force || spats[idx].pat == NULL)
5311 {
5312 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5313 TRUE);
5314 if (val != NULL)
5315 {
5316 set_last_search_pat(val, idx, magic, setlast);
5317 vim_free(val);
5318 spats[idx].no_scs = no_scs;
5319 spats[idx].off.line = off_line;
5320 spats[idx].off.end = off_end;
5321 spats[idx].off.off = off;
5322#ifdef FEAT_SEARCH_EXTRA
5323 if (setlast)
5324 no_hlsearch = !hlsearch_on;
5325#endif
5326 }
5327 }
5328 }
5329 return viminfo_readline(virp);
5330}
5331
5332 void
5333write_viminfo_search_pattern(fp)
5334 FILE *fp;
5335{
5336 if (get_viminfo_parameter('/') != 0)
5337 {
5338#ifdef FEAT_SEARCH_EXTRA
5339 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5340 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5341#endif
5342 wvsp_one(fp, RE_SEARCH, "", '/');
5343 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5344 }
5345}
5346
5347 static void
5348wvsp_one(fp, idx, s, sc)
5349 FILE *fp; /* file to write to */
5350 int idx; /* spats[] index */
5351 char *s; /* search pat */
5352 int sc; /* dir char */
5353{
5354 if (spats[idx].pat != NULL)
5355 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005356 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 /* off.dir is not stored, it's reset to forward */
5358 fprintf(fp, "%c%c%c%c%ld%s%c",
5359 spats[idx].magic ? 'M' : 'm', /* magic */
5360 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5361 spats[idx].off.line ? 'L' : 'l', /* line offset */
5362 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5363 spats[idx].off.off, /* offset */
5364 last_idx == idx ? "~" : "", /* last used pat */
5365 sc);
5366 viminfo_writestring(fp, spats[idx].pat);
5367 }
5368}
5369#endif /* FEAT_VIMINFO */